-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolve.ts
More file actions
97 lines (88 loc) · 2.97 KB
/
resolve.ts
File metadata and controls
97 lines (88 loc) · 2.97 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
/**
* @file `resolveCdxgen()` — cdxgen resolution entry point. Tries each source in
* order:
*
* 1. VFS — smol binary's embedded cdxgen (if packed)
* 2. PATH — `cdxgen` on the system PATH
* 3. download — upstream SEA binary from the GitHub release (slim by default;
* pass `variant: 'full'` for the bun+deno-bundled flavor) Single source of
* truth: SEA binary only. No npm-package fallback — every fleet
* platform-arch is covered by the SEA matrix, and routing through npm
* would split the install surface in two for no benefit. Returns
* `undefined` if all of the enabled sources miss. Memoized per
* option-shape.
*/
import { cdxgenFromDownload } from './from-download'
import { cdxgenFromPath } from './from-path'
import { cdxgenFromVfs } from './from-vfs'
import type { BinaryDownloader } from '../from-download'
import type { HashSpec } from '../../integrity'
import type { CdxgenVariant } from './asset-names'
import type { ResolvedCdxgen } from './types'
import { MapCtor } from '../../primordials/map-set'
export interface ResolveCdxgenOptions {
downloadIfMissing?:
| {
version: string
platformArch: string
variant?: CdxgenVariant | undefined
integrity?: HashSpec | undefined
cacheDir?: string | undefined
downloader?: BinaryDownloader | undefined
}
| undefined
}
const resolutionCache = new MapCtor<
string,
Promise<ResolvedCdxgen | undefined>
>()
export function cacheKey(options: ResolveCdxgenOptions | undefined): string {
options = { __proto__: null, ...options } as typeof options
if (!options?.downloadIfMissing) {
return 'local-only'
}
const { cacheDir, integrity, platformArch, variant, version } =
options.downloadIfMissing
const integrityKey =
typeof integrity === 'string'
? integrity
: integrity
? `${integrity.type}:${integrity.value}`
: ''
return `dl:${version}:${platformArch}:${variant ?? 'slim'}:${integrityKey}:${cacheDir ?? ''}`
}
export async function doResolveCdxgen(
options?: ResolveCdxgenOptions | undefined,
): Promise<ResolvedCdxgen | undefined> {
options = { __proto__: null, ...options } as typeof options
const fromVfs = await cdxgenFromVfs()
/* c8 ignore start - smol Node binary only. */
if (fromVfs) {
return fromVfs
}
/* c8 ignore stop */
const fromPath = await cdxgenFromPath()
if (fromPath) {
return fromPath
}
if (options?.downloadIfMissing) {
return cdxgenFromDownload(options.downloadIfMissing)
}
return undefined
}
/* c8 ignore start - test-only escape hatch. */
export function resetCdxgenResolution(): void {
resolutionCache.clear()
}
/* c8 ignore stop */
export function resolveCdxgen(
options?: ResolveCdxgenOptions | undefined,
): Promise<ResolvedCdxgen | undefined> {
const key = cacheKey(options)
let cached = resolutionCache.get(key)
if (!cached) {
cached = doResolveCdxgen(options)
resolutionCache.set(key, cached)
}
return cached
}