@marko/run 0.0.1-beta2 → 0.0.1-beta3
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/default-entry.mjs +9 -3
- package/dist/adapter/dev-server.d.ts +1 -1
- package/dist/adapter/index.cjs +23 -8
- package/dist/adapter/index.js +22 -7
- package/dist/adapter/middleware.cjs +200 -0
- package/dist/adapter/middleware.d.ts +59 -0
- package/dist/adapter/middleware.js +169 -0
- package/dist/cli/default.config.mjs +1 -1
- package/dist/cli/index.mjs +45 -24
- package/dist/runtime/index.cjs +0 -16
- package/dist/runtime/index.d.ts +10 -2
- package/dist/runtime/index.js +0 -7
- package/dist/runtime/internal.cjs +148 -0
- package/dist/runtime/internal.d.ts +10 -0
- package/dist/runtime/internal.js +115 -0
- package/dist/runtime/router.cjs +6 -6
- package/dist/runtime/router.d.ts +4 -4
- package/dist/runtime/router.js +4 -4
- package/dist/runtime/types.d.ts +31 -16
- package/dist/runtime/utils.d.ts +3 -0
- package/dist/vite/codegen/index.d.ts +4 -3
- package/dist/vite/codegen/writer.d.ts +1 -1
- package/dist/vite/constants.d.ts +3 -2
- package/dist/vite/index.cjs +542 -275
- package/dist/vite/index.d.ts +4 -3
- package/dist/vite/index.js +539 -275
- package/dist/vite/types.d.ts +9 -7
- package/dist/vite/utils/config.d.ts +2 -2
- package/dist/vite/utils/route.d.ts +1 -1
- package/dist/vite/utils/server.d.ts +3 -1
- package/package.json +18 -6
package/dist/cli/index.mjs
CHANGED
|
@@ -2,17 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
// src/cli/index.ts
|
|
4
4
|
import path from "path";
|
|
5
|
-
import
|
|
5
|
+
import fs2 from "fs";
|
|
6
6
|
import { fileURLToPath } from "url";
|
|
7
7
|
import { build as viteBuild, resolveConfig } from "vite";
|
|
8
8
|
import sade from "sade";
|
|
9
9
|
|
|
10
10
|
// src/vite/utils/config.ts
|
|
11
11
|
var KEY = "__MARKO_SERVE_OPTIONS__";
|
|
12
|
-
function
|
|
12
|
+
function getMarkoRunOptions(viteConfig) {
|
|
13
13
|
return viteConfig[KEY];
|
|
14
14
|
}
|
|
15
|
-
function
|
|
15
|
+
function setMarkoRunOptions(viteConfig, options) {
|
|
16
16
|
viteConfig[KEY] = options;
|
|
17
17
|
return viteConfig;
|
|
18
18
|
}
|
|
@@ -23,16 +23,27 @@ import { MemoryStore } from "@marko/vite";
|
|
|
23
23
|
// src/vite/utils/server.ts
|
|
24
24
|
import net from "net";
|
|
25
25
|
import cp from "child_process";
|
|
26
|
-
|
|
26
|
+
import { parse, config } from "dotenv";
|
|
27
|
+
import fs from "fs";
|
|
28
|
+
async function parseEnv(envFile) {
|
|
29
|
+
if (fs.existsSync(envFile)) {
|
|
30
|
+
const content = await fs.promises.readFile(envFile, "utf8");
|
|
31
|
+
return parse(content);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async function spawnServer(cmd, port = 0, env, cwd2 = process.cwd(), wait = 3e4) {
|
|
27
35
|
if (port <= 0) {
|
|
28
36
|
port = await getAvailablePort();
|
|
29
37
|
}
|
|
38
|
+
if (typeof env === "string") {
|
|
39
|
+
env = await parseEnv(env);
|
|
40
|
+
}
|
|
30
41
|
const proc = cp.spawn(cmd, {
|
|
31
42
|
cwd: cwd2,
|
|
32
43
|
shell: true,
|
|
33
44
|
stdio: "inherit",
|
|
34
45
|
windowsHide: true,
|
|
35
|
-
env: { NODE_ENV: "development", ...process.env, PORT: `${port}` }
|
|
46
|
+
env: { ...env, NODE_ENV: "development", ...process.env, PORT: `${port}` }
|
|
36
47
|
});
|
|
37
48
|
const close = () => {
|
|
38
49
|
proc.unref();
|
|
@@ -80,23 +91,23 @@ function sleep(ms) {
|
|
|
80
91
|
var __dirname = fileURLToPath(new URL(".", import.meta.url));
|
|
81
92
|
var cwd = process.cwd();
|
|
82
93
|
var defaultPort = +process.env.PORT || 3e3;
|
|
83
|
-
var prog = sade("marko-run").version("0.0.1").option("-c, --config", "Provide path to a Vite config");
|
|
94
|
+
var prog = sade("marko-run").version("0.0.1").option("-c, --config", "Provide path to a Vite config").option("-e, --env", "Provide path to a dotenv file");
|
|
84
95
|
prog.command("preview [entry]", "", { default: true }).describe("Start production-like server against built assets").option("-o, --output", "Directory to serve files").option("-p, --port", "Port to use for dev server").option("-f, --file", "Output file to start").action(async (entry, opts) => {
|
|
85
|
-
const
|
|
86
|
-
await build(entry,
|
|
87
|
-
await preview(opts.entry,
|
|
96
|
+
const config2 = await getViteConfig(cwd, opts.config);
|
|
97
|
+
await build(entry, config2, opts.output, false, opts.env);
|
|
98
|
+
await preview(opts.entry, config2, opts.port, opts.output, opts.env);
|
|
88
99
|
});
|
|
89
100
|
prog.command("dev [entry]").describe("Start dev server").option("-p, --port", "Port to use for dev server").example("dev --config vite.config.js").action(async (entry, opts) => {
|
|
90
101
|
const cmd = opts._.length ? `${entry} ${opts._.join(" ")}` : entry ? `node ${entry}` : void 0;
|
|
91
|
-
const
|
|
92
|
-
await dev(cmd,
|
|
102
|
+
const config2 = await getViteConfig(cwd, opts.config);
|
|
103
|
+
await dev(cmd, config2, opts.port, opts.env);
|
|
93
104
|
});
|
|
94
105
|
prog.command("build [entry]").describe("Build the application").option("-o, --output", "Directory to ouput built files").option("--skip-client", "Skip the client build").example("build --config vite.config.js").action(async (entry, opts) => {
|
|
95
|
-
const
|
|
96
|
-
await build(entry,
|
|
106
|
+
const config2 = await getViteConfig(cwd, opts.config);
|
|
107
|
+
await build(entry, config2, opts.ouput, opts["skip-client"], opts.env);
|
|
97
108
|
});
|
|
98
109
|
prog.parse(process.argv);
|
|
99
|
-
async function preview(entry, configFile, port, outDir) {
|
|
110
|
+
async function preview(entry, configFile, port, outDir, envFile) {
|
|
100
111
|
const resolvedConfig = await resolveConfig(
|
|
101
112
|
{ root: cwd, configFile, build: { outDir } },
|
|
102
113
|
"serve"
|
|
@@ -112,9 +123,12 @@ async function preview(entry, configFile, port, outDir) {
|
|
|
112
123
|
}
|
|
113
124
|
const dir = path.resolve(cwd, resolvedConfig.build.outDir);
|
|
114
125
|
const entryFile = entry ? path.join(dir, entry) : await findFileWithExt(dir, "index", [".mjs", ".js"]);
|
|
115
|
-
|
|
126
|
+
if (envFile) {
|
|
127
|
+
envFile = path.resolve(cwd, envFile);
|
|
128
|
+
}
|
|
129
|
+
await adapter.startPreview(dir, entryFile, port, envFile);
|
|
116
130
|
}
|
|
117
|
-
async function dev(cmd, configFile, port) {
|
|
131
|
+
async function dev(cmd, configFile, port, envFile) {
|
|
118
132
|
const resolvedConfig = await resolveConfig(
|
|
119
133
|
{ root: cwd, configFile },
|
|
120
134
|
"build"
|
|
@@ -122,8 +136,11 @@ async function dev(cmd, configFile, port) {
|
|
|
122
136
|
if (port === void 0) {
|
|
123
137
|
port = resolvedConfig.preview.port ?? defaultPort;
|
|
124
138
|
}
|
|
139
|
+
if (envFile) {
|
|
140
|
+
envFile = path.resolve(cwd, envFile);
|
|
141
|
+
}
|
|
125
142
|
if (cmd) {
|
|
126
|
-
await spawnServer(cmd, port);
|
|
143
|
+
await spawnServer(cmd, port, envFile);
|
|
127
144
|
} else {
|
|
128
145
|
const adapter = await resolveAdapter(resolvedConfig);
|
|
129
146
|
if (!adapter) {
|
|
@@ -133,11 +150,11 @@ async function dev(cmd, configFile, port) {
|
|
|
133
150
|
} else if (!adapter.startDev) {
|
|
134
151
|
throw new Error(`Adapter ${adapter.name} does not support serve command`);
|
|
135
152
|
} else {
|
|
136
|
-
await adapter.startDev(configFile, port);
|
|
153
|
+
await adapter.startDev(configFile, port, envFile);
|
|
137
154
|
}
|
|
138
155
|
}
|
|
139
156
|
}
|
|
140
|
-
async function build(entry, configFile, outDir, skipClient = false) {
|
|
157
|
+
async function build(entry, configFile, outDir, skipClient = false, envFile) {
|
|
141
158
|
var _a;
|
|
142
159
|
if (!entry) {
|
|
143
160
|
const resolvedConfig = await resolveConfig(
|
|
@@ -155,7 +172,10 @@ async function build(entry, configFile, outDir, skipClient = false) {
|
|
|
155
172
|
);
|
|
156
173
|
}
|
|
157
174
|
}
|
|
158
|
-
|
|
175
|
+
if (envFile) {
|
|
176
|
+
envFile = path.resolve(cwd, envFile);
|
|
177
|
+
}
|
|
178
|
+
const buildConfig = setMarkoRunOptions(
|
|
159
179
|
{
|
|
160
180
|
root: cwd,
|
|
161
181
|
configFile,
|
|
@@ -171,6 +191,7 @@ async function build(entry, configFile, outDir, skipClient = false) {
|
|
|
171
191
|
await viteBuild({
|
|
172
192
|
...buildConfig,
|
|
173
193
|
build: {
|
|
194
|
+
target: "esnext",
|
|
174
195
|
...buildConfig.build,
|
|
175
196
|
ssr: entry,
|
|
176
197
|
rollupOptions: {
|
|
@@ -193,7 +214,7 @@ async function build(entry, configFile, outDir, skipClient = false) {
|
|
|
193
214
|
function findFileWithExt(dir, base, extensions = [".js", ".cjs", ".mjs", ".ts", ".mts"]) {
|
|
194
215
|
for (const ext of extensions) {
|
|
195
216
|
const filePath = path.join(dir, base + ext);
|
|
196
|
-
if (
|
|
217
|
+
if (fs2.existsSync(filePath)) {
|
|
197
218
|
return filePath;
|
|
198
219
|
}
|
|
199
220
|
}
|
|
@@ -201,7 +222,7 @@ function findFileWithExt(dir, base, extensions = [".js", ".cjs", ".mjs", ".ts",
|
|
|
201
222
|
}
|
|
202
223
|
async function getViteConfig(dir, configFile, bases = ["serve.config", "vite.config"]) {
|
|
203
224
|
if (configFile) {
|
|
204
|
-
if (!
|
|
225
|
+
if (!fs2.existsSync(path.join(dir, configFile))) {
|
|
205
226
|
throw new Error(`Unable to load config file '${configFile}' from ${dir}`);
|
|
206
227
|
}
|
|
207
228
|
return configFile;
|
|
@@ -214,8 +235,8 @@ async function getViteConfig(dir, configFile, bases = ["serve.config", "vite.con
|
|
|
214
235
|
}
|
|
215
236
|
return path.join(__dirname, "default.config.mjs");
|
|
216
237
|
}
|
|
217
|
-
async function resolveAdapter(
|
|
218
|
-
const options =
|
|
238
|
+
async function resolveAdapter(config2) {
|
|
239
|
+
const options = getMarkoRunOptions(config2);
|
|
219
240
|
if (!options) {
|
|
220
241
|
throw new Error("Unable to resolve Marko Serve options");
|
|
221
242
|
}
|
package/dist/runtime/index.cjs
CHANGED
|
@@ -3,10 +3,6 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
6
|
var __copyProps = (to, from, except, desc) => {
|
|
11
7
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
8
|
for (let key of __getOwnPropNames(from))
|
|
@@ -19,16 +15,4 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
15
|
|
|
20
16
|
// src/runtime/index.ts
|
|
21
17
|
var runtime_exports = {};
|
|
22
|
-
__export(runtime_exports, {
|
|
23
|
-
getMatchedRoute: () => import_router.getMatchedRoute,
|
|
24
|
-
handler: () => import_router.handler,
|
|
25
|
-
router: () => import_router.router
|
|
26
|
-
});
|
|
27
18
|
module.exports = __toCommonJS(runtime_exports);
|
|
28
|
-
var import_router = require("@marko/run/router");
|
|
29
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
-
0 && (module.exports = {
|
|
31
|
-
getMatchedRoute,
|
|
32
|
-
handler,
|
|
33
|
-
router
|
|
34
|
-
});
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -1,2 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import type { HandlerLike, ParamsObject, Route } from "./types";
|
|
2
|
+
declare global {
|
|
3
|
+
namespace Marko {
|
|
4
|
+
interface CurrentRoute extends Route {
|
|
5
|
+
}
|
|
6
|
+
type Handler<Params extends ParamsObject = {}, Meta = unknown> = HandlerLike<Route<Params, Meta, string>>;
|
|
7
|
+
function route<Params extends ParamsObject = {}, Meta = unknown>(handler: Handler<Params, Meta>): typeof handler;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export type { HandlerLike, InputObject, InvokeRoute, MatchRoute, NextFunction, RequestContext, Route, RouteContext, RouteContextExtensions, RouteHandler, RouteWithHandler, Router, } from "./types";
|
package/dist/runtime/index.js
CHANGED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/runtime/internal.ts
|
|
21
|
+
var internal_exports = {};
|
|
22
|
+
__export(internal_exports, {
|
|
23
|
+
RequestNotHandled: () => RequestNotHandled,
|
|
24
|
+
RequestNotMatched: () => RequestNotMatched,
|
|
25
|
+
call: () => call,
|
|
26
|
+
compose: () => compose,
|
|
27
|
+
createInput: () => createInput,
|
|
28
|
+
noContent: () => noContent,
|
|
29
|
+
normalize: () => normalize,
|
|
30
|
+
notHandled: () => notHandled,
|
|
31
|
+
notMatched: () => notMatched
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(internal_exports);
|
|
34
|
+
globalThis.Marko ?? (globalThis.Marko = {});
|
|
35
|
+
globalThis.Marko.route = (handler) => handler;
|
|
36
|
+
var RequestNotHandled = Symbol();
|
|
37
|
+
var RequestNotMatched = Symbol();
|
|
38
|
+
function createInput(context) {
|
|
39
|
+
let existing;
|
|
40
|
+
return (data) => {
|
|
41
|
+
existing ?? (existing = {
|
|
42
|
+
$global: {
|
|
43
|
+
context
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
return data ? Object.assign(existing, data) : existing;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
async function call(handler, next, context) {
|
|
50
|
+
let response;
|
|
51
|
+
if (process.env.NODE_ENV !== "production") {
|
|
52
|
+
let nextCallCount = 0;
|
|
53
|
+
let didThrow = false;
|
|
54
|
+
try {
|
|
55
|
+
response = await handler(context, () => {
|
|
56
|
+
nextCallCount++;
|
|
57
|
+
return next();
|
|
58
|
+
});
|
|
59
|
+
} catch (error) {
|
|
60
|
+
didThrow = true;
|
|
61
|
+
if (error instanceof Response) {
|
|
62
|
+
return error;
|
|
63
|
+
}
|
|
64
|
+
throw error;
|
|
65
|
+
} finally {
|
|
66
|
+
if (!response && !didThrow && nextCallCount > 0) {
|
|
67
|
+
console.warn(
|
|
68
|
+
`Handler '${handler.name}' called its next function but no response was returned. This will cause the next function to be called again which is wasteful. Either return or throw the result of calling \`next\`, return or throw a new Response object or finally \`throw null\` to skip handling the request`
|
|
69
|
+
);
|
|
70
|
+
} else if (nextCallCount > 1) {
|
|
71
|
+
console.warn(
|
|
72
|
+
`Handler '${handler.name}' called its next function more than once. Make sure this is intentional because it is inefficient.`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
} else {
|
|
77
|
+
try {
|
|
78
|
+
response = await handler(context, next);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (error == null) {
|
|
81
|
+
throw RequestNotHandled;
|
|
82
|
+
} else if (error instanceof Response) {
|
|
83
|
+
return error;
|
|
84
|
+
}
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (response === null) {
|
|
89
|
+
throw RequestNotMatched;
|
|
90
|
+
}
|
|
91
|
+
return response || next();
|
|
92
|
+
}
|
|
93
|
+
function compose(handlers) {
|
|
94
|
+
const len = handlers.length;
|
|
95
|
+
if (!len) {
|
|
96
|
+
return (_context, next) => next();
|
|
97
|
+
} else if (len === 1) {
|
|
98
|
+
return handlers[0];
|
|
99
|
+
}
|
|
100
|
+
return (context, next) => {
|
|
101
|
+
let i = 0;
|
|
102
|
+
return function nextHandler() {
|
|
103
|
+
return i < len ? call(handlers[i++], nextHandler, context) : next();
|
|
104
|
+
}();
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
function normalize(obj) {
|
|
108
|
+
if (typeof obj === "function") {
|
|
109
|
+
return obj;
|
|
110
|
+
} else if (Array.isArray(obj)) {
|
|
111
|
+
return compose(obj);
|
|
112
|
+
} else if (obj instanceof Promise) {
|
|
113
|
+
const promise = obj.then((value) => {
|
|
114
|
+
fn = Array.isArray(value) ? compose(value) : value;
|
|
115
|
+
});
|
|
116
|
+
let fn = async (context, next) => {
|
|
117
|
+
await promise;
|
|
118
|
+
return fn(context, next);
|
|
119
|
+
};
|
|
120
|
+
return (context, next) => fn(context, next);
|
|
121
|
+
}
|
|
122
|
+
throw new Error(
|
|
123
|
+
`Invalid handler - expected function, array or Promise but received ${obj}`
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
function noContent() {
|
|
127
|
+
return new Response(null, {
|
|
128
|
+
status: 204
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
function notHandled() {
|
|
132
|
+
throw null;
|
|
133
|
+
}
|
|
134
|
+
function notMatched() {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
138
|
+
0 && (module.exports = {
|
|
139
|
+
RequestNotHandled,
|
|
140
|
+
RequestNotMatched,
|
|
141
|
+
call,
|
|
142
|
+
compose,
|
|
143
|
+
createInput,
|
|
144
|
+
noContent,
|
|
145
|
+
normalize,
|
|
146
|
+
notHandled,
|
|
147
|
+
notMatched
|
|
148
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { InputObject, NextFunction, Route, RouteContext, RouteHandler } from "./types";
|
|
2
|
+
export declare const RequestNotHandled: unique symbol;
|
|
3
|
+
export declare const RequestNotMatched: unique symbol;
|
|
4
|
+
export declare function createInput(context: RouteContext): (data: InputObject) => InputObject;
|
|
5
|
+
export declare function call(handler: RouteHandler<Route>, next: NextFunction, context: RouteContext): Promise<Response>;
|
|
6
|
+
export declare function compose(handlers: RouteHandler[]): RouteHandler;
|
|
7
|
+
export declare function normalize(obj: RouteHandler | RouteHandler[] | Promise<RouteHandler | RouteHandler[]>): RouteHandler;
|
|
8
|
+
export declare function noContent(): Response;
|
|
9
|
+
export declare function notHandled(): void;
|
|
10
|
+
export declare function notMatched(): null;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// src/runtime/internal.ts
|
|
2
|
+
globalThis.Marko ?? (globalThis.Marko = {});
|
|
3
|
+
globalThis.Marko.route = (handler) => handler;
|
|
4
|
+
var RequestNotHandled = Symbol();
|
|
5
|
+
var RequestNotMatched = Symbol();
|
|
6
|
+
function createInput(context) {
|
|
7
|
+
let existing;
|
|
8
|
+
return (data) => {
|
|
9
|
+
existing ?? (existing = {
|
|
10
|
+
$global: {
|
|
11
|
+
context
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
return data ? Object.assign(existing, data) : existing;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
async function call(handler, next, context) {
|
|
18
|
+
let response;
|
|
19
|
+
if (process.env.NODE_ENV !== "production") {
|
|
20
|
+
let nextCallCount = 0;
|
|
21
|
+
let didThrow = false;
|
|
22
|
+
try {
|
|
23
|
+
response = await handler(context, () => {
|
|
24
|
+
nextCallCount++;
|
|
25
|
+
return next();
|
|
26
|
+
});
|
|
27
|
+
} catch (error) {
|
|
28
|
+
didThrow = true;
|
|
29
|
+
if (error instanceof Response) {
|
|
30
|
+
return error;
|
|
31
|
+
}
|
|
32
|
+
throw error;
|
|
33
|
+
} finally {
|
|
34
|
+
if (!response && !didThrow && nextCallCount > 0) {
|
|
35
|
+
console.warn(
|
|
36
|
+
`Handler '${handler.name}' called its next function but no response was returned. This will cause the next function to be called again which is wasteful. Either return or throw the result of calling \`next\`, return or throw a new Response object or finally \`throw null\` to skip handling the request`
|
|
37
|
+
);
|
|
38
|
+
} else if (nextCallCount > 1) {
|
|
39
|
+
console.warn(
|
|
40
|
+
`Handler '${handler.name}' called its next function more than once. Make sure this is intentional because it is inefficient.`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
try {
|
|
46
|
+
response = await handler(context, next);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
if (error == null) {
|
|
49
|
+
throw RequestNotHandled;
|
|
50
|
+
} else if (error instanceof Response) {
|
|
51
|
+
return error;
|
|
52
|
+
}
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (response === null) {
|
|
57
|
+
throw RequestNotMatched;
|
|
58
|
+
}
|
|
59
|
+
return response || next();
|
|
60
|
+
}
|
|
61
|
+
function compose(handlers) {
|
|
62
|
+
const len = handlers.length;
|
|
63
|
+
if (!len) {
|
|
64
|
+
return (_context, next) => next();
|
|
65
|
+
} else if (len === 1) {
|
|
66
|
+
return handlers[0];
|
|
67
|
+
}
|
|
68
|
+
return (context, next) => {
|
|
69
|
+
let i = 0;
|
|
70
|
+
return function nextHandler() {
|
|
71
|
+
return i < len ? call(handlers[i++], nextHandler, context) : next();
|
|
72
|
+
}();
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function normalize(obj) {
|
|
76
|
+
if (typeof obj === "function") {
|
|
77
|
+
return obj;
|
|
78
|
+
} else if (Array.isArray(obj)) {
|
|
79
|
+
return compose(obj);
|
|
80
|
+
} else if (obj instanceof Promise) {
|
|
81
|
+
const promise = obj.then((value) => {
|
|
82
|
+
fn = Array.isArray(value) ? compose(value) : value;
|
|
83
|
+
});
|
|
84
|
+
let fn = async (context, next) => {
|
|
85
|
+
await promise;
|
|
86
|
+
return fn(context, next);
|
|
87
|
+
};
|
|
88
|
+
return (context, next) => fn(context, next);
|
|
89
|
+
}
|
|
90
|
+
throw new Error(
|
|
91
|
+
`Invalid handler - expected function, array or Promise but received ${obj}`
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
function noContent() {
|
|
95
|
+
return new Response(null, {
|
|
96
|
+
status: 204
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
function notHandled() {
|
|
100
|
+
throw null;
|
|
101
|
+
}
|
|
102
|
+
function notMatched() {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
export {
|
|
106
|
+
RequestNotHandled,
|
|
107
|
+
RequestNotMatched,
|
|
108
|
+
call,
|
|
109
|
+
compose,
|
|
110
|
+
createInput,
|
|
111
|
+
noContent,
|
|
112
|
+
normalize,
|
|
113
|
+
notHandled,
|
|
114
|
+
notMatched
|
|
115
|
+
};
|
package/dist/runtime/router.cjs
CHANGED
|
@@ -20,20 +20,20 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/runtime/router.ts
|
|
21
21
|
var router_exports = {};
|
|
22
22
|
__export(router_exports, {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
invokeRoute: () => invokeRoute,
|
|
24
|
+
matchRoute: () => matchRoute,
|
|
25
25
|
router: () => router
|
|
26
26
|
});
|
|
27
27
|
module.exports = __toCommonJS(router_exports);
|
|
28
28
|
function notImplemented() {
|
|
29
29
|
throw new Error("This should have been replaced by the @marko/run plugin at build/dev time");
|
|
30
30
|
}
|
|
31
|
-
var handler = notImplemented;
|
|
32
31
|
var router = notImplemented;
|
|
33
|
-
var
|
|
32
|
+
var matchRoute = notImplemented;
|
|
33
|
+
var invokeRoute = notImplemented;
|
|
34
34
|
// Annotate the CommonJS export names for ESM import in node:
|
|
35
35
|
0 && (module.exports = {
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
invokeRoute,
|
|
37
|
+
matchRoute,
|
|
38
38
|
router
|
|
39
39
|
});
|
package/dist/runtime/router.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const
|
|
1
|
+
import type { InvokeRoute, MatchRoute, Router } from './types';
|
|
2
|
+
export declare const router: Router<import("./types").Platform>;
|
|
3
|
+
export declare const matchRoute: MatchRoute;
|
|
4
|
+
export declare const invokeRoute: InvokeRoute<import("./types").Platform>;
|
package/dist/runtime/router.js
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
function notImplemented() {
|
|
3
3
|
throw new Error("This should have been replaced by the @marko/run plugin at build/dev time");
|
|
4
4
|
}
|
|
5
|
-
var handler = notImplemented;
|
|
6
5
|
var router = notImplemented;
|
|
7
|
-
var
|
|
6
|
+
var matchRoute = notImplemented;
|
|
7
|
+
var invokeRoute = notImplemented;
|
|
8
8
|
export {
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
invokeRoute,
|
|
10
|
+
matchRoute,
|
|
11
11
|
router
|
|
12
12
|
};
|
package/dist/runtime/types.d.ts
CHANGED
|
@@ -1,22 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
declare type Awaitable<T> = Promise<T> | T;
|
|
2
|
+
declare type OneOrMany<T> = T | T[];
|
|
3
|
+
declare type Combine<T> = T extends object ? {
|
|
4
|
+
[P in keyof T]: T[P];
|
|
5
|
+
} : T;
|
|
6
|
+
export interface RouteContextExtensions {
|
|
7
|
+
}
|
|
8
|
+
export interface Platform {
|
|
9
|
+
}
|
|
10
|
+
export declare type ParamsObject = Record<string, string>;
|
|
11
|
+
export declare type InputObject = Record<PropertyKey, any>;
|
|
12
|
+
export interface RequestContext<T = Platform> {
|
|
4
13
|
url: URL;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
14
|
+
method: string;
|
|
15
|
+
request: Request;
|
|
16
|
+
platform: T;
|
|
8
17
|
}
|
|
9
|
-
export declare type
|
|
10
|
-
|
|
11
|
-
|
|
18
|
+
export declare type RouteContext<TRoute extends Route = Route> = TRoute extends any ? Combine<RouteContextExtensions & Readonly<RequestContext & {
|
|
19
|
+
route: TRoute["path"];
|
|
20
|
+
params: TRoute["params"];
|
|
21
|
+
meta: TRoute["meta"];
|
|
22
|
+
}>> : never;
|
|
23
|
+
export declare type NextFunction = () => Awaitable<Response>;
|
|
24
|
+
export declare type HandlerLike<TRoute extends Route = Route> = Awaitable<OneOrMany<RouteHandler<TRoute>>>;
|
|
25
|
+
export declare type RouteHandler<TRoute extends Route = Route> = (context: RouteContext<TRoute>, next: NextFunction) => Awaitable<Response | null | void>;
|
|
26
|
+
export interface Route<Params extends ParamsObject = {}, Meta = unknown, Path extends string = string> {
|
|
27
|
+
path: Path;
|
|
12
28
|
params: Params;
|
|
13
29
|
meta: Meta;
|
|
14
|
-
invoke: Router;
|
|
15
30
|
}
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
export declare type Handler = (context: AdapterRequestContext) => Promise<Response>;
|
|
19
|
-
export interface Runtime {
|
|
20
|
-
router: Router;
|
|
21
|
-
getMatchedRoute: RouteMatcher;
|
|
31
|
+
export interface RouteWithHandler<Params extends ParamsObject = {}, Meta = unknown, Path extends string = string> extends Route<Params, Meta, Path> {
|
|
32
|
+
handler: RouteHandler<this>;
|
|
22
33
|
}
|
|
34
|
+
export declare type MatchRoute = (method: string, pathname: string) => RouteWithHandler | null;
|
|
35
|
+
export declare type Router<T = Platform> = (context: RequestContext<T>) => Promise<Response | void>;
|
|
36
|
+
export declare type InvokeRoute<T = Platform> = (route: Route | null, context: RequestContext<T>) => Promise<Response | void>;
|
|
37
|
+
export {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { Route, BuiltRoutes,
|
|
2
|
-
export declare const DefaultCodegenOptions: CodegenOptions;
|
|
1
|
+
import type { Route, BuiltRoutes, RoutableFile, RouterOptions } from "../types";
|
|
3
2
|
export declare function renderRouteTemplate(route: Route): string;
|
|
4
3
|
export declare function renderRouteEntry(route: Route): string;
|
|
5
|
-
export declare function renderRouter(routes: BuiltRoutes, options?:
|
|
4
|
+
export declare function renderRouter(routes: BuiltRoutes, options?: RouterOptions): string;
|
|
5
|
+
export declare function renderMiddleware(middleware: RoutableFile[]): string;
|
|
6
|
+
export declare function renderRouteTypeInfo(routes: BuiltRoutes, pathPrefix?: string): string;
|
|
@@ -2,7 +2,7 @@ export interface Writer {
|
|
|
2
2
|
indent: number;
|
|
3
3
|
branch(name: string): Writer;
|
|
4
4
|
join(closeBranches?: boolean): void;
|
|
5
|
-
write(data: string): Writer;
|
|
5
|
+
write(data: string, indent?: boolean): Writer;
|
|
6
6
|
writeLines(...lines: string[]): Writer;
|
|
7
7
|
writeBlockStart(data: string): Writer;
|
|
8
8
|
writeBlockEnd(data: string): Writer;
|
package/dist/vite/constants.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
declare type ValuesOf<T> = T[keyof T];
|
|
2
|
-
export declare const
|
|
3
|
-
export declare const virtualFilePrefix = "virtual:marko-
|
|
2
|
+
export declare const markoRunFilePrefix = "__marko-run__";
|
|
3
|
+
export declare const virtualFilePrefix = "virtual:marko-run";
|
|
4
4
|
export declare const virtualRoutesPrefix: string;
|
|
5
|
+
export declare const virtualRuntimePrefix: string;
|
|
5
6
|
export declare const httpVerbs: readonly ["get", "post", "put", "delete"];
|
|
6
7
|
export declare const serverEntryQuery = "?marko-server-entry";
|
|
7
8
|
export declare const browserEntryQuery = "?marko-browser-entry";
|