2023-10-27 04:06:21 +02:00
|
|
|
import type { Runtime } from './helper/adapter/index.ts'
|
2023-08-19 09:20:08 +02:00
|
|
|
import type { HonoRequest } from './request.ts'
|
2023-10-18 15:24:19 +02:00
|
|
|
import type { Env, FetchEventLike, NotFoundHandler, Input, TypedResponse } from './types.ts'
|
2022-07-10 17:17:29 +02:00
|
|
|
import type { CookieOptions } from './utils/cookie.ts'
|
|
|
|
import { serialize } from './utils/cookie.ts'
|
2023-11-21 10:05:05 +01:00
|
|
|
import { resolveStream } from './utils/html.ts'
|
2022-07-02 08:09:45 +02:00
|
|
|
import type { StatusCode } from './utils/http-status.ts'
|
2023-09-16 16:28:57 +02:00
|
|
|
import { StreamingApi } from './utils/stream.ts'
|
2023-12-13 01:01:34 +01:00
|
|
|
import type { JSONValue, InterfaceToType, JSONParsed } from './utils/types.ts'
|
2022-07-02 08:09:45 +02:00
|
|
|
|
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 {
|
2023-03-05 15:37:02 +01:00
|
|
|
waitUntil(promise: Promise<unknown>): void
|
2022-12-28 03:25:48 +01:00
|
|
|
passThroughOnException(): void
|
|
|
|
}
|
2023-09-05 09:01:58 +02:00
|
|
|
|
2022-12-28 03:25:48 +01:00
|
|
|
export interface ContextVariableMap {}
|
2022-07-02 08:09:45 +02:00
|
|
|
|
2023-09-05 09:01:58 +02:00
|
|
|
export interface ContextRenderer {}
|
|
|
|
interface DefaultRenderer {
|
2023-11-06 23:09:04 +01:00
|
|
|
(content: string | Promise<string>): Response | Promise<Response>
|
2023-09-05 09:01:58 +02:00
|
|
|
}
|
2023-12-04 09:29:24 +01:00
|
|
|
|
2023-10-05 09:57:33 +02:00
|
|
|
export type Renderer = ContextRenderer extends Function ? ContextRenderer : DefaultRenderer
|
2023-09-05 09:01:58 +02:00
|
|
|
|
2023-05-09 14:55:40 +02: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 15:54:47 +01: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 {
|
2023-06-06 03:23:09 +02:00
|
|
|
<T>(
|
2023-12-04 14:14:49 +01:00
|
|
|
object: InterfaceToType<T> extends JSONValue ? T : JSONValue,
|
2023-06-06 03:23:09 +02:00
|
|
|
status?: StatusCode,
|
|
|
|
headers?: HeaderRecord
|
2023-12-03 01:03:14 +01:00
|
|
|
): Response &
|
|
|
|
TypedResponse<
|
|
|
|
InterfaceToType<T> extends JSONValue
|
|
|
|
? JSONValue extends InterfaceToType<T>
|
|
|
|
? never
|
2023-12-13 01:01:34 +01:00
|
|
|
: JSONParsed<T>
|
2023-12-03 01:03:14 +01:00
|
|
|
: never
|
|
|
|
>
|
|
|
|
<T>(object: InterfaceToType<T> extends JSONValue ? T : JSONValue, init?: ResponseInit): Response &
|
|
|
|
TypedResponse<
|
|
|
|
InterfaceToType<T> extends JSONValue
|
|
|
|
? JSONValue extends InterfaceToType<T>
|
|
|
|
? never
|
2023-12-13 01:01:34 +01:00
|
|
|
: JSONParsed<T>
|
2023-12-03 01:03:14 +01:00
|
|
|
: never
|
|
|
|
>
|
2023-03-09 15:54:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
interface HTMLRespond {
|
2023-11-06 23:09:04 +01:00
|
|
|
(html: string | Promise<string>, status?: StatusCode, headers?: HeaderRecord):
|
|
|
|
| Response
|
|
|
|
| Promise<Response>
|
|
|
|
(html: string | Promise<string>, init?: ResponseInit): Response | Promise<Response>
|
2023-03-09 15:54:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 14:57:47 +01:00
|
|
|
type ContextOptions<E extends Env> = {
|
|
|
|
env: E['Bindings']
|
2023-10-18 15:24:19 +02:00
|
|
|
executionCtx?: FetchEventLike | ExecutionContext | undefined
|
2023-01-16 14:57:47 +01:00
|
|
|
notFoundHandler?: NotFoundHandler<E>
|
2022-12-20 23:05:00 +01:00
|
|
|
}
|
|
|
|
|
2023-12-26 22:55:24 +01:00
|
|
|
export const TEXT_PLAIN = 'text/plain; charset=UTF-8'
|
2023-09-20 15:53:30 +02: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
|
|
|
> {
|
2023-08-19 09:20:08 +02:00
|
|
|
req: HonoRequest<P, I['out']>
|
2022-12-20 23:05:00 +01:00
|
|
|
env: E['Bindings'] = {}
|
2023-09-09 10:11:16 +02:00
|
|
|
private _var: E['Variables'] = {}
|
2022-12-20 23:05:00 +01:00
|
|
|
finalized: boolean = false
|
2022-10-02 08:44:54 +02:00
|
|
|
error: Error | undefined = undefined
|
2022-07-10 16:44:23 +02:00
|
|
|
|
2023-12-04 09:29:24 +01:00
|
|
|
#status: StatusCode = 200
|
|
|
|
#executionCtx: FetchEventLike | ExecutionContext | undefined
|
|
|
|
#headers: Headers | undefined = undefined
|
|
|
|
#preparedHeaders: Record<string, string> | undefined = undefined
|
|
|
|
#res: Response | undefined
|
|
|
|
#isFresh = true
|
|
|
|
private renderer: Renderer = (content: string | Promise<string>) => this.html(content)
|
2023-01-16 14:57:47 +01:00
|
|
|
private notFoundHandler: NotFoundHandler<E> = () => new Response()
|
2022-12-20 23:05:00 +01:00
|
|
|
|
2023-08-19 09:20:08 +02:00
|
|
|
constructor(req: HonoRequest<P, I['out']>, options?: ContextOptions<E>) {
|
|
|
|
this.req = req
|
2022-12-20 23:05:00 +01:00
|
|
|
if (options) {
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#executionCtx = options.executionCtx
|
2022-12-20 23:05:00 +01:00
|
|
|
this.env = options.env
|
|
|
|
if (options.notFoundHandler) {
|
|
|
|
this.notFoundHandler = options.notFoundHandler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-18 15:24:19 +02:00
|
|
|
get event(): FetchEventLike {
|
2023-12-04 09:29:24 +01:00
|
|
|
if (this.#executionCtx && 'respondWith' in this.#executionCtx) {
|
|
|
|
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 {
|
2023-12-04 09:29:24 +01:00
|
|
|
if (this.#executionCtx) {
|
|
|
|
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 {
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#isFresh = false
|
|
|
|
return (this.#res ||= new Response('404 Not Found', { status: 404 }))
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
2023-03-13 14:08:16 +01:00
|
|
|
set res(_res: Response | undefined) {
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#isFresh = false
|
|
|
|
if (this.#res && _res) {
|
|
|
|
this.#res.headers.delete('content-type')
|
|
|
|
this.#res.headers.forEach((v, k) => {
|
2023-01-23 00:35:14 +01:00
|
|
|
_res.headers.set(k, v)
|
|
|
|
})
|
|
|
|
}
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#res = _res
|
2022-07-02 08:09:45 +02:00
|
|
|
this.finalized = true
|
|
|
|
}
|
|
|
|
|
2023-09-05 09:01:58 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2023-12-04 09:29:24 +01:00
|
|
|
render: Renderer = (...args: any[]) => this.renderer(...args)
|
2023-09-05 09:01:58 +02:00
|
|
|
|
|
|
|
setRenderer = (renderer: Renderer) => {
|
2023-12-04 09:29:24 +01:00
|
|
|
this.renderer = renderer
|
2023-09-05 09:01:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 04:46:10 +02:00
|
|
|
header = (name: string, value: string | undefined, options?: { append?: boolean }): void => {
|
|
|
|
// Clear the header
|
|
|
|
if (value === undefined) {
|
2023-12-04 09:29:24 +01:00
|
|
|
if (this.#headers) {
|
|
|
|
this.#headers.delete(name)
|
|
|
|
} else if (this.#preparedHeaders) {
|
|
|
|
delete this.#preparedHeaders[name.toLocaleLowerCase()]
|
2023-05-05 04:46:10 +02:00
|
|
|
}
|
|
|
|
if (this.finalized) {
|
|
|
|
this.res.headers.delete(name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:01:46 +01:00
|
|
|
if (options?.append) {
|
2023-12-04 09:29:24 +01:00
|
|
|
if (!this.#headers) {
|
|
|
|
this.#isFresh = false
|
|
|
|
this.#headers = new Headers(this.#preparedHeaders)
|
|
|
|
this.#preparedHeaders = {}
|
2022-09-20 14:41:45 +02:00
|
|
|
}
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#headers.append(name, value)
|
2022-09-20 14:41:45 +02:00
|
|
|
} else {
|
2023-12-04 09:29:24 +01:00
|
|
|
if (this.#headers) {
|
|
|
|
this.#headers.set(name, value)
|
2023-01-04 16:01:46 +01:00
|
|
|
} else {
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#preparedHeaders ??= {}
|
|
|
|
this.#preparedHeaders[name.toLowerCase()] = value
|
2023-01-04 16:01:46 +01:00
|
|
|
}
|
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 => {
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#isFresh = false
|
|
|
|
this.#status = status
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
2023-05-09 14:55:40 +02:00
|
|
|
set: Set<E> = (key: string, value: unknown) => {
|
2023-09-09 10:11:16 +02:00
|
|
|
this._var ??= {}
|
|
|
|
this._var[key as string] = value
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
2023-05-09 14:55:40 +02:00
|
|
|
get: Get<E> = (key: string) => {
|
2023-09-09 10:11:16 +02:00
|
|
|
return this._var ? this._var[key] : undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
// c.var.propName is a read-only
|
2023-11-12 01:06:40 +01:00
|
|
|
get var(): Readonly<E['Variables'] & ContextVariableMap> {
|
|
|
|
return { ...this._var } as never
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:54:47 +01:00
|
|
|
newResponse: NewResponse = (
|
|
|
|
data: Data | null,
|
|
|
|
arg?: StatusCode | ResponseInit,
|
|
|
|
headers?: HeaderRecord
|
|
|
|
): Response => {
|
2023-01-17 10:30:02 +01:00
|
|
|
// Optimized
|
2023-12-04 09:29:24 +01:00
|
|
|
if (this.#isFresh && !headers && !arg && this.#status === 200) {
|
2023-01-04 16:01:46 +01:00
|
|
|
return new Response(data, {
|
2023-12-04 09:29:24 +01:00
|
|
|
headers: this.#preparedHeaders,
|
2023-01-04 16:01:46 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:54:47 +01:00
|
|
|
if (arg && typeof arg !== 'number') {
|
2023-12-12 05:04:53 +01:00
|
|
|
this.res = new Response(data, arg)
|
2023-03-09 15:54:47 +01:00
|
|
|
}
|
|
|
|
|
2023-12-13 08:21:53 +01:00
|
|
|
const status = typeof arg === 'number' ? arg : arg ? arg.status : this.#status
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#preparedHeaders ??= {}
|
2023-01-04 16:01:46 +01:00
|
|
|
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#headers ??= new Headers()
|
|
|
|
for (const [k, v] of Object.entries(this.#preparedHeaders)) {
|
|
|
|
this.#headers.set(k, v)
|
2023-01-04 16:01:46 +01:00
|
|
|
}
|
|
|
|
|
2023-12-04 09:29:24 +01:00
|
|
|
if (this.#res) {
|
|
|
|
this.#res.headers.forEach((v, k) => {
|
|
|
|
this.#headers?.set(k, v)
|
2023-01-04 16:01:46 +01:00
|
|
|
})
|
2023-12-04 09:29:24 +01:00
|
|
|
for (const [k, v] of Object.entries(this.#preparedHeaders)) {
|
|
|
|
this.#headers.set(k, v)
|
2023-01-04 16:01:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
headers ??= {}
|
|
|
|
for (const [k, v] of Object.entries(headers)) {
|
|
|
|
if (typeof v === 'string') {
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#headers.set(k, v)
|
2023-01-04 16:01:46 +01:00
|
|
|
} else {
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#headers.delete(k)
|
2023-01-04 16:01:46 +01:00
|
|
|
for (const v2 of v) {
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#headers.append(k, v2)
|
2023-01-04 16:01:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 14:41:45 +02:00
|
|
|
return new Response(data, {
|
2022-11-23 10:42:24 +01:00
|
|
|
status,
|
2023-12-04 09:29:24 +01:00
|
|
|
headers: this.#headers,
|
2022-09-20 14:41:45 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:54:47 +01:00
|
|
|
body: BodyRespond = (
|
2023-01-18 00:42:23 +01:00
|
|
|
data: Data | null,
|
2023-08-20 17:23:05 +02:00
|
|
|
arg?: StatusCode | ResponseInit,
|
2023-01-18 00:42:23 +01:00
|
|
|
headers?: HeaderRecord
|
|
|
|
): Response => {
|
2023-03-09 15:54:47 +01:00
|
|
|
return typeof arg === 'number'
|
|
|
|
? this.newResponse(data, arg, headers)
|
|
|
|
: this.newResponse(data, arg)
|
2023-01-04 16:01:46 +01:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:54:47 +01:00
|
|
|
text: TextRespond = (
|
|
|
|
text: string,
|
2023-08-20 17:23:05 +02:00
|
|
|
arg?: StatusCode | ResponseInit,
|
2023-03-09 15:54:47 +01:00
|
|
|
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-12-04 09:29:24 +01:00
|
|
|
if (!this.#preparedHeaders) {
|
|
|
|
if (this.#isFresh && !headers && !arg) {
|
2023-01-17 10:30:02 +01:00
|
|
|
return new Response(text)
|
|
|
|
}
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#preparedHeaders = {}
|
2022-12-14 14:03:02 +01:00
|
|
|
}
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#preparedHeaders['content-type'] = TEXT_PLAIN
|
2023-03-09 15:54:47 +01:00
|
|
|
return typeof arg === 'number'
|
|
|
|
? this.newResponse(text, arg, headers)
|
|
|
|
: this.newResponse(text, arg)
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
2023-12-03 01:03:14 +01:00
|
|
|
json: JSONRespond = <T>(
|
2023-12-04 14:14:49 +01:00
|
|
|
object: InterfaceToType<T> extends JSONValue ? T : JSONValue,
|
2023-08-20 17:23:05 +02:00
|
|
|
arg?: StatusCode | ResponseInit,
|
2023-02-01 16:25:58 +01:00
|
|
|
headers?: HeaderRecord
|
2023-12-03 01:03:14 +01:00
|
|
|
): Response &
|
|
|
|
TypedResponse<
|
|
|
|
InterfaceToType<T> extends JSONValue
|
|
|
|
? JSONValue extends InterfaceToType<T>
|
|
|
|
? never
|
2023-12-13 01:01:34 +01:00
|
|
|
: JSONParsed<T>
|
2023-12-03 01:03:14 +01:00
|
|
|
: never
|
|
|
|
> => {
|
2023-08-05 11:13:39 +02:00
|
|
|
const body = JSON.stringify(object)
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#preparedHeaders ??= {}
|
|
|
|
this.#preparedHeaders['content-type'] = 'application/json; charset=UTF-8'
|
2023-12-03 01:03:14 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
return (
|
|
|
|
typeof arg === 'number' ? this.newResponse(body, arg, headers) : this.newResponse(body, arg)
|
|
|
|
) as any
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
2023-12-03 01:03:14 +01:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* `c.jsonT()` will be removed in v4.
|
|
|
|
* Use `c.json()` instead of `c.jsonT()`.
|
|
|
|
* `c.json()` now returns data type, so you can just replace `c.jsonT()` to `c.json()`.
|
|
|
|
*/
|
|
|
|
jsonT: JSONRespond = <T>(
|
2023-12-04 14:14:49 +01:00
|
|
|
object: InterfaceToType<T> extends JSONValue ? T : JSONValue,
|
2023-08-20 17:23:05 +02:00
|
|
|
arg?: StatusCode | ResponseInit,
|
2023-01-18 00:42:23 +01:00
|
|
|
headers?: HeaderRecord
|
2023-12-03 01:03:14 +01:00
|
|
|
): Response &
|
|
|
|
TypedResponse<
|
|
|
|
InterfaceToType<T> extends JSONValue
|
|
|
|
? JSONValue extends InterfaceToType<T>
|
|
|
|
? never
|
|
|
|
: T
|
|
|
|
: never
|
|
|
|
> => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
return this.json(object, arg as any, headers) as any
|
2022-12-28 03:25:48 +01:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:54:47 +01:00
|
|
|
html: HTMLRespond = (
|
2023-11-06 23:09:04 +01:00
|
|
|
html: string | Promise<string>,
|
2023-08-20 17:23:05 +02:00
|
|
|
arg?: StatusCode | ResponseInit,
|
2023-03-09 15:54:47 +01:00
|
|
|
headers?: HeaderRecord
|
2023-11-06 23:09:04 +01:00
|
|
|
): Response | Promise<Response> => {
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#preparedHeaders ??= {}
|
|
|
|
this.#preparedHeaders['content-type'] = 'text/html; charset=UTF-8'
|
2023-11-06 23:09:04 +01:00
|
|
|
|
|
|
|
if (typeof html === 'object') {
|
|
|
|
if (!(html instanceof Promise)) {
|
|
|
|
html = (html as string).toString() // HtmlEscapedString object to string
|
|
|
|
}
|
|
|
|
if ((html as string | Promise<string>) instanceof Promise) {
|
2023-11-21 10:05:05 +01:00
|
|
|
return (html as unknown as Promise<string>)
|
|
|
|
.then((html) => resolveStream(html))
|
|
|
|
.then((html) => {
|
|
|
|
return typeof arg === 'number'
|
|
|
|
? this.newResponse(html, arg, headers)
|
|
|
|
: this.newResponse(html, arg)
|
|
|
|
})
|
2023-11-06 23:09:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:54:47 +01:00
|
|
|
return typeof arg === 'number'
|
2023-11-06 23:09:04 +01:00
|
|
|
? this.newResponse(html as string, arg, headers)
|
|
|
|
: this.newResponse(html as string, arg)
|
2023-01-04 16:01:46 +01:00
|
|
|
}
|
|
|
|
|
2023-01-18 00:42:23 +01:00
|
|
|
redirect = (location: string, status: StatusCode = 302): Response => {
|
2023-12-04 09:29:24 +01:00
|
|
|
this.#headers ??= new Headers()
|
|
|
|
this.#headers.set('Location', location)
|
2023-01-04 16:01:46 +01:00
|
|
|
return this.newResponse(null, status)
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
2023-12-26 22:55:24 +01:00
|
|
|
/** @deprecated
|
|
|
|
* Use `streamText()` in `hono/streaming` instead of `c.streamText()`. The `c.streamText()` will be removed in v4.
|
|
|
|
*/
|
2023-09-16 16:28:57 +02:00
|
|
|
streamText = (
|
|
|
|
cb: (stream: StreamingApi) => Promise<void>,
|
|
|
|
arg?: StatusCode | ResponseInit,
|
|
|
|
headers?: HeaderRecord
|
|
|
|
): Response => {
|
|
|
|
headers ??= {}
|
2023-09-20 15:53:30 +02:00
|
|
|
this.header('content-type', TEXT_PLAIN)
|
|
|
|
this.header('x-content-type-options', 'nosniff')
|
|
|
|
this.header('transfer-encoding', 'chunked')
|
2023-09-16 16:28:57 +02:00
|
|
|
return this.stream(cb, arg, headers)
|
|
|
|
}
|
|
|
|
|
2023-12-26 22:55:24 +01:00
|
|
|
/** @deprecated
|
|
|
|
* Use `stream()` in `hono/streaming` instead of `c.stream()`. The `c.stream()` will be removed in v4.
|
|
|
|
*/
|
2023-09-16 16:28:57 +02:00
|
|
|
stream = (
|
|
|
|
cb: (stream: StreamingApi) => Promise<void>,
|
|
|
|
arg?: StatusCode | ResponseInit,
|
|
|
|
headers?: HeaderRecord
|
|
|
|
): Response => {
|
|
|
|
const { readable, writable } = new TransformStream()
|
|
|
|
const stream = new StreamingApi(writable)
|
|
|
|
cb(stream).finally(() => stream.close())
|
|
|
|
|
|
|
|
return typeof arg === 'number'
|
|
|
|
? this.newResponse(readable, arg, headers)
|
|
|
|
: this.newResponse(readable, arg)
|
|
|
|
}
|
|
|
|
|
2023-05-05 04:08:03 +02: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-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
|
|
|
|
2023-08-21 08:22:37 +02:00
|
|
|
/** @deprecated
|
|
|
|
* Use `getRuntimeKey()` exported from `hono/adapter` instead of `c.runtime()`. The `c.runtime()` will be removed in v4.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* import { getRuntimeKey } from 'hono/adapter'
|
|
|
|
* // ...
|
|
|
|
* app.get('/', (c) => {
|
|
|
|
* const key = getRuntimeKey()
|
|
|
|
* //...
|
|
|
|
* })
|
|
|
|
*/
|
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-05-02 09:26:39 +02:00
|
|
|
if (global?.fastly !== undefined) {
|
2023-01-08 01:05:49 +01:00
|
|
|
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
|
|
|
}
|