bun-plugin-stx 0.2.898 → 0.2.899

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
@@ -29,6 +29,31 @@ export declare function fillRouteParams(html: string, params: Record<string, str
29
29
  * cache, never correctness.
30
30
  */
31
31
  export declare function serverScriptsReadParams(serverScripts: string[], paramNames: string[]): boolean;
32
+ /**
33
+ * A route's params for a render that may be kept as every address's shell,
34
+ * and whether anything read them while it rendered.
35
+ *
36
+ * `serverScriptsReadParams` sees the page's own server scripts and nothing
37
+ * else. A script-less page's `{{ params.model }}`, a prop bound from it and a
38
+ * component's server script reading `params` all ran with the first address's
39
+ * values, and that render was served for every address after it
40
+ * (stacksjs/stx#2188). A render reads its params through this object, so it
41
+ * can say so wherever the read happens. Only a param's own name counts: a
42
+ * missing key reads the same for every address, and the engine probes context
43
+ * values for markers (`_isSignal`, `then`) that are not reads of a param.
44
+ */
45
+ export declare function trackParamReads(params: Record<string, string>): { params: Record<string, string>, read: () => boolean };
46
+ /**
47
+ * Whether templates use a route segment by its bare name (`[model].stx`
48
+ * binds `model`) in something the server evaluates: `{{ }}`, `{!! !!}`, a
49
+ * directive's arguments, a bound attribute, or a server script.
50
+ *
51
+ * A bare name cannot be tracked as `params` is: it is a plain string, and
52
+ * every expression is handed every context value, so a read of it looks like
53
+ * any other. Coarse like `serverScriptsReadParams`: a match that is not the
54
+ * param costs the shared shell, never correctness.
55
+ */
56
+ export declare function templatesReadParamNames(sources: string[], paramNames: string[]): boolean;
32
57
  /**
33
58
  * The answer to a request whose URL cannot be parsed, or null for any other.
34
59
  *
package/dist/serve.js CHANGED
@@ -9409,6 +9409,45 @@ function serverScriptsReadParams(serverScripts, paramNames) {
9409
9409
  const pattern = new RegExp(`(?:^|[^\\w$.])(?:${names.join("|")})(?![\\w$])`);
9410
9410
  return serverScripts.some((script) => pattern.test(script));
9411
9411
  }
9412
+ function trackParamReads(params) {
9413
+ let read = false;
9414
+ const isParam = (key) => typeof key === "string" && Object.hasOwn(params, key);
9415
+ const tracked = new Proxy(params, {
9416
+ get(target, key, receiver) {
9417
+ if (isParam(key))
9418
+ read = true;
9419
+ return Reflect.get(target, key, receiver);
9420
+ },
9421
+ has(target, key) {
9422
+ if (isParam(key))
9423
+ read = true;
9424
+ return Reflect.has(target, key);
9425
+ },
9426
+ ownKeys(target) {
9427
+ read = true;
9428
+ return Reflect.ownKeys(target);
9429
+ }
9430
+ });
9431
+ return { params: tracked, read: () => read };
9432
+ }
9433
+ function templatesReadParamNames(sources, paramNames) {
9434
+ if (paramNames.length === 0)
9435
+ return false;
9436
+ const names = paramNames.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
9437
+ const reads = new RegExp(`(?:^|[^\\w$.])(?:${names.join("|")})(?![\\w$])`);
9438
+ const evaluated = /\{\{([\s\S]*?)\}\}|\{!!([\s\S]*?)!!\}|@[a-z]\w*\s*\(([^\n]*)\)|\s:[\w.-]+\s*=\s*(?:"([^"]*)"|'([^']*)')/gi;
9439
+ for (const source of sources) {
9440
+ const { serverScripts, templateContent } = extractServerScriptsFromTemplate(source);
9441
+ if (serverScriptsReadParams(serverScripts, paramNames))
9442
+ return true;
9443
+ for (const match of templateContent.matchAll(evaluated)) {
9444
+ const expression = match.slice(1).find((part) => part !== undefined) ?? "";
9445
+ if (reads.test(expression))
9446
+ return true;
9447
+ }
9448
+ }
9449
+ return false;
9450
+ }
9412
9451
  var CSRF_COOKIE = "X-CSRF-Token";
9413
9452
  function malformedRequestResponse(req) {
9414
9453
  if (URL.canParse(req.url))
@@ -9932,6 +9971,7 @@ After installing: bun add bun-plugin-stx`);
9932
9971
  let assetsInitialized = false;
9933
9972
  const htmlCache = boundedCache(options.renderCacheLimit ?? 512);
9934
9973
  const pendingShellRenders = new Map;
9974
+ const paramReaders = new Map;
9935
9975
  const renderAdmission = createRenderAdmission(options);
9936
9976
  const ENABLE_HTML_CACHE = options.renderCache === true;
9937
9977
  const RENDER_CACHE_VARY = options.renderCacheVary ?? "request";
@@ -10074,6 +10114,7 @@ function __stxOverlay(errs){
10074
10114
  const watchersStarted = new Map;
10075
10115
  const clearRenderCaches = () => {
10076
10116
  htmlCache.clear();
10117
+ paramReaders.clear();
10077
10118
  renderCacheStore?.invalidate();
10078
10119
  cssCssCache.clear();
10079
10120
  resolvedStxModule?.clearDevCaches?.();
@@ -10953,7 +10994,12 @@ function __stxOverlay(errs){
10953
10994
  }
10954
10995
  const isMutating = !!reqCtx && reqCtx.method !== "GET" && reqCtx.method !== "HEAD";
10955
10996
  const skipCacheHint = /(?:^|[^\w$])__stx_skip_cache\s*=\s*true/.test(content);
10956
- const cacheShell = ENABLE_HTML_CACHE && RENDER_CACHE_VARY === "source" && paramNames.length > 0 && !isMutating && !skipCacheHint && allowsRenderCache(reqCtx) && !serverScriptsReadParams(dynServerScripts, paramNames);
10997
+ let cacheShell = ENABLE_HTML_CACHE && RENDER_CACHE_VARY === "source" && paramNames.length > 0 && !isMutating && !skipCacheHint && allowsRenderCache(reqCtx) && !serverScriptsReadParams(dynServerScripts, paramNames);
10998
+ if (cacheShell) {
10999
+ const reader = paramReaders.get(filePath);
11000
+ if (reader && await templateSignatureFresh(reader))
11001
+ cacheShell = false;
11002
+ }
10957
11003
  if (reqCtx)
10958
11004
  reqCtx.params = paramsObj;
10959
11005
  if (cacheShell) {
@@ -10987,7 +11033,8 @@ function __stxOverlay(errs){
10987
11033
  for (const name of paramNames) {
10988
11034
  context[name] = paramsObj[name];
10989
11035
  }
10990
- context.params = paramsObj;
11036
+ const paramReads = cacheShell ? trackParamReads(paramsObj) : undefined;
11037
+ context.params = paramReads ? paramReads.params : paramsObj;
10991
11038
  const { processDirectives, extractVariables, defaultConfig, injectRouterScript: injectRouter } = await stxModule;
10992
11039
  injectServeLocaleContext(context);
10993
11040
  injectServeRequestContext(context, reqCtx);
@@ -11051,6 +11098,10 @@ ${output}`;
11051
11098
  });
11052
11099
  if (!cacheShell)
11053
11100
  return output;
11101
+ if (paramReads?.read() || templatesReadParamNames([templateContent, ...await Promise.all([...dependencies].filter((dep) => dep !== filePath).map((dep) => Bun.file(dep).text().catch(() => "")))], paramNames)) {
11102
+ paramReaders.set(filePath, await buildTemplateSignature(filePath, dependencies));
11103
+ return fillRouteParams(output, paramsObj);
11104
+ }
11054
11105
  const recordedResponse = readResponseStatus(context) !== undefined || !!readResponseHeaders(context);
11055
11106
  if (!context.__stx_skip_cache && !recordedResponse && allowsRenderCache(reqCtx) && isRenderableCacheCandidate(output) && placeholdersAreReady) {
11056
11107
  const signature = await buildTemplateSignature(filePath, dependencies);
@@ -12306,5 +12357,7 @@ export {
12306
12357
  sharedScriptCacheControl,
12307
12358
  staticCacheControl,
12308
12359
  staticContentType,
12360
+ templatesReadParamNames,
12361
+ trackParamReads,
12309
12362
  usableErrorPage
12310
12363
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-plugin-stx",
3
3
  "type": "module",
4
- "version": "0.2.898",
4
+ "version": "0.2.899",
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.898",
78
- "stx-router": "0.2.898"
77
+ "@stacksjs/stx": "0.2.899",
78
+ "stx-router": "0.2.899"
79
79
  }
80
80
  }