forked from vercel/next.js
/
nodejs-proxy-handler.ts
58 lines (48 loc) · 1.69 KB
/
nodejs-proxy-handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// IPC need to be the first import to allow it to catch errors happening during
// the other imports
import startHandler from './api-server-handler'
import '../polyfill/app-polyfills'
import { parse as parseUrl } from 'node:url'
import {
NodeNextRequest,
NodeNextResponse,
} from 'next/dist/server/base-http/node'
import { sendResponse } from 'next/dist/server/send-response'
import {
NextRequestAdapter,
signalFromNodeResponse,
} from 'next/dist/server/web/spec-extension/adapters/next-request'
import { RouteHandlerManagerContext } from 'next/dist/server/future/route-handler-managers/route-handler-manager'
import { attachRequestMeta } from './next-request-helpers'
import type { RouteModule } from 'next/dist/server/future/route-modules/route-module'
export default (routeModule: RouteModule) => {
startHandler(async ({ request, response, params }) => {
const req = new NodeNextRequest(request)
const res = new NodeNextResponse(response)
const parsedUrl = parseUrl(req.url!, true)
attachRequestMeta(req, parsedUrl, request.headers.host!)
const context: RouteHandlerManagerContext = {
params,
prerenderManifest: {
version: -1 as any, // letting us know this doesn't conform to spec
routes: {},
dynamicRoutes: {},
notFoundRoutes: [],
preview: {
previewModeId: 'development-id',
} as any,
},
staticGenerationContext: {
supportsDynamicHTML: true,
},
}
const routeResponse = await routeModule.handle(
NextRequestAdapter.fromNodeNextRequest(
req,
signalFromNodeResponse(response)
),
context
)
await sendResponse(req, res, routeResponse)
})
}