@cloudflare/sandbox 0.5.4 → 0.6.0

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 (57) hide show
  1. package/Dockerfile +54 -59
  2. package/README.md +1 -1
  3. package/dist/index.d.ts +1 -0
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +12 -1
  6. package/dist/index.js.map +1 -1
  7. package/package.json +13 -8
  8. package/.turbo/turbo-build.log +0 -23
  9. package/CHANGELOG.md +0 -441
  10. package/src/clients/base-client.ts +0 -356
  11. package/src/clients/command-client.ts +0 -133
  12. package/src/clients/file-client.ts +0 -300
  13. package/src/clients/git-client.ts +0 -98
  14. package/src/clients/index.ts +0 -64
  15. package/src/clients/interpreter-client.ts +0 -333
  16. package/src/clients/port-client.ts +0 -105
  17. package/src/clients/process-client.ts +0 -198
  18. package/src/clients/sandbox-client.ts +0 -39
  19. package/src/clients/types.ts +0 -88
  20. package/src/clients/utility-client.ts +0 -156
  21. package/src/errors/adapter.ts +0 -238
  22. package/src/errors/classes.ts +0 -594
  23. package/src/errors/index.ts +0 -109
  24. package/src/file-stream.ts +0 -169
  25. package/src/index.ts +0 -121
  26. package/src/interpreter.ts +0 -168
  27. package/src/openai/index.ts +0 -465
  28. package/src/request-handler.ts +0 -184
  29. package/src/sandbox.ts +0 -1937
  30. package/src/security.ts +0 -119
  31. package/src/sse-parser.ts +0 -144
  32. package/src/storage-mount/credential-detection.ts +0 -41
  33. package/src/storage-mount/errors.ts +0 -51
  34. package/src/storage-mount/index.ts +0 -17
  35. package/src/storage-mount/provider-detection.ts +0 -93
  36. package/src/storage-mount/types.ts +0 -17
  37. package/src/version.ts +0 -6
  38. package/tests/base-client.test.ts +0 -582
  39. package/tests/command-client.test.ts +0 -444
  40. package/tests/file-client.test.ts +0 -831
  41. package/tests/file-stream.test.ts +0 -310
  42. package/tests/get-sandbox.test.ts +0 -172
  43. package/tests/git-client.test.ts +0 -455
  44. package/tests/openai-shell-editor.test.ts +0 -434
  45. package/tests/port-client.test.ts +0 -283
  46. package/tests/process-client.test.ts +0 -649
  47. package/tests/request-handler.test.ts +0 -292
  48. package/tests/sandbox.test.ts +0 -890
  49. package/tests/sse-parser.test.ts +0 -291
  50. package/tests/storage-mount/credential-detection.test.ts +0 -119
  51. package/tests/storage-mount/provider-detection.test.ts +0 -77
  52. package/tests/utility-client.test.ts +0 -339
  53. package/tests/version.test.ts +0 -16
  54. package/tests/wrangler.jsonc +0 -35
  55. package/tsconfig.json +0 -11
  56. package/tsdown.config.ts +0 -13
  57. package/vitest.config.ts +0 -31
@@ -1,291 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
- import {
3
- asyncIterableToSSEStream,
4
- parseSSEStream,
5
- responseToAsyncIterable
6
- } from '../src/sse-parser';
7
-
8
- function createMockSSEStream(events: string[]): ReadableStream<Uint8Array> {
9
- return new ReadableStream({
10
- start(controller) {
11
- const encoder = new TextEncoder();
12
- for (const event of events) {
13
- controller.enqueue(encoder.encode(event));
14
- }
15
- controller.close();
16
- }
17
- });
18
- }
19
-
20
- describe('SSE Parser', () => {
21
- let consoleErrorSpy: ReturnType<typeof vi.spyOn>;
22
-
23
- beforeEach(() => {
24
- consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
25
- });
26
-
27
- afterEach(() => {
28
- consoleErrorSpy.mockRestore();
29
- });
30
-
31
- describe('parseSSEStream', () => {
32
- it('should parse valid SSE events', async () => {
33
- const stream = createMockSSEStream([
34
- 'data: {"type":"start","command":"echo test"}\n\n',
35
- 'data: {"type":"stdout","data":"test\\n"}\n\n',
36
- 'data: {"type":"complete","exitCode":0}\n\n'
37
- ]);
38
-
39
- const events: any[] = [];
40
- for await (const event of parseSSEStream(stream)) {
41
- events.push(event);
42
- }
43
-
44
- expect(events).toHaveLength(3);
45
- expect(events[0]).toEqual({ type: 'start', command: 'echo test' });
46
- expect(events[1]).toEqual({ type: 'stdout', data: 'test\n' });
47
- expect(events[2]).toEqual({ type: 'complete', exitCode: 0 });
48
- });
49
-
50
- it('should handle empty data lines', async () => {
51
- const stream = createMockSSEStream([
52
- 'data: \n\n',
53
- 'data: {"type":"stdout","data":"valid"}\n\n'
54
- ]);
55
-
56
- const events: any[] = [];
57
- for await (const event of parseSSEStream(stream)) {
58
- events.push(event);
59
- }
60
-
61
- expect(events).toHaveLength(1);
62
- expect(events[0]).toEqual({ type: 'stdout', data: 'valid' });
63
- });
64
-
65
- it('should skip [DONE] markers', async () => {
66
- const stream = createMockSSEStream([
67
- 'data: {"type":"start"}\n\n',
68
- 'data: [DONE]\n\n',
69
- 'data: {"type":"complete"}\n\n'
70
- ]);
71
-
72
- const events: any[] = [];
73
- for await (const event of parseSSEStream(stream)) {
74
- events.push(event);
75
- }
76
-
77
- expect(events).toHaveLength(2);
78
- expect(events[0]).toEqual({ type: 'start' });
79
- expect(events[1]).toEqual({ type: 'complete' });
80
- });
81
-
82
- it('should handle malformed JSON gracefully', async () => {
83
- const stream = createMockSSEStream([
84
- 'data: invalid json\n\n',
85
- 'data: {"type":"stdout","data":"valid"}\n\n',
86
- 'data: {incomplete\n\n'
87
- ]);
88
-
89
- const events: any[] = [];
90
- for await (const event of parseSSEStream(stream)) {
91
- events.push(event);
92
- }
93
-
94
- expect(events).toHaveLength(1);
95
- expect(events[0]).toEqual({ type: 'stdout', data: 'valid' });
96
- });
97
-
98
- it('should handle empty lines and comments', async () => {
99
- const stream = createMockSSEStream([
100
- '\n',
101
- ' \n',
102
- ': this is a comment\n',
103
- 'data: {"type":"test"}\n\n',
104
- '\n'
105
- ]);
106
-
107
- const events: any[] = [];
108
- for await (const event of parseSSEStream(stream)) {
109
- events.push(event);
110
- }
111
-
112
- expect(events).toHaveLength(1);
113
- expect(events[0]).toEqual({ type: 'test' });
114
- });
115
-
116
- it('should handle chunked data properly', async () => {
117
- // Simulate chunked delivery where data arrives in parts
118
- const stream = new ReadableStream({
119
- start(controller) {
120
- const encoder = new TextEncoder();
121
- // Send partial data
122
- controller.enqueue(encoder.encode('data: {"typ'));
123
- controller.enqueue(encoder.encode('e":"start"}\n\n'));
124
- controller.enqueue(encoder.encode('data: {"type":"end"}\n\n'));
125
- controller.close();
126
- }
127
- });
128
-
129
- const events: any[] = [];
130
- for await (const event of parseSSEStream(stream)) {
131
- events.push(event);
132
- }
133
-
134
- expect(events).toHaveLength(2);
135
- expect(events[0]).toEqual({ type: 'start' });
136
- expect(events[1]).toEqual({ type: 'end' });
137
- });
138
-
139
- it('should handle remaining buffer data after stream ends', async () => {
140
- const stream = createMockSSEStream(['data: {"type":"complete"}']);
141
-
142
- const events: any[] = [];
143
- for await (const event of parseSSEStream(stream)) {
144
- events.push(event);
145
- }
146
-
147
- expect(events).toHaveLength(1);
148
- expect(events[0]).toEqual({ type: 'complete' });
149
- });
150
-
151
- it('should support cancellation via AbortSignal', async () => {
152
- const controller = new AbortController();
153
- const stream = createMockSSEStream(['data: {"type":"start"}\n\n']);
154
- controller.abort();
155
-
156
- await expect(async () => {
157
- for await (const event of parseSSEStream(stream, controller.signal)) {
158
- }
159
- }).rejects.toThrow('Operation was aborted');
160
- });
161
-
162
- it('should handle non-data SSE lines', async () => {
163
- const stream = createMockSSEStream([
164
- 'event: message\n',
165
- 'id: 123\n',
166
- 'retry: 3000\n',
167
- 'data: {"type":"test"}\n\n'
168
- ]);
169
-
170
- const events: any[] = [];
171
- for await (const event of parseSSEStream(stream)) {
172
- events.push(event);
173
- }
174
-
175
- expect(events).toHaveLength(1);
176
- expect(events[0]).toEqual({ type: 'test' });
177
- });
178
- });
179
-
180
- describe('responseToAsyncIterable', () => {
181
- it('should convert Response with SSE stream to AsyncIterable', async () => {
182
- const mockBody = createMockSSEStream([
183
- 'data: {"type":"start"}\n\n',
184
- 'data: {"type":"end"}\n\n'
185
- ]);
186
-
187
- const mockResponse = {
188
- ok: true,
189
- body: mockBody
190
- } as Response;
191
-
192
- const events: any[] = [];
193
- for await (const event of responseToAsyncIterable(mockResponse)) {
194
- events.push(event);
195
- }
196
-
197
- expect(events).toHaveLength(2);
198
- expect(events[0]).toEqual({ type: 'start' });
199
- expect(events[1]).toEqual({ type: 'end' });
200
- });
201
-
202
- it('should throw error for non-ok response', async () => {
203
- const mockResponse = {
204
- ok: false,
205
- status: 500,
206
- statusText: 'Internal Server Error'
207
- } as Response;
208
-
209
- await expect(async () => {
210
- for await (const event of responseToAsyncIterable(mockResponse)) {
211
- // Should not reach here
212
- }
213
- }).rejects.toThrow('Response not ok: 500 Internal Server Error');
214
- });
215
-
216
- it('should throw error for response without body', async () => {
217
- const mockResponse = {
218
- ok: true,
219
- body: null
220
- } as Response;
221
-
222
- await expect(async () => {
223
- for await (const event of responseToAsyncIterable(mockResponse)) {
224
- // Should not reach here
225
- }
226
- }).rejects.toThrow('No response body');
227
- });
228
- });
229
-
230
- describe('asyncIterableToSSEStream', () => {
231
- it('should convert AsyncIterable to SSE-formatted ReadableStream', async () => {
232
- async function* mockEvents() {
233
- yield { type: 'start', command: 'test' };
234
- yield { type: 'stdout', data: 'output' };
235
- yield { type: 'complete', exitCode: 0 };
236
- }
237
-
238
- const stream = asyncIterableToSSEStream(mockEvents());
239
- const reader = stream.getReader();
240
- const decoder = new TextDecoder();
241
-
242
- const chunks: string[] = [];
243
- let done = false;
244
-
245
- while (!done) {
246
- const { value, done: readerDone } = await reader.read();
247
- done = readerDone;
248
- if (value) {
249
- chunks.push(decoder.decode(value));
250
- }
251
- }
252
-
253
- const fullOutput = chunks.join('');
254
- expect(fullOutput).toBe(
255
- 'data: {"type":"start","command":"test"}\n\n' +
256
- 'data: {"type":"stdout","data":"output"}\n\n' +
257
- 'data: {"type":"complete","exitCode":0}\n\n' +
258
- 'data: [DONE]\n\n'
259
- );
260
- });
261
-
262
- it('should use custom serializer when provided', async () => {
263
- async function* mockEvents() {
264
- yield { name: 'test', value: 123 };
265
- }
266
-
267
- const stream = asyncIterableToSSEStream(mockEvents(), {
268
- serialize: (event) => `custom:${event.name}=${event.value}`
269
- });
270
-
271
- const reader = stream.getReader();
272
- const decoder = new TextDecoder();
273
- const { value } = await reader.read();
274
-
275
- expect(decoder.decode(value!)).toBe('data: custom:test=123\n\n');
276
- });
277
-
278
- it('should handle errors in async iterable', async () => {
279
- async function* mockEvents() {
280
- yield { type: 'start' };
281
- throw new Error('Async iterable error');
282
- }
283
-
284
- const stream = asyncIterableToSSEStream(mockEvents());
285
- const reader = stream.getReader();
286
-
287
- await reader.read();
288
- await expect(reader.read()).rejects.toThrow('Async iterable error');
289
- });
290
- });
291
- });
@@ -1,119 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { detectCredentials } from '../../src/storage-mount/credential-detection';
3
-
4
- describe('Credential Detection', () => {
5
- it('should use explicit credentials from options', () => {
6
- const envVars = {};
7
- const options = {
8
- endpoint: 'https://test.r2.cloudflarestorage.com',
9
- credentials: {
10
- accessKeyId: 'explicit-key',
11
- secretAccessKey: 'explicit-secret'
12
- }
13
- };
14
-
15
- const credentials = detectCredentials(options, envVars);
16
-
17
- expect(credentials.accessKeyId).toBe('explicit-key');
18
- expect(credentials.secretAccessKey).toBe('explicit-secret');
19
- });
20
-
21
- it('should detect standard AWS env vars', () => {
22
- const envVars = {
23
- AWS_ACCESS_KEY_ID: 'aws-key',
24
- AWS_SECRET_ACCESS_KEY: 'aws-secret'
25
- };
26
- const options = { endpoint: 'https://s3.us-west-2.amazonaws.com' };
27
-
28
- const credentials = detectCredentials(options, envVars);
29
-
30
- expect(credentials.accessKeyId).toBe('aws-key');
31
- expect(credentials.secretAccessKey).toBe('aws-secret');
32
- });
33
-
34
- it('should ignore session token in environment', () => {
35
- const envVars = {
36
- AWS_ACCESS_KEY_ID: 'aws-key',
37
- AWS_SECRET_ACCESS_KEY: 'aws-secret',
38
- AWS_SESSION_TOKEN: 'session-token'
39
- };
40
- const options = { endpoint: 'https://s3.us-west-2.amazonaws.com' };
41
-
42
- const credentials = detectCredentials(options, envVars);
43
-
44
- expect(credentials.accessKeyId).toBe('aws-key');
45
- expect(credentials.secretAccessKey).toBe('aws-secret');
46
- });
47
-
48
- it('should prioritize explicit credentials over env vars', () => {
49
- const envVars = {
50
- AWS_ACCESS_KEY_ID: 'env-key',
51
- AWS_SECRET_ACCESS_KEY: 'env-secret'
52
- };
53
- const options = {
54
- endpoint: 'https://test.r2.cloudflarestorage.com',
55
- credentials: {
56
- accessKeyId: 'explicit-key',
57
- secretAccessKey: 'explicit-secret'
58
- }
59
- };
60
-
61
- const credentials = detectCredentials(options, envVars);
62
-
63
- expect(credentials.accessKeyId).toBe('explicit-key');
64
- expect(credentials.secretAccessKey).toBe('explicit-secret');
65
- });
66
-
67
- it('should throw error when no credentials found', () => {
68
- const envVars = {};
69
- const options = { endpoint: 'https://test.r2.cloudflarestorage.com' };
70
-
71
- expect(() => detectCredentials(options, envVars)).toThrow(
72
- 'No credentials found'
73
- );
74
- });
75
-
76
- it('should include helpful error message with env var hints', () => {
77
- const envVars = {};
78
- const options = { endpoint: 'https://test.r2.cloudflarestorage.com' };
79
-
80
- let thrownError: Error | null = null;
81
- try {
82
- detectCredentials(options, envVars);
83
- } catch (error) {
84
- thrownError = error as Error;
85
- }
86
-
87
- expect(thrownError).toBeTruthy();
88
- if (thrownError) {
89
- const message = thrownError.message;
90
- expect(message).toContain('AWS_ACCESS_KEY_ID');
91
- expect(message).toContain('AWS_SECRET_ACCESS_KEY');
92
- expect(message).toContain('explicit credentials');
93
- }
94
- });
95
-
96
- it('should throw error when only access key is present', () => {
97
- const envVars = {
98
- AWS_ACCESS_KEY_ID: 'aws-key'
99
- // Missing AWS_SECRET_ACCESS_KEY
100
- };
101
- const options = { endpoint: 'https://test.r2.cloudflarestorage.com' };
102
-
103
- expect(() => detectCredentials(options, envVars)).toThrow(
104
- 'No credentials found'
105
- );
106
- });
107
-
108
- it('should throw error when only secret key is present', () => {
109
- const envVars = {
110
- AWS_SECRET_ACCESS_KEY: 'aws-secret'
111
- // Missing AWS_ACCESS_KEY_ID
112
- };
113
- const options = { endpoint: 'https://test.r2.cloudflarestorage.com' };
114
-
115
- expect(() => detectCredentials(options, envVars)).toThrow(
116
- 'No credentials found'
117
- );
118
- });
119
- });
@@ -1,77 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import {
3
- detectProviderFromUrl,
4
- getProviderFlags,
5
- resolveS3fsOptions
6
- } from '../../src/storage-mount/provider-detection';
7
-
8
- describe('Provider Detection', () => {
9
- describe('detectProviderFromUrl', () => {
10
- it.each([
11
- ['https://abc123.r2.cloudflarestorage.com', 'r2'],
12
- ['https://s3.us-west-2.amazonaws.com', 's3'],
13
- ['https://storage.googleapis.com', 'gcs']
14
- ])('should detect %s as %s', (url, expectedProvider) => {
15
- expect(detectProviderFromUrl(url)).toBe(expectedProvider);
16
- });
17
-
18
- it.each([['https://custom.storage.example.com'], ['not-a-url'], ['']])(
19
- 'should return null for unknown/invalid: %s',
20
- (url) => {
21
- expect(detectProviderFromUrl(url)).toBe(null);
22
- }
23
- );
24
- });
25
-
26
- describe('getProviderFlags', () => {
27
- it.each([
28
- ['r2', ['nomixupload']],
29
- ['s3', []],
30
- ['gcs', []]
31
- ])('should return correct flags for %s', (provider, expected) => {
32
- expect(getProviderFlags(provider as any)).toEqual(expected);
33
- });
34
-
35
- it('should return safe defaults for unknown providers', () => {
36
- expect(getProviderFlags(null)).toEqual(['use_path_request_style']);
37
- });
38
- });
39
-
40
- describe('resolveS3fsOptions', () => {
41
- it('should use provider defaults when no user options', () => {
42
- const options = resolveS3fsOptions('r2');
43
- expect(options).toEqual(['nomixupload']);
44
- });
45
-
46
- it('should merge provider flags with user options', () => {
47
- const options = resolveS3fsOptions('r2', ['custom_flag']);
48
- expect(options).toContain('nomixupload');
49
- expect(options).toContain('custom_flag');
50
- });
51
-
52
- it('should allow user options to override provider defaults', () => {
53
- const options = resolveS3fsOptions('r2', ['endpoint=us-east']);
54
- expect(options).toContain('nomixupload');
55
- expect(options).toContain('endpoint=us-east');
56
- expect(options).not.toContain('endpoint=auto');
57
- });
58
-
59
- it('should deduplicate flags keeping last occurrence', () => {
60
- const options = resolveS3fsOptions(null, [
61
- 'use_path_request_style',
62
- 'custom_flag'
63
- ]);
64
- const count = options.filter(
65
- (o) => o === 'use_path_request_style'
66
- ).length;
67
- expect(count).toBe(1);
68
- expect(options).toContain('custom_flag');
69
- });
70
-
71
- it('should use safe defaults for unknown providers', () => {
72
- const options = resolveS3fsOptions(null, ['nomixupload']);
73
- expect(options).toContain('use_path_request_style');
74
- expect(options).toContain('nomixupload');
75
- });
76
- });
77
- });