-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket-btm.ts
More file actions
336 lines (306 loc) · 8.91 KB
/
socket-btm.ts
File metadata and controls
336 lines (306 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
* @file Socket-btm release download utilities.
*/
import { getArch, getOs } from '../constants/platform'
import type { Arch, Libc, Platform } from '../constants/platform'
import { getReleaseAssetUrl } from './github-asset-url'
import { getLatestRelease } from './github-listing'
import { downloadGitHubRelease } from './github-downloads'
import {
getBinaryAssetName,
getBinaryName,
getPlatformArch,
} from './socket-btm-binary-naming'
import { getNodeFs } from '../node/fs'
import type { AssetPattern, DownloadGitHubReleaseConfig } from './github-types'
import { ErrorCtor } from '../primordials/error'
export type { Arch, Libc, Platform }
export { getBinaryAssetName, getBinaryName, getPlatformArch }
/**
* Socket-btm GitHub repository configuration.
*/
export const SOCKET_BTM_REPO = {
owner: 'SocketDev',
repo: 'socket-btm',
} as const
/**
* Configuration for downloading socket-btm generic assets.
*/
export interface SocketBtmAssetConfig {
/**
* Asset name or pattern on GitHub.
*/
asset: string | AssetPattern
/**
* @internal Discriminator fields
*/
bin?: never | undefined
/**
* Working directory (defaults to process.cwd()).
*/
cwd?: string | undefined
/**
* Download destination directory. @default 'build/downloaded'
*/
downloadDir?: string | undefined
/**
* @internal Discriminator fields
*/
libc?: never | undefined
/**
* Output filename. @default resolved asset name.
*/
output?: string | undefined
/**
* Suppress log messages. @default false.
*/
quiet?: boolean | undefined
/**
* Remove macOS quarantine attribute after download. @default false.
*/
removeMacOSQuarantine?: boolean | undefined
/**
* Specific release tag to download.
*/
tag?: string | undefined
/**
* @internal Discriminator fields
*/
targetArch?: never | undefined
/**
* @internal Discriminator fields
*/
targetPlatform?: never | undefined
}
/**
* Configuration for downloading socket-btm binary releases.
*/
export interface SocketBtmBinaryConfig {
/**
* @internal Discriminator field
*/
asset?: never | undefined
/**
* Binary/executable name (without extension). @default tool.
*/
bin?: string | undefined
/**
* Working directory (defaults to process.cwd()).
*/
cwd?: string | undefined
/**
* Download destination directory. @default 'build/downloaded'
*/
downloadDir?: string | undefined
/**
* Linux libc variant. Auto-detected if not specified.
*/
libc?: Libc | undefined
/**
* Suppress log messages. @default false.
*/
quiet?: boolean | undefined
/**
* Remove macOS quarantine attribute after download. @default true.
*/
removeMacOSQuarantine?: boolean | undefined
/**
* Specific release tag to download.
*/
tag?: string | undefined
/**
* Target architecture (defaults to current arch).
*/
targetArch?: Arch | undefined
/**
* Target platform (defaults to current platform).
*/
targetPlatform?: Platform | undefined
}
/**
* Configuration for downloading socket-btm releases (binary or asset).
*/
export type SocketBtmReleaseConfig =
| SocketBtmBinaryConfig
| SocketBtmAssetConfig
/**
* Detect the libc variant (musl or glibc) on Linux systems. Returns undefined
* for non-Linux platforms.
*
* @example
* ;```typescript
* const libc = detectLibc()
* console.log(libc) // 'glibc', 'musl', or undefined
* ```
*
* @returns 'musl', 'glibc', or undefined (for non-Linux)
*/
export function detectLibc(): Libc | undefined {
// Non-linux early-return arm fires on macOS/Windows (the test
// platform); linux-body is c8-ignored separately.
/* c8 ignore next 3 */
if (getOs() !== 'linux') {
return undefined
}
/* c8 ignore start - Linux-only musl/glibc detection; tested on
Linux runners. macOS/Windows runners take the early-return above. */
try {
const fs = getNodeFs()
// Check for musl-specific dynamic linker.
// These files only exist on musl systems.
const muslPaths = [
'/lib/ld-musl-x86_64.so.1',
'/lib/ld-musl-aarch64.so.1',
'/usr/lib/ld-musl-x86_64.so.1',
'/usr/lib/ld-musl-aarch64.so.1',
]
for (let i = 0, { length } = muslPaths; i < length; i += 1) {
const path = muslPaths[i]!
if (fs.existsSync(path)) {
return 'musl'
}
}
// If no musl files found, assume glibc.
return 'glibc'
} catch {
// If detection fails, default to glibc (most common).
return 'glibc'
}
/* c8 ignore stop */
}
/**
* Download a release from socket-btm.
*
* @example
* ;```typescript
* const binPath = await downloadSocketBtmRelease('lief', {
* downloadDir: '/tmp/build/downloaded',
* })
* ```
*
* @param tool - Tool/package name for release matching (e.g., 'lief', 'curl')
* @param options - Download configuration.
*
* @returns Path to the downloaded file
*/
export async function downloadSocketBtmRelease(
tool: string,
options: SocketBtmReleaseConfig | undefined,
): Promise<string> {
// options-undefined fallback fires when caller omits options.
/* c8 ignore next */
const config = { __proto__: null, ...(options ?? {}) } as unknown as {
cwd?: string | undefined
downloadDir?: string | undefined
quiet?: boolean | undefined
tag?: string | undefined
}
const { cwd, downloadDir, quiet = false, tag } = config
// Auto-generate toolPrefix from tool name (follows socket-btm tag pattern: {tool}-{date}-{commit})
const toolPrefix = `${tool}-`
let downloadConfig: DownloadGitHubReleaseConfig
// Infer type from presence of 'asset' field
if (options && 'asset' in options) {
// Asset download
const assetConfig = {
__proto__: null,
...(options as SocketBtmAssetConfig),
} as SocketBtmAssetConfig
const { asset, output, removeMacOSQuarantine = false } = assetConfig
// Resolve asset pattern to actual asset name if needed.
let resolvedAsset: string
let resolvedTag = tag
// Check if asset is a string without wildcard (exact match).
const isExactMatch = typeof asset === 'string' && !asset.includes('*')
if (isExactMatch) {
// Exact asset name provided.
resolvedAsset = asset as string
} else {
// Pattern provided (wildcard string, object, or RegExp) - need to find matching asset.
if (tag) {
throw new ErrorCtor(
'Cannot use asset pattern with explicit tag. Either provide exact asset name or omit tag.',
)
}
// Find latest release with matching asset.
resolvedTag =
(await getLatestRelease(toolPrefix, SOCKET_BTM_REPO, {
assetPattern: asset,
})) ?? undefined
if (!resolvedTag) {
throw new ErrorCtor(
`No ${tool} release with matching asset pattern found`,
)
}
const assetUrl = await getReleaseAssetUrl(
resolvedTag,
asset,
SOCKET_BTM_REPO,
)
// No-asset throw and split-pop-fallback fire only on edge cases.
/* c8 ignore start */
if (!assetUrl) {
throw new ErrorCtor(`No matching asset found in release ${resolvedTag}`)
}
resolvedAsset = assetUrl.split('/').pop() || asset.toString()
/* c8 ignore stop */
}
// output-undefined fallback fires when caller omits output.
/* c8 ignore next */
const outputName = output || resolvedAsset
// For non-binary assets, use a simple 'assets' directory instead of platform-arch
const platformArch = 'assets'
downloadConfig = {
owner: SOCKET_BTM_REPO.owner,
repo: SOCKET_BTM_REPO.repo,
...(cwd !== undefined && { cwd }),
...(downloadDir !== undefined && { downloadDir }),
toolName: tool,
platformArch,
binaryName: outputName,
assetName: resolvedAsset,
toolPrefix,
...(resolvedTag !== undefined && { tag: resolvedTag }),
quiet,
removeMacOSQuarantine,
}
} else {
// Binary download
const binaryConfig = {
__proto__: null,
...(options as SocketBtmBinaryConfig | undefined),
} as SocketBtmBinaryConfig
const {
bin,
libc = detectLibc(),
removeMacOSQuarantine = true,
targetArch = getArch(),
targetPlatform = getOs(),
} = binaryConfig
// Default bin to tool if not provided (like brew/cargo)
const baseName = bin || tool
const assetName = getBinaryAssetName(
baseName,
targetPlatform,
targetArch,
libc,
)
const platformArch = getPlatformArch(targetPlatform, targetArch, libc)
const binaryName = getBinaryName(baseName, targetPlatform)
downloadConfig = {
owner: SOCKET_BTM_REPO.owner,
repo: SOCKET_BTM_REPO.repo,
...(cwd !== undefined && { cwd }),
...(downloadDir !== undefined && { downloadDir }),
toolName: tool,
platformArch,
binaryName,
assetName,
toolPrefix,
...(tag !== undefined && { tag }),
quiet,
removeMacOSQuarantine,
}
}
return await downloadGitHubRelease(downloadConfig)
}