Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
improvement: replace raw-body w/ get-stream (#743)
Browse files Browse the repository at this point in the history
get-stream is small library without dependencies for resolving streams
as promise.

https://packagephobia.com/result?p=raw-body
https://packagephobia.com/result?p=get-stream
  • Loading branch information
TrySound authored Jun 1, 2021
1 parent 9208833 commit 28e4c29
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 51 deletions.
45 changes: 12 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
"dependencies": {
"accepts": "^1.3.7",
"content-type": "^1.0.4",
"http-errors": "1.8.0",
"raw-body": "^2.4.1"
"get-stream": "^6.0.0",
"http-errors": "1.8.0"
},
"devDependencies": {
"@graphiql/toolkit": "^0.1.0",
Expand Down
29 changes: 13 additions & 16 deletions src/parseBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Inflate, Gunzip } from 'zlib';
import zlib from 'zlib';
import querystring from 'querystring';

import getBody from 'raw-body';
import getStream, { MaxBufferError } from 'get-stream';
import httpError from 'http-errors';
import contentType from 'content-type';
import type { ParsedMediaType } from 'content-type';
Expand Down Expand Up @@ -83,7 +83,7 @@ async function readBody(
const charset = typeInfo.parameters.charset?.toLowerCase() ?? 'utf-8';

// Assert charset encoding per JSON RFC 7159 sec 8.1
if (!charset.startsWith('utf-')) {
if (charset !== 'utf8' && charset !== 'utf-8' && charset !== 'utf16le') {
throw httpError(415, `Unsupported charset "${charset.toUpperCase()}".`);
}

Expand All @@ -93,25 +93,22 @@ async function readBody(
typeof contentEncoding === 'string'
? contentEncoding.toLowerCase()
: 'identity';
const length = encoding === 'identity' ? req.headers['content-length'] : null;
const limit = 100 * 1024; // 100kb
const maxBuffer = 100 * 1024; // 100kb
const stream = decompressed(req, encoding);

// Read body from stream.
try {
return await getBody(stream, { encoding: charset, length, limit });
const buffer = await getStream.buffer(stream, { maxBuffer });
return buffer.toString(charset);
} catch (rawError: unknown) {
const error = httpError(
400,
/* istanbul ignore next: Thrown by underlying library. */
rawError instanceof Error ? rawError : String(rawError),
);

error.message =
error.type === 'encoding.unsupported'
? `Unsupported charset "${charset.toUpperCase()}".`
: `Invalid body: ${error.message}.`;
throw error;
/* istanbul ignore else: Thrown by underlying library. */
if (rawError instanceof MaxBufferError) {
throw httpError(413, 'Invalid body: request entity too large.');
} else {
const message =
rawError instanceof Error ? rawError.message : String(rawError);
throw httpError(400, `Invalid body: ${message}.`);
}
}
}

Expand Down

0 comments on commit 28e4c29

Please sign in to comment.