@vercel/sandbox 2.0.0-beta.11 → 2.0.0-beta.13
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 +8 -0
- package/dist/_virtual/rolldown_runtime.cjs +29 -0
- package/dist/api-client/api-client.cjs +456 -0
- package/dist/api-client/api-client.cjs.map +1 -0
- package/dist/api-client/api-client.d.cts +790 -0
- package/dist/api-client/api-client.d.ts +784 -777
- package/dist/api-client/api-client.js +445 -471
- package/dist/api-client/api-client.js.map +1 -1
- package/dist/api-client/api-error.cjs +32 -0
- package/dist/api-client/api-error.cjs.map +1 -0
- package/dist/api-client/api-error.d.cts +29 -0
- package/dist/api-client/api-error.d.ts +21 -18
- package/dist/api-client/api-error.js +28 -33
- package/dist/api-client/api-error.js.map +1 -1
- package/dist/api-client/base-client.cjs +125 -0
- package/dist/api-client/base-client.cjs.map +1 -0
- package/dist/api-client/base-client.d.cts +38 -0
- package/dist/api-client/base-client.d.ts +31 -36
- package/dist/api-client/base-client.js +110 -130
- package/dist/api-client/base-client.js.map +1 -1
- package/dist/api-client/file-writer.cjs +62 -0
- package/dist/api-client/file-writer.cjs.map +1 -0
- package/dist/api-client/file-writer.d.cts +66 -0
- package/dist/api-client/file-writer.d.ts +56 -52
- package/dist/api-client/file-writer.js +57 -61
- package/dist/api-client/file-writer.js.map +1 -1
- package/dist/api-client/index.cjs +2 -0
- package/dist/api-client/index.d.ts +2 -2
- package/dist/api-client/index.js +4 -21
- package/dist/api-client/validators.cjs +182 -0
- package/dist/api-client/validators.cjs.map +1 -0
- package/dist/api-client/validators.d.cts +1469 -0
- package/dist/api-client/validators.d.ts +1282 -3699
- package/dist/api-client/validators.js +155 -170
- package/dist/api-client/validators.js.map +1 -1
- package/dist/api-client/with-retry.cjs +89 -0
- package/dist/api-client/with-retry.cjs.map +1 -0
- package/dist/api-client/with-retry.d.cts +10 -0
- package/dist/api-client/with-retry.d.ts +9 -13
- package/dist/api-client/with-retry.js +81 -102
- package/dist/api-client/with-retry.js.map +1 -1
- package/dist/auth/api.cjs +29 -0
- package/dist/auth/api.cjs.map +1 -0
- package/dist/auth/api.js +26 -25
- package/dist/auth/api.js.map +1 -1
- package/dist/auth/error.cjs +13 -0
- package/dist/auth/error.cjs.map +1 -0
- package/dist/auth/error.js +11 -11
- package/dist/auth/error.js.map +1 -1
- package/dist/auth/file.cjs +64 -0
- package/dist/auth/file.cjs.map +1 -0
- package/dist/auth/file.d.cts +26 -0
- package/dist/auth/file.d.ts +19 -15
- package/dist/auth/file.js +49 -64
- package/dist/auth/file.js.map +1 -1
- package/dist/auth/index.cjs +12 -0
- package/dist/auth/index.d.cts +5 -0
- package/dist/auth/index.d.ts +5 -6
- package/dist/auth/index.js +6 -27
- package/dist/auth/linked-project.cjs +38 -0
- package/dist/auth/linked-project.cjs.map +1 -0
- package/dist/auth/linked-project.js +30 -64
- package/dist/auth/linked-project.js.map +1 -1
- package/dist/auth/oauth.cjs +205 -0
- package/dist/auth/oauth.cjs.map +1 -0
- package/dist/auth/oauth.d.cts +135 -0
- package/dist/auth/oauth.d.ts +113 -109
- package/dist/auth/oauth.js +185 -252
- package/dist/auth/oauth.js.map +1 -1
- package/dist/auth/poll-for-token.cjs +82 -0
- package/dist/auth/poll-for-token.cjs.map +1 -0
- package/dist/auth/poll-for-token.d.cts +28 -0
- package/dist/auth/poll-for-token.d.ts +23 -15
- package/dist/auth/poll-for-token.js +79 -64
- package/dist/auth/poll-for-token.js.map +1 -1
- package/dist/auth/project.cjs +80 -0
- package/dist/auth/project.cjs.map +1 -0
- package/dist/auth/project.d.cts +44 -0
- package/dist/auth/project.d.ts +12 -8
- package/dist/auth/project.js +70 -72
- package/dist/auth/project.js.map +1 -1
- package/dist/auth/zod.cjs +22 -0
- package/dist/auth/zod.cjs.map +1 -0
- package/dist/auth/zod.js +18 -17
- package/dist/auth/zod.js.map +1 -1
- package/dist/command.cjs +328 -0
- package/dist/command.cjs.map +1 -0
- package/dist/command.d.cts +289 -0
- package/dist/command.d.ts +265 -171
- package/dist/command.js +323 -226
- package/dist/command.js.map +1 -1
- package/dist/constants.d.cts +5 -0
- package/dist/constants.d.ts +5 -1
- package/dist/index.cjs +13 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -6
- package/dist/index.js +7 -17
- package/dist/network-policy.d.cts +100 -0
- package/dist/network-policy.d.ts +32 -28
- package/dist/sandbox.cjs +694 -0
- package/dist/sandbox.cjs.map +1 -0
- package/dist/sandbox.d.cts +796 -0
- package/dist/sandbox.d.ts +783 -711
- package/dist/sandbox.js +684 -615
- package/dist/sandbox.js.map +1 -1
- package/dist/session.cjs +520 -0
- package/dist/session.cjs.map +1 -0
- package/dist/session.d.cts +406 -0
- package/dist/session.d.ts +398 -367
- package/dist/session.js +517 -505
- package/dist/session.js.map +1 -1
- package/dist/snapshot.cjs +116 -0
- package/dist/snapshot.cjs.map +1 -0
- package/dist/snapshot.d.cts +107 -0
- package/dist/snapshot.d.ts +98 -91
- package/dist/snapshot.js +114 -115
- package/dist/snapshot.js.map +1 -1
- package/dist/utils/array.cjs +17 -0
- package/dist/utils/array.cjs.map +1 -0
- package/dist/utils/array.js +12 -15
- package/dist/utils/array.js.map +1 -1
- package/dist/utils/consume-readable.cjs +18 -0
- package/dist/utils/consume-readable.cjs.map +1 -0
- package/dist/utils/consume-readable.js +13 -12
- package/dist/utils/consume-readable.js.map +1 -1
- package/dist/utils/decode-base64-url.cjs +15 -0
- package/dist/utils/decode-base64-url.cjs.map +1 -0
- package/dist/utils/decode-base64-url.js +10 -9
- package/dist/utils/decode-base64-url.js.map +1 -1
- package/dist/utils/dev-credentials.cjs +142 -0
- package/dist/utils/dev-credentials.cjs.map +1 -0
- package/dist/utils/dev-credentials.js +126 -184
- package/dist/utils/dev-credentials.js.map +1 -1
- package/dist/utils/get-credentials.cjs +123 -0
- package/dist/utils/get-credentials.cjs.map +1 -0
- package/dist/utils/get-credentials.d.cts +21 -0
- package/dist/utils/get-credentials.d.ts +19 -61
- package/dist/utils/get-credentials.js +106 -140
- package/dist/utils/get-credentials.js.map +1 -1
- package/dist/utils/log.cjs +25 -0
- package/dist/utils/log.cjs.map +1 -0
- package/dist/utils/log.js +15 -17
- package/dist/utils/log.js.map +1 -1
- package/dist/utils/network-policy.cjs +65 -0
- package/dist/utils/network-policy.cjs.map +1 -0
- package/dist/utils/network-policy.js +58 -77
- package/dist/utils/network-policy.js.map +1 -1
- package/dist/utils/normalizePath.cjs +27 -0
- package/dist/utils/normalizePath.cjs.map +1 -0
- package/dist/utils/normalizePath.js +21 -28
- package/dist/utils/normalizePath.js.map +1 -1
- package/dist/utils/resolveSignal.cjs +20 -0
- package/dist/utils/resolveSignal.cjs.map +1 -0
- package/dist/utils/resolveSignal.d.cts +15 -0
- package/dist/utils/resolveSignal.d.ts +12 -10
- package/dist/utils/resolveSignal.js +14 -17
- package/dist/utils/resolveSignal.js.map +1 -1
- package/dist/utils/sandbox-snapshot.cjs +14 -0
- package/dist/utils/sandbox-snapshot.cjs.map +1 -0
- package/dist/utils/sandbox-snapshot.d.cts +10 -0
- package/dist/utils/sandbox-snapshot.d.ts +11 -0
- package/dist/utils/sandbox-snapshot.js +14 -0
- package/dist/utils/sandbox-snapshot.js.map +1 -0
- package/dist/utils/types.cjs +13 -0
- package/dist/utils/types.cjs.map +1 -0
- package/dist/utils/types.d.cts +11 -0
- package/dist/utils/types.d.ts +5 -7
- package/dist/utils/types.js +8 -8
- package/dist/utils/types.js.map +1 -1
- package/dist/version.cjs +7 -0
- package/dist/version.cjs.map +1 -0
- package/dist/version.js +5 -5
- package/dist/version.js.map +1 -1
- package/package.json +23 -3
- package/dist/api-client/index.js.map +0 -1
- package/dist/auth/api.d.ts +0 -6
- package/dist/auth/error.d.ts +0 -11
- package/dist/auth/index.js.map +0 -1
- package/dist/auth/linked-project.d.ts +0 -10
- package/dist/auth/zod.d.ts +0 -5
- package/dist/constants.js +0 -3
- package/dist/constants.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/network-policy.js +0 -3
- package/dist/network-policy.js.map +0 -1
- package/dist/utils/array.d.ts +0 -9
- package/dist/utils/consume-readable.d.ts +0 -5
- package/dist/utils/convert-sandbox.d.ts +0 -6
- package/dist/utils/convert-sandbox.js +0 -14
- package/dist/utils/convert-sandbox.js.map +0 -1
- package/dist/utils/decode-base64-url.d.ts +0 -7
- package/dist/utils/dev-credentials.d.ts +0 -37
- package/dist/utils/log.d.ts +0 -2
- package/dist/utils/network-policy.d.ts +0 -7
- package/dist/utils/normalizePath.d.ts +0 -17
- package/dist/version.d.ts +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.cjs","names":["getCredentials","APIClient","WORKFLOW_SERIALIZE","WORKFLOW_DESERIALIZE","toSandboxSnapshot","Command","params: RunCommandParams","command","CommandFinished","path","consumeReadable","Snapshot"],"sources":["../src/session.ts"],"sourcesContent":["import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE } from \"@workflow/serde\";\nimport { type SessionMetaData, type SandboxRouteData, APIClient } from \"./api-client/index.js\";\nimport type { Writable } from \"stream\";\nimport { pipeline } from \"stream/promises\";\nimport { createWriteStream } from \"fs\";\nimport { mkdir } from \"fs/promises\";\nimport { dirname, resolve } from \"path\";\nimport { Command, CommandFinished } from \"./command.js\";\nimport { Snapshot } from \"./snapshot.js\";\nimport { consumeReadable } from \"./utils/consume-readable.js\";\nimport type {\n NetworkPolicy,\n NetworkPolicyRule,\n NetworkTransformer,\n} from \"./network-policy.js\";\nimport { toSandboxSnapshot, type SandboxSnapshot } from \"./utils/sandbox-snapshot.js\";\nimport { getCredentials } from \"./utils/get-credentials.js\";\n\nexport type { NetworkPolicy, NetworkPolicyRule, NetworkTransformer };\n\n/**\n * Serialized representation of a Session for @workflow/serde.\n */\nexport interface SerializedSession {\n session: SandboxSnapshot;\n routes: SandboxRouteData[];\n}\n\n/** @inline */\nexport interface RunCommandParams {\n /**\n * The command to execute\n */\n cmd: string;\n /**\n * Arguments to pass to the command\n */\n args?: string[];\n /**\n * Working directory to execute the command in\n */\n cwd?: string;\n /**\n * Environment variables to set for this command\n */\n env?: Record<string, string>;\n /**\n * If true, execute this command with root privileges. Defaults to false.\n */\n sudo?: boolean;\n /**\n * If true, the command will return without waiting for `exitCode`\n */\n detached?: boolean;\n /**\n * A `Writable` stream where `stdout` from the command will be piped\n */\n stdout?: Writable;\n /**\n * A `Writable` stream where `stderr` from the command will be piped\n */\n stderr?: Writable;\n /**\n * An AbortSignal to cancel the command execution\n */\n signal?: AbortSignal;\n}\n\n/**\n * A Session represents a running VM instance within a {@link Sandbox}.\n *\n * Obtain a session via {@link Sandbox.currentSession}.\n */\nexport class Session {\n private _client: APIClient | null = null;\n\n /**\n * Lazily resolve credentials and construct an API client.\n * This is used in step contexts where the Sandbox was deserialized\n * without a client (e.g. when crossing workflow/step boundaries).\n * Uses getCredentials() which resolves from OIDC or env vars.\n * @internal\n */\n private async ensureClient(): Promise<APIClient> {\n \"use step\";\n if (this._client) return this._client;\n const credentials = await getCredentials();\n this._client = new APIClient({\n teamId: credentials.teamId,\n token: credentials.token,\n });\n return this._client;\n }\n\n /**\n * Routes from ports to subdomains.\n * @hidden\n */\n public readonly routes: SandboxRouteData[];\n\n /**\n * Internal metadata about the current session.\n */\n private session: SandboxSnapshot;\n\n private get client(): APIClient {\n if (!this._client) throw new Error(\"API client not initialized\");\n return this._client;\n }\n\n /** @internal */\n get _sessionSnapshot(): SandboxSnapshot {\n return this.session;\n }\n\n /**\n * Unique ID of this session.\n */\n public get sessionId(): string {\n return this.session.id;\n }\n\n public get interactivePort(): number | undefined {\n return this.session.interactivePort ?? undefined;\n }\n\n /**\n * The status of this session.\n */\n public get status(): SessionMetaData[\"status\"] {\n return this.session.status;\n }\n\n /**\n * The creation date of this session.\n */\n public get createdAt(): Date {\n return new Date(this.session.createdAt);\n }\n\n /**\n * The timeout of this session in milliseconds.\n */\n public get timeout(): number {\n return this.session.timeout;\n }\n\n /**\n * The network policy of this session.\n */\n public get networkPolicy(): NetworkPolicy | undefined {\n return this.session.networkPolicy;\n }\n\n /**\n * If the session was created from a snapshot, the ID of that snapshot.\n */\n public get sourceSnapshotId(): string | undefined {\n return this.session.sourceSnapshotId;\n }\n\n /**\n * Memory allocated to this session in MB.\n */\n public get memory(): number {\n return this.session.memory;\n }\n\n /**\n * Number of vCPUs allocated to this session.\n */\n public get vcpus(): number {\n return this.session.vcpus;\n }\n\n /**\n * The region where this session is hosted.\n */\n public get region(): string {\n return this.session.region;\n }\n\n /**\n * Runtime identifier (e.g. \"node24\", \"python3.13\").\n */\n public get runtime(): string {\n return this.session.runtime;\n }\n\n /**\n * The working directory of this session.\n */\n public get cwd(): string {\n return this.session.cwd;\n }\n\n /**\n * When this session was requested.\n */\n public get requestedAt(): Date {\n return new Date(this.session.requestedAt);\n }\n\n /**\n * When this session started running.\n */\n public get startedAt(): Date | undefined {\n return this.session.startedAt != null\n ? new Date(this.session.startedAt)\n : undefined;\n }\n\n /**\n * When this session was requested to stop.\n */\n public get requestedStopAt(): Date | undefined {\n return this.session.requestedStopAt != null\n ? new Date(this.session.requestedStopAt)\n : undefined;\n }\n\n /**\n * When this session was stopped.\n */\n public get stoppedAt(): Date | undefined {\n return this.session.stoppedAt != null\n ? new Date(this.session.stoppedAt)\n : undefined;\n }\n\n /**\n * When this session was aborted.\n */\n public get abortedAt(): Date | undefined {\n return this.session.abortedAt != null\n ? new Date(this.session.abortedAt)\n : undefined;\n }\n\n /**\n * The wall-clock duration of this session in milliseconds.\n */\n public get duration(): number | undefined {\n return this.session.duration;\n }\n\n /**\n * When a snapshot was requested for this session.\n */\n public get snapshottedAt(): Date | undefined {\n return this.session.snapshottedAt != null\n ? new Date(this.session.snapshottedAt)\n : undefined;\n }\n\n /**\n * When this session was last updated.\n */\n public get updatedAt(): Date {\n return new Date(this.session.updatedAt);\n }\n\n /**\n * The amount of active CPU used by the session. Only reported once the VM is\n * stopped.\n */\n public get activeCpuUsageMs(): number | undefined {\n return this.session.activeCpuDurationMs;\n }\n\n /**\n * The amount of network data used by the session. Only reported once the VM\n * is stopped.\n */\n public get networkTransfer():\n | { ingress: number; egress: number }\n | undefined {\n return this.session.networkTransfer;\n }\n\n /**\n * Serialize a Session instance to plain data for @workflow/serde.\n *\n * Although Sandbox handles top-level serialization, Session needs these\n * methods so the Workflow SWC compiler can resolve the class by name.\n * The `new Session(...)` self-reference in WORKFLOW_DESERIALIZE forces\n * rolldown to preserve the class name in the compiled output.\n */\n static [WORKFLOW_SERIALIZE](instance: Session): SerializedSession {\n return {\n session: instance.session,\n routes: instance.routes,\n };\n }\n\n static [WORKFLOW_DESERIALIZE](data: SerializedSession): Session {\n return new Session({ routes: data.routes, snapshot: data.session });\n }\n\n constructor(params: {\n client: APIClient;\n routes: SandboxRouteData[];\n session: SessionMetaData;\n } | {\n /** @internal – used during deserialization with an already-converted snapshot */\n routes: SandboxRouteData[];\n snapshot: SandboxSnapshot;\n }) {\n this.routes = params.routes;\n if (\"snapshot\" in params) {\n this.session = params.snapshot;\n } else {\n this._client = params.client;\n this.session = toSandboxSnapshot(params.session);\n }\n }\n\n /**\n * Get a previously run command by its ID.\n *\n * @param cmdId - ID of the command to retrieve\n * @param opts - Optional parameters.\n * @param opts.signal - An AbortSignal to cancel the operation.\n * @returns A {@link Command} instance representing the command\n */\n async getCommand(\n cmdId: string,\n opts?: { signal?: AbortSignal },\n ): Promise<Command> {\n \"use step\";\n const client = await this.ensureClient();\n const command = await client.getCommand({\n sessionId: this.session.id,\n cmdId,\n signal: opts?.signal,\n });\n\n return new Command({\n client,\n sessionId: this.session.id,\n cmd: command.json.command,\n });\n }\n\n /**\n * Start executing a command in this session.\n *\n * @param command - The command to execute.\n * @param args - Arguments to pass to the command.\n * @param opts - Optional parameters.\n * @param opts.signal - An AbortSignal to cancel the command execution.\n * @returns A {@link CommandFinished} result once execution is done.\n */\n async runCommand(\n command: string,\n args?: string[],\n opts?: { signal?: AbortSignal },\n ): Promise<CommandFinished>;\n\n /**\n * Start executing a command in detached mode.\n *\n * @param params - The command parameters.\n * @returns A {@link Command} instance for the running command.\n */\n async runCommand(\n params: RunCommandParams & { detached: true },\n ): Promise<Command>;\n\n /**\n * Start executing a command in this session.\n *\n * @param params - The command parameters.\n * @returns A {@link CommandFinished} result once execution is done.\n */\n async runCommand(params: RunCommandParams): Promise<CommandFinished>;\n\n async runCommand(\n commandOrParams: string | RunCommandParams,\n args?: string[],\n opts?: { signal?: AbortSignal },\n ): Promise<Command | CommandFinished> {\n \"use step\";\n const client = await this.ensureClient();\n const params: RunCommandParams =\n typeof commandOrParams === \"string\"\n ? { cmd: commandOrParams, args, signal: opts?.signal }\n : commandOrParams;\n const wait = params.detached ? false : true;\n const pipeLogs = async (command: Command): Promise<void> => {\n if (!params.stdout && !params.stderr) {\n return;\n }\n\n try {\n for await (const log of command.logs({ signal: params.signal })) {\n if (log.stream === \"stdout\") {\n params.stdout?.write(log.data);\n } else if (log.stream === \"stderr\") {\n params.stderr?.write(log.data);\n }\n }\n } catch (err) {\n if (params.signal?.aborted) {\n return;\n }\n throw err;\n }\n };\n\n if (wait) {\n const commandStream = await client.runCommand({\n sessionId: this.session.id,\n command: params.cmd,\n args: params.args ?? [],\n cwd: params.cwd,\n env: params.env ?? {},\n sudo: params.sudo ?? false,\n wait: true,\n signal: params.signal,\n });\n\n const command = new Command({\n client,\n sessionId: this.session.id,\n cmd: commandStream.command,\n });\n\n const [finished] = await Promise.all([\n commandStream.finished,\n pipeLogs(command),\n ]);\n return new CommandFinished({\n client,\n sessionId: this.session.id,\n cmd: finished,\n exitCode: finished.exitCode ?? 0,\n });\n }\n\n const commandResponse = await client.runCommand({\n sessionId: this.session.id,\n command: params.cmd,\n args: params.args ?? [],\n cwd: params.cwd,\n env: params.env ?? {},\n sudo: params.sudo ?? false,\n signal: params.signal,\n });\n\n const command = new Command({\n client,\n sessionId: this.session.id,\n cmd: commandResponse.json.command,\n });\n\n void pipeLogs(command).catch((err) => {\n if (params.signal?.aborted) {\n return;\n }\n (params.stderr ?? params.stdout)?.emit(\"error\", err);\n });\n\n return command;\n }\n\n /**\n * Create a directory in the filesystem of this session.\n *\n * @param path - Path of the directory to create\n * @param opts - Optional parameters.\n * @param opts.signal - An AbortSignal to cancel the operation.\n */\n async mkDir(path: string, opts?: { signal?: AbortSignal }): Promise<void> {\n \"use step\";\n const client = await this.ensureClient();\n await client.mkDir({\n sessionId: this.session.id,\n path: path,\n signal: opts?.signal,\n });\n }\n\n /**\n * Read a file from the filesystem of this session as a stream.\n *\n * @param file - File to read, with path and optional cwd\n * @param opts - Optional parameters.\n * @param opts.signal - An AbortSignal to cancel the operation.\n * @returns A promise that resolves to a ReadableStream containing the file contents, or null if file not found\n */\n async readFile(\n file: { path: string; cwd?: string },\n opts?: { signal?: AbortSignal },\n ): Promise<NodeJS.ReadableStream | null> {\n \"use step\";\n const client = await this.ensureClient();\n return client.readFile({\n sessionId: this.session.id,\n path: file.path,\n cwd: file.cwd,\n signal: opts?.signal,\n });\n }\n\n /**\n * Read a file from the filesystem of this session as a Buffer.\n *\n * @param file - File to read, with path and optional cwd\n * @param opts - Optional parameters.\n * @param opts.signal - An AbortSignal to cancel the operation.\n * @returns A promise that resolves to the file contents as a Buffer, or null if file not found\n */\n async readFileToBuffer(\n file: { path: string; cwd?: string },\n opts?: { signal?: AbortSignal },\n ): Promise<Buffer | null> {\n \"use step\";\n const client = await this.ensureClient();\n const stream = await client.readFile({\n sessionId: this.session.id,\n path: file.path,\n cwd: file.cwd,\n signal: opts?.signal,\n });\n\n if (stream === null) {\n return null;\n }\n\n return consumeReadable(stream);\n }\n\n /**\n * Download a file from the session to the local filesystem.\n *\n * @param src - Source file on the session, with path and optional cwd\n * @param dst - Destination file on the local machine, with path and optional cwd\n * @param opts - Optional parameters.\n * @param opts.mkdirRecursive - If true, create parent directories for the destination if they don't exist.\n * @param opts.signal - An AbortSignal to cancel the operation.\n * @returns The absolute path to the written file, or null if the source file was not found\n */\n async downloadFile(\n src: { path: string; cwd?: string },\n dst: { path: string; cwd?: string },\n opts?: { mkdirRecursive?: boolean; signal?: AbortSignal },\n ): Promise<string | null> {\n \"use step\";\n const client = await this.ensureClient();\n if (!src?.path) {\n throw new Error(\"downloadFile: source path is required\");\n }\n\n if (!dst?.path) {\n throw new Error(\"downloadFile: destination path is required\");\n }\n\n const stream = await client.readFile({\n sessionId: this.session.id,\n path: src.path,\n cwd: src.cwd,\n signal: opts?.signal,\n });\n\n if (stream === null) {\n return null;\n }\n\n try {\n const dstPath = resolve(dst.cwd ?? \"\", dst.path);\n if (opts?.mkdirRecursive) {\n await mkdir(dirname(dstPath), { recursive: true });\n }\n await pipeline(stream, createWriteStream(dstPath), {\n signal: opts?.signal,\n });\n return dstPath;\n } finally {\n stream.destroy();\n }\n }\n\n /**\n * Write files to the filesystem of this session.\n * Defaults to writing to /vercel/sandbox unless an absolute path is specified.\n * Writes files using the `vercel-sandbox` user.\n *\n * @param files - Array of files with path and stream/buffer contents\n * @param opts - Optional parameters.\n * @param opts.signal - An AbortSignal to cancel the operation.\n * @returns A promise that resolves when the files are written\n */\n async writeFiles(\n files: { path: string; content: string | Uint8Array; mode?: number }[],\n opts?: { signal?: AbortSignal },\n ) {\n \"use step\";\n const client = await this.ensureClient();\n return client.writeFiles({\n sessionId: this.session.id,\n cwd: this.session.cwd,\n extractDir: \"/\",\n files: files,\n signal: opts?.signal,\n });\n }\n\n /**\n * Get the public domain of a port of this session.\n *\n * @param p - Port number to resolve\n * @returns A full domain (e.g. `https://subdomain.vercel.run`)\n * @throws If the port has no associated route\n */\n domain(p: number): string {\n const route = this.routes.find(({ port }) => port == p);\n if (route) {\n return `https://${route.subdomain}.vercel.run`;\n } else {\n throw new Error(`No route for port ${p}`);\n }\n }\n\n /**\n * Stop this session.\n *\n * @param opts - Optional parameters.\n * @param opts.signal - An AbortSignal to cancel the operation.\n * @param opts.blocking - If true, poll until the session has fully stopped and return the final state.\n * @returns The session at the time the stop was acknowledged, or after fully stopped if `blocking` is true.\n */\n async stop(opts?: {\n signal?: AbortSignal;\n blocking?: boolean;\n }): Promise<SandboxSnapshot> {\n \"use step\";\n const client = await this.ensureClient();\n const response = await client.stopSession({\n sessionId: this.session.id,\n signal: opts?.signal,\n blocking: opts?.blocking,\n });\n this.session = toSandboxSnapshot(response.json.session);\n return this.session;\n }\n\n /**\n * Update the current session's settings.\n *\n * @param params - Fields to update.\n * @param params.networkPolicy - The new network policy to apply.\n * @param opts - Optional parameters.\n * @param opts.signal - An AbortSignal to cancel the operation.\n *\n * @example\n * // Restrict to specific domains\n * await session.update({\n * networkPolicy: {\n * allow: [\"*.npmjs.org\", \"github.com\"],\n * }\n * });\n *\n * @example\n * // Inject credentials with per-domain transformers\n * await session.update({\n * networkPolicy: {\n * allow: {\n * \"ai-gateway.vercel.sh\": [{\n * transform: [{\n * headers: { authorization: \"Bearer ...\" }\n * }]\n * }],\n * \"*\": []\n * }\n * }\n * });\n *\n * @example\n * // Deny all network access\n * await session.update({ networkPolicy: \"deny-all\" });\n */\n async update(\n params: {\n networkPolicy?: NetworkPolicy;\n },\n opts?: { signal?: AbortSignal },\n ): Promise<void> {\n \"use step\";\n if (params.networkPolicy !== undefined) {\n const client = await this.ensureClient();\n const response = await client.updateNetworkPolicy({\n sessionId: this.session.id,\n networkPolicy: params.networkPolicy,\n signal: opts?.signal,\n });\n\n // Update the internal session with the new network policy\n this.session = toSandboxSnapshot(response.json.session);\n }\n }\n\n /**\n * Extend the timeout of the session by the specified duration.\n *\n * This allows you to extend the lifetime of a session up until the maximum\n * execution timeout for your plan.\n *\n * @param duration - The duration in milliseconds to extend the timeout by\n * @param opts - Optional parameters.\n * @param opts.signal - An AbortSignal to cancel the operation.\n * @returns A promise that resolves when the timeout is extended\n *\n * @example\n * const sandbox = await Sandbox.create({ timeout: ms('10m') });\n * const session = sandbox.currentSession();\n * // Extends timeout by 5 minutes, to a total of 15 minutes.\n * await session.extendTimeout(ms('5m'));\n */\n async extendTimeout(\n duration: number,\n opts?: { signal?: AbortSignal },\n ): Promise<void> {\n \"use step\";\n const client = await this.ensureClient();\n const response = await client.extendTimeout({\n sessionId: this.session.id,\n duration,\n signal: opts?.signal,\n });\n\n // Update the internal sandbox metadata with the new timeout value\n this.session = toSandboxSnapshot(response.json.session);\n }\n\n /**\n * Create a snapshot from this currently running session. New sandboxes can\n * then be created from this snapshot using {@link Sandbox.create}.\n *\n * Note: this session will be stopped as part of the snapshot creation process.\n *\n * @param opts - Optional parameters.\n * @param opts.expiration - Optional expiration time in milliseconds. Use 0 for no expiration at all.\n * @param opts.signal - An AbortSignal to cancel the operation.\n * @returns A promise that resolves to the Snapshot instance\n */\n async snapshot(opts?: {\n expiration?: number;\n signal?: AbortSignal;\n }): Promise<Snapshot> {\n \"use step\";\n const client = await this.ensureClient();\n const response = await client.createSnapshot({\n sessionId: this.session.id,\n expiration: opts?.expiration,\n signal: opts?.signal,\n });\n\n this.session = toSandboxSnapshot(response.json.session);\n\n return new Snapshot({\n client,\n snapshot: response.json.snapshot,\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAyEA,IAAa,UAAb,MAAa,QAAQ;;;;;;;;CAUnB,MAAc,eAAmC;AAC/C;AACA,MAAI,KAAK,QAAS,QAAO,KAAK;EAC9B,MAAM,cAAc,MAAMA,wCAAgB;AAC1C,OAAK,UAAU,IAAIC,6BAAU;GAC3B,QAAQ,YAAY;GACpB,OAAO,YAAY;GACpB,CAAC;AACF,SAAO,KAAK;;CAcd,IAAY,SAAoB;AAC9B,MAAI,CAAC,KAAK,QAAS,OAAM,IAAI,MAAM,6BAA6B;AAChE,SAAO,KAAK;;;CAId,IAAI,mBAAoC;AACtC,SAAO,KAAK;;;;;CAMd,IAAW,YAAoB;AAC7B,SAAO,KAAK,QAAQ;;CAGtB,IAAW,kBAAsC;AAC/C,SAAO,KAAK,QAAQ,mBAAmB;;;;;CAMzC,IAAW,SAAoC;AAC7C,SAAO,KAAK,QAAQ;;;;;CAMtB,IAAW,YAAkB;AAC3B,SAAO,IAA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;MAElC,OAAM,IAAI,MAAM,qBAAqB,IAAI;;;;;;;;;;CAY7C,MAAM,KAAK,MAGkB;AAC3B;AAOA,OAAK,UAAUN,4CALE,OADF,MAAM,KAAK,cAAc,EACV,YAAY;GACxC,WAAW,KAAK,QAAQ;GACxB,QAAQ,MAAM;GACd,UAAU,MAAM;GACjB,CAAC,EACwC,KAAK,QAAQ;AACvD,SAAO,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAsCd,MAAM,OACJ,QAGA,MACe;AACf;AACA,MAAI,OAAO,kBAAkB,OAS3B,MAAK,UAAUA,4CAPE,OADF,MAAM,KAAK,cAAc,EACV,oBAAoB;GAChD,WAAW,KAAK,QAAQ;GACxB,eAAe,OAAO;GACtB,QAAQ,MAAM;GACf,CAAC,EAGwC,KAAK,QAAQ;;;;;;;;;;;;;;;;;;;CAqB3D,MAAM,cACJ,UACA,MACe;AACf;AASA,OAAK,UAAUA,4CAPE,OADF,MAAM,KAAK,cAAc,EACV,cAAc;GAC1C,WAAW,KAAK,QAAQ;GACxB;GACA,QAAQ,MAAM;GACf,CAAC,EAGwC,KAAK,QAAQ;;;;;;;;;;;;;CAczD,MAAM,SAAS,MAGO;AACpB;EACA,MAAM,SAAS,MAAM,KAAK,cAAc;EACxC,MAAM,WAAW,MAAM,OAAO,eAAe;GAC3C,WAAW,KAAK,QAAQ;GACxB,YAAY,MAAM;GAClB,QAAQ,MAAM;GACf,CAAC;AAEF,OAAK,UAAUA,2CAAkB,SAAS,KAAK,QAAQ;AAEvD,SAAO,IAAIO,0BAAS;GAClB;GACA,UAAU,SAAS,KAAK;GACzB,CAAC"}
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import { SandboxRouteData, SessionMetaData } from "./api-client/validators.cjs";
|
|
2
|
+
import { NetworkPolicy, NetworkPolicyRule, NetworkTransformer } from "./network-policy.cjs";
|
|
3
|
+
import { APIClient } from "./api-client/api-client.cjs";
|
|
4
|
+
import { Command, CommandFinished } from "./command.cjs";
|
|
5
|
+
import { Snapshot } from "./snapshot.cjs";
|
|
6
|
+
import { SandboxSnapshot } from "./utils/sandbox-snapshot.cjs";
|
|
7
|
+
import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE } from "@workflow/serde";
|
|
8
|
+
import { Writable } from "stream";
|
|
9
|
+
|
|
10
|
+
//#region src/session.d.ts
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Serialized representation of a Session for @workflow/serde.
|
|
14
|
+
*/
|
|
15
|
+
interface SerializedSession {
|
|
16
|
+
session: SandboxSnapshot;
|
|
17
|
+
routes: SandboxRouteData[];
|
|
18
|
+
}
|
|
19
|
+
/** @inline */
|
|
20
|
+
interface RunCommandParams {
|
|
21
|
+
/**
|
|
22
|
+
* The command to execute
|
|
23
|
+
*/
|
|
24
|
+
cmd: string;
|
|
25
|
+
/**
|
|
26
|
+
* Arguments to pass to the command
|
|
27
|
+
*/
|
|
28
|
+
args?: string[];
|
|
29
|
+
/**
|
|
30
|
+
* Working directory to execute the command in
|
|
31
|
+
*/
|
|
32
|
+
cwd?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Environment variables to set for this command
|
|
35
|
+
*/
|
|
36
|
+
env?: Record<string, string>;
|
|
37
|
+
/**
|
|
38
|
+
* If true, execute this command with root privileges. Defaults to false.
|
|
39
|
+
*/
|
|
40
|
+
sudo?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* If true, the command will return without waiting for `exitCode`
|
|
43
|
+
*/
|
|
44
|
+
detached?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* A `Writable` stream where `stdout` from the command will be piped
|
|
47
|
+
*/
|
|
48
|
+
stdout?: Writable;
|
|
49
|
+
/**
|
|
50
|
+
* A `Writable` stream where `stderr` from the command will be piped
|
|
51
|
+
*/
|
|
52
|
+
stderr?: Writable;
|
|
53
|
+
/**
|
|
54
|
+
* An AbortSignal to cancel the command execution
|
|
55
|
+
*/
|
|
56
|
+
signal?: AbortSignal;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* A Session represents a running VM instance within a {@link Sandbox}.
|
|
60
|
+
*
|
|
61
|
+
* Obtain a session via {@link Sandbox.currentSession}.
|
|
62
|
+
*/
|
|
63
|
+
declare class Session {
|
|
64
|
+
private _client;
|
|
65
|
+
/**
|
|
66
|
+
* Lazily resolve credentials and construct an API client.
|
|
67
|
+
* This is used in step contexts where the Sandbox was deserialized
|
|
68
|
+
* without a client (e.g. when crossing workflow/step boundaries).
|
|
69
|
+
* Uses getCredentials() which resolves from OIDC or env vars.
|
|
70
|
+
* @internal
|
|
71
|
+
*/
|
|
72
|
+
private ensureClient;
|
|
73
|
+
/**
|
|
74
|
+
* Routes from ports to subdomains.
|
|
75
|
+
* @hidden
|
|
76
|
+
*/
|
|
77
|
+
readonly routes: SandboxRouteData[];
|
|
78
|
+
/**
|
|
79
|
+
* Internal metadata about the current session.
|
|
80
|
+
*/
|
|
81
|
+
private session;
|
|
82
|
+
private get client();
|
|
83
|
+
/** @internal */
|
|
84
|
+
get _sessionSnapshot(): SandboxSnapshot;
|
|
85
|
+
/**
|
|
86
|
+
* Unique ID of this session.
|
|
87
|
+
*/
|
|
88
|
+
get sessionId(): string;
|
|
89
|
+
get interactivePort(): number | undefined;
|
|
90
|
+
/**
|
|
91
|
+
* The status of this session.
|
|
92
|
+
*/
|
|
93
|
+
get status(): SessionMetaData["status"];
|
|
94
|
+
/**
|
|
95
|
+
* The creation date of this session.
|
|
96
|
+
*/
|
|
97
|
+
get createdAt(): Date;
|
|
98
|
+
/**
|
|
99
|
+
* The timeout of this session in milliseconds.
|
|
100
|
+
*/
|
|
101
|
+
get timeout(): number;
|
|
102
|
+
/**
|
|
103
|
+
* The network policy of this session.
|
|
104
|
+
*/
|
|
105
|
+
get networkPolicy(): NetworkPolicy | undefined;
|
|
106
|
+
/**
|
|
107
|
+
* If the session was created from a snapshot, the ID of that snapshot.
|
|
108
|
+
*/
|
|
109
|
+
get sourceSnapshotId(): string | undefined;
|
|
110
|
+
/**
|
|
111
|
+
* Memory allocated to this session in MB.
|
|
112
|
+
*/
|
|
113
|
+
get memory(): number;
|
|
114
|
+
/**
|
|
115
|
+
* Number of vCPUs allocated to this session.
|
|
116
|
+
*/
|
|
117
|
+
get vcpus(): number;
|
|
118
|
+
/**
|
|
119
|
+
* The region where this session is hosted.
|
|
120
|
+
*/
|
|
121
|
+
get region(): string;
|
|
122
|
+
/**
|
|
123
|
+
* Runtime identifier (e.g. "node24", "python3.13").
|
|
124
|
+
*/
|
|
125
|
+
get runtime(): string;
|
|
126
|
+
/**
|
|
127
|
+
* The working directory of this session.
|
|
128
|
+
*/
|
|
129
|
+
get cwd(): string;
|
|
130
|
+
/**
|
|
131
|
+
* When this session was requested.
|
|
132
|
+
*/
|
|
133
|
+
get requestedAt(): Date;
|
|
134
|
+
/**
|
|
135
|
+
* When this session started running.
|
|
136
|
+
*/
|
|
137
|
+
get startedAt(): Date | undefined;
|
|
138
|
+
/**
|
|
139
|
+
* When this session was requested to stop.
|
|
140
|
+
*/
|
|
141
|
+
get requestedStopAt(): Date | undefined;
|
|
142
|
+
/**
|
|
143
|
+
* When this session was stopped.
|
|
144
|
+
*/
|
|
145
|
+
get stoppedAt(): Date | undefined;
|
|
146
|
+
/**
|
|
147
|
+
* When this session was aborted.
|
|
148
|
+
*/
|
|
149
|
+
get abortedAt(): Date | undefined;
|
|
150
|
+
/**
|
|
151
|
+
* The wall-clock duration of this session in milliseconds.
|
|
152
|
+
*/
|
|
153
|
+
get duration(): number | undefined;
|
|
154
|
+
/**
|
|
155
|
+
* When a snapshot was requested for this session.
|
|
156
|
+
*/
|
|
157
|
+
get snapshottedAt(): Date | undefined;
|
|
158
|
+
/**
|
|
159
|
+
* When this session was last updated.
|
|
160
|
+
*/
|
|
161
|
+
get updatedAt(): Date;
|
|
162
|
+
/**
|
|
163
|
+
* The amount of active CPU used by the session. Only reported once the VM is
|
|
164
|
+
* stopped.
|
|
165
|
+
*/
|
|
166
|
+
get activeCpuUsageMs(): number | undefined;
|
|
167
|
+
/**
|
|
168
|
+
* The amount of network data used by the session. Only reported once the VM
|
|
169
|
+
* is stopped.
|
|
170
|
+
*/
|
|
171
|
+
get networkTransfer(): {
|
|
172
|
+
ingress: number;
|
|
173
|
+
egress: number;
|
|
174
|
+
} | undefined;
|
|
175
|
+
/**
|
|
176
|
+
* Serialize a Session instance to plain data for @workflow/serde.
|
|
177
|
+
*
|
|
178
|
+
* Although Sandbox handles top-level serialization, Session needs these
|
|
179
|
+
* methods so the Workflow SWC compiler can resolve the class by name.
|
|
180
|
+
* The `new Session(...)` self-reference in WORKFLOW_DESERIALIZE forces
|
|
181
|
+
* rolldown to preserve the class name in the compiled output.
|
|
182
|
+
*/
|
|
183
|
+
static [WORKFLOW_SERIALIZE](instance: Session): SerializedSession;
|
|
184
|
+
static [WORKFLOW_DESERIALIZE](data: SerializedSession): Session;
|
|
185
|
+
constructor(params: {
|
|
186
|
+
client: APIClient;
|
|
187
|
+
routes: SandboxRouteData[];
|
|
188
|
+
session: SessionMetaData;
|
|
189
|
+
} | {
|
|
190
|
+
/** @internal – used during deserialization with an already-converted snapshot */
|
|
191
|
+
routes: SandboxRouteData[];
|
|
192
|
+
snapshot: SandboxSnapshot;
|
|
193
|
+
});
|
|
194
|
+
/**
|
|
195
|
+
* Get a previously run command by its ID.
|
|
196
|
+
*
|
|
197
|
+
* @param cmdId - ID of the command to retrieve
|
|
198
|
+
* @param opts - Optional parameters.
|
|
199
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
200
|
+
* @returns A {@link Command} instance representing the command
|
|
201
|
+
*/
|
|
202
|
+
getCommand(cmdId: string, opts?: {
|
|
203
|
+
signal?: AbortSignal;
|
|
204
|
+
}): Promise<Command>;
|
|
205
|
+
/**
|
|
206
|
+
* Start executing a command in this session.
|
|
207
|
+
*
|
|
208
|
+
* @param command - The command to execute.
|
|
209
|
+
* @param args - Arguments to pass to the command.
|
|
210
|
+
* @param opts - Optional parameters.
|
|
211
|
+
* @param opts.signal - An AbortSignal to cancel the command execution.
|
|
212
|
+
* @returns A {@link CommandFinished} result once execution is done.
|
|
213
|
+
*/
|
|
214
|
+
runCommand(command: string, args?: string[], opts?: {
|
|
215
|
+
signal?: AbortSignal;
|
|
216
|
+
}): Promise<CommandFinished>;
|
|
217
|
+
/**
|
|
218
|
+
* Start executing a command in detached mode.
|
|
219
|
+
*
|
|
220
|
+
* @param params - The command parameters.
|
|
221
|
+
* @returns A {@link Command} instance for the running command.
|
|
222
|
+
*/
|
|
223
|
+
runCommand(params: RunCommandParams & {
|
|
224
|
+
detached: true;
|
|
225
|
+
}): Promise<Command>;
|
|
226
|
+
/**
|
|
227
|
+
* Start executing a command in this session.
|
|
228
|
+
*
|
|
229
|
+
* @param params - The command parameters.
|
|
230
|
+
* @returns A {@link CommandFinished} result once execution is done.
|
|
231
|
+
*/
|
|
232
|
+
runCommand(params: RunCommandParams): Promise<CommandFinished>;
|
|
233
|
+
/**
|
|
234
|
+
* Create a directory in the filesystem of this session.
|
|
235
|
+
*
|
|
236
|
+
* @param path - Path of the directory to create
|
|
237
|
+
* @param opts - Optional parameters.
|
|
238
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
239
|
+
*/
|
|
240
|
+
mkDir(path: string, opts?: {
|
|
241
|
+
signal?: AbortSignal;
|
|
242
|
+
}): Promise<void>;
|
|
243
|
+
/**
|
|
244
|
+
* Read a file from the filesystem of this session as a stream.
|
|
245
|
+
*
|
|
246
|
+
* @param file - File to read, with path and optional cwd
|
|
247
|
+
* @param opts - Optional parameters.
|
|
248
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
249
|
+
* @returns A promise that resolves to a ReadableStream containing the file contents, or null if file not found
|
|
250
|
+
*/
|
|
251
|
+
readFile(file: {
|
|
252
|
+
path: string;
|
|
253
|
+
cwd?: string;
|
|
254
|
+
}, opts?: {
|
|
255
|
+
signal?: AbortSignal;
|
|
256
|
+
}): Promise<NodeJS.ReadableStream | null>;
|
|
257
|
+
/**
|
|
258
|
+
* Read a file from the filesystem of this session as a Buffer.
|
|
259
|
+
*
|
|
260
|
+
* @param file - File to read, with path and optional cwd
|
|
261
|
+
* @param opts - Optional parameters.
|
|
262
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
263
|
+
* @returns A promise that resolves to the file contents as a Buffer, or null if file not found
|
|
264
|
+
*/
|
|
265
|
+
readFileToBuffer(file: {
|
|
266
|
+
path: string;
|
|
267
|
+
cwd?: string;
|
|
268
|
+
}, opts?: {
|
|
269
|
+
signal?: AbortSignal;
|
|
270
|
+
}): Promise<Buffer | null>;
|
|
271
|
+
/**
|
|
272
|
+
* Download a file from the session to the local filesystem.
|
|
273
|
+
*
|
|
274
|
+
* @param src - Source file on the session, with path and optional cwd
|
|
275
|
+
* @param dst - Destination file on the local machine, with path and optional cwd
|
|
276
|
+
* @param opts - Optional parameters.
|
|
277
|
+
* @param opts.mkdirRecursive - If true, create parent directories for the destination if they don't exist.
|
|
278
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
279
|
+
* @returns The absolute path to the written file, or null if the source file was not found
|
|
280
|
+
*/
|
|
281
|
+
downloadFile(src: {
|
|
282
|
+
path: string;
|
|
283
|
+
cwd?: string;
|
|
284
|
+
}, dst: {
|
|
285
|
+
path: string;
|
|
286
|
+
cwd?: string;
|
|
287
|
+
}, opts?: {
|
|
288
|
+
mkdirRecursive?: boolean;
|
|
289
|
+
signal?: AbortSignal;
|
|
290
|
+
}): Promise<string | null>;
|
|
291
|
+
/**
|
|
292
|
+
* Write files to the filesystem of this session.
|
|
293
|
+
* Defaults to writing to /vercel/sandbox unless an absolute path is specified.
|
|
294
|
+
* Writes files using the `vercel-sandbox` user.
|
|
295
|
+
*
|
|
296
|
+
* @param files - Array of files with path and stream/buffer contents
|
|
297
|
+
* @param opts - Optional parameters.
|
|
298
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
299
|
+
* @returns A promise that resolves when the files are written
|
|
300
|
+
*/
|
|
301
|
+
writeFiles(files: {
|
|
302
|
+
path: string;
|
|
303
|
+
content: string | Uint8Array;
|
|
304
|
+
mode?: number;
|
|
305
|
+
}[], opts?: {
|
|
306
|
+
signal?: AbortSignal;
|
|
307
|
+
}): Promise<void>;
|
|
308
|
+
/**
|
|
309
|
+
* Get the public domain of a port of this session.
|
|
310
|
+
*
|
|
311
|
+
* @param p - Port number to resolve
|
|
312
|
+
* @returns A full domain (e.g. `https://subdomain.vercel.run`)
|
|
313
|
+
* @throws If the port has no associated route
|
|
314
|
+
*/
|
|
315
|
+
domain(p: number): string;
|
|
316
|
+
/**
|
|
317
|
+
* Stop this session.
|
|
318
|
+
*
|
|
319
|
+
* @param opts - Optional parameters.
|
|
320
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
321
|
+
* @param opts.blocking - If true, poll until the session has fully stopped and return the final state.
|
|
322
|
+
* @returns The session at the time the stop was acknowledged, or after fully stopped if `blocking` is true.
|
|
323
|
+
*/
|
|
324
|
+
stop(opts?: {
|
|
325
|
+
signal?: AbortSignal;
|
|
326
|
+
blocking?: boolean;
|
|
327
|
+
}): Promise<SandboxSnapshot>;
|
|
328
|
+
/**
|
|
329
|
+
* Update the current session's settings.
|
|
330
|
+
*
|
|
331
|
+
* @param params - Fields to update.
|
|
332
|
+
* @param params.networkPolicy - The new network policy to apply.
|
|
333
|
+
* @param opts - Optional parameters.
|
|
334
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
335
|
+
*
|
|
336
|
+
* @example
|
|
337
|
+
* // Restrict to specific domains
|
|
338
|
+
* await session.update({
|
|
339
|
+
* networkPolicy: {
|
|
340
|
+
* allow: ["*.npmjs.org", "github.com"],
|
|
341
|
+
* }
|
|
342
|
+
* });
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
* // Inject credentials with per-domain transformers
|
|
346
|
+
* await session.update({
|
|
347
|
+
* networkPolicy: {
|
|
348
|
+
* allow: {
|
|
349
|
+
* "ai-gateway.vercel.sh": [{
|
|
350
|
+
* transform: [{
|
|
351
|
+
* headers: { authorization: "Bearer ..." }
|
|
352
|
+
* }]
|
|
353
|
+
* }],
|
|
354
|
+
* "*": []
|
|
355
|
+
* }
|
|
356
|
+
* }
|
|
357
|
+
* });
|
|
358
|
+
*
|
|
359
|
+
* @example
|
|
360
|
+
* // Deny all network access
|
|
361
|
+
* await session.update({ networkPolicy: "deny-all" });
|
|
362
|
+
*/
|
|
363
|
+
update(params: {
|
|
364
|
+
networkPolicy?: NetworkPolicy;
|
|
365
|
+
}, opts?: {
|
|
366
|
+
signal?: AbortSignal;
|
|
367
|
+
}): Promise<void>;
|
|
368
|
+
/**
|
|
369
|
+
* Extend the timeout of the session by the specified duration.
|
|
370
|
+
*
|
|
371
|
+
* This allows you to extend the lifetime of a session up until the maximum
|
|
372
|
+
* execution timeout for your plan.
|
|
373
|
+
*
|
|
374
|
+
* @param duration - The duration in milliseconds to extend the timeout by
|
|
375
|
+
* @param opts - Optional parameters.
|
|
376
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
377
|
+
* @returns A promise that resolves when the timeout is extended
|
|
378
|
+
*
|
|
379
|
+
* @example
|
|
380
|
+
* const sandbox = await Sandbox.create({ timeout: ms('10m') });
|
|
381
|
+
* const session = sandbox.currentSession();
|
|
382
|
+
* // Extends timeout by 5 minutes, to a total of 15 minutes.
|
|
383
|
+
* await session.extendTimeout(ms('5m'));
|
|
384
|
+
*/
|
|
385
|
+
extendTimeout(duration: number, opts?: {
|
|
386
|
+
signal?: AbortSignal;
|
|
387
|
+
}): Promise<void>;
|
|
388
|
+
/**
|
|
389
|
+
* Create a snapshot from this currently running session. New sandboxes can
|
|
390
|
+
* then be created from this snapshot using {@link Sandbox.create}.
|
|
391
|
+
*
|
|
392
|
+
* Note: this session will be stopped as part of the snapshot creation process.
|
|
393
|
+
*
|
|
394
|
+
* @param opts - Optional parameters.
|
|
395
|
+
* @param opts.expiration - Optional expiration time in milliseconds. Use 0 for no expiration at all.
|
|
396
|
+
* @param opts.signal - An AbortSignal to cancel the operation.
|
|
397
|
+
* @returns A promise that resolves to the Snapshot instance
|
|
398
|
+
*/
|
|
399
|
+
snapshot(opts?: {
|
|
400
|
+
expiration?: number;
|
|
401
|
+
signal?: AbortSignal;
|
|
402
|
+
}): Promise<Snapshot>;
|
|
403
|
+
}
|
|
404
|
+
//#endregion
|
|
405
|
+
export { RunCommandParams, Session };
|
|
406
|
+
//# sourceMappingURL=session.d.cts.map
|