@get-bb/plugin-sdk 0.6.80 → 0.6.90
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/README.md +26 -23
- package/bundled-types/bb-plugin-sdk-app.d.ts +1035 -897
- package/bundled-types/bb-plugin-sdk-internal-file-navigation-validation.d.ts +7 -0
- package/bundled-types/bb-plugin-sdk-internal-rpc-query-host.d.ts +7 -0
- package/bundled-types/bb-plugin-sdk-process.d.ts +61 -0
- package/bundled-types/bb-plugin-sdk-testing-app.d.ts +45 -21
- package/bundled-types/bb-plugin-sdk-testing.d.ts +11 -0
- package/bundled-types/bb-plugin-sdk.d.ts +1029 -895
- package/dist/app.js +16 -0
- package/dist/host.js +557 -13
- package/dist/index.js +1 -1
- package/dist/internal/file-navigation-validation.js +14 -3
- package/dist/internal/host-policy.js +1 -1
- package/dist/internal/plugin-app-collector.js +11 -3
- package/dist/internal/rpc-query-host.js +307 -445
- package/dist/process.js +694 -0
- package/dist/provider-bridge-acp-next.js +739 -320
- package/dist/provider-bridge-acp.js +739 -320
- package/dist/provider-bridge-testing.js +677 -43
- package/dist/provider-bridge-worker-entry.mjs +534 -66
- package/dist/provider-bridge.js +604 -212
- package/dist/testing/app.js +543 -573
- package/dist/testing/index.js +126 -108
- package/package.json +7 -1
package/dist/process.js
ADDED
|
@@ -0,0 +1,694 @@
|
|
|
1
|
+
// ../process-utils/src/host-process.ts
|
|
2
|
+
import parse from "cross-spawn/lib/parse.js";
|
|
3
|
+
import {
|
|
4
|
+
spawn as nodeSpawn,
|
|
5
|
+
fork as nodeFork,
|
|
6
|
+
execFile as nodeExecFile
|
|
7
|
+
} from "node:child_process";
|
|
8
|
+
import crossSpawn from "cross-spawn";
|
|
9
|
+
|
|
10
|
+
// ../process-utils/src/host-toolchain.ts
|
|
11
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
12
|
+
import {
|
|
13
|
+
accessSync,
|
|
14
|
+
constants,
|
|
15
|
+
existsSync,
|
|
16
|
+
readdirSync,
|
|
17
|
+
realpathSync,
|
|
18
|
+
statSync
|
|
19
|
+
} from "node:fs";
|
|
20
|
+
import { createRequire } from "node:module";
|
|
21
|
+
import { homedir } from "node:os";
|
|
22
|
+
import {
|
|
23
|
+
basename,
|
|
24
|
+
delimiter,
|
|
25
|
+
dirname,
|
|
26
|
+
extname,
|
|
27
|
+
isAbsolute,
|
|
28
|
+
join
|
|
29
|
+
} from "node:path";
|
|
30
|
+
import { fileURLToPath } from "node:url";
|
|
31
|
+
|
|
32
|
+
// ../../node_modules/.pnpm/which-command@0.1.0/node_modules/which-command/index.js
|
|
33
|
+
import fs from "node:fs";
|
|
34
|
+
import path from "node:path";
|
|
35
|
+
import process2 from "node:process";
|
|
36
|
+
var isWindows = process2.platform === "win32";
|
|
37
|
+
var separatorPattern = isWindows ? new RegExp("[\\/\\\\]", "v") : new RegExp("\\/", "v");
|
|
38
|
+
var defaultPathExt = ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC";
|
|
39
|
+
function resolveOptions(command, options) {
|
|
40
|
+
if (typeof command !== "string" || command.length === 0) {
|
|
41
|
+
throw new TypeError("Expected a non-empty string.");
|
|
42
|
+
}
|
|
43
|
+
const {
|
|
44
|
+
cwd = process2.cwd(),
|
|
45
|
+
// On Windows, `process.env.PATH` can be undefined in some contexts (for example, worker threads), where it's only exposed as `Path`.
|
|
46
|
+
path: searchPath = process2.env.PATH ?? (isWindows ? process2.env.Path : void 0) ?? "",
|
|
47
|
+
// `||` (not `??`) so an empty `PATHEXT` falls back to the default instead of disabling all lookup on Windows.
|
|
48
|
+
pathExt = process2.env.PATHEXT || defaultPathExt
|
|
49
|
+
} = options;
|
|
50
|
+
return { cwd, searchPath, pathExt };
|
|
51
|
+
}
|
|
52
|
+
function windowsExtensions(command, pathExt) {
|
|
53
|
+
const extensions = pathExt.split(path.delimiter).filter(Boolean);
|
|
54
|
+
const commandExtension = path.extname(command).toLowerCase();
|
|
55
|
+
if (commandExtension !== "" && extensions.some((extension) => extension.toLowerCase() === commandExtension)) {
|
|
56
|
+
return ["", ...extensions];
|
|
57
|
+
}
|
|
58
|
+
return extensions;
|
|
59
|
+
}
|
|
60
|
+
function* candidatePaths(command, { cwd, searchPath, pathExt }) {
|
|
61
|
+
const extensions = isWindows ? windowsExtensions(command, pathExt) : [""];
|
|
62
|
+
if (separatorPattern.test(command)) {
|
|
63
|
+
const base = path.resolve(cwd, command);
|
|
64
|
+
for (const extension of extensions) {
|
|
65
|
+
yield base + extension;
|
|
66
|
+
}
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const directories = [
|
|
70
|
+
// Windows searches the current directory before `PATH`.
|
|
71
|
+
...isWindows ? [cwd] : [],
|
|
72
|
+
...searchPath.split(path.delimiter)
|
|
73
|
+
];
|
|
74
|
+
for (const directory of directories) {
|
|
75
|
+
const unquoted = isWindows && directory.length > 1 && directory.startsWith('"') && directory.endsWith('"') ? directory.slice(1, -1) : directory;
|
|
76
|
+
if (unquoted === "") {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
const base = path.resolve(cwd, unquoted, command);
|
|
80
|
+
for (const extension of extensions) {
|
|
81
|
+
yield base + extension;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function isExecutableSync(filePath) {
|
|
86
|
+
let stats;
|
|
87
|
+
try {
|
|
88
|
+
stats = fs.statSync(filePath);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
return isWindows && error.code === "EACCES";
|
|
91
|
+
}
|
|
92
|
+
if (!stats.isFile()) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
if (isWindows) {
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
fs.accessSync(filePath, fs.constants.X_OK);
|
|
100
|
+
return true;
|
|
101
|
+
} catch {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function whichCommandSync(command, options = {}) {
|
|
106
|
+
const resolved = resolveOptions(command, options);
|
|
107
|
+
for (const candidate of candidatePaths(command, resolved)) {
|
|
108
|
+
if (isExecutableSync(candidate)) {
|
|
109
|
+
return candidate;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return void 0;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ../process-utils/src/host-toolchain.ts
|
|
116
|
+
var NETWORK_KEYS = [
|
|
117
|
+
"HTTP_PROXY",
|
|
118
|
+
"HTTPS_PROXY",
|
|
119
|
+
"NO_PROXY",
|
|
120
|
+
"http_proxy",
|
|
121
|
+
"https_proxy",
|
|
122
|
+
"no_proxy",
|
|
123
|
+
"NODE_EXTRA_CA_CERTS",
|
|
124
|
+
"SSL_CERT_FILE",
|
|
125
|
+
"SSL_CERT_DIR"
|
|
126
|
+
];
|
|
127
|
+
var SHELL_START = "__BB_SHELL_ENV_START__";
|
|
128
|
+
var SHELL_END = "__BB_SHELL_ENV_END__";
|
|
129
|
+
var PROBE_TIMEOUT_MS = 3e3;
|
|
130
|
+
var PROBE_OUTPUT_MAX_CHARS = 1024 * 1024;
|
|
131
|
+
function executable(file) {
|
|
132
|
+
try {
|
|
133
|
+
accessSync(
|
|
134
|
+
file,
|
|
135
|
+
process.platform === "win32" ? constants.F_OK : constants.X_OK
|
|
136
|
+
);
|
|
137
|
+
return statSync(file).isFile();
|
|
138
|
+
} catch {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function envValue(env, key) {
|
|
143
|
+
if (process.platform !== "win32") return env[key];
|
|
144
|
+
return env[Object.keys(env).find(
|
|
145
|
+
(entry) => entry.toUpperCase() === key.toUpperCase()
|
|
146
|
+
) ?? key];
|
|
147
|
+
}
|
|
148
|
+
function mergePath(...paths) {
|
|
149
|
+
const seen = /* @__PURE__ */ new Set();
|
|
150
|
+
return paths.flatMap((value) => (value ?? "").split(delimiter)).filter((entry) => {
|
|
151
|
+
if (!entry) return false;
|
|
152
|
+
const key = process.platform === "win32" ? entry.replace(/[\\/]+$/u, "").toLowerCase() : entry;
|
|
153
|
+
if (seen.has(key)) return false;
|
|
154
|
+
seen.add(key);
|
|
155
|
+
return true;
|
|
156
|
+
}).join(delimiter);
|
|
157
|
+
}
|
|
158
|
+
function onDiskName(file) {
|
|
159
|
+
if (process.platform !== "win32") return file;
|
|
160
|
+
try {
|
|
161
|
+
const name = basename(file).toLowerCase();
|
|
162
|
+
const entry = readdirSync(dirname(file)).find(
|
|
163
|
+
(candidate) => candidate.toLowerCase() === name
|
|
164
|
+
);
|
|
165
|
+
return entry === void 0 ? file : join(dirname(file), entry);
|
|
166
|
+
} catch {
|
|
167
|
+
return file;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function runProbe(command, args, env) {
|
|
171
|
+
return new Promise((resolve) => {
|
|
172
|
+
let stdout = "";
|
|
173
|
+
let settled = false;
|
|
174
|
+
let timer;
|
|
175
|
+
const finish = (status) => {
|
|
176
|
+
if (settled) return;
|
|
177
|
+
settled = true;
|
|
178
|
+
clearTimeout(timer);
|
|
179
|
+
resolve({ status, stdout });
|
|
180
|
+
};
|
|
181
|
+
let child;
|
|
182
|
+
try {
|
|
183
|
+
child = spawn(command, args, {
|
|
184
|
+
cwd: homedir(),
|
|
185
|
+
env,
|
|
186
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
187
|
+
windowsHide: true
|
|
188
|
+
});
|
|
189
|
+
} catch {
|
|
190
|
+
finish(null);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
timer = setTimeout(() => {
|
|
194
|
+
child.kill("SIGKILL");
|
|
195
|
+
finish(null);
|
|
196
|
+
}, PROBE_TIMEOUT_MS);
|
|
197
|
+
child.stdout?.setEncoding("utf8");
|
|
198
|
+
child.stdout?.on("data", (chunk) => {
|
|
199
|
+
if (stdout.length < PROBE_OUTPUT_MAX_CHARS) stdout += chunk;
|
|
200
|
+
});
|
|
201
|
+
child.once("error", () => finish(null));
|
|
202
|
+
child.once("close", (code) => finish(code));
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
async function readShellEnvironment(env, retainPrevious) {
|
|
206
|
+
if (process.platform === "win32") {
|
|
207
|
+
const registry2 = join(
|
|
208
|
+
envValue(env, "SystemRoot") ?? "C:\\Windows",
|
|
209
|
+
"System32",
|
|
210
|
+
"reg.exe"
|
|
211
|
+
);
|
|
212
|
+
const values = { ...env };
|
|
213
|
+
const paths = [];
|
|
214
|
+
for (const key of [
|
|
215
|
+
"HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",
|
|
216
|
+
"HKCU\\Environment"
|
|
217
|
+
]) {
|
|
218
|
+
const result = await runProbe(registry2, ["query", key], env);
|
|
219
|
+
if (result.status !== 0) continue;
|
|
220
|
+
for (const match of result.stdout.matchAll(
|
|
221
|
+
/^ {4}(.+?) {4}REG_[A-Z_]+ {4}(.*)$/gmu
|
|
222
|
+
)) {
|
|
223
|
+
const name = match[1]?.trim();
|
|
224
|
+
if (!name) continue;
|
|
225
|
+
const value = (match[2] ?? "").trim();
|
|
226
|
+
if (name.toUpperCase() === "PATH") paths.push(value);
|
|
227
|
+
else {
|
|
228
|
+
const existing = Object.keys(values).find(
|
|
229
|
+
(key2) => key2.toUpperCase() === name.toUpperCase()
|
|
230
|
+
);
|
|
231
|
+
values[existing ?? name] = value;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const expand = (value) => {
|
|
236
|
+
for (let depth = 0; depth < 10; depth += 1) {
|
|
237
|
+
const expanded = value.replace(
|
|
238
|
+
/%([^%;\\/]+)%/gu,
|
|
239
|
+
(reference, variable) => envValue(values, variable) ?? reference
|
|
240
|
+
);
|
|
241
|
+
if (expanded === value) break;
|
|
242
|
+
value = expanded;
|
|
243
|
+
}
|
|
244
|
+
return value;
|
|
245
|
+
};
|
|
246
|
+
return paths.length === 0 ? null : { PATH: mergePath(...paths.map(expand), envValue(env, "PATH")) };
|
|
247
|
+
}
|
|
248
|
+
const shell = env.SHELL?.trim() || (process.platform === "darwin" ? "/bin/zsh" : "/bin/sh");
|
|
249
|
+
const command = `printf '%s\\n' ${SHELL_START}; /usr/bin/env; printf '%s\\n' ${SHELL_END}`;
|
|
250
|
+
const shellArgs = ["sh", "dash"].includes(basename(shell)) ? ["-lc"] : ["-ilc", "-lc"];
|
|
251
|
+
for (const arg of shellArgs) {
|
|
252
|
+
const result = await runProbe(shell, [arg, command], env);
|
|
253
|
+
if (result.status !== 0) {
|
|
254
|
+
if (retainPrevious) return null;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
const start = result.stdout.indexOf(`${SHELL_START}
|
|
258
|
+
`);
|
|
259
|
+
const end = result.stdout.indexOf(`
|
|
260
|
+
${SHELL_END}`, start);
|
|
261
|
+
if (start < 0 || end < 0) {
|
|
262
|
+
if (retainPrevious) return null;
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
const values = {};
|
|
266
|
+
for (const line of result.stdout.slice(start + SHELL_START.length + 1, end).split("\n")) {
|
|
267
|
+
const separator = line.indexOf("=");
|
|
268
|
+
const key = line.slice(0, separator);
|
|
269
|
+
if (key === "PATH" || NETWORK_KEYS.includes(key))
|
|
270
|
+
values[key] = line.slice(separator + 1);
|
|
271
|
+
}
|
|
272
|
+
if (values.PATH) return values;
|
|
273
|
+
}
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
var HostToolchain = class {
|
|
277
|
+
constructor(baseEnv = process.env, runtimePath = process.execPath) {
|
|
278
|
+
this.baseEnv = baseEnv;
|
|
279
|
+
this.runtimePath = runtimePath;
|
|
280
|
+
this.inheritedPath = envValue(baseEnv, "PATH");
|
|
281
|
+
this.nodePath = runtimePath;
|
|
282
|
+
}
|
|
283
|
+
baseEnv;
|
|
284
|
+
runtimePath;
|
|
285
|
+
shellEnv = {};
|
|
286
|
+
inheritedPath;
|
|
287
|
+
misses = /* @__PURE__ */ new Set();
|
|
288
|
+
nodePath;
|
|
289
|
+
refreshing = null;
|
|
290
|
+
refreshedAt = 0;
|
|
291
|
+
refresh() {
|
|
292
|
+
if (this.refreshing !== null) return this.refreshing;
|
|
293
|
+
this.misses.clear();
|
|
294
|
+
this.refreshedAt = Date.now();
|
|
295
|
+
const retainPrevious = this.shellEnv.PATH !== void 0;
|
|
296
|
+
this.refreshing = readShellEnvironment(this.baseEnv, retainPrevious).then((env) => {
|
|
297
|
+
if (env !== null) this.shellEnv = env;
|
|
298
|
+
}).finally(() => {
|
|
299
|
+
this.refreshing = null;
|
|
300
|
+
});
|
|
301
|
+
return this.refreshing;
|
|
302
|
+
}
|
|
303
|
+
invalidateFailure(errorOrCode) {
|
|
304
|
+
const code = typeof errorOrCode === "object" && errorOrCode !== null && "code" in errorOrCode ? errorOrCode.code : errorOrCode;
|
|
305
|
+
if (code === "ENOENT" || code === 127) void this.refresh();
|
|
306
|
+
}
|
|
307
|
+
searchPath(env) {
|
|
308
|
+
const inherited = envValue(env, "PATH");
|
|
309
|
+
const home = envValue(env, process.platform === "win32" ? "USERPROFILE" : "HOME") ?? homedir();
|
|
310
|
+
const fallbacks = [
|
|
311
|
+
join(home, ".local", "bin"),
|
|
312
|
+
join(home, ".local", "share", "mise", "shims"),
|
|
313
|
+
join(home, ".asdf", "shims"),
|
|
314
|
+
join(home, ".asdf", "bin"),
|
|
315
|
+
join(home, ".volta", "bin")
|
|
316
|
+
];
|
|
317
|
+
if (process.platform === "darwin") fallbacks.push("/opt/homebrew/bin");
|
|
318
|
+
if (process.platform !== "win32")
|
|
319
|
+
fallbacks.push("/usr/local/bin", "/usr/bin", "/bin");
|
|
320
|
+
const [first, second] = inherited === void 0 || inherited === this.inheritedPath ? [this.shellEnv.PATH, inherited] : [inherited, this.shellEnv.PATH];
|
|
321
|
+
return mergePath(
|
|
322
|
+
first,
|
|
323
|
+
second,
|
|
324
|
+
executable(this.nodePath) && /^node(?:\.exe)?$/iu.test(basename(this.nodePath)) ? dirname(this.nodePath) : void 0,
|
|
325
|
+
...fallbacks
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
environment(env = this.baseEnv) {
|
|
329
|
+
const path2 = this.searchPath(env);
|
|
330
|
+
const result = { ...env };
|
|
331
|
+
if (process.platform === "win32") {
|
|
332
|
+
for (const key of Object.keys(result))
|
|
333
|
+
if (key !== "PATH" && key.toUpperCase() === "PATH") delete result[key];
|
|
334
|
+
}
|
|
335
|
+
if (path2 !== "") result.PATH = path2;
|
|
336
|
+
const callerKeys = new Set(
|
|
337
|
+
Object.keys(env).filter((key) => env[key] !== void 0).map((key) => key.toUpperCase())
|
|
338
|
+
);
|
|
339
|
+
for (const key of NETWORK_KEYS)
|
|
340
|
+
if (!callerKeys.has(key.toUpperCase()) && this.shellEnv[key] !== void 0)
|
|
341
|
+
result[key] = this.shellEnv[key];
|
|
342
|
+
return result;
|
|
343
|
+
}
|
|
344
|
+
lookup(command, options) {
|
|
345
|
+
const env = this.environment(options.env);
|
|
346
|
+
const cwd = options.cwd instanceof URL ? fileURLToPath(options.cwd) : options.cwd ?? process.cwd();
|
|
347
|
+
if (command === "bb") {
|
|
348
|
+
for (const candidate of [
|
|
349
|
+
env.BB_CLI_LAUNCHER,
|
|
350
|
+
env.BB_CLI,
|
|
351
|
+
env.BB_CLI_DIR ? join(env.BB_CLI_DIR, "bb") : void 0
|
|
352
|
+
]) {
|
|
353
|
+
if (candidate?.trim() && isAbsolute(candidate.trim()) && executable(candidate.trim()))
|
|
354
|
+
return candidate.trim();
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
const extension = extname(command);
|
|
358
|
+
const pathExt = envValue(env, "PATHEXT");
|
|
359
|
+
const path2 = envValue(env, "PATH") ?? "";
|
|
360
|
+
const lookupOptions = {
|
|
361
|
+
cwd,
|
|
362
|
+
path: path2,
|
|
363
|
+
pathExt: extension ? `${extension};${pathExt ?? ".EXE;.COM;.CMD;.BAT"}` : pathExt
|
|
364
|
+
};
|
|
365
|
+
const found = command === "bb" ? path2.split(delimiter).filter(isAbsolute).map(
|
|
366
|
+
(directory) => whichCommandSync(join(directory, command), lookupOptions)
|
|
367
|
+
).find(Boolean) : whichCommandSync(command, lookupOptions);
|
|
368
|
+
return found ? onDiskName(found) : null;
|
|
369
|
+
}
|
|
370
|
+
resolve(command, options = {}) {
|
|
371
|
+
const found = this.lookup(command, options);
|
|
372
|
+
if (found !== null) return found;
|
|
373
|
+
const key = `${command}\0${String(options.cwd ?? "")}\0${envValue(options.env ?? this.baseEnv, "PATH") ?? ""}`;
|
|
374
|
+
const sinceRefresh = Date.now() - this.refreshedAt;
|
|
375
|
+
if (!(this.misses.has(key) && sinceRefresh < 3e4) && (this.misses.size === 0 || sinceRefresh >= 1e3))
|
|
376
|
+
void this.refresh();
|
|
377
|
+
this.misses.add(key);
|
|
378
|
+
return null;
|
|
379
|
+
}
|
|
380
|
+
node(options = {}) {
|
|
381
|
+
if (executable(this.nodePath)) return this.nodePath;
|
|
382
|
+
const stableHomebrew = this.runtimePath.replace(
|
|
383
|
+
/\/Cellar\/([^/]+)\/[^/]+\//u,
|
|
384
|
+
"/opt/$1/"
|
|
385
|
+
);
|
|
386
|
+
const candidates = [
|
|
387
|
+
stableHomebrew,
|
|
388
|
+
process.argv0,
|
|
389
|
+
this.lookup(process.platform === "win32" ? "node.exe" : "node", options)
|
|
390
|
+
];
|
|
391
|
+
for (const candidate of candidates) {
|
|
392
|
+
if (!candidate || !isAbsolute(candidate) || !executable(candidate))
|
|
393
|
+
continue;
|
|
394
|
+
const probe = spawnSync(
|
|
395
|
+
candidate,
|
|
396
|
+
[
|
|
397
|
+
"-p",
|
|
398
|
+
"JSON.stringify({path:process.execPath,abi:process.versions.modules,electron:!!process.versions.electron})"
|
|
399
|
+
],
|
|
400
|
+
{
|
|
401
|
+
env: this.environment(options.env),
|
|
402
|
+
cwd: options.cwd,
|
|
403
|
+
encoding: "utf8",
|
|
404
|
+
timeout: PROBE_TIMEOUT_MS,
|
|
405
|
+
killSignal: "SIGKILL",
|
|
406
|
+
windowsHide: true
|
|
407
|
+
}
|
|
408
|
+
);
|
|
409
|
+
if (probe.status !== 0) continue;
|
|
410
|
+
try {
|
|
411
|
+
const runtime = JSON.parse(probe.stdout.trim());
|
|
412
|
+
if (typeof runtime !== "object" || runtime === null || !("path" in runtime) || typeof runtime.path !== "string" || !("abi" in runtime) || runtime.abi !== process.versions.modules || !("electron" in runtime) || runtime.electron !== Boolean(process.versions.electron))
|
|
413
|
+
continue;
|
|
414
|
+
if (isAbsolute(runtime.path) && executable(runtime.path)) {
|
|
415
|
+
this.nodePath = runtime.path;
|
|
416
|
+
return runtime.path;
|
|
417
|
+
}
|
|
418
|
+
} catch {
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
throw Object.assign(
|
|
422
|
+
new Error(
|
|
423
|
+
"The Node runtime was removed and no replacement was found. Reinstall Node or restart bb with an available runtime."
|
|
424
|
+
),
|
|
425
|
+
{ code: "ENOENT" }
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
bundledNpmCli(command = "npm") {
|
|
429
|
+
const require2 = createRequire(import.meta.url);
|
|
430
|
+
return join(
|
|
431
|
+
dirname(require2.resolve("npm/package.json")),
|
|
432
|
+
"bin",
|
|
433
|
+
`${command}-cli.js`
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
npmCli(command = "npm", options = {}) {
|
|
437
|
+
const bin = `${command}-cli.js`;
|
|
438
|
+
const runtime = process.versions.electron ? null : this.node(options);
|
|
439
|
+
const candidates = [];
|
|
440
|
+
if (runtime !== null)
|
|
441
|
+
for (const cli of [
|
|
442
|
+
join(dirname(runtime), "..", "lib", "node_modules", "npm", "bin", bin),
|
|
443
|
+
join(dirname(runtime), "node_modules", "npm", "bin", bin)
|
|
444
|
+
])
|
|
445
|
+
candidates.push({ node: runtime, cli });
|
|
446
|
+
const installed = this.resolve(command, options);
|
|
447
|
+
if (installed) {
|
|
448
|
+
try {
|
|
449
|
+
const cli = realpathSync(installed);
|
|
450
|
+
const npmRoot = dirname(dirname(cli));
|
|
451
|
+
const node = [
|
|
452
|
+
join(npmRoot, "..", "..", "..", "bin", "node"),
|
|
453
|
+
join(npmRoot, "..", "..", "node.exe"),
|
|
454
|
+
join(dirname(installed), "node")
|
|
455
|
+
].find(executable);
|
|
456
|
+
if (basename(cli) === bin && node !== void 0)
|
|
457
|
+
candidates.push({ node: realpathSync(node), cli });
|
|
458
|
+
} catch {
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
if (runtime !== null) {
|
|
462
|
+
try {
|
|
463
|
+
candidates.push({ node: runtime, cli: this.bundledNpmCli(command) });
|
|
464
|
+
} catch {
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
return candidates.find((candidate) => existsSync(candidate.cli)) ?? null;
|
|
468
|
+
}
|
|
469
|
+
command(command, args = [], options = {}) {
|
|
470
|
+
const file = command === this.runtimePath ? this.node(options) : this.resolve(command, options) ?? command;
|
|
471
|
+
const env = this.environment(options.env);
|
|
472
|
+
if (/(?:^|[\\/])(?:npm|npx)-cli\.js$/u.test(args[0] ?? "")) {
|
|
473
|
+
env.PATH = mergePath(dirname(file), env.PATH);
|
|
474
|
+
}
|
|
475
|
+
return { command: file, args: [...args], env };
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
var toolchainKey = /* @__PURE__ */ Symbol.for("@bb/process-utils/host-toolchain");
|
|
479
|
+
var shared = globalThis;
|
|
480
|
+
var hostToolchain = shared[toolchainKey] ??= new HostToolchain();
|
|
481
|
+
|
|
482
|
+
// ../process-utils/src/host-process.ts
|
|
483
|
+
function observe(child) {
|
|
484
|
+
child.once("error", (error) => hostToolchain.invalidateFailure(error));
|
|
485
|
+
child.once("exit", (code) => hostToolchain.invalidateFailure(code));
|
|
486
|
+
return child;
|
|
487
|
+
}
|
|
488
|
+
function spawnHostProcess(command, args = [], options = {}) {
|
|
489
|
+
const launch = options.shell ? { command, args: [...args], env: hostToolchain.environment(options.env) } : hostToolchain.command(command, args, options);
|
|
490
|
+
const spawn2 = options.shell ? nodeSpawn : crossSpawn;
|
|
491
|
+
return observe(
|
|
492
|
+
spawn2(launch.command, launch.args, {
|
|
493
|
+
...options,
|
|
494
|
+
env: launch.env,
|
|
495
|
+
windowsHide: options.windowsHide ?? true
|
|
496
|
+
})
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
function forkHostProcess(modulePath, args = [], options = {}) {
|
|
500
|
+
const env = hostToolchain.environment(options.env);
|
|
501
|
+
const execPath = options.execPath === void 0 || options.execPath === process.execPath ? hostToolchain.node({ env, cwd: options.cwd }) : options.execPath;
|
|
502
|
+
return observe(nodeFork(modulePath, args, { ...options, env, execPath }));
|
|
503
|
+
}
|
|
504
|
+
function prepareExecFile(command, args, options) {
|
|
505
|
+
const launch = hostToolchain.command(command, args, options);
|
|
506
|
+
const parsed = process.platform === "win32" && hostToolchain.resolve(launch.command, {
|
|
507
|
+
env: launch.env,
|
|
508
|
+
cwd: options.cwd
|
|
509
|
+
}) === null ? { command: launch.command, args: launch.args, options } : parse(launch.command, launch.args, { ...options, env: launch.env });
|
|
510
|
+
return {
|
|
511
|
+
command: parsed.command,
|
|
512
|
+
args: parsed.args,
|
|
513
|
+
options: {
|
|
514
|
+
...options,
|
|
515
|
+
...parsed.options,
|
|
516
|
+
env: launch.env,
|
|
517
|
+
windowsHide: options.windowsHide ?? true
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
function execHostFileCallback(command, args, options, callback) {
|
|
522
|
+
const launch = prepareExecFile(command, args, options);
|
|
523
|
+
return observe(
|
|
524
|
+
nodeExecFile(
|
|
525
|
+
launch.command,
|
|
526
|
+
launch.args,
|
|
527
|
+
{ ...launch.options, encoding: options.encoding ?? "utf8" },
|
|
528
|
+
callback
|
|
529
|
+
)
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
function settleExecFile(start) {
|
|
533
|
+
let resolve;
|
|
534
|
+
let reject;
|
|
535
|
+
const promise = new Promise(
|
|
536
|
+
(resolvePromise, rejectPromise) => {
|
|
537
|
+
resolve = resolvePromise;
|
|
538
|
+
reject = rejectPromise;
|
|
539
|
+
}
|
|
540
|
+
);
|
|
541
|
+
const child = start((error, stdout, stderr) => {
|
|
542
|
+
if (error) reject(Object.assign(error, { stdout, stderr }));
|
|
543
|
+
else resolve({ stdout, stderr });
|
|
544
|
+
});
|
|
545
|
+
return Object.assign(promise, { child });
|
|
546
|
+
}
|
|
547
|
+
function execHostFile(command, args = [], options = {}) {
|
|
548
|
+
return settleExecFile(
|
|
549
|
+
(callback) => execHostFileCallback(command, args, options, callback)
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
// ../process-utils/src/vcs-process-scheduler.ts
|
|
554
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
555
|
+
import { availableParallelism } from "node:os";
|
|
556
|
+
var ranks = { interactive: 0, status: 1, background: 2 };
|
|
557
|
+
function localProcessCapacities(parallelism) {
|
|
558
|
+
const total = Math.min(6, Math.max(4, parallelism - 2));
|
|
559
|
+
return [total, total - 1, total - 2];
|
|
560
|
+
}
|
|
561
|
+
var capacities = {
|
|
562
|
+
local: localProcessCapacities(availableParallelism()),
|
|
563
|
+
network: [4, 3, 2]
|
|
564
|
+
};
|
|
565
|
+
var githubCapacity = 2;
|
|
566
|
+
var agingMs = 15e3;
|
|
567
|
+
var networkGitSubcommands = /* @__PURE__ */ new Set([
|
|
568
|
+
"fetch",
|
|
569
|
+
"pull",
|
|
570
|
+
"push",
|
|
571
|
+
"clone",
|
|
572
|
+
"ls-remote"
|
|
573
|
+
]);
|
|
574
|
+
var gitGlobalOptionsWithValue = /* @__PURE__ */ new Set([
|
|
575
|
+
"-C",
|
|
576
|
+
"-c",
|
|
577
|
+
"--git-dir",
|
|
578
|
+
"--work-tree",
|
|
579
|
+
"--namespace",
|
|
580
|
+
"--config-env"
|
|
581
|
+
]);
|
|
582
|
+
function isNetworkGitCommand(args) {
|
|
583
|
+
let index = 0;
|
|
584
|
+
while (index < args.length && args[index].startsWith("-")) {
|
|
585
|
+
index += gitGlobalOptionsWithValue.has(args[index]) ? 2 : 1;
|
|
586
|
+
}
|
|
587
|
+
const subcommand = args[index];
|
|
588
|
+
if (subcommand === void 0) return false;
|
|
589
|
+
if (networkGitSubcommands.has(subcommand)) return true;
|
|
590
|
+
const action = args.slice(index + 1).find((arg) => !arg.startsWith("-"));
|
|
591
|
+
return (subcommand === "submodule" || subcommand === "remote") && action === "update";
|
|
592
|
+
}
|
|
593
|
+
var VcsProcessScheduler = class {
|
|
594
|
+
priority = new AsyncLocalStorage();
|
|
595
|
+
admitted = new AsyncLocalStorage();
|
|
596
|
+
active = { local: 0, network: 0 };
|
|
597
|
+
githubActive = 0;
|
|
598
|
+
queue = [];
|
|
599
|
+
timer;
|
|
600
|
+
acquire(command, args, options) {
|
|
601
|
+
const pool = command === "gh" || isNetworkGitCommand(args) ? "network" : "local";
|
|
602
|
+
const slots = options.slots ?? 1;
|
|
603
|
+
if (!Number.isInteger(slots) || slots < 1 || slots > capacities[pool][ranks.background]) {
|
|
604
|
+
return Promise.reject(new Error("Invalid VCS process slot count"));
|
|
605
|
+
}
|
|
606
|
+
if (options.signal?.aborted) {
|
|
607
|
+
return Promise.reject(this.abortError(options.signal));
|
|
608
|
+
}
|
|
609
|
+
return new Promise((grant, reject) => {
|
|
610
|
+
const waiter = {
|
|
611
|
+
command,
|
|
612
|
+
pool,
|
|
613
|
+
priority: options.priority ?? this.priority.getStore() ?? "status",
|
|
614
|
+
slots,
|
|
615
|
+
queuedAt: performance.now(),
|
|
616
|
+
signal: options.signal,
|
|
617
|
+
grant,
|
|
618
|
+
onAbort: () => {
|
|
619
|
+
const index = this.queue.indexOf(waiter);
|
|
620
|
+
if (index === -1) return;
|
|
621
|
+
this.queue.splice(index, 1);
|
|
622
|
+
reject(this.abortError(options.signal));
|
|
623
|
+
this.drain();
|
|
624
|
+
}
|
|
625
|
+
};
|
|
626
|
+
this.queue.push(waiter);
|
|
627
|
+
options.signal?.addEventListener("abort", waiter.onAbort, { once: true });
|
|
628
|
+
this.drain();
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
abortError(signal) {
|
|
632
|
+
return Object.assign(
|
|
633
|
+
new Error("The operation was aborted", { cause: signal?.reason }),
|
|
634
|
+
{
|
|
635
|
+
name: "AbortError",
|
|
636
|
+
code: "ABORT_ERR"
|
|
637
|
+
}
|
|
638
|
+
);
|
|
639
|
+
}
|
|
640
|
+
rank(waiter) {
|
|
641
|
+
return Math.max(
|
|
642
|
+
0,
|
|
643
|
+
ranks[waiter.priority] - Math.floor((performance.now() - waiter.queuedAt) / agingMs)
|
|
644
|
+
);
|
|
645
|
+
}
|
|
646
|
+
drain() {
|
|
647
|
+
clearTimeout(this.timer);
|
|
648
|
+
this.queue.sort(
|
|
649
|
+
(a, b) => this.rank(a) - this.rank(b) || a.queuedAt - b.queuedAt
|
|
650
|
+
);
|
|
651
|
+
const blocked = /* @__PURE__ */ new Set();
|
|
652
|
+
for (let index = 0; index < this.queue.length; ) {
|
|
653
|
+
const waiter = this.queue[index];
|
|
654
|
+
if (blocked.has(waiter.pool) || waiter.command === "gh" && this.githubActive + waiter.slots > githubCapacity) {
|
|
655
|
+
index += 1;
|
|
656
|
+
continue;
|
|
657
|
+
}
|
|
658
|
+
if (this.active[waiter.pool] + waiter.slots > capacities[waiter.pool][this.rank(waiter)]) {
|
|
659
|
+
blocked.add(waiter.pool);
|
|
660
|
+
index += 1;
|
|
661
|
+
continue;
|
|
662
|
+
}
|
|
663
|
+
this.queue.splice(index, 1);
|
|
664
|
+
waiter.signal?.removeEventListener("abort", waiter.onAbort);
|
|
665
|
+
this.active[waiter.pool] += waiter.slots;
|
|
666
|
+
if (waiter.command === "gh") this.githubActive += waiter.slots;
|
|
667
|
+
let released = false;
|
|
668
|
+
waiter.grant(() => {
|
|
669
|
+
if (released) return;
|
|
670
|
+
released = true;
|
|
671
|
+
this.active[waiter.pool] -= waiter.slots;
|
|
672
|
+
if (waiter.command === "gh") this.githubActive -= waiter.slots;
|
|
673
|
+
this.drain();
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
if (this.queue.length > 0) {
|
|
677
|
+
const delay = Math.min(
|
|
678
|
+
...this.queue.map(
|
|
679
|
+
(waiter) => agingMs - (performance.now() - waiter.queuedAt) % agingMs
|
|
680
|
+
)
|
|
681
|
+
);
|
|
682
|
+
this.timer = setTimeout(() => this.drain(), delay);
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
};
|
|
686
|
+
var registry = globalThis;
|
|
687
|
+
var scheduler = registry.__bbVcsProcessScheduler ??= new VcsProcessScheduler();
|
|
688
|
+
export {
|
|
689
|
+
execHostFile as experimental_execHostFile,
|
|
690
|
+
execHostFileCallback as experimental_execHostFileCallback,
|
|
691
|
+
forkHostProcess as experimental_forkHostProcess,
|
|
692
|
+
hostToolchain as experimental_hostToolchain,
|
|
693
|
+
spawnHostProcess as experimental_spawnHostProcess
|
|
694
|
+
};
|