@vercel/sandbox 1.8.1 → 1.9.1
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 +375 -0
- package/dist/api-client/api-client.cjs.map +1 -0
- package/dist/api-client/api-client.d.cts +403 -0
- package/dist/api-client/api-client.d.ts +397 -391
- package/dist/api-client/api-client.js +365 -404
- package/dist/api-client/api-client.js.map +1 -1
- package/dist/api-client/api-error.cjs +31 -0
- package/dist/api-client/api-error.cjs.map +1 -0
- package/dist/api-client/api-error.d.cts +27 -0
- package/dist/api-client/api-error.d.ts +19 -16
- package/dist/api-client/api-error.js +27 -32
- package/dist/api-client/api-error.js.map +1 -1
- package/dist/api-client/base-client.cjs +113 -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 +101 -118
- 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 -42
- 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 +149 -0
- package/dist/api-client/validators.cjs.map +1 -0
- package/dist/api-client/validators.d.cts +1677 -0
- package/dist/api-client/validators.d.ts +1501 -2412
- package/dist/api-client/validators.js +124 -154
- 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 +326 -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 +321 -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 +11 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -5
- package/dist/index.js +6 -15
- package/dist/network-policy.d.cts +100 -0
- package/dist/network-policy.d.ts +32 -28
- package/dist/sandbox.cjs +543 -0
- package/dist/sandbox.cjs.map +1 -0
- package/dist/sandbox.d.cts +538 -0
- package/dist/sandbox.d.ts +521 -472
- package/dist/sandbox.js +539 -506
- package/dist/sandbox.js.map +1 -1
- package/dist/snapshot.cjs +116 -0
- package/dist/snapshot.cjs.map +1 -0
- package/dist/snapshot.d.cts +109 -0
- package/dist/snapshot.d.ts +100 -92
- package/dist/snapshot.js +114 -114
- 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
|
@@ -1,411 +1,372 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const oidc_1 = require("@vercel/oidc");
|
|
19
|
-
const network_policy_1 = require("../utils/network-policy");
|
|
20
|
-
const types_1 = require("../utils/types");
|
|
21
|
-
const promises_1 = require("node:timers/promises");
|
|
1
|
+
import { APIError, StreamError } from "./api-error.js";
|
|
2
|
+
import { BaseClient, parseOrThrow } from "./base-client.js";
|
|
3
|
+
import { CommandFinishedResponse, CommandResponse, CreateSnapshotResponse, EmptyResponse, ExtendTimeoutResponse, LogLine, SandboxAndRoutesResponse, SandboxResponse, SandboxesResponse, SnapshotResponse, SnapshotsResponse, UpdateNetworkPolicyResponse } from "./validators.js";
|
|
4
|
+
import { FileWriter } from "./file-writer.js";
|
|
5
|
+
import { VERSION } from "../version.js";
|
|
6
|
+
import { consumeReadable } from "../utils/consume-readable.js";
|
|
7
|
+
import { normalizePath } from "../utils/normalizePath.js";
|
|
8
|
+
import { toAPINetworkPolicy } from "../utils/network-policy.js";
|
|
9
|
+
import { getPrivateParams } from "../utils/types.js";
|
|
10
|
+
import { setTimeout } from "node:timers/promises";
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
import { Readable } from "stream";
|
|
13
|
+
import jsonlines from "jsonlines";
|
|
14
|
+
import os from "os";
|
|
15
|
+
import { getVercelOidcToken } from "@vercel/oidc";
|
|
16
|
+
|
|
17
|
+
//#region src/api-client/api-client.ts
|
|
22
18
|
function decodeUnverifiedToken(token) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return null;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
class APIClient extends base_client_1.BaseClient {
|
|
38
|
-
constructor(params) {
|
|
39
|
-
super({
|
|
40
|
-
baseUrl: params.baseUrl ?? "https://vercel.com/api",
|
|
41
|
-
token: params.token,
|
|
42
|
-
debug: false,
|
|
43
|
-
fetch: params.fetch,
|
|
44
|
-
});
|
|
45
|
-
this.teamId = params.teamId;
|
|
46
|
-
this.isJwtToken = false;
|
|
47
|
-
const claims = decodeUnverifiedToken(params.token);
|
|
48
|
-
if (claims) {
|
|
49
|
-
this.isJwtToken = true;
|
|
50
|
-
this.projectId = claims.project_id;
|
|
51
|
-
this.teamId = claims.owner_id;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
async ensureValidToken() {
|
|
55
|
-
if (!this.isJwtToken) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
try {
|
|
59
|
-
// Use getVercelOidcToken to refresh the token with team/project scope
|
|
60
|
-
const freshToken = await (0, oidc_1.getVercelOidcToken)({
|
|
61
|
-
expirationBufferMs: 5 * 60 * 1000, // 5 minutes
|
|
62
|
-
team: this.teamId,
|
|
63
|
-
project: this.projectId,
|
|
64
|
-
});
|
|
65
|
-
// Update token if it changed
|
|
66
|
-
if (freshToken !== this.token) {
|
|
67
|
-
this.token = freshToken;
|
|
68
|
-
const claims = decodeUnverifiedToken(freshToken);
|
|
69
|
-
if (claims) {
|
|
70
|
-
this.teamId = claims.owner_id;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
catch {
|
|
75
|
-
// Ignore refresh errors and continue with current token
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
async request(path, params) {
|
|
79
|
-
await this.ensureValidToken();
|
|
80
|
-
return super.request(path, {
|
|
81
|
-
...params,
|
|
82
|
-
query: { teamId: this.teamId, ...params?.query },
|
|
83
|
-
headers: {
|
|
84
|
-
"content-type": "application/json",
|
|
85
|
-
"user-agent": `vercel/sandbox/${version_1.VERSION} (Node.js/${process.version}; ${os_1.default.platform()}/${os_1.default.arch()})`,
|
|
86
|
-
...params?.headers,
|
|
87
|
-
},
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
async getSandbox(params) {
|
|
91
|
-
const privateParams = (0, types_1.getPrivateParams)(params);
|
|
92
|
-
let querystring = new URLSearchParams(privateParams).toString();
|
|
93
|
-
querystring = querystring ? `?${querystring}` : "";
|
|
94
|
-
return (0, base_client_1.parseOrThrow)(validators_1.SandboxAndRoutesResponse, await this.request(`/v1/sandboxes/${params.sandboxId}${querystring}`, {
|
|
95
|
-
signal: params.signal,
|
|
96
|
-
}));
|
|
97
|
-
}
|
|
98
|
-
async createSandbox(params) {
|
|
99
|
-
const privateParams = (0, types_1.getPrivateParams)(params);
|
|
100
|
-
return (0, base_client_1.parseOrThrow)(validators_1.SandboxAndRoutesResponse, await this.request("/v1/sandboxes", {
|
|
101
|
-
method: "POST",
|
|
102
|
-
body: JSON.stringify({
|
|
103
|
-
projectId: params.projectId,
|
|
104
|
-
ports: params.ports,
|
|
105
|
-
source: params.source,
|
|
106
|
-
timeout: params.timeout,
|
|
107
|
-
resources: params.resources,
|
|
108
|
-
runtime: params.runtime,
|
|
109
|
-
networkPolicy: params.networkPolicy
|
|
110
|
-
? (0, network_policy_1.toAPINetworkPolicy)(params.networkPolicy)
|
|
111
|
-
: undefined,
|
|
112
|
-
env: params.env,
|
|
113
|
-
...privateParams,
|
|
114
|
-
}),
|
|
115
|
-
signal: params.signal,
|
|
116
|
-
}));
|
|
117
|
-
}
|
|
118
|
-
async runCommand(params) {
|
|
119
|
-
if (params.wait) {
|
|
120
|
-
const response = await this.request(`/v1/sandboxes/${params.sandboxId}/cmd`, {
|
|
121
|
-
method: "POST",
|
|
122
|
-
body: JSON.stringify({
|
|
123
|
-
command: params.command,
|
|
124
|
-
args: params.args,
|
|
125
|
-
cwd: params.cwd,
|
|
126
|
-
env: params.env,
|
|
127
|
-
sudo: params.sudo,
|
|
128
|
-
wait: true,
|
|
129
|
-
}),
|
|
130
|
-
signal: params.signal,
|
|
131
|
-
});
|
|
132
|
-
if (!response.ok) {
|
|
133
|
-
await (0, base_client_1.parseOrThrow)(zod_1.z.any(), response);
|
|
134
|
-
}
|
|
135
|
-
if (response.headers.get("content-type") !== "application/x-ndjson") {
|
|
136
|
-
throw new api_error_1.APIError(response, {
|
|
137
|
-
message: "Expected a stream of command data",
|
|
138
|
-
sandboxId: params.sandboxId,
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
if (response.body === null) {
|
|
142
|
-
throw new api_error_1.APIError(response, {
|
|
143
|
-
message: "No response body",
|
|
144
|
-
sandboxId: params.sandboxId,
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
const jsonlinesStream = jsonlines_1.default.parse();
|
|
148
|
-
pipe(response.body, jsonlinesStream).catch((err) => {
|
|
149
|
-
console.error("Error piping command stream:", err);
|
|
150
|
-
});
|
|
151
|
-
const iterator = jsonlinesStream[Symbol.asyncIterator]();
|
|
152
|
-
const commandChunk = await iterator.next();
|
|
153
|
-
const { command } = validators_1.CommandResponse.parse(commandChunk.value);
|
|
154
|
-
const finished = (async () => {
|
|
155
|
-
const finishedChunk = await iterator.next();
|
|
156
|
-
const { command } = validators_1.CommandFinishedResponse.parse(finishedChunk.value);
|
|
157
|
-
return command;
|
|
158
|
-
})();
|
|
159
|
-
return { command, finished };
|
|
160
|
-
}
|
|
161
|
-
return (0, base_client_1.parseOrThrow)(validators_1.CommandResponse, await this.request(`/v1/sandboxes/${params.sandboxId}/cmd`, {
|
|
162
|
-
method: "POST",
|
|
163
|
-
body: JSON.stringify({
|
|
164
|
-
command: params.command,
|
|
165
|
-
args: params.args,
|
|
166
|
-
cwd: params.cwd,
|
|
167
|
-
env: params.env,
|
|
168
|
-
sudo: params.sudo,
|
|
169
|
-
}),
|
|
170
|
-
signal: params.signal,
|
|
171
|
-
}));
|
|
172
|
-
}
|
|
173
|
-
async getCommand(params) {
|
|
174
|
-
return params.wait
|
|
175
|
-
? (0, base_client_1.parseOrThrow)(validators_1.CommandFinishedResponse, await this.request(`/v1/sandboxes/${params.sandboxId}/cmd/${params.cmdId}`, { signal: params.signal, query: { wait: "true" } }))
|
|
176
|
-
: (0, base_client_1.parseOrThrow)(validators_1.CommandResponse, await this.request(`/v1/sandboxes/${params.sandboxId}/cmd/${params.cmdId}`, { signal: params.signal }));
|
|
177
|
-
}
|
|
178
|
-
async mkDir(params) {
|
|
179
|
-
return (0, base_client_1.parseOrThrow)(validators_1.EmptyResponse, await this.request(`/v1/sandboxes/${params.sandboxId}/fs/mkdir`, {
|
|
180
|
-
method: "POST",
|
|
181
|
-
body: JSON.stringify({ path: params.path, cwd: params.cwd }),
|
|
182
|
-
signal: params.signal,
|
|
183
|
-
}));
|
|
184
|
-
}
|
|
185
|
-
getFileWriter(params) {
|
|
186
|
-
const writer = new file_writer_1.FileWriter();
|
|
187
|
-
return {
|
|
188
|
-
response: (async () => {
|
|
189
|
-
return this.request(`/v1/sandboxes/${params.sandboxId}/fs/write`, {
|
|
190
|
-
method: "POST",
|
|
191
|
-
headers: {
|
|
192
|
-
"content-type": "application/gzip",
|
|
193
|
-
"x-cwd": params.extractDir,
|
|
194
|
-
},
|
|
195
|
-
body: await (0, consume_readable_1.consumeReadable)(writer.readable),
|
|
196
|
-
signal: params.signal,
|
|
197
|
-
});
|
|
198
|
-
})(),
|
|
199
|
-
writer,
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
async listSandboxes(params) {
|
|
203
|
-
return (0, base_client_1.parseOrThrow)(validators_1.SandboxesResponse, await this.request(`/v1/sandboxes`, {
|
|
204
|
-
query: {
|
|
205
|
-
project: params.projectId,
|
|
206
|
-
limit: params.limit,
|
|
207
|
-
since: typeof params.since === "number"
|
|
208
|
-
? params.since
|
|
209
|
-
: params.since?.getTime(),
|
|
210
|
-
until: typeof params.until === "number"
|
|
211
|
-
? params.until
|
|
212
|
-
: params.until?.getTime(),
|
|
213
|
-
},
|
|
214
|
-
method: "GET",
|
|
215
|
-
signal: params.signal,
|
|
216
|
-
}));
|
|
217
|
-
}
|
|
218
|
-
async listSnapshots(params) {
|
|
219
|
-
return (0, base_client_1.parseOrThrow)(validators_1.SnapshotsResponse, await this.request(`/v1/sandboxes/snapshots`, {
|
|
220
|
-
query: {
|
|
221
|
-
project: params.projectId,
|
|
222
|
-
limit: params.limit,
|
|
223
|
-
since: typeof params.since === "number"
|
|
224
|
-
? params.since
|
|
225
|
-
: params.since?.getTime(),
|
|
226
|
-
until: typeof params.until === "number"
|
|
227
|
-
? params.until
|
|
228
|
-
: params.until?.getTime(),
|
|
229
|
-
},
|
|
230
|
-
method: "GET",
|
|
231
|
-
signal: params.signal,
|
|
232
|
-
}));
|
|
233
|
-
}
|
|
234
|
-
async writeFiles(params) {
|
|
235
|
-
const { writer, response } = this.getFileWriter({
|
|
236
|
-
sandboxId: params.sandboxId,
|
|
237
|
-
extractDir: params.extractDir,
|
|
238
|
-
signal: params.signal,
|
|
239
|
-
});
|
|
240
|
-
for (const file of params.files) {
|
|
241
|
-
await writer.addFile({
|
|
242
|
-
name: (0, normalizePath_1.normalizePath)({
|
|
243
|
-
filePath: file.path,
|
|
244
|
-
extractDir: params.extractDir,
|
|
245
|
-
cwd: params.cwd,
|
|
246
|
-
}),
|
|
247
|
-
content: file.content,
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
writer.end();
|
|
251
|
-
await (0, base_client_1.parseOrThrow)(validators_1.EmptyResponse, await response);
|
|
252
|
-
}
|
|
253
|
-
async readFile(params) {
|
|
254
|
-
const response = await this.request(`/v1/sandboxes/${params.sandboxId}/fs/read`, {
|
|
255
|
-
method: "POST",
|
|
256
|
-
body: JSON.stringify({ path: params.path, cwd: params.cwd }),
|
|
257
|
-
signal: params.signal,
|
|
258
|
-
});
|
|
259
|
-
if (response.status === 404) {
|
|
260
|
-
return null;
|
|
261
|
-
}
|
|
262
|
-
if (response.body === null) {
|
|
263
|
-
return null;
|
|
264
|
-
}
|
|
265
|
-
return stream_1.Readable.fromWeb(response.body);
|
|
266
|
-
}
|
|
267
|
-
async killCommand(params) {
|
|
268
|
-
return (0, base_client_1.parseOrThrow)(validators_1.CommandResponse, await this.request(`/v1/sandboxes/${params.sandboxId}/${params.commandId}/kill`, {
|
|
269
|
-
method: "POST",
|
|
270
|
-
body: JSON.stringify({ signal: params.signal }),
|
|
271
|
-
signal: params.abortSignal,
|
|
272
|
-
}));
|
|
273
|
-
}
|
|
274
|
-
getLogs(params) {
|
|
275
|
-
const self = this;
|
|
276
|
-
const disposer = new AbortController();
|
|
277
|
-
const signal = !params.signal
|
|
278
|
-
? disposer.signal
|
|
279
|
-
: mergeSignals(params.signal, disposer.signal);
|
|
280
|
-
const generator = (async function* () {
|
|
281
|
-
const url = `/v1/sandboxes/${params.sandboxId}/cmd/${params.cmdId}/logs`;
|
|
282
|
-
const response = await self.request(url, {
|
|
283
|
-
method: "GET",
|
|
284
|
-
signal,
|
|
285
|
-
});
|
|
286
|
-
if (!response.ok) {
|
|
287
|
-
await (0, base_client_1.parseOrThrow)(zod_1.z.any(), response);
|
|
288
|
-
}
|
|
289
|
-
if (response.headers.get("content-type") !== "application/x-ndjson") {
|
|
290
|
-
throw new api_error_1.APIError(response, {
|
|
291
|
-
message: "Expected a stream of logs",
|
|
292
|
-
sandboxId: params.sandboxId,
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
if (response.body === null) {
|
|
296
|
-
throw new api_error_1.APIError(response, {
|
|
297
|
-
message: "No response body",
|
|
298
|
-
sandboxId: params.sandboxId,
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
const jsonlinesStream = jsonlines_1.default.parse();
|
|
302
|
-
pipe(response.body, jsonlinesStream).catch((err) => {
|
|
303
|
-
console.error("Error piping logs:", err);
|
|
304
|
-
});
|
|
305
|
-
for await (const chunk of jsonlinesStream) {
|
|
306
|
-
const parsed = validators_1.LogLine.parse(chunk);
|
|
307
|
-
if (parsed.stream === "error") {
|
|
308
|
-
throw new api_error_1.StreamError(parsed.data.code, parsed.data.message, params.sandboxId);
|
|
309
|
-
}
|
|
310
|
-
yield parsed;
|
|
311
|
-
}
|
|
312
|
-
})();
|
|
313
|
-
return Object.assign(generator, {
|
|
314
|
-
[Symbol.dispose]() {
|
|
315
|
-
disposer.abort("Disposed");
|
|
316
|
-
},
|
|
317
|
-
close: () => disposer.abort("Disposed"),
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
async stopSandbox(params) {
|
|
321
|
-
const url = `/v1/sandboxes/${params.sandboxId}/stop`;
|
|
322
|
-
const response = await (0, base_client_1.parseOrThrow)(validators_1.SandboxResponse, await this.request(url, { method: "POST", signal: params.signal }));
|
|
323
|
-
if (params.blocking) {
|
|
324
|
-
let sandbox = response.json.sandbox;
|
|
325
|
-
while (sandbox.status !== "stopped" && sandbox.status !== "failed" && sandbox.status !== "aborted") {
|
|
326
|
-
await (0, promises_1.setTimeout)(500, undefined, { signal: params.signal });
|
|
327
|
-
const poll = await this.getSandbox({
|
|
328
|
-
sandboxId: params.sandboxId,
|
|
329
|
-
signal: params.signal,
|
|
330
|
-
});
|
|
331
|
-
sandbox = poll.json.sandbox;
|
|
332
|
-
response.json.sandbox = sandbox;
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
return response;
|
|
336
|
-
}
|
|
337
|
-
async updateNetworkPolicy(params) {
|
|
338
|
-
const url = `/v1/sandboxes/${params.sandboxId}/network-policy`;
|
|
339
|
-
return (0, base_client_1.parseOrThrow)(validators_1.UpdateNetworkPolicyResponse, await this.request(url, {
|
|
340
|
-
method: "POST",
|
|
341
|
-
body: JSON.stringify((0, network_policy_1.toAPINetworkPolicy)(params.networkPolicy)),
|
|
342
|
-
signal: params.signal,
|
|
343
|
-
}));
|
|
344
|
-
}
|
|
345
|
-
async extendTimeout(params) {
|
|
346
|
-
const url = `/v1/sandboxes/${params.sandboxId}/extend-timeout`;
|
|
347
|
-
return (0, base_client_1.parseOrThrow)(validators_1.ExtendTimeoutResponse, await this.request(url, {
|
|
348
|
-
method: "POST",
|
|
349
|
-
body: JSON.stringify({ duration: params.duration }),
|
|
350
|
-
signal: params.signal,
|
|
351
|
-
}));
|
|
352
|
-
}
|
|
353
|
-
async createSnapshot(params) {
|
|
354
|
-
const url = `/v1/sandboxes/${params.sandboxId}/snapshot`;
|
|
355
|
-
const body = params.expiration === undefined
|
|
356
|
-
? undefined
|
|
357
|
-
: JSON.stringify({ expiration: params.expiration });
|
|
358
|
-
return (0, base_client_1.parseOrThrow)(validators_1.CreateSnapshotResponse, await this.request(url, {
|
|
359
|
-
method: "POST",
|
|
360
|
-
body,
|
|
361
|
-
signal: params.signal,
|
|
362
|
-
}));
|
|
363
|
-
}
|
|
364
|
-
async deleteSnapshot(params) {
|
|
365
|
-
const url = `/v1/sandboxes/snapshots/${params.snapshotId}`;
|
|
366
|
-
return (0, base_client_1.parseOrThrow)(validators_1.SnapshotResponse, await this.request(url, { method: "DELETE", signal: params.signal }));
|
|
367
|
-
}
|
|
368
|
-
async getSnapshot(params) {
|
|
369
|
-
const url = `/v1/sandboxes/snapshots/${params.snapshotId}`;
|
|
370
|
-
return (0, base_client_1.parseOrThrow)(validators_1.SnapshotResponse, await this.request(url, { signal: params.signal }));
|
|
371
|
-
}
|
|
19
|
+
if (token.split(".").length !== 3) return null;
|
|
20
|
+
try {
|
|
21
|
+
const payload = JSON.parse(Buffer.from(token.split(".")[1], "base64url").toString("utf8"));
|
|
22
|
+
if (payload.owner_id) return {
|
|
23
|
+
owner_id: payload.owner_id,
|
|
24
|
+
project_id: payload.project_id
|
|
25
|
+
};
|
|
26
|
+
return null;
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
372
30
|
}
|
|
373
|
-
|
|
31
|
+
var APIClient = class extends BaseClient {
|
|
32
|
+
constructor(params) {
|
|
33
|
+
super({
|
|
34
|
+
baseUrl: params.baseUrl ?? "https://vercel.com/api",
|
|
35
|
+
token: params.token,
|
|
36
|
+
debug: false,
|
|
37
|
+
fetch: params.fetch
|
|
38
|
+
});
|
|
39
|
+
this.teamId = params.teamId;
|
|
40
|
+
this.isJwtToken = false;
|
|
41
|
+
const claims = decodeUnverifiedToken(params.token);
|
|
42
|
+
if (claims) {
|
|
43
|
+
this.isJwtToken = true;
|
|
44
|
+
this.projectId = claims.project_id;
|
|
45
|
+
this.teamId = claims.owner_id;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async ensureValidToken() {
|
|
49
|
+
if (!this.isJwtToken) return;
|
|
50
|
+
try {
|
|
51
|
+
const freshToken = await getVercelOidcToken({
|
|
52
|
+
expirationBufferMs: 300 * 1e3,
|
|
53
|
+
team: this.teamId,
|
|
54
|
+
project: this.projectId
|
|
55
|
+
});
|
|
56
|
+
if (freshToken !== this.token) {
|
|
57
|
+
this.token = freshToken;
|
|
58
|
+
const claims = decodeUnverifiedToken(freshToken);
|
|
59
|
+
if (claims) this.teamId = claims.owner_id;
|
|
60
|
+
}
|
|
61
|
+
} catch {}
|
|
62
|
+
}
|
|
63
|
+
async request(path, params) {
|
|
64
|
+
await this.ensureValidToken();
|
|
65
|
+
return super.request(path, {
|
|
66
|
+
...params,
|
|
67
|
+
query: {
|
|
68
|
+
teamId: this.teamId,
|
|
69
|
+
...params?.query
|
|
70
|
+
},
|
|
71
|
+
headers: {
|
|
72
|
+
"content-type": "application/json",
|
|
73
|
+
"user-agent": `vercel/sandbox/${VERSION} (Node.js/${process.version}; ${os.platform()}/${os.arch()})`,
|
|
74
|
+
...params?.headers
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
async getSandbox(params) {
|
|
79
|
+
const privateParams = getPrivateParams(params);
|
|
80
|
+
let querystring = new URLSearchParams(privateParams).toString();
|
|
81
|
+
querystring = querystring ? `?${querystring}` : "";
|
|
82
|
+
return parseOrThrow(SandboxAndRoutesResponse, await this.request(`/v1/sandboxes/${params.sandboxId}${querystring}`, { signal: params.signal }));
|
|
83
|
+
}
|
|
84
|
+
async createSandbox(params) {
|
|
85
|
+
const privateParams = getPrivateParams(params);
|
|
86
|
+
return parseOrThrow(SandboxAndRoutesResponse, await this.request("/v1/sandboxes", {
|
|
87
|
+
method: "POST",
|
|
88
|
+
body: JSON.stringify({
|
|
89
|
+
projectId: params.projectId,
|
|
90
|
+
ports: params.ports,
|
|
91
|
+
source: params.source,
|
|
92
|
+
timeout: params.timeout,
|
|
93
|
+
resources: params.resources,
|
|
94
|
+
runtime: params.runtime,
|
|
95
|
+
networkPolicy: params.networkPolicy ? toAPINetworkPolicy(params.networkPolicy) : void 0,
|
|
96
|
+
env: params.env,
|
|
97
|
+
...privateParams
|
|
98
|
+
}),
|
|
99
|
+
signal: params.signal
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
async runCommand(params) {
|
|
103
|
+
if (params.wait) {
|
|
104
|
+
const response = await this.request(`/v1/sandboxes/${params.sandboxId}/cmd`, {
|
|
105
|
+
method: "POST",
|
|
106
|
+
body: JSON.stringify({
|
|
107
|
+
command: params.command,
|
|
108
|
+
args: params.args,
|
|
109
|
+
cwd: params.cwd,
|
|
110
|
+
env: params.env,
|
|
111
|
+
sudo: params.sudo,
|
|
112
|
+
wait: true
|
|
113
|
+
}),
|
|
114
|
+
signal: params.signal
|
|
115
|
+
});
|
|
116
|
+
if (!response.ok) await parseOrThrow(z.any(), response);
|
|
117
|
+
if (response.headers.get("content-type") !== "application/x-ndjson") throw new APIError(response, {
|
|
118
|
+
message: "Expected a stream of command data",
|
|
119
|
+
sandboxId: params.sandboxId
|
|
120
|
+
});
|
|
121
|
+
if (response.body === null) throw new APIError(response, {
|
|
122
|
+
message: "No response body",
|
|
123
|
+
sandboxId: params.sandboxId
|
|
124
|
+
});
|
|
125
|
+
const jsonlinesStream = jsonlines.parse();
|
|
126
|
+
pipe(response.body, jsonlinesStream).catch((err) => {
|
|
127
|
+
console.error("Error piping command stream:", err);
|
|
128
|
+
});
|
|
129
|
+
const iterator = jsonlinesStream[Symbol.asyncIterator]();
|
|
130
|
+
const commandChunk = await iterator.next();
|
|
131
|
+
const { command } = CommandResponse.parse(commandChunk.value);
|
|
132
|
+
return {
|
|
133
|
+
command,
|
|
134
|
+
finished: (async () => {
|
|
135
|
+
const finishedChunk = await iterator.next();
|
|
136
|
+
const { command: command$1 } = CommandFinishedResponse.parse(finishedChunk.value);
|
|
137
|
+
return command$1;
|
|
138
|
+
})()
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
return parseOrThrow(CommandResponse, await this.request(`/v1/sandboxes/${params.sandboxId}/cmd`, {
|
|
142
|
+
method: "POST",
|
|
143
|
+
body: JSON.stringify({
|
|
144
|
+
command: params.command,
|
|
145
|
+
args: params.args,
|
|
146
|
+
cwd: params.cwd,
|
|
147
|
+
env: params.env,
|
|
148
|
+
sudo: params.sudo
|
|
149
|
+
}),
|
|
150
|
+
signal: params.signal
|
|
151
|
+
}));
|
|
152
|
+
}
|
|
153
|
+
async getCommand(params) {
|
|
154
|
+
return params.wait ? parseOrThrow(CommandFinishedResponse, await this.request(`/v1/sandboxes/${params.sandboxId}/cmd/${params.cmdId}`, {
|
|
155
|
+
signal: params.signal,
|
|
156
|
+
query: { wait: "true" }
|
|
157
|
+
})) : parseOrThrow(CommandResponse, await this.request(`/v1/sandboxes/${params.sandboxId}/cmd/${params.cmdId}`, { signal: params.signal }));
|
|
158
|
+
}
|
|
159
|
+
async mkDir(params) {
|
|
160
|
+
return parseOrThrow(EmptyResponse, await this.request(`/v1/sandboxes/${params.sandboxId}/fs/mkdir`, {
|
|
161
|
+
method: "POST",
|
|
162
|
+
body: JSON.stringify({
|
|
163
|
+
path: params.path,
|
|
164
|
+
cwd: params.cwd
|
|
165
|
+
}),
|
|
166
|
+
signal: params.signal
|
|
167
|
+
}));
|
|
168
|
+
}
|
|
169
|
+
getFileWriter(params) {
|
|
170
|
+
const writer = new FileWriter();
|
|
171
|
+
return {
|
|
172
|
+
response: (async () => {
|
|
173
|
+
return this.request(`/v1/sandboxes/${params.sandboxId}/fs/write`, {
|
|
174
|
+
method: "POST",
|
|
175
|
+
headers: {
|
|
176
|
+
"content-type": "application/gzip",
|
|
177
|
+
"x-cwd": params.extractDir
|
|
178
|
+
},
|
|
179
|
+
body: await consumeReadable(writer.readable),
|
|
180
|
+
signal: params.signal
|
|
181
|
+
});
|
|
182
|
+
})(),
|
|
183
|
+
writer
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
async listSandboxes(params) {
|
|
187
|
+
return parseOrThrow(SandboxesResponse, await this.request(`/v1/sandboxes`, {
|
|
188
|
+
query: {
|
|
189
|
+
project: params.projectId,
|
|
190
|
+
limit: params.limit,
|
|
191
|
+
since: typeof params.since === "number" ? params.since : params.since?.getTime(),
|
|
192
|
+
until: typeof params.until === "number" ? params.until : params.until?.getTime()
|
|
193
|
+
},
|
|
194
|
+
method: "GET",
|
|
195
|
+
signal: params.signal
|
|
196
|
+
}));
|
|
197
|
+
}
|
|
198
|
+
async listSnapshots(params) {
|
|
199
|
+
return parseOrThrow(SnapshotsResponse, await this.request(`/v1/sandboxes/snapshots`, {
|
|
200
|
+
query: {
|
|
201
|
+
project: params.projectId,
|
|
202
|
+
limit: params.limit,
|
|
203
|
+
since: typeof params.since === "number" ? params.since : params.since?.getTime(),
|
|
204
|
+
until: typeof params.until === "number" ? params.until : params.until?.getTime()
|
|
205
|
+
},
|
|
206
|
+
method: "GET",
|
|
207
|
+
signal: params.signal
|
|
208
|
+
}));
|
|
209
|
+
}
|
|
210
|
+
async writeFiles(params) {
|
|
211
|
+
const { writer, response } = this.getFileWriter({
|
|
212
|
+
sandboxId: params.sandboxId,
|
|
213
|
+
extractDir: params.extractDir,
|
|
214
|
+
signal: params.signal
|
|
215
|
+
});
|
|
216
|
+
for (const file of params.files) await writer.addFile({
|
|
217
|
+
name: normalizePath({
|
|
218
|
+
filePath: file.path,
|
|
219
|
+
extractDir: params.extractDir,
|
|
220
|
+
cwd: params.cwd
|
|
221
|
+
}),
|
|
222
|
+
content: file.content,
|
|
223
|
+
mode: file.mode
|
|
224
|
+
});
|
|
225
|
+
writer.end();
|
|
226
|
+
await parseOrThrow(EmptyResponse, await response);
|
|
227
|
+
}
|
|
228
|
+
async readFile(params) {
|
|
229
|
+
const response = await this.request(`/v1/sandboxes/${params.sandboxId}/fs/read`, {
|
|
230
|
+
method: "POST",
|
|
231
|
+
body: JSON.stringify({
|
|
232
|
+
path: params.path,
|
|
233
|
+
cwd: params.cwd
|
|
234
|
+
}),
|
|
235
|
+
signal: params.signal
|
|
236
|
+
});
|
|
237
|
+
if (response.status === 404) return null;
|
|
238
|
+
if (response.body === null) return null;
|
|
239
|
+
return Readable.fromWeb(response.body);
|
|
240
|
+
}
|
|
241
|
+
async killCommand(params) {
|
|
242
|
+
return parseOrThrow(CommandResponse, await this.request(`/v1/sandboxes/${params.sandboxId}/${params.commandId}/kill`, {
|
|
243
|
+
method: "POST",
|
|
244
|
+
body: JSON.stringify({ signal: params.signal }),
|
|
245
|
+
signal: params.abortSignal
|
|
246
|
+
}));
|
|
247
|
+
}
|
|
248
|
+
getLogs(params) {
|
|
249
|
+
const self = this;
|
|
250
|
+
const disposer = new AbortController();
|
|
251
|
+
const signal = !params.signal ? disposer.signal : mergeSignals(params.signal, disposer.signal);
|
|
252
|
+
const generator = (async function* () {
|
|
253
|
+
const url = `/v1/sandboxes/${params.sandboxId}/cmd/${params.cmdId}/logs`;
|
|
254
|
+
const response = await self.request(url, {
|
|
255
|
+
method: "GET",
|
|
256
|
+
signal
|
|
257
|
+
});
|
|
258
|
+
if (!response.ok) await parseOrThrow(z.any(), response);
|
|
259
|
+
if (response.headers.get("content-type") !== "application/x-ndjson") throw new APIError(response, {
|
|
260
|
+
message: "Expected a stream of logs",
|
|
261
|
+
sandboxId: params.sandboxId
|
|
262
|
+
});
|
|
263
|
+
if (response.body === null) throw new APIError(response, {
|
|
264
|
+
message: "No response body",
|
|
265
|
+
sandboxId: params.sandboxId
|
|
266
|
+
});
|
|
267
|
+
const jsonlinesStream = jsonlines.parse();
|
|
268
|
+
pipe(response.body, jsonlinesStream).catch((err) => {
|
|
269
|
+
console.error("Error piping logs:", err);
|
|
270
|
+
});
|
|
271
|
+
for await (const chunk of jsonlinesStream) {
|
|
272
|
+
const parsed = LogLine.parse(chunk);
|
|
273
|
+
if (parsed.stream === "error") throw new StreamError(parsed.data.code, parsed.data.message, params.sandboxId);
|
|
274
|
+
yield parsed;
|
|
275
|
+
}
|
|
276
|
+
})();
|
|
277
|
+
return Object.assign(generator, {
|
|
278
|
+
[Symbol.dispose]() {
|
|
279
|
+
disposer.abort("Disposed");
|
|
280
|
+
},
|
|
281
|
+
close: () => disposer.abort("Disposed")
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
async stopSandbox(params) {
|
|
285
|
+
const url = `/v1/sandboxes/${params.sandboxId}/stop`;
|
|
286
|
+
const response = await parseOrThrow(SandboxResponse, await this.request(url, {
|
|
287
|
+
method: "POST",
|
|
288
|
+
signal: params.signal
|
|
289
|
+
}));
|
|
290
|
+
if (params.blocking) {
|
|
291
|
+
let sandbox = response.json.sandbox;
|
|
292
|
+
while (sandbox.status !== "stopped" && sandbox.status !== "failed" && sandbox.status !== "aborted") {
|
|
293
|
+
await setTimeout(500, void 0, { signal: params.signal });
|
|
294
|
+
sandbox = (await this.getSandbox({
|
|
295
|
+
sandboxId: params.sandboxId,
|
|
296
|
+
signal: params.signal
|
|
297
|
+
})).json.sandbox;
|
|
298
|
+
response.json.sandbox = sandbox;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return response;
|
|
302
|
+
}
|
|
303
|
+
async updateNetworkPolicy(params) {
|
|
304
|
+
const url = `/v1/sandboxes/${params.sandboxId}/network-policy`;
|
|
305
|
+
return parseOrThrow(UpdateNetworkPolicyResponse, await this.request(url, {
|
|
306
|
+
method: "POST",
|
|
307
|
+
body: JSON.stringify(toAPINetworkPolicy(params.networkPolicy)),
|
|
308
|
+
signal: params.signal
|
|
309
|
+
}));
|
|
310
|
+
}
|
|
311
|
+
async extendTimeout(params) {
|
|
312
|
+
const url = `/v1/sandboxes/${params.sandboxId}/extend-timeout`;
|
|
313
|
+
return parseOrThrow(ExtendTimeoutResponse, await this.request(url, {
|
|
314
|
+
method: "POST",
|
|
315
|
+
body: JSON.stringify({ duration: params.duration }),
|
|
316
|
+
signal: params.signal
|
|
317
|
+
}));
|
|
318
|
+
}
|
|
319
|
+
async createSnapshot(params) {
|
|
320
|
+
const url = `/v1/sandboxes/${params.sandboxId}/snapshot`;
|
|
321
|
+
const body = params.expiration === void 0 ? void 0 : JSON.stringify({ expiration: params.expiration });
|
|
322
|
+
return parseOrThrow(CreateSnapshotResponse, await this.request(url, {
|
|
323
|
+
method: "POST",
|
|
324
|
+
body,
|
|
325
|
+
signal: params.signal
|
|
326
|
+
}));
|
|
327
|
+
}
|
|
328
|
+
async deleteSnapshot(params) {
|
|
329
|
+
const url = `/v1/sandboxes/snapshots/${params.snapshotId}`;
|
|
330
|
+
return parseOrThrow(SnapshotResponse, await this.request(url, {
|
|
331
|
+
method: "DELETE",
|
|
332
|
+
signal: params.signal
|
|
333
|
+
}));
|
|
334
|
+
}
|
|
335
|
+
async getSnapshot(params) {
|
|
336
|
+
const url = `/v1/sandboxes/snapshots/${params.snapshotId}`;
|
|
337
|
+
return parseOrThrow(SnapshotResponse, await this.request(url, { signal: params.signal }));
|
|
338
|
+
}
|
|
339
|
+
};
|
|
374
340
|
async function pipe(readable, output) {
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
catch (err) {
|
|
388
|
-
output.emit("error", err);
|
|
389
|
-
}
|
|
390
|
-
finally {
|
|
391
|
-
output.end();
|
|
392
|
-
}
|
|
341
|
+
const reader = readable.getReader();
|
|
342
|
+
try {
|
|
343
|
+
while (true) {
|
|
344
|
+
const read = await reader.read();
|
|
345
|
+
if (read.value) output.write(Buffer.from(read.value));
|
|
346
|
+
if (read.done) break;
|
|
347
|
+
}
|
|
348
|
+
} catch (err) {
|
|
349
|
+
output.emit("error", err);
|
|
350
|
+
} finally {
|
|
351
|
+
output.end();
|
|
352
|
+
}
|
|
393
353
|
}
|
|
394
354
|
function mergeSignals(...signals) {
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
}
|
|
409
|
-
return controller.signal;
|
|
355
|
+
const controller = new AbortController();
|
|
356
|
+
const onAbort = () => {
|
|
357
|
+
controller.abort();
|
|
358
|
+
for (const signal of signals) signal.removeEventListener("abort", onAbort);
|
|
359
|
+
};
|
|
360
|
+
for (const signal of signals) {
|
|
361
|
+
if (signal.aborted) {
|
|
362
|
+
controller.abort();
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
signal.addEventListener("abort", onAbort);
|
|
366
|
+
}
|
|
367
|
+
return controller.signal;
|
|
410
368
|
}
|
|
369
|
+
|
|
370
|
+
//#endregion
|
|
371
|
+
export { APIClient };
|
|
411
372
|
//# sourceMappingURL=api-client.js.map
|