@marko/run 0.2.7 → 0.2.9
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/default-entry.mjs +2 -2
- package/dist/adapter/dev-server.d.ts +1 -1
- package/dist/adapter/index.cjs +469 -28
- package/dist/adapter/index.d.ts +1 -1
- package/dist/adapter/index.js +469 -28
- package/dist/adapter/load-dev-worker.mjs +11 -10
- package/dist/adapter/middleware.cjs +15 -3
- package/dist/adapter/middleware.js +15 -3
- package/dist/adapter/utils.d.ts +10 -0
- package/dist/cli/index.mjs +77 -46
- package/dist/components/routes-explorer/+layout.marko +17 -0
- package/dist/components/routes-explorer/+page.marko +1 -0
- package/dist/runtime/index.d.ts +2 -0
- package/dist/vite/index.cjs +69 -44
- package/dist/vite/index.d.ts +1 -1
- package/dist/vite/index.js +69 -44
- package/dist/vite/routes/builder.d.ts +6 -1
- package/dist/vite/types.d.ts +11 -0
- package/package.json +17 -12
package/dist/adapter/index.js
CHANGED
|
@@ -1,9 +1,231 @@
|
|
|
1
1
|
// src/adapter/index.ts
|
|
2
2
|
import path from "path";
|
|
3
|
+
import fs2 from "fs";
|
|
3
4
|
import { fileURLToPath } from "url";
|
|
4
5
|
|
|
5
6
|
// src/adapter/dev-server.ts
|
|
6
7
|
import { createServer } from "vite";
|
|
8
|
+
|
|
9
|
+
// src/adapter/polyfill.ts
|
|
10
|
+
import * as webStream from "stream/web";
|
|
11
|
+
import { webcrypto } from "crypto";
|
|
12
|
+
import * as undici from "undici";
|
|
13
|
+
globalThis.crypto ?? (globalThis.crypto = webcrypto);
|
|
14
|
+
globalThis.fetch ?? (globalThis.fetch = undici.fetch);
|
|
15
|
+
globalThis.Response ?? (globalThis.Response = undici.Response);
|
|
16
|
+
globalThis.Request ?? (globalThis.Request = undici.Request);
|
|
17
|
+
globalThis.Headers ?? (globalThis.Headers = undici.Headers);
|
|
18
|
+
globalThis.ReadableStream ?? (globalThis.ReadableStream = webStream.ReadableStream);
|
|
19
|
+
globalThis.TransformStream ?? (globalThis.TransformStream = webStream.TransformStream);
|
|
20
|
+
globalThis.WritableStream ?? (globalThis.WritableStream = webStream.WritableStream);
|
|
21
|
+
globalThis.FormData ?? (globalThis.FormData = undici.FormData);
|
|
22
|
+
globalThis.File ?? (globalThis.File = undici.File);
|
|
23
|
+
|
|
24
|
+
// src/adapter/middleware.ts
|
|
25
|
+
function getForwardedHeader(req, name) {
|
|
26
|
+
const value = req.headers["x-forwarded-" + name];
|
|
27
|
+
if (value) {
|
|
28
|
+
if (typeof value === "string") {
|
|
29
|
+
const index = value.indexOf(",");
|
|
30
|
+
return index < 0 ? value : value.slice(0, index);
|
|
31
|
+
}
|
|
32
|
+
return value[0];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function getOrigin(req, trustProxy) {
|
|
36
|
+
const protocol = req.protocol || trustProxy && getForwardedHeader(req, "proto") || (req.socket.encrypted ? "https" : "http");
|
|
37
|
+
let host = req.headers.host || trustProxy && getForwardedHeader(req, "host");
|
|
38
|
+
if (!host) {
|
|
39
|
+
if (process.env.NODE_ENV !== "production") {
|
|
40
|
+
host = "localhost";
|
|
41
|
+
console.warn(
|
|
42
|
+
`Could not automatically determine the origin host, using 'localhost'. Use the 'origin' option or the 'ORIGIN' environment variable to set the origin explicitly.`
|
|
43
|
+
);
|
|
44
|
+
} else {
|
|
45
|
+
throw new Error(
|
|
46
|
+
`Could not automatically determine the origin host. Use the 'origin' option or the 'ORIGIN' environment variable to set the origin explicitly.`
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return `${protocol}://${host}`;
|
|
51
|
+
}
|
|
52
|
+
var inExpiresDateRgs = /Expires\s*=\s*(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*$/i;
|
|
53
|
+
function setResponseHeaders(response, res) {
|
|
54
|
+
for (const [key, value] of response.headers) {
|
|
55
|
+
if (key !== "set-cookie") {
|
|
56
|
+
res.setHeader(key, value);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const setCookies = getSetCookie(response.headers);
|
|
60
|
+
if (setCookies == null ? void 0 : setCookies.length) {
|
|
61
|
+
res.setHeader("set-cookie", setCookies);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
var getSetCookie = Headers.prototype.getSetCookie ? getSetCookie_platform : getSetCookie_fallback;
|
|
65
|
+
function getSetCookie_platform(headers) {
|
|
66
|
+
return headers.getSetCookie();
|
|
67
|
+
}
|
|
68
|
+
function getSetCookie_fallback(headers) {
|
|
69
|
+
const value = headers.get("set-cookie");
|
|
70
|
+
if (!value)
|
|
71
|
+
return void 0;
|
|
72
|
+
let sepIndex = value.indexOf(",") + 1;
|
|
73
|
+
if (!sepIndex)
|
|
74
|
+
return value;
|
|
75
|
+
let index = 0;
|
|
76
|
+
let setCookie = void 0;
|
|
77
|
+
let setCookies = void 0;
|
|
78
|
+
do {
|
|
79
|
+
const valuePart = value.slice(index, sepIndex - 1);
|
|
80
|
+
if (!inExpiresDateRgs.test(valuePart)) {
|
|
81
|
+
if (setCookies) {
|
|
82
|
+
setCookies.push(valuePart);
|
|
83
|
+
} else if (setCookie) {
|
|
84
|
+
setCookies = [setCookie, valuePart];
|
|
85
|
+
} else {
|
|
86
|
+
setCookie = valuePart;
|
|
87
|
+
}
|
|
88
|
+
index = sepIndex;
|
|
89
|
+
while (value.charCodeAt(index) === 32)
|
|
90
|
+
index++;
|
|
91
|
+
}
|
|
92
|
+
sepIndex = value.indexOf(",", sepIndex) + 1;
|
|
93
|
+
} while (sepIndex);
|
|
94
|
+
if (index) {
|
|
95
|
+
const valuePart = value.slice(index);
|
|
96
|
+
if (setCookies) {
|
|
97
|
+
setCookies.push(valuePart);
|
|
98
|
+
return setCookies;
|
|
99
|
+
}
|
|
100
|
+
return [setCookie, valuePart];
|
|
101
|
+
}
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
function createMiddleware(fetch2, options = {}) {
|
|
105
|
+
const {
|
|
106
|
+
origin = process.env.ORIGIN,
|
|
107
|
+
trustProxy = process.env.TRUST_PROXY === "1"
|
|
108
|
+
} = options;
|
|
109
|
+
return async (req, res, next) => {
|
|
110
|
+
var _a;
|
|
111
|
+
const controller = new AbortController();
|
|
112
|
+
const { signal } = controller;
|
|
113
|
+
const url = new URL(req.url, origin || getOrigin(req, trustProxy));
|
|
114
|
+
const ip = req.ip || trustProxy && getForwardedHeader(req, "for") || req.socket.remoteAddress || "";
|
|
115
|
+
req.on("error", onErrorOrClose);
|
|
116
|
+
req.socket.on("error", onErrorOrClose);
|
|
117
|
+
res.on("error", onErrorOrClose);
|
|
118
|
+
res.on("close", onErrorOrClose);
|
|
119
|
+
signal.addEventListener("abort", onSignalAborted);
|
|
120
|
+
function onErrorOrClose(err) {
|
|
121
|
+
req.off("error", onErrorOrClose);
|
|
122
|
+
req.socket.off("error", onErrorOrClose);
|
|
123
|
+
res.off("error", onErrorOrClose);
|
|
124
|
+
res.off("close", onErrorOrClose);
|
|
125
|
+
if (err) {
|
|
126
|
+
signal.removeEventListener("abort", onSignalAborted);
|
|
127
|
+
controller.abort(err);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function onSignalAborted() {
|
|
131
|
+
if (next) {
|
|
132
|
+
next(signal.reason);
|
|
133
|
+
} else {
|
|
134
|
+
if (!res.destroyed && res.socket) {
|
|
135
|
+
res.socket.destroySoon();
|
|
136
|
+
}
|
|
137
|
+
console.error(signal.reason);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
let setDevClientId;
|
|
141
|
+
if (process.env.NODE_ENV !== "production" && globalThis.__marko_run_dev__ && ((_a = req.headers.accept) == null ? void 0 : _a.includes("text/html"))) {
|
|
142
|
+
setDevClientId = globalThis.__marko_run_dev__.onClient((ws) => {
|
|
143
|
+
if (signal.aborted) {
|
|
144
|
+
sendError();
|
|
145
|
+
} else {
|
|
146
|
+
signal.addEventListener("abort", sendError);
|
|
147
|
+
}
|
|
148
|
+
function sendError() {
|
|
149
|
+
const { message, stack = "" } = signal.reason;
|
|
150
|
+
ws.send(
|
|
151
|
+
JSON.stringify({
|
|
152
|
+
type: "error",
|
|
153
|
+
err: { message, stack }
|
|
154
|
+
})
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
let body;
|
|
160
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
161
|
+
if (req.readableDidRead) {
|
|
162
|
+
body = bodyConsumedErrorStream;
|
|
163
|
+
} else {
|
|
164
|
+
body = req;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const request = new Request(url, {
|
|
168
|
+
method: req.method,
|
|
169
|
+
headers: req.headers,
|
|
170
|
+
body,
|
|
171
|
+
// @ts-expect-error: Node requires this for streams
|
|
172
|
+
duplex: "half",
|
|
173
|
+
signal
|
|
174
|
+
});
|
|
175
|
+
const response = await fetch2(request, {
|
|
176
|
+
ip,
|
|
177
|
+
request: req,
|
|
178
|
+
response: res
|
|
179
|
+
});
|
|
180
|
+
if (!response) {
|
|
181
|
+
if (next) {
|
|
182
|
+
next();
|
|
183
|
+
}
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (process.env.NODE_ENV !== "production" && setDevClientId) {
|
|
187
|
+
setDevClientId(response);
|
|
188
|
+
}
|
|
189
|
+
res.statusCode = response.status;
|
|
190
|
+
setResponseHeaders(response, res);
|
|
191
|
+
if (!response.body) {
|
|
192
|
+
if (!response.headers.has("content-length")) {
|
|
193
|
+
res.setHeader("content-length", "0");
|
|
194
|
+
}
|
|
195
|
+
res.end();
|
|
196
|
+
return;
|
|
197
|
+
} else if (res.destroyed) {
|
|
198
|
+
controller.abort(new Error("Response stream destroyed"));
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
writeResponse(response.body.getReader(), res, controller);
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
async function writeResponse(reader, res, controller) {
|
|
205
|
+
try {
|
|
206
|
+
while (!controller.signal.aborted) {
|
|
207
|
+
const { done, value } = await reader.read();
|
|
208
|
+
if (done) {
|
|
209
|
+
res.end();
|
|
210
|
+
return;
|
|
211
|
+
} else if (!res.write(value)) {
|
|
212
|
+
res.once("drain", () => writeResponse(reader, res, controller));
|
|
213
|
+
return;
|
|
214
|
+
} else if (res.flush) {
|
|
215
|
+
res.flush();
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
} catch (err) {
|
|
219
|
+
controller.abort(err);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
var bodyConsumedErrorStream = new ReadableStream({
|
|
223
|
+
start(controller) {
|
|
224
|
+
controller.error(new Error("The request body stream was already consumed by something before Marko Run."));
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
// src/adapter/dev-server.ts
|
|
7
229
|
import stripAnsi from "strip-ansi";
|
|
8
230
|
function createViteDevMiddleware(devServer, load, factory) {
|
|
9
231
|
let value;
|
|
@@ -38,22 +260,25 @@ async function createViteDevServer(config2) {
|
|
|
38
260
|
}
|
|
39
261
|
async function createDevServer(config2) {
|
|
40
262
|
const devServer = await createViteDevServer(config2);
|
|
41
|
-
const
|
|
42
|
-
|
|
263
|
+
const routerMiddleware = createMiddleware(
|
|
264
|
+
(request, platform) => globalThis.__marko_run__.fetch(request, platform)
|
|
43
265
|
);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
266
|
+
devServer.middlewares.use(async (req, res, next) => {
|
|
267
|
+
await devServer.ssrLoadModule("@marko/run/router");
|
|
268
|
+
routerMiddleware(req, res, (err) => {
|
|
269
|
+
if (err) {
|
|
270
|
+
res.statusCode = 500;
|
|
271
|
+
if (err instanceof Error) {
|
|
272
|
+
devServer.ssrFixStacktrace(err);
|
|
273
|
+
res.end(err.stack && stripAnsi(err.stack));
|
|
274
|
+
} else {
|
|
275
|
+
res.end();
|
|
276
|
+
}
|
|
277
|
+
} else {
|
|
278
|
+
next == null ? void 0 : next();
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
});
|
|
57
282
|
return devServer;
|
|
58
283
|
}
|
|
59
284
|
var ClientIdCookieName = "marko-run-client-id";
|
|
@@ -124,6 +349,153 @@ function getDevGlobal() {
|
|
|
124
349
|
return devGlobal;
|
|
125
350
|
}
|
|
126
351
|
|
|
352
|
+
// src/adapter/utils.ts
|
|
353
|
+
import supporsColor from "supports-color";
|
|
354
|
+
import kleur from "kleur";
|
|
355
|
+
function logInfoBox(address, explorer) {
|
|
356
|
+
const color = !!supporsColor.stdout;
|
|
357
|
+
let message = kleur.bold("Marko Run");
|
|
358
|
+
if (true) {
|
|
359
|
+
message += ` v${"0.2.9"}`;
|
|
360
|
+
}
|
|
361
|
+
message += "\n\n";
|
|
362
|
+
message += kleur.dim("Server listening at");
|
|
363
|
+
message += "\n";
|
|
364
|
+
message += kleur.cyan(kleur.underline(address));
|
|
365
|
+
if (explorer) {
|
|
366
|
+
message += "\n\n";
|
|
367
|
+
message += kleur.dim("Explore your routes at");
|
|
368
|
+
message += "\n";
|
|
369
|
+
message += kleur.dim(kleur.green(kleur.underline(explorer)));
|
|
370
|
+
}
|
|
371
|
+
const lines = drawMarkoBox(message, { color, fill: color });
|
|
372
|
+
console.log(lines.join("\n"));
|
|
373
|
+
}
|
|
374
|
+
function drawMarkoBox(message, options) {
|
|
375
|
+
const textPaddingWidth = 3;
|
|
376
|
+
const logoPaddingWidth = 2;
|
|
377
|
+
const textPadding = " ".repeat(textPaddingWidth);
|
|
378
|
+
const logoPadding = " ".repeat(logoPaddingWidth);
|
|
379
|
+
const logo = drawMarkoLogo(options);
|
|
380
|
+
const textLines = message.split(/\n/);
|
|
381
|
+
const textWidths = textLines.map(
|
|
382
|
+
(line) => line.replace(/\x1b\[\d+m/g, "").length
|
|
383
|
+
);
|
|
384
|
+
const textWidth = Math.max(...textWidths);
|
|
385
|
+
const height = Math.max(textLines.length + 2, logo.lines.length);
|
|
386
|
+
const width = textPaddingWidth * 2 + logoPaddingWidth + textWidth + logo.width;
|
|
387
|
+
const hBorder = "\u2500".repeat(width);
|
|
388
|
+
const vBorder = "\u2502";
|
|
389
|
+
const lineDiff = logo.lines.length - textLines.length;
|
|
390
|
+
const textStartLine = lineDiff > 0 ? Math.max(Math.floor(lineDiff / 2), 1) : 1;
|
|
391
|
+
const textEndLine = height - (lineDiff > 0 ? Math.ceil(lineDiff / 2) : 1);
|
|
392
|
+
const logoEndLine = logo.lines.length;
|
|
393
|
+
const logoFill = " ".repeat(logo.width);
|
|
394
|
+
const textFill = " ".repeat(textWidth);
|
|
395
|
+
const lines = [`\u256D${hBorder}\u256E`];
|
|
396
|
+
for (let i = 0; i < height; i++) {
|
|
397
|
+
let line = vBorder;
|
|
398
|
+
line += logoPadding;
|
|
399
|
+
if (i < logoEndLine) {
|
|
400
|
+
line += logo.lines[i];
|
|
401
|
+
} else {
|
|
402
|
+
line += logoFill;
|
|
403
|
+
}
|
|
404
|
+
line += textPadding;
|
|
405
|
+
if (i >= textStartLine && i < textEndLine) {
|
|
406
|
+
let index = i - textStartLine;
|
|
407
|
+
line += textLines[index];
|
|
408
|
+
line += " ".repeat(textWidth - textWidths[index]);
|
|
409
|
+
} else {
|
|
410
|
+
line += textFill;
|
|
411
|
+
}
|
|
412
|
+
line += textPadding;
|
|
413
|
+
line += vBorder;
|
|
414
|
+
lines.push(line);
|
|
415
|
+
}
|
|
416
|
+
lines.push(`\u2570${hBorder}\u256F`);
|
|
417
|
+
return lines;
|
|
418
|
+
}
|
|
419
|
+
function drawMarkoLogo(options = {}) {
|
|
420
|
+
const { fill = true, color = true } = options;
|
|
421
|
+
const source = `
|
|
422
|
+
TT____ YY____ R____
|
|
423
|
+
C\u2571T\u2572 \u2572G\u2571Y\u2572 \u2572 R\u2572 \u2572
|
|
424
|
+
C\u2571 T\u2572 G\u2571 Y\u2572 \u2572 R\u2572 \u2572
|
|
425
|
+
C\u2571 \u2571T\u2572G\u2571 \u2571Y\u2572 \u2572 R\u2572 \u2572
|
|
426
|
+
B\u2572 \u2572 GG\u203E\u203E\u203E\u203E O\u2571 \u2571 P\u2571 \u2571
|
|
427
|
+
B\u2572 \u2572 OOO\u2571 \u2571 P\u2571 \u2571
|
|
428
|
+
B\u2572 \u2572 OOO\u2571 \u2571 P\u2571 \u2571
|
|
429
|
+
B\u203E\u203E\u203E\u203E OOO\u203E\u203E\u203E\u203E P\u203E\u203E\u203E\u203E
|
|
430
|
+
`;
|
|
431
|
+
const resetEscape = "\x1B[0m";
|
|
432
|
+
const colorEscapeCodes = Object.entries({
|
|
433
|
+
B: "#06cfe5",
|
|
434
|
+
C: "#05a5f0",
|
|
435
|
+
T: "#19d89c",
|
|
436
|
+
G: "#81dc09",
|
|
437
|
+
Y: "#ffd900",
|
|
438
|
+
O: "#ff9500",
|
|
439
|
+
R: "#f3154d",
|
|
440
|
+
P: "#ce176c"
|
|
441
|
+
}).reduce((acc, [key, hex]) => {
|
|
442
|
+
const r = parseInt(hex.slice(1, 3), 16);
|
|
443
|
+
const g = parseInt(hex.slice(3, 5), 16);
|
|
444
|
+
const b = parseInt(hex.slice(5, 7), 16);
|
|
445
|
+
acc[key] = `\x1B[38;2;${r};${g};${b}m`;
|
|
446
|
+
return acc;
|
|
447
|
+
}, {});
|
|
448
|
+
const lines = [];
|
|
449
|
+
const lineWidths = [];
|
|
450
|
+
let line = "";
|
|
451
|
+
let lineWidth = 0;
|
|
452
|
+
let width = 0;
|
|
453
|
+
for (let i = 0; i < source.length; i++) {
|
|
454
|
+
let char = source[i];
|
|
455
|
+
if (char === "\n") {
|
|
456
|
+
if (line) {
|
|
457
|
+
if (color) {
|
|
458
|
+
line += resetEscape;
|
|
459
|
+
}
|
|
460
|
+
width = Math.max(lineWidth, width);
|
|
461
|
+
lines.push(line);
|
|
462
|
+
lineWidths.push(lineWidth);
|
|
463
|
+
line = "";
|
|
464
|
+
lineWidth = 0;
|
|
465
|
+
}
|
|
466
|
+
} else if (/[A-Z]/.test(char)) {
|
|
467
|
+
while (source[i + 1] === char)
|
|
468
|
+
i++;
|
|
469
|
+
if (color) {
|
|
470
|
+
line += colorEscapeCodes[char];
|
|
471
|
+
}
|
|
472
|
+
if (fill) {
|
|
473
|
+
let fillChar = "";
|
|
474
|
+
for (; i < source.length; i++) {
|
|
475
|
+
char = source[i + 1];
|
|
476
|
+
if (fillChar && char !== " ") {
|
|
477
|
+
break;
|
|
478
|
+
} else if (!fillChar) {
|
|
479
|
+
fillChar = char;
|
|
480
|
+
}
|
|
481
|
+
line += fillChar;
|
|
482
|
+
lineWidth++;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
} else {
|
|
486
|
+
line += char;
|
|
487
|
+
lineWidth++;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
for (let i = 0; i < lines.length; i++) {
|
|
491
|
+
const padding = width - lineWidths[i];
|
|
492
|
+
if (padding > 0) {
|
|
493
|
+
lines[i] += " ".repeat(width - lineWidths[i]);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
return { lines, width };
|
|
497
|
+
}
|
|
498
|
+
|
|
127
499
|
// src/vite/utils/server.ts
|
|
128
500
|
import net from "net";
|
|
129
501
|
import cp from "child_process";
|
|
@@ -267,7 +639,14 @@ function sleep(ms) {
|
|
|
267
639
|
}
|
|
268
640
|
|
|
269
641
|
// src/adapter/index.ts
|
|
642
|
+
import { createRequire } from "module";
|
|
270
643
|
import parseNodeArgs from "parse-node-args";
|
|
644
|
+
|
|
645
|
+
// src/vite/constants.ts
|
|
646
|
+
var markoRunFilePrefix = "__marko-run__";
|
|
647
|
+
var virtualFilePrefix = "virtual:marko-run";
|
|
648
|
+
|
|
649
|
+
// src/adapter/index.ts
|
|
271
650
|
var __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
272
651
|
var defaultEntry = path.join(__dirname, "default-entry");
|
|
273
652
|
var loadDevWorker = path.join(__dirname, "load-dev-worker.mjs");
|
|
@@ -279,6 +658,8 @@ function adapter() {
|
|
|
279
658
|
},
|
|
280
659
|
async startDev(entry, config2, options) {
|
|
281
660
|
const { port = 3e3, envFile } = options;
|
|
661
|
+
globalThis.__marko_run_vite_config__ = config2;
|
|
662
|
+
const explorerPromise = startExplorer();
|
|
282
663
|
if (entry) {
|
|
283
664
|
const { nodeArgs } = parseNodeArgs(options.args);
|
|
284
665
|
let worker;
|
|
@@ -309,39 +690,99 @@ function adapter() {
|
|
|
309
690
|
}
|
|
310
691
|
worker = nextWorker;
|
|
311
692
|
}
|
|
312
|
-
await start();
|
|
693
|
+
const [explorer2] = await Promise.all([explorerPromise, start()]);
|
|
313
694
|
return {
|
|
314
695
|
port,
|
|
315
|
-
close() {
|
|
316
|
-
worker.kill();
|
|
696
|
+
async close() {
|
|
697
|
+
await Promise.all([worker.kill(), explorer2 == null ? void 0 : explorer2.close()]);
|
|
317
698
|
}
|
|
318
699
|
};
|
|
319
700
|
}
|
|
320
701
|
const devServer = await createDevServer(config2);
|
|
321
702
|
envFile && await loadEnv(envFile);
|
|
322
|
-
|
|
703
|
+
const listen = new Promise((resolve) => {
|
|
323
704
|
const listener = devServer.middlewares.listen(port, () => {
|
|
324
|
-
|
|
325
|
-
console.log(`Dev server started: http://localhost:${address.port}`);
|
|
326
|
-
resolve({
|
|
327
|
-
port,
|
|
328
|
-
async close() {
|
|
329
|
-
await devServer.close();
|
|
330
|
-
}
|
|
331
|
-
});
|
|
705
|
+
resolve(listener.address());
|
|
332
706
|
});
|
|
333
707
|
});
|
|
708
|
+
const [explorer, address] = await Promise.all([explorerPromise, listen]);
|
|
709
|
+
logInfoBox(
|
|
710
|
+
`http://localhost:${address.port}`,
|
|
711
|
+
explorer && `http://localhost:${explorer.port}`
|
|
712
|
+
);
|
|
713
|
+
return {
|
|
714
|
+
port: address.port,
|
|
715
|
+
async close() {
|
|
716
|
+
await Promise.all([devServer.close(), explorer == null ? void 0 : explorer.close()]);
|
|
717
|
+
}
|
|
718
|
+
};
|
|
334
719
|
},
|
|
335
720
|
async startPreview(entry, options) {
|
|
336
721
|
const { port = 3e3, envFile } = options;
|
|
337
722
|
const { nodeArgs } = parseNodeArgs(options.args);
|
|
338
723
|
const args = [...nodeArgs, entry];
|
|
339
724
|
const server = await spawnServer("node", args, port, envFile);
|
|
340
|
-
|
|
725
|
+
if (!options.sourceEntry) {
|
|
726
|
+
logInfoBox(`http://localhost:${port}`);
|
|
727
|
+
}
|
|
341
728
|
return server;
|
|
729
|
+
},
|
|
730
|
+
async routesGenerated(routes, virtualFiles, meta) {
|
|
731
|
+
if (process.env.MR_EXPLORER !== "true") {
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
const promises = [];
|
|
735
|
+
const cacheDir = path.resolve(__dirname, "../../.cache/explorer");
|
|
736
|
+
const codeDir = path.join(cacheDir, "code");
|
|
737
|
+
if (fs2.existsSync(codeDir)) {
|
|
738
|
+
await fs2.promises.rm(codeDir, { recursive: true });
|
|
739
|
+
}
|
|
740
|
+
await fs2.promises.mkdir(codeDir, { recursive: true });
|
|
741
|
+
const data = {
|
|
742
|
+
meta,
|
|
743
|
+
routes: {},
|
|
744
|
+
files: {}
|
|
745
|
+
};
|
|
746
|
+
for (const [name, code] of virtualFiles) {
|
|
747
|
+
let fileName = "";
|
|
748
|
+
let index = name.indexOf(markoRunFilePrefix);
|
|
749
|
+
if (index >= 0) {
|
|
750
|
+
fileName = name.slice(index);
|
|
751
|
+
data.files[fileName] = `${virtualFilePrefix}/${fileName}`;
|
|
752
|
+
} else if (name.startsWith("@marko/run")) {
|
|
753
|
+
fileName = name.slice(11);
|
|
754
|
+
data.files[fileName] = name;
|
|
755
|
+
}
|
|
756
|
+
if (fileName) {
|
|
757
|
+
promises.push(
|
|
758
|
+
fs2.promises.writeFile(path.join(codeDir, fileName), code, {})
|
|
759
|
+
);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
for (const route of routes.list) {
|
|
763
|
+
data.routes[route.index] = route;
|
|
764
|
+
}
|
|
765
|
+
for (const [id, route] of Object.entries(routes.special)) {
|
|
766
|
+
data.routes["s" + id] = route;
|
|
767
|
+
}
|
|
768
|
+
promises.push(
|
|
769
|
+
fs2.promises.writeFile(
|
|
770
|
+
path.join(cacheDir, "data.json"),
|
|
771
|
+
JSON.stringify(data),
|
|
772
|
+
{}
|
|
773
|
+
)
|
|
774
|
+
);
|
|
775
|
+
await Promise.all(promises);
|
|
342
776
|
}
|
|
343
777
|
};
|
|
344
778
|
}
|
|
779
|
+
var require2 = createRequire(import.meta.url);
|
|
780
|
+
async function startExplorer() {
|
|
781
|
+
if (process.env.MR_EXPLORER === "true") {
|
|
782
|
+
const entry = require2.resolve("@marko/run-explorer");
|
|
783
|
+
return await spawnServer("node", [entry], 1234);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
345
786
|
export {
|
|
346
787
|
createDevServer,
|
|
347
788
|
createViteDevMiddleware,
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { createServer } from "vite";
|
|
2
|
-
|
|
3
|
-
let getDevGlobal;
|
|
2
|
+
import { getDevGlobal } from "@marko/run/adapter";
|
|
4
3
|
|
|
5
4
|
process
|
|
6
5
|
.on("message", (message) => {
|
|
@@ -16,8 +15,12 @@ process
|
|
|
16
15
|
async function start(entry, config) {
|
|
17
16
|
globalThis.__marko_run_vite_config__ = config;
|
|
18
17
|
let changed = false;
|
|
19
|
-
const loader = await createServer({
|
|
20
|
-
|
|
18
|
+
const loader = await createServer({
|
|
19
|
+
...config,
|
|
20
|
+
ssr: { external: ["@marko/run/router"] },
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
await loader.listen(0);
|
|
21
24
|
await loader.ssrLoadModule(entry);
|
|
22
25
|
|
|
23
26
|
loader.watcher.on("change", (path) => {
|
|
@@ -29,11 +32,9 @@ async function start(entry, config) {
|
|
|
29
32
|
}
|
|
30
33
|
|
|
31
34
|
function shutdown() {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
devServer.ws.send({ type: "full-reload" });
|
|
36
|
-
}
|
|
37
|
-
devGlobal.clear();
|
|
35
|
+
const devGlobal = getDevGlobal();
|
|
36
|
+
for (const devServer of devGlobal.devServers) {
|
|
37
|
+
devServer.ws.send({ type: "full-reload" });
|
|
38
38
|
}
|
|
39
|
+
devGlobal.clear();
|
|
39
40
|
}
|
|
@@ -147,7 +147,6 @@ function createMiddleware(fetch2, options = {}) {
|
|
|
147
147
|
const { signal } = controller;
|
|
148
148
|
const url = new URL(req.url, origin || getOrigin(req, trustProxy));
|
|
149
149
|
const ip = req.ip || trustProxy && getForwardedHeader(req, "for") || req.socket.remoteAddress || "";
|
|
150
|
-
const headers = req.headers;
|
|
151
150
|
req.on("error", onErrorOrClose);
|
|
152
151
|
req.socket.on("error", onErrorOrClose);
|
|
153
152
|
res.on("error", onErrorOrClose);
|
|
@@ -192,10 +191,18 @@ function createMiddleware(fetch2, options = {}) {
|
|
|
192
191
|
}
|
|
193
192
|
});
|
|
194
193
|
}
|
|
194
|
+
let body;
|
|
195
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
196
|
+
if (req.readableDidRead) {
|
|
197
|
+
body = bodyConsumedErrorStream;
|
|
198
|
+
} else {
|
|
199
|
+
body = req;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
195
202
|
const request = new Request(url, {
|
|
196
203
|
method: req.method,
|
|
197
|
-
headers,
|
|
198
|
-
body
|
|
204
|
+
headers: req.headers,
|
|
205
|
+
body,
|
|
199
206
|
// @ts-expect-error: Node requires this for streams
|
|
200
207
|
duplex: "half",
|
|
201
208
|
signal
|
|
@@ -247,6 +254,11 @@ async function writeResponse(reader, res, controller) {
|
|
|
247
254
|
controller.abort(err);
|
|
248
255
|
}
|
|
249
256
|
}
|
|
257
|
+
var bodyConsumedErrorStream = new ReadableStream({
|
|
258
|
+
start(controller) {
|
|
259
|
+
controller.error(new Error("The request body stream was already consumed by something before Marko Run."));
|
|
260
|
+
}
|
|
261
|
+
});
|
|
250
262
|
// Annotate the CommonJS export names for ESM import in node:
|
|
251
263
|
0 && (module.exports = {
|
|
252
264
|
createMiddleware,
|
|
@@ -104,7 +104,6 @@ function createMiddleware(fetch2, options = {}) {
|
|
|
104
104
|
const { signal } = controller;
|
|
105
105
|
const url = new URL(req.url, origin || getOrigin(req, trustProxy));
|
|
106
106
|
const ip = req.ip || trustProxy && getForwardedHeader(req, "for") || req.socket.remoteAddress || "";
|
|
107
|
-
const headers = req.headers;
|
|
108
107
|
req.on("error", onErrorOrClose);
|
|
109
108
|
req.socket.on("error", onErrorOrClose);
|
|
110
109
|
res.on("error", onErrorOrClose);
|
|
@@ -149,10 +148,18 @@ function createMiddleware(fetch2, options = {}) {
|
|
|
149
148
|
}
|
|
150
149
|
});
|
|
151
150
|
}
|
|
151
|
+
let body;
|
|
152
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
153
|
+
if (req.readableDidRead) {
|
|
154
|
+
body = bodyConsumedErrorStream;
|
|
155
|
+
} else {
|
|
156
|
+
body = req;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
152
159
|
const request = new Request(url, {
|
|
153
160
|
method: req.method,
|
|
154
|
-
headers,
|
|
155
|
-
body
|
|
161
|
+
headers: req.headers,
|
|
162
|
+
body,
|
|
156
163
|
// @ts-expect-error: Node requires this for streams
|
|
157
164
|
duplex: "half",
|
|
158
165
|
signal
|
|
@@ -204,6 +211,11 @@ async function writeResponse(reader, res, controller) {
|
|
|
204
211
|
controller.abort(err);
|
|
205
212
|
}
|
|
206
213
|
}
|
|
214
|
+
var bodyConsumedErrorStream = new ReadableStream({
|
|
215
|
+
start(controller) {
|
|
216
|
+
controller.error(new Error("The request body stream was already consumed by something before Marko Run."));
|
|
217
|
+
}
|
|
218
|
+
});
|
|
207
219
|
export {
|
|
208
220
|
createMiddleware,
|
|
209
221
|
getOrigin,
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare function logInfoBox(address: string, explorer?: string): void;
|
|
2
|
+
export declare function drawMarkoBox(message: string, options?: LogoOptions): string[];
|
|
3
|
+
export interface LogoOptions {
|
|
4
|
+
fill?: boolean;
|
|
5
|
+
color?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function drawMarkoLogo(options?: LogoOptions): {
|
|
8
|
+
lines: string[];
|
|
9
|
+
width: number;
|
|
10
|
+
};
|