@zonastery/bursar 2.0.3 → 2.0.4
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/dist/billing/auto-recharge-port.d.ts +3 -2
- package/dist/billing/auto-recharge-port.d.ts.map +1 -1
- package/dist/billing/auto-recharge-port.js.map +1 -1
- package/dist/billing/auto-recharge-service.js +3 -3
- package/dist/billing/auto-recharge-service.js.map +1 -1
- package/dist/billing/billing-service.d.ts +3 -2
- package/dist/billing/billing-service.d.ts.map +1 -1
- package/dist/billing/billing-service.js +2 -10
- package/dist/billing/billing-service.js.map +1 -1
- package/dist/billing/billing-store.d.ts +6 -6
- 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 +8 -7
- package/dist/billing/contracts.d.ts.map +1 -1
- package/dist/billing/contracts.js.map +1 -1
- package/dist/billing/event-handlers.d.ts +5 -9
- package/dist/billing/event-handlers.d.ts.map +1 -1
- package/dist/billing/event-handlers.js +191 -285
- package/dist/billing/event-handlers.js.map +1 -1
- package/dist/billing/event-processor.d.ts +1 -1
- package/dist/billing/event-processor.d.ts.map +1 -1
- package/dist/billing/event-processor.js +32 -23
- package/dist/billing/event-processor.js.map +1 -1
- package/dist/billing/financial-event-handlers.d.ts +3 -9
- package/dist/billing/financial-event-handlers.d.ts.map +1 -1
- package/dist/billing/financial-event-handlers.js +10 -12
- package/dist/billing/financial-event-handlers.js.map +1 -1
- package/dist/billing/index.d.ts +1 -1
- package/dist/billing/index.d.ts.map +1 -1
- package/dist/billing/index.js.map +1 -1
- package/dist/billing/management.d.ts +3 -2
- package/dist/billing/management.d.ts.map +1 -1
- package/dist/billing/management.js.map +1 -1
- package/dist/billing/postgres/repositories/auto-recharge.d.ts +2 -1
- package/dist/billing/postgres/repositories/auto-recharge.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/auto-recharge.js +58 -40
- package/dist/billing/postgres/repositories/auto-recharge.js.map +1 -1
- package/dist/billing/postgres/repositories/dispute.d.ts +2 -1
- package/dist/billing/postgres/repositories/dispute.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/dispute.js.map +1 -1
- package/dist/billing/postgres/repositories/event.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/event.js.map +1 -1
- package/dist/billing/postgres/repositories/invoice.d.ts +2 -1
- package/dist/billing/postgres/repositories/invoice.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/invoice.js +8 -12
- package/dist/billing/postgres/repositories/invoice.js.map +1 -1
- package/dist/billing/postgres/repositories/offer.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/offer.js +5 -0
- package/dist/billing/postgres/repositories/offer.js.map +1 -1
- package/dist/billing/postgres/repositories/payment.d.ts +1 -1
- package/dist/billing/postgres/repositories/payment.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/payment.js +1 -1
- package/dist/billing/postgres/repositories/payment.js.map +1 -1
- package/dist/billing/postgres/repositories/subscription-change.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/subscription-change.js +1 -9
- package/dist/billing/postgres/repositories/subscription-change.js.map +1 -1
- package/dist/billing/postgres/repositories/subscription.d.ts +6 -6
- package/dist/billing/postgres/repositories/subscription.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/subscription.js +29 -30
- package/dist/billing/postgres/repositories/subscription.js.map +1 -1
- package/dist/billing/postgres/store.d.ts +6 -5
- package/dist/billing/postgres/store.d.ts.map +1 -1
- package/dist/billing/postgres/store.js +15 -13
- package/dist/billing/postgres/store.js.map +1 -1
- package/dist/billing/types/auto-recharge.d.ts +2 -1
- package/dist/billing/types/auto-recharge.d.ts.map +1 -1
- package/dist/billing/types/auto-recharge.js.map +1 -1
- package/dist/billing/types/documents.d.ts +2 -1
- package/dist/billing/types/documents.d.ts.map +1 -1
- package/dist/billing/types/documents.js.map +1 -1
- package/dist/billing/types/events.d.ts +3 -2
- package/dist/billing/types/events.d.ts.map +1 -1
- package/dist/billing/types/events.js +73 -39
- package/dist/billing/types/events.js.map +1 -1
- package/dist/billing/types/subscriptions.d.ts +4 -1
- package/dist/billing/types/subscriptions.d.ts.map +1 -1
- package/dist/billing/types/subscriptions.js.map +1 -1
- package/dist/breakdown.d.ts +3 -2
- package/dist/breakdown.d.ts.map +1 -1
- package/dist/breakdown.js.map +1 -1
- package/dist/bursar.d.ts.map +1 -1
- package/dist/bursar.js +4 -3
- package/dist/bursar.js.map +1 -1
- package/dist/catalog.d.ts.map +1 -1
- package/dist/catalog.js +45 -42
- package/dist/catalog.js.map +1 -1
- package/dist/commerce/provider-registry.d.ts.map +1 -1
- package/dist/commerce/provider-registry.js +11 -6
- package/dist/commerce/provider-registry.js.map +1 -1
- package/dist/commerce/service.d.ts.map +1 -1
- package/dist/commerce/service.js +16 -22
- package/dist/commerce/service.js.map +1 -1
- package/dist/commerce/types.d.ts.map +1 -1
- package/dist/commerce/types.js.map +1 -1
- package/dist/config/parse-access.d.ts +5 -2
- package/dist/config/parse-access.d.ts.map +1 -1
- package/dist/config/parse-access.js +60 -41
- package/dist/config/parse-access.js.map +1 -1
- package/dist/config/parse-commerce.d.ts +2 -1
- package/dist/config/parse-commerce.d.ts.map +1 -1
- package/dist/config/parse-commerce.js +49 -29
- package/dist/config/parse-commerce.js.map +1 -1
- package/dist/config/parse-credits.d.ts +2 -1
- package/dist/config/parse-credits.d.ts.map +1 -1
- package/dist/config/parse-credits.js +39 -23
- package/dist/config/parse-credits.js.map +1 -1
- package/dist/config/parse-plans.d.ts +7 -2
- package/dist/config/parse-plans.d.ts.map +1 -1
- package/dist/config/parse-plans.js +49 -22
- package/dist/config/parse-plans.js.map +1 -1
- package/dist/config/parse-pricing.d.ts +2 -1
- package/dist/config/parse-pricing.d.ts.map +1 -1
- package/dist/config/parse-pricing.js +41 -31
- package/dist/config/parse-pricing.js.map +1 -1
- package/dist/config/parse-utils.d.ts +15 -11
- package/dist/config/parse-utils.d.ts.map +1 -1
- package/dist/config/parse-utils.js +90 -27
- package/dist/config/parse-utils.js.map +1 -1
- package/dist/config/types.d.ts.map +1 -1
- package/dist/config/types.js.map +1 -1
- package/dist/config.d.ts +6 -5
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +65 -35
- package/dist/config.js.map +1 -1
- package/dist/credits/amount.d.ts +1 -1
- package/dist/credits/amount.d.ts.map +1 -1
- package/dist/credits/amount.js +5 -4
- package/dist/credits/amount.js.map +1 -1
- package/dist/credits/catalog-runtime.d.ts.map +1 -1
- package/dist/credits/catalog-runtime.js +2 -3
- package/dist/credits/catalog-runtime.js.map +1 -1
- package/dist/credits/events.d.ts +12 -1
- package/dist/credits/events.d.ts.map +1 -1
- package/dist/credits/events.js +35 -21
- package/dist/credits/events.js.map +1 -1
- package/dist/credits/lease-workflow.d.ts +2 -1
- package/dist/credits/lease-workflow.d.ts.map +1 -1
- package/dist/credits/lease-workflow.js +3 -2
- package/dist/credits/lease-workflow.js.map +1 -1
- package/dist/credits/low-balance-monitor.d.ts +2 -1
- package/dist/credits/low-balance-monitor.d.ts.map +1 -1
- package/dist/credits/low-balance-monitor.js.map +1 -1
- package/dist/credits/postgres/repositories/analytics.d.ts +2 -2
- package/dist/credits/postgres/repositories/analytics.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/analytics.js +14 -6
- package/dist/credits/postgres/repositories/analytics.js.map +1 -1
- package/dist/credits/postgres/repositories/balance.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/balance.js +6 -6
- package/dist/credits/postgres/repositories/balance.js.map +1 -1
- package/dist/credits/postgres/repositories/catalog.d.ts +4 -3
- package/dist/credits/postgres/repositories/catalog.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/catalog.js +4 -3
- package/dist/credits/postgres/repositories/catalog.js.map +1 -1
- package/dist/credits/postgres/repositories/deduction.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/deduction.js +2 -2
- package/dist/credits/postgres/repositories/deduction.js.map +1 -1
- package/dist/credits/postgres/repositories/plan.d.ts +1 -1
- package/dist/credits/postgres/store.d.ts +3 -2
- package/dist/credits/postgres/store.d.ts.map +1 -1
- package/dist/credits/postgres/store.js +17 -10
- package/dist/credits/postgres/store.js.map +1 -1
- package/dist/credits/postgres/value-mappers.d.ts +4 -3
- package/dist/credits/postgres/value-mappers.d.ts.map +1 -1
- package/dist/credits/postgres/value-mappers.js +10 -11
- package/dist/credits/postgres/value-mappers.js.map +1 -1
- package/dist/credits/service.d.ts.map +1 -1
- package/dist/credits/service.js +10 -5
- package/dist/credits/service.js.map +1 -1
- package/dist/credits/store.d.ts +5 -4
- package/dist/credits/store.d.ts.map +1 -1
- package/dist/credits/store.js.map +1 -1
- package/dist/credits/types/account.d.ts +5 -4
- package/dist/credits/types/account.d.ts.map +1 -1
- package/dist/credits/types/account.js.map +1 -1
- package/dist/credits/types/catalog.d.ts +2 -1
- package/dist/credits/types/catalog.d.ts.map +1 -1
- package/dist/credits/types/catalog.js.map +1 -1
- package/dist/credits/types/grants.d.ts.map +1 -1
- package/dist/credits/types/grants.js.map +1 -1
- package/dist/credits/types/ledger.d.ts +3 -2
- package/dist/credits/types/ledger.d.ts.map +1 -1
- package/dist/credits/types/ledger.js.map +1 -1
- package/dist/engine.d.ts +2 -2
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +39 -10
- package/dist/engine.js.map +1 -1
- package/dist/errors.d.ts +7 -4
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +18 -11
- package/dist/errors.js.map +1 -1
- package/dist/expr/ast.d.ts.map +1 -1
- package/dist/expr/ast.js.map +1 -1
- package/dist/expr/parser.d.ts.map +1 -1
- package/dist/expr/parser.js.map +1 -1
- package/dist/expr/tokenizer.d.ts.map +1 -1
- package/dist/expr/tokenizer.js +34 -12
- package/dist/expr/tokenizer.js.map +1 -1
- package/dist/expr.d.ts.map +1 -1
- package/dist/expr.js +12 -6
- package/dist/expr.js.map +1 -1
- package/dist/generated/pricing-config.types.d.ts.map +1 -1
- package/dist/generated/pricing-config.types.js.map +1 -1
- package/dist/integrations/better-auth.d.ts +4 -1
- package/dist/integrations/better-auth.d.ts.map +1 -1
- package/dist/integrations/better-auth.js +7 -4
- package/dist/integrations/better-auth.js.map +1 -1
- package/dist/load-config-file.d.ts +2 -1
- package/dist/load-config-file.d.ts.map +1 -1
- package/dist/load-config-file.js +19 -13
- package/dist/load-config-file.js.map +1 -1
- package/dist/metrics.d.ts +2 -1
- package/dist/metrics.d.ts.map +1 -1
- package/dist/metrics.js.map +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js.map +1 -1
- package/dist/providers/_shared.d.ts +8 -5
- package/dist/providers/_shared.d.ts.map +1 -1
- package/dist/providers/_shared.js +15 -11
- package/dist/providers/_shared.js.map +1 -1
- package/dist/providers/dodo/better-auth-core.d.ts +32 -0
- package/dist/providers/dodo/better-auth-core.d.ts.map +1 -0
- package/dist/providers/dodo/better-auth-core.js +14 -0
- package/dist/providers/dodo/better-auth-core.js.map +1 -0
- package/dist/providers/dodo/better-auth.d.ts +5 -7
- package/dist/providers/dodo/better-auth.d.ts.map +1 -1
- package/dist/providers/dodo/better-auth.js +16 -13
- package/dist/providers/dodo/better-auth.js.map +1 -1
- package/dist/providers/dodo/client-contract.d.ts +11 -10
- package/dist/providers/dodo/client-contract.d.ts.map +1 -1
- package/dist/providers/dodo/client-contract.js.map +1 -1
- package/dist/providers/dodo/event-mapper.d.ts +7 -4
- package/dist/providers/dodo/event-mapper.d.ts.map +1 -1
- package/dist/providers/dodo/event-mapper.js +121 -113
- package/dist/providers/dodo/event-mapper.js.map +1 -1
- package/dist/providers/dodo/nextjs-core.d.ts +22 -0
- package/dist/providers/dodo/nextjs-core.d.ts.map +1 -0
- package/dist/providers/dodo/nextjs-core.js +14 -0
- package/dist/providers/dodo/nextjs-core.js.map +1 -0
- package/dist/providers/dodo/nextjs.d.ts +5 -7
- package/dist/providers/dodo/nextjs.d.ts.map +1 -1
- package/dist/providers/dodo/nextjs.js +11 -10
- package/dist/providers/dodo/nextjs.js.map +1 -1
- package/dist/providers/dodo/provider.d.ts +3 -3
- package/dist/providers/dodo/provider.d.ts.map +1 -1
- package/dist/providers/dodo/provider.js +42 -35
- package/dist/providers/dodo/provider.js.map +1 -1
- package/dist/providers/dodo/react-native-core.d.ts +56 -0
- package/dist/providers/dodo/react-native-core.d.ts.map +1 -0
- package/dist/providers/dodo/react-native-core.js +56 -0
- package/dist/providers/dodo/react-native-core.js.map +1 -0
- package/dist/providers/dodo/react-native.d.ts +6 -50
- package/dist/providers/dodo/react-native.d.ts.map +1 -1
- package/dist/providers/dodo/react-native.js +5 -49
- package/dist/providers/dodo/react-native.js.map +1 -1
- package/dist/providers/dodo/webhook-bridge.d.ts +4 -2
- package/dist/providers/dodo/webhook-bridge.d.ts.map +1 -1
- package/dist/providers/dodo/webhook-bridge.js.map +1 -1
- package/dist/providers/environment.d.ts +1 -1
- package/dist/providers/environment.d.ts.map +1 -1
- package/dist/providers/environment.js +4 -2
- package/dist/providers/environment.js.map +1 -1
- package/dist/providers/mock/provider.d.ts.map +1 -1
- package/dist/providers/mock/provider.js +5 -10
- package/dist/providers/mock/provider.js.map +1 -1
- package/dist/providers/stripe/event-mapper.d.ts.map +1 -1
- package/dist/providers/stripe/event-mapper.js +33 -9
- package/dist/providers/stripe/event-mapper.js.map +1 -1
- package/dist/providers/stripe/provider.d.ts.map +1 -1
- package/dist/providers/stripe/provider.js +100 -60
- package/dist/providers/stripe/provider.js.map +1 -1
- package/dist/providers/types.d.ts.map +1 -1
- package/dist/providers/types.js.map +1 -1
- package/dist/retry.d.ts.map +1 -1
- package/dist/retry.js +16 -9
- package/dist/retry.js.map +1 -1
- package/dist/shared/diagnostics.d.ts +5 -5
- package/dist/shared/diagnostics.d.ts.map +1 -1
- package/dist/shared/diagnostics.js +32 -22
- package/dist/shared/diagnostics.js.map +1 -1
- package/dist/shared/idempotency.d.ts +4 -3
- package/dist/shared/idempotency.d.ts.map +1 -1
- package/dist/shared/idempotency.js +21 -10
- package/dist/shared/idempotency.js.map +1 -1
- package/dist/shared/json.d.ts +28 -0
- package/dist/shared/json.d.ts.map +1 -0
- package/dist/shared/json.js +41 -0
- package/dist/shared/json.js.map +1 -0
- package/dist/shared/logger.d.ts +10 -4
- package/dist/shared/logger.d.ts.map +1 -1
- package/dist/shared/logger.js.map +1 -1
- package/dist/shared/postgres-client.d.ts +8 -6
- package/dist/shared/postgres-client.d.ts.map +1 -1
- package/dist/shared/postgres-client.js +69 -47
- package/dist/shared/postgres-client.js.map +1 -1
- package/dist/shared/postgres-errors.d.ts +1 -1
- package/dist/shared/postgres-errors.d.ts.map +1 -1
- package/dist/shared/postgres-errors.js +32 -31
- package/dist/shared/postgres-errors.js.map +1 -1
- package/dist/shared/postgres-types.d.ts +3 -2
- package/dist/shared/postgres-types.d.ts.map +1 -1
- package/dist/shared/postgres-types.js.map +1 -1
- package/dist/shared/postgres-validation.d.ts +7 -6
- package/dist/shared/postgres-validation.d.ts.map +1 -1
- package/dist/shared/postgres-validation.js +17 -10
- package/dist/shared/postgres-validation.js.map +1 -1
- package/dist/storage/adapters/clickhouse.d.ts +11 -4
- package/dist/storage/adapters/clickhouse.d.ts.map +1 -1
- package/dist/storage/adapters/clickhouse.js +8 -6
- package/dist/storage/adapters/clickhouse.js.map +1 -1
- package/dist/storage/adapters/s3.d.ts +28 -5
- package/dist/storage/adapters/s3.d.ts.map +1 -1
- package/dist/storage/adapters/s3.js +48 -33
- package/dist/storage/adapters/s3.js.map +1 -1
- package/dist/storage/diagnostics.d.ts +1 -1
- package/dist/storage/diagnostics.js +13 -12
- package/dist/storage/diagnostics.js.map +1 -1
- package/dist/storage/maintenance.d.ts.map +1 -1
- package/dist/storage/maintenance.js +31 -25
- package/dist/storage/maintenance.js.map +1 -1
- package/dist/storage/outbox-worker.d.ts +1 -1
- package/dist/storage/outbox-worker.js +18 -18
- package/dist/storage/outbox-worker.js.map +1 -1
- package/dist/storage/ports.d.ts +10 -7
- package/dist/storage/ports.d.ts.map +1 -1
- package/dist/storage/ports.js.map +1 -1
- package/dist/storage/postgres-repository.d.ts +3 -1
- package/dist/storage/postgres-repository.d.ts.map +1 -1
- package/dist/storage/postgres-repository.js +10 -4
- package/dist/storage/postgres-repository.js.map +1 -1
- package/dist/storage/runtime.d.ts +1 -1
- package/dist/storage/runtime.d.ts.map +1 -1
- package/dist/storage/runtime.js +165 -182
- package/dist/storage/runtime.js.map +1 -1
- package/dist/telemetry/index.d.ts +10 -6
- package/dist/telemetry/index.d.ts.map +1 -1
- package/dist/telemetry/index.js +26 -23
- package/dist/telemetry/index.js.map +1 -1
- package/dist/telemetry/opentelemetry.js +3 -3
- package/dist/telemetry/opentelemetry.js.map +1 -1
- package/package.json +14 -14
package/dist/storage/runtime.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Bursar } from "../bursar.js";
|
|
2
1
|
import { z } from "zod";
|
|
2
|
+
import { Bursar } from "../bursar.js";
|
|
3
3
|
import { ConfigError, ImportError as BursarImportError, isRetryableBursarError, CatalogNotLoadedError, StoreClosedError, } from "../errors.js";
|
|
4
4
|
import { retryBursarOperation } from "../retry.js";
|
|
5
5
|
import { PostgresBillingStore } from "../billing/postgres/store.js";
|
|
@@ -12,20 +12,23 @@ import { RuntimeDiagnosticsTracker, } from "./diagnostics.js";
|
|
|
12
12
|
import { BursarMaintenance, BursarOperatorMaintenance } from "./maintenance.js";
|
|
13
13
|
import { OutboxWorker } from "./outbox-worker.js";
|
|
14
14
|
import { PostgresStorageRepository } from "./postgres-repository.js";
|
|
15
|
+
const abortSignalSchema = z.custom((value) => z
|
|
16
|
+
.object({
|
|
17
|
+
aborted: z.boolean(),
|
|
18
|
+
addEventListener: z.function(),
|
|
19
|
+
removeEventListener: z.function(),
|
|
20
|
+
throwIfAborted: z.function(),
|
|
21
|
+
})
|
|
22
|
+
.safeParse(value).success, "signal must be an AbortSignal");
|
|
23
|
+
const shouldRetrySchema = z.custom((value) => z.function().safeParse(value).success, "shouldRetry must be a function");
|
|
15
24
|
const runtimeStartOptionsSchema = z
|
|
16
25
|
.object({
|
|
17
26
|
loadCatalog: z.boolean().default(true),
|
|
18
27
|
maxAttempts: z.number().finite().int().min(1).max(Number.MAX_SAFE_INTEGER).default(1),
|
|
19
28
|
retryDelayMs: z.number().finite().min(0).max(5_000).default(250),
|
|
20
|
-
shouldRetry:
|
|
21
|
-
.custom((value) => typeof value === "function", "shouldRetry must be a function")
|
|
22
|
-
.optional(),
|
|
29
|
+
shouldRetry: shouldRetrySchema.optional(),
|
|
23
30
|
maxElapsedMs: z.number().finite().min(0).max(2_147_483_647).optional(),
|
|
24
|
-
signal:
|
|
25
|
-
.custom((value) => typeof value === "object" &&
|
|
26
|
-
value !== null &&
|
|
27
|
-
typeof value.throwIfAborted === "function", "signal must be an AbortSignal")
|
|
28
|
-
.optional(),
|
|
31
|
+
signal: abortSignalSchema.optional(),
|
|
29
32
|
})
|
|
30
33
|
.strict();
|
|
31
34
|
/** Coalesce concurrently dispatched usage events into one optional sink write. */
|
|
@@ -58,8 +61,9 @@ class UsageWriteBatcher {
|
|
|
58
61
|
write.resolve();
|
|
59
62
|
}
|
|
60
63
|
catch (error) {
|
|
64
|
+
const failure = error instanceof Error ? error : new Error("Usage batch write failed", { cause: error });
|
|
61
65
|
for (const write of pending)
|
|
62
|
-
write.reject(
|
|
66
|
+
write.reject(failure);
|
|
63
67
|
}
|
|
64
68
|
}
|
|
65
69
|
}
|
|
@@ -99,17 +103,21 @@ export class BursarRuntime {
|
|
|
99
103
|
if (options.operatorPostgres === undefined || options.operatorPostgres === null) {
|
|
100
104
|
throw new TypeError("operatorPostgres is required");
|
|
101
105
|
}
|
|
102
|
-
|
|
106
|
+
const postgresConnection = connectionStringValue(options.postgres);
|
|
107
|
+
const operatorPostgresConnection = connectionStringValue(options.operatorPostgres);
|
|
108
|
+
const ownsPool = postgresConnection !== null;
|
|
109
|
+
const ownsOperatorPool = operatorPostgresConnection !== null;
|
|
110
|
+
if (postgresConnection !== null && !postgresConnection.trim()) {
|
|
103
111
|
throw new TypeError("postgres connection string must not be empty");
|
|
104
112
|
}
|
|
105
|
-
if (
|
|
113
|
+
if (operatorPostgresConnection !== null && !operatorPostgresConnection.trim()) {
|
|
106
114
|
throw new TypeError("operatorPostgres connection string must not be empty");
|
|
107
115
|
}
|
|
108
116
|
if (options.postgres === options.operatorPostgres) {
|
|
109
117
|
throw new TypeError("postgres and operatorPostgres must use distinct connections");
|
|
110
118
|
}
|
|
111
119
|
let pg;
|
|
112
|
-
if (
|
|
120
|
+
if (ownsPool || ownsOperatorPool) {
|
|
113
121
|
try {
|
|
114
122
|
pg = await import("pg");
|
|
115
123
|
}
|
|
@@ -119,19 +127,18 @@ export class BursarRuntime {
|
|
|
119
127
|
});
|
|
120
128
|
}
|
|
121
129
|
}
|
|
122
|
-
const ownsPool = typeof options.postgres === "string";
|
|
123
|
-
const ownsOperatorPool = typeof options.operatorPostgres === "string";
|
|
124
130
|
let pool;
|
|
125
131
|
let operatorPool;
|
|
126
132
|
try {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
133
|
+
if ((ownsPool || ownsOperatorPool) && pg === undefined) {
|
|
134
|
+
throw new BursarImportError("pg is required for the Bursar runtime: npm install pg");
|
|
135
|
+
}
|
|
136
|
+
pool = ownsPool
|
|
137
|
+
? createOwnedPool(pg, postgresConnection ?? requiredConnectionString(options.postgres), options.postgresOptions)
|
|
138
|
+
: providedPool(options.postgres);
|
|
139
|
+
operatorPool = ownsOperatorPool
|
|
140
|
+
? createOwnedPool(pg, operatorPostgresConnection ?? requiredConnectionString(options.operatorPostgres), options.postgresOptions)
|
|
141
|
+
: providedPool(options.operatorPostgres);
|
|
135
142
|
return new BursarRuntime(pool, ownsPool, operatorPool, ownsOperatorPool, options);
|
|
136
143
|
}
|
|
137
144
|
catch (error) {
|
|
@@ -217,9 +224,7 @@ export class BursarRuntime {
|
|
|
217
224
|
creditsOptions: {
|
|
218
225
|
...(bursarOptions.creditsOptions ?? {}),
|
|
219
226
|
analytics: this.clickhouse ?? undefined,
|
|
220
|
-
usageStore: this.clickhouse &&
|
|
221
|
-
? this.clickhouse
|
|
222
|
-
: undefined,
|
|
227
|
+
usageStore: this.clickhouse && supportsUsageHistory(this.clickhouse) ? this.clickhouse : undefined,
|
|
223
228
|
},
|
|
224
229
|
});
|
|
225
230
|
this.postgres = new PostgresClient(operatorPool, {
|
|
@@ -266,8 +271,7 @@ export class BursarRuntime {
|
|
|
266
271
|
const rows = await this.query("SELECT 1 AS bursar_reachable");
|
|
267
272
|
const row = rows[0];
|
|
268
273
|
if (!row ||
|
|
269
|
-
|
|
270
|
-
!("bursar_reachable" in row) ||
|
|
274
|
+
!z.number().int().safeParse(row.bursar_reachable).success ||
|
|
271
275
|
row.bursar_reachable !== 1) {
|
|
272
276
|
throw new Error("PostgreSQL reachability check returned an invalid result");
|
|
273
277
|
}
|
|
@@ -300,7 +304,7 @@ export class BursarRuntime {
|
|
|
300
304
|
await this.verifyTenantIdentity();
|
|
301
305
|
if (startOptions.loadCatalog) {
|
|
302
306
|
const shouldRetry = startOptions.shouldRetry ??
|
|
303
|
-
((
|
|
307
|
+
((cause) => cause instanceof CatalogNotLoadedError || isRetryableBursarError(cause));
|
|
304
308
|
await retryBursarOperation(() => this.bursar.loadCatalog(), {
|
|
305
309
|
maxAttempts: startOptions.maxAttempts,
|
|
306
310
|
baseDelayMs: startOptions.retryDelayMs,
|
|
@@ -475,10 +479,8 @@ export class BursarRuntime {
|
|
|
475
479
|
return;
|
|
476
480
|
const rows = await this.query("SELECT bursar.resolve_active_tenant_for_trigger($1)::text AS tenant_id", [this.tenantSlug]);
|
|
477
481
|
const resolved = rows[0];
|
|
478
|
-
const tenantId = resolved
|
|
479
|
-
|
|
480
|
-
: undefined;
|
|
481
|
-
if (tenantId !== this.tenantId) {
|
|
482
|
+
const tenantId = z.string().safeParse(resolved?.tenant_id);
|
|
483
|
+
if (!tenantId.success || tenantId.data !== this.tenantId) {
|
|
482
484
|
throw new ConfigError(`Bursar tenant slug '${this.tenantSlug}' resolves to a different tenant ID`);
|
|
483
485
|
}
|
|
484
486
|
}
|
|
@@ -487,171 +489,152 @@ export async function createBursarRuntime(options) {
|
|
|
487
489
|
return BursarRuntime.create(options);
|
|
488
490
|
}
|
|
489
491
|
function outboxStatusProvider(repository) {
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
raw = await stats.call(repository, { limit });
|
|
495
|
-
}
|
|
496
|
-
catch (error) {
|
|
497
|
-
if (!isInputShapeError(error))
|
|
498
|
-
throw error;
|
|
499
|
-
raw = await stats.call(repository, limit);
|
|
500
|
-
}
|
|
501
|
-
if (!isJsonObject(raw))
|
|
502
|
-
throw new TypeError("outbox stats returned a malformed result");
|
|
503
|
-
return {
|
|
504
|
-
pendingCount: statsCount(raw, "pendingCount", "pending_count", "pending"),
|
|
505
|
-
processingCount: statsCount(raw, "processingCount", "processing_count", "processing"),
|
|
506
|
-
deliveredCount: statsCount(raw, "deliveredCount", "delivered_count", "delivered"),
|
|
507
|
-
deadLetterCount: statsCount(raw, "deadLetterCount", "dead_letter_count", "dead_letter"),
|
|
508
|
-
oldestPendingAt: statsTimestamp(raw, "oldestPendingAt", "oldest_pending_at"),
|
|
509
|
-
};
|
|
510
|
-
};
|
|
492
|
+
return async (limit) => repository.stats({ limit });
|
|
493
|
+
}
|
|
494
|
+
function isPostgresConnectionString(value) {
|
|
495
|
+
return z.string().safeParse(value).success;
|
|
511
496
|
}
|
|
512
|
-
function
|
|
513
|
-
const
|
|
514
|
-
|
|
515
|
-
|
|
497
|
+
function connectionStringValue(value) {
|
|
498
|
+
const parsed = z.string().safeParse(value);
|
|
499
|
+
return parsed.success ? parsed.data : null;
|
|
500
|
+
}
|
|
501
|
+
function requiredConnectionString(value) {
|
|
502
|
+
const parsed = z.string().min(1).safeParse(value);
|
|
503
|
+
if (!parsed.success) {
|
|
504
|
+
throw new TypeError("an owned PostgreSQL pool requires a connection string");
|
|
516
505
|
}
|
|
517
|
-
return
|
|
506
|
+
return parsed.data;
|
|
518
507
|
}
|
|
519
|
-
function
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
return value;
|
|
527
|
-
throw new TypeError(`outbox stats field ${keys[0]} must be a timestamp string or null`);
|
|
508
|
+
function createOwnedPool(pg, connectionString, options) {
|
|
509
|
+
if (pg === undefined) {
|
|
510
|
+
throw new BursarImportError("pg is required for the Bursar runtime: npm install pg");
|
|
511
|
+
}
|
|
512
|
+
// SAFETY: the runtime's PostgresPool contract is the subset used by Bursar,
|
|
513
|
+
// and the node-postgres Pool is created from the validated connection options above.
|
|
514
|
+
return new pg.Pool(postgresPoolConfig(connectionString, options));
|
|
528
515
|
}
|
|
529
|
-
function
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
516
|
+
function providedPool(value) {
|
|
517
|
+
if (isPostgresConnectionString(value)) {
|
|
518
|
+
throw new TypeError("a connection string cannot be used as an external PostgreSQL pool");
|
|
519
|
+
}
|
|
520
|
+
return value;
|
|
521
|
+
}
|
|
522
|
+
function supportsUsageHistory(value) {
|
|
523
|
+
return z.object({ listUsageCharges: z.function() }).safeParse(value).success;
|
|
533
524
|
}
|
|
534
525
|
function normalizeTenantSlug(value) {
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
const normalized = value.trim().toLowerCase();
|
|
538
|
-
if (normalized.length < 1 ||
|
|
539
|
-
normalized.length > 100 ||
|
|
540
|
-
!/^[a-z0-9]+(?:[a-z0-9-]*[a-z0-9])?$/.test(normalized)) {
|
|
526
|
+
const trimmed = z.string().trim().min(1).max(100).safeParse(value);
|
|
527
|
+
if (!trimmed.success)
|
|
541
528
|
throw new TypeError("tenantSlug must be a valid Bursar tenant slug");
|
|
542
|
-
|
|
543
|
-
|
|
529
|
+
const normalized = z
|
|
530
|
+
.string()
|
|
531
|
+
.regex(/^[a-z0-9]+(?:[a-z0-9-]*[a-z0-9])?$/u)
|
|
532
|
+
.safeParse(trimmed.data.toLowerCase());
|
|
533
|
+
if (!normalized.success)
|
|
534
|
+
throw new TypeError("tenantSlug must be a valid Bursar tenant slug");
|
|
535
|
+
return normalized.data;
|
|
544
536
|
}
|
|
537
|
+
const outboxJsonObjectSchema = z.record(z.string(), z.json());
|
|
538
|
+
const usageOutboxPayloadSchema = z
|
|
539
|
+
.object({
|
|
540
|
+
tenant_id: z.string().min(1),
|
|
541
|
+
charge_id: z.string().min(1),
|
|
542
|
+
account_id: z.string().min(1),
|
|
543
|
+
subject_id: z.string().min(1),
|
|
544
|
+
operation: z.string().min(1),
|
|
545
|
+
feature: z.string().nullable().optional(),
|
|
546
|
+
model: z.string().nullable().optional(),
|
|
547
|
+
region: z.string().nullable().optional(),
|
|
548
|
+
measures: outboxJsonObjectSchema,
|
|
549
|
+
dimensions: outboxJsonObjectSchema,
|
|
550
|
+
metadata: outboxJsonObjectSchema,
|
|
551
|
+
requested: z.string().min(1),
|
|
552
|
+
charged: z.string().min(1),
|
|
553
|
+
allowance_requested: z.string().min(1),
|
|
554
|
+
allowance_covered: z.string().min(1),
|
|
555
|
+
billing_disposition: z.enum(["billable", "record_only"]).optional(),
|
|
556
|
+
catalog_revision_id: z.string().nullable().optional(),
|
|
557
|
+
plan_id: z.string().nullable().optional(),
|
|
558
|
+
rate_card_key: z.string().nullable().optional(),
|
|
559
|
+
pricing_snapshot: outboxJsonObjectSchema,
|
|
560
|
+
ledger_entry_id: z.string().nullable().optional(),
|
|
561
|
+
correction_of_charge_id: z.string().nullable().optional(),
|
|
562
|
+
idempotency_key: z.string().min(1),
|
|
563
|
+
request_digest: z.string().min(1),
|
|
564
|
+
event_at: z.string().min(1),
|
|
565
|
+
created_at: z.string().min(1),
|
|
566
|
+
})
|
|
567
|
+
.passthrough();
|
|
568
|
+
const billingOutboxPayloadSchema = z
|
|
569
|
+
.object({
|
|
570
|
+
tenant_id: z.string().min(1),
|
|
571
|
+
event_id: z.string().min(1),
|
|
572
|
+
provider: z.string().min(1),
|
|
573
|
+
provider_environment: z.string().min(1),
|
|
574
|
+
provider_event_id: z.string().min(1),
|
|
575
|
+
event_type: z.string().min(1),
|
|
576
|
+
status: z.string().min(1),
|
|
577
|
+
received_at: z.string().min(1),
|
|
578
|
+
completed_at: z.string().nullable().optional(),
|
|
579
|
+
envelope: outboxJsonObjectSchema,
|
|
580
|
+
object_key: z.string().nullable().optional(),
|
|
581
|
+
object_version: z.string().nullable().optional(),
|
|
582
|
+
archived_at: z.string().nullable().optional(),
|
|
583
|
+
})
|
|
584
|
+
.passthrough();
|
|
545
585
|
function usageExportFromOutbox(payload) {
|
|
546
|
-
const
|
|
547
|
-
|
|
548
|
-
"charge_id",
|
|
549
|
-
"account_id",
|
|
550
|
-
"subject_id",
|
|
551
|
-
"operation",
|
|
552
|
-
"requested",
|
|
553
|
-
"charged",
|
|
554
|
-
"allowance_requested",
|
|
555
|
-
"allowance_covered",
|
|
556
|
-
"idempotency_key",
|
|
557
|
-
"request_digest",
|
|
558
|
-
"event_at",
|
|
559
|
-
"created_at",
|
|
560
|
-
];
|
|
561
|
-
if (requiredStrings.some((key) => !isNonEmptyString(payload[key])))
|
|
562
|
-
return null;
|
|
563
|
-
if (!isJsonObject(payload.measures) ||
|
|
564
|
-
!isJsonObject(payload.dimensions) ||
|
|
565
|
-
!isJsonObject(payload.metadata) ||
|
|
566
|
-
!isJsonObject(payload.pricing_snapshot)) {
|
|
586
|
+
const parsed = usageOutboxPayloadSchema.safeParse(payload);
|
|
587
|
+
if (!parsed.success)
|
|
567
588
|
return null;
|
|
568
|
-
|
|
569
|
-
const optionalStrings = [
|
|
570
|
-
"feature",
|
|
571
|
-
"model",
|
|
572
|
-
"region",
|
|
573
|
-
"catalog_revision_id",
|
|
574
|
-
"plan_id",
|
|
575
|
-
"rate_card_key",
|
|
576
|
-
"ledger_entry_id",
|
|
577
|
-
"correction_of_charge_id",
|
|
578
|
-
];
|
|
579
|
-
if (optionalStrings.some((key) => !isOptionalString(payload[key])))
|
|
580
|
-
return null;
|
|
581
|
-
if (payload.billing_disposition !== undefined &&
|
|
582
|
-
payload.billing_disposition !== "billable" &&
|
|
583
|
-
payload.billing_disposition !== "record_only") {
|
|
584
|
-
return null;
|
|
585
|
-
}
|
|
589
|
+
const value = parsed.data;
|
|
586
590
|
return {
|
|
587
|
-
tenantId:
|
|
588
|
-
chargeId:
|
|
589
|
-
accountId:
|
|
590
|
-
subjectId:
|
|
591
|
-
operation:
|
|
592
|
-
feature:
|
|
593
|
-
model:
|
|
594
|
-
region:
|
|
595
|
-
measures:
|
|
596
|
-
dimensions:
|
|
597
|
-
metadata:
|
|
598
|
-
requested:
|
|
599
|
-
charged:
|
|
600
|
-
allowanceRequested:
|
|
601
|
-
allowanceCovered:
|
|
602
|
-
billingDisposition:
|
|
603
|
-
catalogRevisionId:
|
|
604
|
-
planId:
|
|
605
|
-
rateCardKey:
|
|
606
|
-
pricingSnapshot:
|
|
607
|
-
ledgerEntryId:
|
|
608
|
-
correctionOfChargeId:
|
|
609
|
-
idempotencyKey:
|
|
610
|
-
requestDigest:
|
|
611
|
-
eventAt:
|
|
612
|
-
createdAt:
|
|
591
|
+
tenantId: value.tenant_id,
|
|
592
|
+
chargeId: value.charge_id,
|
|
593
|
+
accountId: value.account_id,
|
|
594
|
+
subjectId: value.subject_id,
|
|
595
|
+
operation: value.operation,
|
|
596
|
+
feature: value.feature ?? null,
|
|
597
|
+
model: value.model ?? null,
|
|
598
|
+
region: value.region ?? null,
|
|
599
|
+
measures: value.measures,
|
|
600
|
+
dimensions: value.dimensions,
|
|
601
|
+
metadata: value.metadata,
|
|
602
|
+
requested: value.requested,
|
|
603
|
+
charged: value.charged,
|
|
604
|
+
allowanceRequested: value.allowance_requested,
|
|
605
|
+
allowanceCovered: value.allowance_covered,
|
|
606
|
+
billingDisposition: value.billing_disposition ?? "billable",
|
|
607
|
+
catalogRevisionId: value.catalog_revision_id ?? null,
|
|
608
|
+
planId: value.plan_id ?? null,
|
|
609
|
+
rateCardKey: value.rate_card_key ?? null,
|
|
610
|
+
pricingSnapshot: value.pricing_snapshot,
|
|
611
|
+
ledgerEntryId: value.ledger_entry_id ?? null,
|
|
612
|
+
correctionOfChargeId: value.correction_of_charge_id ?? null,
|
|
613
|
+
idempotencyKey: value.idempotency_key,
|
|
614
|
+
requestDigest: value.request_digest,
|
|
615
|
+
eventAt: value.event_at,
|
|
616
|
+
createdAt: value.created_at,
|
|
613
617
|
};
|
|
614
618
|
}
|
|
615
619
|
function billingExportFromOutbox(payload) {
|
|
616
|
-
const
|
|
617
|
-
|
|
618
|
-
"event_id",
|
|
619
|
-
"provider",
|
|
620
|
-
"provider_environment",
|
|
621
|
-
"provider_event_id",
|
|
622
|
-
"event_type",
|
|
623
|
-
"status",
|
|
624
|
-
"received_at",
|
|
625
|
-
];
|
|
626
|
-
if (requiredStrings.some((key) => !isNonEmptyString(payload[key])))
|
|
627
|
-
return null;
|
|
628
|
-
if (!isJsonObject(payload.envelope))
|
|
629
|
-
return null;
|
|
630
|
-
if (!isOptionalString(payload.completed_at))
|
|
620
|
+
const parsed = billingOutboxPayloadSchema.safeParse(payload);
|
|
621
|
+
if (!parsed.success)
|
|
631
622
|
return null;
|
|
623
|
+
const value = parsed.data;
|
|
632
624
|
return {
|
|
633
|
-
tenantId:
|
|
634
|
-
eventId:
|
|
635
|
-
provider:
|
|
636
|
-
providerEnvironment:
|
|
637
|
-
providerEventId:
|
|
638
|
-
eventType:
|
|
639
|
-
status:
|
|
640
|
-
receivedAt:
|
|
641
|
-
completedAt:
|
|
642
|
-
envelope:
|
|
643
|
-
objectKey:
|
|
644
|
-
objectVersion:
|
|
645
|
-
archivedAt:
|
|
625
|
+
tenantId: value.tenant_id,
|
|
626
|
+
eventId: value.event_id,
|
|
627
|
+
provider: value.provider,
|
|
628
|
+
providerEnvironment: value.provider_environment,
|
|
629
|
+
providerEventId: value.provider_event_id,
|
|
630
|
+
eventType: value.event_type,
|
|
631
|
+
status: value.status,
|
|
632
|
+
receivedAt: value.received_at,
|
|
633
|
+
completedAt: value.completed_at ?? null,
|
|
634
|
+
envelope: value.envelope,
|
|
635
|
+
objectKey: value.object_key ?? null,
|
|
636
|
+
objectVersion: value.object_version ?? null,
|
|
637
|
+
archivedAt: value.archived_at ?? null,
|
|
646
638
|
};
|
|
647
639
|
}
|
|
648
|
-
function isNonEmptyString(value) {
|
|
649
|
-
return typeof value === "string" && value.length > 0;
|
|
650
|
-
}
|
|
651
|
-
function isOptionalString(value) {
|
|
652
|
-
return value === null || value === undefined || typeof value === "string";
|
|
653
|
-
}
|
|
654
|
-
function isJsonObject(value) {
|
|
655
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
656
|
-
}
|
|
657
640
|
//# sourceMappingURL=runtime.js.map
|