2022-12-20 22:05:00 +00:00
|
|
|
import { HonoRequest } from './request.ts'
|
2023-06-29 07:48:00 +00:00
|
|
|
import { FetchEventLike } from './types.ts'
|
|
|
|
import type { Env, NotFoundHandler, Input, TypedResponse } from './types.ts'
|
2022-07-10 15:17:29 +00:00
|
|
|
import type { CookieOptions } from './utils/cookie.ts'
|
|
|
|
import { serialize } from './utils/cookie.ts'
|
2022-07-02 06:09:45 +00:00
|
|
|
import type { StatusCode } from './utils/http-status.ts'
|
2023-06-06 01:23:09 +00:00
|
|
|
import type { JSONValue, InterfaceToType } from './utils/types.ts'
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2023-01-29 05:47:35 +00:00
|
|
|
type Runtime = 'node' | 'deno' | 'bun' | 'workerd' | 'fastly' | 'edge-light' | 'lagon' | 'other'
|
2023-01-04 15:01:46 +00:00
|
|
|
type HeaderRecord = Record<string, string | string[]>
|
2022-12-28 02:25:48 +00:00
|
|
|
type Data = string | ArrayBuffer | ReadableStream
|
|
|
|
|
|
|
|
export interface ExecutionContext {
|
2023-03-05 14:37:02 +00:00
|
|
|
waitUntil(promise: Promise<unknown>): void
|
2022-12-28 02:25:48 +00:00
|
|
|
passThroughOnException(): void
|
|
|
|
}
|
|
|
|
export interface ContextVariableMap {}
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2023-05-09 12:55:40 +00:00
|
|
|
interface Get<E extends Env> {
|
|
|
|
<Key extends keyof ContextVariableMap>(key: Key): ContextVariableMap[Key]
|
|
|
|
<Key extends keyof E['Variables']>(key: Key): E['Variables'][Key]
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Set<E extends Env> {
|
|
|
|
<Key extends keyof ContextVariableMap>(key: Key, value: ContextVariableMap[Key]): void
|
|
|
|
<Key extends keyof E['Variables']>(key: Key, value: E['Variables'][Key]): void
|
|
|
|
}
|
|
|
|
|
2023-03-09 14:54:47 +00:00
|
|
|
interface NewResponse {
|
|
|
|
(data: Data | null, status?: StatusCode, headers?: HeaderRecord): Response
|
|
|
|
(data: Data | null, init?: ResponseInit): Response
|
|
|
|
}
|
|
|
|
|
|
|
|
interface BodyRespond extends NewResponse {}
|
|
|
|
|
|
|
|
interface TextRespond {
|
|
|
|
(text: string, status?: StatusCode, headers?: HeaderRecord): Response
|
|
|
|
(text: string, init?: ResponseInit): Response
|
|
|
|
}
|
|
|
|
|
|
|
|
interface JSONRespond {
|
|
|
|
<T = JSONValue>(object: T, status?: StatusCode, headers?: HeaderRecord): Response
|
|
|
|
<T = JSONValue>(object: T, init?: ResponseInit): Response
|
|
|
|
}
|
|
|
|
|
|
|
|
interface JSONTRespond {
|
2023-06-06 01:23:09 +00:00
|
|
|
<T>(
|
|
|
|
object: InterfaceToType<T> extends JSONValue ? T : JSONValue,
|
|
|
|
status?: StatusCode,
|
|
|
|
headers?: HeaderRecord
|
|
|
|
): TypedResponse<
|
|
|
|
InterfaceToType<T> extends JSONValue
|
|
|
|
? JSONValue extends InterfaceToType<T>
|
|
|
|
? never
|
|
|
|
: T
|
|
|
|
: never
|
|
|
|
>
|
|
|
|
<T>(
|
|
|
|
object: InterfaceToType<T> extends JSONValue ? T : JSONValue,
|
|
|
|
init?: ResponseInit
|
|
|
|
): TypedResponse<
|
|
|
|
InterfaceToType<T> extends JSONValue
|
|
|
|
? JSONValue extends InterfaceToType<T>
|
|
|
|
? never
|
|
|
|
: T
|
|
|
|
: never
|
2023-03-09 14:54:47 +00:00
|
|
|
>
|
|
|
|
}
|
|
|
|
|
|
|
|
interface HTMLRespond {
|
|
|
|
(html: string, status?: StatusCode, headers?: HeaderRecord): Response
|
|
|
|
(html: string, init?: ResponseInit): Response
|
|
|
|
}
|
|
|
|
|
2023-01-16 13:57:47 +00:00
|
|
|
type ContextOptions<E extends Env> = {
|
|
|
|
env: E['Bindings']
|
2023-06-29 07:48:00 +00:00
|
|
|
executionCtx?: FetchEventLike | ExecutionContext | undefined
|
2023-01-16 13:57:47 +00:00
|
|
|
notFoundHandler?: NotFoundHandler<E>
|
2023-03-17 07:32:19 +00:00
|
|
|
path?: string
|
2023-06-04 00:16:29 +00:00
|
|
|
params?: Record<string, string>
|
2022-12-20 22:05:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 06:53:41 +00:00
|
|
|
export class Context<
|
2022-12-10 07:37:14 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2023-01-16 13:57:47 +00:00
|
|
|
E extends Env = any,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2023-01-31 21:36:48 +00:00
|
|
|
P extends string = any,
|
|
|
|
I extends Input = {}
|
2022-08-23 11:53:25 +00:00
|
|
|
> {
|
2022-12-20 22:05:00 +00:00
|
|
|
env: E['Bindings'] = {}
|
|
|
|
finalized: boolean = false
|
2022-10-02 06:44:54 +00:00
|
|
|
error: Error | undefined = undefined
|
2022-07-10 14:44:23 +00:00
|
|
|
|
2023-01-16 13:57:47 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
private _req?: HonoRequest<any, any>
|
2022-11-23 09:42:24 +00:00
|
|
|
private _status: StatusCode = 200
|
2023-06-29 07:48:00 +00:00
|
|
|
private _exCtx: FetchEventLike | ExecutionContext | undefined // _executionCtx
|
2022-10-22 23:10:00 +00:00
|
|
|
private _map: Record<string, unknown> | undefined
|
2023-04-30 12:07:00 +00:00
|
|
|
private _h: Headers | undefined = undefined // _headers
|
|
|
|
private _pH: Record<string, string> | undefined = undefined // _preparedHeaders
|
2022-07-02 06:09:45 +00:00
|
|
|
private _res: Response | undefined
|
2023-03-17 07:32:19 +00:00
|
|
|
private _path: string = '/'
|
2023-06-04 00:16:29 +00:00
|
|
|
private _params?: Record<string, string> | null
|
2023-08-03 15:09:12 +00:00
|
|
|
private _init = true
|
2023-01-17 09:30:02 +00:00
|
|
|
private rawRequest?: Request | null
|
2023-01-16 13:57:47 +00:00
|
|
|
private notFoundHandler: NotFoundHandler<E> = () => new Response()
|
2022-12-20 22:05:00 +00:00
|
|
|
|
2023-01-16 13:57:47 +00:00
|
|
|
constructor(req: Request, options?: ContextOptions<E>) {
|
2022-12-20 22:05:00 +00:00
|
|
|
this.rawRequest = req
|
|
|
|
if (options) {
|
2023-04-30 12:07:00 +00:00
|
|
|
this._exCtx = options.executionCtx
|
2023-03-17 07:32:19 +00:00
|
|
|
this._path = options.path ?? '/'
|
2023-06-04 00:16:29 +00:00
|
|
|
this._params = options.params
|
2022-12-20 22:05:00 +00:00
|
|
|
this.env = options.env
|
|
|
|
if (options.notFoundHandler) {
|
|
|
|
this.notFoundHandler = options.notFoundHandler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 13:17:08 +00:00
|
|
|
get req(): HonoRequest<P, I['out']> {
|
2022-12-20 22:05:00 +00:00
|
|
|
if (this._req) {
|
|
|
|
return this._req
|
|
|
|
} else {
|
2023-01-17 09:30:02 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
2023-06-04 00:16:29 +00:00
|
|
|
this._req = new HonoRequest(this.rawRequest!, this._path, this._params!)
|
2023-01-22 04:59:31 +00:00
|
|
|
this.rawRequest = undefined
|
2023-06-04 00:16:29 +00:00
|
|
|
this._params = undefined
|
2022-12-20 22:05:00 +00:00
|
|
|
return this._req
|
|
|
|
}
|
2022-07-17 09:11:09 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 07:48:00 +00:00
|
|
|
get event(): FetchEventLike {
|
|
|
|
if (this._exCtx instanceof FetchEventLike) {
|
2023-04-30 12:07:00 +00:00
|
|
|
return this._exCtx
|
2022-07-02 06:09:45 +00:00
|
|
|
} else {
|
2022-07-17 09:11:09 +00:00
|
|
|
throw Error('This context has no FetchEvent')
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
2022-07-17 09:11:09 +00:00
|
|
|
}
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2022-07-17 09:11:09 +00:00
|
|
|
get executionCtx(): ExecutionContext {
|
2023-04-30 12:07:00 +00:00
|
|
|
if (this._exCtx) {
|
|
|
|
return this._exCtx as ExecutionContext
|
2022-07-17 09:11:09 +00:00
|
|
|
} else {
|
|
|
|
throw Error('This context has no ExecutionContext')
|
|
|
|
}
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get res(): Response {
|
2023-08-03 15:09:12 +00:00
|
|
|
this._init = false
|
2022-09-21 14:05:26 +00:00
|
|
|
return (this._res ||= new Response('404 Not Found', { status: 404 }))
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 13:08:16 +00:00
|
|
|
set res(_res: Response | undefined) {
|
2023-08-03 15:09:12 +00:00
|
|
|
this._init = false
|
2023-03-13 13:08:16 +00:00
|
|
|
if (this._res && _res) {
|
2023-01-23 23:53:31 +00:00
|
|
|
this._res.headers.delete('content-type')
|
2023-01-22 23:35:14 +00:00
|
|
|
this._res.headers.forEach((v, k) => {
|
|
|
|
_res.headers.set(k, v)
|
|
|
|
})
|
|
|
|
}
|
2022-07-02 06:09:45 +00:00
|
|
|
this._res = _res
|
|
|
|
this.finalized = true
|
|
|
|
}
|
|
|
|
|
2023-05-05 02:46:10 +00:00
|
|
|
header = (name: string, value: string | undefined, options?: { append?: boolean }): void => {
|
|
|
|
// Clear the header
|
|
|
|
if (value === undefined) {
|
|
|
|
if (this._h) {
|
|
|
|
this._h.delete(name)
|
|
|
|
} else if (this._pH) {
|
|
|
|
delete this._pH[name.toLocaleLowerCase()]
|
|
|
|
}
|
|
|
|
if (this.finalized) {
|
|
|
|
this.res.headers.delete(name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-04 15:01:46 +00:00
|
|
|
if (options?.append) {
|
2023-04-30 12:07:00 +00:00
|
|
|
if (!this._h) {
|
2023-08-03 15:09:12 +00:00
|
|
|
this._init = false
|
2023-04-30 12:07:00 +00:00
|
|
|
this._h = new Headers(this._pH)
|
|
|
|
this._pH = {}
|
2022-09-20 12:41:45 +00:00
|
|
|
}
|
2023-04-30 12:07:00 +00:00
|
|
|
this._h.append(name, value)
|
2022-09-20 12:41:45 +00:00
|
|
|
} else {
|
2023-04-30 12:07:00 +00:00
|
|
|
if (this._h) {
|
|
|
|
this._h.set(name, value)
|
2023-01-04 15:01:46 +00:00
|
|
|
} else {
|
2023-04-30 12:07:00 +00:00
|
|
|
this._pH ??= {}
|
|
|
|
this._pH[name.toLowerCase()] = value
|
2023-01-04 15:01:46 +00:00
|
|
|
}
|
2022-09-20 12:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 06:09:45 +00:00
|
|
|
if (this.finalized) {
|
2023-01-04 15:01:46 +00:00
|
|
|
if (options?.append) {
|
2022-09-20 12:41:45 +00:00
|
|
|
this.res.headers.append(name, value)
|
|
|
|
} else {
|
|
|
|
this.res.headers.set(name, value)
|
|
|
|
}
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 23:23:58 +00:00
|
|
|
status = (status: StatusCode): void => {
|
2022-07-02 06:09:45 +00:00
|
|
|
this._status = status
|
|
|
|
}
|
|
|
|
|
2023-05-09 12:55:40 +00:00
|
|
|
set: Set<E> = (key: string, value: unknown) => {
|
2022-07-02 06:09:45 +00:00
|
|
|
this._map ||= {}
|
2023-01-03 13:00:29 +00:00
|
|
|
this._map[key as string] = value
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 12:55:40 +00:00
|
|
|
get: Get<E> = (key: string) => {
|
|
|
|
return this._map ? this._map[key] : undefined
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 14:54:47 +00:00
|
|
|
newResponse: NewResponse = (
|
|
|
|
data: Data | null,
|
|
|
|
arg?: StatusCode | ResponseInit,
|
|
|
|
headers?: HeaderRecord
|
|
|
|
): Response => {
|
2023-01-17 09:30:02 +00:00
|
|
|
// Optimized
|
2023-08-03 15:09:12 +00:00
|
|
|
if (this._init && !headers && !arg && this._status === 200) {
|
2023-01-04 15:01:46 +00:00
|
|
|
return new Response(data, {
|
2023-04-30 12:07:00 +00:00
|
|
|
headers: this._pH,
|
2023-01-04 15:01:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-09 14:54:47 +00:00
|
|
|
// Return Response immediately if arg is RequestInit.
|
|
|
|
if (arg && typeof arg !== 'number') {
|
|
|
|
const res = new Response(data, arg)
|
2023-04-30 12:07:00 +00:00
|
|
|
const contentType = this._pH?.['content-type']
|
2023-03-09 14:54:47 +00:00
|
|
|
if (contentType) {
|
|
|
|
res.headers.set('content-type', contentType)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
const status = arg ?? this._status
|
2023-04-30 12:07:00 +00:00
|
|
|
this._pH ??= {}
|
2023-01-04 15:01:46 +00:00
|
|
|
|
2023-04-30 12:07:00 +00:00
|
|
|
this._h ??= new Headers()
|
|
|
|
for (const [k, v] of Object.entries(this._pH)) {
|
|
|
|
this._h.set(k, v)
|
2023-01-04 15:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this._res) {
|
|
|
|
this._res.headers.forEach((v, k) => {
|
2023-04-30 12:07:00 +00:00
|
|
|
this._h?.set(k, v)
|
2023-01-04 15:01:46 +00:00
|
|
|
})
|
2023-04-30 12:07:00 +00:00
|
|
|
for (const [k, v] of Object.entries(this._pH)) {
|
|
|
|
this._h.set(k, v)
|
2023-01-04 15:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
headers ??= {}
|
|
|
|
for (const [k, v] of Object.entries(headers)) {
|
|
|
|
if (typeof v === 'string') {
|
2023-04-30 12:07:00 +00:00
|
|
|
this._h.set(k, v)
|
2023-01-04 15:01:46 +00:00
|
|
|
} else {
|
2023-04-30 12:07:00 +00:00
|
|
|
this._h.delete(k)
|
2023-01-04 15:01:46 +00:00
|
|
|
for (const v2 of v) {
|
2023-04-30 12:07:00 +00:00
|
|
|
this._h.append(k, v2)
|
2023-01-04 15:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 12:41:45 +00:00
|
|
|
return new Response(data, {
|
2022-11-23 09:42:24 +00:00
|
|
|
status,
|
2023-04-30 12:07:00 +00:00
|
|
|
headers: this._h,
|
2022-09-20 12:41:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-09 14:54:47 +00:00
|
|
|
body: BodyRespond = (
|
2023-01-17 23:42:23 +00:00
|
|
|
data: Data | null,
|
2023-03-09 14:54:47 +00:00
|
|
|
arg?: StatusCode | RequestInit,
|
2023-01-17 23:42:23 +00:00
|
|
|
headers?: HeaderRecord
|
|
|
|
): Response => {
|
2023-03-09 14:54:47 +00:00
|
|
|
return typeof arg === 'number'
|
|
|
|
? this.newResponse(data, arg, headers)
|
|
|
|
: this.newResponse(data, arg)
|
2023-01-04 15:01:46 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 14:54:47 +00:00
|
|
|
text: TextRespond = (
|
|
|
|
text: string,
|
|
|
|
arg?: StatusCode | RequestInit,
|
|
|
|
headers?: HeaderRecord
|
|
|
|
): Response => {
|
2022-12-14 13:03:02 +00:00
|
|
|
// If the header is empty, return Response immediately.
|
|
|
|
// Content-Type will be added automatically as `text/plain`.
|
2023-04-30 12:07:00 +00:00
|
|
|
if (!this._pH) {
|
2023-08-03 15:09:12 +00:00
|
|
|
if (this._init && !headers && !arg) {
|
2023-01-17 09:30:02 +00:00
|
|
|
return new Response(text)
|
|
|
|
}
|
2023-04-30 12:07:00 +00:00
|
|
|
this._pH = {}
|
2022-12-14 13:03:02 +00:00
|
|
|
}
|
2023-01-17 09:30:02 +00:00
|
|
|
// If Content-Type is not set, we don't have to set `text/plain`.
|
|
|
|
// Fewer the header values, it will be faster.
|
2023-04-30 12:07:00 +00:00
|
|
|
if (this._pH['content-type']) {
|
2023-05-09 23:27:40 +00:00
|
|
|
this._pH['content-type'] = 'text/plain; charset=UTF-8'
|
2023-01-17 09:30:02 +00:00
|
|
|
}
|
2023-03-09 14:54:47 +00:00
|
|
|
return typeof arg === 'number'
|
|
|
|
? this.newResponse(text, arg, headers)
|
|
|
|
: this.newResponse(text, arg)
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 14:54:47 +00:00
|
|
|
json: JSONRespond = <T = {}>(
|
2023-02-01 15:25:58 +00:00
|
|
|
object: T,
|
2023-03-09 14:54:47 +00:00
|
|
|
arg?: StatusCode | RequestInit,
|
2023-02-01 15:25:58 +00:00
|
|
|
headers?: HeaderRecord
|
2023-03-09 14:54:47 +00:00
|
|
|
) => {
|
2023-08-05 09:13:39 +00:00
|
|
|
const body = JSON.stringify(object)
|
2023-04-30 12:07:00 +00:00
|
|
|
this._pH ??= {}
|
|
|
|
this._pH['content-type'] = 'application/json; charset=UTF-8'
|
2023-03-09 14:54:47 +00:00
|
|
|
return typeof arg === 'number'
|
|
|
|
? this.newResponse(body, arg, headers)
|
|
|
|
: this.newResponse(body, arg)
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 14:54:47 +00:00
|
|
|
jsonT: JSONTRespond = <T>(
|
2023-06-06 01:23:09 +00:00
|
|
|
object: InterfaceToType<T> extends JSONValue ? T : JSONValue,
|
2023-03-09 14:54:47 +00:00
|
|
|
arg?: StatusCode | RequestInit,
|
2023-01-17 23:42:23 +00:00
|
|
|
headers?: HeaderRecord
|
2023-06-06 01:23:09 +00:00
|
|
|
): TypedResponse<
|
|
|
|
InterfaceToType<T> extends JSONValue
|
|
|
|
? JSONValue extends InterfaceToType<T>
|
|
|
|
? never
|
|
|
|
: T
|
|
|
|
: never
|
|
|
|
> => {
|
2022-12-28 02:25:48 +00:00
|
|
|
return {
|
2023-03-09 14:54:47 +00:00
|
|
|
response: typeof arg === 'number' ? this.json(object, arg, headers) : this.json(object, arg),
|
2023-03-01 23:56:13 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
data: object as any,
|
2022-12-28 02:25:48 +00:00
|
|
|
format: 'json',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 14:54:47 +00:00
|
|
|
html: HTMLRespond = (
|
|
|
|
html: string,
|
|
|
|
arg?: StatusCode | RequestInit,
|
|
|
|
headers?: HeaderRecord
|
|
|
|
): Response => {
|
2023-04-30 12:07:00 +00:00
|
|
|
this._pH ??= {}
|
|
|
|
this._pH['content-type'] = 'text/html; charset=UTF-8'
|
2023-03-09 14:54:47 +00:00
|
|
|
return typeof arg === 'number'
|
|
|
|
? this.newResponse(html, arg, headers)
|
|
|
|
: this.newResponse(html, arg)
|
2023-01-04 15:01:46 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 23:42:23 +00:00
|
|
|
redirect = (location: string, status: StatusCode = 302): Response => {
|
2023-04-30 12:07:00 +00:00
|
|
|
this._h ??= new Headers()
|
|
|
|
this._h.set('Location', location)
|
2023-01-04 15:01:46 +00:00
|
|
|
return this.newResponse(null, status)
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:08:03 +00:00
|
|
|
/** @deprecated
|
|
|
|
* Use Cookie Middleware instead of `c.cookie()`. The `c.cookie()` will be removed in v4.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* import { setCookie } from 'hono/cookie'
|
|
|
|
* // ...
|
|
|
|
* app.get('/', (c) => {
|
|
|
|
* setCookie(c, 'key', 'value')
|
|
|
|
* //...
|
|
|
|
* })
|
|
|
|
*/
|
2023-01-17 23:23:58 +00:00
|
|
|
cookie = (name: string, value: string, opt?: CookieOptions): void => {
|
2022-07-10 15:17:29 +00:00
|
|
|
const cookie = serialize(name, value, opt)
|
2022-09-20 12:41:45 +00:00
|
|
|
this.header('set-cookie', cookie, { append: true })
|
2022-07-10 15:17:29 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 23:42:23 +00:00
|
|
|
notFound = (): Response | Promise<Response> => {
|
2023-01-16 13:57:47 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
2022-12-28 02:25:48 +00:00
|
|
|
return this.notFoundHandler(this)
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
2022-11-03 06:35:23 +00:00
|
|
|
|
|
|
|
get runtime(): Runtime {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const global = globalThis as any
|
|
|
|
|
|
|
|
if (global?.Deno !== undefined) {
|
|
|
|
return 'deno'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global?.Bun !== undefined) {
|
|
|
|
return 'bun'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof global?.WebSocketPair === 'function') {
|
2023-01-29 05:47:35 +00:00
|
|
|
return 'workerd'
|
2022-11-03 06:35:23 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 02:28:49 +00:00
|
|
|
if (typeof global?.EdgeRuntime === 'string') {
|
2023-01-29 05:47:35 +00:00
|
|
|
return 'edge-light'
|
2022-11-03 06:35:23 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 07:26:39 +00:00
|
|
|
if (global?.fastly !== undefined) {
|
2023-01-08 00:05:49 +00:00
|
|
|
return 'fastly'
|
2022-11-03 22:14:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-24 16:42:59 +00:00
|
|
|
if (global?.__lagon__ !== undefined) {
|
|
|
|
return 'lagon'
|
|
|
|
}
|
|
|
|
|
2023-01-08 00:05:49 +00:00
|
|
|
if (global?.process?.release?.name === 'node') {
|
|
|
|
return 'node'
|
|
|
|
}
|
|
|
|
|
2022-11-03 06:35:23 +00:00
|
|
|
return 'other'
|
|
|
|
}
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|