Skip to content

Commit cbb7fcd

Browse files
Merge pull request #9 from ItsWendell/fix/baseurl-stub
fix: baseUrl and stub forwarding
2 parents c5bd973 + 24f5470 commit cbb7fcd

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

.changeset/angry-pots-sniff.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/sandbox": patch
3+
---
4+
5+
fix baseUrl for stub and stub forwarding

packages/sandbox/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export class HttpClient {
212212
let response: Response;
213213

214214
if (this.options.stub) {
215-
response = await this.options.stub.containerFetch(path, options, this.options.port);
215+
response = await this.options.stub.containerFetch(this.baseUrl + path, options, this.options.port);
216216
} else {
217217
response = await fetch(this.baseUrl + path, options);
218218
}

packages/sandbox/src/index.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,29 @@ export function getSandbox(ns: DurableObjectNamespace<Sandbox>, id: string) {
88
export class Sandbox<Env = unknown> extends Container<Env> {
99
defaultPort = 3000; // The default port for the container to listen on
1010
sleepAfter = "3m"; // Sleep the sandbox if no requests are made in this timeframe
11+
client: HttpClient;
1112

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-
});
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+
}
2934

3035
envVars = {
3136
MESSAGE: "I was passed in via the Sandbox class!",

0 commit comments

Comments
 (0)