bun-plugin-stx 0.2.896 → 0.2.898

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.
@@ -1,5 +1,7 @@
1
+ /** A caller can lower its own priority, never take priority above navigation. */
2
+ export declare function renderRequestPriority(request?: Request): RenderPriority;
1
3
  export declare function createRenderAdmission(options?: RenderAdmissionOptions): {
2
- run: <T>(start: () => Promise<T>, signal?: AbortSignal) => Promise<T>
4
+ run: <T>(start: () => Promise<T>, signal?: AbortSignal, priority?: RenderPriority) => Promise<T>
3
5
  };
4
6
  /** Bounds work before a render allocates its script contexts and HTML. */
5
7
  export declare interface RenderAdmissionOptions {
@@ -7,6 +9,7 @@ export declare interface RenderAdmissionOptions {
7
9
  renderQueueLimit?: number
8
10
  renderQueueTimeoutMs?: number
9
11
  }
12
+ declare type RenderPriority = 'foreground' | 'background';
10
13
  export declare class RenderAdmissionError extends Error {
11
14
  readonly status: 408 | 503;
12
15
  constructor(status: 408 | 503);
package/dist/serve.js CHANGED
@@ -9160,6 +9160,10 @@ class RenderAdmissionError extends Error {
9160
9160
  this.name = "RenderAdmissionError";
9161
9161
  }
9162
9162
  }
9163
+ function renderRequestPriority(request) {
9164
+ const purpose = `${request?.headers.get("purpose") || ""} ${request?.headers.get("sec-purpose") || ""}`;
9165
+ return purpose.toLowerCase().split(/[;,\s]+/).some((value) => value === "prefetch" || value === "prerender") ? "background" : "foreground";
9166
+ }
9163
9167
  function integer(value, fallback, minimum) {
9164
9168
  if (value === undefined)
9165
9169
  return fallback;
@@ -9171,20 +9175,32 @@ function createRenderAdmission(options = {}) {
9171
9175
  const concurrency = integer(options.renderConcurrency, 4, 1);
9172
9176
  const queueLimit = integer(options.renderQueueLimit, 32, 0);
9173
9177
  const timeoutMs = integer(options.renderQueueTimeoutMs, 15000, 1);
9178
+ const backgroundConcurrency = Math.max(1, Math.floor(concurrency / 2));
9179
+ const backgroundQueueLimit = Math.max(0, queueLimit - concurrency);
9174
9180
  const waiting = [];
9175
9181
  let active = 0;
9176
- function release() {
9182
+ let activeBackground = 0;
9183
+ function release(priority) {
9177
9184
  active--;
9178
- waiting.shift()?.start();
9185
+ if (priority === "background")
9186
+ activeBackground--;
9187
+ while (active < concurrency && waiting.length) {
9188
+ const foreground = waiting.findIndex((entry) => entry.priority === "foreground");
9189
+ if (foreground < 0 && activeBackground >= backgroundConcurrency)
9190
+ break;
9191
+ waiting.splice(foreground < 0 ? 0 : foreground, 1)[0]?.start();
9192
+ }
9179
9193
  }
9180
- async function acquire(signal) {
9194
+ async function acquire(signal, priority) {
9181
9195
  if (signal?.aborted)
9182
9196
  throw new RenderAdmissionError(408);
9183
- if (active < concurrency) {
9197
+ if (active < concurrency && (priority === "foreground" || activeBackground < backgroundConcurrency)) {
9184
9198
  active++;
9199
+ if (priority === "background")
9200
+ activeBackground++;
9185
9201
  return;
9186
9202
  }
9187
- if (waiting.length >= queueLimit)
9203
+ if (waiting.length >= queueLimit || priority === "background" && waiting.filter((entry) => entry.priority === "background").length >= backgroundQueueLimit)
9188
9204
  throw new RenderAdmissionError(503);
9189
9205
  await new Promise((resolve, reject) => {
9190
9206
  let settled = false;
@@ -9204,12 +9220,15 @@ function createRenderAdmission(options = {}) {
9204
9220
  };
9205
9221
  const abort = () => fail(408);
9206
9222
  const entry = {
9223
+ priority,
9207
9224
  start() {
9208
9225
  if (settled)
9209
9226
  return;
9210
9227
  settled = true;
9211
9228
  cleanup();
9212
9229
  active++;
9230
+ if (priority === "background")
9231
+ activeBackground++;
9213
9232
  resolve();
9214
9233
  }
9215
9234
  };
@@ -9222,14 +9241,14 @@ function createRenderAdmission(options = {}) {
9222
9241
  });
9223
9242
  }
9224
9243
  return {
9225
- async run(start, signal) {
9226
- await acquire(signal);
9244
+ async run(start, signal, priority = "foreground") {
9245
+ await acquire(signal, priority);
9227
9246
  try {
9228
9247
  if (signal?.aborted)
9229
9248
  throw new RenderAdmissionError(408);
9230
9249
  return await start();
9231
9250
  } finally {
9232
- release();
9251
+ release(priority);
9233
9252
  }
9234
9253
  }
9235
9254
  };
@@ -10719,7 +10738,7 @@ function __stxOverlay(errs){
10719
10738
  reqCtx.responseHeaders = { ...reqCtx.responseHeaders, ...headers };
10720
10739
  }
10721
10740
  async function processTemplate(filePath, reqCtx) {
10722
- return renderAdmission.run(() => renderTemplate(filePath, reqCtx), reqCtx?.request?.signal);
10741
+ return renderAdmission.run(() => renderTemplate(filePath, reqCtx), reqCtx?.request?.signal, renderRequestPriority(reqCtx?.request));
10723
10742
  }
10724
10743
  async function renderTemplate(filePath, reqCtx) {
10725
10744
  const ticket = await renderTicket(reqCtx);
@@ -10916,7 +10935,7 @@ function __stxOverlay(errs){
10916
10935
  return null;
10917
10936
  }
10918
10937
  async function processTemplateDynamic(filePath, paramNames, paramValues, routePath, reqCtx) {
10919
- return renderAdmission.run(() => renderTemplateDynamic(filePath, paramNames, paramValues, routePath, reqCtx), reqCtx?.request?.signal);
10938
+ return renderAdmission.run(() => renderTemplateDynamic(filePath, paramNames, paramValues, routePath, reqCtx), reqCtx?.request?.signal, renderRequestPriority(reqCtx?.request));
10920
10939
  }
10921
10940
  async function renderTemplateDynamic(filePath, paramNames, paramValues, routePath, reqCtx) {
10922
10941
  const ticket = await renderTicket(reqCtx);
@@ -11565,7 +11584,7 @@ ${output}`;
11565
11584
  ...partialsDir && { partialsDir },
11566
11585
  autoShell: false
11567
11586
  };
11568
- const html = await renderAdmission.run(() => processDirectives(componentTemplate, {}, `${componentsDir}/${componentName}.stx`, componentOpts, new Set), req.signal);
11587
+ const html = await renderAdmission.run(() => processDirectives(componentTemplate, {}, `${componentsDir}/${componentName}.stx`, componentOpts, new Set), req.signal, renderRequestPriority(req));
11569
11588
  return new Response(html, {
11570
11589
  headers: { "Content-Type": "text/html; charset=utf-8", ...corsHeaders }
11571
11590
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-plugin-stx",
3
3
  "type": "module",
4
- "version": "0.2.896",
4
+ "version": "0.2.898",
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.896",
78
- "stx-router": "0.2.896"
77
+ "@stacksjs/stx": "0.2.898",
78
+ "stx-router": "0.2.898"
79
79
  }
80
80
  }