@cloudflare/sandbox 0.2.1 → 0.2.2
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/CHANGELOG.md +6 -0
- package/README.md +1 -1
- package/container_src/circuit-breaker.ts +121 -0
- package/container_src/index.ts +228 -103
- package/container_src/jupyter-server.ts +289 -46
- package/container_src/jupyter-service.ts +448 -0
- package/container_src/startup.sh +59 -28
- package/dist/chunk-LALY4SFU.js +129 -0
- package/dist/chunk-LALY4SFU.js.map +1 -0
- package/dist/{chunk-SYMWNYWA.js → chunk-VTKZL632.js} +116 -64
- package/dist/chunk-VTKZL632.js.map +1 -0
- package/dist/{chunk-IATLC32Y.js → chunk-ZMPO44U4.js} +8 -8
- package/dist/{client-C7rKCYBD.d.ts → client-bzEV222a.d.ts} +10 -0
- package/dist/client.d.ts +1 -1
- package/dist/errors.d.ts +95 -0
- package/dist/errors.js +27 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +27 -3
- package/dist/interpreter.d.ts +1 -1
- package/dist/jupyter-client.d.ts +1 -1
- package/dist/jupyter-client.js +2 -1
- package/dist/request-handler.d.ts +1 -1
- package/dist/request-handler.js +4 -3
- package/dist/sandbox.d.ts +1 -1
- package/dist/sandbox.js +4 -3
- package/package.json +1 -1
- package/src/errors.ts +218 -0
- package/src/index.ts +33 -8
- package/src/jupyter-client.ts +225 -142
- package/dist/chunk-SYMWNYWA.js.map +0 -1
- /package/dist/{chunk-IATLC32Y.js.map → chunk-ZMPO44U4.js.map} +0 -0
package/src/errors.ts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standard error response from the sandbox API
|
|
3
|
+
*/
|
|
4
|
+
export interface SandboxErrorResponse {
|
|
5
|
+
error?: string;
|
|
6
|
+
status?: string;
|
|
7
|
+
progress?: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Base error class for all Sandbox-related errors
|
|
12
|
+
*/
|
|
13
|
+
export class SandboxError extends Error {
|
|
14
|
+
constructor(message: string) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = this.constructor.name;
|
|
17
|
+
|
|
18
|
+
// Maintains proper stack trace for where our error was thrown (only available on V8)
|
|
19
|
+
if (Error.captureStackTrace) {
|
|
20
|
+
Error.captureStackTrace(this, this.constructor);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Error thrown when Jupyter functionality is requested but the service is still initializing.
|
|
27
|
+
*
|
|
28
|
+
* Note: With the current implementation, requests wait for Jupyter to be ready.
|
|
29
|
+
* This error is only thrown when:
|
|
30
|
+
* 1. The request times out waiting for Jupyter (default: 30 seconds)
|
|
31
|
+
* 2. Jupyter initialization actually fails
|
|
32
|
+
*
|
|
33
|
+
* Most requests will succeed after a delay, not throw this error.
|
|
34
|
+
*/
|
|
35
|
+
export class JupyterNotReadyError extends SandboxError {
|
|
36
|
+
public readonly code = "JUPYTER_NOT_READY";
|
|
37
|
+
public readonly retryAfter: number;
|
|
38
|
+
public readonly progress?: number;
|
|
39
|
+
|
|
40
|
+
constructor(
|
|
41
|
+
message?: string,
|
|
42
|
+
options?: { retryAfter?: number; progress?: number }
|
|
43
|
+
) {
|
|
44
|
+
super(
|
|
45
|
+
message || "Jupyter is still initializing. Please retry in a few seconds."
|
|
46
|
+
);
|
|
47
|
+
this.retryAfter = options?.retryAfter || 5;
|
|
48
|
+
this.progress = options?.progress;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Error thrown when a context is not found
|
|
54
|
+
*/
|
|
55
|
+
export class ContextNotFoundError extends SandboxError {
|
|
56
|
+
public readonly code = "CONTEXT_NOT_FOUND";
|
|
57
|
+
public readonly contextId: string;
|
|
58
|
+
|
|
59
|
+
constructor(contextId: string) {
|
|
60
|
+
super(`Context ${contextId} not found`);
|
|
61
|
+
this.contextId = contextId;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Error thrown when code execution fails
|
|
67
|
+
*/
|
|
68
|
+
export class CodeExecutionError extends SandboxError {
|
|
69
|
+
public readonly code = "CODE_EXECUTION_ERROR";
|
|
70
|
+
public readonly executionError?: {
|
|
71
|
+
ename?: string;
|
|
72
|
+
evalue?: string;
|
|
73
|
+
traceback?: string[];
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
constructor(message: string, executionError?: any) {
|
|
77
|
+
super(message);
|
|
78
|
+
this.executionError = executionError;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Error thrown when the sandbox container is not ready
|
|
84
|
+
*/
|
|
85
|
+
export class ContainerNotReadyError extends SandboxError {
|
|
86
|
+
public readonly code = "CONTAINER_NOT_READY";
|
|
87
|
+
|
|
88
|
+
constructor(message?: string) {
|
|
89
|
+
super(
|
|
90
|
+
message ||
|
|
91
|
+
"Container is not ready. Please wait for initialization to complete."
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Error thrown when a network request to the sandbox fails
|
|
98
|
+
*/
|
|
99
|
+
export class SandboxNetworkError extends SandboxError {
|
|
100
|
+
public readonly code = "NETWORK_ERROR";
|
|
101
|
+
public readonly statusCode?: number;
|
|
102
|
+
public readonly statusText?: string;
|
|
103
|
+
|
|
104
|
+
constructor(message: string, statusCode?: number, statusText?: string) {
|
|
105
|
+
super(message);
|
|
106
|
+
this.statusCode = statusCode;
|
|
107
|
+
this.statusText = statusText;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Error thrown when service is temporarily unavailable (e.g., circuit breaker open)
|
|
113
|
+
*/
|
|
114
|
+
export class ServiceUnavailableError extends SandboxError {
|
|
115
|
+
public readonly code = "SERVICE_UNAVAILABLE";
|
|
116
|
+
public readonly retryAfter?: number;
|
|
117
|
+
|
|
118
|
+
constructor(message?: string, retryAfter?: number) {
|
|
119
|
+
// Simple, user-friendly message without implementation details
|
|
120
|
+
super(message || "Service temporarily unavailable");
|
|
121
|
+
this.retryAfter = retryAfter;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Type guard to check if an error is a JupyterNotReadyError
|
|
127
|
+
*/
|
|
128
|
+
export function isJupyterNotReadyError(
|
|
129
|
+
error: unknown
|
|
130
|
+
): error is JupyterNotReadyError {
|
|
131
|
+
return error instanceof JupyterNotReadyError;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Type guard to check if an error is any SandboxError
|
|
136
|
+
*/
|
|
137
|
+
export function isSandboxError(error: unknown): error is SandboxError {
|
|
138
|
+
return error instanceof SandboxError;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Helper to determine if an error is retryable
|
|
143
|
+
*/
|
|
144
|
+
export function isRetryableError(error: unknown): boolean {
|
|
145
|
+
if (
|
|
146
|
+
error instanceof JupyterNotReadyError ||
|
|
147
|
+
error instanceof ContainerNotReadyError ||
|
|
148
|
+
error instanceof ServiceUnavailableError
|
|
149
|
+
) {
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (error instanceof SandboxNetworkError) {
|
|
154
|
+
// Retry on 502, 503, 504 (gateway/service unavailable errors)
|
|
155
|
+
return error.statusCode
|
|
156
|
+
? [502, 503, 504].includes(error.statusCode)
|
|
157
|
+
: false;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Parse error response from the sandbox API and return appropriate error instance
|
|
165
|
+
*/
|
|
166
|
+
export async function parseErrorResponse(
|
|
167
|
+
response: Response
|
|
168
|
+
): Promise<SandboxError> {
|
|
169
|
+
let data: SandboxErrorResponse;
|
|
170
|
+
|
|
171
|
+
try {
|
|
172
|
+
data = (await response.json()) as SandboxErrorResponse;
|
|
173
|
+
} catch {
|
|
174
|
+
// If JSON parsing fails, return a generic network error
|
|
175
|
+
return new SandboxNetworkError(
|
|
176
|
+
`Request failed with status ${response.status}`,
|
|
177
|
+
response.status,
|
|
178
|
+
response.statusText
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Check for specific error types based on response
|
|
183
|
+
if (response.status === 503) {
|
|
184
|
+
// Circuit breaker error
|
|
185
|
+
if (data.status === "circuit_open") {
|
|
186
|
+
return new ServiceUnavailableError(
|
|
187
|
+
"Service temporarily unavailable",
|
|
188
|
+
parseInt(response.headers.get("Retry-After") || "30")
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Jupyter initialization error
|
|
193
|
+
if (data.status === "initializing") {
|
|
194
|
+
return new JupyterNotReadyError(data.error, {
|
|
195
|
+
retryAfter: parseInt(response.headers.get("Retry-After") || "5"),
|
|
196
|
+
progress: data.progress,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Check for context not found
|
|
202
|
+
if (
|
|
203
|
+
response.status === 404 &&
|
|
204
|
+
data.error?.includes("Context") &&
|
|
205
|
+
data.error?.includes("not found")
|
|
206
|
+
) {
|
|
207
|
+
const contextId =
|
|
208
|
+
data.error.match(/Context (\S+) not found/)?.[1] || "unknown";
|
|
209
|
+
return new ContextNotFoundError(contextId);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Default network error
|
|
213
|
+
return new SandboxNetworkError(
|
|
214
|
+
data.error || `Request failed with status ${response.status}`,
|
|
215
|
+
response.status,
|
|
216
|
+
response.statusText
|
|
217
|
+
);
|
|
218
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,29 +1,54 @@
|
|
|
1
1
|
// Export types from client
|
|
2
2
|
export type {
|
|
3
|
-
DeleteFileResponse,
|
|
3
|
+
DeleteFileResponse,
|
|
4
|
+
ExecuteResponse,
|
|
4
5
|
GitCheckoutResponse,
|
|
5
|
-
MkdirResponse,
|
|
6
|
-
|
|
6
|
+
MkdirResponse,
|
|
7
|
+
MoveFileResponse,
|
|
8
|
+
ReadFileResponse,
|
|
9
|
+
RenameFileResponse,
|
|
10
|
+
WriteFileResponse,
|
|
7
11
|
} from "./client";
|
|
12
|
+
// Export errors
|
|
13
|
+
export {
|
|
14
|
+
CodeExecutionError,
|
|
15
|
+
ContainerNotReadyError,
|
|
16
|
+
ContextNotFoundError,
|
|
17
|
+
isJupyterNotReadyError,
|
|
18
|
+
isRetryableError,
|
|
19
|
+
isSandboxError,
|
|
20
|
+
JupyterNotReadyError,
|
|
21
|
+
parseErrorResponse,
|
|
22
|
+
SandboxError,
|
|
23
|
+
type SandboxErrorResponse,
|
|
24
|
+
SandboxNetworkError,
|
|
25
|
+
ServiceUnavailableError,
|
|
26
|
+
} from "./errors";
|
|
8
27
|
// Export code interpreter types
|
|
9
28
|
export type {
|
|
10
|
-
ChartData,
|
|
29
|
+
ChartData,
|
|
11
30
|
CodeContext,
|
|
12
31
|
CreateContextOptions,
|
|
13
32
|
Execution,
|
|
14
33
|
ExecutionError,
|
|
15
34
|
OutputMessage,
|
|
16
35
|
Result,
|
|
17
|
-
RunCodeOptions
|
|
36
|
+
RunCodeOptions,
|
|
18
37
|
} from "./interpreter-types";
|
|
19
38
|
// Export the implementations
|
|
20
39
|
export { ResultImpl } from "./interpreter-types";
|
|
21
40
|
// Re-export request handler utilities
|
|
22
41
|
export {
|
|
23
|
-
proxyToSandbox,
|
|
24
|
-
|
|
42
|
+
proxyToSandbox,
|
|
43
|
+
type RouteInfo,
|
|
44
|
+
type SandboxEnv,
|
|
45
|
+
} from "./request-handler";
|
|
25
46
|
export { getSandbox, Sandbox } from "./sandbox";
|
|
26
47
|
// Export SSE parser for converting ReadableStream to AsyncIterable
|
|
27
|
-
export {
|
|
48
|
+
export {
|
|
49
|
+
asyncIterableToSSEStream,
|
|
50
|
+
parseSSEStream,
|
|
51
|
+
responseToAsyncIterable,
|
|
52
|
+
} from "./sse-parser";
|
|
28
53
|
// Export event types for streaming
|
|
29
54
|
export type { ExecEvent, LogEvent } from "./types";
|