Skip to content

Commit

Permalink
Revert "Replace raw-body with get-stream and bytes" (#32305)
Browse files Browse the repository at this point in the history
Reverts #21915 since it cannot handle max buffer size.

See sindresorhus/get-stream#42
  • Loading branch information
styfle committed Dec 8, 2021
1 parent 0f7f332 commit 0b86bfa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 59 deletions.
3 changes: 1 addition & 2 deletions packages/next/package.json
Expand Up @@ -79,7 +79,6 @@
"browserify-zlib": "0.2.0",
"browserslist": "4.16.6",
"buffer": "5.6.0",
"bytes": "3.1.0",
"caniuse-lite": "^1.0.30001228",
"chalk": "2.4.2",
"chokidar": "3.5.1",
Expand All @@ -104,6 +103,7 @@
"postcss": "8.2.15",
"process": "0.11.10",
"querystring-es3": "0.2.1",
"raw-body": "2.4.1",
"react-is": "17.0.2",
"react-refresh": "0.8.3",
"regenerator-runtime": "0.13.4",
Expand Down Expand Up @@ -168,7 +168,6 @@
"@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/compression": "0.0.36",
"@types/content-disposition": "0.5.4",
Expand Down
50 changes: 4 additions & 46 deletions packages/next/server/api-utils.ts
@@ -1,8 +1,7 @@
import { IncomingMessage, ServerResponse } from 'http'
import { parse } from 'next/dist/compiled/content-type'
import { CookieSerializeOptions } from 'next/dist/compiled/cookie'
import getStream from 'get-stream'
import bytes from 'bytes'
import getRawBody from 'raw-body'
import { PageConfig, PreviewData } from 'next/types'
import { Stream } from 'stream'
import { isResSent, NextApiRequest, NextApiResponse } from '../shared/lib/utils'
Expand Down Expand Up @@ -137,45 +136,6 @@ export async function apiResolver(
}
}

// Ensures that the user-provided encoding type is a valid encoding
function getValidEncoding(encoding: string): BufferEncoding | undefined {
switch (encoding) {
case 'ascii': {
return 'ascii'
}
case 'utf8': {
return 'utf8'
}
case 'utf-8': {
return 'utf-8'
}
case 'utf16le': {
return 'utf16le'
}
case 'ucs2': {
return 'ucs2'
}
case 'ucs-2': {
return 'ucs-2'
}
case 'base64': {
return 'base64'
}
case 'latin1': {
return 'latin1'
}
case 'binary': {
return 'binary'
}
case 'hex': {
return 'hex'
}
default: {
return undefined
}
}
}

/**
* Parse incoming message like `json` or `urlencoded`
* @param req request object
Expand All @@ -191,16 +151,14 @@ export async function parseBody(
contentType = parse('text/plain')
}
const { type, parameters } = contentType

const encoding = getValidEncoding(parameters.charset) || 'utf-8'
const encoding = parameters.charset || 'utf-8'

let buffer

try {
const maxBuffer = bytes.parse(limit)
buffer = await getStream.buffer(req, { maxBuffer, encoding })
buffer = await getRawBody(req, { encoding, limit })
} catch (e) {
if (isError(e) && e.name === 'MaxBufferError') {
if (isError(e) && e.type === 'entity.too.large') {
throw new ApiError(413, `Body exceeded ${limit} limit`)
} else {
throw new ApiError(400, 'Invalid body')
Expand Down
26 changes: 15 additions & 11 deletions yarn.lock
Expand Up @@ -4400,11 +4400,6 @@
resolved "https://registry.yarnpkg.com/@types/braces/-/braces-3.0.1.tgz#5a284d193cfc61abb2e5a50d36ebbc50d942a32b"
integrity sha512-+euflG6ygo4bn0JHtn4pYqcXwRtLvElQ7/nnjDu7iYG56H0+OhCd7d6Ug0IE3WcFpZozBKW2+80FUbv5QGk5AQ==

"@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 @@ -10092,11 +10087,6 @@ 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, 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 All @@ -10114,6 +10104,11 @@ get-stream@^5.0.0, get-stream@^5.1.0:
dependencies:
pump "^3.0.0"

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-symbol-description@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/get-symbol-description/-/get-symbol-description-1.0.0.tgz#7fdb81c900101fbd564dd5f1a30af5aadc1e58d6"
Expand Down Expand Up @@ -10878,7 +10873,7 @@ http-errors@1.7.2:
statuses ">= 1.5.0 < 2"
toidentifier "1.0.0"

http-errors@~1.7.2:
http-errors@1.7.3, http-errors@~1.7.2:
version "1.7.3"
resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.7.3.tgz#6c619e4f9c60308c38519498c14fbb10aacebb06"
dependencies:
Expand Down Expand Up @@ -16495,6 +16490,15 @@ raw-body@2.4.0:
iconv-lite "0.4.24"
unpipe "1.0.0"

raw-body@2.4.1:
version "2.4.1"
resolved "https://registry.yarnpkg.com/raw-body/-/raw-body-2.4.1.tgz#30ac82f98bb5ae8c152e67149dac8d55153b168c"
dependencies:
bytes "3.1.0"
http-errors "1.7.3"
iconv-lite "0.4.24"
unpipe "1.0.0"

rc@^1.0.1, rc@^1.1.6, rc@^1.2.7, rc@^1.2.8:
version "1.2.8"
resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.8.tgz#cd924bf5200a075b83c188cd6b9e211b7fc0d3ed"
Expand Down

0 comments on commit 0b86bfa

Please sign in to comment.