@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.
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 -391
  7. package/dist/api-client/api-client.js +365 -404
  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 -42
  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 -472
  104. package/dist/sandbox.js +539 -506
  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,157 +1,127 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SnapshotResponse = exports.CreateSnapshotResponse = exports.UpdateNetworkPolicyResponse = exports.ExtendTimeoutResponse = exports.SnapshotsResponse = exports.SandboxesResponse = exports.LogLine = exports.LogError = exports.LogLineStderr = exports.LogLineStdout = exports.EmptyResponse = exports.CommandFinishedResponse = exports.CommandResponse = exports.SandboxAndRoutesResponse = exports.SandboxResponse = exports.Command = exports.Pagination = exports.Snapshot = exports.SandboxRoute = exports.Sandbox = exports.NetworkPolicyValidator = exports.InjectionRuleValidator = void 0;
4
- const zod_1 = require("zod");
5
- exports.InjectionRuleValidator = zod_1.z.object({
6
- domain: zod_1.z.string(),
7
- // headers are only sent in requests
8
- headers: zod_1.z.record(zod_1.z.string()).optional(),
9
- // headerNames are returned in responses
10
- headerNames: zod_1.z.array(zod_1.z.string()).optional(),
11
- });
12
- exports.NetworkPolicyValidator = zod_1.z.union([
13
- zod_1.z.object({ mode: zod_1.z.literal("allow-all") }).passthrough(),
14
- zod_1.z.object({ mode: zod_1.z.literal("deny-all") }).passthrough(),
15
- zod_1.z
16
- .object({
17
- mode: zod_1.z.literal("custom"),
18
- allowedDomains: zod_1.z.array(zod_1.z.string()).optional(),
19
- allowedCIDRs: zod_1.z.array(zod_1.z.string()).optional(),
20
- deniedCIDRs: zod_1.z.array(zod_1.z.string()).optional(),
21
- injectionRules: zod_1.z.array(exports.InjectionRuleValidator).optional(),
22
- })
23
- .passthrough(),
1
+ import { z } from "zod";
2
+
3
+ //#region src/api-client/validators.ts
4
+ const InjectionRuleValidator = z.object({
5
+ domain: z.string(),
6
+ headers: z.record(z.string()).optional(),
7
+ headerNames: z.array(z.string()).optional()
8
+ });
9
+ const NetworkPolicyValidator = z.union([
10
+ z.object({ mode: z.literal("allow-all") }).passthrough(),
11
+ z.object({ mode: z.literal("deny-all") }).passthrough(),
12
+ z.object({
13
+ mode: z.literal("custom"),
14
+ allowedDomains: z.array(z.string()).optional(),
15
+ allowedCIDRs: z.array(z.string()).optional(),
16
+ deniedCIDRs: z.array(z.string()).optional(),
17
+ injectionRules: z.array(InjectionRuleValidator).optional()
18
+ }).passthrough()
24
19
  ]);
25
- exports.Sandbox = zod_1.z.object({
26
- id: zod_1.z.string(),
27
- memory: zod_1.z.number(),
28
- vcpus: zod_1.z.number(),
29
- region: zod_1.z.string(),
30
- runtime: zod_1.z.string(),
31
- timeout: zod_1.z.number(),
32
- status: zod_1.z.enum([
33
- "pending",
34
- "running",
35
- "stopping",
36
- "stopped",
37
- "failed",
38
- "aborted",
39
- "snapshotting",
40
- ]),
41
- requestedAt: zod_1.z.number(),
42
- startedAt: zod_1.z.number().optional(),
43
- requestedStopAt: zod_1.z.number().optional(),
44
- stoppedAt: zod_1.z.number().optional(),
45
- abortedAt: zod_1.z.number().optional(),
46
- duration: zod_1.z.number().optional(),
47
- sourceSnapshotId: zod_1.z.string().optional(),
48
- snapshottedAt: zod_1.z.number().optional(),
49
- createdAt: zod_1.z.number(),
50
- cwd: zod_1.z.string(),
51
- updatedAt: zod_1.z.number(),
52
- interactivePort: zod_1.z.number().optional(),
53
- networkPolicy: exports.NetworkPolicyValidator.optional(),
54
- activeCpuDurationMs: zod_1.z.number().optional(),
55
- networkTransfer: zod_1.z.object({
56
- ingress: zod_1.z.number(),
57
- egress: zod_1.z.number(),
58
- }).optional(),
59
- });
60
- exports.SandboxRoute = zod_1.z.object({
61
- url: zod_1.z.string(),
62
- subdomain: zod_1.z.string(),
63
- port: zod_1.z.number(),
64
- });
65
- exports.Snapshot = zod_1.z.object({
66
- id: zod_1.z.string(),
67
- sourceSandboxId: zod_1.z.string(),
68
- region: zod_1.z.string(),
69
- status: zod_1.z.enum(["created", "deleted", "failed"]),
70
- sizeBytes: zod_1.z.number(),
71
- expiresAt: zod_1.z.number().optional(),
72
- createdAt: zod_1.z.number(),
73
- updatedAt: zod_1.z.number(),
74
- });
75
- exports.Pagination = zod_1.z.object({
76
- /**
77
- * Amount of items in the current page.
78
- * @example 20
79
- */
80
- count: zod_1.z.number(),
81
- /**
82
- * Timestamp that must be used to request the next page.
83
- * @example 1540095775951
84
- */
85
- next: zod_1.z.number().nullable(),
86
- /**
87
- * Timestamp that must be used to request the previous page.
88
- * @example 1540095775951
89
- */
90
- prev: zod_1.z.number().nullable(),
91
- });
92
- exports.Command = zod_1.z.object({
93
- id: zod_1.z.string(),
94
- name: zod_1.z.string(),
95
- args: zod_1.z.array(zod_1.z.string()),
96
- cwd: zod_1.z.string(),
97
- sandboxId: zod_1.z.string(),
98
- exitCode: zod_1.z.number().nullable(),
99
- startedAt: zod_1.z.number(),
100
- });
101
- const CommandFinished = exports.Command.extend({
102
- exitCode: zod_1.z.number(),
103
- });
104
- exports.SandboxResponse = zod_1.z.object({
105
- sandbox: exports.Sandbox,
106
- });
107
- exports.SandboxAndRoutesResponse = exports.SandboxResponse.extend({
108
- routes: zod_1.z.array(exports.SandboxRoute),
109
- });
110
- exports.CommandResponse = zod_1.z.object({
111
- command: exports.Command,
112
- });
113
- exports.CommandFinishedResponse = zod_1.z.object({
114
- command: CommandFinished,
115
- });
116
- exports.EmptyResponse = zod_1.z.object({});
117
- const LogLineBase = zod_1.z.object({ data: zod_1.z.string() });
118
- exports.LogLineStdout = LogLineBase.extend({
119
- stream: zod_1.z.literal("stdout"),
120
- });
121
- exports.LogLineStderr = LogLineBase.extend({
122
- stream: zod_1.z.literal("stderr"),
123
- });
124
- exports.LogError = zod_1.z.object({
125
- stream: zod_1.z.literal("error"),
126
- data: zod_1.z.object({
127
- code: zod_1.z.string(),
128
- message: zod_1.z.string(),
129
- }),
130
- });
131
- exports.LogLine = zod_1.z.discriminatedUnion("stream", [
132
- exports.LogLineStdout,
133
- exports.LogLineStderr,
134
- exports.LogError,
20
+ const Sandbox = z.object({
21
+ id: z.string(),
22
+ memory: z.number(),
23
+ vcpus: z.number(),
24
+ region: z.string(),
25
+ runtime: z.string(),
26
+ timeout: z.number(),
27
+ status: z.enum([
28
+ "pending",
29
+ "running",
30
+ "stopping",
31
+ "stopped",
32
+ "failed",
33
+ "aborted",
34
+ "snapshotting"
35
+ ]),
36
+ requestedAt: z.number(),
37
+ startedAt: z.number().optional(),
38
+ requestedStopAt: z.number().optional(),
39
+ stoppedAt: z.number().optional(),
40
+ abortedAt: z.number().optional(),
41
+ duration: z.number().optional(),
42
+ sourceSnapshotId: z.string().optional(),
43
+ snapshottedAt: z.number().optional(),
44
+ createdAt: z.number(),
45
+ cwd: z.string(),
46
+ updatedAt: z.number(),
47
+ interactivePort: z.number().optional(),
48
+ networkPolicy: NetworkPolicyValidator.optional(),
49
+ activeCpuDurationMs: z.number().optional(),
50
+ networkTransfer: z.object({
51
+ ingress: z.number(),
52
+ egress: z.number()
53
+ }).optional()
54
+ });
55
+ const SandboxRoute = z.object({
56
+ url: z.string(),
57
+ subdomain: z.string(),
58
+ port: z.number()
59
+ });
60
+ const Snapshot = z.object({
61
+ id: z.string(),
62
+ sourceSandboxId: z.string(),
63
+ region: z.string(),
64
+ status: z.enum([
65
+ "created",
66
+ "deleted",
67
+ "failed"
68
+ ]),
69
+ sizeBytes: z.number(),
70
+ expiresAt: z.number().optional(),
71
+ createdAt: z.number(),
72
+ updatedAt: z.number()
73
+ });
74
+ const Pagination = z.object({
75
+ count: z.number(),
76
+ next: z.number().nullable(),
77
+ prev: z.number().nullable()
78
+ });
79
+ const Command = z.object({
80
+ id: z.string(),
81
+ name: z.string(),
82
+ args: z.array(z.string()),
83
+ cwd: z.string(),
84
+ sandboxId: z.string(),
85
+ exitCode: z.number().nullable(),
86
+ startedAt: z.number()
87
+ });
88
+ const CommandFinished = Command.extend({ exitCode: z.number() });
89
+ const SandboxResponse = z.object({ sandbox: Sandbox });
90
+ const SandboxAndRoutesResponse = SandboxResponse.extend({ routes: z.array(SandboxRoute) });
91
+ const CommandResponse = z.object({ command: Command });
92
+ const CommandFinishedResponse = z.object({ command: CommandFinished });
93
+ const EmptyResponse = z.object({});
94
+ const LogLineBase = z.object({ data: z.string() });
95
+ const LogLineStdout = LogLineBase.extend({ stream: z.literal("stdout") });
96
+ const LogLineStderr = LogLineBase.extend({ stream: z.literal("stderr") });
97
+ const LogError = z.object({
98
+ stream: z.literal("error"),
99
+ data: z.object({
100
+ code: z.string(),
101
+ message: z.string()
102
+ })
103
+ });
104
+ const LogLine = z.discriminatedUnion("stream", [
105
+ LogLineStdout,
106
+ LogLineStderr,
107
+ LogError
135
108
  ]);
136
- exports.SandboxesResponse = zod_1.z.object({
137
- sandboxes: zod_1.z.array(exports.Sandbox),
138
- pagination: exports.Pagination,
139
- });
140
- exports.SnapshotsResponse = zod_1.z.object({
141
- snapshots: zod_1.z.array(exports.Snapshot),
142
- pagination: exports.Pagination,
143
- });
144
- exports.ExtendTimeoutResponse = zod_1.z.object({
145
- sandbox: exports.Sandbox,
146
- });
147
- exports.UpdateNetworkPolicyResponse = zod_1.z.object({
148
- sandbox: exports.Sandbox,
149
- });
150
- exports.CreateSnapshotResponse = zod_1.z.object({
151
- snapshot: exports.Snapshot,
152
- sandbox: exports.Sandbox,
153
- });
154
- exports.SnapshotResponse = zod_1.z.object({
155
- snapshot: exports.Snapshot,
156
- });
109
+ const SandboxesResponse = z.object({
110
+ sandboxes: z.array(Sandbox),
111
+ pagination: Pagination
112
+ });
113
+ const SnapshotsResponse = z.object({
114
+ snapshots: z.array(Snapshot),
115
+ pagination: Pagination
116
+ });
117
+ const ExtendTimeoutResponse = z.object({ sandbox: Sandbox });
118
+ const UpdateNetworkPolicyResponse = z.object({ sandbox: Sandbox });
119
+ const CreateSnapshotResponse = z.object({
120
+ snapshot: Snapshot,
121
+ sandbox: Sandbox
122
+ });
123
+ const SnapshotResponse = z.object({ snapshot: Snapshot });
124
+
125
+ //#endregion
126
+ export { Command, CommandFinishedResponse, CommandResponse, CreateSnapshotResponse, EmptyResponse, ExtendTimeoutResponse, InjectionRuleValidator, LogError, LogLine, LogLineStderr, LogLineStdout, NetworkPolicyValidator, Pagination, Sandbox, SandboxAndRoutesResponse, SandboxResponse, SandboxRoute, SandboxesResponse, Snapshot, SnapshotResponse, SnapshotsResponse, UpdateNetworkPolicyResponse };
157
127
  //# sourceMappingURL=validators.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"validators.js","sourceRoot":"","sources":["../../src/api-client/validators.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AAIX,QAAA,sBAAsB,GAAG,OAAC,CAAC,MAAM,CAAC;IAC7C,MAAM,EAAE,OAAC,CAAC,MAAM,EAAE;IAClB,oCAAoC;IACpC,OAAO,EAAE,OAAC,CAAC,MAAM,CAAC,OAAC,CAAC,MAAM,EAAE,CAAC,CAAC,QAAQ,EAAE;IACxC,wCAAwC;IACxC,WAAW,EAAE,OAAC,CAAC,KAAK,CAAC,OAAC,CAAC,MAAM,EAAE,CAAC,CAAC,QAAQ,EAAE;CAC5C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B,UAAU,EAAE,kBAAU;CACvB,CAAC,CAAC;AAEU,QAAA,iBAAiB,GAAG,OAAC,CAAC,MAAM,CAAC;IACxC,SAAS,EAAE,OAAC,CAAC,KAAK,CAAC,gBAAQ,CAAC;IAC5B,UAAU,EAAE,kBAAU;CACvB,CAAC,CAAC;AAEU,QAAA,qBAAqB,GAAG,OAAC,CAAC,MAAM,CAAC;IAC5C,OAAO,EAAE,eAAO;CACjB,CAAC,CAAC;AAEU,QAAA,2BAA2B,GAAG,OAAC,CAAC,MAAM,CAAC;IAClD,OAAO,EAAE,eAAO;CACjB,CAAC,CAAC;AAEU,QAAA,sBAAsB,GAAG,OAAC,CAAC,MAAM,CAAC;IAC7C,QAAQ,EAAE,gBAAQ;IAClB,OAAO,EAAE,eAAO;CACjB,CAAC,CAAC;AAEU,QAAA,gBAAgB,GAAG,OAAC,CAAC,MAAM,CAAC;IACvC,QAAQ,EAAE,gBAAQ;CACnB,CAAC,CAAC"}
1
+ {"version":3,"file":"validators.js","names":[],"sources":["../../src/api-client/validators.ts"],"sourcesContent":["import { z } from \"zod\";\n\nexport type SandboxMetaData = z.infer<typeof Sandbox>;\n\nexport const InjectionRuleValidator = z.object({\n domain: z.string(),\n // headers are only sent in requests\n headers: z.record(z.string()).optional(),\n // headerNames are returned in responses\n headerNames: z.array(z.string()).optional(),\n});\n\nexport const NetworkPolicyValidator = z.union([\n z.object({ mode: z.literal(\"allow-all\") }).passthrough(),\n z.object({ mode: z.literal(\"deny-all\") }).passthrough(),\n z\n .object({\n mode: z.literal(\"custom\"),\n allowedDomains: z.array(z.string()).optional(),\n allowedCIDRs: z.array(z.string()).optional(),\n deniedCIDRs: z.array(z.string()).optional(),\n injectionRules: z.array(InjectionRuleValidator).optional(),\n })\n .passthrough(),\n]);\n\nexport const Sandbox = z.object({\n id: z.string(),\n memory: z.number(),\n vcpus: z.number(),\n region: z.string(),\n runtime: z.string(),\n timeout: z.number(),\n status: z.enum([\n \"pending\",\n \"running\",\n \"stopping\",\n \"stopped\",\n \"failed\",\n \"aborted\",\n \"snapshotting\",\n ]),\n requestedAt: z.number(),\n startedAt: z.number().optional(),\n requestedStopAt: z.number().optional(),\n stoppedAt: z.number().optional(),\n abortedAt: z.number().optional(),\n duration: z.number().optional(),\n sourceSnapshotId: z.string().optional(),\n snapshottedAt: z.number().optional(),\n createdAt: z.number(),\n cwd: z.string(),\n updatedAt: z.number(),\n interactivePort: z.number().optional(),\n networkPolicy: NetworkPolicyValidator.optional(),\n activeCpuDurationMs: z.number().optional(),\n networkTransfer: z.object({\n ingress: z.number(),\n egress: z.number(),\n }).optional(),\n});\n\nexport type SandboxRouteData = z.infer<typeof SandboxRoute>;\n\nexport const SandboxRoute = z.object({\n url: z.string(),\n subdomain: z.string(),\n port: z.number(),\n});\n\nexport type SnapshotMetadata = z.infer<typeof Snapshot>;\n\nexport const Snapshot = z.object({\n id: z.string(),\n sourceSandboxId: z.string(),\n region: z.string(),\n status: z.enum([\"created\", \"deleted\", \"failed\"]),\n sizeBytes: z.number(),\n expiresAt: z.number().optional(),\n createdAt: z.number(),\n updatedAt: z.number(),\n});\n\nexport const Pagination = z.object({\n /**\n * Amount of items in the current page.\n * @example 20\n */\n count: z.number(),\n /**\n * Timestamp that must be used to request the next page.\n * @example 1540095775951\n */\n next: z.number().nullable(),\n /**\n * Timestamp that must be used to request the previous page.\n * @example 1540095775951\n */\n prev: z.number().nullable(),\n});\n\nexport type CommandData = z.infer<typeof Command>;\n\nexport const Command = z.object({\n id: z.string(),\n name: z.string(),\n args: z.array(z.string()),\n cwd: z.string(),\n sandboxId: z.string(),\n exitCode: z.number().nullable(),\n startedAt: z.number(),\n});\n\nconst CommandFinished = Command.extend({\n exitCode: z.number(),\n});\n\nexport const SandboxResponse = z.object({\n sandbox: Sandbox,\n});\n\nexport const SandboxAndRoutesResponse = SandboxResponse.extend({\n routes: z.array(SandboxRoute),\n});\n\nexport const CommandResponse = z.object({\n command: Command,\n});\n\nexport type CommandFinishedData = z.infer<typeof CommandFinishedResponse>[\"command\"];\n\nexport const CommandFinishedResponse = z.object({\n command: CommandFinished,\n});\n\nexport const EmptyResponse = z.object({});\n\nconst LogLineBase = z.object({ data: z.string() });\nexport const LogLineStdout = LogLineBase.extend({\n stream: z.literal(\"stdout\"),\n});\nexport const LogLineStderr = LogLineBase.extend({\n stream: z.literal(\"stderr\"),\n});\n\nexport const LogError = z.object({\n stream: z.literal(\"error\"),\n data: z.object({\n code: z.string(),\n message: z.string(),\n }),\n});\n\nexport const LogLine = z.discriminatedUnion(\"stream\", [\n LogLineStdout,\n LogLineStderr,\n LogError,\n]);\n\nexport const SandboxesResponse = z.object({\n sandboxes: z.array(Sandbox),\n pagination: Pagination,\n});\n\nexport const SnapshotsResponse = z.object({\n snapshots: z.array(Snapshot),\n pagination: Pagination,\n});\n\nexport const ExtendTimeoutResponse = z.object({\n sandbox: Sandbox,\n});\n\nexport const UpdateNetworkPolicyResponse = z.object({\n sandbox: Sandbox,\n});\n\nexport const CreateSnapshotResponse = z.object({\n snapshot: Snapshot,\n sandbox: Sandbox,\n});\n\nexport const SnapshotResponse = z.object({\n snapshot: Snapshot,\n});\n"],"mappings":";;;AAIA,MAAa,yBAAyB,EAAE,OAAO;CAC7C,QAAQ,EAAE,QAAQ;CAElB,SAAS,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC,UAAU;CAExC,aAAa,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,UAAU;CAC5C,CAAC;AAEF,MAAa,yBAAyB,EAAE,MAAM;CAC5C,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,YAAY,EAAE,CAAC,CAAC,aAAa;CACxD,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,WAAW,EAAE,CAAC,CAAC,aAAa;CACvD,EACG,OAAO;EACN,MAAM,EAAE,QAAQ,SAAS;EACzB,gBAAgB,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,UAAU;EAC9C,cAAc,EAAE,MAAM,EAAE,QAAQ,CAAC,C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EAAE,OAAO;EACb,MAAM,EAAE,QAAQ;EAChB,SAAS,EAAE,QAAQ;EACpB,CAAC;CACH,CAAC;AAEF,MAAa,UAAU,EAAE,mBAAmB,UAAU;CACpD;CACA;CACA;CACD,CAAC;AAEF,MAAa,oBAAoB,EAAE,OAAO;CACxC,WAAW,EAAE,MAAM,QAAQ;CAC3B,YAAY;CACb,CAAC;AAEF,MAAa,oBAAoB,EAAE,OAAO;CACxC,WAAW,EAAE,MAAM,SAAS;CAC5B,YAAY;CACb,CAAC;AAEF,MAAa,wBAAwB,EAAE,OAAO,EAC5C,SAAS,SACV,CAAC;AAEF,MAAa,8BAA8B,EAAE,OAAO,EAClD,SAAS,SACV,CAAC;AAEF,MAAa,yBAAyB,EAAE,OAAO;CAC7C,UAAU;CACV,SAAS;CACV,CAAC;AAEF,MAAa,mBAAmB,EAAE,OAAO,EACvC,UAAU,UACX,CAAC"}
@@ -0,0 +1,89 @@
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
+ const require_api_error = require('./api-error.cjs');
3
+ let node_timers_promises = require("node:timers/promises");
4
+ let async_retry = require("async-retry");
5
+ async_retry = require_rolldown_runtime.__toESM(async_retry);
6
+
7
+ //#region src/api-client/with-retry.ts
8
+ /**
9
+ * Wraps a fetch function with retry logic. The retry logic will retry
10
+ * on network errors, 429 responses and 5xx responses. The retry logic
11
+ * will not retry on 4xx responses.
12
+ *
13
+ * @param rawFetch The fetch function to wrap.
14
+ * @returns The wrapped fetch function.
15
+ */
16
+ function withRetry(rawFetch) {
17
+ return async (url, opts = {}) => {
18
+ /**
19
+ * Timeouts by default will be [10, 60, 360, 2160, 12960]
20
+ * before randomization is added.
21
+ */
22
+ const retryOpts = Object.assign({
23
+ minTimeout: 10,
24
+ retries: 5,
25
+ factor: 6,
26
+ maxRetryAfter: 20
27
+ }, opts.retry);
28
+ if (opts.onRetry) retryOpts.onRetry = (error, attempts) => {
29
+ opts.onRetry(error, opts);
30
+ if (opts.retry && opts.retry.onRetry) opts.retry.onRetry(error, attempts);
31
+ };
32
+ try {
33
+ return await (0, async_retry.default)(async (bail) => {
34
+ try {
35
+ if (opts.signal?.aborted) return bail(opts.signal.reason || /* @__PURE__ */ new Error("Request aborted"));
36
+ const response = await rawFetch(url, opts);
37
+ /**
38
+ * When the response is 429 we will try to parse the Retry-After
39
+ * header. If the header exists we will try to parse it and, if
40
+ * the wait time is higher than the maximum defined, we respond.
41
+ * Otherwise we wait for the time given in the header and throw
42
+ * to retry.
43
+ */
44
+ if (response.status === 429) {
45
+ const retryAfter = parseInt(response.headers.get("retry-after") || "", 10);
46
+ if (retryAfter && !isNaN(retryAfter)) {
47
+ if (retryAfter > retryOpts.maxRetryAfter) return response;
48
+ await (0, node_timers_promises.setTimeout)(retryAfter * 1e3);
49
+ }
50
+ throw new require_api_error.APIError(response);
51
+ }
52
+ /**
53
+ * If the response is a a retryable error, we throw in
54
+ * order to retry.
55
+ */
56
+ if (response.status >= 500 && response.status < 600) throw new require_api_error.APIError(response);
57
+ return response;
58
+ } catch (error) {
59
+ /**
60
+ * If the request was aborted using the AbortController
61
+ * we bail from retrying throwing the original error.
62
+ */
63
+ if (isAbortError(error)) return bail(error);
64
+ /**
65
+ * If the signal was aborted meanwhile we were
66
+ * waiting, we bail from retrying.
67
+ */
68
+ if (opts.signal?.aborted) return bail(opts.signal.reason || /* @__PURE__ */ new Error("Request aborted"));
69
+ throw error;
70
+ }
71
+ }, retryOpts);
72
+ } catch (error) {
73
+ /**
74
+ * The ResponseError is only intended for retries so in case we
75
+ * ran out of attempts we will respond with the last response
76
+ * we obtained.
77
+ */
78
+ if (error instanceof require_api_error.APIError) return error.response;
79
+ throw error;
80
+ }
81
+ };
82
+ }
83
+ function isAbortError(error) {
84
+ return error !== void 0 && error !== null && error.name === "AbortError";
85
+ }
86
+
87
+ //#endregion
88
+ exports.withRetry = withRetry;
89
+ //# sourceMappingURL=with-retry.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"with-retry.cjs","names":["APIError"],"sources":["../../src/api-client/with-retry.ts"],"sourcesContent":["import type { Options as RetryOptions } from \"async-retry\";\nimport { APIError } from \"./api-error.js\";\nimport { setTimeout } from \"node:timers/promises\";\nimport retry from \"async-retry\";\n\nexport interface RequestOptions {\n onRetry?(error: any, options: RequestOptions): void;\n retry?: Partial<RetryOptions>;\n}\n\n/**\n * Wraps a fetch function with retry logic. The retry logic will retry\n * on network errors, 429 responses and 5xx responses. The retry logic\n * will not retry on 4xx responses.\n *\n * @param rawFetch The fetch function to wrap.\n * @returns The wrapped fetch function.\n */\nexport function withRetry<T extends RequestInit>(\n rawFetch: (url: URL | string, init?: T) => Promise<Response>,\n) {\n return async (\n url: URL | string,\n opts: T & RequestOptions = <T & RequestOptions>{},\n ) => {\n /**\n * Timeouts by default will be [10, 60, 360, 2160, 12960]\n * before randomization is added.\n */\n const retryOpts = Object.assign(\n {\n minTimeout: 10,\n retries: 5,\n factor: 6,\n maxRetryAfter: 20,\n },\n opts.retry,\n );\n\n if (opts.onRetry) {\n retryOpts.onRetry = (error, attempts) => {\n opts.onRetry!(error, opts);\n if (opts.retry && opts.retry.onRetry) {\n opts.retry.onRetry(error, attempts);\n }\n };\n }\n\n try {\n return (await retry(async (bail) => {\n try {\n if (opts.signal?.aborted) {\n return bail(opts.signal.reason || new Error(\"Request aborted\"));\n }\n const response = await rawFetch(url, opts);\n\n /**\n * When the response is 429 we will try to parse the Retry-After\n * header. If the header exists we will try to parse it and, if\n * the wait time is higher than the maximum defined, we respond.\n * Otherwise we wait for the time given in the header and throw\n * to retry.\n */\n if (response.status === 429) {\n const retryAfter = parseInt(\n response.headers.get(\"retry-after\") || \"\",\n 10,\n );\n\n if (retryAfter && !isNaN(retryAfter)) {\n if (retryAfter > retryOpts.maxRetryAfter) {\n return response;\n }\n\n await setTimeout(retryAfter * 1e3);\n }\n\n throw new APIError(response);\n }\n\n /**\n * If the response is a a retryable error, we throw in\n * order to retry.\n */\n if (response.status >= 500 && response.status < 600) {\n throw new APIError(response);\n }\n\n return response;\n } catch (error) {\n /**\n * If the request was aborted using the AbortController\n * we bail from retrying throwing the original error.\n */\n if (isAbortError(error)) {\n return bail(error);\n }\n\n /**\n * If the signal was aborted meanwhile we were\n * waiting, we bail from retrying.\n */\n if (opts.signal?.aborted) {\n return bail(opts.signal.reason || new Error(\"Request aborted\"));\n }\n\n throw error;\n }\n }, retryOpts)) as Response;\n } catch (error) {\n /**\n * The ResponseError is only intended for retries so in case we\n * ran out of attempts we will respond with the last response\n * we obtained.\n */\n if (error instanceof APIError) {\n return error.response;\n }\n\n throw error;\n }\n };\n}\n\nfunction isAbortError(error: unknown): error is Error {\n return (\n error !== undefined &&\n error !== null &&\n (error as Error).name === \"AbortError\"\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;AAkBA,SAAgB,UACd,UACA;AACA,QAAO,OACL,KACA,OAA+C,EAAE,KAC9C;;;;;EAKH,MAAM,YAAY,OAAO,OACvB;GACE,YAAY;GACZ,SAAS;GACT,QAAQ;GACR,eAAe;GAChB,EACD,KAAK,MACN;AAED,MAAI,KAAK,QACP,WAAU,WAAW,OAAO,aAAa;AACvC,QAAK,QAAS,OAAO,KAAK;AAC1B,OAAI,KAAK,SAAS,KAAK,MAAM,QAC3B,MAAK,MAAM,QAAQ,OAAO,SAAS;;AAKzC,MAAI;AACF,UAAQ,+BAAY,OAAO,SAAS;AAClC,QAAI;AACF,SAAI,KAAK,QAAQ,QACf,QAAO,KAAK,KAAK,OAAO,0BAAU,IAAI,MAAM,kBAAkB,CAAC;KAEjE,MAAM,WAAW,MAAM,SAAS,KAAK,KAAK;;;;;;;;AAS1C,SAAI,SAAS,WAAW,KAAK;MAC3B,MAAM,aAAa,SACjB,SAAS,QAAQ,IAAI,cAAc,IAAI,IACvC,GACD;AAED,UAAI,cAAc,CAAC,MAAM,WAAW,EAAE;AACpC,WAAI,aAAa,UAAU,cACzB,QAAO;AAGT,kDAAiB,aAAa,IAAI;;AAGpC,YAAM,IAAIA,2BAAS,SAAS;;;;;;AAO9B,SAAI,SAAS,UAAU,OAAO,SAAS,SAAS,IAC9C,OAAM,IAAIA,2BAAS,SAAS;AAG9B,YAAO;aACA,OAAO;;;;;AAKd,SAAI,aAAa,MAAM,CACrB,QAAO,KAAK,MAAM;;;;;AAOpB,SAAI,KAAK,QAAQ,QACf,QAAO,KAAK,KAAK,OAAO,0BAAU,IAAI,MAAM,kBAAkB,CAAC;AAGjE,WAAM;;MAEP,UAAU;WACN,OAAO;;;;;;AAMd,OAAI,iBAAiBA,2BACnB,QAAO,MAAM;AAGf,SAAM;;;;AAKZ,SAAS,aAAa,OAAgC;AACpD,QACE,UAAU,UACV,UAAU,QACT,MAAgB,SAAS"}
@@ -0,0 +1,10 @@
1
+ import { Options } from "async-retry";
2
+
3
+ //#region src/api-client/with-retry.d.ts
4
+ interface RequestOptions {
5
+ onRetry?(error: any, options: RequestOptions): void;
6
+ retry?: Partial<Options>;
7
+ }
8
+ //#endregion
9
+ export { RequestOptions };
10
+ //# sourceMappingURL=with-retry.d.cts.map
@@ -1,14 +1,10 @@
1
- import type { Options as RetryOptions } from "async-retry";
2
- export interface RequestOptions {
3
- onRetry?(error: any, options: RequestOptions): void;
4
- retry?: Partial<RetryOptions>;
1
+ import { Options } from "async-retry";
2
+
3
+ //#region src/api-client/with-retry.d.ts
4
+ interface RequestOptions {
5
+ onRetry?(error: any, options: RequestOptions): void;
6
+ retry?: Partial<Options>;
5
7
  }
6
- /**
7
- * Wraps a fetch function with retry logic. The retry logic will retry
8
- * on network errors, 429 responses and 5xx responses. The retry logic
9
- * will not retry on 4xx responses.
10
- *
11
- * @param rawFetch The fetch function to wrap.
12
- * @returns The wrapped fetch function.
13
- */
14
- export declare function withRetry<T extends RequestInit>(rawFetch: (url: URL | string, init?: T) => Promise<Response>): (url: URL | string, opts?: T & RequestOptions) => Promise<Response>;
8
+ //#endregion
9
+ export { RequestOptions };
10
+ //# sourceMappingURL=with-retry.d.ts.map