@cloudflare/sandbox 0.0.0-037c848 → 0.0.0-16a90ce

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