@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.
@@ -44,10 +44,232 @@ var __importMetaURL = (0, import_url.pathToFileURL)(__filename);
44
44
 
45
45
  // src/adapter/index.ts
46
46
  var import_path = __toESM(require("path"), 1);
47
+ var import_fs2 = __toESM(require("fs"), 1);
47
48
  var import_url2 = require("url");
48
49
 
49
50
  // src/adapter/dev-server.ts
50
51
  var import_vite = require("vite");
52
+
53
+ // src/adapter/polyfill.ts
54
+ var webStream = __toESM(require("stream/web"), 1);
55
+ var import_crypto = require("crypto");
56
+ var undici = __toESM(require("undici"), 1);
57
+ globalThis.crypto ?? (globalThis.crypto = import_crypto.webcrypto);
58
+ globalThis.fetch ?? (globalThis.fetch = undici.fetch);
59
+ globalThis.Response ?? (globalThis.Response = undici.Response);
60
+ globalThis.Request ?? (globalThis.Request = undici.Request);
61
+ globalThis.Headers ?? (globalThis.Headers = undici.Headers);
62
+ globalThis.ReadableStream ?? (globalThis.ReadableStream = webStream.ReadableStream);
63
+ globalThis.TransformStream ?? (globalThis.TransformStream = webStream.TransformStream);
64
+ globalThis.WritableStream ?? (globalThis.WritableStream = webStream.WritableStream);
65
+ globalThis.FormData ?? (globalThis.FormData = undici.FormData);
66
+ globalThis.File ?? (globalThis.File = undici.File);
67
+
68
+ // src/adapter/middleware.ts
69
+ function getForwardedHeader(req, name) {
70
+ const value = req.headers["x-forwarded-" + name];
71
+ if (value) {
72
+ if (typeof value === "string") {
73
+ const index = value.indexOf(",");
74
+ return index < 0 ? value : value.slice(0, index);
75
+ }
76
+ return value[0];
77
+ }
78
+ }
79
+ function getOrigin(req, trustProxy) {
80
+ const protocol = req.protocol || trustProxy && getForwardedHeader(req, "proto") || (req.socket.encrypted ? "https" : "http");
81
+ let host = req.headers.host || trustProxy && getForwardedHeader(req, "host");
82
+ if (!host) {
83
+ if (process.env.NODE_ENV !== "production") {
84
+ host = "localhost";
85
+ console.warn(
86
+ `Could not automatically determine the origin host, using 'localhost'. Use the 'origin' option or the 'ORIGIN' environment variable to set the origin explicitly.`
87
+ );
88
+ } else {
89
+ throw new Error(
90
+ `Could not automatically determine the origin host. Use the 'origin' option or the 'ORIGIN' environment variable to set the origin explicitly.`
91
+ );
92
+ }
93
+ }
94
+ return `${protocol}://${host}`;
95
+ }
96
+ var inExpiresDateRgs = /Expires\s*=\s*(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*$/i;
97
+ function setResponseHeaders(response, res) {
98
+ for (const [key, value] of response.headers) {
99
+ if (key !== "set-cookie") {
100
+ res.setHeader(key, value);
101
+ }
102
+ }
103
+ const setCookies = getSetCookie(response.headers);
104
+ if (setCookies == null ? void 0 : setCookies.length) {
105
+ res.setHeader("set-cookie", setCookies);
106
+ }
107
+ }
108
+ var getSetCookie = Headers.prototype.getSetCookie ? getSetCookie_platform : getSetCookie_fallback;
109
+ function getSetCookie_platform(headers) {
110
+ return headers.getSetCookie();
111
+ }
112
+ function getSetCookie_fallback(headers) {
113
+ const value = headers.get("set-cookie");
114
+ if (!value)
115
+ return void 0;
116
+ let sepIndex = value.indexOf(",") + 1;
117
+ if (!sepIndex)
118
+ return value;
119
+ let index = 0;
120
+ let setCookie = void 0;
121
+ let setCookies = void 0;
122
+ do {
123
+ const valuePart = value.slice(index, sepIndex - 1);
124
+ if (!inExpiresDateRgs.test(valuePart)) {
125
+ if (setCookies) {
126
+ setCookies.push(valuePart);
127
+ } else if (setCookie) {
128
+ setCookies = [setCookie, valuePart];
129
+ } else {
130
+ setCookie = valuePart;
131
+ }
132
+ index = sepIndex;
133
+ while (value.charCodeAt(index) === 32)
134
+ index++;
135
+ }
136
+ sepIndex = value.indexOf(",", sepIndex) + 1;
137
+ } while (sepIndex);
138
+ if (index) {
139
+ const valuePart = value.slice(index);
140
+ if (setCookies) {
141
+ setCookies.push(valuePart);
142
+ return setCookies;
143
+ }
144
+ return [setCookie, valuePart];
145
+ }
146
+ return value;
147
+ }
148
+ function createMiddleware(fetch2, options = {}) {
149
+ const {
150
+ origin = process.env.ORIGIN,
151
+ trustProxy = process.env.TRUST_PROXY === "1"
152
+ } = options;
153
+ return async (req, res, next) => {
154
+ var _a;
155
+ const controller = new AbortController();
156
+ const { signal } = controller;
157
+ const url = new URL(req.url, origin || getOrigin(req, trustProxy));
158
+ const ip = req.ip || trustProxy && getForwardedHeader(req, "for") || req.socket.remoteAddress || "";
159
+ req.on("error", onErrorOrClose);
160
+ req.socket.on("error", onErrorOrClose);
161
+ res.on("error", onErrorOrClose);
162
+ res.on("close", onErrorOrClose);
163
+ signal.addEventListener("abort", onSignalAborted);
164
+ function onErrorOrClose(err) {
165
+ req.off("error", onErrorOrClose);
166
+ req.socket.off("error", onErrorOrClose);
167
+ res.off("error", onErrorOrClose);
168
+ res.off("close", onErrorOrClose);
169
+ if (err) {
170
+ signal.removeEventListener("abort", onSignalAborted);
171
+ controller.abort(err);
172
+ }
173
+ }
174
+ function onSignalAborted() {
175
+ if (next) {
176
+ next(signal.reason);
177
+ } else {
178
+ if (!res.destroyed && res.socket) {
179
+ res.socket.destroySoon();
180
+ }
181
+ console.error(signal.reason);
182
+ }
183
+ }
184
+ let setDevClientId;
185
+ if (process.env.NODE_ENV !== "production" && globalThis.__marko_run_dev__ && ((_a = req.headers.accept) == null ? void 0 : _a.includes("text/html"))) {
186
+ setDevClientId = globalThis.__marko_run_dev__.onClient((ws) => {
187
+ if (signal.aborted) {
188
+ sendError();
189
+ } else {
190
+ signal.addEventListener("abort", sendError);
191
+ }
192
+ function sendError() {
193
+ const { message, stack = "" } = signal.reason;
194
+ ws.send(
195
+ JSON.stringify({
196
+ type: "error",
197
+ err: { message, stack }
198
+ })
199
+ );
200
+ }
201
+ });
202
+ }
203
+ let body;
204
+ if (req.method !== "GET" && req.method !== "HEAD") {
205
+ if (req.readableDidRead) {
206
+ body = bodyConsumedErrorStream;
207
+ } else {
208
+ body = req;
209
+ }
210
+ }
211
+ const request = new Request(url, {
212
+ method: req.method,
213
+ headers: req.headers,
214
+ body,
215
+ // @ts-expect-error: Node requires this for streams
216
+ duplex: "half",
217
+ signal
218
+ });
219
+ const response = await fetch2(request, {
220
+ ip,
221
+ request: req,
222
+ response: res
223
+ });
224
+ if (!response) {
225
+ if (next) {
226
+ next();
227
+ }
228
+ return;
229
+ }
230
+ if (process.env.NODE_ENV !== "production" && setDevClientId) {
231
+ setDevClientId(response);
232
+ }
233
+ res.statusCode = response.status;
234
+ setResponseHeaders(response, res);
235
+ if (!response.body) {
236
+ if (!response.headers.has("content-length")) {
237
+ res.setHeader("content-length", "0");
238
+ }
239
+ res.end();
240
+ return;
241
+ } else if (res.destroyed) {
242
+ controller.abort(new Error("Response stream destroyed"));
243
+ return;
244
+ }
245
+ writeResponse(response.body.getReader(), res, controller);
246
+ };
247
+ }
248
+ async function writeResponse(reader, res, controller) {
249
+ try {
250
+ while (!controller.signal.aborted) {
251
+ const { done, value } = await reader.read();
252
+ if (done) {
253
+ res.end();
254
+ return;
255
+ } else if (!res.write(value)) {
256
+ res.once("drain", () => writeResponse(reader, res, controller));
257
+ return;
258
+ } else if (res.flush) {
259
+ res.flush();
260
+ }
261
+ }
262
+ } catch (err) {
263
+ controller.abort(err);
264
+ }
265
+ }
266
+ var bodyConsumedErrorStream = new ReadableStream({
267
+ start(controller) {
268
+ controller.error(new Error("The request body stream was already consumed by something before Marko Run."));
269
+ }
270
+ });
271
+
272
+ // src/adapter/dev-server.ts
51
273
  var import_strip_ansi = __toESM(require("strip-ansi"), 1);
52
274
  function createViteDevMiddleware(devServer, load, factory) {
53
275
  let value;
@@ -82,22 +304,25 @@ async function createViteDevServer(config2) {
82
304
  }
83
305
  async function createDevServer(config2) {
84
306
  const devServer = await createViteDevServer(config2);
85
- const { createMiddleware } = await devServer.ssrLoadModule(
86
- "@marko/run/adapter/middleware"
87
- );
88
- let fetch;
89
- const nodeMiddleware = createMiddleware(
90
- (request, platform) => fetch(request, platform)
307
+ const routerMiddleware = createMiddleware(
308
+ (request, platform) => globalThis.__marko_run__.fetch(request, platform)
91
309
  );
92
- const middleware = createViteDevMiddleware(
93
- devServer,
94
- async () => await devServer.ssrLoadModule("@marko/run/router"),
95
- (module2) => {
96
- fetch = module2.fetch;
97
- return nodeMiddleware;
98
- }
99
- );
100
- devServer.middlewares.use(middleware);
310
+ devServer.middlewares.use(async (req, res, next) => {
311
+ await devServer.ssrLoadModule("@marko/run/router");
312
+ routerMiddleware(req, res, (err) => {
313
+ if (err) {
314
+ res.statusCode = 500;
315
+ if (err instanceof Error) {
316
+ devServer.ssrFixStacktrace(err);
317
+ res.end(err.stack && (0, import_strip_ansi.default)(err.stack));
318
+ } else {
319
+ res.end();
320
+ }
321
+ } else {
322
+ next == null ? void 0 : next();
323
+ }
324
+ });
325
+ });
101
326
  return devServer;
102
327
  }
103
328
  var ClientIdCookieName = "marko-run-client-id";
@@ -168,6 +393,153 @@ function getDevGlobal() {
168
393
  return devGlobal;
169
394
  }
170
395
 
396
+ // src/adapter/utils.ts
397
+ var import_supports_color = __toESM(require("supports-color"), 1);
398
+ var import_kleur = __toESM(require("kleur"), 1);
399
+ function logInfoBox(address, explorer) {
400
+ const color = !!import_supports_color.default.stdout;
401
+ let message = import_kleur.default.bold("Marko Run");
402
+ if (true) {
403
+ message += ` v${"0.2.9"}`;
404
+ }
405
+ message += "\n\n";
406
+ message += import_kleur.default.dim("Server listening at");
407
+ message += "\n";
408
+ message += import_kleur.default.cyan(import_kleur.default.underline(address));
409
+ if (explorer) {
410
+ message += "\n\n";
411
+ message += import_kleur.default.dim("Explore your routes at");
412
+ message += "\n";
413
+ message += import_kleur.default.dim(import_kleur.default.green(import_kleur.default.underline(explorer)));
414
+ }
415
+ const lines = drawMarkoBox(message, { color, fill: color });
416
+ console.log(lines.join("\n"));
417
+ }
418
+ function drawMarkoBox(message, options) {
419
+ const textPaddingWidth = 3;
420
+ const logoPaddingWidth = 2;
421
+ const textPadding = " ".repeat(textPaddingWidth);
422
+ const logoPadding = " ".repeat(logoPaddingWidth);
423
+ const logo = drawMarkoLogo(options);
424
+ const textLines = message.split(/\n/);
425
+ const textWidths = textLines.map(
426
+ (line) => line.replace(/\x1b\[\d+m/g, "").length
427
+ );
428
+ const textWidth = Math.max(...textWidths);
429
+ const height = Math.max(textLines.length + 2, logo.lines.length);
430
+ const width = textPaddingWidth * 2 + logoPaddingWidth + textWidth + logo.width;
431
+ const hBorder = "\u2500".repeat(width);
432
+ const vBorder = "\u2502";
433
+ const lineDiff = logo.lines.length - textLines.length;
434
+ const textStartLine = lineDiff > 0 ? Math.max(Math.floor(lineDiff / 2), 1) : 1;
435
+ const textEndLine = height - (lineDiff > 0 ? Math.ceil(lineDiff / 2) : 1);
436
+ const logoEndLine = logo.lines.length;
437
+ const logoFill = " ".repeat(logo.width);
438
+ const textFill = " ".repeat(textWidth);
439
+ const lines = [`\u256D${hBorder}\u256E`];
440
+ for (let i = 0; i < height; i++) {
441
+ let line = vBorder;
442
+ line += logoPadding;
443
+ if (i < logoEndLine) {
444
+ line += logo.lines[i];
445
+ } else {
446
+ line += logoFill;
447
+ }
448
+ line += textPadding;
449
+ if (i >= textStartLine && i < textEndLine) {
450
+ let index = i - textStartLine;
451
+ line += textLines[index];
452
+ line += " ".repeat(textWidth - textWidths[index]);
453
+ } else {
454
+ line += textFill;
455
+ }
456
+ line += textPadding;
457
+ line += vBorder;
458
+ lines.push(line);
459
+ }
460
+ lines.push(`\u2570${hBorder}\u256F`);
461
+ return lines;
462
+ }
463
+ function drawMarkoLogo(options = {}) {
464
+ const { fill = true, color = true } = options;
465
+ const source = `
466
+ TT____ YY____ R____
467
+ C\u2571T\u2572 \u2572G\u2571Y\u2572 \u2572 R\u2572 \u2572
468
+ C\u2571 T\u2572 G\u2571 Y\u2572 \u2572 R\u2572 \u2572
469
+ C\u2571 \u2571T\u2572G\u2571 \u2571Y\u2572 \u2572 R\u2572 \u2572
470
+ B\u2572 \u2572 GG\u203E\u203E\u203E\u203E O\u2571 \u2571 P\u2571 \u2571
471
+ B\u2572 \u2572 OOO\u2571 \u2571 P\u2571 \u2571
472
+ B\u2572 \u2572 OOO\u2571 \u2571 P\u2571 \u2571
473
+ B\u203E\u203E\u203E\u203E OOO\u203E\u203E\u203E\u203E P\u203E\u203E\u203E\u203E
474
+ `;
475
+ const resetEscape = "\x1B[0m";
476
+ const colorEscapeCodes = Object.entries({
477
+ B: "#06cfe5",
478
+ C: "#05a5f0",
479
+ T: "#19d89c",
480
+ G: "#81dc09",
481
+ Y: "#ffd900",
482
+ O: "#ff9500",
483
+ R: "#f3154d",
484
+ P: "#ce176c"
485
+ }).reduce((acc, [key, hex]) => {
486
+ const r = parseInt(hex.slice(1, 3), 16);
487
+ const g = parseInt(hex.slice(3, 5), 16);
488
+ const b = parseInt(hex.slice(5, 7), 16);
489
+ acc[key] = `\x1B[38;2;${r};${g};${b}m`;
490
+ return acc;
491
+ }, {});
492
+ const lines = [];
493
+ const lineWidths = [];
494
+ let line = "";
495
+ let lineWidth = 0;
496
+ let width = 0;
497
+ for (let i = 0; i < source.length; i++) {
498
+ let char = source[i];
499
+ if (char === "\n") {
500
+ if (line) {
501
+ if (color) {
502
+ line += resetEscape;
503
+ }
504
+ width = Math.max(lineWidth, width);
505
+ lines.push(line);
506
+ lineWidths.push(lineWidth);
507
+ line = "";
508
+ lineWidth = 0;
509
+ }
510
+ } else if (/[A-Z]/.test(char)) {
511
+ while (source[i + 1] === char)
512
+ i++;
513
+ if (color) {
514
+ line += colorEscapeCodes[char];
515
+ }
516
+ if (fill) {
517
+ let fillChar = "";
518
+ for (; i < source.length; i++) {
519
+ char = source[i + 1];
520
+ if (fillChar && char !== " ") {
521
+ break;
522
+ } else if (!fillChar) {
523
+ fillChar = char;
524
+ }
525
+ line += fillChar;
526
+ lineWidth++;
527
+ }
528
+ }
529
+ } else {
530
+ line += char;
531
+ lineWidth++;
532
+ }
533
+ }
534
+ for (let i = 0; i < lines.length; i++) {
535
+ const padding = width - lineWidths[i];
536
+ if (padding > 0) {
537
+ lines[i] += " ".repeat(width - lineWidths[i]);
538
+ }
539
+ }
540
+ return { lines, width };
541
+ }
542
+
171
543
  // src/vite/utils/server.ts
172
544
  var import_net = __toESM(require("net"), 1);
173
545
  var import_child_process = __toESM(require("child_process"), 1);
@@ -311,7 +683,14 @@ function sleep(ms) {
311
683
  }
312
684
 
313
685
  // src/adapter/index.ts
686
+ var import_module = require("module");
314
687
  var import_parse_node_args = __toESM(require("parse-node-args"), 1);
688
+
689
+ // src/vite/constants.ts
690
+ var markoRunFilePrefix = "__marko-run__";
691
+ var virtualFilePrefix = "virtual:marko-run";
692
+
693
+ // src/adapter/index.ts
315
694
  var __dirname = import_path.default.dirname((0, import_url2.fileURLToPath)(__importMetaURL));
316
695
  var defaultEntry = import_path.default.join(__dirname, "default-entry");
317
696
  var loadDevWorker = import_path.default.join(__dirname, "load-dev-worker.mjs");
@@ -323,6 +702,8 @@ function adapter() {
323
702
  },
324
703
  async startDev(entry, config2, options) {
325
704
  const { port = 3e3, envFile } = options;
705
+ globalThis.__marko_run_vite_config__ = config2;
706
+ const explorerPromise = startExplorer();
326
707
  if (entry) {
327
708
  const { nodeArgs } = (0, import_parse_node_args.default)(options.args);
328
709
  let worker;
@@ -353,39 +734,99 @@ function adapter() {
353
734
  }
354
735
  worker = nextWorker;
355
736
  }
356
- await start();
737
+ const [explorer2] = await Promise.all([explorerPromise, start()]);
357
738
  return {
358
739
  port,
359
- close() {
360
- worker.kill();
740
+ async close() {
741
+ await Promise.all([worker.kill(), explorer2 == null ? void 0 : explorer2.close()]);
361
742
  }
362
743
  };
363
744
  }
364
745
  const devServer = await createDevServer(config2);
365
746
  envFile && await loadEnv(envFile);
366
- return new Promise((resolve) => {
747
+ const listen = new Promise((resolve) => {
367
748
  const listener = devServer.middlewares.listen(port, () => {
368
- const address = listener.address();
369
- console.log(`Dev server started: http://localhost:${address.port}`);
370
- resolve({
371
- port,
372
- async close() {
373
- await devServer.close();
374
- }
375
- });
749
+ resolve(listener.address());
376
750
  });
377
751
  });
752
+ const [explorer, address] = await Promise.all([explorerPromise, listen]);
753
+ logInfoBox(
754
+ `http://localhost:${address.port}`,
755
+ explorer && `http://localhost:${explorer.port}`
756
+ );
757
+ return {
758
+ port: address.port,
759
+ async close() {
760
+ await Promise.all([devServer.close(), explorer == null ? void 0 : explorer.close()]);
761
+ }
762
+ };
378
763
  },
379
764
  async startPreview(entry, options) {
380
765
  const { port = 3e3, envFile } = options;
381
766
  const { nodeArgs } = (0, import_parse_node_args.default)(options.args);
382
767
  const args = [...nodeArgs, entry];
383
768
  const server = await spawnServer("node", args, port, envFile);
384
- console.log(`Preview server started: http://localhost:${server.port}`);
769
+ if (!options.sourceEntry) {
770
+ logInfoBox(`http://localhost:${port}`);
771
+ }
385
772
  return server;
773
+ },
774
+ async routesGenerated(routes, virtualFiles, meta) {
775
+ if (process.env.MR_EXPLORER !== "true") {
776
+ return;
777
+ }
778
+ const promises = [];
779
+ const cacheDir = import_path.default.resolve(__dirname, "../../.cache/explorer");
780
+ const codeDir = import_path.default.join(cacheDir, "code");
781
+ if (import_fs2.default.existsSync(codeDir)) {
782
+ await import_fs2.default.promises.rm(codeDir, { recursive: true });
783
+ }
784
+ await import_fs2.default.promises.mkdir(codeDir, { recursive: true });
785
+ const data = {
786
+ meta,
787
+ routes: {},
788
+ files: {}
789
+ };
790
+ for (const [name, code] of virtualFiles) {
791
+ let fileName = "";
792
+ let index = name.indexOf(markoRunFilePrefix);
793
+ if (index >= 0) {
794
+ fileName = name.slice(index);
795
+ data.files[fileName] = `${virtualFilePrefix}/${fileName}`;
796
+ } else if (name.startsWith("@marko/run")) {
797
+ fileName = name.slice(11);
798
+ data.files[fileName] = name;
799
+ }
800
+ if (fileName) {
801
+ promises.push(
802
+ import_fs2.default.promises.writeFile(import_path.default.join(codeDir, fileName), code, {})
803
+ );
804
+ }
805
+ }
806
+ for (const route of routes.list) {
807
+ data.routes[route.index] = route;
808
+ }
809
+ for (const [id, route] of Object.entries(routes.special)) {
810
+ data.routes["s" + id] = route;
811
+ }
812
+ promises.push(
813
+ import_fs2.default.promises.writeFile(
814
+ import_path.default.join(cacheDir, "data.json"),
815
+ JSON.stringify(data),
816
+ {}
817
+ )
818
+ );
819
+ await Promise.all(promises);
386
820
  }
387
821
  };
388
822
  }
823
+ var require2 = (0, import_module.createRequire)(__importMetaURL);
824
+ async function startExplorer() {
825
+ if (process.env.MR_EXPLORER === "true") {
826
+ const entry = require2.resolve("@marko/run-explorer");
827
+ return await spawnServer("node", [entry], 1234);
828
+ }
829
+ }
389
830
  // Annotate the CommonJS export names for ESM import in node:
390
831
  0 && (module.exports = {
391
832
  createDevServer,
@@ -1,7 +1,7 @@
1
1
  import type { Adapter } from "../vite";
2
2
  import { type MarkoRunDev } from "./dev-server";
3
3
  import { type SpawnedServer } from "../vite/utils/server";
4
- export { getDevGlobal, createDevServer, createViteDevServer, createViteDevMiddleware, type MarkoRunDev } from "./dev-server";
4
+ export { getDevGlobal, createDevServer, createViteDevServer, createViteDevMiddleware, type MarkoRunDev, } from "./dev-server";
5
5
  export type { Adapter, SpawnedServer };
6
6
  export type { NodePlatformInfo } from "./middleware";
7
7
  export type MarkoRunDevAccessor = () => MarkoRunDev;