diff --git a/.changeset/angry-pots-sniff.md b/.changeset/angry-pots-sniff.md new file mode 100644 index 00000000..5c795099 --- /dev/null +++ b/.changeset/angry-pots-sniff.md @@ -0,0 +1,5 @@ +--- +"@cloudflare/sandbox": patch +--- + +fix baseUrl for stub and stub forwarding diff --git a/packages/sandbox/src/client.ts b/packages/sandbox/src/client.ts index 490ab3d3..51445304 100644 --- a/packages/sandbox/src/client.ts +++ b/packages/sandbox/src/client.ts @@ -212,7 +212,7 @@ export class HttpClient { let response: Response; if (this.options.stub) { - response = await this.options.stub.containerFetch(path, options, this.options.port); + response = await this.options.stub.containerFetch(this.baseUrl + path, options, this.options.port); } else { response = await fetch(this.baseUrl + path, options); } diff --git a/packages/sandbox/src/index.ts b/packages/sandbox/src/index.ts index 77753ce7..0fdf492a 100644 --- a/packages/sandbox/src/index.ts +++ b/packages/sandbox/src/index.ts @@ -8,24 +8,29 @@ export function getSandbox(ns: DurableObjectNamespace, id: string) { export class Sandbox extends Container { defaultPort = 3000; // The default port for the container to listen on sleepAfter = "3m"; // Sleep the sandbox if no requests are made in this timeframe + client: HttpClient; - client: HttpClient = new HttpClient({ - onCommandComplete: (success, exitCode, stdout, stderr, command, args) => { - console.log( - `[Container] Command completed: ${command}, Success: ${success}, Exit code: ${exitCode}` - ); - }, - onCommandStart: (command, args) => { - console.log(`[Container] Command started: ${command} ${args.join(" ")}`); - }, - onError: (error, command, args) => { - console.error(`[Container] Command error: ${error}`); - }, - onOutput: (stream, data, command) => { - console.log(`[Container] [${stream}] ${data}`); - }, - port: this.defaultPort, - }); + constructor(ctx: DurableObjectState, env: Env) { + super(ctx, env); + this.client = new HttpClient({ + onCommandComplete: (success, exitCode, stdout, stderr, command, args) => { + console.log( + `[Container] Command completed: ${command}, Success: ${success}, Exit code: ${exitCode}` + ); + }, + onCommandStart: (command, args) => { + console.log(`[Container] Command started: ${command} ${args.join(" ")}`); + }, + onError: (error, command, args) => { + console.error(`[Container] Command error: ${error}`); + }, + onOutput: (stream, data, command) => { + console.log(`[Container] [${stream}] ${data}`); + }, + port: this.defaultPort, + stub: this, + }); + } envVars = { MESSAGE: "I was passed in via the Sandbox class!",