@cloudflare/sandbox 0.0.9 → 0.1.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 (57) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/Dockerfile +1 -14
  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 +82 -2973
  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 +3 -1
  24. package/dist/index.js +13 -3
  25. package/dist/request-handler.d.ts +2 -1
  26. package/dist/request-handler.js +4 -2
  27. package/dist/sandbox.d.ts +2 -1
  28. package/dist/sandbox.js +4 -2
  29. package/dist/security.d.ts +30 -0
  30. package/dist/security.js +13 -0
  31. package/dist/security.js.map +1 -0
  32. package/dist/sse-parser.d.ts +28 -0
  33. package/dist/sse-parser.js +11 -0
  34. package/dist/sse-parser.js.map +1 -0
  35. package/dist/types.d.ts +284 -0
  36. package/dist/types.js +19 -0
  37. package/dist/types.js.map +1 -0
  38. package/package.json +2 -7
  39. package/src/client.ts +235 -1286
  40. package/src/index.ts +6 -0
  41. package/src/request-handler.ts +69 -20
  42. package/src/sandbox.ts +463 -70
  43. package/src/security.ts +113 -0
  44. package/src/sse-parser.ts +147 -0
  45. package/src/types.ts +386 -0
  46. package/tsconfig.json +1 -1
  47. package/README.md +0 -65
  48. package/dist/chunk-4J5LQCCN.js +0 -1446
  49. package/dist/chunk-4J5LQCCN.js.map +0 -1
  50. package/dist/chunk-5SZ3RVJZ.js +0 -250
  51. package/dist/chunk-5SZ3RVJZ.js.map +0 -1
  52. package/dist/client-BuVjqV00.d.ts +0 -247
  53. package/tests/client.example.ts +0 -308
  54. package/tests/connection-test.ts +0 -81
  55. package/tests/simple-test.ts +0 -81
  56. package/tests/test1.ts +0 -281
  57. package/tests/test2.ts +0 -929
@@ -1,81 +0,0 @@
1
- import {
2
- HttpClient,
3
- quickExecute,
4
- quickExecuteStream,
5
- } from "../../sandbox/src/client";
6
-
7
- async function testHttpClient() {
8
- console.log("šŸš€ Testing HTTP Client...\n");
9
-
10
- // Create a client instance
11
- const client = new HttpClient({
12
- baseUrl: "http://localhost:3000",
13
- onCommandComplete: (success, exitCode, stdout, stderr, command, args) => {
14
- console.log(
15
- `āœ… Command completed: ${command}, Success: ${success}, Exit code: ${exitCode}`
16
- );
17
- },
18
- onCommandStart: (command, args) => {
19
- console.log(`šŸ“ Command started: ${command} ${args.join(" ")}`);
20
- },
21
- onError: (error, command, args) => {
22
- console.error(`āŒ Command error: ${error}`);
23
- },
24
- onOutput: (stream, data, command) => {
25
- console.log(`šŸ“¤ [${stream}] ${data.trim()}`);
26
- },
27
- });
28
-
29
- try {
30
- // Test ping
31
- console.log("1. Testing ping...");
32
- const pingTime = await client.ping();
33
- console.log(` Ping response time: ${pingTime}\n`);
34
-
35
- // Test getting available commands
36
- console.log("2. Testing get commands...");
37
- const commands = await client.getCommands();
38
- console.log(` Available commands: ${commands.join(", ")}\n`);
39
-
40
- // Test session creation
41
- console.log("3. Testing session creation...");
42
- const sessionId = await client.createSession();
43
- console.log(` Created session: ${sessionId}\n`);
44
-
45
- // Test listing sessions
46
- console.log("4. Testing session listing...");
47
- const sessions = await client.listSessions();
48
- console.log(` Active sessions: ${sessions.count}\n`);
49
-
50
- // Test regular command execution
51
- console.log("5. Testing regular command execution...");
52
- const result = await client.execute("echo", ["Hello from HTTP client!"]);
53
- console.log(` Command result: ${result.stdout.trim()}\n`);
54
-
55
- // Test another command
56
- console.log("6. Testing another command...");
57
- const pwdResult = await client.execute("pwd");
58
- console.log(` Current directory: ${pwdResult.stdout.trim()}\n`);
59
-
60
- // Test streaming command execution
61
- console.log("7. Testing streaming command execution...");
62
- await client.executeStream("ls", ["-la"]);
63
- console.log(" Streaming command completed\n");
64
-
65
- // Test quick execute utility
66
- console.log("8. Testing quick execute utility...");
67
- const quickResult = await quickExecute("whoami");
68
- console.log(` Quick execute result: ${quickResult.stdout.trim()}\n`);
69
-
70
- console.log("šŸŽ‰ All tests completed successfully!");
71
- } catch (error) {
72
- console.error("āŒ Test failed:", error);
73
- }
74
- }
75
-
76
- // Run the test if this file is executed directly
77
- if (import.meta.main) {
78
- testHttpClient().catch(console.error);
79
- }
80
-
81
- export { testHttpClient };
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);