@orangecharts/wrun-chart-host 0.2.0 → 0.4.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 (342) hide show
  1. package/README.md +22 -8
  2. package/dist/constants/common.constants.d.ts +15 -0
  3. package/dist/constants/common.constants.js +23 -0
  4. package/dist/constants/data-source.constants.d.ts +1 -0
  5. package/dist/constants/data-source.constants.js +1 -0
  6. package/dist/constants/economic.constants.d.ts +39 -0
  7. package/dist/constants/economic.constants.js +77 -0
  8. package/dist/constants/etf-covered-coins.constants.d.ts +1 -0
  9. package/dist/constants/etf-covered-coins.constants.js +9 -0
  10. package/dist/constants/feature-flags.constants.d.ts +60 -0
  11. package/dist/constants/feature-flags.constants.js +308 -0
  12. package/dist/constants/feature-limits.constants.d.ts +134 -0
  13. package/dist/constants/feature-limits.constants.js +155 -0
  14. package/dist/constants/marketCapabilities.constants.d.ts +87 -0
  15. package/dist/constants/marketCapabilities.constants.js +213 -0
  16. package/dist/constants/option-venues.constants.d.ts +100 -0
  17. package/dist/constants/option-venues.constants.js +167 -0
  18. package/dist/constants/overlay-data.constants.d.ts +53 -0
  19. package/dist/constants/overlay-data.constants.js +80 -0
  20. package/dist/constants/pane-grid.constants.d.ts +15 -0
  21. package/dist/constants/pane-grid.constants.js +17 -0
  22. package/dist/constants/scriptEditor/builtin-script-snippets.d.ts +133 -0
  23. package/dist/constants/scriptEditor/builtin-script-snippets.js +451 -0
  24. package/dist/constants/venue-presets.constants.d.ts +10 -0
  25. package/dist/constants/venue-presets.constants.js +22 -0
  26. package/dist/enums/chart.enum.d.ts +68 -0
  27. package/dist/enums/chart.enum.js +70 -0
  28. package/dist/helpers/data.helpers.d.ts +2 -0
  29. package/dist/helpers/data.helpers.js +14 -0
  30. package/dist/host-env.d.ts +163 -5
  31. package/dist/host-ports.types.d.ts +431 -0
  32. package/dist/host-ports.types.js +1 -0
  33. package/dist/index.d.ts +138 -0
  34. package/dist/index.js +138 -0
  35. package/dist/indicators/controls/technical-script/technical-script-input-rendering.d.ts +86 -0
  36. package/dist/indicators/controls/technical-script/technical-script-input-rendering.js +179 -0
  37. package/dist/interfaces/asset-venue.interface.d.ts +91 -0
  38. package/dist/interfaces/asset-venue.interface.js +1 -0
  39. package/dist/interfaces/cancel-reason.interface.d.ts +24 -0
  40. package/dist/interfaces/cancel-reason.interface.js +2 -0
  41. package/dist/interfaces/catalog-display.interface.d.ts +3 -0
  42. package/dist/interfaces/catalog-display.interface.js +1 -0
  43. package/dist/interfaces/cme-realtime.interface.d.ts +251 -0
  44. package/dist/interfaces/cme-realtime.interface.js +46 -0
  45. package/dist/interfaces/feed.interface.d.ts +80 -0
  46. package/dist/interfaces/feed.interface.js +7 -0
  47. package/dist/interfaces/market-kind.interface.d.ts +8 -0
  48. package/dist/interfaces/market-kind.interface.js +1 -0
  49. package/dist/interfaces/opra-realtime.interface.d.ts +224 -0
  50. package/dist/interfaces/opra-realtime.interface.js +105 -0
  51. package/dist/interfaces/option-instrument.interface.d.ts +32 -0
  52. package/dist/interfaces/option-instrument.interface.js +1 -0
  53. package/dist/interfaces/options-chain-scope.interface.d.ts +29 -0
  54. package/dist/interfaces/options-chain-scope.interface.js +7 -0
  55. package/dist/interfaces/prediction-market.interface.d.ts +88 -0
  56. package/dist/interfaces/prediction-market.interface.js +8 -0
  57. package/dist/interfaces/wrun-runtime.interface.d.ts +5 -0
  58. package/dist/kscript-data/controls/live-trades-merge.util.d.ts +185 -0
  59. package/dist/kscript-data/controls/live-trades-merge.util.js +357 -0
  60. package/dist/settings-templates/frvp-fields.d.ts +27 -0
  61. package/dist/settings-templates/frvp-fields.js +108 -0
  62. package/dist/settings-templates/index.d.ts +119 -0
  63. package/dist/settings-templates/index.js +442 -0
  64. package/dist/settings-templates/indicator-fields.d.ts +56 -0
  65. package/dist/settings-templates/indicator-fields.js +793 -0
  66. package/dist/settings-templates/tool-rules.d.ts +59 -0
  67. package/dist/settings-templates/tool-rules.js +49 -0
  68. package/dist/settings-templates/tool-tables.generated.d.ts +2 -0
  69. package/dist/settings-templates/tool-tables.generated.js +1668 -0
  70. package/dist/utility/blk-market.util.d.ts +5 -0
  71. package/dist/utility/blk-market.util.js +12 -0
  72. package/dist/utility/chart/chart-load-phase.util.d.ts +32 -0
  73. package/dist/utility/chart/chart-load-phase.util.js +78 -0
  74. package/dist/utility/chart/cme-cache-context.util.d.ts +108 -0
  75. package/dist/utility/chart/cme-cache-context.util.js +166 -0
  76. package/dist/utility/chart/cme-session-bucket.util.d.ts +82 -0
  77. package/dist/utility/chart/cme-session-bucket.util.js +343 -0
  78. package/dist/utility/chart/cme-trade-calendar.util.d.ts +26 -0
  79. package/dist/utility/chart/cme-trade-calendar.util.js +147 -0
  80. package/dist/utility/chart/continuous-contracts.util.d.ts +138 -0
  81. package/dist/utility/chart/continuous-contracts.util.js +374 -0
  82. package/dist/utility/chart/date-labelled-interval.leaf.d.ts +15 -0
  83. package/dist/utility/chart/date-labelled-interval.leaf.js +19 -0
  84. package/dist/utility/chart/feed-interval.util.d.ts +119 -0
  85. package/dist/utility/chart/feed-interval.util.js +321 -0
  86. package/dist/utility/chart/fetch-dedupe-policy.util.d.ts +110 -0
  87. package/dist/utility/chart/fetch-dedupe-policy.util.js +407 -0
  88. package/dist/utility/chart/footprint-band.util.d.ts +76 -0
  89. package/dist/utility/chart/footprint-band.util.js +205 -0
  90. package/dist/utility/chart/footprint-cell-identity.util.d.ts +82 -0
  91. package/dist/utility/chart/footprint-cell-identity.util.js +346 -0
  92. package/dist/utility/chart/formula-calendar.util.d.ts +27 -0
  93. package/dist/utility/chart/formula-calendar.util.js +59 -0
  94. package/dist/utility/chart/history-wall.util.d.ts +67 -0
  95. package/dist/utility/chart/history-wall.util.js +87 -0
  96. package/dist/utility/chart/index-mode.util.d.ts +66 -0
  97. package/dist/utility/chart/index-mode.util.js +360 -0
  98. package/dist/utility/chart/indicator-coverage-planner.util.d.ts +217 -0
  99. package/dist/utility/chart/indicator-coverage-planner.util.js +443 -0
  100. package/dist/utility/chart/indicator-coverage-state.util.d.ts +33 -0
  101. package/dist/utility/chart/indicator-coverage-state.util.js +110 -0
  102. package/dist/utility/chart/indicator-gap-detector.util.d.ts +127 -0
  103. package/dist/utility/chart/indicator-gap-detector.util.js +260 -0
  104. package/dist/utility/chart/indicator-gap-repair.util.d.ts +88 -0
  105. package/dist/utility/chart/indicator-gap-repair.util.js +177 -0
  106. package/dist/utility/chart/indicator-inflight-registry.util.d.ts +26 -0
  107. package/dist/utility/chart/indicator-inflight-registry.util.js +254 -0
  108. package/dist/utility/chart/indicator-run-interval.util.d.ts +130 -0
  109. package/dist/utility/chart/indicator-run-interval.util.js +268 -0
  110. package/dist/utility/chart/interval-registry.util.d.ts +23 -0
  111. package/dist/utility/chart/interval-registry.util.js +83 -0
  112. package/dist/utility/chart/kscript-main-grid-stages.util.d.ts +21 -0
  113. package/dist/utility/chart/kscript-main-grid-stages.util.js +332 -0
  114. package/dist/utility/chart/legacy-exchange.leaf.d.ts +11 -0
  115. package/dist/utility/chart/legacy-exchange.leaf.js +43 -0
  116. package/dist/utility/chart/live-bucket.util.d.ts +66 -0
  117. package/dist/utility/chart/live-bucket.util.js +165 -0
  118. package/dist/utility/chart/market-session-schedule.util.d.ts +91 -0
  119. package/dist/utility/chart/market-session-schedule.util.js +337 -0
  120. package/dist/utility/chart/marketKind.util.d.ts +181 -0
  121. package/dist/utility/chart/marketKind.util.js +371 -0
  122. package/dist/utility/chart/opra-read-access.leaf.d.ts +26 -0
  123. package/dist/utility/chart/opra-read-access.leaf.js +25 -0
  124. package/dist/utility/chart/prediction-identity.util.d.ts +159 -0
  125. package/dist/utility/chart/prediction-identity.util.js +236 -0
  126. package/dist/utility/chart/rth-session.util.d.ts +352 -0
  127. package/dist/utility/chart/rth-session.util.js +926 -0
  128. package/dist/utility/chart/titan-constants.pin.d.ts +43 -0
  129. package/dist/utility/chart/titan-constants.pin.js +50 -0
  130. package/dist/utility/chart/us-equities-session.util.d.ts +127 -0
  131. package/dist/utility/chart/us-equities-session.util.js +251 -0
  132. package/dist/utility/chart/window-geometry.leaf.d.ts +119 -0
  133. package/dist/utility/chart/window-geometry.leaf.js +391 -0
  134. package/dist/utility/chart/window-owner.leaf.d.ts +19 -0
  135. package/dist/utility/chart/window-owner.leaf.js +24 -0
  136. package/dist/utility/chart/wrun-bar-session-facts.util.d.ts +100 -0
  137. package/dist/utility/chart/wrun-bar-session-facts.util.js +354 -0
  138. package/dist/utility/chart/wrun-wire-ref.util.d.ts +23 -0
  139. package/dist/utility/chart/wrun-wire-ref.util.js +16 -0
  140. package/dist/utility/common/safe-math-eval.d.ts +18 -0
  141. package/dist/utility/common/safe-math-eval.js +160 -0
  142. package/dist/utility/common/time.util.d.ts +134 -0
  143. package/dist/utility/common/time.util.js +428 -0
  144. package/dist/utility/common/transformations.util.d.ts +126 -0
  145. package/dist/utility/common/transformations.util.js +711 -0
  146. package/dist/utility/common/visible-timeout.util.d.ts +17 -0
  147. package/dist/utility/common/visible-timeout.util.js +75 -0
  148. package/dist/utility/exchange-normalization.util.d.ts +8 -0
  149. package/dist/utility/exchange-normalization.util.js +32 -0
  150. package/dist/utility/html-escape.leaf.d.ts +11 -0
  151. package/dist/utility/html-escape.leaf.js +20 -0
  152. package/dist/utility/intl-locale.util.d.ts +2 -0
  153. package/dist/utility/intl-locale.util.js +28 -0
  154. package/dist/utility/number-format.util.d.ts +14 -1
  155. package/dist/utility/number-format.util.js +50 -19
  156. package/dist/utility/options/option-symbol.util.d.ts +28 -0
  157. package/dist/utility/options/option-symbol.util.js +176 -0
  158. package/dist/utility/options/option-trade-units.util.d.ts +190 -0
  159. package/dist/utility/options/option-trade-units.util.js +283 -0
  160. package/dist/utility/options/option-venue.util.d.ts +75 -0
  161. package/dist/utility/options/option-venue.util.js +85 -0
  162. package/dist/utility/pool-symbol.leaf.d.ts +34 -0
  163. package/dist/utility/pool-symbol.leaf.js +95 -0
  164. package/dist/utility/scriptEditor/wrun-runtime-diagnostics.leaf.d.ts +109 -0
  165. package/dist/utility/scriptEditor/wrun-runtime-diagnostics.leaf.js +302 -0
  166. package/dist/utility/symbol-selection/contract-expiry.util.d.ts +39 -0
  167. package/dist/utility/symbol-selection/contract-expiry.util.js +111 -0
  168. package/dist/utility/v2/gate-plane.util.d.ts +58 -0
  169. package/dist/utility/v2/gate-plane.util.js +99 -0
  170. package/dist/utility/v2/gate-session-latch.util.d.ts +22 -0
  171. package/dist/utility/v2/gate-session-latch.util.js +34 -0
  172. package/dist/utility/watchlist/import/tradingview-symbols.leaf.d.ts +76 -0
  173. package/dist/utility/watchlist/import/tradingview-symbols.leaf.js +221 -0
  174. package/dist/utility/wrun/sources/host-grid-landing.d.ts +15 -0
  175. package/dist/utility/wrun/sources/host-grid-landing.js +26 -0
  176. package/dist/utility/wrun/sources/lanes/bitfinex_funding.d.ts +27 -0
  177. package/dist/utility/wrun/sources/lanes/bitfinex_funding.js +85 -0
  178. package/dist/utility/wrun/sources/lanes/economic.d.ts +27 -0
  179. package/dist/utility/wrun/sources/lanes/economic.js +79 -0
  180. package/dist/utility/wrun/sources/lanes/etf_holdings.d.ts +35 -0
  181. package/dist/utility/wrun/sources/lanes/etf_holdings.js +165 -0
  182. package/dist/utility/wrun/sources/lanes/etf_premium.d.ts +19 -0
  183. package/dist/utility/wrun/sources/lanes/etf_premium.js +114 -0
  184. package/dist/utility/wrun/sources/lanes/ethena_positions.d.ts +30 -0
  185. package/dist/utility/wrun/sources/lanes/ethena_positions.js +51 -0
  186. package/dist/utility/wrun/sources/lanes/index.d.ts +2 -0
  187. package/dist/utility/wrun/sources/lanes/index.js +32 -0
  188. package/dist/utility/wrun/sources/lanes/long_short_ratio.d.ts +17 -0
  189. package/dist/utility/wrun/sources/lanes/long_short_ratio.js +47 -0
  190. package/dist/utility/wrun/sources/lanes/option_daily.d.ts +58 -0
  191. package/dist/utility/wrun/sources/lanes/option_daily.js +111 -0
  192. package/dist/utility/wrun/sources/lanes/option_stats_10m.d.ts +27 -0
  193. package/dist/utility/wrun/sources/lanes/option_stats_10m.js +21 -0
  194. package/dist/utility/wrun/sources/lanes/options_flow.d.ts +50 -0
  195. package/dist/utility/wrun/sources/lanes/options_flow.js +155 -0
  196. package/dist/utility/wrun/sources/lanes/options_oi.d.ts +29 -0
  197. package/dist/utility/wrun/sources/lanes/options_oi.js +59 -0
  198. package/dist/utility/wrun/sources/lanes/options_volume.d.ts +38 -0
  199. package/dist/utility/wrun/sources/lanes/options_volume.js +63 -0
  200. package/dist/utility/wrun/sources/lanes/series.d.ts +67 -0
  201. package/dist/utility/wrun/sources/lanes/series.js +321 -0
  202. package/dist/utility/wrun/sources/lanes/token_supply.d.ts +30 -0
  203. package/dist/utility/wrun/sources/lanes/token_supply.js +145 -0
  204. package/dist/utility/wrun/sources/lanes/treasury_balance.d.ts +25 -0
  205. package/dist/utility/wrun/sources/lanes/treasury_balance.js +47 -0
  206. package/dist/utility/wrun/sources/lanes/volatility_index.d.ts +21 -0
  207. package/dist/utility/wrun/sources/lanes/volatility_index.js +11 -0
  208. package/dist/utility/wrun/sources/opra-option-lanes.util.d.ts +215 -0
  209. package/dist/utility/wrun/sources/opra-option-lanes.util.js +477 -0
  210. package/dist/utility/wrun/sources/registry.d.ts +21 -0
  211. package/dist/utility/wrun/sources/registry.js +237 -0
  212. package/dist/utility/wrun/sources/row-lane-registry.d.ts +28 -0
  213. package/dist/utility/wrun/sources/row-lane-registry.js +230 -0
  214. package/dist/utility/wrun/sources/row-lanes/index.d.ts +2 -0
  215. package/dist/utility/wrun/sources/row-lanes/index.js +12 -0
  216. package/dist/utility/wrun/sources/row-lanes/option_contract_flow.d.ts +2 -0
  217. package/dist/utility/wrun/sources/row-lanes/option_contract_flow.js +38 -0
  218. package/dist/utility/wrun/sources/row-lanes/option_contract_state.d.ts +55 -0
  219. package/dist/utility/wrun/sources/row-lanes/option_contract_state.js +126 -0
  220. package/dist/utility/wrun/sources/row-lanes/option_prints.d.ts +4 -0
  221. package/dist/utility/wrun/sources/row-lanes/option_prints.js +191 -0
  222. package/dist/utility/wrun/sources/row-lanes/option_strikes.d.ts +62 -0
  223. package/dist/utility/wrun/sources/row-lanes/option_strikes.js +147 -0
  224. package/dist/utility/wrun/sources/row-lanes/option_surface.d.ts +70 -0
  225. package/dist/utility/wrun/sources/row-lanes/option_surface.js +144 -0
  226. package/dist/utility/wrun/sources/wrun-poll-lane.types.d.ts +309 -0
  227. package/dist/utility/wrun/sources/wrun-poll-lane.types.js +26 -0
  228. package/dist/utility/wrun/sources/wrun-poll-lanes.util.d.ts +350 -0
  229. package/dist/utility/wrun/sources/wrun-poll-lanes.util.js +576 -0
  230. package/dist/utility/wrun/sources/wrun-row-lane.types.d.ts +275 -0
  231. package/dist/utility/wrun/sources/wrun-row-lane.types.js +16 -0
  232. package/dist/utility/wrun/sources/wrun-row-lanes.util.d.ts +207 -0
  233. package/dist/utility/wrun/sources/wrun-row-lanes.util.js +450 -0
  234. package/dist/utility/wrun/wrun-browser-sources.leaf.d.ts +28 -0
  235. package/dist/utility/wrun/wrun-browser-sources.leaf.js +66 -0
  236. package/dist/utility/wrun/wrun-cloud-record.util.d.ts +65 -0
  237. package/dist/utility/wrun/wrun-cloud-record.util.js +121 -0
  238. package/dist/utility/wrun/wrun-daily-lane.util.d.ts +95 -0
  239. package/dist/utility/wrun/wrun-daily-lane.util.js +297 -0
  240. package/dist/utility/wrun/wrun-deadline.util.d.ts +52 -0
  241. package/dist/utility/wrun/wrun-deadline.util.js +82 -0
  242. package/dist/utility/wrun/wrun-etf-flow-lane.util.d.ts +94 -0
  243. package/dist/utility/wrun/wrun-etf-flow-lane.util.js +224 -0
  244. package/dist/utility/wrun/wrun-etf-funds.util.d.ts +82 -0
  245. package/dist/utility/wrun/wrun-etf-funds.util.js +242 -0
  246. package/dist/utility/wrun/wrun-frames.d.ts +20 -1
  247. package/dist/utility/wrun/wrun-frames.js +103 -49
  248. package/dist/utility/wrun/wrun-hosted-wire-caps.d.ts +2 -2
  249. package/dist/utility/wrun/wrun-hosted-wire-caps.js +27 -0
  250. package/dist/utility/wrun/wrun-lane-refusal.leaf.d.ts +22 -0
  251. package/dist/utility/wrun/wrun-lane-refusal.leaf.js +32 -0
  252. package/dist/utility/wrun/wrun-lane-warmup.util.d.ts +18 -0
  253. package/dist/utility/wrun/wrun-lane-warmup.util.js +112 -0
  254. package/dist/utility/wrun/wrun-leg-close.util.d.ts +57 -0
  255. package/dist/utility/wrun/wrun-leg-close.util.js +187 -0
  256. package/dist/utility/wrun/wrun-leg-fetch.util.d.ts +144 -0
  257. package/dist/utility/wrun/wrun-leg-fetch.util.js +212 -0
  258. package/dist/utility/wrun/wrun-leg-stream-classes.d.ts +73 -0
  259. package/dist/utility/wrun/wrun-leg-stream-classes.js +86 -0
  260. package/dist/utility/wrun/wrun-levels-child.util.d.ts +101 -6
  261. package/dist/utility/wrun/wrun-levels-child.util.js +335 -157
  262. package/dist/utility/wrun/wrun-memory-clamp.util.d.ts +33 -0
  263. package/dist/utility/wrun/wrun-memory-clamp.util.js +146 -0
  264. package/dist/utility/wrun/wrun-odds-template-market.util.d.ts +51 -0
  265. package/dist/utility/wrun/wrun-odds-template-market.util.js +251 -0
  266. package/dist/utility/wrun/wrun-opra-flow.leaf.d.ts +65 -0
  267. package/dist/utility/wrun/wrun-opra-flow.leaf.js +94 -0
  268. package/dist/utility/wrun/wrun-opra-lane.util.d.ts +47 -0
  269. package/dist/utility/wrun/wrun-opra-lane.util.js +58 -0
  270. package/dist/utility/wrun/wrun-opra-stats.leaf.d.ts +49 -0
  271. package/dist/utility/wrun/wrun-opra-stats.leaf.js +115 -0
  272. package/dist/utility/wrun/wrun-opra-underlying.util.d.ts +12 -0
  273. package/dist/utility/wrun/wrun-opra-underlying.util.js +1 -0
  274. package/dist/utility/wrun/wrun-option-contract-flow.util.d.ts +142 -0
  275. package/dist/utility/wrun/wrun-option-contract-flow.util.js +451 -0
  276. package/dist/utility/wrun/wrun-option-knobs.util.d.ts +43 -0
  277. package/dist/utility/wrun/wrun-option-knobs.util.js +81 -0
  278. package/dist/utility/wrun/wrun-option-prints.util.d.ts +144 -0
  279. package/dist/utility/wrun/wrun-option-prints.util.js +356 -0
  280. package/dist/utility/wrun/wrun-option-summary-lane.util.d.ts +102 -0
  281. package/dist/utility/wrun/wrun-option-summary-lane.util.js +143 -0
  282. package/dist/utility/wrun/wrun-options-chain.util.d.ts +308 -0
  283. package/dist/utility/wrun/wrun-options-chain.util.js +502 -0
  284. package/dist/utility/wrun/wrun-overlay-identity.util.d.ts +9 -4
  285. package/dist/utility/wrun/wrun-overlay-identity.util.js +16 -6
  286. package/dist/utility/wrun/wrun-panel-mount.lazy.d.ts +10 -0
  287. package/dist/utility/wrun/wrun-panel-mount.lazy.js +68 -0
  288. package/dist/utility/wrun/wrun-panel-mount.util.d.ts +377 -0
  289. package/dist/utility/wrun/wrun-panel-mount.util.js +2251 -0
  290. package/dist/utility/wrun/wrun-param-map.util.d.ts +298 -0
  291. package/dist/utility/wrun/wrun-param-map.util.js +581 -0
  292. package/dist/utility/wrun/wrun-pause-dim.util.d.ts +52 -0
  293. package/dist/utility/wrun/wrun-pause-dim.util.js +153 -0
  294. package/dist/utility/wrun/wrun-pin-spelling.util.d.ts +149 -0
  295. package/dist/utility/wrun/wrun-pin-spelling.util.js +523 -0
  296. package/dist/utility/wrun/wrun-pin-venues.util.d.ts +116 -0
  297. package/dist/utility/wrun/wrun-pin-venues.util.js +320 -0
  298. package/dist/utility/wrun/wrun-pinned-interval.util.d.ts +20 -0
  299. package/dist/utility/wrun/wrun-pinned-interval.util.js +47 -0
  300. package/dist/utility/wrun/wrun-plot-gate.types.d.ts +23 -0
  301. package/dist/utility/wrun/wrun-plot-gate.types.js +7 -0
  302. package/dist/utility/wrun/wrun-present-bits.util.d.ts +37 -0
  303. package/dist/utility/wrun/wrun-present-bits.util.js +73 -0
  304. package/dist/utility/wrun/wrun-protocol-pending.util.d.ts +33 -0
  305. package/dist/utility/wrun/wrun-protocol-pending.util.js +57 -0
  306. package/dist/utility/wrun/wrun-refusal.util.d.ts +41 -1
  307. package/dist/utility/wrun/wrun-refusal.util.js +69 -2
  308. package/dist/utility/wrun/wrun-registry-errors.util.d.ts +1 -14
  309. package/dist/utility/wrun/wrun-registry-errors.util.js +0 -16
  310. package/dist/utility/wrun/wrun-row-source.d.ts +158 -0
  311. package/dist/utility/wrun/wrun-row-source.js +75 -0
  312. package/dist/utility/wrun/wrun-run-health.util.d.ts +155 -0
  313. package/dist/utility/wrun/wrun-run-health.util.js +273 -0
  314. package/dist/utility/wrun/wrun-run-interval-projection.types.d.ts +14 -0
  315. package/dist/utility/wrun/wrun-run-interval-projection.types.js +1 -0
  316. package/dist/utility/wrun/wrun-socket-row-source.d.ts +439 -0
  317. package/dist/utility/wrun/wrun-socket-row-source.js +1689 -0
  318. package/dist/utility/wrun/wrun-socket-session-losses.util.d.ts +18 -0
  319. package/dist/utility/wrun/wrun-socket-session-losses.util.js +25 -0
  320. package/dist/utility/wrun/wrun-source-rows.util.d.ts +911 -0
  321. package/dist/utility/wrun/wrun-source-rows.util.js +2597 -0
  322. package/dist/utility/wrun/wrun-tape-source.util.d.ts +82 -0
  323. package/dist/utility/wrun/wrun-tape-source.util.js +151 -0
  324. package/dist/utility/wrun/wrun-tier-grid.util.d.ts +137 -0
  325. package/dist/utility/wrun/wrun-tier-grid.util.js +279 -0
  326. package/dist/utility/wrun/wrun-trades-lane.util.d.ts +52 -0
  327. package/dist/utility/wrun/wrun-trades-lane.util.js +58 -0
  328. package/dist/utility/wrun/wrun-volume-profile-lane.util.d.ts +41 -0
  329. package/dist/utility/wrun/wrun-volume-profile-lane.util.js +66 -0
  330. package/dist/utility/wrun-display-name.util.d.ts +33 -0
  331. package/dist/utility/wrun-display-name.util.js +64 -0
  332. package/dist/workers/constants/kscript-transport-codes.d.ts +15 -0
  333. package/dist/workers/constants/kscript-transport-codes.js +15 -0
  334. package/dist/workers/constants/socket-slots.d.ts +574 -0
  335. package/dist/workers/constants/socket-slots.js +539 -0
  336. package/dist/workers/utils/number.worker.util.d.ts +5 -0
  337. package/dist/workers/utils/number.worker.util.js +10 -0
  338. package/dist/workers/utils/time.worker.util.d.ts +12 -0
  339. package/dist/workers/utils/time.worker.util.js +24 -0
  340. package/dist/workers/utils/volume-profile.worker.util.d.ts +200 -0
  341. package/dist/workers/utils/volume-profile.worker.util.js +631 -0
  342. package/package.json +10 -4
@@ -0,0 +1,354 @@
1
+ /**
2
+ * The market facts a wrun indicator reads per bar (WRUN-SESSIONS contract
3
+ * C3): `time.trade_date` and `time.session`. Both are facts of the bar's
4
+ * market (exchange, symbol), its interval and its open time ONLY, never of
5
+ * the chart's RTH / extended mode, and every hour comes from the chart's own
6
+ * session definitions, the RTH window from the kScript engine's table (see
7
+ * NYMEX below).
8
+ *
9
+ * Calendar bars (an interval of one day or longer: 1D, 3D, 1W, 1M) read the
10
+ * UTC date of their open stamp and 1 on every market: the chart stamps them
11
+ * at 00:00 UTC of their trade date, an instant the intraday rules below would
12
+ * read as the previous New York evening (amendment 10-01). Intraday bars:
13
+ *
14
+ * - trade date, epoch SECONDS at 00:00 UTC of the bar's exchange trade date:
15
+ * - CME family (`isCmeGoverned`: CME, CBOT, NYMEX, COMEX and the GLOBEX
16
+ * alias): the chart's CME trade date, `cmeSessionBucketMs(t, 1d)`, so the
17
+ * 17:00 Chicago open belongs to the NEXT date;
18
+ * - US equities (`getMarketKind` 'tradfi'): the New York calendar date;
19
+ * - everything else (crypto, forex, HIP-3, Polymarket, ...): the UTC date.
20
+ * - session, 1 regular, 2 pre-market, 3 after-hours, 0 closed:
21
+ * - US equities: `resolveUsEquitySessionPhase`;
22
+ * - CME family: 0 while the weekly schedule says closed
23
+ * (`getGovernedFuturesScheduleStatusForSymbol`, which carries the 24/7
24
+ * roots); else 1 inside the engine's RTH window
25
+ * (`engineRthWindowForSymbol`, 09:30 to 16:15 New York on every mapped
26
+ * root; a root without a window is regular all session), 2 before its
27
+ * trade date's RTH window and 3 after it;
28
+ * - everything else: 1.
29
+ *
30
+ * The golden vectors every other host (kscript-alerts, kscript-backend, om)
31
+ * tests against are generated from this module:
32
+ * src/fixtures/wrun-session-facts/golden.json.
33
+ *
34
+ * NYMEX: the chart judges RTH there on the energy pit session, 09:00 to 14:30
35
+ * New York (rth-session.util `rthWindowForExchange`, the gateway's REGULAR
36
+ * window), but wrun and kScript session facts on NYMEX follow the kScript
37
+ * engine's table, 09:30 to 16:15, until a coordinated change moves the
38
+ * engine, the golden and the cloud lanes together. Moving this browser lane
39
+ * alone would split the browser and cloud runs of the same indicator.
40
+ *
41
+ * Cost: the definitions run at microseconds a call (the equities phase reads
42
+ * dayjs timezone parts, about 30 us), so a 50k-bar run cannot afford one per
43
+ * bar. Each is memoized on the grain it is constant on, always evaluated at
44
+ * the START of that grain so a result never depends on call order:
45
+ * - the venue-zone calendar day (Intl parts, host-independent: the dayjs
46
+ * drift caveat in rth-session.util never applies), once per local day;
47
+ * - the CME schedule per Chicago (weekday, hour): its boundaries are whole
48
+ * Chicago hours (16:00 and 17:00, the Saturday 02:00 to 04:00 window);
49
+ * - the equities phase per New York (weekday, half hour): its boundaries are
50
+ * 04:00, 09:30, 16:00 and 20:00;
51
+ * - the CME RTH window per (window, trade date).
52
+ * Both zones sit a whole number of hours off UTC, so a reader also keeps its
53
+ * last answer per UTC hour (CME: the trade date and the schedule bit) or half
54
+ * hour (equities), and a sorted walk pays the lookups once per grain, not per
55
+ * bar. The spec walks every minute of both 2026 DST weeks against the
56
+ * definitions themselves, so a boundary moved off those grains fails there,
57
+ * not in the field, and measures 50k one-minute bars from a cold module.
58
+ *
59
+ * Worker-safe: imports only the chart's session utils (no Vue, store, i18n).
60
+ */
61
+ import { Intervals } from '../../constants/interval.constants.js';
62
+ import { cmeSessionBucketMs } from './cme-session-bucket.util.js';
63
+ import { isCmeGoverned } from './cme-realtime.util.js';
64
+ import { getMarketKind } from './marketKind.util.js';
65
+ import { getGovernedFuturesScheduleStatusForSymbol, GOVERNED_FUTURES_TIME_ZONE, isCmeAlwaysOnSymbol, } from './market-session-schedule.util.js';
66
+ import { engineRthWindowForSymbol, sessionWindowsInRange, } from './rth-session.util.js';
67
+ import { resolveUsEquitySessionPhase, US_EQUITIES_TIME_ZONE, } from './us-equities-session.util.js';
68
+ const MINUTE_MS = 60_000;
69
+ const HALF_HOUR_MS = 30 * MINUTE_MS;
70
+ const HOUR_MS = 60 * MINUTE_MS;
71
+ const DAY_MS = 24 * HOUR_MS;
72
+ const ONE_DAY_INTERVAL_MS = Intervals.map_unit['1d'];
73
+ /** The `time.session` codes (contract C3). */
74
+ export const WRUN_BAR_SESSION = Object.freeze({
75
+ closed: 0,
76
+ regular: 1,
77
+ premarket: 2,
78
+ afterHours: 3,
79
+ });
80
+ export function wrunBarFactsMarketOf(exchange) {
81
+ if (isCmeGoverned(exchange))
82
+ return 'cme';
83
+ return getMarketKind(exchange) === 'tradfi' ? 'us_equities' : 'utc';
84
+ }
85
+ /**
86
+ * The venue of chart metadata (or a live context's market) at the series'
87
+ * bar interval: undefined unless exchange and symbol are non-empty strings and
88
+ * the interval is a positive number.
89
+ */
90
+ export function wrunBarVenueOf(market, intervalMs) {
91
+ if (!market || typeof market !== 'object')
92
+ return undefined;
93
+ const { exchange, symbol } = market;
94
+ if (typeof exchange !== 'string' || exchange.length === 0)
95
+ return undefined;
96
+ if (typeof symbol !== 'string' || symbol.length === 0)
97
+ return undefined;
98
+ if (!Number.isFinite(intervalMs) || intervalMs <= 0)
99
+ return undefined;
100
+ return { exchange, symbol, intervalMs };
101
+ }
102
+ const NO_FACTS = Object.freeze({ tradeDateSec: Number.NaN, session: Number.NaN });
103
+ /** Every session code, indexable by its own value. */
104
+ const SESSION_CODES = [
105
+ WRUN_BAR_SESSION.closed,
106
+ WRUN_BAR_SESSION.regular,
107
+ WRUN_BAR_SESSION.premarket,
108
+ WRUN_BAR_SESSION.afterHours,
109
+ ];
110
+ const floorTo = (value, step) => value - (((value % step) + step) % step);
111
+ const ZONE_DAYS_MAX = 4096;
112
+ function zoneClock(zone) {
113
+ return {
114
+ formatter: new Intl.DateTimeFormat('en-US', {
115
+ timeZone: zone,
116
+ hourCycle: 'h23',
117
+ year: 'numeric',
118
+ month: 'numeric',
119
+ day: 'numeric',
120
+ hour: 'numeric',
121
+ minute: 'numeric',
122
+ second: 'numeric',
123
+ }),
124
+ days: new Map(),
125
+ last: null,
126
+ };
127
+ }
128
+ const newYorkClock = zoneClock(US_EQUITIES_TIME_ZONE);
129
+ const chicagoClock = zoneClock(GOVERNED_FUTURES_TIME_ZONE);
130
+ /** The wall clock `tMs` reads in the zone, as if it were UTC (whole seconds). */
131
+ function wallAsUtcMs(clock, tMs) {
132
+ let year = 1970;
133
+ let month = 1;
134
+ let day = 1;
135
+ let hour = 0;
136
+ let minute = 0;
137
+ let second = 0;
138
+ for (const part of clock.formatter.formatToParts(tMs)) {
139
+ switch (part.type) {
140
+ case 'year':
141
+ year = Number(part.value);
142
+ break;
143
+ case 'month':
144
+ month = Number(part.value);
145
+ break;
146
+ case 'day':
147
+ day = Number(part.value);
148
+ break;
149
+ case 'hour':
150
+ // Some ICU builds render midnight as 24 even under h23.
151
+ hour = Number(part.value) % 24;
152
+ break;
153
+ case 'minute':
154
+ minute = Number(part.value);
155
+ break;
156
+ case 'second':
157
+ second = Number(part.value);
158
+ break;
159
+ default:
160
+ break;
161
+ }
162
+ }
163
+ return Date.UTC(year, month - 1, day, hour, minute, second);
164
+ }
165
+ function offsetAtMs(clock, tMs) {
166
+ return wallAsUtcMs(clock, tMs) - Math.floor(tMs / 1000) * 1000;
167
+ }
168
+ /** The instant whose wall clock reads `wallMs` (as if UTC): two-pass, exact off the transition hour. */
169
+ function wallToUtcMs(clock, wallMs) {
170
+ const first = wallMs - offsetAtMs(clock, wallMs);
171
+ return wallMs - offsetAtMs(clock, first);
172
+ }
173
+ function zoneDayOf(clock, tMs) {
174
+ const last = clock.last;
175
+ if (last !== null && tMs >= last.startMs && tMs < last.endMs)
176
+ return last;
177
+ const wall = wallAsUtcMs(clock, tMs);
178
+ const dateMs = floorTo(wall, DAY_MS);
179
+ let day = clock.days.get(dateMs);
180
+ if (day === undefined) {
181
+ // Local midnight is never inside a US transition hour (those run at 02:00).
182
+ const startMs = wallToUtcMs(clock, dateMs);
183
+ const endMs = wallToUtcMs(clock, dateMs + DAY_MS);
184
+ const startOffsetMs = dateMs - startMs;
185
+ day = {
186
+ startMs,
187
+ endMs,
188
+ dateMs,
189
+ weekday: new Date(dateMs).getUTCDay(),
190
+ offsetMs: offsetAtMs(clock, endMs - 1) === startOffsetMs ? startOffsetMs : Number.NaN,
191
+ };
192
+ if (clock.days.size >= ZONE_DAYS_MAX)
193
+ clock.days.clear();
194
+ clock.days.set(dateMs, day);
195
+ }
196
+ clock.last = day;
197
+ return day;
198
+ }
199
+ /** Wall-clock ms since local midnight of `day` (the day holding `tMs`). */
200
+ function wallSinceMidnightMs(clock, day, tMs) {
201
+ const offsetMs = Number.isNaN(day.offsetMs) ? offsetAtMs(clock, tMs) : day.offsetMs;
202
+ return tMs + offsetMs - day.dateMs;
203
+ }
204
+ // ─── US equities ─────────────────────────────────────────────────────────────
205
+ const PHASE_CODE = Object.freeze({
206
+ regular: WRUN_BAR_SESSION.regular,
207
+ pre: WRUN_BAR_SESSION.premarket,
208
+ after: WRUN_BAR_SESSION.afterHours,
209
+ closed: WRUN_BAR_SESSION.closed,
210
+ });
211
+ const HALF_HOURS_PER_DAY = 48;
212
+ /** `resolveUsEquitySessionPhase` per New York (weekday, half hour); -1 = not read yet. */
213
+ const equityPhaseByWeekHalfHour = new Int8Array(7 * HALF_HOURS_PER_DAY).fill(-1);
214
+ /** The phase code `resolveUsEquitySessionPhase` gives the bar's New York (weekday, half hour). */
215
+ function usEquitySession(day, tMs) {
216
+ const sinceMidnightMs = wallSinceMidnightMs(newYorkClock, day, tMs);
217
+ const key = day.weekday * HALF_HOURS_PER_DAY + Math.floor(sinceMidnightMs / HALF_HOUR_MS);
218
+ let session = equityPhaseByWeekHalfHour[key];
219
+ if (session === -1) {
220
+ session = PHASE_CODE[resolveUsEquitySessionPhase(tMs - (sinceMidnightMs % HALF_HOUR_MS))];
221
+ equityPhaseByWeekHalfHour[key] = session;
222
+ }
223
+ return session;
224
+ }
225
+ function usEquityFactsResolver() {
226
+ // New York's offset is whole hours, so one UTC half hour is one New York (date, weekday, half
227
+ // hour): one answer for every bar inside it.
228
+ let slotMs = Number.NaN;
229
+ let answer = NO_FACTS;
230
+ return (tMs) => {
231
+ const barSlotMs = floorTo(tMs, HALF_HOUR_MS);
232
+ if (barSlotMs !== slotMs) {
233
+ const day = zoneDayOf(newYorkClock, tMs);
234
+ answer = Object.freeze({ tradeDateSec: day.dateMs / 1000, session: usEquitySession(day, tMs) });
235
+ slotMs = barSlotMs;
236
+ }
237
+ return answer;
238
+ };
239
+ }
240
+ // ─── CME family ──────────────────────────────────────────────────────────────
241
+ const HOURS_PER_DAY = 24;
242
+ /** The schedule's open bit per Chicago (weekday, hour), one table per schedule; -1 = not read yet. */
243
+ const globexOpenByWeekHour = new Int8Array(7 * HOURS_PER_DAY).fill(-1);
244
+ const alwaysOnOpenByWeekHour = new Int8Array(7 * HOURS_PER_DAY).fill(-1);
245
+ function cmeScheduleOpen(symbol, alwaysOn, tMs) {
246
+ const day = zoneDayOf(chicagoClock, tMs);
247
+ const sinceMidnightMs = wallSinceMidnightMs(chicagoClock, day, tMs);
248
+ const key = day.weekday * HOURS_PER_DAY + Math.floor(sinceMidnightMs / HOUR_MS);
249
+ const table = alwaysOn ? alwaysOnOpenByWeekHour : globexOpenByWeekHour;
250
+ let open = table[key];
251
+ if (open === -1) {
252
+ open = getGovernedFuturesScheduleStatusForSymbol(symbol, tMs - (sinceMidnightMs % HOUR_MS)).open ? 1 : 0;
253
+ table[key] = open;
254
+ }
255
+ return open === 1;
256
+ }
257
+ const TRADE_DATE_WINDOWS_MAX = 4096;
258
+ /** Each RTH window object's window per trade date (null: no window opens on that date). */
259
+ const rthWindowByTradeDate = new WeakMap();
260
+ /**
261
+ * The RTH window of the CME trade date `tradeDateMs` (00:00Z label): the
262
+ * calendar-day window whose open carries that trade date. The 09:30 to 16:15
263
+ * New York window sits inside one trade date (the 17:00 Chicago roll is
264
+ * after its close), so exactly one window qualifies.
265
+ */
266
+ function rthWindowOfTradeDate(window, tradeDateMs) {
267
+ let byTradeDate = rthWindowByTradeDate.get(window);
268
+ if (byTradeDate === undefined) {
269
+ byTradeDate = new Map();
270
+ rthWindowByTradeDate.set(window, byTradeDate);
271
+ }
272
+ const cached = byTradeDate.get(tradeDateMs);
273
+ if (cached !== undefined)
274
+ return cached;
275
+ const found = sessionWindowsInRange(window, tradeDateMs - DAY_MS, tradeDateMs + 2 * DAY_MS).find((candidate) => cmeSessionBucketMs(candidate.openMs, ONE_DAY_INTERVAL_MS) === tradeDateMs) ?? null;
276
+ if (byTradeDate.size >= TRADE_DATE_WINDOWS_MAX)
277
+ byTradeDate.clear();
278
+ byTradeDate.set(tradeDateMs, found);
279
+ return found;
280
+ }
281
+ function cmeFactsResolver(symbol) {
282
+ const alwaysOn = isCmeAlwaysOnSymbol(symbol);
283
+ const window = engineRthWindowForSymbol(symbol);
284
+ // Per UTC hour: `cmeSessionBucketMs` reads the 1h source row holding the bar, so the trade date
285
+ // is one per UTC hour, and the schedule's grain (a Chicago hour) is one UTC hour too.
286
+ let hourMs = Number.NaN;
287
+ let open = false;
288
+ let tradeDateMs = Number.NaN;
289
+ // Per trade date: its RTH window and one read-only answer per session code.
290
+ let rthTradeDateMs = Number.NaN;
291
+ let rth = null;
292
+ let answers = [];
293
+ return (tMs) => {
294
+ const barHourMs = floorTo(tMs, HOUR_MS);
295
+ if (barHourMs !== hourMs) {
296
+ hourMs = barHourMs;
297
+ open = cmeScheduleOpen(symbol, alwaysOn, tMs);
298
+ const barTradeDateMs = cmeSessionBucketMs(tMs, ONE_DAY_INTERVAL_MS);
299
+ if (barTradeDateMs !== tradeDateMs) {
300
+ tradeDateMs = barTradeDateMs;
301
+ answers = SESSION_CODES.map((session) => Object.freeze({ tradeDateSec: barTradeDateMs / 1000, session }));
302
+ }
303
+ }
304
+ if (!open)
305
+ return answers[WRUN_BAR_SESSION.closed];
306
+ // `isWithinRthWindow` fails open for a root without a window: regular all session.
307
+ if (window === null)
308
+ return answers[WRUN_BAR_SESSION.regular];
309
+ if (rthTradeDateMs !== tradeDateMs) {
310
+ rth = rthWindowOfTradeDate(window, tradeDateMs);
311
+ rthTradeDateMs = tradeDateMs;
312
+ }
313
+ // No window on the trade date is unreachable with the uniform window; read it like a root without one.
314
+ if (rth === null || (tMs >= rth.openMs && tMs < rth.closeMs))
315
+ return answers[WRUN_BAR_SESSION.regular];
316
+ return answers[tMs < rth.openMs ? WRUN_BAR_SESSION.premarket : WRUN_BAR_SESSION.afterHours];
317
+ };
318
+ }
319
+ // ─── Everything else, and every calendar bar ─────────────────────────────────
320
+ function utcFactsResolver() {
321
+ let dayMs = Number.NaN;
322
+ let answer = NO_FACTS;
323
+ return (tMs) => {
324
+ const barDayMs = floorTo(tMs, DAY_MS);
325
+ if (barDayMs !== dayMs) {
326
+ answer = Object.freeze({ tradeDateSec: barDayMs / 1000, session: WRUN_BAR_SESSION.regular });
327
+ dayMs = barDayMs;
328
+ }
329
+ return answer;
330
+ };
331
+ }
332
+ // ─── The resolver ────────────────────────────────────────────────────────────
333
+ /**
334
+ * The facts reader for one series: its rule is decided once (a calendar bar
335
+ * reads its stamp's UTC date on every market, an intraday bar its market's
336
+ * rule), then every call reads one bar open time (UTC ms). The row builders
337
+ * bind one per build and call it per row. The answer objects are shared and
338
+ * frozen.
339
+ */
340
+ export function wrunBarSessionFactsResolver(venue) {
341
+ const market = wrunBarFactsMarketOf(venue.exchange);
342
+ const facts = venue.intervalMs >= ONE_DAY_INTERVAL_MS
343
+ ? utcFactsResolver()
344
+ : market === 'cme'
345
+ ? cmeFactsResolver(venue.symbol)
346
+ : market === 'us_equities'
347
+ ? usEquityFactsResolver()
348
+ : utcFactsResolver();
349
+ return (barOpenMs) => (Number.isFinite(barOpenMs) ? facts(barOpenMs) : NO_FACTS);
350
+ }
351
+ /** One bar's facts (a one-off read; hot loops bind `wrunBarSessionFactsResolver` once). */
352
+ export function wrunBarSessionFacts(venue, barOpenMs) {
353
+ return wrunBarSessionFactsResolver(venue)(barOpenMs);
354
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * The ONE reader from persisted overlay settings to the WRUN wire tuple
3
+ * `{package, version}`.
4
+ *
5
+ * Three wire hops consume this exact narrowing — the capability MINT (primary
6
+ * ensure + the once-only FORBIDDEN heal in data-fetching) and the backend
7
+ * createScript payload (kscript-ws worker) — and the mint's tuple MUST equal
8
+ * the payload's tuple byte-for-byte: the backend refuses a createScript whose
9
+ * `wrun` differs from the capability's signed claim. Open-coding the guard at
10
+ * each site is how the two sides drift (the settingsToSave/initializeOverlay
11
+ * incident class), so every consumer calls this instead.
12
+ *
13
+ * Returns null for anything malformed (absent ref, non-string fields, empty
14
+ * or whitespace-only values, over-cap lengths — the gateway trims and caps
15
+ * the same way, so a value this returns would otherwise 400 as an UNTYPED
16
+ * error and be misread as a transient outage). Callers treat null as "not a
17
+ * wrun overlay" / deterministic invalid, never as retryable.
18
+ */
19
+ export interface WrunWireRef {
20
+ package: string;
21
+ version: string;
22
+ }
23
+ export declare function readWrunWireRef(settings: unknown): WrunWireRef | null;
@@ -0,0 +1,16 @@
1
+ const WRUN_PACKAGE_MAX = 128;
2
+ const WRUN_VERSION_MAX = 64;
3
+ export function readWrunWireRef(settings) {
4
+ const ref = settings?.wrunRef;
5
+ if (!ref || typeof ref !== 'object')
6
+ return null;
7
+ if (typeof ref.package !== 'string' || typeof ref.version !== 'string')
8
+ return null;
9
+ const pkg = ref.package.trim();
10
+ const version = ref.version.trim();
11
+ if (!pkg || !version)
12
+ return null;
13
+ if (pkg.length > WRUN_PACKAGE_MAX || version.length > WRUN_VERSION_MAX)
14
+ return null;
15
+ return { package: pkg, version };
16
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Safe arithmetic expression evaluator using a recursive descent parser.
3
+ * Only supports: numbers (including scientific notation), +, -, *, /, parentheses.
4
+ * Returns NaN for invalid expressions. Returns Infinity for division by zero.
5
+ *
6
+ * Grammar:
7
+ * expression ::= term (('+' | '-') term)*
8
+ * term ::= factor (('*' | '/') factor)*
9
+ * factor ::= ('+' | '-')? atom
10
+ * atom ::= NUMBER | '(' expression ')'
11
+ */
12
+ /**
13
+ * Safely evaluate a mathematical expression string containing only
14
+ * numbers, operators (+, -, *, /), parentheses, and whitespace.
15
+ * Returns NaN for malformed or empty expressions.
16
+ * Returns Infinity/-Infinity for division by zero (matches JS behavior).
17
+ */
18
+ export declare function safeMathEval(expression: string): number;
@@ -0,0 +1,160 @@
1
+ /**
2
+ * Safe arithmetic expression evaluator using a recursive descent parser.
3
+ * Only supports: numbers (including scientific notation), +, -, *, /, parentheses.
4
+ * Returns NaN for invalid expressions. Returns Infinity for division by zero.
5
+ *
6
+ * Grammar:
7
+ * expression ::= term (('+' | '-') term)*
8
+ * term ::= factor (('*' | '/') factor)*
9
+ * factor ::= ('+' | '-')? atom
10
+ * atom ::= NUMBER | '(' expression ')'
11
+ */
12
+ function tokenize(expr) {
13
+ const tokens = [];
14
+ let i = 0;
15
+ while (i < expr.length) {
16
+ const ch = expr[i];
17
+ // Skip whitespace
18
+ if (ch === ' ' || ch === '\t') {
19
+ i++;
20
+ continue;
21
+ }
22
+ // Operators and parentheses
23
+ if (ch === '+' || ch === '-' || ch === '*' || ch === '/' || ch === '(' || ch === ')') {
24
+ tokens.push({ type: 'op', value: ch });
25
+ i++;
26
+ continue;
27
+ }
28
+ // Numbers: digits, decimal point, scientific notation
29
+ if ((ch >= '0' && ch <= '9') || ch === '.') {
30
+ let num = '';
31
+ while (i < expr.length && ((expr[i] >= '0' && expr[i] <= '9') || expr[i] === '.')) {
32
+ num += expr[i];
33
+ i++;
34
+ }
35
+ // Scientific notation (e.g., 1e-7, 2.5E+3)
36
+ if (i < expr.length && (expr[i] === 'e' || expr[i] === 'E')) {
37
+ num += expr[i];
38
+ i++;
39
+ if (i < expr.length && (expr[i] === '+' || expr[i] === '-')) {
40
+ num += expr[i];
41
+ i++;
42
+ }
43
+ while (i < expr.length && expr[i] >= '0' && expr[i] <= '9') {
44
+ num += expr[i];
45
+ i++;
46
+ }
47
+ }
48
+ const value = Number(num);
49
+ if (isNaN(value))
50
+ return [{ type: 'eof' }];
51
+ tokens.push({ type: 'number', value });
52
+ continue;
53
+ }
54
+ // Any other character is invalid
55
+ return [{ type: 'eof' }];
56
+ }
57
+ tokens.push({ type: 'eof' });
58
+ return tokens;
59
+ }
60
+ class Parser {
61
+ tokens;
62
+ pos = 0;
63
+ failed = false;
64
+ constructor(tokens) {
65
+ this.tokens = tokens;
66
+ }
67
+ peek() {
68
+ return this.tokens[this.pos] ?? { type: 'eof' };
69
+ }
70
+ consume() {
71
+ return this.tokens[this.pos++] ?? { type: 'eof' };
72
+ }
73
+ // expression ::= term (('+' | '-') term)*
74
+ parseExpression() {
75
+ let result = this.parseTerm();
76
+ while (!this.failed) {
77
+ const tok = this.peek();
78
+ if (tok.type === 'op' && (tok.value === '+' || tok.value === '-')) {
79
+ this.consume();
80
+ const right = this.parseTerm();
81
+ result = tok.value === '+' ? result + right : result - right;
82
+ }
83
+ else {
84
+ break;
85
+ }
86
+ }
87
+ return result;
88
+ }
89
+ // term ::= factor (('*' | '/') factor)*
90
+ parseTerm() {
91
+ let result = this.parseFactor();
92
+ while (!this.failed) {
93
+ const tok = this.peek();
94
+ if (tok.type === 'op' && (tok.value === '*' || tok.value === '/')) {
95
+ this.consume();
96
+ const right = this.parseFactor();
97
+ result = tok.value === '*' ? result * right : result / right;
98
+ }
99
+ else {
100
+ break;
101
+ }
102
+ }
103
+ return result;
104
+ }
105
+ // factor ::= ('+' | '-')? atom
106
+ parseFactor() {
107
+ const tok = this.peek();
108
+ if (tok.type === 'op' && (tok.value === '+' || tok.value === '-')) {
109
+ this.consume();
110
+ const value = this.parseFactor();
111
+ return tok.value === '-' ? -value : value;
112
+ }
113
+ return this.parseAtom();
114
+ }
115
+ // atom ::= NUMBER | '(' expression ')'
116
+ parseAtom() {
117
+ const tok = this.peek();
118
+ if (tok.type === 'number') {
119
+ this.consume();
120
+ return tok.value;
121
+ }
122
+ if (tok.type === 'op' && tok.value === '(') {
123
+ this.consume();
124
+ const result = this.parseExpression();
125
+ const closing = this.peek();
126
+ if (closing.type === 'op' && closing.value === ')') {
127
+ this.consume();
128
+ return result;
129
+ }
130
+ // Missing closing paren
131
+ this.failed = true;
132
+ return NaN;
133
+ }
134
+ // Unexpected token
135
+ this.failed = true;
136
+ return NaN;
137
+ }
138
+ parse() {
139
+ const result = this.parseExpression();
140
+ // Ensure entire input was consumed
141
+ if (this.peek().type !== 'eof' || this.failed) {
142
+ return NaN;
143
+ }
144
+ return result;
145
+ }
146
+ }
147
+ /**
148
+ * Safely evaluate a mathematical expression string containing only
149
+ * numbers, operators (+, -, *, /), parentheses, and whitespace.
150
+ * Returns NaN for malformed or empty expressions.
151
+ * Returns Infinity/-Infinity for division by zero (matches JS behavior).
152
+ */
153
+ export function safeMathEval(expression) {
154
+ if (!expression || !expression.trim()) {
155
+ return NaN;
156
+ }
157
+ const tokens = tokenize(expression);
158
+ const parser = new Parser(tokens);
159
+ return parser.parse();
160
+ }