@cloudflare/sandbox 0.5.6 → 0.6.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 (56) hide show
  1. package/Dockerfile +54 -56
  2. package/dist/index.d.ts +1 -0
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +3 -1
  5. package/dist/index.js.map +1 -1
  6. package/package.json +11 -6
  7. package/.turbo/turbo-build.log +0 -23
  8. package/CHANGELOG.md +0 -463
  9. package/src/clients/base-client.ts +0 -356
  10. package/src/clients/command-client.ts +0 -133
  11. package/src/clients/file-client.ts +0 -300
  12. package/src/clients/git-client.ts +0 -98
  13. package/src/clients/index.ts +0 -64
  14. package/src/clients/interpreter-client.ts +0 -339
  15. package/src/clients/port-client.ts +0 -105
  16. package/src/clients/process-client.ts +0 -198
  17. package/src/clients/sandbox-client.ts +0 -39
  18. package/src/clients/types.ts +0 -88
  19. package/src/clients/utility-client.ts +0 -156
  20. package/src/errors/adapter.ts +0 -238
  21. package/src/errors/classes.ts +0 -594
  22. package/src/errors/index.ts +0 -109
  23. package/src/file-stream.ts +0 -175
  24. package/src/index.ts +0 -121
  25. package/src/interpreter.ts +0 -168
  26. package/src/openai/index.ts +0 -465
  27. package/src/request-handler.ts +0 -184
  28. package/src/sandbox.ts +0 -1937
  29. package/src/security.ts +0 -119
  30. package/src/sse-parser.ts +0 -147
  31. package/src/storage-mount/credential-detection.ts +0 -41
  32. package/src/storage-mount/errors.ts +0 -51
  33. package/src/storage-mount/index.ts +0 -17
  34. package/src/storage-mount/provider-detection.ts +0 -93
  35. package/src/storage-mount/types.ts +0 -17
  36. package/src/version.ts +0 -6
  37. package/tests/base-client.test.ts +0 -582
  38. package/tests/command-client.test.ts +0 -444
  39. package/tests/file-client.test.ts +0 -831
  40. package/tests/file-stream.test.ts +0 -310
  41. package/tests/get-sandbox.test.ts +0 -172
  42. package/tests/git-client.test.ts +0 -455
  43. package/tests/openai-shell-editor.test.ts +0 -434
  44. package/tests/port-client.test.ts +0 -283
  45. package/tests/process-client.test.ts +0 -649
  46. package/tests/request-handler.test.ts +0 -292
  47. package/tests/sandbox.test.ts +0 -890
  48. package/tests/sse-parser.test.ts +0 -291
  49. package/tests/storage-mount/credential-detection.test.ts +0 -119
  50. package/tests/storage-mount/provider-detection.test.ts +0 -77
  51. package/tests/utility-client.test.ts +0 -339
  52. package/tests/version.test.ts +0 -16
  53. package/tests/wrangler.jsonc +0 -35
  54. package/tsconfig.json +0 -11
  55. package/tsdown.config.ts +0 -13
  56. package/vitest.config.ts +0 -31
@@ -1,339 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
- import type {
3
- CommandsResponse,
4
- PingResponse,
5
- VersionResponse
6
- } from '../src/clients';
7
- import { UtilityClient } from '../src/clients/utility-client';
8
- import { SandboxError } from '../src/errors';
9
-
10
- // Mock data factory for creating test responses
11
- const mockPingResponse = (
12
- overrides: Partial<PingResponse> = {}
13
- ): PingResponse => ({
14
- success: true,
15
- message: 'pong',
16
- uptime: 12345,
17
- timestamp: '2023-01-01T00:00:00Z',
18
- ...overrides
19
- });
20
-
21
- const mockCommandsResponse = (
22
- commands: string[],
23
- overrides: Partial<CommandsResponse> = {}
24
- ): CommandsResponse => ({
25
- success: true,
26
- availableCommands: commands,
27
- count: commands.length,
28
- timestamp: '2023-01-01T00:00:00Z',
29
- ...overrides
30
- });
31
-
32
- const mockVersionResponse = (
33
- version: string = '0.4.5',
34
- overrides: Partial<VersionResponse> = {}
35
- ): VersionResponse => ({
36
- success: true,
37
- version,
38
- timestamp: '2023-01-01T00:00:00Z',
39
- ...overrides
40
- });
41
-
42
- describe('UtilityClient', () => {
43
- let client: UtilityClient;
44
- let mockFetch: ReturnType<typeof vi.fn>;
45
-
46
- beforeEach(() => {
47
- vi.clearAllMocks();
48
-
49
- mockFetch = vi.fn();
50
- global.fetch = mockFetch as unknown as typeof fetch;
51
-
52
- client = new UtilityClient({
53
- baseUrl: 'http://test.com',
54
- port: 3000
55
- });
56
- });
57
-
58
- afterEach(() => {
59
- vi.restoreAllMocks();
60
- });
61
-
62
- describe('health checking', () => {
63
- it('should check sandbox health successfully', async () => {
64
- mockFetch.mockResolvedValue(
65
- new Response(JSON.stringify(mockPingResponse()), { status: 200 })
66
- );
67
-
68
- const result = await client.ping();
69
-
70
- expect(result).toBe('pong');
71
- });
72
-
73
- it('should handle different health messages', async () => {
74
- const messages = ['pong', 'alive', 'ok'];
75
-
76
- for (const message of messages) {
77
- mockFetch.mockResolvedValueOnce(
78
- new Response(JSON.stringify(mockPingResponse({ message })), {
79
- status: 200
80
- })
81
- );
82
-
83
- const result = await client.ping();
84
- expect(result).toBe(message);
85
- }
86
- });
87
-
88
- it('should handle concurrent health checks', async () => {
89
- mockFetch.mockImplementation(() =>
90
- Promise.resolve(new Response(JSON.stringify(mockPingResponse())))
91
- );
92
-
93
- const healthChecks = await Promise.all([
94
- client.ping(),
95
- client.ping(),
96
- client.ping()
97
- ]);
98
-
99
- expect(healthChecks).toHaveLength(3);
100
- healthChecks.forEach((result) => {
101
- expect(result).toBe('pong');
102
- });
103
-
104
- expect(mockFetch).toHaveBeenCalledTimes(3);
105
- });
106
-
107
- it('should detect unhealthy sandbox conditions', async () => {
108
- const errorResponse = {
109
- error: 'Service Unavailable',
110
- code: 'HEALTH_CHECK_FAILED'
111
- };
112
-
113
- mockFetch.mockResolvedValue(
114
- new Response(JSON.stringify(errorResponse), { status: 503 })
115
- );
116
-
117
- await expect(client.ping()).rejects.toThrow();
118
- });
119
-
120
- it('should handle network failures during health checks', async () => {
121
- mockFetch.mockRejectedValue(new Error('Network connection failed'));
122
-
123
- await expect(client.ping()).rejects.toThrow('Network connection failed');
124
- });
125
- });
126
-
127
- describe('command discovery', () => {
128
- it('should discover available system commands', async () => {
129
- const systemCommands = ['ls', 'cat', 'echo', 'grep', 'find'];
130
-
131
- mockFetch.mockResolvedValue(
132
- new Response(JSON.stringify(mockCommandsResponse(systemCommands)), {
133
- status: 200
134
- })
135
- );
136
-
137
- const result = await client.getCommands();
138
-
139
- expect(result).toEqual(systemCommands);
140
- expect(result).toContain('ls');
141
- expect(result).toContain('grep');
142
- expect(result).toHaveLength(systemCommands.length);
143
- });
144
-
145
- it('should handle minimal command environments', async () => {
146
- const minimalCommands = ['sh', 'echo', 'cat'];
147
-
148
- mockFetch.mockResolvedValue(
149
- new Response(JSON.stringify(mockCommandsResponse(minimalCommands)), {
150
- status: 200
151
- })
152
- );
153
-
154
- const result = await client.getCommands();
155
-
156
- expect(result).toEqual(minimalCommands);
157
- expect(result).toHaveLength(3);
158
- });
159
-
160
- it('should handle large command environments', async () => {
161
- const richCommands = Array.from({ length: 150 }, (_, i) => `cmd_${i}`);
162
-
163
- mockFetch.mockResolvedValue(
164
- new Response(JSON.stringify(mockCommandsResponse(richCommands)), {
165
- status: 200
166
- })
167
- );
168
-
169
- const result = await client.getCommands();
170
-
171
- expect(result).toEqual(richCommands);
172
- expect(result).toHaveLength(150);
173
- });
174
-
175
- it('should handle empty command environments', async () => {
176
- mockFetch.mockResolvedValue(
177
- new Response(JSON.stringify(mockCommandsResponse([])), { status: 200 })
178
- );
179
-
180
- const result = await client.getCommands();
181
-
182
- expect(result).toEqual([]);
183
- expect(result).toHaveLength(0);
184
- });
185
-
186
- it('should handle command discovery failures', async () => {
187
- const errorResponse = {
188
- error: 'Access denied to command list',
189
- code: 'PERMISSION_DENIED'
190
- };
191
-
192
- mockFetch.mockResolvedValue(
193
- new Response(JSON.stringify(errorResponse), { status: 403 })
194
- );
195
-
196
- await expect(client.getCommands()).rejects.toThrow();
197
- });
198
- });
199
-
200
- describe('error handling and resilience', () => {
201
- it('should handle malformed server responses gracefully', async () => {
202
- mockFetch.mockResolvedValue(
203
- new Response('invalid json {', { status: 200 })
204
- );
205
-
206
- await expect(client.ping()).rejects.toThrow(SandboxError);
207
- });
208
-
209
- it('should handle network timeouts and connectivity issues', async () => {
210
- const networkError = new Error('Network timeout');
211
- mockFetch.mockRejectedValue(networkError);
212
-
213
- await expect(client.ping()).rejects.toThrow(networkError.message);
214
- });
215
-
216
- it('should handle partial service failures', async () => {
217
- // First call (ping) succeeds
218
- mockFetch.mockResolvedValueOnce(
219
- new Response(JSON.stringify(mockPingResponse()), { status: 200 })
220
- );
221
-
222
- // Second call (getCommands) fails
223
- const errorResponse = {
224
- error: 'Command enumeration service unavailable',
225
- code: 'SERVICE_UNAVAILABLE'
226
- };
227
-
228
- mockFetch.mockResolvedValueOnce(
229
- new Response(JSON.stringify(errorResponse), { status: 503 })
230
- );
231
-
232
- const pingResult = await client.ping();
233
- expect(pingResult).toBe('pong');
234
-
235
- await expect(client.getCommands()).rejects.toThrow();
236
- });
237
-
238
- it('should handle concurrent operations with mixed success', async () => {
239
- let callCount = 0;
240
- mockFetch.mockImplementation(() => {
241
- callCount++;
242
- if (callCount % 2 === 0) {
243
- return Promise.reject(new Error('Intermittent failure'));
244
- } else {
245
- return Promise.resolve(
246
- new Response(JSON.stringify(mockPingResponse()))
247
- );
248
- }
249
- });
250
-
251
- const results = await Promise.allSettled([
252
- client.ping(), // Should succeed (call 1)
253
- client.ping(), // Should fail (call 2)
254
- client.ping(), // Should succeed (call 3)
255
- client.ping() // Should fail (call 4)
256
- ]);
257
-
258
- expect(results[0].status).toBe('fulfilled');
259
- expect(results[1].status).toBe('rejected');
260
- expect(results[2].status).toBe('fulfilled');
261
- expect(results[3].status).toBe('rejected');
262
- });
263
- });
264
-
265
- describe('version checking', () => {
266
- it('should get container version successfully', async () => {
267
- mockFetch.mockResolvedValue(
268
- new Response(JSON.stringify(mockVersionResponse('0.4.5')), {
269
- status: 200
270
- })
271
- );
272
-
273
- const result = await client.getVersion();
274
-
275
- expect(result).toBe('0.4.5');
276
- });
277
-
278
- it('should handle different version formats', async () => {
279
- const versions = ['1.0.0', '2.5.3-beta', '0.0.1', '10.20.30'];
280
-
281
- for (const version of versions) {
282
- mockFetch.mockResolvedValueOnce(
283
- new Response(JSON.stringify(mockVersionResponse(version)), {
284
- status: 200
285
- })
286
- );
287
-
288
- const result = await client.getVersion();
289
- expect(result).toBe(version);
290
- }
291
- });
292
-
293
- it('should return "unknown" when version endpoint does not exist (backward compatibility)', async () => {
294
- // Simulate 404 or other error for old containers
295
- mockFetch.mockResolvedValue(
296
- new Response(JSON.stringify({ error: 'Not Found' }), { status: 404 })
297
- );
298
-
299
- const result = await client.getVersion();
300
-
301
- expect(result).toBe('unknown');
302
- });
303
-
304
- it('should return "unknown" on network failure (backward compatibility)', async () => {
305
- mockFetch.mockRejectedValue(new Error('Network connection failed'));
306
-
307
- const result = await client.getVersion();
308
-
309
- expect(result).toBe('unknown');
310
- });
311
-
312
- it('should handle version response with unknown value', async () => {
313
- mockFetch.mockResolvedValue(
314
- new Response(JSON.stringify(mockVersionResponse('unknown')), {
315
- status: 200
316
- })
317
- );
318
-
319
- const result = await client.getVersion();
320
-
321
- expect(result).toBe('unknown');
322
- });
323
- });
324
-
325
- describe('constructor options', () => {
326
- it('should initialize with minimal options', () => {
327
- const minimalClient = new UtilityClient();
328
- expect(minimalClient).toBeInstanceOf(UtilityClient);
329
- });
330
-
331
- it('should initialize with full options', () => {
332
- const fullOptionsClient = new UtilityClient({
333
- baseUrl: 'http://custom.com',
334
- port: 8080
335
- });
336
- expect(fullOptionsClient).toBeInstanceOf(UtilityClient);
337
- });
338
- });
339
- });
@@ -1,16 +0,0 @@
1
- import { describe, expect, test } from 'vitest';
2
- import packageJson from '../package.json';
3
- import { SDK_VERSION } from '../src/version';
4
-
5
- describe('Version Sync', () => {
6
- test('SDK_VERSION matches package.json version', () => {
7
- // Verify versions match
8
- expect(SDK_VERSION).toBe(packageJson.version);
9
- });
10
-
11
- test('SDK_VERSION is a valid semver version', () => {
12
- // Check if version matches semver pattern (major.minor.patch)
13
- const semverPattern = /^\d+\.\d+\.\d+(-[\w.]+)?$/;
14
- expect(SDK_VERSION).toMatch(semverPattern);
15
- });
16
- });
@@ -1,35 +0,0 @@
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 DELETED
@@ -1,11 +0,0 @@
1
- {
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", "tests/**/*.ts"],
10
- "exclude": ["node_modules", "dist"]
11
- }
package/tsdown.config.ts DELETED
@@ -1,13 +0,0 @@
1
- import { defineConfig } from 'tsdown';
2
-
3
- export default defineConfig({
4
- entry: ['src/index.ts', 'src/openai/index.ts'],
5
- outDir: 'dist',
6
- dts: {
7
- sourcemap: true,
8
- resolve: ['@repo/shared']
9
- },
10
- sourcemap: true,
11
- format: 'esm',
12
- fixedExtension: false
13
- });
package/vitest.config.ts DELETED
@@ -1,31 +0,0 @@
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
- });