/
mod.ts
411 lines (391 loc) · 15.1 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
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
401
402
403
404
405
406
407
408
409
410
411
import type { ConnInfo, ServeInit } from "https://deno.land/std@0.136.0/http/server.ts";
import { serve as stdServe, serveTls } from "https://deno.land/std@0.136.0/http/server.ts";
import { readableStreamFromReader } from "https://deno.land/std@0.136.0/streams/conversion.ts";
import FetchError from "../framework/core/fetch_error.ts";
import type { RouteRecord } from "../framework/core/route.ts";
import log, { LevelName } from "../lib/log.ts";
import { getContentType } from "../lib/mime.ts";
import util from "../lib/util.ts";
import { type ErrorCallback, generateErrorHtml } from "./error.ts";
import { DependencyGraph } from "./graph.ts";
import { getDeploymentId, initModuleLoaders, loadImportMap, loadJSXConfig, regFullVersion } from "./helpers.ts";
import { type HTMLRewriterHandlers, loadAndFixIndexHtml } from "./html.ts";
import renderer, { type SSR } from "./renderer.ts";
import { content, type CookieOptions, json, setCookieHeader } from "./response.ts";
import { importRouteModule, initRoutes, revive } from "./routing.ts";
import clientModuleTransformer from "./transformer.ts";
import type { AlephConfig, FetchHandler, Middleware } from "./types.ts";
export type ServerOptions = Omit<ServeInit, "onError"> & {
certFile?: string;
keyFile?: string;
logLevel?: LevelName;
middlewares?: Middleware[];
fetch?: FetchHandler;
ssr?: SSR;
onError?: ErrorCallback;
} & AlephConfig;
export const serve = (options: ServerOptions = {}) => {
const { routes, build, devServer, middlewares, fetch, ssr, logLevel, onError } = options;
const isDev = Deno.env.get("ALEPH_ENV") === "development";
const importMapPromise = loadImportMap();
const jsxConfigPromise = importMapPromise.then(loadJSXConfig);
const moduleLoadersPromise = importMapPromise.then(initModuleLoaders);
const routesPromise = routes ? initRoutes(routes) : Promise.resolve({ routes: [] });
const handler = async (req: Request, connInfo: ConnInfo): Promise<Response> => {
const url = new URL(req.url);
const { host, pathname, searchParams } = url;
// close the hot-reloading websocket connection and tell the client to reload
// this request occurs when the client try to connect to the hot-reloading websocket in production mode
if (pathname === "/-/hmr") {
const { socket, response } = Deno.upgradeWebSocket(req, {});
socket.addEventListener("open", () => {
socket.send(JSON.stringify({ type: "reload" }));
setTimeout(() => {
socket.close();
}, 50);
});
return response;
}
const postMiddlewares: Middleware[] = [];
const customHTMLRewriter: [string, HTMLRewriterHandlers][] = [];
// create the context object
const ctx = {
connInfo,
params: {},
headers: new Headers(),
cookies: {
_cookies: null as Map<string, string> | null,
get(name: string) {
if (this._cookies === null) {
this._cookies = new Map<string, string>();
const cookieHeader = req.headers.get("Cookie");
if (cookieHeader) {
for (const cookie of cookieHeader.split(";")) {
const [key, value] = util.splitBy(cookie, "=");
this._cookies.set(key.trim(), value);
}
}
}
return this._cookies.get(name);
},
set(name: string, value: string, options?: CookieOptions) {
this._cookies?.set(name, value);
ctx.headers.set("Set-Cookie", setCookieHeader(name, value, options));
},
delete(name: string, options?: CookieOptions) {
this._cookies?.delete(name);
ctx.headers.set("Set-Cookie", setCookieHeader(name, "", { ...options, expires: new Date(0) }));
},
},
htmlRewriter: {
on: (selector: string, handlers: HTMLRewriterHandlers) => {
customHTMLRewriter.push([selector, handlers]);
},
},
};
// use eager middlewares
if (Array.isArray(middlewares)) {
const len = middlewares.length;
for (let i = 0; i < len; i++) {
const mw = middlewares[i];
const handler = mw.fetch;
if (typeof handler === "function") {
if (mw.eager) {
try {
let res = handler(req, ctx);
if (res instanceof Promise) {
res = await res;
}
if (res instanceof Response) {
return res;
}
if (typeof res === "function") {
setTimeout(res, 0);
}
} catch (err) {
log.error(`Middleare${mw.name ? `(${mw.name})` : ""}:`, err);
return onError?.(err, { by: "middleware", url: req.url, context: ctx }) ??
new Response(generateErrorHtml(err.stack ?? err.message), {
status: 500,
headers: [["Content-Type", "text/html"]],
});
}
} else {
postMiddlewares.push(mw);
}
}
}
}
// transform client modules
if (clientModuleTransformer.test(pathname)) {
try {
const [jsxConfig, importMap] = await Promise.all([
jsxConfigPromise,
importMapPromise,
]);
return await clientModuleTransformer.fetch(req, {
importMap,
jsxConfig,
buildTarget: build?.target,
isDev,
});
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
log.error(err);
return onError?.(err, { by: "transplie", url: req.url }) ??
new Response(generateErrorHtml(err.stack ?? err.message), {
status: 500,
headers: [["Content-Type", "text/html"]],
});
}
}
}
// use loader to load modules
const moduleLoaders = await moduleLoadersPromise;
const loader = moduleLoaders.find((loader) => loader.test(pathname));
if (loader) {
try {
const [jsxConfig, importMap] = await Promise.all([
jsxConfigPromise,
importMapPromise,
]);
const loaded = await loader.load(pathname, { isDev, importMap });
return await clientModuleTransformer.fetch(req, {
loaded,
importMap,
jsxConfig,
buildTarget: build?.target,
isDev,
});
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
log.error(err);
return onError?.(err, { by: "transplie", url: req.url }) ??
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 = `.${pathname}`;
let stat = await Deno.lstat(filePath);
if (stat.isDirectory && pathname !== "/") {
filePath = `${util.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(readableStreamFromReader(file), { headers });
}
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
log.error(err);
return onError?.(err, { by: "fs", url: req.url }) ??
new Response(generateErrorHtml(err.stack ?? err.message), {
status: 500,
headers: [["Content-Type", "text/html"]],
});
}
}
}
// use post middlewares
for (const mw of postMiddlewares) {
try {
let res = mw.fetch(req, ctx);
if (res instanceof Promise) {
res = await res;
}
if (res instanceof Response) {
return res;
}
if (typeof res === "function") {
setTimeout(res, 0);
}
} catch (err) {
log.error(`Middleare${mw.name ? `(${mw.name})` : ""}:`, err);
return onError?.(err, { by: "middleware", url: req.url, context: ctx }) ??
new Response(generateErrorHtml(err.stack ?? err.message), {
status: 500,
headers: [["Content-Type", "text/html"]],
});
}
}
// use the `fetch` handler if available
if (typeof fetch === "function") {
return fetch(req, ctx);
}
// request route api
const routes: RouteRecord = Reflect.get(globalThis, "__ALEPH_ROUTES") || await routesPromise;
if (routes.routes.length > 0) {
for (const [pattern, { filename }] of routes.routes) {
const ret = pattern.exec({ host, pathname });
const accept = req.headers.get("Accept");
const fromFetchApi = accept === "application/json" || !accept?.includes("html");
if (ret) {
try {
const { method } = req;
const mod = await importRouteModule(filename);
const dataConfig = util.isPlainObject(mod.data) ? mod.data : mod;
if (method !== "GET" || mod.default === undefined || fromFetchApi) {
Object.assign(ctx.params, ret.pathname.groups);
const anyFetcher = dataConfig.any ?? dataConfig.ANY;
if (typeof anyFetcher === "function") {
const res = await anyFetcher(req, ctx);
if (res instanceof Response) {
return res;
}
}
const fetcher = dataConfig[method.toLowerCase()] ?? dataConfig[method];
if (typeof fetcher === "function") {
const res = await fetcher(req, ctx);
if (res instanceof Response) {
if (res.status >= 300 && fromFetchApi) {
const err = await FetchError.fromResponse(res);
return json({ ...err }, { status: err.status >= 400 ? err.status : 501, headers: ctx.headers });
}
let headers: Headers | null = null;
ctx.headers.forEach((value, name) => {
if (!headers) {
headers = new Headers(res.headers);
}
headers.set(name, value);
});
if (headers) {
return new Response(res.body, { status: res.status, statusText: res.statusText, headers });
}
return res;
}
if (
typeof res === "string" ||
res instanceof ArrayBuffer ||
res instanceof Uint8Array ||
res instanceof ReadableStream
) {
return new Response(res, { headers: ctx.headers });
}
if (res instanceof Blob || res instanceof File) {
ctx.headers.set("Content-Type", res.type);
ctx.headers.set("Content-Length", res.size.toString());
return new Response(res, { headers: ctx.headers });
}
if (util.isPlainObject(res) || Array.isArray(res)) {
return json(res, { headers: ctx.headers });
}
if (res === null) {
return new Response(null, { headers: ctx.headers });
}
return new Response("Invalid Reponse Type", { status: 500 });
}
return new Response("Method Not Allowed", { status: 405 });
}
} catch (err) {
const res = onError?.(err, { by: "route-api", url: req.url, context: ctx });
if (res instanceof Response) {
return res;
}
if (err instanceof Response) {
return err;
}
if (err instanceof Error || typeof err === "string") {
log.error(err);
}
const status: number = util.isUint(err.status || err.code) ? err.status || err.code : 500;
return json({ ...err, message: err.message || String(err), status }, {
status: status >= 400 ? status : 501,
headers: ctx.headers,
});
}
}
}
}
// don't render those special asset files
switch (pathname) {
case "/favicon.ico":
case "/robots.txt":
return new Response("Not found", { status: 404 });
}
// load the `index.html`
let indexHtml: Uint8Array | null | undefined = Reflect.get(globalThis, "__ALEPH_INDEX_HTML");
if (indexHtml === undefined) {
try {
indexHtml = await loadAndFixIndexHtml({
isDev,
importMap: await importMapPromise,
ssr: typeof ssr === "function" ? {} : ssr,
hmrWebSocketUrl: options.devServer?.hmrWebSocketUrl,
});
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
indexHtml = null;
} else {
log.error("read index.html:", err);
return onError?.(err, { by: "fs", url: req.url }) ??
new Response(generateErrorHtml(err.stack ?? err.message), {
status: 500,
headers: [["Content-Type", "text/html"]],
});
}
}
}
// cache `index.html` to memory
Reflect.set(globalThis, "__ALEPH_INDEX_HTML", indexHtml);
// no root `index.html` found
if (indexHtml === null) {
return new Response("Not Found", { status: 404 });
}
// render html
return renderer.fetch(req, ctx, {
indexHtml,
routes,
customHTMLRewriter,
isDev,
ssr,
onError,
});
};
// set log level if specified
if (logLevel) {
log.setLevel(logLevel);
}
// inject global objects
Reflect.set(globalThis, "__ALEPH_CONFIG", { build, routes, devServer });
Reflect.set(globalThis, "clientDependencyGraph", new DependencyGraph());
const { hostname, port = 8080, certFile, keyFile, signal } = options;
if (Deno.env.get("ALEPH_CLI")) {
Reflect.set(globalThis, "__ALEPH_SERVER", { hostname, port, certFile, keyFile, handler, signal });
} else {
if (certFile && keyFile) {
serveTls(handler, { hostname, port, certFile, keyFile, signal });
} else {
stdServe(handler, { hostname, port, signal });
}
log.info(`Server ready on http://localhost:${port}`);
}
};
export { content, json, revive, setCookieHeader };