Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(getRequestWebStream): reuse buffered body if available #616

Merged
merged 3 commits into from
Jan 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions src/utils/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,23 +256,43 @@
if (!PayloadMethods.includes(event.method)) {
return;
}
return (
event.web?.request?.body ||
(event._requestBody as ReadableStream) ||
new ReadableStream({
start: (controller) => {
event.node.req.on("data", (chunk) => {
controller.enqueue(chunk);
});
event.node.req.on("end", () => {
controller.close();
});
event.node.req.on("error", (err) => {
controller.error(err);
});

const bodyStream = event.web?.request?.body || event._requestBody;
if (bodyStream) {
return bodyStream as ReadableStream;
}

Check warning on line 263 in src/utils/body.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/body.ts#L262-L263

Added lines #L262 - L263 were not covered by tests

// Use provided body (same as readBody)
const _hasRawBody =
RawBodySymbol in event.node.req ||
"rawBody" in event.node.req /* firebase */ ||
"body" in event.node.req /* unenv */ ||
"__unenv__" in event.node.req;
if (_hasRawBody) {
return new ReadableStream({
async start(controller) {
const _rawBody = await readRawBody(event, false);
if (_rawBody) {
controller.enqueue(_rawBody);
}
controller.close();

Check warning on line 278 in src/utils/body.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/body.ts#L272-L278

Added lines #L272 - L278 were not covered by tests
},
})
);
});
}

Check warning on line 281 in src/utils/body.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/body.ts#L280-L281

Added lines #L280 - L281 were not covered by tests

return new ReadableStream({
start: (controller) => {
event.node.req.on("data", (chunk) => {
controller.enqueue(chunk);
});
event.node.req.on("end", () => {
controller.close();
});
event.node.req.on("error", (err) => {
controller.error(err);

Check warning on line 292 in src/utils/body.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/body.ts#L292

Added line #L292 was not covered by tests
});
},
});
}

// --- Internal ---
Expand Down
Loading