@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,1014 @@
1
+ /**
2
+ * WASM-backed SpanBuffer class generation.
3
+ *
4
+ * Implements spec 01q: WASM Memory Architecture for SpanBuffer Storage
5
+ *
6
+ * Key differences from JS SpanBuffer:
7
+ * - System columns (timestamp, entry_type) live in WASM memory at _systemPtr
8
+ * - Numeric columns allocated lazily from WASM freelists
9
+ * - String columns (category, text) remain as JS string[] arrays
10
+ * - All TypedArray views created on-demand from WASM memory.buffer
11
+ */
12
+
13
+ import type { ColumnValueType } from '@smoothbricks/arrow-builder';
14
+ import { getSchemaType } from '@smoothbricks/arrow-builder';
15
+ import { checkCapacityTuning } from '../capacityTuning.js';
16
+ import type { OpMetadata } from '../op.js';
17
+ import type { LogSchema } from '../schema/LogSchema.js';
18
+ import type { SpanBufferStats } from '../spanBufferStats.js';
19
+ import type { ITraceRoot } from '../traceRoot.js';
20
+ import type { AnySpanBuffer } from '../types.js';
21
+ import type { WasmAllocator } from './wasmAllocator.js';
22
+
23
+ // =============================================================================
24
+ // Types
25
+ // =============================================================================
26
+
27
+ /**
28
+ * Options for WASM SpanBuffer instances.
29
+ * Used by createWasmSpanBuffer (root), createWasmChildSpanBuffer (child), and createWasmOverflowBuffer.
30
+ * Internal fields (_traceRoot, _scopeValues, _opMetadata, _callsiteMetadata) are passed separately
31
+ * by the create* functions.
32
+ */
33
+ export interface WasmSpanBufferOptions {
34
+ allocator: WasmAllocator;
35
+ capacity: number;
36
+ trace_id: string;
37
+ thread_id: bigint;
38
+ span_id: number;
39
+ parent_thread_id?: bigint;
40
+ parent_span_id?: number;
41
+ logSchema: LogSchema;
42
+ _traceRoot: ITraceRoot;
43
+ _scopeValues: Readonly<Record<string, unknown>>;
44
+ _opMetadata: OpMetadata;
45
+ _callsiteMetadata: OpMetadata;
46
+ }
47
+
48
+ /**
49
+ * WASM SpanBuffer instance interface.
50
+ * Extends AnySpanBuffer with WASM-specific pointer properties.
51
+ */
52
+ export interface WasmSpanBufferInstance extends AnySpanBuffer {
53
+ // ===========================================================================
54
+ // WASM pointers (internal implementation details)
55
+ // ===========================================================================
56
+ /** Byte offset into WASM memory for system columns (timestamp + entry_type) */
57
+ readonly _systemPtr: number;
58
+ /** Byte offset into WASM memory for identity block (writeIndex, span_id, trace_id) */
59
+ readonly _identityPtr: number;
60
+ /** Array of byte offsets for each column (-1 = not allocated) */
61
+ readonly _columnPtrs: Int32Array;
62
+ /** Reference to the WASM allocator */
63
+ readonly _allocator: WasmAllocator;
64
+
65
+ // Override tree structure with WASM-typed versions
66
+ _parent?: WasmSpanBufferInstance;
67
+ _children: WasmSpanBufferInstance[];
68
+ _overflow?: WasmSpanBufferInstance;
69
+
70
+ // Override message column - WASM stores as JS array (not WASM memory)
71
+ readonly _message: string[];
72
+
73
+ // ===========================================================================
74
+ // WASM-specific methods
75
+ // ===========================================================================
76
+ /** Free all WASM memory for this buffer */
77
+ free(): void;
78
+
79
+ /** Check if column is allocated */
80
+ isColumnAllocated(columnIndex: number): boolean;
81
+
82
+ /** Get column values array if allocated */
83
+ getColumnIfAllocated(columnName: string): ColumnValueType | undefined;
84
+
85
+ /** Get column nulls array if allocated */
86
+ getNullsIfAllocated(columnName: string): Uint8Array | undefined;
87
+ }
88
+
89
+ /**
90
+ * Constructor type for generated WASM SpanBuffer classes.
91
+ */
92
+ export interface WasmSpanBufferConstructor {
93
+ new (opts: WasmSpanBufferOptions): WasmSpanBufferInstance;
94
+ readonly schema: LogSchema;
95
+ stats: SpanBufferStats; // Mutable stats shared across all instances
96
+ }
97
+
98
+ // =============================================================================
99
+ // Column Metadata
100
+ // =============================================================================
101
+
102
+ /**
103
+ * Size class for WASM allocation (determines which freelist to use).
104
+ */
105
+ type SizeClass = '1b' | '4b' | '8b' | 'string';
106
+
107
+ /**
108
+ * Metadata for a single column - used during code generation.
109
+ */
110
+ interface ColumnMeta {
111
+ /** Column name from schema */
112
+ name: string;
113
+ /** WASM allocation size class */
114
+ sizeClass: SizeClass;
115
+ /** Schema type (enum, category, text, number, etc.) */
116
+ schemaType: string;
117
+ /** Index in the _columnPtrs array */
118
+ columnIndex: number;
119
+ /** Whether this column is eager (always allocated) */
120
+ isEager: boolean;
121
+ /** Enum values if schemaType === 'enum' */
122
+ enumValues?: readonly string[];
123
+ }
124
+
125
+ function getFieldEager(value: unknown): boolean {
126
+ return typeof value === 'object' && value !== null && Reflect.get(value, '__eager') === true;
127
+ }
128
+
129
+ function getFieldEnumValues(value: unknown): readonly string[] | undefined {
130
+ if (typeof value !== 'object' || value === null) {
131
+ return undefined;
132
+ }
133
+ const enumValues = Reflect.get(value, '__enum_values');
134
+ return Array.isArray(enumValues) ? enumValues : undefined;
135
+ }
136
+
137
+ function isColumnValueType(value: unknown): value is ColumnValueType {
138
+ return value === undefined || Array.isArray(value) || ArrayBuffer.isView(value);
139
+ }
140
+
141
+ function getFreeMethod(
142
+ sizeClass: Exclude<SizeClass, 'string'>,
143
+ ): keyof Pick<WasmAllocator, 'free1B' | 'free4B' | 'free8B'> {
144
+ switch (sizeClass) {
145
+ case '1b':
146
+ return 'free1B';
147
+ case '4b':
148
+ return 'free4B';
149
+ case '8b':
150
+ return 'free8B';
151
+ }
152
+ }
153
+
154
+ function isWasmSpanBufferConstructor(value: unknown): value is WasmSpanBufferConstructor {
155
+ return (
156
+ typeof value === 'function' &&
157
+ typeof Reflect.get(value, 'schema') === 'object' &&
158
+ Reflect.get(value, 'schema') !== null &&
159
+ typeof Reflect.get(value, 'stats') === 'object' &&
160
+ Reflect.get(value, 'stats') !== null
161
+ );
162
+ }
163
+
164
+ export function isWasmSpanBufferInstance(value: unknown): value is WasmSpanBufferInstance {
165
+ return typeof value === 'object' && value !== null && typeof Reflect.get(value, '_systemPtr') === 'number';
166
+ }
167
+
168
+ function getWasmSpanBufferConstructor(instance: { constructor: unknown }): WasmSpanBufferConstructor {
169
+ if (!isWasmSpanBufferConstructor(instance.constructor)) {
170
+ throw new Error('Expected generated WasmSpanBuffer constructor');
171
+ }
172
+ return instance.constructor;
173
+ }
174
+
175
+ function isWasmSpanBufferFactory(value: unknown): value is () => WasmSpanBufferConstructor {
176
+ return typeof value === 'function';
177
+ }
178
+
179
+ /**
180
+ * Build column metadata array from schema.
181
+ * Maps each schema field to its storage characteristics.
182
+ */
183
+ function buildColumnMeta(schema: LogSchema): ColumnMeta[] {
184
+ const result: ColumnMeta[] = [];
185
+ const fields = schema.fields;
186
+ let columnIndex = 0;
187
+
188
+ for (const name of schema._columnNames) {
189
+ const field = fields[name];
190
+ const schemaType = getSchemaType(field);
191
+ const isEager = getFieldEager(field);
192
+ const enumValues = getFieldEnumValues(field);
193
+
194
+ let sizeClass: SizeClass;
195
+ switch (schemaType) {
196
+ case 'enum':
197
+ case 'boolean':
198
+ sizeClass = '1b';
199
+ break;
200
+ case 'number':
201
+ case 'bigUint64':
202
+ sizeClass = '8b';
203
+ break;
204
+ case 'category':
205
+ case 'text':
206
+ sizeClass = 'string';
207
+ break;
208
+ default:
209
+ sizeClass = '8b'; // Default to largest for unknown types
210
+ }
211
+
212
+ result.push({
213
+ name,
214
+ sizeClass,
215
+ schemaType: schemaType ?? 'unknown',
216
+ columnIndex,
217
+ isEager,
218
+ enumValues,
219
+ });
220
+ columnIndex++;
221
+ }
222
+
223
+ return result;
224
+ }
225
+
226
+ // =============================================================================
227
+ // Common WasmSpanBuffer Methods (shared via prototype)
228
+ // =============================================================================
229
+
230
+ /**
231
+ * Get timestamp column view from WASM memory.
232
+ * Assigned to prototype after class generation.
233
+ */
234
+ function wasmGetTimestamp(this: WasmSpanBufferInstance): BigInt64Array {
235
+ return new BigInt64Array(this._allocator.memory.buffer, this._systemPtr, this._capacity);
236
+ }
237
+
238
+ /**
239
+ * Get entry_type column view from WASM memory.
240
+ * Assigned to prototype after class generation.
241
+ */
242
+ function wasmGetEntryType(this: WasmSpanBufferInstance): Uint8Array {
243
+ return new Uint8Array(this._allocator.memory.buffer, this._systemPtr + this._capacity * 8, this._capacity);
244
+ }
245
+
246
+ /**
247
+ * Get writeIndex from WASM identity block.
248
+ */
249
+ function wasmGetWriteIndex(this: WasmSpanBufferInstance): number {
250
+ return this._allocator.readWriteIndex(this._identityPtr);
251
+ }
252
+
253
+ /**
254
+ * Set writeIndex in WASM identity block.
255
+ */
256
+ function wasmSetWriteIndex(this: WasmSpanBufferInstance, value: number): void {
257
+ new DataView(this._allocator.memory.buffer).setUint32(this._identityPtr, value, true);
258
+ }
259
+
260
+ /**
261
+ * Get trace_id from WASM identity block.
262
+ */
263
+ function wasmGetTraceId(this: WasmSpanBufferInstance): string {
264
+ const len = this._allocator.readIdentityTraceIdLen(this._identityPtr);
265
+ if (len === 0) {
266
+ let p = this._parent;
267
+ while (p?._parent) p = p._parent;
268
+ return p ? p.trace_id : '';
269
+ }
270
+ const traceIdPtr = this._allocator.getIdentityTraceIdPtr(this._identityPtr);
271
+ return new TextDecoder().decode(new Uint8Array(this._allocator.memory.buffer, traceIdPtr, len));
272
+ }
273
+
274
+ function wasmGetSpanStartTime(this: WasmSpanBufferInstance): bigint {
275
+ return this.timestamp[0];
276
+ }
277
+
278
+ function wasmGetLastLoggedTime(this: WasmSpanBufferInstance): bigint | null {
279
+ const chain: WasmSpanBufferInstance[] = [];
280
+ let current: WasmSpanBufferInstance | undefined = this;
281
+ while (current) {
282
+ chain.push(current);
283
+ current = current._overflow;
284
+ }
285
+
286
+ for (let i = chain.length - 1; i >= 0; i--) {
287
+ const buffer = chain[i];
288
+ for (let row = buffer._writeIndex - 1; row >= 0; row--) {
289
+ const ts = buffer.timestamp[row];
290
+ if (ts !== 0n) {
291
+ return ts;
292
+ }
293
+ }
294
+ }
295
+
296
+ return null;
297
+ }
298
+
299
+ /**
300
+ * Get thread_id from WASM identity block.
301
+ */
302
+ function wasmGetThreadId(this: WasmSpanBufferInstance): bigint {
303
+ const high = this._allocator.getThreadIdHigh();
304
+ const low = this._allocator.getThreadIdLow();
305
+ return (BigInt(high) << 32n) | BigInt(low);
306
+ }
307
+
308
+ /**
309
+ * Get span_id from WASM identity block.
310
+ */
311
+ function wasmGetSpanId(this: WasmSpanBufferInstance): number {
312
+ return this._allocator.readIdentitySpanId(this._identityPtr);
313
+ }
314
+
315
+ /**
316
+ * Get parent_thread_id.
317
+ */
318
+ function wasmGetParentThreadId(this: WasmSpanBufferInstance): bigint {
319
+ return this._parent ? this._parent.thread_id : 0n;
320
+ }
321
+
322
+ /**
323
+ * Get parent_span_id.
324
+ */
325
+ function wasmGetParentSpanId(this: WasmSpanBufferInstance): number {
326
+ return this._parent ? this._parent.span_id : 0;
327
+ }
328
+
329
+ /**
330
+ * Check if buffer has parent.
331
+ */
332
+ function wasmGetHasParent(this: WasmSpanBufferInstance): boolean {
333
+ return this._parent !== null;
334
+ }
335
+
336
+ /**
337
+ * Message column setter.
338
+ */
339
+ function wasmMessage(this: WasmSpanBufferInstance, idx: number, value: string): WasmSpanBufferInstance {
340
+ this._message[idx] = value;
341
+ return this;
342
+ }
343
+
344
+ /**
345
+ * Get message column values.
346
+ */
347
+ function wasmGetMessageValues(this: WasmSpanBufferInstance): string[] {
348
+ return this._message;
349
+ }
350
+
351
+ /**
352
+ * Get message column nulls (always undefined for eager column).
353
+ */
354
+ function wasmGetMessageNulls(this: WasmSpanBufferInstance): undefined {
355
+ return undefined;
356
+ }
357
+
358
+ /**
359
+ * Free all WASM memory for this buffer.
360
+ */
361
+ function wasmFree(this: WasmSpanBufferInstance): void {
362
+ // Free identity block
363
+ this._allocator.freeIdentity(this._identityPtr);
364
+
365
+ // Free system block
366
+ this._allocator.freeSpanSystem(this._systemPtr);
367
+
368
+ // Free column blocks
369
+ const columnMeta = buildColumnMeta(this._logSchema);
370
+ for (const col of columnMeta) {
371
+ if (col.sizeClass !== 'string' && this._columnPtrs[col.columnIndex] >= 0) {
372
+ const freeMethod = getFreeMethod(col.sizeClass);
373
+ this._allocator[freeMethod](this._columnPtrs[col.columnIndex]);
374
+ }
375
+ }
376
+ }
377
+
378
+ /**
379
+ * Check if column is allocated.
380
+ */
381
+ function wasmIsColumnAllocated(this: WasmSpanBufferInstance, columnIndex: number): boolean {
382
+ return this._columnPtrs[columnIndex] >= 0;
383
+ }
384
+
385
+ /**
386
+ * Get column values if allocated.
387
+ */
388
+ function wasmGetColumnIfAllocated(this: WasmSpanBufferInstance, columnName: string): ColumnValueType | undefined {
389
+ const idx = this._logSchema._columnNames.indexOf(columnName);
390
+ if (idx === -1) return undefined;
391
+ const getter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this), `${columnName}_values`);
392
+ if (getter?.get) {
393
+ const value = getter.get.call(this);
394
+ return isColumnValueType(value) ? value : undefined;
395
+ }
396
+ return undefined;
397
+ }
398
+
399
+ /**
400
+ * Get column nulls if allocated.
401
+ */
402
+ function wasmGetNullsIfAllocated(this: WasmSpanBufferInstance, columnName: string): Uint8Array | undefined {
403
+ const getter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this), `${columnName}_nulls`);
404
+ if (getter?.get) {
405
+ return getter.get.call(this);
406
+ }
407
+ return undefined;
408
+ }
409
+
410
+ /**
411
+ * Get system ArrayBuffer for compatibility with AnySpanBuffer.
412
+ * WASM uses shared memory, so this returns the entire WASM memory buffer.
413
+ */
414
+ function wasmGetSystem(this: WasmSpanBufferInstance): ArrayBuffer {
415
+ return this._allocator.memory.buffer;
416
+ }
417
+
418
+ /**
419
+ * Get identity Uint8Array for compatibility with AnySpanBuffer.
420
+ * Returns a view of the identity block (48 bytes) in WASM memory.
421
+ */
422
+ function wasmGetIdentity(this: WasmSpanBufferInstance): Uint8Array {
423
+ // Identity block is 48 bytes: writeIndex(4) + reserved(4) + span_id(4) + trace_id_len(1) + trace_id(up to 35)
424
+ return new Uint8Array(this._allocator.memory.buffer, this._identityPtr, 48);
425
+ }
426
+
427
+ /**
428
+ * Get or create overflow buffer.
429
+ */
430
+ function wasmGetOrCreateOverflow(this: WasmSpanBufferInstance): WasmSpanBufferInstance {
431
+ if (this._overflow) return this._overflow;
432
+ const tracer = this._traceRoot.tracer;
433
+ tracer.onStatsWillResetFor(this);
434
+ checkCapacityTuning(getWasmSpanBufferConstructor(this).stats);
435
+ // WasmSpanBufferInstance extends AnySpanBuffer and has all SpanBuffer properties at runtime
436
+ const overflow = tracer.bufferStrategy.createOverflowBuffer(this);
437
+ if (!isWasmSpanBufferInstance(overflow)) {
438
+ throw new Error('Expected overflow buffer to be WASM-backed');
439
+ }
440
+ return overflow;
441
+ }
442
+
443
+ /**
444
+ * Get _stats property.
445
+ */
446
+ function wasmGetStats(this: WasmSpanBufferInstance): SpanBufferStats {
447
+ return getWasmSpanBufferConstructor(this).stats;
448
+ }
449
+
450
+ /**
451
+ * Get _columns property.
452
+ */
453
+ function wasmGetColumns(this: WasmSpanBufferInstance): LogSchema['_columns'] {
454
+ return getWasmSpanBufferConstructor(this).schema._columns;
455
+ }
456
+
457
+ /**
458
+ * Custom inspect for debugging.
459
+ */
460
+ function wasmInspect(this: WasmSpanBufferInstance): string {
461
+ return `WasmSpanBuffer { _writeIndex: ${this._writeIndex}, _capacity: ${this._capacity}, trace_id: ${this.trace_id ?? 'N/A'}, _systemPtr: ${this._systemPtr} }`;
462
+ }
463
+
464
+ // =============================================================================
465
+ // Code Generation Helpers
466
+ // =============================================================================
467
+
468
+ /**
469
+ * Generate eager column initialization code for constructor.
470
+ * Eager columns are pre-allocated, lazy columns start as undefined/-1.
471
+ */
472
+ function generateEagerColumnInit(columnMeta: ColumnMeta[]): string {
473
+ const lines: string[] = [];
474
+
475
+ for (const col of columnMeta) {
476
+ if (col.name === 'message') continue; // Message handled separately
477
+
478
+ if (col.sizeClass === 'string') {
479
+ if (col.isEager) {
480
+ lines.push(`this._${col.name}_values = new Array(this._capacity);`);
481
+ } else {
482
+ lines.push(`this._${col.name}_values = undefined;`);
483
+ lines.push(`this._${col.name}_nulls = undefined;`);
484
+ }
485
+ } else if (col.isEager) {
486
+ lines.push(`this._columnPtrs[${col.columnIndex}] = this._allocator.alloc${col.sizeClass.toUpperCase()}();`);
487
+ }
488
+ }
489
+
490
+ return lines.join('\n ');
491
+ }
492
+
493
+ /**
494
+ * Generate setter method for a numeric column (stored in WASM).
495
+ * Calls WASM exports directly for hot path optimization.
496
+ */
497
+ function generateNumericSetter(col: ColumnMeta): string {
498
+ const allocMethod = `alloc${col.sizeClass.toUpperCase()}`;
499
+
500
+ // Map to WASM export names
501
+ const writeExportName =
502
+ col.schemaType === 'number' || col.schemaType === 'bigUint64'
503
+ ? 'write_col_f64'
504
+ : col.sizeClass === '1b'
505
+ ? 'write_col_u8'
506
+ : col.sizeClass === '4b'
507
+ ? 'write_col_u32'
508
+ : 'write_col_f64';
509
+
510
+ if (col.isEager) {
511
+ // Eager: column is pre-allocated, just write value (call WASM export directly)
512
+ return `${col.name}(idx, value) {
513
+ const ptr = this._columnPtrs[${col.columnIndex}];
514
+ this._allocator.exports.${writeExportName}(ptr, idx, value, this._capacity);
515
+ return this;
516
+ }`;
517
+ }
518
+
519
+ // Lazy: allocate on first write (call WASM export directly)
520
+ return `${col.name}(idx, value) {
521
+ let ptr = this._columnPtrs[${col.columnIndex}];
522
+ if (ptr < 0) {
523
+ ptr = this._allocator.${allocMethod}();
524
+ this._columnPtrs[${col.columnIndex}] = ptr;
525
+ }
526
+ this._allocator.exports.${writeExportName}(ptr, idx, value, this._capacity);
527
+ return this;
528
+ }`;
529
+ }
530
+
531
+ /**
532
+ * Generate getter for numeric column values (creates view on demand).
533
+ */
534
+ function generateNumericValuesGetter(col: ColumnMeta): string {
535
+ const arrayType =
536
+ col.schemaType === 'number'
537
+ ? 'Float64Array'
538
+ : col.schemaType === 'bigUint64'
539
+ ? 'BigUint64Array'
540
+ : col.sizeClass === '1b'
541
+ ? 'Uint8Array'
542
+ : col.sizeClass === '4b'
543
+ ? 'Uint32Array'
544
+ : 'Float64Array';
545
+
546
+ const bytesPerElement = col.sizeClass === '1b' ? 1 : col.sizeClass === '4b' ? 4 : 8;
547
+
548
+ return `get ${col.name}_values() {
549
+ const ptr = this._columnPtrs[${col.columnIndex}];
550
+ if (ptr < 0) return ${col.isEager ? 'null' : 'undefined'};
551
+ const nullBitmapSize = Math.ceil(this._capacity / 8);
552
+ const valueOffset = ptr + nullBitmapSize;
553
+ // Align offset for typed array
554
+ const alignedOffset = (valueOffset + ${bytesPerElement - 1}) & ~${bytesPerElement - 1};
555
+ return new ${arrayType}(
556
+ this._allocator.memory.buffer,
557
+ alignedOffset,
558
+ this._capacity
559
+ );
560
+ }`;
561
+ }
562
+
563
+ /**
564
+ * Generate getter for numeric column nulls bitmap.
565
+ */
566
+ function generateNumericNullsGetter(col: ColumnMeta): string {
567
+ if (col.isEager) {
568
+ // Eager columns don't have null bitmaps
569
+ return `get ${col.name}_nulls() {
570
+ return undefined;
571
+ }`;
572
+ }
573
+
574
+ return `get ${col.name}_nulls() {
575
+ const ptr = this._columnPtrs[${col.columnIndex}];
576
+ if (ptr < 0) return undefined;
577
+ const nullBitmapSize = Math.ceil(this._capacity / 8);
578
+ return new Uint8Array(
579
+ this._allocator.memory.buffer,
580
+ ptr,
581
+ nullBitmapSize
582
+ );
583
+ }`;
584
+ }
585
+
586
+ /**
587
+ * Generate setter method for a string column (stored in JS).
588
+ */
589
+ function generateStringSetter(col: ColumnMeta): string {
590
+ if (col.isEager) {
591
+ return `${col.name}(idx, value) {
592
+ this._${col.name}_values[idx] = value;
593
+ return this;
594
+ }`;
595
+ }
596
+
597
+ // Lazy string column: allocate arrays on first write
598
+ return `${col.name}(idx, value) {
599
+ if (this._${col.name}_values === undefined) {
600
+ this._${col.name}_values = new Array(this._capacity);
601
+ const nullBitmapSize = Math.ceil(this._capacity / 8);
602
+ this._${col.name}_nulls = new Uint8Array(nullBitmapSize);
603
+ }
604
+ if (value == null) {
605
+ // Clear validity bit
606
+ this._${col.name}_nulls[idx >>> 3] &= ~(1 << (idx & 7));
607
+ } else {
608
+ this._${col.name}_values[idx] = value;
609
+ // Set validity bit
610
+ this._${col.name}_nulls[idx >>> 3] |= (1 << (idx & 7));
611
+ }
612
+ return this;
613
+ }`;
614
+ }
615
+
616
+ /**
617
+ * Generate getter for string column values.
618
+ */
619
+ function generateStringValuesGetter(col: ColumnMeta): string {
620
+ if (col.isEager) {
621
+ return `get ${col.name}_values() {
622
+ return this._${col.name}_values;
623
+ }`;
624
+ }
625
+
626
+ return `get ${col.name}_values() {
627
+ return this._${col.name}_values;
628
+ }`;
629
+ }
630
+
631
+ /**
632
+ * Generate getter for string column nulls.
633
+ */
634
+ function generateStringNullsGetter(col: ColumnMeta): string {
635
+ if (col.isEager) {
636
+ return `get ${col.name}_nulls() {
637
+ return undefined;
638
+ }`;
639
+ }
640
+
641
+ return `get ${col.name}_nulls() {
642
+ return this._${col.name}_nulls;
643
+ }`;
644
+ }
645
+
646
+ /**
647
+ * Generate column methods for all schema columns.
648
+ * Note: 'message' column is handled specially by generateMessageMethods() and excluded here.
649
+ */
650
+ function generateColumnMethods(columnMeta: ColumnMeta[]): string {
651
+ const methods: string[] = [];
652
+
653
+ for (const col of columnMeta) {
654
+ // Skip 'message' - handled specially by generateMessageMethods()
655
+ if (col.name === 'message') {
656
+ continue;
657
+ }
658
+
659
+ if (col.sizeClass === 'string') {
660
+ methods.push(generateStringSetter(col));
661
+ methods.push(generateStringValuesGetter(col));
662
+ methods.push(generateStringNullsGetter(col));
663
+ } else {
664
+ methods.push(generateNumericSetter(col));
665
+ methods.push(generateNumericValuesGetter(col));
666
+ methods.push(generateNumericNullsGetter(col));
667
+ }
668
+ }
669
+
670
+ return methods.join('\n\n ');
671
+ }
672
+
673
+ // =============================================================================
674
+ // Class Cache and Generation
675
+ // =============================================================================
676
+
677
+ /**
678
+ * Cache for generated WASM SpanBuffer classes.
679
+ * Key is the schema object reference (WeakMap for GC).
680
+ */
681
+ const wasmSpanBufferClassCache = new WeakMap<LogSchema, WasmSpanBufferConstructor>();
682
+
683
+ /**
684
+ * Generate a WASM SpanBuffer class for the given schema.
685
+ *
686
+ * Generates minimal constructor + column methods, then assigns common methods to prototype.
687
+ *
688
+ * @param schema - LogSchema defining the buffer structure
689
+ * @returns WasmSpanBufferConstructor for creating buffer instances
690
+ */
691
+ export function getWasmSpanBufferClass(schema: LogSchema): WasmSpanBufferConstructor {
692
+ const cached = wasmSpanBufferClassCache.get(schema);
693
+ if (cached) {
694
+ return cached;
695
+ }
696
+
697
+ // Pre-compute column metadata
698
+ const columnMeta = buildColumnMeta(schema);
699
+
700
+ // Generate ONLY schema-specific code (constructor + column methods)
701
+ const classCode = `
702
+ class WasmSpanBuffer {
703
+ constructor(opts) {
704
+ // Store allocator and capacity
705
+ this._allocator = opts.allocator;
706
+ this._capacity = opts.capacity;
707
+ this._logSchema = opts.logSchema;
708
+
709
+ // Allocate identity block from WASM (root has trace_id, child does not)
710
+ if (opts.trace_id) {
711
+ const traceIdBytes = new TextEncoder().encode(opts.trace_id);
712
+ const packed = opts.allocator.allocIdentityRootForJsWrite(traceIdBytes.length);
713
+ this._identityPtr = Number(packed >> 32n);
714
+ const traceIdOffset = Number(packed & 0xFFFFFFFFn);
715
+ new Uint8Array(opts.allocator.memory.buffer).set(traceIdBytes, traceIdOffset);
716
+ } else {
717
+ this._identityPtr = opts.allocator.allocIdentityChild();
718
+ }
719
+
720
+ // Allocate system block from WASM
721
+ this._systemPtr = opts.allocator.allocSpanSystem();
722
+
723
+ // Initialize column pointers as unallocated (-1)
724
+ this._columnPtrs = new Int32Array(${columnMeta.length}).fill(-1);
725
+
726
+ // Tree structure
727
+ this._parent = null;
728
+ this._children = [];
729
+ this._overflow = null;
730
+
731
+ // Assign context properties from opts
732
+ this._traceRoot = opts._traceRoot;
733
+ this._scopeValues = opts._scopeValues;
734
+ this._opMetadata = opts._opMetadata;
735
+ this._callsiteMetadata = opts._callsiteMetadata;
736
+
737
+ // Initialize message array
738
+ this._message = new Array(opts.capacity);
739
+
740
+ // Initialize eager columns
741
+ ${generateEagerColumnInit(columnMeta)}
742
+ }
743
+
744
+ // Schema-specific column methods (ONLY part that differs per schema)
745
+ ${generateColumnMethods(columnMeta)}
746
+ }
747
+
748
+ return WasmSpanBuffer;
749
+ `;
750
+
751
+ // Create the class using Function constructor
752
+ const factory = new Function(classCode);
753
+ if (!isWasmSpanBufferFactory(factory)) {
754
+ throw new Error('Expected WasmSpanBuffer factory function');
755
+ }
756
+ const WasmSpanBufferClass = factory();
757
+
758
+ // Assign common methods to prototype (shared by all schemas)
759
+ Object.defineProperty(WasmSpanBufferClass.prototype, 'timestamp', {
760
+ get: wasmGetTimestamp,
761
+ enumerable: true,
762
+ configurable: true,
763
+ });
764
+ Object.defineProperty(WasmSpanBufferClass.prototype, 'entry_type', {
765
+ get: wasmGetEntryType,
766
+ enumerable: true,
767
+ configurable: true,
768
+ });
769
+ Object.defineProperty(WasmSpanBufferClass.prototype, '_writeIndex', {
770
+ get: wasmGetWriteIndex,
771
+ set: wasmSetWriteIndex,
772
+ enumerable: true,
773
+ configurable: true,
774
+ });
775
+ Object.defineProperty(WasmSpanBufferClass.prototype, 'trace_id', {
776
+ get: wasmGetTraceId,
777
+ enumerable: true,
778
+ configurable: true,
779
+ });
780
+ Object.defineProperty(WasmSpanBufferClass.prototype, 'thread_id', {
781
+ get: wasmGetThreadId,
782
+ enumerable: true,
783
+ configurable: true,
784
+ });
785
+ Object.defineProperty(WasmSpanBufferClass.prototype, 'span_id', {
786
+ get: wasmGetSpanId,
787
+ enumerable: true,
788
+ configurable: true,
789
+ });
790
+ Object.defineProperty(WasmSpanBufferClass.prototype, 'parent_thread_id', {
791
+ get: wasmGetParentThreadId,
792
+ enumerable: true,
793
+ configurable: true,
794
+ });
795
+ Object.defineProperty(WasmSpanBufferClass.prototype, 'parent_span_id', {
796
+ get: wasmGetParentSpanId,
797
+ enumerable: true,
798
+ configurable: true,
799
+ });
800
+ Object.defineProperty(WasmSpanBufferClass.prototype, '_hasParent', {
801
+ get: wasmGetHasParent,
802
+ enumerable: true,
803
+ configurable: true,
804
+ });
805
+ Object.defineProperty(WasmSpanBufferClass.prototype, 'message_values', {
806
+ get: wasmGetMessageValues,
807
+ enumerable: true,
808
+ configurable: true,
809
+ });
810
+ Object.defineProperty(WasmSpanBufferClass.prototype, 'message_nulls', {
811
+ get: wasmGetMessageNulls,
812
+ enumerable: true,
813
+ configurable: true,
814
+ });
815
+ Object.defineProperty(WasmSpanBufferClass.prototype, '_stats', {
816
+ get: wasmGetStats,
817
+ enumerable: true,
818
+ configurable: true,
819
+ });
820
+ Object.defineProperty(WasmSpanBufferClass.prototype, '_columns', {
821
+ get: wasmGetColumns,
822
+ enumerable: true,
823
+ configurable: true,
824
+ });
825
+ Object.defineProperty(WasmSpanBufferClass.prototype, '_spanStartTime', {
826
+ get: wasmGetSpanStartTime,
827
+ enumerable: true,
828
+ configurable: true,
829
+ });
830
+ Object.defineProperty(WasmSpanBufferClass.prototype, '_lastLoggedTime', {
831
+ get: wasmGetLastLoggedTime,
832
+ enumerable: true,
833
+ configurable: true,
834
+ });
835
+
836
+ // Assign compatibility getters for AnySpanBuffer interface
837
+ Object.defineProperty(WasmSpanBufferClass.prototype, '_system', {
838
+ get: wasmGetSystem,
839
+ enumerable: true,
840
+ configurable: true,
841
+ });
842
+ Object.defineProperty(WasmSpanBufferClass.prototype, '_identity', {
843
+ get: wasmGetIdentity,
844
+ enumerable: true,
845
+ configurable: true,
846
+ });
847
+
848
+ WasmSpanBufferClass.prototype.message = wasmMessage;
849
+ WasmSpanBufferClass.prototype.free = wasmFree;
850
+ WasmSpanBufferClass.prototype.isColumnAllocated = wasmIsColumnAllocated;
851
+ WasmSpanBufferClass.prototype.getColumnIfAllocated = wasmGetColumnIfAllocated;
852
+ WasmSpanBufferClass.prototype.getNullsIfAllocated = wasmGetNullsIfAllocated;
853
+ WasmSpanBufferClass.prototype.getOrCreateOverflow = wasmGetOrCreateOverflow;
854
+ WasmSpanBufferClass.prototype[Symbol.for('nodejs.util.inspect.custom')] = wasmInspect;
855
+
856
+ // Add static schema property
857
+ Object.defineProperty(WasmSpanBufferClass, 'schema', {
858
+ value: schema,
859
+ writable: false,
860
+ enumerable: true,
861
+ configurable: false,
862
+ });
863
+
864
+ // Add static stats property (required by SpanContext and arrow-builder's ColumnWriter)
865
+ // This matches the structure in spanBuffer.ts
866
+ // IMPORTANT: stats object itself must be writable so totalWrites++ and spansCreated++ can modify it
867
+ const statsObject = {
868
+ capacity: 64, // Default capacity, will be overridden by allocator
869
+ totalWrites: 0,
870
+ spansCreated: 0,
871
+ };
872
+ Object.defineProperty(WasmSpanBufferClass, 'stats', {
873
+ value: statsObject,
874
+ writable: true,
875
+ enumerable: true,
876
+ configurable: true,
877
+ });
878
+
879
+ // Cache the class
880
+ wasmSpanBufferClassCache.set(schema, WasmSpanBufferClass);
881
+
882
+ return WasmSpanBufferClass;
883
+ }
884
+
885
+ // =============================================================================
886
+ // Factory Functions
887
+ // =============================================================================
888
+
889
+ /**
890
+ * Create a WASM SpanBuffer instance.
891
+ *
892
+ * @param schema - LogSchema defining the buffer structure
893
+ * @param opts - Options for buffer creation
894
+ * @param _traceRoot - Trace root context
895
+ * @param _scopeValues - Scope values for this span
896
+ * @param _opMetadata - Op metadata for attribution
897
+ * @param _callsiteMetadata - Callsite metadata for attribution
898
+ * @returns WasmSpanBufferInstance
899
+ */
900
+ export function createWasmSpanBuffer(
901
+ schema: LogSchema,
902
+ opts: Omit<WasmSpanBufferOptions, 'logSchema' | '_traceRoot' | '_scopeValues' | '_opMetadata' | '_callsiteMetadata'>,
903
+ _traceRoot: ITraceRoot,
904
+ _scopeValues: Readonly<Record<string, unknown>>,
905
+ _opMetadata: OpMetadata,
906
+ _callsiteMetadata: OpMetadata,
907
+ ): WasmSpanBufferInstance {
908
+ const WasmSpanBufferClass = getWasmSpanBufferClass(schema);
909
+ return new WasmSpanBufferClass({
910
+ ...opts,
911
+ logSchema: schema,
912
+ _traceRoot,
913
+ _scopeValues,
914
+ _opMetadata,
915
+ _callsiteMetadata,
916
+ });
917
+ }
918
+
919
+ /**
920
+ * Create a child WASM SpanBuffer linked to a parent.
921
+ *
922
+ * For cross-library calls, the child buffer may use a different schema than the parent.
923
+ * Pass the schema option to use a different schema.
924
+ *
925
+ * @param parent - Parent WASM SpanBuffer
926
+ * @param opts - Options for child buffer creation (schema is optional, defaults to parent's)
927
+ * @param _traceRoot - Trace root context
928
+ * @param _scopeValues - Scope values for this span
929
+ * @param _opMetadata - Op metadata for attribution
930
+ * @param _callsiteMetadata - Callsite metadata for attribution
931
+ * @returns WasmSpanBufferInstance linked to parent
932
+ */
933
+ export function createWasmChildSpanBuffer(
934
+ parent: WasmSpanBufferInstance,
935
+ opts: Omit<
936
+ WasmSpanBufferOptions,
937
+ | 'logSchema'
938
+ | 'trace_id'
939
+ | 'parent_thread_id'
940
+ | 'parent_span_id'
941
+ | '_traceRoot'
942
+ | '_scopeValues'
943
+ | '_opMetadata'
944
+ | '_callsiteMetadata'
945
+ > & {
946
+ schema?: LogSchema;
947
+ },
948
+ _traceRoot: ITraceRoot,
949
+ _scopeValues: Readonly<Record<string, unknown>>,
950
+ _opMetadata: OpMetadata,
951
+ _callsiteMetadata: OpMetadata,
952
+ ): WasmSpanBufferInstance {
953
+ // Use provided schema (for cross-library calls) or parent's schema
954
+ const childSchema = opts.schema ?? parent._logSchema;
955
+ const WasmSpanBufferClass = getWasmSpanBufferClass(childSchema);
956
+
957
+ const child = new WasmSpanBufferClass({
958
+ ...opts,
959
+ logSchema: childSchema, // Use the child's schema (may differ from parent)
960
+ trace_id: parent.trace_id,
961
+ parent_thread_id: parent.thread_id,
962
+ parent_span_id: parent.span_id,
963
+ _traceRoot,
964
+ _scopeValues,
965
+ _opMetadata,
966
+ _callsiteMetadata,
967
+ });
968
+
969
+ // Link to parent (SpanContext will push to parent._children with possible RemappedBufferView wrapper)
970
+ child._parent = parent;
971
+
972
+ return child;
973
+ }
974
+
975
+ /**
976
+ * Create an overflow WASM SpanBuffer for chaining.
977
+ *
978
+ * @param buffer - The full buffer that needs overflow handling
979
+ * @param _traceRoot - Trace root context
980
+ * @param _scopeValues - Scope values for this span
981
+ * @param _opMetadata - Op metadata for attribution
982
+ * @param _callsiteMetadata - Callsite metadata for attribution
983
+ * @returns WasmSpanBufferInstance linked as overflow
984
+ */
985
+ export function createWasmOverflowBuffer(
986
+ buffer: WasmSpanBufferInstance,
987
+ _traceRoot: ITraceRoot,
988
+ _scopeValues: Readonly<Record<string, unknown>>,
989
+ _opMetadata: OpMetadata,
990
+ _callsiteMetadata: OpMetadata,
991
+ ): WasmSpanBufferInstance {
992
+ const WasmSpanBufferClass = getWasmSpanBufferClass(buffer._logSchema);
993
+
994
+ const overflow = new WasmSpanBufferClass({
995
+ allocator: buffer._allocator,
996
+ capacity: buffer._capacity,
997
+ trace_id: buffer.trace_id,
998
+ thread_id: buffer.thread_id,
999
+ span_id: buffer.span_id,
1000
+ parent_thread_id: buffer.parent_thread_id,
1001
+ parent_span_id: buffer.parent_span_id,
1002
+ logSchema: buffer._logSchema,
1003
+ _traceRoot,
1004
+ _scopeValues,
1005
+ _opMetadata,
1006
+ _callsiteMetadata,
1007
+ });
1008
+
1009
+ // Link as overflow (not child - same logical span)
1010
+ overflow._parent = buffer._parent;
1011
+ buffer._overflow = overflow;
1012
+
1013
+ return overflow;
1014
+ }