-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
127 lines (109 loc) · 3.08 KB
/
mod.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
import {
extname,
} from "https://deno.land/[email protected]/path/mod.ts"
import { contentType } from "https://deno.land/[email protected]/media_types/content_type.ts"
import { transpile } from "./src/transpile.ts"
import {
relative,
join,
normalize,
fromFileUrl,
} from "https://deno.land/[email protected]/path/mod.ts"
const cwd = Deno.cwd()
const scriptDir = join(fromFileUrl(import.meta.url), "..")
console.log({cwd, scriptDir})
const relPath = normalize(relative(scriptDir, cwd))
const relImport = async (path: string) => {
const adjustedPath = join(relPath, path).replaceAll("\\", "/")
return await import("./" + adjustedPath)
}
const getFile = async (filepath: string) =>
await Deno.open(
filepath,
{ read: true }
)
.then(file => new Response(file.readable))
.catch(e => new Response(e, { status: 404 }))
const handler =
(transformer: Record<
string,
(filepath: string, url: URL, req: Request) => Promise<Response>
>) =>
async (req: Request) => {
const url = new URL(req.url)
const filepath = "." + decodeURIComponent(url.pathname)
console.log("Request:", filepath)
const ext = extname(filepath).substring(1)
let response
if (ext in transformer) {
response = await transformer[ext](filepath, url, req)
} else {
response = await getFile(filepath)
}
response.headers.get("content-type") ||
response.headers.set(
"content-type",
contentType(extname(filepath))!,
)
return response
}
const handleTs =
async (filepath: string, url: URL) => {
if (url.searchParams.get("ts") == "true") {
const response = await getFile(filepath)
response.headers.set(
"content-type",
"text/plain",
)
return response
} else {
const label = `Transpile "${filepath}"`
console.time(label)
const result = await transpile(filepath)
console.timeEnd(label)
return new Response(
result,
{ headers: {
"content-type": "application/javascript",
"x-typescript-types": appendParam("ts", "true")(url).href,
}}
)
}
}
const htmlPreset =
(body: string) => `<!doctype html><html>
<head>
<meta charset="UTF-8">
</head>
<body>
${body}
</body>
</html>`
let cacheBuster = 0
Deno.serve(handler({
ts: handleTs,
async tsx(filepath, url, req) {
const accept = req.headers.get("accept") || ""
if (accept.includes("text/html")) {
return new Response(
htmlPreset(
(await relImport(
filepath + "#" + cacheBuster++
)).default
),
{ headers: {
"content-type": "text/html",
}}
)
} else {
return await handleTs(filepath, url)
}
}
}))
const appendParam =
(name: string, value: string) =>
(url: URL | string) => {
url = new URL(url)
url.searchParams.append(name, value)
return url
}