@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,176 @@
|
|
|
1
|
+
import { at as CloudflareSandboxOptions, l as Sandbox } from "../index-DsoQAUb7.js";
|
|
2
|
+
//#region src/internal/cloudflare-durable-object.d.ts
|
|
3
|
+
interface CloudflareContainerExecProcess {
|
|
4
|
+
readonly exitCode: Promise<number>;
|
|
5
|
+
readonly stderr: ReadableStream<Uint8Array> | null;
|
|
6
|
+
readonly stdin: WritableStream<Uint8Array> | null;
|
|
7
|
+
readonly stdout: ReadableStream<Uint8Array> | null;
|
|
8
|
+
kill(signal?: number): void;
|
|
9
|
+
output(): Promise<{
|
|
10
|
+
exitCode: number;
|
|
11
|
+
stderr: ArrayBuffer;
|
|
12
|
+
stdout: ArrayBuffer;
|
|
13
|
+
}>;
|
|
14
|
+
}
|
|
15
|
+
/** The part of the Workers `Container` API this Durable Object uses. */
|
|
16
|
+
interface CloudflareContainer {
|
|
17
|
+
readonly running: boolean;
|
|
18
|
+
destroy(error?: unknown): Promise<void>;
|
|
19
|
+
exec(command: string[], options?: {
|
|
20
|
+
cwd?: string;
|
|
21
|
+
env?: Record<string, string>;
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
stderr?: "pipe" | "ignore" | "combined";
|
|
24
|
+
stdin?: ReadableStream | "pipe";
|
|
25
|
+
stdout?: "pipe" | "ignore";
|
|
26
|
+
}): Promise<CloudflareContainerExecProcess>;
|
|
27
|
+
/** Sends HTTP requests on port 80 to `target` through `handler`. Missing on Workers runtimes without outbound interception. */
|
|
28
|
+
interceptOutboundHttp?(target: string, handler: unknown): Promise<void>;
|
|
29
|
+
/** Decrypts HTTPS requests on port 443 to `target` and sends them through `handler`. */
|
|
30
|
+
interceptOutboundHttps?(target: string, handler: unknown): Promise<void>;
|
|
31
|
+
setInactivityTimeout(durationMs: number | bigint): Promise<void>;
|
|
32
|
+
start(options?: {
|
|
33
|
+
enableInternet?: boolean;
|
|
34
|
+
labels?: Record<string, string>;
|
|
35
|
+
}): void;
|
|
36
|
+
}
|
|
37
|
+
interface CloudflareDurableObjectState {
|
|
38
|
+
readonly container?: CloudflareContainer;
|
|
39
|
+
/** The Worker's exported entrypoints. Needs compatibility date 2025-11-17 or later, or the `enable_ctx_exports` flag. */
|
|
40
|
+
readonly exports?: Readonly<Record<string, unknown>>;
|
|
41
|
+
readonly id: {
|
|
42
|
+
toString(): string;
|
|
43
|
+
readonly name?: string;
|
|
44
|
+
};
|
|
45
|
+
readonly storage: {
|
|
46
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
47
|
+
put(key: string, value: unknown): Promise<void>;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/** The part of `@cloudflare/sandbox` `Files` this Durable Object uses. */
|
|
51
|
+
interface CloudflareSandboxFiles {
|
|
52
|
+
mkdir(path: string, options?: {
|
|
53
|
+
cwd?: string;
|
|
54
|
+
recursive?: boolean;
|
|
55
|
+
}): Promise<void>;
|
|
56
|
+
readFile(path: string, options?: {
|
|
57
|
+
cwd?: string;
|
|
58
|
+
}): Promise<Response>;
|
|
59
|
+
remove(path: string, options?: {
|
|
60
|
+
cwd?: string;
|
|
61
|
+
force?: boolean;
|
|
62
|
+
recursive?: boolean;
|
|
63
|
+
}): Promise<void>;
|
|
64
|
+
rename(source: string, destination: string, options?: {
|
|
65
|
+
cwd?: string;
|
|
66
|
+
}): Promise<void>;
|
|
67
|
+
stat(path: string, options?: {
|
|
68
|
+
cwd?: string;
|
|
69
|
+
}): Promise<unknown>;
|
|
70
|
+
writeFile(path: string, content: Uint8Array, options?: {
|
|
71
|
+
cwd?: string;
|
|
72
|
+
}): Promise<void>;
|
|
73
|
+
}
|
|
74
|
+
interface CloudflareSandboxDurableObjectDependencies {
|
|
75
|
+
DurableObject: abstract new (ctx: any, env: any) => object;
|
|
76
|
+
Files: new (container: Pick<CloudflareContainer, "exec">) => CloudflareSandboxFiles;
|
|
77
|
+
/** The name of the exported `WorkerEntrypoint` that sends allowed outbound requests on unchanged. */
|
|
78
|
+
outboundEntrypoint?: string;
|
|
79
|
+
SandboxFileError: {
|
|
80
|
+
is(cause: unknown): cause is Error & {
|
|
81
|
+
code: string;
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
interface CloudflareSandboxConfiguration {
|
|
86
|
+
keepAlive?: boolean;
|
|
87
|
+
/** Validated by the caller. Missing means `"allow-all"`, as for Containers that started before this option existed. */
|
|
88
|
+
networkPolicy?: "allow-all" | "deny-all" | {
|
|
89
|
+
allow: readonly string[];
|
|
90
|
+
};
|
|
91
|
+
sleepAfter?: number | string;
|
|
92
|
+
}
|
|
93
|
+
/** The RPC methods of the generated Sandbox Durable Object, matching `CloudflareSandboxStub`. */
|
|
94
|
+
interface CloudflareSandboxDurableObject {
|
|
95
|
+
configure(configuration: CloudflareSandboxConfiguration): Promise<void>;
|
|
96
|
+
deleteFile(path: string): Promise<{
|
|
97
|
+
success: boolean;
|
|
98
|
+
}>;
|
|
99
|
+
destroy(): Promise<void>;
|
|
100
|
+
/** Stops the `exec` command started with this `id`. The Container and other commands keep running. */
|
|
101
|
+
cancelExec(id: string): Promise<void>;
|
|
102
|
+
exec(command: string, options?: CloudflareExecOptions): Promise<{
|
|
103
|
+
exitCode: number;
|
|
104
|
+
stderr: string;
|
|
105
|
+
stdout: string;
|
|
106
|
+
success: boolean;
|
|
107
|
+
}>;
|
|
108
|
+
exists(path: string): Promise<{
|
|
109
|
+
exists: boolean;
|
|
110
|
+
}>;
|
|
111
|
+
mkdir(path: string, options?: {
|
|
112
|
+
recursive?: boolean;
|
|
113
|
+
}): Promise<{
|
|
114
|
+
success: boolean;
|
|
115
|
+
}>;
|
|
116
|
+
moveFile(source: string, destination: string): Promise<{
|
|
117
|
+
success: boolean;
|
|
118
|
+
}>;
|
|
119
|
+
readFile(path: string, options?: {
|
|
120
|
+
encoding?: string;
|
|
121
|
+
}): Promise<{
|
|
122
|
+
content: string;
|
|
123
|
+
success: boolean;
|
|
124
|
+
}>;
|
|
125
|
+
startProcess(command: string, options?: {
|
|
126
|
+
cwd?: string;
|
|
127
|
+
env?: Record<string, string>;
|
|
128
|
+
stdin?: boolean;
|
|
129
|
+
}): Promise<{
|
|
130
|
+
getLogs(): Promise<{
|
|
131
|
+
stderr: string;
|
|
132
|
+
stdout: string;
|
|
133
|
+
}>;
|
|
134
|
+
id: string;
|
|
135
|
+
kill(signal?: string): Promise<void>;
|
|
136
|
+
stdin?: WritableStream<Uint8Array>;
|
|
137
|
+
waitForExit(): Promise<{
|
|
138
|
+
exitCode: number;
|
|
139
|
+
}>;
|
|
140
|
+
}>;
|
|
141
|
+
streamProcessLogs(processId: string): Promise<ReadableStream<Uint8Array>>;
|
|
142
|
+
writeFile(path: string, contents: string, options?: {
|
|
143
|
+
encoding?: string;
|
|
144
|
+
}): Promise<{
|
|
145
|
+
success: boolean;
|
|
146
|
+
}>;
|
|
147
|
+
}
|
|
148
|
+
interface CloudflareExecOptions {
|
|
149
|
+
cwd?: string;
|
|
150
|
+
env?: Record<string, string | undefined>;
|
|
151
|
+
/** Names the command for `cancelExec`. */
|
|
152
|
+
id?: string;
|
|
153
|
+
timeout?: number;
|
|
154
|
+
}
|
|
155
|
+
interface CloudflareSandboxOutboundDependencies {
|
|
156
|
+
WorkerEntrypoint: abstract new (ctx: any, env: any) => object;
|
|
157
|
+
}
|
|
158
|
+
/** What the Durable Object passes to each outbound intercept. */
|
|
159
|
+
interface CloudflareSandboxOutboundProps {
|
|
160
|
+
allow: readonly string[];
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The Worker entrypoint that receives the intercepted HTTP and HTTPS requests of a Sandbox Container.
|
|
164
|
+
* Cloudflare picks an HTTPS intercept by TLS SNI, but the request URL comes from the Host header that the
|
|
165
|
+
* Container sends. So the entrypoint checks the URL against the allowlist again before it fetches.
|
|
166
|
+
*/
|
|
167
|
+
export declare function createCloudflareSandboxOutbound(dependencies: CloudflareSandboxOutboundDependencies): new (ctx: {
|
|
168
|
+
readonly props?: Partial<CloudflareSandboxOutboundProps>;
|
|
169
|
+
}, env: unknown) => {
|
|
170
|
+
fetch(request: Request): Promise<Response>;
|
|
171
|
+
};
|
|
172
|
+
export declare function createCloudflareSandboxDurableObject(dependencies: CloudflareSandboxDurableObjectDependencies): new (ctx: CloudflareDurableObjectState, env: unknown) => CloudflareSandboxDurableObject;
|
|
173
|
+
//#endregion
|
|
174
|
+
//#region src/internal/cloudflare.d.ts
|
|
175
|
+
export declare function resolveCloudflareSandbox(options: CloudflareSandboxOptions, requirements: readonly string[]): Promise<Sandbox>;
|
|
176
|
+
//#endregion
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { a as resolveRemoteSandboxRuntime } from "../remote-70ZVZZlq.js";
|
|
2
|
+
import { i as createCloudflareSandboxOutbound, n as createCloudflareRuntime, r as createCloudflareSandboxDurableObject } from "../cloudflare-Cog_r2Me.js";
|
|
3
|
+
//#region src/internal/cloudflare.ts
|
|
4
|
+
async function resolveCloudflareSandbox(options, requirements) {
|
|
5
|
+
return await resolveRemoteSandboxRuntime(createCloudflareRuntime(options), requirements);
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
export { createCloudflareSandboxDurableObject, createCloudflareSandboxOutbound, resolveCloudflareSandbox };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { L as SandboxToolchain, R as SandboxToolchainInput, k as SandboxResolvedToolchain } from "../index-DsoQAUb7.js";
|
|
2
|
+
//#region src/internal/host-toolchain.d.ts
|
|
3
|
+
export interface HostToolchainOptions {
|
|
4
|
+
abortSignal?: AbortSignal;
|
|
5
|
+
/** Shared cache directory. Defaults to `$XDG_CACHE_HOME/vitehub/toolchains`. */
|
|
6
|
+
cacheRoot?: string;
|
|
7
|
+
/** Environment of the install and verification shell. Its PATH follows the toolchain entries. */
|
|
8
|
+
env?: Readonly<Record<string, string | undefined>>;
|
|
9
|
+
}
|
|
10
|
+
/** Default host cache. A configured Sandbox stateRoot keeps toolchains beside durable state. */
|
|
11
|
+
export declare function hostToolchainCacheRoot(stateRoot?: string): string;
|
|
12
|
+
export declare function readHostProjectFiles(directory: string, paths?: readonly string[]): Promise<Record<string, string | undefined>>;
|
|
13
|
+
/**
|
|
14
|
+
* Provision a project-pinned toolchain for a directory on this host.
|
|
15
|
+
* Concurrent processes that share the cache download each version once.
|
|
16
|
+
*/
|
|
17
|
+
export declare function provisionHostToolchain(toolchain: SandboxToolchainInput, directory: string, options?: HostToolchainOptions): Promise<SandboxResolvedToolchain>;
|
|
18
|
+
/** PATH with the toolchain entries before the base PATH. */
|
|
19
|
+
export declare function toolchainPath(toolchain: SandboxResolvedToolchain, base: string | undefined): string;
|
|
20
|
+
/** Validate a toolchain declaration and provision it for a host directory. */
|
|
21
|
+
export declare function prepareHostToolchain(declaration: SandboxToolchain, directory: string, options?: HostToolchainOptions): Promise<SandboxResolvedToolchain>;
|
|
22
|
+
//#endregion
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { t as sandboxErrorDiagnostics } from "../error-diagnostics-BJtLRL9w.js";
|
|
2
|
+
import { t as acquireFileLock } from "../file-lock-D0-XPogS.js";
|
|
3
|
+
import { a as verifyToolchain, n as provisionToolchain, r as toolchainProjectFiles, t as normalizeToolchain } from "../toolchain-BIVGMRty.js";
|
|
4
|
+
import { lstat, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
5
|
+
import { delimiter, dirname, join, resolve } from "node:path";
|
|
6
|
+
import { execFile } from "node:child_process";
|
|
7
|
+
import { promisify } from "node:util";
|
|
8
|
+
import { homedir } from "node:os";
|
|
9
|
+
//#region src/internal/host-toolchain.ts
|
|
10
|
+
/** Default host cache. A configured Sandbox stateRoot keeps toolchains beside durable state. */
|
|
11
|
+
function hostToolchainCacheRoot(stateRoot) {
|
|
12
|
+
return stateRoot ? join(stateRoot, "toolchains") : join(process.env.XDG_CACHE_HOME || join(homedir(), ".cache"), "vitehub", "toolchains");
|
|
13
|
+
}
|
|
14
|
+
async function readHostProjectFiles(directory, paths = toolchainProjectFiles) {
|
|
15
|
+
const files = {};
|
|
16
|
+
for (const path of paths) files[path] = await readFile(join(directory, path), "utf8").catch((error) => {
|
|
17
|
+
if (error.code === "ENOENT" || error.code === "ENOTDIR") return void 0;
|
|
18
|
+
throw error;
|
|
19
|
+
});
|
|
20
|
+
return files;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Provision a project-pinned toolchain for a directory on this host.
|
|
24
|
+
* Concurrent processes that share the cache download each version once.
|
|
25
|
+
*/
|
|
26
|
+
async function provisionHostToolchain(toolchain, directory, options = {}) {
|
|
27
|
+
if (process.platform === "win32") throw sandboxErrorDiagnostics.SANDBOX_R0150({ message: "[vitehub] Sandbox toolchain provisioning requires a Linux or macOS host." });
|
|
28
|
+
const cacheRoot = resolve(options.cacheRoot ?? hostToolchainCacheRoot());
|
|
29
|
+
await mkdir(cacheRoot, {
|
|
30
|
+
mode: 448,
|
|
31
|
+
recursive: true
|
|
32
|
+
});
|
|
33
|
+
const cache = await lstat(cacheRoot);
|
|
34
|
+
const uid = process.getuid?.();
|
|
35
|
+
if (!cache.isDirectory() || uid !== void 0 && cache.uid !== uid || (cache.mode & 18) !== 0) throw sandboxErrorDiagnostics.SANDBOX_R0154({ message: `[vitehub] Sandbox toolchain cache must be a directory owned by the current user and not writable by others: ${cacheRoot}` });
|
|
36
|
+
const env = Object.fromEntries(Object.entries(options.env ?? process.env).filter((entry) => entry[1] !== void 0));
|
|
37
|
+
const run = async (script, environment = env) => {
|
|
38
|
+
try {
|
|
39
|
+
const result = await promisify(execFile)("sh", ["-c", script], {
|
|
40
|
+
cwd: cacheRoot,
|
|
41
|
+
encoding: "utf8",
|
|
42
|
+
env: environment,
|
|
43
|
+
maxBuffer: 16777216,
|
|
44
|
+
signal: options.abortSignal
|
|
45
|
+
});
|
|
46
|
+
return {
|
|
47
|
+
exitCode: 0,
|
|
48
|
+
stderr: result.stderr,
|
|
49
|
+
stdout: result.stdout
|
|
50
|
+
};
|
|
51
|
+
} catch (error) {
|
|
52
|
+
options.abortSignal?.throwIfAborted();
|
|
53
|
+
const code = error instanceof Error && "code" in error ? error.code : void 0;
|
|
54
|
+
if (typeof code !== "number") throw error;
|
|
55
|
+
const output = (name) => {
|
|
56
|
+
const value = error instanceof Error && name in error ? Reflect.get(error, name) : void 0;
|
|
57
|
+
return typeof value === "string" ? value : "";
|
|
58
|
+
};
|
|
59
|
+
return {
|
|
60
|
+
exitCode: code,
|
|
61
|
+
stderr: output("stderr"),
|
|
62
|
+
stdout: output("stdout")
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const toolchainResult = await provisionToolchain(toolchain, {
|
|
67
|
+
abortSignal: options.abortSignal,
|
|
68
|
+
cacheRoot,
|
|
69
|
+
lock: async (key) => await acquireFileLock(join(cacheRoot, `.${key}.lock`), options.abortSignal),
|
|
70
|
+
readProjectFiles: async (paths) => await readHostProjectFiles(directory, paths),
|
|
71
|
+
run,
|
|
72
|
+
scratch: cacheRoot,
|
|
73
|
+
async write(path, contents) {
|
|
74
|
+
await mkdir(dirname(path), {
|
|
75
|
+
mode: 448,
|
|
76
|
+
recursive: true
|
|
77
|
+
});
|
|
78
|
+
await writeFile(path, contents, { mode: 384 });
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
await verifyToolchain(toolchainResult, cacheRoot, async (script) => await run(script, {
|
|
82
|
+
...env,
|
|
83
|
+
PATH: toolchainPath(toolchainResult, env.PATH)
|
|
84
|
+
}));
|
|
85
|
+
return toolchainResult;
|
|
86
|
+
}
|
|
87
|
+
/** PATH with the toolchain entries before the base PATH. */
|
|
88
|
+
function toolchainPath(toolchain, base) {
|
|
89
|
+
return [...toolchain.bin, base].filter(Boolean).join(delimiter);
|
|
90
|
+
}
|
|
91
|
+
/** Validate a toolchain declaration and provision it for a host directory. */
|
|
92
|
+
async function prepareHostToolchain(declaration, directory, options = {}) {
|
|
93
|
+
return await provisionHostToolchain(normalizeToolchain(declaration), directory, options);
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
export { hostToolchainCacheRoot, prepareHostToolchain, provisionHostToolchain, readHostProjectFiles, toolchainPath };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { a as resolveRemoteSandboxRuntime } from "../remote-70ZVZZlq.js";
|
|
2
|
+
import { t as createVercelRuntime } from "../vercel-BGGrNpza.js";
|
|
3
|
+
//#region src/internal/vercel.ts
|
|
4
|
+
async function resolveVercelSandbox(options, requirements) {
|
|
5
|
+
return await resolveRemoteSandboxRuntime(createVercelRuntime(options), requirements);
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
export { resolveVercelSandbox };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { defineDiagnostics } from "nostics";
|
|
2
|
+
//#region ../internal/src/invariant.ts
|
|
3
|
+
const invariantDiagnostics = /*#__PURE__*/ defineDiagnostics({
|
|
4
|
+
docsBase: "https://vitehub.dev/docs/development/diagnostics",
|
|
5
|
+
codes: { INTERNAL_INVARIANT_FAILED: {
|
|
6
|
+
why: ({ what }) => `ViteHub reached a state that it does not expect: ${what}`,
|
|
7
|
+
fix: "This is a ViteHub bug. Open an issue at https://github.com/vite-hub/vitehub/issues with this error message and its stack trace."
|
|
8
|
+
} }
|
|
9
|
+
});
|
|
10
|
+
/**
|
|
11
|
+
* The error for a state that only a ViteHub bug can reach, such as an impossible branch. `what` says which rule broke.
|
|
12
|
+
* Use a code from the package catalog when a developer can cause the error or fix it.
|
|
13
|
+
*/
|
|
14
|
+
function invariantError(what) {
|
|
15
|
+
return invariantDiagnostics.INTERNAL_INVARIANT_FAILED({ what });
|
|
16
|
+
}
|
|
17
|
+
/** Throws `INTERNAL_INVARIANT_FAILED` when `condition` is falsy. */
|
|
18
|
+
function invariant(condition, what) {
|
|
19
|
+
if (!condition) throw invariantError(what);
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
export { invariantError as n, invariant as t };
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Q as VercelSandboxSource, X as VercelSandboxNetworkPolicy, st as SandboxNetworkPolicy } from "./index-DsoQAUb7.js";
|
|
2
|
+
//#region src/task/project.d.ts
|
|
3
|
+
type SandboxTaskPackageManager = 'bun' | 'npm' | 'pnpm' | 'yarn';
|
|
4
|
+
interface SandboxTaskProjectFile {
|
|
5
|
+
contents: string;
|
|
6
|
+
encoding: 'base64';
|
|
7
|
+
mode?: number;
|
|
8
|
+
}
|
|
9
|
+
interface SandboxTaskProject {
|
|
10
|
+
digest: string;
|
|
11
|
+
files: Record<string, SandboxTaskProjectFile>;
|
|
12
|
+
install: {
|
|
13
|
+
args: string[];
|
|
14
|
+
command: SandboxTaskPackageManager;
|
|
15
|
+
cwd: string;
|
|
16
|
+
};
|
|
17
|
+
options?: SandboxTaskProjectOptions;
|
|
18
|
+
packagePath: string;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/task/size.d.ts
|
|
22
|
+
/** The size of the machine that runs every Sandbox Task. Both values are minimums. */
|
|
23
|
+
interface SandboxTaskSizeOptions {
|
|
24
|
+
/** Minimum vCPUs. ViteHub picks the smallest provider size that gives at least this many. */
|
|
25
|
+
cpu?: number;
|
|
26
|
+
/** Minimum memory in MiB. ViteHub picks the smallest provider size that gives at least this much. */
|
|
27
|
+
memory?: number;
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/task/module-types.d.ts
|
|
31
|
+
type SandboxTaskDriver = 'cloudflare' | 'vercel';
|
|
32
|
+
interface CloudflareSandboxTaskOptions {
|
|
33
|
+
sleepAfter?: string | number;
|
|
34
|
+
keepAlive?: boolean;
|
|
35
|
+
normalizeId?: boolean;
|
|
36
|
+
}
|
|
37
|
+
interface VercelSandboxTaskDriverOptions extends SandboxTaskSizeOptions {
|
|
38
|
+
driver: 'vercel';
|
|
39
|
+
runtime?: string;
|
|
40
|
+
ports?: number[];
|
|
41
|
+
source?: VercelSandboxSource;
|
|
42
|
+
/** `"allow-all"` (default), `"deny-all"`, or `{ allow: hostnames }`. Vercel also takes `subnets`. */
|
|
43
|
+
networkPolicy?: VercelSandboxNetworkPolicy;
|
|
44
|
+
token?: string;
|
|
45
|
+
teamId?: string;
|
|
46
|
+
projectId?: string;
|
|
47
|
+
}
|
|
48
|
+
interface CloudflareSandboxTaskDefinitionDriverOptions extends SandboxTaskSizeOptions {
|
|
49
|
+
driver: 'cloudflare';
|
|
50
|
+
/** Maximum number of Containers that run at once. Defaults to 12. */
|
|
51
|
+
maxInstances?: number;
|
|
52
|
+
binding?: string;
|
|
53
|
+
className?: string;
|
|
54
|
+
migrationTag?: string;
|
|
55
|
+
name?: string;
|
|
56
|
+
sandboxId?: string;
|
|
57
|
+
sleepAfter?: CloudflareSandboxTaskOptions['sleepAfter'];
|
|
58
|
+
keepAlive?: CloudflareSandboxTaskOptions['keepAlive'];
|
|
59
|
+
normalizeId?: CloudflareSandboxTaskOptions['normalizeId'];
|
|
60
|
+
/** `"allow-all"` (default), `"deny-all"`, or `{ allow: hostnames }`. Applies when the Container starts. */
|
|
61
|
+
networkPolicy?: SandboxNetworkPolicy;
|
|
62
|
+
}
|
|
63
|
+
type SandboxTaskDefinitionDriverOptions = VercelSandboxTaskDriverOptions | CloudflareSandboxTaskDefinitionDriverOptions;
|
|
64
|
+
type AgentSandboxTaskConfig = SandboxTaskDefinitionDriverOptions | ({
|
|
65
|
+
driver?: undefined;
|
|
66
|
+
name?: string;
|
|
67
|
+
} & SandboxTaskSizeOptions);
|
|
68
|
+
interface SandboxTaskDefinitionBundle {
|
|
69
|
+
entry: string;
|
|
70
|
+
execution?: 'definition' | 'module';
|
|
71
|
+
modules: Record<string, string>;
|
|
72
|
+
project?: SandboxTaskProject;
|
|
73
|
+
}
|
|
74
|
+
interface SandboxTaskExecutionOptions {
|
|
75
|
+
/** Passed to the handler; defaults to an empty object. */
|
|
76
|
+
context?: Record<string, unknown>;
|
|
77
|
+
/** Abort this invocation and propagate the caller's abort reason. */
|
|
78
|
+
signal?: AbortSignal;
|
|
79
|
+
sandboxId?: string;
|
|
80
|
+
}
|
|
81
|
+
type SandboxTaskRunArguments<TPayload> = undefined extends TPayload ? [payload?: TPayload, options?: SandboxTaskExecutionOptions] : [payload: TPayload, options?: SandboxTaskExecutionOptions];
|
|
82
|
+
/** The handler of a Sandbox Task. The runtime calls it with the payload of the run and the run context. */
|
|
83
|
+
type SandboxTaskDefinitionHandler<TPayload = unknown, TResult = unknown> = (payload: TPayload, context: Record<string, unknown>) => TResult | Promise<TResult>;
|
|
84
|
+
type SandboxTaskDefinitionPayload<THandler extends (...args: any[]) => any> = Parameters<THandler> extends [infer TPayload, ...unknown[]] ? TPayload : unknown;
|
|
85
|
+
type SandboxTaskDefinitionResult<THandler extends (...args: any[]) => any> = Awaited<ReturnType<THandler>>;
|
|
86
|
+
interface SandboxTaskDefinitionOptions {
|
|
87
|
+
timeout?: number;
|
|
88
|
+
env?: Record<string, string>;
|
|
89
|
+
}
|
|
90
|
+
interface SandboxTaskProjectOptions {
|
|
91
|
+
timeout?: number;
|
|
92
|
+
}
|
|
93
|
+
interface SandboxTaskDefinitionInput<TPayload = unknown, TResult = unknown> extends SandboxTaskDefinitionOptions {
|
|
94
|
+
/** Force project packaging on or off. Omit to detect project use automatically. */
|
|
95
|
+
project?: boolean;
|
|
96
|
+
handler: SandboxTaskDefinitionHandler<TPayload, TResult>;
|
|
97
|
+
}
|
|
98
|
+
interface SandboxTaskDefinition<TPayload = unknown, TResult = unknown> {
|
|
99
|
+
handler: SandboxTaskDefinitionHandler<TPayload, TResult>;
|
|
100
|
+
options?: SandboxTaskDefinitionOptions;
|
|
101
|
+
}
|
|
102
|
+
type SandboxTaskDefinitionFromHandler<THandler extends (...args: any[]) => any> = SandboxTaskDefinition<SandboxTaskDefinitionPayload<THandler>, SandboxTaskDefinitionResult<THandler>> & {
|
|
103
|
+
handler: THandler;
|
|
104
|
+
};
|
|
105
|
+
/** The result of a sandbox invocation. HTTP semantics are used for both
|
|
106
|
+
* successful values and failures. */
|
|
107
|
+
type SandboxTaskRunResult = Response;
|
|
108
|
+
declare function getSandboxTaskFeatureDriver(config?: AgentSandboxTaskConfig | false): SandboxTaskDefinitionDriverOptions | undefined;
|
|
109
|
+
//#endregion
|
|
110
|
+
export { getSandboxTaskFeatureDriver as _, SandboxTaskDefinitionBundle as a, SandboxTaskDefinitionHandler as c, SandboxTaskDriver as d, SandboxTaskExecutionOptions as f, VercelSandboxTaskDriverOptions as g, SandboxTaskRunResult as h, SandboxTaskDefinition as i, SandboxTaskDefinitionInput as l, SandboxTaskRunArguments as m, CloudflareSandboxTaskDefinitionDriverOptions as n, SandboxTaskDefinitionDriverOptions as o, SandboxTaskProjectOptions as p, CloudflareSandboxTaskOptions as r, SandboxTaskDefinitionFromHandler as s, AgentSandboxTaskConfig as t, SandboxTaskDefinitionOptions as u, SandboxTaskSizeOptions as v };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { readFile, readdir } from "node:fs/promises";
|
|
2
|
+
import { setTimeout } from "node:timers/promises";
|
|
3
|
+
//#region ../internal/src/process-sweep.ts
|
|
4
|
+
const maxRounds = 10;
|
|
5
|
+
/**
|
|
6
|
+
* Stops every process of this user that started with `name=value` in its environment: SIGTERM, then
|
|
7
|
+
* SIGKILL after `graceMs`. It repeats while marked processes remain, because a dying process can start
|
|
8
|
+
* another one. Does nothing outside Linux.
|
|
9
|
+
*/
|
|
10
|
+
async function stopMarkedProcesses(name, value, options = {}) {
|
|
11
|
+
if (process.platform !== "linux") return;
|
|
12
|
+
const marker = `\0${name}=${value}\0`;
|
|
13
|
+
for (let round = 0; round < maxRounds; round++) {
|
|
14
|
+
const marked = await markedProcesses(marker);
|
|
15
|
+
if (!marked.length) return;
|
|
16
|
+
for (const pid of marked) signal(pid, "SIGTERM");
|
|
17
|
+
await setTimeout(options.graceMs ?? 250);
|
|
18
|
+
for (const pid of await markedProcesses(marker)) signal(pid, "SIGKILL");
|
|
19
|
+
await setTimeout(10);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async function markedProcesses(marker) {
|
|
23
|
+
const pids = (await readdir("/proc")).filter((entry) => /^\d+$/.test(entry)).map(Number).filter((pid) => pid !== process.pid);
|
|
24
|
+
const environments = await Promise.all(pids.map((pid) => readFile(`/proc/${pid}/environ`, "latin1").catch(() => "")));
|
|
25
|
+
return pids.filter((_, index) => `\0${environments[index]}`.includes(marker));
|
|
26
|
+
}
|
|
27
|
+
function signal(pid, name) {
|
|
28
|
+
try {
|
|
29
|
+
process.kill(pid, name);
|
|
30
|
+
} catch {}
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
export { stopMarkedProcesses as t };
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { t as sandboxTaskErrorDiagnostics } from "./error-diagnostics-5QT_BjbQ.js";
|
|
2
|
+
import { t as getActiveCloudflareEnv } from "./cloudflare-env-lookup-DzI6Xv1Y.js";
|
|
3
|
+
import { ViteHubError, getViteHubErrorShape } from "@vite-hub/runtime";
|
|
4
|
+
//#region src/task/sandbox-task/errors.ts
|
|
5
|
+
function sanitizeSandboxTaskDetail(value, depth = 0, seen = /* @__PURE__ */ new WeakSet()) {
|
|
6
|
+
if (value === null || typeof value === "boolean") return value;
|
|
7
|
+
if (typeof value === "string") return value.slice(0, 16384);
|
|
8
|
+
if (typeof value === "number") return Number.isFinite(value) ? value : void 0;
|
|
9
|
+
if (!value || typeof value !== "object" || depth >= 8 || seen.has(value)) return void 0;
|
|
10
|
+
seen.add(value);
|
|
11
|
+
try {
|
|
12
|
+
if (Array.isArray(value)) return value.slice(0, 128).flatMap((item) => {
|
|
13
|
+
const sanitized = sanitizeSandboxTaskDetail(item, depth + 1, seen);
|
|
14
|
+
return sanitized === void 0 ? [] : [sanitized];
|
|
15
|
+
});
|
|
16
|
+
if (Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) return void 0;
|
|
17
|
+
const result = {};
|
|
18
|
+
for (const [key, item] of Object.entries(value).slice(0, 128)) {
|
|
19
|
+
if (!key || key.length > 128) continue;
|
|
20
|
+
result[key] = sanitizeSandboxTaskDetail(item, depth + 1, seen);
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
} catch {
|
|
24
|
+
return;
|
|
25
|
+
} finally {
|
|
26
|
+
seen.delete(value);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function sanitizeSandboxTaskDetails(details) {
|
|
30
|
+
const sanitized = sanitizeSandboxTaskDetail(details);
|
|
31
|
+
return sanitized && !Array.isArray(sanitized) && typeof sanitized === "object" ? sanitized : void 0;
|
|
32
|
+
}
|
|
33
|
+
function sandboxTaskError(message, metadata = {}) {
|
|
34
|
+
const { cause, code = "SANDBOX_TASK_RUNTIME_ERROR", details, httpStatus, method, provider } = metadata;
|
|
35
|
+
return new ViteHubError(code, message.slice(0, 16384) || "Sandbox execution failed.", {
|
|
36
|
+
cause,
|
|
37
|
+
details: sanitizeSandboxTaskDetails({
|
|
38
|
+
...provider === void 0 ? {} : { provider },
|
|
39
|
+
...method === void 0 ? {} : { method },
|
|
40
|
+
...httpStatus === void 0 ? {} : { httpStatus },
|
|
41
|
+
...sanitizeSandboxTaskDetails(details)
|
|
42
|
+
})
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function isSandboxTaskError(value) {
|
|
46
|
+
return getViteHubErrorShape(value)?.code.startsWith("SANDBOX_TASK_") === true;
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/task/internal/shared/provider-detection.ts
|
|
50
|
+
function createProviderDetector(rules) {
|
|
51
|
+
return () => {
|
|
52
|
+
for (const rule of rules) if (rule.when()) return rule.provider;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function getRuntimeEnv() {
|
|
56
|
+
return typeof process !== "undefined" ? process.env : {};
|
|
57
|
+
}
|
|
58
|
+
const isCloudflare = () => {
|
|
59
|
+
const env = getRuntimeEnv();
|
|
60
|
+
return !!(env.CLOUDFLARE_WORKER || env.CF_PAGES);
|
|
61
|
+
};
|
|
62
|
+
const isVercel = () => {
|
|
63
|
+
const env = getRuntimeEnv();
|
|
64
|
+
return !!(env.VERCEL || env.VERCEL_ENV);
|
|
65
|
+
};
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/task/runtime/provider-resolution.ts
|
|
68
|
+
const allowedDefinitionKeys = /* @__PURE__ */ new Set(["timeout", "env"]);
|
|
69
|
+
const maxTimeout = 2147483647;
|
|
70
|
+
/** The timeout of a Sandbox Task run that sets no `timeout`, on every provider. */
|
|
71
|
+
const DEFAULT_SANDBOX_TASK_TIMEOUT_MS = 3e5;
|
|
72
|
+
const detectProvider = createProviderDetector([{
|
|
73
|
+
provider: "cloudflare",
|
|
74
|
+
when: isCloudflare
|
|
75
|
+
}, {
|
|
76
|
+
provider: "vercel",
|
|
77
|
+
when: isVercel
|
|
78
|
+
}]);
|
|
79
|
+
function detectSandbox() {
|
|
80
|
+
const env = typeof process !== "undefined" ? process.env : {};
|
|
81
|
+
const type = detectProvider() || "none";
|
|
82
|
+
if (type === "cloudflare") return {
|
|
83
|
+
type,
|
|
84
|
+
details: { runtime: typeof process === "undefined" ? "workerd" : "node" }
|
|
85
|
+
};
|
|
86
|
+
if (type === "vercel") return {
|
|
87
|
+
type,
|
|
88
|
+
details: { env: env.VERCEL_ENV }
|
|
89
|
+
};
|
|
90
|
+
return { type };
|
|
91
|
+
}
|
|
92
|
+
function hashCloudflareSandboxName(name) {
|
|
93
|
+
let hash = 2166136261;
|
|
94
|
+
for (let index = 0; index < name.length; index++) hash = Math.imul(hash ^ name.charCodeAt(index), 16777619);
|
|
95
|
+
return (hash >>> 0).toString(36);
|
|
96
|
+
}
|
|
97
|
+
function createCloudflareExecutionSandboxId(name, sandboxId) {
|
|
98
|
+
if (sandboxId) return sandboxId;
|
|
99
|
+
const prefix = "vitehub-";
|
|
100
|
+
const suffix = `-${hashCloudflareSandboxName(name)}-${crypto.randomUUID()}`;
|
|
101
|
+
return `${prefix}${encodeURIComponent(name).slice(0, 55 - suffix.length)}${suffix}`;
|
|
102
|
+
}
|
|
103
|
+
function resolveRuntimeProvider(options) {
|
|
104
|
+
if (options?.driver) return options.driver;
|
|
105
|
+
const envProvider = typeof process !== "undefined" ? process.env?.SANDBOX_PROVIDER : void 0;
|
|
106
|
+
if (envProvider === "cloudflare" || envProvider === "vercel") return envProvider;
|
|
107
|
+
if (getActiveCloudflareEnv()) return "cloudflare";
|
|
108
|
+
const detected = detectSandbox();
|
|
109
|
+
if (detected.type === "cloudflare" || detected.type === "vercel") return detected.type;
|
|
110
|
+
throw sandboxTaskError("Sandbox provider could not be inferred. Configure `sandboxTask.driver` as `cloudflare` or `vercel`.", { code: "SANDBOX_TASK_PROVIDER_REQUIRED" });
|
|
111
|
+
}
|
|
112
|
+
function unsupportedHostedSandboxTaskProviderError(provider) {
|
|
113
|
+
return sandboxTaskErrorDiagnostics.SANDBOX_TASK_R0076({ message: `[vitehub] Unsupported sandbox provider for this hosted build: ${String(provider)}` });
|
|
114
|
+
}
|
|
115
|
+
function assertSandboxTaskDefinitionOptions(local) {
|
|
116
|
+
const invalidKeys = Object.keys(local).filter((key) => !allowedDefinitionKeys.has(key));
|
|
117
|
+
if (invalidKeys.length > 0) throw sandboxTaskErrorDiagnostics.SANDBOX_TASK_R0069({ message: `[vitehub] Sandbox Task Definition options only support timeout and env. Unsupported: ${invalidKeys.join(", ")}` });
|
|
118
|
+
if (local.timeout !== void 0 && (!Number.isInteger(local.timeout) || local.timeout <= 0 || local.timeout > maxTimeout)) throw sandboxTaskErrorDiagnostics.SANDBOX_TASK_R0070({ message: `[vitehub] Sandbox Task Definition timeout must be a positive integer no greater than ${maxTimeout}.` });
|
|
119
|
+
}
|
|
120
|
+
async function resolveTaskSandbox(provider, providerOptions, local) {
|
|
121
|
+
return await (await (await import("#vitehub-sandbox-task-provider-loader")).loadSandboxTaskRuntimeProvider(provider)).resolveTaskSandbox({
|
|
122
|
+
local,
|
|
123
|
+
provider: providerOptions
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
function withSandboxTaskProvider(provider, options) {
|
|
127
|
+
return {
|
|
128
|
+
...options,
|
|
129
|
+
driver: provider
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
//#endregion
|
|
133
|
+
export { resolveTaskSandbox as a, isSandboxTaskError as c, resolveRuntimeProvider as i, sandboxTaskError as l, assertSandboxTaskDefinitionOptions as n, unsupportedHostedSandboxTaskProviderError as o, createCloudflareExecutionSandboxId as r, withSandboxTaskProvider as s, DEFAULT_SANDBOX_TASK_TIMEOUT_MS as t };
|