-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttp_deno.mjs
More file actions
46 lines (37 loc) · 1.3 KB
/
http_deno.mjs
File metadata and controls
46 lines (37 loc) · 1.3 KB
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
/* global Deno */
import * as l from './lang.mjs'
import * as io from './io_deno.mjs'
import * as hs from './http_srv.mjs'
export * from './http.mjs'
export * from './http_srv.mjs'
export function serve(opt) {
const {onRequest, ...rest} = l.optRec(opt)
return Deno.serve({handler: onRequest, ...rest})
}
export function srvUrl({hostname, port}) {
if (hostname === `0.0.0.0` || hostname === `::1`) hostname = ``
const url = new URL(`http://` + (hostname || `localhost`))
url.port = port
return url
}
export class HttpFile extends hs.BaseHttpFile {
static async resolve(opt) {
const {fsPath} = opt
l.reqScalar(fsPath)
const info = await io.statOpt(fsPath)
if (!info?.isFile) return undefined
const out = new this(opt)
out.setOpt({info})
return out
}
async response(opt) {
l.optRec(opt)
const Res = opt?.Res ?? this.Res
const body = this.bytes ?? this.text ?? (await Deno.open(this.fsPath)).readable
return new Res(body, await this.resOpt(opt?.resOpt))
}
async getBytes() {return this.bytes ??= await io.readFileBytes(this.fsPath)}
async getText() {return this.text ??= await io.readFileText(this.fsPath)}
async getInfo() {return this.info ??= await io.stat(this.fsPath)}
}
export class HttpDir extends hs.BaseHttpDir {get HttpFile() {return HttpFile}}