@cloudflare/sandbox 0.0.0-af082ab → 0.0.0-b4913fb

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 (75) hide show
  1. package/CHANGELOG.md +156 -21
  2. package/Dockerfile +157 -70
  3. package/LICENSE +176 -0
  4. package/README.md +92 -822
  5. package/dist/index.d.ts +1953 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +3280 -0
  8. package/dist/index.js.map +1 -0
  9. package/package.json +16 -8
  10. package/src/clients/base-client.ts +295 -0
  11. package/src/clients/command-client.ts +115 -0
  12. package/src/clients/file-client.ts +300 -0
  13. package/src/clients/git-client.ts +98 -0
  14. package/src/clients/index.ts +64 -0
  15. package/src/clients/interpreter-client.ts +333 -0
  16. package/src/clients/port-client.ts +105 -0
  17. package/src/clients/process-client.ts +180 -0
  18. package/src/clients/sandbox-client.ts +39 -0
  19. package/src/clients/types.ts +88 -0
  20. package/src/clients/utility-client.ts +156 -0
  21. package/src/errors/adapter.ts +238 -0
  22. package/src/errors/classes.ts +594 -0
  23. package/src/errors/index.ts +109 -0
  24. package/src/file-stream.ts +123 -116
  25. package/src/index.ts +89 -66
  26. package/src/interpreter.ts +58 -40
  27. package/src/request-handler.ts +94 -55
  28. package/src/sandbox.ts +1009 -497
  29. package/src/security.ts +34 -28
  30. package/src/sse-parser.ts +8 -11
  31. package/src/version.ts +6 -0
  32. package/startup.sh +3 -0
  33. package/tests/base-client.test.ts +364 -0
  34. package/tests/command-client.test.ts +444 -0
  35. package/tests/file-client.test.ts +831 -0
  36. package/tests/file-stream.test.ts +310 -0
  37. package/tests/get-sandbox.test.ts +149 -0
  38. package/tests/git-client.test.ts +487 -0
  39. package/tests/port-client.test.ts +293 -0
  40. package/tests/process-client.test.ts +683 -0
  41. package/tests/request-handler.test.ts +292 -0
  42. package/tests/sandbox.test.ts +739 -0
  43. package/tests/sse-parser.test.ts +291 -0
  44. package/tests/utility-client.test.ts +339 -0
  45. package/tests/version.test.ts +16 -0
  46. package/tests/wrangler.jsonc +35 -0
  47. package/tsconfig.json +9 -1
  48. package/tsdown.config.ts +12 -0
  49. package/vitest.config.ts +31 -0
  50. package/container_src/bun.lock +0 -76
  51. package/container_src/circuit-breaker.ts +0 -121
  52. package/container_src/control-process.ts +0 -784
  53. package/container_src/handler/exec.ts +0 -185
  54. package/container_src/handler/file.ts +0 -457
  55. package/container_src/handler/git.ts +0 -130
  56. package/container_src/handler/ports.ts +0 -314
  57. package/container_src/handler/process.ts +0 -568
  58. package/container_src/handler/session.ts +0 -92
  59. package/container_src/index.ts +0 -601
  60. package/container_src/interpreter-service.ts +0 -276
  61. package/container_src/isolation.ts +0 -1213
  62. package/container_src/mime-processor.ts +0 -255
  63. package/container_src/package.json +0 -18
  64. package/container_src/runtime/executors/javascript/node_executor.ts +0 -123
  65. package/container_src/runtime/executors/python/ipython_executor.py +0 -338
  66. package/container_src/runtime/executors/typescript/ts_executor.ts +0 -138
  67. package/container_src/runtime/process-pool.ts +0 -464
  68. package/container_src/shell-escape.ts +0 -42
  69. package/container_src/startup.sh +0 -11
  70. package/container_src/types.ts +0 -131
  71. package/src/client.ts +0 -1048
  72. package/src/errors.ts +0 -219
  73. package/src/interpreter-client.ts +0 -352
  74. package/src/interpreter-types.ts +0 -390
  75. package/src/types.ts +0 -571
@@ -0,0 +1,293 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
+ import type {
3
+ ExposePortResponse,
4
+ GetExposedPortsResponse,
5
+ UnexposePortResponse
6
+ } from '../src/clients';
7
+ import { PortClient } from '../src/clients/port-client';
8
+ import {
9
+ InvalidPortError,
10
+ PortAlreadyExposedError,
11
+ PortError,
12
+ PortInUseError,
13
+ PortNotExposedError,
14
+ SandboxError,
15
+ ServiceNotRespondingError
16
+ } from '../src/errors';
17
+
18
+ describe('PortClient', () => {
19
+ let client: PortClient;
20
+ let mockFetch: ReturnType<typeof vi.fn>;
21
+
22
+ beforeEach(() => {
23
+ vi.clearAllMocks();
24
+
25
+ mockFetch = vi.fn();
26
+ global.fetch = mockFetch as unknown as typeof fetch;
27
+
28
+ client = new PortClient({
29
+ baseUrl: 'http://test.com',
30
+ port: 3000
31
+ });
32
+ });
33
+
34
+ afterEach(() => {
35
+ vi.restoreAllMocks();
36
+ });
37
+
38
+ describe('service exposure', () => {
39
+ it('should expose web services successfully', async () => {
40
+ const mockResponse: ExposePortResponse = {
41
+ success: true,
42
+ port: 3001,
43
+ exposedAt: 'https://preview-abc123.workers.dev',
44
+ name: 'web-server',
45
+ timestamp: '2023-01-01T00:00:00Z'
46
+ };
47
+
48
+ mockFetch.mockResolvedValue(
49
+ new Response(JSON.stringify(mockResponse), { status: 200 })
50
+ );
51
+
52
+ const result = await client.exposePort(3001, 'session-123', 'web-server');
53
+
54
+ expect(result.success).toBe(true);
55
+ expect(result.port).toBe(3001);
56
+ expect(result.exposedAt).toBe('https://preview-abc123.workers.dev');
57
+ expect(result.name).toBe('web-server');
58
+ expect(result.exposedAt.startsWith('https://')).toBe(true);
59
+ });
60
+
61
+ it('should expose API services on different ports', async () => {
62
+ const mockResponse: ExposePortResponse = {
63
+ success: true,
64
+ port: 8080,
65
+ exposedAt: 'https://api-def456.workers.dev',
66
+ name: 'api-server',
67
+ timestamp: '2023-01-01T00:00:00Z'
68
+ };
69
+
70
+ mockFetch.mockResolvedValue(
71
+ new Response(JSON.stringify(mockResponse), { status: 200 })
72
+ );
73
+
74
+ const result = await client.exposePort(8080, 'session-456', 'api-server');
75
+
76
+ expect(result.success).toBe(true);
77
+ expect(result.port).toBe(8080);
78
+ expect(result.name).toBe('api-server');
79
+ expect(result.exposedAt).toContain('api-');
80
+ });
81
+
82
+ it('should expose services without explicit names', async () => {
83
+ const mockResponse: ExposePortResponse = {
84
+ success: true,
85
+ port: 5000,
86
+ exposedAt: 'https://service-ghi789.workers.dev',
87
+ timestamp: '2023-01-01T00:00:00Z'
88
+ };
89
+
90
+ mockFetch.mockResolvedValue(
91
+ new Response(JSON.stringify(mockResponse), { status: 200 })
92
+ );
93
+
94
+ const result = await client.exposePort(5000, 'session-789');
95
+
96
+ expect(result.success).toBe(true);
97
+ expect(result.port).toBe(5000);
98
+ expect(result.name).toBeUndefined();
99
+ expect(result.exposedAt).toBeDefined();
100
+ });
101
+ });
102
+
103
+ describe('service management', () => {
104
+ it('should list all exposed services', async () => {
105
+ const mockResponse: GetExposedPortsResponse = {
106
+ success: true,
107
+ ports: [
108
+ {
109
+ port: 3000,
110
+ exposedAt: 'https://frontend-abc123.workers.dev',
111
+ name: 'frontend'
112
+ },
113
+ {
114
+ port: 4000,
115
+ exposedAt: 'https://api-def456.workers.dev',
116
+ name: 'api'
117
+ },
118
+ {
119
+ port: 5432,
120
+ exposedAt: 'https://db-ghi789.workers.dev',
121
+ name: 'database'
122
+ }
123
+ ],
124
+ count: 3,
125
+ timestamp: '2023-01-01T00:10:00Z'
126
+ };
127
+
128
+ mockFetch.mockResolvedValue(
129
+ new Response(JSON.stringify(mockResponse), { status: 200 })
130
+ );
131
+
132
+ const result = await client.getExposedPorts('session-list');
133
+
134
+ expect(result.success).toBe(true);
135
+ expect(result.count).toBe(3);
136
+ expect(result.ports).toHaveLength(3);
137
+
138
+ result.ports.forEach((service) => {
139
+ expect(service.exposedAt).toContain('.workers.dev');
140
+ expect(service.port).toBeGreaterThan(0);
141
+ expect(service.name).toBeDefined();
142
+ });
143
+ });
144
+
145
+ it('should handle empty exposed ports list', async () => {
146
+ const mockResponse: GetExposedPortsResponse = {
147
+ success: true,
148
+ ports: [],
149
+ count: 0,
150
+ timestamp: '2023-01-01T00:00:00Z'
151
+ };
152
+
153
+ mockFetch.mockResolvedValue(
154
+ new Response(JSON.stringify(mockResponse), { status: 200 })
155
+ );
156
+
157
+ const result = await client.getExposedPorts('session-empty');
158
+
159
+ expect(result.success).toBe(true);
160
+ expect(result.count).toBe(0);
161
+ expect(result.ports).toHaveLength(0);
162
+ });
163
+
164
+ it('should unexpose services cleanly', async () => {
165
+ const mockResponse: UnexposePortResponse = {
166
+ success: true,
167
+ port: 3001,
168
+ timestamp: '2023-01-01T00:15:00Z'
169
+ };
170
+
171
+ mockFetch.mockResolvedValue(
172
+ new Response(JSON.stringify(mockResponse), { status: 200 })
173
+ );
174
+
175
+ const result = await client.unexposePort(3001, 'session-unexpose');
176
+
177
+ expect(result.success).toBe(true);
178
+ expect(result.port).toBe(3001);
179
+ });
180
+ });
181
+
182
+ describe('port validation and error handling', () => {
183
+ it('should handle port already exposed errors', async () => {
184
+ const errorResponse = {
185
+ error: 'Port already exposed: 3000',
186
+ code: 'PORT_ALREADY_EXPOSED'
187
+ };
188
+
189
+ mockFetch.mockResolvedValue(
190
+ new Response(JSON.stringify(errorResponse), { status: 409 })
191
+ );
192
+
193
+ await expect(client.exposePort(3000, 'session-err')).rejects.toThrow(
194
+ PortAlreadyExposedError
195
+ );
196
+ });
197
+
198
+ it('should handle invalid port numbers', async () => {
199
+ const errorResponse = {
200
+ error: 'Invalid port number: 0',
201
+ code: 'INVALID_PORT_NUMBER'
202
+ };
203
+
204
+ mockFetch.mockResolvedValue(
205
+ new Response(JSON.stringify(errorResponse), { status: 400 })
206
+ );
207
+
208
+ await expect(client.exposePort(0, 'session-err')).rejects.toThrow(
209
+ InvalidPortError
210
+ );
211
+ });
212
+
213
+ it('should handle port in use errors', async () => {
214
+ const errorResponse = {
215
+ error: 'Port in use: 3000 is already bound by another process',
216
+ code: 'PORT_IN_USE'
217
+ };
218
+
219
+ mockFetch.mockResolvedValue(
220
+ new Response(JSON.stringify(errorResponse), { status: 409 })
221
+ );
222
+
223
+ await expect(client.exposePort(3000, 'session-err')).rejects.toThrow(
224
+ PortInUseError
225
+ );
226
+ });
227
+
228
+ it('should handle service not responding errors', async () => {
229
+ const errorResponse = {
230
+ error: 'Service not responding on port 8080',
231
+ code: 'SERVICE_NOT_RESPONDING'
232
+ };
233
+
234
+ mockFetch.mockResolvedValue(
235
+ new Response(JSON.stringify(errorResponse), { status: 503 })
236
+ );
237
+
238
+ await expect(client.exposePort(8080, 'session-err')).rejects.toThrow(
239
+ ServiceNotRespondingError
240
+ );
241
+ });
242
+
243
+ it('should handle unexpose non-existent port', async () => {
244
+ const errorResponse = {
245
+ error: 'Port not exposed: 9999',
246
+ code: 'PORT_NOT_EXPOSED'
247
+ };
248
+
249
+ mockFetch.mockResolvedValue(
250
+ new Response(JSON.stringify(errorResponse), { status: 404 })
251
+ );
252
+
253
+ await expect(client.unexposePort(9999, 'session-err')).rejects.toThrow(
254
+ PortNotExposedError
255
+ );
256
+ });
257
+
258
+ it('should handle port operation failures', async () => {
259
+ const errorResponse = {
260
+ error: 'Port operation failed: unable to setup proxy',
261
+ code: 'PORT_OPERATION_ERROR'
262
+ };
263
+
264
+ mockFetch.mockResolvedValue(
265
+ new Response(JSON.stringify(errorResponse), { status: 500 })
266
+ );
267
+
268
+ await expect(client.exposePort(3000, 'session-err')).rejects.toThrow(
269
+ PortError
270
+ );
271
+ });
272
+ });
273
+
274
+ describe('edge cases and resilience', () => {
275
+ it('should handle network failures gracefully', async () => {
276
+ mockFetch.mockRejectedValue(new Error('Network connection failed'));
277
+
278
+ await expect(client.exposePort(3000, 'session-net')).rejects.toThrow(
279
+ 'Network connection failed'
280
+ );
281
+ });
282
+
283
+ it('should handle malformed server responses', async () => {
284
+ mockFetch.mockResolvedValue(
285
+ new Response('invalid json {', { status: 200 })
286
+ );
287
+
288
+ await expect(client.exposePort(3000, 'session-malform')).rejects.toThrow(
289
+ SandboxError
290
+ );
291
+ });
292
+ });
293
+ });