-
-
Notifications
You must be signed in to change notification settings - Fork 446
/
context.ts
384 lines (336 loc) · 10.8 KB
/
context.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import { HonoRequest } from './request.ts'
import type { TypedResponse } from './types.ts'
import type { Env, NotFoundHandler, Input } from './types.ts'
import type { CookieOptions } from './utils/cookie.ts'
import { serialize } from './utils/cookie.ts'
import type { StatusCode } from './utils/http-status.ts'
import type { JSONValue } from './utils/types.ts'
type Runtime = 'node' | 'deno' | 'bun' | 'workerd' | 'fastly' | 'edge-light' | 'lagon' | 'other'
type HeaderRecord = Record<string, string | string[]>
type Data = string | ArrayBuffer | ReadableStream
export interface ExecutionContext {
waitUntil(promise: Promise<unknown>): void
passThroughOnException(): void
}
export interface ContextVariableMap {}
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 {
<T>(
object: T extends JSONValue ? T : JSONValue,
status?: StatusCode,
headers?: HeaderRecord
): TypedResponse<T extends JSONValue ? (JSONValue extends T ? never : T) : never>
<T>(object: T extends JSONValue ? T : JSONValue, init?: ResponseInit): TypedResponse<
T extends JSONValue ? (JSONValue extends T ? never : T) : never
>
}
interface HTMLRespond {
(html: string, status?: StatusCode, headers?: HeaderRecord): Response
(html: string, init?: ResponseInit): Response
}
type GetVariable<K, E extends Env> = K extends keyof E['Variables']
? E['Variables'][K]
: K extends keyof ContextVariableMap
? ContextVariableMap[K]
: unknown
type ContextOptions<E extends Env> = {
env: E['Bindings']
executionCtx?: FetchEvent | ExecutionContext | undefined
notFoundHandler?: NotFoundHandler<E>
path?: string
paramData?: Record<string, string>
}
export class Context<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
E extends Env = any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
P extends string = any,
I extends Input = {}
> {
env: E['Bindings'] = {}
finalized: boolean = false
error: Error | undefined = undefined
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _req?: HonoRequest<any, any>
private _status: StatusCode = 200
private _executionCtx: FetchEvent | ExecutionContext | undefined
private _pretty: boolean = false
private _prettySpace: number = 2
private _map: Record<string, unknown> | undefined
private _headers: Headers | undefined = undefined
private _preparedHeaders: Record<string, string> | undefined = undefined
private _res: Response | undefined
private _path: string = '/'
private _paramData?: Record<string, string> | null
private rawRequest?: Request | null
private notFoundHandler: NotFoundHandler<E> = () => new Response()
constructor(req: Request, options?: ContextOptions<E>) {
this.rawRequest = req
if (options) {
this._executionCtx = options.executionCtx
this._path = options.path ?? '/'
this._paramData = options.paramData
this.env = options.env
if (options.notFoundHandler) {
this.notFoundHandler = options.notFoundHandler
}
}
}
get req(): HonoRequest<P, I['out']> {
if (this._req) {
return this._req
} else {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this._req = new HonoRequest(this.rawRequest!, this._path, this._paramData!)
this.rawRequest = undefined
this._paramData = undefined
return this._req
}
}
get event(): FetchEvent {
if (this._executionCtx instanceof FetchEvent) {
return this._executionCtx
} else {
throw Error('This context has no FetchEvent')
}
}
get executionCtx(): ExecutionContext {
if (this._executionCtx) {
return this._executionCtx as ExecutionContext
} else {
throw Error('This context has no ExecutionContext')
}
}
get res(): Response {
return (this._res ||= new Response('404 Not Found', { status: 404 }))
}
set res(_res: Response | undefined) {
if (this._res && _res) {
this._res.headers.delete('content-type')
this._res.headers.forEach((v, k) => {
_res.headers.set(k, v)
})
}
this._res = _res
this.finalized = true
}
header = (name: string, value: string, options?: { append?: boolean }): void => {
if (options?.append) {
if (!this._headers) {
this._headers = new Headers(this._preparedHeaders)
this._preparedHeaders = {}
}
this._headers.append(name, value)
} else {
if (this._headers) {
this._headers.set(name, value)
} else {
this._preparedHeaders ??= {}
this._preparedHeaders[name.toLowerCase()] = value
}
}
if (this.finalized) {
if (options?.append) {
this.res.headers.append(name, value)
} else {
this.res.headers.set(name, value)
}
}
}
status = (status: StatusCode): void => {
this._status = status
}
set = <Key extends keyof E['Variables'] | keyof ContextVariableMap>(
key: Key,
value: GetVariable<Key, E>
): void => {
this._map ||= {}
this._map[key as string] = value
}
get = <Key extends keyof E['Variables'] | keyof ContextVariableMap>(
key: Key
): GetVariable<Key, E> => {
return this._map?.[key as string] as GetVariable<Key, E>
}
pretty = (prettyJSON: boolean, space: number = 2): void => {
this._pretty = prettyJSON
this._prettySpace = space
}
newResponse: NewResponse = (
data: Data | null,
arg?: StatusCode | ResponseInit,
headers?: HeaderRecord
): Response => {
// Optimized
if (!headers && !this._headers && !this._res && !arg && this._status === 200) {
return new Response(data, {
headers: this._preparedHeaders,
})
}
// Return Response immediately if arg is RequestInit.
if (arg && typeof arg !== 'number') {
const res = new Response(data, arg)
const contentType = this._preparedHeaders?.['content-type']
if (contentType) {
res.headers.set('content-type', contentType)
}
return res
}
const status = arg ?? this._status
this._preparedHeaders ??= {}
this._headers ??= new Headers()
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)
}
}
}
return new Response(data, {
status,
headers: this._headers,
})
}
body: BodyRespond = (
data: Data | null,
arg?: StatusCode | RequestInit,
headers?: HeaderRecord
): Response => {
return typeof arg === 'number'
? this.newResponse(data, arg, headers)
: this.newResponse(data, arg)
}
text: TextRespond = (
text: string,
arg?: StatusCode | RequestInit,
headers?: HeaderRecord
): Response => {
// If the header is empty, return Response immediately.
// Content-Type will be added automatically as `text/plain`.
if (!this._preparedHeaders) {
if (!headers && !this._res && !this._headers && !arg) {
return new Response(text)
}
this._preparedHeaders = {}
}
// 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=UTF-8'
}
return typeof arg === 'number'
? this.newResponse(text, arg, headers)
: this.newResponse(text, arg)
}
json: JSONRespond = <T = {}>(
object: T,
arg?: StatusCode | RequestInit,
headers?: HeaderRecord
) => {
const body = this._pretty
? JSON.stringify(object, null, this._prettySpace)
: JSON.stringify(object)
this._preparedHeaders ??= {}
this._preparedHeaders['content-type'] = 'application/json; charset=UTF-8'
return typeof arg === 'number'
? this.newResponse(body, arg, headers)
: this.newResponse(body, arg)
}
jsonT: JSONTRespond = <T>(
object: T extends JSONValue ? T : JSONValue,
arg?: StatusCode | RequestInit,
headers?: HeaderRecord
): TypedResponse<T extends JSONValue ? (JSONValue extends T ? never : T) : never> => {
return {
response: typeof arg === 'number' ? this.json(object, arg, headers) : this.json(object, arg),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data: object as any,
format: 'json',
}
}
html: HTMLRespond = (
html: string,
arg?: StatusCode | RequestInit,
headers?: HeaderRecord
): Response => {
this._preparedHeaders ??= {}
this._preparedHeaders['content-type'] = 'text/html; charset=UTF-8'
return typeof arg === 'number'
? this.newResponse(html, arg, headers)
: this.newResponse(html, arg)
}
redirect = (location: string, status: StatusCode = 302): Response => {
this._headers ??= new Headers()
this._headers.set('Location', location)
return this.newResponse(null, status)
}
cookie = (name: string, value: string, opt?: CookieOptions): void => {
const cookie = serialize(name, value, opt)
this.header('set-cookie', cookie, { append: true })
}
notFound = (): Response | Promise<Response> => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return this.notFoundHandler(this)
}
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') {
return 'workerd'
}
if (typeof global?.EdgeRuntime === 'string') {
return 'edge-light'
}
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'
}
if (global?.__lagon__ !== undefined) {
return 'lagon'
}
if (global?.process?.release?.name === 'node') {
return 'node'
}
return 'other'
}
}