@cloudflare/sandbox 0.0.0-c0d9d33 → 0.0.0-cbb7fcd
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 +14 -0
- package/container_src/index.ts +8 -2
- package/package.json +4 -2
- package/src/client.ts +24 -3
- package/src/index.ts +22 -17
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @cloudflare/sandbox
|
|
2
2
|
|
|
3
|
+
## 0.0.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#5](https://github.com/cloudflare/sandbox-sdk/pull/5) [`7c15b81`](https://github.com/cloudflare/sandbox-sdk/commit/7c15b817899e4d9e1f25747aaf439e5e9e880d15) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Make package ready for deployment
|
|
8
|
+
|
|
9
|
+
## 0.0.4
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [`c0d9d33`](https://github.com/cloudflare/sandbox-sdk/commit/c0d9d3396badee1eab45e6b4a73d48957f31409b) Thanks [@threepointone](https://github.com/threepointone)! - actually work
|
|
14
|
+
|
|
15
|
+
- [`444d2da`](https://github.com/cloudflare/sandbox-sdk/commit/444d2dafde9a0f190e50c879b0e768da1b289b51) Thanks [@threepointone](https://github.com/threepointone)! - add experimental label
|
|
16
|
+
|
|
3
17
|
## 0.0.3
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
package/container_src/index.ts
CHANGED
|
@@ -84,6 +84,8 @@ const server = serve({
|
|
|
84
84
|
const url = new URL(req.url);
|
|
85
85
|
const pathname = url.pathname;
|
|
86
86
|
|
|
87
|
+
console.log(`[Container] Incoming ${req.method} request to ${pathname}`);
|
|
88
|
+
|
|
87
89
|
// Handle CORS
|
|
88
90
|
const corsHeaders = {
|
|
89
91
|
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
|
@@ -93,11 +95,13 @@ const server = serve({
|
|
|
93
95
|
|
|
94
96
|
// Handle preflight requests
|
|
95
97
|
if (req.method === "OPTIONS") {
|
|
98
|
+
console.log(`[Container] Handling CORS preflight for ${pathname}`);
|
|
96
99
|
return new Response(null, { headers: corsHeaders, status: 200 });
|
|
97
100
|
}
|
|
98
101
|
|
|
99
102
|
try {
|
|
100
103
|
// Handle different routes
|
|
104
|
+
console.log(`[Container] Processing ${req.method} ${pathname}`);
|
|
101
105
|
switch (pathname) {
|
|
102
106
|
case "/":
|
|
103
107
|
return new Response("Hello from Bun server! 🚀", {
|
|
@@ -352,13 +356,14 @@ const server = serve({
|
|
|
352
356
|
break;
|
|
353
357
|
|
|
354
358
|
default:
|
|
359
|
+
console.log(`[Container] Route not found: ${pathname}`);
|
|
355
360
|
return new Response("Not Found", {
|
|
356
361
|
headers: corsHeaders,
|
|
357
362
|
status: 404,
|
|
358
363
|
});
|
|
359
364
|
}
|
|
360
365
|
} catch (error) {
|
|
361
|
-
console.error(
|
|
366
|
+
console.error(`[Container] Error handling ${req.method} ${pathname}:`, error);
|
|
362
367
|
return new Response(
|
|
363
368
|
JSON.stringify({
|
|
364
369
|
error: "Internal server error",
|
|
@@ -374,6 +379,7 @@ const server = serve({
|
|
|
374
379
|
);
|
|
375
380
|
}
|
|
376
381
|
},
|
|
382
|
+
hostname: "0.0.0.0",
|
|
377
383
|
port: 3000,
|
|
378
384
|
} as any);
|
|
379
385
|
|
|
@@ -2874,7 +2880,7 @@ function executeMoveFile(
|
|
|
2874
2880
|
});
|
|
2875
2881
|
}
|
|
2876
2882
|
|
|
2877
|
-
console.log(`🚀 Bun server running on http://
|
|
2883
|
+
console.log(`🚀 Bun server running on http://0.0.0.0:${server.port}`);
|
|
2878
2884
|
console.log(`📡 HTTP API endpoints available:`);
|
|
2879
2885
|
console.log(` POST /api/session/create - Create a new session`);
|
|
2880
2886
|
console.log(` GET /api/session/list - List all sessions`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudflare/sandbox",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-cbb7fcd",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/cloudflare/sandbox-sdk"
|
|
@@ -17,7 +17,9 @@
|
|
|
17
17
|
"durable objects"
|
|
18
18
|
],
|
|
19
19
|
"scripts": {
|
|
20
|
-
"build": "rm -rf dist && tsup src/*.ts --outDir dist --dts --sourcemap --format esm"
|
|
20
|
+
"build": "rm -rf dist && tsup src/*.ts --outDir dist --dts --sourcemap --format esm",
|
|
21
|
+
"docker:build": "docker build -t ghostwriternr/cloudflare-sandbox:$npm_package_version .",
|
|
22
|
+
"docker:publish": "docker push docker.io/ghostwriternr/cloudflare-sandbox:$npm_package_version"
|
|
21
23
|
},
|
|
22
24
|
"exports": {
|
|
23
25
|
".": {
|
package/src/client.ts
CHANGED
|
@@ -203,10 +203,31 @@ export class HttpClient {
|
|
|
203
203
|
path: string,
|
|
204
204
|
options?: RequestInit
|
|
205
205
|
): Promise<Response> {
|
|
206
|
-
|
|
207
|
-
|
|
206
|
+
const url = this.options.stub ? `stub:${path}` : `${this.baseUrl}${path}`;
|
|
207
|
+
const method = options?.method || "GET";
|
|
208
|
+
|
|
209
|
+
console.log(`[HTTP Client] Making ${method} request to ${url}`);
|
|
210
|
+
|
|
211
|
+
try {
|
|
212
|
+
let response: Response;
|
|
213
|
+
|
|
214
|
+
if (this.options.stub) {
|
|
215
|
+
response = await this.options.stub.containerFetch(this.baseUrl + path, options, this.options.port);
|
|
216
|
+
} else {
|
|
217
|
+
response = await fetch(this.baseUrl + path, options);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
console.log(`[HTTP Client] Response: ${response.status} ${response.statusText}`);
|
|
221
|
+
|
|
222
|
+
if (!response.ok) {
|
|
223
|
+
console.error(`[HTTP Client] Request failed: ${method} ${url} - ${response.status} ${response.statusText}`);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return response;
|
|
227
|
+
} catch (error) {
|
|
228
|
+
console.error(`[HTTP Client] Request error: ${method} ${url}`, error);
|
|
229
|
+
throw error;
|
|
208
230
|
}
|
|
209
|
-
return fetch(this.baseUrl + path, options);
|
|
210
231
|
}
|
|
211
232
|
// Public methods to set event handlers
|
|
212
233
|
setOnOutput(
|
package/src/index.ts
CHANGED
|
@@ -8,24 +8,29 @@ export function getSandbox(ns: DurableObjectNamespace<Sandbox>, id: string) {
|
|
|
8
8
|
export class Sandbox<Env = unknown> extends Container<Env> {
|
|
9
9
|
defaultPort = 3000; // The default port for the container to listen on
|
|
10
10
|
sleepAfter = "3m"; // Sleep the sandbox if no requests are made in this timeframe
|
|
11
|
+
client: HttpClient;
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
13
|
+
constructor(ctx: DurableObjectState, env: Env) {
|
|
14
|
+
super(ctx, env);
|
|
15
|
+
this.client = new HttpClient({
|
|
16
|
+
onCommandComplete: (success, exitCode, stdout, stderr, command, args) => {
|
|
17
|
+
console.log(
|
|
18
|
+
`[Container] Command completed: ${command}, Success: ${success}, Exit code: ${exitCode}`
|
|
19
|
+
);
|
|
20
|
+
},
|
|
21
|
+
onCommandStart: (command, args) => {
|
|
22
|
+
console.log(`[Container] Command started: ${command} ${args.join(" ")}`);
|
|
23
|
+
},
|
|
24
|
+
onError: (error, command, args) => {
|
|
25
|
+
console.error(`[Container] Command error: ${error}`);
|
|
26
|
+
},
|
|
27
|
+
onOutput: (stream, data, command) => {
|
|
28
|
+
console.log(`[Container] [${stream}] ${data}`);
|
|
29
|
+
},
|
|
30
|
+
port: this.defaultPort,
|
|
31
|
+
stub: this,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
29
34
|
|
|
30
35
|
envVars = {
|
|
31
36
|
MESSAGE: "I was passed in via the Sandbox class!",
|