-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.ts
344 lines (304 loc) · 9.24 KB
/
app.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
import { path } from './deps.ts'
import { extendMiddleware } from './extend.ts'
import { getRouteFromApp } from './extensions/req/route.ts'
import { onErrorHandler } from './onError.ts'
import { THRequest } from './request.ts'
import { DummyResponse, THResponse } from './response.ts'
import { pushMiddleware, Router, UseMethodParams } from './router.ts'
import type {
AppConstructor,
AppSettings,
Handler,
Middleware,
NextFunction,
} from './types.ts'
import { TemplateEngineOptions, TemplateFunc } from './utils/template.ts'
/**
* Add leading slash if not present (e.g. path -> /path, /path -> /path)
* @param x
*/
const lead = (x: string) => (x.charCodeAt(0) === 47 ? x : '/' + x)
/**
* Add trailing slash if not present (e.g. path -> path/, path/ -> path/)
* @param x
*/
const trail = (x: string) => (x.charCodeAt(x.length - 1) === 47 ? x : x + '/')
const applyHandler =
<Req extends THRequest = THRequest, Res extends THResponse = THResponse>(
h: Handler<Req, Res>,
) =>
async (req: Req, res: Res, next: NextFunction) => {
try {
await h(req, res, next!)
} catch (e) {
await next(e)
}
}
async function notFound(req: Request, res: THResponse) {
res.status(404)
await res.send(
`Cannot ${req.method} ${new URL(req.url).pathname}`,
)
}
const mount = (fn: App | Handler) => (fn instanceof App ? fn.attach : fn)
export class App<
RenderOptions = any,
Req extends THRequest = THRequest,
Res extends THResponse<RenderOptions> = THResponse<RenderOptions>,
> extends Router<App, Req, Res> {
middleware: Middleware<Req, Res>[]
settings: AppSettings & { views?: string } & Record<string, unknown>
locals: Record<string, string> = {}
engines: Record<string, TemplateFunc<RenderOptions>> = {}
onError: (err: unknown, req?: Request) => Response | Promise<Response>
notFound: Handler<Req, Res>
attach: (req: Req, res: Res, next: NextFunction) => void
// this symbol tells if a custom error handler has been set
// and thus, helps determine the error handling precedence
#hasSetCustomErrorHandler: boolean
constructor(options: AppConstructor<Req, Res> = {}) {
super()
this.settings = options.settings || { xPoweredBy: true }
this.middleware = []
this.onError = options?.onError || onErrorHandler
this.notFound = options?.noMatchHandler || notFound
this.#hasSetCustomErrorHandler = !!(options?.onError)
this.attach = (req, res) => this.#prepare.bind(this, req, res)()
}
/**
* Set app setting
* @param setting setting name
* @param value setting value
*/
set<T = unknown>(setting: string, value: T): this {
this.settings[setting] = value
return this
}
/**
* Enable app setting
* @param setting Setting name
*/
enable(setting: string): this {
this.settings[setting] = true
return this
}
/**
* Disable app setting
* @param setting
*/
disable(setting: string): this {
this.settings[setting] = false
return this
}
/**
* Render a template
* @param file What to render
* @param data data that is passed to a template
* @param options Template engine options
* @param cb Callback that consumes error and html
*/
render(
file: string,
data: Record<string, unknown> = {},
cb?: (err: Error | null, html: string) => void,
options: TemplateEngineOptions<RenderOptions> = {},
): this {
options.viewsFolder = options.viewsFolder || this.settings.views ||
`${Deno.cwd()}/views`
options.ext = options.ext || file.slice(file.lastIndexOf('.') + 1) || 'ejs'
options._locals = options._locals || {}
let locals = { ...data, ...this.locals }
if (options._locals) locals = { ...locals, ...options._locals }
if (!file.endsWith(`.${options.ext}`)) file = `${file}.${options.ext}`
const dest = options.viewsFolder
? path.join(options.viewsFolder, file)
: file
this.engines[options.ext](dest, locals, options.renderOptions!, cb)
return this
}
use(...args: UseMethodParams<Req, Res, App>): this {
const base = args[0]
const fns = args.slice(1).flat()
if (typeof base === 'function' || base instanceof App) {
fns.unshift(base)
} else if (Array.isArray(base)) {
fns.unshift(...base)
}
const path = typeof base === 'string' ? base : '/'
for (const fn of fns) {
if (fn instanceof App) {
fn.mountpath = path
this.apps[path] = fn
fn.parent = this as any
}
}
const handlerPaths = []
const handlerFunctions = []
const handlerPathBase = path === '/' ? '' : lead(path)
for (const fn of fns) {
if (fn instanceof App && fn.middleware?.length) {
for (const mw of fn.middleware) {
handlerPaths.push(handlerPathBase + lead(mw.path!))
handlerFunctions.push(fn)
}
} else {
handlerPaths.push('')
handlerFunctions.push(fn)
}
}
pushMiddleware(this.middleware)({
path,
type: 'mw',
handler: mount(handlerFunctions[0] as Handler),
// @ts-ignore type conflict
handlers: handlerFunctions.slice(1).map(mount),
fullPaths: handlerPaths,
})
return this
}
/**
* Register a template engine with extension
*/
engine(ext: string, fn: TemplateFunc<RenderOptions>): this {
this.engines[ext] = fn
return this
}
route(path: string): App {
const app = new App()
this.use(path, app)
return app
}
#find(url: URL) {
const result = this.middleware.map((m) => {
const urlPath = m.fullPath! || (m.path!)
const joinedPath = lead(
path.posix.join(
this.mountpath,
typeof urlPath !== 'string' ? '/' : urlPath,
),
)
return {
...m,
pattern: new URLPattern({
pathname: m.type === 'mw'
? m.path === '/' ? `${trail(joinedPath)}([^\/]*)?` : '*'
: (urlPath === '/' ? trail(joinedPath) : joinedPath),
}),
}
}).filter((m) => {
return m.pattern.test(url)
})
return result
}
/**
* Extends Req / Res objects, pushes 404 and 500 handlers, dispatches middleware
* @param req Request object
*/
async #prepare(
req: Req,
res: { _init?: ResponseInit; _body?: BodyInit },
) {
req._urlObject = new URL(req.url)
const exts = extendMiddleware<RenderOptions>(this as unknown as App)
const matched = this.#find(req._urlObject).filter((x) =>
req.method === 'HEAD' || (x.method ? x.method === req.method : true)
)
const mw: Middleware<Req, Res>[] = 'fresh' in req ? matched : [
{
handler: exts,
type: 'mw',
path: '/',
},
...matched,
]
if (matched[0] != null) {
mw.push({
type: 'mw',
handler: async (req, res, next) => {
if (req.method === 'HEAD') {
res.status(204).end('')
} else await next()
},
path: '/',
})
}
mw.push({ type: 'mw', handler: this.notFound, path: '/' })
const handle =
(mw: Middleware<Req, Res>) =>
async (req: Req, res: Res, next: NextFunction) => {
const { handler, type, pattern } = mw
const params =
type === 'route' && pattern?.exec(req.url)?.pathname.groups || {}
req.params = params as Record<string, string>
if (this.settings?.enableReqRoute) {
req.route = getRouteFromApp(this.middleware as any, handler as any)
}
await applyHandler<Req, Res>(handler)(req, res, next)
}
let idx = 0, err: unknown | undefined
const next: NextFunction = async (error) => {
if (error) err = error
return await loop()
}
const loop = async () =>
idx < mw.length
? await handle(mw[idx++])(req, res as Res, next)
: undefined
await loop()
if (err instanceof Response) throw err
else if (err) {
if (this.#hasSetCustomErrorHandler) throw await this.onError(err, req)
else throw err
}
throw new Response(res._body, res._init)
}
handler = async (
_req: Request,
conn?: Deno.Conn,
) => {
const req = _req.clone() as Req
req.conn = conn! as unknown as Deno.Conn
const res: DummyResponse = {
_init: {
headers: new Headers(
this.settings.xPoweredBy
? {
'X-Powered-By': typeof this.settings.xPoweredBy === 'string'
? this.settings.xPoweredBy
: 'tinyhttp',
}
: {},
),
},
_body: undefined,
locals: {},
}
let err
try {
await this.#prepare(req, res)
} catch (error) {
err = error
}
if (err instanceof Response) return err
return await this.onError(err, req)
}
/**
* Creates HTTP server and dispatches middleware
* @param port server listening port
* @param Server callback after server starts listening
* @param host server listening host
*/
async listen(port: number, cb?: () => void, hostname?: string) {
const listener = Deno.listen({ hostname, port })
cb?.()
for await (const conn of listener) {
const requests = Deno.serveHttp(conn)
for await (const { request, respondWith } of requests) {
const response = await this.handler.bind(this, request, conn)()
if (response) {
respondWith(response)
}
}
}
}
}