@cloudflare/sandbox 0.3.7 → 0.4.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/.turbo/turbo-build.log +44 -0
- package/CHANGELOG.md +8 -10
- package/Dockerfile +82 -18
- package/README.md +89 -824
- package/dist/chunk-53JFOF7F.js +2352 -0
- package/dist/chunk-53JFOF7F.js.map +1 -0
- package/dist/chunk-BFVUNTP4.js +104 -0
- package/dist/chunk-BFVUNTP4.js.map +1 -0
- package/dist/{chunk-NNGBXDMY.js → chunk-EKSWCBCA.js} +3 -6
- package/dist/chunk-EKSWCBCA.js.map +1 -0
- package/dist/chunk-JXZMAU2C.js +559 -0
- package/dist/chunk-JXZMAU2C.js.map +1 -0
- package/dist/{chunk-6UAWTJ5S.js → chunk-Z532A7QC.js} +13 -20
- package/dist/{chunk-6UAWTJ5S.js.map → chunk-Z532A7QC.js.map} +1 -1
- package/dist/file-stream.d.ts +16 -38
- package/dist/file-stream.js +1 -2
- package/dist/index.d.ts +6 -5
- package/dist/index.js +45 -38
- package/dist/interpreter.d.ts +3 -3
- package/dist/interpreter.js +2 -2
- package/dist/request-handler.d.ts +4 -3
- package/dist/request-handler.js +4 -7
- package/dist/sandbox-D9K2ypln.d.ts +583 -0
- package/dist/sandbox.d.ts +3 -3
- package/dist/sandbox.js +4 -7
- package/dist/security.d.ts +4 -3
- package/dist/security.js +3 -3
- package/dist/sse-parser.js +1 -1
- package/package.json +12 -4
- package/src/clients/base-client.ts +280 -0
- package/src/clients/command-client.ts +115 -0
- package/src/clients/file-client.ts +269 -0
- package/src/clients/git-client.ts +92 -0
- package/src/clients/index.ts +63 -0
- package/src/{interpreter-client.ts → clients/interpreter-client.ts} +148 -171
- package/src/clients/port-client.ts +105 -0
- package/src/clients/process-client.ts +177 -0
- package/src/clients/sandbox-client.ts +41 -0
- package/src/clients/types.ts +84 -0
- package/src/clients/utility-client.ts +94 -0
- package/src/errors/adapter.ts +180 -0
- package/src/errors/classes.ts +469 -0
- package/src/errors/index.ts +105 -0
- package/src/file-stream.ts +119 -117
- package/src/index.ts +81 -69
- package/src/interpreter.ts +17 -8
- package/src/request-handler.ts +69 -43
- package/src/sandbox.ts +694 -533
- package/src/security.ts +14 -23
- package/src/sse-parser.ts +4 -8
- package/startup.sh +3 -0
- package/tests/base-client.test.ts +328 -0
- package/tests/command-client.test.ts +407 -0
- package/tests/file-client.test.ts +643 -0
- package/tests/file-stream.test.ts +306 -0
- package/tests/git-client.test.ts +328 -0
- package/tests/port-client.test.ts +301 -0
- package/tests/process-client.test.ts +658 -0
- package/tests/sandbox.test.ts +465 -0
- package/tests/sse-parser.test.ts +290 -0
- package/tests/utility-client.test.ts +266 -0
- package/tests/wrangler.jsonc +35 -0
- package/tsconfig.json +9 -1
- package/vitest.config.ts +31 -0
- package/container_src/bun.lock +0 -76
- package/container_src/circuit-breaker.ts +0 -121
- package/container_src/control-process.ts +0 -784
- package/container_src/handler/exec.ts +0 -185
- package/container_src/handler/file.ts +0 -457
- package/container_src/handler/git.ts +0 -130
- package/container_src/handler/ports.ts +0 -314
- package/container_src/handler/process.ts +0 -568
- package/container_src/handler/session.ts +0 -92
- package/container_src/index.ts +0 -601
- package/container_src/interpreter-service.ts +0 -276
- package/container_src/isolation.ts +0 -1213
- package/container_src/mime-processor.ts +0 -255
- package/container_src/package.json +0 -18
- package/container_src/runtime/executors/javascript/node_executor.ts +0 -123
- package/container_src/runtime/executors/python/ipython_executor.py +0 -338
- package/container_src/runtime/executors/typescript/ts_executor.ts +0 -138
- package/container_src/runtime/process-pool.ts +0 -464
- package/container_src/shell-escape.ts +0 -42
- package/container_src/startup.sh +0 -11
- package/container_src/types.ts +0 -131
- package/dist/chunk-32UDXUPC.js +0 -671
- package/dist/chunk-32UDXUPC.js.map +0 -1
- package/dist/chunk-5DILEXGY.js +0 -85
- package/dist/chunk-5DILEXGY.js.map +0 -1
- package/dist/chunk-D3U63BZP.js +0 -240
- package/dist/chunk-D3U63BZP.js.map +0 -1
- package/dist/chunk-FXYPFGOZ.js +0 -129
- package/dist/chunk-FXYPFGOZ.js.map +0 -1
- package/dist/chunk-JTKON2SH.js +0 -113
- package/dist/chunk-JTKON2SH.js.map +0 -1
- package/dist/chunk-NNGBXDMY.js.map +0 -1
- package/dist/chunk-SQLJNZ3K.js +0 -674
- package/dist/chunk-SQLJNZ3K.js.map +0 -1
- package/dist/chunk-W7TVRPBG.js +0 -108
- package/dist/chunk-W7TVRPBG.js.map +0 -1
- package/dist/client-B3RUab0s.d.ts +0 -225
- package/dist/client.d.ts +0 -4
- package/dist/client.js +0 -7
- package/dist/client.js.map +0 -1
- package/dist/errors.d.ts +0 -95
- package/dist/errors.js +0 -27
- package/dist/errors.js.map +0 -1
- package/dist/interpreter-client.d.ts +0 -4
- package/dist/interpreter-client.js +0 -9
- package/dist/interpreter-client.js.map +0 -1
- package/dist/interpreter-types.d.ts +0 -259
- package/dist/interpreter-types.js +0 -9
- package/dist/interpreter-types.js.map +0 -1
- package/dist/types.d.ts +0 -453
- package/dist/types.js +0 -45
- package/dist/types.js.map +0 -1
- package/src/client.ts +0 -1048
- package/src/errors.ts +0 -219
- package/src/interpreter-types.ts +0 -390
- package/src/types.ts +0 -571
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { asyncIterableToSSEStream, parseSSEStream, responseToAsyncIterable } from '../src/sse-parser';
|
|
3
|
+
|
|
4
|
+
function createMockSSEStream(events: string[]): ReadableStream<Uint8Array> {
|
|
5
|
+
return new ReadableStream({
|
|
6
|
+
start(controller) {
|
|
7
|
+
const encoder = new TextEncoder();
|
|
8
|
+
for (const event of events) {
|
|
9
|
+
controller.enqueue(encoder.encode(event));
|
|
10
|
+
}
|
|
11
|
+
controller.close();
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
describe('SSE Parser', () => {
|
|
17
|
+
let consoleErrorSpy: ReturnType<typeof vi.spyOn>;
|
|
18
|
+
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
consoleErrorSpy.mockRestore();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('parseSSEStream', () => {
|
|
28
|
+
|
|
29
|
+
it('should parse valid SSE events', async () => {
|
|
30
|
+
const stream = createMockSSEStream([
|
|
31
|
+
'data: {"type":"start","command":"echo test"}\n\n',
|
|
32
|
+
'data: {"type":"stdout","data":"test\\n"}\n\n',
|
|
33
|
+
'data: {"type":"complete","exitCode":0}\n\n'
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
const events: any[] = [];
|
|
37
|
+
for await (const event of parseSSEStream(stream)) {
|
|
38
|
+
events.push(event);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
expect(events).toHaveLength(3);
|
|
42
|
+
expect(events[0]).toEqual({ type: 'start', command: 'echo test' });
|
|
43
|
+
expect(events[1]).toEqual({ type: 'stdout', data: 'test\n' });
|
|
44
|
+
expect(events[2]).toEqual({ type: 'complete', exitCode: 0 });
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should handle empty data lines', async () => {
|
|
48
|
+
const stream = createMockSSEStream([
|
|
49
|
+
'data: \n\n',
|
|
50
|
+
'data: {"type":"stdout","data":"valid"}\n\n'
|
|
51
|
+
]);
|
|
52
|
+
|
|
53
|
+
const events: any[] = [];
|
|
54
|
+
for await (const event of parseSSEStream(stream)) {
|
|
55
|
+
events.push(event);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
expect(events).toHaveLength(1);
|
|
59
|
+
expect(events[0]).toEqual({ type: 'stdout', data: 'valid' });
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should skip [DONE] markers', async () => {
|
|
63
|
+
const stream = createMockSSEStream([
|
|
64
|
+
'data: {"type":"start"}\n\n',
|
|
65
|
+
'data: [DONE]\n\n',
|
|
66
|
+
'data: {"type":"complete"}\n\n'
|
|
67
|
+
]);
|
|
68
|
+
|
|
69
|
+
const events: any[] = [];
|
|
70
|
+
for await (const event of parseSSEStream(stream)) {
|
|
71
|
+
events.push(event);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
expect(events).toHaveLength(2);
|
|
75
|
+
expect(events[0]).toEqual({ type: 'start' });
|
|
76
|
+
expect(events[1]).toEqual({ type: 'complete' });
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('should handle malformed JSON gracefully', async () => {
|
|
80
|
+
const stream = createMockSSEStream([
|
|
81
|
+
'data: invalid json\n\n',
|
|
82
|
+
'data: {"type":"stdout","data":"valid"}\n\n',
|
|
83
|
+
'data: {incomplete\n\n'
|
|
84
|
+
]);
|
|
85
|
+
|
|
86
|
+
const events: any[] = [];
|
|
87
|
+
for await (const event of parseSSEStream(stream)) {
|
|
88
|
+
events.push(event);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
expect(events).toHaveLength(1);
|
|
92
|
+
expect(events[0]).toEqual({ type: 'stdout', data: 'valid' });
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should handle empty lines and comments', async () => {
|
|
96
|
+
const stream = createMockSSEStream([
|
|
97
|
+
'\n',
|
|
98
|
+
' \n',
|
|
99
|
+
': this is a comment\n',
|
|
100
|
+
'data: {"type":"test"}\n\n',
|
|
101
|
+
'\n'
|
|
102
|
+
]);
|
|
103
|
+
|
|
104
|
+
const events: any[] = [];
|
|
105
|
+
for await (const event of parseSSEStream(stream)) {
|
|
106
|
+
events.push(event);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
expect(events).toHaveLength(1);
|
|
110
|
+
expect(events[0]).toEqual({ type: 'test' });
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('should handle chunked data properly', async () => {
|
|
114
|
+
// Simulate chunked delivery where data arrives in parts
|
|
115
|
+
const stream = new ReadableStream({
|
|
116
|
+
start(controller) {
|
|
117
|
+
const encoder = new TextEncoder();
|
|
118
|
+
// Send partial data
|
|
119
|
+
controller.enqueue(encoder.encode('data: {"typ'));
|
|
120
|
+
controller.enqueue(encoder.encode('e":"start"}\n\n'));
|
|
121
|
+
controller.enqueue(encoder.encode('data: {"type":"end"}\n\n'));
|
|
122
|
+
controller.close();
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const events: any[] = [];
|
|
127
|
+
for await (const event of parseSSEStream(stream)) {
|
|
128
|
+
events.push(event);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
expect(events).toHaveLength(2);
|
|
132
|
+
expect(events[0]).toEqual({ type: 'start' });
|
|
133
|
+
expect(events[1]).toEqual({ type: 'end' });
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('should handle remaining buffer data after stream ends', async () => {
|
|
137
|
+
const stream = createMockSSEStream([
|
|
138
|
+
'data: {"type":"complete"}'
|
|
139
|
+
]);
|
|
140
|
+
|
|
141
|
+
const events: any[] = [];
|
|
142
|
+
for await (const event of parseSSEStream(stream)) {
|
|
143
|
+
events.push(event);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
expect(events).toHaveLength(1);
|
|
147
|
+
expect(events[0]).toEqual({ type: 'complete' });
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('should support cancellation via AbortSignal', async () => {
|
|
151
|
+
const controller = new AbortController();
|
|
152
|
+
const stream = createMockSSEStream(['data: {"type":"start"}\n\n']);
|
|
153
|
+
controller.abort();
|
|
154
|
+
|
|
155
|
+
await expect(async () => {
|
|
156
|
+
for await (const event of parseSSEStream(stream, controller.signal)) {}
|
|
157
|
+
}).rejects.toThrow('Operation was aborted');
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('should handle non-data SSE lines', async () => {
|
|
161
|
+
const stream = createMockSSEStream([
|
|
162
|
+
'event: message\n',
|
|
163
|
+
'id: 123\n',
|
|
164
|
+
'retry: 3000\n',
|
|
165
|
+
'data: {"type":"test"}\n\n'
|
|
166
|
+
]);
|
|
167
|
+
|
|
168
|
+
const events: any[] = [];
|
|
169
|
+
for await (const event of parseSSEStream(stream)) {
|
|
170
|
+
events.push(event);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
expect(events).toHaveLength(1);
|
|
174
|
+
expect(events[0]).toEqual({ type: 'test' });
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
describe('responseToAsyncIterable', () => {
|
|
179
|
+
it('should convert Response with SSE stream to AsyncIterable', async () => {
|
|
180
|
+
const mockBody = createMockSSEStream([
|
|
181
|
+
'data: {"type":"start"}\n\n',
|
|
182
|
+
'data: {"type":"end"}\n\n'
|
|
183
|
+
]);
|
|
184
|
+
|
|
185
|
+
const mockResponse = {
|
|
186
|
+
ok: true,
|
|
187
|
+
body: mockBody
|
|
188
|
+
} as Response;
|
|
189
|
+
|
|
190
|
+
const events: any[] = [];
|
|
191
|
+
for await (const event of responseToAsyncIterable(mockResponse)) {
|
|
192
|
+
events.push(event);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
expect(events).toHaveLength(2);
|
|
196
|
+
expect(events[0]).toEqual({ type: 'start' });
|
|
197
|
+
expect(events[1]).toEqual({ type: 'end' });
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('should throw error for non-ok response', async () => {
|
|
201
|
+
const mockResponse = {
|
|
202
|
+
ok: false,
|
|
203
|
+
status: 500,
|
|
204
|
+
statusText: 'Internal Server Error'
|
|
205
|
+
} as Response;
|
|
206
|
+
|
|
207
|
+
await expect(async () => {
|
|
208
|
+
for await (const event of responseToAsyncIterable(mockResponse)) {
|
|
209
|
+
// Should not reach here
|
|
210
|
+
}
|
|
211
|
+
}).rejects.toThrow('Response not ok: 500 Internal Server Error');
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('should throw error for response without body', async () => {
|
|
215
|
+
const mockResponse = {
|
|
216
|
+
ok: true,
|
|
217
|
+
body: null
|
|
218
|
+
} as Response;
|
|
219
|
+
|
|
220
|
+
await expect(async () => {
|
|
221
|
+
for await (const event of responseToAsyncIterable(mockResponse)) {
|
|
222
|
+
// Should not reach here
|
|
223
|
+
}
|
|
224
|
+
}).rejects.toThrow('No response body');
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
describe('asyncIterableToSSEStream', () => {
|
|
229
|
+
it('should convert AsyncIterable to SSE-formatted ReadableStream', async () => {
|
|
230
|
+
async function* mockEvents() {
|
|
231
|
+
yield { type: 'start', command: 'test' };
|
|
232
|
+
yield { type: 'stdout', data: 'output' };
|
|
233
|
+
yield { type: 'complete', exitCode: 0 };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const stream = asyncIterableToSSEStream(mockEvents());
|
|
237
|
+
const reader = stream.getReader();
|
|
238
|
+
const decoder = new TextDecoder();
|
|
239
|
+
|
|
240
|
+
const chunks: string[] = [];
|
|
241
|
+
let done = false;
|
|
242
|
+
|
|
243
|
+
while (!done) {
|
|
244
|
+
const { value, done: readerDone } = await reader.read();
|
|
245
|
+
done = readerDone;
|
|
246
|
+
if (value) {
|
|
247
|
+
chunks.push(decoder.decode(value));
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const fullOutput = chunks.join('');
|
|
252
|
+
expect(fullOutput).toBe(
|
|
253
|
+
'data: {"type":"start","command":"test"}\n\n' +
|
|
254
|
+
'data: {"type":"stdout","data":"output"}\n\n' +
|
|
255
|
+
'data: {"type":"complete","exitCode":0}\n\n' +
|
|
256
|
+
'data: [DONE]\n\n'
|
|
257
|
+
);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('should use custom serializer when provided', async () => {
|
|
261
|
+
async function* mockEvents() {
|
|
262
|
+
yield { name: 'test', value: 123 };
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const stream = asyncIterableToSSEStream(
|
|
266
|
+
mockEvents(),
|
|
267
|
+
{ serialize: (event) => `custom:${event.name}=${event.value}` }
|
|
268
|
+
);
|
|
269
|
+
|
|
270
|
+
const reader = stream.getReader();
|
|
271
|
+
const decoder = new TextDecoder();
|
|
272
|
+
const { value } = await reader.read();
|
|
273
|
+
|
|
274
|
+
expect(decoder.decode(value!)).toBe('data: custom:test=123\n\n');
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('should handle errors in async iterable', async () => {
|
|
278
|
+
async function* mockEvents() {
|
|
279
|
+
yield { type: 'start' };
|
|
280
|
+
throw new Error('Async iterable error');
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const stream = asyncIterableToSSEStream(mockEvents());
|
|
284
|
+
const reader = stream.getReader();
|
|
285
|
+
|
|
286
|
+
await reader.read();
|
|
287
|
+
await expect(reader.read()).rejects.toThrow('Async iterable error');
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
});
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import type {
|
|
3
|
+
CommandsResponse,
|
|
4
|
+
PingResponse
|
|
5
|
+
} from '../src/clients';
|
|
6
|
+
import { UtilityClient } from '../src/clients/utility-client';
|
|
7
|
+
import {
|
|
8
|
+
SandboxError
|
|
9
|
+
} from '../src/errors';
|
|
10
|
+
|
|
11
|
+
// Mock data factory for creating test responses
|
|
12
|
+
const mockPingResponse = (overrides: Partial<PingResponse> = {}): PingResponse => ({
|
|
13
|
+
success: true,
|
|
14
|
+
message: 'pong',
|
|
15
|
+
uptime: 12345,
|
|
16
|
+
timestamp: '2023-01-01T00:00:00Z',
|
|
17
|
+
...overrides
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const mockCommandsResponse = (commands: string[], overrides: Partial<CommandsResponse> = {}): CommandsResponse => ({
|
|
21
|
+
success: true,
|
|
22
|
+
availableCommands: commands,
|
|
23
|
+
count: commands.length,
|
|
24
|
+
timestamp: '2023-01-01T00:00:00Z',
|
|
25
|
+
...overrides
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe('UtilityClient', () => {
|
|
29
|
+
let client: UtilityClient;
|
|
30
|
+
let mockFetch: ReturnType<typeof vi.fn>;
|
|
31
|
+
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
vi.clearAllMocks();
|
|
34
|
+
|
|
35
|
+
mockFetch = vi.fn();
|
|
36
|
+
global.fetch = mockFetch as unknown as typeof fetch;
|
|
37
|
+
|
|
38
|
+
client = new UtilityClient({
|
|
39
|
+
baseUrl: 'http://test.com',
|
|
40
|
+
port: 3000,
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
afterEach(() => {
|
|
45
|
+
vi.restoreAllMocks();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('health checking', () => {
|
|
49
|
+
it('should check sandbox health successfully', async () => {
|
|
50
|
+
mockFetch.mockResolvedValue(new Response(
|
|
51
|
+
JSON.stringify(mockPingResponse()),
|
|
52
|
+
{ status: 200 }
|
|
53
|
+
));
|
|
54
|
+
|
|
55
|
+
const result = await client.ping();
|
|
56
|
+
|
|
57
|
+
expect(result).toBe('pong');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should handle different health messages', async () => {
|
|
61
|
+
const messages = ['pong', 'alive', 'ok'];
|
|
62
|
+
|
|
63
|
+
for (const message of messages) {
|
|
64
|
+
mockFetch.mockResolvedValueOnce(new Response(
|
|
65
|
+
JSON.stringify(mockPingResponse({ message })),
|
|
66
|
+
{ status: 200 }
|
|
67
|
+
));
|
|
68
|
+
|
|
69
|
+
const result = await client.ping();
|
|
70
|
+
expect(result).toBe(message);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('should handle concurrent health checks', async () => {
|
|
75
|
+
mockFetch.mockImplementation(() =>
|
|
76
|
+
Promise.resolve(new Response(JSON.stringify(mockPingResponse())))
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
const healthChecks = await Promise.all([
|
|
80
|
+
client.ping(),
|
|
81
|
+
client.ping(),
|
|
82
|
+
client.ping(),
|
|
83
|
+
]);
|
|
84
|
+
|
|
85
|
+
expect(healthChecks).toHaveLength(3);
|
|
86
|
+
healthChecks.forEach(result => {
|
|
87
|
+
expect(result).toBe('pong');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
expect(mockFetch).toHaveBeenCalledTimes(3);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should detect unhealthy sandbox conditions', async () => {
|
|
94
|
+
const errorResponse = {
|
|
95
|
+
error: 'Service Unavailable',
|
|
96
|
+
code: 'HEALTH_CHECK_FAILED'
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
mockFetch.mockResolvedValue(new Response(
|
|
100
|
+
JSON.stringify(errorResponse),
|
|
101
|
+
{ status: 503 }
|
|
102
|
+
));
|
|
103
|
+
|
|
104
|
+
await expect(client.ping()).rejects.toThrow();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should handle network failures during health checks', async () => {
|
|
108
|
+
mockFetch.mockRejectedValue(new Error('Network connection failed'));
|
|
109
|
+
|
|
110
|
+
await expect(client.ping()).rejects.toThrow('Network connection failed');
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe('command discovery', () => {
|
|
115
|
+
it('should discover available system commands', async () => {
|
|
116
|
+
const systemCommands = ['ls', 'cat', 'echo', 'grep', 'find'];
|
|
117
|
+
|
|
118
|
+
mockFetch.mockResolvedValue(new Response(
|
|
119
|
+
JSON.stringify(mockCommandsResponse(systemCommands)),
|
|
120
|
+
{ status: 200 }
|
|
121
|
+
));
|
|
122
|
+
|
|
123
|
+
const result = await client.getCommands();
|
|
124
|
+
|
|
125
|
+
expect(result).toEqual(systemCommands);
|
|
126
|
+
expect(result).toContain('ls');
|
|
127
|
+
expect(result).toContain('grep');
|
|
128
|
+
expect(result).toHaveLength(systemCommands.length);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('should handle minimal command environments', async () => {
|
|
132
|
+
const minimalCommands = ['sh', 'echo', 'cat'];
|
|
133
|
+
|
|
134
|
+
mockFetch.mockResolvedValue(new Response(
|
|
135
|
+
JSON.stringify(mockCommandsResponse(minimalCommands)),
|
|
136
|
+
{ status: 200 }
|
|
137
|
+
));
|
|
138
|
+
|
|
139
|
+
const result = await client.getCommands();
|
|
140
|
+
|
|
141
|
+
expect(result).toEqual(minimalCommands);
|
|
142
|
+
expect(result).toHaveLength(3);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('should handle large command environments', async () => {
|
|
146
|
+
const richCommands = Array.from({ length: 150 }, (_, i) => `cmd_${i}`);
|
|
147
|
+
|
|
148
|
+
mockFetch.mockResolvedValue(new Response(
|
|
149
|
+
JSON.stringify(mockCommandsResponse(richCommands)),
|
|
150
|
+
{ status: 200 }
|
|
151
|
+
));
|
|
152
|
+
|
|
153
|
+
const result = await client.getCommands();
|
|
154
|
+
|
|
155
|
+
expect(result).toEqual(richCommands);
|
|
156
|
+
expect(result).toHaveLength(150);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('should handle empty command environments', async () => {
|
|
160
|
+
mockFetch.mockResolvedValue(new Response(
|
|
161
|
+
JSON.stringify(mockCommandsResponse([])),
|
|
162
|
+
{ status: 200 }
|
|
163
|
+
));
|
|
164
|
+
|
|
165
|
+
const result = await client.getCommands();
|
|
166
|
+
|
|
167
|
+
expect(result).toEqual([]);
|
|
168
|
+
expect(result).toHaveLength(0);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('should handle command discovery failures', async () => {
|
|
172
|
+
const errorResponse = {
|
|
173
|
+
error: 'Access denied to command list',
|
|
174
|
+
code: 'PERMISSION_DENIED'
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
mockFetch.mockResolvedValue(new Response(
|
|
178
|
+
JSON.stringify(errorResponse),
|
|
179
|
+
{ status: 403 }
|
|
180
|
+
));
|
|
181
|
+
|
|
182
|
+
await expect(client.getCommands()).rejects.toThrow();
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe('error handling and resilience', () => {
|
|
187
|
+
it('should handle malformed server responses gracefully', async () => {
|
|
188
|
+
mockFetch.mockResolvedValue(new Response(
|
|
189
|
+
'invalid json {',
|
|
190
|
+
{ status: 200 }
|
|
191
|
+
));
|
|
192
|
+
|
|
193
|
+
await expect(client.ping()).rejects.toThrow(SandboxError);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('should handle network timeouts and connectivity issues', async () => {
|
|
197
|
+
const networkError = new Error('Network timeout');
|
|
198
|
+
mockFetch.mockRejectedValue(networkError);
|
|
199
|
+
|
|
200
|
+
await expect(client.ping()).rejects.toThrow(networkError.message);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('should handle partial service failures', async () => {
|
|
204
|
+
// First call (ping) succeeds
|
|
205
|
+
mockFetch.mockResolvedValueOnce(new Response(
|
|
206
|
+
JSON.stringify(mockPingResponse()),
|
|
207
|
+
{ status: 200 }
|
|
208
|
+
));
|
|
209
|
+
|
|
210
|
+
// Second call (getCommands) fails
|
|
211
|
+
const errorResponse = {
|
|
212
|
+
error: 'Command enumeration service unavailable',
|
|
213
|
+
code: 'SERVICE_UNAVAILABLE'
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
mockFetch.mockResolvedValueOnce(new Response(
|
|
217
|
+
JSON.stringify(errorResponse),
|
|
218
|
+
{ status: 503 }
|
|
219
|
+
));
|
|
220
|
+
|
|
221
|
+
const pingResult = await client.ping();
|
|
222
|
+
expect(pingResult).toBe('pong');
|
|
223
|
+
|
|
224
|
+
await expect(client.getCommands()).rejects.toThrow();
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it('should handle concurrent operations with mixed success', async () => {
|
|
228
|
+
let callCount = 0;
|
|
229
|
+
mockFetch.mockImplementation(() => {
|
|
230
|
+
callCount++;
|
|
231
|
+
if (callCount % 2 === 0) {
|
|
232
|
+
return Promise.reject(new Error('Intermittent failure'));
|
|
233
|
+
} else {
|
|
234
|
+
return Promise.resolve(new Response(JSON.stringify(mockPingResponse())));
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
const results = await Promise.allSettled([
|
|
239
|
+
client.ping(), // Should succeed (call 1)
|
|
240
|
+
client.ping(), // Should fail (call 2)
|
|
241
|
+
client.ping(), // Should succeed (call 3)
|
|
242
|
+
client.ping(), // Should fail (call 4)
|
|
243
|
+
]);
|
|
244
|
+
|
|
245
|
+
expect(results[0].status).toBe('fulfilled');
|
|
246
|
+
expect(results[1].status).toBe('rejected');
|
|
247
|
+
expect(results[2].status).toBe('fulfilled');
|
|
248
|
+
expect(results[3].status).toBe('rejected');
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
describe('constructor options', () => {
|
|
253
|
+
it('should initialize with minimal options', () => {
|
|
254
|
+
const minimalClient = new UtilityClient();
|
|
255
|
+
expect(minimalClient).toBeInstanceOf(UtilityClient);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it('should initialize with full options', () => {
|
|
259
|
+
const fullOptionsClient = new UtilityClient({
|
|
260
|
+
baseUrl: 'http://custom.com',
|
|
261
|
+
port: 8080,
|
|
262
|
+
});
|
|
263
|
+
expect(fullOptionsClient).toBeInstanceOf(UtilityClient);
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sandbox-test",
|
|
3
|
+
"main": "src/index.ts",
|
|
4
|
+
"compatibility_date": "2025-05-06",
|
|
5
|
+
"compatibility_flags": ["nodejs_compat"],
|
|
6
|
+
|
|
7
|
+
"observability": {
|
|
8
|
+
"enabled": true
|
|
9
|
+
},
|
|
10
|
+
|
|
11
|
+
"containers": [
|
|
12
|
+
{
|
|
13
|
+
"class_name": "Sandbox",
|
|
14
|
+
"image": "../Dockerfile",
|
|
15
|
+
"name": "sandbox",
|
|
16
|
+
"max_instances": 1
|
|
17
|
+
}
|
|
18
|
+
],
|
|
19
|
+
|
|
20
|
+
"durable_objects": {
|
|
21
|
+
"bindings": [
|
|
22
|
+
{
|
|
23
|
+
"class_name": "Sandbox",
|
|
24
|
+
"name": "Sandbox"
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
"migrations": [
|
|
30
|
+
{
|
|
31
|
+
"new_sqlite_classes": ["Sandbox"],
|
|
32
|
+
"tag": "v1"
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
1
|
{
|
|
2
|
-
"extends": "
|
|
2
|
+
"extends": "@repo/typescript-config/base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": ".",
|
|
5
|
+
"types": ["node", "@cloudflare/workers-types"],
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"resolveJsonModule": true
|
|
8
|
+
},
|
|
9
|
+
"include": ["src/**/*.ts"],
|
|
10
|
+
"exclude": ["tests/**", "node_modules", "dist"]
|
|
3
11
|
}
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Workers runtime test configuration
|
|
5
|
+
*
|
|
6
|
+
* Tests the SDK code in Cloudflare Workers environment with Durable Objects.
|
|
7
|
+
* Uses @cloudflare/vitest-pool-workers to run tests in workerd runtime.
|
|
8
|
+
*
|
|
9
|
+
* Run with: npm test
|
|
10
|
+
*/
|
|
11
|
+
export default defineWorkersConfig({
|
|
12
|
+
test: {
|
|
13
|
+
globals: true,
|
|
14
|
+
include: ['tests/**/*.test.ts'],
|
|
15
|
+
testTimeout: 10000,
|
|
16
|
+
hookTimeout: 10000,
|
|
17
|
+
teardownTimeout: 10000,
|
|
18
|
+
poolOptions: {
|
|
19
|
+
workers: {
|
|
20
|
+
wrangler: {
|
|
21
|
+
configPath: './tests/wrangler.jsonc',
|
|
22
|
+
},
|
|
23
|
+
singleWorker: true,
|
|
24
|
+
isolatedStorage: false,
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
esbuild: {
|
|
29
|
+
target: 'esnext',
|
|
30
|
+
},
|
|
31
|
+
});
|