-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathnpx-resolver.ts
More file actions
424 lines (369 loc) · 12.2 KB
/
npx-resolver.ts
File metadata and controls
424 lines (369 loc) · 12.2 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// npx-resolver.ts - Resolve npx/npm exec binaries to avoid npm parent processes
import { existsSync, readFileSync, realpathSync, readdirSync, statSync, writeFileSync, renameSync, mkdirSync, openSync, readSync, closeSync } from "node:fs";
import { join, dirname, extname, resolve, sep } from "node:path";
import { getAgentPath } from "./agent-dir.ts";
import { spawn, spawnSync } from "node:child_process";
const CACHE_VERSION = 1;
const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
interface NpxCacheEntry {
resolvedBin: string;
resolvedAt: number;
packageVersion?: string;
isJs: boolean;
}
interface NpxCache {
version: number;
entries: Record<string, NpxCacheEntry>;
}
export interface NpxResolution {
binPath: string;
extraArgs: string[];
isJs: boolean;
}
interface ParsedInvocation {
packageSpec: string;
binName?: string;
extraArgs: string[];
}
export async function resolveNpxBinary(
command: string,
args: string[]
): Promise<NpxResolution | null> {
const parsed = command === "npx"
? parseNpxArgs(args)
: command === "npm"
? parseNpmExecArgs(args)
: null;
if (!parsed) return null;
const cacheKey = JSON.stringify([command, ...args]);
const cache = loadCache();
const cached = cache?.entries?.[cacheKey];
if (cached && Date.now() - cached.resolvedAt < CACHE_TTL_MS && existsSync(cached.resolvedBin)) {
return { binPath: cached.resolvedBin, extraArgs: parsed.extraArgs, isJs: cached.isJs };
}
const resolved = resolveFromNpmCache(parsed.packageSpec, parsed.binName);
if (resolved) {
saveCacheEntry(cacheKey, resolved);
return { binPath: resolved.resolvedBin, extraArgs: parsed.extraArgs, isJs: resolved.isJs };
}
// Slow path: force npx cache population
await forceNpxCache(parsed.packageSpec);
const resolvedAfterInstall = resolveFromNpmCache(parsed.packageSpec, parsed.binName);
if (resolvedAfterInstall) {
saveCacheEntry(cacheKey, resolvedAfterInstall);
return { binPath: resolvedAfterInstall.resolvedBin, extraArgs: parsed.extraArgs, isJs: resolvedAfterInstall.isJs };
}
return null;
}
function parseNpxArgs(args: string[]): ParsedInvocation | null {
const separatorIndex = args.indexOf("--");
const before = separatorIndex >= 0 ? args.slice(0, separatorIndex) : args;
const after = separatorIndex >= 0 ? args.slice(separatorIndex + 1) : [];
const positionals: string[] = [];
let packageSpec: string | undefined;
let sawPackageFlag = false;
let foundFirstPositional = false;
for (let i = 0; i < before.length; i++) {
const arg = before[i];
if (foundFirstPositional) {
positionals.push(arg);
continue;
}
if (arg === "-y" || arg === "--yes") continue;
if (arg === "-p" || arg === "--package") {
const value = before[i + 1];
if (!value || value.startsWith("-")) return null;
if (!packageSpec) packageSpec = value;
sawPackageFlag = true;
i++;
continue;
}
if (arg.startsWith("--package=")) {
const value = arg.slice("--package=".length);
if (!value) return null;
if (!packageSpec) packageSpec = value;
sawPackageFlag = true;
continue;
}
if (arg.startsWith("-")) {
return null;
}
positionals.push(arg);
foundFirstPositional = true;
}
if (sawPackageFlag) {
const binName = positionals[0];
if (!packageSpec || !binName) return null;
const extraArgs = positionals.slice(1).concat(after);
return { packageSpec, binName, extraArgs };
}
const packagePositional = positionals[0];
if (!packagePositional) return null;
const extraArgs = positionals.slice(1).concat(after);
return { packageSpec: packagePositional, extraArgs };
}
function parseNpmExecArgs(args: string[]): ParsedInvocation | null {
if (args[0] !== "exec") return null;
const execArgs = args.slice(1);
const separatorIndex = execArgs.indexOf("--");
if (separatorIndex < 0) return null;
const before = execArgs.slice(0, separatorIndex);
const after = execArgs.slice(separatorIndex + 1);
let packageSpec: string | undefined;
for (let i = 0; i < before.length; i++) {
const arg = before[i];
if (arg === "-y" || arg === "--yes") continue;
if (arg === "--package") {
const value = before[i + 1];
if (!value || value.startsWith("-")) return null;
if (!packageSpec) packageSpec = value;
i++;
continue;
}
if (arg.startsWith("--package=")) {
const value = arg.slice("--package=".length);
if (!value) return null;
if (!packageSpec) packageSpec = value;
continue;
}
if (arg.startsWith("-")) {
return null;
}
}
const binName = after[0];
if (!packageSpec || !binName) return null;
const extraArgs = after.slice(1);
return { packageSpec, binName, extraArgs };
}
function resolveFromNpmCache(packageSpec: string, binName?: string): NpxCacheEntry | null {
const cacheDir = getNpmCacheDir();
if (!cacheDir) return null;
const packageName = extractPackageName(packageSpec);
if (!packageName) return null;
const packageDir = findCachedPackageDir(cacheDir, packageName);
if (!packageDir) return null;
const packageJsonPath = join(packageDir, "package.json");
if (!existsSync(packageJsonPath)) return null;
let pkg: { bin?: string | Record<string, string>; version?: string } | null = null;
try {
pkg = JSON.parse(readFileSync(packageJsonPath, "utf-8")) as {
bin?: string | Record<string, string>;
version?: string;
};
} catch {
return null;
}
const binField = pkg?.bin;
if (!binField) return null;
const candidates = buildBinCandidates(packageName, binName);
let chosenBinName: string | undefined;
let binRel: string | undefined;
if (typeof binField === "string") {
chosenBinName = defaultBinName(packageName);
binRel = binField;
} else {
for (const candidate of candidates) {
if (binField[candidate]) {
chosenBinName = candidate;
binRel = binField[candidate];
break;
}
}
if (!binRel) {
const firstEntry = Object.entries(binField)[0];
if (firstEntry) {
chosenBinName = firstEntry[0];
binRel = firstEntry[1];
}
}
}
if (!binRel) return null;
const nodeModulesDir = findNodeModulesDir(packageDir);
const binLink = chosenBinName ? join(nodeModulesDir, ".bin", chosenBinName) : null;
let resolvedBin = binLink && existsSync(binLink) ? safeRealpath(binLink) : "";
if (!resolvedBin) {
resolvedBin = resolve(packageDir, binRel);
if (!existsSync(resolvedBin)) return null;
}
const isJs = detectJsBinary(resolvedBin);
return {
resolvedBin,
resolvedAt: Date.now(),
packageVersion: pkg?.version,
isJs,
};
}
const FORCE_CACHE_TIMEOUT_MS = 30_000;
async function forceNpxCache(packageSpec: string): Promise<void> {
try {
await new Promise<void>((resolve, reject) => {
const proc = spawn(
"npm",
["exec", "--yes", "--package", packageSpec, "--", "node", "-e", "1"],
{ stdio: "ignore" }
);
const timer = setTimeout(() => {
proc.kill();
reject(new Error("timeout"));
}, FORCE_CACHE_TIMEOUT_MS);
timer.unref();
proc.on("close", () => { clearTimeout(timer); resolve(); });
proc.on("error", (err) => { clearTimeout(timer); reject(err); });
});
} catch {
// Ignore failures, resolution will fall back to original command
}
}
function buildBinCandidates(packageName: string, explicitBin?: string): string[] {
const candidates: string[] = [];
if (explicitBin) candidates.push(explicitBin);
if (packageName.startsWith("@")) {
const namePart = packageName.split("/")[1] ?? "";
const scopePart = packageName.split("/")[0]?.replace("@", "") ?? "";
if (namePart) candidates.push(namePart);
if (scopePart && namePart) candidates.push(`${scopePart}-${namePart}`);
} else {
candidates.push(packageName);
}
return [...new Set(candidates.filter(Boolean))];
}
function extractPackageName(spec: string): string | null {
const trimmed = spec.trim();
if (!trimmed) return null;
if (trimmed.startsWith("@")) {
const slashIndex = trimmed.indexOf("/");
if (slashIndex < 0) return null;
const atIndex = trimmed.lastIndexOf("@");
if (atIndex > slashIndex) {
return trimmed.slice(0, atIndex);
}
return trimmed;
}
const atIndex = trimmed.indexOf("@");
return atIndex >= 0 ? trimmed.slice(0, atIndex) : trimmed;
}
function defaultBinName(packageName: string): string {
if (packageName.startsWith("@")) {
const parts = packageName.split("/");
return parts[1] ?? packageName.replace("@", "").replace("/", "-");
}
return packageName;
}
function findCachedPackageDir(cacheDir: string, packageName: string): string | null {
const npxDir = join(cacheDir, "_npx");
if (!existsSync(npxDir)) return null;
const packagePathParts = packageName.startsWith("@")
? packageName.split("/")
: [packageName];
const candidates = readdirSync(npxDir, { withFileTypes: true })
.filter(entry => entry.isDirectory())
.map(entry => {
const full = join(npxDir, entry.name);
const mtime = safeStatMtime(full);
return { name: entry.name, mtime };
})
.sort((a, b) => b.mtime - a.mtime);
for (const entry of candidates) {
const pkgDir = join(npxDir, entry.name, "node_modules", ...packagePathParts);
if (existsSync(join(pkgDir, "package.json"))) {
return pkgDir;
}
}
return null;
}
function findNodeModulesDir(packageDir: string): string {
const parts = packageDir.split(sep);
const idx = parts.lastIndexOf("node_modules");
if (idx >= 0) {
return parts.slice(0, idx + 1).join(sep);
}
return join(packageDir, "..");
}
function detectJsBinary(binPath: string): boolean {
const ext = extname(binPath).toLowerCase();
if (ext === ".js" || ext === ".mjs" || ext === ".cjs") return true;
try {
const fd = openSync(binPath, "r");
try {
const buf = Buffer.alloc(256);
readSync(fd, buf, 0, 256, 0);
const firstLine = buf.toString("utf-8").split("\n")[0] ?? "";
return firstLine.startsWith("#!") && firstLine.includes("node");
} finally {
closeSync(fd);
}
} catch {
return false;
}
}
let npmCacheDirCached: string | null | undefined;
function getNpmCacheDir(): string | null {
if (npmCacheDirCached !== undefined) return npmCacheDirCached;
if (process.env.NPM_CONFIG_CACHE) {
npmCacheDirCached = process.env.NPM_CONFIG_CACHE;
return npmCacheDirCached;
}
try {
const result = spawnSync("npm", ["config", "get", "cache"], { encoding: "utf-8" });
if (result.status === 0) {
const path = String(result.stdout).trim();
npmCacheDirCached = path || null;
return npmCacheDirCached;
}
} catch {
npmCacheDirCached = null;
return null;
}
npmCacheDirCached = null;
return null;
}
function getNpxCachePath(): string {
return getAgentPath("mcp-npx-cache.json");
}
function loadCache(): NpxCache | null {
const cachePath = getNpxCachePath();
if (!existsSync(cachePath)) return null;
try {
const raw = JSON.parse(readFileSync(cachePath, "utf-8"));
if (!raw || typeof raw !== "object") return null;
if (raw.version !== CACHE_VERSION) return null;
if (!raw.entries || typeof raw.entries !== "object") return null;
return raw as NpxCache;
} catch {
return null;
}
}
function saveCacheEntry(key: string, entry: NpxCacheEntry): void {
const cachePath = getNpxCachePath();
const dir = dirname(cachePath);
mkdirSync(dir, { recursive: true });
let merged: NpxCache = { version: CACHE_VERSION, entries: {} };
try {
if (existsSync(cachePath)) {
const existing = JSON.parse(readFileSync(cachePath, "utf-8")) as NpxCache;
if (existing && existing.version === CACHE_VERSION && existing.entries) {
merged.entries = { ...existing.entries };
}
}
} catch {
// Ignore parse errors
}
merged.entries[key] = entry;
const tmpPath = `${cachePath}.${process.pid}.tmp`;
writeFileSync(tmpPath, JSON.stringify(merged, null, 2), "utf-8");
renameSync(tmpPath, cachePath);
}
function safeRealpath(path: string): string {
try {
return realpathSync(path);
} catch {
return "";
}
}
function safeStatMtime(path: string): number {
try {
return statSync(path).mtimeMs;
} catch {
return 0;
}
}