@markopack/core 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/codegen/index.d.ts.map +1 -1
- package/dist/codegen/index.js +429 -370
- package/dist/codegen/index.js.map +1 -1
- package/dist/constants.d.ts +5 -5
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +5 -4
- package/dist/constants.js.map +1 -1
- package/dist/routes/builder.d.ts.map +1 -1
- package/dist/routes/builder.js +38 -7
- package/dist/routes/builder.js.map +1 -1
- package/dist/routes/parse.d.ts.map +1 -1
- package/dist/routes/parse.js +0 -2
- package/dist/routes/parse.js.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/agent-fix-guide.d.ts +16 -0
- package/dist/utils/agent-fix-guide.d.ts.map +1 -0
- package/dist/utils/agent-fix-guide.js +74 -0
- package/dist/utils/agent-fix-guide.js.map +1 -0
- package/dist/utils/server.d.ts +2 -1
- package/dist/utils/server.d.ts.map +1 -1
- package/dist/utils/server.js +30 -3
- package/dist/utils/server.js.map +1 -1
- package/package.json +2 -2
package/dist/codegen/index.js
CHANGED
|
@@ -3,44 +3,20 @@ import { httpVerbs, markoRunFilePrefix, RoutableFileTypes, virtualFilePrefix, }
|
|
|
3
3
|
import { normalizePath } from "../utils/fs";
|
|
4
4
|
import { getRouteVirtualFileName, getVerbs, hasVerb } from "../utils/route";
|
|
5
5
|
import { createStringWriter } from "./writer";
|
|
6
|
-
function normalizedRelativePath(from, to) {
|
|
7
|
-
const relativePath = normalizePath(path.relative(from, to));
|
|
8
|
-
return relativePath.startsWith(".") ? relativePath : "./" + relativePath;
|
|
9
|
-
}
|
|
10
6
|
export function renderRouteTemplate(route, markoApi) {
|
|
11
7
|
if (!route.page) {
|
|
12
8
|
throw new Error(`Route ${route.key} has no page to render`);
|
|
13
9
|
}
|
|
14
|
-
if (!route.templateFilePath) {
|
|
15
|
-
throw new Error(`Route ${route.key} has no template file path`);
|
|
16
|
-
}
|
|
17
10
|
const writer = createStringWriter();
|
|
18
11
|
if (markoApi) {
|
|
19
12
|
writer.writeLines(`<!-- use ${markoApi} -->\n`);
|
|
20
13
|
}
|
|
14
|
+
// Opened here so the tag's imports land above the template.
|
|
21
15
|
writer.branch("imports");
|
|
22
16
|
writer.writeLines("");
|
|
23
17
|
writeEntryTemplateTag(writer, [...route.layouts, route.page].map((file) => normalizedRelativePath(path.dirname(route.templateFilePath), file.filePath)), route.key === RoutableFileTypes.Error ? ["error"] : []);
|
|
24
18
|
return writer.end();
|
|
25
19
|
}
|
|
26
|
-
function writeEntryTemplateTag(writer, [file, ...rest], pageInputs, index = 1) {
|
|
27
|
-
if (file) {
|
|
28
|
-
const isLast = !rest.length;
|
|
29
|
-
const tag = isLast ? "Page" : `Layout${index}`;
|
|
30
|
-
writer.branch("imports").writeLines(`import ${tag} from "${file}";`);
|
|
31
|
-
if (isLast) {
|
|
32
|
-
const attributes = pageInputs.length
|
|
33
|
-
? " " + pageInputs.map((name) => `${name}=input.${name}`).join(" ")
|
|
34
|
-
: "";
|
|
35
|
-
writer.writeLines(`<${tag}${attributes}/>`);
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
writer.writeBlockStart(`<${tag}>`);
|
|
39
|
-
writeEntryTemplateTag(writer, rest, pageInputs, index + 1);
|
|
40
|
-
writer.writeBlockEnd(`</>`);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
20
|
export function renderRouteEntry(route, rootDir) {
|
|
45
21
|
const { key, index, handler, page, middleware, meta } = route;
|
|
46
22
|
const verbs = getVerbs(route);
|
|
@@ -57,12 +33,16 @@ export function renderRouteEntry(route, rootDir) {
|
|
|
57
33
|
runtimeImports.push("normalizeMeta");
|
|
58
34
|
}
|
|
59
35
|
if (handler || middleware.length) {
|
|
60
|
-
runtimeImports.push("call");
|
|
36
|
+
runtimeImports.push("call", "normalizeOptions");
|
|
61
37
|
}
|
|
62
38
|
if (page) {
|
|
63
39
|
runtimeImports.push("render");
|
|
64
40
|
}
|
|
65
|
-
if (!page ||
|
|
41
|
+
if (!page ||
|
|
42
|
+
verbs.some((verb) => !(verb === "get" ||
|
|
43
|
+
verb === "head" ||
|
|
44
|
+
verb === "post" ||
|
|
45
|
+
verb === "query"))) {
|
|
66
46
|
runtimeImports.push("noContent");
|
|
67
47
|
}
|
|
68
48
|
if (verbs.includes("head")) {
|
|
@@ -81,12 +61,12 @@ export function renderRouteEntry(route, rootDir) {
|
|
|
81
61
|
for (const verb of handler.verbs) {
|
|
82
62
|
const importName = verb.toUpperCase();
|
|
83
63
|
names.push(importName);
|
|
84
|
-
writer.writeLines(`const ${verb}Handler = normalizeHandler(${importName});`);
|
|
64
|
+
writer.writeLines(`const ${verb}Handler = normalizeHandler(${importName}, '${importName}');`);
|
|
85
65
|
}
|
|
86
66
|
imports.writeLines(`import { ${names.join(", ")} } from "${normalizedRelativePath(rootDir, handler.filePath)}";`);
|
|
87
67
|
}
|
|
88
68
|
if (page) {
|
|
89
|
-
imports.writeLines(`import page from "${normalizedRelativePath(rootDir, route.templateFilePath
|
|
69
|
+
imports.writeLines(`import page from "${normalizedRelativePath(rootDir, route.templateFilePath)}?server-entry";`);
|
|
90
70
|
}
|
|
91
71
|
if (meta) {
|
|
92
72
|
const metaName = `meta${index}`;
|
|
@@ -102,11 +82,354 @@ export function renderRouteEntry(route, rootDir) {
|
|
|
102
82
|
imports.writeLines(`import ${metaName} from "${normalizedRelativePath(rootDir, meta.filePath)}";`);
|
|
103
83
|
writer.writeLines(`export const { ${metaVerbsExports} } = normalizeMeta(${metaName});`);
|
|
104
84
|
}
|
|
85
|
+
const optionsWriter = writer.branch("options").writeLines("");
|
|
105
86
|
for (const verb of verbs) {
|
|
87
|
+
writeRouteOptions(optionsWriter, route, verb);
|
|
106
88
|
writeRouteEntryHandler(writer, route, verb);
|
|
107
89
|
}
|
|
90
|
+
optionsWriter.join();
|
|
108
91
|
return writer.end();
|
|
109
92
|
}
|
|
93
|
+
export function renderRouter(routes, rootDir, runtimeInclude, options = {
|
|
94
|
+
trailingSlashes: "RedirectWithout",
|
|
95
|
+
}) {
|
|
96
|
+
const writer = createStringWriter();
|
|
97
|
+
const hasErrorPage = Boolean(routes.special[RoutableFileTypes.Error]);
|
|
98
|
+
const hasNotFoundPage = Boolean(routes.special[RoutableFileTypes.NotFound]);
|
|
99
|
+
const imports = writer.branch("imports");
|
|
100
|
+
if (runtimeInclude) {
|
|
101
|
+
imports.writeLines(`import "${normalizePath(runtimeInclude)}";`);
|
|
102
|
+
}
|
|
103
|
+
imports.writeLines(`import { NotHandled, NotMatched, createContext } from "${virtualFilePrefix}/runtime/internal";`);
|
|
104
|
+
for (const route of routes.list) {
|
|
105
|
+
const verbs = getVerbs(route);
|
|
106
|
+
const routeImports = [];
|
|
107
|
+
for (const verb of verbs) {
|
|
108
|
+
const verbName = `${verb}${route.index}`;
|
|
109
|
+
routeImports.push(verbName);
|
|
110
|
+
routeImports.push(`${verbName}_options`);
|
|
111
|
+
if (route.meta) {
|
|
112
|
+
routeImports.push(`${verbName}_meta`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
imports.writeLines(`import { ${routeImports.join(", ")} } from "${virtualFilePrefix}/${getRouteVirtualFileName(route)}";`);
|
|
116
|
+
}
|
|
117
|
+
for (const route of Object.values(routes.special)) {
|
|
118
|
+
imports.writeLines(`import page${route.key} from "${normalizedRelativePath(rootDir, route.templateFilePath)}?server-entry";`);
|
|
119
|
+
}
|
|
120
|
+
writer
|
|
121
|
+
.writeLines(`
|
|
122
|
+
globalThis.__marko_run__ = { match, fetch, invoke };
|
|
123
|
+
`)
|
|
124
|
+
.writeBlockStart(`export function match(method, pathname) {`)
|
|
125
|
+
.writeLines(`return match_internal(method, pathname.length > 1 && pathname.endsWith('/') ? pathname.slice(0, -1) : pathname)
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
function match_internal(method, pathname) {
|
|
129
|
+
const len = pathname.length;`)
|
|
130
|
+
.writeBlockStart(`try {`)
|
|
131
|
+
.writeBlockStart(`switch (method) {`);
|
|
132
|
+
for (const verb of httpVerbs) {
|
|
133
|
+
const filteredRoutes = routes.list.filter((route) => hasVerb(route, verb));
|
|
134
|
+
if (filteredRoutes.length) {
|
|
135
|
+
const trie = createRouteTrie(filteredRoutes);
|
|
136
|
+
writer.writeLines(`case '${verb.toUpperCase()}':`);
|
|
137
|
+
writer.writeBlockStart(`case '${verb.toLowerCase()}': {`);
|
|
138
|
+
writeRouterVerb(writer, trie, verb);
|
|
139
|
+
writer.writeBlockEnd("}");
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
writer.writeBlockEnd("}").writeBlockEnd("} catch (error) {").indent++;
|
|
143
|
+
writer
|
|
144
|
+
.writeLines("// A malformed percent-escape is an invalid URI: no route can match it.", "if (error instanceof URIError) return null;", "throw error;")
|
|
145
|
+
.writeBlockEnd("}")
|
|
146
|
+
.writeLines("return null;")
|
|
147
|
+
.writeBlockEnd("}");
|
|
148
|
+
writer
|
|
149
|
+
.writeLines("")
|
|
150
|
+
.writeBlockStart("export async function invoke(route, request, platform, url) {");
|
|
151
|
+
renderTrailingSlashPolicy(writer, options);
|
|
152
|
+
writer.writeLines("const context = createContext(route, request, platform, url);");
|
|
153
|
+
if (hasErrorPage) {
|
|
154
|
+
writer.writeBlockStart("try {");
|
|
155
|
+
}
|
|
156
|
+
writer
|
|
157
|
+
.writeBlockStart("if (route) {")
|
|
158
|
+
.writeBlockStart("try {")
|
|
159
|
+
.writeLines("const response = await route.handler(context);", "if (response) return response;").indent--;
|
|
160
|
+
writer
|
|
161
|
+
.writeBlockStart("} catch (error) {")
|
|
162
|
+
.writeLines("if (error === NotHandled) return;", "if (error !== NotMatched) throw error;")
|
|
163
|
+
.writeBlockEnd("}")
|
|
164
|
+
.writeBlockEnd("}");
|
|
165
|
+
if (hasNotFoundPage) {
|
|
166
|
+
writer.write(`
|
|
167
|
+
if (context.request.headers.get('Accept')?.includes('text/html')) {
|
|
168
|
+
return context.render(page404, {}, { status: 404 });
|
|
169
|
+
}`);
|
|
170
|
+
}
|
|
171
|
+
writer.indent--;
|
|
172
|
+
if (routes.list.length) {
|
|
173
|
+
writer.writeLines(`
|
|
174
|
+
return new Response(null, {
|
|
175
|
+
status: 404,
|
|
176
|
+
});`);
|
|
177
|
+
}
|
|
178
|
+
if (hasErrorPage) {
|
|
179
|
+
writer
|
|
180
|
+
.writeBlockStart(`} catch (error) {`)
|
|
181
|
+
.writeBlockStart(`if (context.request.headers.get('Accept')?.includes('text/html')) {`)
|
|
182
|
+
.writeLines(`return context.render(page500, { error }, { status: 500 });`)
|
|
183
|
+
.writeBlockEnd("}")
|
|
184
|
+
.writeLines("throw error;")
|
|
185
|
+
.writeBlockEnd("}");
|
|
186
|
+
}
|
|
187
|
+
writer.writeBlockEnd("}");
|
|
188
|
+
renderFetch(writer);
|
|
189
|
+
return writer.end();
|
|
190
|
+
}
|
|
191
|
+
export function renderMiddleware(middleware, rootDir) {
|
|
192
|
+
const writer = createStringWriter();
|
|
193
|
+
const imports = writer.branch("imports");
|
|
194
|
+
imports.writeLines(`import { normalizeHandler } from "${virtualFilePrefix}/runtime/internal";`);
|
|
195
|
+
writer.writeLines("");
|
|
196
|
+
for (const { id, filePath } of middleware) {
|
|
197
|
+
const importName = `middleware${id}`;
|
|
198
|
+
imports.writeLines(`import ${importName} from "${normalizedRelativePath(rootDir, filePath)}";`);
|
|
199
|
+
writer.writeLines(`export const mware${id} = normalizeHandler(${importName});`);
|
|
200
|
+
}
|
|
201
|
+
imports.join();
|
|
202
|
+
return writer.end();
|
|
203
|
+
}
|
|
204
|
+
export async function renderRouteTypeInfo(routes, outDir, adapter) {
|
|
205
|
+
const writer = createStringWriter();
|
|
206
|
+
writer.writeLines(`/*
|
|
207
|
+
WARNING: This file is automatically generated and any changes made to it will be overwritten without warning.
|
|
208
|
+
Do NOT manually edit this file or your changes will be lost.
|
|
209
|
+
|
|
210
|
+
AGENTS: before writing or changing route files, READ the cheat sheet shipped with @marko/run
|
|
211
|
+
— require.resolve("@marko/run/cheatsheet.md"), typically node_modules/@marko/run/cheatsheet.md — for the
|
|
212
|
+
routing, handler, middleware, and validation conventions this file's types describe.
|
|
213
|
+
*/
|
|
214
|
+
`, `import { NotHandled, NotMatched, GetPaths, PostPaths, GetablePath, GetableHref, PostablePath, PostableHref, Platform } from "@marko/run/namespace";`, `import type * as $ from "@marko/run";`, "");
|
|
215
|
+
const headWriter = writer.branch("head");
|
|
216
|
+
writer.writeLines("").writeBlockStart(`declare module "@marko/run" {`);
|
|
217
|
+
if (adapter && adapter.typeInfo) {
|
|
218
|
+
const platformType = await adapter.typeInfo((data) => headWriter.write(data));
|
|
219
|
+
if (platformType) {
|
|
220
|
+
writer.writeLines(`interface Platform extends ${platformType} {}\n`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
headWriter.join();
|
|
224
|
+
const fileInfoByType = new Map();
|
|
225
|
+
let fileIndex = 1;
|
|
226
|
+
function addFile(file) {
|
|
227
|
+
let group = fileInfoByType.get(file.type);
|
|
228
|
+
if (!group) {
|
|
229
|
+
fileInfoByType.set(file.type, (group = new Map()));
|
|
230
|
+
}
|
|
231
|
+
let info = group.get(file);
|
|
232
|
+
if (!info) {
|
|
233
|
+
info = {
|
|
234
|
+
id: "",
|
|
235
|
+
typeName: null,
|
|
236
|
+
modulePath: stripTsExtension(normalizedRelativePath(outDir, file.filePath)),
|
|
237
|
+
routes: new Set(),
|
|
238
|
+
};
|
|
239
|
+
switch (file.type) {
|
|
240
|
+
case RoutableFileTypes.Middleware:
|
|
241
|
+
info.id = `M${group.size + 1}`;
|
|
242
|
+
info.typeName = "Middleware";
|
|
243
|
+
break;
|
|
244
|
+
case RoutableFileTypes.Handler:
|
|
245
|
+
info.id = `H${group.size + 1}`;
|
|
246
|
+
info.typeName = "Handler";
|
|
247
|
+
break;
|
|
248
|
+
case RoutableFileTypes.Meta:
|
|
249
|
+
info.id = `D${group.size + 1}`;
|
|
250
|
+
info.typeName = "Meta";
|
|
251
|
+
break;
|
|
252
|
+
case RoutableFileTypes.Layout:
|
|
253
|
+
info.id = `L${group.size + 1}`;
|
|
254
|
+
info.typeName = "Template";
|
|
255
|
+
break;
|
|
256
|
+
case RoutableFileTypes.Page:
|
|
257
|
+
info.id = `P${group.size + 1}`;
|
|
258
|
+
info.typeName = "Template";
|
|
259
|
+
break;
|
|
260
|
+
case RoutableFileTypes.Error:
|
|
261
|
+
case RoutableFileTypes.NotFound:
|
|
262
|
+
info.id = "";
|
|
263
|
+
info.typeName = "Template";
|
|
264
|
+
break;
|
|
265
|
+
default:
|
|
266
|
+
info.id = `F${fileIndex++}`;
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
group.set(file, info);
|
|
270
|
+
}
|
|
271
|
+
return info;
|
|
272
|
+
}
|
|
273
|
+
writer.writeBlockStart(`interface App extends $.DefineRoutes<{`);
|
|
274
|
+
for (const route of routes.list) {
|
|
275
|
+
let routeDefFiles = "";
|
|
276
|
+
for (const file of routeFileIter(route)) {
|
|
277
|
+
const fileInfo = addFile(file);
|
|
278
|
+
fileInfo.routes.add(route);
|
|
279
|
+
if (routeDefFiles) {
|
|
280
|
+
routeDefFiles += ", ";
|
|
281
|
+
}
|
|
282
|
+
routeDefFiles += fileInfo.id;
|
|
283
|
+
}
|
|
284
|
+
writer.writeLines(`${JSON.stringify(route.path.path)}: [${routeDefFiles}];`);
|
|
285
|
+
}
|
|
286
|
+
for (const special of Object.values(routes.special)) {
|
|
287
|
+
addFile(special.page);
|
|
288
|
+
}
|
|
289
|
+
writer.writeBlockEnd(`}> {}`).writeBlockEnd(`}`);
|
|
290
|
+
for (const fileType of Object.values(RoutableFileTypes)) {
|
|
291
|
+
const fileGroup = fileInfoByType.get(fileType);
|
|
292
|
+
if (!fileGroup)
|
|
293
|
+
continue;
|
|
294
|
+
const hasModule = fileType !== RoutableFileTypes.Meta;
|
|
295
|
+
if (!hasModule) {
|
|
296
|
+
writer.writeLines("");
|
|
297
|
+
}
|
|
298
|
+
for (const info of fileGroup.values()) {
|
|
299
|
+
if (hasModule) {
|
|
300
|
+
writer.writeLines("");
|
|
301
|
+
}
|
|
302
|
+
if (info.typeName && info.id) {
|
|
303
|
+
writer.writeLines(`type ${info.id} = $.${info.typeName}<"${info.id}", typeof import("${info.modulePath}")>;`);
|
|
304
|
+
}
|
|
305
|
+
if (!hasModule)
|
|
306
|
+
continue;
|
|
307
|
+
writer.write(`declare module "${info.modulePath}" {`);
|
|
308
|
+
switch (fileType) {
|
|
309
|
+
case RoutableFileTypes.Layout:
|
|
310
|
+
writer.write(`
|
|
311
|
+
interface Input extends $.LayoutInput<${info.id}> {}`);
|
|
312
|
+
break;
|
|
313
|
+
case RoutableFileTypes.Error:
|
|
314
|
+
writer.write(`
|
|
315
|
+
export interface Input {
|
|
316
|
+
error: unknown;
|
|
317
|
+
}`);
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
if (info.typeName) {
|
|
321
|
+
const id = info.id || "any";
|
|
322
|
+
writer.write(`
|
|
323
|
+
const Run: $.Namespace<${id}>;
|
|
324
|
+
namespace Run {
|
|
325
|
+
type Context = $.ContextForFile<${id}>${info.typeName === "Template" ? " & Marko.Global" : ""};
|
|
326
|
+
}\n`);
|
|
327
|
+
}
|
|
328
|
+
writer.write(`
|
|
329
|
+
/** @deprecated use \`Run\` namespace instead */
|
|
330
|
+
namespace MarkoRun {
|
|
331
|
+
export { NotHandled, NotMatched, GetPaths, PostPaths, GetablePath, GetableHref, PostablePath, PostableHref, Platform };
|
|
332
|
+
/** @deprecated use the \`Run\` namespace instead */
|
|
333
|
+
export type Route = ${info.routes.size
|
|
334
|
+
? `$.Routes[${[...info.routes]
|
|
335
|
+
.map((route) => JSON.stringify(route.path.path))
|
|
336
|
+
.join(" | ")}]`
|
|
337
|
+
: "globalThis.MarkoRun.Route"};
|
|
338
|
+
/** @deprecated use \`Run.Context\` instead */
|
|
339
|
+
export type Context = ${info.modulePath.endsWith(".marko")
|
|
340
|
+
? "Run.Context"
|
|
341
|
+
: "$.MultiRouteContext<Route>"};
|
|
342
|
+
/** @deprecated define handlers with \`Run.GET(...)\`, \`Run.POST(...)\`, etc. instead */
|
|
343
|
+
export type Handler = $.HandlerLike<Route>;`);
|
|
344
|
+
for (const verb of httpVerbs) {
|
|
345
|
+
writer.write(`
|
|
346
|
+
/** @deprecated define handlers with \`Run.${verb.toUpperCase()}(...)\` instead */
|
|
347
|
+
export type ${verb.toUpperCase()} = $.HandlerLike<Route, "${verb.toUpperCase()}">;`);
|
|
348
|
+
}
|
|
349
|
+
writer.write(`
|
|
350
|
+
}`);
|
|
351
|
+
writer.writeLines(`
|
|
352
|
+
}`);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return writer.end();
|
|
356
|
+
}
|
|
357
|
+
function createRouteTrie(routes) {
|
|
358
|
+
const root = {
|
|
359
|
+
key: "",
|
|
360
|
+
};
|
|
361
|
+
function insert(path, route) {
|
|
362
|
+
let node = root;
|
|
363
|
+
for (const segment of path.segments) {
|
|
364
|
+
if (segment === "$$") {
|
|
365
|
+
node.catchAll ??= { route, path };
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
else if (segment === "$") {
|
|
369
|
+
node = node.dynamic ??= {
|
|
370
|
+
key: "",
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
node.static ??= new Map();
|
|
375
|
+
let next = node.static.get(segment);
|
|
376
|
+
if (!next) {
|
|
377
|
+
next = {
|
|
378
|
+
key: segment,
|
|
379
|
+
};
|
|
380
|
+
node.static.set(segment, next);
|
|
381
|
+
}
|
|
382
|
+
node = next;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
node.path ??= path;
|
|
386
|
+
node.route ??= route;
|
|
387
|
+
}
|
|
388
|
+
for (const route of routes) {
|
|
389
|
+
insert(route.path, route);
|
|
390
|
+
}
|
|
391
|
+
return root;
|
|
392
|
+
}
|
|
393
|
+
function normalizedRelativePath(from, to) {
|
|
394
|
+
const relativePath = normalizePath(path.relative(from, to));
|
|
395
|
+
return relativePath.startsWith(".") ? relativePath : "./" + relativePath;
|
|
396
|
+
}
|
|
397
|
+
function writeEntryTemplateTag(writer, [file, ...rest], pageInputs, index = 1) {
|
|
398
|
+
if (file) {
|
|
399
|
+
const isLast = !rest.length;
|
|
400
|
+
const tag = isLast ? "Page" : `Layout${index}`;
|
|
401
|
+
writer.branch("imports").writeLines(`import ${tag} from "${file}";`);
|
|
402
|
+
if (isLast) {
|
|
403
|
+
const attributes = pageInputs.length
|
|
404
|
+
? " " + pageInputs.map((name) => `${name}=input.${name}`).join(" ")
|
|
405
|
+
: "";
|
|
406
|
+
writer.writeLines(`<${tag}${attributes}/>`);
|
|
407
|
+
}
|
|
408
|
+
else {
|
|
409
|
+
writer.writeBlockStart(`<${tag}>`);
|
|
410
|
+
writeEntryTemplateTag(writer, rest, pageInputs, index + 1);
|
|
411
|
+
writer.writeBlockEnd(`</>`);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
function writeRouteOptions(writer, route, verb) {
|
|
416
|
+
const hasHandler = route.handler?.verbs?.includes(verb);
|
|
417
|
+
writer.write(`export const ${verb}${route.index}_options = `);
|
|
418
|
+
if (route.middleware.length || hasHandler) {
|
|
419
|
+
writer.write(`normalizeOptions('${verb.toUpperCase()}'`);
|
|
420
|
+
for (const { id } of route.middleware) {
|
|
421
|
+
writer.write(`, mware${id}`);
|
|
422
|
+
}
|
|
423
|
+
if (hasHandler) {
|
|
424
|
+
writer.write(`, ${verb}Handler`);
|
|
425
|
+
}
|
|
426
|
+
writer.write(");");
|
|
427
|
+
}
|
|
428
|
+
else {
|
|
429
|
+
writer.write("{};");
|
|
430
|
+
}
|
|
431
|
+
writer.write("\n");
|
|
432
|
+
}
|
|
110
433
|
function writeRouteEntryHandler(writer, route, verb) {
|
|
111
434
|
const { key, index, page, handler, middleware } = route;
|
|
112
435
|
const len = middleware.length;
|
|
@@ -114,14 +437,10 @@ function writeRouteEntryHandler(writer, route, verb) {
|
|
|
114
437
|
let currentName;
|
|
115
438
|
let hasBody = false;
|
|
116
439
|
writer.writeLines("");
|
|
117
|
-
|
|
118
|
-
writer.writeBlockStart(`export function ${verb}${index}(context) {`);
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
writer.writeBlockStart(`export function ${verb}${index}(context) {`);
|
|
122
|
-
}
|
|
440
|
+
writer.writeBlockStart(`export function ${verb}${index}(context) {`);
|
|
123
441
|
const continuations = writer.branch("cont");
|
|
124
|
-
if (page &&
|
|
442
|
+
if (page &&
|
|
443
|
+
(verb === "get" || verb === "head" || verb === "post" || verb === "query")) {
|
|
125
444
|
currentName = "__page";
|
|
126
445
|
if (handler?.verbs?.includes(verb)) {
|
|
127
446
|
const name = `${verb}Handler`;
|
|
@@ -198,156 +517,67 @@ function writeRouteEntryHandler(writer, route, verb) {
|
|
|
198
517
|
continuations.join();
|
|
199
518
|
writer.writeBlockEnd("}");
|
|
200
519
|
}
|
|
201
|
-
|
|
202
|
-
trailingSlashes: "RedirectWithout",
|
|
203
|
-
}) {
|
|
204
|
-
const writer = createStringWriter();
|
|
205
|
-
const hasErrorPage = Boolean(routes.special[RoutableFileTypes.Error]);
|
|
206
|
-
const hasNotFoundPage = Boolean(routes.special[RoutableFileTypes.NotFound]);
|
|
207
|
-
const imports = writer.branch("imports");
|
|
208
|
-
if (runtimeInclude) {
|
|
209
|
-
imports.writeLines(`import "${normalizePath(runtimeInclude)}";`);
|
|
210
|
-
}
|
|
211
|
-
imports.writeLines(`import { NotHandled, NotMatched, createContext } from "${virtualFilePrefix}/runtime/internal";`);
|
|
212
|
-
for (const route of routes.list) {
|
|
213
|
-
const verbs = getVerbs(route);
|
|
214
|
-
const routeImports = [];
|
|
215
|
-
for (const verb of verbs) {
|
|
216
|
-
const verbName = `${verb}${route.index}`;
|
|
217
|
-
routeImports.push(verbName);
|
|
218
|
-
if (route.meta) {
|
|
219
|
-
routeImports.push(`${verbName}_meta`);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
imports.writeLines(`import { ${routeImports.join(", ")} } from "${virtualFilePrefix}/${getRouteVirtualFileName(route)}";`);
|
|
223
|
-
}
|
|
224
|
-
for (const route of Object.values(routes.special)) {
|
|
225
|
-
imports.writeLines(`import page${route.key} from "${normalizedRelativePath(rootDir, route.templateFilePath || route.page.filePath)}?server-entry";`);
|
|
226
|
-
}
|
|
227
|
-
writer
|
|
228
|
-
.writeLines(`
|
|
229
|
-
globalThis.__marko_run__ = { match, fetch, invoke };
|
|
230
|
-
`)
|
|
231
|
-
.writeBlockStart(`export function match(method, pathname) {`)
|
|
232
|
-
.writeLines(`const last = pathname.length - 1;
|
|
233
|
-
return match_internal(method, last && pathname.charAt(last) === '/' ? pathname.slice(0, last) : pathname)
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
function match_internal(method, pathname) {
|
|
237
|
-
const len = pathname.length;`)
|
|
238
|
-
.writeBlockStart(`switch (method) {`);
|
|
239
|
-
for (const verb of httpVerbs) {
|
|
240
|
-
const filteredRoutes = routes.list.filter((route) => hasVerb(route, verb));
|
|
241
|
-
if (filteredRoutes.length) {
|
|
242
|
-
const trie = createRouteTrie(filteredRoutes);
|
|
243
|
-
writer.writeLines(`case '${verb.toUpperCase()}':`);
|
|
244
|
-
writer.writeBlockStart(`case '${verb.toLowerCase()}': {`);
|
|
245
|
-
writeRouterVerb(writer, trie, verb);
|
|
246
|
-
writer.writeBlockEnd("}");
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
writer.writeBlockEnd("}").writeLines("return null;").writeBlockEnd("}");
|
|
250
|
-
writer
|
|
251
|
-
.writeLines("")
|
|
252
|
-
.writeBlockStart("export async function invoke(route, request, platform, url) {")
|
|
253
|
-
.writeLines("const context = createContext(route, request, platform, url);");
|
|
254
|
-
if (hasErrorPage) {
|
|
255
|
-
writer.writeBlockStart("try {");
|
|
256
|
-
}
|
|
257
|
-
writer
|
|
258
|
-
.writeBlockStart("if (route) {")
|
|
259
|
-
.writeBlockStart("try {")
|
|
260
|
-
.writeLines("const response = await route.handler(context);", "if (response) return response;").indent--;
|
|
261
|
-
writer
|
|
262
|
-
.writeBlockStart("} catch (error) {")
|
|
263
|
-
.writeLines("if (error === NotHandled) return;", "if (error !== NotMatched) throw error;")
|
|
264
|
-
.writeBlockEnd("}")
|
|
265
|
-
.writeBlockEnd("}");
|
|
266
|
-
if (hasNotFoundPage) {
|
|
267
|
-
imports.writeLines(`
|
|
268
|
-
const page404ResponseInit = {
|
|
269
|
-
status: 404,
|
|
270
|
-
headers: { "content-type": "text/html;charset=UTF-8" },
|
|
271
|
-
};`);
|
|
272
|
-
writer.write(`
|
|
273
|
-
if (context.request.headers.get('Accept')?.includes('text/html')) {
|
|
274
|
-
return context.render(page404, {}, page404ResponseInit);
|
|
275
|
-
}`);
|
|
276
|
-
}
|
|
277
|
-
writer.indent--;
|
|
278
|
-
if (routes.list.length) {
|
|
279
|
-
writer.writeLines(`
|
|
280
|
-
return new Response(null, {
|
|
281
|
-
status: 404,
|
|
282
|
-
});`);
|
|
283
|
-
}
|
|
284
|
-
if (hasErrorPage) {
|
|
285
|
-
imports.writeLines(`
|
|
286
|
-
const page500ResponseInit = {
|
|
287
|
-
status: 500,
|
|
288
|
-
headers: { "content-type": "text/html;charset=UTF-8" },
|
|
289
|
-
};`);
|
|
290
|
-
writer
|
|
291
|
-
.writeBlockStart(`} catch (error) {`)
|
|
292
|
-
.writeBlockStart(`if (context.request.headers.get('Accept')?.includes('text/html')) {`)
|
|
293
|
-
.writeLines(`return context.render(page500, { error }, page500ResponseInit);`)
|
|
294
|
-
.writeBlockEnd("}")
|
|
295
|
-
.writeLines("throw error;")
|
|
296
|
-
.writeBlockEnd("}");
|
|
297
|
-
}
|
|
298
|
-
writer.writeBlockEnd("}");
|
|
299
|
-
renderFetch(writer, options);
|
|
300
|
-
return writer.end();
|
|
301
|
-
}
|
|
302
|
-
function renderFetch(writer, options) {
|
|
520
|
+
function renderFetch(writer) {
|
|
303
521
|
writer.write(`
|
|
304
522
|
export async function fetch(request, platform) {
|
|
305
523
|
try {
|
|
306
524
|
const url = new URL(request.url);
|
|
307
525
|
const { pathname } = url;
|
|
308
|
-
const
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
526
|
+
const route = match_internal(request.method, pathname.length > 1 && pathname.endsWith('/') ? pathname.slice(0, -1) : pathname);
|
|
527
|
+
return await invoke(route, request, platform, url);
|
|
528
|
+
} catch (error) {
|
|
529
|
+
if (import.meta.env.DEV) {
|
|
530
|
+
throw error;
|
|
531
|
+
}
|
|
532
|
+
return new Response(null, {
|
|
533
|
+
status: 500
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
}`);
|
|
537
|
+
}
|
|
538
|
+
// The policy lives in `invoke` so every entry point — the generated `fetch`
|
|
539
|
+
// and the public `match`/`invoke` pair adapters compose — applies it.
|
|
540
|
+
function renderTrailingSlashPolicy(writer, options) {
|
|
541
|
+
if (!options.trailingSlashes || options.trailingSlashes === "Ignore") {
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
writer
|
|
545
|
+
.writeBlockStart("if (route) {")
|
|
546
|
+
.writeLines("url ??= new URL(request.url);", "const { pathname } = url;");
|
|
312
547
|
switch (options.trailingSlashes) {
|
|
313
548
|
case "RedirectWithout":
|
|
314
|
-
writer
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
}`);
|
|
549
|
+
writer
|
|
550
|
+
.writeBlockStart("if (pathname.length > 1 && pathname.endsWith('/')) {")
|
|
551
|
+
.writeLines("url.pathname = pathname.slice(0, -1);", "return Response.redirect(url);")
|
|
552
|
+
.writeBlockEnd("}");
|
|
319
553
|
break;
|
|
320
554
|
case "RedirectWith":
|
|
321
|
-
writer
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
}`);
|
|
555
|
+
writer
|
|
556
|
+
.writeBlockStart("if (!pathname.endsWith('/')) {")
|
|
557
|
+
.writeLines("url.pathname += '/';", "return Response.redirect(url);")
|
|
558
|
+
.writeBlockEnd("}");
|
|
326
559
|
break;
|
|
327
560
|
case "RewriteWithout":
|
|
328
|
-
writer
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
561
|
+
writer
|
|
562
|
+
.writeBlockStart("if (pathname.length > 1 && pathname.endsWith('/')) {")
|
|
563
|
+
.writeLines("url.pathname = pathname.slice(0, -1);")
|
|
564
|
+
.writeBlockEnd("}");
|
|
332
565
|
break;
|
|
333
566
|
case "RewriteWith":
|
|
334
|
-
writer
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
567
|
+
writer
|
|
568
|
+
.writeBlockStart("if (!pathname.endsWith('/')) {")
|
|
569
|
+
.writeLines("url.pathname += '/';")
|
|
570
|
+
.writeBlockEnd("}");
|
|
338
571
|
break;
|
|
339
572
|
}
|
|
340
|
-
writer.
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
});
|
|
349
|
-
}
|
|
350
|
-
}`);
|
|
573
|
+
writer.writeBlockEnd("}");
|
|
574
|
+
}
|
|
575
|
+
// Anything outside RFC 3986's unreserved set (or stored %-escapes) may
|
|
576
|
+
// legitimately arrive percent-encoded, so it must be compared decoded.
|
|
577
|
+
const encodedOnWire = /[^\w.~-]/;
|
|
578
|
+
function needsDecode({ key }) {
|
|
579
|
+
const decoded = decodeURIComponent(key);
|
|
580
|
+
return decoded !== key || encodedOnWire.test(decoded);
|
|
351
581
|
}
|
|
352
582
|
function writeRouterVerb(writer, trie, verb, level = 0, offset = 1) {
|
|
353
583
|
const { route, dynamic, catchAll } = trie;
|
|
@@ -386,7 +616,7 @@ function writeRouterVerb(writer, trie, verb, level = 0, offset = 1) {
|
|
|
386
616
|
writer.writeLines(`const ${segment} = decodeURIComponent(${value});`);
|
|
387
617
|
value = segment;
|
|
388
618
|
}
|
|
389
|
-
else if (terminal?.some(
|
|
619
|
+
else if (terminal?.some(needsDecode)) {
|
|
390
620
|
value = `decodeURIComponent(${value})`;
|
|
391
621
|
}
|
|
392
622
|
if (terminal) {
|
|
@@ -397,10 +627,10 @@ function writeRouterVerb(writer, trie, verb, level = 0, offset = 1) {
|
|
|
397
627
|
for (const { key, path, route } of terminal) {
|
|
398
628
|
const decodedKey = decodeURIComponent(key);
|
|
399
629
|
if (useSwitch) {
|
|
400
|
-
writer.write(`case
|
|
630
|
+
writer.write(`case ${JSON.stringify(decodedKey)}: `, true);
|
|
401
631
|
}
|
|
402
632
|
else {
|
|
403
|
-
writer.write(`if (${value} ===
|
|
633
|
+
writer.write(`if (${value} === ${JSON.stringify(decodedKey)}) `, true);
|
|
404
634
|
}
|
|
405
635
|
writer.write(`return ${renderMatch(verb, route, path)};\n`);
|
|
406
636
|
}
|
|
@@ -421,12 +651,13 @@ function writeRouterVerb(writer, trie, verb, level = 0, offset = 1) {
|
|
|
421
651
|
closeCount++;
|
|
422
652
|
}
|
|
423
653
|
let value = `pathname.slice(${offset}, ${index} - 1)`;
|
|
654
|
+
const decodeChildren = !!children?.some(needsDecode);
|
|
424
655
|
if (dynamic?.static || dynamic?.dynamic || dynamic?.catchAll) {
|
|
425
656
|
const segment = `s${next}`;
|
|
426
657
|
writer.writeLines(`const ${segment} = decodeURIComponent(${value});`);
|
|
427
658
|
value = segment;
|
|
428
659
|
}
|
|
429
|
-
else if (
|
|
660
|
+
else if (decodeChildren) {
|
|
430
661
|
value = `decodeURIComponent(${value})`;
|
|
431
662
|
}
|
|
432
663
|
if (children) {
|
|
@@ -437,12 +668,16 @@ function writeRouterVerb(writer, trie, verb, level = 0, offset = 1) {
|
|
|
437
668
|
for (const child of children) {
|
|
438
669
|
const decodedKey = decodeURIComponent(child.key);
|
|
439
670
|
if (useSwitch) {
|
|
440
|
-
writer.writeBlockStart(`case
|
|
671
|
+
writer.writeBlockStart(`case ${JSON.stringify(decodedKey)}: {`);
|
|
441
672
|
}
|
|
442
673
|
else {
|
|
443
|
-
writer.writeBlockStart(`if (${value} ===
|
|
674
|
+
writer.writeBlockStart(`if (${value} === ${JSON.stringify(decodedKey)}) {`);
|
|
444
675
|
}
|
|
445
|
-
|
|
676
|
+
// A decoded comparison means the wire length can differ from the
|
|
677
|
+
// key length, so the next segment starts at the runtime index.
|
|
678
|
+
const nextOffset = typeof offset === "string" || decodeChildren
|
|
679
|
+
? index
|
|
680
|
+
: offset + child.key.length + 1;
|
|
446
681
|
writeRouterVerb(writer, child, verb, next, nextOffset);
|
|
447
682
|
if (useSwitch) {
|
|
448
683
|
writer.writeBlockEnd("} break;");
|
|
@@ -474,7 +709,7 @@ function writeRouterVerb(writer, trie, verb, level = 0, offset = 1) {
|
|
|
474
709
|
}
|
|
475
710
|
function wrapPropertyName(name) {
|
|
476
711
|
name = decodeURIComponent(name);
|
|
477
|
-
return /^[^A-Za-z_$]|[^A-Za-z0-9$_]/.test(name) ?
|
|
712
|
+
return /^[^A-Za-z_$]|[^A-Za-z0-9$_]/.test(name) ? JSON.stringify(name) : name;
|
|
478
713
|
}
|
|
479
714
|
function renderParams(params, pathIndex) {
|
|
480
715
|
let result = "";
|
|
@@ -490,28 +725,17 @@ function renderParams(params, pathIndex) {
|
|
|
490
725
|
}
|
|
491
726
|
}
|
|
492
727
|
if (catchAll) {
|
|
493
|
-
|
|
728
|
+
// Decoded like the `s${n}` vars behind dynamic segments, so both param
|
|
729
|
+
// kinds agree and `Run.href`'s encode round-trips.
|
|
730
|
+
result += `${sep} ${wrapPropertyName(catchAll)}: decodeURIComponent(pathname.slice(${pathIndex}))`;
|
|
494
731
|
}
|
|
495
732
|
return result ? result + " }" : "{}";
|
|
496
733
|
}
|
|
497
734
|
function renderMatch(verb, route, path, pathIndex) {
|
|
498
|
-
const
|
|
735
|
+
const name = `${verb}${route.index}`;
|
|
499
736
|
const params = path.params ? renderParams(path.params, pathIndex) : "{}";
|
|
500
|
-
const meta = route.meta ? `${
|
|
501
|
-
return `{ handler: ${
|
|
502
|
-
}
|
|
503
|
-
export function renderMiddleware(middleware, rootDir) {
|
|
504
|
-
const writer = createStringWriter();
|
|
505
|
-
const imports = writer.branch("imports");
|
|
506
|
-
imports.writeLines(`import { normalizeHandler } from "${virtualFilePrefix}/runtime/internal";`);
|
|
507
|
-
writer.writeLines("");
|
|
508
|
-
for (const { id, filePath } of middleware) {
|
|
509
|
-
const importName = `middleware${id}`;
|
|
510
|
-
imports.writeLines(`import ${importName} from "${normalizedRelativePath(rootDir, filePath)}";`);
|
|
511
|
-
writer.writeLines(`export const mware${id} = normalizeHandler(${importName});`);
|
|
512
|
-
}
|
|
513
|
-
imports.join();
|
|
514
|
-
return writer.end();
|
|
737
|
+
const meta = route.meta ? `${name}_meta` : "{}";
|
|
738
|
+
return `{ handler: ${name}, path: ${JSON.stringify(path.path)}, params: ${params}, options: ${name}_options, meta: ${meta} }`;
|
|
515
739
|
}
|
|
516
740
|
function stripTsExtension(path) {
|
|
517
741
|
const index = path.lastIndexOf(".");
|
|
@@ -523,179 +747,14 @@ function stripTsExtension(path) {
|
|
|
523
747
|
}
|
|
524
748
|
return path;
|
|
525
749
|
}
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
if (adapter && adapter.typeInfo) {
|
|
536
|
-
const platformType = await adapter.typeInfo((data) => headWriter.write(data));
|
|
537
|
-
if (platformType) {
|
|
538
|
-
writer.writeLines(`interface Platform extends ${platformType} {}\n`);
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
headWriter.join();
|
|
542
|
-
writer
|
|
543
|
-
.writeBlockStart(`interface AppData extends Run.DefineApp<{`)
|
|
544
|
-
.writeBlockStart("routes: {");
|
|
545
|
-
const routesWriter = writer.branch("routes");
|
|
546
|
-
writer.writeBlockEnd("}").writeBlockEnd(`}> {}`).writeBlockEnd(`}`);
|
|
547
|
-
const routeTypes = new Map();
|
|
548
|
-
for (const route of routes.list) {
|
|
549
|
-
let routeType = "";
|
|
550
|
-
let routeDefinition = "";
|
|
551
|
-
if (route.page || route.handler) {
|
|
552
|
-
const verbs = [];
|
|
553
|
-
if (route.page || route.handler?.verbs?.includes("get")) {
|
|
554
|
-
verbs.push(`"get"`);
|
|
555
|
-
}
|
|
556
|
-
if (route.handler?.verbs?.includes("post")) {
|
|
557
|
-
verbs.push(`"post"`);
|
|
558
|
-
}
|
|
559
|
-
routeDefinition = `{ verb: ${verbs.join(" | ")};`;
|
|
560
|
-
if (route.meta) {
|
|
561
|
-
const metaPath = stripTsExtension(normalizedRelativePath(outDir, route.meta.filePath));
|
|
562
|
-
let metaType = `typeof import("${metaPath}")`;
|
|
563
|
-
if (/\.(ts|js|mjs)$/.test(route.meta.name)) {
|
|
564
|
-
metaType += `["default"]`;
|
|
565
|
-
}
|
|
566
|
-
routeDefinition += ` meta: ${metaType};`;
|
|
567
|
-
}
|
|
568
|
-
routeDefinition += " }";
|
|
569
|
-
}
|
|
570
|
-
const pathType = `"${route.path.path}"`;
|
|
571
|
-
routeType += routeType ? " | " + pathType : pathType;
|
|
572
|
-
routesWriter.writeLines(`${pathType}: ${routeDefinition};`);
|
|
573
|
-
for (const file of [route.handler, route.page]) {
|
|
574
|
-
if (file) {
|
|
575
|
-
const existing = routeTypes.get(file);
|
|
576
|
-
if (!existing) {
|
|
577
|
-
routeTypes.set(file, [routeType]);
|
|
578
|
-
}
|
|
579
|
-
else {
|
|
580
|
-
existing.push(routeType);
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
for (const files of [route.middleware, route.layouts]) {
|
|
585
|
-
if (files) {
|
|
586
|
-
for (const file of files) {
|
|
587
|
-
const existing = routeTypes.get(file);
|
|
588
|
-
if (!existing) {
|
|
589
|
-
routeTypes.set(file, [routeType]);
|
|
590
|
-
}
|
|
591
|
-
else {
|
|
592
|
-
existing.push(routeType);
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
for (const special of Object.values(routes.special)) {
|
|
599
|
-
routeTypes.set(special.page, []);
|
|
600
|
-
}
|
|
601
|
-
routesWriter.join();
|
|
602
|
-
const handlerWriter = writer.branch("handler");
|
|
603
|
-
const middlewareWriter = writer.branch("middleware");
|
|
604
|
-
const pageWriter = writer.branch("page");
|
|
605
|
-
const layoutWriter = writer.branch("layout");
|
|
606
|
-
for (const [file, types] of routeTypes) {
|
|
607
|
-
const modulePath = stripTsExtension(normalizedRelativePath(outDir, file.filePath));
|
|
608
|
-
const routeType = `Run.Routes[${types.join(" | ")}]`;
|
|
609
|
-
switch (file.type) {
|
|
610
|
-
case RoutableFileTypes.Handler:
|
|
611
|
-
writeModuleDeclaration(handlerWriter, modulePath, routeType);
|
|
612
|
-
break;
|
|
613
|
-
case RoutableFileTypes.Middleware:
|
|
614
|
-
writeModuleDeclaration(middlewareWriter, modulePath, routeType);
|
|
615
|
-
break;
|
|
616
|
-
case RoutableFileTypes.Page:
|
|
617
|
-
writeModuleDeclaration(pageWriter, modulePath, routeType);
|
|
618
|
-
break;
|
|
619
|
-
case RoutableFileTypes.Layout:
|
|
620
|
-
writeModuleDeclaration(layoutWriter, modulePath, routeType, `
|
|
621
|
-
export interface Input extends Run.LayoutInput<typeof import("${modulePath}")> {}`);
|
|
622
|
-
break;
|
|
623
|
-
case RoutableFileTypes.Error:
|
|
624
|
-
writeModuleDeclaration(writer, modulePath, "globalThis.MarkoRun.Route", `
|
|
625
|
-
export interface Input {
|
|
626
|
-
error: unknown;
|
|
627
|
-
}`);
|
|
628
|
-
break;
|
|
629
|
-
case RoutableFileTypes.NotFound:
|
|
630
|
-
writeModuleDeclaration(writer, modulePath, "Run.Route");
|
|
631
|
-
break;
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
handlerWriter.join();
|
|
635
|
-
middlewareWriter.join();
|
|
636
|
-
pageWriter.join();
|
|
637
|
-
layoutWriter.join();
|
|
638
|
-
return writer.end();
|
|
639
|
-
}
|
|
640
|
-
function writeModuleDeclaration(writer, name, routeType, moduleTypes) {
|
|
641
|
-
writer.writeLines("").write(`declare module "${name}" {`);
|
|
642
|
-
if (moduleTypes) {
|
|
643
|
-
writer.write(moduleTypes);
|
|
644
|
-
}
|
|
645
|
-
if (routeType) {
|
|
646
|
-
const isMarko = name.endsWith(".marko");
|
|
647
|
-
writer.write(`
|
|
648
|
-
namespace MarkoRun {
|
|
649
|
-
export { NotHandled, NotMatched, GetPaths, PostPaths, GetablePath, GetableHref, PostablePath, PostableHref, Platform };
|
|
650
|
-
export type Route = ${routeType};
|
|
651
|
-
export type Context = Run.MultiRouteContext<Route>${isMarko ? " & Marko.Global" : ""};
|
|
652
|
-
export type Handler = Run.HandlerLike<Route>;`);
|
|
653
|
-
for (const verb of httpVerbs) {
|
|
654
|
-
writer.write(`
|
|
655
|
-
export type ${verb.toUpperCase()} = Run.HandlerLike<Route, "${verb.toUpperCase()}">;`);
|
|
656
|
-
}
|
|
657
|
-
writer.write(`
|
|
658
|
-
/** @deprecated use \`((context, next) => { ... }) satisfies MarkoRun.Handler\` instead */
|
|
659
|
-
export const route: Run.HandlerTypeFn<Route>;
|
|
660
|
-
}`);
|
|
661
|
-
}
|
|
662
|
-
writer.writeLines(`
|
|
663
|
-
}`);
|
|
664
|
-
}
|
|
665
|
-
function createRouteTrie(routes) {
|
|
666
|
-
const root = {
|
|
667
|
-
key: "",
|
|
668
|
-
};
|
|
669
|
-
function insert(path, route) {
|
|
670
|
-
let node = root;
|
|
671
|
-
for (const segment of path.segments) {
|
|
672
|
-
if (segment === "$$") {
|
|
673
|
-
node.catchAll ??= { route, path };
|
|
674
|
-
return;
|
|
675
|
-
}
|
|
676
|
-
else if (segment === "$") {
|
|
677
|
-
node = node.dynamic ??= {
|
|
678
|
-
key: "",
|
|
679
|
-
};
|
|
680
|
-
}
|
|
681
|
-
else {
|
|
682
|
-
node.static ??= new Map();
|
|
683
|
-
let next = node.static.get(segment);
|
|
684
|
-
if (!next) {
|
|
685
|
-
next = {
|
|
686
|
-
key: segment,
|
|
687
|
-
};
|
|
688
|
-
node.static.set(segment, next);
|
|
689
|
-
}
|
|
690
|
-
node = next;
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
|
-
node.path ??= path;
|
|
694
|
-
node.route ??= route;
|
|
695
|
-
}
|
|
696
|
-
for (const route of routes) {
|
|
697
|
-
insert(route.path, route);
|
|
698
|
-
}
|
|
699
|
-
return root;
|
|
750
|
+
function* routeFileIter(route) {
|
|
751
|
+
yield* route.middleware;
|
|
752
|
+
if (route.handler)
|
|
753
|
+
yield route.handler;
|
|
754
|
+
yield* route.layouts;
|
|
755
|
+
if (route.page)
|
|
756
|
+
yield route.page;
|
|
757
|
+
if (route.meta)
|
|
758
|
+
yield route.meta;
|
|
700
759
|
}
|
|
701
760
|
//# sourceMappingURL=index.js.map
|