Skip to content

Commit d1c7c99

Browse files
committed
fix file operations
1 parent 18fac92 commit d1c7c99

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

.changeset/little-toes-retire.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 file operations

examples/basic/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ export default {
1515
return sandbox.containerFetch(request);
1616
}
1717

18+
if (pathname.startsWith("/test-file")) {
19+
// write a file to the sandbox
20+
const sandbox = getSandbox(env.Sandbox, "my-sandbox");
21+
await sandbox.writeFile("/test-file.txt", "Hello, world!" + Date.now());
22+
const file = await sandbox.readFile("/test-file.txt");
23+
return new Response(file!.content, { status: 200 });
24+
}
25+
1826
return new Response("Not found", { status: 404 });
1927
},
2028
};

packages/sandbox/src/client.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ export class HttpClient {
203203
path: string,
204204
options?: RequestInit
205205
): Promise<Response> {
206-
const url = this.options.stub ? `stub:${path}` : `${this.baseUrl}${path}`;
206+
const url = this.options.stub
207+
? `http://localhost:${this.options.port}${path}`
208+
: `${this.baseUrl}${path}`;
207209
const method = options?.method || "GET";
208210

209211
console.log(`[HTTP Client] Making ${method} request to ${url}`);
@@ -212,15 +214,23 @@ export class HttpClient {
212214
let response: Response;
213215

214216
if (this.options.stub) {
215-
response = await this.options.stub.containerFetch(this.baseUrl + path, options, this.options.port);
217+
response = await this.options.stub.containerFetch(
218+
url,
219+
options,
220+
this.options.port
221+
);
216222
} else {
217-
response = await fetch(this.baseUrl + path, options);
223+
response = await fetch(url, options);
218224
}
219225

220-
console.log(`[HTTP Client] Response: ${response.status} ${response.statusText}`);
226+
console.log(
227+
`[HTTP Client] Response: ${response.status} ${response.statusText}`
228+
);
221229

222230
if (!response.ok) {
223-
console.error(`[HTTP Client] Request failed: ${method} ${url} - ${response.status} ${response.statusText}`);
231+
console.error(
232+
`[HTTP Client] Request failed: ${method} ${url} - ${response.status} ${response.statusText}`
233+
);
224234
}
225235

226236
return response;

packages/sandbox/src/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ export class Sandbox<Env = unknown> extends Container<Env> {
1919
);
2020
},
2121
onCommandStart: (command, args) => {
22-
console.log(`[Container] Command started: ${command} ${args.join(" ")}`);
22+
console.log(
23+
`[Container] Command started: ${command} ${args.join(" ")}`
24+
);
2325
},
2426
onError: (error, command, args) => {
2527
console.error(`[Container] Command error: ${error}`);
@@ -74,7 +76,7 @@ export class Sandbox<Env = unknown> extends Container<Env> {
7476

7577
async mkdir(
7678
path: string,
77-
options: { recursive?: boolean; stream?: boolean }
79+
options: { recursive?: boolean; stream?: boolean } = {}
7880
) {
7981
if (options?.stream) {
8082
return this.client.mkdirStream(path, options.recursive);
@@ -85,15 +87,15 @@ export class Sandbox<Env = unknown> extends Container<Env> {
8587
async writeFile(
8688
path: string,
8789
content: string,
88-
options: { encoding?: string; stream?: boolean }
90+
options: { encoding?: string; stream?: boolean } = {}
8991
) {
9092
if (options?.stream) {
9193
return this.client.writeFileStream(path, content, options.encoding);
9294
}
9395
return this.client.writeFile(path, content, options.encoding);
9496
}
9597

96-
async deleteFile(path: string, options: { stream?: boolean }) {
98+
async deleteFile(path: string, options: { stream?: boolean } = {}) {
9799
if (options?.stream) {
98100
return this.client.deleteFileStream(path);
99101
}
@@ -103,7 +105,7 @@ export class Sandbox<Env = unknown> extends Container<Env> {
103105
async renameFile(
104106
oldPath: string,
105107
newPath: string,
106-
options: { stream?: boolean }
108+
options: { stream?: boolean } = {}
107109
) {
108110
if (options?.stream) {
109111
return this.client.renameFileStream(oldPath, newPath);
@@ -114,7 +116,7 @@ export class Sandbox<Env = unknown> extends Container<Env> {
114116
async moveFile(
115117
sourcePath: string,
116118
destinationPath: string,
117-
options: { stream?: boolean }
119+
options: { stream?: boolean } = {}
118120
) {
119121
if (options?.stream) {
120122
return this.client.moveFileStream(sourcePath, destinationPath);
@@ -124,7 +126,7 @@ export class Sandbox<Env = unknown> extends Container<Env> {
124126

125127
async readFile(
126128
path: string,
127-
options: { encoding?: string; stream?: boolean }
129+
options: { encoding?: string; stream?: boolean } = {}
128130
) {
129131
if (options?.stream) {
130132
return this.client.readFileStream(path, options.encoding);

0 commit comments

Comments
 (0)