Skip to content

Commit

Permalink
extract mutation logic into a function
Browse files Browse the repository at this point in the history
and remove the body modification example
  • Loading branch information
Schniz committed Feb 14, 2022
1 parent cda0f41 commit 05edc74
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 46 deletions.
32 changes: 19 additions & 13 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || []) {
Expand All @@ -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)

Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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<T>(originalStream: ReadableStream<T>): {
duplicate(): ReadableStream<T>
} {
return {
duplicate() {
const [stream1, stream2] = originalStream.tee()
originalStream = stream1
return stream2
},
}
}
33 changes: 0 additions & 33 deletions test/production/reading-request-body-in-middleware/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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,
})
})
})

0 comments on commit 05edc74

Please sign in to comment.