-
Notifications
You must be signed in to change notification settings - Fork 743
Expand file tree
/
Copy pathmod.ts
More file actions
488 lines (430 loc) · 14.2 KB
/
mod.ts
File metadata and controls
488 lines (430 loc) · 14.2 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import type { AnyComponent } from "preact";
import type { App } from "../../app.ts";
import type { WalkEntry } from "@std/fs/walk";
import * as path from "@std/path";
import type { RouteConfig } from "../../types.ts";
import type { RouteHandler } from "../../handlers.ts";
import type { MiddlewareFn } from "../../middlewares/mod.ts";
import {
type AsyncAnyComponent,
renderMiddleware,
} from "./render_middleware.ts";
import { type Method, pathToPattern } from "../../router.ts";
import { type HandlerFn, isHandlerByMethod } from "../../handlers.ts";
import { type FsAdapter, fsAdapter } from "../../fs.ts";
import { HttpError } from "../../error.ts";
import { parseRootPath } from "../../config.ts";
import type { FreshReqContext, PageProps } from "../../context.ts";
const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/;
const GROUP_REG = /(^|[/\\\\])\((_[^/\\\\]+)\)[/\\\\]/;
interface InternalRoute<State> {
path: string;
base: string;
filePath: string;
config: RouteConfig | null;
handlers: RouteHandler<unknown, State> | null;
component: AnyComponent<PageProps<unknown, State>> | null;
}
export interface FreshFsItem<State> {
config?: RouteConfig;
handler?: RouteHandler<unknown, State> | HandlerFn<unknown, State>[];
handlers?: RouteHandler<unknown, State>;
default?:
| AnyComponent<PageProps<unknown, State>>
| AsyncAnyComponent<PageProps<unknown, State>>;
}
// deno-lint-ignore no-explicit-any
function isFreshFile<State>(mod: any): mod is FreshFsItem<State> {
return mod !== null && typeof mod === "object" &&
typeof mod.default === "function" ||
typeof mod.config === "object" || typeof mod.handlers === "object" ||
typeof mod.handlers === "function" || typeof mod.handler === "object" ||
typeof mod.handler === "function";
}
export interface FsRoutesOptions {
dir?: string;
ignoreFilePattern?: RegExp[];
loadRoute: (path: string) => Promise<unknown>;
loadIsland: (path: string) => Promise<unknown>;
}
export interface TESTING_ONLY__FsRoutesOptions {
_fs?: FsAdapter;
}
export async function fsRoutes<State>(
app: App<State>,
options_: FsRoutesOptions,
) {
const options = options_ as FsRoutesOptions & TESTING_ONLY__FsRoutesOptions;
const ignore = options.ignoreFilePattern ?? [TEST_FILE_PATTERN];
const fs = options._fs ?? fsAdapter;
const dir = options.dir
? parseRootPath(options.dir, fs.cwd())
: app.config.root;
const islandDir = path.join(dir, "islands");
const routesDir = path.join(dir, "routes");
const islandPaths: string[] = [];
const relRoutePaths: string[] = [];
// Walk routes folder
await Promise.all([
walkDir(
islandDir,
(entry) => {
islandPaths.push(entry.path);
},
ignore,
fs,
),
walkDir(
routesDir,
(entry) => {
const relative = path.relative(routesDir, entry.path);
// A `(_islands)` path segment is a local island folder.
// Any route path segment wrapped in `(_...)` is ignored
// during route collection.
const match = relative.match(GROUP_REG);
if (match && match[2][0] === "_") {
if (match[2] === "_islands") {
islandPaths.push(entry.path);
}
return;
}
const url = new URL(relative, "http://localhost/");
relRoutePaths.push(url.pathname.slice(1));
},
ignore,
fs,
),
]);
await Promise.all(islandPaths.map(async (islandPath) => {
const relative = path.relative(islandDir, islandPath);
// deno-lint-ignore no-explicit-any
const mod = await options.loadIsland(relative) as any;
for (const key of Object.keys(mod)) {
const maybeFn = mod[key];
if (typeof maybeFn === "function") {
app.island(islandPath, key, maybeFn);
}
}
}));
const routeModules: InternalRoute<State>[] = await Promise.all(
relRoutePaths.map(async (routePath) => {
const mod = await options.loadRoute(routePath);
if (!isFreshFile(mod)) {
throw new Error(
`Expected a route, middleware, layout or error template, but couldn't find relevant exports in: ${routePath}`,
);
}
const handlers = mod.handlers ?? mod.handler ?? null;
if (typeof handlers === "function" && handlers.length > 1) {
throw new Error(
`Handlers must only have one argument but found more than one. Check the function signature in: ${
path.join(routesDir, routePath)
}`,
);
}
const normalizedPath = `/${
routePath.slice(0, routePath.lastIndexOf("."))
}`;
const base = normalizedPath.slice(0, normalizedPath.lastIndexOf("/"));
const isMiddleware = normalizedPath.endsWith("/_middleware");
return {
path: normalizedPath,
filePath: routePath,
base,
handlers: mod.handlers ?? mod.handler ??
(isMiddleware ? mod.default ?? null : null),
config: mod.config ?? null,
component: isMiddleware ? null : mod.default ?? null,
} as InternalRoute<State>;
}),
);
routeModules.sort((a, b) => sortRoutePaths(a.path, b.path));
const stack: InternalRoute<State>[] = [];
let hasApp = false;
const specialPaths = new Set<string>();
for (let i = 0; i < routeModules.length; i++) {
const routeMod = routeModules[i];
const normalized = routeMod.path;
let j = stack.length - 1;
while (
j >= 0 && stack[j].base !== "" &&
!routeMod.path.startsWith(stack[j].base + "/")
) {
j--;
stack.pop();
}
if (normalized.endsWith("/_app")) {
hasApp = true;
stack.push(routeMod);
continue;
} else if (normalized.endsWith("/_middleware")) {
stack.push(routeMod);
continue;
} else if (normalized.endsWith("/_layout")) {
stack.push(routeMod);
continue;
} else if (normalized.endsWith("/_error")) {
stack.push(routeMod);
continue;
} else if (normalized.endsWith("/_404")) {
stack.push(routeMod);
continue;
} else if (normalized.endsWith("/_500")) {
stack.push(routeMod);
continue;
}
// Remove any elements not matching our parent path anymore
const middlewares: MiddlewareFn<State>[] = [];
let components: AnyComponent<PageProps<unknown, State>>[] = [];
let skipApp = !!routeMod.config?.skipAppWrapper;
const skipLayouts = !!routeMod.config?.skipInheritedLayouts;
for (let k = 0; k < stack.length; k++) {
const mod = stack[k];
if (mod.path.endsWith("/_middleware")) {
if (mod.handlers !== null && !isHandlerByMethod(mod.handlers)) {
middlewares.push(mod.handlers as MiddlewareFn<State>);
} else if (Array.isArray(mod.handlers)) {
middlewares.push(...mod.handlers);
}
}
// _app template
if (skipApp && mod.path === "/_app") {
hasApp = false;
continue;
} else if (!skipApp && mod.config?.skipAppWrapper) {
skipApp = true;
if (hasApp) {
hasApp = false;
// _app component is always first
components.shift();
}
}
// _layouts
if (skipLayouts && mod.path.endsWith("/_layout")) {
continue;
} else if (!skipLayouts && mod.config?.skipInheritedLayouts) {
const first = components.length > 0 ? components[0] : null;
components = [];
if (!skipApp && hasApp && first !== null) {
components.push(first);
}
}
if (mod.path.endsWith("/_error") || mod.path.endsWith("/_500")) {
const handlers = mod.handlers;
const handler = handlers === null ||
(isHandlerByMethod(handlers) && Object.keys(handlers).length === 0)
? undefined
: typeof handlers === "function"
? handlers
: undefined; // FIXME: Method handler
const errorComponents = components.slice();
if (mod.component !== null) {
errorComponents.push(mod.component);
}
let parent = mod.path.slice(0, -"_error".length);
parent = parent + "*";
// Add error route as its own route
if (!specialPaths.has(mod.path)) {
specialPaths.add(mod.path);
app.all(
parent,
errorMiddleware(errorComponents, handler),
);
}
middlewares.push(errorMiddleware(errorComponents, handler));
continue;
}
if (mod.path.endsWith("/_404")) {
const handlers = mod.handlers;
const handler = handlers === null ||
(isHandlerByMethod(handlers) && Object.keys(handlers).length === 0)
? undefined
: typeof handlers === "function"
? handlers
: undefined; // FIXME: Method handler
const notFoundComponents = components.slice();
if (mod.component !== null) {
notFoundComponents.push(mod.component);
}
app.use(notFoundMiddleware(notFoundComponents, handler));
continue;
}
if (mod.component !== null) {
components.push(mod.component);
}
}
const handlers = routeMod.handlers;
const routePath = routeMod.config?.routeOverride ??
pathToPattern(normalized.slice(1));
if (routeMod.component !== null) {
components.push(routeMod.component);
const missingGetHandler = handlers !== null &&
isHandlerByMethod(handlers) &&
!Object.keys(handlers).includes("GET");
if (missingGetHandler) {
const combined = middlewares.concat(
renderMiddleware(components, undefined),
);
app.get(routePath, ...combined);
}
}
if (
handlers === null ||
(isHandlerByMethod(handlers) && Object.keys(handlers).length === 0)
) {
const combined = middlewares.concat(
renderMiddleware(components, undefined),
);
app.get(routePath, ...combined);
} else if (isHandlerByMethod(handlers)) {
for (const method of Object.keys(handlers) as Method[]) {
const fn = handlers[method];
if (fn !== undefined) {
const combined = middlewares.concat(renderMiddleware(components, fn));
const lower = method.toLowerCase() as Lowercase<Method>;
app[lower](routePath, ...combined);
}
}
} else if (typeof handlers === "function") {
const combined = middlewares.concat(
renderMiddleware(components, handlers),
);
app.all(routePath, ...combined);
}
}
}
function errorMiddleware<State>(
components: AnyComponent<PageProps<unknown, State>>[],
handler: HandlerFn<unknown, State> | undefined,
): MiddlewareFn<State> {
const mid = renderMiddleware<State>(components, handler);
return async function errorMiddleware(ctx) {
try {
return await ctx.next();
} catch (err) {
(ctx as FreshReqContext<State>).error = err;
return mid(ctx);
}
};
}
function notFoundMiddleware<State>(
components: AnyComponent<PageProps<unknown, State>>[],
handler: HandlerFn<unknown, State> | undefined,
): MiddlewareFn<State> {
const mid = renderMiddleware<State>(components, handler);
return async function notFoundMiddleware(ctx) {
try {
return await ctx.next();
} catch (err) {
if (err instanceof HttpError && err.status === 404) {
return mid(ctx);
}
throw err;
}
};
}
async function walkDir(
dir: string,
callback: (entry: WalkEntry) => void,
ignore: RegExp[],
fs: FsAdapter,
) {
if (!await fs.isDirectory(dir)) return;
const entries = fs.walk(dir, {
includeDirs: false,
includeFiles: true,
exts: ["tsx", "jsx", "ts", "js"],
skip: ignore,
});
for await (const entry of entries) {
callback(entry);
}
}
const APP_REG = /_app(?!\.[tj]sx?)?$/;
/**
* Sort route paths where special Fresh files like `_app`,
* `_layout` and `_middleware` are sorted in front.
*/
export function sortRoutePaths(a: string, b: string) {
// The `_app` route should always be the first
if (APP_REG.test(a)) return -1;
else if (APP_REG.test(b)) return 1;
const aLen = a.length;
const bLen = b.length;
let segment = false;
let aIdx = 0;
let bIdx = 0;
for (; aIdx < aLen && bIdx < bLen; aIdx++, bIdx++) {
const charA = a.charAt(aIdx);
const charB = b.charAt(bIdx);
// When comparing a grouped route with a non-grouped one, we
// need to skip over the group name to effectively compare the
// actual route.
if (charA === "(" && charB !== "(") {
if (charB == "[") return -1;
return 1;
} else if (charB === "(" && charA !== "(") {
if (charA == "[") return 1;
return -1;
}
if (charA === "/" || charB === "/") {
segment = true;
// If the other path doesn't close the segment
// then we don't need to continue
if (charA !== "/") return 1;
if (charB !== "/") return -1;
continue;
}
if (segment) {
segment = false;
const scoreA = getRoutePathScore(charA, a, aIdx);
const scoreB = getRoutePathScore(charB, b, bIdx);
if (scoreA === scoreB) {
if (charA !== charB) {
// TODO: Do we need localeSort here or is this good enough?
return charA < charB ? 0 : 1;
}
continue;
}
return scoreA > scoreB ? -1 : 1;
}
if (charA !== charB) {
// TODO: Do we need localeSort here or is this good enough?
return charA < charB ? 0 : 1;
}
// If we're at the end of A or B, then we assume that the longer
// path is more specific
if (aIdx === aLen - 1 && bIdx < bLen - 1) {
return 1;
} else if (bIdx === bLen - 1 && aIdx < aLen - 1) {
return -1;
}
}
return 0;
}
/**
* Assign a score based on the first two characters of a path segment.
* The goal is to sort `_middleware` and `_layout` in front of everything
* and `[` or `[...` last respectively.
*/
function getRoutePathScore(char: string, s: string, i: number): number {
if (char === "_") {
if (i + 1 < s.length) {
if (s[i + 1] === "e") return 4;
if (s[i + 1] === "m") return 6;
}
return 5;
} else if (char === "[") {
if (i + 1 < s.length && s[i + 1] === ".") {
return 0;
}
return 1;
}
if (
i + 4 === s.length - 1 && char === "i" && s[i + 1] === "n" &&
s[i + 2] === "d" && s[i + 3] === "e" && s[i + 4] === "x"
) {
return 3;
}
return 2;
}