@farm.js/core 0.1.0-beta.3 → 0.1.0-beta.5

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.
Files changed (193) hide show
  1. package/dist/agent-runtime.d.mts +2 -1
  2. package/dist/agent-runtime.d.ts +2 -1
  3. package/dist/api/route-manager.cjs +313 -10
  4. package/dist/api/route-manager.cjs.map +1 -1
  5. package/dist/api/route-manager.d.mts +3 -2
  6. package/dist/api/route-manager.d.ts +3 -2
  7. package/dist/api/route-manager.mjs +6 -5
  8. package/dist/api/runtime.cjs +313 -10
  9. package/dist/api/runtime.cjs.map +1 -1
  10. package/dist/api/runtime.d.mts +2 -1
  11. package/dist/api/runtime.d.ts +2 -1
  12. package/dist/api/runtime.mjs +4 -3
  13. package/dist/api.cjs +642 -57
  14. package/dist/api.cjs.map +1 -1
  15. package/dist/api.d.mts +3 -2
  16. package/dist/api.d.ts +3 -2
  17. package/dist/api.mjs +29 -9
  18. package/dist/{app-DOKxo-LH.d.ts → app-BjN-da13.d.ts} +38 -3
  19. package/dist/{app-BB3Iu3xh.d.mts → app-CakAr8Be.d.mts} +38 -3
  20. package/dist/app-markdown.d.mts +2 -1
  21. package/dist/app-markdown.d.ts +2 -1
  22. package/dist/cache.cjs +395 -12
  23. package/dist/cache.cjs.map +1 -1
  24. package/dist/cache.d.mts +100 -6
  25. package/dist/cache.d.ts +100 -6
  26. package/dist/cache.mjs +8 -2
  27. package/dist/{chunk-XUBZ7JI4.mjs → chunk-2CUKEMVR.mjs} +187 -16
  28. package/dist/chunk-2CUKEMVR.mjs.map +1 -0
  29. package/dist/{chunk-OHC6VCDI.mjs → chunk-2NHCNRF6.mjs} +521 -42
  30. package/dist/chunk-2NHCNRF6.mjs.map +1 -0
  31. package/dist/{chunk-6WXJ54TC.mjs → chunk-65A7QIKI.mjs} +5 -5
  32. package/dist/{chunk-6WXJ54TC.mjs.map → chunk-65A7QIKI.mjs.map} +1 -1
  33. package/dist/{chunk-5QH4IKY3.mjs → chunk-7N62DNSO.mjs} +2 -2
  34. package/dist/{chunk-XJSHJN4M.mjs → chunk-A2LUXHD2.mjs} +593 -49
  35. package/dist/chunk-A2LUXHD2.mjs.map +1 -0
  36. package/dist/chunk-ALCLXXTY.mjs +187 -0
  37. package/dist/chunk-ALCLXXTY.mjs.map +1 -0
  38. package/dist/{chunk-CSU4KJJF.mjs → chunk-C2ROET2V.mjs} +2 -2
  39. package/dist/{chunk-JNYVN7YP.mjs → chunk-EEABSBKK.mjs} +2 -2
  40. package/dist/{chunk-JNYVN7YP.mjs.map → chunk-EEABSBKK.mjs.map} +1 -1
  41. package/dist/{chunk-BIOIOWXZ.mjs → chunk-HNIQFZKB.mjs} +2 -2
  42. package/dist/{chunk-MH6FKW2I.mjs → chunk-J5YWQVI5.mjs} +4 -4
  43. package/dist/chunk-J5YWQVI5.mjs.map +1 -0
  44. package/dist/{chunk-G5I34UA6.mjs → chunk-JDEOUK5V.mjs} +4 -4
  45. package/dist/{chunk-T543TMFY.mjs → chunk-NM3CRAAL.mjs} +3 -2
  46. package/dist/chunk-NM3CRAAL.mjs.map +1 -0
  47. package/dist/{chunk-LWMGIEJA.mjs → chunk-PFE6GMNE.mjs} +47 -37
  48. package/dist/chunk-PFE6GMNE.mjs.map +1 -0
  49. package/dist/{chunk-6VS6CKAV.mjs → chunk-QM746KUO.mjs} +8 -1
  50. package/dist/chunk-QM746KUO.mjs.map +1 -0
  51. package/dist/{chunk-5FW6PQ3J.mjs → chunk-QTZZ2ISQ.mjs} +41 -16
  52. package/dist/chunk-QTZZ2ISQ.mjs.map +1 -0
  53. package/dist/{chunk-SHRJG6OG.mjs → chunk-RBRLLZL5.mjs} +18 -3
  54. package/dist/chunk-RBRLLZL5.mjs.map +1 -0
  55. package/dist/{chunk-NRRFGX45.mjs → chunk-TQAEEJB4.mjs} +3 -3
  56. package/dist/{chunk-45LKHSHJ.mjs → chunk-UB5UOSHB.mjs} +24 -12
  57. package/dist/chunk-UB5UOSHB.mjs.map +1 -0
  58. package/dist/chunk-V3XURYJO.mjs +756 -0
  59. package/dist/chunk-V3XURYJO.mjs.map +1 -0
  60. package/dist/{chunk-HZYXGV4K.mjs → chunk-VY2UYN5R.mjs} +24 -4
  61. package/dist/chunk-VY2UYN5R.mjs.map +1 -0
  62. package/dist/{chunk-C33RALN7.mjs → chunk-W2CRQFF6.mjs} +79 -15
  63. package/dist/chunk-W2CRQFF6.mjs.map +1 -0
  64. package/dist/{chunk-LG3YINQN.mjs → chunk-Z5CQMP54.mjs} +3 -3
  65. package/dist/{client-DiViK9yK.d.mts → client-BT1mFQas.d.mts} +23 -4
  66. package/dist/{client-DpEBJbts.d.ts → client-Nn0q4eO4.d.ts} +23 -4
  67. package/dist/{client-plugins-CTnsZz_A.d.ts → client-plugins-Cw4qUGsw.d.ts} +6 -0
  68. package/dist/{client-plugins-Cnr0D3kk.d.mts → client-plugins-DQQ1adXM.d.mts} +6 -0
  69. package/dist/client-plugins.d.mts +2 -1
  70. package/dist/client-plugins.d.ts +2 -1
  71. package/dist/client.cjs +665 -82
  72. package/dist/client.cjs.map +1 -1
  73. package/dist/client.d.mts +71 -5
  74. package/dist/client.d.ts +71 -5
  75. package/dist/client.mjs +10 -7
  76. package/dist/config.d.mts +2 -1
  77. package/dist/config.d.ts +2 -1
  78. package/dist/{devtools-HE4FTUDN.mjs → devtools-LVC3VB5P.mjs} +4 -4
  79. package/dist/docs.cjs +11 -1
  80. package/dist/docs.cjs.map +1 -1
  81. package/dist/docs.mjs +4 -4
  82. package/dist/index.cjs +2113 -198
  83. package/dist/index.cjs.map +1 -1
  84. package/dist/index.d.mts +16 -16
  85. package/dist/index.d.ts +16 -16
  86. package/dist/index.mjs +49 -21
  87. package/dist/index.mjs.map +1 -1
  88. package/dist/integrations.d.mts +2 -1
  89. package/dist/integrations.d.ts +2 -1
  90. package/dist/internal/build-runtime.cjs +1455 -90
  91. package/dist/internal/build-runtime.cjs.map +1 -1
  92. package/dist/internal/build-runtime.d.mts +4 -3
  93. package/dist/internal/build-runtime.d.ts +4 -3
  94. package/dist/internal/build-runtime.mjs +15 -14
  95. package/dist/internal/config-runtime.cjs +12 -1
  96. package/dist/internal/config-runtime.cjs.map +1 -1
  97. package/dist/internal/config-runtime.d.mts +3 -2
  98. package/dist/internal/config-runtime.d.ts +3 -2
  99. package/dist/internal/config-runtime.mjs +3 -3
  100. package/dist/internal/production-runtime.cjs +282 -6
  101. package/dist/internal/production-runtime.cjs.map +1 -1
  102. package/dist/internal/production-runtime.d.mts +4 -4
  103. package/dist/internal/production-runtime.d.ts +4 -4
  104. package/dist/internal/production-runtime.mjs +4 -2
  105. package/dist/{metadata-eoVmHoyv.d.ts → metadata-BAPg_Nmo.d.ts} +1 -1
  106. package/dist/{metadata-Dp8AdvsB.d.mts → metadata-_6i9oFG3.d.mts} +1 -1
  107. package/dist/middleware.d.mts +4 -3
  108. package/dist/middleware.d.ts +4 -3
  109. package/dist/navigation.cjs +40 -15
  110. package/dist/navigation.cjs.map +1 -1
  111. package/dist/navigation.mjs +2 -2
  112. package/dist/plugin.cjs +913 -53
  113. package/dist/plugin.cjs.map +1 -1
  114. package/dist/plugin.d.mts +2 -1
  115. package/dist/plugin.d.ts +2 -1
  116. package/dist/plugin.mjs +11 -10
  117. package/dist/{production-runtime-BqTnUTai.d.ts → production-runtime-BoJDc5mc.d.ts} +1 -1
  118. package/dist/{production-runtime-DtDdG9fc.d.mts → production-runtime-DPfgMV9Y.d.mts} +1 -1
  119. package/dist/routes.cjs +276 -6
  120. package/dist/routes.cjs.map +1 -1
  121. package/dist/routes.d.mts +1 -1
  122. package/dist/routes.d.ts +1 -1
  123. package/dist/routes.mjs +3 -3
  124. package/dist/{runtime-C1B7pSGT.d.mts → runtime-9nQjVM7X.d.mts} +8 -9
  125. package/dist/{runtime-CdPHt0bv.d.ts → runtime-DgeS2skB.d.ts} +8 -9
  126. package/dist/server-action-security.cjs +28 -10
  127. package/dist/server-action-security.cjs.map +1 -1
  128. package/dist/server-action-security.d.mts +2 -1
  129. package/dist/server-action-security.d.ts +2 -1
  130. package/dist/server-action-security.mjs +2 -2
  131. package/dist/server-fn-client.d.mts +1 -0
  132. package/dist/server-fn-client.d.ts +1 -0
  133. package/dist/server-fn.cjs +785 -3
  134. package/dist/server-fn.cjs.map +1 -1
  135. package/dist/server-fn.d.mts +10 -1
  136. package/dist/server-fn.d.ts +10 -1
  137. package/dist/server-fn.mjs +6 -3
  138. package/dist/server-plugins.d.mts +3 -2
  139. package/dist/server-plugins.d.ts +3 -2
  140. package/dist/server-query-client.cjs +279 -8
  141. package/dist/server-query-client.cjs.map +1 -1
  142. package/dist/server-query-client.mjs +4 -4
  143. package/dist/server-query.cjs +381 -10
  144. package/dist/server-query.cjs.map +1 -1
  145. package/dist/server-query.mjs +5 -5
  146. package/dist/server.cjs +1086 -76
  147. package/dist/server.cjs.map +1 -1
  148. package/dist/server.d.mts +8 -7
  149. package/dist/server.d.ts +8 -7
  150. package/dist/server.mjs +31 -16
  151. package/dist/server.mjs.map +1 -1
  152. package/dist/testing.cjs +341 -20
  153. package/dist/testing.cjs.map +1 -1
  154. package/dist/testing.d.mts +3 -2
  155. package/dist/testing.d.ts +3 -2
  156. package/dist/testing.mjs +7 -6
  157. package/dist/testing.mjs.map +1 -1
  158. package/dist/transport-BmbGTXA5.d.mts +49 -0
  159. package/dist/transport-BmbGTXA5.d.ts +49 -0
  160. package/dist/{utils-8USa5BiY.d.mts → utils-CHXlMDkg.d.mts} +1 -1
  161. package/dist/{utils-DKJb6K6G.d.ts → utils-Drsyz1Vj.d.ts} +1 -1
  162. package/dist/version.cjs +1 -1
  163. package/dist/version.cjs.map +1 -1
  164. package/dist/version.mjs +1 -1
  165. package/dist/vite.cjs +914 -54
  166. package/dist/vite.cjs.map +1 -1
  167. package/dist/vite.d.mts +2 -1
  168. package/dist/vite.d.ts +2 -1
  169. package/dist/vite.mjs +11 -10
  170. package/package.json +1 -1
  171. package/types/api.d.ts +44 -0
  172. package/types/client.d.ts +85 -4
  173. package/dist/chunk-45LKHSHJ.mjs.map +0 -1
  174. package/dist/chunk-5FW6PQ3J.mjs.map +0 -1
  175. package/dist/chunk-6VS6CKAV.mjs.map +0 -1
  176. package/dist/chunk-C33RALN7.mjs.map +0 -1
  177. package/dist/chunk-GZWT6VI5.mjs +0 -382
  178. package/dist/chunk-GZWT6VI5.mjs.map +0 -1
  179. package/dist/chunk-HZYXGV4K.mjs.map +0 -1
  180. package/dist/chunk-LWMGIEJA.mjs.map +0 -1
  181. package/dist/chunk-MH6FKW2I.mjs.map +0 -1
  182. package/dist/chunk-OHC6VCDI.mjs.map +0 -1
  183. package/dist/chunk-SHRJG6OG.mjs.map +0 -1
  184. package/dist/chunk-T543TMFY.mjs.map +0 -1
  185. package/dist/chunk-XJSHJN4M.mjs.map +0 -1
  186. package/dist/chunk-XUBZ7JI4.mjs.map +0 -1
  187. /package/dist/{chunk-5QH4IKY3.mjs.map → chunk-7N62DNSO.mjs.map} +0 -0
  188. /package/dist/{chunk-CSU4KJJF.mjs.map → chunk-C2ROET2V.mjs.map} +0 -0
  189. /package/dist/{chunk-BIOIOWXZ.mjs.map → chunk-HNIQFZKB.mjs.map} +0 -0
  190. /package/dist/{chunk-G5I34UA6.mjs.map → chunk-JDEOUK5V.mjs.map} +0 -0
  191. /package/dist/{chunk-NRRFGX45.mjs.map → chunk-TQAEEJB4.mjs.map} +0 -0
  192. /package/dist/{chunk-LG3YINQN.mjs.map → chunk-Z5CQMP54.mjs.map} +0 -0
  193. /package/dist/{devtools-HE4FTUDN.mjs.map → devtools-LVC3VB5P.mjs.map} +0 -0
@@ -1,6 +1,6 @@
1
- export { a4 as loadConfig, a3 as resolveConfig, a5 as resolveDeployConfig } from '../client-plugins-Cnr0D3kk.mjs';
1
+ export { a4 as loadConfig, a3 as resolveConfig, a5 as resolveDeployConfig } from '../client-plugins-DQQ1adXM.mjs';
2
2
  import { build, createServer, loadEnv } from 'vite';
3
- export { l as logger } from '../utils-8USa5BiY.mjs';
3
+ export { l as logger } from '../utils-CHXlMDkg.mjs';
4
4
  import '../plugin-BJDf43dc.mjs';
5
5
  import 'react';
6
6
  import 'http';
@@ -19,6 +19,7 @@ import '../env.mjs';
19
19
  import '../server-action-security.mjs';
20
20
  import '../image-config-Duob7wER.mjs';
21
21
  import '../types-BH1lhg5G.mjs';
22
+ import '../cache.mjs';
22
23
  import '../route-runtime-DoOHzCAD.mjs';
23
24
 
24
25
  type FarmProductionViteBuilder = "rollup" | "rolldown";
@@ -1,6 +1,6 @@
1
- export { a4 as loadConfig, a3 as resolveConfig, a5 as resolveDeployConfig } from '../client-plugins-CTnsZz_A.js';
1
+ export { a4 as loadConfig, a3 as resolveConfig, a5 as resolveDeployConfig } from '../client-plugins-Cw4qUGsw.js';
2
2
  import { build, createServer, loadEnv } from 'vite';
3
- export { l as logger } from '../utils-DKJb6K6G.js';
3
+ export { l as logger } from '../utils-Drsyz1Vj.js';
4
4
  import '../plugin-BJDf43dc.js';
5
5
  import 'react';
6
6
  import 'http';
@@ -19,6 +19,7 @@ import '../env.js';
19
19
  import '../server-action-security.js';
20
20
  import '../image-config-Duob7wER.js';
21
21
  import '../types-BH1lhg5G.js';
22
+ import '../cache.js';
22
23
  import '../route-runtime-DoOHzCAD.js';
23
24
 
24
25
  type FarmProductionViteBuilder = "rollup" | "rolldown";
@@ -5,14 +5,14 @@ import {
5
5
  loadConfig,
6
6
  resolveConfig,
7
7
  resolveDeployConfig
8
- } from "../chunk-T543TMFY.mjs";
8
+ } from "../chunk-NM3CRAAL.mjs";
9
9
  import "../chunk-UVCIEUJC.mjs";
10
10
  import "../chunk-RSKVYESE.mjs";
11
11
  import "../chunk-74YTBPTY.mjs";
12
12
  import "../chunk-7MABZJIM.mjs";
13
13
  import "../chunk-YW7OBE6Q.mjs";
14
14
  import "../chunk-GY2STC2S.mjs";
15
- import "../chunk-45LKHSHJ.mjs";
15
+ import "../chunk-UB5UOSHB.mjs";
16
16
  import "../chunk-LLC7BNXW.mjs";
17
17
  import "../chunk-QGQX7SFC.mjs";
18
18
  import "../chunk-XIVV5IWN.mjs";
@@ -26,7 +26,7 @@ import {
26
26
  } from "../chunk-J4FNJ5IU.mjs";
27
27
  import "../chunk-B232I2GP.mjs";
28
28
  import "../chunk-JK25YEN6.mjs";
29
- import "../chunk-SHRJG6OG.mjs";
29
+ import "../chunk-RBRLLZL5.mjs";
30
30
  import "../chunk-CGILA3WO.mjs";
31
31
  export {
32
32
  loadConfig,
@@ -27,6 +27,7 @@ __export(production_runtime_exports, {
27
27
  _runWithMiddlewareData: () => _runWithMiddlewareData,
28
28
  addMetadataImageReference: () => addMetadataImageReference,
29
29
  applyProductionMiddlewareHeaders: () => applyProductionMiddlewareHeaders,
30
+ configureFarmCache: () => configureFarmCache,
30
31
  configureFarmObservability: () => configureFarmObservability,
31
32
  createFarmCacheKey: () => createFarmCacheKey,
32
33
  createFarmLocaleCookie: () => createFarmLocaleCookie,
@@ -401,11 +402,51 @@ __name(formatError, "formatError");
401
402
 
402
403
  // src/cache.ts
403
404
  var _FarmDataCache = class _FarmDataCache {
404
- constructor() {
405
+ constructor(config = {}) {
405
406
  this.entries = /* @__PURE__ */ new Map();
406
407
  this.inflight = /* @__PURE__ */ new Map();
407
408
  this.invalidatedTagVersions = /* @__PURE__ */ new Map();
408
409
  this.version = 0;
410
+ this.namespace = "farm";
411
+ this.local = true;
412
+ this.lease = {
413
+ enabled: true,
414
+ ttlMs: 1e4,
415
+ waitTimeoutMs: 1e4,
416
+ pollIntervalMs: 25
417
+ };
418
+ this.configure(config);
419
+ }
420
+ configure(config = {}) {
421
+ this.adapter = config.adapter;
422
+ this.namespace = normalizeCacheNamespace(config.namespace || "farm");
423
+ this.local = !config.adapter;
424
+ this.lease = config.lease === false ? { ...this.lease, enabled: false } : {
425
+ enabled: true,
426
+ ttlMs: normalizePositiveDuration(config.lease?.ttlMs, 1e4, "cache.lease.ttlMs"),
427
+ waitTimeoutMs: normalizePositiveDuration(
428
+ config.lease?.waitTimeoutMs,
429
+ 1e4,
430
+ "cache.lease.waitTimeoutMs"
431
+ ),
432
+ pollIntervalMs: normalizePositiveDuration(
433
+ config.lease?.pollIntervalMs,
434
+ 25,
435
+ "cache.lease.pollIntervalMs"
436
+ )
437
+ };
438
+ if (!this.local) {
439
+ this.entries.clear();
440
+ this.inflight.clear();
441
+ this.invalidatedTagVersions.clear();
442
+ this.version = 0;
443
+ }
444
+ }
445
+ get adapterName() {
446
+ return this.adapter?.name || (this.adapter ? "custom" : "memory");
447
+ }
448
+ get hasAdapter() {
449
+ return this.adapter !== void 0;
409
450
  }
410
451
  get size() {
411
452
  return this.entries.size;
@@ -441,6 +482,45 @@ var _FarmDataCache = class _FarmDataCache {
441
482
  });
442
483
  return this.toPublicEntry(entry);
443
484
  }
485
+ async getEntryAsync(key, options = {}) {
486
+ if (this.local) {
487
+ const localEntry = this.getEntry(key, options);
488
+ if (localEntry) return localEntry;
489
+ }
490
+ if (!this.adapter) {
491
+ return this.local ? void 0 : this.getEntry(key, options);
492
+ }
493
+ const entry = await this.adapter.get(this.createAdapterKey(key));
494
+ if (!entry) {
495
+ emitFarmEvent({ type: "cache.miss", key });
496
+ return void 0;
497
+ }
498
+ assertFarmCacheEntry(entry, key);
499
+ const stale = await this.isAdapterEntryStale(entry, options.now);
500
+ if (stale) {
501
+ emitFarmEvent({
502
+ type: "cache.stale",
503
+ key,
504
+ tags: [...entry.tags],
505
+ revalidate: entry.revalidate
506
+ });
507
+ if (!options.allowStale) {
508
+ emitFarmEvent({ type: "cache.miss", key, reason: "stale" });
509
+ return void 0;
510
+ }
511
+ }
512
+ emitFarmEvent({
513
+ type: "cache.hit",
514
+ key,
515
+ tags: [...entry.tags],
516
+ revalidate: entry.revalidate,
517
+ stale
518
+ });
519
+ if (this.local && !stale) {
520
+ this.hydrateLocalEntry(entry);
521
+ }
522
+ return { ...entry, key };
523
+ }
444
524
  set(key, value, options = {}) {
445
525
  const tags = /* @__PURE__ */ new Set();
446
526
  for (const tag of options.tags ?? []) {
@@ -453,6 +533,7 @@ var _FarmDataCache = class _FarmDataCache {
453
533
  key,
454
534
  value,
455
535
  tags,
536
+ tagVersions: void 0,
456
537
  createdAt: options.createdAt ?? Date.now(),
457
538
  createdVersion: ++this.version,
458
539
  revalidate: normalizeRevalidate(options.revalidate)
@@ -466,11 +547,45 @@ var _FarmDataCache = class _FarmDataCache {
466
547
  });
467
548
  return this.toPublicEntry(entry);
468
549
  }
550
+ async setAsync(key, value, options = {}, tagVersions) {
551
+ const tags = normalizeCacheOptionsTags(options);
552
+ const capturedVersions = tagVersions ?? await this.getAdapterTagVersions(Array.from(tags.values()));
553
+ const entry = {
554
+ key,
555
+ value,
556
+ tags: Array.from(tags),
557
+ tagVersions: capturedVersions,
558
+ createdAt: options.createdAt ?? Date.now(),
559
+ createdVersion: ++this.version,
560
+ revalidate: normalizeRevalidate(options.revalidate)
561
+ };
562
+ if (this.local) {
563
+ this.hydrateLocalEntry(entry);
564
+ }
565
+ if (this.adapter) {
566
+ await this.adapter.set(this.createAdapterKey(key), entry);
567
+ }
568
+ emitFarmEvent({
569
+ type: "cache.set",
570
+ key,
571
+ tags: [...entry.tags],
572
+ revalidate: entry.revalidate
573
+ });
574
+ return entry;
575
+ }
469
576
  delete(key) {
470
577
  const deleted = this.entries.delete(key);
471
578
  emitFarmEvent({ type: "cache.delete", key, deleted });
472
579
  return deleted;
473
580
  }
581
+ async deleteAsync(key) {
582
+ const deleted = this.entries.delete(key);
583
+ if (this.adapter) {
584
+ await this.adapter.delete(this.createAdapterKey(key));
585
+ }
586
+ emitFarmEvent({ type: "cache.delete", key, deleted: this.adapter ? true : deleted });
587
+ return this.adapter ? true : deleted;
588
+ }
474
589
  clear() {
475
590
  const count = this.entries.size;
476
591
  this.entries.clear();
@@ -479,6 +594,10 @@ var _FarmDataCache = class _FarmDataCache {
479
594
  this.version = 0;
480
595
  emitFarmEvent({ type: "cache.clear", count });
481
596
  }
597
+ async clearAsync() {
598
+ this.clear();
599
+ await this.adapter?.clear?.();
600
+ }
482
601
  isStale(entry, now = Date.now()) {
483
602
  if (typeof entry.revalidate === "number" && entry.revalidate > 0 && now - entry.createdAt >= entry.revalidate * 1e3) {
484
603
  return true;
@@ -491,6 +610,12 @@ var _FarmDataCache = class _FarmDataCache {
491
610
  }
492
611
  return false;
493
612
  }
613
+ async isStaleAsync(entry, now = Date.now()) {
614
+ if (this.adapter) {
615
+ return this.isAdapterEntryStale(entry, now);
616
+ }
617
+ return this.isStale(entry, now);
618
+ }
494
619
  revalidateTag(tag, options = {}) {
495
620
  const normalized = normalizeCacheTag(tag);
496
621
  const count = this.invalidateTag(normalized);
@@ -499,6 +624,15 @@ var _FarmDataCache = class _FarmDataCache {
499
624
  );
500
625
  return count;
501
626
  }
627
+ async revalidateTagAsync(tag, options = {}) {
628
+ const normalized = normalizeCacheTag(tag);
629
+ const count = this.invalidateTag(normalized);
630
+ await this.adapter?.invalidateTags?.([this.createAdapterTag(normalized)]);
631
+ emitFarmEvent(
632
+ options.source === "updateTag" ? { type: "cache.updateTag", tag: normalized, count } : { type: "cache.revalidateTag", tag: normalized, profile: options.profile, count }
633
+ );
634
+ return count;
635
+ }
502
636
  revalidatePath(routePath) {
503
637
  const normalizedPath = normalizeRevalidatePath(routePath);
504
638
  const pathTag = createPathCacheTag(normalizedPath);
@@ -515,8 +649,25 @@ var _FarmDataCache = class _FarmDataCache {
515
649
  }
516
650
  return count;
517
651
  }
652
+ async revalidatePathAsync(routePath) {
653
+ const normalizedPath = normalizeRevalidatePath(routePath);
654
+ const pathTag = createPathCacheTag(normalizedPath);
655
+ const pprCount = this.countEntriesForTags([pathTag, "ppr"]);
656
+ const count = this.invalidateTag(pathTag);
657
+ await this.adapter?.invalidateTags?.([this.createAdapterTag(pathTag)]);
658
+ emitFarmEvent({ type: "cache.revalidatePath", path: normalizedPath, count });
659
+ if (pprCount > 0) {
660
+ emitFarmEvent({
661
+ type: "ppr.shell.invalidated",
662
+ route: normalizedPath,
663
+ reason: "revalidatePath",
664
+ count: pprCount
665
+ });
666
+ }
667
+ return count;
668
+ }
518
669
  async getOrSet(key, producer, options = {}) {
519
- const cached = this.getEntry(key);
670
+ const cached = await this.getEntryAsync(key);
520
671
  if (cached) {
521
672
  return cached.value;
522
673
  }
@@ -525,10 +676,8 @@ var _FarmDataCache = class _FarmDataCache {
525
676
  emitFarmEvent({ type: "cache.dedupe", key });
526
677
  return inflight;
527
678
  }
528
- const promise = Promise.resolve().then(producer).then((value) => {
529
- this.set(key, value, options);
530
- return value;
531
- }).catch((error) => {
679
+ const tags = Array.from(normalizeCacheOptionsTags(options));
680
+ const promise = this.fillCacheEntry(key, producer, options, tags).catch((error) => {
532
681
  emitFarmEvent({ type: "cache.error", key, operation: "set", error });
533
682
  throw error;
534
683
  }).finally(() => {
@@ -537,6 +686,40 @@ var _FarmDataCache = class _FarmDataCache {
537
686
  this.inflight.set(key, promise);
538
687
  return promise;
539
688
  }
689
+ async fillCacheEntry(key, producer, options, tags) {
690
+ const leaseKey = `${this.namespace}:lease:${key}`;
691
+ let leaseToken;
692
+ if (this.adapter?.acquireLease && this.adapter.releaseLease && this.lease.enabled) {
693
+ leaseToken = await this.adapter.acquireLease(leaseKey, this.lease.ttlMs);
694
+ if (!leaseToken) {
695
+ const shared = await this.waitForAdapterEntry(key);
696
+ if (shared) {
697
+ emitFarmEvent({ type: "cache.dedupe", key });
698
+ return shared.value;
699
+ }
700
+ leaseToken = await this.adapter.acquireLease(leaseKey, this.lease.ttlMs);
701
+ }
702
+ }
703
+ try {
704
+ const initialTagVersions = await this.getAdapterTagVersions(tags);
705
+ const value = await producer();
706
+ await this.setAsync(key, value, options, initialTagVersions);
707
+ return value;
708
+ } finally {
709
+ if (leaseToken && this.adapter?.releaseLease) {
710
+ await this.adapter.releaseLease(leaseKey, leaseToken);
711
+ }
712
+ }
713
+ }
714
+ async waitForAdapterEntry(key) {
715
+ const deadline = Date.now() + this.lease.waitTimeoutMs;
716
+ while (Date.now() < deadline) {
717
+ await delay(this.lease.pollIntervalMs);
718
+ const entry = await this.getEntryAsync(key);
719
+ if (entry) return entry;
720
+ }
721
+ return void 0;
722
+ }
540
723
  countEntriesForTag(tag) {
541
724
  let count = 0;
542
725
  for (const entry of this.entries.values()) {
@@ -564,11 +747,53 @@ var _FarmDataCache = class _FarmDataCache {
564
747
  key: entry.key,
565
748
  value: entry.value,
566
749
  tags: Array.from(entry.tags),
750
+ tagVersions: entry.tagVersions,
567
751
  createdAt: entry.createdAt,
568
752
  createdVersion: entry.createdVersion,
569
753
  revalidate: entry.revalidate
570
754
  };
571
755
  }
756
+ hydrateLocalEntry(entry) {
757
+ this.entries.set(entry.key, {
758
+ key: entry.key,
759
+ value: entry.value,
760
+ tags: new Set(entry.tags.map(normalizeCacheTag)),
761
+ tagVersions: entry.tagVersions,
762
+ createdAt: entry.createdAt,
763
+ createdVersion: entry.createdVersion ?? ++this.version,
764
+ revalidate: normalizeRevalidate(entry.revalidate)
765
+ });
766
+ }
767
+ createAdapterKey(key) {
768
+ return `${this.namespace}:entry:${key}`;
769
+ }
770
+ createAdapterTag(tag) {
771
+ return `${this.namespace}:tag:${tag}`;
772
+ }
773
+ async getAdapterTagVersions(tags) {
774
+ if (!this.adapter?.getTagVersions || tags.length === 0) return {};
775
+ const normalized = tags.map(normalizeCacheTag);
776
+ const physicalTags = normalized.map((tag) => this.createAdapterTag(tag));
777
+ const versions = await this.adapter.getTagVersions(physicalTags);
778
+ return Object.fromEntries(
779
+ normalized.map((tag, index) => [
780
+ tag,
781
+ normalizeAdapterVersion(versions[physicalTags[index]])
782
+ ])
783
+ );
784
+ }
785
+ async isAdapterEntryStale(entry, now = Date.now()) {
786
+ if (typeof entry.revalidate === "number" && entry.revalidate > 0 && now - entry.createdAt >= entry.revalidate * 1e3) {
787
+ return true;
788
+ }
789
+ const currentVersions = await this.getAdapterTagVersions(entry.tags);
790
+ for (const tag of entry.tags) {
791
+ if (normalizeAdapterVersion(currentVersions[tag]) > normalizeAdapterVersion(entry.tagVersions?.[tag])) {
792
+ return true;
793
+ }
794
+ }
795
+ return false;
796
+ }
572
797
  };
573
798
  __name(_FarmDataCache, "FarmDataCache");
574
799
  var FarmDataCache = _FarmDataCache;
@@ -581,6 +806,10 @@ function getFarmDataCache() {
581
806
  return sharedFarmDataCache;
582
807
  }
583
808
  __name(getFarmDataCache, "getFarmDataCache");
809
+ function configureFarmCache(config) {
810
+ sharedFarmDataCache.configure(config);
811
+ }
812
+ __name(configureFarmCache, "configureFarmCache");
584
813
  function createPathCacheTag(routePath) {
585
814
  return `path:${normalizeRevalidatePath(routePath)}`;
586
815
  }
@@ -633,6 +862,52 @@ function normalizeCacheTag(tag) {
633
862
  return normalized;
634
863
  }
635
864
  __name(normalizeCacheTag, "normalizeCacheTag");
865
+ function normalizeCacheNamespace(namespace) {
866
+ if (typeof namespace !== "string") {
867
+ throw new TypeError("Cache namespace must be a string.");
868
+ }
869
+ const normalized = namespace.trim().replace(/:+$/g, "");
870
+ if (!normalized) {
871
+ throw new Error("Cache namespace cannot be empty.");
872
+ }
873
+ return normalized;
874
+ }
875
+ __name(normalizeCacheNamespace, "normalizeCacheNamespace");
876
+ function normalizeAdapterVersion(value) {
877
+ return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0;
878
+ }
879
+ __name(normalizeAdapterVersion, "normalizeAdapterVersion");
880
+ function normalizePositiveDuration(value, fallback, name) {
881
+ if (value === void 0) return fallback;
882
+ if (!Number.isFinite(value) || value <= 0) {
883
+ throw new TypeError(`${name} must be a positive number of milliseconds.`);
884
+ }
885
+ return Math.floor(value);
886
+ }
887
+ __name(normalizePositiveDuration, "normalizePositiveDuration");
888
+ function delay(ms) {
889
+ return new Promise((resolve) => setTimeout(resolve, ms));
890
+ }
891
+ __name(delay, "delay");
892
+ function normalizeCacheOptionsTags(options) {
893
+ const tags = /* @__PURE__ */ new Set();
894
+ for (const tag of options.tags ?? []) {
895
+ tags.add(normalizeCacheTag(tag));
896
+ }
897
+ for (const routePath of options.paths ?? []) {
898
+ tags.add(createPathCacheTag(routePath));
899
+ }
900
+ return tags;
901
+ }
902
+ __name(normalizeCacheOptionsTags, "normalizeCacheOptionsTags");
903
+ function assertFarmCacheEntry(entry, expectedKey) {
904
+ if (!entry || typeof entry !== "object" || entry.key !== expectedKey || !Array.isArray(entry.tags) || typeof entry.createdAt !== "number") {
905
+ throw new TypeError(
906
+ `Cache adapter returned an invalid entry for ${JSON.stringify(expectedKey)}.`
907
+ );
908
+ }
909
+ }
910
+ __name(assertFarmCacheEntry, "assertFarmCacheEntry");
636
911
  function getFunctionCacheIdentity(fn) {
637
912
  if (fn.name) {
638
913
  return `name:${fn.name}`;
@@ -1873,6 +2148,7 @@ __name(withFarmRouteContext, "withFarmRouteContext");
1873
2148
  _runWithMiddlewareData,
1874
2149
  addMetadataImageReference,
1875
2150
  applyProductionMiddlewareHeaders,
2151
+ configureFarmCache,
1876
2152
  configureFarmObservability,
1877
2153
  createFarmCacheKey,
1878
2154
  createFarmLocaleCookie,