@cloudflare/sandbox 0.0.0-24ceb90 → 0.0.0-2f85e95
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 +18 -0
- package/package.json +2 -2
- package/src/client.ts +15 -5
- package/src/index.ts +30 -23
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @cloudflare/sandbox
|
|
2
2
|
|
|
3
|
+
## 0.0.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [`60af265`](https://github.com/cloudflare/sandbox-sdk/commit/60af265d834e83fd30a921a3e1be232f13fe24da) Thanks [@threepointone](https://github.com/threepointone)! - update dependencies
|
|
8
|
+
|
|
9
|
+
## 0.0.7
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [`d1c7c99`](https://github.com/cloudflare/sandbox-sdk/commit/d1c7c99df6555eff71bcd59852e4b8eed2ad8cb6) Thanks [@threepointone](https://github.com/threepointone)! - fix file operations
|
|
14
|
+
|
|
15
|
+
## 0.0.6
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- [#9](https://github.com/cloudflare/sandbox-sdk/pull/9) [`24f5470`](https://github.com/cloudflare/sandbox-sdk/commit/24f547048d5a26137de4656cea13d83ad2cc0b43) Thanks [@ItsWendell](https://github.com/ItsWendell)! - fix baseUrl for stub and stub forwarding
|
|
20
|
+
|
|
3
21
|
## 0.0.5
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudflare/sandbox",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-2f85e95",
|
|
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.19"
|
|
11
11
|
},
|
|
12
12
|
"tags": [
|
|
13
13
|
"sandbox",
|
package/src/client.ts
CHANGED
|
@@ -203,7 +203,9 @@ export class HttpClient {
|
|
|
203
203
|
path: string,
|
|
204
204
|
options?: RequestInit
|
|
205
205
|
): Promise<Response> {
|
|
206
|
-
const url = this.options.stub
|
|
206
|
+
const url = this.options.stub
|
|
207
|
+
? `http://localhost:${this.options.port}${path}`
|
|
208
|
+
: `${this.baseUrl}${path}`;
|
|
207
209
|
const method = options?.method || "GET";
|
|
208
210
|
|
|
209
211
|
console.log(`[HTTP Client] Making ${method} request to ${url}`);
|
|
@@ -212,15 +214,23 @@ export class HttpClient {
|
|
|
212
214
|
let response: Response;
|
|
213
215
|
|
|
214
216
|
if (this.options.stub) {
|
|
215
|
-
response = await this.options.stub.containerFetch(
|
|
217
|
+
response = await this.options.stub.containerFetch(
|
|
218
|
+
url,
|
|
219
|
+
options,
|
|
220
|
+
this.options.port
|
|
221
|
+
);
|
|
216
222
|
} else {
|
|
217
|
-
response = await fetch(
|
|
223
|
+
response = await fetch(url, options);
|
|
218
224
|
}
|
|
219
225
|
|
|
220
|
-
console.log(
|
|
226
|
+
console.log(
|
|
227
|
+
`[HTTP Client] Response: ${response.status} ${response.statusText}`
|
|
228
|
+
);
|
|
221
229
|
|
|
222
230
|
if (!response.ok) {
|
|
223
|
-
console.error(
|
|
231
|
+
console.error(
|
|
232
|
+
`[HTTP Client] Request failed: ${method} ${url} - ${response.status} ${response.statusText}`
|
|
233
|
+
);
|
|
224
234
|
}
|
|
225
235
|
|
|
226
236
|
return response;
|
package/src/index.ts
CHANGED
|
@@ -8,24 +8,31 @@ 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(
|
|
23
|
+
`[Container] Command started: ${command} ${args.join(" ")}`
|
|
24
|
+
);
|
|
25
|
+
},
|
|
26
|
+
onError: (error, command, args) => {
|
|
27
|
+
console.error(`[Container] Command error: ${error}`);
|
|
28
|
+
},
|
|
29
|
+
onOutput: (stream, data, command) => {
|
|
30
|
+
console.log(`[Container] [${stream}] ${data}`);
|
|
31
|
+
},
|
|
32
|
+
port: this.defaultPort,
|
|
33
|
+
stub: this,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
29
36
|
|
|
30
37
|
envVars = {
|
|
31
38
|
MESSAGE: "I was passed in via the Sandbox class!",
|
|
@@ -69,7 +76,7 @@ export class Sandbox<Env = unknown> extends Container<Env> {
|
|
|
69
76
|
|
|
70
77
|
async mkdir(
|
|
71
78
|
path: string,
|
|
72
|
-
options: { recursive?: boolean; stream?: boolean }
|
|
79
|
+
options: { recursive?: boolean; stream?: boolean } = {}
|
|
73
80
|
) {
|
|
74
81
|
if (options?.stream) {
|
|
75
82
|
return this.client.mkdirStream(path, options.recursive);
|
|
@@ -80,7 +87,7 @@ export class Sandbox<Env = unknown> extends Container<Env> {
|
|
|
80
87
|
async writeFile(
|
|
81
88
|
path: string,
|
|
82
89
|
content: string,
|
|
83
|
-
options: { encoding?: string; stream?: boolean }
|
|
90
|
+
options: { encoding?: string; stream?: boolean } = {}
|
|
84
91
|
) {
|
|
85
92
|
if (options?.stream) {
|
|
86
93
|
return this.client.writeFileStream(path, content, options.encoding);
|
|
@@ -88,7 +95,7 @@ export class Sandbox<Env = unknown> extends Container<Env> {
|
|
|
88
95
|
return this.client.writeFile(path, content, options.encoding);
|
|
89
96
|
}
|
|
90
97
|
|
|
91
|
-
async deleteFile(path: string, options: { stream?: boolean }) {
|
|
98
|
+
async deleteFile(path: string, options: { stream?: boolean } = {}) {
|
|
92
99
|
if (options?.stream) {
|
|
93
100
|
return this.client.deleteFileStream(path);
|
|
94
101
|
}
|
|
@@ -98,7 +105,7 @@ export class Sandbox<Env = unknown> extends Container<Env> {
|
|
|
98
105
|
async renameFile(
|
|
99
106
|
oldPath: string,
|
|
100
107
|
newPath: string,
|
|
101
|
-
options: { stream?: boolean }
|
|
108
|
+
options: { stream?: boolean } = {}
|
|
102
109
|
) {
|
|
103
110
|
if (options?.stream) {
|
|
104
111
|
return this.client.renameFileStream(oldPath, newPath);
|
|
@@ -109,7 +116,7 @@ export class Sandbox<Env = unknown> extends Container<Env> {
|
|
|
109
116
|
async moveFile(
|
|
110
117
|
sourcePath: string,
|
|
111
118
|
destinationPath: string,
|
|
112
|
-
options: { stream?: boolean }
|
|
119
|
+
options: { stream?: boolean } = {}
|
|
113
120
|
) {
|
|
114
121
|
if (options?.stream) {
|
|
115
122
|
return this.client.moveFileStream(sourcePath, destinationPath);
|
|
@@ -119,7 +126,7 @@ export class Sandbox<Env = unknown> extends Container<Env> {
|
|
|
119
126
|
|
|
120
127
|
async readFile(
|
|
121
128
|
path: string,
|
|
122
|
-
options: { encoding?: string; stream?: boolean }
|
|
129
|
+
options: { encoding?: string; stream?: boolean } = {}
|
|
123
130
|
) {
|
|
124
131
|
if (options?.stream) {
|
|
125
132
|
return this.client.readFileStream(path, options.encoding);
|