@zonastery/bursar 0.0.2 → 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.
- package/README.md +85 -344
- package/dist/billing/auto-recharge-port.d.ts +27 -0
- package/dist/billing/auto-recharge-port.d.ts.map +1 -0
- package/dist/billing/auto-recharge-port.js +2 -0
- package/dist/billing/auto-recharge-port.js.map +1 -0
- package/dist/billing/auto-recharge-service.d.ts +43 -0
- package/dist/billing/auto-recharge-service.d.ts.map +1 -0
- package/dist/billing/auto-recharge-service.js +218 -0
- package/dist/billing/auto-recharge-service.js.map +1 -0
- package/dist/billing/billing-service.d.ts +71 -0
- package/dist/billing/billing-service.d.ts.map +1 -0
- package/dist/billing/billing-service.js +172 -0
- package/dist/billing/billing-service.js.map +1 -0
- package/dist/billing/billing-store.d.ts +45 -13
- package/dist/billing/billing-store.d.ts.map +1 -1
- package/dist/billing/billing-store.js.map +1 -1
- package/dist/billing/contracts.d.ts +149 -0
- package/dist/billing/contracts.d.ts.map +1 -0
- package/dist/billing/contracts.js +2 -0
- package/dist/billing/contracts.js.map +1 -0
- package/dist/billing/event-handlers.d.ts +57 -0
- package/dist/billing/event-handlers.d.ts.map +1 -0
- package/dist/billing/event-handlers.js +667 -0
- package/dist/billing/event-handlers.js.map +1 -0
- package/dist/billing/event-processor.d.ts +19 -0
- package/dist/billing/event-processor.d.ts.map +1 -0
- package/dist/billing/event-processor.js +105 -0
- package/dist/billing/event-processor.js.map +1 -0
- package/dist/billing/financial-event-handlers.d.ts +26 -0
- package/dist/billing/financial-event-handlers.d.ts.map +1 -0
- package/dist/billing/financial-event-handlers.js +241 -0
- package/dist/billing/financial-event-handlers.js.map +1 -0
- package/dist/billing/index.d.ts +8 -5
- package/dist/billing/index.d.ts.map +1 -1
- package/dist/billing/index.js +4 -4
- package/dist/billing/index.js.map +1 -1
- package/dist/billing/management.d.ts +59 -0
- package/dist/billing/management.d.ts.map +1 -0
- package/dist/billing/management.js +113 -0
- package/dist/billing/management.js.map +1 -0
- package/dist/billing/policy-window.d.ts +24 -0
- package/dist/billing/policy-window.d.ts.map +1 -0
- package/dist/billing/policy-window.js +88 -0
- package/dist/billing/policy-window.js.map +1 -0
- package/dist/billing/postgres/repositories/auto-recharge.d.ts +32 -0
- package/dist/billing/postgres/repositories/auto-recharge.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/auto-recharge.js +178 -0
- package/dist/billing/postgres/repositories/auto-recharge.js.map +1 -0
- package/dist/billing/postgres/repositories/customer.d.ts +12 -0
- package/dist/billing/postgres/repositories/customer.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/customer.js +33 -0
- package/dist/billing/postgres/repositories/customer.js.map +1 -0
- package/dist/billing/postgres/repositories/dispute.d.ts +7 -0
- package/dist/billing/postgres/repositories/dispute.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/dispute.js +18 -0
- package/dist/billing/postgres/repositories/dispute.js.map +1 -0
- package/dist/billing/postgres/repositories/event.d.ts +21 -0
- package/dist/billing/postgres/repositories/event.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/event.js +47 -0
- package/dist/billing/postgres/repositories/event.js.map +1 -0
- package/dist/billing/postgres/repositories/invoice.d.ts +9 -0
- package/dist/billing/postgres/repositories/invoice.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/invoice.js +36 -0
- package/dist/billing/postgres/repositories/invoice.js.map +1 -0
- package/dist/billing/postgres/repositories/offer.d.ts +26 -0
- package/dist/billing/postgres/repositories/offer.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/offer.js +73 -0
- package/dist/billing/postgres/repositories/offer.js.map +1 -0
- package/dist/billing/postgres/repositories/payment.d.ts +32 -0
- package/dist/billing/postgres/repositories/payment.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/payment.js +78 -0
- package/dist/billing/postgres/repositories/payment.js.map +1 -0
- package/dist/billing/postgres/repositories/preferences.d.ts +15 -0
- package/dist/billing/postgres/repositories/preferences.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/preferences.js +23 -0
- package/dist/billing/postgres/repositories/preferences.js.map +1 -0
- package/dist/billing/postgres/repositories/refund.d.ts +7 -0
- package/dist/billing/postgres/repositories/refund.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/refund.js +32 -0
- package/dist/billing/postgres/repositories/refund.js.map +1 -0
- package/dist/billing/postgres/repositories/subscription.d.ts +46 -0
- package/dist/billing/postgres/repositories/subscription.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/subscription.js +187 -0
- package/dist/billing/postgres/repositories/subscription.js.map +1 -0
- package/dist/billing/postgres/repositories/topup.d.ts +27 -0
- package/dist/billing/postgres/repositories/topup.d.ts.map +1 -0
- package/dist/billing/postgres/repositories/topup.js +51 -0
- package/dist/billing/postgres/repositories/topup.js.map +1 -0
- package/dist/billing/postgres/store.d.ts +87 -0
- package/dist/billing/postgres/store.d.ts.map +1 -0
- package/dist/billing/postgres/store.js +527 -0
- package/dist/billing/postgres/store.js.map +1 -0
- package/dist/billing/service-types.d.ts +37 -0
- package/dist/billing/service-types.d.ts.map +1 -0
- package/dist/billing/service-types.js +2 -0
- package/dist/billing/service-types.js.map +1 -0
- package/dist/billing/types/auto-recharge.d.ts +56 -0
- package/dist/billing/types/auto-recharge.d.ts.map +1 -0
- package/dist/billing/types/auto-recharge.js +2 -0
- package/dist/billing/types/auto-recharge.js.map +1 -0
- package/dist/billing/types/catalog.d.ts +47 -0
- package/dist/billing/types/catalog.d.ts.map +1 -0
- package/dist/billing/types/catalog.js +2 -0
- package/dist/billing/types/catalog.js.map +1 -0
- package/dist/billing/types/common.d.ts +16 -0
- package/dist/billing/types/common.d.ts.map +1 -0
- package/dist/billing/types/common.js +2 -0
- package/dist/billing/types/common.js.map +1 -0
- package/dist/billing/types/documents.d.ts +39 -0
- package/dist/billing/types/documents.d.ts.map +1 -0
- package/dist/billing/types/documents.js +2 -0
- package/dist/billing/types/documents.js.map +1 -0
- package/dist/billing/types/events.d.ts +75 -0
- package/dist/billing/types/events.d.ts.map +1 -0
- package/dist/billing/types/events.js +38 -0
- package/dist/billing/types/events.js.map +1 -0
- package/dist/billing/types/index.d.ts +8 -0
- package/dist/billing/types/index.d.ts.map +1 -0
- package/dist/billing/types/index.js +8 -0
- package/dist/billing/types/index.js.map +1 -0
- package/dist/billing/types/preferences.d.ts +13 -0
- package/dist/billing/types/preferences.d.ts.map +1 -0
- package/dist/billing/types/preferences.js +2 -0
- package/dist/billing/types/preferences.js.map +1 -0
- package/dist/billing/types/subscriptions.d.ts +97 -0
- package/dist/billing/types/subscriptions.d.ts.map +1 -0
- package/dist/billing/types/subscriptions.js +12 -0
- package/dist/billing/types/subscriptions.js.map +1 -0
- package/dist/breakdown.d.ts +3 -1
- package/dist/breakdown.d.ts.map +1 -1
- package/dist/breakdown.js +4 -1
- package/dist/breakdown.js.map +1 -1
- package/dist/browser.d.ts +8 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +7 -0
- package/dist/browser.js.map +1 -0
- package/dist/bursar.d.ts +80 -0
- package/dist/bursar.d.ts.map +1 -0
- package/dist/bursar.js +129 -0
- package/dist/bursar.js.map +1 -0
- package/dist/catalog.d.ts +61 -0
- package/dist/catalog.d.ts.map +1 -0
- package/dist/catalog.js +95 -0
- package/dist/catalog.js.map +1 -0
- package/dist/commerce/errors.d.ts +69 -0
- package/dist/commerce/errors.d.ts.map +1 -0
- package/dist/commerce/errors.js +107 -0
- package/dist/commerce/errors.js.map +1 -0
- package/dist/commerce/index.d.ts +6 -0
- package/dist/commerce/index.d.ts.map +1 -0
- package/dist/commerce/index.js +6 -0
- package/dist/commerce/index.js.map +1 -0
- package/dist/commerce/plan-change.d.ts +10 -0
- package/dist/commerce/plan-change.d.ts.map +1 -0
- package/dist/commerce/plan-change.js +42 -0
- package/dist/commerce/plan-change.js.map +1 -0
- package/dist/commerce/provider-registry.d.ts +18 -0
- package/dist/commerce/provider-registry.d.ts.map +1 -0
- package/dist/commerce/provider-registry.js +70 -0
- package/dist/commerce/provider-registry.js.map +1 -0
- package/dist/commerce/service.d.ts +73 -0
- package/dist/commerce/service.d.ts.map +1 -0
- package/dist/commerce/service.js +981 -0
- package/dist/commerce/service.js.map +1 -0
- package/dist/commerce/types.d.ts +217 -0
- package/dist/commerce/types.d.ts.map +1 -0
- package/dist/commerce/types.js.map +1 -0
- package/dist/config/parse-access.d.ts +6 -0
- package/dist/config/parse-access.d.ts.map +1 -0
- package/dist/config/parse-access.js +72 -0
- package/dist/config/parse-access.js.map +1 -0
- package/dist/config/parse-commerce.d.ts +3 -0
- package/dist/config/parse-commerce.d.ts.map +1 -0
- package/dist/config/parse-commerce.js +236 -0
- package/dist/config/parse-commerce.js.map +1 -0
- package/dist/config/parse-credits.d.ts +3 -0
- package/dist/config/parse-credits.d.ts.map +1 -0
- package/dist/config/parse-credits.js +99 -0
- package/dist/config/parse-credits.js.map +1 -0
- package/dist/config/parse-plans.d.ts +5 -0
- package/dist/config/parse-plans.d.ts.map +1 -0
- package/dist/config/parse-plans.js +126 -0
- package/dist/config/parse-plans.js.map +1 -0
- package/dist/config/parse-pricing.d.ts +4 -0
- package/dist/config/parse-pricing.d.ts.map +1 -0
- package/dist/config/parse-pricing.js +252 -0
- package/dist/config/parse-pricing.js.map +1 -0
- package/dist/config/parse-utils.d.ts +18 -0
- package/dist/config/parse-utils.d.ts.map +1 -0
- package/dist/config/parse-utils.js +118 -0
- package/dist/config/parse-utils.js.map +1 -0
- package/dist/config/types.d.ts +327 -0
- package/dist/config/types.d.ts.map +1 -0
- package/dist/config/types.js +2 -0
- package/dist/config/types.js.map +1 -0
- package/dist/config.d.ts +6 -23
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +86 -385
- package/dist/config.js.map +1 -1
- package/dist/{stores → credits}/events.d.ts +6 -6
- package/dist/credits/events.d.ts.map +1 -0
- package/dist/{stores → credits}/events.js +5 -5
- package/dist/credits/events.js.map +1 -0
- package/dist/credits/lease-workflow.d.ts +90 -0
- package/dist/credits/lease-workflow.d.ts.map +1 -0
- package/dist/credits/lease-workflow.js +378 -0
- package/dist/credits/lease-workflow.js.map +1 -0
- package/dist/credits/low-balance-monitor.d.ts +26 -0
- package/dist/credits/low-balance-monitor.d.ts.map +1 -0
- package/dist/credits/low-balance-monitor.js +110 -0
- package/dist/credits/low-balance-monitor.js.map +1 -0
- package/dist/credits/postgres/repositories/analytics.d.ts +77 -0
- package/dist/credits/postgres/repositories/analytics.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/analytics.js +151 -0
- package/dist/credits/postgres/repositories/analytics.js.map +1 -0
- package/dist/credits/postgres/repositories/balance.d.ts +66 -0
- package/dist/credits/postgres/repositories/balance.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/balance.js +139 -0
- package/dist/credits/postgres/repositories/balance.js.map +1 -0
- package/dist/credits/postgres/repositories/bucket.d.ts +25 -0
- package/dist/credits/postgres/repositories/bucket.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/bucket.js +52 -0
- package/dist/credits/postgres/repositories/bucket.js.map +1 -0
- package/dist/credits/postgres/repositories/deduction.d.ts +55 -0
- package/dist/credits/postgres/repositories/deduction.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/deduction.js +130 -0
- package/dist/credits/postgres/repositories/deduction.js.map +1 -0
- package/dist/credits/postgres/repositories/lease.d.ts +69 -0
- package/dist/credits/postgres/repositories/lease.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/lease.js +148 -0
- package/dist/credits/postgres/repositories/lease.js.map +1 -0
- package/dist/credits/postgres/repositories/plan.d.ts +105 -0
- package/dist/credits/postgres/repositories/plan.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/plan.js +182 -0
- package/dist/credits/postgres/repositories/plan.js.map +1 -0
- package/dist/credits/postgres/repositories/pricing.d.ts +28 -0
- package/dist/credits/postgres/repositories/pricing.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/pricing.js +70 -0
- package/dist/credits/postgres/repositories/pricing.js.map +1 -0
- package/dist/credits/postgres/repositories/team.d.ts +63 -0
- package/dist/credits/postgres/repositories/team.d.ts.map +1 -0
- package/dist/credits/postgres/repositories/team.js +120 -0
- package/dist/credits/postgres/repositories/team.js.map +1 -0
- package/dist/credits/postgres/store.d.ts +84 -0
- package/dist/credits/postgres/store.d.ts.map +1 -0
- package/dist/credits/postgres/store.js +765 -0
- package/dist/credits/postgres/store.js.map +1 -0
- package/dist/credits/postgres/value-mappers.d.ts +17 -0
- package/dist/credits/postgres/value-mappers.d.ts.map +1 -0
- package/dist/credits/postgres/value-mappers.js +98 -0
- package/dist/credits/postgres/value-mappers.js.map +1 -0
- package/dist/credits/pricing-runtime.d.ts +34 -0
- package/dist/credits/pricing-runtime.d.ts.map +1 -0
- package/dist/credits/pricing-runtime.js +141 -0
- package/dist/credits/pricing-runtime.js.map +1 -0
- package/dist/credits/queries.d.ts +33 -0
- package/dist/credits/queries.d.ts.map +1 -0
- package/dist/credits/queries.js +71 -0
- package/dist/credits/queries.js.map +1 -0
- package/dist/credits/service-errors.d.ts +4 -0
- package/dist/credits/service-errors.d.ts.map +1 -0
- package/dist/credits/service-errors.js +57 -0
- package/dist/credits/service-errors.js.map +1 -0
- package/dist/credits/service-types.d.ts +122 -0
- package/dist/credits/service-types.d.ts.map +1 -0
- package/dist/credits/service-types.js +2 -0
- package/dist/credits/service-types.js.map +1 -0
- package/dist/credits/service.d.ts +207 -0
- package/dist/credits/service.d.ts.map +1 -0
- package/dist/credits/service.js +594 -0
- package/dist/credits/service.js.map +1 -0
- package/dist/credits/store.d.ts +167 -0
- package/dist/credits/store.d.ts.map +1 -0
- package/dist/{stores/credit-store.js → credits/store.js} +23 -43
- package/dist/credits/store.js.map +1 -0
- package/dist/credits/types/account.d.ts +82 -0
- package/dist/credits/types/account.d.ts.map +1 -0
- package/dist/credits/types/account.js +2 -0
- package/dist/credits/types/account.js.map +1 -0
- package/dist/credits/types/admission.d.ts +39 -0
- package/dist/credits/types/admission.d.ts.map +1 -0
- package/dist/credits/types/admission.js +2 -0
- package/dist/credits/types/admission.js.map +1 -0
- package/dist/credits/types/analytics.d.ts +52 -0
- package/dist/credits/types/analytics.d.ts.map +1 -0
- package/dist/credits/types/analytics.js +2 -0
- package/dist/credits/types/analytics.js.map +1 -0
- package/dist/credits/types/catalog.d.ts +69 -0
- package/dist/credits/types/catalog.d.ts.map +1 -0
- package/dist/credits/types/catalog.js +2 -0
- package/dist/credits/types/catalog.js.map +1 -0
- package/dist/credits/types/grants.d.ts +24 -0
- package/dist/credits/types/grants.d.ts.map +1 -0
- package/dist/credits/types/grants.js +2 -0
- package/dist/credits/types/grants.js.map +1 -0
- package/dist/credits/types/index.d.ts +9 -0
- package/dist/credits/types/index.d.ts.map +1 -0
- package/dist/credits/types/index.js +2 -0
- package/dist/credits/types/index.js.map +1 -0
- package/dist/credits/types/ledger.d.ts +77 -0
- package/dist/credits/types/ledger.d.ts.map +1 -0
- package/dist/credits/types/ledger.js +2 -0
- package/dist/credits/types/ledger.js.map +1 -0
- package/dist/credits/types/quotas.d.ts +40 -0
- package/dist/credits/types/quotas.d.ts.map +1 -0
- package/dist/credits/types/quotas.js +2 -0
- package/dist/credits/types/quotas.js.map +1 -0
- package/dist/credits/types/teams.d.ts +44 -0
- package/dist/credits/types/teams.d.ts.map +1 -0
- package/dist/credits/types/teams.js +2 -0
- package/dist/credits/types/teams.js.map +1 -0
- package/dist/engine.d.ts +18 -39
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +169 -145
- package/dist/engine.js.map +1 -1
- package/dist/errors.d.ts +84 -25
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +117 -24
- package/dist/errors.js.map +1 -1
- package/dist/expr/ast.d.ts +46 -0
- package/dist/expr/ast.d.ts.map +1 -0
- package/dist/expr/ast.js +18 -0
- package/dist/expr/ast.js.map +1 -0
- package/dist/expr/evaluator.d.ts +4 -0
- package/dist/expr/evaluator.d.ts.map +1 -0
- package/dist/expr/evaluator.js +144 -0
- package/dist/expr/evaluator.js.map +1 -0
- package/dist/expr/language.d.ts +5 -0
- package/dist/expr/language.d.ts.map +1 -0
- package/dist/expr/language.js +73 -0
- package/dist/expr/language.js.map +1 -0
- package/dist/expr/parser.d.ts +24 -0
- package/dist/expr/parser.d.ts.map +1 -0
- package/dist/expr/parser.js +175 -0
- package/dist/expr/parser.js.map +1 -0
- package/dist/expr/tokenizer.d.ts +7 -0
- package/dist/expr/tokenizer.d.ts.map +1 -0
- package/dist/expr/tokenizer.js +85 -0
- package/dist/expr/tokenizer.js.map +1 -0
- package/dist/expr.d.ts +7 -58
- package/dist/expr.d.ts.map +1 -1
- package/dist/expr.js +41 -640
- package/dist/expr.js.map +1 -1
- package/dist/generated/pricing-config.schema.json +2764 -0
- package/dist/generated/pricing-config.types.d.ts +674 -0
- package/dist/generated/pricing-config.types.d.ts.map +1 -0
- package/dist/generated/pricing-config.types.js +8 -0
- package/dist/generated/pricing-config.types.js.map +1 -0
- package/dist/index.d.ts +21 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -10
- package/dist/index.js.map +1 -1
- package/dist/load-pricing-file.d.ts.map +1 -1
- package/dist/load-pricing-file.js.map +1 -1
- package/dist/metrics.d.ts +6 -16
- package/dist/metrics.d.ts.map +1 -1
- package/dist/node.d.ts +9 -2
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +6 -2
- package/dist/node.js.map +1 -1
- package/dist/providers/_shared.d.ts +9 -0
- package/dist/providers/_shared.d.ts.map +1 -0
- package/dist/providers/_shared.js +25 -0
- package/dist/providers/_shared.js.map +1 -0
- package/dist/providers/dodo/client-contract.d.ts +106 -0
- package/dist/providers/dodo/client-contract.d.ts.map +1 -0
- package/dist/providers/dodo/client-contract.js +2 -0
- package/dist/providers/dodo/client-contract.js.map +1 -0
- package/dist/providers/dodo/event-mapper.d.ts +5 -3
- package/dist/providers/dodo/event-mapper.d.ts.map +1 -1
- package/dist/providers/dodo/event-mapper.js +213 -38
- package/dist/providers/dodo/event-mapper.js.map +1 -1
- package/dist/providers/dodo/index.d.ts +1 -0
- package/dist/providers/dodo/index.d.ts.map +1 -1
- package/dist/providers/dodo/index.js.map +1 -1
- package/dist/providers/dodo/provider.d.ts +24 -14
- package/dist/providers/dodo/provider.d.ts.map +1 -1
- package/dist/providers/dodo/provider.js +249 -31
- package/dist/providers/dodo/provider.js.map +1 -1
- package/dist/providers/index.d.ts +3 -1
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +1 -0
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/mock/provider.d.ts +14 -10
- package/dist/providers/mock/provider.d.ts.map +1 -1
- package/dist/providers/mock/provider.js +78 -10
- package/dist/providers/mock/provider.js.map +1 -1
- package/dist/providers/stripe/event-mapper.d.ts +3 -3
- package/dist/providers/stripe/event-mapper.d.ts.map +1 -1
- package/dist/providers/stripe/event-mapper.js +139 -30
- package/dist/providers/stripe/event-mapper.js.map +1 -1
- package/dist/providers/stripe/provider.d.ts +21 -12
- package/dist/providers/stripe/provider.d.ts.map +1 -1
- package/dist/providers/stripe/provider.js +175 -17
- package/dist/providers/stripe/provider.js.map +1 -1
- package/dist/providers/types.d.ts +128 -32
- package/dist/providers/types.d.ts.map +1 -1
- package/dist/providers/types.js +17 -1
- package/dist/providers/types.js.map +1 -1
- package/dist/retry.d.ts +13 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +28 -0
- package/dist/retry.js.map +1 -0
- package/dist/shared/logger.d.ts +10 -0
- package/dist/shared/logger.d.ts.map +1 -0
- package/dist/shared/logger.js +15 -0
- package/dist/shared/logger.js.map +1 -0
- package/dist/shared/postgres-client.d.ts +51 -0
- package/dist/shared/postgres-client.d.ts.map +1 -0
- package/dist/shared/postgres-client.js +101 -0
- package/dist/shared/postgres-client.js.map +1 -0
- package/dist/shared/postgres-types.d.ts +7 -0
- package/dist/shared/postgres-types.d.ts.map +1 -0
- package/dist/shared/postgres-types.js +2 -0
- package/dist/shared/postgres-types.js.map +1 -0
- package/dist/shared/postgres-validation.d.ts +8 -0
- package/dist/shared/postgres-validation.d.ts.map +1 -0
- package/dist/shared/postgres-validation.js +40 -0
- package/dist/shared/postgres-validation.js.map +1 -0
- package/dist/storage/adapters/clickhouse.d.ts +61 -0
- package/dist/storage/adapters/clickhouse.d.ts.map +1 -0
- package/dist/storage/adapters/clickhouse.js +329 -0
- package/dist/storage/adapters/clickhouse.js.map +1 -0
- package/dist/storage/adapters/s3.d.ts +35 -0
- package/dist/storage/adapters/s3.d.ts.map +1 -0
- package/dist/storage/adapters/s3.js +100 -0
- package/dist/storage/adapters/s3.js.map +1 -0
- package/dist/storage/outbox-worker.d.ts +40 -0
- package/dist/storage/outbox-worker.d.ts.map +1 -0
- package/dist/storage/outbox-worker.js +132 -0
- package/dist/storage/outbox-worker.js.map +1 -0
- package/dist/storage/ports.d.ts +77 -0
- package/dist/storage/ports.d.ts.map +1 -0
- package/dist/storage/ports.js +2 -0
- package/dist/storage/ports.js.map +1 -0
- package/dist/storage/postgres-repository.d.ts +14 -0
- package/dist/storage/postgres-repository.d.ts.map +1 -0
- package/dist/storage/postgres-repository.js +130 -0
- package/dist/storage/postgres-repository.js.map +1 -0
- package/dist/storage/runtime.d.ts +67 -0
- package/dist/storage/runtime.d.ts.map +1 -0
- package/dist/storage/runtime.js +313 -0
- package/dist/storage/runtime.js.map +1 -0
- package/package.json +36 -20
- package/dist/allowance.d.ts +0 -48
- package/dist/allowance.d.ts.map +0 -1
- package/dist/allowance.js +0 -140
- package/dist/allowance.js.map +0 -1
- package/dist/billing/billing-manager.d.ts +0 -66
- package/dist/billing/billing-manager.d.ts.map +0 -1
- package/dist/billing/billing-manager.js +0 -560
- package/dist/billing/billing-manager.js.map +0 -1
- package/dist/billing/billing-types.d.ts +0 -203
- package/dist/billing/billing-types.d.ts.map +0 -1
- package/dist/billing/billing-types.js +0 -6
- package/dist/billing/billing-types.js.map +0 -1
- package/dist/billing/memory-billing-store.d.ts +0 -37
- package/dist/billing/memory-billing-store.d.ts.map +0 -1
- package/dist/billing/memory-billing-store.js +0 -197
- package/dist/billing/memory-billing-store.js.map +0 -1
- package/dist/billing/postgres-billing-store.d.ts +0 -35
- package/dist/billing/postgres-billing-store.d.ts.map +0 -1
- package/dist/billing/postgres-billing-store.js +0 -274
- package/dist/billing/postgres-billing-store.js.map +0 -1
- package/dist/billing/supabase-billing-store.d.ts +0 -29
- package/dist/billing/supabase-billing-store.d.ts.map +0 -1
- package/dist/billing/supabase-billing-store.js +0 -250
- package/dist/billing/supabase-billing-store.js.map +0 -1
- package/dist/manager.d.ts +0 -444
- package/dist/manager.d.ts.map +0 -1
- package/dist/manager.js +0 -1153
- package/dist/manager.js.map +0 -1
- package/dist/stores/credit-store.d.ts +0 -198
- package/dist/stores/credit-store.d.ts.map +0 -1
- package/dist/stores/credit-store.js.map +0 -1
- package/dist/stores/events.d.ts.map +0 -1
- package/dist/stores/events.js.map +0 -1
- package/dist/stores/memory-store.d.ts +0 -187
- package/dist/stores/memory-store.d.ts.map +0 -1
- package/dist/stores/memory-store.js +0 -1890
- package/dist/stores/memory-store.js.map +0 -1
- package/dist/stores/postgres-store.d.ts +0 -100
- package/dist/stores/postgres-store.d.ts.map +0 -1
- package/dist/stores/postgres-store.js +0 -891
- package/dist/stores/postgres-store.js.map +0 -1
- package/dist/stores/supabase-store.d.ts +0 -87
- package/dist/stores/supabase-store.d.ts.map +0 -1
- package/dist/stores/supabase-store.js +0 -948
- package/dist/stores/supabase-store.js.map +0 -1
- package/dist/types.d.ts +0 -463
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- /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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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(
|
|
10
|
+
return value.toDecimalPlaces(MONEY_DECIMAL_PLACES, Decimal.ROUND_HALF_UP);
|
|
10
11
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
|
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(
|
|
24
|
+
export function validateExpression(expression, knownVariables) {
|
|
442
25
|
try {
|
|
443
|
-
const
|
|
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
|
|
451
|
-
if (
|
|
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
|
|
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 (
|
|
464
|
-
if (
|
|
465
|
-
throw
|
|
466
|
-
throw new ExpressionError(`invalid expression: ${
|
|
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
|
|
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(
|
|
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
|
|
56
|
+
let node;
|
|
482
57
|
try {
|
|
483
|
-
|
|
58
|
+
node = parseExpression(expression, "unexpected token: '{token}'");
|
|
484
59
|
}
|
|
485
|
-
catch (
|
|
486
|
-
if (
|
|
487
|
-
throw
|
|
488
|
-
throw new ExpressionError(`syntax error: ${
|
|
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
|
-
|
|
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
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
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
|