Skip to content

Commit

Permalink
fix: wait request body infinitely in handler.js (#93)
Browse files Browse the repository at this point in the history
Co-authored-by: daylilyfield <[email protected]>
  • Loading branch information
daylilyfield and daylilyfield authored May 30, 2021
1 parent 3b7411d commit 94c4158
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/files/handler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {URL} from 'url';
import {getRawBody} from '@sveltejs/kit/node';
import '@sveltejs/kit/install-fetch'; // eslint-disable-line import/no-unassigned-import

// TODO: hardcoding the relative location makes this brittle
Expand All @@ -14,7 +13,7 @@ const svelteKit = async (request, response) => {
headers: request.headers,
path: pathname,
query: searchParameters,
body: await getRawBody(request)
rawBody: getRawBody(request)
});

if (rendered) {
Expand All @@ -25,4 +24,20 @@ const svelteKit = async (request, response) => {
return response.writeHead(404).end();
};

const getRawBody = request => {
if (!request.headers['content-type']) {
return request.rawBody;
}

const [type] = request.headers['content-type'].split(/;\s*/);

if (type === 'application/octet-stream') {
return request.body;
}

const encoding = request.headers['content-encoding'] || 'utf-8';

return new TextDecoder(encoding).decode(request.rawBody);
};

export default svelteKit;

0 comments on commit 94c4158

Please sign in to comment.