@vercel/sandbox 1.9.0 → 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.
Files changed (190) hide show
  1. package/README.md +8 -0
  2. package/dist/_virtual/rolldown_runtime.cjs +29 -0
  3. package/dist/api-client/api-client.cjs +375 -0
  4. package/dist/api-client/api-client.cjs.map +1 -0
  5. package/dist/api-client/api-client.d.cts +403 -0
  6. package/dist/api-client/api-client.d.ts +397 -392
  7. package/dist/api-client/api-client.js +365 -405
  8. package/dist/api-client/api-client.js.map +1 -1
  9. package/dist/api-client/api-error.cjs +31 -0
  10. package/dist/api-client/api-error.cjs.map +1 -0
  11. package/dist/api-client/api-error.d.cts +27 -0
  12. package/dist/api-client/api-error.d.ts +19 -16
  13. package/dist/api-client/api-error.js +27 -32
  14. package/dist/api-client/api-error.js.map +1 -1
  15. package/dist/api-client/base-client.cjs +113 -0
  16. package/dist/api-client/base-client.cjs.map +1 -0
  17. package/dist/api-client/base-client.d.cts +38 -0
  18. package/dist/api-client/base-client.d.ts +31 -36
  19. package/dist/api-client/base-client.js +101 -118
  20. package/dist/api-client/base-client.js.map +1 -1
  21. package/dist/api-client/file-writer.cjs +62 -0
  22. package/dist/api-client/file-writer.cjs.map +1 -0
  23. package/dist/api-client/file-writer.d.cts +66 -0
  24. package/dist/api-client/file-writer.d.ts +56 -52
  25. package/dist/api-client/file-writer.js +57 -61
  26. package/dist/api-client/file-writer.js.map +1 -1
  27. package/dist/api-client/index.cjs +2 -0
  28. package/dist/api-client/index.d.ts +2 -2
  29. package/dist/api-client/index.js +4 -21
  30. package/dist/api-client/validators.cjs +149 -0
  31. package/dist/api-client/validators.cjs.map +1 -0
  32. package/dist/api-client/validators.d.cts +1677 -0
  33. package/dist/api-client/validators.d.ts +1501 -2412
  34. package/dist/api-client/validators.js +124 -154
  35. package/dist/api-client/validators.js.map +1 -1
  36. package/dist/api-client/with-retry.cjs +89 -0
  37. package/dist/api-client/with-retry.cjs.map +1 -0
  38. package/dist/api-client/with-retry.d.cts +10 -0
  39. package/dist/api-client/with-retry.d.ts +9 -13
  40. package/dist/api-client/with-retry.js +81 -102
  41. package/dist/api-client/with-retry.js.map +1 -1
  42. package/dist/auth/api.cjs +29 -0
  43. package/dist/auth/api.cjs.map +1 -0
  44. package/dist/auth/api.js +26 -25
  45. package/dist/auth/api.js.map +1 -1
  46. package/dist/auth/error.cjs +13 -0
  47. package/dist/auth/error.cjs.map +1 -0
  48. package/dist/auth/error.js +11 -11
  49. package/dist/auth/error.js.map +1 -1
  50. package/dist/auth/file.cjs +64 -0
  51. package/dist/auth/file.cjs.map +1 -0
  52. package/dist/auth/file.d.cts +26 -0
  53. package/dist/auth/file.d.ts +19 -15
  54. package/dist/auth/file.js +49 -64
  55. package/dist/auth/file.js.map +1 -1
  56. package/dist/auth/index.cjs +12 -0
  57. package/dist/auth/index.d.cts +5 -0
  58. package/dist/auth/index.d.ts +5 -6
  59. package/dist/auth/index.js +6 -27
  60. package/dist/auth/linked-project.cjs +38 -0
  61. package/dist/auth/linked-project.cjs.map +1 -0
  62. package/dist/auth/linked-project.js +30 -64
  63. package/dist/auth/linked-project.js.map +1 -1
  64. package/dist/auth/oauth.cjs +205 -0
  65. package/dist/auth/oauth.cjs.map +1 -0
  66. package/dist/auth/oauth.d.cts +135 -0
  67. package/dist/auth/oauth.d.ts +113 -109
  68. package/dist/auth/oauth.js +185 -252
  69. package/dist/auth/oauth.js.map +1 -1
  70. package/dist/auth/poll-for-token.cjs +82 -0
  71. package/dist/auth/poll-for-token.cjs.map +1 -0
  72. package/dist/auth/poll-for-token.d.cts +28 -0
  73. package/dist/auth/poll-for-token.d.ts +23 -15
  74. package/dist/auth/poll-for-token.js +79 -64
  75. package/dist/auth/poll-for-token.js.map +1 -1
  76. package/dist/auth/project.cjs +80 -0
  77. package/dist/auth/project.cjs.map +1 -0
  78. package/dist/auth/project.d.cts +44 -0
  79. package/dist/auth/project.d.ts +12 -8
  80. package/dist/auth/project.js +70 -72
  81. package/dist/auth/project.js.map +1 -1
  82. package/dist/auth/zod.cjs +22 -0
  83. package/dist/auth/zod.cjs.map +1 -0
  84. package/dist/auth/zod.js +18 -17
  85. package/dist/auth/zod.js.map +1 -1
  86. package/dist/command.cjs +326 -0
  87. package/dist/command.cjs.map +1 -0
  88. package/dist/command.d.cts +289 -0
  89. package/dist/command.d.ts +265 -171
  90. package/dist/command.js +321 -226
  91. package/dist/command.js.map +1 -1
  92. package/dist/constants.d.cts +5 -0
  93. package/dist/constants.d.ts +5 -1
  94. package/dist/index.cjs +11 -0
  95. package/dist/index.d.cts +6 -0
  96. package/dist/index.d.ts +6 -5
  97. package/dist/index.js +6 -15
  98. package/dist/network-policy.d.cts +100 -0
  99. package/dist/network-policy.d.ts +32 -28
  100. package/dist/sandbox.cjs +543 -0
  101. package/dist/sandbox.cjs.map +1 -0
  102. package/dist/sandbox.d.cts +538 -0
  103. package/dist/sandbox.d.ts +521 -479
  104. package/dist/sandbox.js +539 -512
  105. package/dist/sandbox.js.map +1 -1
  106. package/dist/snapshot.cjs +116 -0
  107. package/dist/snapshot.cjs.map +1 -0
  108. package/dist/snapshot.d.cts +109 -0
  109. package/dist/snapshot.d.ts +100 -92
  110. package/dist/snapshot.js +114 -114
  111. package/dist/snapshot.js.map +1 -1
  112. package/dist/utils/array.cjs +17 -0
  113. package/dist/utils/array.cjs.map +1 -0
  114. package/dist/utils/array.js +12 -15
  115. package/dist/utils/array.js.map +1 -1
  116. package/dist/utils/consume-readable.cjs +18 -0
  117. package/dist/utils/consume-readable.cjs.map +1 -0
  118. package/dist/utils/consume-readable.js +13 -12
  119. package/dist/utils/consume-readable.js.map +1 -1
  120. package/dist/utils/decode-base64-url.cjs +15 -0
  121. package/dist/utils/decode-base64-url.cjs.map +1 -0
  122. package/dist/utils/decode-base64-url.js +10 -9
  123. package/dist/utils/decode-base64-url.js.map +1 -1
  124. package/dist/utils/dev-credentials.cjs +142 -0
  125. package/dist/utils/dev-credentials.cjs.map +1 -0
  126. package/dist/utils/dev-credentials.js +126 -184
  127. package/dist/utils/dev-credentials.js.map +1 -1
  128. package/dist/utils/get-credentials.cjs +123 -0
  129. package/dist/utils/get-credentials.cjs.map +1 -0
  130. package/dist/utils/get-credentials.d.cts +21 -0
  131. package/dist/utils/get-credentials.d.ts +19 -61
  132. package/dist/utils/get-credentials.js +106 -140
  133. package/dist/utils/get-credentials.js.map +1 -1
  134. package/dist/utils/log.cjs +25 -0
  135. package/dist/utils/log.cjs.map +1 -0
  136. package/dist/utils/log.js +15 -17
  137. package/dist/utils/log.js.map +1 -1
  138. package/dist/utils/network-policy.cjs +65 -0
  139. package/dist/utils/network-policy.cjs.map +1 -0
  140. package/dist/utils/network-policy.js +58 -77
  141. package/dist/utils/network-policy.js.map +1 -1
  142. package/dist/utils/normalizePath.cjs +27 -0
  143. package/dist/utils/normalizePath.cjs.map +1 -0
  144. package/dist/utils/normalizePath.js +21 -28
  145. package/dist/utils/normalizePath.js.map +1 -1
  146. package/dist/utils/resolveSignal.cjs +20 -0
  147. package/dist/utils/resolveSignal.cjs.map +1 -0
  148. package/dist/utils/resolveSignal.d.cts +15 -0
  149. package/dist/utils/resolveSignal.d.ts +12 -10
  150. package/dist/utils/resolveSignal.js +14 -17
  151. package/dist/utils/resolveSignal.js.map +1 -1
  152. package/dist/utils/sandbox-snapshot.cjs +14 -0
  153. package/dist/utils/sandbox-snapshot.cjs.map +1 -0
  154. package/dist/utils/sandbox-snapshot.d.cts +10 -0
  155. package/dist/utils/sandbox-snapshot.d.ts +11 -0
  156. package/dist/utils/sandbox-snapshot.js +14 -0
  157. package/dist/utils/sandbox-snapshot.js.map +1 -0
  158. package/dist/utils/types.cjs +13 -0
  159. package/dist/utils/types.cjs.map +1 -0
  160. package/dist/utils/types.d.cts +11 -0
  161. package/dist/utils/types.d.ts +5 -7
  162. package/dist/utils/types.js +8 -8
  163. package/dist/utils/types.js.map +1 -1
  164. package/dist/version.cjs +7 -0
  165. package/dist/version.cjs.map +1 -0
  166. package/dist/version.js +5 -5
  167. package/dist/version.js.map +1 -1
  168. package/package.json +23 -3
  169. package/dist/api-client/index.js.map +0 -1
  170. package/dist/auth/api.d.ts +0 -6
  171. package/dist/auth/error.d.ts +0 -11
  172. package/dist/auth/index.js.map +0 -1
  173. package/dist/auth/linked-project.d.ts +0 -10
  174. package/dist/auth/zod.d.ts +0 -5
  175. package/dist/constants.js +0 -3
  176. package/dist/constants.js.map +0 -1
  177. package/dist/index.js.map +0 -1
  178. package/dist/network-policy.js +0 -3
  179. package/dist/network-policy.js.map +0 -1
  180. package/dist/utils/array.d.ts +0 -9
  181. package/dist/utils/consume-readable.d.ts +0 -5
  182. package/dist/utils/convert-sandbox.d.ts +0 -6
  183. package/dist/utils/convert-sandbox.js +0 -14
  184. package/dist/utils/convert-sandbox.js.map +0 -1
  185. package/dist/utils/decode-base64-url.d.ts +0 -7
  186. package/dist/utils/dev-credentials.d.ts +0 -37
  187. package/dist/utils/log.d.ts +0 -2
  188. package/dist/utils/network-policy.d.ts +0 -7
  189. package/dist/utils/normalizePath.d.ts +0 -17
  190. package/dist/version.d.ts +0 -1
@@ -1,128 +1,111 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BaseClient = void 0;
4
- exports.parse = parse;
5
- exports.parseOrThrow = parseOrThrow;
6
- const api_error_1 = require("./api-error");
7
- const array_1 = require("../utils/array");
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
10
7
  /**
11
- * A base API client that provides a convenience wrapper for fetching where
12
- * we can pass query parameters as an object, support retries, debugging
13
- * and automatic authorization.
14
- */
15
- class BaseClient {
16
- constructor(params) {
17
- this.fetch = (0, with_retry_1.withRetry)(params.fetch ?? globalThis.fetch);
18
- this.baseUrl = params.baseUrl;
19
- this.debug = params.debug ?? process.env.DEBUG_FETCH === "true";
20
- this.token = params.token;
21
- this.agent = new undici_1.Agent({
22
- bodyTimeout: 0, // disable body timeout to allow long logs streaming
23
- });
24
- }
25
- async request(path, opts) {
26
- const url = new URL(`${this.baseUrl}${path}`);
27
- if (opts?.query) {
28
- for (const [key, value] of Object.entries(opts.query)) {
29
- (0, array_1.array)(value).forEach((value) => {
30
- url.searchParams.append(key, value.toString());
31
- });
32
- }
33
- }
34
- const start = Date.now();
35
- const response = await this.fetch(url.toString(), {
36
- ...opts,
37
- body: opts?.body,
38
- method: opts?.method || "GET",
39
- headers: this.token
40
- ? { Authorization: `Bearer ${this.token}`, ...opts?.headers }
41
- : opts?.headers,
42
- // @ts-expect-error Node.js' and undici's Agent have different types
43
- dispatcher: this.agent,
44
- signal: opts?.signal,
45
- });
46
- if (this.debug) {
47
- const duration = Date.now() - start;
48
- console.log(`[API] ${url} (${response.status}) ${duration}ms`);
49
- if (response.status === 429) {
50
- const retry = parseInt(response.headers.get("Retry-After") ?? "", 10);
51
- const hours = Math.floor(retry / 60 / 60);
52
- const minutes = Math.floor(retry / 60) % 60;
53
- const seconds = retry % 60;
54
- console.warn(`[API] ${url} Rate Limited, Retry After ${hours}h ${minutes}m ${seconds}s`);
55
- }
56
- }
57
- return response;
58
- }
59
- }
60
- exports.BaseClient = BaseClient;
8
+ * A base API client that provides a convenience wrapper for fetching where
9
+ * we can pass query parameters as an object, support retries, debugging
10
+ * and automatic authorization.
11
+ */
12
+ var BaseClient = class {
13
+ constructor(params) {
14
+ this.fetch = withRetry(params.fetch ?? globalThis.fetch);
15
+ this.baseUrl = params.baseUrl;
16
+ this.debug = params.debug ?? process.env.DEBUG_FETCH === "true";
17
+ this.token = params.token;
18
+ this.agent = new Agent({ bodyTimeout: 0 });
19
+ }
20
+ async request(path, opts) {
21
+ const url = new URL(`${this.baseUrl}${path}`);
22
+ if (opts?.query) for (const [key, value] of Object.entries(opts.query)) array(value).forEach((value$1) => {
23
+ url.searchParams.append(key, value$1.toString());
24
+ });
25
+ const start = Date.now();
26
+ const response = await this.fetch(url.toString(), {
27
+ ...opts,
28
+ body: opts?.body,
29
+ method: opts?.method || "GET",
30
+ headers: this.token ? {
31
+ Authorization: `Bearer ${this.token}`,
32
+ ...opts?.headers
33
+ } : opts?.headers,
34
+ dispatcher: this.agent,
35
+ signal: opts?.signal
36
+ });
37
+ if (this.debug) {
38
+ const duration = Date.now() - start;
39
+ console.log(`[API] ${url} (${response.status}) ${duration}ms`);
40
+ if (response.status === 429) {
41
+ const retry = parseInt(response.headers.get("Retry-After") ?? "", 10);
42
+ const hours = Math.floor(retry / 60 / 60);
43
+ const minutes = Math.floor(retry / 60) % 60;
44
+ const seconds = retry % 60;
45
+ console.warn(`[API] ${url} Rate Limited, Retry After ${hours}h ${minutes}m ${seconds}s`);
46
+ }
47
+ }
48
+ return response;
49
+ }
50
+ };
61
51
  /**
62
- * Extract sandboxId from a sandbox API URL.
63
- * URLs follow the pattern: /v1/sandboxes/{sandboxId}/...
64
- */
52
+ * Extract sandboxId from a sandbox API URL.
53
+ * URLs follow the pattern: /v1/sandboxes/{sandboxId}/...
54
+ */
65
55
  function extractSandboxId(url) {
66
- const match = url.match(/\/v1\/sandboxes\/([^/?]+)/);
67
- return match?.[1];
56
+ return url.match(/\/v1\/sandboxes\/([^/?]+)/)?.[1];
68
57
  }
69
58
  /**
70
- * Allows to read the response text and parse it as JSON casting to the given
71
- * type. If the response is not ok or cannot be parsed it will return error.
72
- *
73
- * @param response Response to parse.
74
- * @returns Parsed response or error.
75
- */
59
+ * Allows to read the response text and parse it as JSON casting to the given
60
+ * type. If the response is not ok or cannot be parsed it will return error.
61
+ *
62
+ * @param response Response to parse.
63
+ * @returns Parsed response or error.
64
+ */
76
65
  async function parse(validator, response) {
77
- const sandboxId = extractSandboxId(response.url);
78
- const text = await response.text().catch((err) => {
79
- return new api_error_1.APIError(response, {
80
- message: `Can't read response text: ${String(err)}`,
81
- sandboxId,
82
- });
83
- });
84
- if (typeof text !== "string") {
85
- return text;
86
- }
87
- let json;
88
- try {
89
- json = JSON.parse(text || "{}");
90
- }
91
- catch (error) {
92
- return new api_error_1.APIError(response, {
93
- message: `Can't parse JSON: ${String(error)}`,
94
- text,
95
- sandboxId,
96
- });
97
- }
98
- if (!response.ok) {
99
- return new api_error_1.APIError(response, {
100
- message: `Status code ${response.status} is not ok`,
101
- json: json,
102
- text,
103
- sandboxId,
104
- });
105
- }
106
- const validated = validator.safeParse(json);
107
- if (!validated.success) {
108
- return new api_error_1.APIError(response, {
109
- message: `Response JSON is not valid: ${validated.error}`,
110
- json: json,
111
- text,
112
- sandboxId,
113
- });
114
- }
115
- return {
116
- json: validated.data,
117
- response,
118
- text,
119
- };
66
+ const sandboxId = extractSandboxId(response.url);
67
+ const text = await response.text().catch((err) => {
68
+ return new APIError(response, {
69
+ message: `Can't read response text: ${String(err)}`,
70
+ sandboxId
71
+ });
72
+ });
73
+ if (typeof text !== "string") return text;
74
+ let json;
75
+ try {
76
+ json = JSON.parse(text || "{}");
77
+ } catch (error) {
78
+ return new APIError(response, {
79
+ message: `Can't parse JSON: ${String(error)}`,
80
+ text,
81
+ sandboxId
82
+ });
83
+ }
84
+ if (!response.ok) return new APIError(response, {
85
+ message: `Status code ${response.status} is not ok`,
86
+ json,
87
+ text,
88
+ sandboxId
89
+ });
90
+ const validated = validator.safeParse(json);
91
+ if (!validated.success) return new APIError(response, {
92
+ message: `Response JSON is not valid: ${validated.error}`,
93
+ json,
94
+ text,
95
+ sandboxId
96
+ });
97
+ return {
98
+ json: validated.data,
99
+ response,
100
+ text
101
+ };
120
102
  }
121
103
  async function parseOrThrow(validator, response) {
122
- const result = await parse(validator, response);
123
- if (result instanceof api_error_1.APIError) {
124
- throw result;
125
- }
126
- return result;
104
+ const result = await parse(validator, response);
105
+ if (result instanceof APIError) throw result;
106
+ return result;
127
107
  }
108
+
109
+ //#endregion
110
+ export { BaseClient, parseOrThrow };
128
111
  //# sourceMappingURL=base-client.js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AED,MAAM,SAAS,GAAG,SAAS,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IAC5C,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC;QACvB,OAAO,IAAI,oBAAQ,CAAY,QAAQ,EAAE;YACvC,OAAO,EAAE,+BAA+B,SAAS,CAAC,KAAK,EAAE;YACzD,IAAI,EAAE,IAAiB;YACvB,IAAI;YACJ,SAAS;SACV,CAAC,CAAC;IACL,CAAC;IAED,OAAO;QACL,IAAI,EAAE,SAAS,CAAC,IAAI;QACpB,QAAQ;QACR,IAAI;KACL,CAAC;AACJ,CAAC;AAEM,KAAK,UAAU,YAAY,CAChC,SAAwB,EACxB,QAAkB;IAElB,MAAM,MAAM,GAAG,MAAM,KAAK,CAAkB,SAAS,EAAE,QAAQ,CAAC,CAAC;IACjE,IAAI,MAAM,YAAY,oBAAQ,EAAE,CAAC;QAC/B,MAAM,MAAM,CAAC;IACf,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
1
+ {"version":3,"file":"base-client.js","names":["value","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\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 = new Agent({\n bodyTimeout: 0, // disable body timeout to allow long logs streaming\n });\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 sandboxId from a sandbox API URL.\n * URLs follow the pattern: /v1/sandboxes/{sandboxId}/...\n */\nfunction extractSandboxId(url: string): string | undefined {\n const match = url.match(/\\/v1\\/sandboxes\\/([^/?]+)/);\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 sandboxId = extractSandboxId(response.url);\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 sandboxId,\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 sandboxId,\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 sandboxId,\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 sandboxId,\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":";;;;;;;;;;;AAoBA,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,IAAI,MAAM,EACrB,aAAa,GACd,CAAC;;CAGJ,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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SAAS,OAAO;EAClC;EACN;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;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,62 +1,66 @@
1
1
  import { Readable } from "stream";
2
- interface FileBuffer {
3
- /**
4
- * The name (path) of the file to write.
5
- */
6
- name: string;
7
- /**
8
- * The content of the file as a Buffer.
9
- */
10
- content: Buffer;
11
- /**
12
- * The file mode (permissions) to set on the file.
13
- * For example, 0o755 for executable files.
14
- */
15
- mode?: number;
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;
16
18
  }
17
19
  interface FileStream {
18
- /**
19
- * The name (path) of the file to write.
20
- */
21
- name: string;
22
- /**
23
- * A Readable stream to consume the content of the file.
24
- */
25
- content: Readable;
26
- /**
27
- * The expected size of the file. This is required to write
28
- * the header of the compressed file.
29
- */
30
- size: number;
31
- /**
32
- * The file mode (permissions) to set on the file.
33
- * For example, 0o755 for executable files.
34
- */
35
- mode?: number;
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;
36
38
  }
37
39
  /**
38
40
  * Allows to create a Readable stream with methods to write files
39
41
  * to it and to finish it. Files written are compressed together
40
42
  * and gzipped in the stream.
41
43
  */
42
- export declare class FileWriter {
43
- readable: Readable;
44
- private pack;
45
- constructor();
46
- /**
47
- * Allows to add a file to the stream. Size is required to write
48
- * the tarball header so when content is a stream it must be
49
- * provided.
50
- *
51
- * Returns a Promise resolved once the file is written in the
52
- * stream.
53
- */
54
- addFile(file: FileBuffer | FileStream): Promise<void>;
55
- /**
56
- * Allows to finish the stream returning a Promise that will
57
- * resolve once the readable is effectively closed or
58
- * errored.
59
- */
60
- end(): Promise<void>;
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>;
61
63
  }
62
- export {};
64
+ //#endregion
65
+ export { FileWriter };
66
+ //# sourceMappingURL=file-writer.d.ts.map
@@ -1,63 +1,59 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.FileWriter = void 0;
7
- const zlib_1 = __importDefault(require("zlib"));
8
- const tar_stream_1 = __importDefault(require("tar-stream"));
9
- const stream_1 = require("stream");
1
+ import zlib from "zlib";
2
+ import tar from "tar-stream";
3
+ import { Readable } from "stream";
4
+
5
+ //#region src/api-client/file-writer.ts
10
6
  /**
11
- * Allows to create a Readable stream with methods to write files
12
- * to it and to finish it. Files written are compressed together
13
- * and gzipped in the stream.
14
- */
15
- class FileWriter {
16
- constructor() {
17
- const gzip = zlib_1.default.createGzip();
18
- this.pack = tar_stream_1.default.pack();
19
- this.readable = this.pack.pipe(gzip);
20
- }
21
- /**
22
- * Allows to add a file to the stream. Size is required to write
23
- * the tarball header so when content is a stream it must be
24
- * provided.
25
- *
26
- * Returns a Promise resolved once the file is written in the
27
- * stream.
28
- */
29
- async addFile(file) {
30
- return new Promise((resolve, reject) => {
31
- const entry = this.pack.entry("size" in file
32
- ? { name: file.name, size: file.size, mode: file.mode }
33
- : { name: file.name, size: file.content.length, mode: file.mode }, (error) => {
34
- if (error) {
35
- return reject(error);
36
- }
37
- else {
38
- resolve();
39
- }
40
- });
41
- if (file.content instanceof stream_1.Readable) {
42
- file.content.pipe(entry);
43
- }
44
- else {
45
- entry.end(file.content);
46
- }
47
- });
48
- }
49
- /**
50
- * Allows to finish the stream returning a Promise that will
51
- * resolve once the readable is effectively closed or
52
- * errored.
53
- */
54
- async end() {
55
- return new Promise((resolve, reject) => {
56
- this.readable.on("error", reject);
57
- this.readable.on("end", resolve);
58
- this.pack.finalize();
59
- });
60
- }
61
- }
62
- exports.FileWriter = FileWriter;
7
+ * Allows to create a Readable stream with methods to write files
8
+ * to it and to finish it. Files written are compressed together
9
+ * and gzipped in the stream.
10
+ */
11
+ var FileWriter = class {
12
+ constructor() {
13
+ const gzip = zlib.createGzip();
14
+ this.pack = tar.pack();
15
+ this.readable = this.pack.pipe(gzip);
16
+ }
17
+ /**
18
+ * Allows to add a file to the stream. Size is required to write
19
+ * the tarball header so when content is a stream it must be
20
+ * provided.
21
+ *
22
+ * Returns a Promise resolved once the file is written in the
23
+ * stream.
24
+ */
25
+ async addFile(file) {
26
+ return new Promise((resolve, reject) => {
27
+ const entry = this.pack.entry("size" in file ? {
28
+ name: file.name,
29
+ size: file.size,
30
+ mode: file.mode
31
+ } : {
32
+ name: file.name,
33
+ size: Buffer.byteLength(file.content),
34
+ mode: file.mode
35
+ }, (error) => {
36
+ if (error) return reject(error);
37
+ else resolve();
38
+ });
39
+ if (file.content instanceof Readable) file.content.pipe(entry);
40
+ else entry.end(file.content);
41
+ });
42
+ }
43
+ /**
44
+ * Allows to finish the stream returning a Promise that will
45
+ * resolve once the readable is effectively closed or
46
+ * errored.
47
+ */
48
+ async end() {
49
+ return new Promise((resolve, reject) => {
50
+ this.readable.on("error", reject);
51
+ this.readable.on("end", resolve);
52
+ this.pack.finalize();
53
+ });
54
+ }
55
+ };
56
+
57
+ //#endregion
58
+ export { FileWriter };
63
59
  //# sourceMappingURL=file-writer.js.map