@marko/run 0.10.0 → 0.11.0-rc.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/.tsbuildinfo +1 -1
- package/dist/adapter/index.cjs +5 -4
- package/dist/adapter/index.js +7 -4
- package/dist/cli/index.mjs +524 -167
- package/dist/runtime/client.cjs +65 -0
- package/dist/runtime/client.d.ts +1 -0
- package/dist/runtime/client.js +59 -0
- package/dist/runtime/index.d.ts +9 -4
- package/dist/runtime/internal.cjs +279 -27
- package/dist/runtime/internal.d.ts +8 -4
- package/dist/runtime/internal.js +276 -27
- package/dist/runtime/legacy-types.d.ts +60 -0
- package/dist/runtime/namespace.d.ts +2 -1
- package/dist/runtime/router.d.ts +1 -1
- package/dist/runtime/types.d.ts +393 -100
- package/dist/runtime/url-builder.cjs +96 -0
- package/dist/runtime/url-builder.d.ts +6 -0
- package/dist/runtime/url-builder.js +61 -0
- package/dist/vite/codegen/index.d.ts +1 -1
- package/dist/vite/constants.d.ts +3 -3
- package/dist/vite/index.cjs +522 -167
- package/dist/vite/index.js +524 -167
- package/dist/vite/utils/href-replace.d.ts +44 -0
- package/dist/vite/utils/meta-data.d.ts +2 -2
- package/package.json +13 -23
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// scripts/importMetaURL.js
|
|
4
|
+
var import_url = require("url");
|
|
5
|
+
var __importMetaURL = (0, import_url.pathToFileURL)(__filename);
|
|
6
|
+
|
|
7
|
+
// src/runtime/url-builder.ts
|
|
8
|
+
var encode = encodeURIComponent;
|
|
9
|
+
var pathParts = /* @__PURE__ */ new Map();
|
|
10
|
+
function parsePathParts(path) {
|
|
11
|
+
let parts = pathParts.get(path);
|
|
12
|
+
if (!parts) {
|
|
13
|
+
let lastEnd = 0;
|
|
14
|
+
let paramStart;
|
|
15
|
+
pathParts.set(path, parts = [[]]);
|
|
16
|
+
while (lastEnd >= 0 && (paramStart = path.indexOf("/$", lastEnd) + 1)) {
|
|
17
|
+
parts.push(path.slice(lastEnd, paramStart++));
|
|
18
|
+
if (path.charAt(paramStart) === "$") {
|
|
19
|
+
paramStart++;
|
|
20
|
+
lastEnd = -1;
|
|
21
|
+
} else {
|
|
22
|
+
lastEnd = path.indexOf("/", paramStart);
|
|
23
|
+
}
|
|
24
|
+
parts[0].push(path.slice(paramStart, lastEnd < 0 ? void 0 : lastEnd));
|
|
25
|
+
}
|
|
26
|
+
parts.push(lastEnd >= 0 ? path.slice(lastEnd) : "");
|
|
27
|
+
}
|
|
28
|
+
return parts;
|
|
29
|
+
}
|
|
30
|
+
function joinHref(path, options) {
|
|
31
|
+
let result = path;
|
|
32
|
+
if (options.search) {
|
|
33
|
+
const query = "" + new URLSearchParams(options.search);
|
|
34
|
+
if (query) result += "?" + query;
|
|
35
|
+
}
|
|
36
|
+
if (options.hash) result += "#" + encode(options.hash);
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
function href(path, ...[options]) {
|
|
40
|
+
return options ? "params" in options ? ((parts) => href_keys(parts, options, ...parts[0]))(
|
|
41
|
+
parsePathParts(path)
|
|
42
|
+
) : joinHref(path, options) : path;
|
|
43
|
+
}
|
|
44
|
+
function href_path(strings, ...params) {
|
|
45
|
+
let i = 0;
|
|
46
|
+
let j = 0;
|
|
47
|
+
let result = strings[i++];
|
|
48
|
+
if (!result || Array.isArray(result)) result = strings[i++];
|
|
49
|
+
while (i < strings.length) {
|
|
50
|
+
const param = params[j++];
|
|
51
|
+
result += (Array.isArray(param) ? param.map(encode).join("/") : encode(param)) + strings[i++];
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
function href_values(strings, options, ...params) {
|
|
56
|
+
return joinHref(href_path(strings, ...params), options);
|
|
57
|
+
}
|
|
58
|
+
function href_keys(strings, options, ...keys) {
|
|
59
|
+
return href_values(strings, options, ...keys.map((k) => options.params[k]));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// src/runtime/client.ts
|
|
63
|
+
window.Run || (window.Run = {
|
|
64
|
+
href
|
|
65
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// src/runtime/url-builder.ts
|
|
2
|
+
var encode = encodeURIComponent;
|
|
3
|
+
var pathParts = /* @__PURE__ */ new Map();
|
|
4
|
+
function parsePathParts(path) {
|
|
5
|
+
let parts = pathParts.get(path);
|
|
6
|
+
if (!parts) {
|
|
7
|
+
let lastEnd = 0;
|
|
8
|
+
let paramStart;
|
|
9
|
+
pathParts.set(path, parts = [[]]);
|
|
10
|
+
while (lastEnd >= 0 && (paramStart = path.indexOf("/$", lastEnd) + 1)) {
|
|
11
|
+
parts.push(path.slice(lastEnd, paramStart++));
|
|
12
|
+
if (path.charAt(paramStart) === "$") {
|
|
13
|
+
paramStart++;
|
|
14
|
+
lastEnd = -1;
|
|
15
|
+
} else {
|
|
16
|
+
lastEnd = path.indexOf("/", paramStart);
|
|
17
|
+
}
|
|
18
|
+
parts[0].push(path.slice(paramStart, lastEnd < 0 ? void 0 : lastEnd));
|
|
19
|
+
}
|
|
20
|
+
parts.push(lastEnd >= 0 ? path.slice(lastEnd) : "");
|
|
21
|
+
}
|
|
22
|
+
return parts;
|
|
23
|
+
}
|
|
24
|
+
function joinHref(path, options) {
|
|
25
|
+
let result = path;
|
|
26
|
+
if (options.search) {
|
|
27
|
+
const query = "" + new URLSearchParams(options.search);
|
|
28
|
+
if (query) result += "?" + query;
|
|
29
|
+
}
|
|
30
|
+
if (options.hash) result += "#" + encode(options.hash);
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
function href(path, ...[options]) {
|
|
34
|
+
return options ? "params" in options ? ((parts) => href_keys(parts, options, ...parts[0]))(
|
|
35
|
+
parsePathParts(path)
|
|
36
|
+
) : joinHref(path, options) : path;
|
|
37
|
+
}
|
|
38
|
+
function href_path(strings, ...params) {
|
|
39
|
+
let i = 0;
|
|
40
|
+
let j = 0;
|
|
41
|
+
let result = strings[i++];
|
|
42
|
+
if (!result || Array.isArray(result)) result = strings[i++];
|
|
43
|
+
while (i < strings.length) {
|
|
44
|
+
const param = params[j++];
|
|
45
|
+
result += (Array.isArray(param) ? param.map(encode).join("/") : encode(param)) + strings[i++];
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
function href_values(strings, options, ...params) {
|
|
50
|
+
return joinHref(href_path(strings, ...params), options);
|
|
51
|
+
}
|
|
52
|
+
function href_keys(strings, options, ...keys) {
|
|
53
|
+
return href_values(strings, options, ...keys.map((k) => options.params[k]));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/runtime/client.ts
|
|
57
|
+
window.Run || (window.Run = {
|
|
58
|
+
href
|
|
59
|
+
});
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import { InlineConfig } from "vite";
|
|
2
|
+
import type { AnyRoute, GetableHref, GetablePath, GetPaths, HandlerLike, MultiRouteContext, PostableHref, PostablePath, PostPaths } from "./legacy-types";
|
|
2
3
|
import { NotHandled, NotMatched } from "./namespace";
|
|
3
|
-
import type {
|
|
4
|
+
import type { GetContext, GlobalNamespace, Platform, RuntimeModule } from "./types";
|
|
4
5
|
declare global {
|
|
5
6
|
var __marko_run__: RuntimeModule;
|
|
6
7
|
var __marko_run_vite_config__: InlineConfig | undefined;
|
|
8
|
+
var Run: GlobalNamespace;
|
|
9
|
+
namespace Run {
|
|
10
|
+
type Context = GetContext;
|
|
11
|
+
}
|
|
12
|
+
/** @deprecated use \`Run\` namespace instead */
|
|
7
13
|
namespace MarkoRun {
|
|
8
14
|
export { GetableHref, GetablePath, GetPaths, NotHandled, NotMatched, Platform, PostableHref, PostablePath, PostPaths, };
|
|
9
15
|
export type Route = AnyRoute;
|
|
@@ -16,8 +22,7 @@ declare global {
|
|
|
16
22
|
export type DELETE = HandlerLike<AnyRoute, "DELETE">;
|
|
17
23
|
export type PATCH = HandlerLike<AnyRoute, "PATCH">;
|
|
18
24
|
export type OPTIONS = HandlerLike<AnyRoute, "OPTIONS">;
|
|
19
|
-
/** @deprecated use `((context, next) => { ... }) satisfies MarkoRun.Handler` instead */
|
|
20
|
-
export const route: HandlerTypeFn;
|
|
21
25
|
}
|
|
22
26
|
}
|
|
23
|
-
export type {
|
|
27
|
+
export type { HandlerLike, HandlerTypeFn, InputObject, MultiRouteContext, ParamsObject, Route, RouteHandler, Routes, } from "./legacy-types";
|
|
28
|
+
export type { App, Context, ContextForFile, DefineRoutes, Fetch, GetContext, Handler, Invoke, LayoutInput, Match, Meta, Middleware, Namespace, NextFunction, NormalizedHandler, PartialTemplate, Platform, RouteForFileDef, RouteMatch, RuntimeModule, Template, HttpVerb as Verb, } from "./types";
|
|
@@ -25,12 +25,15 @@ __export(internal_exports, {
|
|
|
25
25
|
call: () => call,
|
|
26
26
|
compose: () => compose,
|
|
27
27
|
createContext: () => createContext,
|
|
28
|
+
mergeOptions: () => mergeOptions,
|
|
28
29
|
noContent: () => noContent,
|
|
29
30
|
normalizeHandler: () => normalizeHandler,
|
|
30
31
|
normalizeMeta: () => getMetaDataLookup,
|
|
32
|
+
normalizeValidator: () => normalizeValidator,
|
|
31
33
|
notHandled: () => notHandled,
|
|
32
34
|
notMatched: () => notMatched,
|
|
33
35
|
passthrough: () => passthrough,
|
|
36
|
+
render: () => render,
|
|
34
37
|
stripResponseBody: () => stripResponseBody,
|
|
35
38
|
stripResponseBodySync: () => stripResponseBodySync
|
|
36
39
|
});
|
|
@@ -40,6 +43,10 @@ module.exports = __toCommonJS(internal_exports);
|
|
|
40
43
|
var import_url = require("url");
|
|
41
44
|
var __importMetaURL = (0, import_url.pathToFileURL)(__filename);
|
|
42
45
|
|
|
46
|
+
// src/runtime/internal.ts
|
|
47
|
+
var import_node_url = require("node:url");
|
|
48
|
+
var import_form_data_parser = require("@remix-run/form-data-parser");
|
|
49
|
+
|
|
43
50
|
// src/vite/constants.ts
|
|
44
51
|
var httpVerbs = [
|
|
45
52
|
"get",
|
|
@@ -51,6 +58,61 @@ var httpVerbs = [
|
|
|
51
58
|
"options"
|
|
52
59
|
];
|
|
53
60
|
|
|
61
|
+
// src/runtime/url-builder.ts
|
|
62
|
+
var encode = encodeURIComponent;
|
|
63
|
+
var pathParts = /* @__PURE__ */ new Map();
|
|
64
|
+
function parsePathParts(path) {
|
|
65
|
+
let parts = pathParts.get(path);
|
|
66
|
+
if (!parts) {
|
|
67
|
+
let lastEnd = 0;
|
|
68
|
+
let paramStart;
|
|
69
|
+
pathParts.set(path, parts = [[]]);
|
|
70
|
+
while (lastEnd >= 0 && (paramStart = path.indexOf("/$", lastEnd) + 1)) {
|
|
71
|
+
parts.push(path.slice(lastEnd, paramStart++));
|
|
72
|
+
if (path.charAt(paramStart) === "$") {
|
|
73
|
+
paramStart++;
|
|
74
|
+
lastEnd = -1;
|
|
75
|
+
} else {
|
|
76
|
+
lastEnd = path.indexOf("/", paramStart);
|
|
77
|
+
}
|
|
78
|
+
parts[0].push(path.slice(paramStart, lastEnd < 0 ? void 0 : lastEnd));
|
|
79
|
+
}
|
|
80
|
+
parts.push(lastEnd >= 0 ? path.slice(lastEnd) : "");
|
|
81
|
+
}
|
|
82
|
+
return parts;
|
|
83
|
+
}
|
|
84
|
+
function joinHref(path, options) {
|
|
85
|
+
let result = path;
|
|
86
|
+
if (options.search) {
|
|
87
|
+
const query = "" + new URLSearchParams(options.search);
|
|
88
|
+
if (query) result += "?" + query;
|
|
89
|
+
}
|
|
90
|
+
if (options.hash) result += "#" + encode(options.hash);
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
function href(path, ...[options]) {
|
|
94
|
+
return options ? "params" in options ? ((parts) => href_keys(parts, options, ...parts[0]))(
|
|
95
|
+
parsePathParts(path)
|
|
96
|
+
) : joinHref(path, options) : path;
|
|
97
|
+
}
|
|
98
|
+
function href_path(strings, ...params) {
|
|
99
|
+
let i = 0;
|
|
100
|
+
let j = 0;
|
|
101
|
+
let result = strings[i++];
|
|
102
|
+
if (!result || Array.isArray(result)) result = strings[i++];
|
|
103
|
+
while (i < strings.length) {
|
|
104
|
+
const param = params[j++];
|
|
105
|
+
result += (Array.isArray(param) ? param.map(encode).join("/") : encode(param)) + strings[i++];
|
|
106
|
+
}
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
function href_values(strings, options, ...params) {
|
|
110
|
+
return joinHref(href_path(strings, ...params), options);
|
|
111
|
+
}
|
|
112
|
+
function href_keys(strings, options, ...keys) {
|
|
113
|
+
return href_values(strings, options, ...keys.map((k) => options.params[k]));
|
|
114
|
+
}
|
|
115
|
+
|
|
54
116
|
// src/vite/utils/meta-data.ts
|
|
55
117
|
var verbKeys = new Set(httpVerbs.map((v) => v.toUpperCase()));
|
|
56
118
|
function isObject(obj) {
|
|
@@ -98,11 +160,20 @@ var pageResponseInit = {
|
|
|
98
160
|
};
|
|
99
161
|
globalThis.MarkoRun ?? (globalThis.MarkoRun = {
|
|
100
162
|
NotHandled,
|
|
101
|
-
NotMatched
|
|
102
|
-
route(handler) {
|
|
103
|
-
return handler;
|
|
104
|
-
}
|
|
163
|
+
NotMatched
|
|
105
164
|
});
|
|
165
|
+
if (!globalThis.Run) {
|
|
166
|
+
const namespace = {
|
|
167
|
+
href
|
|
168
|
+
};
|
|
169
|
+
for (const v of [...httpVerbs, "all"]) {
|
|
170
|
+
const verb = v.toUpperCase();
|
|
171
|
+
const def = createDefineHandler();
|
|
172
|
+
def.href = href;
|
|
173
|
+
namespace[verb] = def;
|
|
174
|
+
}
|
|
175
|
+
globalThis.Run = namespace;
|
|
176
|
+
}
|
|
106
177
|
var toReadable = (rendered) => {
|
|
107
178
|
toReadable = rendered.toReadable ? (rendered2) => rendered2.toReadable() : (rendered2) => {
|
|
108
179
|
let cancelled = false;
|
|
@@ -130,27 +201,113 @@ var toReadable = (rendered) => {
|
|
|
130
201
|
};
|
|
131
202
|
return toReadable(rendered);
|
|
132
203
|
};
|
|
133
|
-
function
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
meta = route.meta;
|
|
139
|
-
params = route.params;
|
|
140
|
-
path = route.path;
|
|
141
|
-
} else {
|
|
142
|
-
meta = {};
|
|
143
|
-
params = {};
|
|
144
|
-
path = "";
|
|
204
|
+
function searchParamsToObject(params) {
|
|
205
|
+
const obj = {};
|
|
206
|
+
for (const [key, value] of params) {
|
|
207
|
+
const prev = obj[key];
|
|
208
|
+
obj[key] = prev ? Array.isArray(prev) ? [...prev, value] : [prev, value] : value;
|
|
145
209
|
}
|
|
146
|
-
return
|
|
147
|
-
|
|
210
|
+
return obj;
|
|
211
|
+
}
|
|
212
|
+
async function readBodyWithLimit(request, maxBytes) {
|
|
213
|
+
if (maxBytes < 0) {
|
|
214
|
+
return await request.text();
|
|
215
|
+
}
|
|
216
|
+
const contentLength = request.headers.get("content-length");
|
|
217
|
+
if (contentLength !== null && Number(contentLength) > maxBytes) {
|
|
218
|
+
throw new Error("Request body too large");
|
|
219
|
+
}
|
|
220
|
+
if (!request.body) {
|
|
221
|
+
throw new Error("Missing request body");
|
|
222
|
+
}
|
|
223
|
+
const reader = request.body.getReader();
|
|
224
|
+
const bytes = new Uint8Array(maxBytes);
|
|
225
|
+
let receivedBytes = 0;
|
|
226
|
+
try {
|
|
227
|
+
while (true) {
|
|
228
|
+
const { done, value } = await reader.read();
|
|
229
|
+
if (done) break;
|
|
230
|
+
if (receivedBytes + value.byteLength > maxBytes) {
|
|
231
|
+
await reader.cancel();
|
|
232
|
+
throw new Error("Request body too large");
|
|
233
|
+
}
|
|
234
|
+
bytes.set(value, receivedBytes);
|
|
235
|
+
receivedBytes += value.byteLength;
|
|
236
|
+
}
|
|
237
|
+
} finally {
|
|
238
|
+
reader.releaseLock();
|
|
239
|
+
}
|
|
240
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(
|
|
241
|
+
bytes.subarray(0, receivedBytes)
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
function createContext(route, request, platform, url = new URL(request.url)) {
|
|
245
|
+
const context = {
|
|
246
|
+
route: (route == null ? void 0 : route.path) || "",
|
|
148
247
|
method: request.method,
|
|
248
|
+
meta: (route == null ? void 0 : route.meta) || {},
|
|
249
|
+
get params() {
|
|
250
|
+
const value = route ? route.options.params ? route.options.params(route.params) : route.params : {};
|
|
251
|
+
Object.defineProperty(context, "params", {
|
|
252
|
+
configurable: true,
|
|
253
|
+
enumerable: true,
|
|
254
|
+
value
|
|
255
|
+
});
|
|
256
|
+
return value;
|
|
257
|
+
},
|
|
258
|
+
get search() {
|
|
259
|
+
const search = searchParamsToObject(url.searchParams);
|
|
260
|
+
const value = (route == null ? void 0 : route.options.search) ? route.options.search(search) : search;
|
|
261
|
+
Object.defineProperty(context, "search", {
|
|
262
|
+
configurable: true,
|
|
263
|
+
enumerable: true,
|
|
264
|
+
value
|
|
265
|
+
});
|
|
266
|
+
return value;
|
|
267
|
+
},
|
|
268
|
+
body: route && request.body ? async () => {
|
|
269
|
+
const contentType = request.headers.get("Content-Type");
|
|
270
|
+
let value;
|
|
271
|
+
if (contentType == null ? void 0 : contentType.includes("application/json")) {
|
|
272
|
+
const { maxBytes, validator } = route.options.json;
|
|
273
|
+
const json = maxBytes < 0 ? await request.json() : JSON.parse(await readBodyWithLimit(request, maxBytes));
|
|
274
|
+
value = validator ? validator(json) : json;
|
|
275
|
+
} else {
|
|
276
|
+
const {
|
|
277
|
+
maxBytes,
|
|
278
|
+
maxParts,
|
|
279
|
+
maxFiles,
|
|
280
|
+
maxFileBytes,
|
|
281
|
+
onFile,
|
|
282
|
+
validator
|
|
283
|
+
} = route.options.form;
|
|
284
|
+
const data = searchParamsToObject(
|
|
285
|
+
(contentType == null ? void 0 : contentType.includes("multipart/form-data")) ? await (0, import_form_data_parser.parseFormData)(
|
|
286
|
+
request,
|
|
287
|
+
{
|
|
288
|
+
maxParts,
|
|
289
|
+
maxFiles,
|
|
290
|
+
maxFileSize: maxFileBytes,
|
|
291
|
+
maxTotalSize: maxBytes
|
|
292
|
+
},
|
|
293
|
+
onFile ? (file) => onFile(context, file) : void 0
|
|
294
|
+
) : new import_node_url.URLSearchParams(
|
|
295
|
+
await readBodyWithLimit(request, maxBytes)
|
|
296
|
+
)
|
|
297
|
+
);
|
|
298
|
+
value = validator ? validator(data) : validator;
|
|
299
|
+
}
|
|
300
|
+
Object.defineProperty(context, "body", {
|
|
301
|
+
configurable: true,
|
|
302
|
+
enumerable: true,
|
|
303
|
+
value
|
|
304
|
+
});
|
|
305
|
+
return value;
|
|
306
|
+
} : void 0,
|
|
307
|
+
data: {},
|
|
149
308
|
url,
|
|
309
|
+
request,
|
|
150
310
|
platform,
|
|
151
|
-
meta,
|
|
152
|
-
params,
|
|
153
|
-
route: path,
|
|
154
311
|
serializedGlobals,
|
|
155
312
|
parent: parentContextLookup.get(request),
|
|
156
313
|
async fetch(resource, init) {
|
|
@@ -192,16 +349,26 @@ function createContext(route, request, platform, url = new URL(request.url)) {
|
|
|
192
349
|
);
|
|
193
350
|
}
|
|
194
351
|
};
|
|
352
|
+
return context;
|
|
353
|
+
}
|
|
354
|
+
function render(context, template, input, data) {
|
|
355
|
+
if (data) {
|
|
356
|
+
Object.assign(context.data, data);
|
|
357
|
+
}
|
|
358
|
+
return context.render(template, input);
|
|
195
359
|
}
|
|
196
|
-
async function call(handler, next, context) {
|
|
360
|
+
async function call(handler, next, context, data) {
|
|
197
361
|
let response;
|
|
362
|
+
if (data) {
|
|
363
|
+
Object.assign(context.data, data);
|
|
364
|
+
}
|
|
198
365
|
if (!process.env.NODE_ENV || process.env.NODE_ENV === "development") {
|
|
199
366
|
let nextCallCount = 0;
|
|
200
367
|
let didThrow = false;
|
|
201
368
|
try {
|
|
202
|
-
response = await handler(context, () => {
|
|
369
|
+
response = await handler(context, (d) => {
|
|
203
370
|
nextCallCount++;
|
|
204
|
-
return next();
|
|
371
|
+
return next(d);
|
|
205
372
|
});
|
|
206
373
|
} catch (error) {
|
|
207
374
|
didThrow = true;
|
|
@@ -240,14 +407,14 @@ async function call(handler, next, context) {
|
|
|
240
407
|
function compose(handlers) {
|
|
241
408
|
const len = handlers.length;
|
|
242
409
|
if (!len) {
|
|
243
|
-
return
|
|
410
|
+
return passthroughHandler;
|
|
244
411
|
} else if (len === 1) {
|
|
245
412
|
return handlers[0];
|
|
246
413
|
}
|
|
247
414
|
return (context, next) => {
|
|
248
415
|
let i = 0;
|
|
249
|
-
return (function nextHandler() {
|
|
250
|
-
return i < len ? call(handlers[i++], nextHandler, context) : next();
|
|
416
|
+
return (function nextHandler(data) {
|
|
417
|
+
return i < len ? call(handlers[i++], nextHandler, context, data) : next(data);
|
|
251
418
|
})();
|
|
252
419
|
};
|
|
253
420
|
}
|
|
@@ -268,6 +435,87 @@ function normalizeHandler(obj) {
|
|
|
268
435
|
}
|
|
269
436
|
return passthrough;
|
|
270
437
|
}
|
|
438
|
+
function createDefineHandler() {
|
|
439
|
+
return (optionsOrHandlers, handlers) => {
|
|
440
|
+
let handler;
|
|
441
|
+
if (typeof optionsOrHandlers === "function") {
|
|
442
|
+
handler = optionsOrHandlers;
|
|
443
|
+
handler.options = {};
|
|
444
|
+
} else if (Array.isArray(optionsOrHandlers)) {
|
|
445
|
+
handler = compose(optionsOrHandlers);
|
|
446
|
+
handler.options = {};
|
|
447
|
+
} else if (typeof handlers === "function") {
|
|
448
|
+
handler = handlers;
|
|
449
|
+
handler.options = optionsOrHandlers;
|
|
450
|
+
} else if (Array.isArray(handlers)) {
|
|
451
|
+
handler = compose(handlers);
|
|
452
|
+
handler.options = optionsOrHandlers;
|
|
453
|
+
} else {
|
|
454
|
+
handler = passthroughHandler;
|
|
455
|
+
handler.options = optionsOrHandlers;
|
|
456
|
+
}
|
|
457
|
+
return handler;
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
function normalizeValidator(validator) {
|
|
461
|
+
return validator && typeof validator !== "function" ? (input) => {
|
|
462
|
+
const result = validator["~standard"].validate(input);
|
|
463
|
+
if (result instanceof Promise) {
|
|
464
|
+
throw new TypeError("Schema validation must be synchronous");
|
|
465
|
+
}
|
|
466
|
+
return result.issues ? [input, result.issues] : [result.value, void 0];
|
|
467
|
+
} : validator;
|
|
468
|
+
}
|
|
469
|
+
var defaultMaxBytes = 1024 * 1024;
|
|
470
|
+
var defaultMaxParts = 1e3;
|
|
471
|
+
var defaultMaxFiles = 20;
|
|
472
|
+
function mergeOptions(...fns) {
|
|
473
|
+
const merged = {};
|
|
474
|
+
for (const fn of fns) {
|
|
475
|
+
if (typeof fn === "function" && "options" in fn) {
|
|
476
|
+
const { options } = fn;
|
|
477
|
+
for (const k in options) {
|
|
478
|
+
const key = k;
|
|
479
|
+
const option = options[key];
|
|
480
|
+
if (typeof option === "object" && typeof merged[key] === "object") {
|
|
481
|
+
Object.assign(merged[key], option);
|
|
482
|
+
} else if (option) {
|
|
483
|
+
merged[key] = option;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
const result = {
|
|
489
|
+
params: normalizeValidator(merged.params),
|
|
490
|
+
search: normalizeValidator(merged.search)
|
|
491
|
+
};
|
|
492
|
+
if (merged.json) {
|
|
493
|
+
const { maxBytes = defaultMaxBytes, validator } = typeof merged.json === "function" || "~standard" in merged.json ? { validator: merged.json } : merged.json;
|
|
494
|
+
result.json = {
|
|
495
|
+
maxBytes,
|
|
496
|
+
validator: normalizeValidator(validator)
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
if (merged.form) {
|
|
500
|
+
const {
|
|
501
|
+
maxBytes,
|
|
502
|
+
maxFiles = defaultMaxFiles,
|
|
503
|
+
maxFileBytes = defaultMaxBytes,
|
|
504
|
+
maxParts = defaultMaxParts,
|
|
505
|
+
onFile,
|
|
506
|
+
validator
|
|
507
|
+
} = typeof merged.form === "function" || "~standard" in merged.form ? { validator: merged.form } : merged.form;
|
|
508
|
+
result.form = {
|
|
509
|
+
maxBytes: maxBytes ?? maxFiles * maxFileBytes,
|
|
510
|
+
maxFileBytes,
|
|
511
|
+
maxFiles,
|
|
512
|
+
maxParts,
|
|
513
|
+
onFile,
|
|
514
|
+
validator: normalizeValidator(validator)
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
return result;
|
|
518
|
+
}
|
|
271
519
|
function stripResponseBodySync(response) {
|
|
272
520
|
return response.body ? new Response(null, response) : response;
|
|
273
521
|
}
|
|
@@ -276,6 +524,7 @@ function stripResponseBody(response) {
|
|
|
276
524
|
}
|
|
277
525
|
function passthrough() {
|
|
278
526
|
}
|
|
527
|
+
var passthroughHandler = (_ctx, next) => next();
|
|
279
528
|
function noContent() {
|
|
280
529
|
return new Response(null, {
|
|
281
530
|
status: 204
|
|
@@ -294,12 +543,15 @@ function notMatched() {
|
|
|
294
543
|
call,
|
|
295
544
|
compose,
|
|
296
545
|
createContext,
|
|
546
|
+
mergeOptions,
|
|
297
547
|
noContent,
|
|
298
548
|
normalizeHandler,
|
|
299
549
|
normalizeMeta,
|
|
550
|
+
normalizeValidator,
|
|
300
551
|
notHandled,
|
|
301
552
|
notMatched,
|
|
302
553
|
passthrough,
|
|
554
|
+
render,
|
|
303
555
|
stripResponseBody,
|
|
304
556
|
stripResponseBodySync
|
|
305
557
|
});
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Awaitable, RouteHandler } from "./legacy-types";
|
|
2
|
+
import type { Context, HandlerFunction, HandlerOptions, NextFunction, NormalizedHandler, NormalizedHandlerOptions, Platform, RouteData, RouteMatch, Validator } from "./types";
|
|
2
3
|
export { getMetaDataLookup as normalizeMeta } from "../vite/utils/meta-data";
|
|
3
4
|
export declare const NotHandled: typeof MarkoRun.NotHandled;
|
|
4
5
|
export declare const NotMatched: typeof MarkoRun.NotMatched;
|
|
5
|
-
export declare function createContext
|
|
6
|
-
export declare function
|
|
7
|
-
export declare function
|
|
6
|
+
export declare function createContext(route: RouteMatch | null, request: Request, platform: Platform, url?: URL): Context;
|
|
7
|
+
export declare function render<T>(context: Context, template: Marko.Template<T>, input: T, data?: RouteData): Response;
|
|
8
|
+
export declare function call(handler: HandlerFunction, next: NextFunction, context: Context, data?: RouteData): Promise<Response>;
|
|
9
|
+
export declare function compose(handlers: HandlerFunction[]): HandlerFunction;
|
|
8
10
|
export declare function normalizeHandler(obj: RouteHandler | RouteHandler[] | Promise<RouteHandler | RouteHandler[]>): RouteHandler;
|
|
11
|
+
export declare function normalizeValidator<T>(validator: Validator<T> | undefined): import("./types").ValidatorFn<T> | undefined;
|
|
12
|
+
export declare function mergeOptions(...fns: (NormalizedHandler<Context, "ALL", any, HandlerOptions> | HandlerFunction)[]): NormalizedHandlerOptions;
|
|
9
13
|
export declare function stripResponseBodySync(response: Response): Response;
|
|
10
14
|
export declare function stripResponseBody(response: Awaitable<Response>): Awaitable<Response>;
|
|
11
15
|
export declare function passthrough(): void;
|