-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
60 lines (55 loc) · 1.61 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
/*
emit@>0.25.0 doesn't work in Deno Deploy.
https://github.com/denoland/deno_cache/issues/35
*/
import { transpile } from "https://deno.land/x/[email protected]/mod.ts"
const handler =
async (request: Request) => {
const url = new URL(request.url)
try {
const target = new URL(url.pathname.substring(1))
const result = await transpile(
target,
{
cacheRoot: Deno.cwd(),
}
)
const originHeaders = await fetch(target).then(x => x.headers)
const cacheHeaders = Object.fromEntries(
[
"cache-control",
"age",
"expires",
"etag",
].map(header => [header, originHeaders.get(header)!])
)
return new Response(
result.get(target.href) || "",
{
status: 200,
headers: {
"content-type": "application/javascript",
"access-control-allow-origin": "*",
"x-typescript-types": target.href,
...cacheHeaders,
},
}
)
} catch(e) {
return new Response(
url.pathname.substring(1)
+ "\n"
+ e,
{
status: 404,
}
)
}
}
console.log(`http://localhost:8081`)
Deno.serve(
{
port: 8081,
},
handler,
)