|
|
import type { WaitUntil } from '../../after/builtin-request-context' |
|
|
import { PageSignatureError } from '../error' |
|
|
import type { NextRequest } from './request' |
|
|
|
|
|
const responseSymbol = Symbol('response') |
|
|
const passThroughSymbol = Symbol('passThrough') |
|
|
const waitUntilSymbol = Symbol('waitUntil') |
|
|
|
|
|
class FetchEvent { |
|
|
|
|
|
|
|
|
readonly [waitUntilSymbol]: |
|
|
| { kind: 'internal'; promises: Promise<any>[] } |
|
|
| { kind: 'external'; function: WaitUntil }; |
|
|
|
|
|
[responseSymbol]?: Promise<Response>; |
|
|
[passThroughSymbol] = false |
|
|
|
|
|
constructor(_request: Request, waitUntil?: WaitUntil) { |
|
|
this[waitUntilSymbol] = waitUntil |
|
|
? { kind: 'external', function: waitUntil } |
|
|
: { kind: 'internal', promises: [] } |
|
|
} |
|
|
|
|
|
|
|
|
respondWith(response: Response | Promise<Response>): void { |
|
|
if (!this[responseSymbol]) { |
|
|
this[responseSymbol] = Promise.resolve(response) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
passThroughOnException(): void { |
|
|
this[passThroughSymbol] = true |
|
|
} |
|
|
|
|
|
waitUntil(promise: Promise<any>): void { |
|
|
if (this[waitUntilSymbol].kind === 'external') { |
|
|
|
|
|
|
|
|
const waitUntil = this[waitUntilSymbol].function |
|
|
return waitUntil(promise) |
|
|
} else { |
|
|
|
|
|
|
|
|
this[waitUntilSymbol].promises.push(promise) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
export function getWaitUntilPromiseFromEvent( |
|
|
event: FetchEvent |
|
|
): Promise<void> | undefined { |
|
|
return event[waitUntilSymbol].kind === 'internal' |
|
|
? Promise.all(event[waitUntilSymbol].promises).then(() => {}) |
|
|
: undefined |
|
|
} |
|
|
|
|
|
export class NextFetchEvent extends FetchEvent { |
|
|
sourcePage: string |
|
|
|
|
|
constructor(params: { |
|
|
request: NextRequest |
|
|
page: string |
|
|
context: { waitUntil: WaitUntil } | undefined |
|
|
}) { |
|
|
super(params.request, params.context?.waitUntil) |
|
|
this.sourcePage = params.page |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get request() { |
|
|
throw new PageSignatureError({ |
|
|
page: this.sourcePage, |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
respondWith() { |
|
|
throw new PageSignatureError({ |
|
|
page: this.sourcePage, |
|
|
}) |
|
|
} |
|
|
} |
|
|
|