-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolve.ts
More file actions
74 lines (65 loc) · 1.95 KB
/
resolve.ts
File metadata and controls
74 lines (65 loc) · 1.95 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
/**
* @file `resolveSynp()` — synp resolution entry point. Tries each source in
* order:
*
* 1. VFS — smol binary's embedded synp (if packed)
* 2. PATH — `synp` on the system PATH
* 3. download — pinned npm package via dlx (only when `downloadIfMissing` is
* passed) Returns `undefined` if all of the enabled sources miss. Memoized
* per option-shape.
*/
import { synpFromDownload } from './from-download'
import { synpFromPath } from './from-path'
import { synpFromVfs } from './from-vfs'
import type { ResolvedSynp } from './types'
import { MapCtor } from '../../primordials/map-set'
export interface ResolveSynpOptions {
downloadIfMissing?:
| {
version: string
integrity?: string | undefined
}
| undefined
}
const resolutionCache = new MapCtor<string, Promise<ResolvedSynp | undefined>>()
export function cacheKey(opts: ResolveSynpOptions | undefined): string {
if (!opts?.downloadIfMissing) {
return 'local-only'
}
const { integrity, version } = opts.downloadIfMissing
return `dl:${version}:${integrity ?? ''}`
}
export async function doResolveSynp(
opts?: ResolveSynpOptions | undefined,
): Promise<ResolvedSynp | undefined> {
const fromVfs = await synpFromVfs()
/* c8 ignore start - smol Node binary only. */
if (fromVfs) {
return fromVfs
}
/* c8 ignore stop */
const fromPath = await synpFromPath()
if (fromPath) {
return fromPath
}
if (opts?.downloadIfMissing) {
return synpFromDownload(opts.downloadIfMissing)
}
return undefined
}
/* c8 ignore start - test-only escape hatch. */
export function resetSynpResolution(): void {
resolutionCache.clear()
}
/* c8 ignore stop */
export function resolveSynp(
opts?: ResolveSynpOptions | undefined,
): Promise<ResolvedSynp | undefined> {
const key = cacheKey(opts)
let cached = resolutionCache.get(key)
if (!cached) {
cached = doResolveSynp(opts)
resolutionCache.set(key, cached)
}
return cached
}