@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.
- package/CHANGELOG.md +16 -0
- package/Dockerfile +73 -9
- 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 +102 -2647
- 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 +5 -200
- package/dist/index.js +17 -106
- package/dist/index.js.map +1 -1
- package/dist/request-handler.d.ts +16 -0
- package/dist/request-handler.js +12 -0
- package/dist/request-handler.js.map +1 -0
- package/dist/sandbox.d.ts +3 -0
- package/dist/sandbox.js +12 -0
- package/dist/sandbox.js.map +1 -0
- 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 +320 -1242
- package/src/index.ts +20 -136
- package/src/request-handler.ts +144 -0
- package/src/sandbox.ts +645 -0
- 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-7WZJ3TRE.js +0 -1364
- package/dist/chunk-7WZJ3TRE.js.map +0 -1
- 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
|
@@ -0,0 +1,638 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
var HttpClient = class {
|
|
3
|
+
baseUrl;
|
|
4
|
+
options;
|
|
5
|
+
sessionId = null;
|
|
6
|
+
constructor(options = {}) {
|
|
7
|
+
this.options = {
|
|
8
|
+
...options
|
|
9
|
+
};
|
|
10
|
+
this.baseUrl = this.options.baseUrl;
|
|
11
|
+
}
|
|
12
|
+
async doFetch(path, options) {
|
|
13
|
+
const url = this.options.stub ? `http://localhost:${this.options.port}${path}` : `${this.baseUrl}${path}`;
|
|
14
|
+
const method = options?.method || "GET";
|
|
15
|
+
console.log(`[HTTP Client] Making ${method} request to ${url}`);
|
|
16
|
+
try {
|
|
17
|
+
let response;
|
|
18
|
+
if (this.options.stub) {
|
|
19
|
+
response = await this.options.stub.containerFetch(
|
|
20
|
+
url,
|
|
21
|
+
options,
|
|
22
|
+
this.options.port
|
|
23
|
+
);
|
|
24
|
+
} else {
|
|
25
|
+
response = await fetch(url, options);
|
|
26
|
+
}
|
|
27
|
+
console.log(
|
|
28
|
+
`[HTTP Client] Response: ${response.status} ${response.statusText}`
|
|
29
|
+
);
|
|
30
|
+
if (!response.ok) {
|
|
31
|
+
console.error(
|
|
32
|
+
`[HTTP Client] Request failed: ${method} ${url} - ${response.status} ${response.statusText}`
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
return response;
|
|
36
|
+
} catch (error) {
|
|
37
|
+
console.error(`[HTTP Client] Request error: ${method} ${url}`, error);
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async execute(command, sessionId) {
|
|
42
|
+
try {
|
|
43
|
+
const targetSessionId = sessionId || this.sessionId;
|
|
44
|
+
const response = await this.doFetch(`/api/execute`, {
|
|
45
|
+
body: JSON.stringify({
|
|
46
|
+
command,
|
|
47
|
+
sessionId: targetSessionId
|
|
48
|
+
}),
|
|
49
|
+
headers: {
|
|
50
|
+
"Content-Type": "application/json"
|
|
51
|
+
},
|
|
52
|
+
method: "POST"
|
|
53
|
+
});
|
|
54
|
+
if (!response.ok) {
|
|
55
|
+
const errorData = await response.json().catch(() => ({}));
|
|
56
|
+
throw new Error(
|
|
57
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
const data = await response.json();
|
|
61
|
+
console.log(
|
|
62
|
+
`[HTTP Client] Command executed: ${command}, Success: ${data.success}`
|
|
63
|
+
);
|
|
64
|
+
this.options.onCommandComplete?.(
|
|
65
|
+
data.success,
|
|
66
|
+
data.exitCode,
|
|
67
|
+
data.stdout,
|
|
68
|
+
data.stderr,
|
|
69
|
+
data.command
|
|
70
|
+
);
|
|
71
|
+
return data;
|
|
72
|
+
} catch (error) {
|
|
73
|
+
console.error("[HTTP Client] Error executing command:", error);
|
|
74
|
+
this.options.onError?.(
|
|
75
|
+
error instanceof Error ? error.message : "Unknown error",
|
|
76
|
+
command
|
|
77
|
+
);
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async executeCommandStream(command, sessionId) {
|
|
82
|
+
try {
|
|
83
|
+
const targetSessionId = sessionId || this.sessionId;
|
|
84
|
+
const response = await this.doFetch(`/api/execute/stream`, {
|
|
85
|
+
body: JSON.stringify({
|
|
86
|
+
command,
|
|
87
|
+
sessionId: targetSessionId
|
|
88
|
+
}),
|
|
89
|
+
headers: {
|
|
90
|
+
"Content-Type": "application/json",
|
|
91
|
+
"Accept": "text/event-stream"
|
|
92
|
+
},
|
|
93
|
+
method: "POST"
|
|
94
|
+
});
|
|
95
|
+
if (!response.ok) {
|
|
96
|
+
const errorData = await response.json().catch(() => ({}));
|
|
97
|
+
throw new Error(
|
|
98
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
if (!response.body) {
|
|
102
|
+
throw new Error("No response body for streaming request");
|
|
103
|
+
}
|
|
104
|
+
console.log(
|
|
105
|
+
`[HTTP Client] Started command stream: ${command}`
|
|
106
|
+
);
|
|
107
|
+
return response.body;
|
|
108
|
+
} catch (error) {
|
|
109
|
+
console.error("[HTTP Client] Error in command stream:", error);
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async gitCheckout(repoUrl, branch = "main", targetDir, sessionId) {
|
|
114
|
+
try {
|
|
115
|
+
const targetSessionId = sessionId || this.sessionId;
|
|
116
|
+
const response = await this.doFetch(`/api/git/checkout`, {
|
|
117
|
+
body: JSON.stringify({
|
|
118
|
+
branch,
|
|
119
|
+
repoUrl,
|
|
120
|
+
sessionId: targetSessionId,
|
|
121
|
+
targetDir
|
|
122
|
+
}),
|
|
123
|
+
headers: {
|
|
124
|
+
"Content-Type": "application/json"
|
|
125
|
+
},
|
|
126
|
+
method: "POST"
|
|
127
|
+
});
|
|
128
|
+
if (!response.ok) {
|
|
129
|
+
const errorData = await response.json().catch(() => ({}));
|
|
130
|
+
throw new Error(
|
|
131
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
const data = await response.json();
|
|
135
|
+
console.log(
|
|
136
|
+
`[HTTP Client] Git checkout completed: ${repoUrl}, Success: ${data.success}, Target: ${data.targetDir}`
|
|
137
|
+
);
|
|
138
|
+
return data;
|
|
139
|
+
} catch (error) {
|
|
140
|
+
console.error("[HTTP Client] Error in git checkout:", error);
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async mkdir(path, recursive = false, sessionId) {
|
|
145
|
+
try {
|
|
146
|
+
const targetSessionId = sessionId || this.sessionId;
|
|
147
|
+
const response = await this.doFetch(`/api/mkdir`, {
|
|
148
|
+
body: JSON.stringify({
|
|
149
|
+
path,
|
|
150
|
+
recursive,
|
|
151
|
+
sessionId: targetSessionId
|
|
152
|
+
}),
|
|
153
|
+
headers: {
|
|
154
|
+
"Content-Type": "application/json"
|
|
155
|
+
},
|
|
156
|
+
method: "POST"
|
|
157
|
+
});
|
|
158
|
+
if (!response.ok) {
|
|
159
|
+
const errorData = await response.json().catch(() => ({}));
|
|
160
|
+
throw new Error(
|
|
161
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
const data = await response.json();
|
|
165
|
+
console.log(
|
|
166
|
+
`[HTTP Client] Directory created: ${path}, Success: ${data.success}, Recursive: ${data.recursive}`
|
|
167
|
+
);
|
|
168
|
+
return data;
|
|
169
|
+
} catch (error) {
|
|
170
|
+
console.error("[HTTP Client] Error creating directory:", error);
|
|
171
|
+
throw error;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
async writeFile(path, content, encoding = "utf-8", sessionId) {
|
|
175
|
+
try {
|
|
176
|
+
const targetSessionId = sessionId || this.sessionId;
|
|
177
|
+
const response = await this.doFetch(`/api/write`, {
|
|
178
|
+
body: JSON.stringify({
|
|
179
|
+
content,
|
|
180
|
+
encoding,
|
|
181
|
+
path,
|
|
182
|
+
sessionId: targetSessionId
|
|
183
|
+
}),
|
|
184
|
+
headers: {
|
|
185
|
+
"Content-Type": "application/json"
|
|
186
|
+
},
|
|
187
|
+
method: "POST"
|
|
188
|
+
});
|
|
189
|
+
if (!response.ok) {
|
|
190
|
+
const errorData = await response.json().catch(() => ({}));
|
|
191
|
+
throw new Error(
|
|
192
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
const data = await response.json();
|
|
196
|
+
console.log(
|
|
197
|
+
`[HTTP Client] File written: ${path}, Success: ${data.success}`
|
|
198
|
+
);
|
|
199
|
+
return data;
|
|
200
|
+
} catch (error) {
|
|
201
|
+
console.error("[HTTP Client] Error writing file:", error);
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
async readFile(path, encoding = "utf-8", sessionId) {
|
|
206
|
+
try {
|
|
207
|
+
const targetSessionId = sessionId || this.sessionId;
|
|
208
|
+
const response = await this.doFetch(`/api/read`, {
|
|
209
|
+
body: JSON.stringify({
|
|
210
|
+
encoding,
|
|
211
|
+
path,
|
|
212
|
+
sessionId: targetSessionId
|
|
213
|
+
}),
|
|
214
|
+
headers: {
|
|
215
|
+
"Content-Type": "application/json"
|
|
216
|
+
},
|
|
217
|
+
method: "POST"
|
|
218
|
+
});
|
|
219
|
+
if (!response.ok) {
|
|
220
|
+
const errorData = await response.json().catch(() => ({}));
|
|
221
|
+
throw new Error(
|
|
222
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
const data = await response.json();
|
|
226
|
+
console.log(
|
|
227
|
+
`[HTTP Client] File read: ${path}, Success: ${data.success}, Content length: ${data.content.length}`
|
|
228
|
+
);
|
|
229
|
+
return data;
|
|
230
|
+
} catch (error) {
|
|
231
|
+
console.error("[HTTP Client] Error reading file:", error);
|
|
232
|
+
throw error;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
async deleteFile(path, sessionId) {
|
|
236
|
+
try {
|
|
237
|
+
const targetSessionId = sessionId || this.sessionId;
|
|
238
|
+
const response = await this.doFetch(`/api/delete`, {
|
|
239
|
+
body: JSON.stringify({
|
|
240
|
+
path,
|
|
241
|
+
sessionId: targetSessionId
|
|
242
|
+
}),
|
|
243
|
+
headers: {
|
|
244
|
+
"Content-Type": "application/json"
|
|
245
|
+
},
|
|
246
|
+
method: "POST"
|
|
247
|
+
});
|
|
248
|
+
if (!response.ok) {
|
|
249
|
+
const errorData = await response.json().catch(() => ({}));
|
|
250
|
+
throw new Error(
|
|
251
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
const data = await response.json();
|
|
255
|
+
console.log(
|
|
256
|
+
`[HTTP Client] File deleted: ${path}, Success: ${data.success}`
|
|
257
|
+
);
|
|
258
|
+
return data;
|
|
259
|
+
} catch (error) {
|
|
260
|
+
console.error("[HTTP Client] Error deleting file:", error);
|
|
261
|
+
throw error;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
async renameFile(oldPath, newPath, sessionId) {
|
|
265
|
+
try {
|
|
266
|
+
const targetSessionId = sessionId || this.sessionId;
|
|
267
|
+
const response = await this.doFetch(`/api/rename`, {
|
|
268
|
+
body: JSON.stringify({
|
|
269
|
+
newPath,
|
|
270
|
+
oldPath,
|
|
271
|
+
sessionId: targetSessionId
|
|
272
|
+
}),
|
|
273
|
+
headers: {
|
|
274
|
+
"Content-Type": "application/json"
|
|
275
|
+
},
|
|
276
|
+
method: "POST"
|
|
277
|
+
});
|
|
278
|
+
if (!response.ok) {
|
|
279
|
+
const errorData = await response.json().catch(() => ({}));
|
|
280
|
+
throw new Error(
|
|
281
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
const data = await response.json();
|
|
285
|
+
console.log(
|
|
286
|
+
`[HTTP Client] File renamed: ${oldPath} -> ${newPath}, Success: ${data.success}`
|
|
287
|
+
);
|
|
288
|
+
return data;
|
|
289
|
+
} catch (error) {
|
|
290
|
+
console.error("[HTTP Client] Error renaming file:", error);
|
|
291
|
+
throw error;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
async moveFile(sourcePath, destinationPath, sessionId) {
|
|
295
|
+
try {
|
|
296
|
+
const targetSessionId = sessionId || this.sessionId;
|
|
297
|
+
const response = await this.doFetch(`/api/move`, {
|
|
298
|
+
body: JSON.stringify({
|
|
299
|
+
destinationPath,
|
|
300
|
+
sessionId: targetSessionId,
|
|
301
|
+
sourcePath
|
|
302
|
+
}),
|
|
303
|
+
headers: {
|
|
304
|
+
"Content-Type": "application/json"
|
|
305
|
+
},
|
|
306
|
+
method: "POST"
|
|
307
|
+
});
|
|
308
|
+
if (!response.ok) {
|
|
309
|
+
const errorData = await response.json().catch(() => ({}));
|
|
310
|
+
throw new Error(
|
|
311
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
const data = await response.json();
|
|
315
|
+
console.log(
|
|
316
|
+
`[HTTP Client] File moved: ${sourcePath} -> ${destinationPath}, Success: ${data.success}`
|
|
317
|
+
);
|
|
318
|
+
return data;
|
|
319
|
+
} catch (error) {
|
|
320
|
+
console.error("[HTTP Client] Error moving file:", error);
|
|
321
|
+
throw error;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
async exposePort(port, name) {
|
|
325
|
+
try {
|
|
326
|
+
const response = await this.doFetch(`/api/expose-port`, {
|
|
327
|
+
body: JSON.stringify({
|
|
328
|
+
port,
|
|
329
|
+
name
|
|
330
|
+
}),
|
|
331
|
+
headers: {
|
|
332
|
+
"Content-Type": "application/json"
|
|
333
|
+
},
|
|
334
|
+
method: "POST"
|
|
335
|
+
});
|
|
336
|
+
if (!response.ok) {
|
|
337
|
+
const errorData = await response.json().catch(() => ({}));
|
|
338
|
+
console.log(errorData);
|
|
339
|
+
throw new Error(
|
|
340
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
const data = await response.json();
|
|
344
|
+
console.log(
|
|
345
|
+
`[HTTP Client] Port exposed: ${port}${name ? ` (${name})` : ""}, Success: ${data.success}`
|
|
346
|
+
);
|
|
347
|
+
return data;
|
|
348
|
+
} catch (error) {
|
|
349
|
+
console.error("[HTTP Client] Error exposing port:", error);
|
|
350
|
+
throw error;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
async unexposePort(port) {
|
|
354
|
+
try {
|
|
355
|
+
const response = await this.doFetch(`/api/unexpose-port`, {
|
|
356
|
+
body: JSON.stringify({
|
|
357
|
+
port
|
|
358
|
+
}),
|
|
359
|
+
headers: {
|
|
360
|
+
"Content-Type": "application/json"
|
|
361
|
+
},
|
|
362
|
+
method: "DELETE"
|
|
363
|
+
});
|
|
364
|
+
if (!response.ok) {
|
|
365
|
+
const errorData = await response.json().catch(() => ({}));
|
|
366
|
+
throw new Error(
|
|
367
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
const data = await response.json();
|
|
371
|
+
console.log(
|
|
372
|
+
`[HTTP Client] Port unexposed: ${port}, Success: ${data.success}`
|
|
373
|
+
);
|
|
374
|
+
return data;
|
|
375
|
+
} catch (error) {
|
|
376
|
+
console.error("[HTTP Client] Error unexposing port:", error);
|
|
377
|
+
throw error;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
async getExposedPorts() {
|
|
381
|
+
try {
|
|
382
|
+
const response = await this.doFetch(`/api/exposed-ports`, {
|
|
383
|
+
headers: {
|
|
384
|
+
"Content-Type": "application/json"
|
|
385
|
+
},
|
|
386
|
+
method: "GET"
|
|
387
|
+
});
|
|
388
|
+
if (!response.ok) {
|
|
389
|
+
const errorData = await response.json().catch(() => ({}));
|
|
390
|
+
throw new Error(
|
|
391
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
const data = await response.json();
|
|
395
|
+
console.log(
|
|
396
|
+
`[HTTP Client] Got ${data.count} exposed ports`
|
|
397
|
+
);
|
|
398
|
+
return data;
|
|
399
|
+
} catch (error) {
|
|
400
|
+
console.error("[HTTP Client] Error getting exposed ports:", error);
|
|
401
|
+
throw error;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
async ping() {
|
|
405
|
+
try {
|
|
406
|
+
const response = await this.doFetch(`/api/ping`, {
|
|
407
|
+
headers: {
|
|
408
|
+
"Content-Type": "application/json"
|
|
409
|
+
},
|
|
410
|
+
method: "GET"
|
|
411
|
+
});
|
|
412
|
+
if (!response.ok) {
|
|
413
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
414
|
+
}
|
|
415
|
+
const data = await response.json();
|
|
416
|
+
console.log(`[HTTP Client] Ping response: ${data.message}`);
|
|
417
|
+
return data.timestamp;
|
|
418
|
+
} catch (error) {
|
|
419
|
+
console.error("[HTTP Client] Error pinging server:", error);
|
|
420
|
+
throw error;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
async getCommands() {
|
|
424
|
+
try {
|
|
425
|
+
const response = await fetch(`${this.baseUrl}/api/commands`, {
|
|
426
|
+
headers: {
|
|
427
|
+
"Content-Type": "application/json"
|
|
428
|
+
},
|
|
429
|
+
method: "GET"
|
|
430
|
+
});
|
|
431
|
+
if (!response.ok) {
|
|
432
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
433
|
+
}
|
|
434
|
+
const data = await response.json();
|
|
435
|
+
console.log(
|
|
436
|
+
`[HTTP Client] Available commands: ${data.availableCommands.length}`
|
|
437
|
+
);
|
|
438
|
+
return data.availableCommands;
|
|
439
|
+
} catch (error) {
|
|
440
|
+
console.error("[HTTP Client] Error getting commands:", error);
|
|
441
|
+
throw error;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
getSessionId() {
|
|
445
|
+
return this.sessionId;
|
|
446
|
+
}
|
|
447
|
+
setSessionId(sessionId) {
|
|
448
|
+
this.sessionId = sessionId;
|
|
449
|
+
}
|
|
450
|
+
clearSession() {
|
|
451
|
+
this.sessionId = null;
|
|
452
|
+
}
|
|
453
|
+
// Process management methods
|
|
454
|
+
async startProcess(command, options) {
|
|
455
|
+
try {
|
|
456
|
+
const targetSessionId = options?.sessionId || this.sessionId;
|
|
457
|
+
const response = await this.doFetch("/api/process/start", {
|
|
458
|
+
body: JSON.stringify({
|
|
459
|
+
command,
|
|
460
|
+
options: {
|
|
461
|
+
...options,
|
|
462
|
+
sessionId: targetSessionId
|
|
463
|
+
}
|
|
464
|
+
}),
|
|
465
|
+
headers: {
|
|
466
|
+
"Content-Type": "application/json"
|
|
467
|
+
},
|
|
468
|
+
method: "POST"
|
|
469
|
+
});
|
|
470
|
+
if (!response.ok) {
|
|
471
|
+
const errorData = await response.json().catch(() => ({}));
|
|
472
|
+
throw new Error(
|
|
473
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
const data = await response.json();
|
|
477
|
+
console.log(
|
|
478
|
+
`[HTTP Client] Process started: ${command}, ID: ${data.process.id}`
|
|
479
|
+
);
|
|
480
|
+
return data;
|
|
481
|
+
} catch (error) {
|
|
482
|
+
console.error("[HTTP Client] Error starting process:", error);
|
|
483
|
+
throw error;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
async listProcesses() {
|
|
487
|
+
try {
|
|
488
|
+
const response = await this.doFetch("/api/process/list", {
|
|
489
|
+
headers: {
|
|
490
|
+
"Content-Type": "application/json"
|
|
491
|
+
},
|
|
492
|
+
method: "GET"
|
|
493
|
+
});
|
|
494
|
+
if (!response.ok) {
|
|
495
|
+
const errorData = await response.json().catch(() => ({}));
|
|
496
|
+
throw new Error(
|
|
497
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
const data = await response.json();
|
|
501
|
+
console.log(
|
|
502
|
+
`[HTTP Client] Listed ${data.processes.length} processes`
|
|
503
|
+
);
|
|
504
|
+
return data;
|
|
505
|
+
} catch (error) {
|
|
506
|
+
console.error("[HTTP Client] Error listing processes:", error);
|
|
507
|
+
throw error;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
async getProcess(processId) {
|
|
511
|
+
try {
|
|
512
|
+
const response = await this.doFetch(`/api/process/${processId}`, {
|
|
513
|
+
headers: {
|
|
514
|
+
"Content-Type": "application/json"
|
|
515
|
+
},
|
|
516
|
+
method: "GET"
|
|
517
|
+
});
|
|
518
|
+
if (!response.ok) {
|
|
519
|
+
const errorData = await response.json().catch(() => ({}));
|
|
520
|
+
throw new Error(
|
|
521
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
const data = await response.json();
|
|
525
|
+
console.log(
|
|
526
|
+
`[HTTP Client] Got process ${processId}: ${data.process?.status || "not found"}`
|
|
527
|
+
);
|
|
528
|
+
return data;
|
|
529
|
+
} catch (error) {
|
|
530
|
+
console.error("[HTTP Client] Error getting process:", error);
|
|
531
|
+
throw error;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
async killProcess(processId) {
|
|
535
|
+
try {
|
|
536
|
+
const response = await this.doFetch(`/api/process/${processId}`, {
|
|
537
|
+
headers: {
|
|
538
|
+
"Content-Type": "application/json"
|
|
539
|
+
},
|
|
540
|
+
method: "DELETE"
|
|
541
|
+
});
|
|
542
|
+
if (!response.ok) {
|
|
543
|
+
const errorData = await response.json().catch(() => ({}));
|
|
544
|
+
throw new Error(
|
|
545
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
const data = await response.json();
|
|
549
|
+
console.log(
|
|
550
|
+
`[HTTP Client] Killed process ${processId}`
|
|
551
|
+
);
|
|
552
|
+
return data;
|
|
553
|
+
} catch (error) {
|
|
554
|
+
console.error("[HTTP Client] Error killing process:", error);
|
|
555
|
+
throw error;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
async killAllProcesses() {
|
|
559
|
+
try {
|
|
560
|
+
const response = await this.doFetch("/api/process/kill-all", {
|
|
561
|
+
headers: {
|
|
562
|
+
"Content-Type": "application/json"
|
|
563
|
+
},
|
|
564
|
+
method: "DELETE"
|
|
565
|
+
});
|
|
566
|
+
if (!response.ok) {
|
|
567
|
+
const errorData = await response.json().catch(() => ({}));
|
|
568
|
+
throw new Error(
|
|
569
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
const data = await response.json();
|
|
573
|
+
console.log(
|
|
574
|
+
`[HTTP Client] Killed ${data.killedCount} processes`
|
|
575
|
+
);
|
|
576
|
+
return data;
|
|
577
|
+
} catch (error) {
|
|
578
|
+
console.error("[HTTP Client] Error killing all processes:", error);
|
|
579
|
+
throw error;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
async getProcessLogs(processId) {
|
|
583
|
+
try {
|
|
584
|
+
const response = await this.doFetch(`/api/process/${processId}/logs`, {
|
|
585
|
+
headers: {
|
|
586
|
+
"Content-Type": "application/json"
|
|
587
|
+
},
|
|
588
|
+
method: "GET"
|
|
589
|
+
});
|
|
590
|
+
if (!response.ok) {
|
|
591
|
+
const errorData = await response.json().catch(() => ({}));
|
|
592
|
+
throw new Error(
|
|
593
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
const data = await response.json();
|
|
597
|
+
console.log(
|
|
598
|
+
`[HTTP Client] Got logs for process ${processId}`
|
|
599
|
+
);
|
|
600
|
+
return data;
|
|
601
|
+
} catch (error) {
|
|
602
|
+
console.error("[HTTP Client] Error getting process logs:", error);
|
|
603
|
+
throw error;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
async streamProcessLogs(processId) {
|
|
607
|
+
try {
|
|
608
|
+
const response = await this.doFetch(`/api/process/${processId}/stream`, {
|
|
609
|
+
headers: {
|
|
610
|
+
"Accept": "text/event-stream",
|
|
611
|
+
"Cache-Control": "no-cache"
|
|
612
|
+
},
|
|
613
|
+
method: "GET"
|
|
614
|
+
});
|
|
615
|
+
if (!response.ok) {
|
|
616
|
+
const errorData = await response.json().catch(() => ({}));
|
|
617
|
+
throw new Error(
|
|
618
|
+
errorData.error || `HTTP error! status: ${response.status}`
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
if (!response.body) {
|
|
622
|
+
throw new Error("No response body for streaming request");
|
|
623
|
+
}
|
|
624
|
+
console.log(
|
|
625
|
+
`[HTTP Client] Started streaming logs for process ${processId}`
|
|
626
|
+
);
|
|
627
|
+
return response.body;
|
|
628
|
+
} catch (error) {
|
|
629
|
+
console.error("[HTTP Client] Error streaming process logs:", error);
|
|
630
|
+
throw error;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
export {
|
|
636
|
+
HttpClient
|
|
637
|
+
};
|
|
638
|
+
//# sourceMappingURL=chunk-G4XT4SP7.js.map
|