@zonastery/bursar 0.0.1 → 1.0.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 (483) hide show
  1. package/README.md +85 -344
  2. package/dist/billing/auto-recharge-port.d.ts +27 -0
  3. package/dist/billing/auto-recharge-port.d.ts.map +1 -0
  4. package/dist/billing/auto-recharge-port.js +2 -0
  5. package/dist/billing/auto-recharge-port.js.map +1 -0
  6. package/dist/billing/auto-recharge-service.d.ts +43 -0
  7. package/dist/billing/auto-recharge-service.d.ts.map +1 -0
  8. package/dist/billing/auto-recharge-service.js +218 -0
  9. package/dist/billing/auto-recharge-service.js.map +1 -0
  10. package/dist/billing/billing-service.d.ts +71 -0
  11. package/dist/billing/billing-service.d.ts.map +1 -0
  12. package/dist/billing/billing-service.js +172 -0
  13. package/dist/billing/billing-service.js.map +1 -0
  14. package/dist/billing/billing-store.d.ts +59 -0
  15. package/dist/billing/billing-store.d.ts.map +1 -0
  16. package/dist/billing/billing-store.js +9 -0
  17. package/dist/billing/billing-store.js.map +1 -0
  18. package/dist/billing/contracts.d.ts +149 -0
  19. package/dist/billing/contracts.d.ts.map +1 -0
  20. package/dist/billing/contracts.js +2 -0
  21. package/dist/billing/contracts.js.map +1 -0
  22. package/dist/billing/event-handlers.d.ts +57 -0
  23. package/dist/billing/event-handlers.d.ts.map +1 -0
  24. package/dist/billing/event-handlers.js +667 -0
  25. package/dist/billing/event-handlers.js.map +1 -0
  26. package/dist/billing/event-processor.d.ts +19 -0
  27. package/dist/billing/event-processor.d.ts.map +1 -0
  28. package/dist/billing/event-processor.js +105 -0
  29. package/dist/billing/event-processor.js.map +1 -0
  30. package/dist/billing/financial-event-handlers.d.ts +26 -0
  31. package/dist/billing/financial-event-handlers.d.ts.map +1 -0
  32. package/dist/billing/financial-event-handlers.js +241 -0
  33. package/dist/billing/financial-event-handlers.js.map +1 -0
  34. package/dist/billing/index.d.ts +10 -0
  35. package/dist/billing/index.d.ts.map +1 -0
  36. package/dist/billing/index.js +6 -0
  37. package/dist/billing/index.js.map +1 -0
  38. package/dist/billing/management.d.ts +59 -0
  39. package/dist/billing/management.d.ts.map +1 -0
  40. package/dist/billing/management.js +113 -0
  41. package/dist/billing/management.js.map +1 -0
  42. package/dist/billing/policy-window.d.ts +24 -0
  43. package/dist/billing/policy-window.d.ts.map +1 -0
  44. package/dist/billing/policy-window.js +88 -0
  45. package/dist/billing/policy-window.js.map +1 -0
  46. package/dist/billing/postgres/repositories/auto-recharge.d.ts +32 -0
  47. package/dist/billing/postgres/repositories/auto-recharge.d.ts.map +1 -0
  48. package/dist/billing/postgres/repositories/auto-recharge.js +178 -0
  49. package/dist/billing/postgres/repositories/auto-recharge.js.map +1 -0
  50. package/dist/billing/postgres/repositories/customer.d.ts +12 -0
  51. package/dist/billing/postgres/repositories/customer.d.ts.map +1 -0
  52. package/dist/billing/postgres/repositories/customer.js +33 -0
  53. package/dist/billing/postgres/repositories/customer.js.map +1 -0
  54. package/dist/billing/postgres/repositories/dispute.d.ts +7 -0
  55. package/dist/billing/postgres/repositories/dispute.d.ts.map +1 -0
  56. package/dist/billing/postgres/repositories/dispute.js +18 -0
  57. package/dist/billing/postgres/repositories/dispute.js.map +1 -0
  58. package/dist/billing/postgres/repositories/event.d.ts +21 -0
  59. package/dist/billing/postgres/repositories/event.d.ts.map +1 -0
  60. package/dist/billing/postgres/repositories/event.js +47 -0
  61. package/dist/billing/postgres/repositories/event.js.map +1 -0
  62. package/dist/billing/postgres/repositories/invoice.d.ts +9 -0
  63. package/dist/billing/postgres/repositories/invoice.d.ts.map +1 -0
  64. package/dist/billing/postgres/repositories/invoice.js +36 -0
  65. package/dist/billing/postgres/repositories/invoice.js.map +1 -0
  66. package/dist/billing/postgres/repositories/offer.d.ts +26 -0
  67. package/dist/billing/postgres/repositories/offer.d.ts.map +1 -0
  68. package/dist/billing/postgres/repositories/offer.js +73 -0
  69. package/dist/billing/postgres/repositories/offer.js.map +1 -0
  70. package/dist/billing/postgres/repositories/payment.d.ts +32 -0
  71. package/dist/billing/postgres/repositories/payment.d.ts.map +1 -0
  72. package/dist/billing/postgres/repositories/payment.js +78 -0
  73. package/dist/billing/postgres/repositories/payment.js.map +1 -0
  74. package/dist/billing/postgres/repositories/preferences.d.ts +15 -0
  75. package/dist/billing/postgres/repositories/preferences.d.ts.map +1 -0
  76. package/dist/billing/postgres/repositories/preferences.js +23 -0
  77. package/dist/billing/postgres/repositories/preferences.js.map +1 -0
  78. package/dist/billing/postgres/repositories/refund.d.ts +7 -0
  79. package/dist/billing/postgres/repositories/refund.d.ts.map +1 -0
  80. package/dist/billing/postgres/repositories/refund.js +32 -0
  81. package/dist/billing/postgres/repositories/refund.js.map +1 -0
  82. package/dist/billing/postgres/repositories/subscription.d.ts +46 -0
  83. package/dist/billing/postgres/repositories/subscription.d.ts.map +1 -0
  84. package/dist/billing/postgres/repositories/subscription.js +187 -0
  85. package/dist/billing/postgres/repositories/subscription.js.map +1 -0
  86. package/dist/billing/postgres/repositories/topup.d.ts +27 -0
  87. package/dist/billing/postgres/repositories/topup.d.ts.map +1 -0
  88. package/dist/billing/postgres/repositories/topup.js +51 -0
  89. package/dist/billing/postgres/repositories/topup.js.map +1 -0
  90. package/dist/billing/postgres/store.d.ts +87 -0
  91. package/dist/billing/postgres/store.d.ts.map +1 -0
  92. package/dist/billing/postgres/store.js +527 -0
  93. package/dist/billing/postgres/store.js.map +1 -0
  94. package/dist/billing/service-types.d.ts +37 -0
  95. package/dist/billing/service-types.d.ts.map +1 -0
  96. package/dist/billing/service-types.js +2 -0
  97. package/dist/billing/service-types.js.map +1 -0
  98. package/dist/billing/types/auto-recharge.d.ts +56 -0
  99. package/dist/billing/types/auto-recharge.d.ts.map +1 -0
  100. package/dist/billing/types/auto-recharge.js +2 -0
  101. package/dist/billing/types/auto-recharge.js.map +1 -0
  102. package/dist/billing/types/catalog.d.ts +47 -0
  103. package/dist/billing/types/catalog.d.ts.map +1 -0
  104. package/dist/billing/types/catalog.js +2 -0
  105. package/dist/billing/types/catalog.js.map +1 -0
  106. package/dist/billing/types/common.d.ts +16 -0
  107. package/dist/billing/types/common.d.ts.map +1 -0
  108. package/dist/billing/types/common.js +2 -0
  109. package/dist/billing/types/common.js.map +1 -0
  110. package/dist/billing/types/documents.d.ts +39 -0
  111. package/dist/billing/types/documents.d.ts.map +1 -0
  112. package/dist/billing/types/documents.js +2 -0
  113. package/dist/billing/types/documents.js.map +1 -0
  114. package/dist/billing/types/events.d.ts +75 -0
  115. package/dist/billing/types/events.d.ts.map +1 -0
  116. package/dist/billing/types/events.js +38 -0
  117. package/dist/billing/types/events.js.map +1 -0
  118. package/dist/billing/types/index.d.ts +8 -0
  119. package/dist/billing/types/index.d.ts.map +1 -0
  120. package/dist/billing/types/index.js +8 -0
  121. package/dist/billing/types/index.js.map +1 -0
  122. package/dist/billing/types/preferences.d.ts +13 -0
  123. package/dist/billing/types/preferences.d.ts.map +1 -0
  124. package/dist/billing/types/preferences.js +2 -0
  125. package/dist/billing/types/preferences.js.map +1 -0
  126. package/dist/billing/types/subscriptions.d.ts +97 -0
  127. package/dist/billing/types/subscriptions.d.ts.map +1 -0
  128. package/dist/billing/types/subscriptions.js +12 -0
  129. package/dist/billing/types/subscriptions.js.map +1 -0
  130. package/dist/breakdown.d.ts +3 -1
  131. package/dist/breakdown.d.ts.map +1 -1
  132. package/dist/breakdown.js +4 -1
  133. package/dist/breakdown.js.map +1 -1
  134. package/dist/browser.d.ts +8 -0
  135. package/dist/browser.d.ts.map +1 -0
  136. package/dist/browser.js +7 -0
  137. package/dist/browser.js.map +1 -0
  138. package/dist/bursar.d.ts +80 -0
  139. package/dist/bursar.d.ts.map +1 -0
  140. package/dist/bursar.js +129 -0
  141. package/dist/bursar.js.map +1 -0
  142. package/dist/catalog.d.ts +61 -0
  143. package/dist/catalog.d.ts.map +1 -0
  144. package/dist/catalog.js +95 -0
  145. package/dist/catalog.js.map +1 -0
  146. package/dist/commerce/errors.d.ts +69 -0
  147. package/dist/commerce/errors.d.ts.map +1 -0
  148. package/dist/commerce/errors.js +107 -0
  149. package/dist/commerce/errors.js.map +1 -0
  150. package/dist/commerce/index.d.ts +6 -0
  151. package/dist/commerce/index.d.ts.map +1 -0
  152. package/dist/commerce/index.js +6 -0
  153. package/dist/commerce/index.js.map +1 -0
  154. package/dist/commerce/plan-change.d.ts +10 -0
  155. package/dist/commerce/plan-change.d.ts.map +1 -0
  156. package/dist/commerce/plan-change.js +42 -0
  157. package/dist/commerce/plan-change.js.map +1 -0
  158. package/dist/commerce/provider-registry.d.ts +18 -0
  159. package/dist/commerce/provider-registry.d.ts.map +1 -0
  160. package/dist/commerce/provider-registry.js +70 -0
  161. package/dist/commerce/provider-registry.js.map +1 -0
  162. package/dist/commerce/service.d.ts +73 -0
  163. package/dist/commerce/service.d.ts.map +1 -0
  164. package/dist/commerce/service.js +981 -0
  165. package/dist/commerce/service.js.map +1 -0
  166. package/dist/commerce/types.d.ts +217 -0
  167. package/dist/commerce/types.d.ts.map +1 -0
  168. package/dist/commerce/types.js.map +1 -0
  169. package/dist/config/parse-access.d.ts +6 -0
  170. package/dist/config/parse-access.d.ts.map +1 -0
  171. package/dist/config/parse-access.js +72 -0
  172. package/dist/config/parse-access.js.map +1 -0
  173. package/dist/config/parse-commerce.d.ts +3 -0
  174. package/dist/config/parse-commerce.d.ts.map +1 -0
  175. package/dist/config/parse-commerce.js +236 -0
  176. package/dist/config/parse-commerce.js.map +1 -0
  177. package/dist/config/parse-credits.d.ts +3 -0
  178. package/dist/config/parse-credits.d.ts.map +1 -0
  179. package/dist/config/parse-credits.js +99 -0
  180. package/dist/config/parse-credits.js.map +1 -0
  181. package/dist/config/parse-plans.d.ts +5 -0
  182. package/dist/config/parse-plans.d.ts.map +1 -0
  183. package/dist/config/parse-plans.js +126 -0
  184. package/dist/config/parse-plans.js.map +1 -0
  185. package/dist/config/parse-pricing.d.ts +4 -0
  186. package/dist/config/parse-pricing.d.ts.map +1 -0
  187. package/dist/config/parse-pricing.js +252 -0
  188. package/dist/config/parse-pricing.js.map +1 -0
  189. package/dist/config/parse-utils.d.ts +18 -0
  190. package/dist/config/parse-utils.d.ts.map +1 -0
  191. package/dist/config/parse-utils.js +118 -0
  192. package/dist/config/parse-utils.js.map +1 -0
  193. package/dist/config/types.d.ts +327 -0
  194. package/dist/config/types.d.ts.map +1 -0
  195. package/dist/config/types.js +2 -0
  196. package/dist/config/types.js.map +1 -0
  197. package/dist/config.d.ts +6 -22
  198. package/dist/config.d.ts.map +1 -1
  199. package/dist/config.js +82 -263
  200. package/dist/config.js.map +1 -1
  201. package/dist/{stores → credits}/events.d.ts +6 -6
  202. package/dist/credits/events.d.ts.map +1 -0
  203. package/dist/{stores → credits}/events.js +5 -5
  204. package/dist/credits/events.js.map +1 -0
  205. package/dist/credits/lease-workflow.d.ts +90 -0
  206. package/dist/credits/lease-workflow.d.ts.map +1 -0
  207. package/dist/credits/lease-workflow.js +378 -0
  208. package/dist/credits/lease-workflow.js.map +1 -0
  209. package/dist/credits/low-balance-monitor.d.ts +26 -0
  210. package/dist/credits/low-balance-monitor.d.ts.map +1 -0
  211. package/dist/credits/low-balance-monitor.js +110 -0
  212. package/dist/credits/low-balance-monitor.js.map +1 -0
  213. package/dist/credits/postgres/repositories/analytics.d.ts +77 -0
  214. package/dist/credits/postgres/repositories/analytics.d.ts.map +1 -0
  215. package/dist/credits/postgres/repositories/analytics.js +151 -0
  216. package/dist/credits/postgres/repositories/analytics.js.map +1 -0
  217. package/dist/credits/postgres/repositories/balance.d.ts +66 -0
  218. package/dist/credits/postgres/repositories/balance.d.ts.map +1 -0
  219. package/dist/credits/postgres/repositories/balance.js +139 -0
  220. package/dist/credits/postgres/repositories/balance.js.map +1 -0
  221. package/dist/credits/postgres/repositories/bucket.d.ts +25 -0
  222. package/dist/credits/postgres/repositories/bucket.d.ts.map +1 -0
  223. package/dist/credits/postgres/repositories/bucket.js +52 -0
  224. package/dist/credits/postgres/repositories/bucket.js.map +1 -0
  225. package/dist/credits/postgres/repositories/deduction.d.ts +55 -0
  226. package/dist/credits/postgres/repositories/deduction.d.ts.map +1 -0
  227. package/dist/credits/postgres/repositories/deduction.js +130 -0
  228. package/dist/credits/postgres/repositories/deduction.js.map +1 -0
  229. package/dist/credits/postgres/repositories/lease.d.ts +69 -0
  230. package/dist/credits/postgres/repositories/lease.d.ts.map +1 -0
  231. package/dist/credits/postgres/repositories/lease.js +148 -0
  232. package/dist/credits/postgres/repositories/lease.js.map +1 -0
  233. package/dist/credits/postgres/repositories/plan.d.ts +105 -0
  234. package/dist/credits/postgres/repositories/plan.d.ts.map +1 -0
  235. package/dist/credits/postgres/repositories/plan.js +182 -0
  236. package/dist/credits/postgres/repositories/plan.js.map +1 -0
  237. package/dist/credits/postgres/repositories/pricing.d.ts +28 -0
  238. package/dist/credits/postgres/repositories/pricing.d.ts.map +1 -0
  239. package/dist/credits/postgres/repositories/pricing.js +70 -0
  240. package/dist/credits/postgres/repositories/pricing.js.map +1 -0
  241. package/dist/credits/postgres/repositories/team.d.ts +63 -0
  242. package/dist/credits/postgres/repositories/team.d.ts.map +1 -0
  243. package/dist/credits/postgres/repositories/team.js +120 -0
  244. package/dist/credits/postgres/repositories/team.js.map +1 -0
  245. package/dist/credits/postgres/store.d.ts +84 -0
  246. package/dist/credits/postgres/store.d.ts.map +1 -0
  247. package/dist/credits/postgres/store.js +765 -0
  248. package/dist/credits/postgres/store.js.map +1 -0
  249. package/dist/credits/postgres/value-mappers.d.ts +17 -0
  250. package/dist/credits/postgres/value-mappers.d.ts.map +1 -0
  251. package/dist/credits/postgres/value-mappers.js +98 -0
  252. package/dist/credits/postgres/value-mappers.js.map +1 -0
  253. package/dist/credits/pricing-runtime.d.ts +34 -0
  254. package/dist/credits/pricing-runtime.d.ts.map +1 -0
  255. package/dist/credits/pricing-runtime.js +141 -0
  256. package/dist/credits/pricing-runtime.js.map +1 -0
  257. package/dist/credits/queries.d.ts +33 -0
  258. package/dist/credits/queries.d.ts.map +1 -0
  259. package/dist/credits/queries.js +71 -0
  260. package/dist/credits/queries.js.map +1 -0
  261. package/dist/credits/service-errors.d.ts +4 -0
  262. package/dist/credits/service-errors.d.ts.map +1 -0
  263. package/dist/credits/service-errors.js +57 -0
  264. package/dist/credits/service-errors.js.map +1 -0
  265. package/dist/credits/service-types.d.ts +122 -0
  266. package/dist/credits/service-types.d.ts.map +1 -0
  267. package/dist/credits/service-types.js +2 -0
  268. package/dist/credits/service-types.js.map +1 -0
  269. package/dist/credits/service.d.ts +207 -0
  270. package/dist/credits/service.d.ts.map +1 -0
  271. package/dist/credits/service.js +594 -0
  272. package/dist/credits/service.js.map +1 -0
  273. package/dist/{stores/credit-store.d.ts → credits/store.d.ts} +67 -71
  274. package/dist/credits/store.d.ts.map +1 -0
  275. package/dist/{stores/credit-store.js → credits/store.js} +30 -6
  276. package/dist/credits/store.js.map +1 -0
  277. package/dist/credits/types/account.d.ts +82 -0
  278. package/dist/credits/types/account.d.ts.map +1 -0
  279. package/dist/credits/types/account.js +2 -0
  280. package/dist/credits/types/account.js.map +1 -0
  281. package/dist/credits/types/admission.d.ts +39 -0
  282. package/dist/credits/types/admission.d.ts.map +1 -0
  283. package/dist/credits/types/admission.js +2 -0
  284. package/dist/credits/types/admission.js.map +1 -0
  285. package/dist/credits/types/analytics.d.ts +52 -0
  286. package/dist/credits/types/analytics.d.ts.map +1 -0
  287. package/dist/credits/types/analytics.js +2 -0
  288. package/dist/credits/types/analytics.js.map +1 -0
  289. package/dist/credits/types/catalog.d.ts +69 -0
  290. package/dist/credits/types/catalog.d.ts.map +1 -0
  291. package/dist/credits/types/catalog.js +2 -0
  292. package/dist/credits/types/catalog.js.map +1 -0
  293. package/dist/credits/types/grants.d.ts +24 -0
  294. package/dist/credits/types/grants.d.ts.map +1 -0
  295. package/dist/credits/types/grants.js +2 -0
  296. package/dist/credits/types/grants.js.map +1 -0
  297. package/dist/credits/types/index.d.ts +9 -0
  298. package/dist/credits/types/index.d.ts.map +1 -0
  299. package/dist/credits/types/index.js +2 -0
  300. package/dist/credits/types/index.js.map +1 -0
  301. package/dist/credits/types/ledger.d.ts +77 -0
  302. package/dist/credits/types/ledger.d.ts.map +1 -0
  303. package/dist/credits/types/ledger.js +2 -0
  304. package/dist/credits/types/ledger.js.map +1 -0
  305. package/dist/credits/types/quotas.d.ts +40 -0
  306. package/dist/credits/types/quotas.d.ts.map +1 -0
  307. package/dist/credits/types/quotas.js +2 -0
  308. package/dist/credits/types/quotas.js.map +1 -0
  309. package/dist/credits/types/teams.d.ts +44 -0
  310. package/dist/credits/types/teams.d.ts.map +1 -0
  311. package/dist/credits/types/teams.js +2 -0
  312. package/dist/credits/types/teams.js.map +1 -0
  313. package/dist/engine.d.ts +18 -39
  314. package/dist/engine.d.ts.map +1 -1
  315. package/dist/engine.js +169 -145
  316. package/dist/engine.js.map +1 -1
  317. package/dist/errors.d.ts +84 -25
  318. package/dist/errors.d.ts.map +1 -1
  319. package/dist/errors.js +117 -24
  320. package/dist/errors.js.map +1 -1
  321. package/dist/expr/ast.d.ts +46 -0
  322. package/dist/expr/ast.d.ts.map +1 -0
  323. package/dist/expr/ast.js +18 -0
  324. package/dist/expr/ast.js.map +1 -0
  325. package/dist/expr/evaluator.d.ts +4 -0
  326. package/dist/expr/evaluator.d.ts.map +1 -0
  327. package/dist/expr/evaluator.js +144 -0
  328. package/dist/expr/evaluator.js.map +1 -0
  329. package/dist/expr/language.d.ts +5 -0
  330. package/dist/expr/language.d.ts.map +1 -0
  331. package/dist/expr/language.js +73 -0
  332. package/dist/expr/language.js.map +1 -0
  333. package/dist/expr/parser.d.ts +24 -0
  334. package/dist/expr/parser.d.ts.map +1 -0
  335. package/dist/expr/parser.js +175 -0
  336. package/dist/expr/parser.js.map +1 -0
  337. package/dist/expr/tokenizer.d.ts +7 -0
  338. package/dist/expr/tokenizer.d.ts.map +1 -0
  339. package/dist/expr/tokenizer.js +85 -0
  340. package/dist/expr/tokenizer.js.map +1 -0
  341. package/dist/expr.d.ts +7 -58
  342. package/dist/expr.d.ts.map +1 -1
  343. package/dist/expr.js +41 -640
  344. package/dist/expr.js.map +1 -1
  345. package/dist/generated/pricing-config.schema.json +2764 -0
  346. package/dist/generated/pricing-config.types.d.ts +674 -0
  347. package/dist/generated/pricing-config.types.d.ts.map +1 -0
  348. package/dist/generated/pricing-config.types.js +8 -0
  349. package/dist/generated/pricing-config.types.js.map +1 -0
  350. package/dist/index.d.ts +21 -15
  351. package/dist/index.d.ts.map +1 -1
  352. package/dist/index.js +13 -9
  353. package/dist/index.js.map +1 -1
  354. package/dist/load-pricing-file.d.ts.map +1 -1
  355. package/dist/load-pricing-file.js +61 -6
  356. package/dist/load-pricing-file.js.map +1 -1
  357. package/dist/metrics.d.ts +6 -16
  358. package/dist/metrics.d.ts.map +1 -1
  359. package/dist/node.d.ts +9 -2
  360. package/dist/node.d.ts.map +1 -1
  361. package/dist/node.js +6 -2
  362. package/dist/node.js.map +1 -1
  363. package/dist/providers/_shared.d.ts +9 -0
  364. package/dist/providers/_shared.d.ts.map +1 -0
  365. package/dist/providers/_shared.js +25 -0
  366. package/dist/providers/_shared.js.map +1 -0
  367. package/dist/providers/dodo/client-contract.d.ts +106 -0
  368. package/dist/providers/dodo/client-contract.d.ts.map +1 -0
  369. package/dist/providers/dodo/client-contract.js +2 -0
  370. package/dist/providers/dodo/client-contract.js.map +1 -0
  371. package/dist/providers/dodo/event-mapper.d.ts +6 -0
  372. package/dist/providers/dodo/event-mapper.d.ts.map +1 -0
  373. package/dist/providers/dodo/event-mapper.js +344 -0
  374. package/dist/providers/dodo/event-mapper.js.map +1 -0
  375. package/dist/providers/dodo/index.d.ts +4 -0
  376. package/dist/providers/dodo/index.d.ts.map +1 -0
  377. package/dist/providers/dodo/index.js +3 -0
  378. package/dist/providers/dodo/index.js.map +1 -0
  379. package/dist/providers/dodo/provider.d.ts +53 -0
  380. package/dist/providers/dodo/provider.d.ts.map +1 -0
  381. package/dist/providers/dodo/provider.js +353 -0
  382. package/dist/providers/dodo/provider.js.map +1 -0
  383. package/dist/providers/index.d.ts +9 -0
  384. package/dist/providers/index.d.ts.map +1 -0
  385. package/dist/providers/index.js +7 -0
  386. package/dist/providers/index.js.map +1 -0
  387. package/dist/providers/mock/index.d.ts +2 -0
  388. package/dist/providers/mock/index.d.ts.map +1 -0
  389. package/dist/providers/mock/index.js +2 -0
  390. package/dist/providers/mock/index.js.map +1 -0
  391. package/dist/providers/mock/provider.d.ts +41 -0
  392. package/dist/providers/mock/provider.d.ts.map +1 -0
  393. package/dist/providers/mock/provider.js +124 -0
  394. package/dist/providers/mock/provider.js.map +1 -0
  395. package/dist/providers/stripe/event-mapper.d.ts +7 -0
  396. package/dist/providers/stripe/event-mapper.d.ts.map +1 -0
  397. package/dist/providers/stripe/event-mapper.js +342 -0
  398. package/dist/providers/stripe/event-mapper.js.map +1 -0
  399. package/dist/providers/stripe/index.d.ts +3 -0
  400. package/dist/providers/stripe/index.d.ts.map +1 -0
  401. package/dist/providers/stripe/index.js +3 -0
  402. package/dist/providers/stripe/index.js.map +1 -0
  403. package/dist/providers/stripe/provider.d.ts +48 -0
  404. package/dist/providers/stripe/provider.d.ts.map +1 -0
  405. package/dist/providers/stripe/provider.js +289 -0
  406. package/dist/providers/stripe/provider.js.map +1 -0
  407. package/dist/providers/types.d.ts +175 -0
  408. package/dist/providers/types.d.ts.map +1 -0
  409. package/dist/providers/types.js +18 -0
  410. package/dist/providers/types.js.map +1 -0
  411. package/dist/retry.d.ts +13 -0
  412. package/dist/retry.d.ts.map +1 -0
  413. package/dist/retry.js +28 -0
  414. package/dist/retry.js.map +1 -0
  415. package/dist/shared/logger.d.ts +10 -0
  416. package/dist/shared/logger.d.ts.map +1 -0
  417. package/dist/shared/logger.js +15 -0
  418. package/dist/shared/logger.js.map +1 -0
  419. package/dist/shared/postgres-client.d.ts +51 -0
  420. package/dist/shared/postgres-client.d.ts.map +1 -0
  421. package/dist/shared/postgres-client.js +101 -0
  422. package/dist/shared/postgres-client.js.map +1 -0
  423. package/dist/shared/postgres-types.d.ts +7 -0
  424. package/dist/shared/postgres-types.d.ts.map +1 -0
  425. package/dist/shared/postgres-types.js +2 -0
  426. package/dist/shared/postgres-types.js.map +1 -0
  427. package/dist/shared/postgres-validation.d.ts +8 -0
  428. package/dist/shared/postgres-validation.d.ts.map +1 -0
  429. package/dist/shared/postgres-validation.js +40 -0
  430. package/dist/shared/postgres-validation.js.map +1 -0
  431. package/dist/storage/adapters/clickhouse.d.ts +61 -0
  432. package/dist/storage/adapters/clickhouse.d.ts.map +1 -0
  433. package/dist/storage/adapters/clickhouse.js +329 -0
  434. package/dist/storage/adapters/clickhouse.js.map +1 -0
  435. package/dist/storage/adapters/s3.d.ts +35 -0
  436. package/dist/storage/adapters/s3.d.ts.map +1 -0
  437. package/dist/storage/adapters/s3.js +100 -0
  438. package/dist/storage/adapters/s3.js.map +1 -0
  439. package/dist/storage/outbox-worker.d.ts +40 -0
  440. package/dist/storage/outbox-worker.d.ts.map +1 -0
  441. package/dist/storage/outbox-worker.js +132 -0
  442. package/dist/storage/outbox-worker.js.map +1 -0
  443. package/dist/storage/ports.d.ts +77 -0
  444. package/dist/storage/ports.d.ts.map +1 -0
  445. package/dist/storage/ports.js +2 -0
  446. package/dist/storage/ports.js.map +1 -0
  447. package/dist/storage/postgres-repository.d.ts +14 -0
  448. package/dist/storage/postgres-repository.d.ts.map +1 -0
  449. package/dist/storage/postgres-repository.js +130 -0
  450. package/dist/storage/postgres-repository.js.map +1 -0
  451. package/dist/storage/runtime.d.ts +67 -0
  452. package/dist/storage/runtime.d.ts.map +1 -0
  453. package/dist/storage/runtime.js +313 -0
  454. package/dist/storage/runtime.js.map +1 -0
  455. package/package.json +70 -12
  456. package/dist/allowance.d.ts +0 -48
  457. package/dist/allowance.d.ts.map +0 -1
  458. package/dist/allowance.js +0 -140
  459. package/dist/allowance.js.map +0 -1
  460. package/dist/manager.d.ts +0 -421
  461. package/dist/manager.d.ts.map +0 -1
  462. package/dist/manager.js +0 -1112
  463. package/dist/manager.js.map +0 -1
  464. package/dist/stores/credit-store.d.ts.map +0 -1
  465. package/dist/stores/credit-store.js.map +0 -1
  466. package/dist/stores/events.d.ts.map +0 -1
  467. package/dist/stores/events.js.map +0 -1
  468. package/dist/stores/memory-store.d.ts +0 -178
  469. package/dist/stores/memory-store.d.ts.map +0 -1
  470. package/dist/stores/memory-store.js +0 -1833
  471. package/dist/stores/memory-store.js.map +0 -1
  472. package/dist/stores/postgres-store.d.ts +0 -95
  473. package/dist/stores/postgres-store.d.ts.map +0 -1
  474. package/dist/stores/postgres-store.js +0 -872
  475. package/dist/stores/postgres-store.js.map +0 -1
  476. package/dist/stores/supabase-store.d.ts +0 -76
  477. package/dist/stores/supabase-store.d.ts.map +0 -1
  478. package/dist/stores/supabase-store.js +0 -865
  479. package/dist/stores/supabase-store.js.map +0 -1
  480. package/dist/types.d.ts +0 -462
  481. package/dist/types.d.ts.map +0 -1
  482. package/dist/types.js.map +0 -1
  483. /package/dist/{types.js → commerce/types.js} +0 -0
package/dist/expr.js CHANGED
@@ -1,681 +1,82 @@
1
1
  import Decimal from "decimal.js";
2
2
  import { ExpressionError } from "./errors.js";
3
- // Quantization: all credit amounts round to 4 decimal places, ROUND_HALF_UP.
4
- // This must match the Python SDK's Decimal quantize(0.0001, ROUND_HALF_UP).
5
- export const MONEY_DP = 4;
6
- export const MONEY_ROUNDING = Decimal.ROUND_HALF_UP;
7
- /** Quantize a Decimal credit amount to 4dp, ROUND_HALF_UP. */
3
+ import { children } from "./expr/ast.js";
4
+ import { evaluateNode } from "./expr/evaluator.js";
5
+ import { ALLOWED_FUNCTIONS, collectVariables, validateCalls } from "./expr/language.js";
6
+ import { ExpressionParser } from "./expr/parser.js";
7
+ const MONEY_DECIMAL_PLACES = 6;
8
+ /** Quantize a Decimal credit amount to 6dp using ROUND_HALF_UP. */
8
9
  export function quantizeMoney(value) {
9
- return value.toDecimalPlaces(MONEY_DP, MONEY_ROUNDING);
10
+ return value.toDecimalPlaces(MONEY_DECIMAL_PLACES, Decimal.ROUND_HALF_UP);
10
11
  }
11
- const ALLOWED_FUNCTIONS = new Set([
12
- "ceil",
13
- "floor",
14
- "min",
15
- "max",
16
- "round",
17
- "if",
18
- "tier",
19
- "clamp",
20
- "percentile",
21
- ]);
22
- function tokenize(src) {
23
- const tokens = [];
24
- let i = 0;
25
- while (i < src.length) {
26
- if (src[i] === " " || src[i] === "\t" || src[i] === "\n") {
27
- i++;
28
- continue;
29
- }
30
- if (src[i] === "(") {
31
- tokens.push({ type: "(", value: "(" });
32
- i++;
33
- continue;
34
- }
35
- if (src[i] === ")") {
36
- tokens.push({ type: ")", value: ")" });
37
- i++;
38
- continue;
39
- }
40
- if (src[i] === ",") {
41
- tokens.push({ type: ",", value: "," });
42
- i++;
43
- continue;
44
- }
45
- // Two-char operators
46
- const two = src.slice(i, i + 2);
47
- if (two === "**") {
48
- tokens.push({ type: "**", value: "**" });
49
- i += 2;
50
- continue;
51
- }
52
- if (two === "//") {
53
- tokens.push({ type: "//", value: "//" });
54
- i += 2;
55
- continue;
56
- }
57
- if (two === "==") {
58
- tokens.push({ type: "==", value: "==" });
59
- i += 2;
60
- continue;
61
- }
62
- if (two === "!=") {
63
- tokens.push({ type: "!=", value: "!=" });
64
- i += 2;
65
- continue;
66
- }
67
- if (two === "<=") {
68
- tokens.push({ type: "<=", value: "<=" });
69
- i += 2;
70
- continue;
71
- }
72
- if (two === ">=") {
73
- tokens.push({ type: ">=", value: ">=" });
74
- i += 2;
75
- continue;
76
- }
77
- // "not in" needs special handling — we tokenize "not" and then "in" separately
78
- // The parser will handle "not" + "in" as "not in"
79
- // One-char operators
80
- if (src[i] === "+") {
81
- tokens.push({ type: "+", value: "+" });
82
- i++;
83
- continue;
84
- }
85
- if (src[i] === "-") {
86
- tokens.push({ type: "-", value: "-" });
87
- i++;
88
- continue;
89
- }
90
- if (src[i] === "*") {
91
- tokens.push({ type: "*", value: "*" });
92
- i++;
93
- continue;
94
- }
95
- if (src[i] === "/") {
96
- tokens.push({ type: "/", value: "/" });
97
- i++;
98
- continue;
99
- }
100
- if (src[i] === "%") {
101
- tokens.push({ type: "%", value: "%" });
102
- i++;
103
- continue;
104
- }
105
- if (src[i] === "<") {
106
- tokens.push({ type: "<", value: "<" });
107
- i++;
108
- continue;
109
- }
110
- if (src[i] === ">") {
111
- tokens.push({ type: ">", value: ">" });
112
- i++;
113
- continue;
114
- }
115
- // Numbers — strict literal: digits with optional '.' and optional 'e'/'E' exponent.
116
- // Supports scientific notation (1e3, 1e-6, 2.5E+10) so per-token pricing
117
- // configs like {"_default":"input_tokens * 1e-6"} load in JS (H5 parity fix).
118
- // Rejects malformed literals like "1.2.3" (H11 parity fix).
119
- if (/[0-9.]/.test(src[i])) {
120
- let num = "";
121
- let dotSeen = false;
122
- // Mantissa: digits and at most one '.'
123
- while (i < src.length && /[0-9.]/.test(src[i])) {
124
- if (src[i] === ".") {
125
- if (dotSeen) {
126
- throw new ExpressionError(`invalid number literal: '${num}.'`);
127
- }
128
- dotSeen = true;
129
- }
130
- num += src[i];
131
- i++;
132
- }
133
- if (num === "." || !/^[0-9]*\.?[0-9]*$/.test(num) || !/[0-9]/.test(num)) {
134
- throw new ExpressionError(`invalid number literal: '${num}'`);
135
- }
136
- // Optional exponent: e/E followed by optional +/- and one or more digits.
137
- if (i < src.length && (src[i] === "e" || src[i] === "E")) {
138
- num += src[i];
139
- i++;
140
- if (i < src.length && (src[i] === "+" || src[i] === "-")) {
141
- num += src[i];
142
- i++;
143
- }
144
- if (i >= src.length || !/[0-9]/.test(src[i])) {
145
- throw new ExpressionError(`invalid number literal: '${num}'`);
146
- }
147
- while (i < src.length && /[0-9]/.test(src[i])) {
148
- num += src[i];
149
- i++;
150
- }
151
- }
152
- tokens.push({ type: "number", value: num });
153
- continue;
154
- }
155
- // Identifiers and keywords
156
- if (/[a-zA-Z_]/.test(src[i])) {
157
- let word = "";
158
- while (i < src.length && /[a-zA-Z0-9_]/.test(src[i])) {
159
- word += src[i];
160
- i++;
161
- }
162
- const kw = word === "and"
163
- ? "and"
164
- : word === "or"
165
- ? "or"
166
- : word === "if"
167
- ? "if"
168
- : word === "else"
169
- ? "else"
170
- : word === "in"
171
- ? "in"
172
- : word === "not"
173
- ? "not"
174
- : word === "true"
175
- ? "number"
176
- : word === "false"
177
- ? "number"
178
- : "identifier";
179
- tokens.push({
180
- type: kw,
181
- value: word === "true" ? "1" : word === "false" ? "0" : word,
182
- });
183
- continue;
184
- }
185
- throw new ExpressionError(`unexpected character: '${src[i]}'`);
186
- }
187
- return tokens;
188
- }
189
- // ── Parser ─────────────────────────────────────────────────────────────────
190
- class Parser {
191
- tokens;
192
- pos = 0;
193
- constructor(tokens) {
194
- this.tokens = tokens;
195
- }
196
- peek() {
197
- return this.tokens[this.pos];
198
- }
199
- previous() {
200
- return this.tokens[this.pos - 1];
201
- }
202
- isAtEnd() {
203
- return this.pos >= this.tokens.length;
204
- }
205
- check(...types) {
206
- if (this.isAtEnd())
207
- return false;
208
- return types.includes(this.tokens[this.pos].type);
209
- }
210
- match(...types) {
211
- for (const t of types) {
212
- if (this.check(t)) {
213
- this.pos++;
214
- return true;
215
- }
216
- }
217
- return false;
218
- }
219
- consume(type, msg) {
220
- if (this.check(type))
221
- return this.tokens[this.pos++];
222
- throw new ExpressionError(msg);
223
- }
224
- parse() {
225
- const expr = this.boolExpr();
226
- // Handle ternary: X if cond else Y (at top level so operators bind correctly)
227
- if (this.match("if")) {
228
- const cond = this.boolExpr();
229
- this.consume("else", "expected 'else' in ternary expression");
230
- const elseBranch = this.boolExpr();
231
- return { type: "ternary", cond, then: expr, else: elseBranch };
232
- }
233
- return expr;
234
- }
235
- comparison() {
236
- let left = this.addition();
237
- // Handle "not in" as two tokens
238
- if (this.match("not") && this.match("in")) {
239
- const right = this.addition();
240
- left = { type: "comparison", op: "not in", left, right };
241
- }
242
- while (this.match("==", "!=", "<", "<=", ">", ">=", "in")) {
243
- const op = this.previous().value;
244
- const right = this.addition();
245
- left = { type: "comparison", op, left, right };
246
- }
247
- return left;
248
- }
249
- addition() {
250
- let left = this.multiplication();
251
- while (this.match("+", "-")) {
252
- const op = this.previous().value;
253
- const right = this.multiplication();
254
- left = { type: "binary", op, left, right };
255
- }
256
- return left;
257
- }
258
- multiplication() {
259
- let left = this.unary();
260
- // NOTE: "**" is intentionally NOT accepted here. Exponentiation is rejected
261
- // entirely (sandbox decision C5/C7, matching the Python SDK) — a "**" token
262
- // surfaces as a leftover token and fails validation/evaluation. We also
263
- // raise an explicit error if one is seen mid-expression.
264
- while (this.match("*", "/", "//", "%")) {
265
- const op = this.previous().value;
266
- const right = this.unary();
267
- left = { type: "binary", op, left, right };
268
- }
269
- if (this.check("**")) {
270
- throw new ExpressionError("exponentiation operator '**' is not allowed in pricing expressions");
271
- }
272
- return left;
273
- }
274
- notExpr() {
275
- if (this.match("not")) {
276
- const operand = this.notExpr();
277
- return { type: "unary", op: "not", operand };
278
- }
279
- return this.comparison();
280
- }
281
- boolExpr() {
282
- let left = this.notExpr();
283
- while (this.match("and", "or")) {
284
- const op = this.previous().value;
285
- const right = this.notExpr();
286
- left = { type: "boolean", op, left, right };
287
- }
288
- return left;
289
- }
290
- unary() {
291
- if (this.match("+", "-")) {
292
- const op = this.previous().value;
293
- return { type: "unary", op, operand: this.unary() };
294
- }
295
- return this.callOrPrimary();
296
- }
297
- callOrPrimary() {
298
- return this.primary();
299
- }
300
- primary() {
301
- if (this.check("**")) {
302
- throw new ExpressionError("exponentiation operator '**' is not allowed in pricing expressions");
303
- }
304
- if (this.match("number")) {
305
- return { type: "number", value: this.previous().value };
306
- }
307
- if (this.match("identifier")) {
308
- const name = this.previous().value;
309
- // Check if this is a function call like ceil(...)
310
- if (this.match("(")) {
311
- const args = [];
312
- if (!this.check(")")) {
313
- do {
314
- args.push(this.boolExpr());
315
- } while (this.match(","));
316
- }
317
- this.consume(")", "expected ')'");
318
- if (!ALLOWED_FUNCTIONS.has(name)) {
319
- throw new ExpressionError(`disallowed function: ${name}`);
320
- }
321
- return { type: "call", name, args };
322
- }
323
- return { type: "identifier", name };
324
- }
325
- // if(cond, then, else) — disambiguate from ternary by checking for '('
326
- if (this.match("if") && this.match("(")) {
327
- const args = [];
328
- if (!this.check(")")) {
329
- do {
330
- args.push(this.boolExpr());
331
- } while (this.match(","));
332
- }
333
- this.consume(")", "expected ')'");
334
- return { type: "call", name: "if", args };
335
- }
336
- if (this.match("(")) {
337
- const expr = this.parse();
338
- this.consume(")", "expected ')'");
339
- return expr;
340
- }
341
- throw new ExpressionError(`unexpected token: '${this.peek()?.value ?? "EOF"}'`);
342
- }
343
- }
344
- // ── Helper arity / range validation (H6) ─────────────────────────────────────
345
- // Static checks applied at parse/validate time so config-load (and the parity
346
- // fixture's expect_error cases) reject malformed helper calls. Semantics must
347
- // match the Python SDK exactly.
348
- function checkCallArity(name, argc) {
349
- switch (name) {
350
- case "if":
351
- if (argc !== 3)
352
- throw new ExpressionError("if() requires exactly 3 arguments: if(condition, then, else)");
353
- break;
354
- case "clamp":
355
- if (argc !== 3)
356
- throw new ExpressionError("clamp() requires exactly 3 arguments: clamp(x, min, max)");
357
- break;
358
- case "tier":
359
- // tier(value, t1, r1, [t2, r2, ...], default) — the value, then N>=1
360
- // (threshold, rate) pairs, then a trailing default. Arg count must be
361
- // EVEN and >= 4 (REFACTOR_CONTRACT.md §1): 4 args = 1 pair + default,
362
- // 6 args = 2 pairs + default, etc. Odd arg counts (3/5/7) and < 4 are
363
- // rejected. Semantics: return r_i for the first value < t_i, else default.
364
- if (argc < 4 || argc % 2 !== 0)
365
- throw new ExpressionError("tier() requires an even number of arguments >= 4 (value, threshold, rate, ..., default)");
366
- break;
367
- case "percentile":
368
- if (argc < 2)
369
- throw new ExpressionError("percentile() requires at least 2 arguments (p, v1, [v2, ...])");
370
- break;
371
- case "min":
372
- if (argc < 1)
373
- throw new ExpressionError("min() requires at least 1 argument");
374
- break;
375
- case "max":
376
- if (argc < 1)
377
- throw new ExpressionError("max() requires at least 1 argument");
378
- break;
379
- case "ceil":
380
- case "floor":
381
- if (argc !== 1)
382
- throw new ExpressionError(`${name}() requires exactly 1 argument`);
383
- break;
384
- case "round":
385
- if (argc !== 1 && argc !== 2)
386
- throw new ExpressionError("round() requires 1 or 2 arguments: round(x[, ndigits])");
387
- break;
388
- default:
389
- break;
390
- }
391
- }
392
- function validateCalls(node) {
393
- if (node.type === "call") {
394
- checkCallArity(node.name, node.args.length);
395
- }
396
- for (const child of children(node))
397
- validateCalls(child);
398
- }
399
- // ── Validation ─────────────────────────────────────────────────────────────
400
- function collectVariables(node) {
401
- const seen = new Set();
402
- function walk(n) {
403
- if (n.type === "identifier" && !ALLOWED_FUNCTIONS.has(n.name)) {
404
- seen.add(n.name);
405
- }
406
- for (const child of children(n))
407
- walk(child);
408
- }
409
- walk(node);
410
- return seen;
411
- }
412
- function children(n) {
413
- switch (n.type) {
414
- case "binary":
415
- return [n.left, n.right];
416
- case "unary":
417
- return [n.operand];
418
- case "call":
419
- return n.args;
420
- case "ternary":
421
- return [n.cond, n.then, n.else];
422
- case "comparison":
423
- return [n.left, n.right];
424
- case "boolean":
425
- return [n.left, n.right];
426
- case "number":
427
- case "identifier":
428
- return [];
12
+ function parseExpression(source, trailingTokenMessage) {
13
+ const parser = ExpressionParser.fromSource(source);
14
+ const node = parser.parse();
15
+ if (!parser.isAtEnd()) {
16
+ throw new ExpressionError(trailingTokenMessage.replace("{token}", parser.peek()?.value ?? "EOF"));
429
17
  }
18
+ return node;
430
19
  }
431
- // ── Public API ─────────────────────────────────────────────────────────────
432
20
  /**
433
- * Validate that an expression string is safe and syntactically valid.
434
- *
435
- * @param expr The expression source.
436
- * @param knownVariables Optional set of metric variable names allowed in the
437
- * expression. When provided (config-load passes the metric set from
438
- * `PricingEngine.buildVariables`), any identifier not in this set and not an
439
- * allowed function is rejected (M5) — typos fail at load, not at runtime.
21
+ * Validate that an expression is safe, syntactically valid, and refers only
22
+ * to the optional set of known metric variables.
440
23
  */
441
- export function validateExpression(expr, knownVariables) {
24
+ export function validateExpression(expression, knownVariables) {
442
25
  try {
443
- const tokens = tokenize(expr);
444
- const parser = new Parser(tokens);
445
- const node = parser.parse();
446
- if (!parser.isAtEnd()) {
447
- throw new ExpressionError(`unexpected token after expression: '${parser.peek()?.value}'`);
448
- }
26
+ const node = parseExpression(expression, "unexpected token after expression: '{token}'");
449
27
  validateCalls(node);
450
- const vars = collectVariables(node);
451
- if (vars.size === 0) {
28
+ const variables = collectVariables(node);
29
+ if (variables.size === 0) {
452
30
  throw new ExpressionError("expression references no variables -- must use at least one metric");
453
31
  }
454
32
  if (knownVariables !== undefined) {
455
33
  const known = knownVariables instanceof Set ? knownVariables : new Set(knownVariables);
456
- for (const name of vars) {
457
- if (!known.has(name)) {
34
+ for (const name of variables) {
35
+ if (!known.has(name))
458
36
  throw new ExpressionError(`unknown variable: '${name}'`);
459
- }
460
37
  }
461
38
  }
462
39
  }
463
- catch (e) {
464
- if (e instanceof ExpressionError)
465
- throw e;
466
- throw new ExpressionError(`invalid expression: ${e.message}`);
40
+ catch (error) {
41
+ if (error instanceof ExpressionError)
42
+ throw error;
43
+ throw new ExpressionError(`invalid expression: ${error.message}`);
467
44
  }
468
45
  }
469
46
  /**
470
- * Safely evaluate a validated expression. Returns an exact `Decimal` — never a
471
- * binary-float `number` — so credit math is precise and parity-stable with the
472
- * Python `decimal.Decimal` engine.
47
+ * Safely evaluate a validated expression using exact Decimal arithmetic.
473
48
  */
474
- export function evaluateExpression(expr, variables) {
49
+ export function evaluateExpression(expression, variables) {
475
50
  if (!variables || typeof variables !== "object") {
476
51
  throw new ExpressionError("variables must be a dict");
477
52
  }
478
53
  if (Object.keys(variables).length === 0) {
479
54
  throw new ExpressionError("cannot evaluate: variables dict is empty");
480
55
  }
481
- let tokens;
56
+ let node;
482
57
  try {
483
- tokens = tokenize(expr);
58
+ node = parseExpression(expression, "unexpected token: '{token}'");
484
59
  }
485
- catch (e) {
486
- if (e instanceof ExpressionError)
487
- throw e;
488
- throw new ExpressionError(`syntax error: ${e.message}`);
489
- }
490
- const parser = new Parser(tokens);
491
- const node = parser.parse();
492
- if (!parser.isAtEnd()) {
493
- throw new ExpressionError(`unexpected token: '${parser.peek()?.value}'`);
60
+ catch (error) {
61
+ if (error instanceof ExpressionError)
62
+ throw error;
63
+ throw new ExpressionError(`syntax error: ${error.message}`);
494
64
  }
495
- // Arity/range validation (parity with Python, which validates on every eval).
496
65
  validateCalls(node);
497
- // Validate undefined variables using OWN-property checks only (C6). The `in`
498
- // operator walks the prototype chain, so identifiers like __proto__,
499
- // constructor, prototype, toString, hasOwnProperty would otherwise resolve to
500
- // inherited members. Object.prototype.hasOwnProperty.call rejects all of them.
501
- function checkVars(n) {
502
- if (n.type === "identifier" &&
503
- !ALLOWED_FUNCTIONS.has(n.name) &&
504
- !Object.prototype.hasOwnProperty.call(variables, n.name)) {
505
- throw new ExpressionError(`undefined variable: '${n.name}'`);
506
- }
507
- for (const child of children(n))
508
- checkVars(child);
509
- }
510
- checkVars(node);
66
+ validateVariables(node, variables);
511
67
  const result = evaluateNode(node, variables);
512
- // Guard non-finite results (C7): NaN/Infinity must never become a charge.
513
68
  if (!result.isFinite()) {
514
69
  throw new ExpressionError(`expression evaluated to a non-finite value: ${result.toString()}`);
515
70
  }
516
71
  return result;
517
72
  }
518
- const ZERO = new Decimal(0);
519
- const ONE = new Decimal(1);
520
- function toDecimal(v) {
521
- return v instanceof Decimal ? v : new Decimal(v);
522
- }
523
- function truthy(d) {
524
- return !d.isZero();
525
- }
526
- function evaluateNode(node, vars) {
527
- switch (node.type) {
528
- case "number":
529
- // Parse from the exact literal string (never via binary float).
530
- return new Decimal(node.value);
531
- case "identifier": {
532
- // Own-property lookup only (C6); undefined identifiers were already
533
- // rejected by checkVars, but guard defensively here too.
534
- if (!Object.prototype.hasOwnProperty.call(vars, node.name)) {
535
- return ZERO;
536
- }
537
- return toDecimal(vars[node.name]);
538
- }
539
- case "unary": {
540
- const v = evaluateNode(node.operand, vars);
541
- if (node.op === "not")
542
- return truthy(v) ? ZERO : ONE;
543
- return node.op === "-" ? v.negated() : v;
544
- }
545
- case "binary": {
546
- const l = evaluateNode(node.left, vars);
547
- const r = evaluateNode(node.right, vars);
548
- switch (node.op) {
549
- case "+":
550
- return l.plus(r);
551
- case "-":
552
- return l.minus(r);
553
- case "*":
554
- return l.times(r);
555
- case "/":
556
- if (r.isZero())
557
- throw new ExpressionError("division by zero");
558
- return l.dividedBy(r);
559
- case "//":
560
- if (r.isZero())
561
- throw new ExpressionError("division by zero");
562
- // Floor division — matches Python's // and JS Math.floor(l / r).
563
- return l.dividedBy(r).floor();
564
- case "%":
565
- if (r.isZero())
566
- throw new ExpressionError("modulo by zero");
567
- return l.modulo(r);
568
- default:
569
- throw new ExpressionError(`unknown operator: ${node.op}`);
570
- }
571
- }
572
- case "call": {
573
- const args = node.args.map((a) => evaluateNode(a, vars));
574
- switch (node.name) {
575
- case "ceil":
576
- return args[0].ceil();
577
- case "floor":
578
- return args[0].floor();
579
- case "min":
580
- if (args.length < 1)
581
- throw new ExpressionError("min() requires at least 1 argument");
582
- return Decimal.min(...args);
583
- case "max":
584
- if (args.length < 1)
585
- throw new ExpressionError("max() requires at least 1 argument");
586
- return Decimal.max(...args);
587
- case "round": {
588
- if (args.length !== 1 && args.length !== 2)
589
- throw new ExpressionError("round() requires 1 or 2 arguments: round(x[, ndigits])");
590
- const ndigits = args.length === 2 ? args[1].toNumber() : 0;
591
- return args[0].toDecimalPlaces(ndigits, Decimal.ROUND_HALF_UP);
592
- }
593
- case "if":
594
- if (args.length !== 3)
595
- throw new ExpressionError("if() requires exactly 3 arguments: if(condition, then, else)");
596
- return truthy(args[0]) ? args[1] : args[2];
597
- case "tier": {
598
- if (args.length < 4 || args.length % 2 !== 0)
599
- throw new ExpressionError("tier() requires an even number of arguments >= 4 (value, threshold, rate, ..., default)");
600
- const val = args[0];
601
- for (let i = 1; i < args.length - 1; i += 2) {
602
- if (val.lessThan(args[i]))
603
- return args[i + 1];
604
- }
605
- return args[args.length - 1]; // default
606
- }
607
- case "clamp":
608
- if (args.length !== 3)
609
- throw new ExpressionError("clamp() requires exactly 3 arguments: clamp(x, min, max)");
610
- return Decimal.max(args[1], Decimal.min(args[0], args[2]));
611
- case "percentile": {
612
- if (args.length < 2)
613
- throw new ExpressionError("percentile() requires at least 2 arguments (p, v1, [v2, ...])");
614
- const p = args[0];
615
- if (p.lessThan(0) || p.greaterThan(100))
616
- throw new ExpressionError("percentile() p must be between 0 and 100");
617
- const sorted = args.slice(1).sort((a, b) => a.comparedTo(b));
618
- const n = sorted.length;
619
- if (n === 1)
620
- return sorted[0];
621
- // rank = p/100 * (n - 1); linear interpolation between neighbours.
622
- const rank = p.dividedBy(100).times(n - 1);
623
- const lower = rank.floor();
624
- const lowerIdx = lower.toNumber();
625
- const upperIdx = Math.min(lowerIdx + 1, n - 1);
626
- const frac = rank.minus(lower);
627
- return sorted[lowerIdx].times(ONE.minus(frac)).plus(sorted[upperIdx].times(frac));
628
- }
629
- default:
630
- throw new ExpressionError(`disallowed function: ${node.name}`);
631
- }
632
- }
633
- case "ternary":
634
- return truthy(evaluateNode(node.cond, vars))
635
- ? evaluateNode(node.then, vars)
636
- : evaluateNode(node.else, vars);
637
- case "comparison": {
638
- const l = evaluateNode(node.left, vars);
639
- const r = evaluateNode(node.right, vars);
640
- switch (node.op) {
641
- case "==":
642
- return l.equals(r) ? ONE : ZERO;
643
- case "!=":
644
- return l.equals(r) ? ZERO : ONE;
645
- case "<":
646
- return l.lessThan(r) ? ONE : ZERO;
647
- case "<=":
648
- return l.lessThanOrEqualTo(r) ? ONE : ZERO;
649
- case ">":
650
- return l.greaterThan(r) ? ONE : ZERO;
651
- case ">=":
652
- return l.greaterThanOrEqualTo(r) ? ONE : ZERO;
653
- case "in":
654
- return l.toString().includes(r.toString()) ? ONE : ZERO;
655
- case "not in":
656
- return l.toString().includes(r.toString()) ? ZERO : ONE;
657
- default:
658
- throw new ExpressionError(`unknown comparison: ${node.op}`);
659
- }
660
- }
661
- case "boolean": {
662
- const l = evaluateNode(node.left, vars);
663
- switch (node.op) {
664
- case "and":
665
- // H4 fix: return operand (Python short-circuit semantics), not 0/1.
666
- // (5 and 7) → 7; (0 and 7) → 0; (5 and 0) → 0.
667
- if (!truthy(l))
668
- return l;
669
- return evaluateNode(node.right, vars);
670
- case "or":
671
- // (5 or 7) → 5; (0 or 7) → 7; (0 or 0) → 0.
672
- if (truthy(l))
673
- return l;
674
- return evaluateNode(node.right, vars);
675
- default:
676
- throw new ExpressionError(`unknown boolean op: ${node.op}`);
677
- }
678
- }
73
+ function validateVariables(node, variables) {
74
+ if (node.type === "identifier" &&
75
+ !ALLOWED_FUNCTIONS.has(node.name) &&
76
+ !Object.prototype.hasOwnProperty.call(variables, node.name)) {
77
+ throw new ExpressionError(`undefined variable: '${node.name}'`);
679
78
  }
79
+ for (const child of children(node))
80
+ validateVariables(child, variables);
680
81
  }
681
82
  //# sourceMappingURL=expr.js.map