Skip to content

Write HTML. Render video. Built for agents.

fix(cli): serve empty project assets without a stream error (#5234)

Abdullah M. Nazif58b7a4662dc4
diff --git a/packages/cli/src/utils/staticProjectServer.test.ts b/packages/cli/src/utils/staticProjectServer.test.ts
index 1789678964..013e81d4f0 100644
--- a/packages/cli/src/utils/staticProjectServer.test.ts
+++ b/packages/cli/src/utils/staticProjectServer.test.ts
@@ -146,6 +146,36 @@ describe("serveStaticProjectHtml range support", () => {
expect(await res.text()).toBe("abcdef");
});
+ it.each([undefined, "invalid", "items=0-1"])(
+ "serves an empty asset with Range %j and keeps serving the project",
+ async (range) => {
+ const { url } = await serveWith(Buffer.alloc(0));
+ const res = await fetch(`${url}tone.wav`, {
+ headers: range === undefined ? {} : { Range: range },
+ signal: AbortSignal.timeout(1500),
+ });
+ expect(res.status).toBe(200);
+ expect(res.headers.get("content-length")).toBe("0");
+ expect(res.headers.get("accept-ranges")).toBe("bytes");
+ expect(res.headers.get("content-range")).toBeNull();
+ expect(await res.text()).toBe("");
+ const project = await fetch(url);
+ expect(project.status).toBe(200);
+ expect(await project.text()).toContain("<html></html>");
+ },
+ );
+
+ it.each(["bytes=0-", "bytes=0-0", "bytes=-1"])(
+ "rejects the unsatisfiable range %s on an empty asset",
+ async (range) => {
+ const { url } = await serveWith(Buffer.alloc(0));
+ const res = await fetch(`${url}tone.wav`, { headers: { Range: range } });
+ expect(res.status).toBe(416);
+ expect(res.headers.get("content-range")).toBe("bytes */0");
+ expect(await res.text()).toBe("");
+ },
+ );
+
it("streams a small slice out of a large file without buffering the whole thing", async () => {
// 8MB file, ask for 4 bytes deep inside it. The handler must createReadStream
// the [start,end] window only, not readFileSync the whole 8MB and slice.
diff --git a/packages/cli/src/utils/staticProjectServer.ts b/packages/cli/src/utils/staticProjectServer.ts
index 6be887b0d8..a737470cf8 100644
--- a/packages/cli/src/utils/staticProjectServer.ts
+++ b/packages/cli/src/utils/staticProjectServer.ts
@@ -69,6 +69,12 @@ function serveFileWithRange(
}
headers["Content-Length"] = String(end - start + 1);
+ if (size === 0) {
+ res.writeHead(status, headers);
+ res.end();
+ return;
+ }
+
// Stream only the requested window instead of buffering the whole file: a
// 1KB Range of a 50MB asset must not allocate 50MB. createReadStream reads
// just `[start, end]` and closes its own fd on end/error. writeHead is