tina4-nodejs 3.13.163 → 3.13.165

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.
@@ -1717,8 +1717,14 @@ var Frond = class _Frond {
1717
1717
  // and every future ``new Frond()`` inherits it automatically.
1718
1718
  // Mirrors Python's _class_globals / _class_filters / _class_tests.
1719
1719
  static classFilters = /* @__PURE__ */ new Map();
1720
- static classGlobals = /* @__PURE__ */ new Map();
1720
+ // Globals are read LIVE at render time (not copied at construction), so a
1721
+ // Frond.addGlobal() made after an engine exists still reaches it. A plain
1722
+ // object rather than a Map so render() can spread it in one step.
1723
+ static classGlobals = {};
1721
1724
  static classTests = /* @__PURE__ */ new Map();
1725
+ // Bumped on every class-level filter/test change, so an engine knows when
1726
+ // to rebuild its merged filter/test maps (see syncRegistries).
1727
+ static classRegistryVersion = 0;
1722
1728
  // ── Live-block registries (server-rendered {% live %} regions) ──
1723
1729
  // A {% live %} block registers three things when its page first renders:
1724
1730
  // liveFragments[name] -> the raw body source, re-rendered on every
@@ -1738,20 +1744,27 @@ var Frond = class _Frond {
1738
1744
  static liveBroadcaster = null;
1739
1745
  /**
1740
1746
  * Register a custom filter at the class level — available to every
1741
- * future ``new Frond()`` instance. Callable as ``Frond.addFilter()``
1747
+ * instance, existing ones included: an engine merges the class filters
1748
+ * again at its next render (ADR-0107). Callable as ``Frond.addFilter()``
1742
1749
  * (static) or ``frond.addFilter()`` (instance). See instance method
1743
1750
  * below for the dual-call semantics.
1744
1751
  */
1745
1752
  static addFilter(name, fn) {
1746
1753
  _Frond.classFilters.set(name, fn);
1754
+ _Frond.classRegistryVersion++;
1747
1755
  }
1748
1756
  /**
1749
1757
  * Register a global variable available in all templates of every
1750
- * future instance. Callable as ``Frond.addGlobal()`` (static) or
1758
+ * instance, existing ones included: class globals are read at render time
1759
+ * (ADR-0106). Callable as ``Frond.addGlobal()`` (static) or
1751
1760
  * ``frond.addGlobal()`` (instance).
1752
1761
  */
1753
1762
  static addGlobal(name, value) {
1754
- _Frond.classGlobals.set(name, value);
1763
+ _Frond.classGlobals[name] = value;
1764
+ }
1765
+ /** True when a class-level global with this name is registered. */
1766
+ static hasGlobal(name) {
1767
+ return Object.prototype.hasOwnProperty.call(_Frond.classGlobals, name);
1755
1768
  }
1756
1769
  /**
1757
1770
  * Register a custom test (``{% if x is positive %}``) at the class
@@ -1760,6 +1773,7 @@ var Frond = class _Frond {
1760
1773
  */
1761
1774
  static addTest(name, fn) {
1762
1775
  _Frond.classTests.set(name, fn);
1776
+ _Frond.classRegistryVersion++;
1763
1777
  }
1764
1778
  /**
1765
1779
  * Clear the class-level globals/filters/tests registries.
@@ -1769,15 +1783,25 @@ var Frond = class _Frond {
1769
1783
  */
1770
1784
  static clearRegistry() {
1771
1785
  _Frond.classFilters.clear();
1772
- _Frond.classGlobals.clear();
1786
+ _Frond.classGlobals = {};
1773
1787
  _Frond.classTests.clear();
1788
+ _Frond.classRegistryVersion++;
1774
1789
  _Frond.liveFragments.clear();
1775
1790
  _Frond.liveSources.clear();
1776
1791
  _Frond.liveWsPaths.clear();
1777
1792
  }
1778
1793
  templateDir;
1779
- filters;
1794
+ /** Effective filters: built-in < class < instance (see syncRegistries). */
1795
+ filters = {};
1796
+ /** Filters / tests registered on THIS instance only (ADR-0052). */
1797
+ localFilters = {};
1798
+ localTests = {};
1799
+ /** The class-registry version `filters` / `tests` were last merged at. */
1800
+ registryVersion = -1;
1801
+ /** Globals registered on THIS instance only (ADR-0052). */
1780
1802
  globals;
1803
+ /** Built-in globals (formToken, range, dump, csp_nonce, ...). */
1804
+ builtinGlobals;
1781
1805
  tests;
1782
1806
  _sandbox;
1783
1807
  _allowedFilters;
@@ -1807,9 +1831,10 @@ var Frond = class _Frond {
1807
1831
  }
1808
1832
  constructor(templateDir = "src/templates") {
1809
1833
  this.templateDir = resolve(templateDir);
1810
- this.filters = { ...BUILTIN_FILTERS };
1811
1834
  this.globals = {};
1835
+ this.builtinGlobals = {};
1812
1836
  this.tests = {};
1837
+ this.syncRegistries();
1813
1838
  this._sandbox = false;
1814
1839
  this._allowedFilters = null;
1815
1840
  this._allowedTags = null;
@@ -1857,16 +1882,25 @@ var Frond = class _Frond {
1857
1882
  return { output, next };
1858
1883
  }
1859
1884
  };
1860
- this.globals.formToken = (descriptor) => _generateFormToken(descriptor || "");
1861
- this.globals.form_token = (descriptor) => _generateFormToken(descriptor || "");
1862
- this.globals.formTokenValue = (descriptor) => _generateFormTokenValue(descriptor || "");
1863
- this.globals.form_token_value = (descriptor) => _generateFormTokenValue(descriptor || "");
1864
- this.globals.range = frondRange;
1865
- this.globals.dump = (value) => renderDump(value);
1866
- this.globals.csp_nonce = () => _cspNonceProvider();
1867
- for (const [k, v] of _Frond.classFilters) this.filters[k] = v;
1868
- for (const [k, v] of _Frond.classGlobals) this.globals[k] = v;
1869
- for (const [k, v] of _Frond.classTests) this.tests[k] = v;
1885
+ this.builtinGlobals.formToken = (descriptor) => _generateFormToken(descriptor || "");
1886
+ this.builtinGlobals.form_token = (descriptor) => _generateFormToken(descriptor || "");
1887
+ this.builtinGlobals.formTokenValue = (descriptor) => _generateFormTokenValue(descriptor || "");
1888
+ this.builtinGlobals.form_token_value = (descriptor) => _generateFormTokenValue(descriptor || "");
1889
+ this.builtinGlobals.range = frondRange;
1890
+ this.builtinGlobals.dump = (value) => renderDump(value);
1891
+ this.builtinGlobals.csp_nonce = () => _cspNonceProvider();
1892
+ }
1893
+ /**
1894
+ * Rebuild the merged filter/test maps when the class registry changed.
1895
+ * Same layering as globals -- built-ins < class < this instance. Filter
1896
+ * lookup is on the hot path, so the merge is a plain object rebuilt only
1897
+ * when the class-registry version moves, never a per-lookup chain.
1898
+ */
1899
+ syncRegistries() {
1900
+ if (this.registryVersion === _Frond.classRegistryVersion) return;
1901
+ this.filters = { ...BUILTIN_FILTERS, ...Object.fromEntries(_Frond.classFilters), ...this.localFilters };
1902
+ this.tests = { ...Object.fromEntries(_Frond.classTests), ...this.localTests };
1903
+ this.registryVersion = _Frond.classRegistryVersion;
1870
1904
  }
1871
1905
  sandbox(filters, tags, vars) {
1872
1906
  this._sandbox = true;
@@ -1887,6 +1921,7 @@ var Frond = class _Frond {
1887
1921
  * for process-global registration. tina4: ADR-0052.
1888
1922
  */
1889
1923
  addFilter(name, fn) {
1924
+ this.localFilters[name] = fn;
1890
1925
  this.filters[name] = fn;
1891
1926
  }
1892
1927
  /**
@@ -1899,6 +1934,7 @@ var Frond = class _Frond {
1899
1934
  * Register a custom test on this instance only.
1900
1935
  */
1901
1936
  addTest(name, fn) {
1937
+ this.localTests[name] = fn;
1902
1938
  this.tests[name] = fn;
1903
1939
  }
1904
1940
  /**
@@ -1914,8 +1950,19 @@ var Frond = class _Frond {
1914
1950
  const n = parseInt(raw, 10);
1915
1951
  return isNaN(n) || n < 0 ? 0 : n;
1916
1952
  }
1953
+ /**
1954
+ * Layer the globals under the render data: built-ins < class globals (read
1955
+ * live, so registration order never matters) < this instance's own globals
1956
+ * < the data passed to render(). Every render starts here, so this is also
1957
+ * where the filter/test maps catch up with any class registration made
1958
+ * since the last render.
1959
+ */
1960
+ withGlobals(data) {
1961
+ this.syncRegistries();
1962
+ return { ...this.builtinGlobals, ..._Frond.classGlobals, ...this.globals, ...data || {} };
1963
+ }
1917
1964
  render(template, data) {
1918
- const context = { ...this.globals, ...data || {} };
1965
+ const context = this.withGlobals(data);
1919
1966
  const filePath = join(this.templateDir, template);
1920
1967
  if (!existsSync(filePath)) {
1921
1968
  throw new Error(`Template not found: ${filePath}`);
@@ -1938,7 +1985,7 @@ var Frond = class _Frond {
1938
1985
  return this.executeWithSource(source, tokens, context);
1939
1986
  }
1940
1987
  renderString(source, data) {
1941
- const context = { ...this.globals, ...data || {} };
1988
+ const context = this.withGlobals(data);
1942
1989
  const key = createHash("md5").update(source).digest("hex");
1943
1990
  const ttlMs = this.cacheTtlSeconds() * 1e3;
1944
1991
  const cached = this.compiledStrings.get(key);
@@ -2162,8 +2162,14 @@ export class Frond {
2162
2162
  // and every future ``new Frond()`` inherits it automatically.
2163
2163
  // Mirrors Python's _class_globals / _class_filters / _class_tests.
2164
2164
  private static classFilters: Map<string, FilterFn> = new Map();
2165
- private static classGlobals: Map<string, unknown> = new Map();
2165
+ // Globals are read LIVE at render time (not copied at construction), so a
2166
+ // Frond.addGlobal() made after an engine exists still reaches it. A plain
2167
+ // object rather than a Map so render() can spread it in one step.
2168
+ private static classGlobals: Record<string, unknown> = {};
2166
2169
  private static classTests: Map<string, TestFn> = new Map();
2170
+ // Bumped on every class-level filter/test change, so an engine knows when
2171
+ // to rebuild its merged filter/test maps (see syncRegistries).
2172
+ private static classRegistryVersion = 0;
2167
2173
 
2168
2174
  // ── Live-block registries (server-rendered {% live %} regions) ──
2169
2175
  // A {% live %} block registers three things when its page first renders:
@@ -2185,21 +2191,29 @@ export class Frond {
2185
2191
 
2186
2192
  /**
2187
2193
  * Register a custom filter at the class level — available to every
2188
- * future ``new Frond()`` instance. Callable as ``Frond.addFilter()``
2194
+ * instance, existing ones included: an engine merges the class filters
2195
+ * again at its next render (ADR-0107). Callable as ``Frond.addFilter()``
2189
2196
  * (static) or ``frond.addFilter()`` (instance). See instance method
2190
2197
  * below for the dual-call semantics.
2191
2198
  */
2192
2199
  static addFilter(name: string, fn: FilterFn): void {
2193
2200
  Frond.classFilters.set(name, fn);
2201
+ Frond.classRegistryVersion++;
2194
2202
  }
2195
2203
 
2196
2204
  /**
2197
2205
  * Register a global variable available in all templates of every
2198
- * future instance. Callable as ``Frond.addGlobal()`` (static) or
2206
+ * instance, existing ones included: class globals are read at render time
2207
+ * (ADR-0106). Callable as ``Frond.addGlobal()`` (static) or
2199
2208
  * ``frond.addGlobal()`` (instance).
2200
2209
  */
2201
2210
  static addGlobal(name: string, value: unknown): void {
2202
- Frond.classGlobals.set(name, value);
2211
+ Frond.classGlobals[name] = value;
2212
+ }
2213
+
2214
+ /** True when a class-level global with this name is registered. */
2215
+ static hasGlobal(name: string): boolean {
2216
+ return Object.prototype.hasOwnProperty.call(Frond.classGlobals, name);
2203
2217
  }
2204
2218
 
2205
2219
  /**
@@ -2209,6 +2223,7 @@ export class Frond {
2209
2223
  */
2210
2224
  static addTest(name: string, fn: TestFn): void {
2211
2225
  Frond.classTests.set(name, fn);
2226
+ Frond.classRegistryVersion++;
2212
2227
  }
2213
2228
 
2214
2229
  /**
@@ -2219,16 +2234,26 @@ export class Frond {
2219
2234
  */
2220
2235
  static clearRegistry(): void {
2221
2236
  Frond.classFilters.clear();
2222
- Frond.classGlobals.clear();
2237
+ Frond.classGlobals = {};
2223
2238
  Frond.classTests.clear();
2239
+ Frond.classRegistryVersion++;
2224
2240
  Frond.liveFragments.clear();
2225
2241
  Frond.liveSources.clear();
2226
2242
  Frond.liveWsPaths.clear();
2227
2243
  }
2228
2244
 
2229
2245
  private templateDir: string;
2230
- private filters: Record<string, FilterFn>;
2246
+ /** Effective filters: built-in < class < instance (see syncRegistries). */
2247
+ private filters: Record<string, FilterFn> = {};
2248
+ /** Filters / tests registered on THIS instance only (ADR-0052). */
2249
+ private localFilters: Record<string, FilterFn> = {};
2250
+ private localTests: Record<string, TestFn> = {};
2251
+ /** The class-registry version `filters` / `tests` were last merged at. */
2252
+ private registryVersion = -1;
2253
+ /** Globals registered on THIS instance only (ADR-0052). */
2231
2254
  private globals: Record<string, unknown>;
2255
+ /** Built-in globals (formToken, range, dump, csp_nonce, ...). */
2256
+ private builtinGlobals: Record<string, unknown>;
2232
2257
  private tests: Record<string, TestFn>;
2233
2258
  private _sandbox: boolean;
2234
2259
  private _allowedFilters: Set<string> | null;
@@ -2259,9 +2284,10 @@ export class Frond {
2259
2284
 
2260
2285
  constructor(templateDir: string = "src/templates") {
2261
2286
  this.templateDir = resolve(templateDir);
2262
- this.filters = { ...BUILTIN_FILTERS };
2263
2287
  this.globals = {};
2288
+ this.builtinGlobals = {};
2264
2289
  this.tests = {};
2290
+ this.syncRegistries(); // builds this.filters / this.tests
2265
2291
  this._sandbox = false;
2266
2292
  this._allowedFilters = null;
2267
2293
  this._allowedTags = null;
@@ -2311,16 +2337,16 @@ export class Frond {
2311
2337
  };
2312
2338
 
2313
2339
  // Built-in global functions
2314
- this.globals.formToken = (descriptor?: string) => _generateFormToken(descriptor || "");
2315
- this.globals.form_token = (descriptor?: string) => _generateFormToken(descriptor || "");
2316
- this.globals.formTokenValue = (descriptor?: string) => _generateFormTokenValue(descriptor || "");
2317
- this.globals.form_token_value = (descriptor?: string) => _generateFormTokenValue(descriptor || "");
2318
- this.globals.range = frondRange;
2340
+ this.builtinGlobals.formToken = (descriptor?: string) => _generateFormToken(descriptor || "");
2341
+ this.builtinGlobals.form_token = (descriptor?: string) => _generateFormToken(descriptor || "");
2342
+ this.builtinGlobals.formTokenValue = (descriptor?: string) => _generateFormTokenValue(descriptor || "");
2343
+ this.builtinGlobals.form_token_value = (descriptor?: string) => _generateFormTokenValue(descriptor || "");
2344
+ this.builtinGlobals.range = frondRange;
2319
2345
 
2320
2346
  // Debug helper: {{ dump(x) }} — gated on TINA4_DEBUG, see renderDump().
2321
2347
  // Available alongside the |dump filter so both styles work:
2322
2348
  // {{ user|dump }} and {{ dump(user) }}
2323
- this.globals.dump = (value: unknown) => renderDump(value);
2349
+ this.builtinGlobals.dump = (value: unknown) => renderDump(value);
2324
2350
 
2325
2351
  // CSP nonce (ADR-0088): {{ csp_nonce() }} returns the current response's
2326
2352
  // nonce so a template can serve an inline <style>/<script> under the strict
@@ -2328,15 +2354,26 @@ export class Frond {
2328
2354
  // value matches the 'nonce-X' the security middleware puts in the CSP header.
2329
2355
  // Frond stays zero-dependency: @tina4/core injects the provider (it never
2330
2356
  // imports core); standalone Frond returns "".
2331
- this.globals.csp_nonce = () => _cspNonceProvider();
2357
+ this.builtinGlobals.csp_nonce = () => _cspNonceProvider();
2332
2358
 
2333
- // Drain the class-level registry. This is the key to surviving
2334
- // hot-reloads AND the static-facade pattern: app.ts calls
2335
- // Frond.addFilter("money", fn) once, the class remembers it,
2336
- // and every future Frond() instance picks it up automatically.
2337
- for (const [k, v] of Frond.classFilters) this.filters[k] = v;
2338
- for (const [k, v] of Frond.classGlobals) this.globals[k] = v;
2339
- for (const [k, v] of Frond.classTests) this.tests[k] = v;
2359
+ // Class registrations are never copied in for good: withGlobals() reads
2360
+ // the class globals at render time and syncRegistries() re-merges the
2361
+ // class filters/tests whenever they change, so a Frond.addGlobal /
2362
+ // addFilter / addTest made after this engine exists still reaches it
2363
+ // (ADR-0106, ADR-0107).
2364
+ }
2365
+
2366
+ /**
2367
+ * Rebuild the merged filter/test maps when the class registry changed.
2368
+ * Same layering as globals -- built-ins < class < this instance. Filter
2369
+ * lookup is on the hot path, so the merge is a plain object rebuilt only
2370
+ * when the class-registry version moves, never a per-lookup chain.
2371
+ */
2372
+ private syncRegistries(): void {
2373
+ if (this.registryVersion === Frond.classRegistryVersion) return;
2374
+ this.filters = { ...BUILTIN_FILTERS, ...Object.fromEntries(Frond.classFilters), ...this.localFilters };
2375
+ this.tests = { ...Object.fromEntries(Frond.classTests), ...this.localTests };
2376
+ this.registryVersion = Frond.classRegistryVersion;
2340
2377
  }
2341
2378
 
2342
2379
  sandbox(filters?: string[], tags?: string[], vars?: string[]): Frond {
@@ -2360,6 +2397,7 @@ export class Frond {
2360
2397
  * for process-global registration. tina4: ADR-0052.
2361
2398
  */
2362
2399
  addFilter(name: string, fn: FilterFn): void {
2400
+ this.localFilters[name] = fn;
2363
2401
  this.filters[name] = fn;
2364
2402
  }
2365
2403
 
@@ -2374,6 +2412,7 @@ export class Frond {
2374
2412
  * Register a custom test on this instance only.
2375
2413
  */
2376
2414
  addTest(name: string, fn: TestFn): void {
2415
+ this.localTests[name] = fn;
2377
2416
  this.tests[name] = fn;
2378
2417
  }
2379
2418
 
@@ -2391,8 +2430,20 @@ export class Frond {
2391
2430
  return isNaN(n) || n < 0 ? 0 : n;
2392
2431
  }
2393
2432
 
2433
+ /**
2434
+ * Layer the globals under the render data: built-ins < class globals (read
2435
+ * live, so registration order never matters) < this instance's own globals
2436
+ * < the data passed to render(). Every render starts here, so this is also
2437
+ * where the filter/test maps catch up with any class registration made
2438
+ * since the last render.
2439
+ */
2440
+ private withGlobals(data?: Record<string, unknown>): Record<string, unknown> {
2441
+ this.syncRegistries();
2442
+ return { ...this.builtinGlobals, ...Frond.classGlobals, ...this.globals, ...(data || {}) };
2443
+ }
2444
+
2394
2445
  render(template: string, data?: Record<string, unknown>): string {
2395
- const context = { ...this.globals, ...(data || {}) };
2446
+ const context = this.withGlobals(data);
2396
2447
  const filePath = join(this.templateDir, template);
2397
2448
 
2398
2449
  if (!existsSync(filePath)) {
@@ -2426,7 +2477,7 @@ export class Frond {
2426
2477
  }
2427
2478
 
2428
2479
  renderString(source: string, data?: Record<string, unknown>): string {
2429
- const context = { ...this.globals, ...(data || {}) };
2480
+ const context = this.withGlobals(data);
2430
2481
 
2431
2482
  const key = createHash("md5").update(source).digest("hex");
2432
2483
  const ttlMs = this.cacheTtlSeconds() * 1000;