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

Replace raw-body with get-stream and bytes #21915

Merged
merged 7 commits into from Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions packages/next/next-server/server/api-utils.ts
@@ -1,7 +1,8 @@
import { IncomingMessage, ServerResponse } from 'http'
import { parse } from 'next/dist/compiled/content-type'
import { CookieSerializeOptions } from 'next/dist/compiled/cookie'
import getRawBody from 'raw-body'
import getStream from 'get-stream'
import bytes from 'bytes'
import { PageConfig } from 'next/types'
import { Stream } from 'stream'
import { isResSent, NextApiRequest, NextApiResponse } from '../lib/utils'
Expand Down Expand Up @@ -124,16 +125,17 @@ export async function parseBody(
let buffer

try {
buffer = await getRawBody(req, { encoding, limit })
const maxBuffer = bytes.parse(limit)
buffer = await getStream.buffer(req, { maxBuffer })
} catch (e) {
if (e.type === 'entity.too.large') {
if (e.name === 'MaxBufferError') {
throw new ApiError(413, `Body exceeded ${limit} limit`)
} else {
throw new ApiError(400, 'Invalid body')
}
}

const body = buffer.toString()
const body = buffer.toString(encoding)

if (type === 'application/json' || type === 'application/ld+json') {
return parseJson(body)
Expand Down
3 changes: 3 additions & 0 deletions packages/next/package.json
Expand Up @@ -71,13 +71,15 @@
"ast-types": "0.13.2",
"browserslist": "4.16.1",
"buffer": "5.6.0",
"bytes": "3.1.0",
"caniuse-lite": "^1.0.30001179",
"chalk": "2.4.2",
"chokidar": "3.5.1",
"crypto-browserify": "3.12.0",
"cssnano-simple": "1.2.2",
"etag": "1.8.1",
"find-cache-dir": "3.3.1",
"get-stream": "6.0.0",
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
"jest-worker": "24.9.0",
"native-url": "0.3.4",
"node-fetch": "2.6.1",
Expand Down Expand Up @@ -145,6 +147,7 @@
"@types/babel__generator": "7.6.2",
"@types/babel__template": "7.4.0",
"@types/babel__traverse": "7.11.0",
"@types/bytes": "3.1.0",
"@types/ci-info": "2.0.0",
"@types/comment-json": "1.1.1",
"@types/compression": "0.0.36",
Expand Down
13 changes: 12 additions & 1 deletion yarn.lock
Expand Up @@ -2662,6 +2662,11 @@
"@types/connect" "*"
"@types/node" "*"

"@types/bytes@3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@types/bytes/-/bytes-3.1.0.tgz#835a3e4aea3b4d7604aca216a78de372bff3ecc3"
integrity sha512-5YG1AiIC8HPPXRvYAIa7ehK3YMAwd0DWiPCtpuL9sgKceWLyWsVtLRA+lT4NkoanDNF9slwQ66lPizWDpgRlWA==

"@types/cacheable-request@^6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/@types/cacheable-request/-/cacheable-request-6.0.1.tgz#5d22f3dded1fd3a84c0bbeb5039a7419c2c91976"
Expand Down Expand Up @@ -4303,6 +4308,7 @@ bytes@3.0.0:
bytes@3.1.0, bytes@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.0.tgz#f6cf7933a360e0588fa9fde85651cdc7f805d1f6"
integrity sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==

cacache@15.0.5:
version "15.0.5"
Expand Down Expand Up @@ -4511,7 +4517,7 @@ caniuse-api@^3.0.0:
lodash.memoize "^4.1.2"
lodash.uniq "^4.5.0"

caniuse-lite@1.0.30001179, caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001020, caniuse-lite@^1.0.30001093, caniuse-lite@^1.0.30001165, caniuse-lite@^1.0.30001173, caniuse-lite@^1.0.30001179:
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001020, caniuse-lite@^1.0.30001093, caniuse-lite@^1.0.30001165, caniuse-lite@^1.0.30001173, caniuse-lite@^1.0.30001179:
version "1.0.30001179"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001179.tgz#b0803883b4471a6c62066fb1752756f8afc699c8"
integrity sha512-blMmO0QQujuUWZKyVrD1msR4WNDAqb/UPO1Sw2WWsQ7deoM5bJiicKnWJ1Y0NS/aGINSnKPIWBMw5luX+NDUCA==
Expand Down Expand Up @@ -7413,6 +7419,11 @@ get-stdin@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-4.0.1.tgz#b968c6b0a04384324902e8bf1a5df32579a450fe"

get-stream@6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-6.0.0.tgz#3e0012cb6827319da2706e601a1583e8629a6718"
integrity sha512-A1B3Bh1UmL0bidM/YX2NsCOTnGJePL9rO/M+Mw3m9f2gUpfokS0hi5Eah0WSUEWZdZhIZtMjkIYS7mDfOqNHbg==

get-stream@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-3.0.0.tgz#8e943d1358dc37555054ecbe2edb05aa174ede14"
Expand Down