mirror of
https://github.com/vercel/next.js.git
synced 2026-09-20 02:25:18 +08:00
f3f56ecec2
Webpack's source-level NFT pass in `next-trace-entrypoints-plugin.ts`
previously ran only for `pages/` and `app/` entries, so middleware fell
back to the post-bundle chunk trace in `collect-build-traces.ts`. That
trace cannot see through webpack's numeric module-ID indirection (`f =
c(3024)` where module 3024 re-exports `require("node:fs")`), so runtime
file references inside Node middleware — e.g.
`fs.readFile(path.join(process.cwd(), 'package.json'))` — never made it
into `middleware.js.nft.json`. Vercel deploys then 500'd with `ENOENT
'/var/task/package.json'` because the file was not shipped into the
function bundle. Turbopack avoids this through its own pre-bundle asset
graph trace, which is why only the webpack deploy path was affected; the
test that surfaced it (the `/test-node-fs` case in
`test/e2e/middleware-general`) had been skipped on deploy until #93614
re-enabled it.
The fix extends the Pass 1 entry filter to also process middleware
entries, keyed off `isMiddlewareFilename` and using
`buildInfo.route.absolutePagePath` as the entry source — middleware
lives at the project root rather than under `pagesDir`/`appDir`, so
`getPageFilePath` does not apply. The existing `createTraceAssets`
emission and the `collect-build-traces.ts` merge already handle
arbitrary entry names, so no further changes are needed downstream.
`test/e2e/middleware-general/app/middleware-node.js` is also cleaned up
to use static `import * as fs from 'node:fs'` / `node:path`, drop the
redundant `process.env.NEXT_RUNTIME === 'nodejs'` guard, and wrap the
callback `fs.readFile` in a Promise. `@vercel/nft`'s symbol table only
recognizes the callback and sync `fs` APIs, not `fs.promises.*`, so the
call site has to remain a direct `fs.readFile(...)` for the path
argument to be backtracked into the trace; a TODO references the
upstream gap.
297 lines
7.9 KiB
JavaScript
297 lines
7.9 KiB
JavaScript
/* global globalThis */
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import * as fs from 'node:fs'
|
|
import * as path from 'node:path'
|
|
import { getSomeData } from './lib/some-data'
|
|
import magicValue from 'shared-package'
|
|
|
|
export const config = {
|
|
regions: 'auto',
|
|
runtime: 'nodejs',
|
|
}
|
|
|
|
// When there is a top level await, requiring middleware.js will return a Promise
|
|
await new Promise((resolve) => {
|
|
setTimeout(resolve, 100)
|
|
})
|
|
|
|
const PATTERNS = []
|
|
|
|
const params = (url) => {
|
|
const input = url.split('?')[0]
|
|
let result = {}
|
|
|
|
for (const [pattern, handler] of PATTERNS) {
|
|
const patternResult = pattern.exec(input)
|
|
if (patternResult !== null && 'pathname' in patternResult) {
|
|
result = handler(patternResult)
|
|
break
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
export async function middleware(request) {
|
|
getSomeData()
|
|
|
|
const url = request.nextUrl
|
|
|
|
if (url.pathname.startsWith('/test-node-fs')) {
|
|
const pkgPath = path.join(process.cwd(), 'package.json')
|
|
// @vercel/nft's static analysis only recognizes the callback `fs.readFile`
|
|
// (and `readFileSync` etc.), not `fs.promises.readFile`. Using the
|
|
// callback form lets NFT resolve the path argument and include
|
|
// `package.json` in the middleware NFT trace, which Vercel relies on to
|
|
// ship files into the deployed function bundle. Turbopack's analysis
|
|
// handles `fs.promises.*` natively, so this only matters for webpack.
|
|
// TODO: Drop this workaround once @vercel/nft adds static-analysis support
|
|
// for `fs.promises.*`.
|
|
const pkgData = JSON.parse(
|
|
await new Promise((resolve, reject) => {
|
|
fs.readFile(pkgPath, 'utf8', (err, data) => {
|
|
if (err) {
|
|
reject(err)
|
|
} else {
|
|
resolve(data)
|
|
}
|
|
})
|
|
})
|
|
)
|
|
return NextResponse.json(pkgData)
|
|
}
|
|
|
|
if (request.headers.get('x-prerender-revalidate')) {
|
|
return NextResponse.next({
|
|
headers: { 'x-middleware': 'hi' },
|
|
})
|
|
}
|
|
|
|
// this is needed for tests to get the BUILD_ID
|
|
if (url.pathname.startsWith('/_next/static/__BUILD_ID')) {
|
|
return NextResponse.next()
|
|
}
|
|
|
|
if (
|
|
url.pathname.includes('/_next/static/chunks/pages/_app-non-existent.js')
|
|
) {
|
|
return NextResponse.rewrite('https://example.vercel.sh')
|
|
}
|
|
|
|
if (url.pathname === '/api/edge-search-params') {
|
|
const newUrl = url.clone()
|
|
newUrl.searchParams.set('foo', 'bar')
|
|
return NextResponse.rewrite(newUrl)
|
|
}
|
|
|
|
if (url.pathname === '/') {
|
|
url.pathname = '/ssg/first'
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname === '/to-ssg') {
|
|
url.pathname = '/ssg/hello'
|
|
url.searchParams.set('from', 'middleware')
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname === '/sha') {
|
|
url.pathname = '/shallow'
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname.startsWith('/fetch-user-agent-default')) {
|
|
try {
|
|
const apiRoute = new URL(url)
|
|
apiRoute.pathname = '/api/headers'
|
|
const res = await fetch(withLocalIp(apiRoute))
|
|
return serializeData(await res.text())
|
|
} catch (err) {
|
|
return serializeError(err)
|
|
}
|
|
}
|
|
|
|
if (url.pathname === '/rewrite-to-dynamic') {
|
|
url.pathname = '/blog/from-middleware'
|
|
url.searchParams.set('some', 'middleware')
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname === '/rewrite-to-config-rewrite') {
|
|
url.pathname = '/rewrite-3'
|
|
url.searchParams.set('some', 'middleware')
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname.startsWith('/fetch-user-agent-crypto')) {
|
|
try {
|
|
const apiRoute = new URL(url)
|
|
apiRoute.pathname = '/api/headers'
|
|
const res = await fetch(withLocalIp(apiRoute), {
|
|
headers: {
|
|
'user-agent': 'custom-agent',
|
|
},
|
|
})
|
|
return serializeData(await res.text())
|
|
} catch (err) {
|
|
return serializeError(err)
|
|
}
|
|
}
|
|
|
|
if (url.pathname === '/global') {
|
|
return serializeData(
|
|
JSON.stringify({
|
|
process: {
|
|
env: {
|
|
ANOTHER_MIDDLEWARE_TEST: process.env.ANOTHER_MIDDLEWARE_TEST,
|
|
STRING_ENV_VAR: process.env.STRING_ENV_VAR,
|
|
MIDDLEWARE_TEST: process.env.MIDDLEWARE_TEST,
|
|
NEXT_RUNTIME: process.env.NEXT_RUNTIME,
|
|
},
|
|
},
|
|
})
|
|
)
|
|
}
|
|
|
|
if (url.pathname.endsWith('/globalthis')) {
|
|
return serializeData(JSON.stringify(Object.keys(globalThis)))
|
|
}
|
|
|
|
if (url.pathname.endsWith('/webcrypto')) {
|
|
const response = {}
|
|
try {
|
|
const algorithm = {
|
|
name: 'RSA-PSS',
|
|
hash: 'SHA-256',
|
|
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
|
|
modulusLength: 2048,
|
|
}
|
|
const keyUsages = ['sign', 'verify']
|
|
await crypto.subtle.generateKey(algorithm, false, keyUsages)
|
|
} catch (err) {
|
|
response.error = true
|
|
} finally {
|
|
return serializeData(JSON.stringify(response))
|
|
}
|
|
}
|
|
|
|
if (url.pathname.endsWith('/fetch-url')) {
|
|
const response = {}
|
|
try {
|
|
await fetch(new URL('http://localhost'))
|
|
} catch (err) {
|
|
response.error = {
|
|
name: err.name,
|
|
message: err.message,
|
|
}
|
|
} finally {
|
|
return serializeData(JSON.stringify(response))
|
|
}
|
|
}
|
|
|
|
if (url.pathname === '/abort-controller') {
|
|
const controller = new AbortController()
|
|
const signal = controller.signal
|
|
|
|
controller.abort()
|
|
const response = {}
|
|
|
|
try {
|
|
await fetch('https://example.vercel.sh', { signal })
|
|
} catch (err) {
|
|
response.error = {
|
|
name: err.name,
|
|
message: err.message,
|
|
}
|
|
} finally {
|
|
return serializeData(JSON.stringify(response))
|
|
}
|
|
}
|
|
|
|
if (url.pathname.endsWith('/root-subrequest')) {
|
|
const res = await fetch(url)
|
|
res.headers.set('x-dynamic-path', 'true')
|
|
return res
|
|
}
|
|
|
|
if (url.pathname === '/about') {
|
|
if (magicValue !== 42) throw new Error('shared-package problem')
|
|
return NextResponse.rewrite(new URL('/about/a', request.url))
|
|
}
|
|
|
|
if (url.pathname === '/redirect-to-somewhere') {
|
|
url.pathname = '/somewhere'
|
|
return NextResponse.redirect(url, {
|
|
headers: {
|
|
'x-redirect-header': 'hi',
|
|
},
|
|
})
|
|
}
|
|
|
|
if (url.pathname.startsWith('/url')) {
|
|
try {
|
|
if (request.nextUrl.pathname === '/url/relative-url') {
|
|
new URL('/relative')
|
|
return Response.next()
|
|
}
|
|
|
|
if (request.nextUrl.pathname === '/url/relative-request') {
|
|
await fetch(new Request('/urls-b'))
|
|
return Response.next()
|
|
}
|
|
|
|
if (request.nextUrl.pathname === '/url/relative-redirect') {
|
|
return Response.redirect('/urls-b')
|
|
}
|
|
|
|
if (request.nextUrl.pathname === '/url/relative-next-redirect') {
|
|
return NextResponse.redirect('/urls-b')
|
|
}
|
|
|
|
if (request.nextUrl.pathname === '/url/relative-next-rewrite') {
|
|
return NextResponse.rewrite('/urls-b')
|
|
}
|
|
|
|
if (request.nextUrl.pathname === '/url/relative-next-request') {
|
|
await fetch(new NextRequest('/urls-b'))
|
|
return NextResponse.next()
|
|
}
|
|
} catch (error) {
|
|
return new NextResponse(null, { headers: { error: error.message } })
|
|
}
|
|
}
|
|
|
|
if (url.pathname === '/ssr-page') {
|
|
url.pathname = '/ssr-page-2'
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
if (url.pathname === '/error-throw' && request.__isData) {
|
|
throw new Error('test error')
|
|
}
|
|
|
|
const original = new URL(request.url)
|
|
return NextResponse.next({
|
|
headers: {
|
|
'req-url-path': `${original.pathname}${original.search}`,
|
|
'req-url-basepath': request.nextUrl.basePath,
|
|
'req-url-pathname': request.nextUrl.pathname,
|
|
'req-url-query': request.nextUrl.searchParams.get('foo'),
|
|
'req-url-locale': request.nextUrl.locale,
|
|
'req-url-params':
|
|
url.pathname !== '/static' ? JSON.stringify(params(request.url)) : '{}',
|
|
},
|
|
})
|
|
}
|
|
|
|
function serializeData(data) {
|
|
return new NextResponse(null, { headers: { data } })
|
|
}
|
|
|
|
function serializeError(error) {
|
|
return new NextResponse(null, { headers: { error: error.message } })
|
|
}
|
|
|
|
function withLocalIp(url) {
|
|
return String(url).replace('localhost', '127.0.0.1')
|
|
}
|