@markopack/core 0.1.5 → 0.2.1
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/adapter/middleware.d.ts +2 -44
- package/dist/adapter/middleware.d.ts.map +1 -1
- package/dist/adapter/middleware.js +4 -133
- package/dist/adapter/middleware.js.map +1 -1
- package/dist/codegen/index.d.ts.map +1 -0
- package/dist/{vite/codegen → codegen}/index.js +436 -374
- package/dist/codegen/index.js.map +1 -0
- package/dist/codegen/writer.d.ts.map +1 -0
- package/dist/codegen/writer.js.map +1 -0
- package/dist/{vite/constants.d.ts → constants.d.ts} +5 -5
- package/dist/constants.d.ts.map +1 -0
- package/dist/{vite/constants.js → constants.js} +5 -4
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/routes/builder.d.ts.map +1 -0
- package/dist/{vite/routes → routes}/builder.js +38 -7
- package/dist/routes/builder.js.map +1 -0
- package/dist/routes/parse.d.ts.map +1 -0
- package/dist/{vite/routes → routes}/parse.js +0 -2
- package/dist/routes/parse.js.map +1 -0
- package/dist/routes/vdir.d.ts.map +1 -0
- package/dist/routes/vdir.js.map +1 -0
- package/dist/routes/walk.d.ts.map +1 -0
- package/dist/routes/walk.js.map +1 -0
- package/dist/{vite/types.d.ts → types.d.ts} +3 -11
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js.map +1 -0
- 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/fs.d.ts.map +1 -0
- package/dist/utils/fs.js.map +1 -0
- package/dist/utils/route.d.ts.map +1 -0
- package/dist/utils/route.js.map +1 -0
- package/dist/utils/server.d.ts +9 -0
- package/dist/utils/server.d.ts.map +1 -0
- package/dist/{vite/utils → utils}/server.js +9 -74
- package/dist/utils/server.js.map +1 -0
- package/package.json +28 -32
- package/dist/vite/codegen/index.d.ts.map +0 -1
- package/dist/vite/codegen/index.js.map +0 -1
- package/dist/vite/codegen/writer.d.ts.map +0 -1
- package/dist/vite/codegen/writer.js.map +0 -1
- package/dist/vite/constants.d.ts.map +0 -1
- package/dist/vite/constants.js.map +0 -1
- package/dist/vite/routes/builder.d.ts.map +0 -1
- package/dist/vite/routes/builder.js.map +0 -1
- package/dist/vite/routes/parse.d.ts.map +0 -1
- package/dist/vite/routes/parse.js.map +0 -1
- package/dist/vite/routes/vdir.d.ts.map +0 -1
- package/dist/vite/routes/vdir.js.map +0 -1
- package/dist/vite/routes/walk.d.ts.map +0 -1
- package/dist/vite/routes/walk.js.map +0 -1
- package/dist/vite/types.d.ts.map +0 -1
- package/dist/vite/types.js.map +0 -1
- package/dist/vite/utils/fs.d.ts.map +0 -1
- package/dist/vite/utils/fs.js.map +0 -1
- package/dist/vite/utils/route.d.ts.map +0 -1
- package/dist/vite/utils/route.js.map +0 -1
- package/dist/vite/utils/server.d.ts +0 -23
- package/dist/vite/utils/server.d.ts.map +0 -1
- package/dist/vite/utils/server.js.map +0 -1
- /package/dist/{vite/codegen → codegen}/index.d.ts +0 -0
- /package/dist/{vite/codegen → codegen}/writer.d.ts +0 -0
- /package/dist/{vite/codegen → codegen}/writer.js +0 -0
- /package/dist/{vite/routes → routes}/builder.d.ts +0 -0
- /package/dist/{vite/routes → routes}/parse.d.ts +0 -0
- /package/dist/{vite/routes → routes}/vdir.d.ts +0 -0
- /package/dist/{vite/routes → routes}/vdir.js +0 -0
- /package/dist/{vite/routes → routes}/walk.d.ts +0 -0
- /package/dist/{vite/routes → routes}/walk.js +0 -0
- /package/dist/{vite/types.js → types.js} +0 -0
- /package/dist/{vite/utils → utils}/fs.d.ts +0 -0
- /package/dist/{vite/utils → utils}/fs.js +0 -0
- /package/dist/{vite/utils → utils}/route.d.ts +0 -0
- /package/dist/{vite/utils → utils}/route.js +0 -0
|
@@ -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,9 +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");
|
|
37
|
+
}
|
|
38
|
+
if (page) {
|
|
39
|
+
runtimeImports.push("render");
|
|
61
40
|
}
|
|
62
|
-
if (!page ||
|
|
41
|
+
if (!page ||
|
|
42
|
+
verbs.some((verb) => !(verb === "get" ||
|
|
43
|
+
verb === "head" ||
|
|
44
|
+
verb === "post" ||
|
|
45
|
+
verb === "query"))) {
|
|
63
46
|
runtimeImports.push("noContent");
|
|
64
47
|
}
|
|
65
48
|
if (verbs.includes("head")) {
|
|
@@ -78,12 +61,12 @@ export function renderRouteEntry(route, rootDir) {
|
|
|
78
61
|
for (const verb of handler.verbs) {
|
|
79
62
|
const importName = verb.toUpperCase();
|
|
80
63
|
names.push(importName);
|
|
81
|
-
writer.writeLines(`const ${verb}Handler = normalizeHandler(${importName});`);
|
|
64
|
+
writer.writeLines(`const ${verb}Handler = normalizeHandler(${importName}, '${importName}');`);
|
|
82
65
|
}
|
|
83
66
|
imports.writeLines(`import { ${names.join(", ")} } from "${normalizedRelativePath(rootDir, handler.filePath)}";`);
|
|
84
67
|
}
|
|
85
68
|
if (page) {
|
|
86
|
-
imports.writeLines(`import page from "${normalizedRelativePath(rootDir, route.templateFilePath
|
|
69
|
+
imports.writeLines(`import page from "${normalizedRelativePath(rootDir, route.templateFilePath)}?server-entry";`);
|
|
87
70
|
}
|
|
88
71
|
if (meta) {
|
|
89
72
|
const metaName = `meta${index}`;
|
|
@@ -99,102 +82,14 @@ export function renderRouteEntry(route, rootDir) {
|
|
|
99
82
|
imports.writeLines(`import ${metaName} from "${normalizedRelativePath(rootDir, meta.filePath)}";`);
|
|
100
83
|
writer.writeLines(`export const { ${metaVerbsExports} } = normalizeMeta(${metaName});`);
|
|
101
84
|
}
|
|
85
|
+
const optionsWriter = writer.branch("options").writeLines("");
|
|
102
86
|
for (const verb of verbs) {
|
|
87
|
+
writeRouteOptions(optionsWriter, route, verb);
|
|
103
88
|
writeRouteEntryHandler(writer, route, verb);
|
|
104
89
|
}
|
|
90
|
+
optionsWriter.join();
|
|
105
91
|
return writer.end();
|
|
106
92
|
}
|
|
107
|
-
function writeRouteEntryHandler(writer, route, verb) {
|
|
108
|
-
const { key, index, page, handler, middleware } = route;
|
|
109
|
-
const len = middleware.length;
|
|
110
|
-
let nextName;
|
|
111
|
-
let currentName;
|
|
112
|
-
let hasBody = false;
|
|
113
|
-
writer.writeLines("");
|
|
114
|
-
if (page && (verb === "get" || verb === "head")) {
|
|
115
|
-
writer.writeBlockStart(`export function ${verb}${index}(context) {`);
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
writer.writeBlockStart(`export function ${verb}${index}(context) {`);
|
|
119
|
-
}
|
|
120
|
-
const continuations = writer.branch("cont");
|
|
121
|
-
if (page && (verb === "get" || verb === "head")) {
|
|
122
|
-
currentName = "__page";
|
|
123
|
-
if (handler?.verbs?.includes(verb)) {
|
|
124
|
-
const name = `${verb}Handler`;
|
|
125
|
-
continuations.writeLines(`const ${currentName} = () => context.render(page, {});`);
|
|
126
|
-
if (len) {
|
|
127
|
-
nextName = currentName;
|
|
128
|
-
currentName = `__${name}`;
|
|
129
|
-
continuations.writeLines(`const ${currentName} = () => call(${name}, ${nextName}, context);`);
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
if (verb === "head") {
|
|
133
|
-
writer.writeLines(`return stripResponseBody(call(${name}, ${currentName}, context));`);
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
136
|
-
writer.writeLines(`return call(${name}, ${currentName}, context);`);
|
|
137
|
-
}
|
|
138
|
-
hasBody = true;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
else if (verb === "head") {
|
|
142
|
-
writer.writeLines(`return stripResponseBody(get${index}(context));`);
|
|
143
|
-
hasBody = true;
|
|
144
|
-
}
|
|
145
|
-
else if (len) {
|
|
146
|
-
continuations.writeLines(`const ${currentName} = () => context.render(page, {});`);
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
writer.writeLines(`return context.render(page, {});`);
|
|
150
|
-
hasBody = true;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
else if (handler?.verbs?.includes(verb)) {
|
|
154
|
-
const name = `${verb}Handler`;
|
|
155
|
-
currentName = `__${name}`;
|
|
156
|
-
nextName = "noContent";
|
|
157
|
-
if (len) {
|
|
158
|
-
continuations.writeLines(`const ${currentName} = () => call(${name}, ${nextName}, context);`);
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
if (verb === "head") {
|
|
162
|
-
writer.writeLines(`return stripResponseBody(call(${name}, ${nextName}, context));`);
|
|
163
|
-
}
|
|
164
|
-
else {
|
|
165
|
-
writer.writeLines(`return call(${name}, ${nextName}, context);`);
|
|
166
|
-
}
|
|
167
|
-
hasBody = true;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
else if (verb === "head" && route.handler?.verbs?.includes("get")) {
|
|
171
|
-
writer.writeLines(`return stripResponseBody(get${index}(context));`);
|
|
172
|
-
hasBody = true;
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
throw new Error(`Route ${key} has no handler for ${verb} requests`);
|
|
176
|
-
}
|
|
177
|
-
if (!hasBody) {
|
|
178
|
-
let i = len;
|
|
179
|
-
while (i--) {
|
|
180
|
-
const { id } = middleware[i];
|
|
181
|
-
const name = `mware${id}`;
|
|
182
|
-
nextName = currentName;
|
|
183
|
-
currentName = i ? `__${name}` : "";
|
|
184
|
-
if (currentName) {
|
|
185
|
-
continuations.writeLines(`const ${currentName} = () => call(${name}, ${nextName}, context);`);
|
|
186
|
-
}
|
|
187
|
-
else if (verb === "head") {
|
|
188
|
-
continuations.writeLines(`return stripResponseBody(call(${name}, ${nextName}, context));`);
|
|
189
|
-
}
|
|
190
|
-
else {
|
|
191
|
-
continuations.writeLines(`return call(${name}, ${nextName}, context);`);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
continuations.join();
|
|
196
|
-
writer.writeBlockEnd("}");
|
|
197
|
-
}
|
|
198
93
|
export function renderRouter(routes, rootDir, runtimeInclude, options = {
|
|
199
94
|
trailingSlashes: "RedirectWithout",
|
|
200
95
|
}) {
|
|
@@ -212,6 +107,7 @@ export function renderRouter(routes, rootDir, runtimeInclude, options = {
|
|
|
212
107
|
for (const verb of verbs) {
|
|
213
108
|
const verbName = `${verb}${route.index}`;
|
|
214
109
|
routeImports.push(verbName);
|
|
110
|
+
routeImports.push(`${verbName}_options`);
|
|
215
111
|
if (route.meta) {
|
|
216
112
|
routeImports.push(`${verbName}_meta`);
|
|
217
113
|
}
|
|
@@ -219,19 +115,19 @@ export function renderRouter(routes, rootDir, runtimeInclude, options = {
|
|
|
219
115
|
imports.writeLines(`import { ${routeImports.join(", ")} } from "${virtualFilePrefix}/${getRouteVirtualFileName(route)}";`);
|
|
220
116
|
}
|
|
221
117
|
for (const route of Object.values(routes.special)) {
|
|
222
|
-
imports.writeLines(`import page${route.key} from "${normalizedRelativePath(rootDir, route.templateFilePath
|
|
118
|
+
imports.writeLines(`import page${route.key} from "${normalizedRelativePath(rootDir, route.templateFilePath)}?server-entry";`);
|
|
223
119
|
}
|
|
224
120
|
writer
|
|
225
121
|
.writeLines(`
|
|
226
122
|
globalThis.__marko_run__ = { match, fetch, invoke };
|
|
227
123
|
`)
|
|
228
124
|
.writeBlockStart(`export function match(method, pathname) {`)
|
|
229
|
-
.writeLines(`
|
|
230
|
-
return match_internal(method, last && pathname.charAt(last) === '/' ? pathname.slice(0, last) : pathname)
|
|
125
|
+
.writeLines(`return match_internal(method, pathname.length > 1 && pathname.endsWith('/') ? pathname.slice(0, -1) : pathname)
|
|
231
126
|
};
|
|
232
|
-
|
|
127
|
+
|
|
233
128
|
function match_internal(method, pathname) {
|
|
234
129
|
const len = pathname.length;`)
|
|
130
|
+
.writeBlockStart(`try {`)
|
|
235
131
|
.writeBlockStart(`switch (method) {`);
|
|
236
132
|
for (const verb of httpVerbs) {
|
|
237
133
|
const filteredRoutes = routes.list.filter((route) => hasVerb(route, verb));
|
|
@@ -243,11 +139,17 @@ function match_internal(method, pathname) {
|
|
|
243
139
|
writer.writeBlockEnd("}");
|
|
244
140
|
}
|
|
245
141
|
}
|
|
246
|
-
writer.writeBlockEnd("}").
|
|
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("}");
|
|
247
148
|
writer
|
|
248
149
|
.writeLines("")
|
|
249
|
-
.writeBlockStart("export async function invoke(route, request, platform, url) {")
|
|
250
|
-
|
|
150
|
+
.writeBlockStart("export async function invoke(route, request, platform, url) {");
|
|
151
|
+
renderTrailingSlashPolicy(writer, options);
|
|
152
|
+
writer.writeLines("const context = createContext(route, request, platform, url);");
|
|
251
153
|
if (hasErrorPage) {
|
|
252
154
|
writer.writeBlockStart("try {");
|
|
253
155
|
}
|
|
@@ -261,14 +163,9 @@ function match_internal(method, pathname) {
|
|
|
261
163
|
.writeBlockEnd("}")
|
|
262
164
|
.writeBlockEnd("}");
|
|
263
165
|
if (hasNotFoundPage) {
|
|
264
|
-
|
|
265
|
-
const page404ResponseInit = {
|
|
266
|
-
status: 404,
|
|
267
|
-
headers: { "content-type": "text/html;charset=UTF-8" },
|
|
268
|
-
};`);
|
|
269
|
-
writer.write(`
|
|
166
|
+
writer.write(`
|
|
270
167
|
if (context.request.headers.get('Accept')?.includes('text/html')) {
|
|
271
|
-
return context.render(page404, {},
|
|
168
|
+
return context.render(page404, {}, { status: 404 });
|
|
272
169
|
}`);
|
|
273
170
|
}
|
|
274
171
|
writer.indent--;
|
|
@@ -279,62 +176,354 @@ const page404ResponseInit = {
|
|
|
279
176
|
});`);
|
|
280
177
|
}
|
|
281
178
|
if (hasErrorPage) {
|
|
282
|
-
imports.writeLines(`
|
|
283
|
-
const page500ResponseInit = {
|
|
284
|
-
status: 500,
|
|
285
|
-
headers: { "content-type": "text/html;charset=UTF-8" },
|
|
286
|
-
};`);
|
|
287
179
|
writer
|
|
288
180
|
.writeBlockStart(`} catch (error) {`)
|
|
289
181
|
.writeBlockStart(`if (context.request.headers.get('Accept')?.includes('text/html')) {`)
|
|
290
|
-
.writeLines(`return context.render(page500, { error },
|
|
182
|
+
.writeLines(`return context.render(page500, { error }, { status: 500 });`)
|
|
291
183
|
.writeBlockEnd("}")
|
|
292
184
|
.writeLines("throw error;")
|
|
293
185
|
.writeBlockEnd("}");
|
|
294
186
|
}
|
|
295
187
|
writer.writeBlockEnd("}");
|
|
296
|
-
renderFetch(writer
|
|
188
|
+
renderFetch(writer);
|
|
297
189
|
return writer.end();
|
|
298
190
|
}
|
|
299
|
-
function
|
|
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
|
+
}
|
|
433
|
+
function writeRouteEntryHandler(writer, route, verb) {
|
|
434
|
+
const { key, index, page, handler, middleware } = route;
|
|
435
|
+
const len = middleware.length;
|
|
436
|
+
let nextName;
|
|
437
|
+
let currentName;
|
|
438
|
+
let hasBody = false;
|
|
439
|
+
writer.writeLines("");
|
|
440
|
+
writer.writeBlockStart(`export function ${verb}${index}(context) {`);
|
|
441
|
+
const continuations = writer.branch("cont");
|
|
442
|
+
if (page &&
|
|
443
|
+
(verb === "get" || verb === "head" || verb === "post" || verb === "query")) {
|
|
444
|
+
currentName = "__page";
|
|
445
|
+
if (handler?.verbs?.includes(verb)) {
|
|
446
|
+
const name = `${verb}Handler`;
|
|
447
|
+
continuations.writeLines(`const ${currentName} = (data) => render(context, page, {}, data);`);
|
|
448
|
+
if (len) {
|
|
449
|
+
nextName = currentName;
|
|
450
|
+
currentName = `__${name}`;
|
|
451
|
+
continuations.writeLines(`const ${currentName} = (data) => call(${name}, ${nextName}, context, data);`);
|
|
452
|
+
}
|
|
453
|
+
else {
|
|
454
|
+
if (verb === "head") {
|
|
455
|
+
writer.writeLines(`return stripResponseBody(call(${name}, ${currentName}, context));`);
|
|
456
|
+
}
|
|
457
|
+
else {
|
|
458
|
+
writer.writeLines(`return call(${name}, ${currentName}, context);`);
|
|
459
|
+
}
|
|
460
|
+
hasBody = true;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
else if (verb === "head") {
|
|
464
|
+
writer.writeLines(`return stripResponseBody(get${index}(context));`);
|
|
465
|
+
hasBody = true;
|
|
466
|
+
}
|
|
467
|
+
else if (len) {
|
|
468
|
+
continuations.writeLines(`const ${currentName} = (data) => render(context, page, {}, data);`);
|
|
469
|
+
}
|
|
470
|
+
else {
|
|
471
|
+
writer.writeLines(`return render(context, page, {});`);
|
|
472
|
+
hasBody = true;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
else if (handler?.verbs?.includes(verb)) {
|
|
476
|
+
const name = `${verb}Handler`;
|
|
477
|
+
currentName = `__${name}`;
|
|
478
|
+
nextName = "noContent";
|
|
479
|
+
if (len) {
|
|
480
|
+
continuations.writeLines(`const ${currentName} = (data) => call(${name}, ${nextName}, context, data);`);
|
|
481
|
+
}
|
|
482
|
+
else {
|
|
483
|
+
if (verb === "head") {
|
|
484
|
+
writer.writeLines(`return stripResponseBody(call(${name}, ${nextName}, context));`);
|
|
485
|
+
}
|
|
486
|
+
else {
|
|
487
|
+
writer.writeLines(`return call(${name}, ${nextName}, context);`);
|
|
488
|
+
}
|
|
489
|
+
hasBody = true;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
else if (verb === "head" && route.handler?.verbs?.includes("get")) {
|
|
493
|
+
writer.writeLines(`return stripResponseBody(get${index}(context));`);
|
|
494
|
+
hasBody = true;
|
|
495
|
+
}
|
|
496
|
+
else {
|
|
497
|
+
throw new Error(`Route ${key} has no handler for ${verb} requests`);
|
|
498
|
+
}
|
|
499
|
+
if (!hasBody) {
|
|
500
|
+
let i = len;
|
|
501
|
+
while (i--) {
|
|
502
|
+
const { id } = middleware[i];
|
|
503
|
+
const name = `mware${id}`;
|
|
504
|
+
nextName = currentName;
|
|
505
|
+
currentName = i ? `__${name}` : "";
|
|
506
|
+
if (currentName) {
|
|
507
|
+
continuations.writeLines(`const ${currentName} = (data) => call(${name}, ${nextName}, context, data);`);
|
|
508
|
+
}
|
|
509
|
+
else if (verb === "head") {
|
|
510
|
+
continuations.writeLines(`return stripResponseBody(call(${name}, ${nextName}, context));`);
|
|
511
|
+
}
|
|
512
|
+
else {
|
|
513
|
+
continuations.writeLines(`return call(${name}, ${nextName}, context);`);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
continuations.join();
|
|
518
|
+
writer.writeBlockEnd("}");
|
|
519
|
+
}
|
|
520
|
+
function renderFetch(writer) {
|
|
300
521
|
writer.write(`
|
|
301
522
|
export async function fetch(request, platform) {
|
|
302
523
|
try {
|
|
303
524
|
const url = new URL(request.url);
|
|
304
525
|
const { pathname } = url;
|
|
305
|
-
const
|
|
306
|
-
const hasTrailingSlash = last && pathname.charAt(last) === '/';
|
|
307
|
-
const normalizedPathname = hasTrailingSlash ? pathname.slice(0, last) : pathname;
|
|
308
|
-
const route = match_internal(request.method, normalizedPathname);`);
|
|
309
|
-
switch (options.trailingSlashes) {
|
|
310
|
-
case "RedirectWithout":
|
|
311
|
-
writer.write(`
|
|
312
|
-
if (route && hasTrailingSlash) {
|
|
313
|
-
url.pathname = normalizedPathname
|
|
314
|
-
return Response.redirect(url);
|
|
315
|
-
}`);
|
|
316
|
-
break;
|
|
317
|
-
case "RedirectWith":
|
|
318
|
-
writer.write(`
|
|
319
|
-
if (route && pathname !== '/' && !hasTrailingSlash) {
|
|
320
|
-
url.pathname += '/';
|
|
321
|
-
return Response.redirect(url);
|
|
322
|
-
}`);
|
|
323
|
-
break;
|
|
324
|
-
case "RewriteWithout":
|
|
325
|
-
writer.write(`
|
|
326
|
-
if (route && hasTrailingSlash) {
|
|
327
|
-
url.pathname = normalizedPathname;
|
|
328
|
-
}`);
|
|
329
|
-
break;
|
|
330
|
-
case "RewriteWith":
|
|
331
|
-
writer.write(`
|
|
332
|
-
if (route && pathname !== '/' && !hasTrailingSlash) {
|
|
333
|
-
url.pathname += '/';
|
|
334
|
-
}`);
|
|
335
|
-
break;
|
|
336
|
-
}
|
|
337
|
-
writer.write(`
|
|
526
|
+
const route = match_internal(request.method, pathname.length > 1 && pathname.endsWith('/') ? pathname.slice(0, -1) : pathname);
|
|
338
527
|
return await invoke(route, request, platform, url);
|
|
339
528
|
} catch (error) {
|
|
340
529
|
if (import.meta.env.DEV) {
|
|
@@ -346,6 +535,50 @@ export async function fetch(request, platform) {
|
|
|
346
535
|
}
|
|
347
536
|
}`);
|
|
348
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;");
|
|
547
|
+
switch (options.trailingSlashes) {
|
|
548
|
+
case "RedirectWithout":
|
|
549
|
+
writer
|
|
550
|
+
.writeBlockStart("if (pathname.length > 1 && pathname.endsWith('/')) {")
|
|
551
|
+
.writeLines("url.pathname = pathname.slice(0, -1);", "return Response.redirect(url);")
|
|
552
|
+
.writeBlockEnd("}");
|
|
553
|
+
break;
|
|
554
|
+
case "RedirectWith":
|
|
555
|
+
writer
|
|
556
|
+
.writeBlockStart("if (!pathname.endsWith('/')) {")
|
|
557
|
+
.writeLines("url.pathname += '/';", "return Response.redirect(url);")
|
|
558
|
+
.writeBlockEnd("}");
|
|
559
|
+
break;
|
|
560
|
+
case "RewriteWithout":
|
|
561
|
+
writer
|
|
562
|
+
.writeBlockStart("if (pathname.length > 1 && pathname.endsWith('/')) {")
|
|
563
|
+
.writeLines("url.pathname = pathname.slice(0, -1);")
|
|
564
|
+
.writeBlockEnd("}");
|
|
565
|
+
break;
|
|
566
|
+
case "RewriteWith":
|
|
567
|
+
writer
|
|
568
|
+
.writeBlockStart("if (!pathname.endsWith('/')) {")
|
|
569
|
+
.writeLines("url.pathname += '/';")
|
|
570
|
+
.writeBlockEnd("}");
|
|
571
|
+
break;
|
|
572
|
+
}
|
|
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);
|
|
581
|
+
}
|
|
349
582
|
function writeRouterVerb(writer, trie, verb, level = 0, offset = 1) {
|
|
350
583
|
const { route, dynamic, catchAll } = trie;
|
|
351
584
|
let closeCount = 0;
|
|
@@ -383,7 +616,7 @@ function writeRouterVerb(writer, trie, verb, level = 0, offset = 1) {
|
|
|
383
616
|
writer.writeLines(`const ${segment} = decodeURIComponent(${value});`);
|
|
384
617
|
value = segment;
|
|
385
618
|
}
|
|
386
|
-
else if (terminal?.some(
|
|
619
|
+
else if (terminal?.some(needsDecode)) {
|
|
387
620
|
value = `decodeURIComponent(${value})`;
|
|
388
621
|
}
|
|
389
622
|
if (terminal) {
|
|
@@ -394,10 +627,10 @@ function writeRouterVerb(writer, trie, verb, level = 0, offset = 1) {
|
|
|
394
627
|
for (const { key, path, route } of terminal) {
|
|
395
628
|
const decodedKey = decodeURIComponent(key);
|
|
396
629
|
if (useSwitch) {
|
|
397
|
-
writer.write(`case
|
|
630
|
+
writer.write(`case ${JSON.stringify(decodedKey)}: `, true);
|
|
398
631
|
}
|
|
399
632
|
else {
|
|
400
|
-
writer.write(`if (${value} ===
|
|
633
|
+
writer.write(`if (${value} === ${JSON.stringify(decodedKey)}) `, true);
|
|
401
634
|
}
|
|
402
635
|
writer.write(`return ${renderMatch(verb, route, path)};\n`);
|
|
403
636
|
}
|
|
@@ -418,12 +651,13 @@ function writeRouterVerb(writer, trie, verb, level = 0, offset = 1) {
|
|
|
418
651
|
closeCount++;
|
|
419
652
|
}
|
|
420
653
|
let value = `pathname.slice(${offset}, ${index} - 1)`;
|
|
654
|
+
const decodeChildren = !!children?.some(needsDecode);
|
|
421
655
|
if (dynamic?.static || dynamic?.dynamic || dynamic?.catchAll) {
|
|
422
656
|
const segment = `s${next}`;
|
|
423
657
|
writer.writeLines(`const ${segment} = decodeURIComponent(${value});`);
|
|
424
658
|
value = segment;
|
|
425
659
|
}
|
|
426
|
-
else if (
|
|
660
|
+
else if (decodeChildren) {
|
|
427
661
|
value = `decodeURIComponent(${value})`;
|
|
428
662
|
}
|
|
429
663
|
if (children) {
|
|
@@ -434,12 +668,16 @@ function writeRouterVerb(writer, trie, verb, level = 0, offset = 1) {
|
|
|
434
668
|
for (const child of children) {
|
|
435
669
|
const decodedKey = decodeURIComponent(child.key);
|
|
436
670
|
if (useSwitch) {
|
|
437
|
-
writer.writeBlockStart(`case
|
|
671
|
+
writer.writeBlockStart(`case ${JSON.stringify(decodedKey)}: {`);
|
|
438
672
|
}
|
|
439
673
|
else {
|
|
440
|
-
writer.writeBlockStart(`if (${value} ===
|
|
674
|
+
writer.writeBlockStart(`if (${value} === ${JSON.stringify(decodedKey)}) {`);
|
|
441
675
|
}
|
|
442
|
-
|
|
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;
|
|
443
681
|
writeRouterVerb(writer, child, verb, next, nextOffset);
|
|
444
682
|
if (useSwitch) {
|
|
445
683
|
writer.writeBlockEnd("} break;");
|
|
@@ -471,7 +709,7 @@ function writeRouterVerb(writer, trie, verb, level = 0, offset = 1) {
|
|
|
471
709
|
}
|
|
472
710
|
function wrapPropertyName(name) {
|
|
473
711
|
name = decodeURIComponent(name);
|
|
474
|
-
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;
|
|
475
713
|
}
|
|
476
714
|
function renderParams(params, pathIndex) {
|
|
477
715
|
let result = "";
|
|
@@ -487,28 +725,17 @@ function renderParams(params, pathIndex) {
|
|
|
487
725
|
}
|
|
488
726
|
}
|
|
489
727
|
if (catchAll) {
|
|
490
|
-
|
|
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}))`;
|
|
491
731
|
}
|
|
492
732
|
return result ? result + " }" : "{}";
|
|
493
733
|
}
|
|
494
734
|
function renderMatch(verb, route, path, pathIndex) {
|
|
495
|
-
const
|
|
735
|
+
const name = `${verb}${route.index}`;
|
|
496
736
|
const params = path.params ? renderParams(path.params, pathIndex) : "{}";
|
|
497
|
-
const meta = route.meta ? `${
|
|
498
|
-
return `{ handler: ${
|
|
499
|
-
}
|
|
500
|
-
export function renderMiddleware(middleware, rootDir) {
|
|
501
|
-
const writer = createStringWriter();
|
|
502
|
-
const imports = writer.branch("imports");
|
|
503
|
-
imports.writeLines(`import { normalizeHandler } from "${virtualFilePrefix}/runtime/internal";`);
|
|
504
|
-
writer.writeLines("");
|
|
505
|
-
for (const { id, filePath } of middleware) {
|
|
506
|
-
const importName = `middleware${id}`;
|
|
507
|
-
imports.writeLines(`import ${importName} from "${normalizedRelativePath(rootDir, filePath)}";`);
|
|
508
|
-
writer.writeLines(`export const mware${id} = normalizeHandler(${importName});`);
|
|
509
|
-
}
|
|
510
|
-
imports.join();
|
|
511
|
-
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} }`;
|
|
512
739
|
}
|
|
513
740
|
function stripTsExtension(path) {
|
|
514
741
|
const index = path.lastIndexOf(".");
|
|
@@ -520,179 +747,14 @@ function stripTsExtension(path) {
|
|
|
520
747
|
}
|
|
521
748
|
return path;
|
|
522
749
|
}
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
if (adapter && adapter.typeInfo) {
|
|
533
|
-
const platformType = await adapter.typeInfo((data) => headWriter.write(data));
|
|
534
|
-
if (platformType) {
|
|
535
|
-
writer.writeLines(`interface Platform extends ${platformType} {}\n`);
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
headWriter.join();
|
|
539
|
-
writer
|
|
540
|
-
.writeBlockStart(`interface AppData extends Run.DefineApp<{`)
|
|
541
|
-
.writeBlockStart("routes: {");
|
|
542
|
-
const routesWriter = writer.branch("routes");
|
|
543
|
-
writer.writeBlockEnd("}").writeBlockEnd(`}> {}`).writeBlockEnd(`}`);
|
|
544
|
-
const routeTypes = new Map();
|
|
545
|
-
for (const route of routes.list) {
|
|
546
|
-
let routeType = "";
|
|
547
|
-
let routeDefinition = "";
|
|
548
|
-
if (route.page || route.handler) {
|
|
549
|
-
const verbs = [];
|
|
550
|
-
if (route.page || route.handler?.verbs?.includes("get")) {
|
|
551
|
-
verbs.push(`"get"`);
|
|
552
|
-
}
|
|
553
|
-
if (route.handler?.verbs?.includes("post")) {
|
|
554
|
-
verbs.push(`"post"`);
|
|
555
|
-
}
|
|
556
|
-
routeDefinition = `{ verb: ${verbs.join(" | ")};`;
|
|
557
|
-
if (route.meta) {
|
|
558
|
-
const metaPath = stripTsExtension(normalizedRelativePath(outDir, route.meta.filePath));
|
|
559
|
-
let metaType = `typeof import("${metaPath}")`;
|
|
560
|
-
if (/\.(ts|js|mjs)$/.test(route.meta.name)) {
|
|
561
|
-
metaType += `["default"]`;
|
|
562
|
-
}
|
|
563
|
-
routeDefinition += ` meta: ${metaType};`;
|
|
564
|
-
}
|
|
565
|
-
routeDefinition += " }";
|
|
566
|
-
}
|
|
567
|
-
const pathType = `"${route.path.path}"`;
|
|
568
|
-
routeType += routeType ? " | " + pathType : pathType;
|
|
569
|
-
routesWriter.writeLines(`${pathType}: ${routeDefinition};`);
|
|
570
|
-
for (const file of [route.handler, route.page]) {
|
|
571
|
-
if (file) {
|
|
572
|
-
const existing = routeTypes.get(file);
|
|
573
|
-
if (!existing) {
|
|
574
|
-
routeTypes.set(file, [routeType]);
|
|
575
|
-
}
|
|
576
|
-
else {
|
|
577
|
-
existing.push(routeType);
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
for (const files of [route.middleware, route.layouts]) {
|
|
582
|
-
if (files) {
|
|
583
|
-
for (const file of files) {
|
|
584
|
-
const existing = routeTypes.get(file);
|
|
585
|
-
if (!existing) {
|
|
586
|
-
routeTypes.set(file, [routeType]);
|
|
587
|
-
}
|
|
588
|
-
else {
|
|
589
|
-
existing.push(routeType);
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
for (const special of Object.values(routes.special)) {
|
|
596
|
-
routeTypes.set(special.page, []);
|
|
597
|
-
}
|
|
598
|
-
routesWriter.join();
|
|
599
|
-
const handlerWriter = writer.branch("handler");
|
|
600
|
-
const middlewareWriter = writer.branch("middleware");
|
|
601
|
-
const pageWriter = writer.branch("page");
|
|
602
|
-
const layoutWriter = writer.branch("layout");
|
|
603
|
-
for (const [file, types] of routeTypes) {
|
|
604
|
-
const modulePath = stripTsExtension(normalizedRelativePath(outDir, file.filePath));
|
|
605
|
-
const routeType = `Run.Routes[${types.join(" | ")}]`;
|
|
606
|
-
switch (file.type) {
|
|
607
|
-
case RoutableFileTypes.Handler:
|
|
608
|
-
writeModuleDeclaration(handlerWriter, modulePath, routeType);
|
|
609
|
-
break;
|
|
610
|
-
case RoutableFileTypes.Middleware:
|
|
611
|
-
writeModuleDeclaration(middlewareWriter, modulePath, routeType);
|
|
612
|
-
break;
|
|
613
|
-
case RoutableFileTypes.Page:
|
|
614
|
-
writeModuleDeclaration(pageWriter, modulePath, routeType);
|
|
615
|
-
break;
|
|
616
|
-
case RoutableFileTypes.Layout:
|
|
617
|
-
writeModuleDeclaration(layoutWriter, modulePath, routeType, `
|
|
618
|
-
export interface Input extends Run.LayoutInput<typeof import("${modulePath}")> {}`);
|
|
619
|
-
break;
|
|
620
|
-
case RoutableFileTypes.Error:
|
|
621
|
-
writeModuleDeclaration(writer, modulePath, "globalThis.MarkoRun.Route", `
|
|
622
|
-
export interface Input {
|
|
623
|
-
error: unknown;
|
|
624
|
-
}`);
|
|
625
|
-
break;
|
|
626
|
-
case RoutableFileTypes.NotFound:
|
|
627
|
-
writeModuleDeclaration(writer, modulePath, "Run.Route");
|
|
628
|
-
break;
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
handlerWriter.join();
|
|
632
|
-
middlewareWriter.join();
|
|
633
|
-
pageWriter.join();
|
|
634
|
-
layoutWriter.join();
|
|
635
|
-
return writer.end();
|
|
636
|
-
}
|
|
637
|
-
function writeModuleDeclaration(writer, name, routeType, moduleTypes) {
|
|
638
|
-
writer.writeLines("").write(`declare module "${name}" {`);
|
|
639
|
-
if (moduleTypes) {
|
|
640
|
-
writer.write(moduleTypes);
|
|
641
|
-
}
|
|
642
|
-
if (routeType) {
|
|
643
|
-
const isMarko = name.endsWith(".marko");
|
|
644
|
-
writer.write(`
|
|
645
|
-
namespace MarkoRun {
|
|
646
|
-
export { NotHandled, NotMatched, GetPaths, PostPaths, GetablePath, GetableHref, PostablePath, PostableHref, Platform };
|
|
647
|
-
export type Route = ${routeType};
|
|
648
|
-
export type Context = Run.MultiRouteContext<Route>${isMarko ? " & Marko.Global" : ""};
|
|
649
|
-
export type Handler = Run.HandlerLike<Route>;`);
|
|
650
|
-
for (const verb of httpVerbs) {
|
|
651
|
-
writer.write(`
|
|
652
|
-
export type ${verb.toUpperCase()} = Run.HandlerLike<Route, "${verb.toUpperCase()}">;`);
|
|
653
|
-
}
|
|
654
|
-
writer.write(`
|
|
655
|
-
/** @deprecated use \`((context, next) => { ... }) satisfies MarkoRun.Handler\` instead */
|
|
656
|
-
export const route: Run.HandlerTypeFn<Route>;
|
|
657
|
-
}`);
|
|
658
|
-
}
|
|
659
|
-
writer.writeLines(`
|
|
660
|
-
}`);
|
|
661
|
-
}
|
|
662
|
-
function createRouteTrie(routes) {
|
|
663
|
-
const root = {
|
|
664
|
-
key: "",
|
|
665
|
-
};
|
|
666
|
-
function insert(path, route) {
|
|
667
|
-
let node = root;
|
|
668
|
-
for (const segment of path.segments) {
|
|
669
|
-
if (segment === "$$") {
|
|
670
|
-
node.catchAll ??= { route, path };
|
|
671
|
-
return;
|
|
672
|
-
}
|
|
673
|
-
else if (segment === "$") {
|
|
674
|
-
node = node.dynamic ??= {
|
|
675
|
-
key: "",
|
|
676
|
-
};
|
|
677
|
-
}
|
|
678
|
-
else {
|
|
679
|
-
node.static ??= new Map();
|
|
680
|
-
let next = node.static.get(segment);
|
|
681
|
-
if (!next) {
|
|
682
|
-
next = {
|
|
683
|
-
key: segment,
|
|
684
|
-
};
|
|
685
|
-
node.static.set(segment, next);
|
|
686
|
-
}
|
|
687
|
-
node = next;
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
node.path ??= path;
|
|
691
|
-
node.route ??= route;
|
|
692
|
-
}
|
|
693
|
-
for (const route of routes) {
|
|
694
|
-
insert(route.path, route);
|
|
695
|
-
}
|
|
696
|
-
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;
|
|
697
759
|
}
|
|
698
760
|
//# sourceMappingURL=index.js.map
|