@marko/run 0.5.14 → 0.5.15

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.
@@ -52,7 +52,15 @@ import path from "path";
52
52
  // src/vite/constants.ts
53
53
  var markoRunFilePrefix = "__marko-run__";
54
54
  var virtualFilePrefix = "virtual:marko-run";
55
- var httpVerbs = ["get", "post", "put", "delete"];
55
+ var httpVerbs = [
56
+ "get",
57
+ "head",
58
+ "post",
59
+ "put",
60
+ "delete",
61
+ "patch",
62
+ "options"
63
+ ];
56
64
  var serverEntryQuery = "?marko-server-entry";
57
65
  var RoutableFileTypes = {
58
66
  Page: "page",
@@ -65,17 +73,27 @@ var RoutableFileTypes = {
65
73
  };
66
74
 
67
75
  // src/vite/utils/route.ts
68
- function getVerbs(route) {
69
- var _a, _b;
70
- const verbs = ((_b = (_a = route.handler) == null ? void 0 : _a.verbs) == null ? void 0 : _b.slice()) || [];
71
- if (route.page && !verbs.includes("get")) {
72
- verbs.unshift("get");
76
+ var httpVerbOrder = httpVerbs.reduce(
77
+ (order, verb, index) => {
78
+ order[verb] = index;
79
+ return order;
80
+ },
81
+ {}
82
+ );
83
+ function getVerbs(route, noAutoHead) {
84
+ var _a;
85
+ const verbs = new Set((_a = route.handler) == null ? void 0 : _a.verbs);
86
+ if (route.page) {
87
+ verbs.add("get");
73
88
  }
74
- return verbs;
89
+ if (!noAutoHead && verbs.has("get")) {
90
+ verbs.add("head");
91
+ }
92
+ return [...verbs].sort((a, b) => httpVerbOrder[a] - httpVerbOrder[b]);
75
93
  }
76
94
  function hasVerb(route, verb) {
77
95
  var _a, _b;
78
- return verb === "get" && route.page || ((_b = (_a = route.handler) == null ? void 0 : _a.verbs) == null ? void 0 : _b.includes(verb));
96
+ return verb === "get" && !!route.page || ((_b = (_a = route.handler) == null ? void 0 : _a.verbs) == null ? void 0 : _b.includes(verb)) || verb === "head" && hasVerb(route, "get");
79
97
  }
80
98
 
81
99
  // src/vite/codegen/writer.ts
@@ -279,12 +297,15 @@ function renderRouteEntry(route, entriesDir) {
279
297
  if (handler || middleware.length) {
280
298
  runtimeImports.push("call");
281
299
  }
282
- if (!page || verbs.length > 1) {
300
+ if (!page || verbs.some((verb) => verb !== "get" && verb !== "head")) {
283
301
  runtimeImports.push("noContent");
284
302
  }
285
303
  if (page) {
286
304
  runtimeImports.push("pageResponse");
287
305
  }
306
+ if (verbs.includes("head")) {
307
+ runtimeImports.push("stripResponseBody");
308
+ }
288
309
  if (runtimeImports.length) {
289
310
  imports.writeLines(
290
311
  `import { ${runtimeImports.join(
@@ -328,66 +349,80 @@ function renderRouteEntry(route, entriesDir) {
328
349
  }
329
350
  return writer.end();
330
351
  }
331
- function writePageResponse(writer, wrapFn) {
332
- writer.writeLines(
333
- `${wrapFn ? `const ${wrapFn} = () =>` : `return`} pageResponse(page, buildInput());`
334
- );
335
- }
336
- function writeMiddleware(writer, middleware, next, wrapFn) {
337
- if (wrapFn) {
338
- writer.writeLines(
339
- `const ${wrapFn} = () => call(${middleware}, ${next}, context);`
340
- );
341
- } else {
342
- writer.writeLines(`return call(${middleware}, ${next}, context);`);
343
- }
344
- }
345
352
  function writeRouteEntryHandler(writer, route, verb) {
346
- var _a;
353
+ var _a, _b, _c, _d;
347
354
  const { key, index, page, handler, middleware } = route;
348
355
  const len = middleware.length;
349
356
  let nextName;
350
357
  let currentName;
351
358
  let hasBody = false;
352
359
  writer.writeLines("");
353
- if (page) {
360
+ if (page && (verb === "get" || verb === "head")) {
354
361
  writer.writeBlockStart(
355
- `export async function ${verb}${index}(context, buildInput) {`
362
+ `export function ${verb}${index}(context, buildInput) {`
356
363
  );
357
364
  } else {
358
- writer.writeBlockStart(`export async function ${verb}${index}(context) {`);
365
+ writer.writeBlockStart(`export function ${verb}${index}(context) {`);
359
366
  }
360
367
  const continuations = writer.branch("cont");
361
- if (page && verb === "get") {
368
+ if (page && (verb === "get" || verb === "head")) {
362
369
  currentName = "__page";
363
370
  if ((_a = handler == null ? void 0 : handler.verbs) == null ? void 0 : _a.includes(verb)) {
364
371
  const name = `${verb}Handler`;
365
- writePageResponse(continuations, currentName);
372
+ continuations.writeLines(
373
+ `const ${currentName} = () => pageResponse(page, buildInput());`
374
+ );
366
375
  if (len) {
367
376
  nextName = currentName;
368
377
  currentName = `__${name}`;
369
- writeMiddleware(continuations, name, nextName, currentName);
378
+ continuations.writeLines(
379
+ `const ${currentName} = () => call(${name}, ${nextName}, context);`
380
+ );
370
381
  } else {
371
- writeMiddleware(writer, name, currentName);
382
+ if (verb === "head") {
383
+ writer.writeLines(
384
+ `return stripResponseBody(call(${name}, ${currentName}, context));`
385
+ );
386
+ } else {
387
+ writer.writeLines(`return call(${name}, ${currentName}, context);`);
388
+ }
372
389
  hasBody = true;
373
390
  }
391
+ } else if (verb === "head") {
392
+ writer.writeLines(
393
+ `return stripResponseBody(get${index}(context, buildInput));`
394
+ );
395
+ hasBody = true;
374
396
  } else if (len) {
375
- writePageResponse(continuations, currentName);
397
+ continuations.writeLines(
398
+ `const ${currentName} = () => pageResponse(page, buildInput());`
399
+ );
376
400
  nextName = currentName;
377
401
  } else {
378
- writePageResponse(continuations);
402
+ writer.writeLines(`return pageResponse(page, buildInput());`);
379
403
  hasBody = true;
380
404
  }
381
- } else if (handler) {
405
+ } else if ((_b = handler == null ? void 0 : handler.verbs) == null ? void 0 : _b.includes(verb)) {
382
406
  const name = `${verb}Handler`;
383
407
  currentName = `__${name}`;
384
408
  nextName = "noContent";
385
409
  if (len) {
386
- writeMiddleware(continuations, name, nextName, currentName);
410
+ continuations.writeLines(
411
+ `const ${currentName} = () => call(${name}, ${nextName}, context);`
412
+ );
387
413
  } else {
388
- writeMiddleware(writer, name, nextName);
414
+ if (verb === "head") {
415
+ writer.writeLines(
416
+ `return stripResponseBody(call(${name}, ${nextName}, context));`
417
+ );
418
+ } else {
419
+ writer.writeLines(`return call(${name}, ${nextName}, context);`);
420
+ }
389
421
  hasBody = true;
390
422
  }
423
+ } else if (verb === "head" && ((_d = (_c = route.handler) == null ? void 0 : _c.verbs) == null ? void 0 : _d.includes("get"))) {
424
+ writer.writeLines(`return stripResponseBody(get${index}(context));`);
425
+ hasBody = true;
391
426
  } else {
392
427
  throw new Error(`Route ${key} has no handler for ${verb} requests`);
393
428
  }
@@ -398,7 +433,17 @@ function writeRouteEntryHandler(writer, route, verb) {
398
433
  const name = `mware${id}`;
399
434
  nextName = currentName;
400
435
  currentName = i ? `__${name}` : "";
401
- writeMiddleware(continuations, name, nextName, currentName);
436
+ if (currentName) {
437
+ continuations.writeLines(
438
+ `const ${currentName} = () => call(${name}, ${nextName}, context);`
439
+ );
440
+ } else if (verb === "head") {
441
+ continuations.writeLines(
442
+ `return stripResponseBody(call(${name}, ${nextName}, context));`
443
+ );
444
+ } else {
445
+ continuations.writeLines(`return call(${name}, ${nextName}, context);`);
446
+ }
402
447
  }
403
448
  }
404
449
  continuations.join();
@@ -1295,7 +1340,7 @@ async function buildRoutes(sources) {
1295
1340
  filePath: path5,
1296
1341
  relativePath,
1297
1342
  importPath: `${importPrefix}/${relativePath}`,
1298
- verbs: type === RoutableFileTypes.Page ? ["get"] : void 0
1343
+ verbs: type === RoutableFileTypes.Page ? ["get", "head"] : void 0
1299
1344
  };
1300
1345
  for (const dir of dirs) {
1301
1346
  dir.addFile(file);
@@ -1536,17 +1581,16 @@ import format from "human-format";
1536
1581
  import kleur2 from "kleur";
1537
1582
  var HttpVerbColors = {
1538
1583
  get: kleur2.green,
1584
+ head: kleur2.dim().green,
1539
1585
  post: kleur2.magenta,
1540
1586
  put: kleur2.cyan,
1541
1587
  delete: kleur2.red,
1542
- other: kleur2.white
1543
- };
1544
- var HttpVerbOrder = {
1545
- get: 0,
1546
- post: 1,
1547
- put: 2,
1548
- delete: 3
1588
+ patch: kleur2.yellow,
1589
+ options: kleur2.grey
1549
1590
  };
1591
+ function verbColor(verb) {
1592
+ return verb in HttpVerbColors ? HttpVerbColors[verb] : kleur2.gray;
1593
+ }
1550
1594
  function logRoutesTable(routes, bundle, options) {
1551
1595
  function getRouteChunkName(route) {
1552
1596
  return options.sanitizeFileName(`${route.entryName}.marko`);
@@ -1573,23 +1617,27 @@ function logRoutesTable(routes, bundle, options) {
1573
1617
  });
1574
1618
  for (const route of routes.list) {
1575
1619
  for (const path5 of route.paths) {
1576
- const verbs = getVerbs(route).sort(
1577
- (a, b) => HttpVerbOrder[a] - HttpVerbOrder[b]
1578
- );
1620
+ const verbs = getVerbs(route, true);
1579
1621
  let firstRow = true;
1580
1622
  for (const verb of verbs) {
1581
- let size = "";
1582
1623
  const entryType = [];
1624
+ let size = "";
1625
+ let verbCell = verbColor(verb)(verb.toUpperCase());
1626
+ if (verb === "get" && !verbs.includes("head")) {
1627
+ verbCell += kleur2.dim(`,${verbColor(verb)("HEAD")}`);
1628
+ }
1583
1629
  if (route.handler) {
1584
1630
  entryType.push(kleur2.blue("handler"));
1585
1631
  }
1586
- if (verb === "get" && route.page) {
1632
+ if (route.page && (verb === "get" || verb === "head")) {
1587
1633
  entryType.push(kleur2.yellow("page"));
1588
- size = prettySize(computeRouteSize(getRouteChunkName(route), bundle));
1634
+ if (verb === "get") {
1635
+ size = prettySize(
1636
+ computeRouteSize(getRouteChunkName(route), bundle)
1637
+ );
1638
+ }
1589
1639
  }
1590
- const row = [
1591
- kleur2.bold(HttpVerbColors[verb](verb.toUpperCase()))
1592
- ];
1640
+ const row = [verbCell];
1593
1641
  if (verbs.length === 1 || firstRow) {
1594
1642
  row.push({ rowSpan: verbs.length, content: prettyPath(path5.path) });
1595
1643
  firstRow = false;
@@ -2125,7 +2173,6 @@ function markoRun(opts = {}) {
2125
2173
  },
2126
2174
  async resolveId(importee, importer) {
2127
2175
  if (importee === "@marko/run/router") {
2128
- console.log("plugin resolveId router");
2129
2176
  return path4.resolve(root, ROUTER_FILENAME);
2130
2177
  } else if (importee.endsWith(".marko") && importee.includes(relativeEntryFilesDirPosix)) {
2131
2178
  if (!importee.startsWith(root)) {
@@ -1,3 +1,3 @@
1
1
  import type { HttpVerb, Route } from "../types";
2
- export declare function getVerbs(route: Route): ("get" | "post" | "put" | "delete")[];
3
- export declare function hasVerb(route: Route, verb: HttpVerb): boolean | import("..").RoutableFile | undefined;
2
+ export declare function getVerbs(route: Route, noAutoHead?: boolean): HttpVerb[];
3
+ export declare function hasVerb(route: Route, verb: HttpVerb): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marko/run",
3
- "version": "0.5.14",
3
+ "version": "0.5.15",
4
4
  "description": "The Marko application framework.",
5
5
  "keywords": [
6
6
  "marko"
@@ -51,11 +51,7 @@
51
51
  "dist"
52
52
  ],
53
53
  "scripts": {
54
- "build": "rm -rf ./dist && tsc -b && tsx scripts/build.ts",
55
- "test": "cross-env NODE_ENV=test NODE_OPTIONS='$NODE_OPTIONS --import tsx' mocha \"./src/**/__tests__/*.test.?(c)ts\"",
56
- "test:inspect": "npm test -- --inspect",
57
- "test:update": "npm test -- --update",
58
- "test:watch": "npm test -- --watch"
54
+ "build": "rm -rf ./dist && tsc -b && tsx scripts/build.ts"
59
55
  },
60
56
  "dependencies": {
61
57
  "@marko/run-explorer": "^0.1.2",