From 05edc74a354c2b46c823b61f4177848d2ea95e51 Mon Sep 17 00:00:00 2001 From: Gal Schlezinger Date: Mon, 14 Feb 2022 15:12:45 +0200 Subject: [PATCH] extract mutation logic into a function and remove the body modification example --- packages/next/server/next-server.ts | 32 ++++++++++-------- .../index.test.ts | 33 ------------------- 2 files changed, 19 insertions(+), 46 deletions(-) diff --git a/packages/next/server/next-server.ts b/packages/next/server/next-server.ts index 5be7edcad796f0..f610fe8d20a83d 100644 --- a/packages/next/server/next-server.ts +++ b/packages/next/server/next-server.ts @@ -1244,7 +1244,7 @@ export default class NextNodeServer extends BaseServer { const method = (params.request.method || 'GET').toUpperCase() let originalBody = method !== 'GET' && method !== 'HEAD' - ? requestToBodyStream(params.request.body) + ? teeableStream(requestToBodyStream(params.request.body)) : undefined for (const middleware of this.middleware || []) { @@ -1255,12 +1255,7 @@ export default class NextNodeServer extends BaseServer { } await this.ensureMiddleware(middleware.page, middleware.ssr) - let currentBody: typeof originalBody = undefined - if (originalBody) { - const [body1, body2] = originalBody.tee() - originalBody = body1 - currentBody = body2 - } + const currentBody = originalBody?.duplicate() const middlewareInfo = this.getMiddlewareInfo(middleware.page) @@ -1290,12 +1285,7 @@ export default class NextNodeServer extends BaseServer { }) for (let [key, value] of result.response.headers) { - // TODO: remove, or rethink if really needed - if (key === 'x-middleware-use-body' && value === '1') { - if (result.response.body) { - originalBody = result.response.body as any - } - } else if (key !== 'x-middleware-next') { + if (key !== 'x-middleware-next') { allHeaders.append(key, value) } } @@ -1373,3 +1363,19 @@ function requestToBodyStream( return transform.readable } + +/** + * A simple utility to take an original stream and have + * an API to duplicate it without closing it or mutate any variables + */ +function teeableStream(originalStream: ReadableStream): { + duplicate(): ReadableStream +} { + return { + duplicate() { + const [stream1, stream2] = originalStream.tee() + originalStream = stream1 + return stream2 + }, + } +} diff --git a/test/production/reading-request-body-in-middleware/index.test.ts b/test/production/reading-request-body-in-middleware/index.test.ts index fe5c5147f02ce4..c0c54eef74e6bd 100644 --- a/test/production/reading-request-body-in-middleware/index.test.ts +++ b/test/production/reading-request-body-in-middleware/index.test.ts @@ -36,17 +36,6 @@ describe('reading request body in middleware', () => { if (request.nextUrl.searchParams.has("next")) { return NextResponse.next(); - } else if (request.nextUrl.searchParams.has("change")) { - return new Response(JSON.stringify({ - ...json, - changeFromRoot: true, - }), { - status: 200, - headers: { - 'x-middleware-next': '1', - 'x-middleware-use-body': '1', - } - }); } return new Response(JSON.stringify({ @@ -135,26 +124,4 @@ describe('reading request body in middleware', () => { root: false, }) }) - - it('modifies the body (returns changeFromRoot: true)', async () => { - const response = await fetchViaHTTP( - next.url, - '/nested/hello', - { - change: '1', - }, - { - method: 'POST', - body: JSON.stringify({ - foo: 'bar', - }), - } - ) - expect(response.status).toEqual(200) - expect(await response.json()).toEqual({ - foo: 'bar', - changeFromRoot: true, - root: false, - }) - }) })