forked from vercel/next.js
/
adapter.ts
51 lines (43 loc) · 1.47 KB
/
adapter.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
import type { RequestData, FetchEventResult } from './types'
import { DeprecationError } from './error'
import { fromNodeHeaders } from './utils'
import { NextFetchEvent } from './spec-extension/fetch-event'
import { NextRequest, RequestInit } from './spec-extension/request'
import { NextResponse } from './spec-extension/response'
import { waitUntilSymbol } from './spec-compliant/fetch-event'
export async function adapter(params: {
handler: (request: NextRequest, event: NextFetchEvent) => Promise<Response>
request: RequestData
}): Promise<FetchEventResult> {
const url = params.request.url.startsWith('/')
? `https://${params.request.headers.host}${params.request.url}`
: params.request.url
const request = new NextRequestHint(url, {
geo: params.request.geo,
headers: fromNodeHeaders(params.request.headers),
ip: params.request.ip,
method: params.request.method,
nextConfig: params.request.nextConfig,
page: params.request.page,
})
const event = new NextFetchEvent(request)
const original = await params.handler(request, event)
return {
response: original || NextResponse.next(),
waitUntil: Promise.all(event[waitUntilSymbol]),
}
}
class NextRequestHint extends NextRequest {
constructor(input: Request | string, init: RequestInit = {}) {
super(input, init)
}
get request() {
throw new DeprecationError()
}
respondWith() {
throw new DeprecationError()
}
waitUntil() {
throw new DeprecationError()
}
}