Unverified Commit aeaa006b authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Handle duplex automatically for NextRequest (#76531)

When configuring a body with `NextRequest` in node runtime without the
`duplex: 'half'` option it can throw an error un-necessarily since it's
an extension of the node `Request` instance. This auto-configures
`duplex: 'half'` when using `NextRequest` for easier migration from edge
-> node runtime for middleware.

```sh
TypeError: RequestInit: duplex option is required when sending a body.
    at new Request (/opt/rust/undici-runtime.js:40:31096)
```

x-ref: [slack
thread](https://vercel.slack.com/archives/C07LQPFERGF/p1740490607381379)
parent 14e58aee
...@@ -107,7 +107,6 @@ export class NextRequestAdapter { ...@@ -107,7 +107,6 @@ export class NextRequestAdapter {
return new NextRequest(url, { return new NextRequest(url, {
method: request.method, method: request.method,
headers: fromNodeOutgoingHttpHeaders(request.headers), headers: fromNodeOutgoingHttpHeaders(request.headers),
// @ts-expect-error - see https://github.com/whatwg/fetch/pull/1457
duplex: 'half', duplex: 'half',
signal, signal,
// geo // geo
...@@ -134,7 +133,6 @@ export class NextRequestAdapter { ...@@ -134,7 +133,6 @@ export class NextRequestAdapter {
return new NextRequest(request.url, { return new NextRequest(request.url, {
method: request.method, method: request.method,
headers: fromNodeOutgoingHttpHeaders(request.headers), headers: fromNodeOutgoingHttpHeaders(request.headers),
// @ts-expect-error - see https://github.com/whatwg/fetch/pull/1457
duplex: 'half', duplex: 'half',
signal: request.request.signal, signal: request.request.signal,
// geo // geo
......
...@@ -21,9 +21,22 @@ export class NextRequest extends Request { ...@@ -21,9 +21,22 @@ export class NextRequest extends Request {
constructor(input: URL | RequestInfo, init: RequestInit = {}) { constructor(input: URL | RequestInfo, init: RequestInit = {}) {
const url = const url =
typeof input !== 'string' && 'url' in input ? input.url : String(input) typeof input !== 'string' && 'url' in input ? input.url : String(input)
validateURL(url) validateURL(url)
// node Request instance requires duplex option when a body
// is present or it errors, we don't handle this for
// Request being passed in since it would have already
// errored if this wasn't configured
if (process.env.NEXT_RUNTIME !== 'edge') {
if (init.body && init.duplex !== 'half') {
init.duplex = 'half'
}
}
if (input instanceof Request) super(input, init) if (input instanceof Request) super(input, init)
else super(url, init) else super(url, init)
const nextUrl = new NextURL(url, { const nextUrl = new NextURL(url, {
headers: toNodeOutgoingHttpHeaders(this.headers), headers: toNodeOutgoingHttpHeaders(this.headers),
nextConfig: init.nextConfig, nextConfig: init.nextConfig,
...@@ -97,4 +110,6 @@ export interface RequestInit extends globalThis.RequestInit { ...@@ -97,4 +110,6 @@ export interface RequestInit extends globalThis.RequestInit {
trailingSlash?: boolean trailingSlash?: boolean
} }
signal?: AbortSignal signal?: AbortSignal
// see https://github.com/whatwg/fetch/pull/1457
duplex?: 'half'
} }
// this file must not use the edge-runtime env setup
// so that we test node runtime properly
import { expectTypeOf } from 'expect-type'
import { NextRequest } from 'next/dist/server/web/spec-extension/request'
it('should have 1 required parameter for constructor', () => {
expect(NextRequest.length).toBe(1)
})
it('should allow the 2nd parameter to be undefined', () => {
const request = new NextRequest('https://vercel.com')
expectTypeOf(request).toMatchTypeOf<NextRequest>()
expect(new NextRequest('https://vercel.com')).toHaveProperty(
'nextUrl.pathname',
'/'
)
})
it('should clone Request with headers', () => {
const request = new Request('https://example.com', {
headers: { 'x-foo': 'bar' },
})
const nextRequest = new NextRequest(request)
expect(Object.fromEntries(nextRequest.headers)).toEqual(
Object.fromEntries(request.headers)
)
// Second argument should override headers
const headers = new Headers({ 'x-header': 'some header' })
const nextRequest2 = new NextRequest(request, { headers })
expect(Object.fromEntries(nextRequest2.headers)).toEqual(
Object.fromEntries(headers)
)
})
it('should handle Request with body', () => {
let nextRequest = new NextRequest('https://example.com', {
body: new ReadableStream(),
method: 'POST',
})
expect(nextRequest.body).toBeTruthy()
expect(nextRequest.method).toBe('POST')
const request = new Request('https://example.com', {
body: new ReadableStream(),
method: 'POST',
// @ts-expect-error this exists but not in type
duplex: 'half',
})
nextRequest = new NextRequest(request)
expect(nextRequest.body).toBeTruthy()
expect(nextRequest.method).toBe('POST')
})
...@@ -38,3 +38,24 @@ it('should clone Request with headers', () => { ...@@ -38,3 +38,24 @@ it('should clone Request with headers', () => {
Object.fromEntries(headers) Object.fromEntries(headers)
) )
}) })
it('should handle Request with body', () => {
let nextRequest = new NextRequest('https://example.com', {
body: new ReadableStream(),
method: 'POST',
})
expect(nextRequest.body).toBeTruthy()
expect(nextRequest.method).toBe('POST')
const request = new Request('https://example.com', {
body: new ReadableStream(),
method: 'POST',
// @ts-expect-error this exists but not in type
duplex: 'half',
})
nextRequest = new NextRequest(request)
expect(nextRequest.body).toBeTruthy()
expect(nextRequest.method).toBe('POST')
})
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