@vercel/sandbox 2.0.0-beta.2 → 2.0.0-beta.20
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 +48 -1
- package/dist/_virtual/rolldown_runtime.cjs +29 -0
- package/dist/api-client/api-client.cjs +445 -0
- package/dist/api-client/api-client.cjs.map +1 -0
- package/dist/api-client/api-client.d.cts +6235 -0
- package/dist/api-client/api-client.d.ts +6229 -706
- package/dist/api-client/api-client.js +436 -472
- 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 -16
- package/dist/api-client/api-error.js +28 -32
- package/dist/api-client/api-error.js.map +1 -1
- package/dist/api-client/base-client.cjs +126 -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 +114 -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 +229 -0
- package/dist/api-client/validators.cjs.map +1 -0
- package/dist/api-client/validators.d.cts +26885 -0
- package/dist/api-client/validators.d.ts +26732 -3706
- package/dist/api-client/validators.js +198 -191
- 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 +11 -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 +178 -0
- package/dist/auth/project.cjs.map +1 -0
- package/dist/auth/project.d.cts +40 -0
- package/dist/auth/project.d.ts +19 -19
- package/dist/auth/project.js +169 -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/filesystem.cjs +499 -0
- package/dist/filesystem.cjs.map +1 -0
- package/dist/filesystem.d.cts +258 -0
- package/dist/filesystem.d.ts +258 -0
- package/dist/filesystem.js +497 -0
- package/dist/filesystem.js.map +1 -0
- package/dist/index.cjs +15 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -6
- package/dist/index.js +8 -17
- package/dist/network-policy.d.cts +156 -0
- package/dist/network-policy.d.ts +88 -28
- package/dist/sandbox.cjs +816 -0
- package/dist/sandbox.cjs.map +1 -0
- package/dist/sandbox.d.cts +2847 -0
- package/dist/sandbox.d.ts +2834 -628
- package/dist/sandbox.js +808 -557
- package/dist/sandbox.js.map +1 -1
- package/dist/session.cjs +527 -0
- package/dist/session.cjs.map +1 -0
- package/dist/session.d.cts +410 -0
- package/dist/session.d.ts +403 -368
- package/dist/session.js +524 -489
- package/dist/session.js.map +1 -1
- package/dist/snapshot.cjs +204 -0
- package/dist/snapshot.cjs.map +1 -0
- package/dist/snapshot.d.cts +161 -0
- package/dist/snapshot.d.ts +152 -92
- package/dist/snapshot.js +201 -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 +49 -0
- package/dist/utils/network-policy.cjs.map +1 -0
- package/dist/utils/network-policy.js +42 -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/paginator.cjs +41 -0
- package/dist/utils/paginator.cjs.map +1 -0
- package/dist/utils/paginator.d.cts +16 -0
- package/dist/utils/paginator.d.ts +16 -0
- package/dist/utils/paginator.js +40 -0
- package/dist/utils/paginator.js.map +1 -0
- 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,128 +1,124 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const with_retry_1 = require("./with-retry");
|
|
9
|
-
const undici_1 = require("undici");
|
|
1
|
+
import { APIError } from "./api-error.js";
|
|
2
|
+
import { array } from "../utils/array.js";
|
|
3
|
+
import { withRetry } from "./with-retry.js";
|
|
4
|
+
import { Agent } from "undici";
|
|
5
|
+
|
|
6
|
+
//#region src/api-client/base-client.ts
|
|
7
|
+
const DEFAULT_AGENT = new Agent({ bodyTimeout: 0 });
|
|
10
8
|
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
9
|
+
* A base API client that provides a convenience wrapper for fetching where
|
|
10
|
+
* we can pass query parameters as an object, support retries, debugging
|
|
11
|
+
* and automatic authorization.
|
|
12
|
+
*/
|
|
13
|
+
var BaseClient = class {
|
|
14
|
+
constructor(params) {
|
|
15
|
+
this.fetch = withRetry(params.fetch ?? globalThis.fetch);
|
|
16
|
+
this.baseUrl = params.baseUrl;
|
|
17
|
+
this.debug = params.debug ?? process.env.DEBUG_FETCH === "true";
|
|
18
|
+
this.token = params.token;
|
|
19
|
+
this.agent = DEFAULT_AGENT;
|
|
20
|
+
}
|
|
21
|
+
async request(path, opts) {
|
|
22
|
+
const url = new URL(`${this.baseUrl}${path}`);
|
|
23
|
+
if (opts?.query) for (const [key, value] of Object.entries(opts.query)) array(value).forEach((value$1) => {
|
|
24
|
+
url.searchParams.append(key, value$1.toString());
|
|
25
|
+
});
|
|
26
|
+
const start = Date.now();
|
|
27
|
+
const response = await this.fetch(url.toString(), {
|
|
28
|
+
...opts,
|
|
29
|
+
body: opts?.body,
|
|
30
|
+
method: opts?.method || "GET",
|
|
31
|
+
headers: this.token ? {
|
|
32
|
+
Authorization: `Bearer ${this.token}`,
|
|
33
|
+
...opts?.headers
|
|
34
|
+
} : opts?.headers,
|
|
35
|
+
dispatcher: this.agent,
|
|
36
|
+
signal: opts?.signal
|
|
37
|
+
});
|
|
38
|
+
if (this.debug) {
|
|
39
|
+
const duration = Date.now() - start;
|
|
40
|
+
console.log(`[API] ${url} (${response.status}) ${duration}ms`);
|
|
41
|
+
if (response.status === 429) {
|
|
42
|
+
const retry = parseInt(response.headers.get("Retry-After") ?? "", 10);
|
|
43
|
+
const hours = Math.floor(retry / 60 / 60);
|
|
44
|
+
const minutes = Math.floor(retry / 60) % 60;
|
|
45
|
+
const seconds = retry % 60;
|
|
46
|
+
console.warn(`[API] ${url} Rate Limited, Retry After ${hours}h ${minutes}m ${seconds}s`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return response;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Extract sessionId from a sandbox API URL.
|
|
54
|
+
*/
|
|
55
|
+
function extractSessionId(url) {
|
|
56
|
+
return url.match(/\/v2\/sandboxes\/sessions\/([^/?]+)/)?.[1];
|
|
59
57
|
}
|
|
60
|
-
exports.BaseClient = BaseClient;
|
|
61
58
|
/**
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
function
|
|
66
|
-
|
|
67
|
-
return match?.[1];
|
|
59
|
+
* Extract sandbox name from a sandbox API url.
|
|
60
|
+
* Excludes known sub-paths like /sessions/ and /snapshots/.
|
|
61
|
+
*/
|
|
62
|
+
function extractSandboxName(url) {
|
|
63
|
+
return url.match(/\/v2\/sandboxes\/(?!sessions(?:\/|$|\?))(?!snapshots(?:\/|$|\?))([^/?]+)/)?.[1];
|
|
68
64
|
}
|
|
69
65
|
/**
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
66
|
+
* Allows to read the response text and parse it as JSON casting to the given
|
|
67
|
+
* type. If the response is not ok or cannot be parsed it will return error.
|
|
68
|
+
*
|
|
69
|
+
* @param response Response to parse.
|
|
70
|
+
* @returns Parsed response or error.
|
|
71
|
+
*/
|
|
76
72
|
async function parse(validator, response) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
};
|
|
73
|
+
const sessionId = extractSessionId(response.url);
|
|
74
|
+
let sandboxName;
|
|
75
|
+
if (!sessionId) sandboxName = extractSandboxName(response.url);
|
|
76
|
+
const text = await response.text().catch((err) => {
|
|
77
|
+
return new APIError(response, {
|
|
78
|
+
message: `Can't read response text: ${String(err)}`,
|
|
79
|
+
sessionId,
|
|
80
|
+
sandboxName
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
if (typeof text !== "string") return text;
|
|
84
|
+
let json;
|
|
85
|
+
try {
|
|
86
|
+
json = JSON.parse(text || "{}");
|
|
87
|
+
} catch (error) {
|
|
88
|
+
return new APIError(response, {
|
|
89
|
+
message: `Can't parse JSON: ${String(error)}`,
|
|
90
|
+
text,
|
|
91
|
+
sessionId,
|
|
92
|
+
sandboxName
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (!response.ok) return new APIError(response, {
|
|
96
|
+
message: `Status code ${response.status} is not ok`,
|
|
97
|
+
json,
|
|
98
|
+
text,
|
|
99
|
+
sessionId,
|
|
100
|
+
sandboxName
|
|
101
|
+
});
|
|
102
|
+
const validated = validator.safeParse(json);
|
|
103
|
+
if (!validated.success) return new APIError(response, {
|
|
104
|
+
message: `Response JSON is not valid: ${validated.error}`,
|
|
105
|
+
json,
|
|
106
|
+
text,
|
|
107
|
+
sessionId,
|
|
108
|
+
sandboxName
|
|
109
|
+
});
|
|
110
|
+
return {
|
|
111
|
+
json: validated.data,
|
|
112
|
+
response,
|
|
113
|
+
text
|
|
114
|
+
};
|
|
120
115
|
}
|
|
121
116
|
async function parseOrThrow(validator, response) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
return result;
|
|
117
|
+
const result = await parse(validator, response);
|
|
118
|
+
if (result instanceof APIError) throw result;
|
|
119
|
+
return result;
|
|
127
120
|
}
|
|
121
|
+
|
|
122
|
+
//#endregion
|
|
123
|
+
export { BaseClient, parseOrThrow };
|
|
128
124
|
//# sourceMappingURL=base-client.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-client.js","
|
|
1
|
+
{"version":3,"file":"base-client.js","names":["value","sandboxName: string | undefined","json: Data | ErrorData"],"sources":["../../src/api-client/base-client.ts"],"sourcesContent":["import type { Options as RetryOptions } from \"async-retry\";\nimport { APIError } from \"./api-error.js\";\nimport { ZodType } from \"zod\";\nimport { array } from \"../utils/array.js\";\nimport { withRetry, type RequestOptions } from \"./with-retry.js\";\nimport { Agent } from \"undici\";\n\nexport interface RequestParams extends RequestInit {\n headers?: Record<string, string>;\n method?: string;\n onRetry?(error: any, options: RequestOptions): void;\n query?: Record<string, number | string | null | undefined | string[]>;\n retry?: Partial<RetryOptions>;\n}\n\nconst DEFAULT_AGENT = new Agent({\n bodyTimeout: 0, // disable body timeout to allow long logs streaming\n});\n\n/**\n * A base API client that provides a convenience wrapper for fetching where\n * we can pass query parameters as an object, support retries, debugging\n * and automatic authorization.\n */\nexport class BaseClient {\n protected token?: string;\n private fetch: ReturnType<typeof withRetry<RequestInit>>;\n private debug: boolean;\n private baseUrl: string;\n private agent: Agent;\n\n constructor(params: {\n debug?: boolean;\n baseUrl: string;\n token?: string;\n fetch?: typeof globalThis.fetch;\n }) {\n this.fetch = withRetry(params.fetch ?? globalThis.fetch);\n this.baseUrl = params.baseUrl;\n this.debug = params.debug ?? process.env.DEBUG_FETCH === \"true\";\n this.token = params.token;\n this.agent = DEFAULT_AGENT;\n }\n\n protected async request(path: string, opts?: RequestParams) {\n const url = new URL(`${this.baseUrl}${path}`);\n if (opts?.query) {\n for (const [key, value] of Object.entries(opts.query)) {\n array(value).forEach((value) => {\n url.searchParams.append(key, value.toString());\n });\n }\n }\n\n const start = Date.now();\n const response = await this.fetch(url.toString(), {\n ...opts,\n body: opts?.body,\n method: opts?.method || \"GET\",\n headers: this.token\n ? { Authorization: `Bearer ${this.token}`, ...opts?.headers }\n : opts?.headers,\n // @ts-expect-error Node.js' and undici's Agent have different types\n dispatcher: this.agent,\n signal: opts?.signal,\n });\n\n if (this.debug) {\n const duration = Date.now() - start;\n console.log(`[API] ${url} (${response.status}) ${duration}ms`);\n if (response.status === 429) {\n const retry = parseInt(response.headers.get(\"Retry-After\") ?? \"\", 10);\n const hours = Math.floor(retry / 60 / 60);\n const minutes = Math.floor(retry / 60) % 60;\n const seconds = retry % 60;\n console.warn(\n `[API] ${url} Rate Limited, Retry After ${hours}h ${minutes}m ${seconds}s`,\n );\n }\n }\n\n return response;\n }\n}\n\nexport interface Parsed<Data> {\n response: Response;\n text: string;\n json: Data;\n}\n\n/**\n * Extract sessionId from a sandbox API URL.\n */\nfunction extractSessionId(url: string): string | undefined {\n const match = url.match(/\\/v2\\/sandboxes\\/sessions\\/([^/?]+)/);\n return match?.[1];\n}\n\n/**\n * Extract sandbox name from a sandbox API url.\n * Excludes known sub-paths like /sessions/ and /snapshots/.\n */\nfunction extractSandboxName(url: string): string | undefined {\n const match = url.match(/\\/v2\\/sandboxes\\/(?!sessions(?:\\/|$|\\?))(?!snapshots(?:\\/|$|\\?))([^/?]+)/);\n return match?.[1];\n}\n\n/**\n * Allows to read the response text and parse it as JSON casting to the given\n * type. If the response is not ok or cannot be parsed it will return error.\n *\n * @param response Response to parse.\n * @returns Parsed response or error.\n */\nexport async function parse<Data, ErrorData>(\n validator: ZodType<Data>,\n response: Response,\n): Promise<Parsed<Data> | APIError<ErrorData>> {\n const sessionId = extractSessionId(response.url);\n let sandboxName: string | undefined;\n if (!sessionId) {\n sandboxName = extractSandboxName(response.url);\n }\n\n const text = await response.text().catch((err) => {\n return new APIError<ErrorData>(response, {\n message: `Can't read response text: ${String(err)}`,\n sessionId,\n sandboxName\n });\n });\n\n if (typeof text !== \"string\") {\n return text;\n }\n\n let json: Data | ErrorData;\n\n try {\n json = JSON.parse(text || \"{}\");\n } catch (error) {\n return new APIError<ErrorData>(response, {\n message: `Can't parse JSON: ${String(error)}`,\n text,\n sessionId,\n sandboxName\n });\n }\n\n if (!response.ok) {\n return new APIError<ErrorData>(response, {\n message: `Status code ${response.status} is not ok`,\n json: json as ErrorData,\n text,\n sessionId,\n sandboxName\n });\n }\n\n const validated = validator.safeParse(json);\n if (!validated.success) {\n return new APIError<ErrorData>(response, {\n message: `Response JSON is not valid: ${validated.error}`,\n json: json as ErrorData,\n text,\n sessionId,\n sandboxName\n });\n }\n\n return {\n json: validated.data,\n response,\n text,\n };\n}\n\nexport async function parseOrThrow<Data, ErrorData>(\n validator: ZodType<Data>,\n response: Response,\n): Promise<Parsed<Data>> {\n const result = await parse<Data, ErrorData>(validator, response);\n if (result instanceof APIError) {\n throw result;\n }\n\n return result;\n}\n"],"mappings":";;;;;;AAeA,MAAM,gBAAgB,IAAI,MAAM,EAC9B,aAAa,GACd,CAAC;;;;;;AAOF,IAAa,aAAb,MAAwB;CAOtB,YAAY,QAKT;AACD,OAAK,QAAQ,UAAU,OAAO,SAAS,WAAW,MAAM;AACxD,OAAK,UAAU,OAAO;AACtB,OAAK,QAAQ,OAAO,SAAS,QAAQ,IAAI,gBAAgB;AACzD,OAAK,QAAQ,OAAO;AACpB,OAAK,QAAQ;;CAGf,MAAgB,QAAQ,MAAc,MAAsB;EAC1D,MAAM,MAAM,IAAI,IAAI,GAAG,KAAK,UAAU,OAAO;AAC7C,MAAI,MAAM,MACR,MAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,KAAK,MAAM,CACnD,OAAM,MAAM,CAAC,SAAS,YAAU;AAC9B,OAAI,aAAa,OAAO,KAAKA,QAAM,UAAU,CAAC;IAC9C;EAIN,MAAM,QAAQ,KAAK,KAAK;EACxB,MAAM,WAAW,MAAM,KAAK,MAAM,IAAI,UAAU,EAAE;GAChD,GAAG;GACH,MAAM,MAAM;GACZ,QAAQ,MAAM,UAAU;GACxB,SAAS,KAAK,QACV;IAAE,eAAe,UAAU,KAAK;IAAS,GAAG,MAAM;IAAS,GAC3D,MAAM;GAEV,YAAY,KAAK;GACjB,QAAQ,MAAM;GACf,CAAC;AAEF,MAAI,KAAK,OAAO;GACd,MAAM,WAAW,KAAK,KAAK,GAAG;AAC9B,WAAQ,IAAI,SAAS,IAAI,IAAI,SAAS,OAAO,IAAI,SAAS,IAAI;AAC9D,OAAI,SAAS,WAAW,KAAK;IAC3B,MAAM,QAAQ,SAAS,SAAS,QAAQ,IAAI,cAAc,IAAI,IAAI,GAAG;IACrE,MAAM,QAAQ,KAAK,MAAM,QAAQ,KAAK,GAAG;IACzC,MAAM,UAAU,KAAK,MAAM,QAAQ,GAAG,GAAG;IACzC,MAAM,UAAU,QAAQ;AACxB,YAAQ,KACN,SAAS,IAAI,6BAA6B,MAAM,IAAI,QAAQ,IAAI,QAAQ,GACzE;;;AAIL,SAAO;;;;;;AAaX,SAAS,iBAAiB,KAAiC;AAEzD,QADc,IAAI,MAAM,sCAAsC,GAC/C;;;;;;AAOjB,SAAS,mBAAmB,KAAiC;AAE3D,QADc,IAAI,MAAM,2EAA2E,GACpF;;;;;;;;;AAUjB,eAAsB,MACpB,WACA,UAC6C;CAC7C,MAAM,YAAY,iBAAiB,SAAS,IAAI;CAChD,IAAIC;AACJ,KAAI,CAAC,UACH,eAAc,mBAAmB,SAAS,IAAI;CAGhD,MAAM,OAAO,MAAM,SAAS,MAAM,CAAC,OAAO,QAAQ;AAChD,SAAO,IAAI,SAAoB,UAAU;GACvC,SAAS,6BAA6B,OAAO,IAAI;GACjD;GACA;GACD,CAAC;GACF;AAEF,KAAI,OAAO,SAAS,SAClB,QAAO;CAGT,IAAIC;AAEJ,KAAI;AACF,SAAO,KAAK,MAAM,QAAQ,KAAK;UACxB,OAAO;AACd,SAAO,IAAI,SAAoB,UAAU;GACvC,SAAS,qBAAqB,OAAO,MAAM;GAC3C;GACA;GACA;GACD,CAAC;;AAGJ,KAAI,CAAC,SAAS,GACZ,QAAO,IAAI,SAAoB,UAAU;EACvC,SAAS,eAAe,SAAS,OAAO;EAClC;EACN;EACA;EACA;EACD,CAAC;CAGJ,MAAM,YAAY,UAAU,UAAU,KAAK;AAC3C,KAAI,CAAC,UAAU,QACb,QAAO,IAAI,SAAoB,UAAU;EACvC,SAAS,+BAA+B,UAAU;EAC5C;EACN;EACA;EACA;EACD,CAAC;AAGJ,QAAO;EACL,MAAM,UAAU;EAChB;EACA;EACD;;AAGH,eAAsB,aACpB,WACA,UACuB;CACvB,MAAM,SAAS,MAAM,MAAuB,WAAW,SAAS;AAChE,KAAI,kBAAkB,SACpB,OAAM;AAGR,QAAO"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let zlib = require("zlib");
|
|
3
|
+
zlib = require_rolldown_runtime.__toESM(zlib);
|
|
4
|
+
let tar_stream = require("tar-stream");
|
|
5
|
+
tar_stream = require_rolldown_runtime.__toESM(tar_stream);
|
|
6
|
+
let stream = require("stream");
|
|
7
|
+
|
|
8
|
+
//#region src/api-client/file-writer.ts
|
|
9
|
+
/**
|
|
10
|
+
* Allows to create a Readable stream with methods to write files
|
|
11
|
+
* to it and to finish it. Files written are compressed together
|
|
12
|
+
* and gzipped in the stream.
|
|
13
|
+
*/
|
|
14
|
+
var FileWriter = class {
|
|
15
|
+
constructor() {
|
|
16
|
+
const gzip = zlib.default.createGzip();
|
|
17
|
+
this.pack = tar_stream.default.pack();
|
|
18
|
+
this.readable = this.pack.pipe(gzip);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Allows to add a file to the stream. Size is required to write
|
|
22
|
+
* the tarball header so when content is a stream it must be
|
|
23
|
+
* provided.
|
|
24
|
+
*
|
|
25
|
+
* Returns a Promise resolved once the file is written in the
|
|
26
|
+
* stream.
|
|
27
|
+
*/
|
|
28
|
+
async addFile(file) {
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
const entry = this.pack.entry("size" in file ? {
|
|
31
|
+
name: file.name,
|
|
32
|
+
size: file.size,
|
|
33
|
+
mode: file.mode
|
|
34
|
+
} : {
|
|
35
|
+
name: file.name,
|
|
36
|
+
size: Buffer.byteLength(file.content),
|
|
37
|
+
mode: file.mode
|
|
38
|
+
}, (error) => {
|
|
39
|
+
if (error) return reject(error);
|
|
40
|
+
else resolve();
|
|
41
|
+
});
|
|
42
|
+
if (file.content instanceof stream.Readable) file.content.pipe(entry);
|
|
43
|
+
else entry.end(file.content);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Allows to finish the stream returning a Promise that will
|
|
48
|
+
* resolve once the readable is effectively closed or
|
|
49
|
+
* errored.
|
|
50
|
+
*/
|
|
51
|
+
async end() {
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
this.readable.on("error", reject);
|
|
54
|
+
this.readable.on("end", resolve);
|
|
55
|
+
this.pack.finalize();
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
exports.FileWriter = FileWriter;
|
|
62
|
+
//# sourceMappingURL=file-writer.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-writer.cjs","names":["tar","Readable"],"sources":["../../src/api-client/file-writer.ts"],"sourcesContent":["import zlib from \"zlib\";\nimport tar, { type Pack } from \"tar-stream\";\nimport { Readable } from \"stream\";\n\ninterface FileData {\n /**\n * The name (path) of the file to write.\n */\n name: string;\n /**\n * The content of the file.\n */\n content: string | Uint8Array;\n /**\n * The file mode (permissions) to set on the file.\n * For example, 0o755 for executable files.\n */\n mode?: number;\n}\n\ninterface FileStream {\n /**\n * The name (path) of the file to write.\n */\n name: string;\n /**\n * A Readable stream to consume the content of the file.\n */\n content: Readable;\n /**\n * The expected size of the file. This is required to write\n * the header of the compressed file.\n */\n size: number;\n /**\n * The file mode (permissions) to set on the file.\n * For example, 0o755 for executable files.\n */\n mode?: number;\n}\n\n/**\n * Allows to create a Readable stream with methods to write files\n * to it and to finish it. Files written are compressed together\n * and gzipped in the stream.\n */\nexport class FileWriter {\n public readable: Readable;\n private pack: Pack;\n\n constructor() {\n const gzip = zlib.createGzip();\n this.pack = tar.pack();\n this.readable = this.pack.pipe(gzip);\n }\n\n /**\n * Allows to add a file to the stream. Size is required to write\n * the tarball header so when content is a stream it must be\n * provided.\n *\n * Returns a Promise resolved once the file is written in the\n * stream.\n */\n async addFile(file: FileData | FileStream) {\n return new Promise<void>((resolve, reject) => {\n const entry = this.pack.entry(\n \"size\" in file\n ? { name: file.name, size: file.size, mode: file.mode }\n : {\n name: file.name,\n size: Buffer.byteLength(file.content),\n mode: file.mode,\n },\n (error) => {\n if (error) {\n return reject(error);\n } else {\n resolve();\n }\n },\n );\n\n if (file.content instanceof Readable) {\n file.content.pipe(entry);\n } else {\n entry.end(file.content);\n }\n });\n }\n\n /**\n * Allows to finish the stream returning a Promise that will\n * resolve once the readable is effectively closed or\n * errored.\n */\n async end() {\n return new Promise<void>((resolve, reject) => {\n this.readable.on(\"error\", reject);\n this.readable.on(\"end\", resolve);\n this.pack.finalize();\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;AA8CA,IAAa,aAAb,MAAwB;CAItB,cAAc;EACZ,MAAM,OAAO,aAAK,YAAY;AAC9B,OAAK,OAAOA,mBAAI,MAAM;AACtB,OAAK,WAAW,KAAK,KAAK,KAAK,KAAK;;;;;;;;;;CAWtC,MAAM,QAAQ,MAA6B;AACzC,SAAO,IAAI,SAAe,SAAS,WAAW;GAC5C,MAAM,QAAQ,KAAK,KAAK,MACtB,UAAU,OACN;IAAE,MAAM,KAAK;IAAM,MAAM,KAAK;IAAM,MAAM,KAAK;IAAM,GACrD;IACE,MAAM,KAAK;IACX,MAAM,OAAO,WAAW,KAAK,QAAQ;IACrC,MAAM,KAAK;IACZ,GACJ,UAAU;AACT,QAAI,MACF,QAAO,OAAO,MAAM;QAEpB,UAAS;KAGd;AAED,OAAI,KAAK,mBAAmBC,gBAC1B,MAAK,QAAQ,KAAK,MAAM;OAExB,OAAM,IAAI,KAAK,QAAQ;IAEzB;;;;;;;CAQJ,MAAM,MAAM;AACV,SAAO,IAAI,SAAe,SAAS,WAAW;AAC5C,QAAK,SAAS,GAAG,SAAS,OAAO;AACjC,QAAK,SAAS,GAAG,OAAO,QAAQ;AAChC,QAAK,KAAK,UAAU;IACpB"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Readable } from "stream";
|
|
2
|
+
|
|
3
|
+
//#region src/api-client/file-writer.d.ts
|
|
4
|
+
interface FileData {
|
|
5
|
+
/**
|
|
6
|
+
* The name (path) of the file to write.
|
|
7
|
+
*/
|
|
8
|
+
name: string;
|
|
9
|
+
/**
|
|
10
|
+
* The content of the file.
|
|
11
|
+
*/
|
|
12
|
+
content: string | Uint8Array;
|
|
13
|
+
/**
|
|
14
|
+
* The file mode (permissions) to set on the file.
|
|
15
|
+
* For example, 0o755 for executable files.
|
|
16
|
+
*/
|
|
17
|
+
mode?: number;
|
|
18
|
+
}
|
|
19
|
+
interface FileStream {
|
|
20
|
+
/**
|
|
21
|
+
* The name (path) of the file to write.
|
|
22
|
+
*/
|
|
23
|
+
name: string;
|
|
24
|
+
/**
|
|
25
|
+
* A Readable stream to consume the content of the file.
|
|
26
|
+
*/
|
|
27
|
+
content: Readable;
|
|
28
|
+
/**
|
|
29
|
+
* The expected size of the file. This is required to write
|
|
30
|
+
* the header of the compressed file.
|
|
31
|
+
*/
|
|
32
|
+
size: number;
|
|
33
|
+
/**
|
|
34
|
+
* The file mode (permissions) to set on the file.
|
|
35
|
+
* For example, 0o755 for executable files.
|
|
36
|
+
*/
|
|
37
|
+
mode?: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Allows to create a Readable stream with methods to write files
|
|
41
|
+
* to it and to finish it. Files written are compressed together
|
|
42
|
+
* and gzipped in the stream.
|
|
43
|
+
*/
|
|
44
|
+
declare class FileWriter {
|
|
45
|
+
readable: Readable;
|
|
46
|
+
private pack;
|
|
47
|
+
constructor();
|
|
48
|
+
/**
|
|
49
|
+
* Allows to add a file to the stream. Size is required to write
|
|
50
|
+
* the tarball header so when content is a stream it must be
|
|
51
|
+
* provided.
|
|
52
|
+
*
|
|
53
|
+
* Returns a Promise resolved once the file is written in the
|
|
54
|
+
* stream.
|
|
55
|
+
*/
|
|
56
|
+
addFile(file: FileData | FileStream): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Allows to finish the stream returning a Promise that will
|
|
59
|
+
* resolve once the readable is effectively closed or
|
|
60
|
+
* errored.
|
|
61
|
+
*/
|
|
62
|
+
end(): Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
export { FileWriter };
|
|
66
|
+
//# sourceMappingURL=file-writer.d.cts.map
|
|
@@ -1,52 +1,66 @@
|
|
|
1
1
|
import { Readable } from "stream";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
|
|
3
|
+
//#region src/api-client/file-writer.d.ts
|
|
4
|
+
interface FileData {
|
|
5
|
+
/**
|
|
6
|
+
* The name (path) of the file to write.
|
|
7
|
+
*/
|
|
8
|
+
name: string;
|
|
9
|
+
/**
|
|
10
|
+
* The content of the file.
|
|
11
|
+
*/
|
|
12
|
+
content: string | Uint8Array;
|
|
13
|
+
/**
|
|
14
|
+
* The file mode (permissions) to set on the file.
|
|
15
|
+
* For example, 0o755 for executable files.
|
|
16
|
+
*/
|
|
17
|
+
mode?: number;
|
|
11
18
|
}
|
|
12
19
|
interface FileStream {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
/**
|
|
21
|
+
* The name (path) of the file to write.
|
|
22
|
+
*/
|
|
23
|
+
name: string;
|
|
24
|
+
/**
|
|
25
|
+
* A Readable stream to consume the content of the file.
|
|
26
|
+
*/
|
|
27
|
+
content: Readable;
|
|
28
|
+
/**
|
|
29
|
+
* The expected size of the file. This is required to write
|
|
30
|
+
* the header of the compressed file.
|
|
31
|
+
*/
|
|
32
|
+
size: number;
|
|
33
|
+
/**
|
|
34
|
+
* The file mode (permissions) to set on the file.
|
|
35
|
+
* For example, 0o755 for executable files.
|
|
36
|
+
*/
|
|
37
|
+
mode?: number;
|
|
26
38
|
}
|
|
27
39
|
/**
|
|
28
40
|
* Allows to create a Readable stream with methods to write files
|
|
29
41
|
* to it and to finish it. Files written are compressed together
|
|
30
42
|
* and gzipped in the stream.
|
|
31
43
|
*/
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
44
|
+
declare class FileWriter {
|
|
45
|
+
readable: Readable;
|
|
46
|
+
private pack;
|
|
47
|
+
constructor();
|
|
48
|
+
/**
|
|
49
|
+
* Allows to add a file to the stream. Size is required to write
|
|
50
|
+
* the tarball header so when content is a stream it must be
|
|
51
|
+
* provided.
|
|
52
|
+
*
|
|
53
|
+
* Returns a Promise resolved once the file is written in the
|
|
54
|
+
* stream.
|
|
55
|
+
*/
|
|
56
|
+
addFile(file: FileData | FileStream): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Allows to finish the stream returning a Promise that will
|
|
59
|
+
* resolve once the readable is effectively closed or
|
|
60
|
+
* errored.
|
|
61
|
+
*/
|
|
62
|
+
end(): Promise<void>;
|
|
51
63
|
}
|
|
52
|
-
|
|
64
|
+
//#endregion
|
|
65
|
+
export { FileWriter };
|
|
66
|
+
//# sourceMappingURL=file-writer.d.ts.map
|