@cloudflare/sandbox 0.12.10 → 0.12.11

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.
@@ -1,6 +1,6 @@
1
- import "../dist-B_eXrP83.js";
1
+ import "../dist-C3AQh4A0.js";
2
2
  import "../errors-CpkKPbWC.js";
3
- import { S as validateTunnelName, r as getSandbox, w as streamFile, x as validatePort, y as SandboxSecurityError } from "../sandbox-Co2NYKhX.js";
3
+ import { S as validateTunnelName, r as getSandbox, w as streamFile, x as validatePort, y as SandboxSecurityError } from "../sandbox-DKbJiquI.js";
4
4
  import { DurableObject, env, tracing } from "cloudflare:workers";
5
5
  import { Hono } from "hono";
6
6
 
@@ -115,12 +115,23 @@ function validateSessionId(id) {
115
115
  /**
116
116
  * Convert an SSE stream from readFileStream() into a raw byte stream.
117
117
  * Decodes base64 chunks for binary files and UTF-8-encodes text chunks.
118
+ * Reads up to the first chunk before returning. A missing file arrives as an
119
+ * error event inside a successful stream, so it rejects here, while the caller
120
+ * can still choose the HTTP status.
118
121
  */
119
- function sseToByteStream(sse) {
122
+ async function sseToByteStream(sse) {
120
123
  const encoder = new TextEncoder();
124
+ const enqueue = (controller, chunk) => {
125
+ controller.enqueue(chunk instanceof Uint8Array ? chunk : encoder.encode(chunk));
126
+ };
127
+ const chunks = streamFile(sse);
128
+ const first = await chunks.next();
121
129
  return new ReadableStream({ async start(controller) {
122
130
  try {
123
- for await (const chunk of streamFile(sse)) controller.enqueue(chunk instanceof Uint8Array ? chunk : encoder.encode(chunk));
131
+ if (!first.done) {
132
+ enqueue(controller, first.value);
133
+ for await (const chunk of chunks) enqueue(controller, chunk);
134
+ }
124
135
  controller.close();
125
136
  } catch (err) {
126
137
  controller.error(err);
@@ -2056,14 +2067,16 @@ function createBridgeApp(config) {
2056
2067
  executor = await sandbox.getSession(sessionId);
2057
2068
  }
2058
2069
  try {
2059
- const stream = await executor.readFileStream(resolvedPath);
2060
- return new Response(sseToByteStream(stream), {
2070
+ const byteStream = await sseToByteStream(await executor.readFileStream(resolvedPath));
2071
+ return new Response(byteStream, {
2061
2072
  status: 200,
2062
2073
  headers: { "Content-Type": "application/octet-stream" }
2063
2074
  });
2064
2075
  } catch (err) {
2065
- if (err.code === "FILE_NOT_FOUND") return errorJson(`File not found: ${resolvedPath}`, "workspace_read_not_found", 404);
2066
- return errorJson(`read failed: ${err instanceof Error ? err.message : String(err)}`, "exec_transport_error", 502);
2076
+ const code = err.code;
2077
+ const msg = err instanceof Error ? err.message : String(err);
2078
+ if (code === "FILE_NOT_FOUND" || msg.startsWith("File streaming error: File not found: ")) return errorJson(`File not found: ${resolvedPath}`, "workspace_read_not_found", 404);
2079
+ return errorJson(`read failed: ${msg}`, "exec_transport_error", 502);
2067
2080
  }
2068
2081
  }));
2069
2082
  app.put(`${apiPrefix}/sandbox/:id/file/*`, traced("write_file", async (c) => {
@@ -2185,7 +2198,7 @@ function createBridgeApp(config) {
2185
2198
  if (result.exitCode !== 0) return errorJson(`tar failed (exit ${result.exitCode}): ${result.stderr}`, "workspace_archive_read_error", 502);
2186
2199
  const stream = await sandbox.readFileStream(tmpPath);
2187
2200
  sandbox.exec(`rm -f ${shellQuote(tmpPath)}`).catch(() => {});
2188
- return new Response(sseToByteStream(stream), {
2201
+ return new Response(await sseToByteStream(stream), {
2189
2202
  status: 200,
2190
2203
  headers: { "Content-Type": "application/octet-stream" }
2191
2204
  });