@openlup/platform 0.16.0 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +84 -0
- package/README.md +285 -5
- package/dist/communications/accountingDocumentDelivery.d.ts +35 -0
- package/dist/communications/accountingDocumentDelivery.js +33 -0
- package/dist/communications/authEmailError.d.ts +5 -0
- package/dist/communications/authEmailError.js +8 -0
- package/dist/communications/authEmailPorts.d.ts +2 -0
- package/dist/communications/authEmailPorts.js +1 -0
- package/dist/communications/authSendEmailHook.d.ts +35 -0
- package/dist/communications/authSendEmailHook.js +250 -0
- package/dist/communications/capturedTransactionalDelivery.d.ts +13 -0
- package/dist/communications/capturedTransactionalDelivery.js +14 -0
- package/dist/communications/communicationSync.d.ts +11 -0
- package/dist/communications/communicationSync.js +79 -0
- package/dist/communications/communicationSyncWorker.d.ts +68 -0
- package/dist/communications/communicationSyncWorker.js +218 -0
- package/dist/communications/consentPolicy.d.ts +19 -0
- package/dist/communications/consentPolicy.js +43 -0
- package/dist/communications/consentPurposeRegistry.d.ts +4 -0
- package/dist/communications/consentPurposeRegistry.js +24 -0
- package/dist/communications/contracts/abandonedCart.d.ts +27 -0
- package/dist/communications/contracts/abandonedCart.js +25 -0
- package/dist/communications/contracts/authEmail.d.ts +62 -0
- package/dist/communications/contracts/authEmail.js +1 -0
- package/dist/communications/contracts/backInStock.d.ts +28 -0
- package/dist/communications/contracts/backInStock.js +28 -0
- package/dist/communications/contracts/blocks.d.ts +24 -0
- package/dist/communications/contracts/blocks.js +23 -0
- package/dist/communications/contracts/checkoutExpired.d.ts +22 -0
- package/dist/communications/contracts/checkoutExpired.js +25 -0
- package/dist/communications/contracts/checkoutRecovery.d.ts +26 -0
- package/dist/communications/contracts/checkoutRecovery.js +37 -0
- package/dist/communications/contracts/commerceEmailContent.d.ts +176 -0
- package/dist/communications/contracts/commerceEmailContent.js +1 -0
- package/dist/communications/contracts/commerceEngagementEmailContent.d.ts +95 -0
- package/dist/communications/contracts/commerceEngagementEmailContent.js +1 -0
- package/dist/communications/contracts/consent.d.ts +12 -0
- package/dist/communications/contracts/consent.js +1 -0
- package/dist/communications/contracts/email.d.ts +182 -0
- package/dist/communications/contracts/email.js +1 -0
- package/dist/communications/contracts/guideCard.d.ts +3 -0
- package/dist/communications/contracts/guideCard.js +26 -0
- package/dist/communications/contracts/index.d.ts +51 -0
- package/dist/communications/contracts/index.js +42 -0
- package/dist/communications/contracts/inquiryContent.d.ts +32 -0
- package/dist/communications/contracts/inquiryContent.js +33 -0
- package/dist/communications/contracts/invoiceDocumentContent.d.ts +26 -0
- package/dist/communications/contracts/invoiceDocumentContent.js +36 -0
- package/dist/communications/contracts/marketingAutomation.d.ts +52 -0
- package/dist/communications/contracts/marketingAutomation.js +1 -0
- package/dist/communications/contracts/newsletter.d.ts +63 -0
- package/dist/communications/contracts/newsletter.js +1 -0
- package/dist/communications/contracts/newsletterWebhook.d.ts +26 -0
- package/dist/communications/contracts/newsletterWebhook.js +1 -0
- package/dist/communications/contracts/optOutContent.d.ts +11 -0
- package/dist/communications/contracts/optOutContent.js +17 -0
- package/dist/communications/contracts/orderCanceled.d.ts +25 -0
- package/dist/communications/contracts/orderCanceled.js +27 -0
- package/dist/communications/contracts/orderDraft.d.ts +46 -0
- package/dist/communications/contracts/orderDraft.js +46 -0
- package/dist/communications/contracts/orderPaid.d.ts +53 -0
- package/dist/communications/contracts/orderPaid.js +71 -0
- package/dist/communications/contracts/orderRefunded.d.ts +24 -0
- package/dist/communications/contracts/orderRefunded.js +27 -0
- package/dist/communications/contracts/paymentFailed.d.ts +24 -0
- package/dist/communications/contracts/paymentFailed.js +24 -0
- package/dist/communications/contracts/publicIntake.d.ts +49 -0
- package/dist/communications/contracts/publicIntake.js +1 -0
- package/dist/communications/contracts/render.d.ts +2 -0
- package/dist/communications/contracts/render.js +229 -0
- package/dist/communications/contracts/reorderReminder.d.ts +24 -0
- package/dist/communications/contracts/reorderReminder.js +24 -0
- package/dist/communications/contracts/returnEmails.d.ts +25 -0
- package/dist/communications/contracts/returnEmails.js +62 -0
- package/dist/communications/contracts/reviewEffects.d.ts +26 -0
- package/dist/communications/contracts/reviewEffects.js +25 -0
- package/dist/communications/contracts/reviewRequest.d.ts +26 -0
- package/dist/communications/contracts/reviewRequest.js +24 -0
- package/dist/communications/contracts/roleNotificationContent.d.ts +25 -0
- package/dist/communications/contracts/roleNotificationContent.js +46 -0
- package/dist/communications/contracts/shipmentDelivered.d.ts +23 -0
- package/dist/communications/contracts/shipmentDelivered.js +24 -0
- package/dist/communications/contracts/shipmentDispatched.d.ts +27 -0
- package/dist/communications/contracts/shipmentDispatched.js +35 -0
- package/dist/communications/contracts/shipmentException.d.ts +21 -0
- package/dist/communications/contracts/shipmentException.js +26 -0
- package/dist/communications/contracts/subscriptionActivationActionRequired.d.ts +17 -0
- package/dist/communications/contracts/subscriptionActivationActionRequired.js +22 -0
- package/dist/communications/contracts/subscriptionAddressChanged.d.ts +19 -0
- package/dist/communications/contracts/subscriptionAddressChanged.js +25 -0
- package/dist/communications/contracts/subscriptionCancelled.d.ts +19 -0
- package/dist/communications/contracts/subscriptionCancelled.js +25 -0
- package/dist/communications/contracts/subscriptionCycleSkipped.d.ts +25 -0
- package/dist/communications/contracts/subscriptionCycleSkipped.js +40 -0
- package/dist/communications/contracts/subscriptionDeliveryRescheduled.d.ts +25 -0
- package/dist/communications/contracts/subscriptionDeliveryRescheduled.js +39 -0
- package/dist/communications/contracts/subscriptionEmailContent.d.ts +145 -0
- package/dist/communications/contracts/subscriptionEmailContent.js +1 -0
- package/dist/communications/contracts/subscriptionPackageChanged.d.ts +20 -0
- package/dist/communications/contracts/subscriptionPackageChanged.js +26 -0
- package/dist/communications/contracts/subscriptionPauseReminder.d.ts +23 -0
- package/dist/communications/contracts/subscriptionPauseReminder.js +29 -0
- package/dist/communications/contracts/subscriptionPaused.d.ts +21 -0
- package/dist/communications/contracts/subscriptionPaused.js +25 -0
- package/dist/communications/contracts/subscriptionPaymentExpired.d.ts +34 -0
- package/dist/communications/contracts/subscriptionPaymentExpired.js +37 -0
- package/dist/communications/contracts/subscriptionPaymentFailed.d.ts +49 -0
- package/dist/communications/contracts/subscriptionPaymentFailed.js +56 -0
- package/dist/communications/contracts/subscriptionPaymentRecovered.d.ts +19 -0
- package/dist/communications/contracts/subscriptionPaymentRecovered.js +22 -0
- package/dist/communications/contracts/subscriptionRenewalAtRisk.d.ts +36 -0
- package/dist/communications/contracts/subscriptionRenewalAtRisk.js +36 -0
- package/dist/communications/contracts/subscriptionRenewalUpcoming.d.ts +49 -0
- package/dist/communications/contracts/subscriptionRenewalUpcoming.js +43 -0
- package/dist/communications/contracts/subscriptionResumed.d.ts +23 -0
- package/dist/communications/contracts/subscriptionResumed.js +25 -0
- package/dist/communications/contracts/subscriptionWelcome.d.ts +44 -0
- package/dist/communications/contracts/subscriptionWelcome.js +45 -0
- package/dist/communications/contracts/subscriptionWinback.d.ts +21 -0
- package/dist/communications/contracts/subscriptionWinback.js +28 -0
- package/dist/communications/contracts/transactionalDelivery.d.ts +42 -0
- package/dist/communications/contracts/transactionalDelivery.js +1 -0
- package/dist/communications/contracts/types.d.ts +46 -0
- package/dist/communications/contracts/types.js +1 -0
- package/dist/communications/contribution.d.ts +10 -0
- package/dist/communications/contribution.js +24 -0
- package/dist/communications/deliveryReconciliation.d.ts +64 -0
- package/dist/communications/deliveryReconciliation.js +60 -0
- package/dist/communications/emailEgress.d.ts +38 -0
- package/dist/communications/emailEgress.js +100 -0
- package/dist/communications/emailHealthAssessment.d.ts +39 -0
- package/dist/communications/emailHealthAssessment.js +59 -0
- package/dist/communications/emailHealthMetricsPort.d.ts +17 -0
- package/dist/communications/emailHealthMetricsPort.js +1 -0
- package/dist/communications/emailNotificationControl.d.ts +27 -0
- package/dist/communications/emailNotificationControl.js +10 -0
- package/dist/communications/emailObservation.d.ts +28 -0
- package/dist/communications/emailObservation.js +23 -0
- package/dist/communications/emailTransport.d.ts +82 -0
- package/dist/communications/emailTransport.js +95 -0
- package/dist/communications/errors.d.ts +15 -0
- package/dist/communications/errors.js +30 -0
- package/dist/communications/index.d.ts +46 -0
- package/dist/communications/index.js +38 -0
- package/dist/communications/integrationEventSignature.d.ts +15 -0
- package/dist/communications/integrationEventSignature.js +37 -0
- package/dist/communications/managedTransactionalDeliveryStore.d.ts +12 -0
- package/dist/communications/managedTransactionalDeliveryStore.js +49 -0
- package/dist/communications/nativeOperations.d.ts +79 -0
- package/dist/communications/nativeOperations.js +3 -0
- package/dist/communications/newsletterIntegrationContracts.d.ts +130 -0
- package/dist/communications/newsletterIntegrationContracts.js +128 -0
- package/dist/communications/newsletterWebhookPort.d.ts +11 -0
- package/dist/communications/newsletterWebhookPort.js +84 -0
- package/dist/communications/newsletterWebhookProcessing.d.ts +17 -0
- package/dist/communications/newsletterWebhookProcessing.js +71 -0
- package/dist/communications/originPolicy.d.ts +40 -0
- package/dist/communications/originPolicy.js +100 -0
- package/dist/communications/postgresCommunicationSync.d.ts +79 -0
- package/dist/communications/postgresCommunicationSync.js +111 -0
- package/dist/communications/postgresConsentEvaluator.d.ts +19 -0
- package/dist/communications/postgresConsentEvaluator.js +43 -0
- package/dist/communications/postgresDeliveryReconciliation.d.ts +3 -0
- package/dist/communications/postgresDeliveryReconciliation.js +113 -0
- package/dist/communications/postgresNativeOperations.d.ts +13 -0
- package/dist/communications/postgresNativeOperations.js +63 -0
- package/dist/communications/postgresNotificationRecipients.d.ts +37 -0
- package/dist/communications/postgresNotificationRecipients.js +60 -0
- package/dist/communications/postgresTransactionalDeliveryStore.d.ts +7 -0
- package/dist/communications/postgresTransactionalDeliveryStore.js +55 -0
- package/dist/communications/preferencesToken.d.ts +10 -0
- package/dist/communications/preferencesToken.js +40 -0
- package/dist/communications/publicPreferenceToken.d.ts +24 -0
- package/dist/communications/publicPreferenceToken.js +53 -0
- package/dist/communications/shipmentNotifications.d.ts +34 -0
- package/dist/communications/shipmentNotifications.js +61 -0
- package/dist/communications/sql.d.ts +26 -0
- package/dist/communications/sql.js +7 -0
- package/dist/communications/standardWebhooks.d.ts +14 -0
- package/dist/communications/standardWebhooks.js +73 -0
- package/dist/communications/status.d.ts +11 -0
- package/dist/communications/status.js +36 -0
- package/dist/communications/transactionalDelivery.d.ts +19 -0
- package/dist/communications/transactionalDelivery.js +96 -0
- package/dist/communications/transactionalDeliveryIdentifier.d.ts +3 -0
- package/dist/communications/transactionalDeliveryIdentifier.js +6 -0
- package/dist/communications/unsubscribeToken.d.ts +77 -0
- package/dist/communications/unsubscribeToken.js +150 -0
- package/dist/communications/validation.d.ts +2 -0
- package/dist/communications/validation.js +3 -0
- package/dist/http/index.d.ts +1 -0
- package/dist/http/index.js +1 -0
- package/dist/http/webhookSignature.d.ts +42 -0
- package/dist/http/webhookSignature.js +86 -0
- package/package.json +12 -2
- package/release-gates.json +41 -8
- package/sql/20260807190000_comms_outbox_rail.sql +329 -0
- package/sql/20260810120001_transactional_delivery_receipts.sql +209 -0
- package/sql/20260813120000_communications_control_plane.sql +568 -0
- package/sql/20261019200000_communications_native_operations.sql +68 -0
- package/src/communications/accountingDocumentDelivery.ts +63 -0
- package/src/communications/authEmailError.ts +10 -0
- package/src/communications/authEmailPorts.ts +2 -0
- package/src/communications/authSendEmailHook.ts +310 -0
- package/src/communications/capturedTransactionalDelivery.ts +29 -0
- package/src/communications/communicationSync.ts +96 -0
- package/src/communications/communicationSyncWorker.ts +302 -0
- package/src/communications/consentPolicy.ts +82 -0
- package/src/communications/consentPurposeRegistry.ts +31 -0
- package/src/communications/contracts/abandonedCart.ts +64 -0
- package/src/communications/contracts/authEmail.ts +73 -0
- package/src/communications/contracts/backInStock.ts +69 -0
- package/src/communications/contracts/blocks.ts +47 -0
- package/src/communications/contracts/checkoutExpired.ts +62 -0
- package/src/communications/contracts/checkoutRecovery.ts +68 -0
- package/src/communications/contracts/commerceEmailContent.ts +189 -0
- package/src/communications/contracts/commerceEngagementEmailContent.ts +106 -0
- package/src/communications/contracts/consent.ts +13 -0
- package/src/communications/contracts/email.ts +210 -0
- package/src/communications/contracts/guideCard.ts +34 -0
- package/src/communications/contracts/index.ts +51 -0
- package/src/communications/contracts/inquiryContent.ts +53 -0
- package/src/communications/contracts/invoiceDocumentContent.ts +57 -0
- package/src/communications/contracts/marketingAutomation.ts +45 -0
- package/src/communications/contracts/newsletter.ts +78 -0
- package/src/communications/contracts/newsletterWebhook.ts +23 -0
- package/src/communications/contracts/optOutContent.ts +23 -0
- package/src/communications/contracts/orderCanceled.ts +64 -0
- package/src/communications/contracts/orderDraft.ts +112 -0
- package/src/communications/contracts/orderPaid.ts +147 -0
- package/src/communications/contracts/orderRefunded.ts +63 -0
- package/src/communications/contracts/paymentFailed.ts +59 -0
- package/src/communications/contracts/publicIntake.ts +53 -0
- package/src/communications/contracts/render.ts +271 -0
- package/src/communications/contracts/reorderReminder.ts +59 -0
- package/src/communications/contracts/returnEmails.ts +113 -0
- package/src/communications/contracts/reviewEffects.ts +60 -0
- package/src/communications/contracts/reviewRequest.ts +59 -0
- package/src/communications/contracts/roleNotificationContent.ts +65 -0
- package/src/communications/contracts/shipmentDelivered.ts +59 -0
- package/src/communications/contracts/shipmentDispatched.ts +77 -0
- package/src/communications/contracts/shipmentException.ts +62 -0
- package/src/communications/contracts/subscriptionActivationActionRequired.ts +37 -0
- package/src/communications/contracts/subscriptionAddressChanged.ts +56 -0
- package/src/communications/contracts/subscriptionCancelled.ts +57 -0
- package/src/communications/contracts/subscriptionCycleSkipped.ts +80 -0
- package/src/communications/contracts/subscriptionDeliveryRescheduled.ts +79 -0
- package/src/communications/contracts/subscriptionEmailContent.ts +163 -0
- package/src/communications/contracts/subscriptionPackageChanged.ts +59 -0
- package/src/communications/contracts/subscriptionPauseReminder.ts +63 -0
- package/src/communications/contracts/subscriptionPaused.ts +58 -0
- package/src/communications/contracts/subscriptionPaymentExpired.ts +83 -0
- package/src/communications/contracts/subscriptionPaymentFailed.ts +117 -0
- package/src/communications/contracts/subscriptionPaymentRecovered.ts +50 -0
- package/src/communications/contracts/subscriptionRenewalAtRisk.ts +85 -0
- package/src/communications/contracts/subscriptionRenewalUpcoming.ts +103 -0
- package/src/communications/contracts/subscriptionResumed.ts +60 -0
- package/src/communications/contracts/subscriptionWelcome.ts +102 -0
- package/src/communications/contracts/subscriptionWinback.ts +62 -0
- package/src/communications/contracts/transactionalDelivery.ts +43 -0
- package/src/communications/contracts/types.ts +58 -0
- package/src/communications/contribution.ts +33 -0
- package/src/communications/deliveryReconciliation.ts +108 -0
- package/src/communications/emailEgress.ts +111 -0
- package/src/communications/emailHealthAssessment.ts +106 -0
- package/src/communications/emailHealthMetricsPort.ts +17 -0
- package/src/communications/emailNotificationControl.ts +28 -0
- package/src/communications/emailObservation.ts +43 -0
- package/src/communications/emailTransport.ts +167 -0
- package/src/communications/errors.ts +34 -0
- package/src/communications/index.ts +46 -0
- package/src/communications/integrationEventSignature.ts +49 -0
- package/src/communications/managedTransactionalDeliveryStore.ts +59 -0
- package/src/communications/nativeOperations.ts +40 -0
- package/src/communications/newsletterIntegrationContracts.ts +154 -0
- package/src/communications/newsletterWebhookPort.ts +104 -0
- package/src/communications/newsletterWebhookProcessing.ts +102 -0
- package/src/communications/originPolicy.ts +151 -0
- package/src/communications/postgresCommunicationSync.ts +127 -0
- package/src/communications/postgresConsentEvaluator.ts +49 -0
- package/src/communications/postgresDeliveryReconciliation.ts +101 -0
- package/src/communications/postgresNativeOperations.ts +55 -0
- package/src/communications/postgresNotificationRecipients.ts +57 -0
- package/src/communications/postgresTransactionalDeliveryStore.ts +79 -0
- package/src/communications/preferencesToken.ts +62 -0
- package/src/communications/publicPreferenceToken.ts +68 -0
- package/src/communications/shipmentNotifications.ts +78 -0
- package/src/communications/sql.ts +7 -0
- package/src/communications/standardWebhooks.ts +84 -0
- package/src/communications/status.ts +51 -0
- package/src/communications/transactionalDelivery.ts +114 -0
- package/src/communications/transactionalDeliveryIdentifier.ts +9 -0
- package/src/communications/unsubscribeToken.ts +186 -0
- package/src/communications/validation.ts +3 -0
- package/src/http/index.ts +1 -0
- package/src/http/webhookSignature.ts +96 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
2
|
+
/** Hard cap on raw webhook body bytes — the payment provider events are well under 256 KiB. */
|
|
3
|
+
export const MAX_WEBHOOK_BODY_BYTES = 1_048_576;
|
|
4
|
+
/** Thrown by readRawBody when the streamed body exceeds MAX_WEBHOOK_BODY_BYTES. */
|
|
5
|
+
export class WebhookBodyTooLargeError extends Error {
|
|
6
|
+
constructor() {
|
|
7
|
+
super("Webhook body exceeds maximum size");
|
|
8
|
+
this.name = "WebhookBodyTooLargeError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Reads the exact raw request body bytes for provider webhook signature
|
|
13
|
+
* verification.
|
|
14
|
+
*
|
|
15
|
+
* `@vercel/node` (v3, the builder for `/api` functions in this Vite project)
|
|
16
|
+
* auto-parses `application/json` request bodies into `req.body` and IGNORES the
|
|
17
|
+
* `export const config = { api: { bodyParser: false } }` opt-out — that opt-out
|
|
18
|
+
* is a Next.js Pages Router convention, not a vanilla `@vercel/node` one
|
|
19
|
+
* (verified empirically against the hidden-preview deploy: a JSON payload posted
|
|
20
|
+
* with `Content-Type: application/json` arrives parsed, the same bytes posted as
|
|
21
|
+
* `text/plain` arrive raw). Re-serializing the parsed object with
|
|
22
|
+
* `JSON.stringify` does NOT reproduce the provider's exact bytes — the payment provider signs
|
|
23
|
+
* pretty-printed JSON, so a compacted re-serialization breaks HMAC verification.
|
|
24
|
+
*
|
|
25
|
+
* Reading the underlying request stream FIRST — before touching `req.body`,
|
|
26
|
+
* whose getter triggers the parse that consumes the stream — preserves the exact
|
|
27
|
+
* bytes when the platform parses lazily. When `req` is not a stream (unit tests
|
|
28
|
+
* pass a plain `{ body }` object) or the stream yields nothing (already
|
|
29
|
+
* consumed), we fall back to `req.body`.
|
|
30
|
+
*/
|
|
31
|
+
export async function readRawBody(req) {
|
|
32
|
+
if (isAsyncIterable(req)) {
|
|
33
|
+
const chunks = [];
|
|
34
|
+
let total = 0;
|
|
35
|
+
for await (const chunk of req) {
|
|
36
|
+
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
37
|
+
total += buf.byteLength;
|
|
38
|
+
// Enforce the cap mid-stream so an oversized body is rejected before it is
|
|
39
|
+
// fully buffered into memory, not after.
|
|
40
|
+
if (total > MAX_WEBHOOK_BODY_BYTES)
|
|
41
|
+
throw new WebhookBodyTooLargeError();
|
|
42
|
+
chunks.push(buf);
|
|
43
|
+
}
|
|
44
|
+
if (chunks.length > 0)
|
|
45
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
46
|
+
}
|
|
47
|
+
if (typeof req.body === "string")
|
|
48
|
+
return capBody(req.body);
|
|
49
|
+
if (Buffer.isBuffer(req.body))
|
|
50
|
+
return capBody(req.body.toString("utf8"));
|
|
51
|
+
if (req.body && typeof req.body === "object")
|
|
52
|
+
return capBody(JSON.stringify(req.body));
|
|
53
|
+
return "";
|
|
54
|
+
}
|
|
55
|
+
function capBody(body) {
|
|
56
|
+
if (Buffer.byteLength(body, "utf8") > MAX_WEBHOOK_BODY_BYTES) {
|
|
57
|
+
throw new WebhookBodyTooLargeError();
|
|
58
|
+
}
|
|
59
|
+
return body;
|
|
60
|
+
}
|
|
61
|
+
function isAsyncIterable(value) {
|
|
62
|
+
return (value != null &&
|
|
63
|
+
typeof value[Symbol.asyncIterator] ===
|
|
64
|
+
"function");
|
|
65
|
+
}
|
|
66
|
+
export function verifyHmacSignature(input) {
|
|
67
|
+
if (!input.signatureHeader || !input.secret)
|
|
68
|
+
return false;
|
|
69
|
+
return timingSafeStringEqual(input.signatureHeader, hmac(input.rawBody, input.secret));
|
|
70
|
+
}
|
|
71
|
+
function hmac(value, secret) {
|
|
72
|
+
return createHmac("sha256", secret).update(value).digest("hex");
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Constant-time comparison of two shared-secret strings.
|
|
76
|
+
*
|
|
77
|
+
* Exported because it is not payment-specific: any route holding a shared token has to compare it
|
|
78
|
+
* the same way, and a second hand-rolled copy of a security primitive is how one of them quietly
|
|
79
|
+
* stops being constant-time. The length check comes first because `timingSafeEqual` THROWS on a
|
|
80
|
+
* length mismatch rather than returning false, and the length of a shared token is not the secret.
|
|
81
|
+
*/
|
|
82
|
+
export function timingSafeStringEqual(left, right) {
|
|
83
|
+
const leftBuffer = Buffer.from(left);
|
|
84
|
+
const rightBuffer = Buffer.from(right);
|
|
85
|
+
return leftBuffer.length === rightBuffer.length && timingSafeEqual(leftBuffer, rightBuffer);
|
|
86
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openlup/platform",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Runnable server substrate: Node HTTP runtime, BFF dispatch, scheduler, storage, data gateways and bundle composition; development preview.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"api:update": "npm run build && node --experimental-strip-types ./scripts/api-contract.ts --update"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@openlup/core": "0.
|
|
34
|
+
"@openlup/core": "0.17.0",
|
|
35
35
|
"pg": "^8.22.0",
|
|
36
36
|
"@aws-sdk/client-s3": "^3.1144.0",
|
|
37
37
|
"@types/pg": "^8.20.0"
|
|
@@ -105,6 +105,16 @@
|
|
|
105
105
|
"types": "./dist/identity/contracts/index.d.ts",
|
|
106
106
|
"import": "./dist/identity/contracts/index.js",
|
|
107
107
|
"default": "./dist/identity/contracts/index.js"
|
|
108
|
+
},
|
|
109
|
+
"./communications": {
|
|
110
|
+
"types": "./dist/communications/index.d.ts",
|
|
111
|
+
"import": "./dist/communications/index.js",
|
|
112
|
+
"default": "./dist/communications/index.js"
|
|
113
|
+
},
|
|
114
|
+
"./communications/contracts": {
|
|
115
|
+
"types": "./dist/communications/contracts/index.d.ts",
|
|
116
|
+
"import": "./dist/communications/contracts/index.js",
|
|
117
|
+
"default": "./dist/communications/contracts/index.js"
|
|
108
118
|
}
|
|
109
119
|
},
|
|
110
120
|
"repository": {
|
package/release-gates.json
CHANGED
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"compatibilityPolicy": "internal-candidate",
|
|
12
12
|
"packageSmokeEvidence": [
|
|
13
13
|
"test/packageBoundary.test.ts",
|
|
14
|
-
"test/http/runtime.test.ts"
|
|
14
|
+
"test/http/runtime.test.ts",
|
|
15
|
+
"test/http/webhookSignature.test.ts"
|
|
15
16
|
],
|
|
16
17
|
"entrypoint": "dist/http/index.d.ts",
|
|
17
18
|
"snapshot": "api/http.api.md"
|
|
@@ -125,19 +126,51 @@
|
|
|
125
126
|
],
|
|
126
127
|
"entrypoint": "dist/identity/contracts/index.d.ts",
|
|
127
128
|
"snapshot": "api/identity-contracts.api.md"
|
|
129
|
+
},
|
|
130
|
+
"./communications": {
|
|
131
|
+
"role": "capability",
|
|
132
|
+
"maturity": "experimental",
|
|
133
|
+
"compatibilityPolicy": "internal-candidate",
|
|
134
|
+
"packageSmokeEvidence": [
|
|
135
|
+
"test/packageBoundary.test.ts",
|
|
136
|
+
"test/communications/sql.test.ts",
|
|
137
|
+
"test/communications/contribution.test.ts",
|
|
138
|
+
"test/communications/transportConformance.test.ts"
|
|
139
|
+
],
|
|
140
|
+
"entrypoint": "dist/communications/index.d.ts",
|
|
141
|
+
"snapshot": "api/communications.api.md"
|
|
142
|
+
},
|
|
143
|
+
"./communications/contracts": {
|
|
144
|
+
"role": "capability",
|
|
145
|
+
"maturity": "experimental",
|
|
146
|
+
"compatibilityPolicy": "internal-candidate",
|
|
147
|
+
"packageSmokeEvidence": [
|
|
148
|
+
"test/packageBoundary.test.ts",
|
|
149
|
+
"test/communications/contracts.test.ts"
|
|
150
|
+
],
|
|
151
|
+
"entrypoint": "dist/communications/contracts/index.d.ts",
|
|
152
|
+
"snapshot": "api/communications-contracts.api.md"
|
|
128
153
|
}
|
|
129
154
|
},
|
|
130
155
|
"pack": {
|
|
131
156
|
"code": {
|
|
132
|
-
"maxPackedBytes":
|
|
133
|
-
"maxFiles":
|
|
157
|
+
"maxPackedBytes": 440320,
|
|
158
|
+
"maxFiles": 800
|
|
134
159
|
},
|
|
135
160
|
"docs": {
|
|
136
|
-
"maxPackedBytes":
|
|
137
|
-
"maxFiles":
|
|
161
|
+
"maxPackedBytes": 40960,
|
|
162
|
+
"maxFiles": 10
|
|
138
163
|
},
|
|
139
|
-
"maxPackedBytes":
|
|
140
|
-
"maxUnpackedBytes":
|
|
141
|
-
"maxFiles":
|
|
164
|
+
"maxPackedBytes": 522240,
|
|
165
|
+
"maxUnpackedBytes": 2088960,
|
|
166
|
+
"maxFiles": 800,
|
|
167
|
+
"baseline": {
|
|
168
|
+
"version": "0.16.0",
|
|
169
|
+
"packedBytes": 256973
|
|
170
|
+
},
|
|
171
|
+
"browserEntries": {
|
|
172
|
+
"./communications/contracts": 11776,
|
|
173
|
+
"./identity/contracts": 4800
|
|
174
|
+
}
|
|
142
175
|
}
|
|
143
176
|
}
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
-- Public platform communications outbox rail: the queue table, the dormant-event-type
|
|
2
|
+
-- allowlist, and the claim/ack protocol that drains them.
|
|
3
|
+
--
|
|
4
|
+
-- WHAT THIS FORWARD SHIPS, AND WHY IT IS THE FIRST ONE CARRYING FUNCTIONS.
|
|
5
|
+
-- The baseline and the read-parity forward both hold `grep -c 'CREATE FUNCTION'` = 0 on
|
|
6
|
+
-- purpose. This migration breaks that property deliberately, because the dispatch rail
|
|
7
|
+
-- has no non-RPC surface: claim is an atomic `UPDATE ... FOR UPDATE SKIP LOCKED` that
|
|
8
|
+
-- hands back a claim token and evaluates a per-aggregate ordering barrier inside the same
|
|
9
|
+
-- statement. Expressing it through the gateway's query builder would mean reimplementing
|
|
10
|
+
-- pessimistic locking in the application, i.e. a second and divergent rail. Four functions
|
|
11
|
+
-- is the whole crossing: no privilege model, no `private` schema, no queue statistics, no
|
|
12
|
+
-- pruning, no preview-matrix claim scope, and above all no enqueue -- rows are written by
|
|
13
|
+
-- capabilities that own their own business logic, and none of them is authored here.
|
|
14
|
+
--
|
|
15
|
+
-- AUTHORED, NOT COPIED. The protocol below was written for this migration against the
|
|
16
|
+
-- observable contract the platform outbox store port issues (five named RPC arguments sets,
|
|
17
|
+
-- a boolean/text/integer/SETOF return shape). It is not a cut of any historical migration:
|
|
18
|
+
-- the provider-specific idempotency window and the hardcoded product event-type vocabulary
|
|
19
|
+
-- that the managed implementation calls into are absent, the preview-matrix claim scope is
|
|
20
|
+
-- absent, row-level security and role grants are absent because this kernel creates no
|
|
21
|
+
-- roles to grant to, `SECURITY DEFINER` is absent for the same reason, every constraint is
|
|
22
|
+
-- named rather than left to a generated identifier, and `CREATE` is unconditional because a
|
|
23
|
+
-- manifest-ordered forward runs exactly once against a known prefix -- `IF NOT EXISTS` there
|
|
24
|
+
-- would hide the drift the migration ledger exists to catch. `SET search_path` is kept: it
|
|
25
|
+
-- is a real injection-hardening property that does not depend on a role model.
|
|
26
|
+
|
|
27
|
+
CREATE TABLE public.outbox_events (
|
|
28
|
+
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
29
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
30
|
+
available_at timestamptz NOT NULL DEFAULT now(),
|
|
31
|
+
processed_at timestamptz,
|
|
32
|
+
aggregate_type text NOT NULL,
|
|
33
|
+
aggregate_id uuid NOT NULL,
|
|
34
|
+
event_type text NOT NULL,
|
|
35
|
+
idempotency_key text NOT NULL,
|
|
36
|
+
status text NOT NULL DEFAULT 'pending',
|
|
37
|
+
attempts integer NOT NULL DEFAULT 0,
|
|
38
|
+
payload jsonb NOT NULL,
|
|
39
|
+
error text,
|
|
40
|
+
metadata jsonb NOT NULL DEFAULT '{}'::jsonb,
|
|
41
|
+
CONSTRAINT outbox_events_aggregate_type_nonempty_check CHECK (btrim(aggregate_type) <> ''),
|
|
42
|
+
CONSTRAINT outbox_events_event_type_nonempty_check CHECK (btrim(event_type) <> ''),
|
|
43
|
+
CONSTRAINT outbox_events_idempotency_key_nonempty_check CHECK (btrim(idempotency_key) <> ''),
|
|
44
|
+
CONSTRAINT outbox_events_attempts_check CHECK (attempts >= 0),
|
|
45
|
+
CONSTRAINT outbox_events_status_check
|
|
46
|
+
CHECK (status IN ('pending', 'processing', 'processed', 'failed', 'discarded')),
|
|
47
|
+
CONSTRAINT outbox_events_event_type_idempotency_key_key UNIQUE (event_type, idempotency_key)
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
CREATE INDEX idx_outbox_events_status_available
|
|
51
|
+
ON public.outbox_events (status, available_at);
|
|
52
|
+
|
|
53
|
+
CREATE INDEX idx_outbox_events_aggregate
|
|
54
|
+
ON public.outbox_events (aggregate_type, aggregate_id);
|
|
55
|
+
|
|
56
|
+
-- The claim barrier below refuses to skip an older unprocessed row for the same aggregate,
|
|
57
|
+
-- INCLUDING one whose event type the caller does not know about. Without a way to declare a
|
|
58
|
+
-- type intentionally unconsumed, a single retired producer would stall its aggregate
|
|
59
|
+
-- forever. This table is that declaration, and it ships empty: which types are dormant is
|
|
60
|
+
-- operator data, not platform content.
|
|
61
|
+
CREATE TABLE public.outbox_dormant_event_types (
|
|
62
|
+
event_type text PRIMARY KEY,
|
|
63
|
+
owner text NOT NULL,
|
|
64
|
+
reason text NOT NULL,
|
|
65
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
66
|
+
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
67
|
+
CONSTRAINT outbox_dormant_event_types_event_type_nonempty_check CHECK (btrim(event_type) <> ''),
|
|
68
|
+
CONSTRAINT outbox_dormant_event_types_owner_nonempty_check CHECK (btrim(owner) <> ''),
|
|
69
|
+
CONSTRAINT outbox_dormant_event_types_reason_nonempty_check CHECK (btrim(reason) <> '')
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
-- Claim a batch for one allowlist of event types.
|
|
73
|
+
--
|
|
74
|
+
-- Three properties make this more than a SELECT, and each is load-bearing:
|
|
75
|
+
-- 1. an empty allowlist is refused, never widened -- a caller that forgot its filter must
|
|
76
|
+
-- not drain every event type on the instance;
|
|
77
|
+
-- 2. rows whose attempts already reached the ceiling are discarded before the scan, so an
|
|
78
|
+
-- exhausted row cannot be re-selected on every tick;
|
|
79
|
+
-- 3. per aggregate, an older unprocessed row blocks the newer one. A prior of an event
|
|
80
|
+
-- type the caller neither claims nor knows also blocks, unless it has been declared
|
|
81
|
+
-- dormant -- ordering within an aggregate is the guarantee producers rely on.
|
|
82
|
+
-- The claim token written into metadata is the authorization for every later acknowledgement:
|
|
83
|
+
-- knowing the row id is not enough, which is what makes a stale ack detectable rather than
|
|
84
|
+
-- destructive.
|
|
85
|
+
CREATE FUNCTION public.outbox_claim_batch(
|
|
86
|
+
p_event_types text[],
|
|
87
|
+
p_known_event_types text[] DEFAULT ARRAY[]::text[],
|
|
88
|
+
p_batch_size integer DEFAULT 25,
|
|
89
|
+
p_visibility_seconds integer DEFAULT 300,
|
|
90
|
+
p_max_attempts integer DEFAULT 8
|
|
91
|
+
)
|
|
92
|
+
RETURNS SETOF public.outbox_events
|
|
93
|
+
LANGUAGE plpgsql
|
|
94
|
+
SET search_path = public, pg_catalog
|
|
95
|
+
AS $$
|
|
96
|
+
DECLARE
|
|
97
|
+
v_batch integer := LEAST(GREATEST(COALESCE(p_batch_size, 25), 1), 100);
|
|
98
|
+
v_visibility integer := LEAST(GREATEST(COALESCE(p_visibility_seconds, 300), 300), 3600);
|
|
99
|
+
v_max integer := LEAST(GREATEST(COALESCE(p_max_attempts, 8), 1), 20);
|
|
100
|
+
v_known text[];
|
|
101
|
+
BEGIN
|
|
102
|
+
IF p_event_types IS NULL OR cardinality(p_event_types) = 0 THEN
|
|
103
|
+
RAISE EXCEPTION 'outbox_claim_allowlist_required' USING ERRCODE = '22023';
|
|
104
|
+
END IF;
|
|
105
|
+
|
|
106
|
+
-- Everything the caller can recognise: what it claims now, plus what it declares it knows
|
|
107
|
+
-- about. A prior outside this set is treated as foreign, and only the dormant table can
|
|
108
|
+
-- release it.
|
|
109
|
+
SELECT array_agg(DISTINCT event_type)
|
|
110
|
+
INTO v_known
|
|
111
|
+
FROM unnest(COALESCE(p_known_event_types, ARRAY[]::text[]) || p_event_types) AS known(event_type)
|
|
112
|
+
WHERE event_type IS NOT NULL AND btrim(event_type) <> '';
|
|
113
|
+
|
|
114
|
+
WITH exhausted AS (
|
|
115
|
+
SELECT event.id
|
|
116
|
+
FROM public.outbox_events event
|
|
117
|
+
WHERE event.event_type = ANY (p_event_types)
|
|
118
|
+
AND event.status IN ('pending', 'failed', 'processing')
|
|
119
|
+
AND event.available_at <= now()
|
|
120
|
+
AND event.attempts >= v_max
|
|
121
|
+
ORDER BY event.created_at, event.id
|
|
122
|
+
LIMIT 100
|
|
123
|
+
FOR UPDATE SKIP LOCKED
|
|
124
|
+
)
|
|
125
|
+
UPDATE public.outbox_events event
|
|
126
|
+
SET status = 'discarded',
|
|
127
|
+
error = left(COALESCE(event.error, '') || ' | max_attempts_exhausted_at_claim', 2000),
|
|
128
|
+
metadata = event.metadata || jsonb_build_object(
|
|
129
|
+
'discardReason', 'max_attempts',
|
|
130
|
+
'discardedAt', now(),
|
|
131
|
+
'finalAttempts', event.attempts)
|
|
132
|
+
FROM exhausted
|
|
133
|
+
WHERE event.id = exhausted.id;
|
|
134
|
+
|
|
135
|
+
RETURN QUERY
|
|
136
|
+
WITH candidates AS (
|
|
137
|
+
SELECT event.id
|
|
138
|
+
FROM public.outbox_events event
|
|
139
|
+
WHERE event.event_type = ANY (p_event_types)
|
|
140
|
+
AND event.available_at <= now()
|
|
141
|
+
AND event.status IN ('pending', 'failed', 'processing')
|
|
142
|
+
AND event.attempts < v_max
|
|
143
|
+
AND NOT EXISTS (
|
|
144
|
+
SELECT 1
|
|
145
|
+
FROM public.outbox_events prior
|
|
146
|
+
WHERE prior.aggregate_type = event.aggregate_type
|
|
147
|
+
AND prior.aggregate_id = event.aggregate_id
|
|
148
|
+
AND prior.status IN ('pending', 'failed', 'processing')
|
|
149
|
+
AND (prior.created_at, prior.id) < (event.created_at, event.id)
|
|
150
|
+
AND (
|
|
151
|
+
prior.event_type = ANY (p_event_types)
|
|
152
|
+
OR (
|
|
153
|
+
prior.event_type <> ALL (COALESCE(v_known, ARRAY[]::text[]))
|
|
154
|
+
AND NOT EXISTS (
|
|
155
|
+
SELECT 1
|
|
156
|
+
FROM public.outbox_dormant_event_types dormant
|
|
157
|
+
WHERE dormant.event_type = prior.event_type
|
|
158
|
+
)
|
|
159
|
+
)
|
|
160
|
+
))
|
|
161
|
+
ORDER BY event.created_at, event.id
|
|
162
|
+
LIMIT v_batch
|
|
163
|
+
FOR UPDATE SKIP LOCKED
|
|
164
|
+
)
|
|
165
|
+
UPDATE public.outbox_events event
|
|
166
|
+
SET status = 'processing',
|
|
167
|
+
attempts = event.attempts + 1,
|
|
168
|
+
available_at = now() + make_interval(secs => v_visibility),
|
|
169
|
+
metadata = event.metadata || jsonb_build_object(
|
|
170
|
+
'lastClaimedAt', now(),
|
|
171
|
+
'claimToken', gen_random_uuid()::text)
|
|
172
|
+
FROM candidates
|
|
173
|
+
WHERE event.id = candidates.id
|
|
174
|
+
RETURNING event.*;
|
|
175
|
+
END;
|
|
176
|
+
$$;
|
|
177
|
+
|
|
178
|
+
-- Acknowledge success. Returns false rather than raising when the row is no longer the
|
|
179
|
+
-- caller's: a lease that expired and was re-claimed elsewhere is an ordinary race, and the
|
|
180
|
+
-- caller logs it instead of aborting a batch.
|
|
181
|
+
CREATE FUNCTION public.outbox_mark_processed(
|
|
182
|
+
p_event_id uuid,
|
|
183
|
+
p_claim_token text,
|
|
184
|
+
p_metadata jsonb DEFAULT '{}'::jsonb
|
|
185
|
+
)
|
|
186
|
+
RETURNS boolean
|
|
187
|
+
LANGUAGE sql
|
|
188
|
+
SET search_path = public, pg_catalog
|
|
189
|
+
AS $$
|
|
190
|
+
WITH updated AS (
|
|
191
|
+
UPDATE public.outbox_events
|
|
192
|
+
SET status = 'processed',
|
|
193
|
+
processed_at = now(),
|
|
194
|
+
error = NULL,
|
|
195
|
+
metadata = metadata || COALESCE(p_metadata, '{}'::jsonb)
|
|
196
|
+
WHERE id = p_event_id
|
|
197
|
+
AND status = 'processing'
|
|
198
|
+
AND metadata->>'claimToken' = p_claim_token
|
|
199
|
+
RETURNING id
|
|
200
|
+
)
|
|
201
|
+
SELECT EXISTS (SELECT 1 FROM updated);
|
|
202
|
+
$$;
|
|
203
|
+
|
|
204
|
+
-- Acknowledge failure. The outcome the handler asks for is a request, not a verdict: a
|
|
205
|
+
-- 'retry' on a row that has already spent its attempts becomes a discard, so the ceiling is
|
|
206
|
+
-- enforced in one place instead of in every caller.
|
|
207
|
+
--
|
|
208
|
+
-- 'snooze' means the downstream is unavailable rather than the row being bad, so the attempt
|
|
209
|
+
-- taken at claim is refunded -- an outage must not consume the DLQ budget of the events it
|
|
210
|
+
-- happened to touch. Retry delay is exponential with equal jitter in [d/2, d]: without the
|
|
211
|
+
-- jitter every row that failed in the same tick returns in the same later tick, and the
|
|
212
|
+
-- convoy reforms on each retry instead of dispersing.
|
|
213
|
+
CREATE FUNCTION public.outbox_mark_failed(
|
|
214
|
+
p_event_id uuid,
|
|
215
|
+
p_claim_token text,
|
|
216
|
+
p_error text,
|
|
217
|
+
p_outcome text DEFAULT 'retry',
|
|
218
|
+
p_base_delay_seconds integer DEFAULT 60,
|
|
219
|
+
p_max_delay_seconds integer DEFAULT 3600,
|
|
220
|
+
p_max_attempts integer DEFAULT 8,
|
|
221
|
+
p_snooze_seconds integer DEFAULT 300
|
|
222
|
+
)
|
|
223
|
+
RETURNS text
|
|
224
|
+
LANGUAGE plpgsql
|
|
225
|
+
SET search_path = public, pg_catalog
|
|
226
|
+
AS $$
|
|
227
|
+
DECLARE
|
|
228
|
+
v_row public.outbox_events%ROWTYPE;
|
|
229
|
+
v_base integer := LEAST(GREATEST(COALESCE(p_base_delay_seconds, 60), 5), 3600);
|
|
230
|
+
v_cap integer;
|
|
231
|
+
v_max integer := LEAST(GREATEST(COALESCE(p_max_attempts, 8), 1), 20);
|
|
232
|
+
v_snooze integer := LEAST(GREATEST(COALESCE(p_snooze_seconds, 300), 60), 3600);
|
|
233
|
+
v_delay double precision;
|
|
234
|
+
BEGIN
|
|
235
|
+
IF p_outcome IS NULL OR p_outcome NOT IN ('retry', 'discard', 'snooze') THEN
|
|
236
|
+
RAISE EXCEPTION 'outbox_mark_failed_invalid_outcome' USING ERRCODE = '22023';
|
|
237
|
+
END IF;
|
|
238
|
+
|
|
239
|
+
v_cap := LEAST(GREATEST(v_base, COALESCE(p_max_delay_seconds, 3600)), 86400);
|
|
240
|
+
|
|
241
|
+
SELECT *
|
|
242
|
+
INTO v_row
|
|
243
|
+
FROM public.outbox_events
|
|
244
|
+
WHERE id = p_event_id
|
|
245
|
+
AND status = 'processing'
|
|
246
|
+
AND metadata->>'claimToken' = p_claim_token
|
|
247
|
+
FOR UPDATE;
|
|
248
|
+
|
|
249
|
+
IF NOT FOUND THEN
|
|
250
|
+
RETURN 'missed';
|
|
251
|
+
END IF;
|
|
252
|
+
|
|
253
|
+
IF p_outcome = 'discard' OR (p_outcome = 'retry' AND v_row.attempts >= v_max) THEN
|
|
254
|
+
UPDATE public.outbox_events
|
|
255
|
+
SET status = 'discarded',
|
|
256
|
+
error = left(p_error, 2000),
|
|
257
|
+
metadata = metadata || jsonb_build_object(
|
|
258
|
+
'discardReason',
|
|
259
|
+
CASE WHEN p_outcome = 'discard' THEN 'permanent_error' ELSE 'max_attempts' END,
|
|
260
|
+
'discardedAt', now(),
|
|
261
|
+
'finalAttempts', v_row.attempts)
|
|
262
|
+
WHERE id = v_row.id;
|
|
263
|
+
RETURN 'discarded';
|
|
264
|
+
END IF;
|
|
265
|
+
|
|
266
|
+
IF p_outcome = 'snooze' THEN
|
|
267
|
+
UPDATE public.outbox_events
|
|
268
|
+
SET status = 'failed',
|
|
269
|
+
attempts = GREATEST(v_row.attempts - 1, 0),
|
|
270
|
+
available_at = now() + make_interval(secs => v_snooze),
|
|
271
|
+
error = left(p_error, 2000),
|
|
272
|
+
metadata = metadata || jsonb_build_object(
|
|
273
|
+
'snoozeCount', COALESCE((metadata->>'snoozeCount')::integer, 0) + 1,
|
|
274
|
+
'lastSnoozedAt', now())
|
|
275
|
+
WHERE id = v_row.id;
|
|
276
|
+
RETURN 'snoozed';
|
|
277
|
+
END IF;
|
|
278
|
+
|
|
279
|
+
v_delay := LEAST(
|
|
280
|
+
v_cap::double precision,
|
|
281
|
+
v_base::double precision * power(2, LEAST(GREATEST(v_row.attempts - 1, 0), 16))
|
|
282
|
+
) * (0.5 + random() * 0.5);
|
|
283
|
+
|
|
284
|
+
UPDATE public.outbox_events
|
|
285
|
+
SET status = 'failed',
|
|
286
|
+
available_at = now() + make_interval(secs => v_delay),
|
|
287
|
+
error = left(p_error, 2000)
|
|
288
|
+
WHERE id = v_row.id;
|
|
289
|
+
RETURN 'failed';
|
|
290
|
+
END;
|
|
291
|
+
$$;
|
|
292
|
+
|
|
293
|
+
-- Hand back rows the caller claimed but never ran -- a drain loop that aborts mid-batch, or
|
|
294
|
+
-- one handler reporting the downstream is out. The attempt taken at claim is refunded for
|
|
295
|
+
-- exactly the same reason as in 'snooze': nothing was tried, so nothing was spent. Ids and
|
|
296
|
+
-- tokens are supplied as parallel arrays and must agree in length, because a mismatch means
|
|
297
|
+
-- the caller lost track of which token belongs to which row and releasing the wrong pairing
|
|
298
|
+
-- would silently release nothing.
|
|
299
|
+
CREATE FUNCTION public.outbox_release_unprocessed(
|
|
300
|
+
p_event_ids uuid[],
|
|
301
|
+
p_claim_tokens text[],
|
|
302
|
+
p_delay_seconds integer DEFAULT 0
|
|
303
|
+
)
|
|
304
|
+
RETURNS integer
|
|
305
|
+
LANGUAGE plpgsql
|
|
306
|
+
SET search_path = public, pg_catalog
|
|
307
|
+
AS $$
|
|
308
|
+
DECLARE
|
|
309
|
+
v_delay integer := LEAST(GREATEST(COALESCE(p_delay_seconds, 0), 0), 3600);
|
|
310
|
+
v_count integer;
|
|
311
|
+
BEGIN
|
|
312
|
+
IF p_event_ids IS NULL OR p_claim_tokens IS NULL
|
|
313
|
+
OR cardinality(p_event_ids) <> cardinality(p_claim_tokens) THEN
|
|
314
|
+
RAISE EXCEPTION 'outbox_release_invalid_input' USING ERRCODE = '22023';
|
|
315
|
+
END IF;
|
|
316
|
+
|
|
317
|
+
UPDATE public.outbox_events event
|
|
318
|
+
SET status = 'pending',
|
|
319
|
+
available_at = now() + make_interval(secs => v_delay),
|
|
320
|
+
attempts = GREATEST(event.attempts - 1, 0)
|
|
321
|
+
FROM unnest(p_event_ids, p_claim_tokens) AS released(id, token)
|
|
322
|
+
WHERE event.id = released.id
|
|
323
|
+
AND event.status = 'processing'
|
|
324
|
+
AND event.metadata->>'claimToken' = released.token;
|
|
325
|
+
|
|
326
|
+
GET DIAGNOSTICS v_count = ROW_COUNT;
|
|
327
|
+
RETURN v_count;
|
|
328
|
+
END;
|
|
329
|
+
$$;
|