sendwend 0.0.1 → 12.7.3
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/LICENSE +1 -1
- package/README.md +2065 -22
- package/dist/_virtual/_rolldown/runtime.cjs +22 -0
- package/dist/apns-relay.cjs +91 -0
- package/dist/apns-relay.cjs.map +1 -0
- package/dist/apns-relay.d.cts +26 -0
- package/dist/apns-relay.d.cts.map +1 -0
- package/dist/apns-relay.d.mts +26 -0
- package/dist/apns-relay.d.mts.map +1 -0
- package/dist/apns-relay.mjs +90 -0
- package/dist/apns-relay.mjs.map +1 -0
- package/dist/apns.cjs +215 -0
- package/dist/apns.cjs.map +1 -0
- package/dist/apns.d.cts +110 -0
- package/dist/apns.d.cts.map +1 -0
- package/dist/apns.d.mts +110 -0
- package/dist/apns.d.mts.map +1 -0
- package/dist/apns.mjs +207 -0
- package/dist/apns.mjs.map +1 -0
- package/dist/aws/engine-step.cjs +105 -0
- package/dist/aws/engine-step.cjs.map +1 -0
- package/dist/aws/engine-step.d.cts +42 -0
- package/dist/aws/engine-step.d.cts.map +1 -0
- package/dist/aws/engine-step.d.mts +42 -0
- package/dist/aws/engine-step.d.mts.map +1 -0
- package/dist/aws/engine-step.mjs +103 -0
- package/dist/aws/engine-step.mjs.map +1 -0
- package/dist/aws/function-url.cjs +53 -0
- package/dist/aws/function-url.cjs.map +1 -0
- package/dist/aws/function-url.d.cts +36 -0
- package/dist/aws/function-url.d.cts.map +1 -0
- package/dist/aws/function-url.d.mts +36 -0
- package/dist/aws/function-url.d.mts.map +1 -0
- package/dist/aws/function-url.mjs +50 -0
- package/dist/aws/function-url.mjs.map +1 -0
- package/dist/aws/index.cjs +232 -0
- package/dist/aws/index.cjs.map +1 -0
- package/dist/aws/index.d.cts +73 -0
- package/dist/aws/index.d.cts.map +1 -0
- package/dist/aws/index.d.mts +73 -0
- package/dist/aws/index.d.mts.map +1 -0
- package/dist/aws/index.mjs +218 -0
- package/dist/aws/index.mjs.map +1 -0
- package/dist/aws/instances.cjs +218 -0
- package/dist/aws/instances.cjs.map +1 -0
- package/dist/aws/instances.d.cts +102 -0
- package/dist/aws/instances.d.cts.map +1 -0
- package/dist/aws/instances.d.mts +102 -0
- package/dist/aws/instances.d.mts.map +1 -0
- package/dist/aws/instances.mjs +214 -0
- package/dist/aws/instances.mjs.map +1 -0
- package/dist/aws/lambda-api.cjs +86 -0
- package/dist/aws/lambda-api.cjs.map +1 -0
- package/dist/aws/lambda-api.d.cts +41 -0
- package/dist/aws/lambda-api.d.cts.map +1 -0
- package/dist/aws/lambda-api.d.mts +41 -0
- package/dist/aws/lambda-api.d.mts.map +1 -0
- package/dist/aws/lambda-api.mjs +85 -0
- package/dist/aws/lambda-api.mjs.map +1 -0
- package/dist/client.cjs +216 -0
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.cts +193 -0
- package/dist/client.d.cts.map +1 -0
- package/dist/client.d.mts +193 -0
- package/dist/client.d.mts.map +1 -0
- package/dist/client.mjs +212 -0
- package/dist/client.mjs.map +1 -0
- package/dist/cloudflare/bindings.cjs +49 -0
- package/dist/cloudflare/bindings.cjs.map +1 -0
- package/dist/cloudflare/bindings.d.cts +31 -0
- package/dist/cloudflare/bindings.d.cts.map +1 -0
- package/dist/cloudflare/bindings.d.mts +31 -0
- package/dist/cloudflare/bindings.d.mts.map +1 -0
- package/dist/cloudflare/bindings.mjs +46 -0
- package/dist/cloudflare/bindings.mjs.map +1 -0
- package/dist/cloudflare/index.cjs +73 -0
- package/dist/cloudflare/index.d.cts +25 -0
- package/dist/cloudflare/index.d.mts +25 -0
- package/dist/cloudflare/index.mjs +20 -0
- package/dist/cloudflare/runner.cjs +200 -0
- package/dist/cloudflare/runner.cjs.map +1 -0
- package/dist/cloudflare/runner.d.cts +83 -0
- package/dist/cloudflare/runner.d.cts.map +1 -0
- package/dist/cloudflare/runner.d.mts +83 -0
- package/dist/cloudflare/runner.d.mts.map +1 -0
- package/dist/cloudflare/runner.mjs +199 -0
- package/dist/cloudflare/runner.mjs.map +1 -0
- package/dist/cloudflare/worker.cjs +106 -0
- package/dist/cloudflare/worker.cjs.map +1 -0
- package/dist/cloudflare/worker.d.cts +52 -0
- package/dist/cloudflare/worker.d.cts.map +1 -0
- package/dist/cloudflare/worker.d.mts +52 -0
- package/dist/cloudflare/worker.d.mts.map +1 -0
- package/dist/cloudflare/worker.mjs +104 -0
- package/dist/cloudflare/worker.mjs.map +1 -0
- package/dist/cloudflare/workers.d.cjs +0 -0
- package/dist/cloudflare/workers.d.cts +42 -0
- package/dist/cloudflare/workers.d.cts.map +1 -0
- package/dist/cloudflare/workers.d.mts +42 -0
- package/dist/cloudflare/workers.d.mts.map +1 -0
- package/dist/config.cjs +257 -0
- package/dist/config.cjs.map +1 -0
- package/dist/config.d.cts +184 -0
- package/dist/config.d.cts.map +1 -0
- package/dist/config.d.mts +184 -0
- package/dist/config.d.mts.map +1 -0
- package/dist/config.mjs +246 -0
- package/dist/config.mjs.map +1 -0
- package/dist/contract.cjs +222 -0
- package/dist/contract.cjs.map +1 -0
- package/dist/contract.d.cts +110 -0
- package/dist/contract.d.cts.map +1 -0
- package/dist/contract.d.mts +110 -0
- package/dist/contract.d.mts.map +1 -0
- package/dist/contract.mjs +216 -0
- package/dist/contract.mjs.map +1 -0
- package/dist/date-time.cjs +22 -0
- package/dist/date-time.cjs.map +1 -0
- package/dist/date-time.d.cts +10 -0
- package/dist/date-time.d.cts.map +1 -0
- package/dist/date-time.d.mts +10 -0
- package/dist/date-time.d.mts.map +1 -0
- package/dist/date-time.mjs +21 -0
- package/dist/date-time.mjs.map +1 -0
- package/dist/dsl/action.cjs +25 -0
- package/dist/dsl/action.cjs.map +1 -0
- package/dist/dsl/action.d.cts +63 -0
- package/dist/dsl/action.d.cts.map +1 -0
- package/dist/dsl/action.d.mts +63 -0
- package/dist/dsl/action.d.mts.map +1 -0
- package/dist/dsl/action.mjs +24 -0
- package/dist/dsl/action.mjs.map +1 -0
- package/dist/dsl/base.cjs +33 -0
- package/dist/dsl/base.cjs.map +1 -0
- package/dist/dsl/base.d.cts +43 -0
- package/dist/dsl/base.d.cts.map +1 -0
- package/dist/dsl/base.d.mts +43 -0
- package/dist/dsl/base.d.mts.map +1 -0
- package/dist/dsl/base.mjs +29 -0
- package/dist/dsl/base.mjs.map +1 -0
- package/dist/dsl/bundle.cjs +287 -0
- package/dist/dsl/bundle.cjs.map +1 -0
- package/dist/dsl/bundle.d.cts +33 -0
- package/dist/dsl/bundle.d.cts.map +1 -0
- package/dist/dsl/bundle.d.mts +33 -0
- package/dist/dsl/bundle.d.mts.map +1 -0
- package/dist/dsl/bundle.mjs +284 -0
- package/dist/dsl/bundle.mjs.map +1 -0
- package/dist/dsl/condition.cjs +200 -0
- package/dist/dsl/condition.cjs.map +1 -0
- package/dist/dsl/condition.d.cts +151 -0
- package/dist/dsl/condition.d.cts.map +1 -0
- package/dist/dsl/condition.d.mts +151 -0
- package/dist/dsl/condition.d.mts.map +1 -0
- package/dist/dsl/condition.mjs +178 -0
- package/dist/dsl/condition.mjs.map +1 -0
- package/dist/dsl/flow.cjs +335 -0
- package/dist/dsl/flow.cjs.map +1 -0
- package/dist/dsl/flow.d.cts +278 -0
- package/dist/dsl/flow.d.cts.map +1 -0
- package/dist/dsl/flow.d.mts +278 -0
- package/dist/dsl/flow.d.mts.map +1 -0
- package/dist/dsl/flow.mjs +331 -0
- package/dist/dsl/flow.mjs.map +1 -0
- package/dist/dsl/index.cjs +37 -0
- package/dist/dsl/index.d.cts +34 -0
- package/dist/dsl/index.d.mts +34 -0
- package/dist/dsl/index.mjs +9 -0
- package/dist/dsl/refs.cjs +101 -0
- package/dist/dsl/refs.cjs.map +1 -0
- package/dist/dsl/refs.d.cts +127 -0
- package/dist/dsl/refs.d.cts.map +1 -0
- package/dist/dsl/refs.d.mts +127 -0
- package/dist/dsl/refs.d.mts.map +1 -0
- package/dist/dsl/refs.mjs +93 -0
- package/dist/dsl/refs.mjs.map +1 -0
- package/dist/dsl/template.cjs +19 -0
- package/dist/dsl/template.cjs.map +1 -0
- package/dist/dsl/template.d.cts +30 -0
- package/dist/dsl/template.d.cts.map +1 -0
- package/dist/dsl/template.d.mts +30 -0
- package/dist/dsl/template.d.mts.map +1 -0
- package/dist/dsl/template.mjs +18 -0
- package/dist/dsl/template.mjs.map +1 -0
- package/dist/dsl/trigger.cjs +53 -0
- package/dist/dsl/trigger.cjs.map +1 -0
- package/dist/dsl/trigger.d.cts +112 -0
- package/dist/dsl/trigger.d.cts.map +1 -0
- package/dist/dsl/trigger.d.mts +112 -0
- package/dist/dsl/trigger.d.mts.map +1 -0
- package/dist/dsl/trigger.mjs +52 -0
- package/dist/dsl/trigger.mjs.map +1 -0
- package/dist/dsl/workflow.cjs +143 -0
- package/dist/dsl/workflow.cjs.map +1 -0
- package/dist/dsl/workflow.d.cts +88 -0
- package/dist/dsl/workflow.d.cts.map +1 -0
- package/dist/dsl/workflow.d.mts +88 -0
- package/dist/dsl/workflow.d.mts.map +1 -0
- package/dist/dsl/workflow.mjs +142 -0
- package/dist/dsl/workflow.mjs.map +1 -0
- package/dist/email-context.cjs +28 -0
- package/dist/email-context.cjs.map +1 -0
- package/dist/email-context.d.cts +9 -0
- package/dist/email-context.d.cts.map +1 -0
- package/dist/email-context.d.mts +9 -0
- package/dist/email-context.d.mts.map +1 -0
- package/dist/email-context.mjs +26 -0
- package/dist/email-context.mjs.map +1 -0
- package/dist/email-sender.cjs +93 -0
- package/dist/email-sender.cjs.map +1 -0
- package/dist/email-sender.d.cts +88 -0
- package/dist/email-sender.d.cts.map +1 -0
- package/dist/email-sender.d.mts +88 -0
- package/dist/email-sender.d.mts.map +1 -0
- package/dist/email-sender.mjs +90 -0
- package/dist/email-sender.mjs.map +1 -0
- package/dist/email.cjs +73 -0
- package/dist/email.cjs.map +1 -0
- package/dist/email.d.cts +59 -0
- package/dist/email.d.cts.map +1 -0
- package/dist/email.d.mts +59 -0
- package/dist/email.d.mts.map +1 -0
- package/dist/email.mjs +68 -0
- package/dist/email.mjs.map +1 -0
- package/dist/engine/actions.cjs +19 -0
- package/dist/engine/actions.cjs.map +1 -0
- package/dist/engine/actions.d.cts +27 -0
- package/dist/engine/actions.d.cts.map +1 -0
- package/dist/engine/actions.d.mts +27 -0
- package/dist/engine/actions.d.mts.map +1 -0
- package/dist/engine/actions.mjs +18 -0
- package/dist/engine/actions.mjs.map +1 -0
- package/dist/engine/bucket.cjs +71 -0
- package/dist/engine/bucket.cjs.map +1 -0
- package/dist/engine/bucket.d.cts +30 -0
- package/dist/engine/bucket.d.cts.map +1 -0
- package/dist/engine/bucket.d.mts +30 -0
- package/dist/engine/bucket.d.mts.map +1 -0
- package/dist/engine/bucket.mjs +68 -0
- package/dist/engine/bucket.mjs.map +1 -0
- package/dist/engine/condition.cjs +199 -0
- package/dist/engine/condition.cjs.map +1 -0
- package/dist/engine/condition.d.cts +76 -0
- package/dist/engine/condition.d.cts.map +1 -0
- package/dist/engine/condition.d.mts +76 -0
- package/dist/engine/condition.d.mts.map +1 -0
- package/dist/engine/condition.mjs +192 -0
- package/dist/engine/condition.mjs.map +1 -0
- package/dist/engine/duration.cjs +33 -0
- package/dist/engine/duration.cjs.map +1 -0
- package/dist/engine/duration.d.cts +3 -0
- package/dist/engine/duration.d.cts.map +1 -0
- package/dist/engine/duration.d.mts +3 -0
- package/dist/engine/duration.d.mts.map +1 -0
- package/dist/engine/duration.mjs +32 -0
- package/dist/engine/duration.mjs.map +1 -0
- package/dist/engine/index.cjs +33 -0
- package/dist/engine/index.d.cts +10 -0
- package/dist/engine/index.d.mts +10 -0
- package/dist/engine/index.mjs +9 -0
- package/dist/engine/interpreter.cjs +613 -0
- package/dist/engine/interpreter.cjs.map +1 -0
- package/dist/engine/interpreter.d.cts +4 -0
- package/dist/engine/interpreter.d.cts.map +1 -0
- package/dist/engine/interpreter.d.mts +4 -0
- package/dist/engine/interpreter.d.mts.map +1 -0
- package/dist/engine/interpreter.mjs +612 -0
- package/dist/engine/interpreter.mjs.map +1 -0
- package/dist/engine/ports.cjs +0 -0
- package/dist/engine/ports.d.cts +373 -0
- package/dist/engine/ports.d.cts.map +1 -0
- package/dist/engine/ports.d.mts +373 -0
- package/dist/engine/ports.d.mts.map +1 -0
- package/dist/engine/ports.mjs +1 -0
- package/dist/engine/recipient.cjs +22 -0
- package/dist/engine/recipient.cjs.map +1 -0
- package/dist/engine/recipient.d.cts +11 -0
- package/dist/engine/recipient.d.cts.map +1 -0
- package/dist/engine/recipient.d.mts +11 -0
- package/dist/engine/recipient.d.mts.map +1 -0
- package/dist/engine/recipient.mjs +21 -0
- package/dist/engine/recipient.mjs.map +1 -0
- package/dist/engine/retry.cjs +106 -0
- package/dist/engine/retry.cjs.map +1 -0
- package/dist/engine/retry.d.cts +73 -0
- package/dist/engine/retry.d.cts.map +1 -0
- package/dist/engine/retry.d.mts +73 -0
- package/dist/engine/retry.d.mts.map +1 -0
- package/dist/engine/retry.mjs +98 -0
- package/dist/engine/retry.mjs.map +1 -0
- package/dist/engine/time-scale.cjs +153 -0
- package/dist/engine/time-scale.cjs.map +1 -0
- package/dist/engine/time-scale.d.cts +41 -0
- package/dist/engine/time-scale.d.cts.map +1 -0
- package/dist/engine/time-scale.d.mts +41 -0
- package/dist/engine/time-scale.d.mts.map +1 -0
- package/dist/engine/time-scale.mjs +150 -0
- package/dist/engine/time-scale.mjs.map +1 -0
- package/dist/engine/time-window.cjs +148 -0
- package/dist/engine/time-window.cjs.map +1 -0
- package/dist/engine/time-window.d.cts +20 -0
- package/dist/engine/time-window.d.cts.map +1 -0
- package/dist/engine/time-window.d.mts +20 -0
- package/dist/engine/time-window.d.mts.map +1 -0
- package/dist/engine/time-window.mjs +146 -0
- package/dist/engine/time-window.mjs.map +1 -0
- package/dist/fcm.cjs +208 -0
- package/dist/fcm.cjs.map +1 -0
- package/dist/fcm.d.cts +89 -0
- package/dist/fcm.d.cts.map +1 -0
- package/dist/fcm.d.mts +89 -0
- package/dist/fcm.d.mts.map +1 -0
- package/dist/fcm.mjs +202 -0
- package/dist/fcm.mjs.map +1 -0
- package/dist/hash.cjs +244 -0
- package/dist/hash.cjs.map +1 -0
- package/dist/hash.d.cts +28 -0
- package/dist/hash.d.cts.map +1 -0
- package/dist/hash.d.mts +28 -0
- package/dist/hash.d.mts.map +1 -0
- package/dist/hash.mjs +238 -0
- package/dist/hash.mjs.map +1 -0
- package/dist/host/auth.cjs +48 -0
- package/dist/host/auth.cjs.map +1 -0
- package/dist/host/auth.d.cts +33 -0
- package/dist/host/auth.d.cts.map +1 -0
- package/dist/host/auth.d.mts +33 -0
- package/dist/host/auth.d.mts.map +1 -0
- package/dist/host/auth.mjs +45 -0
- package/dist/host/auth.mjs.map +1 -0
- package/dist/host/batch.cjs +178 -0
- package/dist/host/batch.cjs.map +1 -0
- package/dist/host/batch.d.cts +92 -0
- package/dist/host/batch.d.cts.map +1 -0
- package/dist/host/batch.d.mts +92 -0
- package/dist/host/batch.d.mts.map +1 -0
- package/dist/host/batch.mjs +169 -0
- package/dist/host/batch.mjs.map +1 -0
- package/dist/host/channels.cjs +256 -0
- package/dist/host/channels.cjs.map +1 -0
- package/dist/host/channels.d.cts +66 -0
- package/dist/host/channels.d.cts.map +1 -0
- package/dist/host/channels.d.mts +66 -0
- package/dist/host/channels.d.mts.map +1 -0
- package/dist/host/channels.mjs +252 -0
- package/dist/host/channels.mjs.map +1 -0
- package/dist/host/execute.cjs +102 -0
- package/dist/host/execute.cjs.map +1 -0
- package/dist/host/execute.d.cts +38 -0
- package/dist/host/execute.d.cts.map +1 -0
- package/dist/host/execute.d.mts +38 -0
- package/dist/host/execute.d.mts.map +1 -0
- package/dist/host/execute.mjs +101 -0
- package/dist/host/execute.mjs.map +1 -0
- package/dist/host/fresh-read.cjs +29 -0
- package/dist/host/fresh-read.cjs.map +1 -0
- package/dist/host/fresh-read.d.cts +16 -0
- package/dist/host/fresh-read.d.cts.map +1 -0
- package/dist/host/fresh-read.d.mts +16 -0
- package/dist/host/fresh-read.d.mts.map +1 -0
- package/dist/host/fresh-read.mjs +28 -0
- package/dist/host/fresh-read.mjs.map +1 -0
- package/dist/host/http.cjs +393 -0
- package/dist/host/http.cjs.map +1 -0
- package/dist/host/http.d.cts +61 -0
- package/dist/host/http.d.cts.map +1 -0
- package/dist/host/http.d.mts +61 -0
- package/dist/host/http.d.mts.map +1 -0
- package/dist/host/http.mjs +389 -0
- package/dist/host/http.mjs.map +1 -0
- package/dist/host/ingest.cjs +1210 -0
- package/dist/host/ingest.cjs.map +1 -0
- package/dist/host/ingest.d.cts +497 -0
- package/dist/host/ingest.d.cts.map +1 -0
- package/dist/host/ingest.d.mts +497 -0
- package/dist/host/ingest.d.mts.map +1 -0
- package/dist/host/ingest.mjs +1172 -0
- package/dist/host/ingest.mjs.map +1 -0
- package/dist/host/journey-host.cjs +223 -0
- package/dist/host/journey-host.cjs.map +1 -0
- package/dist/host/journey-host.d.cts +118 -0
- package/dist/host/journey-host.d.cts.map +1 -0
- package/dist/host/journey-host.d.mts +118 -0
- package/dist/host/journey-host.d.mts.map +1 -0
- package/dist/host/journey-host.mjs +219 -0
- package/dist/host/journey-host.mjs.map +1 -0
- package/dist/host/platform.cjs +20 -0
- package/dist/host/platform.cjs.map +1 -0
- package/dist/host/platform.d.cts +120 -0
- package/dist/host/platform.d.cts.map +1 -0
- package/dist/host/platform.d.mts +120 -0
- package/dist/host/platform.d.mts.map +1 -0
- package/dist/host/platform.mjs +18 -0
- package/dist/host/platform.mjs.map +1 -0
- package/dist/host/senders.cjs +71 -0
- package/dist/host/senders.cjs.map +1 -0
- package/dist/host/senders.d.cts +34 -0
- package/dist/host/senders.d.cts.map +1 -0
- package/dist/host/senders.d.mts +34 -0
- package/dist/host/senders.d.mts.map +1 -0
- package/dist/host/senders.mjs +67 -0
- package/dist/host/senders.mjs.map +1 -0
- package/dist/host/workflow-cache.cjs +47 -0
- package/dist/host/workflow-cache.cjs.map +1 -0
- package/dist/host/workflow-cache.d.cts +21 -0
- package/dist/host/workflow-cache.d.cts.map +1 -0
- package/dist/host/workflow-cache.d.mts +21 -0
- package/dist/host/workflow-cache.d.mts.map +1 -0
- package/dist/host/workflow-cache.mjs +46 -0
- package/dist/host/workflow-cache.mjs.map +1 -0
- package/dist/http2-client.cjs +89 -0
- package/dist/http2-client.cjs.map +1 -0
- package/dist/http2-client.d.cts +45 -0
- package/dist/http2-client.d.cts.map +1 -0
- package/dist/http2-client.d.mts +45 -0
- package/dist/http2-client.d.mts.map +1 -0
- package/dist/http2-client.mjs +87 -0
- package/dist/http2-client.mjs.map +1 -0
- package/dist/index.cjs +130 -0
- package/dist/index.d.cts +21 -0
- package/dist/index.d.mts +21 -0
- package/dist/index.mjs +20 -0
- package/dist/inflight.cjs +116 -0
- package/dist/inflight.cjs.map +1 -0
- package/dist/inflight.d.cts +49 -0
- package/dist/inflight.d.cts.map +1 -0
- package/dist/inflight.d.mts +49 -0
- package/dist/inflight.d.mts.map +1 -0
- package/dist/inflight.mjs +113 -0
- package/dist/inflight.mjs.map +1 -0
- package/dist/ir.cjs +429 -0
- package/dist/ir.cjs.map +1 -0
- package/dist/ir.d.cts +593 -0
- package/dist/ir.d.cts.map +1 -0
- package/dist/ir.d.mts +593 -0
- package/dist/ir.d.mts.map +1 -0
- package/dist/ir.mjs +404 -0
- package/dist/ir.mjs.map +1 -0
- package/dist/jwt.cjs +17 -0
- package/dist/jwt.cjs.map +1 -0
- package/dist/jwt.d.cts +7 -0
- package/dist/jwt.d.cts.map +1 -0
- package/dist/jwt.d.mts +7 -0
- package/dist/jwt.d.mts.map +1 -0
- package/dist/jwt.mjs +15 -0
- package/dist/jwt.mjs.map +1 -0
- package/dist/node/index.cjs +231 -0
- package/dist/node/index.cjs.map +1 -0
- package/dist/node/index.d.cts +59 -0
- package/dist/node/index.d.cts.map +1 -0
- package/dist/node/index.d.mts +59 -0
- package/dist/node/index.d.mts.map +1 -0
- package/dist/node/index.mjs +225 -0
- package/dist/node/index.mjs.map +1 -0
- package/dist/node/runs.cjs +222 -0
- package/dist/node/runs.cjs.map +1 -0
- package/dist/node/runs.d.cts +93 -0
- package/dist/node/runs.d.cts.map +1 -0
- package/dist/node/runs.d.mts +93 -0
- package/dist/node/runs.d.mts.map +1 -0
- package/dist/node/runs.mjs +221 -0
- package/dist/node/runs.mjs.map +1 -0
- package/dist/node/runtime.cjs +351 -0
- package/dist/node/runtime.cjs.map +1 -0
- package/dist/node/runtime.d.cts +96 -0
- package/dist/node/runtime.d.cts.map +1 -0
- package/dist/node/runtime.d.mts +96 -0
- package/dist/node/runtime.d.mts.map +1 -0
- package/dist/node/runtime.mjs +348 -0
- package/dist/node/runtime.mjs.map +1 -0
- package/dist/plan.cjs +221 -0
- package/dist/plan.cjs.map +1 -0
- package/dist/plan.d.cts +68 -0
- package/dist/plan.d.cts.map +1 -0
- package/dist/plan.d.mts +68 -0
- package/dist/plan.d.mts.map +1 -0
- package/dist/plan.mjs +218 -0
- package/dist/plan.mjs.map +1 -0
- package/dist/postgres-js.cjs +51 -0
- package/dist/postgres-js.cjs.map +1 -0
- package/dist/postgres-js.d.cts +32 -0
- package/dist/postgres-js.d.cts.map +1 -0
- package/dist/postgres-js.d.mts +32 -0
- package/dist/postgres-js.d.mts.map +1 -0
- package/dist/postgres-js.mjs +46 -0
- package/dist/postgres-js.mjs.map +1 -0
- package/dist/provenance.cjs +66 -0
- package/dist/provenance.cjs.map +1 -0
- package/dist/provenance.d.cts +40 -0
- package/dist/provenance.d.cts.map +1 -0
- package/dist/provenance.d.mts +40 -0
- package/dist/provenance.d.mts.map +1 -0
- package/dist/provenance.mjs +64 -0
- package/dist/provenance.mjs.map +1 -0
- package/dist/push.cjs +11 -0
- package/dist/push.cjs.map +1 -0
- package/dist/push.d.cts +14 -0
- package/dist/push.d.cts.map +1 -0
- package/dist/push.d.mts +14 -0
- package/dist/push.d.mts.map +1 -0
- package/dist/push.mjs +10 -0
- package/dist/push.mjs.map +1 -0
- package/dist/react-email.cjs +56 -0
- package/dist/react-email.cjs.map +1 -0
- package/dist/react-email.d.cts +5 -0
- package/dist/react-email.d.cts.map +1 -0
- package/dist/react-email.d.mts +5 -0
- package/dist/react-email.d.mts.map +1 -0
- package/dist/react-email.mjs +55 -0
- package/dist/react-email.mjs.map +1 -0
- package/dist/reporting/classify.cjs +46 -0
- package/dist/reporting/classify.cjs.map +1 -0
- package/dist/reporting/classify.d.cts +65 -0
- package/dist/reporting/classify.d.cts.map +1 -0
- package/dist/reporting/classify.d.mts +65 -0
- package/dist/reporting/classify.d.mts.map +1 -0
- package/dist/reporting/classify.mjs +42 -0
- package/dist/reporting/classify.mjs.map +1 -0
- package/dist/reporting/observer.cjs +137 -0
- package/dist/reporting/observer.cjs.map +1 -0
- package/dist/reporting/observer.d.cts +31 -0
- package/dist/reporting/observer.d.cts.map +1 -0
- package/dist/reporting/observer.d.mts +31 -0
- package/dist/reporting/observer.d.mts.map +1 -0
- package/dist/reporting/observer.mjs +134 -0
- package/dist/reporting/observer.mjs.map +1 -0
- package/dist/reporting/tracking.cjs +185 -0
- package/dist/reporting/tracking.cjs.map +1 -0
- package/dist/reporting/tracking.d.cts +63 -0
- package/dist/reporting/tracking.d.cts.map +1 -0
- package/dist/reporting/tracking.d.mts +63 -0
- package/dist/reporting/tracking.d.mts.map +1 -0
- package/dist/reporting/tracking.mjs +182 -0
- package/dist/reporting/tracking.mjs.map +1 -0
- package/dist/runtime.cjs +18 -0
- package/dist/runtime.cjs.map +1 -0
- package/dist/runtime.d.cts +44 -0
- package/dist/runtime.d.cts.map +1 -0
- package/dist/runtime.d.mts +44 -0
- package/dist/runtime.d.mts.map +1 -0
- package/dist/runtime.mjs +17 -0
- package/dist/runtime.mjs.map +1 -0
- package/dist/store/condition-sql.cjs +145 -0
- package/dist/store/condition-sql.cjs.map +1 -0
- package/dist/store/condition-sql.d.cts +38 -0
- package/dist/store/condition-sql.d.cts.map +1 -0
- package/dist/store/condition-sql.d.mts +38 -0
- package/dist/store/condition-sql.d.mts.map +1 -0
- package/dist/store/condition-sql.mjs +144 -0
- package/dist/store/condition-sql.mjs.map +1 -0
- package/dist/store/erasure.cjs +21 -0
- package/dist/store/erasure.cjs.map +1 -0
- package/dist/store/erasure.d.cts +16 -0
- package/dist/store/erasure.d.cts.map +1 -0
- package/dist/store/erasure.d.mts +16 -0
- package/dist/store/erasure.d.mts.map +1 -0
- package/dist/store/erasure.mjs +20 -0
- package/dist/store/erasure.mjs.map +1 -0
- package/dist/store/facts.cjs +79 -0
- package/dist/store/facts.cjs.map +1 -0
- package/dist/store/facts.d.cts +41 -0
- package/dist/store/facts.d.cts.map +1 -0
- package/dist/store/facts.d.mts +41 -0
- package/dist/store/facts.d.mts.map +1 -0
- package/dist/store/facts.mjs +77 -0
- package/dist/store/facts.mjs.map +1 -0
- package/dist/store/identity.cjs +34 -0
- package/dist/store/identity.cjs.map +1 -0
- package/dist/store/identity.d.cts +13 -0
- package/dist/store/identity.d.cts.map +1 -0
- package/dist/store/identity.d.mts +13 -0
- package/dist/store/identity.d.mts.map +1 -0
- package/dist/store/identity.mjs +30 -0
- package/dist/store/identity.mjs.map +1 -0
- package/dist/store/index.cjs +35 -0
- package/dist/store/index.cjs.map +1 -0
- package/dist/store/index.d.cts +766 -0
- package/dist/store/index.d.cts.map +1 -0
- package/dist/store/index.d.mts +766 -0
- package/dist/store/index.d.mts.map +1 -0
- package/dist/store/index.mjs +22 -0
- package/dist/store/index.mjs.map +1 -0
- package/dist/store/postgres-reporting.cjs +327 -0
- package/dist/store/postgres-reporting.cjs.map +1 -0
- package/dist/store/postgres-reporting.d.cts +95 -0
- package/dist/store/postgres-reporting.d.cts.map +1 -0
- package/dist/store/postgres-reporting.d.mts +95 -0
- package/dist/store/postgres-reporting.d.mts.map +1 -0
- package/dist/store/postgres-reporting.mjs +326 -0
- package/dist/store/postgres-reporting.mjs.map +1 -0
- package/dist/store/postgres.cjs +1173 -0
- package/dist/store/postgres.cjs.map +1 -0
- package/dist/store/postgres.d.cts +199 -0
- package/dist/store/postgres.d.cts.map +1 -0
- package/dist/store/postgres.d.mts +199 -0
- package/dist/store/postgres.d.mts.map +1 -0
- package/dist/store/postgres.mjs +1170 -0
- package/dist/store/postgres.mjs.map +1 -0
- package/dist/store/sql.cjs +24 -0
- package/dist/store/sql.cjs.map +1 -0
- package/dist/store/sql.d.cts +26 -0
- package/dist/store/sql.d.cts.map +1 -0
- package/dist/store/sql.d.mts +26 -0
- package/dist/store/sql.d.mts.map +1 -0
- package/dist/store/sql.mjs +23 -0
- package/dist/store/sql.mjs.map +1 -0
- package/dist/store/tables.cjs +660 -0
- package/dist/store/tables.cjs.map +1 -0
- package/dist/store/tables.d.cts +103 -0
- package/dist/store/tables.d.cts.map +1 -0
- package/dist/store/tables.d.mts +103 -0
- package/dist/store/tables.d.mts.map +1 -0
- package/dist/store/tables.mjs +653 -0
- package/dist/store/tables.mjs.map +1 -0
- package/dist/store/user-lock.cjs +42 -0
- package/dist/store/user-lock.cjs.map +1 -0
- package/dist/store/user-lock.d.cts +30 -0
- package/dist/store/user-lock.d.cts.map +1 -0
- package/dist/store/user-lock.d.mts +30 -0
- package/dist/store/user-lock.d.mts.map +1 -0
- package/dist/store/user-lock.mjs +39 -0
- package/dist/store/user-lock.mjs.map +1 -0
- package/dist/template-rules.cjs +94 -0
- package/dist/template-rules.cjs.map +1 -0
- package/dist/template-rules.d.cts +80 -0
- package/dist/template-rules.d.cts.map +1 -0
- package/dist/template-rules.d.mts +80 -0
- package/dist/template-rules.d.mts.map +1 -0
- package/dist/template-rules.mjs +86 -0
- package/dist/template-rules.mjs.map +1 -0
- package/dist/templates.cjs +375 -0
- package/dist/templates.cjs.map +1 -0
- package/dist/templates.d.cts +358 -0
- package/dist/templates.d.cts.map +1 -0
- package/dist/templates.d.mts +358 -0
- package/dist/templates.d.mts.map +1 -0
- package/dist/templates.mjs +353 -0
- package/dist/templates.mjs.map +1 -0
- package/dist/transports/cloudflare-email.cjs +69 -0
- package/dist/transports/cloudflare-email.cjs.map +1 -0
- package/dist/transports/cloudflare-email.d.cts +63 -0
- package/dist/transports/cloudflare-email.d.cts.map +1 -0
- package/dist/transports/cloudflare-email.d.mts +63 -0
- package/dist/transports/cloudflare-email.d.mts.map +1 -0
- package/dist/transports/cloudflare-email.mjs +67 -0
- package/dist/transports/cloudflare-email.mjs.map +1 -0
- package/dist/transports/ses.cjs +142 -0
- package/dist/transports/ses.cjs.map +1 -0
- package/dist/transports/ses.d.cts +49 -0
- package/dist/transports/ses.d.cts.map +1 -0
- package/dist/transports/ses.d.mts +49 -0
- package/dist/transports/ses.d.mts.map +1 -0
- package/dist/transports/ses.mjs +139 -0
- package/dist/transports/ses.mjs.map +1 -0
- package/dist/transports/sigv4.cjs +86 -0
- package/dist/transports/sigv4.cjs.map +1 -0
- package/dist/transports/sigv4.d.cts +25 -0
- package/dist/transports/sigv4.d.cts.map +1 -0
- package/dist/transports/sigv4.d.mts +25 -0
- package/dist/transports/sigv4.d.mts.map +1 -0
- package/dist/transports/sigv4.mjs +85 -0
- package/dist/transports/sigv4.mjs.map +1 -0
- package/dist/webhooks.cjs +97 -0
- package/dist/webhooks.cjs.map +1 -0
- package/dist/webhooks.d.cts +23 -0
- package/dist/webhooks.d.cts.map +1 -0
- package/dist/webhooks.d.mts +23 -0
- package/dist/webhooks.d.mts.map +1 -0
- package/dist/webhooks.mjs +95 -0
- package/dist/webhooks.mjs.map +1 -0
- package/dist/webpush.cjs +361 -0
- package/dist/webpush.cjs.map +1 -0
- package/dist/webpush.d.cts +135 -0
- package/dist/webpush.d.cts.map +1 -0
- package/dist/webpush.d.mts +135 -0
- package/dist/webpush.d.mts.map +1 -0
- package/dist/webpush.mjs +350 -0
- package/dist/webpush.mjs.map +1 -0
- package/package.json +270 -31
- package/src/cloudflare/workers.d.ts +44 -0
- package/index.cjs +0 -18
- package/index.d.cts +0 -4
- package/index.d.mts +0 -4
- package/index.mjs +0 -1
|
@@ -0,0 +1,1173 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_date_time = require("../date-time.cjs");
|
|
3
|
+
const require_ir = require("../ir.cjs");
|
|
4
|
+
const require_store_condition_sql = require("./condition-sql.cjs");
|
|
5
|
+
const require_store_erasure = require("./erasure.cjs");
|
|
6
|
+
const require_store_identity = require("./identity.cjs");
|
|
7
|
+
const require_store_postgres_reporting = require("./postgres-reporting.cjs");
|
|
8
|
+
const require_store_sql = require("./sql.cjs");
|
|
9
|
+
const require_store_tables = require("./tables.cjs");
|
|
10
|
+
const require_store_user_lock = require("./user-lock.cjs");
|
|
11
|
+
// src/store/postgres.ts
|
|
12
|
+
/** Who a device row belongs to: its user, or before sign-in its visitor. */
|
|
13
|
+
function deviceOwner(row) {
|
|
14
|
+
if (row.user_id !== null) return row.user_id;
|
|
15
|
+
return row.visitor_id === null ? void 0 : require_store_identity.visitorSubject(row.visitor_id);
|
|
16
|
+
}
|
|
17
|
+
/** information_schema's udt_name per column type. */
|
|
18
|
+
const UDT = {
|
|
19
|
+
uuid: "uuid",
|
|
20
|
+
text: "text",
|
|
21
|
+
jsonb: "jsonb",
|
|
22
|
+
timestamptz: "timestamptz",
|
|
23
|
+
integer: "int4",
|
|
24
|
+
boolean: "bool"
|
|
25
|
+
};
|
|
26
|
+
/** Epoch milliseconds → timestamptz, so the tables keep the database's native type. */
|
|
27
|
+
const TS = (param) => `to_timestamp(${param}::double precision / 1000)`;
|
|
28
|
+
/** A timestamptz column as epoch milliseconds, the inverse of `TS`. */
|
|
29
|
+
const MS = (column) => `(extract(epoch FROM ${column}) * 1000)::double precision`;
|
|
30
|
+
function asJson(value) {
|
|
31
|
+
return typeof value === "string" ? JSON.parse(value) : value;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The journey data plane on Postgres: native types where Postgres has them
|
|
35
|
+
* (jsonb, timestamptz), and the concurrency guarantees come from the database: the user lock for the entry ledger, ON CONFLICT for
|
|
36
|
+
* idempotent subscriptions, a transaction for the route swap, a jsonb merge
|
|
37
|
+
* for /identify. The tables are declared in tables.ts.
|
|
38
|
+
*
|
|
39
|
+
* Every write about a user is conditional on their erasure tombstone not existing
|
|
40
|
+
* (`INSERT ... SELECT ... WHERE NOT EXISTS`, see `notErased`), and runs in a transaction that
|
|
41
|
+
* holds the user's advisory lock (`lockUser`, the protocol in user-lock.ts): an event, a
|
|
42
|
+
* profile merge, a device, a subscription, a segment membership with its starts, an entry, an
|
|
43
|
+
* admission, and the reporting's instance and message rows (postgres-reporting.ts). The
|
|
44
|
+
* erasure's two steps, `requestErasure` and `forgetUser`, take the same lock. Under READ
|
|
45
|
+
* COMMITTED a conditional write sees only what was committed when its statement began, and an
|
|
46
|
+
* erasure sees only rows committed when it looked — so without the lock a write's transaction
|
|
47
|
+
* could commit its rows after the erase deleted everything (the user comes back), and a runner
|
|
48
|
+
* could admit itself after the tombstone (the instance runs on). With it, every write and the
|
|
49
|
+
* erasure of one user run one after another: a write either commits before the tombstone,
|
|
50
|
+
* where the erasure finds and deletes it, or waits for the lock, reads the tombstone and is
|
|
51
|
+
* refused (`UserErasedError`). Nothing lands after the erase, so there is no second pass. The
|
|
52
|
+
* conditional statement stays the in-transaction check; a write that wrote nothing reads the
|
|
53
|
+
* tombstone on that path only, to tell a refusal from a plain no-op (`refuseIfErased`).
|
|
54
|
+
* Suppressions are the one exception: an opt-out is written whatever the user's state.
|
|
55
|
+
*/
|
|
56
|
+
var PostgresJourneyStore = class {
|
|
57
|
+
sql;
|
|
58
|
+
t;
|
|
59
|
+
schema;
|
|
60
|
+
reporting;
|
|
61
|
+
executions;
|
|
62
|
+
constructor(sql, options = {}) {
|
|
63
|
+
this.sql = sql;
|
|
64
|
+
this.schema = options.schema ?? "workflow";
|
|
65
|
+
const qualify = (name) => `"${this.schema}"."${name}"`;
|
|
66
|
+
this.t = Object.fromEntries(Object.keys(require_store_tables.TABLES).map((key) => [key, qualify(require_store_tables.TABLES[key].name)]));
|
|
67
|
+
this.reporting = new require_store_postgres_reporting.PostgresReporting(sql, this.t, this.schema);
|
|
68
|
+
this.executions = new PostgresExecutions(sql, this.t.execution, this.t.entry);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* What the live database lacks against tables.ts: missing tables, missing columns, a column
|
|
72
|
+
* of another type or nullability, and missing unique indexes (the store's ON CONFLICT targets
|
|
73
|
+
* — without one the first ingest fails, not the deploy). Read-only — the engine never migrates
|
|
74
|
+
* the host's database; a deploy fails with these problems listed, and the fix is to regenerate
|
|
75
|
+
* the drizzle file and run the host's migrations.
|
|
76
|
+
*/
|
|
77
|
+
async checkSchema() {
|
|
78
|
+
const { columns, indexes } = await this.sql.transaction(async (tx) => ({
|
|
79
|
+
columns: await tx.query(`SELECT table_name, column_name, udt_name, is_nullable FROM information_schema.columns WHERE table_schema = $1`, [this.schema]),
|
|
80
|
+
indexes: await tx.query(`SELECT tablename, indexname, indexdef FROM pg_indexes WHERE schemaname = $1`, [this.schema])
|
|
81
|
+
}));
|
|
82
|
+
const live = new Map(columns.map((row) => [`${row.table_name}.${row.column_name}`, row]));
|
|
83
|
+
const tables = new Set(columns.map((row) => row.table_name));
|
|
84
|
+
const liveIndexes = new Map(indexes.map((row) => [`${row.tablename}.${row.indexname}`, row]));
|
|
85
|
+
const problems = [];
|
|
86
|
+
for (const spec of Object.values(require_store_tables.TABLES)) {
|
|
87
|
+
const where = `"${this.schema}"."${spec.name}"`;
|
|
88
|
+
if (!tables.has(spec.name)) {
|
|
89
|
+
problems.push(`table ${where} is missing`);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
for (const [key, column] of Object.entries(spec.columns)) {
|
|
93
|
+
const name = require_store_tables.columnName(key);
|
|
94
|
+
const row = live.get(`${spec.name}.${name}`);
|
|
95
|
+
if (row === void 0) problems.push(`column ${where}."${name}" is missing`);
|
|
96
|
+
else if (row.udt_name !== UDT[column.type]) problems.push(`column ${where}."${name}" is ${row.udt_name}, expected ${UDT[column.type]}`);
|
|
97
|
+
else if (row.is_nullable === "YES" !== (column.nullable === true)) problems.push(`column ${where}."${name}" should be ${column.nullable ? "nullable" : "NOT NULL"}`);
|
|
98
|
+
}
|
|
99
|
+
for (const keys of spec.uniqueIndexes ?? []) {
|
|
100
|
+
const name = require_store_tables.indexName(spec, keys);
|
|
101
|
+
const row = liveIndexes.get(`${spec.name}.${name}`);
|
|
102
|
+
if (row === void 0) problems.push(`missing unique index "${this.schema}"."${name}"`);
|
|
103
|
+
else if (!/^CREATE UNIQUE INDEX/i.test(row.indexdef)) problems.push(`index "${this.schema}"."${name}" is not unique`);
|
|
104
|
+
}
|
|
105
|
+
for (const unique of spec.uniqueConstraints ?? []) {
|
|
106
|
+
const name = require_store_tables.uniqueConstraintName(spec, unique);
|
|
107
|
+
const row = liveIndexes.get(`${spec.name}.${name}`);
|
|
108
|
+
if (row === void 0) problems.push(`missing unique constraint "${this.schema}"."${name}"`);
|
|
109
|
+
else if (!/^CREATE UNIQUE INDEX/i.test(row.indexdef)) problems.push(`index "${this.schema}"."${name}" is not unique`);
|
|
110
|
+
else if (!/NULLS NOT DISTINCT/i.test(row.indexdef)) problems.push(`unique constraint "${this.schema}"."${name}" must be NULLS NOT DISTINCT`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return problems;
|
|
114
|
+
}
|
|
115
|
+
async insertEvent(input) {
|
|
116
|
+
return this.sql.transaction(async (tx) => {
|
|
117
|
+
await this.lockUser(tx, input.userId);
|
|
118
|
+
if ((await tx.query(`INSERT INTO ${this.t.event} (id, event_id, user_id, name, ts, payload)
|
|
119
|
+
SELECT $1::uuid, $2, $3, $4, ${TS("$5")}, $6::text::jsonb ${this.notErased("$3")}
|
|
120
|
+
ON CONFLICT (user_id, event_id) DO NOTHING RETURNING id`, [
|
|
121
|
+
require_store_sql.uuidv7(input.ts),
|
|
122
|
+
input.id,
|
|
123
|
+
input.userId,
|
|
124
|
+
input.event,
|
|
125
|
+
input.ts,
|
|
126
|
+
JSON.stringify(input.payload)
|
|
127
|
+
])).length > 0) return true;
|
|
128
|
+
await this.refuseIfErased(tx, input.userId);
|
|
129
|
+
return false;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
async storedEvent(userId, id) {
|
|
133
|
+
const row = (await this.sql.transaction((tx) => tx.query(`SELECT name, (extract(epoch from ts) * 1000)::double precision AS ts, payload
|
|
134
|
+
FROM ${this.t.event} WHERE user_id = $1 AND event_id = $2`, [userId, id]))).at(0);
|
|
135
|
+
if (row === void 0) return void 0;
|
|
136
|
+
return {
|
|
137
|
+
event: row.name,
|
|
138
|
+
ts: Math.round(Number(row.ts)),
|
|
139
|
+
payload: asJson(row.payload)
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
/** The condition every write about a user carries: no erasure tombstone for `$user`. */
|
|
143
|
+
notErased(user) {
|
|
144
|
+
return `WHERE NOT EXISTS (SELECT 1 FROM ${this.t.erasure} WHERE user_id = ${user})`;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* A conditional write wrote nothing: on that path only, read why. Throws when the tombstone
|
|
148
|
+
* refused it; returns when the statement had another reason (a duplicate, an entry taken).
|
|
149
|
+
*/
|
|
150
|
+
async refuseIfErased(sql, userId) {
|
|
151
|
+
if ((await sql.query(`SELECT 1 AS x FROM ${this.t.erasure} WHERE user_id = $1`, [userId])).length > 0) throw new require_store_erasure.UserErasedError(userId);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The first two statements of every transaction that writes about the user (user-lock.ts):
|
|
155
|
+
* READ COMMITTED, then the user's advisory lock, held until the transaction ends. Any read
|
|
156
|
+
* about the user comes after, on a snapshot taken once the lock is held.
|
|
157
|
+
*/
|
|
158
|
+
lockUser(tx, userId) {
|
|
159
|
+
return require_store_user_lock.lockUserTransaction(tx, this.schema, userId);
|
|
160
|
+
}
|
|
161
|
+
/** The event rows of one user and name, within the window's bounds (both inclusive). */
|
|
162
|
+
eventWindow(userId, event, window) {
|
|
163
|
+
const params = [userId, event];
|
|
164
|
+
let where = "user_id = $1 AND name = $2";
|
|
165
|
+
if (window?.sinceMs !== void 0) {
|
|
166
|
+
params.push(window.sinceMs);
|
|
167
|
+
where += ` AND ts >= ${TS(`$${params.length}`)}`;
|
|
168
|
+
}
|
|
169
|
+
if (window?.untilMs !== void 0) {
|
|
170
|
+
params.push(window.untilMs);
|
|
171
|
+
where += ` AND ts <= ${TS(`$${params.length}`)}`;
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
where,
|
|
175
|
+
params
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
async countEvents(userId, event, window, atLeast) {
|
|
179
|
+
const { where, params } = this.eventWindow(userId, event, window);
|
|
180
|
+
const capped = atLeast !== void 0 && Number.isInteger(atLeast) && atLeast > 0;
|
|
181
|
+
if (capped) params.push(atLeast);
|
|
182
|
+
const rows = await this.sql.query(capped ? `SELECT count(*)::int AS c FROM (SELECT 1 FROM ${this.t.event} WHERE ${where} LIMIT $${params.length}) capped` : `SELECT count(*)::int AS c FROM ${this.t.event} WHERE ${where}`, params);
|
|
183
|
+
return Number(rows.at(0)?.c ?? 0);
|
|
184
|
+
}
|
|
185
|
+
async pageEventPayloads(userId, event, window, page) {
|
|
186
|
+
const { where, params } = this.eventWindow(userId, event, window);
|
|
187
|
+
let keyset = "";
|
|
188
|
+
if (page.after !== void 0) {
|
|
189
|
+
params.push(page.after.ts, page.after.id);
|
|
190
|
+
keyset = ` AND (ts, id) > ($${params.length - 1}::timestamptz, $${params.length}::uuid)`;
|
|
191
|
+
}
|
|
192
|
+
params.push(page.limit);
|
|
193
|
+
const rows = await this.sql.query(`SELECT payload, ts::text AS ts, id::text AS id FROM ${this.t.event}
|
|
194
|
+
WHERE ${where}${keyset} ORDER BY ts, id LIMIT $${params.length}`, params);
|
|
195
|
+
const last = rows.at(-1);
|
|
196
|
+
return {
|
|
197
|
+
payloads: rows.map((row) => asJson(row.payload)),
|
|
198
|
+
...last !== void 0 && rows.length === page.limit ? { next: {
|
|
199
|
+
ts: last.ts,
|
|
200
|
+
id: last.id
|
|
201
|
+
} } : {}
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
async getProfile(userId) {
|
|
205
|
+
const row = (await this.sql.query(`SELECT props FROM ${this.t.profile} WHERE user_id = $1`, [userId])).at(0);
|
|
206
|
+
return row === void 0 ? void 0 : asJson(row.props);
|
|
207
|
+
}
|
|
208
|
+
async mergeProfile(userId, props, at = Date.now()) {
|
|
209
|
+
return this.sql.transaction(async (tx) => {
|
|
210
|
+
await this.lockUser(tx, userId);
|
|
211
|
+
const times = asJson((await tx.query(`SELECT props_at FROM ${this.t.profile} WHERE user_id = $1`, [userId]))[0]?.props_at ?? null) ?? {};
|
|
212
|
+
const patch = Object.fromEntries(Object.entries(props).filter(([key]) => (times[key] ?? Number.NEGATIVE_INFINITY) <= at));
|
|
213
|
+
const stamped = {
|
|
214
|
+
...times,
|
|
215
|
+
...Object.fromEntries(Object.keys(patch).map((k) => [k, at]))
|
|
216
|
+
};
|
|
217
|
+
const rows = await tx.query(`INSERT INTO ${this.t.profile} (user_id, props, props_at)
|
|
218
|
+
SELECT $1, jsonb_strip_nulls($2::text::jsonb), $4::text::jsonb ${this.notErased("$1")}
|
|
219
|
+
ON CONFLICT (user_id) DO UPDATE SET
|
|
220
|
+
props = jsonb_strip_nulls(${this.t.profile}.props || $3::text::jsonb),
|
|
221
|
+
props_at = $4::text::jsonb
|
|
222
|
+
RETURNING props`, [
|
|
223
|
+
userId,
|
|
224
|
+
JSON.stringify(patch),
|
|
225
|
+
JSON.stringify(patch),
|
|
226
|
+
JSON.stringify(stamped)
|
|
227
|
+
]);
|
|
228
|
+
if (rows.length === 0) throw new require_store_erasure.UserErasedError(userId);
|
|
229
|
+
return asJson(rows[0].props);
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
async listSegmentCandidates(page) {
|
|
233
|
+
if (page.segments.length === 0) return [];
|
|
234
|
+
const params = [];
|
|
235
|
+
const bind = (value) => {
|
|
236
|
+
params.push(value);
|
|
237
|
+
return `$${params.length}`;
|
|
238
|
+
};
|
|
239
|
+
const definitions = new Map(page.segments.map((segment) => [segment.id, segment.condition]));
|
|
240
|
+
const branches = page.segments.map(({ id, condition }) => {
|
|
241
|
+
const segment = bind(id);
|
|
242
|
+
const matches = require_store_condition_sql.conditionToSql(condition, {
|
|
243
|
+
tables: this.t,
|
|
244
|
+
user: "u.user_id",
|
|
245
|
+
props: "p.props",
|
|
246
|
+
nowMs: page.now,
|
|
247
|
+
segments: definitions,
|
|
248
|
+
bind
|
|
249
|
+
});
|
|
250
|
+
const key = `segment = ${segment}::text AND user_id = u.user_id`;
|
|
251
|
+
return `SELECT ${segment}::text AS segment
|
|
252
|
+
WHERE (${matches}) IS DISTINCT FROM EXISTS (SELECT 1 FROM ${this.t.segmentMember} WHERE ${key})
|
|
253
|
+
OR EXISTS (SELECT 1 FROM ${this.t.segmentStart} WHERE ${key})`;
|
|
254
|
+
});
|
|
255
|
+
let keyset = "";
|
|
256
|
+
if (page.after !== void 0) keyset = ` AND user_id > ${bind(page.after)}::text`;
|
|
257
|
+
return (await this.sql.query(`WITH u AS MATERIALIZED (
|
|
258
|
+
SELECT user_id FROM (
|
|
259
|
+
SELECT user_id FROM ${this.t.profile}
|
|
260
|
+
UNION SELECT user_id FROM ${this.t.event}
|
|
261
|
+
UNION SELECT user_id FROM ${this.t.segmentMember}
|
|
262
|
+
) known
|
|
263
|
+
-- Segments are about users: a visitor is not one until they sign in
|
|
264
|
+
WHERE user_id NOT LIKE '${require_store_identity.visitorSubject("")}%'${keyset}
|
|
265
|
+
)
|
|
266
|
+
SELECT u.user_id, json_agg(c.segment) AS segments
|
|
267
|
+
FROM u
|
|
268
|
+
LEFT JOIN ${this.t.profile} p ON p.user_id = u.user_id
|
|
269
|
+
CROSS JOIN LATERAL (${branches.join("\n UNION ALL ")}) c
|
|
270
|
+
GROUP BY u.user_id
|
|
271
|
+
ORDER BY u.user_id
|
|
272
|
+
LIMIT ${bind(page.limit)}::int`, params)).map((row) => ({
|
|
273
|
+
userId: row.user_id,
|
|
274
|
+
segments: asJson(row.segments)
|
|
275
|
+
}));
|
|
276
|
+
}
|
|
277
|
+
async listDateCandidates(page) {
|
|
278
|
+
const params = [];
|
|
279
|
+
const bind = (value) => {
|
|
280
|
+
params.push(value);
|
|
281
|
+
return `$${params.length}`;
|
|
282
|
+
};
|
|
283
|
+
let matches = "TRUE";
|
|
284
|
+
if (page.filter !== void 0) {
|
|
285
|
+
const segments = new Map((await this.listSegments()).map((s) => [s.id, s.condition]));
|
|
286
|
+
matches = require_store_condition_sql.conditionToSql(page.filter, {
|
|
287
|
+
tables: this.t,
|
|
288
|
+
user: "u.user_id",
|
|
289
|
+
props: "p.props",
|
|
290
|
+
nowMs: page.now,
|
|
291
|
+
segments,
|
|
292
|
+
bind
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
let keyset = "";
|
|
296
|
+
if (page.after !== void 0) keyset = ` AND user_id > ${bind(page.after)}::text`;
|
|
297
|
+
return (await this.sql.query(`WITH u AS MATERIALIZED (
|
|
298
|
+
SELECT user_id FROM (
|
|
299
|
+
SELECT user_id FROM ${this.t.profile}
|
|
300
|
+
UNION SELECT user_id FROM ${this.t.event}
|
|
301
|
+
) known
|
|
302
|
+
-- A date trigger is about users: a visitor is not one until they sign in
|
|
303
|
+
WHERE user_id NOT LIKE '${require_store_identity.visitorSubject("")}%'${keyset}
|
|
304
|
+
)
|
|
305
|
+
SELECT u.user_id, m.matches IS TRUE AS certain
|
|
306
|
+
FROM u
|
|
307
|
+
LEFT JOIN ${this.t.profile} p ON p.user_id = u.user_id
|
|
308
|
+
CROSS JOIN LATERAL (SELECT (${matches}) AS matches) m
|
|
309
|
+
-- NULL is what SQL cannot decide: the evaluator takes it
|
|
310
|
+
WHERE m.matches IS NOT FALSE
|
|
311
|
+
ORDER BY u.user_id
|
|
312
|
+
LIMIT ${bind(page.limit)}::int`, params)).map((row) => ({
|
|
313
|
+
userId: row.user_id,
|
|
314
|
+
certain: row.certain
|
|
315
|
+
}));
|
|
316
|
+
}
|
|
317
|
+
listDevices(subject) {
|
|
318
|
+
const visitorId = require_store_identity.visitorIdOf(subject);
|
|
319
|
+
return this.sql.query(visitorId === void 0 ? `SELECT token, platform FROM ${this.t.device} WHERE user_id = $1 ORDER BY updated_at DESC` : `SELECT token, platform FROM ${this.t.device} WHERE visitor_id = $1 AND user_id IS NULL ORDER BY updated_at DESC`, [visitorId ?? subject]);
|
|
320
|
+
}
|
|
321
|
+
async upsertDevice(device) {
|
|
322
|
+
const { deviceId = null, userId, visitorId = null, token, platform, at } = device;
|
|
323
|
+
const subject = userId ?? require_store_identity.visitorSubject(visitorId ?? "");
|
|
324
|
+
return this.sql.transaction(async (tx) => {
|
|
325
|
+
await this.lockUser(tx, subject);
|
|
326
|
+
const previous = await tx.query(`SELECT token, user_id, visitor_id FROM ${this.t.device} WHERE token = $1 OR device_id = $2 FOR UPDATE`, [token, deviceId]);
|
|
327
|
+
for (const row of previous) {
|
|
328
|
+
if (row.token === token) continue;
|
|
329
|
+
const owner = deviceOwner(row);
|
|
330
|
+
await tx.query(owner === void 0 || owner === subject ? `DELETE FROM ${this.t.device} WHERE token = $1` : `UPDATE ${this.t.device} SET device_id = NULL WHERE token = $1`, [row.token]);
|
|
331
|
+
}
|
|
332
|
+
if ((await tx.query(`INSERT INTO ${this.t.device} (device_id, user_id, visitor_id, token, platform, created_at, updated_at)
|
|
333
|
+
SELECT $1, $2, $3, $4, $5, ${TS("$6")}, ${TS("$6")} ${this.notErased("$7")}
|
|
334
|
+
ON CONFLICT (token) DO UPDATE SET
|
|
335
|
+
device_id = COALESCE(EXCLUDED.device_id, ${this.t.device}.device_id),
|
|
336
|
+
user_id = EXCLUDED.user_id,
|
|
337
|
+
visitor_id = COALESCE(EXCLUDED.visitor_id, ${this.t.device}.visitor_id),
|
|
338
|
+
platform = EXCLUDED.platform,
|
|
339
|
+
updated_at = EXCLUDED.updated_at
|
|
340
|
+
RETURNING token`, [
|
|
341
|
+
deviceId,
|
|
342
|
+
userId,
|
|
343
|
+
visitorId,
|
|
344
|
+
token,
|
|
345
|
+
platform,
|
|
346
|
+
at,
|
|
347
|
+
subject
|
|
348
|
+
])).length === 0) throw new require_store_erasure.UserErasedError(subject);
|
|
349
|
+
const owners = previous.flatMap((row) => {
|
|
350
|
+
const owner = deviceOwner(row);
|
|
351
|
+
return owner === void 0 || owner === subject ? [] : [owner];
|
|
352
|
+
});
|
|
353
|
+
return [...new Set(owners)];
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
async linkedUser(visitorId) {
|
|
357
|
+
return (await this.sql.query(`SELECT user_id FROM ${this.t.visitor} WHERE visitor_id = $1`, [visitorId]))[0]?.user_id ?? void 0;
|
|
358
|
+
}
|
|
359
|
+
async touchVisitor(visitorId, at) {
|
|
360
|
+
return (await this.sql.query(`INSERT INTO ${this.t.visitor} (visitor_id, user_id, created_at, updated_at)
|
|
361
|
+
VALUES ($1, NULL, ${TS("$2")}, ${TS("$2")})
|
|
362
|
+
ON CONFLICT (visitor_id) DO UPDATE SET updated_at = EXCLUDED.updated_at
|
|
363
|
+
RETURNING user_id`, [visitorId, at]))[0]?.user_id ?? require_store_identity.visitorSubject(visitorId);
|
|
364
|
+
}
|
|
365
|
+
async pruneVisitors(before, limit) {
|
|
366
|
+
const idle = `user_id IS NULL AND updated_at < ${TS("$1")} AND NOT EXISTS (
|
|
367
|
+
SELECT 1 FROM ${this.t.entry} e
|
|
368
|
+
WHERE e.user_id = '${require_store_identity.visitorSubject("")}' || visitor_id AND e.status = 'running')`;
|
|
369
|
+
const candidates = await this.sql.query(`SELECT visitor_id FROM ${this.t.visitor} WHERE ${idle} ORDER BY updated_at LIMIT $2`, [before, limit]);
|
|
370
|
+
let pruned = 0;
|
|
371
|
+
for (const { visitor_id: visitorId } of candidates) {
|
|
372
|
+
const subject = require_store_identity.visitorSubject(visitorId);
|
|
373
|
+
await this.sql.transaction(async (tx) => {
|
|
374
|
+
await this.lockUser(tx, subject);
|
|
375
|
+
if ((await tx.query(`DELETE FROM ${this.t.visitor} WHERE visitor_id = $2 AND ${idle} RETURNING visitor_id`, [before, visitorId])).length === 0) return;
|
|
376
|
+
await this.deleteSubjectRows(tx, subject);
|
|
377
|
+
pruned += 1;
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
return pruned;
|
|
381
|
+
}
|
|
382
|
+
async entryOwner(instanceId) {
|
|
383
|
+
return (await this.sql.query(`SELECT user_id FROM ${this.t.entry} WHERE instance_id = $1`, [instanceId]))[0]?.user_id;
|
|
384
|
+
}
|
|
385
|
+
async linkVisitor(visitorId, userId, at) {
|
|
386
|
+
const subject = require_store_identity.visitorSubject(visitorId);
|
|
387
|
+
if (await this.linkedUser(visitorId) !== void 0) return null;
|
|
388
|
+
return this.sql.transaction(async (tx) => {
|
|
389
|
+
await require_store_user_lock.beginReadCommitted(tx);
|
|
390
|
+
for (const key of [subject, userId].sort()) await require_store_user_lock.lockUser(tx, this.schema, key);
|
|
391
|
+
if ((await tx.query(`INSERT INTO ${this.t.visitor} (visitor_id, user_id, created_at, updated_at)
|
|
392
|
+
SELECT $1, $2, ${TS("$3")}, ${TS("$3")} ${this.notErased("$2")}
|
|
393
|
+
ON CONFLICT (visitor_id) DO UPDATE SET user_id = EXCLUDED.user_id, updated_at = EXCLUDED.updated_at
|
|
394
|
+
WHERE ${this.t.visitor}.user_id IS NULL
|
|
395
|
+
RETURNING visitor_id`, [
|
|
396
|
+
visitorId,
|
|
397
|
+
userId,
|
|
398
|
+
at
|
|
399
|
+
])).length === 0) {
|
|
400
|
+
if ((await tx.query(`SELECT 1 FROM ${this.t.erasure} WHERE user_id = $1`, [userId])).length > 0) throw new require_store_erasure.UserErasedError(userId);
|
|
401
|
+
return null;
|
|
402
|
+
}
|
|
403
|
+
const droppedIds = (await tx.query(`DELETE FROM ${this.t.entry} v USING ${this.t.entry} u
|
|
404
|
+
WHERE v.user_id = $1 AND u.user_id = $2 AND u.workflow = v.workflow
|
|
405
|
+
RETURNING v.instance_id`, [subject, userId])).map((row) => row.instance_id);
|
|
406
|
+
await tx.query(`DELETE FROM ${this.t.subscription} WHERE wake_handle = ANY($1::text[])`, [droppedIds]);
|
|
407
|
+
await tx.query(`DELETE FROM ${this.t.execution} WHERE instance_id = ANY($1::text[])`, [droppedIds]);
|
|
408
|
+
await tx.query(`DELETE FROM ${this.t.event} v USING ${this.t.event} u
|
|
409
|
+
WHERE v.user_id = $1 AND u.user_id = $2 AND u.event_id = v.event_id`, [subject, userId]);
|
|
410
|
+
const events = await tx.query(`SELECT DISTINCT name FROM ${this.t.event} WHERE user_id = $1`, [subject]);
|
|
411
|
+
await tx.query(`INSERT INTO ${this.t.suppression} (user_id, channel, topic, ts)
|
|
412
|
+
SELECT $2, channel, topic, ts FROM ${this.t.suppression} WHERE user_id = $1
|
|
413
|
+
ON CONFLICT (user_id, channel, topic) DO NOTHING`, [subject, userId]);
|
|
414
|
+
await tx.query(`DELETE FROM ${this.t.suppression} WHERE user_id = $1`, [subject]);
|
|
415
|
+
const props = await this.mergeProfileInto(tx, subject, userId);
|
|
416
|
+
for (const table of [
|
|
417
|
+
this.t.event,
|
|
418
|
+
this.t.entry,
|
|
419
|
+
this.t.execution,
|
|
420
|
+
this.t.subscription,
|
|
421
|
+
this.t.instance,
|
|
422
|
+
this.t.message
|
|
423
|
+
]) await tx.query(`UPDATE ${table} SET user_id = $2 WHERE user_id = $1`, [subject, userId]);
|
|
424
|
+
await tx.query(`UPDATE ${this.t.device} SET user_id = $2, updated_at = ${TS("$3")}
|
|
425
|
+
WHERE visitor_id = $1 AND user_id IS NULL`, [
|
|
426
|
+
visitorId,
|
|
427
|
+
userId,
|
|
428
|
+
at
|
|
429
|
+
]);
|
|
430
|
+
return {
|
|
431
|
+
dropped: droppedIds,
|
|
432
|
+
events: events.map((row) => row.name),
|
|
433
|
+
props
|
|
434
|
+
};
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* The visitor's properties under the user's, a key the user has a time for going to the later
|
|
439
|
+
* write, one a row older than the times going to the user if they hold it. The visitor's row
|
|
440
|
+
* goes. Answers the keys that moved.
|
|
441
|
+
*/
|
|
442
|
+
async mergeProfileInto(tx, subject, userId) {
|
|
443
|
+
const rows = await tx.query(`SELECT user_id, props, props_at FROM ${this.t.profile} WHERE user_id = ANY($1::text[])`, [[subject, userId]]);
|
|
444
|
+
const from = rows.find((row) => row.user_id === subject);
|
|
445
|
+
if (from === void 0) return [];
|
|
446
|
+
const to = rows.find((row) => row.user_id === userId);
|
|
447
|
+
const fromTimes = asJson(from.props_at ?? null) ?? {};
|
|
448
|
+
const toTimes = asJson(to?.props_at ?? null) ?? {};
|
|
449
|
+
const toProps = to === void 0 ? {} : asJson(to.props);
|
|
450
|
+
const moved = Object.entries(asJson(from.props)).filter(([key]) => {
|
|
451
|
+
const written = toTimes[key];
|
|
452
|
+
return written === void 0 ? !Object.hasOwn(toProps, key) : (fromTimes[key] ?? 0) > written;
|
|
453
|
+
});
|
|
454
|
+
const props = {
|
|
455
|
+
...toProps,
|
|
456
|
+
...Object.fromEntries(moved)
|
|
457
|
+
};
|
|
458
|
+
const times = {
|
|
459
|
+
...toTimes,
|
|
460
|
+
...Object.fromEntries(moved.map(([key]) => [key, fromTimes[key] ?? 0]))
|
|
461
|
+
};
|
|
462
|
+
await tx.query(`INSERT INTO ${this.t.profile} (user_id, props, props_at) VALUES ($1, $2::text::jsonb, $3::text::jsonb)
|
|
463
|
+
ON CONFLICT (user_id) DO UPDATE SET props = EXCLUDED.props, props_at = EXCLUDED.props_at`, [
|
|
464
|
+
userId,
|
|
465
|
+
JSON.stringify(props),
|
|
466
|
+
JSON.stringify(times)
|
|
467
|
+
]);
|
|
468
|
+
await tx.query(`DELETE FROM ${this.t.profile} WHERE user_id = $1`, [subject]);
|
|
469
|
+
return moved.map(([key]) => key);
|
|
470
|
+
}
|
|
471
|
+
async requestErasure(userId, at) {
|
|
472
|
+
await this.sql.transaction(async (tx) => {
|
|
473
|
+
await this.lockUser(tx, userId);
|
|
474
|
+
await tx.query(`INSERT INTO ${this.t.erasure} (user_id, requested_at) VALUES ($1, ${TS("$2")})
|
|
475
|
+
ON CONFLICT DO NOTHING`, [userId, at]);
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
async pendingErasures() {
|
|
479
|
+
return (await this.sql.query(`SELECT user_id FROM ${this.t.erasure} WHERE erased_at IS NULL ORDER BY requested_at`)).map((row) => row.user_id);
|
|
480
|
+
}
|
|
481
|
+
async erasureState(userId) {
|
|
482
|
+
const row = (await this.sql.query(`SELECT erased_at IS NOT NULL AS erased FROM ${this.t.erasure} WHERE user_id = $1`, [userId])).at(0);
|
|
483
|
+
if (row === void 0) return "none";
|
|
484
|
+
return row.erased ? "erased" : "erasing";
|
|
485
|
+
}
|
|
486
|
+
async runningInstances(userId) {
|
|
487
|
+
return (await this.sql.query(`SELECT instance_id, admitted_at IS NOT NULL AS admitted
|
|
488
|
+
FROM ${this.t.entry} WHERE user_id = $1 AND status = 'running'`, [userId])).map((row) => ({
|
|
489
|
+
instanceId: row.instance_id,
|
|
490
|
+
admitted: row.admitted
|
|
491
|
+
}));
|
|
492
|
+
}
|
|
493
|
+
async admitInstance(instanceId, userId) {
|
|
494
|
+
return this.sql.transaction(async (tx) => {
|
|
495
|
+
await this.lockUser(tx, userId);
|
|
496
|
+
return (await tx.query(`UPDATE ${this.t.entry} SET admitted_at = COALESCE(admitted_at, ${TS("$3")})
|
|
497
|
+
WHERE instance_id = $1 AND user_id = $2
|
|
498
|
+
AND (status = 'running' OR admitted_at IS NOT NULL)
|
|
499
|
+
AND NOT EXISTS (SELECT 1 FROM ${this.t.erasure} WHERE user_id = $2)
|
|
500
|
+
RETURNING instance_id`, [
|
|
501
|
+
instanceId,
|
|
502
|
+
userId,
|
|
503
|
+
Date.now()
|
|
504
|
+
])).length > 0;
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
async forgetUser(userId, at) {
|
|
508
|
+
await this.sql.transaction(async (tx) => {
|
|
509
|
+
await this.lockUser(tx, userId);
|
|
510
|
+
await this.deleteSubjectRows(tx, userId);
|
|
511
|
+
await tx.query(`UPDATE ${this.t.erasure} SET erased_at = COALESCE(erased_at, ${TS("$2")}) WHERE user_id = $1`, [userId, at]);
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
/** Everything kept about a user or visitor, on a transaction holding their lock. */
|
|
515
|
+
async deleteSubjectRows(tx, subject) {
|
|
516
|
+
for (const child of [
|
|
517
|
+
this.t.messageOpen,
|
|
518
|
+
this.t.messageClick,
|
|
519
|
+
this.t.messageLink
|
|
520
|
+
]) await tx.query(`DELETE FROM ${child} WHERE message_id IN (SELECT id FROM ${this.t.message} WHERE user_id = $1)`, [subject]);
|
|
521
|
+
for (const table of [this.t.runStep, this.t.run]) await tx.query(`DELETE FROM ${table} WHERE instance_id IN (SELECT instance_id FROM ${this.t.entry} WHERE user_id = $1)`, [subject]);
|
|
522
|
+
for (const table of [
|
|
523
|
+
this.t.message,
|
|
524
|
+
this.t.instance,
|
|
525
|
+
this.t.entry,
|
|
526
|
+
this.t.execution,
|
|
527
|
+
this.t.subscription,
|
|
528
|
+
this.t.segmentStart,
|
|
529
|
+
this.t.segmentMember,
|
|
530
|
+
this.t.suppression,
|
|
531
|
+
this.t.device,
|
|
532
|
+
this.t.event,
|
|
533
|
+
this.t.profile
|
|
534
|
+
]) await tx.query(`DELETE FROM ${table} WHERE user_id = $1`, [subject]);
|
|
535
|
+
const visitorId = require_store_identity.visitorIdOf(subject);
|
|
536
|
+
if (visitorId !== void 0) await tx.query(`DELETE FROM ${this.t.device} WHERE visitor_id = $1 AND user_id IS NULL`, [visitorId]);
|
|
537
|
+
}
|
|
538
|
+
async isSuppressed(userId, channel, topic) {
|
|
539
|
+
return (await this.sql.query(`SELECT 1 AS x FROM ${this.t.suppression}
|
|
540
|
+
WHERE user_id = $1 AND channel = $2 AND (topic IS NULL OR topic = $3::text)`, [
|
|
541
|
+
userId,
|
|
542
|
+
channel,
|
|
543
|
+
topic ?? null
|
|
544
|
+
])).length > 0;
|
|
545
|
+
}
|
|
546
|
+
async listSuppressions(userId, channel) {
|
|
547
|
+
return (await this.sql.query(`SELECT topic FROM ${this.t.suppression} WHERE user_id = $1 AND channel = $2
|
|
548
|
+
ORDER BY topic NULLS FIRST`, [userId, channel])).map((row) => row.topic);
|
|
549
|
+
}
|
|
550
|
+
async updateSuppressions(userId, channel, changes, at) {
|
|
551
|
+
if (changes.length === 0) return;
|
|
552
|
+
await this.sql.transaction(async (tx) => {
|
|
553
|
+
await this.lockUser(tx, userId);
|
|
554
|
+
for (const { topic, suppressed } of changes) if (suppressed) await tx.query(`INSERT INTO ${this.t.suppression} (user_id, channel, topic, ts)
|
|
555
|
+
VALUES ($1, $2, $3::text, ${TS("$4")}) ON CONFLICT DO NOTHING`, [
|
|
556
|
+
userId,
|
|
557
|
+
channel,
|
|
558
|
+
topic,
|
|
559
|
+
at
|
|
560
|
+
]);
|
|
561
|
+
else await tx.query(`DELETE FROM ${this.t.suppression}
|
|
562
|
+
WHERE user_id = $1 AND channel = $2 AND topic IS NOT DISTINCT FROM $3::text`, [
|
|
563
|
+
userId,
|
|
564
|
+
channel,
|
|
565
|
+
topic
|
|
566
|
+
]);
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
async removeDevice(token) {
|
|
570
|
+
const rows = await this.sql.query(`DELETE FROM ${this.t.device} WHERE token = $1 RETURNING user_id, visitor_id`, [token]);
|
|
571
|
+
return rows[0] === void 0 ? void 0 : deviceOwner(rows[0]);
|
|
572
|
+
}
|
|
573
|
+
async unbindDevice(token) {
|
|
574
|
+
const rows = await this.sql.query(`WITH old AS (SELECT token, user_id, visitor_id FROM ${this.t.device} WHERE token = $1 FOR UPDATE)
|
|
575
|
+
UPDATE ${this.t.device} d SET user_id = NULL, visitor_id = NULL, updated_at = ${TS("$2")} FROM old WHERE d.token = old.token
|
|
576
|
+
RETURNING old.user_id, old.visitor_id`, [token, Date.now()]);
|
|
577
|
+
return rows[0] === void 0 ? void 0 : deviceOwner(rows[0]);
|
|
578
|
+
}
|
|
579
|
+
async listSegments() {
|
|
580
|
+
return (await this.sql.query(`SELECT id, condition FROM ${this.t.segment} ORDER BY id`)).map((row) => ({
|
|
581
|
+
id: row.id,
|
|
582
|
+
condition: require_ir.ConditionIR.parse(asJson(row.condition))
|
|
583
|
+
}));
|
|
584
|
+
}
|
|
585
|
+
async seedingSegments() {
|
|
586
|
+
return (await this.sql.query(`SELECT id, ${MS("seeding_since")} AS since FROM ${this.t.segment}
|
|
587
|
+
WHERE seeding_since IS NOT NULL ORDER BY id`)).map((row) => ({
|
|
588
|
+
id: row.id,
|
|
589
|
+
since: Number(row.since)
|
|
590
|
+
}));
|
|
591
|
+
}
|
|
592
|
+
async finishSeeding(segments) {
|
|
593
|
+
for (const { id, since } of segments) await this.sql.query(`UPDATE ${this.t.segment} SET seeding_since = NULL
|
|
594
|
+
WHERE id = $1 AND seeding_since = ${TS("$2")}`, [id, since]);
|
|
595
|
+
}
|
|
596
|
+
async openSweepPass(now) {
|
|
597
|
+
return this.sql.transaction(async (tx) => {
|
|
598
|
+
const seeding = await tx.query(`SELECT id, ${MS("seeding_since")} AS since FROM ${this.t.segment}
|
|
599
|
+
WHERE seeding_since IS NOT NULL ORDER BY id`);
|
|
600
|
+
await tx.query(`INSERT INTO ${this.t.sweepPass} (id, started_at, cursor, seeding, evaluated)
|
|
601
|
+
VALUES ('segments', ${TS("$1")}, NULL, $2::text::jsonb, 0)
|
|
602
|
+
ON CONFLICT (id) DO NOTHING`, [now, JSON.stringify(seeding.map((row) => ({
|
|
603
|
+
id: row.id,
|
|
604
|
+
since: Number(row.since)
|
|
605
|
+
})))]);
|
|
606
|
+
const [row] = await tx.query(`SELECT ${MS("started_at")} AS started_at, cursor, seeding, evaluated
|
|
607
|
+
FROM ${this.t.sweepPass} WHERE id = 'segments'`);
|
|
608
|
+
if (row === void 0) throw new Error("sweep_pass: the pass just opened is gone");
|
|
609
|
+
return {
|
|
610
|
+
startedAt: Number(row.started_at),
|
|
611
|
+
after: row.cursor ?? void 0,
|
|
612
|
+
seeding: asJson(row.seeding),
|
|
613
|
+
evaluated: row.evaluated
|
|
614
|
+
};
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
async advanceSweepPass(pass, progress) {
|
|
618
|
+
return (await this.sql.query(`UPDATE ${this.t.sweepPass} SET cursor = $1, evaluated = evaluated + $2::int
|
|
619
|
+
WHERE id = 'segments' AND started_at = ${TS("$3")} AND cursor IS NOT DISTINCT FROM $4
|
|
620
|
+
RETURNING id`, [
|
|
621
|
+
progress.after,
|
|
622
|
+
progress.evaluated,
|
|
623
|
+
pass.startedAt,
|
|
624
|
+
pass.after ?? null
|
|
625
|
+
])).length > 0;
|
|
626
|
+
}
|
|
627
|
+
async finishSweepPass(pass) {
|
|
628
|
+
return (await this.sql.query(`DELETE FROM ${this.t.sweepPass}
|
|
629
|
+
WHERE id = 'segments' AND started_at = ${TS("$1")} AND cursor IS NOT DISTINCT FROM $2
|
|
630
|
+
RETURNING id`, [pass.startedAt, pass.after ?? null])).length > 0;
|
|
631
|
+
}
|
|
632
|
+
async getSegmentCondition(id) {
|
|
633
|
+
const row = (await this.sql.query(`SELECT condition FROM ${this.t.segment} WHERE id = $1`, [id])).at(0);
|
|
634
|
+
return row === void 0 ? void 0 : require_ir.ConditionIR.parse(asJson(row.condition));
|
|
635
|
+
}
|
|
636
|
+
async findTriggers(event) {
|
|
637
|
+
return (await this.sql.query(`SELECT workflow, hash, "where" AS where_clause, filter, visitors, reentry, version FROM ${this.t.trigger} WHERE event = $1`, [event])).map((row) => ({
|
|
638
|
+
workflow: row.workflow,
|
|
639
|
+
hash: row.hash,
|
|
640
|
+
where: row.where_clause === null ? null : require_ir.ConditionIR.parse(asJson(row.where_clause)),
|
|
641
|
+
filter: row.filter === null ? null : require_ir.ConditionIR.parse(asJson(row.filter)),
|
|
642
|
+
visitors: row.visitors === true,
|
|
643
|
+
reentry: require_ir.ReentryIR.parse(asJson(row.reentry)),
|
|
644
|
+
version: row.version ?? void 0
|
|
645
|
+
}));
|
|
646
|
+
}
|
|
647
|
+
async listSegmentTriggers() {
|
|
648
|
+
return (await this.sql.query(`SELECT r.workflow, r.hash, r.segment, r.reentry, r.version,
|
|
649
|
+
COALESCE(s.seeding_since IS NOT NULL, false) AS seeding
|
|
650
|
+
FROM ${this.t.segmentTrigger} r LEFT JOIN ${this.t.segment} s ON s.id = r.segment`)).map((row) => ({
|
|
651
|
+
workflow: row.workflow,
|
|
652
|
+
hash: row.hash,
|
|
653
|
+
segment: row.segment,
|
|
654
|
+
seeding: row.seeding ? true : void 0,
|
|
655
|
+
reentry: require_ir.ReentryIR.parse(asJson(row.reentry)),
|
|
656
|
+
version: row.version ?? void 0
|
|
657
|
+
}));
|
|
658
|
+
}
|
|
659
|
+
async dueDateTriggers(now) {
|
|
660
|
+
return (await this.sql.transaction((tx) => tx.query(`SELECT r.workflow, r.hash, r.reentry, r.version, ${MS("r.at")} AS at, r.filter,
|
|
661
|
+
d.cursor, d.entered
|
|
662
|
+
FROM ${this.t.dateTrigger} r
|
|
663
|
+
JOIN ${this.t.dateRun} d ON d.workflow = r.workflow AND d.at = r.at
|
|
664
|
+
WHERE d.finished_at IS NULL AND r.at <= ${TS("$1")}
|
|
665
|
+
ORDER BY r.at, r.workflow`, [now]))).map((row) => ({
|
|
666
|
+
workflow: row.workflow,
|
|
667
|
+
hash: row.hash,
|
|
668
|
+
reentry: require_ir.ReentryIR.parse(asJson(row.reentry)),
|
|
669
|
+
version: row.version ?? void 0,
|
|
670
|
+
at: Number(row.at),
|
|
671
|
+
filter: row.filter === null ? void 0 : require_ir.ConditionIR.parse(asJson(row.filter)),
|
|
672
|
+
after: row.cursor ?? void 0,
|
|
673
|
+
entered: row.entered
|
|
674
|
+
}));
|
|
675
|
+
}
|
|
676
|
+
async advanceDateRun(run, progress) {
|
|
677
|
+
const over = progress.finished === true || progress.skipped !== void 0;
|
|
678
|
+
await this.sql.query(`UPDATE ${this.t.dateRun} SET
|
|
679
|
+
cursor = COALESCE($3, cursor),
|
|
680
|
+
entered = entered + $4::int,
|
|
681
|
+
finished_at = CASE WHEN $5 THEN ${TS("$6")} ELSE finished_at END,
|
|
682
|
+
skipped = COALESCE($7, skipped)
|
|
683
|
+
WHERE workflow = $1 AND at = ${TS("$2")} AND finished_at IS NULL
|
|
684
|
+
AND cursor IS NOT DISTINCT FROM $8`, [
|
|
685
|
+
run.workflow,
|
|
686
|
+
run.at,
|
|
687
|
+
progress.after ?? null,
|
|
688
|
+
progress.entered ?? 0,
|
|
689
|
+
over,
|
|
690
|
+
progress.now,
|
|
691
|
+
progress.skipped ?? null,
|
|
692
|
+
run.after ?? null
|
|
693
|
+
]);
|
|
694
|
+
}
|
|
695
|
+
async putWorkflowIR(workflow) {
|
|
696
|
+
await this.sql.query(`INSERT INTO ${this.t.ir} (content_hash, ir) VALUES ($1, $2::text::jsonb)
|
|
697
|
+
ON CONFLICT (content_hash) DO UPDATE SET ir = EXCLUDED.ir`, [workflow.contentHash, JSON.stringify(workflow)]);
|
|
698
|
+
}
|
|
699
|
+
async getWorkflowIR(contentHash) {
|
|
700
|
+
const row = (await this.sql.query(`SELECT ir FROM ${this.t.ir} WHERE content_hash = $1`, [contentHash])).at(0);
|
|
701
|
+
return row === void 0 ? void 0 : require_ir.WorkflowIR.parse(asJson(row.ir));
|
|
702
|
+
}
|
|
703
|
+
async runningPins() {
|
|
704
|
+
return (await this.sql.query(`SELECT workflow, hash, COUNT(*)::int AS running FROM ${this.t.entry}
|
|
705
|
+
WHERE status = 'running' GROUP BY workflow, hash ORDER BY workflow, hash`)).map((row) => ({
|
|
706
|
+
workflow: row.workflow,
|
|
707
|
+
contentHash: row.hash,
|
|
708
|
+
running: row.running
|
|
709
|
+
}));
|
|
710
|
+
}
|
|
711
|
+
replaceRoutes(bundle, version) {
|
|
712
|
+
return this.sql.transaction(async (tx) => {
|
|
713
|
+
if (version !== void 0) await tx.query(`INSERT INTO ${this.t.deployment} (scope, version_id, version_created_at, applied_at)
|
|
714
|
+
VALUES ('routes', $1, ${TS("$2")}, ${TS("$3")})
|
|
715
|
+
ON CONFLICT (scope) DO UPDATE SET version_id = EXCLUDED.version_id,
|
|
716
|
+
version_created_at = EXCLUDED.version_created_at, applied_at = EXCLUDED.applied_at`, [
|
|
717
|
+
version.id,
|
|
718
|
+
version.createdAt,
|
|
719
|
+
Date.now()
|
|
720
|
+
]);
|
|
721
|
+
await this.swapRoutes(tx, bundle, version?.id);
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
async liveBundleVersion() {
|
|
725
|
+
const row = (await this.sql.transaction((tx) => tx.query(`SELECT version_id, (extract(epoch FROM version_created_at) * 1000)::double precision AS created_at
|
|
726
|
+
FROM ${this.t.deployment} WHERE scope = 'routes'`))).at(0);
|
|
727
|
+
return row === void 0 ? void 0 : {
|
|
728
|
+
id: row.version_id,
|
|
729
|
+
createdAt: Number(row.created_at)
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
replaceRoutesIfNewer(bundle, version) {
|
|
733
|
+
return this.sql.transaction(async (tx) => {
|
|
734
|
+
if ((await tx.query(`INSERT INTO ${this.t.deployment} AS live (scope, version_id, version_created_at, applied_at)
|
|
735
|
+
VALUES ('routes', $1, ${TS("$2")}, ${TS("$3")})
|
|
736
|
+
ON CONFLICT (scope) DO UPDATE SET version_id = EXCLUDED.version_id,
|
|
737
|
+
version_created_at = EXCLUDED.version_created_at, applied_at = EXCLUDED.applied_at
|
|
738
|
+
WHERE live.version_created_at < EXCLUDED.version_created_at
|
|
739
|
+
RETURNING scope`, [
|
|
740
|
+
version.id,
|
|
741
|
+
version.createdAt,
|
|
742
|
+
Date.now()
|
|
743
|
+
])).length === 0) return false;
|
|
744
|
+
await this.swapRoutes(tx, bundle, version.id);
|
|
745
|
+
return true;
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
/** `version`: the code version applying the bundle, kept on every route it writes. */
|
|
749
|
+
async swapRoutes(tx, bundle, version) {
|
|
750
|
+
await tx.query(`DELETE FROM ${this.t.trigger}`);
|
|
751
|
+
await tx.query(`DELETE FROM ${this.t.segmentTrigger}`);
|
|
752
|
+
await tx.query(`DELETE FROM ${this.t.dateTrigger}`);
|
|
753
|
+
const before = new Map((await tx.query(`SELECT id, hash, ${MS("seeding_since")} AS since FROM ${this.t.segment}`)).map((row) => [row.id, row]));
|
|
754
|
+
await tx.query(`DELETE FROM ${this.t.segment}`);
|
|
755
|
+
const reseeded = changedSegments(bundle.segments, before);
|
|
756
|
+
const now = Date.now();
|
|
757
|
+
for (const segment of bundle.segments) {
|
|
758
|
+
const kept = before.get(segment.id)?.since ?? null;
|
|
759
|
+
const since = reseeded.has(segment.id) ? now : kept === null ? null : Number(kept);
|
|
760
|
+
await tx.query(`INSERT INTO ${this.t.segment} (id, condition, hash, seeding_since)
|
|
761
|
+
VALUES ($1, $2::text::jsonb, $3, ${TS("$4")})`, [
|
|
762
|
+
segment.id,
|
|
763
|
+
JSON.stringify(segment.condition),
|
|
764
|
+
segment.contentHash,
|
|
765
|
+
since
|
|
766
|
+
]);
|
|
767
|
+
}
|
|
768
|
+
for (const workflow of bundle.workflows) if (workflow.trigger.type === "event") await tx.query(`INSERT INTO ${this.t.trigger} (workflow, hash, event, "where", filter, visitors, reentry, version) VALUES ($1, $2, $3, $4::text::jsonb, $5::text::jsonb, $6, $7::text::jsonb, $8)`, [
|
|
769
|
+
workflow.id,
|
|
770
|
+
workflow.contentHash,
|
|
771
|
+
workflow.trigger.event,
|
|
772
|
+
workflow.trigger.where === void 0 ? null : JSON.stringify(workflow.trigger.where),
|
|
773
|
+
workflow.trigger.filter === void 0 ? null : JSON.stringify(workflow.trigger.filter),
|
|
774
|
+
workflow.trigger.visitors === true,
|
|
775
|
+
JSON.stringify(workflow.reentry),
|
|
776
|
+
version ?? null
|
|
777
|
+
]);
|
|
778
|
+
else if (workflow.trigger.type === "date") {
|
|
779
|
+
const at = require_date_time.dateTimeMs(workflow.trigger.at);
|
|
780
|
+
if (at === void 0) throw new Error(`workflow '${workflow.id}': date trigger '${workflow.trigger.at}' is not 'YYYY-MM-DD HH:mm:ss'`);
|
|
781
|
+
await tx.query(`INSERT INTO ${this.t.dateTrigger} (workflow, hash, reentry, at, filter, version) VALUES ($1, $2, $3::text::jsonb, ${TS("$4")}, $5::text::jsonb, $6)`, [
|
|
782
|
+
workflow.id,
|
|
783
|
+
workflow.contentHash,
|
|
784
|
+
JSON.stringify(workflow.reentry),
|
|
785
|
+
at,
|
|
786
|
+
workflow.trigger.filter === void 0 ? null : JSON.stringify(workflow.trigger.filter),
|
|
787
|
+
version ?? null
|
|
788
|
+
]);
|
|
789
|
+
await tx.query(`INSERT INTO ${this.t.dateRun} (workflow, at, cursor, entered, finished_at, skipped)
|
|
790
|
+
SELECT $1, ${TS("$2")}, NULL, 0,
|
|
791
|
+
CASE WHEN $2::double precision <= $3::double precision THEN ${TS("$3")} END,
|
|
792
|
+
CASE WHEN $2::double precision <= $3::double precision THEN 'past' END
|
|
793
|
+
ON CONFLICT DO NOTHING`, [
|
|
794
|
+
workflow.id,
|
|
795
|
+
at,
|
|
796
|
+
now
|
|
797
|
+
]);
|
|
798
|
+
} else if (workflow.trigger.type === "segment") await tx.query(`INSERT INTO ${this.t.segmentTrigger} (workflow, hash, segment, reentry, version) VALUES ($1, $2, $3, $4::text::jsonb, $5)`, [
|
|
799
|
+
workflow.id,
|
|
800
|
+
workflow.contentHash,
|
|
801
|
+
workflow.trigger.segment,
|
|
802
|
+
JSON.stringify(workflow.reentry),
|
|
803
|
+
version ?? null
|
|
804
|
+
]);
|
|
805
|
+
}
|
|
806
|
+
async isSegmentMember(segment, userId) {
|
|
807
|
+
return (await this.sql.query(`SELECT 1 AS x FROM ${this.t.segmentMember} WHERE segment = $1 AND user_id = $2`, [segment, userId])).length > 0;
|
|
808
|
+
}
|
|
809
|
+
async addSegmentMember(segment, userId, ts, workflows) {
|
|
810
|
+
await this.sql.transaction(async (tx) => {
|
|
811
|
+
await this.lockUser(tx, userId);
|
|
812
|
+
if ((await tx.query(`INSERT INTO ${this.t.segmentMember} (segment, user_id, ts)
|
|
813
|
+
SELECT $1, $2, ${TS("$3")} ${this.notErased("$2")}
|
|
814
|
+
ON CONFLICT DO NOTHING RETURNING segment`, [
|
|
815
|
+
segment,
|
|
816
|
+
userId,
|
|
817
|
+
ts
|
|
818
|
+
])).length === 0) {
|
|
819
|
+
await this.refuseIfErased(tx, userId);
|
|
820
|
+
return;
|
|
821
|
+
}
|
|
822
|
+
for (const workflow of workflows) await tx.query(`INSERT INTO ${this.t.segmentStart} (segment, workflow, user_id, ts)
|
|
823
|
+
SELECT $1, $2, $3, ${TS("$4")} ${this.notErased("$3")}
|
|
824
|
+
ON CONFLICT DO NOTHING`, [
|
|
825
|
+
segment,
|
|
826
|
+
workflow,
|
|
827
|
+
userId,
|
|
828
|
+
ts
|
|
829
|
+
]);
|
|
830
|
+
});
|
|
831
|
+
}
|
|
832
|
+
async pendingSegmentStarts(segment, userId) {
|
|
833
|
+
return (await this.sql.query(`SELECT workflow, (extract(epoch from ts) * 1000)::double precision AS since
|
|
834
|
+
FROM ${this.t.segmentStart} WHERE segment = $1 AND user_id = $2`, [segment, userId])).map((row) => ({
|
|
835
|
+
workflow: row.workflow,
|
|
836
|
+
since: row.since
|
|
837
|
+
}));
|
|
838
|
+
}
|
|
839
|
+
async completeSegmentStart(segment, userId, workflow) {
|
|
840
|
+
await this.sql.query(`DELETE FROM ${this.t.segmentStart} WHERE segment = $1 AND user_id = $2 AND workflow = $3`, [
|
|
841
|
+
segment,
|
|
842
|
+
userId,
|
|
843
|
+
workflow
|
|
844
|
+
]);
|
|
845
|
+
}
|
|
846
|
+
async removeSegmentMember(segment, userId) {
|
|
847
|
+
await this.sql.transaction(async (tx) => {
|
|
848
|
+
await tx.query(`DELETE FROM ${this.t.segmentStart} WHERE segment = $1 AND user_id = $2`, [segment, userId]);
|
|
849
|
+
await tx.query(`DELETE FROM ${this.t.segmentMember} WHERE segment = $1 AND user_id = $2`, [segment, userId]);
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
async enterJourney(entry) {
|
|
853
|
+
const { execution } = entry;
|
|
854
|
+
return this.sql.transaction(async (tx) => {
|
|
855
|
+
await this.lockUser(tx, entry.userId);
|
|
856
|
+
const outcome = await this.claimEntry(tx, entry);
|
|
857
|
+
if ((outcome === "entered" || outcome === "reclaimed") && execution !== void 0) await this.recordStart(tx, entry, execution);
|
|
858
|
+
return outcome;
|
|
859
|
+
});
|
|
860
|
+
}
|
|
861
|
+
async claimEntry(sql, entry) {
|
|
862
|
+
const claimedAt = Date.now();
|
|
863
|
+
const { workflow, userId, occurrence, reentry } = entry;
|
|
864
|
+
const previous = (await sql.query(`SELECT instance_id, status FROM ${this.t.entry}
|
|
865
|
+
WHERE workflow = $1 AND user_id = $2 AND occurrence = $3`, [
|
|
866
|
+
workflow,
|
|
867
|
+
userId,
|
|
868
|
+
occurrence
|
|
869
|
+
])).at(0);
|
|
870
|
+
if (previous !== void 0) {
|
|
871
|
+
if (previous.status !== "failed") return "duplicate";
|
|
872
|
+
return this.reclaimEntry(sql, entry, previous.instance_id, claimedAt);
|
|
873
|
+
}
|
|
874
|
+
const cooldownMs = reentry.mode === "never" ? 0 : reentry.cooldown?.ms ?? 0;
|
|
875
|
+
const [{ live, running, cooling }] = await sql.query(`SELECT count(*) FILTER (WHERE status <> 'failed')::int AS live,
|
|
876
|
+
count(*) FILTER (WHERE status = 'running')::int AS running,
|
|
877
|
+
count(*) FILTER (WHERE status <> 'failed'
|
|
878
|
+
AND ts > ${TS("$3")} - $4::double precision * interval '1 millisecond')::int AS cooling
|
|
879
|
+
FROM ${this.t.entry} WHERE workflow = $1 AND user_id = $2`, [
|
|
880
|
+
workflow,
|
|
881
|
+
userId,
|
|
882
|
+
entry.ts,
|
|
883
|
+
cooldownMs
|
|
884
|
+
]);
|
|
885
|
+
if (reentry.mode === "never" ? live > 0 : reentry.mode === "after_exit" && running > 0 || cooling > 0) {
|
|
886
|
+
await this.refuseIfErased(sql, userId);
|
|
887
|
+
return "refused";
|
|
888
|
+
}
|
|
889
|
+
if ((await sql.query(`INSERT INTO ${this.t.entry} (workflow, user_id, instance_id, occurrence, hash, status, ts, claimed_at)
|
|
890
|
+
SELECT $1, $2, $3, $4, $5, 'running', ${TS("$6")}, ${TS("$7")} ${this.notErased("$2")}
|
|
891
|
+
RETURNING instance_id`, [
|
|
892
|
+
workflow,
|
|
893
|
+
userId,
|
|
894
|
+
entry.instanceId,
|
|
895
|
+
occurrence,
|
|
896
|
+
entry.hash,
|
|
897
|
+
entry.ts,
|
|
898
|
+
claimedAt
|
|
899
|
+
])).length > 0) return "entered";
|
|
900
|
+
throw new require_store_erasure.UserErasedError(userId);
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* Take over the occurrence's row of a dead instance rather than losing the occurrence over one
|
|
904
|
+
* outage (the unique key leaves no room for a second row). The status test is inside the
|
|
905
|
+
* UPDATE, as a second guard beside the lock; the only other way it matches nothing is the
|
|
906
|
+
* tombstone. The reclaim is a new
|
|
907
|
+
* attempt: it starts unadmitted, even under the same instance id, so the previous attempt's
|
|
908
|
+
* admission is not inherited (`runningInstances` would report a runner that has not been
|
|
909
|
+
* created yet).
|
|
910
|
+
*/
|
|
911
|
+
async reclaimEntry(sql, entry, instanceId, claimedAt) {
|
|
912
|
+
if ((await sql.query(`UPDATE ${this.t.entry} SET instance_id = $1, occurrence = $2, hash = $3, status = 'running',
|
|
913
|
+
ts = ${TS("$4")}, admitted_at = NULL, claimed_at = ${TS("$5")}
|
|
914
|
+
WHERE instance_id = $6 AND status = 'failed'
|
|
915
|
+
AND NOT EXISTS (SELECT 1 FROM ${this.t.erasure} WHERE user_id = $7)
|
|
916
|
+
RETURNING instance_id`, [
|
|
917
|
+
entry.instanceId,
|
|
918
|
+
entry.occurrence,
|
|
919
|
+
entry.hash,
|
|
920
|
+
entry.ts,
|
|
921
|
+
claimedAt,
|
|
922
|
+
instanceId,
|
|
923
|
+
entry.userId
|
|
924
|
+
])).length > 0) return "reclaimed";
|
|
925
|
+
throw new require_store_erasure.UserErasedError(entry.userId);
|
|
926
|
+
}
|
|
927
|
+
/**
|
|
928
|
+
* Only an entry this call claimed records a start, so a row already there is a previous
|
|
929
|
+
* attempt's — a failed entry reclaimed under the same instance id (an event replayed with its
|
|
930
|
+
* ts). It is replaced whole: the new attempt's name and payload, nothing submitted yet.
|
|
931
|
+
*/
|
|
932
|
+
async recordStart(tx, entry, execution) {
|
|
933
|
+
if ((await tx.query(`INSERT INTO ${this.t.execution} (instance_id, workflow, user_id, name, target, input, created_at, generation)
|
|
934
|
+
SELECT $1, $2, $3, $4, $5, $6, ${TS("$7")}, 0 ${this.notErased("$3")}
|
|
935
|
+
ON CONFLICT (instance_id) DO UPDATE SET
|
|
936
|
+
name = EXCLUDED.name, target = EXCLUDED.target, input = EXCLUDED.input,
|
|
937
|
+
created_at = EXCLUDED.created_at, generation = 0, submitted_at = NULL, arn = NULL,
|
|
938
|
+
unresolved_at = NULL, callback_id = NULL, deadline_at = NULL, checked_at = NULL,
|
|
939
|
+
stopped_at = NULL
|
|
940
|
+
RETURNING instance_id`, [
|
|
941
|
+
entry.instanceId,
|
|
942
|
+
entry.workflow,
|
|
943
|
+
entry.userId,
|
|
944
|
+
execution.name,
|
|
945
|
+
execution.target,
|
|
946
|
+
execution.input,
|
|
947
|
+
Date.now()
|
|
948
|
+
])).length === 0) throw new require_store_erasure.UserErasedError(entry.userId);
|
|
949
|
+
}
|
|
950
|
+
async removeEntry(instanceId) {
|
|
951
|
+
await this.sql.query(`DELETE FROM ${this.t.entry} WHERE instance_id = $1`, [instanceId]);
|
|
952
|
+
}
|
|
953
|
+
async getEntry(workflow, userId, occurrence) {
|
|
954
|
+
const row = (await this.sql.transaction((tx) => tx.query(`SELECT instance_id, status, admitted_at IS NOT NULL AS admitted
|
|
955
|
+
FROM ${this.t.entry} WHERE workflow = $1 AND user_id = $2 AND occurrence = $3`, [
|
|
956
|
+
workflow,
|
|
957
|
+
userId,
|
|
958
|
+
occurrence
|
|
959
|
+
]))).at(0);
|
|
960
|
+
return row === void 0 ? void 0 : {
|
|
961
|
+
instanceId: row.instance_id,
|
|
962
|
+
status: row.status,
|
|
963
|
+
admitted: row.admitted
|
|
964
|
+
};
|
|
965
|
+
}
|
|
966
|
+
async unadmittedEntries(claimedBefore, limit) {
|
|
967
|
+
return (await this.sql.query(`SELECT instance_id, workflow, user_id FROM ${this.t.entry}
|
|
968
|
+
WHERE admitted_at IS NULL AND status = 'running'
|
|
969
|
+
AND COALESCE(claimed_at, ts) < ${TS("$1")}
|
|
970
|
+
ORDER BY COALESCE(claimed_at, ts) LIMIT $2`, [claimedBefore, limit])).map((row) => ({
|
|
971
|
+
instanceId: row.instance_id,
|
|
972
|
+
workflow: row.workflow,
|
|
973
|
+
userId: row.user_id
|
|
974
|
+
}));
|
|
975
|
+
}
|
|
976
|
+
async failUnadmittedEntry(instanceId) {
|
|
977
|
+
return (await this.sql.query(`UPDATE ${this.t.entry} SET status = 'failed'
|
|
978
|
+
WHERE instance_id = $1 AND status = 'running' AND admitted_at IS NULL
|
|
979
|
+
RETURNING instance_id`, [instanceId])).length > 0;
|
|
980
|
+
}
|
|
981
|
+
async admittedEntries(admittedBefore, limit) {
|
|
982
|
+
return (await this.sql.query(`SELECT instance_id, workflow, user_id FROM ${this.t.entry}
|
|
983
|
+
WHERE status = 'running' AND admitted_at < ${TS("$1")}
|
|
984
|
+
ORDER BY random() LIMIT $2`, [admittedBefore, limit])).map((row) => ({
|
|
985
|
+
instanceId: row.instance_id,
|
|
986
|
+
workflow: row.workflow,
|
|
987
|
+
userId: row.user_id
|
|
988
|
+
}));
|
|
989
|
+
}
|
|
990
|
+
async failAdmittedEntry(instanceId) {
|
|
991
|
+
return (await this.sql.query(`UPDATE ${this.t.entry} SET status = 'failed'
|
|
992
|
+
WHERE instance_id = $1 AND status = 'running' AND admitted_at IS NOT NULL
|
|
993
|
+
RETURNING instance_id`, [instanceId])).length > 0;
|
|
994
|
+
}
|
|
995
|
+
async setEntryStatus(instanceId, status) {
|
|
996
|
+
await this.sql.query(`UPDATE ${this.t.entry} SET status = $1 WHERE instance_id = $2`, [status, instanceId]);
|
|
997
|
+
}
|
|
998
|
+
async findWakeHandles(userId, event) {
|
|
999
|
+
return (await this.sql.query(`SELECT DISTINCT wake_handle AS handle FROM ${this.t.subscription} WHERE user_id = $1 AND event = $2`, [userId, event])).map((row) => row.handle);
|
|
1000
|
+
}
|
|
1001
|
+
async subscribe(handle, userId, events, ts) {
|
|
1002
|
+
await this.sql.transaction(async (tx) => {
|
|
1003
|
+
await this.lockUser(tx, userId);
|
|
1004
|
+
let skipped = false;
|
|
1005
|
+
for (const event of events) if ((await tx.query(`INSERT INTO ${this.t.subscription} (user_id, event, wake_handle, ts)
|
|
1006
|
+
SELECT $1, $2, $3, ${TS("$4")} ${this.notErased("$1")}
|
|
1007
|
+
ON CONFLICT DO NOTHING RETURNING event`, [
|
|
1008
|
+
userId,
|
|
1009
|
+
event,
|
|
1010
|
+
handle,
|
|
1011
|
+
ts
|
|
1012
|
+
])).length === 0) skipped = true;
|
|
1013
|
+
if (skipped) await this.refuseIfErased(tx, userId);
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
1016
|
+
async unsubscribe(handle) {
|
|
1017
|
+
await this.sql.query(`DELETE FROM ${this.t.subscription} WHERE wake_handle = $1`, [handle]);
|
|
1018
|
+
}
|
|
1019
|
+
};
|
|
1020
|
+
const PLAIN_COLUMNS = [
|
|
1021
|
+
"instance_id",
|
|
1022
|
+
"workflow",
|
|
1023
|
+
"user_id",
|
|
1024
|
+
"name",
|
|
1025
|
+
"target",
|
|
1026
|
+
"input",
|
|
1027
|
+
"arn"
|
|
1028
|
+
];
|
|
1029
|
+
const MORE_PLAIN = ["callback_id", "generation"];
|
|
1030
|
+
const TIME_COLUMNS = [
|
|
1031
|
+
"created_at",
|
|
1032
|
+
"submitted_at",
|
|
1033
|
+
"unresolved_at",
|
|
1034
|
+
"deadline_at",
|
|
1035
|
+
"checked_at",
|
|
1036
|
+
"stopped_at"
|
|
1037
|
+
];
|
|
1038
|
+
/** The execution columns, timestamps as epoch ms; `alias` qualifies them in a join. */
|
|
1039
|
+
function executionColumns(alias = "") {
|
|
1040
|
+
const q = alias === "" ? "" : `${alias}.`;
|
|
1041
|
+
return [...[...PLAIN_COLUMNS, ...MORE_PLAIN].map((column) => `${q}${column}`), ...TIME_COLUMNS.map((column) => `(extract(epoch from ${q}${column}) * 1000)::double precision AS ${column}`)].join(", ");
|
|
1042
|
+
}
|
|
1043
|
+
const optional = (value) => value ?? void 0;
|
|
1044
|
+
const ms = (value) => value === null ? void 0 : Number(value);
|
|
1045
|
+
function toRecord(row) {
|
|
1046
|
+
return {
|
|
1047
|
+
instanceId: row.instance_id,
|
|
1048
|
+
workflow: row.workflow,
|
|
1049
|
+
userId: row.user_id,
|
|
1050
|
+
name: row.name,
|
|
1051
|
+
target: row.target,
|
|
1052
|
+
input: row.input,
|
|
1053
|
+
createdAt: Number(row.created_at),
|
|
1054
|
+
submittedAt: ms(row.submitted_at),
|
|
1055
|
+
arn: optional(row.arn),
|
|
1056
|
+
unresolvedAt: ms(row.unresolved_at),
|
|
1057
|
+
callbackId: optional(row.callback_id),
|
|
1058
|
+
generation: Number(row.generation),
|
|
1059
|
+
deadlineAt: ms(row.deadline_at),
|
|
1060
|
+
checkedAt: ms(row.checked_at),
|
|
1061
|
+
stoppedAt: ms(row.stopped_at)
|
|
1062
|
+
};
|
|
1063
|
+
}
|
|
1064
|
+
/** The execution table (tables.ts): starts to confirm, and the callback each wait parks on. */
|
|
1065
|
+
var PostgresExecutions = class {
|
|
1066
|
+
sql;
|
|
1067
|
+
table;
|
|
1068
|
+
entry;
|
|
1069
|
+
constructor(sql, table, entry) {
|
|
1070
|
+
this.sql = sql;
|
|
1071
|
+
this.table = table;
|
|
1072
|
+
this.entry = entry;
|
|
1073
|
+
}
|
|
1074
|
+
async get(instanceId) {
|
|
1075
|
+
const rows = await this.sql.query(`SELECT ${executionColumns()} FROM ${this.table} WHERE instance_id = $1`, [instanceId]);
|
|
1076
|
+
return rows.length === 0 ? void 0 : toRecord(rows[0]);
|
|
1077
|
+
}
|
|
1078
|
+
async pendingStarts(filter) {
|
|
1079
|
+
const where = [
|
|
1080
|
+
"x.submitted_at IS NULL",
|
|
1081
|
+
"x.unresolved_at IS NULL",
|
|
1082
|
+
"e.status = 'running'"
|
|
1083
|
+
];
|
|
1084
|
+
const params = [];
|
|
1085
|
+
if (filter.workflow !== void 0) {
|
|
1086
|
+
params.push(filter.workflow);
|
|
1087
|
+
where.push(`x.workflow = $${params.length}`);
|
|
1088
|
+
}
|
|
1089
|
+
if (filter.userId !== void 0) {
|
|
1090
|
+
params.push(filter.userId);
|
|
1091
|
+
where.push(`x.user_id = $${params.length}`);
|
|
1092
|
+
}
|
|
1093
|
+
if (filter.createdBefore !== void 0) {
|
|
1094
|
+
params.push(filter.createdBefore);
|
|
1095
|
+
where.push(`x.created_at < ${TS(`$${params.length}`)}`);
|
|
1096
|
+
}
|
|
1097
|
+
params.push(filter.limit);
|
|
1098
|
+
return (await this.sql.query(`SELECT ${executionColumns("x")}
|
|
1099
|
+
FROM ${this.table} x JOIN ${this.entry} e ON e.instance_id = x.instance_id
|
|
1100
|
+
WHERE ${where.join(" AND ")} ORDER BY x.created_at LIMIT $${params.length}`, params)).map(toRecord);
|
|
1101
|
+
}
|
|
1102
|
+
async markSubmitted(instanceId, arn, at) {
|
|
1103
|
+
await this.sql.query(`UPDATE ${this.table} SET submitted_at = COALESCE(submitted_at, ${TS("$2")}), arn = COALESCE($3, arn) WHERE instance_id = $1`, [
|
|
1104
|
+
instanceId,
|
|
1105
|
+
at,
|
|
1106
|
+
arn ?? null
|
|
1107
|
+
]);
|
|
1108
|
+
}
|
|
1109
|
+
async markUnresolved(instanceId, at) {
|
|
1110
|
+
await this.sql.query(`UPDATE ${this.table} SET unresolved_at = ${TS("$2")} WHERE instance_id = $1 AND submitted_at IS NULL`, [instanceId, at]);
|
|
1111
|
+
}
|
|
1112
|
+
async setArn(instanceId, arn) {
|
|
1113
|
+
await this.sql.query(`UPDATE ${this.table} SET arn = $2 WHERE instance_id = $1`, [instanceId, arn]);
|
|
1114
|
+
}
|
|
1115
|
+
async armCallback(instanceId, callback) {
|
|
1116
|
+
return (await this.sql.query(`UPDATE ${this.table} SET callback_id = $2, generation = $3,
|
|
1117
|
+
deadline_at = CASE WHEN $4::double precision IS NULL THEN NULL ELSE ${TS("$4")} END
|
|
1118
|
+
WHERE instance_id = $1 AND generation <= $3 RETURNING instance_id`, [
|
|
1119
|
+
instanceId,
|
|
1120
|
+
callback.id,
|
|
1121
|
+
callback.generation,
|
|
1122
|
+
callback.deadlineAt ?? null
|
|
1123
|
+
])).length > 0;
|
|
1124
|
+
}
|
|
1125
|
+
async clearCallback(instanceId, generation) {
|
|
1126
|
+
await this.sql.query(`UPDATE ${this.table} SET callback_id = NULL, deadline_at = NULL WHERE instance_id = $1 AND generation = $2`, [instanceId, generation]);
|
|
1127
|
+
}
|
|
1128
|
+
async toReconcile(limit, checkedBefore) {
|
|
1129
|
+
return (await this.sql.query(`SELECT ${executionColumns("x")}
|
|
1130
|
+
FROM ${this.table} x JOIN ${this.entry} e ON e.instance_id = x.instance_id
|
|
1131
|
+
WHERE e.status = 'running' AND x.submitted_at IS NOT NULL
|
|
1132
|
+
AND (x.checked_at IS NULL OR x.checked_at < ${TS("$1")})
|
|
1133
|
+
ORDER BY x.checked_at NULLS FIRST LIMIT $2`, [checkedBefore, limit])).map(toRecord);
|
|
1134
|
+
}
|
|
1135
|
+
async markStopped(instanceId, at) {
|
|
1136
|
+
await this.sql.query(`UPDATE ${this.table} SET stopped_at = COALESCE(stopped_at, ${TS("$2")}) WHERE instance_id = $1`, [instanceId, at]);
|
|
1137
|
+
}
|
|
1138
|
+
async markChecked(instanceId, at) {
|
|
1139
|
+
await this.sql.query(`UPDATE ${this.table} SET checked_at = ${TS("$2")} WHERE instance_id = $1`, [instanceId, at]);
|
|
1140
|
+
}
|
|
1141
|
+
};
|
|
1142
|
+
/**
|
|
1143
|
+
* The segments of a bundle whose membership has to be established again: those not deployed
|
|
1144
|
+
* before, those whose condition changed, and those built on one of either (a segment names the
|
|
1145
|
+
* segments it uses by id, so its own hash does not move when theirs do).
|
|
1146
|
+
*/
|
|
1147
|
+
function changedSegments(segments, before) {
|
|
1148
|
+
const changed = new Set(segments.filter((segment) => before.get(segment.id)?.hash !== segment.contentHash).map((segment) => segment.id));
|
|
1149
|
+
const uses = new Map(segments.map((segment) => [segment.id, segmentsNamed(segment.condition)]));
|
|
1150
|
+
for (let grew = true; grew;) {
|
|
1151
|
+
grew = false;
|
|
1152
|
+
for (const [id, named] of uses) {
|
|
1153
|
+
if (changed.has(id) || !named.some((name) => changed.has(name))) continue;
|
|
1154
|
+
changed.add(id);
|
|
1155
|
+
grew = true;
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
return changed;
|
|
1159
|
+
}
|
|
1160
|
+
function segmentsNamed(condition) {
|
|
1161
|
+
switch (condition.type) {
|
|
1162
|
+
case "and":
|
|
1163
|
+
case "or": return condition.conditions.flatMap(segmentsNamed);
|
|
1164
|
+
case "not": return segmentsNamed(condition.condition);
|
|
1165
|
+
case "segment": return [condition.segment];
|
|
1166
|
+
default: return [];
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
exports.DEFAULT_SCHEMA = require_store_tables.DEFAULT_SCHEMA;
|
|
1170
|
+
exports.PostgresJourneyStore = PostgresJourneyStore;
|
|
1171
|
+
exports.uuidv7 = require_store_sql.uuidv7;
|
|
1172
|
+
|
|
1173
|
+
//# sourceMappingURL=postgres.cjs.map
|