-
Notifications
You must be signed in to change notification settings - Fork 2
/
http_deno.mjs
333 lines (273 loc) · 9.02 KB
/
http_deno.mjs
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
/* global Deno */
import * as l from './lang.mjs'
import * as o from './obj.mjs'
import * as s from './str.mjs'
import * as u from './url.mjs'
import * as h from './http.mjs'
import * as io from './io_deno.mjs'
import * as p from './path.mjs'
// TODO move to a non-Deno-specific file.
export class ContentTypeMap extends o.MixMain(Map) {
guess(val) {return this.get(p.posix.ext(val))}
static default() {
return new this()
.set(`.css`, `text/css`)
.set(`.gif`, `image/gif`)
.set(`.htm`, `text/html`)
.set(`.html`, `text/html`)
.set(`.ico`, `image/x-icon`)
.set(`.jpeg`, `image/jpeg`)
.set(`.jpg`, `image/jpeg`)
.set(`.js`, `application/javascript`)
.set(`.json`, `application/json`)
.set(`.mjs`, `application/javascript`)
.set(`.pdf`, `application/pdf`)
.set(`.png`, `image/png`)
.set(`.svg`, `image/svg+xml`)
.set(`.tif`, `image/tiff`)
.set(`.tiff`, `image/tiff`)
.set(`.xml`, `text/xml`)
.set(`.zip`, `application/zip`)
.set(`.webp`, `image/webp`)
.set(`.woff`, `font/woff`)
.set(`.woff2`, `font/woff2`)
}
}
export class DirBase extends l.Emp {
urlPathToFsPath() {return undefined}
fsPathToUrlPath() {return undefined}
fsStat(val) {return this.FileInfo.statOpt(val)}
resolve(url) {return this.fsStat(this.urlPathToFsPath(url))}
async resolveFile(url) {return (await this.resolve(url))?.onlyFile()}
async resolveSiteFile(url) {
url = u.url(url)
const info = await this.resolveFile(url)
if (info) return info
if (p.posix.ext(url.pathname)) return undefined
if (url.pathname.endsWith(`/`)) {
return this.resolveFile(url.addPath(this.index))
}
return this.resolveFile(url.pathname + `.html`)
}
async resolveSiteFileWithNotFound(url) {
return (await this.resolveSiteFile(url)) || (await this.resolveNotFound())
}
// TODO: code 404.
resolveNotFound() {return this.resolveFile(this.notFound)}
get index() {return `index.html`}
get notFound() {return `404.html`}
get FileInfo() {return HttpFileInfo}
}
export function dirAbs() {return new DirAbs()}
export class DirAbs extends DirBase {
urlPathToFsPath(val) {
if (io.IS_WINDOWS) return unslashPre(toFsPathNorm(val))
return toFsPathNorm(val)
}
fsPathToUrlPath(val) {
if (io.paths.isAbs(val)) return slashPre(val)
return undefined
}
}
// Short for "directory relative".
export class DirRel extends DirBase {
constructor(base) {super().base = l.reqStr(base)}
urlPathToFsPath(val) {
val = unslashPre(toFsPathNorm(val))
if (this.testUrlPath(val)) return p.posix.join(this.base, val)
return undefined
}
fsPathToUrlPath(val) {
if (this.testFsPath(val)) return slashPre(this.fsPathRel(val))
return undefined
}
fsPathRel(val) {return p.posix.strictRelTo(fsPathNorm(val), this.base)}
testUrlPath(val) {return l.isStr(val) && !hasDotDot(val)}
testFsPath(val) {return p.posix.isSubOf(fsPathNorm(val), this.base)}
}
export function dirRel(base, fil) {return new DirRelFil(base, fil)}
// Short for "directory relative with filter".
export class DirRelFil extends DirRel {
constructor(base, fil) {
super(base).fil = l.toInstOpt(fil, this.Fil)
}
testUrlPath(val) {return super.testUrlPath(val) && this.test(val)}
testFsPath(val) {return super.testFsPath(val) && this.test(this.fsPathRel(val))}
test(val) {return !this.fil || !!this.fil.test(val)}
get Fil() {return Fil}
}
export class Dirs extends Array {
resolve(val) {
val = u.url(val)
return this.procure(function iter(dir) {return dir.resolve(val)})
}
resolveFile(val) {
val = u.url(val)
return this.procure(function iter(dir) {return dir.resolveFile(val)})
}
resolveSiteFile(val) {
val = u.url(val)
return this.procure(function iter(dir) {return dir.resolveSiteFile(val)})
}
resolveNotFound(val) {
val = u.url(val)
return this.procure(function iter(dir) {return dir.resolveNotFound(val)})
}
async resolveSiteFileWithNotFound(val) {
val = u.url(val)
return (await this.resolveSiteFile(val)) || (await this.resolveNotFound(val))
}
async procure(fun) {
for (const val of this) {
const out = await fun(val)
if (out) return out
}
return undefined
}
fsPathToUrlPath(val) {
for (const dir of this) {
const out = dir.fsPathToUrlPath(val)
if (l.isSome(out)) return out
}
return undefined
}
static of(...val) {return super.of(...val.filter(l.id))}
}
export class HttpFileInfo extends io.FileInfo {
res(opt) {return this.HttpFileStream.res(this.path, opt)}
stream() {return this.HttpFileStream.open(this.path)}
// Not used automatically. May be used by user code.
etag() {
const {size, mtime, birthtime} = this.stat
return s.dashed(birthtime?.valueOf(), mtime?.valueOf(), size)
}
etagQuoted() {return JSON.stringify(l.reqStr(this.etag()))}
etagQuotedWeak() {return s.maybePre(this.etagQuoted(), `W/`)}
get HttpFileStream() {return HttpFileStream}
}
export class HttpFileStream extends io.FileStream {
res(opt) {
try {
const res = new this.Res(this, l.optStruct(opt))
this.setTypeOpt(res.headers)
return res
}
catch (err) {
this.deinit()
throw err
}
}
type() {return ContentTypeMap.main.guess(this.path)}
setType(head) {
const val = this.type()
if (val) head.set(h.HEADER_NAME_CONTENT_TYPE, val)
}
setTypeOpt(head) {
if (!head.get(h.HEADER_NAME_CONTENT_TYPE)) this.setType(head)
}
static async res(path, opt) {
return (await this.open(path)).res(opt)
}
get Res() {return Response}
}
// TODO ability to store multiple listeners.
// TODO convert to `Deno.serve` API.
export class Srv extends l.Emp {
lis = undefined
listen(opt) {
this.deinit()
this.lis = this.listener(opt)
this.onListen(opt)
return this.serve(this.lis)
}
listener(opt) {
if (opt?.certFile || opt?.keyFile) return Deno.listenTls(opt)
return Deno.listen(opt)
}
async serve(lis) {
for await (const conn of lis) {
const onConnErr = err => this.onConnErr(err, conn)
this.serveConn(conn).catch(onConnErr)
}
}
async serveConn(conn) {
for await (const event of Deno.serveHttp(conn)) {
const onEventErr = err => this.onEventErr(err, event)
event.respondWith(l.reqPromise(this.response(event.request))).catch(onEventErr)
}
}
/*
To minimize error potential, subclasses should override `.res` and `.errRes`
rather than this.
This method must ALWAYS be async. Our error handling assumes that we can pass
the resulting promise to `event.respondWith` without having to handle
synchronous exceptions resulting from the call in cases where the response
might be malformed.
*/
async response(req) {
try {return await this.res(req)}
catch (err) {
try {return await this.errRes(err, req)}
catch (err) {
try {
this.onReqErr(err, req)
return errRes(err, req)
}
catch (err) {
console.error(err)
return new Response(`unknown error`, {status: 500})
}
}
}
}
// Subclasses should override this.
res() {return new Response()}
onErr(err) {if (!isErrCancel(err)) console.error(err)}
onConnErr(err, conn) {this.onErr(err, conn)}
onEventErr(err, event) {this.onErr(err, event)}
onReqErr(err, req) {this.onErr(err, req)}
errRes(err, req) {return errRes(err, req)}
onListen(opt) {
console.log(`[${this.constructor.name}] listening on http://${opt.hostname || `localhost`}:${opt.port}`)
}
deinit() {
try {this.lis?.close()}
finally {this.lis = undefined}
}
}
export function errRes(err) {return new Response(l.show(err), {status: 500})}
// Short for "filter".
export class Fil extends l.Emp {
constructor(val) {
super()
this.val = l.req(val, this.isTest)
}
isTest(val) {return l.isFun(val) || l.hasMeth(val, `test`)}
test(val) {return l.isFun(this.val) ? this.val(val) : this.val.test(val)}
}
export function isErrCancel(val) {
return (
h.isErrAbort(val) ||
l.isInst(val, Deno.errors.ConnectionAborted) ||
(l.isInst(val, Deno.errors.Http) && val.message.includes(`connection closed`))
)
}
/*
Various features of this module take arbitrary paths as inputs, including URL
paths and full URLs. After decoding the pathname, we always normalize it to
Posix-style to simplify path testing in `DirRelFil`. On Windows, FS operations
in Deno and Node work even with `/` instead of `\`.
*/
function fsPathNorm(val) {return p.toPosix(val)}
function toFsPathNorm(val) {return fsPathNorm(toFsPath(val))}
function toFsPath(val) {return urlDec(toPathname(val))}
// May consider moving to `url.mjs`.
function toPathname(val) {
if (l.isStr(val) && u.RE_PATHNAME.test(val)) return val
return u.toUrl(val).pathname
}
function urlDec(val) {return decodeURIComponent(l.reqStr(val))}
function slashPre(val) {return s.optPre(val, `/`)}
function unslashPre(val) {return blank(val, /^(?:[/\\])*/g)}
function blank(val, reg) {return l.reqStr(val).replace(reg, ``)}
function hasDotDot(val) {return l.reqStr(val).includes(`..`)}