Skip to content

Commit

Permalink
stream request bodies (#5291)
Browse files Browse the repository at this point in the history
* stream request bodies

* changeset

* Update packages/kit/src/node/index.js

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
  • Loading branch information
Rich-Harris and benmccann authored Jun 29, 2022
1 parent d828ca4 commit 4aac0c0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-flowers-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Stream request bodies
57 changes: 24 additions & 33 deletions packages/kit/src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,42 @@ import * as set_cookie_parser from 'set-cookie-parser';

/** @param {import('http').IncomingMessage} req */
function get_raw_body(req) {
return new Promise((fulfil, reject) => {
const h = req.headers;
const h = req.headers;

if (!h['content-type']) {
return fulfil(null);
}
if (!h['content-type']) {
return null;
}

req.on('error', reject);
const length = Number(h['content-length']);

const length = Number(h['content-length']);
// check if no request body
// https://github.com/jshttp/type-is/blob/c1f4388c71c8a01f79934e68f630ca4a15fffcd6/index.js#L81-L95
if (isNaN(length) && h['transfer-encoding'] == null) {
return null;
}

// https://github.com/jshttp/type-is/blob/c1f4388c71c8a01f79934e68f630ca4a15fffcd6/index.js#L81-L95
if (isNaN(length) && h['transfer-encoding'] == null) {
return fulfil(null);
}
return new ReadableStream({
start(controller) {
req.on('error', (error) => {
controller.error(error);
});

let data = new Uint8Array(length || 0);
let size = 0;

if (length > 0) {
let offset = 0;
req.on('data', (chunk) => {
const new_len = offset + Buffer.byteLength(chunk);
size += chunk.length;

if (new_len > length) {
return reject({
status: 413,
reason: 'Exceeded "Content-Length" limit'
});
if (size > length) {
controller.error(new Error('content-length exceeded'));
}

data.set(chunk, offset);
offset = new_len;
controller.enqueue(chunk);
});
} else {
req.on('data', (chunk) => {
const new_data = new Uint8Array(data.length + chunk.length);
new_data.set(data, 0);
new_data.set(chunk, data.length);
data = new_data;

req.on('end', () => {
controller.close();
});
}

req.on('end', () => {
fulfil(data);
});
});
}

Expand All @@ -67,7 +58,7 @@ export async function getRequest(base, req) {
return new Request(base + req.url, {
method: req.method,
headers,
body: await get_raw_body(req) // TODO stream rather than buffer
body: get_raw_body(req)
});
}

Expand Down

0 comments on commit 4aac0c0

Please sign in to comment.