bun-plugin-stx 0.2.905 → 0.2.908

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.
@@ -19,6 +19,22 @@ function extractLayoutMetadata(html) {
19
19
  group: groupMeta?.[1] || deriveLayoutGroup(layout)
20
20
  };
21
21
  }
22
+ var HAVE_LAYOUT_HEADER = "X-STX-Have-Layout";
23
+ var HAVE_LAYOUT_GROUP_HEADER = "X-STX-Have-Layout-Group";
24
+ function layoutChanges(have, next) {
25
+ const haveGroup = have.group || deriveLayoutGroup(have.layout);
26
+ const nextGroup = next.group || deriveLayoutGroup(next.layout);
27
+ if (haveGroup !== nextGroup)
28
+ return true;
29
+ return Boolean(have.layout && next.layout && have.layout !== next.layout);
30
+ }
31
+ function layoutTheRouterHas(headers) {
32
+ const layout = headers.get(HAVE_LAYOUT_HEADER);
33
+ const group = headers.get(HAVE_LAYOUT_GROUP_HEADER);
34
+ if (layout === null && group === null)
35
+ return null;
36
+ return { layout: layout ?? "", group: group ?? "" };
37
+ }
22
38
 
23
39
  // ../router/src/file-requests.ts
24
40
  var FILE_REQUEST_EXTENSIONS = Object.freeze([
@@ -122,4 +138,4 @@ function isFileRequestPath(pathname) {
122
138
  return last.length > 0 && FILE_SEGMENT.test(last);
123
139
  }
124
140
 
125
- export { extractLayoutMetadata, FILE_SEGMENT_GUARD, isFileRequestPath };
141
+ export { extractLayoutMetadata, layoutChanges, layoutTheRouterHas, FILE_SEGMENT_GUARD, isFileRequestPath };
@@ -1,7 +1,7 @@
1
1
  // @bun
2
2
  import {
3
3
  FILE_SEGMENT_GUARD
4
- } from "./chunk-fqkc01ey.js";
4
+ } from "./chunk-myahzbp9.js";
5
5
 
6
6
  // ../router/src/file-router.ts
7
7
  import fs3 from "fs";
package/dist/serve.d.ts CHANGED
@@ -341,6 +341,8 @@ export declare const MODULE_BUNDLE_PATH: unknown;
341
341
  export declare const DEFAULT_IMAGE_WARMUP_BIND_BUDGET_MS: unknown;
342
342
  /** The header every page warm-up request carries. */
343
343
  export declare const PAGE_WARMUP_HEADER: 'x-stx-warmup';
344
+ /** Where a server answers what its page warm-up did. */
345
+ export declare const PAGE_WARMUP_REPORT_PATH: '/_stx/warmup';
344
346
  /** Default for `pageWarmup.budgetMs`: beside the image budget, inside ts-cloud's 180s bind window. */
345
347
  export declare const DEFAULT_PAGE_WARMUP_BUDGET_MS: unknown;
346
348
  /**
@@ -445,6 +447,7 @@ export declare interface PageWarmupOptions {
445
447
  holdBind?: 'replacing' | 'always'
446
448
  budgetMs?: number
447
449
  concurrency?: number
450
+ report?: boolean
448
451
  }
449
452
  export declare interface ResolvedPageWarmup {
450
453
  enabled: boolean
@@ -452,6 +455,29 @@ export declare interface ResolvedPageWarmup {
452
455
  holdBind: 'replacing' | 'always'
453
456
  budgetMs: number
454
457
  concurrency: number
458
+ report: boolean
459
+ }
460
+ /**
461
+ * What the page warm-up did, as `GET /_stx/warmup` answers it.
462
+ *
463
+ * The warm-up used to say so only in the web process's own log, which a
464
+ * deploy cannot read: a release with a 90 s budget and 19 configured paths
465
+ * could not tell from CI whether they were all warm before it took traffic.
466
+ * Only counts and paths the project configured, so it is answered publicly;
467
+ * `pageWarmup.report: false` turns it off.
468
+ */
469
+ export declare interface PageWarmupReport {
470
+ enabled: boolean
471
+ running: boolean
472
+ held: boolean
473
+ warmed: number
474
+ failed: number
475
+ total: number
476
+ seconds: number
477
+ stoppedAtBudget: boolean
478
+ configured: number
479
+ configuredReached: number
480
+ unreached: string[]
455
481
  }
456
482
  /**
457
483
  * The slice of Css this server calls, taken from the package's own types
package/dist/serve.js CHANGED
@@ -2,9 +2,11 @@
2
2
  // @bun
3
3
  import {
4
4
  extractLayoutMetadata,
5
+ layoutChanges,
6
+ layoutTheRouterHas,
5
7
  FILE_SEGMENT_GUARD,
6
8
  isFileRequestPath
7
- } from "./chunk-fqkc01ey.js";
9
+ } from "./chunk-myahzbp9.js";
8
10
 
9
11
  // src/serve.ts
10
12
  var {serve: bunServe, Glob } = globalThis.Bun;
@@ -9847,9 +9849,10 @@ async function portIsServed(port, timeoutMs = 500) {
9847
9849
  }
9848
9850
  }
9849
9851
  var PAGE_WARMUP_HEADER = "x-stx-warmup";
9852
+ var PAGE_WARMUP_REPORT_PATH = "/_stx/warmup";
9850
9853
  var DEFAULT_PAGE_WARMUP_BUDGET_MS = 60000;
9851
9854
  function resolvePageWarmup(value) {
9852
- const defaults = { enabled: true, paths: [], holdBind: "replacing", budgetMs: DEFAULT_PAGE_WARMUP_BUDGET_MS, concurrency: 4 };
9855
+ const defaults = { enabled: true, paths: [], holdBind: "replacing", budgetMs: DEFAULT_PAGE_WARMUP_BUDGET_MS, concurrency: 4, report: true };
9853
9856
  if (value === false)
9854
9857
  return { ...defaults, enabled: false };
9855
9858
  if (!value || typeof value !== "object")
@@ -9859,7 +9862,7 @@ function resolvePageWarmup(value) {
9859
9862
  const budgetMs = typeof raw.budgetMs === "number" && Number.isFinite(raw.budgetMs) && raw.budgetMs >= 0 ? raw.budgetMs : defaults.budgetMs;
9860
9863
  const concurrency = typeof raw.concurrency === "number" && Number.isFinite(raw.concurrency) && raw.concurrency >= 1 ? Math.floor(raw.concurrency) : defaults.concurrency;
9861
9864
  const holdBind = raw.holdBind === "always" ? "always" : defaults.holdBind;
9862
- return { enabled: true, paths, holdBind, budgetMs, concurrency };
9865
+ return { enabled: true, paths, holdBind, budgetMs, concurrency, report: raw.report !== false };
9863
9866
  }
9864
9867
  async function settleWithin(promise, ms) {
9865
9868
  let timer;
@@ -9930,6 +9933,25 @@ async function serve(options) {
9930
9933
  const imageWarmupGraceMs = Math.max(0, options.imageWarmupGraceMs ?? 1000);
9931
9934
  const imageWarmupBindBudgetMs = resolveImageWarmupBindBudget(options.imageWarmupBindBudgetMs ?? stxConfig.imageWarmupBindBudgetMs);
9932
9935
  const pageWarmup = resolvePageWarmup(options.pageWarmup ?? stxConfig.pageWarmup);
9936
+ const warmupState = { running: false, held: false, warmed: 0, failed: 0, stoppedAtBudget: false, startedAt: 0, finishedAt: 0, routes: [], next: 0 };
9937
+ const pageWarmupReport = () => {
9938
+ const configured = pageWarmup.paths;
9939
+ const started = new Set(warmupState.routes.slice(0, warmupState.next));
9940
+ const end = warmupState.running ? performance.now() : warmupState.finishedAt;
9941
+ return {
9942
+ enabled: production && pageWarmup.enabled,
9943
+ running: warmupState.running,
9944
+ held: warmupState.held,
9945
+ warmed: warmupState.warmed,
9946
+ failed: warmupState.failed,
9947
+ total: warmupState.routes.length,
9948
+ seconds: warmupState.startedAt ? Math.round(Math.max(0, end - warmupState.startedAt)) / 1000 : 0,
9949
+ stoppedAtBudget: warmupState.stoppedAtBudget,
9950
+ configured: configured.length,
9951
+ configuredReached: configured.filter((routePath) => started.has(routePath)).length,
9952
+ unreached: warmupState.stoppedAtBudget ? warmupState.routes.slice(warmupState.next) : []
9953
+ };
9954
+ };
9933
9955
  function awaitImageWarmup() {
9934
9956
  if (placeholdersAreReady || imageWarmupGraceMs === 0)
9935
9957
  return;
@@ -10572,7 +10594,7 @@ function __stxOverlay(errs){
10572
10594
  rebuildDiscoveredPagePaths(kept);
10573
10595
  await indexPageMiddleware(kept);
10574
10596
  try {
10575
- const { Router } = await import("./chunk-zv29b5s3.js");
10597
+ const { Router } = await import("./chunk-tbg4xbed.js");
10576
10598
  const pagesDirs = patterns.map((p) => p.replace(/\/$/, "")).filter(Boolean);
10577
10599
  const router = new Router(process20.cwd(), { pagesDirs, stateDir: stateDirName(), exclude: options.exclude });
10578
10600
  if (!options.quiet)
@@ -11333,6 +11355,8 @@ ${output}`;
11333
11355
  const malformed = malformedRequestResponse(req);
11334
11356
  if (malformed)
11335
11357
  return malformed;
11358
+ if (pageWarmup.report && (req.method === "GET" || req.method === "HEAD") && new URL(req.url).pathname === PAGE_WARMUP_REPORT_PATH)
11359
+ return Response.json(pageWarmupReport(), { headers: { "Cache-Control": "no-store" } });
11336
11360
  const warmup = awaitImageWarmup();
11337
11361
  if (warmup)
11338
11362
  await warmup;
@@ -11752,7 +11776,17 @@ ${output}`;
11752
11776
  if (content) {
11753
11777
  const responseStatus = reqCtx.responseStatus ?? 200;
11754
11778
  const isSpaNav = req.headers.get(SPA_NAV_HEADER) === "true";
11779
+ let layoutAcross = null;
11755
11780
  if (isSpaNav) {
11781
+ const routerHas = layoutTheRouterHas(req.headers);
11782
+ if (routerHas) {
11783
+ const page = extractLayoutMetadata(content);
11784
+ const group = i18nConfig && i18nLocale ? `i18n:${i18nLocale}` : page.group;
11785
+ if (layoutChanges(routerHas, { layout: page.layout, group }))
11786
+ layoutAcross = { layout: page.layout, group };
11787
+ }
11788
+ }
11789
+ if (isSpaNav && !layoutAcross) {
11756
11790
  const layoutMetadata = extractLayoutMetadata(content);
11757
11791
  const pageLayout = layoutMetadata.layout;
11758
11792
  const pageLayoutGroup = i18nConfig && i18nLocale ? `i18n:${i18nLocale}` : layoutMetadata.group;
@@ -11917,6 +11951,11 @@ ${trailingPageScripts.join(`
11917
11951
  pageHeaders.set(name, value);
11918
11952
  for (const cookie of reqCtx.actionCookies ?? [])
11919
11953
  pageHeaders.append("Set-Cookie", cookie);
11954
+ if (layoutAcross) {
11955
+ pageHeaders.set("X-STX-Layout", layoutAcross.layout);
11956
+ pageHeaders.set("X-STX-Layout-Group", layoutAcross.group);
11957
+ pageHeaders.set(BUILD_ID_HEADER, getBuildId());
11958
+ }
11920
11959
  return new Response(injectHmrClient(cleaned), {
11921
11960
  status: pageRenderFailed ? 500 : responseStatus,
11922
11961
  headers: pageHeaders
@@ -12090,20 +12129,34 @@ ${trailingPageScripts.join(`
12090
12129
  }
12091
12130
  if (production && pageWarmup.enabled) {
12092
12131
  const holding = pageWarmup.holdBind === "always" || options.reusePort === true && await portIsServed(port);
12132
+ warmupState.held = holding;
12093
12133
  if (holding) {
12094
12134
  const warmupStartedAt = performance.now();
12135
+ warmupState.startedAt = warmupStartedAt;
12136
+ warmupState.running = true;
12095
12137
  const warmup = { stop: false };
12096
12138
  const warming = warmPages(warmup);
12097
12139
  if (!await settleWithin(warming, pageWarmup.budgetMs)) {
12098
12140
  warmup.stop = true;
12141
+ warmupState.stoppedAtBudget = true;
12099
12142
  console.warn(`[stx] page warm-up stopped at its ${formatSeconds(pageWarmup.budgetMs)} budget; binding now, and the pages left render on their first visit.`);
12100
12143
  }
12101
12144
  warming.then(({ warmed, pages, failed }) => {
12145
+ warmupState.running = false;
12146
+ warmupState.finishedAt = performance.now();
12102
12147
  console.log(`[stx] warmed ${warmed} of ${pages} pages in ${formatSeconds(performance.now() - warmupStartedAt)}${failed ? ` (${failed} answered 5xx or failed)` : ""}`);
12103
12148
  });
12104
12149
  }
12105
12150
  }
12106
12151
  async function warmPages(control) {
12152
+ try {
12153
+ return await warmEveryPage(control);
12154
+ } catch (error) {
12155
+ warmupState.running = false;
12156
+ throw error;
12157
+ }
12158
+ }
12159
+ async function warmEveryPage(control) {
12107
12160
  const files = await discoverFiles();
12108
12161
  const routes = [...new Set([
12109
12162
  "/",
@@ -12112,13 +12165,12 @@ ${trailingPageScripts.join(`
12112
12165
  ...discoveredPagePaths ?? []
12113
12166
  ])];
12114
12167
  const warm = listen(0, false, "127.0.0.1");
12115
- let next = 0;
12116
- let warmed = 0;
12117
- let failed = 0;
12168
+ warmupState.routes = routes;
12169
+ warmupState.next = 0;
12118
12170
  try {
12119
12171
  await Promise.all(Array.from({ length: Math.min(pageWarmup.concurrency, routes.length) }, async () => {
12120
- while (next < routes.length && !control.stop) {
12121
- const routePath = routes[next++];
12172
+ while (warmupState.next < routes.length && !control.stop) {
12173
+ const routePath = routes[warmupState.next++];
12122
12174
  try {
12123
12175
  const response = await fetch(`http://127.0.0.1:${warm.port}${routePath}`, {
12124
12176
  headers: { [PAGE_WARMUP_HEADER]: "1", Accept: "text/html" },
@@ -12126,18 +12178,18 @@ ${trailingPageScripts.join(`
12126
12178
  });
12127
12179
  await response.arrayBuffer();
12128
12180
  if (response.status >= 500)
12129
- failed++;
12181
+ warmupState.failed++;
12130
12182
  else
12131
- warmed++;
12183
+ warmupState.warmed++;
12132
12184
  } catch {
12133
- failed++;
12185
+ warmupState.failed++;
12134
12186
  }
12135
12187
  }
12136
12188
  }));
12137
12189
  } finally {
12138
12190
  warm.stop(true);
12139
12191
  }
12140
- return { warmed, pages: routes.length, failed };
12192
+ return { warmed: warmupState.warmed, pages: routes.length, failed: warmupState.failed };
12141
12193
  }
12142
12194
  const portAutoIncrement = options.autoIncrementPort ?? true;
12143
12195
  const maxPortAttempts = portAutoIncrement === false ? 1 : typeof portAutoIncrement === "number" ? Math.max(1, portAutoIncrement) : 10;
@@ -12321,6 +12373,7 @@ export {
12321
12373
  DEFAULT_PAGE_WARMUP_BUDGET_MS,
12322
12374
  MODULE_BUNDLE_PATH,
12323
12375
  PAGE_WARMUP_HEADER,
12376
+ PAGE_WARMUP_REPORT_PATH,
12324
12377
  ROUTE_PARAMS_PLACEHOLDER,
12325
12378
  SHARED_SCRIPT_PATH,
12326
12379
  boundedCache,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-plugin-stx",
3
3
  "type": "module",
4
- "version": "0.2.905",
4
+ "version": "0.2.908",
5
5
  "description": "A Bun plugin that allows for using Laravel Blade-like syntax.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",
@@ -74,7 +74,7 @@
74
74
  "typecheck": "bun tsc --noEmit"
75
75
  },
76
76
  "dependencies": {
77
- "@stacksjs/stx": "0.2.905",
78
- "stx-router": "0.2.905"
77
+ "@stacksjs/stx": "0.2.908",
78
+ "stx-router": "0.2.908"
79
79
  }
80
80
  }