-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
213 lines (188 loc) · 5.57 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
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
import { Hono, Context } from "https://deno.land/x/[email protected]/mod.ts"
import { etag } from "https://deno.land/x/[email protected]/middleware.ts"
import { assert } from "https://deno.land/[email protected]/assert/mod.ts"
import {
ensureFile,
exists,
} from "https://deno.land/[email protected]/fs/mod.ts"
import * as api from "./api/mod.ts"
const app = new Hono()
import { transpile } from "https://deno.land/x/[email protected]/mod.ts"
import {
parseProject,
Project,
} from "./deps/enz.ts"
import "https://deno.land/[email protected]/dotenv/load.ts"
const scriptHandler = async (c: Context) => {
const url = new URL(c.req.url)
const target = new URL("." + url.pathname, import.meta.url)
console.log("Transpile", url.pathname)
let result
if (await exists("deps/local" + url.pathname)) {
const source = await Deno.stat("." + url.pathname)
const target = await Deno.stat("deps/local" + url.pathname)
if (
// Deno Deploy인 경우 mtime 없으므로 무조건 캐시 사용
// TODO: Env 읽는걸로 바꾸기
!source.mtime ||
!target.mtime ||
source.mtime < target.mtime
) {
console.log("Load from cache")
result = await Deno.readTextFile("deps/local" + url.pathname)
}
}
if (!result) {
console.time(url.pathname)
result = (await transpile(
target,
{
cacheRoot: Deno.cwd(),
load: async (specifier) => {
if (target.href == specifier) {
return {
kind: "module",
specifier,
content: await Deno.readTextFile("." + url.pathname),
}
} else {
return {
kind: "external",
specifier,
}
}
},
},
)).get(target.href)
console.timeEnd(url.pathname)
assert(result)
await ensureFile("deps/local" + url.pathname)
Deno.writeTextFile("deps/local" + url.pathname, result)
}
c.header("content-type", "application/javascript; charset=utf-8")
return c.body(
result
|| `throw new Error("Transpile failed")`
)
}
app.use("/src/*", etag({weak: true}))
app.get("/src/*", scriptHandler)
app.use("/deps/*", etag({weak: true}))
app.get("/deps/*", scriptHandler)
// /image/lib/entryjs/images/
// /image/lib/entry-js/images/
app.get("/image/lib/*/images/*", async c => {
const path = new URL(c.req.url).pathname
.replace(/^\/image\//, "https://playentry.org/")
return c.body(
await api.image(path),
{
headers: {
"cache-control": "max-age=31536000, public, immutable"
}
},
)
})
app.get("/image/:id", async c => {
const id = c.req.param("id")
const [a,b,d,e] = id
return c.body(
await api.image(
`https://playentry.org/uploads/${
a + b
}/${
d + e
}/image/${id}`
),
{
headers: {
"cache-control": "max-age=31536000, public, immutable"
}
},
)
})
app.get("/sound/lib/entry-js/images/*", async c => {
const path = new URL(c.req.url).pathname
.replace(/^\/sound\//, "https://playentry.org/")
return c.body(
await api.image(path),
{
headers: {
"cache-control": "max-age=31536000, public, immutable"
}
},
)
})
app.get("/sound/:id", async c => {
const id = c.req.param("id")
const [a,b,d,e] = id
return c.body(
await api.image(
`https://playentry.org/uploads/${
a + b
}/${
d + e
}/${id}`
),
{
headers: {
"cache-control": "max-age=31536000, public, immutable"
}
},
)
})
app.get("/api/project/:id", async c => c.json(
await api.project(c.req.param("id"))
))
app.get("/api/js/:id", async c => {
c.header("content-type", "application/javascript; charset=utf-8")
const project = await api.project(c.req.param("id"))
.then(JSON.stringify)
.then(parseProject) as Project & {
updated: string
}
c.header(
"last-modified",
new Date(project.updated).toUTCString(),
)
c.header(
"cache-control",
"no-cache",
)
const a = new Date(c.req.header("if-modified-since") || 0)
const b = new Date(new Date(project.updated).toUTCString())
if (a < b) {
const label = `Generate ${c.req.param("id")}.js`
console.time(label)
const result = c.body(api.js(project))
console.timeEnd(label)
return result
} else {
c.status(304)
return c.body(null)
}
})
app.get("/", async c => c.html(
await Deno.readTextFile("view/index.html")
))
app.get("/p/:id", async c => c.html(
(await Deno.readTextFile("view/p.html"))
.replace(
"<!-- VERSION_LABEL -->",
`@${
Deno.env.get("VERSION_LABEL")
|| "dev"
}`
)
.replace("<!-- INSERT SCRIPT HERE -->", `
<script type="module">
import { Entry } from "/api/js/${c.req.param("id")}"
await Entry.init(
document.querySelector("app")
)
Entry.start()
</script>
`)
))
Deno.serve(app.fetch)
export { app }