Skip to content

Commit

Permalink
Revert "Allow reading request bodies in middlewares (#34294)" (#34479)
Browse files Browse the repository at this point in the history
This reverts commit 1edd851.
  • Loading branch information
ijjk committed Feb 17, 2022
1 parent 4202011 commit b70397e
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 256 deletions.
16 changes: 6 additions & 10 deletions packages/next/server/base-http/node.ts
Expand Up @@ -7,11 +7,6 @@ import { NEXT_REQUEST_META, RequestMeta } from '../request-meta'

import { BaseNextRequest, BaseNextResponse } from './index'

type Req = IncomingMessage & {
[NEXT_REQUEST_META]?: RequestMeta
cookies?: NextApiRequestCookies
}

export class NodeNextRequest extends BaseNextRequest<Readable> {
public headers = this._req.headers;

Expand All @@ -26,11 +21,12 @@ export class NodeNextRequest extends BaseNextRequest<Readable> {
return this._req
}

set originalRequest(value: Req) {
this._req = value
}

constructor(private _req: Req) {
constructor(
private _req: IncomingMessage & {
[NEXT_REQUEST_META]?: RequestMeta
cookies?: NextApiRequestCookies
}
) {
super(_req.method!.toUpperCase(), _req.url!, _req)
}

Expand Down
87 changes: 0 additions & 87 deletions packages/next/server/body-streams.ts

This file was deleted.

16 changes: 4 additions & 12 deletions packages/next/server/next-server.ts
Expand Up @@ -38,7 +38,7 @@ import { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import { recursiveReadDirSync } from './lib/recursive-readdir-sync'
import { format as formatUrl, UrlWithParsedQuery } from 'url'
import compression from 'next/dist/compiled/compression'
import HttpProxy from 'next/dist/compiled/http-proxy'
import Proxy from 'next/dist/compiled/http-proxy'
import { route } from './router'
import { run } from './web/sandbox'

Expand Down Expand Up @@ -73,7 +73,6 @@ import { loadEnvConfig } from '@next/env'
import { getCustomRoute } from './server-route-utils'
import { urlQueryToSearchParams } from '../shared/lib/router/utils/querystring'
import ResponseCache from '../server/response-cache'
import { clonableBodyForRequest } from './body-streams'

export * from './base-server'

Expand Down Expand Up @@ -486,7 +485,7 @@ export default class NextNodeServer extends BaseServer {
parsedUrl.search = stringifyQuery(req, query)

const target = formatUrl(parsedUrl)
const proxy = new HttpProxy({
const proxy = new Proxy({
target,
changeOrigin: true,
ignorePath: true,
Expand Down Expand Up @@ -1237,11 +1236,6 @@ export default class NextNodeServer extends BaseServer {

const allHeaders = new Headers()
let result: FetchEventResult | null = null
const method = (params.request.method || 'GET').toUpperCase()
let originalBody =
method !== 'GET' && method !== 'HEAD'
? clonableBodyForRequest(params.request.body)
: undefined

for (const middleware of this.middleware || []) {
if (middleware.match(params.parsedUrl.pathname)) {
Expand All @@ -1251,6 +1245,7 @@ export default class NextNodeServer extends BaseServer {
}

await this.ensureMiddleware(middleware.page, middleware.ssr)

const middlewareInfo = this.getMiddlewareInfo(middleware.page)

result = await run({
Expand All @@ -1259,15 +1254,14 @@ export default class NextNodeServer extends BaseServer {
env: middlewareInfo.env,
request: {
headers: params.request.headers,
method,
method: params.request.method || 'GET',
nextConfig: {
basePath: this.nextConfig.basePath,
i18n: this.nextConfig.i18n,
trailingSlash: this.nextConfig.trailingSlash,
},
url: url,
page: page,
body: originalBody?.cloneBodyStream(),
},
useCache: !this.nextConfig.experimental.runtime,
onWarning: (warning: Error) => {
Expand Down Expand Up @@ -1304,8 +1298,6 @@ export default class NextNodeServer extends BaseServer {
}
}

originalBody?.finalize()

return result
}

Expand Down
1 change: 0 additions & 1 deletion packages/next/server/web/adapter.ts
Expand Up @@ -16,7 +16,6 @@ export async function adapter(params: {
page: params.page,
input: params.request.url,
init: {
body: params.request.body,
geo: params.request.geo,
headers: fromNodeHeaders(params.request.headers),
ip: params.request.ip,
Expand Down
1 change: 0 additions & 1 deletion packages/next/server/web/types.ts
Expand Up @@ -39,7 +39,6 @@ export interface RequestData {
params?: { [key: string]: string }
}
url: string
body?: ReadableStream<Uint8Array>
}

export interface FetchEventResult {
Expand Down
144 changes: 0 additions & 144 deletions test/production/reading-request-body-in-middleware/index.test.ts

This file was deleted.

3 changes: 2 additions & 1 deletion yarn.lock
Expand Up @@ -20812,7 +20812,8 @@ webpack-bundle-analyzer@4.3.0:
source-list-map "^2.0.0"
source-map "~0.6.1"

"webpack-sources3@npm:webpack-sources@3.2.3", webpack-sources@^3.2.2, webpack-sources@^3.2.3:
"webpack-sources3@npm:webpack-sources@3.2.3", webpack-sources@^3.2.3:
name webpack-sources3
version "3.2.3"
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-3.2.3.tgz#2d4daab8451fd4b240cc27055ff6a0c2ccea0cde"
integrity sha512-/DyMEOrDgLKKIG0fmvtz+4dUX/3Ghozwgm6iPp8KRhvn+eQf9+Q7GWxVNMk3+uCPWfdXYC4ExGBckIXdFEfH1w==
Expand Down

0 comments on commit b70397e

Please sign in to comment.