Unverified Commit d7e83704 authored by Ali Yusuf's avatar Ali Yusuf Committed by GitHub

fix(next): bind new stream functions with base incomingMessage object (#37806)

Closes: #37805

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
Co-authored-by: 's avatarJJ Kasper <22380829+ijjk@users.noreply.github.com>
parent 7e3355dd
...@@ -41,7 +41,7 @@ function replaceRequestBody<T extends IncomingMessage>( ...@@ -41,7 +41,7 @@ function replaceRequestBody<T extends IncomingMessage>(
for (const key in stream) { for (const key in stream) {
let v = stream[key as keyof Readable] as any let v = stream[key as keyof Readable] as any
if (typeof v === 'function') { if (typeof v === 'function') {
v = v.bind(stream) v = v.bind(base)
} }
base[key as keyof T] = v base[key as keyof T] = v
} }
......
import { NextResponse } from 'next/server'
export async function middleware() {
return NextResponse.next()
}
import request from 'request'
export const config = {
api: {
bodyParser: false,
},
}
export default function handler(req, res) {
return req
.pipe(
request(
`http://${
// node v18 resolves to IPv6 by default so force IPv4
process.version.startsWith('v18.')
? `127.0.0.1:${req.headers.host.split(':').pop() || ''}`
: req.headers.host
}${req.url}/post`,
{
followAllRedirects: false,
followRedirect: false,
gzip: true,
json: false,
}
)
)
.pipe(res)
}
export const config = {
api: {
bodyParser: false,
},
}
export default function handler(req, res) {
return res.json({
method: req.method,
headers: req.headers,
})
}
/* eslint-env jest */
import { join } from 'path'
import { fetchViaHTTP } from 'next-test-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { createNext, FileRef } from 'e2e-utils'
describe('Requests not effected when middleware used', () => {
let next: NextInstance
afterAll(() => next.destroy())
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, '../app/pages')),
'middleware.js': new FileRef(join(__dirname, '../app/middleware.js')),
},
dependencies: {
request: '^2.88.2',
},
})
})
sendRequest('GET')
sendRequest('POST')
function sendRequest(method) {
const body = !['get', 'head'].includes(method.toLowerCase())
? JSON.stringify({
key: 'value',
})
: undefined
it(`should proxy ${method} request ${
body ? 'with body' : ''
}`, async () => {
const headers = {
'content-type': 'application/json',
'x-custom-header': 'some value',
}
const res = await fetchViaHTTP(next.url, `api`, '', {
method: method.toUpperCase(),
headers,
body: method.toLowerCase() !== 'get' ? body : undefined,
})
const data = await res.json()
expect(data.method).toEqual(method)
if (body) {
expect(data.headers['content-length']).toEqual(String(body.length))
}
expect(data.headers).toEqual(expect.objectContaining(headers))
})
}
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment