Skip to content

Commit 8b061d2

Browse files
MarcoDotIOclaude
andcommitted
fix(server): a chunked preview response body is bounded by the bytes received
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c6f4329 commit 8b061d2

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎apps/server/src/preview/ServerBrowserNetwork.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const makeRequest = (
2020
readonly headers?: Record<string, string>;
2121
readonly postData?: string;
2222
readonly errorText?: string;
23+
readonly responseBodySize?: number;
2324
} = {},
2425
) => ({
2526
url: () => url,
@@ -28,6 +29,10 @@ const makeRequest = (
2829
headers: () => options.headers ?? {},
2930
postDataBuffer: () => (options.postData === undefined ? null : Buffer.from(options.postData)),
3031
failure: () => (options.errorText === undefined ? null : { errorText: options.errorText }),
32+
sizes: async () => {
33+
if (options.responseBodySize === undefined) throw new Error("Unable to fetch sizes");
34+
return { responseBodySize: options.responseBodySize };
35+
},
3136
});
3237

3338
const makeResponse = (
@@ -201,6 +206,39 @@ describe("NetworkLog", () => {
201206
expect(streamResponse.body).not.toHaveBeenCalled();
202207
});
203208

209+
it("bounds a chunked body without a declared length by the bytes received", async () => {
210+
const network = setup();
211+
const export_ = makeRequest("http://localhost:5173/api/export", {
212+
responseBodySize: 6_000_000,
213+
});
214+
const exportResponse = makeResponse(export_, 200, { "content-type": "application/json" });
215+
const small = makeRequest("http://localhost:5173/api/small", { responseBodySize: 11 });
216+
const smallResponse = makeResponse(
217+
small,
218+
500,
219+
{ "content-type": "application/json" },
220+
'{"ok":false}',
221+
);
222+
for (const [request, response] of [
223+
[export_, exportResponse],
224+
[small, smallResponse],
225+
] as const) {
226+
network.start(request);
227+
network.respond(response);
228+
network.finish(request);
229+
}
230+
231+
const detail = async (requestId: string) =>
232+
(await network.log.query("tab-1", { requestId })).requests[0]?.responseBody;
233+
expect(await detail("r1")).toEqual({
234+
sizeBytes: 6_000_000,
235+
truncated: true,
236+
omitted: "unavailable",
237+
});
238+
expect(exportResponse.body).not.toHaveBeenCalled();
239+
expect(await detail("r2")).toEqual({ text: '{"ok":false}', sizeBytes: 12, truncated: false });
240+
});
241+
204242
it("reports a body Chromium no longer holds as unavailable", async () => {
205243
const network = setup();
206244
const request = makeRequest("http://localhost:5173/api/old");

‎apps/server/src/preview/ServerBrowserNetwork.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,24 @@ export const bodyOf = (
7171
return { text, sizeBytes: bytes.byteLength, truncated };
7272
};
7373

74+
const receivedBodyBytes = async (response: Response) => {
75+
try {
76+
return (await response.request().sizes()).responseBodySize;
77+
} catch {
78+
return undefined;
79+
}
80+
};
81+
7482
const responseBodyOf = async (response: Response): Promise<PreviewAutomationNetworkBody> => {
7583
const headers = response.headers();
7684
const contentType = headers["content-type"];
77-
const declared = declaredLength(headers);
78-
const known = declared === undefined ? {} : { sizeBytes: declared };
85+
// A chunked response declares no length, so the bytes Chromium received bound the fetch instead.
86+
const size = declaredLength(headers) ?? (await receivedBodyBytes(response));
87+
const known = size === undefined ? {} : { sizeBytes: size };
7988
if (contentType !== undefined && !isText(contentType)) {
8089
return { ...known, truncated: false, omitted: "binary" };
8190
}
82-
if (declared !== undefined && declared > BODY_FETCH_BYTES) {
91+
if (size !== undefined && size > BODY_FETCH_BYTES) {
8392
return { ...known, truncated: true, omitted: "unavailable" };
8493
}
8594
try {

0 commit comments

Comments
 (0)