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

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 (230) hide show
  1. package/README.md +1 -1
  2. package/dist/agent-runtime.d.mts +1 -1
  3. package/dist/agent-runtime.d.ts +1 -1
  4. package/dist/api/route-manager.cjs +97 -5
  5. package/dist/api/route-manager.cjs.map +1 -1
  6. package/dist/api/route-manager.d.mts +3 -2
  7. package/dist/api/route-manager.d.ts +3 -2
  8. package/dist/api/route-manager.mjs +8 -8
  9. package/dist/api/runtime.cjs +441 -5
  10. package/dist/api/runtime.cjs.map +1 -1
  11. package/dist/api/runtime.d.mts +2 -16
  12. package/dist/api/runtime.d.ts +2 -16
  13. package/dist/api/runtime.mjs +7 -1
  14. package/dist/api.cjs +579 -263
  15. package/dist/api.cjs.map +1 -1
  16. package/dist/api.d.mts +4 -147
  17. package/dist/api.d.ts +4 -147
  18. package/dist/api.mjs +30 -20
  19. package/dist/{app-BiX4qb-d.d.mts → app-BB3Iu3xh.d.mts} +2 -2
  20. package/dist/{app-CFRNJXP1.d.ts → app-DOKxo-LH.d.ts} +2 -2
  21. package/dist/app-markdown.cjs +1 -1
  22. package/dist/app-markdown.cjs.map +1 -1
  23. package/dist/app-markdown.d.mts +1 -1
  24. package/dist/app-markdown.d.ts +1 -1
  25. package/dist/app-markdown.mjs +1 -1
  26. package/dist/cache.cjs +19 -0
  27. package/dist/cache.cjs.map +1 -1
  28. package/dist/cache.d.mts +21 -1
  29. package/dist/cache.d.ts +21 -1
  30. package/dist/cache.mjs +5 -3
  31. package/dist/{chunk-QF4PHCMA.mjs → chunk-2ED5Q2GH.mjs} +3 -3
  32. package/dist/chunk-2ED5Q2GH.mjs.map +1 -0
  33. package/dist/{chunk-GZP5W4K3.mjs → chunk-32W4QEJP.mjs} +1 -1
  34. package/dist/chunk-32W4QEJP.mjs.map +1 -0
  35. package/dist/{chunk-AGS7UJ52.mjs → chunk-45LKHSHJ.mjs} +2 -2
  36. package/dist/{chunk-TU4YJICB.mjs → chunk-5FW6PQ3J.mjs} +1 -1
  37. package/dist/{chunk-TU4YJICB.mjs.map → chunk-5FW6PQ3J.mjs.map} +1 -1
  38. package/dist/{chunk-TRORP7E4.mjs → chunk-5QH4IKY3.mjs} +2 -2
  39. package/dist/{chunk-RKI6TN3A.mjs → chunk-6VS6CKAV.mjs} +2 -2
  40. package/dist/chunk-6VS6CKAV.mjs.map +1 -0
  41. package/dist/{chunk-25D7G34K.mjs → chunk-6WXJ54TC.mjs} +4 -4
  42. package/dist/{chunk-3QJO55ZF.mjs → chunk-BIOIOWXZ.mjs} +5 -3
  43. package/dist/{chunk-3QJO55ZF.mjs.map → chunk-BIOIOWXZ.mjs.map} +1 -1
  44. package/dist/{chunk-QFQRHRZS.mjs → chunk-C33RALN7.mjs} +75 -11
  45. package/dist/chunk-C33RALN7.mjs.map +1 -0
  46. package/dist/{chunk-B6N4ZB2H.mjs → chunk-CSU4KJJF.mjs} +3 -3
  47. package/dist/{chunk-JN3UU635.mjs → chunk-EJ7T4TPB.mjs} +2 -2
  48. package/dist/chunk-EJ7T4TPB.mjs.map +1 -0
  49. package/dist/{chunk-ZJQNATYV.mjs → chunk-FRPWCMMG.mjs} +3 -3
  50. package/dist/chunk-FRPWCMMG.mjs.map +1 -0
  51. package/dist/{chunk-PRNYWD7K.mjs → chunk-G5I34UA6.mjs} +67 -239
  52. package/dist/chunk-G5I34UA6.mjs.map +1 -0
  53. package/dist/{chunk-QM6VV26B.mjs → chunk-GQVGIUSN.mjs} +3 -3
  54. package/dist/{chunk-QM6VV26B.mjs.map → chunk-GQVGIUSN.mjs.map} +1 -1
  55. package/dist/{chunk-UREJA4GZ.mjs → chunk-GXASWWJU.mjs} +1 -1
  56. package/dist/{chunk-UREJA4GZ.mjs.map → chunk-GXASWWJU.mjs.map} +1 -1
  57. package/dist/{chunk-4PTK6KT4.mjs → chunk-GZWT6VI5.mjs} +22 -4
  58. package/dist/chunk-GZWT6VI5.mjs.map +1 -0
  59. package/dist/{chunk-JLWMCADZ.mjs → chunk-HZYXGV4K.mjs} +2 -2
  60. package/dist/chunk-JNYVN7YP.mjs +10 -0
  61. package/dist/chunk-JNYVN7YP.mjs.map +1 -0
  62. package/dist/{chunk-YHU7SS6L.mjs → chunk-LG3YINQN.mjs} +11 -11
  63. package/dist/chunk-LWMGIEJA.mjs +593 -0
  64. package/dist/chunk-LWMGIEJA.mjs.map +1 -0
  65. package/dist/{chunk-HXP3ZSPC.mjs → chunk-MH6FKW2I.mjs} +3 -3
  66. package/dist/{chunk-O3HADRW6.mjs → chunk-MIUW47JX.mjs} +2 -2
  67. package/dist/{chunk-O3HADRW6.mjs.map → chunk-MIUW47JX.mjs.map} +1 -1
  68. package/dist/{chunk-6XDUHOEM.mjs → chunk-NRRFGX45.mjs} +3 -3
  69. package/dist/{chunk-G3B5BRDY.mjs → chunk-OHC6VCDI.mjs} +24 -25
  70. package/dist/{chunk-G3B5BRDY.mjs.map → chunk-OHC6VCDI.mjs.map} +1 -1
  71. package/dist/{chunk-S2XHZS7W.mjs → chunk-QGQX7SFC.mjs} +2 -2
  72. package/dist/chunk-QGQX7SFC.mjs.map +1 -0
  73. package/dist/{chunk-TXCFGOHW.mjs → chunk-SHRJG6OG.mjs} +25 -1
  74. package/dist/chunk-SHRJG6OG.mjs.map +1 -0
  75. package/dist/{chunk-UHE2TTLK.mjs → chunk-T543TMFY.mjs} +8 -8
  76. package/dist/chunk-T543TMFY.mjs.map +1 -0
  77. package/dist/{chunk-SXZTEK3V.mjs → chunk-UXUAWNO5.mjs} +2 -2
  78. package/dist/{chunk-Y2OJJD7R.mjs → chunk-XIVV5IWN.mjs} +7 -9
  79. package/dist/chunk-XIVV5IWN.mjs.map +1 -0
  80. package/dist/{chunk-CHFHNEMO.mjs → chunk-XJSHJN4M.mjs} +41 -42
  81. package/dist/chunk-XJSHJN4M.mjs.map +1 -0
  82. package/dist/{chunk-Y7A27276.mjs → chunk-XUBZ7JI4.mjs} +178 -15
  83. package/dist/chunk-XUBZ7JI4.mjs.map +1 -0
  84. package/dist/{chunk-OYXO65LI.mjs → chunk-YW7OBE6Q.mjs} +4 -4
  85. package/dist/chunk-YW7OBE6Q.mjs.map +1 -0
  86. package/dist/{chunk-SOU3Q3ZA.mjs → chunk-ZZ57EH5Z.mjs} +1 -1
  87. package/dist/chunk-ZZ57EH5Z.mjs.map +1 -0
  88. package/dist/{client-D_yUjVhe.d.mts → client-DiViK9yK.d.mts} +33 -7
  89. package/dist/{client-Dkc03WP8.d.ts → client-DpEBJbts.d.ts} +33 -7
  90. package/dist/{client-plugins-BZnpeTGA.d.ts → client-plugins-CTnsZz_A.d.ts} +1 -0
  91. package/dist/{client-plugins-g4bzuCmo.d.mts → client-plugins-Cnr0D3kk.d.mts} +1 -0
  92. package/dist/client-plugins.d.mts +1 -1
  93. package/dist/client-plugins.d.ts +1 -1
  94. package/dist/client.cjs +302 -54
  95. package/dist/client.cjs.map +1 -1
  96. package/dist/client.d.mts +54 -2
  97. package/dist/client.d.ts +54 -2
  98. package/dist/client.mjs +20 -16
  99. package/dist/config.d.mts +1 -1
  100. package/dist/config.d.ts +1 -1
  101. package/dist/cron.cjs.map +1 -1
  102. package/dist/cron.mjs +1 -1
  103. package/dist/{devtools-AE4RVXCM.mjs → devtools-HE4FTUDN.mjs} +10 -10
  104. package/dist/{devtools-client-6WPY2AQU.mjs → devtools-client-3XRTBND5.mjs} +2 -11
  105. package/dist/devtools-client-3XRTBND5.mjs.map +1 -0
  106. package/dist/docs.cjs +3 -1
  107. package/dist/docs.cjs.map +1 -1
  108. package/dist/docs.mjs +10 -10
  109. package/dist/i18n/server.cjs +2 -2
  110. package/dist/i18n/server.cjs.map +1 -1
  111. package/dist/i18n/server.mjs +1 -1
  112. package/dist/index.cjs +791 -317
  113. package/dist/index.cjs.map +1 -1
  114. package/dist/index.d.mts +11 -11
  115. package/dist/index.d.ts +11 -11
  116. package/dist/index.mjs +125 -110
  117. package/dist/index.mjs.map +1 -1
  118. package/dist/integrations.d.mts +1 -1
  119. package/dist/integrations.d.ts +1 -1
  120. package/dist/internal/build-runtime.cjs +162 -60
  121. package/dist/internal/build-runtime.cjs.map +1 -1
  122. package/dist/internal/build-runtime.d.mts +3 -3
  123. package/dist/internal/build-runtime.d.ts +3 -3
  124. package/dist/internal/build-runtime.mjs +26 -25
  125. package/dist/internal/client-runtime.cjs.map +1 -1
  126. package/dist/internal/client-runtime.mjs +1 -1
  127. package/dist/internal/config-runtime.cjs +1 -1
  128. package/dist/internal/config-runtime.cjs.map +1 -1
  129. package/dist/internal/config-runtime.d.mts +2 -2
  130. package/dist/internal/config-runtime.d.ts +2 -2
  131. package/dist/internal/config-runtime.mjs +9 -9
  132. package/dist/internal/production-runtime.cjs +3 -3
  133. package/dist/internal/production-runtime.cjs.map +1 -1
  134. package/dist/internal/production-runtime.d.mts +3 -3
  135. package/dist/internal/production-runtime.d.ts +3 -3
  136. package/dist/internal/production-runtime.mjs +8 -8
  137. package/dist/{metadata-C9NZxMDW.d.mts → metadata-Dp8AdvsB.d.mts} +1 -1
  138. package/dist/{metadata-DAXRaOHx.d.ts → metadata-eoVmHoyv.d.ts} +1 -1
  139. package/dist/middleware.cjs +2 -2
  140. package/dist/middleware.cjs.map +1 -1
  141. package/dist/middleware.d.mts +3 -3
  142. package/dist/middleware.d.ts +3 -3
  143. package/dist/middleware.mjs +3 -3
  144. package/dist/navigation.cjs.map +1 -1
  145. package/dist/navigation.mjs +3 -3
  146. package/dist/{nitro-KME62MWG.mjs → nitro-QO5LV6QK.mjs} +11 -13
  147. package/dist/{nitro-KME62MWG.mjs.map → nitro-QO5LV6QK.mjs.map} +1 -1
  148. package/dist/plugin.cjs +119 -39
  149. package/dist/plugin.cjs.map +1 -1
  150. package/dist/plugin.d.mts +1 -1
  151. package/dist/plugin.d.ts +1 -1
  152. package/dist/plugin.mjs +22 -21
  153. package/dist/{production-runtime-DRkCHUvJ.d.ts → production-runtime-BqTnUTai.d.ts} +1 -1
  154. package/dist/{production-runtime-flrjp_nC.d.mts → production-runtime-DtDdG9fc.d.mts} +1 -1
  155. package/dist/routes.cjs.map +1 -1
  156. package/dist/routes.d.mts +1 -1
  157. package/dist/routes.d.ts +1 -1
  158. package/dist/routes.mjs +4 -4
  159. package/dist/runtime-C1B7pSGT.d.mts +207 -0
  160. package/dist/runtime-CdPHt0bv.d.ts +207 -0
  161. package/dist/server-action-security.cjs.map +1 -1
  162. package/dist/server-action-security.mjs +2 -2
  163. package/dist/server-fn-client.cjs.map +1 -1
  164. package/dist/server-fn-client.mjs +1 -1
  165. package/dist/server-fn.cjs.map +1 -1
  166. package/dist/server-fn.mjs +3 -3
  167. package/dist/server-plugins.d.mts +2 -2
  168. package/dist/server-plugins.d.ts +2 -2
  169. package/dist/server-query-client.cjs.map +1 -1
  170. package/dist/server-query-client.mjs +4 -4
  171. package/dist/server-query.cjs.map +1 -1
  172. package/dist/server-query.mjs +5 -5
  173. package/dist/server.cjs +148 -43
  174. package/dist/server.cjs.map +1 -1
  175. package/dist/server.d.mts +6 -6
  176. package/dist/server.d.ts +6 -6
  177. package/dist/server.mjs +36 -31
  178. package/dist/server.mjs.map +1 -1
  179. package/dist/testing.cjs +95 -5
  180. package/dist/testing.cjs.map +1 -1
  181. package/dist/testing.d.mts +3 -3
  182. package/dist/testing.d.ts +3 -3
  183. package/dist/testing.mjs +18 -18
  184. package/dist/{utils-DOXXivjL.d.mts → utils-8USa5BiY.d.mts} +1 -1
  185. package/dist/{utils-DA1yo54_.d.ts → utils-DKJb6K6G.d.ts} +1 -1
  186. package/dist/version.cjs +36 -0
  187. package/dist/version.cjs.map +1 -0
  188. package/dist/version.d.mts +3 -0
  189. package/dist/version.d.ts +3 -0
  190. package/dist/version.mjs +8 -0
  191. package/dist/version.mjs.map +1 -0
  192. package/dist/vite.cjs +126 -40
  193. package/dist/vite.cjs.map +1 -1
  194. package/dist/vite.d.mts +1 -1
  195. package/dist/vite.d.ts +1 -1
  196. package/dist/vite.mjs +22 -21
  197. package/dist/workflows.cjs +6 -8
  198. package/dist/workflows.cjs.map +1 -1
  199. package/dist/workflows.mjs +1 -1
  200. package/package.json +12 -5
  201. package/types/client.d.ts +136 -13
  202. package/dist/chunk-4PTK6KT4.mjs.map +0 -1
  203. package/dist/chunk-CHFHNEMO.mjs.map +0 -1
  204. package/dist/chunk-EVGBEZDF.mjs +0 -215
  205. package/dist/chunk-EVGBEZDF.mjs.map +0 -1
  206. package/dist/chunk-GZP5W4K3.mjs.map +0 -1
  207. package/dist/chunk-JN3UU635.mjs.map +0 -1
  208. package/dist/chunk-OYXO65LI.mjs.map +0 -1
  209. package/dist/chunk-PRNYWD7K.mjs.map +0 -1
  210. package/dist/chunk-QF4PHCMA.mjs.map +0 -1
  211. package/dist/chunk-QFQRHRZS.mjs.map +0 -1
  212. package/dist/chunk-RKI6TN3A.mjs.map +0 -1
  213. package/dist/chunk-S2XHZS7W.mjs.map +0 -1
  214. package/dist/chunk-SOU3Q3ZA.mjs.map +0 -1
  215. package/dist/chunk-TXCFGOHW.mjs.map +0 -1
  216. package/dist/chunk-UHE2TTLK.mjs.map +0 -1
  217. package/dist/chunk-Y2OJJD7R.mjs.map +0 -1
  218. package/dist/chunk-Y7A27276.mjs.map +0 -1
  219. package/dist/chunk-ZJQNATYV.mjs.map +0 -1
  220. package/dist/devtools-client-6WPY2AQU.mjs.map +0 -1
  221. /package/dist/{chunk-AGS7UJ52.mjs.map → chunk-45LKHSHJ.mjs.map} +0 -0
  222. /package/dist/{chunk-TRORP7E4.mjs.map → chunk-5QH4IKY3.mjs.map} +0 -0
  223. /package/dist/{chunk-25D7G34K.mjs.map → chunk-6WXJ54TC.mjs.map} +0 -0
  224. /package/dist/{chunk-B6N4ZB2H.mjs.map → chunk-CSU4KJJF.mjs.map} +0 -0
  225. /package/dist/{chunk-JLWMCADZ.mjs.map → chunk-HZYXGV4K.mjs.map} +0 -0
  226. /package/dist/{chunk-YHU7SS6L.mjs.map → chunk-LG3YINQN.mjs.map} +0 -0
  227. /package/dist/{chunk-HXP3ZSPC.mjs.map → chunk-MH6FKW2I.mjs.map} +0 -0
  228. /package/dist/{chunk-6XDUHOEM.mjs.map → chunk-NRRFGX45.mjs.map} +0 -0
  229. /package/dist/{chunk-SXZTEK3V.mjs.map → chunk-UXUAWNO5.mjs.map} +0 -0
  230. /package/dist/{devtools-AE4RVXCM.mjs.map → devtools-HE4FTUDN.mjs.map} +0 -0
@@ -2,9 +2,10 @@ import { ViteDevServer } from 'vite';
2
2
  import { F as FarmRouteRuntimeConfig } from '../route-runtime-DoOHzCAD.mjs';
3
3
  import '../types-BH1lhg5G.mjs';
4
4
  import { F as FarmI18nRuntime } from '../runtime-DeAAZ6gO.mjs';
5
- import { APIRouteMatch } from './runtime.mjs';
6
- export { APIRouteParamValue, APIRouteParams, invokeAPIRouteEndpoint, isWebResponse, matchAPIRoute, normalizeRouteResponse } from './runtime.mjs';
5
+ import { A as APIRouteMatch } from '../runtime-C1B7pSGT.mjs';
6
+ export { c as APIRouteParamValue, b as APIRouteParams, i as invokeAPIRouteEndpoint, a as isWebResponse, m as matchAPIRoute, n as normalizeRouteResponse } from '../runtime-C1B7pSGT.mjs';
7
7
  import '../resolver-DlOPpjkB.mjs';
8
+ import '../cache.mjs';
8
9
 
9
10
  interface APIRoute extends FarmRouteRuntimeConfig {
10
11
  path: string;
@@ -2,9 +2,10 @@ import { ViteDevServer } from 'vite';
2
2
  import { F as FarmRouteRuntimeConfig } from '../route-runtime-DoOHzCAD.js';
3
3
  import '../types-BH1lhg5G.js';
4
4
  import { F as FarmI18nRuntime } from '../runtime-B6GvwDS1.js';
5
- import { APIRouteMatch } from './runtime.js';
6
- export { APIRouteParamValue, APIRouteParams, invokeAPIRouteEndpoint, isWebResponse, matchAPIRoute, normalizeRouteResponse } from './runtime.js';
5
+ import { A as APIRouteMatch } from '../runtime-CdPHt0bv.js';
6
+ export { c as APIRouteParamValue, b as APIRouteParams, i as invokeAPIRouteEndpoint, a as isWebResponse, m as matchAPIRoute, n as normalizeRouteResponse } from '../runtime-CdPHt0bv.js';
7
7
  import '../resolver-C6iecsDV.js';
8
+ import '../cache.js';
8
9
 
9
10
  interface APIRoute extends FarmRouteRuntimeConfig {
10
11
  path: string;
@@ -1,26 +1,26 @@
1
1
  import {
2
2
  APIRouteManager,
3
3
  API_ROUTE_METHODS
4
- } from "../chunk-YHU7SS6L.mjs";
5
- import "../chunk-QF4PHCMA.mjs";
4
+ } from "../chunk-LG3YINQN.mjs";
5
+ import "../chunk-2ED5Q2GH.mjs";
6
6
  import "../chunk-GN72QAIH.mjs";
7
7
  import "../chunk-FQYDHKKU.mjs";
8
+ import "../chunk-CSU4KJJF.mjs";
9
+ import "../chunk-QKVUNGAH.mjs";
10
+ import "../chunk-GQVGIUSN.mjs";
8
11
  import "../chunk-J4FNJ5IU.mjs";
9
12
  import {
10
13
  invokeAPIRouteEndpoint,
11
14
  isWebResponse,
12
15
  matchAPIRoute,
13
16
  normalizeRouteResponse
14
- } from "../chunk-EVGBEZDF.mjs";
17
+ } from "../chunk-LWMGIEJA.mjs";
15
18
  import "../chunk-RHIFZZUI.mjs";
16
19
  import "../chunk-LM2YNO4U.mjs";
17
- import "../chunk-B6N4ZB2H.mjs";
18
- import "../chunk-QKVUNGAH.mjs";
19
- import "../chunk-QM6VV26B.mjs";
20
20
  import "../chunk-44ZCFSKE.mjs";
21
- import "../chunk-4PTK6KT4.mjs";
22
- import "../chunk-TXCFGOHW.mjs";
21
+ import "../chunk-GZWT6VI5.mjs";
23
22
  import "../chunk-WZ5QFROO.mjs";
23
+ import "../chunk-SHRJG6OG.mjs";
24
24
  import "../chunk-7ZY5AIJO.mjs";
25
25
  import "../chunk-CGILA3WO.mjs";
26
26
  export {
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  // src/api/runtime.ts
22
22
  var runtime_exports = {};
23
23
  __export(runtime_exports, {
24
+ createEndpointFailureResponse: () => createEndpointFailureResponse,
24
25
  invokeAPIRouteEndpoint: () => invokeAPIRouteEndpoint,
25
26
  isWebResponse: () => isWebResponse,
26
27
  matchAPIRoute: () => matchAPIRoute,
@@ -62,6 +63,391 @@ async function _runWithCurrentRequest(request, fn) {
62
63
  }
63
64
  __name(_runWithCurrentRequest, "_runWithCurrentRequest");
64
65
 
66
+ // src/cache-invalidation.ts
67
+ var FARM_CACHE_INVALIDATION_HEADER = "x-farm-cache-invalidations";
68
+ function encodeFarmCacheInvalidations(keys) {
69
+ const normalized = Array.from(
70
+ new Set(keys.filter((key) => typeof key === "string" && key.length > 0))
71
+ );
72
+ if (normalized.length === 0) return null;
73
+ return encodeURIComponent(JSON.stringify(normalized));
74
+ }
75
+ __name(encodeFarmCacheInvalidations, "encodeFarmCacheInvalidations");
76
+ function decodeFarmCacheInvalidations(value) {
77
+ if (!value) return [];
78
+ try {
79
+ const parsed = JSON.parse(decodeURIComponent(value));
80
+ return Array.isArray(parsed) ? Array.from(
81
+ new Set(parsed.filter((key) => typeof key === "string" && key.length > 0))
82
+ ) : [];
83
+ } catch {
84
+ return [];
85
+ }
86
+ }
87
+ __name(decodeFarmCacheInvalidations, "decodeFarmCacheInvalidations");
88
+
89
+ // src/api/endpoint.ts
90
+ var import_better_call = require("better-call");
91
+
92
+ // src/observability.ts
93
+ var runtimeHandlers = /* @__PURE__ */ new Set();
94
+ var observabilityState = {
95
+ logs: false,
96
+ handlers: []
97
+ };
98
+ function emitFarmEvent(input) {
99
+ const event = {
100
+ timestamp: Date.now(),
101
+ level: inferFarmEventLevel(input.type),
102
+ ...input
103
+ };
104
+ if (!shouldEmitFarmEvent(event)) {
105
+ return event;
106
+ }
107
+ if (observabilityState.logs) {
108
+ logFarmEvent(event);
109
+ }
110
+ for (const handler of [...observabilityState.handlers, ...runtimeHandlers]) {
111
+ try {
112
+ Promise.resolve(handler(event)).catch((error) => {
113
+ console.warn(`[farm:observability] event handler failed: ${formatError(error)}`);
114
+ });
115
+ } catch (error) {
116
+ console.warn(`[farm:observability] event handler failed: ${formatError(error)}`);
117
+ }
118
+ }
119
+ return event;
120
+ }
121
+ __name(emitFarmEvent, "emitFarmEvent");
122
+ function shouldEmitFarmEvent(event) {
123
+ if (!observabilityState.logs && observabilityState.handlers.length === 0 && runtimeHandlers.size === 0) {
124
+ return false;
125
+ }
126
+ if (observabilityState.events && !observabilityState.events.has(event.type)) {
127
+ return false;
128
+ }
129
+ return true;
130
+ }
131
+ __name(shouldEmitFarmEvent, "shouldEmitFarmEvent");
132
+ function inferFarmEventLevel(type) {
133
+ if (type === "error" || type.endsWith(".error") || type.endsWith(".failed")) {
134
+ return "error";
135
+ }
136
+ if (type.endsWith(".bypass") || type.endsWith(".invalidated") || type.endsWith(".stale") || type.endsWith(".notFound")) {
137
+ return "warn";
138
+ }
139
+ if (type.endsWith(".hit") || type.endsWith(".miss") || type.endsWith(".start") || type.endsWith(".shellReady")) {
140
+ return "debug";
141
+ }
142
+ return "info";
143
+ }
144
+ __name(inferFarmEventLevel, "inferFarmEventLevel");
145
+ function logFarmEvent(event) {
146
+ const message = `[farm:${event.level}] ${event.type}${formatFarmEventDetails(event)}`;
147
+ switch (event.level) {
148
+ case "error":
149
+ console.error(message);
150
+ break;
151
+ case "warn":
152
+ console.warn(message);
153
+ break;
154
+ default:
155
+ console.log(message);
156
+ break;
157
+ }
158
+ }
159
+ __name(logFarmEvent, "logFarmEvent");
160
+ function formatFarmEventDetails(event) {
161
+ const details = [];
162
+ const record = event;
163
+ for (const key of [
164
+ "route",
165
+ "pathname",
166
+ "method",
167
+ "status",
168
+ "key",
169
+ "tag",
170
+ "path",
171
+ "reason",
172
+ "durationMs",
173
+ "integration",
174
+ "operation",
175
+ "plugin",
176
+ "hook",
177
+ "target",
178
+ "count"
179
+ ]) {
180
+ const value = record[key];
181
+ if (value !== void 0) {
182
+ details.push(`${key}=${formatDetailValue(value)}`);
183
+ }
184
+ }
185
+ return details.length > 0 ? ` ${details.join(" ")}` : "";
186
+ }
187
+ __name(formatFarmEventDetails, "formatFarmEventDetails");
188
+ function formatDetailValue(value) {
189
+ if (typeof value === "string") {
190
+ return value;
191
+ }
192
+ if (typeof value === "number" || typeof value === "boolean") {
193
+ return String(value);
194
+ }
195
+ try {
196
+ return JSON.stringify(value);
197
+ } catch {
198
+ return String(value);
199
+ }
200
+ }
201
+ __name(formatDetailValue, "formatDetailValue");
202
+ function formatError(error) {
203
+ return error instanceof Error ? error.message : String(error);
204
+ }
205
+ __name(formatError, "formatError");
206
+
207
+ // src/cache.ts
208
+ var _FarmDataCache = class _FarmDataCache {
209
+ constructor() {
210
+ this.entries = /* @__PURE__ */ new Map();
211
+ this.inflight = /* @__PURE__ */ new Map();
212
+ this.invalidatedTagVersions = /* @__PURE__ */ new Map();
213
+ this.version = 0;
214
+ }
215
+ get size() {
216
+ return this.entries.size;
217
+ }
218
+ get(key, options = {}) {
219
+ return this.getEntry(key, options)?.value;
220
+ }
221
+ getEntry(key, options = {}) {
222
+ const entry = this.entries.get(key);
223
+ if (!entry) {
224
+ emitFarmEvent({ type: "cache.miss", key });
225
+ return void 0;
226
+ }
227
+ const stale = this.isStale(entry, options.now);
228
+ if (stale) {
229
+ emitFarmEvent({
230
+ type: "cache.stale",
231
+ key,
232
+ tags: Array.from(entry.tags),
233
+ revalidate: entry.revalidate
234
+ });
235
+ }
236
+ if (!options.allowStale && stale) {
237
+ emitFarmEvent({ type: "cache.miss", key, reason: "stale" });
238
+ return void 0;
239
+ }
240
+ emitFarmEvent({
241
+ type: "cache.hit",
242
+ key,
243
+ tags: Array.from(entry.tags),
244
+ revalidate: entry.revalidate,
245
+ stale
246
+ });
247
+ return this.toPublicEntry(entry);
248
+ }
249
+ set(key, value, options = {}) {
250
+ const tags = /* @__PURE__ */ new Set();
251
+ for (const tag of options.tags ?? []) {
252
+ tags.add(normalizeCacheTag(tag));
253
+ }
254
+ for (const routePath of options.paths ?? []) {
255
+ tags.add(createPathCacheTag(routePath));
256
+ }
257
+ const entry = {
258
+ key,
259
+ value,
260
+ tags,
261
+ createdAt: options.createdAt ?? Date.now(),
262
+ createdVersion: ++this.version,
263
+ revalidate: normalizeRevalidate(options.revalidate)
264
+ };
265
+ this.entries.set(key, entry);
266
+ emitFarmEvent({
267
+ type: "cache.set",
268
+ key,
269
+ tags: Array.from(tags),
270
+ revalidate: entry.revalidate
271
+ });
272
+ return this.toPublicEntry(entry);
273
+ }
274
+ delete(key) {
275
+ const deleted = this.entries.delete(key);
276
+ emitFarmEvent({ type: "cache.delete", key, deleted });
277
+ return deleted;
278
+ }
279
+ clear() {
280
+ const count = this.entries.size;
281
+ this.entries.clear();
282
+ this.inflight.clear();
283
+ this.invalidatedTagVersions.clear();
284
+ this.version = 0;
285
+ emitFarmEvent({ type: "cache.clear", count });
286
+ }
287
+ isStale(entry, now = Date.now()) {
288
+ if (typeof entry.revalidate === "number" && entry.revalidate > 0 && now - entry.createdAt >= entry.revalidate * 1e3) {
289
+ return true;
290
+ }
291
+ for (const tag of entry.tags) {
292
+ const invalidatedVersion = this.invalidatedTagVersions.get(normalizeCacheTag(tag));
293
+ if (typeof invalidatedVersion === "number" && typeof entry.createdVersion === "number" && invalidatedVersion > entry.createdVersion) {
294
+ return true;
295
+ }
296
+ }
297
+ return false;
298
+ }
299
+ revalidateTag(tag, options = {}) {
300
+ const normalized = normalizeCacheTag(tag);
301
+ const count = this.invalidateTag(normalized);
302
+ emitFarmEvent(
303
+ options.source === "updateTag" ? { type: "cache.updateTag", tag: normalized, count } : { type: "cache.revalidateTag", tag: normalized, profile: options.profile, count }
304
+ );
305
+ return count;
306
+ }
307
+ revalidatePath(routePath) {
308
+ const normalizedPath = normalizeRevalidatePath(routePath);
309
+ const pathTag = createPathCacheTag(normalizedPath);
310
+ const pprCount = this.countEntriesForTags([pathTag, "ppr"]);
311
+ const count = this.invalidateTag(pathTag);
312
+ emitFarmEvent({ type: "cache.revalidatePath", path: normalizedPath, count });
313
+ if (pprCount > 0) {
314
+ emitFarmEvent({
315
+ type: "ppr.shell.invalidated",
316
+ route: normalizedPath,
317
+ reason: "revalidatePath",
318
+ count: pprCount
319
+ });
320
+ }
321
+ return count;
322
+ }
323
+ async getOrSet(key, producer, options = {}) {
324
+ const cached = this.getEntry(key);
325
+ if (cached) {
326
+ return cached.value;
327
+ }
328
+ const inflight = this.inflight.get(key);
329
+ if (inflight) {
330
+ emitFarmEvent({ type: "cache.dedupe", key });
331
+ return inflight;
332
+ }
333
+ const promise = Promise.resolve().then(producer).then((value) => {
334
+ this.set(key, value, options);
335
+ return value;
336
+ }).catch((error) => {
337
+ emitFarmEvent({ type: "cache.error", key, operation: "set", error });
338
+ throw error;
339
+ }).finally(() => {
340
+ this.inflight.delete(key);
341
+ });
342
+ this.inflight.set(key, promise);
343
+ return promise;
344
+ }
345
+ countEntriesForTag(tag) {
346
+ let count = 0;
347
+ for (const entry of this.entries.values()) {
348
+ if (entry.tags.has(tag)) {
349
+ count++;
350
+ }
351
+ }
352
+ return count;
353
+ }
354
+ countEntriesForTags(tags) {
355
+ let count = 0;
356
+ for (const entry of this.entries.values()) {
357
+ if (tags.every((tag) => entry.tags.has(tag))) {
358
+ count++;
359
+ }
360
+ }
361
+ return count;
362
+ }
363
+ invalidateTag(normalizedTag) {
364
+ this.invalidatedTagVersions.set(normalizedTag, ++this.version);
365
+ return this.countEntriesForTag(normalizedTag);
366
+ }
367
+ toPublicEntry(entry) {
368
+ return {
369
+ key: entry.key,
370
+ value: entry.value,
371
+ tags: Array.from(entry.tags),
372
+ createdAt: entry.createdAt,
373
+ createdVersion: entry.createdVersion,
374
+ revalidate: entry.revalidate
375
+ };
376
+ }
377
+ };
378
+ __name(_FarmDataCache, "FarmDataCache");
379
+ var FarmDataCache = _FarmDataCache;
380
+ var FARM_DATA_CACHE_SYMBOL = /* @__PURE__ */ Symbol.for("farm.dataCache");
381
+ var farmDataCacheGlobal = globalThis;
382
+ var sharedFarmDataCache = farmDataCacheGlobal[FARM_DATA_CACHE_SYMBOL] ?? (farmDataCacheGlobal[FARM_DATA_CACHE_SYMBOL] = new FarmDataCache());
383
+ function createPathCacheTag(routePath) {
384
+ return `path:${normalizeRevalidatePath(routePath)}`;
385
+ }
386
+ __name(createPathCacheTag, "createPathCacheTag");
387
+ function normalizeRevalidatePath(routePath) {
388
+ if (typeof routePath !== "string") {
389
+ throw new TypeError("revalidatePath expects a path string.");
390
+ }
391
+ let normalized = routePath.trim();
392
+ if (!normalized) {
393
+ throw new Error("revalidatePath expects a non-empty path.");
394
+ }
395
+ try {
396
+ if (/^https?:\/\//.test(normalized)) {
397
+ normalized = new URL(normalized).pathname;
398
+ }
399
+ } catch {
400
+ }
401
+ normalized = normalized.split("?")[0] ?? normalized;
402
+ normalized = normalized.startsWith("/") ? normalized : `/${normalized}`;
403
+ normalized = normalized.replace(/\/{2,}/g, "/");
404
+ if (normalized.length > 1) {
405
+ normalized = normalized.replace(/\/+$/, "");
406
+ }
407
+ return normalized || "/";
408
+ }
409
+ __name(normalizeRevalidatePath, "normalizeRevalidatePath");
410
+ function normalizeRevalidate(revalidate) {
411
+ if (revalidate === false || revalidate === void 0) {
412
+ return revalidate;
413
+ }
414
+ if (!Number.isFinite(revalidate) || revalidate <= 0) {
415
+ return void 0;
416
+ }
417
+ return revalidate;
418
+ }
419
+ __name(normalizeRevalidate, "normalizeRevalidate");
420
+ function normalizeCacheTag(tag) {
421
+ if (typeof tag !== "string") {
422
+ throw new TypeError("Cache tags must be strings.");
423
+ }
424
+ const normalized = tag.trim();
425
+ if (!normalized) {
426
+ throw new Error("Cache tags cannot be empty.");
427
+ }
428
+ return normalized;
429
+ }
430
+ __name(normalizeCacheTag, "normalizeCacheTag");
431
+
432
+ // src/api/endpoint.ts
433
+ var _EndpointFailure = class _EndpointFailure extends Error {
434
+ constructor(code, data, options) {
435
+ super(options.message);
436
+ this.name = "EndpointFailure";
437
+ this.code = code;
438
+ this.data = data;
439
+ this.status = options.status;
440
+ }
441
+ };
442
+ __name(_EndpointFailure, "EndpointFailure");
443
+ var EndpointFailure = _EndpointFailure;
444
+ function isEndpointFailure(value) {
445
+ return value instanceof EndpointFailure;
446
+ }
447
+ __name(isEndpointFailure, "isEndpointFailure");
448
+ var EMPTY_ENDPOINT_MIDDLEWARE = Object.freeze([]);
449
+ var EMPTY_ENDPOINT_CONTEXT = Object.freeze(/* @__PURE__ */ Object.create(null));
450
+
65
451
  // src/api/runtime.ts
66
452
  function matchAPIRoute(routes, pathname) {
67
453
  const exactRoute = routes.get(pathname);
@@ -94,10 +480,10 @@ __name(invokeAPIRouteEndpoint, "invokeAPIRouteEndpoint");
94
480
  async function invokeAPIRouteEndpointInContext(endpoint, request, params) {
95
481
  const farmHandler = endpoint.__handler || (isFarmContextHandler(endpoint) ? endpoint : null);
96
482
  if (!farmHandler) {
97
- const result2 = await endpoint(request, {
483
+ const result = await endpoint(request, {
98
484
  params: Promise.resolve(params)
99
485
  });
100
- return normalizeRouteResponse(result2);
486
+ return normalizeRouteResponse(result);
101
487
  }
102
488
  const url = new URL(request.url);
103
489
  const query = {};
@@ -122,15 +508,30 @@ async function invokeAPIRouteEndpointInContext(endpoint, request, params) {
122
508
  if (headersValidation instanceof Response) {
123
509
  return headersValidation;
124
510
  }
125
- const result = await farmHandler({
511
+ const handlerContext = {
126
512
  query: queryValidation,
127
513
  body: bodyValidation,
128
514
  headers: headersValidation,
129
515
  request,
130
516
  context: {},
131
517
  params
132
- });
133
- return normalizeRouteResponse(result);
518
+ };
519
+ let execution;
520
+ try {
521
+ execution = typeof endpoint.__farmInvoke === "function" ? await endpoint.__farmInvoke(handlerContext) : {
522
+ result: await farmHandler(handlerContext),
523
+ context: handlerContext.context,
524
+ handlerExecuted: true,
525
+ invalidations: []
526
+ };
527
+ } catch (error) {
528
+ if (isEndpointFailure(error)) {
529
+ return createEndpointFailureResponse(error);
530
+ }
531
+ throw error;
532
+ }
533
+ const response = normalizeRouteResponse(execution.result);
534
+ return attachEndpointInvalidations(response, execution.invalidations);
134
535
  }
135
536
  __name(invokeAPIRouteEndpointInContext, "invokeAPIRouteEndpointInContext");
136
537
  function normalizeRouteResponse(result) {
@@ -150,6 +551,40 @@ function isWebResponse(value) {
150
551
  return value instanceof Response || typeof value === "object" && value !== null && "headers" in value && "status" in value && typeof value.arrayBuffer === "function";
151
552
  }
152
553
  __name(isWebResponse, "isWebResponse");
554
+ function attachEndpointInvalidations(response, keys) {
555
+ const existing = decodeFarmCacheInvalidations(
556
+ response.headers.get(FARM_CACHE_INVALIDATION_HEADER)
557
+ );
558
+ const encoded = encodeFarmCacheInvalidations([...existing, ...keys]);
559
+ if (!encoded) return response;
560
+ const headers = new Headers(response.headers);
561
+ headers.set(FARM_CACHE_INVALIDATION_HEADER, encoded);
562
+ return new Response(response.body, {
563
+ status: response.status,
564
+ statusText: response.statusText,
565
+ headers
566
+ });
567
+ }
568
+ __name(attachEndpointInvalidations, "attachEndpointInvalidations");
569
+ function createEndpointFailureResponse(failure) {
570
+ return new Response(
571
+ JSON.stringify({
572
+ error: {
573
+ code: failure.code,
574
+ message: failure.message,
575
+ data: failure.data
576
+ }
577
+ }),
578
+ {
579
+ status: failure.status,
580
+ headers: {
581
+ "cache-control": "no-store",
582
+ "content-type": "application/json"
583
+ }
584
+ }
585
+ );
586
+ }
587
+ __name(createEndpointFailureResponse, "createEndpointFailureResponse");
153
588
  function isFarmContextHandler(endpoint) {
154
589
  if (typeof endpoint !== "function") {
155
590
  return false;
@@ -264,6 +699,7 @@ function decodePathSegment(segment) {
264
699
  __name(decodePathSegment, "decodePathSegment");
265
700
  // Annotate the CommonJS export names for ESM import in node:
266
701
  0 && (module.exports = {
702
+ createEndpointFailureResponse,
267
703
  invokeAPIRouteEndpoint,
268
704
  isWebResponse,
269
705
  matchAPIRoute,