@timber-js/app 0.2.0-alpha.5 → 0.2.0-alpha.50

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 (333) hide show
  1. package/LICENSE +8 -0
  2. package/dist/_chunks/{als-registry-B7DbZ2hS.js → als-registry-Ba7URUIn.js} +1 -1
  3. package/dist/_chunks/als-registry-Ba7URUIn.js.map +1 -0
  4. package/dist/_chunks/chunk-DYhsFzuS.js +33 -0
  5. package/dist/_chunks/{debug-gwlJkDuf.js → debug-ECi_61pb.js} +2 -2
  6. package/dist/_chunks/debug-ECi_61pb.js.map +1 -0
  7. package/dist/_chunks/define-TK8C1M3x.js +279 -0
  8. package/dist/_chunks/define-TK8C1M3x.js.map +1 -0
  9. package/dist/_chunks/define-cookie-k9btcEfI.js +93 -0
  10. package/dist/_chunks/define-cookie-k9btcEfI.js.map +1 -0
  11. package/dist/_chunks/error-boundary-B9vT_YK_.js +211 -0
  12. package/dist/_chunks/error-boundary-B9vT_YK_.js.map +1 -0
  13. package/dist/_chunks/{format-DviM89f0.js → format-cX7wzEp2.js} +2 -2
  14. package/dist/_chunks/{format-DviM89f0.js.map → format-cX7wzEp2.js.map} +1 -1
  15. package/dist/_chunks/{interception-BOoWmLUA.js → interception-D2djYaIm.js} +112 -77
  16. package/dist/_chunks/interception-D2djYaIm.js.map +1 -0
  17. package/dist/_chunks/{metadata-routes-Cjmvi3rQ.js → metadata-routes-BU684ls2.js} +1 -1
  18. package/dist/_chunks/{metadata-routes-Cjmvi3rQ.js.map → metadata-routes-BU684ls2.js.map} +1 -1
  19. package/dist/_chunks/{request-context-DIkVh_jG.js → request-context-0h-6Voad.js} +95 -69
  20. package/dist/_chunks/request-context-0h-6Voad.js.map +1 -0
  21. package/dist/_chunks/segment-context-DBn-nrMN.js +69 -0
  22. package/dist/_chunks/segment-context-DBn-nrMN.js.map +1 -0
  23. package/dist/_chunks/stale-reload-4L-_skC7.js +47 -0
  24. package/dist/_chunks/stale-reload-4L-_skC7.js.map +1 -0
  25. package/dist/_chunks/{tracing-Cwn7697K.js → tracing-JI4cYUdz.js} +17 -3
  26. package/dist/_chunks/{tracing-Cwn7697K.js.map → tracing-JI4cYUdz.js.map} +1 -1
  27. package/dist/_chunks/{use-query-states-D5KaffOK.js → use-query-states-wEXY2JQB.js} +1 -1
  28. package/dist/_chunks/{use-query-states-D5KaffOK.js.map → use-query-states-wEXY2JQB.js.map} +1 -1
  29. package/dist/_chunks/wrappers-C9XPg7-U.js +63 -0
  30. package/dist/_chunks/wrappers-C9XPg7-U.js.map +1 -0
  31. package/dist/adapters/compress-module.d.ts.map +1 -1
  32. package/dist/adapters/nitro.d.ts +17 -1
  33. package/dist/adapters/nitro.d.ts.map +1 -1
  34. package/dist/adapters/nitro.js +56 -13
  35. package/dist/adapters/nitro.js.map +1 -1
  36. package/dist/cache/fast-hash.d.ts +22 -0
  37. package/dist/cache/fast-hash.d.ts.map +1 -0
  38. package/dist/cache/index.d.ts +5 -2
  39. package/dist/cache/index.d.ts.map +1 -1
  40. package/dist/cache/index.js +90 -20
  41. package/dist/cache/index.js.map +1 -1
  42. package/dist/cache/register-cached-function.d.ts.map +1 -1
  43. package/dist/cache/singleflight.d.ts +18 -1
  44. package/dist/cache/singleflight.d.ts.map +1 -1
  45. package/dist/cache/timber-cache.d.ts +1 -1
  46. package/dist/cache/timber-cache.d.ts.map +1 -1
  47. package/dist/client/error-boundary.d.ts +10 -1
  48. package/dist/client/error-boundary.d.ts.map +1 -1
  49. package/dist/client/error-boundary.js +1 -125
  50. package/dist/client/index.d.ts +3 -2
  51. package/dist/client/index.d.ts.map +1 -1
  52. package/dist/client/index.js +213 -93
  53. package/dist/client/index.js.map +1 -1
  54. package/dist/client/link.d.ts +22 -8
  55. package/dist/client/link.d.ts.map +1 -1
  56. package/dist/client/navigation-context.d.ts +2 -2
  57. package/dist/client/router.d.ts +25 -3
  58. package/dist/client/router.d.ts.map +1 -1
  59. package/dist/client/rsc-fetch.d.ts +23 -2
  60. package/dist/client/rsc-fetch.d.ts.map +1 -1
  61. package/dist/client/segment-cache.d.ts +1 -1
  62. package/dist/client/segment-cache.d.ts.map +1 -1
  63. package/dist/client/segment-context.d.ts +1 -1
  64. package/dist/client/segment-context.d.ts.map +1 -1
  65. package/dist/client/segment-merger.d.ts.map +1 -1
  66. package/dist/client/stale-reload.d.ts +15 -0
  67. package/dist/client/stale-reload.d.ts.map +1 -1
  68. package/dist/client/top-loader.d.ts +1 -1
  69. package/dist/client/top-loader.d.ts.map +1 -1
  70. package/dist/client/transition-root.d.ts +1 -1
  71. package/dist/client/transition-root.d.ts.map +1 -1
  72. package/dist/client/use-params.d.ts +2 -2
  73. package/dist/client/use-params.d.ts.map +1 -1
  74. package/dist/client/use-query-states.d.ts +1 -1
  75. package/dist/codec.d.ts +21 -0
  76. package/dist/codec.d.ts.map +1 -0
  77. package/dist/cookies/define-cookie.d.ts +33 -12
  78. package/dist/cookies/define-cookie.d.ts.map +1 -1
  79. package/dist/cookies/index.js +1 -83
  80. package/dist/fonts/css.d.ts +1 -0
  81. package/dist/fonts/css.d.ts.map +1 -1
  82. package/dist/index.d.ts +112 -35
  83. package/dist/index.d.ts.map +1 -1
  84. package/dist/index.js +467 -246
  85. package/dist/index.js.map +1 -1
  86. package/dist/params/define.d.ts +76 -0
  87. package/dist/params/define.d.ts.map +1 -0
  88. package/dist/params/index.d.ts +8 -0
  89. package/dist/params/index.d.ts.map +1 -0
  90. package/dist/params/index.js +105 -0
  91. package/dist/params/index.js.map +1 -0
  92. package/dist/plugins/adapter-build.d.ts.map +1 -1
  93. package/dist/plugins/build-manifest.d.ts.map +1 -1
  94. package/dist/plugins/client-chunks.d.ts +32 -0
  95. package/dist/plugins/client-chunks.d.ts.map +1 -0
  96. package/dist/plugins/dev-error-overlay.d.ts +26 -1
  97. package/dist/plugins/dev-error-overlay.d.ts.map +1 -1
  98. package/dist/plugins/entries.d.ts.map +1 -1
  99. package/dist/plugins/fonts.d.ts +7 -0
  100. package/dist/plugins/fonts.d.ts.map +1 -1
  101. package/dist/plugins/routing.d.ts.map +1 -1
  102. package/dist/plugins/server-bundle.d.ts.map +1 -1
  103. package/dist/plugins/static-build.d.ts.map +1 -1
  104. package/dist/routing/codegen.d.ts +2 -2
  105. package/dist/routing/codegen.d.ts.map +1 -1
  106. package/dist/routing/index.js +1 -1
  107. package/dist/routing/scanner.d.ts.map +1 -1
  108. package/dist/routing/status-file-lint.d.ts +2 -1
  109. package/dist/routing/status-file-lint.d.ts.map +1 -1
  110. package/dist/routing/types.d.ts +6 -4
  111. package/dist/routing/types.d.ts.map +1 -1
  112. package/dist/rsc-runtime/rsc.d.ts +1 -1
  113. package/dist/rsc-runtime/rsc.d.ts.map +1 -1
  114. package/dist/rsc-runtime/ssr.d.ts +12 -0
  115. package/dist/rsc-runtime/ssr.d.ts.map +1 -1
  116. package/dist/search-params/codecs.d.ts +1 -1
  117. package/dist/search-params/define.d.ts +159 -0
  118. package/dist/search-params/define.d.ts.map +1 -0
  119. package/dist/search-params/index.d.ts +4 -5
  120. package/dist/search-params/index.d.ts.map +1 -1
  121. package/dist/search-params/index.js +4 -474
  122. package/dist/search-params/registry.d.ts +1 -1
  123. package/dist/search-params/wrappers.d.ts +53 -0
  124. package/dist/search-params/wrappers.d.ts.map +1 -0
  125. package/dist/server/access-gate.d.ts +4 -0
  126. package/dist/server/access-gate.d.ts.map +1 -1
  127. package/dist/server/action-client.d.ts.map +1 -1
  128. package/dist/server/action-encryption.d.ts +76 -0
  129. package/dist/server/action-encryption.d.ts.map +1 -0
  130. package/dist/server/action-handler.d.ts.map +1 -1
  131. package/dist/server/als-registry.d.ts +18 -4
  132. package/dist/server/als-registry.d.ts.map +1 -1
  133. package/dist/server/build-manifest.d.ts +2 -2
  134. package/dist/server/debug.d.ts +1 -1
  135. package/dist/server/default-logger.d.ts +22 -0
  136. package/dist/server/default-logger.d.ts.map +1 -0
  137. package/dist/server/deny-renderer.d.ts.map +1 -1
  138. package/dist/server/early-hints.d.ts +13 -5
  139. package/dist/server/early-hints.d.ts.map +1 -1
  140. package/dist/server/error-boundary-wrapper.d.ts +4 -0
  141. package/dist/server/error-boundary-wrapper.d.ts.map +1 -1
  142. package/dist/server/flight-injection-state.d.ts +66 -0
  143. package/dist/server/flight-injection-state.d.ts.map +1 -0
  144. package/dist/server/flight-scripts.d.ts +39 -0
  145. package/dist/server/flight-scripts.d.ts.map +1 -0
  146. package/dist/server/flush.d.ts.map +1 -1
  147. package/dist/server/form-data.d.ts +29 -0
  148. package/dist/server/form-data.d.ts.map +1 -1
  149. package/dist/server/html-injectors.d.ts +51 -11
  150. package/dist/server/html-injectors.d.ts.map +1 -1
  151. package/dist/server/index.d.ts +4 -2
  152. package/dist/server/index.d.ts.map +1 -1
  153. package/dist/server/index.js +1974 -1648
  154. package/dist/server/index.js.map +1 -1
  155. package/dist/server/logger.d.ts +24 -7
  156. package/dist/server/logger.d.ts.map +1 -1
  157. package/dist/server/node-stream-transforms.d.ts +113 -0
  158. package/dist/server/node-stream-transforms.d.ts.map +1 -0
  159. package/dist/server/pipeline.d.ts +7 -4
  160. package/dist/server/pipeline.d.ts.map +1 -1
  161. package/dist/server/primitives.d.ts +30 -3
  162. package/dist/server/primitives.d.ts.map +1 -1
  163. package/dist/server/render-timeout.d.ts +51 -0
  164. package/dist/server/render-timeout.d.ts.map +1 -0
  165. package/dist/server/request-context.d.ts +65 -38
  166. package/dist/server/request-context.d.ts.map +1 -1
  167. package/dist/server/route-element-builder.d.ts +7 -0
  168. package/dist/server/route-element-builder.d.ts.map +1 -1
  169. package/dist/server/route-handler.d.ts.map +1 -1
  170. package/dist/server/route-matcher.d.ts +2 -2
  171. package/dist/server/route-matcher.d.ts.map +1 -1
  172. package/dist/server/rsc-entry/error-renderer.d.ts.map +1 -1
  173. package/dist/server/rsc-entry/helpers.d.ts +46 -3
  174. package/dist/server/rsc-entry/helpers.d.ts.map +1 -1
  175. package/dist/server/rsc-entry/index.d.ts +6 -1
  176. package/dist/server/rsc-entry/index.d.ts.map +1 -1
  177. package/dist/server/rsc-entry/rsc-payload.d.ts.map +1 -1
  178. package/dist/server/rsc-entry/rsc-stream.d.ts +9 -0
  179. package/dist/server/rsc-entry/rsc-stream.d.ts.map +1 -1
  180. package/dist/server/rsc-entry/ssr-renderer.d.ts.map +1 -1
  181. package/dist/server/slot-resolver.d.ts +1 -1
  182. package/dist/server/slot-resolver.d.ts.map +1 -1
  183. package/dist/server/ssr-entry.d.ts +22 -0
  184. package/dist/server/ssr-entry.d.ts.map +1 -1
  185. package/dist/server/ssr-render.d.ts +39 -21
  186. package/dist/server/ssr-render.d.ts.map +1 -1
  187. package/dist/server/ssr-wrappers.d.ts +50 -0
  188. package/dist/server/ssr-wrappers.d.ts.map +1 -0
  189. package/dist/server/tracing.d.ts +10 -0
  190. package/dist/server/tracing.d.ts.map +1 -1
  191. package/dist/server/tree-builder.d.ts +19 -12
  192. package/dist/server/tree-builder.d.ts.map +1 -1
  193. package/dist/server/types.d.ts +1 -3
  194. package/dist/server/types.d.ts.map +1 -1
  195. package/dist/server/version-skew.d.ts +61 -0
  196. package/dist/server/version-skew.d.ts.map +1 -0
  197. package/dist/server/waituntil-bridge.d.ts.map +1 -1
  198. package/dist/shared/merge-search-params.d.ts +22 -0
  199. package/dist/shared/merge-search-params.d.ts.map +1 -0
  200. package/dist/shims/navigation-client.d.ts +1 -1
  201. package/dist/shims/navigation-client.d.ts.map +1 -1
  202. package/dist/shims/navigation.d.ts +1 -1
  203. package/dist/shims/navigation.d.ts.map +1 -1
  204. package/dist/utils/state-machine.d.ts +80 -0
  205. package/dist/utils/state-machine.d.ts.map +1 -0
  206. package/package.json +17 -14
  207. package/src/adapters/compress-module.ts +24 -4
  208. package/src/adapters/nitro.ts +58 -9
  209. package/src/cache/fast-hash.ts +34 -0
  210. package/src/cache/index.ts +5 -2
  211. package/src/cache/register-cached-function.ts +7 -3
  212. package/src/cache/singleflight.ts +62 -4
  213. package/src/cache/timber-cache.ts +40 -29
  214. package/src/cli.ts +0 -0
  215. package/src/client/browser-entry.ts +133 -93
  216. package/src/client/error-boundary.tsx +18 -1
  217. package/src/client/index.ts +10 -1
  218. package/src/client/link.tsx +78 -19
  219. package/src/client/navigation-context.ts +2 -2
  220. package/src/client/router.ts +105 -60
  221. package/src/client/rsc-fetch.ts +63 -2
  222. package/src/client/segment-cache.ts +1 -1
  223. package/src/client/segment-context.ts +6 -1
  224. package/src/client/segment-merger.ts +2 -8
  225. package/src/client/stale-reload.ts +32 -6
  226. package/src/client/top-loader.tsx +10 -9
  227. package/src/client/transition-root.tsx +7 -1
  228. package/src/client/use-params.ts +3 -3
  229. package/src/client/use-query-states.ts +1 -1
  230. package/src/codec.ts +21 -0
  231. package/src/cookies/define-cookie.ts +69 -18
  232. package/src/fonts/css.ts +2 -1
  233. package/src/index.ts +280 -85
  234. package/src/params/define.ts +260 -0
  235. package/src/params/index.ts +28 -0
  236. package/src/plugins/adapter-build.ts +6 -0
  237. package/src/plugins/build-manifest.ts +11 -0
  238. package/src/plugins/client-chunks.ts +65 -0
  239. package/src/plugins/dev-error-overlay.ts +70 -1
  240. package/src/plugins/dev-server.ts +38 -4
  241. package/src/plugins/entries.ts +5 -7
  242. package/src/plugins/fonts.ts +93 -42
  243. package/src/plugins/routing.ts +40 -14
  244. package/src/plugins/server-bundle.ts +32 -1
  245. package/src/plugins/shims.ts +1 -1
  246. package/src/plugins/static-build.ts +8 -4
  247. package/src/routing/codegen.ts +109 -88
  248. package/src/routing/scanner.ts +55 -6
  249. package/src/routing/status-file-lint.ts +2 -1
  250. package/src/routing/types.ts +7 -4
  251. package/src/rsc-runtime/rsc.ts +2 -0
  252. package/src/rsc-runtime/ssr.ts +50 -0
  253. package/src/rsc-runtime/vendor-types.d.ts +7 -0
  254. package/src/search-params/codecs.ts +1 -1
  255. package/src/search-params/define.ts +518 -0
  256. package/src/search-params/index.ts +12 -18
  257. package/src/search-params/registry.ts +1 -1
  258. package/src/search-params/wrappers.ts +85 -0
  259. package/src/server/access-gate.tsx +40 -9
  260. package/src/server/action-client.ts +7 -1
  261. package/src/server/action-encryption.ts +144 -0
  262. package/src/server/action-handler.ts +19 -2
  263. package/src/server/als-registry.ts +18 -4
  264. package/src/server/build-manifest.ts +4 -4
  265. package/src/server/compress.ts +25 -7
  266. package/src/server/debug.ts +1 -1
  267. package/src/server/default-logger.ts +98 -0
  268. package/src/server/deny-renderer.ts +2 -1
  269. package/src/server/early-hints.ts +36 -15
  270. package/src/server/error-boundary-wrapper.ts +57 -14
  271. package/src/server/flight-injection-state.ts +113 -0
  272. package/src/server/flight-scripts.ts +59 -0
  273. package/src/server/flush.ts +2 -1
  274. package/src/server/form-data.ts +76 -0
  275. package/src/server/html-injectors.ts +261 -117
  276. package/src/server/index.ts +9 -4
  277. package/src/server/logger.ts +38 -35
  278. package/src/server/node-stream-transforms.ts +504 -0
  279. package/src/server/pipeline.ts +131 -39
  280. package/src/server/primitives.ts +47 -5
  281. package/src/server/render-timeout.ts +108 -0
  282. package/src/server/request-context.ts +119 -119
  283. package/src/server/route-element-builder.ts +106 -114
  284. package/src/server/route-handler.ts +2 -1
  285. package/src/server/route-matcher.ts +2 -2
  286. package/src/server/rsc-entry/error-renderer.ts +5 -3
  287. package/src/server/rsc-entry/helpers.ts +122 -3
  288. package/src/server/rsc-entry/index.ts +108 -43
  289. package/src/server/rsc-entry/rsc-payload.ts +52 -12
  290. package/src/server/rsc-entry/rsc-stream.ts +49 -12
  291. package/src/server/rsc-entry/ssr-renderer.ts +40 -13
  292. package/src/server/slot-resolver.ts +222 -217
  293. package/src/server/ssr-entry.ts +209 -30
  294. package/src/server/ssr-render.ts +289 -67
  295. package/src/server/ssr-wrappers.tsx +139 -0
  296. package/src/server/tracing.ts +23 -0
  297. package/src/server/tree-builder.ts +91 -57
  298. package/src/server/types.ts +1 -3
  299. package/src/server/version-skew.ts +104 -0
  300. package/src/server/waituntil-bridge.ts +4 -1
  301. package/src/shared/merge-search-params.ts +48 -0
  302. package/src/shims/navigation-client.ts +1 -1
  303. package/src/shims/navigation.ts +1 -1
  304. package/src/utils/state-machine.ts +111 -0
  305. package/dist/_chunks/als-registry-B7DbZ2hS.js.map +0 -1
  306. package/dist/_chunks/debug-gwlJkDuf.js.map +0 -1
  307. package/dist/_chunks/interception-BOoWmLUA.js.map +0 -1
  308. package/dist/_chunks/request-context-DIkVh_jG.js.map +0 -1
  309. package/dist/_chunks/ssr-data-MjmprTmO.js +0 -88
  310. package/dist/_chunks/ssr-data-MjmprTmO.js.map +0 -1
  311. package/dist/_chunks/use-cookie-DX-l1_5E.js +0 -91
  312. package/dist/_chunks/use-cookie-DX-l1_5E.js.map +0 -1
  313. package/dist/client/error-boundary.js.map +0 -1
  314. package/dist/cookies/index.js.map +0 -1
  315. package/dist/plugins/dynamic-transform.d.ts +0 -72
  316. package/dist/plugins/dynamic-transform.d.ts.map +0 -1
  317. package/dist/search-params/analyze.d.ts +0 -54
  318. package/dist/search-params/analyze.d.ts.map +0 -1
  319. package/dist/search-params/builtin-codecs.d.ts +0 -105
  320. package/dist/search-params/builtin-codecs.d.ts.map +0 -1
  321. package/dist/search-params/create.d.ts +0 -106
  322. package/dist/search-params/create.d.ts.map +0 -1
  323. package/dist/search-params/index.js.map +0 -1
  324. package/dist/server/prerender.d.ts +0 -77
  325. package/dist/server/prerender.d.ts.map +0 -1
  326. package/dist/server/response-cache.d.ts +0 -53
  327. package/dist/server/response-cache.d.ts.map +0 -1
  328. package/src/plugins/dynamic-transform.ts +0 -161
  329. package/src/search-params/analyze.ts +0 -192
  330. package/src/search-params/builtin-codecs.ts +0 -228
  331. package/src/search-params/create.ts +0 -321
  332. package/src/server/prerender.ts +0 -139
  333. package/src/server/response-cache.ts +0 -277
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Tiny typed state machine utility.
3
+ *
4
+ * Enforces discriminated-union states, typed transitions with runtime
5
+ * guards, subscribe for external store integration, and assertPhase
6
+ * for function entry guards.
7
+ *
8
+ * Designed for 3–5 state consumers (stream transforms, client navigation,
9
+ * build phase sequencing). No async, no hierarchy, no history.
10
+ *
11
+ * Performance: send() is one object lookup + one function call.
12
+ * Equivalent cost to a boolean check after V8 inlining.
13
+ */
14
+ /** A state machine instance with typed state and events. */
15
+ export interface Machine<TState extends {
16
+ phase: string;
17
+ }, TEvent extends {
18
+ type: string;
19
+ }> {
20
+ /** Current state (discriminated union — narrowed by phase). */
21
+ readonly state: TState;
22
+ /** Transition with runtime guard. Throws on invalid source+event pair. */
23
+ send(event: TEvent): void;
24
+ /** Subscribe to state changes. Returns unsubscribe function. */
25
+ subscribe(listener: (state: TState) => void): () => void;
26
+ /** Throw if not in the expected phase. Entry guard for functions. */
27
+ assertPhase<P extends TState['phase']>(phase: P): void;
28
+ }
29
+ /**
30
+ * Transition map: `transitions[phase][eventType]` returns the next state.
31
+ *
32
+ * Each handler receives the current state (narrowed by phase context)
33
+ * and the event, returning the new state.
34
+ */
35
+ export type TransitionMap<TState extends {
36
+ phase: string;
37
+ }, TEvent extends {
38
+ type: string;
39
+ }> = {
40
+ [P in TState['phase']]?: {
41
+ [E in TEvent['type']]?: (state: Extract<TState, {
42
+ phase: P;
43
+ }>, event: Extract<TEvent, {
44
+ type: E;
45
+ }>) => TState;
46
+ };
47
+ };
48
+ export interface MachineConfig<TState extends {
49
+ phase: string;
50
+ }, TEvent extends {
51
+ type: string;
52
+ }> {
53
+ initial: TState;
54
+ transitions: TransitionMap<TState, TEvent>;
55
+ /** Fires after every valid transition. Use for side effects. */
56
+ onTransition?: (prev: TState, next: TState, event: TEvent) => void;
57
+ }
58
+ /**
59
+ * Create a state machine from a config.
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * type State = { phase: 'idle' } | { phase: 'running'; pid: number };
64
+ * type Event = { type: 'START'; pid: number } | { type: 'STOP' };
65
+ *
66
+ * const m = createMachine<State, Event>({
67
+ * initial: { phase: 'idle' },
68
+ * transitions: {
69
+ * idle: { START: (_s, e) => ({ phase: 'running', pid: e.pid }) },
70
+ * running: { STOP: () => ({ phase: 'idle' }) },
71
+ * },
72
+ * });
73
+ * ```
74
+ */
75
+ export declare function createMachine<TState extends {
76
+ phase: string;
77
+ }, TEvent extends {
78
+ type: string;
79
+ }>(config: MachineConfig<TState, TEvent>): Machine<TState, TEvent>;
80
+ //# sourceMappingURL=state-machine.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state-machine.d.ts","sourceRoot":"","sources":["../../src/utils/state-machine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,4DAA4D;AAC5D,MAAM,WAAW,OAAO,CAAC,MAAM,SAAS;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,EAAE,MAAM,SAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE;IACxF,+DAA+D;IAC/D,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,0EAA0E;IAC1E,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAE1B,gEAAgE;IAChE,SAAS,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;IAEzD,qEAAqE;IACrE,WAAW,CAAC,CAAC,SAAS,MAAM,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;CACxD;AAED;;;;;GAKG;AACH,MAAM,MAAM,aAAa,CAAC,MAAM,SAAS;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,EAAE,MAAM,SAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,IAAI;KAC5F,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;SACtB,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CACtB,KAAK,EAAE,OAAO,CAAC,MAAM,EAAE;YAAE,KAAK,EAAE,CAAC,CAAA;SAAE,CAAC,EACpC,KAAK,EAAE,OAAO,CAAC,MAAM,EAAE;YAAE,IAAI,EAAE,CAAC,CAAA;SAAE,CAAC,KAChC,MAAM;KACZ;CACF,CAAC;AAEF,MAAM,WAAW,aAAa,CAAC,MAAM,SAAS;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,EAAE,MAAM,SAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE;IAC9F,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,aAAa,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC3C,gEAAgE;IAChE,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACpE;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,aAAa,CAAC,MAAM,SAAS;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,EAAE,MAAM,SAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,EAC7F,MAAM,EAAE,aAAa,CAAC,MAAM,EAAE,MAAM,CAAC,GACpC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,CAwCzB"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@timber-js/app",
3
- "version": "0.2.0-alpha.5",
4
- "description": "Vite-native React framework for Cloudflare Workers — correct HTTP semantics, real status codes, pages that work without JavaScript",
3
+ "version": "0.2.0-alpha.50",
4
+ "description": "Vite-native React framework built for Servers and Serverless Platforms — correct HTTP semantics, real status codes, pages that work without JavaScript",
5
5
  "keywords": [
6
6
  "cloudflare-workers",
7
7
  "framework",
@@ -58,6 +58,10 @@
58
58
  "types": "./dist/cookies/index.d.ts",
59
59
  "import": "./dist/cookies/index.js"
60
60
  },
61
+ "./params": {
62
+ "types": "./dist/params/index.d.ts",
63
+ "import": "./dist/params/index.js"
64
+ },
61
65
  "./search-params": {
62
66
  "types": "./dist/search-params/index.d.ts",
63
67
  "import": "./dist/search-params/index.js"
@@ -79,15 +83,10 @@
79
83
  "publishConfig": {
80
84
  "access": "public"
81
85
  },
82
- "scripts": {
83
- "build": "vite build --config vite.lib.config.ts && tsc --emitDeclarationOnly --project tsconfig.json --outDir dist",
84
- "typecheck": "tsgo --noEmit",
85
- "prepublishOnly": "pnpm run build"
86
- },
87
86
  "dependencies": {
88
- "@opentelemetry/api": "^1.9.0",
89
- "@opentelemetry/context-async-hooks": "^2.6.0",
90
- "@opentelemetry/sdk-trace-base": "^2.6.0",
87
+ "@opentelemetry/api": "^1.9.1",
88
+ "@opentelemetry/context-async-hooks": "^2.6.1",
89
+ "@opentelemetry/sdk-trace-base": "^2.6.1",
91
90
  "nitro": "3.0.260311-beta"
92
91
  },
93
92
  "peerDependencies": {
@@ -98,8 +97,8 @@
98
97
  "@vitejs/plugin-react": "^6.0.0",
99
98
  "@vitejs/plugin-rsc": ">=0.5.21",
100
99
  "nuqs": "^2.0.0",
101
- "react": "^19.2.4",
102
- "react-dom": "^19.2.4",
100
+ "react": "19.3.0-canary-ed69815c-20260323",
101
+ "react-dom": "19.3.0-canary-ed69815c-20260323",
103
102
  "vite": "^8.0.1",
104
103
  "zod": "^3.22.0 || ^4.0.0"
105
104
  },
@@ -121,6 +120,10 @@
121
120
  }
122
121
  },
123
122
  "engines": {
124
- "node": ">=20.0.0"
123
+ "node": ">=22.12.0"
124
+ },
125
+ "scripts": {
126
+ "build": "vite build --config vite.lib.config.ts && tsc --emitDeclarationOnly --project tsconfig.json --outDir dist",
127
+ "typecheck": "tsgo --noEmit"
125
128
  }
126
- }
129
+ }
@@ -22,9 +22,11 @@
22
22
  export function generateCompressModule(): string {
23
23
  return `// Generated by @timber-js/app — response compression for self-hosted deployments.
24
24
  // Do not edit — this file is regenerated on each build.
25
- // Uses CompressionStream (Web API) for gzip. Brotli is left to CDNs/reverse
26
- // proxies at streaming quality levels its ratio advantage is marginal and
27
- // node:zlib buffers output internally, breaking streaming.
25
+ // Uses node:zlib createGzip() (C++ native) on Node.js, falls back to
26
+ // CompressionStream (Web API) on other runtimes. Brotli is left to CDNs/reverse
27
+ // proxies at streaming quality levels its ratio advantage is marginal.
28
+ import { Readable } from 'node:stream';
29
+ import { createGzip, constants } from 'node:zlib';
28
30
 
29
31
  const COMPRESSIBLE_TYPES = new Set([
30
32
  'text/html', 'text/css', 'text/plain', 'text/xml', 'text/javascript',
@@ -71,7 +73,25 @@ function shouldCompress(response) {
71
73
  }
72
74
 
73
75
  function compressWithGzip(body) {
74
- return body.pipeThrough(new CompressionStream('gzip'));
76
+ // Use node:zlib (C++ native) for gzip compression. The Web Streams
77
+ // CompressionStream works but every chunk crosses the JS/Promise boundary.
78
+ // node:zlib.createGzip() compresses entirely in C++ with zero per-chunk
79
+ // Promise overhead.
80
+ //
81
+ // Convert: Web ReadableStream → Node Readable → pipe through gzip →
82
+ // Node Readable → Readable.toWeb() → Web ReadableStream
83
+ try {
84
+ const nodeReadable = Readable.fromWeb(body);
85
+ // Z_SYNC_FLUSH ensures each chunk is flushed immediately so the browser
86
+ // receives the HTML shell before Suspense boundaries resolve. Without it,
87
+ // gzip buffers internally and breaks streaming.
88
+ const gzip = createGzip({ flush: constants.Z_SYNC_FLUSH });
89
+ const compressed = nodeReadable.pipe(gzip);
90
+ return Readable.toWeb(compressed);
91
+ } catch {
92
+ // Fallback: CompressionStream (CF Workers, or if node:stream unavailable)
93
+ return body.pipeThrough(new CompressionStream('gzip'));
94
+ }
75
95
  }
76
96
 
77
97
  export function compressResponse(request, response) {
@@ -149,6 +149,23 @@ export interface NitroAdapterOptions {
149
149
  */
150
150
  preset?: NitroPreset;
151
151
 
152
+ /**
153
+ * Enable application-level gzip compression for HTML and RSC responses.
154
+ *
155
+ * When `true` (default), the origin compresses responses using the Web
156
+ * `CompressionStream` API. This is useful for self-hosted deployments
157
+ * where no reverse proxy or CDN handles compression.
158
+ *
159
+ * Set to `false` when deploying behind a reverse proxy (nginx, caddy)
160
+ * or CDN (Cloudflare, Fastly, Vercel) that compresses at the edge.
161
+ * Disabling origin compression saves CPU on the Node.js event loop —
162
+ * compressing 1MB+ streaming HTML responses takes 10-15ms of main
163
+ * thread time per request, directly reducing throughput under load.
164
+ *
165
+ * @default true
166
+ */
167
+ compress?: boolean;
168
+
152
169
  /**
153
170
  * Additional Nitro configuration to merge into the generated config.
154
171
  * Overrides default values for the selected preset.
@@ -176,6 +193,7 @@ export interface NitroAdapterOptions {
176
193
  */
177
194
  export function nitro(options: NitroAdapterOptions = {}): TimberPlatformAdapter {
178
195
  const preset = options.preset ?? 'node-server';
196
+ const compress = options.compress ?? true;
179
197
  const presetConfig = PRESET_CONFIGS[preset];
180
198
  const pendingPromises: Promise<unknown>[] = [];
181
199
 
@@ -229,7 +247,7 @@ export function nitro(options: NitroAdapterOptions = {}): TimberPlatformAdapter
229
247
  }
230
248
 
231
249
  // Generate the Nitro entry point (imports from ./rsc/ within nitro dir)
232
- const entry = generateNitroEntry(buildDir, outDir, preset);
250
+ const entry = generateNitroEntry(buildDir, outDir, preset, compress);
233
251
  await writeFile(join(outDir, 'entry.ts'), entry);
234
252
 
235
253
  // Run the Nitro build to produce a production-ready server bundle.
@@ -273,6 +291,7 @@ export function generateNitroEntry(
273
291
  buildDir: string,
274
292
  outDir: string,
275
293
  preset: NitroPreset,
294
+ compress = true,
276
295
  hasManifestInit = false
277
296
  ): string {
278
297
  // The RSC entry is the main request handler — it exports the fetch handler as default.
@@ -338,12 +357,14 @@ export function generateNitroEntry(
338
357
  // Import runWithWaitUntil only when the preset supports it.
339
358
  const waitUntilImport = supportsWaitUntil ? ', runWithWaitUntil' : '';
340
359
 
360
+ const compressImport = compress ? "import { compressResponse } from './_compress.mjs'\n" : '';
361
+ const compressCall = compress ? 'compressResponse(webRequest, webResponse)' : 'webResponse';
362
+
341
363
  return `// Generated by @timber-js/app/adapters/nitro
342
364
  // Do not edit — this file is regenerated on each build.
343
365
 
344
366
  ${manifestImport}import handler, { runWithEarlyHintsSender${waitUntilImport} } from '${serverEntryRelative}'
345
- import { compressResponse } from './_compress.mjs'
346
-
367
+ ${compressImport}
347
368
  // Set TIMBER_RUNTIME for instrumentation.ts conditional SDK initialization.
348
369
  // See design/25-production-deployments.md §"TIMBER_RUNTIME".
349
370
  process.env.TIMBER_RUNTIME = '${runtimeName}'
@@ -357,7 +378,7 @@ export default async function timberHandler(event) {
357
378
  // h3 v2: event.req is the Web Request
358
379
  const webRequest = event.req
359
380
  ${handlerCall}
360
- return compressResponse(webRequest, webResponse)
381
+ return ${compressCall}
361
382
  }
362
383
  `;
363
384
  }
@@ -544,14 +565,37 @@ const server = createServer(async (req, res) => {
544
565
 
545
566
  if (webResponse.body) {
546
567
  const reader = webResponse.body.getReader();
547
- const pump = async () => {
568
+
569
+ // Cancel the reader when the client disconnects. This causes any
570
+ // pending reader.read() to reject, breaking the pump loop. Critical
571
+ // for SSE and other infinite streams — without this, disconnected
572
+ // clients leak readers.
573
+ let clientDisconnected = false;
574
+ const onClose = () => {
575
+ clientDisconnected = true;
576
+ reader.cancel('Client disconnected').catch(() => {});
577
+ };
578
+ res.on('close', onClose);
579
+
580
+ try {
548
581
  while (true) {
549
582
  const { done, value } = await reader.read();
550
- if (done) { res.end(); return; }
583
+ if (done) break;
551
584
  res.write(value);
552
585
  }
553
- };
554
- await pump();
586
+ } catch (err) {
587
+ // reader.cancel() from the close handler causes read() to reject.
588
+ // This is expected on client disconnect — not an error.
589
+ if (!clientDisconnected) {
590
+ throw err;
591
+ }
592
+ } finally {
593
+ res.off('close', onClose);
594
+ reader.releaseLock();
595
+ if (!res.writableEnded) {
596
+ res.end();
597
+ }
598
+ }
555
599
  } else {
556
600
  res.end();
557
601
  }
@@ -611,7 +655,12 @@ async function runNitroBuild(
611
655
  userConfig?: Record<string, unknown>
612
656
  ): Promise<void> {
613
657
  const presetConfig = PRESET_CONFIGS[preset];
614
- const { createNitro, build: nitroBuild, prepare, copyPublicAssets } = await import('nitro/builder');
658
+ const {
659
+ createNitro,
660
+ build: nitroBuild,
661
+ prepare,
662
+ copyPublicAssets,
663
+ } = await import('nitro/builder');
615
664
 
616
665
  const nitro = await createNitro({
617
666
  rootDir: nitroDir,
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Fast non-cryptographic hash for cache keys.
3
+ *
4
+ * FNV-1a 64-bit produces a well-distributed hash with a collision
5
+ * probability of ~1 in 5 billion at 77k keys (birthday paradox).
6
+ * Not suitable for security, but ideal for cache key generation
7
+ * where we need speed over crypto strength.
8
+ *
9
+ * Uses BigInt for 64-bit arithmetic — supported in all modern runtimes
10
+ * including Cloudflare Workers. No node:crypto dependency.
11
+ *
12
+ * See TIM-370.
13
+ */
14
+
15
+ // FNV-1a constants for 64-bit hash
16
+ const FNV_OFFSET_BASIS = 0xcbf29ce484222325n;
17
+ const FNV_PRIME = 0x100000001b3n;
18
+ const MASK_64 = 0xffffffffffffffffn;
19
+
20
+ /**
21
+ * Compute a 64-bit FNV-1a hash of a string, returned as a 16-char hex string.
22
+ *
23
+ * 64 bits gives ~5 billion keys before a 50% collision probability
24
+ * (birthday paradox), making accidental collisions effectively impossible
25
+ * for cache key use cases.
26
+ */
27
+ export function fnv1aHash(input: string): string {
28
+ let hash = FNV_OFFSET_BASIS;
29
+ for (let i = 0; i < input.length; i++) {
30
+ hash ^= BigInt(input.charCodeAt(i));
31
+ hash = (hash * FNV_PRIME) & MASK_64;
32
+ }
33
+ return hash.toString(16).padStart(16, '0');
34
+ }
@@ -12,6 +12,9 @@ export interface CacheOptions<Fn extends (...args: any[]) => any> {
12
12
  key?: (...args: Parameters<Fn>) => string;
13
13
  staleWhileRevalidate?: boolean;
14
14
  tags?: string[] | ((...args: Parameters<Fn>) => string[]);
15
+ /** Timeout (ms) for singleflight-coalesced calls. Prevents hung fn() from
16
+ * permanently blocking all future callers for the same cache key. See TIM-518. */
17
+ timeoutMs?: number;
15
18
  }
16
19
 
17
20
  export interface MemoryCacheHandlerOptions {
@@ -87,5 +90,5 @@ export { createCache } from './timber-cache';
87
90
  export { registerCachedFunction } from './register-cached-function';
88
91
  export type { RegisterCachedFunctionOptions } from './register-cached-function';
89
92
  export { stableStringify } from './stable-stringify';
90
- export { createSingleflight } from './singleflight';
91
- export type { Singleflight } from './singleflight';
93
+ export { createSingleflight, SingleflightTimeoutError } from './singleflight';
94
+ export type { Singleflight, SingleflightOptions } from './singleflight';
@@ -1,7 +1,7 @@
1
- import { createHash } from 'node:crypto';
2
1
  import type { CacheHandler } from './index';
3
2
  import { stableStringify } from './stable-stringify';
4
3
  import { createSingleflight } from './singleflight';
4
+ import { fnv1aHash } from './fast-hash.js';
5
5
 
6
6
  const singleflight = createSingleflight();
7
7
 
@@ -27,11 +27,15 @@ export interface RegisterCachedFunctionOptions<Fn extends (...args: any[]) => an
27
27
  }
28
28
 
29
29
  /**
30
- * Generate a SHA-256 cache key from a stable function ID and serialized args.
30
+ * Generate a cache key from a stable function ID and serialized args.
31
+ *
32
+ * Uses FNV-1a (fast non-crypto hash) instead of SHA-256. The id prefix
33
+ * provides namespace isolation; the hash covers the args.
34
+ * See TIM-370.
31
35
  */
32
36
  function generateKey(id: string, args: unknown[]): string {
33
37
  const raw = id + ':' + stableStringify(args);
34
- return createHash('sha256').update(raw).digest('hex');
38
+ return id + ':' + fnv1aHash(raw);
35
39
  }
36
40
 
37
41
  /**
@@ -3,24 +3,82 @@
3
3
  * execution. All callers receive the same result (or error).
4
4
  *
5
5
  * Per-process, in-memory. Each process coalesces independently.
6
+ *
7
+ * An optional `timeoutMs` prevents hung `fn()` calls from permanently
8
+ * blocking all future callers for that key. When set, `fn()` is raced
9
+ * against a timeout — if the timeout fires first, the promise rejects
10
+ * with `SingleflightTimeoutError`, `finally` cleans up the key, and
11
+ * subsequent callers can retry. See TIM-518.
6
12
  */
13
+
14
+ export interface SingleflightOptions {
15
+ /** Maximum time (ms) a coalesced call may run before being rejected. */
16
+ timeoutMs?: number;
17
+ }
18
+
7
19
  export interface Singleflight {
8
20
  do<T>(key: string, fn: () => Promise<T>): Promise<T>;
9
21
  }
10
22
 
11
- export function createSingleflight(): Singleflight {
23
+ /**
24
+ * Error thrown when a singleflight call exceeds `timeoutMs`.
25
+ * Exported so callers can distinguish timeout from other errors.
26
+ */
27
+ export class SingleflightTimeoutError extends Error {
28
+ constructor(key: string, timeoutMs: number) {
29
+ super(`Singleflight timeout: key "${key}" exceeded ${timeoutMs}ms`);
30
+ this.name = 'SingleflightTimeoutError';
31
+ }
32
+ }
33
+
34
+ export function createSingleflight(opts?: SingleflightOptions): Singleflight {
12
35
  const inflight = new Map<string, Promise<unknown>>();
36
+ const timeoutMs = opts?.timeoutMs;
13
37
 
14
38
  return {
15
39
  do<T>(key: string, fn: () => Promise<T>): Promise<T> {
16
40
  const existing = inflight.get(key);
17
41
  if (existing) return existing as Promise<T>;
18
42
 
19
- const promise = fn().finally(() => {
43
+ let promise: Promise<T>;
44
+
45
+ if (timeoutMs != null && timeoutMs > 0) {
46
+ // Race fn() against a timeout to prevent hung calls from
47
+ // permanently blocking the key. See TIM-518.
48
+ promise = new Promise<T>((resolve, reject) => {
49
+ const timer = setTimeout(
50
+ () => reject(new SingleflightTimeoutError(key, timeoutMs)),
51
+ timeoutMs
52
+ );
53
+ // Wrap in try/catch so a synchronous throw from fn()
54
+ // (e.g. argument validation) still clears the timer.
55
+ // Without this, the timer leaks until expiry.
56
+ try {
57
+ fn().then(
58
+ (value) => {
59
+ clearTimeout(timer);
60
+ resolve(value);
61
+ },
62
+ (err) => {
63
+ clearTimeout(timer);
64
+ reject(err);
65
+ }
66
+ );
67
+ } catch (err) {
68
+ clearTimeout(timer);
69
+ reject(err);
70
+ }
71
+ });
72
+ } else {
73
+ promise = fn();
74
+ }
75
+
76
+ const tracked = promise.finally(() => {
20
77
  inflight.delete(key);
21
78
  });
22
- inflight.set(key, promise);
23
- return promise;
79
+
80
+ inflight.set(key, tracked);
81
+ return tracked as Promise<T>;
24
82
  },
25
83
  };
26
84
  }
@@ -1,17 +1,23 @@
1
- import { createHash } from 'node:crypto';
2
1
  import type { CacheHandler, CacheOptions } from './index';
3
2
  import { stableStringify } from './stable-stringify';
4
3
  import { createSingleflight } from './singleflight';
5
- import { addSpanEvent } from '#/server/tracing.js';
4
+ import { addSpanEventSync } from '#/server/tracing.js';
5
+ import { fnv1aHash } from './fast-hash.js';
6
6
 
7
- const singleflight = createSingleflight();
7
+ const defaultSingleflight = createSingleflight();
8
8
 
9
9
  /**
10
- * Generate a SHA-256 cache key from function identity and serialized args.
10
+ * Generate a cache key from function identity and serialized args.
11
+ *
12
+ * Uses FNV-1a (fast non-crypto hash) instead of SHA-256. Cache keys don't
13
+ * need collision resistance — they need speed. The fnId prefix provides
14
+ * namespace isolation; the hash covers the args.
15
+ *
16
+ * See TIM-370 for perf motivation.
11
17
  */
12
18
  function defaultKeyGenerator(fnId: string, args: unknown[]): string {
13
19
  const raw = fnId + ':' + stableStringify(args);
14
- return createHash('sha256').update(raw).digest('hex');
20
+ return fnId + ':' + fnv1aHash(raw);
15
21
  }
16
22
 
17
23
  /**
@@ -47,18 +53,25 @@ export function createCache<Fn extends (...args: any[]) => Promise<any>>(
47
53
  fn: Fn,
48
54
  opts: CacheOptions<Fn>,
49
55
  handler: CacheHandler
50
- ): (...args: Parameters<Fn>) => Promise<Awaited<ReturnType<Fn>>> {
56
+ ): Fn {
51
57
  const fnId = `timber-cache:${fnIdCounter++}`;
58
+ const sf = opts.timeoutMs
59
+ ? createSingleflight({ timeoutMs: opts.timeoutMs })
60
+ : defaultSingleflight;
52
61
 
53
- return async (...args: Parameters<Fn>): Promise<Awaited<ReturnType<Fn>>> => {
62
+ // Cast to Fn to preserve the original function's generic call signature.
63
+ // Without this, generic type parameters (e.g. <T> in apiFetch<T>) are
64
+ // erased and callers lose type safety on the return type.
65
+ return (async (...args: Parameters<Fn>): Promise<Awaited<ReturnType<Fn>>> => {
54
66
  const key = opts.key ? opts.key(...args) : defaultKeyGenerator(fnId, args);
55
67
 
56
68
  const cacheStart = performance.now();
57
69
  const cached = await handler.get(key);
58
70
 
59
71
  if (cached && !cached.stale) {
60
- // Record as OTEL span event on enclosing span (not a child span)
61
- await addSpanEvent('timber.cache.hit', {
72
+ // Record as OTEL span event on enclosing span (not a child span).
73
+ // Fire-and-forget — no microtask overhead on the cache hot path.
74
+ addSpanEventSync('timber.cache.hit', {
62
75
  key,
63
76
  duration_ms: Math.round(performance.now() - cacheStart),
64
77
  });
@@ -66,43 +79,41 @@ export function createCache<Fn extends (...args: any[]) => Promise<any>>(
66
79
  }
67
80
 
68
81
  if (cached && cached.stale && opts.staleWhileRevalidate) {
69
- // Record stale cache hit as OTEL span event
70
- await addSpanEvent('timber.cache.hit', {
82
+ // Record stale cache hit as OTEL span event (fire-and-forget).
83
+ addSpanEventSync('timber.cache.hit', {
71
84
  key,
72
85
  duration_ms: Math.round(performance.now() - cacheStart),
73
86
  stale: true,
74
87
  });
75
88
  // Serve stale immediately, trigger background refetch
76
- singleflight
77
- .do(`swr:${key}`, async () => {
78
- try {
79
- const fresh = await fn(...args);
80
- const tags = resolveTags(opts, args);
81
- await handler.set(key, fresh, { ttl: opts.ttl, tags });
82
- } catch {
83
- // Failed refetch stale entry continues to be served.
84
- // Error is swallowed per design doc: "Error is logged."
85
- }
86
- })
87
- .catch(() => {
88
- // Singleflight promise rejection handled — stale continues.
89
- });
89
+ sf.do(`swr:${key}`, async () => {
90
+ try {
91
+ const fresh = await fn(...args);
92
+ const tags = resolveTags(opts, args);
93
+ await handler.set(key, fresh, { ttl: opts.ttl, tags });
94
+ } catch {
95
+ // Failed refetch — stale entry continues to be served.
96
+ // Error is swallowed per design doc: "Error is logged."
97
+ }
98
+ }).catch(() => {
99
+ // Singleflight promise rejection handled — stale continues.
100
+ });
90
101
  return cached.value as Awaited<ReturnType<Fn>>;
91
102
  }
92
103
 
93
104
  // Cache miss (or stale without SWR) — execute with singleflight
94
- const result = await singleflight.do(key, () => fn(...args));
105
+ const result = await sf.do(key, () => fn(...args));
95
106
  const tags = resolveTags(opts, args);
96
107
  await handler.set(key, result, { ttl: opts.ttl, tags });
97
108
 
98
- // Record cache miss as OTEL span event
99
- await addSpanEvent('timber.cache.miss', {
109
+ // Record cache miss as OTEL span event (fire-and-forget).
110
+ addSpanEventSync('timber.cache.miss', {
100
111
  key,
101
112
  duration_ms: Math.round(performance.now() - cacheStart),
102
113
  });
103
114
 
104
115
  return result as Awaited<ReturnType<Fn>>;
105
- };
116
+ }) as unknown as Fn;
106
117
  }
107
118
 
108
119
  /**
package/src/cli.ts CHANGED
File without changes