bun-plugin-stx 0.2.371 → 0.2.373

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/serve.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { serve as bunServe } from 'bun';
1
2
  import type { MiddlewareContext, MiddlewareHandler, MiddlewareRequest, PageMiddleware, PrepareMiddlewareRequest } from './page-middleware';
2
3
  export type { MiddlewareContext, MiddlewareDefinition, MiddlewareHandler, MiddlewareRequest, PageMiddleware, PrepareMiddlewareRequest } from './page-middleware';
3
4
  /**
@@ -334,6 +335,8 @@ export declare interface ServeOptions {
334
335
  watchDirs?: string[]
335
336
  renderCache?: boolean
336
337
  renderCacheVary?: 'request' | 'source'
338
+ renderCacheLimit?: number
339
+ clientAddress?: (req: Request, server: ServeServer) => string | null | undefined
337
340
  cssCacheLimit?: number
338
341
  prewarmRenderCache?: boolean | number
339
342
  imageWarmupGraceMs?: number
@@ -347,7 +350,7 @@ export declare interface ServeOptions {
347
350
  i18n?: import('@stacksjs/stx').ResolvedI18n
348
351
  site?: import('@stacksjs/stx').SiteConfig
349
352
  routes?: Record<string, (req: Request) => Response | Promise<Response>>
350
- onRequest?: (req: Request) =>
353
+ onRequest?: (req: Request, server: ServeServer) =>
351
354
  | Response
352
355
  | Record<string, unknown>
353
356
  | null
@@ -381,3 +384,5 @@ export declare interface ServeOptions {
381
384
  * `cwcss-crosswind.d.ts`). `typeof import` cannot drift from the real engine.
382
385
  */
383
386
  declare type CssEngine = Pick<typeof import('@stacksjs/ts-css/engine'), 'CSSGenerator' | 'config'>;
387
+ /** The Bun server `serve()` started - handed to `onRequest` beside each request. */
388
+ export type ServeServer = ReturnType<typeof bunServe>;
package/dist/serve.js CHANGED
@@ -9323,7 +9323,7 @@ After installing: bun add bun-plugin-stx`);
9323
9323
  const isExcludedPage = pageExcluder(options.exclude);
9324
9324
  let discoveredPagePaths = null;
9325
9325
  let assetsInitialized = false;
9326
- const htmlCache = new Map;
9326
+ const htmlCache = boundedCache(options.renderCacheLimit ?? 512);
9327
9327
  const ENABLE_HTML_CACHE = options.renderCache === true;
9328
9328
  const RENDER_CACHE_VARY = options.renderCacheVary ?? "request";
9329
9329
  const cssCssCache = boundedCache(options.cssCacheLimit ?? 512);
@@ -9667,6 +9667,14 @@ function __stxOverlay(errs){
9667
9667
  full.responseHeaders = { ...full.responseHeaders, [header]: String(value) };
9668
9668
  };
9669
9669
  }
9670
+ function resolveClientAddress(req, server) {
9671
+ try {
9672
+ const address = options.clientAddress ? options.clientAddress(req, server) : server.requestIP(req)?.address;
9673
+ return address ?? "";
9674
+ } catch {
9675
+ return "";
9676
+ }
9677
+ }
9670
9678
  function htmlCacheKey(filePath, reqCtx) {
9671
9679
  if (RENDER_CACHE_VARY === "source")
9672
9680
  return filePath;
@@ -10005,7 +10013,7 @@ function __stxOverlay(errs){
10005
10013
  const isMutating = !!reqCtx && reqCtx.method !== "GET" && reqCtx.method !== "HEAD";
10006
10014
  if (ENABLE_HTML_CACHE && !skipCacheHint && !isMutating) {
10007
10015
  const cacheKey = htmlCacheKey(filePath, reqCtx);
10008
- const cachedEntry = htmlCache.get(cacheKey);
10016
+ const cachedEntry = htmlCache.read(cacheKey);
10009
10017
  if (cachedEntry && await templateSignatureFresh(cachedEntry.signature)) {
10010
10018
  if (reqCtx) {
10011
10019
  reqCtx.responseStatus = cachedEntry.status;
@@ -10082,7 +10090,7 @@ function __stxOverlay(errs){
10082
10090
  }
10083
10091
  if (ENABLE_HTML_CACHE && !context.__stx_skip_cache && !skipCacheHint && !isMutating && isRenderableCacheCandidate(output) && placeholdersAreReady) {
10084
10092
  const signature = await buildTemplateSignature(filePath, dependencies);
10085
- htmlCache.set(htmlCacheKey(filePath, reqCtx), { html: output, signature, status: reqCtx?.responseStatus ?? 200, headers: reqCtx?.responseHeaders });
10093
+ htmlCache.remember(htmlCacheKey(filePath, reqCtx), { html: output, signature, status: reqCtx?.responseStatus ?? 200, headers: reqCtx?.responseHeaders });
10086
10094
  }
10087
10095
  return output;
10088
10096
  }
@@ -10204,7 +10212,7 @@ function __stxOverlay(errs){
10204
10212
  if (reqCtx)
10205
10213
  reqCtx.params = paramsObj;
10206
10214
  if (cacheShell) {
10207
- const cachedEntry = htmlCache.get(filePath);
10215
+ const cachedEntry = htmlCache.read(filePath);
10208
10216
  if (cachedEntry && await templateSignatureFresh(cachedEntry.signature)) {
10209
10217
  if (reqCtx) {
10210
10218
  reqCtx.responseStatus = cachedEntry.status;
@@ -10285,7 +10293,7 @@ ${output}`;
10285
10293
  const recordedResponse = readResponseStatus(context) !== undefined || !!readResponseHeaders(context);
10286
10294
  if (!context.__stx_skip_cache && !recordedResponse && isRenderableCacheCandidate(output) && placeholdersAreReady) {
10287
10295
  const signature = await buildTemplateSignature(filePath, dependencies);
10288
- htmlCache.set(filePath, { html: output, signature, status: reqCtx?.responseStatus ?? 200, headers: reqCtx?.responseHeaders });
10296
+ htmlCache.remember(filePath, { html: output, signature, status: reqCtx?.responseStatus ?? 200, headers: reqCtx?.responseHeaders });
10289
10297
  }
10290
10298
  return fillRouteParams(output, paramsObj);
10291
10299
  }
@@ -10506,7 +10514,7 @@ ${block}`;
10506
10514
  activeServeHost = req.headers.get("host") || "";
10507
10515
  activeServeCookieHeader = req.headers.get("cookie") || "";
10508
10516
  activeServeCookies = parseCookies(req);
10509
- activeServeIp = server.requestIP(req)?.address || (req.headers.get("x-forwarded-for") || "").split(",")[0].trim();
10517
+ activeServeIp = resolveClientAddress(req, server);
10510
10518
  mintedCsrfToken = csrfTokenToMint(req, activeServeCookies);
10511
10519
  if (mintedCsrfToken)
10512
10520
  activeServeCookies[CSRF_COOKIE] = mintedCsrfToken;
@@ -10662,7 +10670,7 @@ ${block}`;
10662
10670
  return new Response(content2, { headers });
10663
10671
  }
10664
10672
  if (options.onRequest) {
10665
- const hookResult = await options.onRequest(req);
10673
+ const hookResult = await options.onRequest(req, server);
10666
10674
  if (hookResult instanceof Response)
10667
10675
  return hookResult;
10668
10676
  if (hookResult && typeof hookResult === "object")
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-plugin-stx",
3
3
  "type": "module",
4
- "version": "0.2.371",
4
+ "version": "0.2.373",
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.371",
78
- "stx-router": "0.2.371"
77
+ "@stacksjs/stx": "0.2.373",
78
+ "stx-router": "0.2.373"
79
79
  }
80
80
  }