@vite-hub/sandbox 0.0.4 → 0.3.0
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 +395 -37
- package/dist/abortable-CR01hzfi.js +57 -0
- package/dist/cloudflare-Cog_r2Me.js +910 -0
- package/dist/cloudflare-env-lookup-DzI6Xv1Y.js +16 -0
- package/dist/cloudflare-failure-CFQhalfh.js +50 -0
- package/dist/container-CqfD8yuE.js +868 -0
- package/dist/crabbox-sbV3Zjuo.js +1110 -0
- package/dist/error-diagnostics-5QT_BjbQ.js +354 -0
- package/dist/error-diagnostics-BJtLRL9w.js +536 -0
- package/dist/file-listing-CHtRrfo1.js +26 -0
- package/dist/file-lock-D0-XPogS.js +74 -0
- package/dist/index-DsoQAUb7.d.ts +594 -0
- package/dist/index.d.ts +2 -69
- package/dist/index.js +281 -1182
- package/dist/internal/cloudflare.d.ts +176 -0
- package/dist/internal/cloudflare.js +8 -0
- package/dist/internal/host-toolchain.d.ts +22 -0
- package/dist/internal/host-toolchain.js +96 -0
- package/dist/internal/vercel.d.ts +4 -0
- package/dist/internal/vercel.js +8 -0
- package/dist/invariant-DolT9EEx.js +22 -0
- package/dist/module-types-B0LQ5H2S.js +7 -0
- package/dist/module-types-DCeJbS9Z.d.ts +110 -0
- package/dist/process-sweep-CMaujWE5.js +33 -0
- package/dist/provider-resolution-C6AQzuFG.js +133 -0
- package/dist/remote-70ZVZZlq.js +265 -0
- package/dist/rolldown-runtime-8H4AJuhK.js +14 -0
- package/dist/runtime-DHp6ImeP.js +32 -0
- package/dist/runtime-type-Deo4qzxN.js +9 -0
- package/dist/session-e0lyzjJ3.js +338 -0
- package/dist/shell-quote-Q3u3plif.js +6 -0
- package/dist/size-7AkVwGZb.js +118 -0
- package/dist/ssh.d.ts +63 -0
- package/dist/ssh.js +286 -0
- package/dist/task/index.d.ts +69 -0
- package/dist/task/index.js +1414 -0
- package/dist/task/runtime/empty-registry.d.ts +6 -0
- package/dist/{runtime → task/runtime}/empty-registry.js +2 -2
- package/dist/task/runtime/provider-loader.d.ts +18 -0
- package/dist/task/runtime/provider-loader.js +16 -0
- package/dist/task/runtime/providers/cloudflare.d.ts +14 -0
- package/dist/task/runtime/providers/cloudflare.js +27 -0
- package/dist/task/runtime/providers/vercel.d.ts +12 -0
- package/dist/task/runtime/providers/vercel.js +85 -0
- package/dist/task/runtime/state.d.ts +27 -0
- package/dist/task/runtime/state.js +82 -0
- package/dist/task/vite.d.ts +486 -0
- package/dist/task/vite.js +3966 -0
- package/dist/toolchain-BIVGMRty.js +528 -0
- package/dist/trusted-host-kxkpaQcs.js +714 -0
- package/dist/vercel-BGGrNpza.js +352 -0
- package/package.json +105 -51
- package/dist/cloudflare-env-QQV4uzCT.js +0 -14
- package/dist/module-types-DKYItiOu.d.ts +0 -93
- package/dist/module-types-Dj1MFmHT.js +0 -7
- package/dist/provider-loader-D0UihePq.d.ts +0 -21
- package/dist/runtime/empty-registry.d.ts +0 -6
- package/dist/runtime/provider-loader.d.ts +0 -2
- package/dist/runtime/provider-loader.js +0 -15
- package/dist/runtime/providers/cloudflare.d.ts +0 -29
- package/dist/runtime/providers/cloudflare.js +0 -25
- package/dist/runtime/providers/vercel.d.ts +0 -121
- package/dist/runtime/providers/vercel.js +0 -51
- package/dist/runtime/state.d.ts +0 -26
- package/dist/runtime/state.js +0 -72
- package/dist/vite.d.ts +0 -48
- package/dist/vite.js +0 -3324
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { t as sandboxErrorDiagnostics } from "./error-diagnostics-BJtLRL9w.js";
|
|
2
|
+
import { t as shellQuote } from "./shell-quote-Q3u3plif.js";
|
|
3
|
+
import { a as sandboxRequirementSecrets, i as sandboxRequirementPlan, o as sandboxRequirementSignal, r as sandboxRequirementError, s as materializeGitCheckout, t as createSandboxSession } from "./session-e0lyzjJ3.js";
|
|
4
|
+
import { normalizeExecutionAuthority } from "@vite-hub/runtime";
|
|
5
|
+
//#region src/internal/remote.ts
|
|
6
|
+
async function resolveRemoteSandboxRuntime(runtime, requirements) {
|
|
7
|
+
const resolvedRequirements = requirements.map((command) => ({
|
|
8
|
+
args: [],
|
|
9
|
+
command,
|
|
10
|
+
name: command
|
|
11
|
+
}));
|
|
12
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(JSON.stringify(resolvedRequirements)));
|
|
13
|
+
const input = {
|
|
14
|
+
identity: Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join(""),
|
|
15
|
+
plan: {
|
|
16
|
+
env: {},
|
|
17
|
+
files: {},
|
|
18
|
+
state: []
|
|
19
|
+
},
|
|
20
|
+
requirements: resolvedRequirements
|
|
21
|
+
};
|
|
22
|
+
const prepared = await runtime.prepare(input);
|
|
23
|
+
const plan = Object.freeze({
|
|
24
|
+
...prepared,
|
|
25
|
+
executionAuthority: normalizeExecutionAuthority(prepared.executionAuthority),
|
|
26
|
+
home: Object.freeze({ state: Object.freeze(prepared.home?.state || []) })
|
|
27
|
+
});
|
|
28
|
+
return Object.freeze({
|
|
29
|
+
async open(options) {
|
|
30
|
+
options?.signal?.throwIfAborted();
|
|
31
|
+
return await runtime.open(input, {
|
|
32
|
+
...options,
|
|
33
|
+
executionAuthority: plan.executionAuthority
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
plan
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function remoteSandboxPlan(input, options) {
|
|
40
|
+
assertRemoteInput(input, options.runtime);
|
|
41
|
+
return {
|
|
42
|
+
cache: { state: "disposable" },
|
|
43
|
+
environment: { env: {} },
|
|
44
|
+
executionAuthority: options.executionAuthority,
|
|
45
|
+
identity: input.identity,
|
|
46
|
+
requirements: sandboxRequirementPlan(input.requirements),
|
|
47
|
+
runtime: options.runtime,
|
|
48
|
+
workspace: {
|
|
49
|
+
state: "disposable",
|
|
50
|
+
workDir: "."
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
async function openRemoteSandbox(input, runtimeSession, options, environment) {
|
|
55
|
+
assertRemoteInput(input, options.runtime);
|
|
56
|
+
const session = createSandboxSession(runtimeSession, {
|
|
57
|
+
executionAuthority: options.executionAuthority,
|
|
58
|
+
initialize: options.initialize,
|
|
59
|
+
signal: options.signal
|
|
60
|
+
});
|
|
61
|
+
try {
|
|
62
|
+
await materializeRemotePlan(input, runtimeSession, options, environment);
|
|
63
|
+
await options.initialize?.(session, { signal: options.signal });
|
|
64
|
+
return session;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
try {
|
|
67
|
+
await session.close();
|
|
68
|
+
} catch (cleanupError) {
|
|
69
|
+
throw new AggregateError([error, cleanupError], `[vitehub] ${options.runtime} Sandbox initialization failed: ${remoteFailureMessage(error)} Cleanup failed: ${remoteFailureMessage(cleanupError)}`);
|
|
70
|
+
}
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function remoteFailureMessage(error) {
|
|
75
|
+
return error instanceof Error ? error.message || error.name : String(error);
|
|
76
|
+
}
|
|
77
|
+
async function resolveRemoteEnvironment(input, options) {
|
|
78
|
+
const home = options.home ?? "/home/vitehub";
|
|
79
|
+
const workspace = options.workspace ?? "/workspace";
|
|
80
|
+
const env = {
|
|
81
|
+
HOME: home,
|
|
82
|
+
INIT_CWD: workspace,
|
|
83
|
+
OLDPWD: workspace,
|
|
84
|
+
PWD: workspace,
|
|
85
|
+
XDG_CACHE_HOME: joinRemotePath(home, ".cache"),
|
|
86
|
+
XDG_CONFIG_HOME: joinRemotePath(home, ".config"),
|
|
87
|
+
XDG_STATE_HOME: joinRemotePath(home, ".local/state")
|
|
88
|
+
};
|
|
89
|
+
for (const [name, resolveValue] of Object.entries(input.plan.env)) env[name] = await resolveValue();
|
|
90
|
+
return env;
|
|
91
|
+
}
|
|
92
|
+
async function materializeRemotePlan(input, session, options, environment) {
|
|
93
|
+
const home = options.home ?? "/home/vitehub";
|
|
94
|
+
const workspace = options.workspace ?? "/workspace";
|
|
95
|
+
const abortSignal = options.signal;
|
|
96
|
+
const diagnosticSecrets = [...sandboxRequirementSecrets(Object.keys(input.plan.env).map((name) => environment[name]))];
|
|
97
|
+
for (const path of /* @__PURE__ */ new Set([...options.preserveHome ? [] : [home], ...options.preserveWorkspace ? [] : [workspace]])) if (await session.existsFile({
|
|
98
|
+
abortSignal,
|
|
99
|
+
path
|
|
100
|
+
})) await session.removeFile({
|
|
101
|
+
abortSignal,
|
|
102
|
+
path,
|
|
103
|
+
recursive: true
|
|
104
|
+
});
|
|
105
|
+
await session.makeDirectory({
|
|
106
|
+
abortSignal,
|
|
107
|
+
path: home,
|
|
108
|
+
recursive: true
|
|
109
|
+
});
|
|
110
|
+
await session.makeDirectory({
|
|
111
|
+
abortSignal,
|
|
112
|
+
path: workspace,
|
|
113
|
+
recursive: true
|
|
114
|
+
});
|
|
115
|
+
const entries = Object.entries(input.plan.files);
|
|
116
|
+
const directories = /* @__PURE__ */ new Set([home, workspace]);
|
|
117
|
+
const fileConcurrency = 8;
|
|
118
|
+
for (let offset = 0; offset < entries.length; offset += fileConcurrency) {
|
|
119
|
+
const files = [];
|
|
120
|
+
for (const [path, file] of entries.slice(offset, offset + fileConcurrency)) {
|
|
121
|
+
abortSignal?.throwIfAborted();
|
|
122
|
+
const content = await file.resolve();
|
|
123
|
+
diagnosticSecrets.push(...sandboxRequirementSecrets([content]));
|
|
124
|
+
files.push({
|
|
125
|
+
content: Uint8Array.from(content),
|
|
126
|
+
path: joinRemotePath(home, path)
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
const missingDirectories = [...new Set(files.map((file) => dirnameRemotePath(file.path)))].filter((path) => !directories.has(path));
|
|
130
|
+
await settleFileOperations(missingDirectories.map((path) => session.makeDirectory({
|
|
131
|
+
abortSignal,
|
|
132
|
+
path,
|
|
133
|
+
recursive: true
|
|
134
|
+
})), abortSignal);
|
|
135
|
+
for (const path of missingDirectories) directories.add(path);
|
|
136
|
+
await settleFileOperations(files.map((file) => session.writeBinaryFile({
|
|
137
|
+
abortSignal,
|
|
138
|
+
...file
|
|
139
|
+
})), abortSignal);
|
|
140
|
+
}
|
|
141
|
+
if (input.checkout) await materializeGitCheckout(input.checkout, workspace, {
|
|
142
|
+
abortSignal,
|
|
143
|
+
async run(args) {
|
|
144
|
+
const result = await session.run({
|
|
145
|
+
abortSignal,
|
|
146
|
+
command: ["git", ...args.map(shellQuote)].join(" "),
|
|
147
|
+
workingDirectory: workspace
|
|
148
|
+
});
|
|
149
|
+
if (result.exitCode !== 0) throw sandboxErrorDiagnostics.SANDBOX_R0121({ message: result.stderr });
|
|
150
|
+
return { stdout: result.stdout };
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
if (input.toolchain) await provisionRemoteToolchain(input, session, {
|
|
154
|
+
abortSignal,
|
|
155
|
+
home,
|
|
156
|
+
workspace
|
|
157
|
+
}, environment);
|
|
158
|
+
await validateRequirements(session, input.requirements, workspace, abortSignal, diagnosticSecrets);
|
|
159
|
+
}
|
|
160
|
+
async function settleFileOperations(operations, signal) {
|
|
161
|
+
const results = await Promise.allSettled(operations);
|
|
162
|
+
signal?.throwIfAborted();
|
|
163
|
+
const failure = results.find((result) => result.status === "rejected");
|
|
164
|
+
if (failure?.status === "rejected") throw failure.reason;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Remote Sandboxes do not share a cache. Reuse an exact image node, otherwise upload
|
|
168
|
+
* the verified toolchain into the Sandbox Home cache for this session.
|
|
169
|
+
*/
|
|
170
|
+
async function provisionRemoteToolchain(input, session, options, environment) {
|
|
171
|
+
const { abortSignal, home, workspace } = options;
|
|
172
|
+
const { provisionToolchain, readProjectFilesWithShell, verifyToolchain } = await import("./toolchain-BIVGMRty.js").then((n) => n.i);
|
|
173
|
+
const run = async (script) => await session.run({
|
|
174
|
+
abortSignal,
|
|
175
|
+
command: `sh -c ${shellQuote(script)}`,
|
|
176
|
+
workingDirectory: workspace
|
|
177
|
+
});
|
|
178
|
+
const basePath = await run(`printf '%s' "$PATH"`);
|
|
179
|
+
if (basePath.exitCode !== 0) throw sandboxErrorDiagnostics.SANDBOX_R0154({ message: `[vitehub] Sandbox toolchain could not read the Sandbox PATH: ${basePath.stderr.trim()}` });
|
|
180
|
+
const cacheRoot = joinRemotePath(home, ".cache/vitehub/toolchains");
|
|
181
|
+
const toolchain = await provisionToolchain(input.toolchain, {
|
|
182
|
+
abortSignal,
|
|
183
|
+
cacheRoot,
|
|
184
|
+
readProjectFiles: async (paths) => await readProjectFilesWithShell(run, workspace, paths),
|
|
185
|
+
reuseExactNode: true,
|
|
186
|
+
run,
|
|
187
|
+
scratch: cacheRoot,
|
|
188
|
+
async write(path, content) {
|
|
189
|
+
await session.makeDirectory({
|
|
190
|
+
abortSignal,
|
|
191
|
+
path: dirnameRemotePath(path),
|
|
192
|
+
recursive: true
|
|
193
|
+
});
|
|
194
|
+
await session.writeBinaryFile({
|
|
195
|
+
abortSignal,
|
|
196
|
+
content,
|
|
197
|
+
path
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
environment.PATH = [...toolchain.bin, basePath.stdout].filter(Boolean).join(":");
|
|
202
|
+
await verifyToolchain(toolchain, cacheRoot, async (script) => await session.run({
|
|
203
|
+
abortSignal,
|
|
204
|
+
command: script,
|
|
205
|
+
workingDirectory: workspace
|
|
206
|
+
}));
|
|
207
|
+
session.toolchain = toolchain;
|
|
208
|
+
}
|
|
209
|
+
function joinRemotePath(...parts) {
|
|
210
|
+
return parts.join("/").replace(/\/{2,}/g, "/");
|
|
211
|
+
}
|
|
212
|
+
function dirnameRemotePath(path) {
|
|
213
|
+
const index = path.lastIndexOf("/");
|
|
214
|
+
return index <= 0 ? "/" : path.slice(0, index);
|
|
215
|
+
}
|
|
216
|
+
async function validateRequirements(session, requirements, workspace, abortSignal, secrets) {
|
|
217
|
+
for (const requirement of requirements) {
|
|
218
|
+
const command = [
|
|
219
|
+
"command -v",
|
|
220
|
+
shellQuote(requirement.command),
|
|
221
|
+
">/dev/null"
|
|
222
|
+
];
|
|
223
|
+
if (requirement.args.length) command.push("&&", shellQuote(requirement.command), ...requirement.args.map(shellQuote));
|
|
224
|
+
let result;
|
|
225
|
+
try {
|
|
226
|
+
result = await session.run({
|
|
227
|
+
abortSignal: sandboxRequirementSignal(requirement, abortSignal),
|
|
228
|
+
command: command.join(" "),
|
|
229
|
+
workingDirectory: workspace
|
|
230
|
+
});
|
|
231
|
+
} catch (cause) {
|
|
232
|
+
if (abortSignal?.aborted) throw abortSignal.reason;
|
|
233
|
+
throw sandboxRequirementError(requirement, cause, secrets);
|
|
234
|
+
}
|
|
235
|
+
if (result.exitCode !== 0) throw sandboxRequirementError(requirement, result, secrets);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function assertRemoteInput(input, runtime) {
|
|
239
|
+
if (input.cwd) throw sandboxErrorDiagnostics.SANDBOX_R0122({ message: `[vitehub] ${runtime} cannot mount a host Sandbox cwd; use Workspace instead.` });
|
|
240
|
+
if (input.plan.state.length) throw sandboxErrorDiagnostics.SANDBOX_R0123({ message: `[vitehub] ${runtime} does not provide durable Sandbox Home state.` });
|
|
241
|
+
}
|
|
242
|
+
async function collectProcessOutput(process) {
|
|
243
|
+
const [{ exitCode }, stdout, stderr] = await Promise.all([
|
|
244
|
+
process.wait(),
|
|
245
|
+
process.stdout(),
|
|
246
|
+
process.stderr()
|
|
247
|
+
]);
|
|
248
|
+
return {
|
|
249
|
+
exitCode,
|
|
250
|
+
stderr,
|
|
251
|
+
stdout
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
function snapshotStream(read) {
|
|
255
|
+
return new ReadableStream({ async start(controller) {
|
|
256
|
+
try {
|
|
257
|
+
controller.enqueue(new TextEncoder().encode(await read()));
|
|
258
|
+
controller.close();
|
|
259
|
+
} catch (error) {
|
|
260
|
+
controller.error(error);
|
|
261
|
+
}
|
|
262
|
+
} });
|
|
263
|
+
}
|
|
264
|
+
//#endregion
|
|
265
|
+
export { resolveRemoteSandboxRuntime as a, resolveRemoteEnvironment as i, openRemoteSandbox as n, snapshotStream as o, remoteSandboxPlan as r, collectProcessOutput as t };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __exportAll = (all, no_symbols) => {
|
|
5
|
+
let target = {};
|
|
6
|
+
for (var name in all) __defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: true
|
|
9
|
+
});
|
|
10
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
11
|
+
return target;
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
export { __exportAll as t };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//#region src/internal/runtime.ts
|
|
2
|
+
const builtInSandboxRuntime = Symbol.for("vitehub.sandbox.internal-runtime");
|
|
3
|
+
function isFunction(value) {
|
|
4
|
+
return typeof value === "function";
|
|
5
|
+
}
|
|
6
|
+
function isOrdinaryObjectPrototype(value) {
|
|
7
|
+
if (Object.getPrototypeOf(value) !== null || !Object.hasOwn(value, "constructor")) return false;
|
|
8
|
+
const constructor = Object.getOwnPropertyDescriptor(value, "constructor")?.value;
|
|
9
|
+
return isFunction(constructor) && Function.prototype.toString.call(constructor) === Function.prototype.toString.call(Object);
|
|
10
|
+
}
|
|
11
|
+
function isBuiltInSandboxRuntime(runtime) {
|
|
12
|
+
return Object.getOwnPropertyDescriptor(runtime, builtInSandboxRuntime)?.value === true;
|
|
13
|
+
}
|
|
14
|
+
function hasDeclaredSandboxRuntimeMember(value, key) {
|
|
15
|
+
if (Object.hasOwn(value, key)) return true;
|
|
16
|
+
let prototype = Object.getPrototypeOf(value);
|
|
17
|
+
while (prototype && prototype !== Object.prototype) {
|
|
18
|
+
if (isOrdinaryObjectPrototype(prototype)) return false;
|
|
19
|
+
if (Object.hasOwn(prototype, key)) {
|
|
20
|
+
const constructor = Object.getOwnPropertyDescriptor(prototype, "constructor")?.value;
|
|
21
|
+
return isFunction(constructor) && constructor.prototype === prototype && /^class\b/.test(Function.prototype.toString.call(constructor));
|
|
22
|
+
}
|
|
23
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
function markBuiltInSandboxRuntime(runtime) {
|
|
28
|
+
Object.defineProperty(runtime, builtInSandboxRuntime, { value: true });
|
|
29
|
+
return runtime;
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
export { isBuiltInSandboxRuntime as n, markBuiltInSandboxRuntime as r, hasDeclaredSandboxRuntimeMember as t };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/internal/runtime-type.ts
|
|
2
|
+
/** Returns a plain JSON-style object, or undefined for primitives, arrays, and functions. */
|
|
3
|
+
function runtimeRecord(value) {
|
|
4
|
+
if (value === null || Object(value) !== value || Array.isArray(value)) return;
|
|
5
|
+
if (Object.prototype.toString.call(value) !== "[object Object]") return;
|
|
6
|
+
return value;
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
export { runtimeRecord as t };
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import { t as sandboxErrorDiagnostics } from "./error-diagnostics-BJtLRL9w.js";
|
|
2
|
+
import { t as shellQuote } from "./shell-quote-Q3u3plif.js";
|
|
3
|
+
import { posix } from "node:path";
|
|
4
|
+
//#region src/internal/git-checkout.ts
|
|
5
|
+
async function materializeGitCheckout(checkout, workspace, runner) {
|
|
6
|
+
await run("initialize", [
|
|
7
|
+
"init",
|
|
8
|
+
"--quiet",
|
|
9
|
+
...checkout.sha.length === 64 ? ["--object-format=sha256"] : [],
|
|
10
|
+
workspace
|
|
11
|
+
]);
|
|
12
|
+
await run("configure remote", [
|
|
13
|
+
"-C",
|
|
14
|
+
workspace,
|
|
15
|
+
"remote",
|
|
16
|
+
"add",
|
|
17
|
+
"--",
|
|
18
|
+
"origin",
|
|
19
|
+
checkout.remote
|
|
20
|
+
]);
|
|
21
|
+
await run("fetch ref", [
|
|
22
|
+
"-C",
|
|
23
|
+
workspace,
|
|
24
|
+
"fetch",
|
|
25
|
+
"--quiet",
|
|
26
|
+
"--no-tags",
|
|
27
|
+
"--depth=100",
|
|
28
|
+
"origin",
|
|
29
|
+
"--",
|
|
30
|
+
checkout.ref
|
|
31
|
+
]);
|
|
32
|
+
const revision = (await run("resolve revision", [
|
|
33
|
+
"-C",
|
|
34
|
+
workspace,
|
|
35
|
+
"rev-parse",
|
|
36
|
+
"--verify",
|
|
37
|
+
"FETCH_HEAD^{commit}"
|
|
38
|
+
])).stdout.trim().toLowerCase();
|
|
39
|
+
if (revision !== checkout.sha) throw sandboxErrorDiagnostics.SANDBOX_R0119({ message: `[vitehub] Sandbox checkout revision mismatch: expected ${checkout.sha}, received ${revision || "<empty>"}.` });
|
|
40
|
+
await run("check out revision", [
|
|
41
|
+
"-C",
|
|
42
|
+
workspace,
|
|
43
|
+
"checkout",
|
|
44
|
+
"--quiet",
|
|
45
|
+
"--detach",
|
|
46
|
+
checkout.sha
|
|
47
|
+
]);
|
|
48
|
+
async function run(operation, args) {
|
|
49
|
+
runner.abortSignal?.throwIfAborted();
|
|
50
|
+
try {
|
|
51
|
+
const result = await runner.run(args);
|
|
52
|
+
runner.abortSignal?.throwIfAborted();
|
|
53
|
+
return result;
|
|
54
|
+
} catch {
|
|
55
|
+
runner.abortSignal?.throwIfAborted();
|
|
56
|
+
throw sandboxErrorDiagnostics.SANDBOX_R0120({ message: `[vitehub] Sandbox checkout failed to ${operation}.` });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/internal/requirements.ts
|
|
62
|
+
const maximumDiagnosticLength = 4e3;
|
|
63
|
+
function sandboxRequirementPlan(requirements) {
|
|
64
|
+
return requirements.map(({ command, name, timeout }) => ({
|
|
65
|
+
command,
|
|
66
|
+
name,
|
|
67
|
+
...timeout === void 0 ? {} : { timeout }
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
function sandboxRequirementSignal(requirement, signal) {
|
|
71
|
+
if (requirement.timeout === void 0) return signal;
|
|
72
|
+
const timeout = AbortSignal.timeout(requirement.timeout);
|
|
73
|
+
return signal ? AbortSignal.any([signal, timeout]) : timeout;
|
|
74
|
+
}
|
|
75
|
+
function sandboxRequirementSecrets(values) {
|
|
76
|
+
return values.map((value) => typeof value === "string" ? value : new TextDecoder().decode(value)).filter(Boolean);
|
|
77
|
+
}
|
|
78
|
+
async function collectSandboxRequirementOutput(stream, secrets) {
|
|
79
|
+
const reader = stream.getReader();
|
|
80
|
+
const decoder = new TextDecoder();
|
|
81
|
+
const patterns = [...new Set(secrets)].filter(Boolean).sort((left, right) => right.length - left.length);
|
|
82
|
+
const maximumSecretLength = Math.max(1, ...patterns.map((value) => value.length));
|
|
83
|
+
let pending = "";
|
|
84
|
+
let output = "";
|
|
85
|
+
let truncated = false;
|
|
86
|
+
const append = (value) => {
|
|
87
|
+
if (!value || truncated) return;
|
|
88
|
+
if (output.length + value.length <= maximumDiagnosticLength) {
|
|
89
|
+
output += value;
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
output = `${(output + value).slice(0, 3999)}…`;
|
|
93
|
+
truncated = true;
|
|
94
|
+
};
|
|
95
|
+
const flush = (final) => {
|
|
96
|
+
while (pending && (final || pending.length >= maximumSecretLength)) {
|
|
97
|
+
const secret = patterns.find((value) => pending.startsWith(value));
|
|
98
|
+
if (secret) {
|
|
99
|
+
append("[redacted]");
|
|
100
|
+
pending = pending.slice(secret.length);
|
|
101
|
+
} else {
|
|
102
|
+
append(pending[0]);
|
|
103
|
+
pending = pending.slice(1);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
try {
|
|
108
|
+
while (true) {
|
|
109
|
+
const { done, value } = await reader.read();
|
|
110
|
+
pending += decoder.decode(value, { stream: !done });
|
|
111
|
+
flush(done);
|
|
112
|
+
if (done) return output;
|
|
113
|
+
}
|
|
114
|
+
} finally {
|
|
115
|
+
reader.releaseLock();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function sandboxRequirementError(requirement, failure, secrets = [], timeout = requirement.timeout, includeDiagnosticOutput = true) {
|
|
119
|
+
const details = commandFailureDetails(failure, secrets, timeout, includeDiagnosticOutput);
|
|
120
|
+
const name = diagnosticText(requirement.name, secrets);
|
|
121
|
+
return sandboxErrorDiagnostics.SANDBOX_R0124({ message: `[vitehub] Sandbox requirement "${name}" failed${details ? `: ${details}` : "."}` });
|
|
122
|
+
}
|
|
123
|
+
function commandFailureDetails(failure, secrets, timeout, includeDiagnosticOutput) {
|
|
124
|
+
const error = failure && typeof failure === "object" ? failure : void 0;
|
|
125
|
+
const status = (error?.name === "TimeoutError" || error?.code === "ETIMEDOUT" || error?.killed === true) && timeout !== void 0 ? `timed out after ${timeout}ms` : typeof error?.exitCode === "number" ? `exit code ${error.exitCode}` : typeof error?.code === "number" ? `exit code ${error.code}` : typeof error?.signal === "string" ? `terminated by ${error.signal}` : error?.name === "AbortError" ? "aborted" : "";
|
|
126
|
+
const output = includeDiagnosticOutput ? diagnosticText(error?.stderr, secrets) || diagnosticText(error?.stdout, secrets) : "";
|
|
127
|
+
const message = includeDiagnosticOutput && !output ? diagnosticText(error?.message ?? failure, secrets) : "";
|
|
128
|
+
return [status, output || message].filter(Boolean).join(": ");
|
|
129
|
+
}
|
|
130
|
+
function diagnosticText(value, secrets) {
|
|
131
|
+
if (value === void 0 || value === null) return "";
|
|
132
|
+
const patterns = [...new Set(secrets)].filter(Boolean).sort((left, right) => right.length - left.length);
|
|
133
|
+
let text;
|
|
134
|
+
if (typeof value === "object") try {
|
|
135
|
+
text = JSON.stringify(value, (_key, nested) => typeof nested === "string" ? redactDiagnosticString(nested, patterns) : nested);
|
|
136
|
+
text = redactDiagnosticJson(text, patterns);
|
|
137
|
+
} catch {}
|
|
138
|
+
text ||= String(value);
|
|
139
|
+
text = redactDiagnosticString(text, patterns);
|
|
140
|
+
text = text.trim();
|
|
141
|
+
if (text.length > maximumDiagnosticLength) text = `${text.slice(0, 3999)}…`;
|
|
142
|
+
return text;
|
|
143
|
+
}
|
|
144
|
+
function redactDiagnosticString(value, patterns) {
|
|
145
|
+
for (const secret of patterns) value = value.replaceAll(secret, "[redacted]");
|
|
146
|
+
return value;
|
|
147
|
+
}
|
|
148
|
+
function redactDiagnosticJson(value, patterns) {
|
|
149
|
+
for (const secret of patterns) {
|
|
150
|
+
const encoded = JSON.stringify(secret).slice(1, -1);
|
|
151
|
+
value = value.replaceAll(encoded, "[redacted]");
|
|
152
|
+
}
|
|
153
|
+
return value;
|
|
154
|
+
}
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/internal/session.ts
|
|
157
|
+
function isExcludedSandboxPath(path, excluded) {
|
|
158
|
+
const normalized = posix.normalize(path);
|
|
159
|
+
return excluded.some((item) => {
|
|
160
|
+
const excludedPath = posix.normalize(item);
|
|
161
|
+
return normalized === excludedPath || excludedPath === "/" || normalized.startsWith(`${excludedPath}/`);
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
async function listSessionFiles(runtime, path, options = {}) {
|
|
165
|
+
const excluded = options.exclude || [];
|
|
166
|
+
if (isExcludedSandboxPath(path, excluded)) return [];
|
|
167
|
+
if (!options.recursive || !excluded.length) return (await runtime.listFiles({
|
|
168
|
+
abortSignal: options.signal,
|
|
169
|
+
path,
|
|
170
|
+
recursive: options.recursive
|
|
171
|
+
})).filter((entry) => !isExcludedSandboxPath(entry.path, excluded));
|
|
172
|
+
const entries = [];
|
|
173
|
+
const directories = [path];
|
|
174
|
+
for (let index = 0; index < directories.length; index++) {
|
|
175
|
+
const directory = directories[index];
|
|
176
|
+
if (isExcludedSandboxPath(directory, excluded)) continue;
|
|
177
|
+
for (const entry of await runtime.listFiles({
|
|
178
|
+
abortSignal: options.signal,
|
|
179
|
+
path: directory,
|
|
180
|
+
recursive: false
|
|
181
|
+
})) {
|
|
182
|
+
if (isExcludedSandboxPath(entry.path, excluded)) continue;
|
|
183
|
+
entries.push(entry);
|
|
184
|
+
if (entry.type === "directory") directories.push(entry.path);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return entries.sort((left, right) => left.path.localeCompare(right.path));
|
|
188
|
+
}
|
|
189
|
+
function createSandboxSession(runtime, openOptions, cwd = runtime.defaultWorkingDirectory) {
|
|
190
|
+
let closePromise;
|
|
191
|
+
let closed = false;
|
|
192
|
+
const assertOpen = () => {
|
|
193
|
+
if (closed) throw sandboxErrorDiagnostics.SANDBOX_R0125({ message: "[vitehub] Sandbox session is closed." });
|
|
194
|
+
};
|
|
195
|
+
const operationSignal = (signal, timeout) => {
|
|
196
|
+
assertOpen();
|
|
197
|
+
const signals = [openOptions.signal, signal].filter((value) => value !== void 0);
|
|
198
|
+
if (timeout !== void 0) {
|
|
199
|
+
if (!Number.isFinite(timeout) || timeout <= 0) throw sandboxErrorDiagnostics.SANDBOX_R0126({ message: "[vitehub] Sandbox command timeout must be a positive number." });
|
|
200
|
+
signals.push(AbortSignal.timeout(timeout));
|
|
201
|
+
}
|
|
202
|
+
const combined = signals.length === 0 ? void 0 : signals.length === 1 ? signals[0] : AbortSignal.any(signals);
|
|
203
|
+
combined?.throwIfAborted();
|
|
204
|
+
return combined;
|
|
205
|
+
};
|
|
206
|
+
const session = {
|
|
207
|
+
cwd,
|
|
208
|
+
executionAuthority: openOptions.executionAuthority,
|
|
209
|
+
files: {
|
|
210
|
+
async exists(path, options) {
|
|
211
|
+
return await runtime.existsFile({
|
|
212
|
+
abortSignal: operationSignal(options?.signal),
|
|
213
|
+
path
|
|
214
|
+
});
|
|
215
|
+
},
|
|
216
|
+
async list(path, options) {
|
|
217
|
+
return await listSessionFiles(runtime, path, {
|
|
218
|
+
...options,
|
|
219
|
+
signal: operationSignal(options?.signal)
|
|
220
|
+
});
|
|
221
|
+
},
|
|
222
|
+
async mkdir(path, options) {
|
|
223
|
+
await runtime.makeDirectory({
|
|
224
|
+
abortSignal: operationSignal(options?.signal),
|
|
225
|
+
path,
|
|
226
|
+
recursive: options?.recursive
|
|
227
|
+
});
|
|
228
|
+
},
|
|
229
|
+
...runtime.moveFile ? { async move(source, destination, options) {
|
|
230
|
+
await runtime.moveFile({
|
|
231
|
+
abortSignal: operationSignal(options?.signal),
|
|
232
|
+
destination,
|
|
233
|
+
source
|
|
234
|
+
});
|
|
235
|
+
} } : {},
|
|
236
|
+
async read(path, options) {
|
|
237
|
+
const contents = await runtime.readBinaryFile({
|
|
238
|
+
abortSignal: operationSignal(options?.signal),
|
|
239
|
+
path
|
|
240
|
+
});
|
|
241
|
+
return contents === null ? null : new Uint8Array(contents);
|
|
242
|
+
},
|
|
243
|
+
async remove(path, options) {
|
|
244
|
+
await runtime.removeFile({
|
|
245
|
+
abortSignal: operationSignal(options?.signal),
|
|
246
|
+
path,
|
|
247
|
+
recursive: options?.recursive
|
|
248
|
+
});
|
|
249
|
+
},
|
|
250
|
+
async write(path, contents, options) {
|
|
251
|
+
await runtime.writeBinaryFile({
|
|
252
|
+
abortSignal: operationSignal(options?.signal),
|
|
253
|
+
content: contents,
|
|
254
|
+
path
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
id: runtime.id,
|
|
259
|
+
get toolchain() {
|
|
260
|
+
return runtime.toolchain;
|
|
261
|
+
},
|
|
262
|
+
...runtime.inspectionConcurrency === void 0 ? {} : { inspectionConcurrency: runtime.inspectionConcurrency },
|
|
263
|
+
...runtime.getPortUrl ? { ports: {
|
|
264
|
+
values: runtime.ports ?? [0],
|
|
265
|
+
async expose(port, options) {
|
|
266
|
+
operationSignal();
|
|
267
|
+
assertPort(port);
|
|
268
|
+
return new URL(await runtime.getPortUrl({
|
|
269
|
+
port,
|
|
270
|
+
protocol: options?.protocol
|
|
271
|
+
}));
|
|
272
|
+
}
|
|
273
|
+
} } : {},
|
|
274
|
+
...runtime.spawn ? { async spawn(command, args = [], options) {
|
|
275
|
+
return adaptProcess(await runtime.spawn({
|
|
276
|
+
abortSignal: operationSignal(options?.signal, options?.timeout),
|
|
277
|
+
command: commandLine(command, args),
|
|
278
|
+
env: options?.env ? { ...options.env } : void 0,
|
|
279
|
+
stdin: options?.stdin,
|
|
280
|
+
timeout: options?.timeout,
|
|
281
|
+
workingDirectory: options?.cwd ?? cwd
|
|
282
|
+
}));
|
|
283
|
+
} } : {},
|
|
284
|
+
async close() {
|
|
285
|
+
if (closePromise) return await closePromise;
|
|
286
|
+
closed = true;
|
|
287
|
+
closePromise = runtime.destroy ? runtime.destroy() : runtime.stop();
|
|
288
|
+
try {
|
|
289
|
+
await closePromise;
|
|
290
|
+
} catch (error) {
|
|
291
|
+
closePromise = void 0;
|
|
292
|
+
throw error;
|
|
293
|
+
}
|
|
294
|
+
},
|
|
295
|
+
async exec(command, args = [], options) {
|
|
296
|
+
const result = await runtime.run({
|
|
297
|
+
abortSignal: operationSignal(options?.signal, options?.timeout),
|
|
298
|
+
command: commandLine(command, args),
|
|
299
|
+
env: options?.env ? { ...options.env } : void 0,
|
|
300
|
+
timeout: options?.timeout,
|
|
301
|
+
workingDirectory: options?.cwd ?? cwd
|
|
302
|
+
});
|
|
303
|
+
return {
|
|
304
|
+
code: result.exitCode,
|
|
305
|
+
ok: result.exitCode === 0,
|
|
306
|
+
stderr: result.stderr,
|
|
307
|
+
stdout: result.stdout
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
return Object.freeze(session);
|
|
312
|
+
}
|
|
313
|
+
function adaptProcess(process) {
|
|
314
|
+
return {
|
|
315
|
+
pid: process.pid,
|
|
316
|
+
stderr: process.stderr,
|
|
317
|
+
get stdin() {
|
|
318
|
+
return process.stdin;
|
|
319
|
+
},
|
|
320
|
+
stdout: process.stdout,
|
|
321
|
+
async kill(signal) {
|
|
322
|
+
await process.kill(signal);
|
|
323
|
+
},
|
|
324
|
+
async wait() {
|
|
325
|
+
return { code: (await process.wait()).exitCode };
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
function commandLine(command, args) {
|
|
330
|
+
if (!command || command.includes("\0")) throw sandboxErrorDiagnostics.SANDBOX_R0127({ message: "[vitehub] Sandbox commands must be non-empty and cannot contain NUL." });
|
|
331
|
+
if (args.some((argument) => argument.includes("\0"))) throw sandboxErrorDiagnostics.SANDBOX_R0128({ message: "[vitehub] Sandbox command arguments cannot contain NUL." });
|
|
332
|
+
return args.length === 0 ? command : [shellQuote(command), ...args.map(shellQuote)].join(" ");
|
|
333
|
+
}
|
|
334
|
+
function assertPort(port) {
|
|
335
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) throw sandboxErrorDiagnostics.SANDBOX_R0129({ message: "[vitehub] Sandbox ports must be integers between 1 and 65535." });
|
|
336
|
+
}
|
|
337
|
+
//#endregion
|
|
338
|
+
export { sandboxRequirementSecrets as a, sandboxRequirementPlan as i, collectSandboxRequirementOutput as n, sandboxRequirementSignal as o, sandboxRequirementError as r, materializeGitCheckout as s, createSandboxSession as t };
|