@scelar/nodepod 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__sw__.js +642 -642
- package/dist/__tests__/bench/integration.bench.d.ts +1 -0
- package/dist/__tests__/bench/memory-volume.bench.d.ts +1 -0
- package/dist/__tests__/bench/polyfills.bench.d.ts +1 -0
- package/dist/__tests__/bench/script-engine.bench.d.ts +1 -0
- package/dist/__tests__/bench/shell.bench.d.ts +1 -0
- package/dist/__tests__/bench/syntax-transforms.bench.d.ts +1 -0
- package/dist/__tests__/bench/version-resolver.bench.d.ts +1 -0
- package/dist/__tests__/buffer.test.d.ts +1 -0
- package/dist/__tests__/byte-encoding.test.d.ts +1 -0
- package/dist/__tests__/digest.test.d.ts +1 -0
- package/dist/__tests__/events.test.d.ts +1 -0
- package/dist/__tests__/memory-volume.test.d.ts +1 -0
- package/dist/__tests__/path.test.d.ts +1 -0
- package/dist/__tests__/process.test.d.ts +1 -0
- package/dist/__tests__/script-engine.test.d.ts +1 -0
- package/dist/__tests__/shell-builtins.test.d.ts +1 -0
- package/dist/__tests__/shell-interpreter.test.d.ts +1 -0
- package/dist/__tests__/shell-parser.test.d.ts +1 -0
- package/dist/__tests__/stream.test.d.ts +1 -0
- package/dist/__tests__/syntax-transforms.test.d.ts +1 -0
- package/dist/__tests__/version-resolver.test.d.ts +1 -0
- package/dist/{child_process-Dopvyd-E.js → child_process-53fMkug_.js} +4 -4
- package/dist/child_process-53fMkug_.js.map +1 -0
- package/dist/{child_process-B38qoN6R.cjs → child_process-lxSKECHq.cjs} +5 -5
- package/dist/child_process-lxSKECHq.cjs.map +1 -0
- package/dist/{index--Qr8LVpQ.js → index-B8lyh_ti.js} +1316 -559
- package/dist/index-B8lyh_ti.js.map +1 -0
- package/dist/{index-cnitc68U.cjs → index-C-TQIrdG.cjs} +1422 -612
- package/dist/index-C-TQIrdG.cjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/memory-volume.d.ts +1 -1
- package/dist/polyfills/wasi.d.ts +45 -4
- package/dist/script-engine.d.ts +2 -0
- package/dist/sdk/nodepod.d.ts +4 -3
- package/dist/sdk/types.d.ts +6 -0
- package/dist/syntax-transforms.d.ts +1 -0
- package/dist/threading/process-manager.d.ts +1 -1
- package/dist/threading/worker-protocol.d.ts +1 -1
- package/package.json +5 -3
- package/src/__tests__/bench/integration.bench.ts +117 -0
- package/src/__tests__/bench/memory-volume.bench.ts +115 -0
- package/src/__tests__/bench/polyfills.bench.ts +147 -0
- package/src/__tests__/bench/script-engine.bench.ts +104 -0
- package/src/__tests__/bench/shell.bench.ts +101 -0
- package/src/__tests__/bench/syntax-transforms.bench.ts +82 -0
- package/src/__tests__/bench/version-resolver.bench.ts +95 -0
- package/src/__tests__/buffer.test.ts +273 -0
- package/src/__tests__/byte-encoding.test.ts +98 -0
- package/src/__tests__/digest.test.ts +44 -0
- package/src/__tests__/events.test.ts +245 -0
- package/src/__tests__/memory-volume.test.ts +443 -0
- package/src/__tests__/path.test.ts +181 -0
- package/src/__tests__/process.test.ts +129 -0
- package/src/__tests__/script-engine.test.ts +229 -0
- package/src/__tests__/shell-builtins.test.ts +357 -0
- package/src/__tests__/shell-interpreter.test.ts +157 -0
- package/src/__tests__/shell-parser.test.ts +204 -0
- package/src/__tests__/stream.test.ts +142 -0
- package/src/__tests__/syntax-transforms.test.ts +158 -0
- package/src/__tests__/version-resolver.test.ts +184 -0
- package/src/constants/cdn-urls.ts +18 -18
- package/src/helpers/byte-encoding.ts +51 -39
- package/src/index.ts +192 -192
- package/src/memory-volume.ts +968 -941
- package/src/module-transformer.ts +368 -368
- package/src/packages/installer.ts +396 -396
- package/src/packages/version-resolver.ts +12 -2
- package/src/polyfills/buffer.ts +633 -628
- package/src/polyfills/child_process.ts +2288 -2288
- package/src/polyfills/esbuild.ts +854 -854
- package/src/polyfills/events.ts +282 -276
- package/src/polyfills/fs.ts +2888 -2888
- package/src/polyfills/http.ts +1450 -1449
- package/src/polyfills/process.ts +721 -690
- package/src/polyfills/readline.ts +692 -692
- package/src/polyfills/stream.ts +1620 -1620
- package/src/polyfills/tty.ts +71 -71
- package/src/polyfills/wasi.ts +1284 -22
- package/src/request-proxy.ts +716 -716
- package/src/script-engine.ts +465 -146
- package/src/sdk/nodepod.ts +525 -509
- package/src/sdk/types.ts +7 -0
- package/src/syntax-transforms.ts +543 -561
- package/src/threading/offload-worker.ts +383 -383
- package/src/threading/offload.ts +271 -271
- package/src/threading/process-manager.ts +956 -956
- package/src/threading/process-worker-entry.ts +858 -854
- package/src/threading/worker-protocol.ts +1 -1
- package/dist/child_process-B38qoN6R.cjs.map +0 -1
- package/dist/child_process-Dopvyd-E.js.map +0 -1
- package/dist/index--Qr8LVpQ.js.map +0 -1
- package/dist/index-cnitc68U.cjs.map +0 -1
|
@@ -1,396 +1,396 @@
|
|
|
1
|
-
// Dependency Installer
|
|
2
|
-
// Handles the full install lifecycle: resolve, download, extract, transform, bin stubs, lock file.
|
|
3
|
-
|
|
4
|
-
import { MemoryVolume } from "../memory-volume";
|
|
5
|
-
import { RegistryClient, RegistryConfig } from "./registry-client";
|
|
6
|
-
import {
|
|
7
|
-
resolveDependencyTree,
|
|
8
|
-
resolveFromManifest,
|
|
9
|
-
ResolvedDependency,
|
|
10
|
-
ResolutionConfig,
|
|
11
|
-
} from "./version-resolver";
|
|
12
|
-
import { downloadAndExtract } from "./archive-extractor";
|
|
13
|
-
import { convertPackage, prepareTransformer } from "../module-transformer";
|
|
14
|
-
import type { PackageManifest } from "../types/manifest";
|
|
15
|
-
import * as path from "../polyfills/path";
|
|
16
|
-
|
|
17
|
-
// ---------------------------------------------------------------------------
|
|
18
|
-
// Public types
|
|
19
|
-
// ---------------------------------------------------------------------------
|
|
20
|
-
|
|
21
|
-
export interface InstallFlags {
|
|
22
|
-
registry?: string;
|
|
23
|
-
persist?: boolean;
|
|
24
|
-
persistDev?: boolean;
|
|
25
|
-
withDevDeps?: boolean;
|
|
26
|
-
withOptionalDeps?: boolean;
|
|
27
|
-
onProgress?: (message: string) => void;
|
|
28
|
-
// default: true
|
|
29
|
-
transformModules?: boolean;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface InstallOutcome {
|
|
33
|
-
resolved: Map<string, ResolvedDependency>;
|
|
34
|
-
newPackages: string[];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// ---------------------------------------------------------------------------
|
|
38
|
-
// Helpers
|
|
39
|
-
// ---------------------------------------------------------------------------
|
|
40
|
-
|
|
41
|
-
// Normalize bin field — handles both shorthand string and object forms
|
|
42
|
-
function normalizeBinField(
|
|
43
|
-
packageName: string,
|
|
44
|
-
bin?: string | Record<string, string>,
|
|
45
|
-
): Record<string, string> {
|
|
46
|
-
if (!bin) return {};
|
|
47
|
-
if (typeof bin === "string") {
|
|
48
|
-
const command = packageName.includes("/")
|
|
49
|
-
? packageName.split("/").pop()!
|
|
50
|
-
: packageName;
|
|
51
|
-
return { [command]: bin };
|
|
52
|
-
}
|
|
53
|
-
return bin;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Split "express@4.18.2" or "@types/node@20" into name + version
|
|
57
|
-
function splitSpecifier(spec: string): { name: string; version?: string } {
|
|
58
|
-
if (spec.startsWith("@")) {
|
|
59
|
-
const slashIdx = spec.indexOf("/");
|
|
60
|
-
if (slashIdx === -1)
|
|
61
|
-
throw new Error(`Malformed package specifier: ${spec}`);
|
|
62
|
-
|
|
63
|
-
const tail = spec.slice(slashIdx + 1);
|
|
64
|
-
const atIdx = tail.indexOf("@");
|
|
65
|
-
if (atIdx === -1) return { name: spec };
|
|
66
|
-
return {
|
|
67
|
-
name: spec.slice(0, slashIdx + 1 + atIdx),
|
|
68
|
-
version: tail.slice(atIdx + 1),
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const atIdx = spec.indexOf("@");
|
|
73
|
-
if (atIdx === -1) return { name: spec };
|
|
74
|
-
return {
|
|
75
|
-
name: spec.slice(0, atIdx),
|
|
76
|
-
version: spec.slice(atIdx + 1),
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// ---------------------------------------------------------------------------
|
|
81
|
-
// Main class
|
|
82
|
-
// ---------------------------------------------------------------------------
|
|
83
|
-
|
|
84
|
-
let transformerReady = false;
|
|
85
|
-
|
|
86
|
-
export class DependencyInstaller {
|
|
87
|
-
private vol: MemoryVolume;
|
|
88
|
-
private registryClient: RegistryClient;
|
|
89
|
-
private workingDir: string;
|
|
90
|
-
|
|
91
|
-
constructor(vol: MemoryVolume, opts: { cwd?: string } & RegistryConfig = {}) {
|
|
92
|
-
this.vol = vol;
|
|
93
|
-
this.registryClient = new RegistryClient(opts);
|
|
94
|
-
this.workingDir = opts.cwd || "/";
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// -----------------------------------------------------------------------
|
|
98
|
-
// Public API
|
|
99
|
-
// -----------------------------------------------------------------------
|
|
100
|
-
|
|
101
|
-
async install(
|
|
102
|
-
packageName: string,
|
|
103
|
-
version?: string,
|
|
104
|
-
flags: InstallFlags = {},
|
|
105
|
-
): Promise<InstallOutcome> {
|
|
106
|
-
const { onProgress } = flags;
|
|
107
|
-
|
|
108
|
-
const spec = splitSpecifier(packageName);
|
|
109
|
-
const targetName = spec.name;
|
|
110
|
-
const targetRange = version || spec.version || "latest";
|
|
111
|
-
|
|
112
|
-
onProgress?.(`Resolving ${targetName}@${targetRange}...`);
|
|
113
|
-
|
|
114
|
-
const resolutionOpts: ResolutionConfig = {
|
|
115
|
-
registry: this.registryClient,
|
|
116
|
-
devDependencies: flags.withDevDeps,
|
|
117
|
-
optionalDependencies: flags.withOptionalDeps,
|
|
118
|
-
onProgress,
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
const tree = await resolveDependencyTree(
|
|
122
|
-
targetName,
|
|
123
|
-
targetRange,
|
|
124
|
-
resolutionOpts,
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
const newPkgs = await this.materializePackages(tree, flags);
|
|
128
|
-
|
|
129
|
-
if (flags.persist || flags.persistDev) {
|
|
130
|
-
const entry = tree.get(targetName);
|
|
131
|
-
if (entry) {
|
|
132
|
-
await this.patchManifest(
|
|
133
|
-
targetName,
|
|
134
|
-
`^${entry.version}`,
|
|
135
|
-
!!flags.persistDev,
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
onProgress?.(`Installed ${tree.size} package(s)`);
|
|
141
|
-
|
|
142
|
-
return { resolved: tree, newPackages: newPkgs };
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
async installFromManifest(
|
|
146
|
-
manifestPath?: string,
|
|
147
|
-
flags: InstallFlags = {},
|
|
148
|
-
): Promise<InstallOutcome> {
|
|
149
|
-
const { onProgress } = flags;
|
|
150
|
-
|
|
151
|
-
const jsonPath = manifestPath || path.join(this.workingDir, "package.json");
|
|
152
|
-
|
|
153
|
-
if (!this.vol.existsSync(jsonPath)) {
|
|
154
|
-
throw new Error(`Manifest not found at ${jsonPath}`);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const raw = this.vol.readFileSync(jsonPath, "utf8");
|
|
158
|
-
const manifest: PackageManifest = JSON.parse(raw);
|
|
159
|
-
|
|
160
|
-
onProgress?.("Resolving dependency tree...");
|
|
161
|
-
|
|
162
|
-
const resolutionOpts: ResolutionConfig = {
|
|
163
|
-
registry: this.registryClient,
|
|
164
|
-
devDependencies: flags.withDevDeps,
|
|
165
|
-
optionalDependencies: flags.withOptionalDeps,
|
|
166
|
-
onProgress,
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
const tree = await resolveFromManifest(manifest, resolutionOpts);
|
|
170
|
-
|
|
171
|
-
const newPkgs = await this.materializePackages(tree, flags);
|
|
172
|
-
|
|
173
|
-
onProgress?.(`Installed ${tree.size} package(s)`);
|
|
174
|
-
|
|
175
|
-
return { resolved: tree, newPackages: newPkgs };
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
listInstalled(): Record<string, string> {
|
|
179
|
-
const nmDir = path.join(this.workingDir, "node_modules");
|
|
180
|
-
if (!this.vol.existsSync(nmDir)) return {};
|
|
181
|
-
|
|
182
|
-
const result: Record<string, string> = {};
|
|
183
|
-
const topLevel = this.vol.readdirSync(nmDir) as string[];
|
|
184
|
-
|
|
185
|
-
for (const entry of topLevel) {
|
|
186
|
-
if (entry.startsWith(".")) continue;
|
|
187
|
-
|
|
188
|
-
if (entry.startsWith("@")) {
|
|
189
|
-
const scopeDir = path.join(nmDir, entry);
|
|
190
|
-
const scopedEntries = this.vol.readdirSync(scopeDir) as string[];
|
|
191
|
-
for (const child of scopedEntries) {
|
|
192
|
-
const manifest = path.join(scopeDir, child, "package.json");
|
|
193
|
-
if (this.vol.existsSync(manifest)) {
|
|
194
|
-
const data = JSON.parse(this.vol.readFileSync(manifest, "utf8"));
|
|
195
|
-
result[`${entry}/${child}`] = data.version;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
} else {
|
|
199
|
-
const manifest = path.join(nmDir, entry, "package.json");
|
|
200
|
-
if (this.vol.existsSync(manifest)) {
|
|
201
|
-
const data = JSON.parse(this.vol.readFileSync(manifest, "utf8"));
|
|
202
|
-
result[entry] = data.version;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
return result;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// -----------------------------------------------------------------------
|
|
211
|
-
// Private helpers
|
|
212
|
-
// -----------------------------------------------------------------------
|
|
213
|
-
|
|
214
|
-
// Download, extract, transform, and wire up packages not already in node_modules
|
|
215
|
-
private async materializePackages(
|
|
216
|
-
tree: Map<string, ResolvedDependency>,
|
|
217
|
-
flags: InstallFlags,
|
|
218
|
-
): Promise<string[]> {
|
|
219
|
-
const { onProgress } = flags;
|
|
220
|
-
const additions: string[] = [];
|
|
221
|
-
|
|
222
|
-
const nmRoot = path.join(this.workingDir, "node_modules");
|
|
223
|
-
this.vol.mkdirSync(nmRoot, { recursive: true });
|
|
224
|
-
|
|
225
|
-
const pending: Array<{
|
|
226
|
-
depName: string;
|
|
227
|
-
dep: ResolvedDependency;
|
|
228
|
-
targetDir: string;
|
|
229
|
-
}> = [];
|
|
230
|
-
|
|
231
|
-
for (const [depName, dep] of tree) {
|
|
232
|
-
const targetDir = path.join(nmRoot, depName);
|
|
233
|
-
const existingManifest = path.join(targetDir, "package.json");
|
|
234
|
-
|
|
235
|
-
if (this.vol.existsSync(existingManifest)) {
|
|
236
|
-
try {
|
|
237
|
-
const current = JSON.parse(
|
|
238
|
-
this.vol.readFileSync(existingManifest, "utf8"),
|
|
239
|
-
);
|
|
240
|
-
if (current.version === dep.version) {
|
|
241
|
-
onProgress?.(`Skipping ${depName}@${dep.version} (up to date)`);
|
|
242
|
-
continue;
|
|
243
|
-
}
|
|
244
|
-
} catch {
|
|
245
|
-
// corrupt manifest, reinstall
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
pending.push({ depName, dep, targetDir });
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// Only need main-thread transformer as fallback when workers aren't available
|
|
253
|
-
const shouldTransform = flags.transformModules !== false;
|
|
254
|
-
if (shouldTransform && !transformerReady) {
|
|
255
|
-
if (typeof Worker === "undefined") {
|
|
256
|
-
onProgress?.("Preparing module transformer...");
|
|
257
|
-
await prepareTransformer();
|
|
258
|
-
}
|
|
259
|
-
transformerReady = true;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
// Safe to batch aggressively since extract + transform are offloaded to workers
|
|
263
|
-
const WORKER_COUNT = 12;
|
|
264
|
-
onProgress?.(`Downloading ${pending.length} package(s)...`);
|
|
265
|
-
|
|
266
|
-
for (let offset = 0; offset < pending.length; offset += WORKER_COUNT) {
|
|
267
|
-
const batch = pending.slice(offset, offset + WORKER_COUNT);
|
|
268
|
-
|
|
269
|
-
await Promise.all(
|
|
270
|
-
batch.map(async ({ depName, dep, targetDir }) => {
|
|
271
|
-
onProgress?.(` Fetching ${depName}@${dep.version}...`);
|
|
272
|
-
|
|
273
|
-
await downloadAndExtract(dep.tarballUrl, this.vol, targetDir, {
|
|
274
|
-
stripComponents: 1,
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
if (shouldTransform) {
|
|
278
|
-
try {
|
|
279
|
-
const transformed = await convertPackage(
|
|
280
|
-
this.vol,
|
|
281
|
-
targetDir,
|
|
282
|
-
onProgress,
|
|
283
|
-
);
|
|
284
|
-
if (transformed > 0) {
|
|
285
|
-
onProgress?.(
|
|
286
|
-
` Transformed ${transformed} file(s) in ${depName}`,
|
|
287
|
-
);
|
|
288
|
-
}
|
|
289
|
-
} catch (err) {
|
|
290
|
-
onProgress?.(
|
|
291
|
-
` Warning: transformation failed for ${depName}: ${err}`,
|
|
292
|
-
);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
this.createBinStubs(nmRoot, depName, targetDir);
|
|
297
|
-
|
|
298
|
-
additions.push(depName);
|
|
299
|
-
}),
|
|
300
|
-
);
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
this.writeLockFile(tree);
|
|
304
|
-
|
|
305
|
-
return additions;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
private createBinStubs(
|
|
309
|
-
nmRoot: string,
|
|
310
|
-
depName: string,
|
|
311
|
-
pkgDir: string,
|
|
312
|
-
): void {
|
|
313
|
-
try {
|
|
314
|
-
const manifestPath = path.join(pkgDir, "package.json");
|
|
315
|
-
if (!this.vol.existsSync(manifestPath)) return;
|
|
316
|
-
|
|
317
|
-
const data = JSON.parse(this.vol.readFileSync(manifestPath, "utf8"));
|
|
318
|
-
const bins = normalizeBinField(depName, data.bin);
|
|
319
|
-
const binDir = path.join(nmRoot, ".bin");
|
|
320
|
-
|
|
321
|
-
for (const [cmd, relPath] of Object.entries(bins)) {
|
|
322
|
-
this.vol.mkdirSync(binDir, { recursive: true });
|
|
323
|
-
const target = path.join(pkgDir, relPath);
|
|
324
|
-
this.vol.writeFileSync(
|
|
325
|
-
path.join(binDir, cmd),
|
|
326
|
-
`node "${target}" "$@"\n`,
|
|
327
|
-
);
|
|
328
|
-
}
|
|
329
|
-
} catch {
|
|
330
|
-
// best-effort
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
private writeLockFile(tree: Map<string, ResolvedDependency>): void {
|
|
335
|
-
const entries: Record<string, { version: string; resolved: string }> = {};
|
|
336
|
-
|
|
337
|
-
for (const [depName, dep] of tree) {
|
|
338
|
-
entries[depName] = {
|
|
339
|
-
version: dep.version,
|
|
340
|
-
resolved: dep.tarballUrl,
|
|
341
|
-
};
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
const lockPath = path.join(
|
|
345
|
-
this.workingDir,
|
|
346
|
-
"node_modules",
|
|
347
|
-
".package-lock.json",
|
|
348
|
-
);
|
|
349
|
-
this.vol.writeFileSync(lockPath, JSON.stringify(entries, null, 2));
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
private async patchManifest(
|
|
353
|
-
depName: string,
|
|
354
|
-
versionSpec: string,
|
|
355
|
-
asDev: boolean,
|
|
356
|
-
): Promise<void> {
|
|
357
|
-
const jsonPath = path.join(this.workingDir, "package.json");
|
|
358
|
-
|
|
359
|
-
let manifest: Record<string, unknown> = {};
|
|
360
|
-
if (this.vol.existsSync(jsonPath)) {
|
|
361
|
-
manifest = JSON.parse(this.vol.readFileSync(jsonPath, "utf8"));
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
const section = asDev ? "devDependencies" : "dependencies";
|
|
365
|
-
if (!manifest[section]) {
|
|
366
|
-
manifest[section] = {};
|
|
367
|
-
}
|
|
368
|
-
(manifest[section] as Record<string, string>)[depName] = versionSpec;
|
|
369
|
-
|
|
370
|
-
this.vol.writeFileSync(jsonPath, JSON.stringify(manifest, null, 2));
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
// ---------------------------------------------------------------------------
|
|
375
|
-
// Convenience function
|
|
376
|
-
// ---------------------------------------------------------------------------
|
|
377
|
-
|
|
378
|
-
// One-shot install: `install("express@4.18.2", vol)`
|
|
379
|
-
export async function install(
|
|
380
|
-
specifier: string,
|
|
381
|
-
vol: MemoryVolume,
|
|
382
|
-
flags?: InstallFlags,
|
|
383
|
-
): Promise<InstallOutcome> {
|
|
384
|
-
const installer = new DependencyInstaller(vol);
|
|
385
|
-
return installer.install(specifier, undefined, flags);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
export { RegistryClient } from "./registry-client";
|
|
389
|
-
export type {
|
|
390
|
-
RegistryConfig,
|
|
391
|
-
VersionDetail,
|
|
392
|
-
PackageMetadata,
|
|
393
|
-
} from "./registry-client";
|
|
394
|
-
export type { ResolvedDependency, ResolutionConfig } from "./version-resolver";
|
|
395
|
-
export type { ExtractionOptions } from "./archive-extractor";
|
|
396
|
-
export { splitSpecifier };
|
|
1
|
+
// Dependency Installer
|
|
2
|
+
// Handles the full install lifecycle: resolve, download, extract, transform, bin stubs, lock file.
|
|
3
|
+
|
|
4
|
+
import { MemoryVolume } from "../memory-volume";
|
|
5
|
+
import { RegistryClient, RegistryConfig } from "./registry-client";
|
|
6
|
+
import {
|
|
7
|
+
resolveDependencyTree,
|
|
8
|
+
resolveFromManifest,
|
|
9
|
+
ResolvedDependency,
|
|
10
|
+
ResolutionConfig,
|
|
11
|
+
} from "./version-resolver";
|
|
12
|
+
import { downloadAndExtract } from "./archive-extractor";
|
|
13
|
+
import { convertPackage, prepareTransformer } from "../module-transformer";
|
|
14
|
+
import type { PackageManifest } from "../types/manifest";
|
|
15
|
+
import * as path from "../polyfills/path";
|
|
16
|
+
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
// Public types
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
export interface InstallFlags {
|
|
22
|
+
registry?: string;
|
|
23
|
+
persist?: boolean;
|
|
24
|
+
persistDev?: boolean;
|
|
25
|
+
withDevDeps?: boolean;
|
|
26
|
+
withOptionalDeps?: boolean;
|
|
27
|
+
onProgress?: (message: string) => void;
|
|
28
|
+
// default: true
|
|
29
|
+
transformModules?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface InstallOutcome {
|
|
33
|
+
resolved: Map<string, ResolvedDependency>;
|
|
34
|
+
newPackages: string[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
// Helpers
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
|
|
41
|
+
// Normalize bin field — handles both shorthand string and object forms
|
|
42
|
+
function normalizeBinField(
|
|
43
|
+
packageName: string,
|
|
44
|
+
bin?: string | Record<string, string>,
|
|
45
|
+
): Record<string, string> {
|
|
46
|
+
if (!bin) return {};
|
|
47
|
+
if (typeof bin === "string") {
|
|
48
|
+
const command = packageName.includes("/")
|
|
49
|
+
? packageName.split("/").pop()!
|
|
50
|
+
: packageName;
|
|
51
|
+
return { [command]: bin };
|
|
52
|
+
}
|
|
53
|
+
return bin;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Split "express@4.18.2" or "@types/node@20" into name + version
|
|
57
|
+
function splitSpecifier(spec: string): { name: string; version?: string } {
|
|
58
|
+
if (spec.startsWith("@")) {
|
|
59
|
+
const slashIdx = spec.indexOf("/");
|
|
60
|
+
if (slashIdx === -1)
|
|
61
|
+
throw new Error(`Malformed package specifier: ${spec}`);
|
|
62
|
+
|
|
63
|
+
const tail = spec.slice(slashIdx + 1);
|
|
64
|
+
const atIdx = tail.indexOf("@");
|
|
65
|
+
if (atIdx === -1) return { name: spec };
|
|
66
|
+
return {
|
|
67
|
+
name: spec.slice(0, slashIdx + 1 + atIdx),
|
|
68
|
+
version: tail.slice(atIdx + 1),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const atIdx = spec.indexOf("@");
|
|
73
|
+
if (atIdx === -1) return { name: spec };
|
|
74
|
+
return {
|
|
75
|
+
name: spec.slice(0, atIdx),
|
|
76
|
+
version: spec.slice(atIdx + 1),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// ---------------------------------------------------------------------------
|
|
81
|
+
// Main class
|
|
82
|
+
// ---------------------------------------------------------------------------
|
|
83
|
+
|
|
84
|
+
let transformerReady = false;
|
|
85
|
+
|
|
86
|
+
export class DependencyInstaller {
|
|
87
|
+
private vol: MemoryVolume;
|
|
88
|
+
private registryClient: RegistryClient;
|
|
89
|
+
private workingDir: string;
|
|
90
|
+
|
|
91
|
+
constructor(vol: MemoryVolume, opts: { cwd?: string } & RegistryConfig = {}) {
|
|
92
|
+
this.vol = vol;
|
|
93
|
+
this.registryClient = new RegistryClient(opts);
|
|
94
|
+
this.workingDir = opts.cwd || "/";
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// -----------------------------------------------------------------------
|
|
98
|
+
// Public API
|
|
99
|
+
// -----------------------------------------------------------------------
|
|
100
|
+
|
|
101
|
+
async install(
|
|
102
|
+
packageName: string,
|
|
103
|
+
version?: string,
|
|
104
|
+
flags: InstallFlags = {},
|
|
105
|
+
): Promise<InstallOutcome> {
|
|
106
|
+
const { onProgress } = flags;
|
|
107
|
+
|
|
108
|
+
const spec = splitSpecifier(packageName);
|
|
109
|
+
const targetName = spec.name;
|
|
110
|
+
const targetRange = version || spec.version || "latest";
|
|
111
|
+
|
|
112
|
+
onProgress?.(`Resolving ${targetName}@${targetRange}...`);
|
|
113
|
+
|
|
114
|
+
const resolutionOpts: ResolutionConfig = {
|
|
115
|
+
registry: this.registryClient,
|
|
116
|
+
devDependencies: flags.withDevDeps,
|
|
117
|
+
optionalDependencies: flags.withOptionalDeps,
|
|
118
|
+
onProgress,
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const tree = await resolveDependencyTree(
|
|
122
|
+
targetName,
|
|
123
|
+
targetRange,
|
|
124
|
+
resolutionOpts,
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const newPkgs = await this.materializePackages(tree, flags);
|
|
128
|
+
|
|
129
|
+
if (flags.persist || flags.persistDev) {
|
|
130
|
+
const entry = tree.get(targetName);
|
|
131
|
+
if (entry) {
|
|
132
|
+
await this.patchManifest(
|
|
133
|
+
targetName,
|
|
134
|
+
`^${entry.version}`,
|
|
135
|
+
!!flags.persistDev,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
onProgress?.(`Installed ${tree.size} package(s)`);
|
|
141
|
+
|
|
142
|
+
return { resolved: tree, newPackages: newPkgs };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async installFromManifest(
|
|
146
|
+
manifestPath?: string,
|
|
147
|
+
flags: InstallFlags = {},
|
|
148
|
+
): Promise<InstallOutcome> {
|
|
149
|
+
const { onProgress } = flags;
|
|
150
|
+
|
|
151
|
+
const jsonPath = manifestPath || path.join(this.workingDir, "package.json");
|
|
152
|
+
|
|
153
|
+
if (!this.vol.existsSync(jsonPath)) {
|
|
154
|
+
throw new Error(`Manifest not found at ${jsonPath}`);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const raw = this.vol.readFileSync(jsonPath, "utf8");
|
|
158
|
+
const manifest: PackageManifest = JSON.parse(raw);
|
|
159
|
+
|
|
160
|
+
onProgress?.("Resolving dependency tree...");
|
|
161
|
+
|
|
162
|
+
const resolutionOpts: ResolutionConfig = {
|
|
163
|
+
registry: this.registryClient,
|
|
164
|
+
devDependencies: flags.withDevDeps,
|
|
165
|
+
optionalDependencies: flags.withOptionalDeps,
|
|
166
|
+
onProgress,
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const tree = await resolveFromManifest(manifest, resolutionOpts);
|
|
170
|
+
|
|
171
|
+
const newPkgs = await this.materializePackages(tree, flags);
|
|
172
|
+
|
|
173
|
+
onProgress?.(`Installed ${tree.size} package(s)`);
|
|
174
|
+
|
|
175
|
+
return { resolved: tree, newPackages: newPkgs };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
listInstalled(): Record<string, string> {
|
|
179
|
+
const nmDir = path.join(this.workingDir, "node_modules");
|
|
180
|
+
if (!this.vol.existsSync(nmDir)) return {};
|
|
181
|
+
|
|
182
|
+
const result: Record<string, string> = {};
|
|
183
|
+
const topLevel = this.vol.readdirSync(nmDir) as string[];
|
|
184
|
+
|
|
185
|
+
for (const entry of topLevel) {
|
|
186
|
+
if (entry.startsWith(".")) continue;
|
|
187
|
+
|
|
188
|
+
if (entry.startsWith("@")) {
|
|
189
|
+
const scopeDir = path.join(nmDir, entry);
|
|
190
|
+
const scopedEntries = this.vol.readdirSync(scopeDir) as string[];
|
|
191
|
+
for (const child of scopedEntries) {
|
|
192
|
+
const manifest = path.join(scopeDir, child, "package.json");
|
|
193
|
+
if (this.vol.existsSync(manifest)) {
|
|
194
|
+
const data = JSON.parse(this.vol.readFileSync(manifest, "utf8"));
|
|
195
|
+
result[`${entry}/${child}`] = data.version;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
} else {
|
|
199
|
+
const manifest = path.join(nmDir, entry, "package.json");
|
|
200
|
+
if (this.vol.existsSync(manifest)) {
|
|
201
|
+
const data = JSON.parse(this.vol.readFileSync(manifest, "utf8"));
|
|
202
|
+
result[entry] = data.version;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return result;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// -----------------------------------------------------------------------
|
|
211
|
+
// Private helpers
|
|
212
|
+
// -----------------------------------------------------------------------
|
|
213
|
+
|
|
214
|
+
// Download, extract, transform, and wire up packages not already in node_modules
|
|
215
|
+
private async materializePackages(
|
|
216
|
+
tree: Map<string, ResolvedDependency>,
|
|
217
|
+
flags: InstallFlags,
|
|
218
|
+
): Promise<string[]> {
|
|
219
|
+
const { onProgress } = flags;
|
|
220
|
+
const additions: string[] = [];
|
|
221
|
+
|
|
222
|
+
const nmRoot = path.join(this.workingDir, "node_modules");
|
|
223
|
+
this.vol.mkdirSync(nmRoot, { recursive: true });
|
|
224
|
+
|
|
225
|
+
const pending: Array<{
|
|
226
|
+
depName: string;
|
|
227
|
+
dep: ResolvedDependency;
|
|
228
|
+
targetDir: string;
|
|
229
|
+
}> = [];
|
|
230
|
+
|
|
231
|
+
for (const [depName, dep] of tree) {
|
|
232
|
+
const targetDir = path.join(nmRoot, depName);
|
|
233
|
+
const existingManifest = path.join(targetDir, "package.json");
|
|
234
|
+
|
|
235
|
+
if (this.vol.existsSync(existingManifest)) {
|
|
236
|
+
try {
|
|
237
|
+
const current = JSON.parse(
|
|
238
|
+
this.vol.readFileSync(existingManifest, "utf8"),
|
|
239
|
+
);
|
|
240
|
+
if (current.version === dep.version) {
|
|
241
|
+
onProgress?.(`Skipping ${depName}@${dep.version} (up to date)`);
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
} catch {
|
|
245
|
+
// corrupt manifest, reinstall
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
pending.push({ depName, dep, targetDir });
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Only need main-thread transformer as fallback when workers aren't available
|
|
253
|
+
const shouldTransform = flags.transformModules !== false;
|
|
254
|
+
if (shouldTransform && !transformerReady) {
|
|
255
|
+
if (typeof Worker === "undefined") {
|
|
256
|
+
onProgress?.("Preparing module transformer...");
|
|
257
|
+
await prepareTransformer();
|
|
258
|
+
}
|
|
259
|
+
transformerReady = true;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Safe to batch aggressively since extract + transform are offloaded to workers
|
|
263
|
+
const WORKER_COUNT = 12;
|
|
264
|
+
onProgress?.(`Downloading ${pending.length} package(s)...`);
|
|
265
|
+
|
|
266
|
+
for (let offset = 0; offset < pending.length; offset += WORKER_COUNT) {
|
|
267
|
+
const batch = pending.slice(offset, offset + WORKER_COUNT);
|
|
268
|
+
|
|
269
|
+
await Promise.all(
|
|
270
|
+
batch.map(async ({ depName, dep, targetDir }) => {
|
|
271
|
+
onProgress?.(` Fetching ${depName}@${dep.version}...`);
|
|
272
|
+
|
|
273
|
+
await downloadAndExtract(dep.tarballUrl, this.vol, targetDir, {
|
|
274
|
+
stripComponents: 1,
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
if (shouldTransform) {
|
|
278
|
+
try {
|
|
279
|
+
const transformed = await convertPackage(
|
|
280
|
+
this.vol,
|
|
281
|
+
targetDir,
|
|
282
|
+
onProgress,
|
|
283
|
+
);
|
|
284
|
+
if (transformed > 0) {
|
|
285
|
+
onProgress?.(
|
|
286
|
+
` Transformed ${transformed} file(s) in ${depName}`,
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
} catch (err) {
|
|
290
|
+
onProgress?.(
|
|
291
|
+
` Warning: transformation failed for ${depName}: ${err}`,
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
this.createBinStubs(nmRoot, depName, targetDir);
|
|
297
|
+
|
|
298
|
+
additions.push(depName);
|
|
299
|
+
}),
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
this.writeLockFile(tree);
|
|
304
|
+
|
|
305
|
+
return additions;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
private createBinStubs(
|
|
309
|
+
nmRoot: string,
|
|
310
|
+
depName: string,
|
|
311
|
+
pkgDir: string,
|
|
312
|
+
): void {
|
|
313
|
+
try {
|
|
314
|
+
const manifestPath = path.join(pkgDir, "package.json");
|
|
315
|
+
if (!this.vol.existsSync(manifestPath)) return;
|
|
316
|
+
|
|
317
|
+
const data = JSON.parse(this.vol.readFileSync(manifestPath, "utf8"));
|
|
318
|
+
const bins = normalizeBinField(depName, data.bin);
|
|
319
|
+
const binDir = path.join(nmRoot, ".bin");
|
|
320
|
+
|
|
321
|
+
for (const [cmd, relPath] of Object.entries(bins)) {
|
|
322
|
+
this.vol.mkdirSync(binDir, { recursive: true });
|
|
323
|
+
const target = path.join(pkgDir, relPath);
|
|
324
|
+
this.vol.writeFileSync(
|
|
325
|
+
path.join(binDir, cmd),
|
|
326
|
+
`node "${target}" "$@"\n`,
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
} catch {
|
|
330
|
+
// best-effort
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
private writeLockFile(tree: Map<string, ResolvedDependency>): void {
|
|
335
|
+
const entries: Record<string, { version: string; resolved: string }> = {};
|
|
336
|
+
|
|
337
|
+
for (const [depName, dep] of tree) {
|
|
338
|
+
entries[depName] = {
|
|
339
|
+
version: dep.version,
|
|
340
|
+
resolved: dep.tarballUrl,
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const lockPath = path.join(
|
|
345
|
+
this.workingDir,
|
|
346
|
+
"node_modules",
|
|
347
|
+
".package-lock.json",
|
|
348
|
+
);
|
|
349
|
+
this.vol.writeFileSync(lockPath, JSON.stringify(entries, null, 2));
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
private async patchManifest(
|
|
353
|
+
depName: string,
|
|
354
|
+
versionSpec: string,
|
|
355
|
+
asDev: boolean,
|
|
356
|
+
): Promise<void> {
|
|
357
|
+
const jsonPath = path.join(this.workingDir, "package.json");
|
|
358
|
+
|
|
359
|
+
let manifest: Record<string, unknown> = {};
|
|
360
|
+
if (this.vol.existsSync(jsonPath)) {
|
|
361
|
+
manifest = JSON.parse(this.vol.readFileSync(jsonPath, "utf8"));
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const section = asDev ? "devDependencies" : "dependencies";
|
|
365
|
+
if (!manifest[section]) {
|
|
366
|
+
manifest[section] = {};
|
|
367
|
+
}
|
|
368
|
+
(manifest[section] as Record<string, string>)[depName] = versionSpec;
|
|
369
|
+
|
|
370
|
+
this.vol.writeFileSync(jsonPath, JSON.stringify(manifest, null, 2));
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// ---------------------------------------------------------------------------
|
|
375
|
+
// Convenience function
|
|
376
|
+
// ---------------------------------------------------------------------------
|
|
377
|
+
|
|
378
|
+
// One-shot install: `install("express@4.18.2", vol)`
|
|
379
|
+
export async function install(
|
|
380
|
+
specifier: string,
|
|
381
|
+
vol: MemoryVolume,
|
|
382
|
+
flags?: InstallFlags,
|
|
383
|
+
): Promise<InstallOutcome> {
|
|
384
|
+
const installer = new DependencyInstaller(vol);
|
|
385
|
+
return installer.install(specifier, undefined, flags);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
export { RegistryClient } from "./registry-client";
|
|
389
|
+
export type {
|
|
390
|
+
RegistryConfig,
|
|
391
|
+
VersionDetail,
|
|
392
|
+
PackageMetadata,
|
|
393
|
+
} from "./registry-client";
|
|
394
|
+
export type { ResolvedDependency, ResolutionConfig } from "./version-resolver";
|
|
395
|
+
export type { ExtractionOptions } from "./archive-extractor";
|
|
396
|
+
export { splitSpecifier };
|