2022-12-20 23:05:00 +01:00
|
|
|
import { HonoRequest } from './request.ts'
|
2023-01-16 14:57:47 +01:00
|
|
|
import type { TypeResponse } from './types.ts'
|
2023-01-31 22:36:48 +01:00
|
|
|
import type { Env, NotFoundHandler, Input } from './types.ts'
|
2022-07-10 17:17:29 +02:00
|
|
|
import type { CookieOptions } from './utils/cookie.ts'
|
|
|
|
import { serialize } from './utils/cookie.ts'
|
2022-07-02 08:09:45 +02:00
|
|
|
import type { StatusCode } from './utils/http-status.ts'
|
|
|
|
|
2023-01-29 06:47:35 +01:00
|
|
|
type Runtime = 'node' | 'deno' | 'bun' | 'workerd' | 'fastly' | 'edge-light' | 'lagon' | 'other'
|
2023-01-04 16:01:46 +01:00
|
|
|
type HeaderRecord = Record<string, string | string[]>
|
2022-12-28 03:25:48 +01:00
|
|
|
type Data = string | ArrayBuffer | ReadableStream
|
|
|
|
|
|
|
|
export interface ExecutionContext {
|
|
|
|
waitUntil(promise: Promise<void>): void
|
|
|
|
passThroughOnException(): void
|
|
|
|
}
|
|
|
|
export interface ContextVariableMap {}
|
2022-07-02 08:09:45 +02:00
|
|
|
|
2023-01-16 14:57:47 +01:00
|
|
|
type GetVariable<K, E extends Env> = K extends keyof E['Variables']
|
2023-01-03 14:00:29 +01:00
|
|
|
? E['Variables'][K]
|
|
|
|
: K extends keyof ContextVariableMap
|
|
|
|
? ContextVariableMap[K]
|
|
|
|
: unknown
|
|
|
|
|
2023-01-16 14:57:47 +01:00
|
|
|
type ContextOptions<E extends Env> = {
|
|
|
|
env: E['Bindings']
|
2022-12-20 23:05:00 +01:00
|
|
|
executionCtx?: FetchEvent | ExecutionContext | undefined
|
2023-01-16 14:57:47 +01:00
|
|
|
notFoundHandler?: NotFoundHandler<E>
|
2022-12-20 23:05:00 +01:00
|
|
|
paramData?: Record<string, string>
|
2023-01-10 13:24:21 +01:00
|
|
|
queryIndex?: number
|
2022-12-20 23:05:00 +01:00
|
|
|
}
|
|
|
|
|
2022-11-03 07:53:41 +01:00
|
|
|
export class Context<
|
2022-12-10 08:37:14 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2023-01-16 14:57:47 +01:00
|
|
|
E extends Env = any,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2023-01-31 22:36:48 +01:00
|
|
|
P extends string = any,
|
|
|
|
I extends Input = {}
|
2022-08-23 13:53:25 +02:00
|
|
|
> {
|
2022-12-20 23:05:00 +01:00
|
|
|
env: E['Bindings'] = {}
|
|
|
|
finalized: boolean = false
|
2022-10-02 08:44:54 +02:00
|
|
|
error: Error | undefined = undefined
|
2022-07-10 16:44:23 +02:00
|
|
|
|
2023-01-16 14:57:47 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
private _req?: HonoRequest<any, any>
|
2022-11-23 10:42:24 +01:00
|
|
|
private _status: StatusCode = 200
|
2022-07-17 11:11:09 +02:00
|
|
|
private _executionCtx: FetchEvent | ExecutionContext | undefined
|
2022-07-02 08:09:45 +02:00
|
|
|
private _pretty: boolean = false
|
|
|
|
private _prettySpace: number = 2
|
2022-10-23 01:10:00 +02:00
|
|
|
private _map: Record<string, unknown> | undefined
|
2023-01-04 16:01:46 +01:00
|
|
|
private _headers: Headers | undefined = undefined
|
|
|
|
private _preparedHeaders: Record<string, string> | undefined = undefined
|
2022-07-02 08:09:45 +02:00
|
|
|
private _res: Response | undefined
|
2023-01-17 10:30:02 +01:00
|
|
|
private _paramData?: Record<string, string> | null
|
2023-01-10 13:24:21 +01:00
|
|
|
private _queryIndex: number | undefined
|
2023-01-17 10:30:02 +01:00
|
|
|
private rawRequest?: Request | null
|
2023-01-16 14:57:47 +01:00
|
|
|
private notFoundHandler: NotFoundHandler<E> = () => new Response()
|
2022-12-20 23:05:00 +01:00
|
|
|
|
2023-01-16 14:57:47 +01:00
|
|
|
constructor(req: Request, options?: ContextOptions<E>) {
|
2022-12-20 23:05:00 +01:00
|
|
|
this.rawRequest = req
|
|
|
|
if (options) {
|
|
|
|
this._executionCtx = options.executionCtx
|
|
|
|
this._paramData = options.paramData
|
|
|
|
this.env = options.env
|
|
|
|
if (options.notFoundHandler) {
|
|
|
|
this.notFoundHandler = options.notFoundHandler
|
|
|
|
}
|
2023-01-10 13:24:21 +01:00
|
|
|
this._queryIndex = options.queryIndex
|
2022-12-20 23:05:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 22:36:48 +01:00
|
|
|
get req(): HonoRequest<P, I> {
|
2022-12-20 23:05:00 +01:00
|
|
|
if (this._req) {
|
|
|
|
return this._req
|
|
|
|
} else {
|
2023-01-17 10:30:02 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
this._req = new HonoRequest(this.rawRequest!, this._paramData!, this._queryIndex)
|
2023-01-22 05:59:31 +01:00
|
|
|
this.rawRequest = undefined
|
|
|
|
this._paramData = undefined
|
2022-12-20 23:05:00 +01:00
|
|
|
return this._req
|
|
|
|
}
|
2022-07-17 11:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get event(): FetchEvent {
|
|
|
|
if (this._executionCtx instanceof FetchEvent) {
|
|
|
|
return this._executionCtx
|
2022-07-02 08:09:45 +02:00
|
|
|
} else {
|
2022-07-17 11:11:09 +02:00
|
|
|
throw Error('This context has no FetchEvent')
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
2022-07-17 11:11:09 +02:00
|
|
|
}
|
2022-07-02 08:09:45 +02:00
|
|
|
|
2022-07-17 11:11:09 +02:00
|
|
|
get executionCtx(): ExecutionContext {
|
|
|
|
if (this._executionCtx) {
|
2022-11-22 23:27:42 +01:00
|
|
|
return this._executionCtx as ExecutionContext
|
2022-07-17 11:11:09 +02:00
|
|
|
} else {
|
|
|
|
throw Error('This context has no ExecutionContext')
|
|
|
|
}
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get res(): Response {
|
2022-09-21 16:05:26 +02:00
|
|
|
return (this._res ||= new Response('404 Not Found', { status: 404 }))
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
set res(_res: Response) {
|
2023-01-23 00:35:14 +01:00
|
|
|
if (this._res) {
|
2023-01-24 00:53:31 +01:00
|
|
|
this._res.headers.delete('content-type')
|
2023-01-23 00:35:14 +01:00
|
|
|
this._res.headers.forEach((v, k) => {
|
|
|
|
_res.headers.set(k, v)
|
|
|
|
})
|
|
|
|
}
|
2022-07-02 08:09:45 +02:00
|
|
|
this._res = _res
|
|
|
|
this.finalized = true
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:42:23 +01:00
|
|
|
header = (name: string, value: string, options?: { append?: boolean }): void => {
|
2023-01-04 16:01:46 +01:00
|
|
|
if (options?.append) {
|
|
|
|
if (!this._headers) {
|
|
|
|
this._headers = new Headers(this._preparedHeaders)
|
2022-09-20 14:41:45 +02:00
|
|
|
}
|
2023-01-04 16:01:46 +01:00
|
|
|
this._headers.append(name, value)
|
2022-09-20 14:41:45 +02:00
|
|
|
} else {
|
2023-01-04 16:01:46 +01:00
|
|
|
if (this._headers) {
|
|
|
|
this._headers.set(name, value)
|
|
|
|
} else {
|
|
|
|
this._preparedHeaders ??= {}
|
|
|
|
this._preparedHeaders[name.toLowerCase()] = value
|
|
|
|
}
|
2022-09-20 14:41:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-02 08:09:45 +02:00
|
|
|
if (this.finalized) {
|
2023-01-04 16:01:46 +01:00
|
|
|
if (options?.append) {
|
2022-09-20 14:41:45 +02:00
|
|
|
this.res.headers.append(name, value)
|
|
|
|
} else {
|
|
|
|
this.res.headers.set(name, value)
|
|
|
|
}
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:23:58 +01:00
|
|
|
status = (status: StatusCode): void => {
|
2022-07-02 08:09:45 +02:00
|
|
|
this._status = status
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:42:23 +01:00
|
|
|
set = <Key extends keyof E['Variables'] | keyof ContextVariableMap>(
|
2023-01-03 14:00:29 +01:00
|
|
|
key: Key,
|
|
|
|
value: GetVariable<Key, E>
|
2023-01-18 00:42:23 +01:00
|
|
|
): void => {
|
2022-07-02 08:09:45 +02:00
|
|
|
this._map ||= {}
|
2023-01-03 14:00:29 +01:00
|
|
|
this._map[key as string] = value
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-18 00:42:23 +01:00
|
|
|
get = <Key extends keyof E['Variables'] | keyof ContextVariableMap>(
|
|
|
|
key: Key
|
|
|
|
): GetVariable<Key, E> => {
|
2023-01-03 14:00:29 +01:00
|
|
|
return this._map?.[key as string] as GetVariable<Key, E>
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-18 00:23:58 +01:00
|
|
|
pretty = (prettyJSON: boolean, space: number = 2): void => {
|
2022-07-02 08:09:45 +02:00
|
|
|
this._pretty = prettyJSON
|
|
|
|
this._prettySpace = space
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:42:23 +01:00
|
|
|
newResponse = (data: Data | null, status?: StatusCode, headers?: HeaderRecord): Response => {
|
2023-01-17 10:30:02 +01:00
|
|
|
// Optimized
|
2023-02-04 18:50:53 +01:00
|
|
|
if (!headers && !this._headers && !this._res && !status) {
|
2023-01-04 16:01:46 +01:00
|
|
|
return new Response(data, {
|
|
|
|
headers: this._preparedHeaders,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
this._preparedHeaders ??= {}
|
|
|
|
|
|
|
|
if (!this._headers) {
|
2023-01-17 10:30:02 +01:00
|
|
|
this._headers = new Headers()
|
2023-01-04 16:01:46 +01:00
|
|
|
for (const [k, v] of Object.entries(this._preparedHeaders)) {
|
|
|
|
this._headers.set(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._res) {
|
|
|
|
this._res.headers.forEach((v, k) => {
|
|
|
|
this._headers?.set(k, v)
|
|
|
|
})
|
|
|
|
for (const [k, v] of Object.entries(this._preparedHeaders)) {
|
|
|
|
this._headers.set(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
headers ??= {}
|
|
|
|
for (const [k, v] of Object.entries(headers)) {
|
|
|
|
if (typeof v === 'string') {
|
|
|
|
this._headers.set(k, v)
|
|
|
|
} else {
|
|
|
|
this._headers.delete(k)
|
|
|
|
for (const v2 of v) {
|
|
|
|
this._headers.append(k, v2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 14:41:45 +02:00
|
|
|
return new Response(data, {
|
2022-11-23 10:42:24 +01:00
|
|
|
status,
|
2023-01-04 16:01:46 +01:00
|
|
|
headers: this._headers,
|
2022-09-20 14:41:45 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:42:23 +01:00
|
|
|
body = (
|
|
|
|
data: Data | null,
|
|
|
|
status: StatusCode = this._status,
|
|
|
|
headers?: HeaderRecord
|
|
|
|
): Response => {
|
2023-01-04 16:01:46 +01:00
|
|
|
return this.newResponse(data, status, headers)
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:42:23 +01:00
|
|
|
text = (text: string, status?: StatusCode, headers?: HeaderRecord): Response => {
|
2022-12-14 14:03:02 +01:00
|
|
|
// If the header is empty, return Response immediately.
|
|
|
|
// Content-Type will be added automatically as `text/plain`.
|
2023-01-17 10:30:02 +01:00
|
|
|
if (!this._preparedHeaders) {
|
|
|
|
if (!headers && !this._res && !this._headers && !status) {
|
|
|
|
return new Response(text)
|
|
|
|
}
|
|
|
|
this._preparedHeaders = {}
|
2022-12-14 14:03:02 +01:00
|
|
|
}
|
2023-01-17 10:30:02 +01:00
|
|
|
// If Content-Type is not set, we don't have to set `text/plain`.
|
|
|
|
// Fewer the header values, it will be faster.
|
|
|
|
if (this._preparedHeaders['content-type']) {
|
|
|
|
this._preparedHeaders['content-type'] = 'text/plain; charset=UTF8'
|
|
|
|
}
|
|
|
|
return this.newResponse(text, status, headers)
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
2023-02-01 16:25:58 +01:00
|
|
|
json = <T = object>(
|
|
|
|
object: T,
|
|
|
|
status: StatusCode = this._status,
|
|
|
|
headers?: HeaderRecord
|
|
|
|
): Response => {
|
2022-07-02 08:09:45 +02:00
|
|
|
const body = this._pretty
|
|
|
|
? JSON.stringify(object, null, this._prettySpace)
|
|
|
|
: JSON.stringify(object)
|
2023-01-04 16:01:46 +01:00
|
|
|
this._preparedHeaders ??= {}
|
|
|
|
this._preparedHeaders['content-type'] = 'application/json; charset=UTF-8'
|
2022-11-23 10:42:24 +01:00
|
|
|
return this.newResponse(body, status, headers)
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
2023-02-01 16:25:58 +01:00
|
|
|
jsonT = <T = object>(
|
2023-01-18 00:42:23 +01:00
|
|
|
object: T,
|
|
|
|
status: StatusCode = this._status,
|
|
|
|
headers?: HeaderRecord
|
|
|
|
): TypeResponse<T> => {
|
2022-12-28 03:25:48 +01:00
|
|
|
return {
|
|
|
|
response: this.json(object, status, headers),
|
|
|
|
data: object,
|
|
|
|
format: 'json',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:42:23 +01:00
|
|
|
html = (html: string, status: StatusCode = this._status, headers?: HeaderRecord): Response => {
|
2023-01-04 16:01:46 +01:00
|
|
|
this._preparedHeaders ??= {}
|
|
|
|
this._preparedHeaders['content-type'] = 'text/html; charset=UTF-8'
|
|
|
|
return this.newResponse(html, status, headers)
|
|
|
|
}
|
|
|
|
|
2023-01-18 00:42:23 +01:00
|
|
|
redirect = (location: string, status: StatusCode = 302): Response => {
|
2023-01-04 16:01:46 +01:00
|
|
|
this._headers ??= new Headers()
|
|
|
|
this._headers.set('Location', location)
|
|
|
|
return this.newResponse(null, status)
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-18 00:23:58 +01:00
|
|
|
cookie = (name: string, value: string, opt?: CookieOptions): void => {
|
2022-07-10 17:17:29 +02:00
|
|
|
const cookie = serialize(name, value, opt)
|
2022-09-20 14:41:45 +02:00
|
|
|
this.header('set-cookie', cookie, { append: true })
|
2022-07-10 17:17:29 +02:00
|
|
|
}
|
|
|
|
|
2023-01-18 00:42:23 +01:00
|
|
|
notFound = (): Response | Promise<Response> => {
|
2023-01-16 14:57:47 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
2022-12-28 03:25:48 +01:00
|
|
|
return this.notFoundHandler(this)
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
2022-11-03 07:35:23 +01: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 06:47:35 +01:00
|
|
|
return 'workerd'
|
2022-11-03 07:35:23 +01:00
|
|
|
}
|
|
|
|
|
2022-12-02 03:28:49 +01:00
|
|
|
if (typeof global?.EdgeRuntime === 'string') {
|
2023-01-29 06:47:35 +01:00
|
|
|
return 'edge-light'
|
2022-11-03 07:35:23 +01:00
|
|
|
}
|
|
|
|
|
2023-01-08 01:05:49 +01:00
|
|
|
let onFastly = false
|
|
|
|
try {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
const { env } = require('fastly:env')
|
|
|
|
if (env instanceof Function) onFastly = true
|
|
|
|
} catch {}
|
|
|
|
if (onFastly) {
|
|
|
|
return 'fastly'
|
2022-11-03 23:14:57 +01:00
|
|
|
}
|
|
|
|
|
2022-12-24 17:42:59 +01:00
|
|
|
if (global?.__lagon__ !== undefined) {
|
|
|
|
return 'lagon'
|
|
|
|
}
|
|
|
|
|
2023-01-08 01:05:49 +01:00
|
|
|
if (global?.process?.release?.name === 'node') {
|
|
|
|
return 'node'
|
|
|
|
}
|
|
|
|
|
2022-11-03 07:35:23 +01:00
|
|
|
return 'other'
|
|
|
|
}
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|