-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolve.ts
More file actions
107 lines (98 loc) · 3.47 KB
/
resolve.ts
File metadata and controls
107 lines (98 loc) · 3.47 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
/**
* @file `resolveJre()` — the JRE resolution entry point. Tries each source in
* order:
*
* 1. VFS — smol binary's embedded JRE (zero network, fast)
* 2. JAVA_HOME — user-pinned via env var
* 3. PATH — `java` (or `java.exe`) on the system PATH
* 4. download — Adoptium fetch + extract (only when `downloadIfMissing` is
* passed) Returns `undefined` if all of the enabled sources miss. The
* caller decides what to do then — typically prompt the user, surface an
* install instruction, or fail with an actionable error. Memoized per
* option-shape: calls with identical options return the same cached
* promise. Calling without `downloadIfMissing` and then with
* `downloadIfMissing` produces two distinct cache entries so the second
* call can fall through to the download tier even after the first call's
* "all local tiers missed → undefined" memoized. Test-only escape hatch:
* `resetJreResolution()` clears the cache so tests can exercise the
* resolver fresh.
*/
import { jreFromDownload } from './from-download'
import { jreFromJavaHome } from './from-java-home'
import { jreFromPath } from './from-path'
import { jreFromVfs } from './from-vfs'
import type { BinaryDownloader } from '../from-download'
import type { HashSpec } from '../../integrity'
import type { ResolvedJre } from './types'
import { MapCtor } from '../../primordials/map-set'
export interface ResolveJreOptions {
/**
* When set, the resolver falls through to an Adoptium download after the
* local-discovery tiers miss. Omit to keep the resolver read-only (no
* network).
*/
downloadIfMissing?:
| {
version: number
platformArch: string
integrity?: HashSpec | undefined
cacheDir?: string | undefined
downloader?: BinaryDownloader | undefined
}
| undefined
}
const resolutionCache = new MapCtor<string, Promise<ResolvedJre | undefined>>()
export function cacheKey(options: ResolveJreOptions | undefined): string {
options = { __proto__: null, ...options } as typeof options
if (!options?.downloadIfMissing) {
return 'local-only'
}
const { cacheDir, integrity, platformArch, version } =
options.downloadIfMissing
const integrityKey =
typeof integrity === 'string'
? integrity
: integrity
? `${integrity.type}:${integrity.value}`
: ''
return `dl:${version}:${platformArch}:${integrityKey}:${cacheDir ?? ''}`
}
export async function doResolveJre(
options?: ResolveJreOptions | undefined,
): Promise<ResolvedJre | undefined> {
options = { __proto__: null, ...options } as typeof options
const fromVfs = await jreFromVfs()
/* c8 ignore start - smol Node binary only. */
if (fromVfs) {
return fromVfs
}
/* c8 ignore stop */
const fromJavaHome = jreFromJavaHome()
if (fromJavaHome) {
return fromJavaHome
}
const fromPath = await jreFromPath()
if (fromPath) {
return fromPath
}
if (options?.downloadIfMissing) {
return jreFromDownload(options.downloadIfMissing)
}
return undefined
}
/* c8 ignore start - test-only escape hatch. */
export function resetJreResolution(): void {
resolutionCache.clear()
}
/* c8 ignore stop */
export function resolveJre(
options?: ResolveJreOptions | undefined,
): Promise<ResolvedJre | undefined> {
const key = cacheKey(options)
let cached = resolutionCache.get(key)
if (!cached) {
cached = doResolveJre(options)
resolutionCache.set(key, cached)
}
return cached
}