@cloudflare/sandbox 0.0.8 → 0.1.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 (56) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/Dockerfile +73 -9
  3. package/container_src/handler/exec.ts +337 -0
  4. package/container_src/handler/file.ts +844 -0
  5. package/container_src/handler/git.ts +182 -0
  6. package/container_src/handler/ports.ts +314 -0
  7. package/container_src/handler/process.ts +640 -0
  8. package/container_src/index.ts +102 -2647
  9. package/container_src/types.ts +103 -0
  10. package/dist/chunk-6THNBO4S.js +46 -0
  11. package/dist/chunk-6THNBO4S.js.map +1 -0
  12. package/dist/chunk-6UAWTJ5S.js +85 -0
  13. package/dist/chunk-6UAWTJ5S.js.map +1 -0
  14. package/dist/chunk-G4XT4SP7.js +638 -0
  15. package/dist/chunk-G4XT4SP7.js.map +1 -0
  16. package/dist/chunk-ISFOIYQC.js +585 -0
  17. package/dist/chunk-ISFOIYQC.js.map +1 -0
  18. package/dist/chunk-NNGBXDMY.js +89 -0
  19. package/dist/chunk-NNGBXDMY.js.map +1 -0
  20. package/dist/client-Da-mLX4p.d.ts +210 -0
  21. package/dist/client.d.ts +2 -1
  22. package/dist/client.js +3 -37
  23. package/dist/index.d.ts +5 -200
  24. package/dist/index.js +17 -106
  25. package/dist/index.js.map +1 -1
  26. package/dist/request-handler.d.ts +16 -0
  27. package/dist/request-handler.js +12 -0
  28. package/dist/request-handler.js.map +1 -0
  29. package/dist/sandbox.d.ts +3 -0
  30. package/dist/sandbox.js +12 -0
  31. package/dist/sandbox.js.map +1 -0
  32. package/dist/security.d.ts +30 -0
  33. package/dist/security.js +13 -0
  34. package/dist/security.js.map +1 -0
  35. package/dist/sse-parser.d.ts +28 -0
  36. package/dist/sse-parser.js +11 -0
  37. package/dist/sse-parser.js.map +1 -0
  38. package/dist/types.d.ts +284 -0
  39. package/dist/types.js +19 -0
  40. package/dist/types.js.map +1 -0
  41. package/package.json +2 -7
  42. package/src/client.ts +320 -1242
  43. package/src/index.ts +20 -136
  44. package/src/request-handler.ts +144 -0
  45. package/src/sandbox.ts +645 -0
  46. package/src/security.ts +113 -0
  47. package/src/sse-parser.ts +147 -0
  48. package/src/types.ts +386 -0
  49. package/README.md +0 -65
  50. package/dist/chunk-7WZJ3TRE.js +0 -1364
  51. package/dist/chunk-7WZJ3TRE.js.map +0 -1
  52. package/tests/client.example.ts +0 -308
  53. package/tests/connection-test.ts +0 -81
  54. package/tests/simple-test.ts +0 -81
  55. package/tests/test1.ts +0 -281
  56. package/tests/test2.ts +0 -929
package/tests/test1.ts DELETED
@@ -1,281 +0,0 @@
1
- import { HttpClient } from "../../sandbox/src/client";
2
-
3
- interface ExecuteRequest {
4
- command: string;
5
- args?: string[];
6
- }
7
-
8
- class HttpCommandTester {
9
- private client: HttpClient;
10
- private sessionId: string | null = null;
11
-
12
- constructor(private baseUrl: string) {
13
- this.client = new HttpClient({
14
- baseUrl,
15
- onCommandComplete: (
16
- success: boolean,
17
- exitCode: number,
18
- stdout: string,
19
- stderr: string,
20
- command: string,
21
- args: string[]
22
- ) => {
23
- const successIcon = success ? "✅" : "❌";
24
- console.log(
25
- `${successIcon} Command completed with exit code: ${exitCode}`
26
- );
27
- if (stderr) {
28
- console.log(`❌ Final stderr: ${stderr.trim()}`);
29
- }
30
- },
31
- onCommandStart: (command: string, args: string[]) => {
32
- console.log(`🚀 Starting command: ${command} ${args.join(" ")}`);
33
- },
34
- onError: (error: string, command?: string, args?: string[]) => {
35
- console.error(`❌ Error: ${error}`);
36
- },
37
- onOutput: (
38
- stream: "stdout" | "stderr",
39
- data: string,
40
- command: string
41
- ) => {
42
- const streamLabel = stream === "stderr" ? "❌ STDERR" : "📤 STDOUT";
43
- console.log(`${streamLabel}: ${data.trim()}`);
44
- },
45
- onStreamEvent: (event) => {
46
- console.log(`📡 Stream event: ${event.type}`);
47
- },
48
- });
49
- }
50
-
51
- async connect(): Promise<void> {
52
- try {
53
- // Test ping to verify server is reachable
54
- console.log("🏓 Testing ping...");
55
- const pingResult = await this.client.ping();
56
- console.log("✅ Ping successful:", pingResult);
57
-
58
- // Create a session
59
- console.log("🔗 Creating session...");
60
- this.sessionId = await this.client.createSession();
61
- console.log("✅ Session created:", this.sessionId);
62
- } catch (error) {
63
- console.error("❌ Failed to connect:", error);
64
- throw error;
65
- }
66
- }
67
-
68
- async executeCommand(command: string, args: string[] = []): Promise<void> {
69
- console.log(`\n🔧 Executing: ${command} ${args.join(" ")}`);
70
-
71
- try {
72
- const result = await this.client.execute(
73
- command,
74
- args,
75
- this.sessionId || undefined
76
- );
77
- console.log(`✅ Command executed successfully`);
78
- } catch (error) {
79
- console.error(`❌ Command execution failed:`, error);
80
- }
81
- }
82
-
83
- async executeStreamingCommand(
84
- command: string,
85
- args: string[] = []
86
- ): Promise<void> {
87
- console.log(`\n🔧 Executing streaming: ${command} ${args.join(" ")}`);
88
-
89
- try {
90
- await this.client.executeStream(
91
- command,
92
- args,
93
- this.sessionId || undefined
94
- );
95
- console.log(`✅ Streaming command completed`);
96
- } catch (error) {
97
- console.error(`❌ Streaming command failed:`, error);
98
- }
99
- }
100
-
101
- async ping(): Promise<void> {
102
- console.log("\n🏓 Sending ping...");
103
- try {
104
- const result = await this.client.ping();
105
- console.log(`✅ Ping successful: ${result}`);
106
- } catch (error) {
107
- console.error(`❌ Ping failed:`, error);
108
- }
109
- }
110
-
111
- async listCommands(): Promise<void> {
112
- console.log("\n📋 Requesting available commands...");
113
- try {
114
- const commands = await this.client.getCommands();
115
- console.log(`✅ Available commands: ${commands.join(", ")}`);
116
- } catch (error) {
117
- console.error(`❌ Failed to get commands:`, error);
118
- }
119
- }
120
-
121
- async listSessions(): Promise<void> {
122
- console.log("\n📝 Listing sessions...");
123
- try {
124
- const sessions = await this.client.listSessions();
125
- console.log(`✅ Active sessions: ${sessions.count}`);
126
- sessions.sessions.forEach((session) => {
127
- console.log(
128
- ` - ${session.sessionId} (active: ${session.hasActiveProcess})`
129
- );
130
- });
131
- } catch (error) {
132
- console.error(`❌ Failed to list sessions:`, error);
133
- }
134
- }
135
-
136
- async testDangerousCommand(): Promise<void> {
137
- console.log("\n⚠️ Testing dangerous command protection...");
138
- try {
139
- await this.client.execute(
140
- "rm",
141
- ["-rf", "/"],
142
- this.sessionId || undefined
143
- );
144
- } catch (error) {
145
- console.log("✅ Dangerous command correctly blocked");
146
- }
147
- }
148
-
149
- async testInvalidCommand(): Promise<void> {
150
- console.log("\n❓ Testing invalid command...");
151
- try {
152
- await this.client.execute(
153
- "nonexistentcommand12345",
154
- [],
155
- this.sessionId || undefined
156
- );
157
- } catch (error) {
158
- console.log("✅ Invalid command handled gracefully");
159
- }
160
- }
161
-
162
- async testLongRunningCommand(): Promise<void> {
163
- console.log("\n⏱️ Testing long-running command...");
164
- try {
165
- await this.client.execute("sleep", ["3"], this.sessionId || undefined);
166
- console.log("✅ Long-running command completed");
167
- } catch (error) {
168
- console.error(`❌ Long-running command failed:`, error);
169
- }
170
- }
171
-
172
- async testStreamingCommand(): Promise<void> {
173
- console.log("\n📡 Testing streaming command...");
174
- try {
175
- await this.client.executeStream(
176
- "ls",
177
- ["-la"],
178
- this.sessionId || undefined
179
- );
180
- console.log("✅ Streaming command completed");
181
- } catch (error) {
182
- console.error(`❌ Streaming command failed:`, error);
183
- }
184
- }
185
-
186
- async testQuickExecute(): Promise<void> {
187
- console.log("\n⚡ Testing quick execute...");
188
- try {
189
- const { quickExecute } = await import("../../sandbox/src/client");
190
- const result = await quickExecute("echo", ["Hello from quick execute!"]);
191
- console.log(`✅ Quick execute result: ${result.stdout.trim()}`);
192
- } catch (error) {
193
- console.error(`❌ Quick execute failed:`, error);
194
- }
195
- }
196
-
197
- async testQuickExecuteStream(): Promise<void> {
198
- console.log("\n⚡ Testing quick execute stream...");
199
- try {
200
- const { quickExecuteStream } = await import("../../sandbox/src/client");
201
- await quickExecuteStream("echo", ["Hello from quick execute stream!"]);
202
- console.log("✅ Quick execute stream completed");
203
- } catch (error) {
204
- console.error(`❌ Quick execute stream failed:`, error);
205
- }
206
- }
207
-
208
- disconnect(): void {
209
- if (this.client) {
210
- this.client.clearSession();
211
- console.log("🔌 Session cleared");
212
- }
213
- }
214
- }
215
-
216
- async function runTests(): Promise<void> {
217
- const tester = new HttpCommandTester("http://127.0.0.1:3000");
218
-
219
- try {
220
- console.log("🚀 Starting HTTP command execution tests...\n");
221
-
222
- // Connect to the server
223
- await tester.connect();
224
-
225
- // Wait a moment for connection to stabilize
226
- await new Promise((resolve) => setTimeout(resolve, 500));
227
-
228
- // Test 1: List available commands
229
- await tester.listCommands();
230
-
231
- // Test 2: List sessions
232
- await tester.listSessions();
233
-
234
- // Test 3: Ping the server
235
- await tester.ping();
236
-
237
- // Test 4: Simple echo command
238
- await tester.executeCommand("echo", ["Hello from HTTP!"]);
239
-
240
- // Test 5: List current directory
241
- await tester.executeCommand("ls", ["-la"]);
242
-
243
- // Test 6: Get current working directory
244
- await tester.executeCommand("pwd");
245
-
246
- // Test 7: Check system info
247
- await tester.executeCommand("uname", ["-a"]);
248
-
249
- // Test 8: Test streaming command
250
- await tester.testStreamingCommand();
251
-
252
- // Test 9: Test quick execute
253
- await tester.testQuickExecute();
254
-
255
- // Test 10: Test quick execute stream
256
- await tester.testQuickExecuteStream();
257
-
258
- // Test 11: Test dangerous command protection
259
- await tester.testDangerousCommand();
260
-
261
- // Test 12: Test invalid command
262
- await tester.testInvalidCommand();
263
-
264
- // Test 13: Test long-running command
265
- await tester.testLongRunningCommand();
266
-
267
- console.log("\n✅ All tests completed!");
268
- } catch (error) {
269
- console.error("❌ Test failed:", error);
270
- } finally {
271
- // Clean up
272
- setTimeout(() => {
273
- tester.disconnect();
274
- console.log("\n🔌 Test completed, disconnecting...");
275
- process.exit(0);
276
- }, 1000);
277
- }
278
- }
279
-
280
- // Run the tests
281
- runTests().catch(console.error);