@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
package/dist/cache.cjs CHANGED
@@ -22,7 +22,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
22
22
  var cache_exports = {};
23
23
  __export(cache_exports, {
24
24
  FarmDataCache: () => FarmDataCache,
25
+ applyFarmCacheInvalidationTargets: () => applyFarmCacheInvalidationTargets,
25
26
  applyFarmCacheInvalidations: () => applyFarmCacheInvalidations,
27
+ configureFarmCache: () => configureFarmCache,
26
28
  createFarmCacheKey: () => createFarmCacheKey,
27
29
  createPathCacheTag: () => createPathCacheTag,
28
30
  createRouteDataCacheKey: () => createRouteDataCacheKey,
@@ -34,6 +36,7 @@ __export(cache_exports, {
34
36
  normalizeRevalidatePath: () => normalizeRevalidatePath,
35
37
  revalidatePath: () => revalidatePath,
36
38
  revalidateTag: () => revalidateTag,
39
+ storageCacheAdapter: () => storageCacheAdapter,
37
40
  unstable_cache: () => unstable_cache,
38
41
  updateTag: () => updateTag
39
42
  });
@@ -159,7 +162,8 @@ var FARM_CACHE_INVALIDATION_STATE = /* @__PURE__ */ Symbol.for("farm.cacheInvali
159
162
  var globalState = globalThis;
160
163
  function getFarmCacheInvalidationState() {
161
164
  return globalState[FARM_CACHE_INVALIDATION_STATE] ?? (globalState[FARM_CACHE_INVALIDATION_STATE] = {
162
- listeners: /* @__PURE__ */ new Set()
165
+ listeners: /* @__PURE__ */ new Set(),
166
+ taskListeners: /* @__PURE__ */ new Set()
163
167
  });
164
168
  }
165
169
  __name(getFarmCacheInvalidationState, "getFarmCacheInvalidationState");
@@ -170,6 +174,12 @@ function notifyFarmCacheInvalidation(key) {
170
174
  }
171
175
  }
172
176
  __name(notifyFarmCacheInvalidation, "notifyFarmCacheInvalidation");
177
+ function notifyFarmCacheTask(task) {
178
+ for (const listener of getFarmCacheInvalidationState().taskListeners) {
179
+ listener(task);
180
+ }
181
+ }
182
+ __name(notifyFarmCacheTask, "notifyFarmCacheTask");
173
183
  function applyFarmCacheInvalidations(keys) {
174
184
  if (!Array.isArray(keys)) return;
175
185
  for (const key of keys) {
@@ -208,12 +218,92 @@ function defineCacheKey() {
208
218
  };
209
219
  }
210
220
  __name(defineCacheKey, "defineCacheKey");
221
+ function storageCacheAdapter(storage, options = {}) {
222
+ if (!storage || typeof storage.getItem !== "function" || typeof storage.setItem !== "function") {
223
+ throw new TypeError("storageCacheAdapter expects a compatible storage client.");
224
+ }
225
+ const base = normalizeCacheNamespace(options.base || "farm-cache");
226
+ const entryKey = /* @__PURE__ */ __name((key) => `${base}:entry:${key}`, "entryKey");
227
+ const tagKey = /* @__PURE__ */ __name((tag) => `${base}:tag:${tag}`, "tagKey");
228
+ return {
229
+ name: "storage",
230
+ get: /* @__PURE__ */ __name((key) => storage.getItem(entryKey(key)), "get"),
231
+ set: /* @__PURE__ */ __name(async (key, entry) => {
232
+ await storage.setItem(entryKey(key), entry);
233
+ }, "set"),
234
+ delete: /* @__PURE__ */ __name(async (key) => {
235
+ await storage.removeItem(entryKey(key));
236
+ }, "delete"),
237
+ clear: storage.clear ? async () => {
238
+ await storage.clear(base);
239
+ } : void 0,
240
+ getTagVersions: /* @__PURE__ */ __name(async (tags) => {
241
+ const versions = await Promise.all(
242
+ tags.map(async (tag) => {
243
+ const value = await storage.getItem(tagKey(tag));
244
+ return [tag, normalizeAdapterVersion(value)];
245
+ })
246
+ );
247
+ return Object.fromEntries(versions);
248
+ }, "getTagVersions"),
249
+ invalidateTags: /* @__PURE__ */ __name(async (tags) => {
250
+ await Promise.all(
251
+ tags.map(async (tag) => {
252
+ const key = tagKey(tag);
253
+ const current = normalizeAdapterVersion(await storage.getItem(key));
254
+ await storage.setItem(key, Math.max(Date.now(), current + 1));
255
+ })
256
+ );
257
+ }, "invalidateTags")
258
+ };
259
+ }
260
+ __name(storageCacheAdapter, "storageCacheAdapter");
211
261
  var _FarmDataCache = class _FarmDataCache {
212
- constructor() {
262
+ constructor(config = {}) {
213
263
  this.entries = /* @__PURE__ */ new Map();
214
264
  this.inflight = /* @__PURE__ */ new Map();
215
265
  this.invalidatedTagVersions = /* @__PURE__ */ new Map();
216
266
  this.version = 0;
267
+ this.namespace = "farm";
268
+ this.local = true;
269
+ this.lease = {
270
+ enabled: true,
271
+ ttlMs: 1e4,
272
+ waitTimeoutMs: 1e4,
273
+ pollIntervalMs: 25
274
+ };
275
+ this.configure(config);
276
+ }
277
+ configure(config = {}) {
278
+ this.adapter = config.adapter;
279
+ this.namespace = normalizeCacheNamespace(config.namespace || "farm");
280
+ this.local = !config.adapter;
281
+ this.lease = config.lease === false ? { ...this.lease, enabled: false } : {
282
+ enabled: true,
283
+ ttlMs: normalizePositiveDuration(config.lease?.ttlMs, 1e4, "cache.lease.ttlMs"),
284
+ waitTimeoutMs: normalizePositiveDuration(
285
+ config.lease?.waitTimeoutMs,
286
+ 1e4,
287
+ "cache.lease.waitTimeoutMs"
288
+ ),
289
+ pollIntervalMs: normalizePositiveDuration(
290
+ config.lease?.pollIntervalMs,
291
+ 25,
292
+ "cache.lease.pollIntervalMs"
293
+ )
294
+ };
295
+ if (!this.local) {
296
+ this.entries.clear();
297
+ this.inflight.clear();
298
+ this.invalidatedTagVersions.clear();
299
+ this.version = 0;
300
+ }
301
+ }
302
+ get adapterName() {
303
+ return this.adapter?.name || (this.adapter ? "custom" : "memory");
304
+ }
305
+ get hasAdapter() {
306
+ return this.adapter !== void 0;
217
307
  }
218
308
  get size() {
219
309
  return this.entries.size;
@@ -249,6 +339,45 @@ var _FarmDataCache = class _FarmDataCache {
249
339
  });
250
340
  return this.toPublicEntry(entry);
251
341
  }
342
+ async getEntryAsync(key, options = {}) {
343
+ if (this.local) {
344
+ const localEntry = this.getEntry(key, options);
345
+ if (localEntry) return localEntry;
346
+ }
347
+ if (!this.adapter) {
348
+ return this.local ? void 0 : this.getEntry(key, options);
349
+ }
350
+ const entry = await this.adapter.get(this.createAdapterKey(key));
351
+ if (!entry) {
352
+ emitFarmEvent({ type: "cache.miss", key });
353
+ return void 0;
354
+ }
355
+ assertFarmCacheEntry(entry, key);
356
+ const stale = await this.isAdapterEntryStale(entry, options.now);
357
+ if (stale) {
358
+ emitFarmEvent({
359
+ type: "cache.stale",
360
+ key,
361
+ tags: [...entry.tags],
362
+ revalidate: entry.revalidate
363
+ });
364
+ if (!options.allowStale) {
365
+ emitFarmEvent({ type: "cache.miss", key, reason: "stale" });
366
+ return void 0;
367
+ }
368
+ }
369
+ emitFarmEvent({
370
+ type: "cache.hit",
371
+ key,
372
+ tags: [...entry.tags],
373
+ revalidate: entry.revalidate,
374
+ stale
375
+ });
376
+ if (this.local && !stale) {
377
+ this.hydrateLocalEntry(entry);
378
+ }
379
+ return { ...entry, key };
380
+ }
252
381
  set(key, value, options = {}) {
253
382
  const tags = /* @__PURE__ */ new Set();
254
383
  for (const tag of options.tags ?? []) {
@@ -261,6 +390,7 @@ var _FarmDataCache = class _FarmDataCache {
261
390
  key,
262
391
  value,
263
392
  tags,
393
+ tagVersions: void 0,
264
394
  createdAt: options.createdAt ?? Date.now(),
265
395
  createdVersion: ++this.version,
266
396
  revalidate: normalizeRevalidate(options.revalidate)
@@ -274,11 +404,45 @@ var _FarmDataCache = class _FarmDataCache {
274
404
  });
275
405
  return this.toPublicEntry(entry);
276
406
  }
407
+ async setAsync(key, value, options = {}, tagVersions) {
408
+ const tags = normalizeCacheOptionsTags(options);
409
+ const capturedVersions = tagVersions ?? await this.getAdapterTagVersions(Array.from(tags.values()));
410
+ const entry = {
411
+ key,
412
+ value,
413
+ tags: Array.from(tags),
414
+ tagVersions: capturedVersions,
415
+ createdAt: options.createdAt ?? Date.now(),
416
+ createdVersion: ++this.version,
417
+ revalidate: normalizeRevalidate(options.revalidate)
418
+ };
419
+ if (this.local) {
420
+ this.hydrateLocalEntry(entry);
421
+ }
422
+ if (this.adapter) {
423
+ await this.adapter.set(this.createAdapterKey(key), entry);
424
+ }
425
+ emitFarmEvent({
426
+ type: "cache.set",
427
+ key,
428
+ tags: [...entry.tags],
429
+ revalidate: entry.revalidate
430
+ });
431
+ return entry;
432
+ }
277
433
  delete(key) {
278
434
  const deleted = this.entries.delete(key);
279
435
  emitFarmEvent({ type: "cache.delete", key, deleted });
280
436
  return deleted;
281
437
  }
438
+ async deleteAsync(key) {
439
+ const deleted = this.entries.delete(key);
440
+ if (this.adapter) {
441
+ await this.adapter.delete(this.createAdapterKey(key));
442
+ }
443
+ emitFarmEvent({ type: "cache.delete", key, deleted: this.adapter ? true : deleted });
444
+ return this.adapter ? true : deleted;
445
+ }
282
446
  clear() {
283
447
  const count = this.entries.size;
284
448
  this.entries.clear();
@@ -287,6 +451,10 @@ var _FarmDataCache = class _FarmDataCache {
287
451
  this.version = 0;
288
452
  emitFarmEvent({ type: "cache.clear", count });
289
453
  }
454
+ async clearAsync() {
455
+ this.clear();
456
+ await this.adapter?.clear?.();
457
+ }
290
458
  isStale(entry, now = Date.now()) {
291
459
  if (typeof entry.revalidate === "number" && entry.revalidate > 0 && now - entry.createdAt >= entry.revalidate * 1e3) {
292
460
  return true;
@@ -299,6 +467,12 @@ var _FarmDataCache = class _FarmDataCache {
299
467
  }
300
468
  return false;
301
469
  }
470
+ async isStaleAsync(entry, now = Date.now()) {
471
+ if (this.adapter) {
472
+ return this.isAdapterEntryStale(entry, now);
473
+ }
474
+ return this.isStale(entry, now);
475
+ }
302
476
  revalidateTag(tag, options = {}) {
303
477
  const normalized = normalizeCacheTag(tag);
304
478
  const count = this.invalidateTag(normalized);
@@ -307,6 +481,15 @@ var _FarmDataCache = class _FarmDataCache {
307
481
  );
308
482
  return count;
309
483
  }
484
+ async revalidateTagAsync(tag, options = {}) {
485
+ const normalized = normalizeCacheTag(tag);
486
+ const count = this.invalidateTag(normalized);
487
+ await this.adapter?.invalidateTags?.([this.createAdapterTag(normalized)]);
488
+ emitFarmEvent(
489
+ options.source === "updateTag" ? { type: "cache.updateTag", tag: normalized, count } : { type: "cache.revalidateTag", tag: normalized, profile: options.profile, count }
490
+ );
491
+ return count;
492
+ }
310
493
  revalidatePath(routePath) {
311
494
  const normalizedPath = normalizeRevalidatePath(routePath);
312
495
  const pathTag = createPathCacheTag(normalizedPath);
@@ -323,8 +506,25 @@ var _FarmDataCache = class _FarmDataCache {
323
506
  }
324
507
  return count;
325
508
  }
509
+ async revalidatePathAsync(routePath) {
510
+ const normalizedPath = normalizeRevalidatePath(routePath);
511
+ const pathTag = createPathCacheTag(normalizedPath);
512
+ const pprCount = this.countEntriesForTags([pathTag, "ppr"]);
513
+ const count = this.invalidateTag(pathTag);
514
+ await this.adapter?.invalidateTags?.([this.createAdapterTag(pathTag)]);
515
+ emitFarmEvent({ type: "cache.revalidatePath", path: normalizedPath, count });
516
+ if (pprCount > 0) {
517
+ emitFarmEvent({
518
+ type: "ppr.shell.invalidated",
519
+ route: normalizedPath,
520
+ reason: "revalidatePath",
521
+ count: pprCount
522
+ });
523
+ }
524
+ return count;
525
+ }
326
526
  async getOrSet(key, producer, options = {}) {
327
- const cached = this.getEntry(key);
527
+ const cached = await this.getEntryAsync(key);
328
528
  if (cached) {
329
529
  return cached.value;
330
530
  }
@@ -333,10 +533,8 @@ var _FarmDataCache = class _FarmDataCache {
333
533
  emitFarmEvent({ type: "cache.dedupe", key });
334
534
  return inflight;
335
535
  }
336
- const promise = Promise.resolve().then(producer).then((value) => {
337
- this.set(key, value, options);
338
- return value;
339
- }).catch((error) => {
536
+ const tags = Array.from(normalizeCacheOptionsTags(options));
537
+ const promise = this.fillCacheEntry(key, producer, options, tags).catch((error) => {
340
538
  emitFarmEvent({ type: "cache.error", key, operation: "set", error });
341
539
  throw error;
342
540
  }).finally(() => {
@@ -345,6 +543,40 @@ var _FarmDataCache = class _FarmDataCache {
345
543
  this.inflight.set(key, promise);
346
544
  return promise;
347
545
  }
546
+ async fillCacheEntry(key, producer, options, tags) {
547
+ const leaseKey = `${this.namespace}:lease:${key}`;
548
+ let leaseToken;
549
+ if (this.adapter?.acquireLease && this.adapter.releaseLease && this.lease.enabled) {
550
+ leaseToken = await this.adapter.acquireLease(leaseKey, this.lease.ttlMs);
551
+ if (!leaseToken) {
552
+ const shared = await this.waitForAdapterEntry(key);
553
+ if (shared) {
554
+ emitFarmEvent({ type: "cache.dedupe", key });
555
+ return shared.value;
556
+ }
557
+ leaseToken = await this.adapter.acquireLease(leaseKey, this.lease.ttlMs);
558
+ }
559
+ }
560
+ try {
561
+ const initialTagVersions = await this.getAdapterTagVersions(tags);
562
+ const value = await producer();
563
+ await this.setAsync(key, value, options, initialTagVersions);
564
+ return value;
565
+ } finally {
566
+ if (leaseToken && this.adapter?.releaseLease) {
567
+ await this.adapter.releaseLease(leaseKey, leaseToken);
568
+ }
569
+ }
570
+ }
571
+ async waitForAdapterEntry(key) {
572
+ const deadline = Date.now() + this.lease.waitTimeoutMs;
573
+ while (Date.now() < deadline) {
574
+ await delay(this.lease.pollIntervalMs);
575
+ const entry = await this.getEntryAsync(key);
576
+ if (entry) return entry;
577
+ }
578
+ return void 0;
579
+ }
348
580
  countEntriesForTag(tag) {
349
581
  let count = 0;
350
582
  for (const entry of this.entries.values()) {
@@ -372,11 +604,53 @@ var _FarmDataCache = class _FarmDataCache {
372
604
  key: entry.key,
373
605
  value: entry.value,
374
606
  tags: Array.from(entry.tags),
607
+ tagVersions: entry.tagVersions,
375
608
  createdAt: entry.createdAt,
376
609
  createdVersion: entry.createdVersion,
377
610
  revalidate: entry.revalidate
378
611
  };
379
612
  }
613
+ hydrateLocalEntry(entry) {
614
+ this.entries.set(entry.key, {
615
+ key: entry.key,
616
+ value: entry.value,
617
+ tags: new Set(entry.tags.map(normalizeCacheTag)),
618
+ tagVersions: entry.tagVersions,
619
+ createdAt: entry.createdAt,
620
+ createdVersion: entry.createdVersion ?? ++this.version,
621
+ revalidate: normalizeRevalidate(entry.revalidate)
622
+ });
623
+ }
624
+ createAdapterKey(key) {
625
+ return `${this.namespace}:entry:${key}`;
626
+ }
627
+ createAdapterTag(tag) {
628
+ return `${this.namespace}:tag:${tag}`;
629
+ }
630
+ async getAdapterTagVersions(tags) {
631
+ if (!this.adapter?.getTagVersions || tags.length === 0) return {};
632
+ const normalized = tags.map(normalizeCacheTag);
633
+ const physicalTags = normalized.map((tag) => this.createAdapterTag(tag));
634
+ const versions = await this.adapter.getTagVersions(physicalTags);
635
+ return Object.fromEntries(
636
+ normalized.map((tag, index) => [
637
+ tag,
638
+ normalizeAdapterVersion(versions[physicalTags[index]])
639
+ ])
640
+ );
641
+ }
642
+ async isAdapterEntryStale(entry, now = Date.now()) {
643
+ if (typeof entry.revalidate === "number" && entry.revalidate > 0 && now - entry.createdAt >= entry.revalidate * 1e3) {
644
+ return true;
645
+ }
646
+ const currentVersions = await this.getAdapterTagVersions(entry.tags);
647
+ for (const tag of entry.tags) {
648
+ if (normalizeAdapterVersion(currentVersions[tag]) > normalizeAdapterVersion(entry.tagVersions?.[tag])) {
649
+ return true;
650
+ }
651
+ }
652
+ return false;
653
+ }
380
654
  };
381
655
  __name(_FarmDataCache, "FarmDataCache");
382
656
  var FarmDataCache = _FarmDataCache;
@@ -389,6 +663,10 @@ function getFarmDataCache() {
389
663
  return sharedFarmDataCache;
390
664
  }
391
665
  __name(getFarmDataCache, "getFarmDataCache");
666
+ function configureFarmCache(config) {
667
+ sharedFarmDataCache.configure(config);
668
+ }
669
+ __name(configureFarmCache, "configureFarmCache");
392
670
  function unstable_cache(fn, keyParts = [], options = {}) {
393
671
  return async (...args) => {
394
672
  const locale = getActiveFarmI18nSnapshot()?.locale;
@@ -404,30 +682,71 @@ function unstable_cache(fn, keyParts = [], options = {}) {
404
682
  }
405
683
  __name(unstable_cache, "unstable_cache");
406
684
  function revalidateTag(_tag, _profile) {
407
- getFarmDataCache().revalidateTag(_tag, {
685
+ const cache = getFarmDataCache();
686
+ if (cache.hasAdapter) {
687
+ const task = cache.revalidateTagAsync(_tag, {
688
+ source: "revalidateTag",
689
+ profile: _profile
690
+ }).then(() => void 0);
691
+ notifyFarmCacheTask(task);
692
+ return task;
693
+ }
694
+ cache.revalidateTag(_tag, {
408
695
  source: "revalidateTag",
409
696
  profile: _profile
410
697
  });
411
698
  }
412
699
  __name(revalidateTag, "revalidateTag");
413
700
  function updateTag(tag) {
414
- getFarmDataCache().revalidateTag(tag, { source: "updateTag" });
701
+ const cache = getFarmDataCache();
702
+ if (cache.hasAdapter) {
703
+ const task = cache.revalidateTagAsync(tag, { source: "updateTag" }).then(() => void 0);
704
+ notifyFarmCacheTask(task);
705
+ return task;
706
+ }
707
+ cache.revalidateTag(tag, { source: "updateTag" });
415
708
  }
416
709
  __name(updateTag, "updateTag");
417
710
  function revalidatePath(routePath) {
418
- getFarmDataCache().revalidatePath(routePath);
711
+ const cache = getFarmDataCache();
712
+ if (cache.hasAdapter) {
713
+ const task = cache.revalidatePathAsync(routePath).then(() => void 0);
714
+ notifyFarmCacheTask(task);
715
+ return task;
716
+ }
717
+ cache.revalidatePath(routePath);
419
718
  }
420
719
  __name(revalidatePath, "revalidatePath");
421
720
  function invalidate(key) {
422
721
  const clientKey = createRouteDataCacheKey(key);
423
- getFarmDataCache().revalidateTag(createRouteDataCacheTag(key), { source: "updateTag" });
722
+ const task = updateTag(createRouteDataCacheTag(key));
424
723
  notifyFarmCacheInvalidation(clientKey);
724
+ return task;
425
725
  }
426
726
  __name(invalidate, "invalidate");
427
727
  function invalidateRouteData(key) {
428
- invalidate(key);
728
+ return invalidate(key);
429
729
  }
430
730
  __name(invalidateRouteData, "invalidateRouteData");
731
+ async function applyFarmCacheInvalidationTargets(targets) {
732
+ if (!Array.isArray(targets)) {
733
+ throw new TypeError("Cache invalidations must resolve to an array of targets.");
734
+ }
735
+ const clientKeys = [];
736
+ for (const target of targets) {
737
+ assertFarmCacheInvalidationTarget(target);
738
+ if ("key" in target) {
739
+ await invalidate(target.key);
740
+ clientKeys.push(createRouteDataCacheKey(target.key));
741
+ } else if ("path" in target) {
742
+ await revalidatePath(target.path);
743
+ } else {
744
+ await updateTag(target.tag);
745
+ }
746
+ }
747
+ return Array.from(new Set(clientKeys));
748
+ }
749
+ __name(applyFarmCacheInvalidationTargets, "applyFarmCacheInvalidationTargets");
431
750
  function createPathCacheTag(routePath) {
432
751
  return `path:${normalizeRevalidatePath(routePath)}`;
433
752
  }
@@ -488,6 +807,67 @@ function normalizeCacheTag(tag) {
488
807
  return normalized;
489
808
  }
490
809
  __name(normalizeCacheTag, "normalizeCacheTag");
810
+ function normalizeCacheNamespace(namespace) {
811
+ if (typeof namespace !== "string") {
812
+ throw new TypeError("Cache namespace must be a string.");
813
+ }
814
+ const normalized = namespace.trim().replace(/:+$/g, "");
815
+ if (!normalized) {
816
+ throw new Error("Cache namespace cannot be empty.");
817
+ }
818
+ return normalized;
819
+ }
820
+ __name(normalizeCacheNamespace, "normalizeCacheNamespace");
821
+ function normalizeAdapterVersion(value) {
822
+ return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0;
823
+ }
824
+ __name(normalizeAdapterVersion, "normalizeAdapterVersion");
825
+ function normalizePositiveDuration(value, fallback, name) {
826
+ if (value === void 0) return fallback;
827
+ if (!Number.isFinite(value) || value <= 0) {
828
+ throw new TypeError(`${name} must be a positive number of milliseconds.`);
829
+ }
830
+ return Math.floor(value);
831
+ }
832
+ __name(normalizePositiveDuration, "normalizePositiveDuration");
833
+ function delay(ms) {
834
+ return new Promise((resolve) => setTimeout(resolve, ms));
835
+ }
836
+ __name(delay, "delay");
837
+ function normalizeCacheOptionsTags(options) {
838
+ const tags = /* @__PURE__ */ new Set();
839
+ for (const tag of options.tags ?? []) {
840
+ tags.add(normalizeCacheTag(tag));
841
+ }
842
+ for (const routePath of options.paths ?? []) {
843
+ tags.add(createPathCacheTag(routePath));
844
+ }
845
+ return tags;
846
+ }
847
+ __name(normalizeCacheOptionsTags, "normalizeCacheOptionsTags");
848
+ function assertFarmCacheEntry(entry, expectedKey) {
849
+ if (!entry || typeof entry !== "object" || entry.key !== expectedKey || !Array.isArray(entry.tags) || typeof entry.createdAt !== "number") {
850
+ throw new TypeError(
851
+ `Cache adapter returned an invalid entry for ${JSON.stringify(expectedKey)}.`
852
+ );
853
+ }
854
+ }
855
+ __name(assertFarmCacheEntry, "assertFarmCacheEntry");
856
+ function assertFarmCacheInvalidationTarget(target) {
857
+ if (!target || typeof target !== "object") {
858
+ throw new TypeError("Cache invalidation targets must be { key }, { path }, or { tag }.");
859
+ }
860
+ if ("key" in target) {
861
+ const key = target.key;
862
+ if (typeof key === "string" || Array.isArray(key)) return;
863
+ } else if ("path" in target && typeof target.path === "string") {
864
+ return;
865
+ } else if ("tag" in target && typeof target.tag === "string") {
866
+ return;
867
+ }
868
+ throw new TypeError("Cache invalidation targets must contain a string/array key, path, or tag.");
869
+ }
870
+ __name(assertFarmCacheInvalidationTarget, "assertFarmCacheInvalidationTarget");
491
871
  function getFunctionCacheIdentity(fn) {
492
872
  if (fn.name) {
493
873
  return `name:${fn.name}`;
@@ -544,7 +924,9 @@ __name(stableSerialize, "stableSerialize");
544
924
  // Annotate the CommonJS export names for ESM import in node:
545
925
  0 && (module.exports = {
546
926
  FarmDataCache,
927
+ applyFarmCacheInvalidationTargets,
547
928
  applyFarmCacheInvalidations,
929
+ configureFarmCache,
548
930
  createFarmCacheKey,
549
931
  createPathCacheTag,
550
932
  createRouteDataCacheKey,
@@ -556,6 +938,7 @@ __name(stableSerialize, "stableSerialize");
556
938
  normalizeRevalidatePath,
557
939
  revalidatePath,
558
940
  revalidateTag,
941
+ storageCacheAdapter,
559
942
  unstable_cache,
560
943
  updateTag
561
944
  });