@smoothbricks/lmao 0.1.0

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 (447) hide show
  1. package/README.md +269 -0
  2. package/dist/allocator.wasm +0 -0
  3. package/dist/bun/preload.d.ts +11 -0
  4. package/dist/bun/preload.d.ts.map +1 -0
  5. package/dist/bun/preload.js +10 -0
  6. package/dist/bun/trace-preload.d.ts +8 -0
  7. package/dist/bun/trace-preload.d.ts.map +1 -0
  8. package/dist/bun/trace-preload.js +12 -0
  9. package/dist/es.d.ts +8 -0
  10. package/dist/es.d.ts.map +1 -0
  11. package/dist/es.js +9 -0
  12. package/dist/index.d.ts +41 -0
  13. package/dist/index.d.ts.map +1 -0
  14. package/dist/index.js +84 -0
  15. package/dist/lib/JsBufferStrategy.d.ts +49 -0
  16. package/dist/lib/JsBufferStrategy.d.ts.map +1 -0
  17. package/dist/lib/JsBufferStrategy.js +60 -0
  18. package/dist/lib/archive/chunkCompaction.d.ts +15 -0
  19. package/dist/lib/archive/chunkCompaction.d.ts.map +1 -0
  20. package/dist/lib/archive/chunkCompaction.js +24 -0
  21. package/dist/lib/archive/chunkEnvelope.d.ts +21 -0
  22. package/dist/lib/archive/chunkEnvelope.d.ts.map +1 -0
  23. package/dist/lib/archive/chunkEnvelope.js +55 -0
  24. package/dist/lib/archive/chunkRouting.d.ts +10 -0
  25. package/dist/lib/archive/chunkRouting.d.ts.map +1 -0
  26. package/dist/lib/archive/chunkRouting.js +47 -0
  27. package/dist/lib/archive/chunkStats.d.ts +12 -0
  28. package/dist/lib/archive/chunkStats.d.ts.map +1 -0
  29. package/dist/lib/archive/chunkStats.js +10 -0
  30. package/dist/lib/archive/index.d.ts +5 -0
  31. package/dist/lib/archive/index.d.ts.map +1 -0
  32. package/dist/lib/archive/index.js +4 -0
  33. package/dist/lib/arrow/capacityStats.d.ts +9 -0
  34. package/dist/lib/arrow/capacityStats.d.ts.map +1 -0
  35. package/dist/lib/arrow/capacityStats.js +231 -0
  36. package/dist/lib/arrow/dictionaries.d.ts +27 -0
  37. package/dist/lib/arrow/dictionaries.d.ts.map +1 -0
  38. package/dist/lib/arrow/dictionaries.js +135 -0
  39. package/dist/lib/arrow/flechette.d.ts +40 -0
  40. package/dist/lib/arrow/flechette.d.ts.map +1 -0
  41. package/dist/lib/arrow/flechette.js +67 -0
  42. package/dist/lib/arrow/utils.d.ts +38 -0
  43. package/dist/lib/arrow/utils.d.ts.map +1 -0
  44. package/dist/lib/arrow/utils.js +112 -0
  45. package/dist/lib/buffer/Nanoseconds.d.ts +109 -0
  46. package/dist/lib/buffer/Nanoseconds.d.ts.map +1 -0
  47. package/dist/lib/buffer/Nanoseconds.js +86 -0
  48. package/dist/lib/bufferStrategy.d.ts +95 -0
  49. package/dist/lib/bufferStrategy.d.ts.map +1 -0
  50. package/dist/lib/bufferStrategy.js +1 -0
  51. package/dist/lib/capacityTuning.d.ts +50 -0
  52. package/dist/lib/capacityTuning.d.ts.map +1 -0
  53. package/dist/lib/capacityTuning.js +65 -0
  54. package/dist/lib/codegen/evaluatorGenerator.d.ts +35 -0
  55. package/dist/lib/codegen/evaluatorGenerator.d.ts.map +1 -0
  56. package/dist/lib/codegen/evaluatorGenerator.js +199 -0
  57. package/dist/lib/codegen/fixedPositionWriterGenerator.d.ts +139 -0
  58. package/dist/lib/codegen/fixedPositionWriterGenerator.d.ts.map +1 -0
  59. package/dist/lib/codegen/fixedPositionWriterGenerator.js +289 -0
  60. package/dist/lib/codegen/spanLoggerGenerator.d.ts +115 -0
  61. package/dist/lib/codegen/spanLoggerGenerator.d.ts.map +1 -0
  62. package/dist/lib/codegen/spanLoggerGenerator.js +575 -0
  63. package/dist/lib/convertToArrow.d.ts +40 -0
  64. package/dist/lib/convertToArrow.d.ts.map +1 -0
  65. package/dist/lib/convertToArrow.js +1467 -0
  66. package/dist/lib/defineOpContext.d.ts +96 -0
  67. package/dist/lib/defineOpContext.d.ts.map +1 -0
  68. package/dist/lib/defineOpContext.js +227 -0
  69. package/dist/lib/errors/Blocked.d.ts +113 -0
  70. package/dist/lib/errors/Blocked.d.ts.map +1 -0
  71. package/dist/lib/errors/Blocked.js +68 -0
  72. package/dist/lib/errors/CodeError.d.ts +101 -0
  73. package/dist/lib/errors/CodeError.d.ts.map +1 -0
  74. package/dist/lib/errors/CodeError.js +42 -0
  75. package/dist/lib/errors/RetriesExhausted.d.ts +67 -0
  76. package/dist/lib/errors/RetriesExhausted.d.ts.map +1 -0
  77. package/dist/lib/errors/RetriesExhausted.js +31 -0
  78. package/dist/lib/errors/Transient.d.ts +115 -0
  79. package/dist/lib/errors/Transient.d.ts.map +1 -0
  80. package/dist/lib/errors/Transient.js +54 -0
  81. package/dist/lib/errors/index.d.ts +14 -0
  82. package/dist/lib/errors/index.d.ts.map +1 -0
  83. package/dist/lib/errors/index.js +16 -0
  84. package/dist/lib/errors/retry-policy.d.ts +60 -0
  85. package/dist/lib/errors/retry-policy.d.ts.map +1 -0
  86. package/dist/lib/errors/retry-policy.js +54 -0
  87. package/dist/lib/flushScheduler.d.ts +162 -0
  88. package/dist/lib/flushScheduler.d.ts.map +1 -0
  89. package/dist/lib/flushScheduler.js +388 -0
  90. package/dist/lib/library.d.ts +149 -0
  91. package/dist/lib/library.d.ts.map +1 -0
  92. package/dist/lib/library.js +588 -0
  93. package/dist/lib/logApi.d.ts +61 -0
  94. package/dist/lib/logApi.d.ts.map +1 -0
  95. package/dist/lib/logApi.js +12 -0
  96. package/dist/lib/logBinding.d.ts +63 -0
  97. package/dist/lib/logBinding.d.ts.map +1 -0
  98. package/dist/lib/logBinding.js +1 -0
  99. package/dist/lib/op.d.ts +105 -0
  100. package/dist/lib/op.d.ts.map +1 -0
  101. package/dist/lib/op.js +59 -0
  102. package/dist/lib/opContext/contextTypes.d.ts +88 -0
  103. package/dist/lib/opContext/contextTypes.d.ts.map +1 -0
  104. package/dist/lib/opContext/contextTypes.js +34 -0
  105. package/dist/lib/opContext/createOpGroup.d.ts +57 -0
  106. package/dist/lib/opContext/createOpGroup.d.ts.map +1 -0
  107. package/dist/lib/opContext/createOpGroup.js +341 -0
  108. package/dist/lib/opContext/defineOp.d.ts +83 -0
  109. package/dist/lib/opContext/defineOp.d.ts.map +1 -0
  110. package/dist/lib/opContext/defineOp.js +197 -0
  111. package/dist/lib/opContext/featureFlagTypes.d.ts +58 -0
  112. package/dist/lib/opContext/featureFlagTypes.d.ts.map +1 -0
  113. package/dist/lib/opContext/featureFlagTypes.js +1 -0
  114. package/dist/lib/opContext/index.d.ts +13 -0
  115. package/dist/lib/opContext/index.d.ts.map +1 -0
  116. package/dist/lib/opContext/index.js +15 -0
  117. package/dist/lib/opContext/opGroupTypes.d.ts +250 -0
  118. package/dist/lib/opContext/opGroupTypes.d.ts.map +1 -0
  119. package/dist/lib/opContext/opGroupTypes.js +31 -0
  120. package/dist/lib/opContext/opTypes.d.ts +48 -0
  121. package/dist/lib/opContext/opTypes.d.ts.map +1 -0
  122. package/dist/lib/opContext/opTypes.js +7 -0
  123. package/dist/lib/opContext/spanContextTypes.d.ts +347 -0
  124. package/dist/lib/opContext/spanContextTypes.d.ts.map +1 -0
  125. package/dist/lib/opContext/spanContextTypes.js +1 -0
  126. package/dist/lib/opContext/types.d.ts +297 -0
  127. package/dist/lib/opContext/types.d.ts.map +1 -0
  128. package/dist/lib/opContext/types.js +16 -0
  129. package/dist/lib/result.d.ts +308 -0
  130. package/dist/lib/result.d.ts.map +1 -0
  131. package/dist/lib/result.js +359 -0
  132. package/dist/lib/schema/LogSchema.d.ts +62 -0
  133. package/dist/lib/schema/LogSchema.d.ts.map +1 -0
  134. package/dist/lib/schema/LogSchema.js +134 -0
  135. package/dist/lib/schema/builder.d.ts +12 -0
  136. package/dist/lib/schema/builder.d.ts.map +1 -0
  137. package/dist/lib/schema/builder.js +169 -0
  138. package/dist/lib/schema/defineFeatureFlags.d.ts +60 -0
  139. package/dist/lib/schema/defineFeatureFlags.d.ts.map +1 -0
  140. package/dist/lib/schema/defineFeatureFlags.js +29 -0
  141. package/dist/lib/schema/defineLogSchema.d.ts +87 -0
  142. package/dist/lib/schema/defineLogSchema.d.ts.map +1 -0
  143. package/dist/lib/schema/defineLogSchema.js +171 -0
  144. package/dist/lib/schema/evaluator.d.ts +267 -0
  145. package/dist/lib/schema/evaluator.d.ts.map +1 -0
  146. package/dist/lib/schema/evaluator.js +169 -0
  147. package/dist/lib/schema/extend.d.ts +14 -0
  148. package/dist/lib/schema/extend.d.ts.map +1 -0
  149. package/dist/lib/schema/extend.js +51 -0
  150. package/dist/lib/schema/systemSchema.d.ts +112 -0
  151. package/dist/lib/schema/systemSchema.d.ts.map +1 -0
  152. package/dist/lib/schema/systemSchema.js +260 -0
  153. package/dist/lib/schema/typeGuards.d.ts +52 -0
  154. package/dist/lib/schema/typeGuards.d.ts.map +1 -0
  155. package/dist/lib/schema/typeGuards.js +127 -0
  156. package/dist/lib/schema/types.d.ts +156 -0
  157. package/dist/lib/schema/types.d.ts.map +1 -0
  158. package/dist/lib/schema/types.js +4 -0
  159. package/dist/lib/spanBuffer.d.ts +133 -0
  160. package/dist/lib/spanBuffer.d.ts.map +1 -0
  161. package/dist/lib/spanBuffer.js +442 -0
  162. package/dist/lib/spanBufferHelpers.d.ts +53 -0
  163. package/dist/lib/spanBufferHelpers.d.ts.map +1 -0
  164. package/dist/lib/spanBufferHelpers.js +65 -0
  165. package/dist/lib/spanBufferStats.d.ts +38 -0
  166. package/dist/lib/spanBufferStats.d.ts.map +1 -0
  167. package/dist/lib/spanBufferStats.js +1 -0
  168. package/dist/lib/spanContext.d.ts +141 -0
  169. package/dist/lib/spanContext.d.ts.map +1 -0
  170. package/dist/lib/spanContext.js +763 -0
  171. package/dist/lib/sqlite/index.d.ts +14 -0
  172. package/dist/lib/sqlite/index.d.ts.map +1 -0
  173. package/dist/lib/sqlite/index.js +12 -0
  174. package/dist/lib/sqlite/sqlite-async-writer.d.ts +24 -0
  175. package/dist/lib/sqlite/sqlite-async-writer.d.ts.map +1 -0
  176. package/dist/lib/sqlite/sqlite-async-writer.js +65 -0
  177. package/dist/lib/sqlite/sqlite-common.d.ts +37 -0
  178. package/dist/lib/sqlite/sqlite-common.d.ts.map +1 -0
  179. package/dist/lib/sqlite/sqlite-common.js +207 -0
  180. package/dist/lib/sqlite/sqlite-d1.d.ts +25 -0
  181. package/dist/lib/sqlite/sqlite-d1.d.ts.map +1 -0
  182. package/dist/lib/sqlite/sqlite-d1.js +67 -0
  183. package/dist/lib/sqlite/sqlite-db.d.ts +29 -0
  184. package/dist/lib/sqlite/sqlite-db.d.ts.map +1 -0
  185. package/dist/lib/sqlite/sqlite-db.js +1 -0
  186. package/dist/lib/sqlite/sqlite-node.d.ts +16 -0
  187. package/dist/lib/sqlite/sqlite-node.d.ts.map +1 -0
  188. package/dist/lib/sqlite/sqlite-node.js +68 -0
  189. package/dist/lib/sqlite/sqlite-writer.d.ts +39 -0
  190. package/dist/lib/sqlite/sqlite-writer.d.ts.map +1 -0
  191. package/dist/lib/sqlite/sqlite-writer.js +78 -0
  192. package/dist/lib/testing/bun-harness.d.ts +150 -0
  193. package/dist/lib/testing/bun-harness.d.ts.map +1 -0
  194. package/dist/lib/testing/bun-harness.js +729 -0
  195. package/dist/lib/testing/extractFacts.d.ts +73 -0
  196. package/dist/lib/testing/extractFacts.d.ts.map +1 -0
  197. package/dist/lib/testing/extractFacts.js +237 -0
  198. package/dist/lib/testing/facts.d.ts +235 -0
  199. package/dist/lib/testing/facts.d.ts.map +1 -0
  200. package/dist/lib/testing/facts.js +209 -0
  201. package/dist/lib/testing/index.d.ts +58 -0
  202. package/dist/lib/testing/index.d.ts.map +1 -0
  203. package/dist/lib/testing/index.js +74 -0
  204. package/dist/lib/testing/queryable-span.d.ts +39 -0
  205. package/dist/lib/testing/queryable-span.d.ts.map +1 -0
  206. package/dist/lib/testing/queryable-span.js +58 -0
  207. package/dist/lib/testing/span-query.d.ts +21 -0
  208. package/dist/lib/testing/span-query.d.ts.map +1 -0
  209. package/dist/lib/testing/span-query.js +47 -0
  210. package/dist/lib/testing/stdio-replay.d.ts +4 -0
  211. package/dist/lib/testing/stdio-replay.d.ts.map +1 -0
  212. package/dist/lib/testing/stdio-replay.js +42 -0
  213. package/dist/lib/testing/trace-query.d.ts +59 -0
  214. package/dist/lib/testing/trace-query.d.ts.map +1 -0
  215. package/dist/lib/testing/trace-query.js +462 -0
  216. package/dist/lib/testing/vitest-harness.d.ts +135 -0
  217. package/dist/lib/testing/vitest-harness.d.ts.map +1 -0
  218. package/dist/lib/testing/vitest-harness.js +486 -0
  219. package/dist/lib/threadId.d.ts +58 -0
  220. package/dist/lib/threadId.d.ts.map +1 -0
  221. package/dist/lib/threadId.js +123 -0
  222. package/dist/lib/traceContext.d.ts +116 -0
  223. package/dist/lib/traceContext.d.ts.map +1 -0
  224. package/dist/lib/traceContext.js +37 -0
  225. package/dist/lib/traceId.d.ts +78 -0
  226. package/dist/lib/traceId.d.ts.map +1 -0
  227. package/dist/lib/traceId.js +87 -0
  228. package/dist/lib/traceRoot.d.ts +133 -0
  229. package/dist/lib/traceRoot.d.ts.map +1 -0
  230. package/dist/lib/traceRoot.es.d.ts +75 -0
  231. package/dist/lib/traceRoot.es.d.ts.map +1 -0
  232. package/dist/lib/traceRoot.es.js +129 -0
  233. package/dist/lib/traceRoot.js +19 -0
  234. package/dist/lib/traceRoot.node.d.ts +77 -0
  235. package/dist/lib/traceRoot.node.d.ts.map +1 -0
  236. package/dist/lib/traceRoot.node.js +133 -0
  237. package/dist/lib/traceRoot.universal.d.ts +3 -0
  238. package/dist/lib/traceRoot.universal.d.ts.map +1 -0
  239. package/dist/lib/traceRoot.universal.js +17 -0
  240. package/dist/lib/tracer.d.ts +296 -0
  241. package/dist/lib/tracer.d.ts.map +1 -0
  242. package/dist/lib/tracer.js +346 -0
  243. package/dist/lib/tracers/ArrayQueueTracer.d.ts +56 -0
  244. package/dist/lib/tracers/ArrayQueueTracer.d.ts.map +1 -0
  245. package/dist/lib/tracers/ArrayQueueTracer.js +67 -0
  246. package/dist/lib/tracers/CompositeTracer.d.ts +24 -0
  247. package/dist/lib/tracers/CompositeTracer.d.ts.map +1 -0
  248. package/dist/lib/tracers/CompositeTracer.js +57 -0
  249. package/dist/lib/tracers/NoOpTracer.d.ts +58 -0
  250. package/dist/lib/tracers/NoOpTracer.d.ts.map +1 -0
  251. package/dist/lib/tracers/NoOpTracer.js +41 -0
  252. package/dist/lib/tracers/SQLiteTracer.d.ts +42 -0
  253. package/dist/lib/tracers/SQLiteTracer.d.ts.map +1 -0
  254. package/dist/lib/tracers/SQLiteTracer.js +84 -0
  255. package/dist/lib/tracers/StdioTracer.d.ts +83 -0
  256. package/dist/lib/tracers/StdioTracer.d.ts.map +1 -0
  257. package/dist/lib/tracers/StdioTracer.js +270 -0
  258. package/dist/lib/tracers/TestTracer.d.ts +95 -0
  259. package/dist/lib/tracers/TestTracer.d.ts.map +1 -0
  260. package/dist/lib/tracers/TestTracer.js +87 -0
  261. package/dist/lib/tracers/index.d.ts +12 -0
  262. package/dist/lib/tracers/index.d.ts.map +1 -0
  263. package/dist/lib/tracers/index.js +11 -0
  264. package/dist/lib/types.d.ts +366 -0
  265. package/dist/lib/types.d.ts.map +1 -0
  266. package/dist/lib/types.js +1 -0
  267. package/dist/lib/utf8Cache.d.ts +111 -0
  268. package/dist/lib/utf8Cache.d.ts.map +1 -0
  269. package/dist/lib/utf8Cache.js +177 -0
  270. package/dist/lib/wasm/WasmBufferStrategy.d.ts +88 -0
  271. package/dist/lib/wasm/WasmBufferStrategy.d.ts.map +1 -0
  272. package/dist/lib/wasm/WasmBufferStrategy.js +138 -0
  273. package/dist/lib/wasm/wasmAllocator.d.ts +109 -0
  274. package/dist/lib/wasm/wasmAllocator.d.ts.map +1 -0
  275. package/dist/lib/wasm/wasmAllocator.js +227 -0
  276. package/dist/lib/wasm/wasmSpanBuffer.d.ts +123 -0
  277. package/dist/lib/wasm/wasmSpanBuffer.d.ts.map +1 -0
  278. package/dist/lib/wasm/wasmSpanBuffer.js +772 -0
  279. package/dist/lib/wasm/wasmTraceRoot.d.ts +167 -0
  280. package/dist/lib/wasm/wasmTraceRoot.d.ts.map +1 -0
  281. package/dist/lib/wasm/wasmTraceRoot.js +264 -0
  282. package/dist/node.d.ts +7 -0
  283. package/dist/node.d.ts.map +1 -0
  284. package/dist/node.js +8 -0
  285. package/dist/testing.d.ts +18 -0
  286. package/dist/testing.d.ts.map +1 -0
  287. package/dist/testing.js +17 -0
  288. package/package.json +193 -0
  289. package/src/bun/preload.ts +11 -0
  290. package/src/bun/trace-preload.ts +14 -0
  291. package/src/es.ts +11 -0
  292. package/src/index.ts +194 -0
  293. package/src/lib/JsBufferStrategy.ts +102 -0
  294. package/src/lib/__tests__/arrow-builder-integration/arrow-conversion.test.ts +485 -0
  295. package/src/lib/__tests__/arrow-builder-integration/binary-arrow-conversion.test.ts +390 -0
  296. package/src/lib/__tests__/arrow-builder-integration/binary-comparison.test.ts +647 -0
  297. package/src/lib/__tests__/arrow-builder-integration/binary-format-compliance.test.ts +292 -0
  298. package/src/lib/__tests__/arrow-builder-integration/buffer-chaining.test.ts +354 -0
  299. package/src/lib/__tests__/arrow-builder-integration/foundation.test.ts +127 -0
  300. package/src/lib/__tests__/arrow-builder-integration/integration.test.ts +392 -0
  301. package/src/lib/__tests__/arrow-builder-integration/lazy-columns.test.ts +389 -0
  302. package/src/lib/__tests__/arrow-conversion-algorithms.test.ts +54 -0
  303. package/src/lib/__tests__/arrow-test-helpers.ts +71 -0
  304. package/src/lib/__tests__/binary-e2e.test.ts +203 -0
  305. package/src/lib/__tests__/buffer-overflow-op-api.test.ts +608 -0
  306. package/src/lib/__tests__/buffer-overflow.test.ts +612 -0
  307. package/src/lib/__tests__/capacity-tuning.test.ts +544 -0
  308. package/src/lib/__tests__/effective-schema.test.ts +439 -0
  309. package/src/lib/__tests__/flechette.test.ts +42 -0
  310. package/src/lib/__tests__/flushScheduler.test.ts +537 -0
  311. package/src/lib/__tests__/integration.test.ts +600 -0
  312. package/src/lib/__tests__/library.test.ts +934 -0
  313. package/src/lib/__tests__/nested-library-tasks.test.ts +849 -0
  314. package/src/lib/__tests__/op-context-binding.test.ts +135 -0
  315. package/src/lib/__tests__/op-metadata.test.ts +198 -0
  316. package/src/lib/__tests__/op-type-safety.test.ts +71 -0
  317. package/src/lib/__tests__/package-exports.test.ts +57 -0
  318. package/src/lib/__tests__/remapped-buffer-view-integration.test.ts +319 -0
  319. package/src/lib/__tests__/remapped-buffer-view.test.ts +635 -0
  320. package/src/lib/__tests__/scope-attributes.test.ts +348 -0
  321. package/src/lib/__tests__/span-lifecycle.test.ts +680 -0
  322. package/src/lib/__tests__/sync-async-trace.test.ts +435 -0
  323. package/src/lib/__tests__/test-helpers.ts +318 -0
  324. package/src/lib/__tests__/timestamp.test.ts +259 -0
  325. package/src/lib/__tests__/trace-archive-primitives.test.ts +98 -0
  326. package/src/lib/__tests__/traceContext-type-enforcement.test.ts +271 -0
  327. package/src/lib/__tests__/tracer.test.ts +211 -0
  328. package/src/lib/__tests__/type-narrowing.test.ts +137 -0
  329. package/src/lib/archive/chunkCompaction.ts +44 -0
  330. package/src/lib/archive/chunkEnvelope.ts +79 -0
  331. package/src/lib/archive/chunkRouting.ts +70 -0
  332. package/src/lib/archive/chunkStats.ts +25 -0
  333. package/src/lib/archive/index.ts +4 -0
  334. package/src/lib/arrow/capacityStats.ts +340 -0
  335. package/src/lib/arrow/dictionaries.ts +159 -0
  336. package/src/lib/arrow/flechette.ts +114 -0
  337. package/src/lib/arrow/utils.ts +126 -0
  338. package/src/lib/buffer/Nanoseconds.ts +131 -0
  339. package/src/lib/bufferStrategy.ts +106 -0
  340. package/src/lib/capacityTuning.ts +102 -0
  341. package/src/lib/codegen/__tests__/__snapshots__/evaluatorGenerator.test.ts.snap +160 -0
  342. package/src/lib/codegen/__tests__/__snapshots__/scopeGenerator.test.ts.snap +196 -0
  343. package/src/lib/codegen/__tests__/__snapshots__/spanLoggerGenerator.test.ts.snap +1965 -0
  344. package/src/lib/codegen/__tests__/evaluatorGenerator.test.ts +516 -0
  345. package/src/lib/codegen/__tests__/nullBitmap.test.ts +207 -0
  346. package/src/lib/codegen/__tests__/spanLoggerGenerator.test.ts +237 -0
  347. package/src/lib/codegen/evaluatorGenerator.ts +309 -0
  348. package/src/lib/codegen/fixedPositionWriterGenerator.ts +444 -0
  349. package/src/lib/codegen/spanLoggerGenerator.ts +715 -0
  350. package/src/lib/convertToArrow.ts +1805 -0
  351. package/src/lib/defineOpContext.ts +398 -0
  352. package/src/lib/errors/Blocked.ts +165 -0
  353. package/src/lib/errors/CodeError.ts +132 -0
  354. package/src/lib/errors/RetriesExhausted.ts +78 -0
  355. package/src/lib/errors/Transient.ts +168 -0
  356. package/src/lib/errors/__tests__/Blocked-config.test.ts +152 -0
  357. package/src/lib/errors/__tests__/Transient.test.ts +129 -0
  358. package/src/lib/errors/__tests__/retry-policy.test.ts +85 -0
  359. package/src/lib/errors/index.ts +24 -0
  360. package/src/lib/errors/retry-policy.ts +87 -0
  361. package/src/lib/flushScheduler.ts +509 -0
  362. package/src/lib/library.ts +682 -0
  363. package/src/lib/logApi.ts +70 -0
  364. package/src/lib/logBinding.ts +67 -0
  365. package/src/lib/op.ts +103 -0
  366. package/src/lib/opContext/__tests__/retry.test.ts +764 -0
  367. package/src/lib/opContext/contextTypes.ts +130 -0
  368. package/src/lib/opContext/createOpGroup.ts +456 -0
  369. package/src/lib/opContext/defineOp.ts +281 -0
  370. package/src/lib/opContext/featureFlagTypes.ts +54 -0
  371. package/src/lib/opContext/index.ts +16 -0
  372. package/src/lib/opContext/opGroupTypes.ts +342 -0
  373. package/src/lib/opContext/opTypes.ts +80 -0
  374. package/src/lib/opContext/spanContextTypes.ts +398 -0
  375. package/src/lib/opContext/types.ts +403 -0
  376. package/src/lib/result.ts +550 -0
  377. package/src/lib/schema/LogSchema.ts +153 -0
  378. package/src/lib/schema/__tests__/defineLogSchema.test.ts +342 -0
  379. package/src/lib/schema/__tests__/featureFlags.test.ts +443 -0
  380. package/src/lib/schema/builder.ts +201 -0
  381. package/src/lib/schema/defineFeatureFlags.ts +91 -0
  382. package/src/lib/schema/defineLogSchema.ts +291 -0
  383. package/src/lib/schema/evaluator.ts +404 -0
  384. package/src/lib/schema/extend.ts +69 -0
  385. package/src/lib/schema/systemSchema.ts +339 -0
  386. package/src/lib/schema/typeGuards.ts +163 -0
  387. package/src/lib/schema/types.ts +265 -0
  388. package/src/lib/spanBuffer.ts +571 -0
  389. package/src/lib/spanBufferHelpers.ts +73 -0
  390. package/src/lib/spanBufferStats.ts +40 -0
  391. package/src/lib/spanContext.ts +1566 -0
  392. package/src/lib/sqlite/__tests__/sqlite-common.test.ts +89 -0
  393. package/src/lib/sqlite/index.ts +26 -0
  394. package/src/lib/sqlite/sqlite-async-writer.ts +101 -0
  395. package/src/lib/sqlite/sqlite-common.ts +309 -0
  396. package/src/lib/sqlite/sqlite-d1.ts +104 -0
  397. package/src/lib/sqlite/sqlite-db.ts +32 -0
  398. package/src/lib/sqlite/sqlite-node.ts +83 -0
  399. package/src/lib/sqlite/sqlite-writer.ts +128 -0
  400. package/src/lib/testing/TRACE-TESTING.md +100 -0
  401. package/src/lib/testing/__tests__/bun-harness.test.ts +108 -0
  402. package/src/lib/testing/__tests__/extractFacts.test.ts +279 -0
  403. package/src/lib/testing/__tests__/facts.test.ts +324 -0
  404. package/src/lib/testing/__tests__/vitest-harness.test.ts +32 -0
  405. package/src/lib/testing/bun-harness.ts +990 -0
  406. package/src/lib/testing/extractFacts.ts +362 -0
  407. package/src/lib/testing/facts.ts +470 -0
  408. package/src/lib/testing/index.ts +141 -0
  409. package/src/lib/testing/queryable-span.ts +108 -0
  410. package/src/lib/testing/span-query.ts +65 -0
  411. package/src/lib/testing/stdio-replay.ts +50 -0
  412. package/src/lib/testing/trace-query.ts +329 -0
  413. package/src/lib/testing/vitest-harness.ts +691 -0
  414. package/src/lib/threadId.ts +135 -0
  415. package/src/lib/traceContext.ts +182 -0
  416. package/src/lib/traceId.ts +142 -0
  417. package/src/lib/traceRoot.es.ts +157 -0
  418. package/src/lib/traceRoot.node.ts +170 -0
  419. package/src/lib/traceRoot.ts +154 -0
  420. package/src/lib/traceRoot.universal.ts +22 -0
  421. package/src/lib/tracer.ts +896 -0
  422. package/src/lib/tracers/ArrayQueueTracer.ts +76 -0
  423. package/src/lib/tracers/CompositeTracer.ts +76 -0
  424. package/src/lib/tracers/NoOpTracer.ts +73 -0
  425. package/src/lib/tracers/SQLiteTracer.ts +115 -0
  426. package/src/lib/tracers/StdioTracer.ts +380 -0
  427. package/src/lib/tracers/TestTracer.ts +146 -0
  428. package/src/lib/tracers/__tests__/ArrayQueueTracer.test.ts +184 -0
  429. package/src/lib/tracers/__tests__/NoOpTracer.test.ts +204 -0
  430. package/src/lib/tracers/__tests__/StdioTracer.test.ts +313 -0
  431. package/src/lib/tracers/__tests__/TestTracer.test.ts +200 -0
  432. package/src/lib/tracers/index.ts +12 -0
  433. package/src/lib/types.ts +498 -0
  434. package/src/lib/utf8Cache.ts +201 -0
  435. package/src/lib/wasm/WasmBufferStrategy.ts +219 -0
  436. package/src/lib/wasm/__tests__/WasmBufferStrategy.test.ts +256 -0
  437. package/src/lib/wasm/__tests__/wasm-integration.test.ts +476 -0
  438. package/src/lib/wasm/__tests__/wasmAllocator.test.ts +699 -0
  439. package/src/lib/wasm/__tests__/wasmSpanBuffer.test.ts +449 -0
  440. package/src/lib/wasm/__tests__/wasmTraceRoot.test.ts +390 -0
  441. package/src/lib/wasm/__tests__/wasmTracer.test.ts.disabled +444 -0
  442. package/src/lib/wasm/allocator.zig +945 -0
  443. package/src/lib/wasm/wasmAllocator.ts +469 -0
  444. package/src/lib/wasm/wasmSpanBuffer.ts +1014 -0
  445. package/src/lib/wasm/wasmTraceRoot.ts +315 -0
  446. package/src/node.ts +10 -0
  447. package/src/testing.ts +18 -0
@@ -0,0 +1,281 @@
1
+ /**
2
+ * Op Definition Functions
3
+ *
4
+ * Factory functions for defining Ops and OpGroups. These are used internally
5
+ * by OpContextFactory to create type-safe Op definitions.
6
+ *
7
+ * @module opContext/defineOp
8
+ */
9
+
10
+ import { intern, PreEncodedEntry } from '@smoothbricks/arrow-builder';
11
+ import { Op as OpClass } from '../op.js';
12
+ import { getSpanBufferClass } from '../spanBuffer.js';
13
+ import type { OpGroup, OpGroupOps } from './opGroupTypes.js';
14
+ import type { Op, OpFn, OpMetadata, OpsFromRecord } from './opTypes.js';
15
+ import type { OpContext } from './types.js';
16
+
17
+ // =============================================================================
18
+ // METADATA HELPERS
19
+ // =============================================================================
20
+
21
+ /**
22
+ * Create OpMetadata with pre-encoded entries for Arrow dictionary building.
23
+ * Uses global interner to deduplicate UTF-8 bytes across all Ops with same strings.
24
+ */
25
+ export function createOpMetadata(
26
+ name: string,
27
+ package_name: string,
28
+ package_file: string,
29
+ git_sha: string,
30
+ line: number,
31
+ ): OpMetadata {
32
+ return {
33
+ name,
34
+ package_name,
35
+ package_file,
36
+ git_sha,
37
+ line,
38
+ // Use global interner for deduplication - same package_name across Ops shares UTF-8 bytes
39
+ package_name_entry: new PreEncodedEntry(package_name, intern(package_name)),
40
+ package_file_entry: new PreEncodedEntry(package_file, intern(package_file)),
41
+ git_sha_entry: new PreEncodedEntry(git_sha, intern(git_sha)),
42
+ };
43
+ }
44
+
45
+ // =============================================================================
46
+ // DEFAULT METADATA
47
+ // =============================================================================
48
+
49
+ /**
50
+ * Default metadata values for Ops when not injected by transformer.
51
+ * These are placeholder values that will be replaced at compile time.
52
+ */
53
+ export const DEFAULT_METADATA: OpMetadata = createOpMetadata('unknown', 'unknown', 'unknown', 'unknown', 0);
54
+
55
+ // =============================================================================
56
+ // STACK TRACE METADATA EXTRACTION
57
+ // =============================================================================
58
+
59
+ /**
60
+ * Extracts metadata from stack trace when transformer is not installed.
61
+ * Provides useful debugging info (file path, line number) even without build-time injection.
62
+ *
63
+ * Stack frame format varies by runtime:
64
+ * - V8 (Node/Chrome): " at functionName (file:///path/to/file.ts:10:5)"
65
+ * - V8 anonymous: " at file:///path/to/file.ts:10:5"
66
+ * - JSC (Safari): "functionName@file:///path/to/file.ts:10:5"
67
+ *
68
+ * @param skipFrames - Number of stack frames to skip (to get caller's location)
69
+ * @returns OpMetadata with extracted file path and line number
70
+ */
71
+ export function extractMetadataFromStack(skipFrames = 2): OpMetadata {
72
+ const err = new Error();
73
+ const stack = err.stack;
74
+
75
+ if (!stack) {
76
+ return DEFAULT_METADATA;
77
+ }
78
+
79
+ const lines = stack.split('\n');
80
+
81
+ // Skip "Error" line + specified frames to get to caller
82
+ // Frame 0: Error
83
+ // Frame 1: extractMetadataFromStack
84
+ // Frame 2: defineOpImpl (or caller)
85
+ // Frame 3: actual defineOp call site
86
+ const targetFrame = lines[skipFrames + 1];
87
+
88
+ if (!targetFrame) {
89
+ return DEFAULT_METADATA;
90
+ }
91
+
92
+ // Try V8 format: " at functionName (file:///path/to/file.ts:10:5)"
93
+ // or anonymous: " at file:///path/to/file.ts:10:5"
94
+ let match = targetFrame.match(/at\s+(?:.*?\s+\()?(.+?):(\d+):\d+\)?$/);
95
+
96
+ // Try JSC format: "functionName@file:///path/to/file.ts:10:5"
97
+ if (!match) {
98
+ match = targetFrame.match(/@(.+?):(\d+):\d+$/);
99
+ }
100
+
101
+ if (!match) {
102
+ return DEFAULT_METADATA;
103
+ }
104
+
105
+ let filePath = match[1];
106
+ const lineNumber = Number.parseInt(match[2], 10);
107
+
108
+ // Clean up file:// protocol if present
109
+ if (filePath.startsWith('file://')) {
110
+ filePath = filePath.slice(7);
111
+ }
112
+
113
+ // Extract package name from path (last directory component before src/)
114
+ // e.g., "/path/to/my-package/src/ops.ts" -> "my-package"
115
+ let packageName = 'unknown';
116
+ const srcIndex = filePath.lastIndexOf('/src/');
117
+ if (srcIndex > 0) {
118
+ const beforeSrc = filePath.slice(0, srcIndex);
119
+ const lastSlash = beforeSrc.lastIndexOf('/');
120
+ if (lastSlash >= 0) {
121
+ packageName = beforeSrc.slice(lastSlash + 1);
122
+ }
123
+ } else {
124
+ // Fallback: use filename without extension
125
+ const lastSlash = filePath.lastIndexOf('/');
126
+ const fileName = lastSlash >= 0 ? filePath.slice(lastSlash + 1) : filePath;
127
+ const dotIndex = fileName.lastIndexOf('.');
128
+ packageName = dotIndex > 0 ? fileName.slice(0, dotIndex) : fileName;
129
+ }
130
+
131
+ return createOpMetadata('unknown', packageName, filePath, 'runtime', lineNumber);
132
+ }
133
+
134
+ // =============================================================================
135
+ // FACTORY CONFIG
136
+ // =============================================================================
137
+
138
+ /**
139
+ * Configuration using OpContext bundle.
140
+ * Simplifies factory config by carrying all context types in one parameter.
141
+ */
142
+ interface DefineOpFactoryConfig<Ctx extends OpContext> {
143
+ readonly logSchema: Ctx['logSchema'];
144
+ readonly flags: Ctx['flags'];
145
+ /** WHY: threaded to Op._opContextBinding so downstream can extract schema */
146
+ readonly opContextBinding?: import('./types.js').OpContextBinding;
147
+ }
148
+
149
+ /**
150
+ * Factory function type for creating OpGroups.
151
+ * Used by createDefineOps to delegate OpGroup creation.
152
+ */
153
+ type CreateOpGroupFn<Ctx extends OpContext> = <Ops extends OpGroupOps<Ctx>>(
154
+ logSchema: Ctx['logSchema'],
155
+ flags: Ctx['flags'],
156
+ ops: Ops,
157
+ ) => OpGroup<Ctx, Ops>;
158
+
159
+ // =============================================================================
160
+ // CREATE DEFINE OP
161
+ // =============================================================================
162
+
163
+ /**
164
+ * Creates a defineOp function bound to a specific factory config.
165
+ *
166
+ * The returned function creates Op objects with:
167
+ * - metadata: includes name + merged with defaults
168
+ * - SpanBufferClass: from getSpanBufferClass(logSchema) - has static schema + stats
169
+ * - fn(ctx, ...args): the user function to execute
170
+ * - remappedViewClass: undefined for original ops (set by .prefix() method)
171
+ *
172
+ * @template Ctx - Bundled OpContext (logSchema, flags, deps, userCtx)
173
+ * @param factoryConfig - Configuration containing logSchema and flags
174
+ * @returns A defineOp function for creating Op instances
175
+ */
176
+ export function createDefineOp<Ctx extends OpContext>(
177
+ factoryConfig: DefineOpFactoryConfig<Ctx>,
178
+ ): <Args extends unknown[], S, E>(
179
+ name: string,
180
+ fn: OpFn<Ctx, Args, S, E>,
181
+ metadata?: Partial<OpMetadata>,
182
+ ) => Op<Ctx, Args, S, E> {
183
+ // Why get class from schema: Class has static schema + stats shared by all instances
184
+ const SpanBufferClass = getSpanBufferClass(factoryConfig.logSchema);
185
+
186
+ return function defineOpImpl<Args extends unknown[], S, E>(
187
+ name: string,
188
+ fn: OpFn<Ctx, Args, S, E>,
189
+ metadata?: Partial<OpMetadata>,
190
+ ): Op<Ctx, Args, S, E> {
191
+ // When transformer is not installed, extract metadata from stack trace
192
+ // This provides useful file/line info for debugging even without build-time injection
193
+ // skipFrames=3: Error -> extractMetadataFromStack -> defineOpImpl -> caller
194
+ const baseMetadata = metadata?.package_file ? DEFAULT_METADATA : extractMetadataFromStack(3);
195
+ // Use name from: 1) explicit metadata.name (transformer), 2) defineOp('name', fn), 3) baseMetadata
196
+ const finalMetadata = { ...baseMetadata, ...metadata, name: metadata?.name ?? name };
197
+
198
+ // Use the Op class which handles all span/buffer management:
199
+ // - Creates SpanBuffer for the op
200
+ // - Registers child buffers with parent
201
+ // - Sets _opMetadata and _callsiteMetadata
202
+ // - Writes span-start/span-ok/span-err entries
203
+ // - Handles exceptions with span-exception
204
+ // Why SpanBufferClass: All ops from same defineOpContext share this class for stats coordination
205
+ // Why undefined for remappedViewClass: Only prefixed ops need remapping - set by .prefix() method
206
+ return new OpClass(finalMetadata, SpanBufferClass, fn, undefined, factoryConfig.opContextBinding);
207
+ };
208
+ }
209
+
210
+ // =============================================================================
211
+ // CREATE DEFINE OPS
212
+ // =============================================================================
213
+
214
+ /**
215
+ * Type guard to check if a definition is an existing Op instance.
216
+ */
217
+ function isOp<Ctx extends OpContext>(def: unknown): def is Op<Ctx, unknown[], unknown, unknown> {
218
+ return def instanceof OpClass;
219
+ }
220
+
221
+ function hasAllDefinedOps<
222
+ Ctx extends OpContext,
223
+ Defs extends Record<string, Op<Ctx, unknown[], unknown, unknown> | OpFn<Ctx, unknown[], unknown, unknown>>,
224
+ >(definitions: Defs, ops: Record<string, Op<Ctx, unknown[], unknown, unknown>>): ops is OpsFromRecord<Ctx, Defs> {
225
+ for (const name in definitions) {
226
+ if (!(name in ops)) {
227
+ return false;
228
+ }
229
+ }
230
+ return true;
231
+ }
232
+
233
+ /**
234
+ * Creates a defineOps function bound to a specific factory config.
235
+ *
236
+ * The returned function:
237
+ * - Iterates over Object.entries(definitions)
238
+ * - For each [name, def]:
239
+ * - If def is an Op instance, use it as-is
240
+ * - Else wrap with defineOpImpl(name, def)
241
+ * - Stores all ops in a record
242
+ * - Creates OpGroup using createOpGroup(logSchema, flags, ops)
243
+ * - Returns Object.assign(opGroup, ops) to get both OpGroup methods and individual ops
244
+ *
245
+ * @template Ctx - Bundled OpContext (logSchema, flags, deps, userCtx)
246
+ * @param factoryConfig - Configuration containing logSchema and flags
247
+ * @param createOpGroup - Factory function for creating OpGroups
248
+ * @returns A defineOps function for creating OpGroups
249
+ */
250
+ export function createDefineOps<Ctx extends OpContext>(
251
+ factoryConfig: DefineOpFactoryConfig<Ctx>,
252
+ createOpGroup: CreateOpGroupFn<Ctx>,
253
+ ): <Defs extends Record<string, Op<Ctx, unknown[], unknown, unknown> | OpFn<Ctx, unknown[], unknown, unknown>>>(
254
+ definitions: Defs & ThisType<OpsFromRecord<Ctx, Defs>>,
255
+ ) => OpGroup<Ctx, OpsFromRecord<Ctx, Defs>> {
256
+ // Get the defineOp function for wrapping raw functions
257
+ const defineOp = createDefineOp<Ctx>(factoryConfig);
258
+
259
+ return function defineOpsImpl<
260
+ Defs extends Record<string, Op<Ctx, unknown[], unknown, unknown> | OpFn<Ctx, unknown[], unknown, unknown>>,
261
+ >(definitions: Defs & ThisType<OpsFromRecord<Ctx, Defs>>): OpGroup<Ctx, OpsFromRecord<Ctx, Defs>> {
262
+ // Build the ops record by processing each definition.
263
+ // Keep the runtime container broad, then narrow once we've populated every key.
264
+ const ops: Record<string, Op<Ctx, unknown[], unknown, unknown>> = {};
265
+
266
+ for (const name in definitions) {
267
+ const def = definitions[name];
268
+ ops[name] = isOp<Ctx>(def) ? def : defineOp(name, def);
269
+ }
270
+
271
+ if (!hasAllDefinedOps(definitions, ops)) {
272
+ throw new Error('defineOps failed to normalize all op definitions');
273
+ }
274
+
275
+ // Create the OpGroup
276
+ const opGroup = createOpGroup(factoryConfig.logSchema, factoryConfig.flags, ops);
277
+
278
+ // Return OpGroup merged with individual ops for direct access
279
+ return Object.assign(opGroup, ops);
280
+ };
281
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Feature Flag Types for Op-Centric API
3
+ *
4
+ * This module contains type definitions for feature flags used in the Op-centric API.
5
+ * Feature flags allow for runtime toggling of functionality with type-safe evaluation.
6
+ *
7
+ * Key concepts:
8
+ * - FeatureFlagSchema: Defines what flags exist and their types
9
+ * - BoundFeatureFlags: Typed accessor for flag values bound to a span context
10
+ *
11
+ * For the FlagEvaluator interface (getSync/getAsync/forContext), see:
12
+ * - packages/lmao/src/lib/schema/evaluator.ts
13
+ */
14
+
15
+ import type { FluentLogEntry } from '../codegen/spanLoggerGenerator.js';
16
+ import type { LogSchema } from '../schema/LogSchema.js';
17
+ import type { FeatureFlagDefinition } from '../schema/types.js';
18
+
19
+ // =============================================================================
20
+ // FEATURE FLAG TYPES
21
+ // =============================================================================
22
+
23
+ /**
24
+ * Feature flag schema - defines what flags are available and their types
25
+ */
26
+ export interface FeatureFlagSchema {
27
+ [key: string]: FeatureFlagDefinition<string | number | boolean>;
28
+ }
29
+
30
+ /**
31
+ * Feature flag accessor bound to a span context
32
+ * Provides typed getters for each defined flag
33
+ *
34
+ * Access patterns:
35
+ * - Sync flags: `ctx.ff.myFlag` returns `{ value: T, track(context?): FluentLogEntry } | undefined`
36
+ * - Async flags: `await ctx.ff.get('myFlag')` returns `{ value: T, track(context?): FluentLogEntry } | undefined`
37
+ *
38
+ * Returns `undefined` when flag is false/disabled.
39
+ * Returns wrapper object when flag is truthy/enabled, with `.track()` for usage analytics.
40
+ */
41
+ export type BoundFeatureFlags<FF extends FeatureFlagSchema> = {
42
+ readonly [K in keyof FF]: FF[K] extends FeatureFlagDefinition<infer V, 'sync'>
43
+ ? { value: V; track(context?: { action?: string; outcome?: string }): FluentLogEntry<LogSchema> } | undefined
44
+ : FF[K] extends FeatureFlagDefinition<infer V, 'async'>
45
+ ? { value: V; track(context?: { action?: string; outcome?: string }): FluentLogEntry<LogSchema> } | undefined
46
+ : never;
47
+ } & {
48
+ /** Get async flag value. Returns undefined when false, FlagContext when truthy. */
49
+ get(
50
+ flag: string,
51
+ ): Promise<
52
+ { value: unknown; track(context?: { action?: string; outcome?: string }): FluentLogEntry<LogSchema> } | undefined
53
+ >;
54
+ };
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Op-Centric API - Module Index
3
+ *
4
+ * Re-exports all types and implementation functions for the Op-centric API.
5
+ * Consumers should import from this file for a single entry point.
6
+ *
7
+ * @module opContext
8
+ */
9
+
10
+ // Re-export implementation functions
11
+ export { createOpGroup } from './createOpGroup.js';
12
+ export { createDefineOp, createDefineOps, createOpMetadata, DEFAULT_METADATA } from './defineOp.js';
13
+ // Re-export OpGroup types and helpers
14
+ export * from './opGroupTypes.js';
15
+ // Re-export all types from the hub
16
+ export * from './types.js';
@@ -0,0 +1,342 @@
1
+ /**
2
+ * OpGroup Types
3
+ *
4
+ * Type definitions for OpGroups - collections of Ops that can be mapped
5
+ * and wired as dependencies. These types enable:
6
+ * - Column mapping (prefix, rename, drop)
7
+ * - Schema contribution tracking
8
+ * - Type-safe dependency resolution
9
+ *
10
+ * Public interfaces (OpGroup, MappedOpGroup) hide internal properties from
11
+ * intellisense - users only see ops and methods they need.
12
+ * Internal interfaces (OpGroupInternal, MappedOpGroupInternal) expose all
13
+ * properties for library implementation.
14
+ *
15
+ * @module opContext/opGroupTypes
16
+ */
17
+
18
+ import type { Op } from '../op.js';
19
+ import type { LogSchema } from '../schema/LogSchema.js';
20
+ import type { SchemaFields } from '../schema/types.js';
21
+ import type { OpContext } from './types.js';
22
+
23
+ // =============================================================================
24
+ // COLUMN MAPPING TYPES
25
+ // =============================================================================
26
+
27
+ /**
28
+ * Column mapping for wiring OpGroups.
29
+ *
30
+ * Maps the library's column names to the app's column names:
31
+ * - `string` → map to that column name (must be type-compatible)
32
+ * - `null` → drop the column (library writes are ignored)
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * // Map query to shared column, rows to prefixed, drop internal column
37
+ * { query: 'query', rows: 'pg_rows', _internal: null }
38
+ * ```
39
+ */
40
+ export type ColumnMapping<LibSchema extends SchemaFields = SchemaFields> = {
41
+ [K in keyof LibSchema]?: string | null;
42
+ };
43
+
44
+ /**
45
+ * Apply a prefix to all schema field names.
46
+ * `{ status: S.number() }` with prefix 'http' → `{ http_status: S.number() }`
47
+ */
48
+ export type PrefixedSchema<T extends SchemaFields, P extends string> = {
49
+ [K in keyof T as `${P}_${K & string}`]: T[K];
50
+ };
51
+
52
+ /**
53
+ * Extract the schema fields from a LogSchema
54
+ */
55
+ export type SchemaFieldsOf<T> = T extends LogSchema<infer Fields> ? Fields : never;
56
+
57
+ /**
58
+ * Apply column mapping to schema.
59
+ * - Mapped columns get new names
60
+ * - Columns mapped to null are dropped
61
+ * - Unmapped columns keep original names (with prefix if using prefix())
62
+ */
63
+ export type MappedSchema<T extends SchemaFields, M extends ColumnMapping<T>> = {
64
+ // For each key in mapping that maps to a string, use that string as the new key
65
+ [K in keyof T as M[K] extends string ? M[K] : M[K] extends null ? never : K]: T[K];
66
+ };
67
+
68
+ // =============================================================================
69
+ // RESERVED OP NAMES
70
+ // =============================================================================
71
+
72
+ /**
73
+ * Reserved names that cannot be used as op names.
74
+ * These conflict with OpGroup methods or internal properties.
75
+ */
76
+ export const RESERVED_OP_NAMES = ['prefix', 'mapColumns'] as const;
77
+
78
+ /**
79
+ * Validate that an op name is allowed.
80
+ * Throws if the name is reserved or starts with underscore.
81
+ *
82
+ * @param name - The op name to validate
83
+ * @throws Error if name is invalid
84
+ */
85
+ export function validateOpName(name: string): void {
86
+ if (name.startsWith('_')) {
87
+ throw new Error(
88
+ `Op name "${name}" cannot start with underscore (reserved for internal properties). ` +
89
+ `Consider using "${name.slice(1)}" instead.`,
90
+ );
91
+ }
92
+ if ((RESERVED_OP_NAMES as readonly string[]).includes(name)) {
93
+ throw new Error(
94
+ `Op name "${name}" is reserved (conflicts with OpGroup method). ` +
95
+ `Consider using a more descriptive name like "${name}Op" or "${name}Operation".`,
96
+ );
97
+ }
98
+ }
99
+
100
+ // =============================================================================
101
+ // PUBLIC OP GROUP INTERFACES (visible to users in intellisense)
102
+ // =============================================================================
103
+
104
+ export type OpGroupOps<Ctx extends OpContext> = Record<string, Op<Ctx, unknown[], unknown, unknown>>;
105
+ type NoOpMembers = Record<never, never>;
106
+
107
+ /**
108
+ * A group of Ops that can be mapped and wired as a dependency.
109
+ *
110
+ * Created by defineOps() - represents a library's exported operations.
111
+ * Libraries declare their schema but don't know how the app will wire them.
112
+ *
113
+ * Note: Ops are accessed directly as properties (`.opName` not `.ops.opName`)
114
+ * for V8 hidden class optimization and cleaner API ergonomics.
115
+ *
116
+ * Internal properties (_logSchema, _flags) are hidden from intellisense
117
+ * but accessible at runtime for library implementation.
118
+ */
119
+ interface OpGroupMethods<Ctx extends OpContext, Ops extends object> {
120
+ /**
121
+ * Apply a prefix to all schema columns.
122
+ *
123
+ * Shorthand for mapColumns where each column gets the prefix.
124
+ * E.g., `httpOps.prefix('http')` transforms 'status' -> 'http_status'
125
+ *
126
+ * The prefixed columns become part of the combined app schema:
127
+ * `appSchema & { http_status: S.number(), http_url: S.category() }`
128
+ *
129
+ * @param prefix - Prefix to apply to all schema columns
130
+ * @returns MappedOpGroup with prefixed schema contribution
131
+ */
132
+ prefix<P extends string>(prefix: P): MappedOpGroup<Ctx, PrefixedSchema<SchemaFieldsOf<Ctx['logSchema']>, P>, Ops>;
133
+
134
+ /**
135
+ * Map schema columns to different names in the app's schema.
136
+ *
137
+ * Use this for:
138
+ * - Deduplication: `{ query: 'query' }` - multiple libs share same column
139
+ * - Renaming: `{ rows: 'pg_row_count' }`
140
+ * - Dropping: `{ _internal: null }` - ignore library's internal column
141
+ * - Mixed: `{ query: 'query', rows: 'pg_rows', _debug: null }`
142
+ *
143
+ * Type-safety: Target columns must have compatible schema types.
144
+ *
145
+ * @param mapping - Maps library column names to app column names (or null to drop)
146
+ * @returns MappedOpGroup with mapped schema contribution
147
+ *
148
+ * @example
149
+ * ```typescript
150
+ * // Two DB libs share 'query' column, each has unique columns, drop debug
151
+ * deps: {
152
+ * pg: postgresOps.mapColumns({ query: 'query', rows: 'pg_rows', _debug: null }),
153
+ * mysql: mysqlOps.mapColumns({ query: 'query', duration: 'mysql_duration' }),
154
+ * }
155
+ * ```
156
+ */
157
+ mapColumns<M extends ColumnMapping<SchemaFieldsOf<Ctx['logSchema']>>>(
158
+ mapping: M,
159
+ ): MappedOpGroup<Ctx, MappedSchema<SchemaFieldsOf<Ctx['logSchema']>, M>, Ops>;
160
+ }
161
+
162
+ export type OpGroup<Ctx extends OpContext, Ops extends object = NoOpMembers> = OpGroupMethods<Ctx, Ops> & Ops;
163
+
164
+ /**
165
+ * An OpGroup with column mapping applied.
166
+ *
167
+ * The mapping is used during wiring to create RemappedBufferView
168
+ * that translates the library's column writes to the app's column names.
169
+ *
170
+ * Note: Ops are accessed directly as properties (`.opName` not `.ops.opName`)
171
+ * for V8 hidden class optimization and cleaner API ergonomics.
172
+ *
173
+ * Internal properties (_logSchema, _flags, _contributedSchema, _columnMapping)
174
+ * are hidden from intellisense but accessible at runtime.
175
+ *
176
+ * @template Ctx - OpContext (bundled type with logSchema, flags, deps, userCtx)
177
+ * @template ContributedSchema - Schema fields this group contributes to app (after mapping)
178
+ */
179
+ interface MappedOpGroupMethods<Ctx extends OpContext, ContributedSchema extends SchemaFields, Ops extends object> {
180
+ /** Chain with prefix (applies prefix to current mapping) */
181
+ prefix<P extends string>(prefix: P): MappedOpGroup<Ctx, PrefixedSchema<ContributedSchema, P>, Ops>;
182
+
183
+ /** Override/extend the mapping */
184
+ mapColumns<M extends ColumnMapping<ContributedSchema>>(
185
+ mapping: M,
186
+ ): MappedOpGroup<Ctx, MappedSchema<ContributedSchema, M>, Ops>;
187
+ }
188
+
189
+ export type MappedOpGroup<
190
+ Ctx extends OpContext,
191
+ ContributedSchema extends SchemaFields,
192
+ Ops extends object = NoOpMembers,
193
+ > = MappedOpGroupMethods<Ctx, ContributedSchema, Ops> & Ops;
194
+
195
+ // =============================================================================
196
+ // INTERNAL OP GROUP INTERFACES (for library implementation)
197
+ // =============================================================================
198
+
199
+ /**
200
+ * Internal OpGroup interface with all properties exposed.
201
+ * Used by library implementation code that needs access to schema/flags.
202
+ *
203
+ * NOT exported from public API - use type assertion to access:
204
+ * `(opGroup as OpGroupInternal<Ctx>)._logSchema`
205
+ */
206
+ export interface OpGroupInternal<Ctx extends OpContext, Ops extends object = NoOpMembers>
207
+ extends OpGroupMethods<Ctx, Ops> {
208
+ /** The log schema for this group's ops (internal) */
209
+ readonly _logSchema: Ctx['logSchema'];
210
+
211
+ /** The feature flag schema (internal) */
212
+ readonly _flags: Ctx['flags'];
213
+ }
214
+
215
+ /**
216
+ * Internal MappedOpGroup interface with all properties exposed.
217
+ * Used by library implementation code that needs access to mapping details.
218
+ */
219
+ export interface MappedOpGroupInternal<
220
+ Ctx extends OpContext,
221
+ ContributedSchema extends SchemaFields,
222
+ Ops extends object = NoOpMembers,
223
+ > extends MappedOpGroupMethods<Ctx, ContributedSchema, Ops> {
224
+ /** The log schema for this group's ops (internal) */
225
+ readonly _logSchema: Ctx['logSchema'];
226
+
227
+ /** The feature flag schema (internal) */
228
+ readonly _flags: Ctx['flags'];
229
+
230
+ /** The schema fields this group contributes to the app's combined schema (internal) */
231
+ readonly _contributedSchema: ContributedSchema;
232
+
233
+ /** The column mapping (library column -> app column, or null to drop) (internal) */
234
+ readonly _columnMapping: ColumnMapping<SchemaFieldsOf<Ctx['logSchema']>>;
235
+ }
236
+
237
+ // =============================================================================
238
+ // DEPENDENCY CONFIG TYPES
239
+ // =============================================================================
240
+
241
+ /**
242
+ * Minimal structural interface for any OpGroup (internal use only).
243
+ *
244
+ * This captures only the internal properties we need to read from an OpGroup
245
+ * in dependency wiring. The internal properties exist at runtime but are
246
+ * hidden from the public TypeScript interfaces.
247
+ *
248
+ * Note: No `.ops` property - ops are accessed directly as own properties.
249
+ */
250
+ export interface AnyOpGroup {
251
+ /** The log schema - read-only access for wiring (internal property) */
252
+ readonly _logSchema: LogSchema;
253
+ }
254
+
255
+ /**
256
+ * Minimal structural interface for a MappedOpGroup (internal use only).
257
+ * Used for dependency wiring when we need access to contributed schema.
258
+ */
259
+ export interface AnyMappedOpGroup extends AnyOpGroup {
260
+ /** The contributed schema after mapping (internal property) */
261
+ readonly _contributedSchema: SchemaFields;
262
+ }
263
+
264
+ /**
265
+ * Type guard to check if an OpGroup has been mapped (has _contributedSchema)
266
+ */
267
+ export function isMappedOpGroup(dep: AnyOpGroup): dep is AnyMappedOpGroup {
268
+ return '_contributedSchema' in dep;
269
+ }
270
+
271
+ /**
272
+ * Minimal public interface for OpGroups used in deps config.
273
+ * Accepts any OpGroup or MappedOpGroup without requiring internal properties.
274
+ * Internal properties are accessed at runtime via type assertion.
275
+ */
276
+ export interface AnyOpGroupPublic {
277
+ /** Apply a prefix to all schema columns */
278
+ prefix(prefix: string): AnyOpGroupPublic;
279
+ /** Map schema columns to different names */
280
+ mapColumns(mapping: ColumnMapping): AnyOpGroupPublic;
281
+ }
282
+
283
+ /**
284
+ * Type for deps config - maps names to OpGroups (optionally mapped)
285
+ *
286
+ * This is used as a constraint (`Deps extends DepsConfig`) so it needs
287
+ * to be wide enough to accept any specific deps configuration.
288
+ *
289
+ * Uses AnyOpGroupPublic to accept OpGroup/MappedOpGroup without requiring
290
+ * internal properties in the type signature.
291
+ */
292
+ export type DepsConfig = Record<string, AnyOpGroupPublic>;
293
+
294
+ // =============================================================================
295
+ // SCHEMA COMBINATION TYPES
296
+ // =============================================================================
297
+
298
+ /**
299
+ * Extract the contributed schema from a dep (for combining into app schema)
300
+ */
301
+ export type ContributedSchemaOf<D> =
302
+ D extends MappedOpGroup<infer _Ctx, infer CS, infer _Ops>
303
+ ? CS
304
+ : D extends OpGroup<infer Ctx, infer _Ops>
305
+ ? SchemaFieldsOf<Ctx['logSchema']>
306
+ : never;
307
+
308
+ /**
309
+ * Combine all contributed schemas from deps into a union of all their fields.
310
+ * App's effective schema = appSchema & pg_contributed & mysql_contributed & ...
311
+ *
312
+ * This is used to type the combined LogSchema that has access to all columns
313
+ * from both the app and all wired dependencies.
314
+ */
315
+ export type CombinedDepsSchema<D extends DepsConfig> = UnionToIntersection<
316
+ { [K in keyof D]: ContributedSchemaOf<D[K]> }[keyof D]
317
+ >;
318
+
319
+ /**
320
+ * Helper: Convert union to intersection
321
+ * `A | B | C` → `A & B & C`
322
+ */
323
+ export type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void
324
+ ? I
325
+ : never;
326
+
327
+ /**
328
+ * The effective schema for an OpContext: app schema + all dep contributed schemas.
329
+ */
330
+ export type EffectiveSchema<T extends SchemaFields, D extends DepsConfig> = T & CombinedDepsSchema<D>;
331
+
332
+ /**
333
+ * Extract the typed deps interface from a DepsConfig.
334
+ * Each dep becomes accessible via ctx.deps.name
335
+ */
336
+ export type ResolvedDeps<D extends DepsConfig> = {
337
+ readonly [K in keyof D]: D[K] extends OpGroup<infer Ctx, infer Ops>
338
+ ? OpGroup<Ctx, Ops>
339
+ : D[K] extends MappedOpGroup<infer Ctx, infer _CS, infer Ops>
340
+ ? OpGroup<Ctx, Ops>
341
+ : never;
342
+ };