@cloudflare/sandbox 0.0.9 → 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.
- package/CHANGELOG.md +10 -0
- package/Dockerfile +1 -14
- package/container_src/handler/exec.ts +337 -0
- package/container_src/handler/file.ts +844 -0
- package/container_src/handler/git.ts +182 -0
- package/container_src/handler/ports.ts +314 -0
- package/container_src/handler/process.ts +640 -0
- package/container_src/index.ts +82 -2973
- package/container_src/types.ts +103 -0
- package/dist/chunk-6THNBO4S.js +46 -0
- package/dist/chunk-6THNBO4S.js.map +1 -0
- package/dist/chunk-6UAWTJ5S.js +85 -0
- package/dist/chunk-6UAWTJ5S.js.map +1 -0
- package/dist/chunk-G4XT4SP7.js +638 -0
- package/dist/chunk-G4XT4SP7.js.map +1 -0
- package/dist/chunk-ISFOIYQC.js +585 -0
- package/dist/chunk-ISFOIYQC.js.map +1 -0
- package/dist/chunk-NNGBXDMY.js +89 -0
- package/dist/chunk-NNGBXDMY.js.map +1 -0
- package/dist/client-Da-mLX4p.d.ts +210 -0
- package/dist/client.d.ts +2 -1
- package/dist/client.js +3 -37
- package/dist/index.d.ts +3 -1
- package/dist/index.js +13 -3
- package/dist/request-handler.d.ts +2 -1
- package/dist/request-handler.js +4 -2
- package/dist/sandbox.d.ts +2 -1
- package/dist/sandbox.js +4 -2
- package/dist/security.d.ts +30 -0
- package/dist/security.js +13 -0
- package/dist/security.js.map +1 -0
- package/dist/sse-parser.d.ts +28 -0
- package/dist/sse-parser.js +11 -0
- package/dist/sse-parser.js.map +1 -0
- package/dist/types.d.ts +284 -0
- package/dist/types.js +19 -0
- package/dist/types.js.map +1 -0
- package/package.json +2 -7
- package/src/client.ts +235 -1286
- package/src/index.ts +6 -0
- package/src/request-handler.ts +69 -20
- package/src/sandbox.ts +463 -70
- package/src/security.ts +113 -0
- package/src/sse-parser.ts +147 -0
- package/src/types.ts +386 -0
- package/README.md +0 -65
- package/dist/chunk-4J5LQCCN.js +0 -1446
- package/dist/chunk-4J5LQCCN.js.map +0 -1
- package/dist/chunk-5SZ3RVJZ.js +0 -250
- package/dist/chunk-5SZ3RVJZ.js.map +0 -1
- package/dist/client-BuVjqV00.d.ts +0 -247
- package/tests/client.example.ts +0 -308
- package/tests/connection-test.ts +0 -81
- package/tests/simple-test.ts +0 -81
- package/tests/test1.ts +0 -281
- package/tests/test2.ts +0 -929
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
interface BaseExecOptions {
|
|
2
|
+
/**
|
|
3
|
+
* Session ID for grouping related commands
|
|
4
|
+
*/
|
|
5
|
+
sessionId?: string;
|
|
6
|
+
/**
|
|
7
|
+
* Maximum execution time in milliseconds
|
|
8
|
+
*/
|
|
9
|
+
timeout?: number;
|
|
10
|
+
/**
|
|
11
|
+
* Environment variables for the command
|
|
12
|
+
*/
|
|
13
|
+
env?: Record<string, string>;
|
|
14
|
+
/**
|
|
15
|
+
* Working directory for command execution
|
|
16
|
+
*/
|
|
17
|
+
cwd?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Text encoding for output (default: 'utf8')
|
|
20
|
+
*/
|
|
21
|
+
encoding?: string;
|
|
22
|
+
}
|
|
23
|
+
interface ExecOptions extends BaseExecOptions {
|
|
24
|
+
/**
|
|
25
|
+
* Enable real-time output streaming via callbacks
|
|
26
|
+
*/
|
|
27
|
+
stream?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Callback for real-time output data
|
|
30
|
+
*/
|
|
31
|
+
onOutput?: (stream: 'stdout' | 'stderr', data: string) => void;
|
|
32
|
+
/**
|
|
33
|
+
* Callback when command completes (only when stream: true)
|
|
34
|
+
*/
|
|
35
|
+
onComplete?: (result: ExecResult) => void;
|
|
36
|
+
/**
|
|
37
|
+
* Callback for execution errors
|
|
38
|
+
*/
|
|
39
|
+
onError?: (error: Error) => void;
|
|
40
|
+
/**
|
|
41
|
+
* AbortSignal for cancelling execution
|
|
42
|
+
*/
|
|
43
|
+
signal?: AbortSignal;
|
|
44
|
+
}
|
|
45
|
+
interface ExecResult {
|
|
46
|
+
/**
|
|
47
|
+
* Whether the command succeeded (exitCode === 0)
|
|
48
|
+
*/
|
|
49
|
+
success: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Process exit code
|
|
52
|
+
*/
|
|
53
|
+
exitCode: number;
|
|
54
|
+
/**
|
|
55
|
+
* Standard output content
|
|
56
|
+
*/
|
|
57
|
+
stdout: string;
|
|
58
|
+
/**
|
|
59
|
+
* Standard error content
|
|
60
|
+
*/
|
|
61
|
+
stderr: string;
|
|
62
|
+
/**
|
|
63
|
+
* Command that was executed
|
|
64
|
+
*/
|
|
65
|
+
command: string;
|
|
66
|
+
/**
|
|
67
|
+
* Execution duration in milliseconds
|
|
68
|
+
*/
|
|
69
|
+
duration: number;
|
|
70
|
+
/**
|
|
71
|
+
* ISO timestamp when command started
|
|
72
|
+
*/
|
|
73
|
+
timestamp: string;
|
|
74
|
+
/**
|
|
75
|
+
* Session ID if provided
|
|
76
|
+
*/
|
|
77
|
+
sessionId?: string;
|
|
78
|
+
}
|
|
79
|
+
interface ProcessOptions extends BaseExecOptions {
|
|
80
|
+
/**
|
|
81
|
+
* Custom process ID for later reference
|
|
82
|
+
* If not provided, a UUID will be generated
|
|
83
|
+
*/
|
|
84
|
+
processId?: string;
|
|
85
|
+
/**
|
|
86
|
+
* Automatically cleanup process record after exit (default: true)
|
|
87
|
+
*/
|
|
88
|
+
autoCleanup?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Callback when process exits
|
|
91
|
+
*/
|
|
92
|
+
onExit?: (code: number | null) => void;
|
|
93
|
+
/**
|
|
94
|
+
* Callback for real-time output (background processes)
|
|
95
|
+
*/
|
|
96
|
+
onOutput?: (stream: 'stdout' | 'stderr', data: string) => void;
|
|
97
|
+
/**
|
|
98
|
+
* Callback when process starts successfully
|
|
99
|
+
*/
|
|
100
|
+
onStart?: (process: Process) => void;
|
|
101
|
+
/**
|
|
102
|
+
* Callback for process errors
|
|
103
|
+
*/
|
|
104
|
+
onError?: (error: Error) => void;
|
|
105
|
+
}
|
|
106
|
+
type ProcessStatus = 'starting' | 'running' | 'completed' | 'failed' | 'killed' | 'error';
|
|
107
|
+
interface Process {
|
|
108
|
+
/**
|
|
109
|
+
* Unique process identifier
|
|
110
|
+
*/
|
|
111
|
+
readonly id: string;
|
|
112
|
+
/**
|
|
113
|
+
* System process ID (if available and running)
|
|
114
|
+
*/
|
|
115
|
+
readonly pid?: number;
|
|
116
|
+
/**
|
|
117
|
+
* Command that was executed
|
|
118
|
+
*/
|
|
119
|
+
readonly command: string;
|
|
120
|
+
/**
|
|
121
|
+
* Current process status
|
|
122
|
+
*/
|
|
123
|
+
readonly status: ProcessStatus;
|
|
124
|
+
/**
|
|
125
|
+
* When the process was started
|
|
126
|
+
*/
|
|
127
|
+
readonly startTime: Date;
|
|
128
|
+
/**
|
|
129
|
+
* When the process ended (if completed)
|
|
130
|
+
*/
|
|
131
|
+
readonly endTime?: Date;
|
|
132
|
+
/**
|
|
133
|
+
* Process exit code (if completed)
|
|
134
|
+
*/
|
|
135
|
+
readonly exitCode?: number;
|
|
136
|
+
/**
|
|
137
|
+
* Session ID if provided
|
|
138
|
+
*/
|
|
139
|
+
readonly sessionId?: string;
|
|
140
|
+
/**
|
|
141
|
+
* Kill the process
|
|
142
|
+
*/
|
|
143
|
+
kill(signal?: string): Promise<void>;
|
|
144
|
+
/**
|
|
145
|
+
* Get current process status (refreshed)
|
|
146
|
+
*/
|
|
147
|
+
getStatus(): Promise<ProcessStatus>;
|
|
148
|
+
/**
|
|
149
|
+
* Get accumulated logs
|
|
150
|
+
*/
|
|
151
|
+
getLogs(): Promise<{
|
|
152
|
+
stdout: string;
|
|
153
|
+
stderr: string;
|
|
154
|
+
}>;
|
|
155
|
+
}
|
|
156
|
+
interface ExecEvent {
|
|
157
|
+
type: 'start' | 'stdout' | 'stderr' | 'complete' | 'error';
|
|
158
|
+
timestamp: string;
|
|
159
|
+
data?: string;
|
|
160
|
+
command?: string;
|
|
161
|
+
exitCode?: number;
|
|
162
|
+
result?: ExecResult;
|
|
163
|
+
error?: string;
|
|
164
|
+
sessionId?: string;
|
|
165
|
+
}
|
|
166
|
+
interface LogEvent {
|
|
167
|
+
type: 'stdout' | 'stderr' | 'exit' | 'error';
|
|
168
|
+
timestamp: string;
|
|
169
|
+
data: string;
|
|
170
|
+
processId: string;
|
|
171
|
+
sessionId?: string;
|
|
172
|
+
exitCode?: number;
|
|
173
|
+
}
|
|
174
|
+
interface StreamOptions extends BaseExecOptions {
|
|
175
|
+
/**
|
|
176
|
+
* Buffer size for streaming output
|
|
177
|
+
*/
|
|
178
|
+
bufferSize?: number;
|
|
179
|
+
/**
|
|
180
|
+
* AbortSignal for cancelling stream
|
|
181
|
+
*/
|
|
182
|
+
signal?: AbortSignal;
|
|
183
|
+
}
|
|
184
|
+
declare class SandboxError extends Error {
|
|
185
|
+
code?: string | undefined;
|
|
186
|
+
constructor(message: string, code?: string | undefined);
|
|
187
|
+
}
|
|
188
|
+
declare class ProcessNotFoundError extends SandboxError {
|
|
189
|
+
constructor(processId: string);
|
|
190
|
+
}
|
|
191
|
+
declare class ProcessAlreadyExistsError extends SandboxError {
|
|
192
|
+
constructor(processId: string);
|
|
193
|
+
}
|
|
194
|
+
declare class ExecutionTimeoutError extends SandboxError {
|
|
195
|
+
constructor(timeout: number);
|
|
196
|
+
}
|
|
197
|
+
interface ProcessRecord {
|
|
198
|
+
id: string;
|
|
199
|
+
pid?: number;
|
|
200
|
+
command: string;
|
|
201
|
+
status: ProcessStatus;
|
|
202
|
+
startTime: Date;
|
|
203
|
+
endTime?: Date;
|
|
204
|
+
exitCode?: number;
|
|
205
|
+
sessionId?: string;
|
|
206
|
+
childProcess?: any;
|
|
207
|
+
stdout: string;
|
|
208
|
+
stderr: string;
|
|
209
|
+
outputListeners: Set<(stream: 'stdout' | 'stderr', data: string) => void>;
|
|
210
|
+
statusListeners: Set<(status: ProcessStatus) => void>;
|
|
211
|
+
}
|
|
212
|
+
interface StartProcessRequest {
|
|
213
|
+
command: string;
|
|
214
|
+
options?: {
|
|
215
|
+
processId?: string;
|
|
216
|
+
sessionId?: string;
|
|
217
|
+
timeout?: number;
|
|
218
|
+
env?: Record<string, string>;
|
|
219
|
+
cwd?: string;
|
|
220
|
+
encoding?: string;
|
|
221
|
+
autoCleanup?: boolean;
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
interface StartProcessResponse {
|
|
225
|
+
process: {
|
|
226
|
+
id: string;
|
|
227
|
+
pid?: number;
|
|
228
|
+
command: string;
|
|
229
|
+
status: ProcessStatus;
|
|
230
|
+
startTime: string;
|
|
231
|
+
sessionId?: string;
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
interface ListProcessesResponse {
|
|
235
|
+
processes: Array<{
|
|
236
|
+
id: string;
|
|
237
|
+
pid?: number;
|
|
238
|
+
command: string;
|
|
239
|
+
status: ProcessStatus;
|
|
240
|
+
startTime: string;
|
|
241
|
+
endTime?: string;
|
|
242
|
+
exitCode?: number;
|
|
243
|
+
sessionId?: string;
|
|
244
|
+
}>;
|
|
245
|
+
}
|
|
246
|
+
interface GetProcessResponse {
|
|
247
|
+
process: {
|
|
248
|
+
id: string;
|
|
249
|
+
pid?: number;
|
|
250
|
+
command: string;
|
|
251
|
+
status: ProcessStatus;
|
|
252
|
+
startTime: string;
|
|
253
|
+
endTime?: string;
|
|
254
|
+
exitCode?: number;
|
|
255
|
+
sessionId?: string;
|
|
256
|
+
} | null;
|
|
257
|
+
}
|
|
258
|
+
interface GetProcessLogsResponse {
|
|
259
|
+
stdout: string;
|
|
260
|
+
stderr: string;
|
|
261
|
+
processId: string;
|
|
262
|
+
}
|
|
263
|
+
interface ISandbox {
|
|
264
|
+
exec(command: string, options?: ExecOptions): Promise<ExecResult>;
|
|
265
|
+
startProcess(command: string, options?: ProcessOptions): Promise<Process>;
|
|
266
|
+
listProcesses(): Promise<Process[]>;
|
|
267
|
+
getProcess(id: string): Promise<Process | null>;
|
|
268
|
+
killProcess(id: string, signal?: string): Promise<void>;
|
|
269
|
+
killAllProcesses(): Promise<number>;
|
|
270
|
+
execStream(command: string, options?: StreamOptions): Promise<ReadableStream<Uint8Array>>;
|
|
271
|
+
streamProcessLogs(processId: string, options?: {
|
|
272
|
+
signal?: AbortSignal;
|
|
273
|
+
}): Promise<ReadableStream<Uint8Array>>;
|
|
274
|
+
cleanupCompletedProcesses(): Promise<number>;
|
|
275
|
+
getProcessLogs(id: string): Promise<{
|
|
276
|
+
stdout: string;
|
|
277
|
+
stderr: string;
|
|
278
|
+
}>;
|
|
279
|
+
}
|
|
280
|
+
declare function isExecResult(value: any): value is ExecResult;
|
|
281
|
+
declare function isProcess(value: any): value is Process;
|
|
282
|
+
declare function isProcessStatus(value: string): value is ProcessStatus;
|
|
283
|
+
|
|
284
|
+
export { type BaseExecOptions, type ExecEvent, type ExecOptions, type ExecResult, ExecutionTimeoutError, type GetProcessLogsResponse, type GetProcessResponse, type ISandbox, type ListProcessesResponse, type LogEvent, type Process, ProcessAlreadyExistsError, ProcessNotFoundError, type ProcessOptions, type ProcessRecord, type ProcessStatus, SandboxError, type StartProcessRequest, type StartProcessResponse, type StreamOptions, isExecResult, isProcess, isProcessStatus };
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ExecutionTimeoutError,
|
|
3
|
+
ProcessAlreadyExistsError,
|
|
4
|
+
ProcessNotFoundError,
|
|
5
|
+
SandboxError,
|
|
6
|
+
isExecResult,
|
|
7
|
+
isProcess,
|
|
8
|
+
isProcessStatus
|
|
9
|
+
} from "./chunk-6THNBO4S.js";
|
|
10
|
+
export {
|
|
11
|
+
ExecutionTimeoutError,
|
|
12
|
+
ProcessAlreadyExistsError,
|
|
13
|
+
ProcessNotFoundError,
|
|
14
|
+
SandboxError,
|
|
15
|
+
isExecResult,
|
|
16
|
+
isProcess,
|
|
17
|
+
isProcessStatus
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudflare/sandbox",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/cloudflare/sandbox-sdk"
|
|
7
7
|
},
|
|
8
8
|
"description": "A sandboxed environment for running commands",
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@cloudflare/containers": "^0.0.
|
|
10
|
+
"@cloudflare/containers": "^0.0.24"
|
|
11
11
|
},
|
|
12
12
|
"tags": [
|
|
13
13
|
"sandbox",
|
|
@@ -26,11 +26,6 @@
|
|
|
26
26
|
"types": "./dist/index.d.ts",
|
|
27
27
|
"import": "./dist/index.js",
|
|
28
28
|
"require": "./dist/index.js"
|
|
29
|
-
},
|
|
30
|
-
"./client": {
|
|
31
|
-
"types": "./dist/client.d.ts",
|
|
32
|
-
"import": "./dist/client.js",
|
|
33
|
-
"require": "./dist/client.js"
|
|
34
29
|
}
|
|
35
30
|
},
|
|
36
31
|
"keywords": [],
|