/
handler.ts
400 lines (377 loc) · 13.2 KB
/
handler.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import { generateErrorHtml, TransformError } from "../framework/core/error.ts";
import type { Router } from "../framework/core/router.ts";
import { isPlainObject, trimSuffix } from "../shared/util.ts";
import { createContext, NEXT } from "./context.ts";
import { handleHMR } from "./dev.ts";
import { HTMLRewriter, path } from "./deps.ts";
import {
existsDir,
existsFile,
fetchCode,
getAlephPkgUri,
getAppDir,
getDeploymentId,
getImportMap,
getJSXConfig,
globalIt,
isNpmPkg,
regFullVersion,
restoreUrl,
toLocalPath,
} from "./helpers.ts";
import { createHtmlResponse, loadIndexHtml } from "./html.ts";
import log from "./log.ts";
import { getContentType } from "./media_type.ts";
import renderer from "./renderer.ts";
import { fetchRoute, importRouteModule, initRouter } from "./router.ts";
import transformer from "./transformer.ts";
import type { AlephConfig, ConnInfo, Context, ModuleLoader } from "./types.ts";
export function createHandler(config: AlephConfig) {
const { loaders, middlewares, onError, build, router: routerConfig, ssr } = config;
const buildMode = Deno.args.includes("--build");
const isDev = Deno.args.includes("--dev");
const appDir = getAppDir();
const staticHandler = async (req: Request): Promise<Response | void> => {
const { pathname, searchParams } = new URL(req.url);
// handle HMR socket
if (pathname === "/-/hmr") {
if (isDev) {
return handleHMR(req);
}
const { socket, response } = Deno.upgradeWebSocket(req);
socket.addEventListener("open", () => {
// tell the client to reload the page if not _dev_ mode
socket.send(JSON.stringify({ type: "reload" }));
});
return response;
}
// getStaticPaths PRC for SSR
if (pathname === "/-/getStaticPaths") {
const router: Router = await globalIt("__ALEPH_ROUTER", () => initRouter(appDir, routerConfig));
const pattern = searchParams.get("pattern");
const route = router.routes.find(([_, r]) => r.pattern.pathname === pattern);
if (route) {
const mod = await importRouteModule(route[1]);
if (typeof mod.getStaticPaths === "function") {
let ret = mod.getStaticPaths();
if (ret instanceof Promise) {
ret = await ret;
}
if (Array.isArray(ret)) {
return Response.json(ret);
}
}
}
return Response.json([]);
}
// check if the `out` directory exists
const outDir = await globalIt("__ALEPH_OUT_DIR", async () => {
if (!isDev && !buildMode) {
const outDir = path.join(appDir, build?.outputDir ?? "output");
if (await existsDir(outDir)) {
return outDir;
}
}
return null;
});
// transform modules
let loader: ModuleLoader | undefined;
if (
!searchParams.has("raw") && (
// 1. check loader first
(loader = loaders?.find((l) => l.test(pathname))) ||
// 2. check built-in modules
transformer.test(pathname)
)
) {
if (pathname.endsWith(".map") && pathname.startsWith("/-/")) {
const [content, contentType] = await fetchCode(restoreUrl(pathname));
return new Response(content, {
headers: [["Content-Type", contentType]],
});
}
// check and use the build output
if (!isDev && !buildMode && outDir && !searchParams.has("ssr")) {
let outFile = path.join(outDir, pathname);
if (pathname.startsWith("/-/") && isNpmPkg(restoreUrl(pathname))) {
outFile += ".js";
}
if (await existsFile(outFile)) {
const file = await Deno.open(outFile, { read: true });
const headers = new Headers();
if (outFile.endsWith(".css")) {
headers.set("Content-Type", "text/css; charset=utf-8");
} else {
headers.set("Content-Type", "application/javascript; charset=utf-8");
}
if (searchParams.get("v") || (pathname.startsWith("/-/") && regFullVersion.test(pathname))) {
headers.append("Cache-Control", "public, max-age=31536000, immutable");
}
return new Response(file.readable, { headers });
}
}
try {
const [importMap, jsxConfig] = await Promise.all([
getImportMap(appDir),
getJSXConfig(appDir),
]);
return await transformer.fetch(req, {
importMap,
jsxConfig,
loader,
isDev,
});
} catch (err) {
if (err instanceof TransformError) {
// todo: pretty error message in terminal
log.error(err.message);
const alephPkgUri = toLocalPath(getAlephPkgUri());
return new Response(
`import { showTransformError } from "${alephPkgUri}/framework/core/error.ts";showTransformError(${
JSON.stringify(err)
});export default null;`,
{
headers: [
["Content-Type", "application/javascript; charset=utf-8"],
["X-Transform-Error", "true"],
],
},
);
} else if (!(err instanceof Deno.errors.NotFound)) {
log.error(err);
return onError?.(err, "transform", req) ??
new Response(generateErrorHtml(err.stack ?? err.message), {
status: 500,
headers: [["Content-Type", "text/html;"]],
});
}
}
}
// serve static files
const contentType = getContentType(pathname);
if (!pathname.startsWith("/.") && contentType !== "application/octet-stream") {
try {
let filePath = path.join(appDir, pathname);
let stat = await Deno.lstat(filePath);
if (stat.isDirectory && pathname !== "/") {
filePath = `${trimSuffix(filePath, "/")}/index.html`;
stat = await Deno.lstat(filePath);
}
if (stat.isFile) {
const headers = new Headers({ "Content-Type": contentType });
const deployId = getDeploymentId();
let etag: string | null = null;
if (deployId) {
etag = `W/${btoa(pathname).replace(/[^a-z0-9]/g, "")}-${deployId}`;
} else {
const { mtime, size } = stat;
if (mtime) {
etag = `W/${mtime.getTime().toString(16)}-${size.toString(16)}`;
headers.append("Last-Modified", new Date(mtime).toUTCString());
}
}
if (etag) {
if (req.headers.get("If-None-Match") === etag) {
return new Response(null, { status: 304 });
}
headers.append("ETag", etag);
}
if (searchParams.get("v") || regFullVersion.test(pathname)) {
headers.append("Cache-Control", "public, max-age=31536000, immutable");
}
const file = await Deno.open(filePath, { read: true });
return new Response(file.readable, { headers });
}
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
log.error(err);
return onError?.(err, "fs", req) ??
new Response(generateErrorHtml(err.stack ?? err.message), {
status: 500,
headers: [["Content-Type", "text/html;"]],
});
}
}
}
};
const routeHandler = async (req: Request, ctx: Context): Promise<Response> => {
const { pathname, searchParams } = new URL(req.url);
// don't render special asset files
switch (pathname) {
case "/favicon.ico":
case "/robots.txt":
return new Response("Not found", { status: 404 });
default:
if (pathname.startsWith("/-/") || pathname.startsWith("/.")) {
return new Response("Not found", { status: 404 });
}
}
// get the router
const router: Router = await globalIt("__ALEPH_ROUTER", () => initRouter(appDir, routerConfig));
// for SSR dynamic data
if (router.routes.length > 0) {
try {
const res = await fetchRoute(req, ctx, router);
if (res) return res;
} catch (err) {
const fetchData = req.method === "GET" && searchParams.has("_data_");
// user throws a response
if (err instanceof Response) {
return err;
}
// javascript syntax error
if (err instanceof TypeError && !fetchData) {
return new Response(generateErrorHtml(err.stack ?? err.message), {
status: 500,
headers: [["Content-Type", "text/html;"]],
});
}
// use the `onError` if available
const res = onError?.(err, fetchData ? "fetch-route-data" : "fetch-route", req, ctx);
if (res instanceof Response) {
return res;
}
// prints the error stack
if (err instanceof Error || typeof err === "string") {
log.error(err);
}
// return the error as a json
let status: number = err.status ?? err.code;
if (!Number.isInteger(status)) {
status = 500;
}
return Response.json({
...err,
status,
message: err.message ?? String(err),
stack: err.stack,
}, { status });
}
}
// load index.html
const indexHtml = await globalIt(
"__ALEPH_INDEX_HTML",
() =>
loadIndexHtml(path.join(appDir, "index.html"), {
hmr: isDev ? { wsUrl: Deno.env.get("HMR_WS_URL") } : undefined,
ssr: ssr ? { root: isPlainObject(ssr) ? ssr.root : undefined } : undefined,
}),
);
if (!indexHtml) {
return new Response("Not found", { status: 404 });
}
// non SSR
if (
!ssr || (isPlainObject(ssr) && (
(ssr.exclude instanceof RegExp && ssr.exclude.test(pathname)) ||
(Array.isArray(ssr.exclude) && ssr.exclude.some((p) => p.test(pathname))) ||
(ssr.include instanceof RegExp && !ssr.include.test(pathname)) ||
(Array.isArray(ssr.include) && !ssr.include.some((p) => p.test(pathname)))
))
) {
const stream = new ReadableStream({
start: (controller) => {
const rewriter = new HTMLRewriter("utf8", (chunk: Uint8Array) => {
controller.enqueue(chunk);
});
// inject the router manifest
rewriter.on("head", {
element(el) {
if (router.routes.length > 0) {
const json = JSON.stringify({
routes: router.routes.map(([_, meta]) => meta),
prefix: router.prefix,
});
el.append(`<script id="router-manifest" type="application/json">${json}</script>`, {
html: true,
});
}
},
});
try {
rewriter.write(indexHtml);
rewriter.end();
} finally {
rewriter.free();
}
controller.close();
},
});
return new Response(stream, {
headers: {
"Content-Type": "text/html; charset=utf-8",
"Cache-Control": "public, max-age=",
},
});
}
// check if the `out` directory exists
const outDir = await globalIt("__ALEPH_OUT_DIR", async () => {
if (!isDev && !buildMode) {
const outDir = path.join(appDir, build?.outputDir ?? "output");
if (await existsDir(outDir)) {
return outDir;
}
}
return null;
});
// use SSG output if exists
if (!isDev && !buildMode && outDir) {
const htmlFile = path.join(outDir, pathname === "/" ? "index.html" : pathname + ".html");
if (await existsFile(htmlFile)) {
return createHtmlResponse(req, htmlFile);
}
}
// SSR
try {
return await renderer.fetch(req, ctx, { indexHtml, router, ssr, isDev });
} catch (err) {
if (err instanceof Response) {
return err;
}
let message: string;
if (err instanceof Error) {
message = err.stack as string;
log.error("SSR", err);
} else {
message = err?.toString?.() || String(err);
}
return new Response(generateErrorHtml(message, "SSR"), {
headers: {
"Cache-Control": "public, max-age=0, must-revalidate",
"Content-Type": "text/html; charset=utf-8",
},
});
}
};
// the deno http server handler
return async (req: Request, connInfo: ConnInfo): Promise<Response> => {
const res = await staticHandler(req);
if (res) return res;
const ctx = createContext(() => Promise.resolve(new Response(null)), {
req,
connInfo,
sessionOptions: config.session,
});
const next = (i: number): Promise<Response> | Response => {
if (Array.isArray(middlewares) && i < middlewares.length) {
const mw = middlewares[i];
try {
Reflect.set(ctx, NEXT, next.bind(null, i + 1));
return mw.fetch(req, ctx);
} catch (err) {
const res = onError?.(err, "middleware", req, ctx);
if (res instanceof Response) {
return res;
}
log.error(`[middleare${mw.name ? `(${mw.name})` : ""}]`, err);
return new Response(generateErrorHtml(err.stack ?? err.message), {
status: 500,
headers: [["Content-Type", "text/html; charset=utf-8"]],
});
}
}
return routeHandler(req, ctx);
};
return next(0);
};
}