@anatta.io/tva 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -2
- package/dist/adapters/cloudflare/bindings.d.ts +10 -0
- package/dist/adapters/cloudflare/bindings.js +1 -0
- package/dist/adapters/cloudflare/blob-store.d.ts +39 -0
- package/dist/adapters/cloudflare/blob-store.js +95 -0
- package/dist/adapters/cloudflare/cache.d.ts +14 -0
- package/dist/adapters/cloudflare/cache.js +48 -0
- package/dist/adapters/cloudflare/faults.d.ts +7 -0
- package/dist/adapters/cloudflare/faults.js +39 -0
- package/dist/adapters/cloudflare/index.d.ts +15 -0
- package/dist/adapters/cloudflare/index.js +16 -0
- package/dist/adapters/cloudflare/journal.d.ts +110 -0
- package/dist/adapters/cloudflare/journal.js +325 -0
- package/dist/adapters/cloudflare/ledger.d.ts +14 -0
- package/dist/adapters/cloudflare/ledger.js +108 -0
- package/dist/adapters/cloudflare/logger.d.ts +22 -0
- package/dist/adapters/cloudflare/logger.js +49 -0
- package/dist/adapters/cloudflare/manifest.d.ts +81 -0
- package/dist/adapters/cloudflare/manifest.js +81 -0
- package/dist/adapters/cloudflare/pipelines.d.ts +97 -0
- package/dist/adapters/cloudflare/pipelines.js +218 -0
- package/dist/adapters/cloudflare/queue.d.ts +80 -0
- package/dist/adapters/cloudflare/queue.js +177 -0
- package/dist/adapters/cloudflare/retention.d.ts +24 -0
- package/dist/adapters/cloudflare/retention.js +103 -0
- package/dist/adapters/cloudflare/session-storage.d.ts +27 -0
- package/dist/adapters/cloudflare/session-storage.js +78 -0
- package/dist/adapters/cloudflare/toolkit.d.ts +16 -0
- package/dist/adapters/cloudflare/toolkit.js +57 -0
- package/dist/adapters/cloudflare/workflow-pipelines.d.ts +66 -0
- package/dist/adapters/cloudflare/workflow-pipelines.js +323 -0
- package/dist/adapters/journal-records.d.ts +80 -0
- package/dist/adapters/journal-records.js +75 -0
- package/dist/adapters/memory/blob-store.d.ts +9 -0
- package/dist/adapters/memory/blob-store.js +19 -0
- package/dist/adapters/memory/cache.d.ts +13 -0
- package/dist/adapters/memory/cache.js +36 -0
- package/dist/adapters/memory/clock.d.ts +1 -0
- package/dist/adapters/memory/clock.js +4 -0
- package/dist/adapters/memory/index.d.ts +11 -0
- package/dist/adapters/memory/index.js +11 -0
- package/dist/adapters/memory/journal.d.ts +35 -0
- package/dist/adapters/memory/journal.js +154 -0
- package/dist/adapters/memory/ledger.d.ts +9 -0
- package/dist/adapters/memory/ledger.js +33 -0
- package/dist/adapters/memory/logger.d.ts +16 -0
- package/dist/adapters/memory/logger.js +21 -0
- package/dist/adapters/memory/manifest.d.ts +59 -0
- package/dist/adapters/memory/manifest.js +59 -0
- package/dist/adapters/memory/pipelines.d.ts +62 -0
- package/dist/adapters/memory/pipelines.js +219 -0
- package/dist/adapters/memory/queue.d.ts +20 -0
- package/dist/adapters/memory/queue.js +102 -0
- package/dist/adapters/memory/session-storage.d.ts +20 -0
- package/dist/adapters/memory/session-storage.js +33 -0
- package/dist/adapters/memory/toolkit.d.ts +8 -0
- package/dist/adapters/memory/toolkit.js +31 -0
- package/dist/adapters/postgres/cache.d.ts +17 -0
- package/dist/adapters/postgres/cache.js +69 -0
- package/dist/adapters/postgres/config.d.ts +27 -0
- package/dist/adapters/postgres/config.js +45 -0
- package/dist/adapters/postgres/faults.d.ts +14 -0
- package/dist/adapters/postgres/faults.js +47 -0
- package/dist/adapters/postgres/index.d.ts +12 -0
- package/dist/adapters/postgres/index.js +12 -0
- package/dist/adapters/postgres/journal.d.ts +86 -0
- package/dist/adapters/postgres/journal.js +337 -0
- package/dist/adapters/postgres/ledger.d.ts +16 -0
- package/dist/adapters/postgres/ledger.js +97 -0
- package/dist/adapters/postgres/logger.d.ts +19 -0
- package/dist/adapters/postgres/logger.js +37 -0
- package/dist/adapters/postgres/manifest.d.ts +52 -0
- package/dist/adapters/postgres/manifest.js +53 -0
- package/dist/adapters/postgres/migrate.d.ts +20 -0
- package/dist/adapters/postgres/migrate.js +96 -0
- package/dist/adapters/postgres/queue.d.ts +64 -0
- package/dist/adapters/postgres/queue.js +333 -0
- package/dist/adapters/postgres/retention.d.ts +18 -0
- package/dist/adapters/postgres/retention.js +104 -0
- package/dist/adapters/postgres/session-storage.d.ts +20 -0
- package/dist/adapters/postgres/session-storage.js +71 -0
- package/dist/adapters/postgres/toolkit.d.ts +25 -0
- package/dist/adapters/postgres/toolkit.js +58 -0
- package/dist/adapters/postgres/with-client.d.ts +11 -0
- package/dist/adapters/postgres/with-client.js +29 -0
- package/dist/adapters/reference-executor.d.ts +171 -0
- package/dist/adapters/reference-executor.js +569 -0
- package/dist/adapters/retention.d.ts +18 -0
- package/dist/adapters/retention.js +13 -0
- package/dist/adapters/s3/blob-store.d.ts +35 -0
- package/dist/adapters/s3/blob-store.js +92 -0
- package/dist/adapters/s3/faults.d.ts +8 -0
- package/dist/adapters/s3/faults.js +34 -0
- package/dist/adapters/s3/index.d.ts +2 -0
- package/dist/adapters/s3/index.js +2 -0
- package/dist/adapters/session-envelope.d.ts +21 -0
- package/dist/adapters/session-envelope.js +122 -0
- package/dist/adapters/system-clock.d.ts +8 -0
- package/dist/adapters/system-clock.js +8 -0
- package/dist/contracts/blob-store.d.ts +11 -0
- package/dist/contracts/blob-store.js +1 -0
- package/dist/contracts/cache.d.ts +27 -0
- package/dist/contracts/cache.js +1 -0
- package/dist/contracts/duration.d.ts +23 -0
- package/dist/contracts/duration.js +27 -0
- package/dist/contracts/faults.d.ts +28 -0
- package/dist/contracts/faults.js +29 -0
- package/dist/contracts/identity.d.ts +12 -0
- package/dist/contracts/identity.js +1 -0
- package/dist/contracts/index.d.ts +14 -0
- package/dist/contracts/index.js +15 -0
- package/dist/contracts/instant.d.ts +17 -0
- package/dist/contracts/instant.js +12 -0
- package/dist/contracts/journal.d.ts +196 -0
- package/dist/contracts/journal.js +10 -0
- package/dist/contracts/ledger.d.ts +34 -0
- package/dist/contracts/ledger.js +1 -0
- package/dist/contracts/logger.d.ts +11 -0
- package/dist/contracts/logger.js +1 -0
- package/dist/contracts/manifest.d.ts +37 -0
- package/dist/contracts/manifest.js +30 -0
- package/dist/contracts/pipelines.d.ts +348 -0
- package/dist/contracts/pipelines.js +1 -0
- package/dist/contracts/queue.d.ts +55 -0
- package/dist/contracts/queue.js +1 -0
- package/dist/contracts/session-storage.d.ts +18 -0
- package/dist/contracts/session-storage.js +1 -0
- package/dist/contracts/toolkit.d.ts +21 -0
- package/dist/contracts/toolkit.js +1 -0
- package/dist/pipelines/index.d.ts +1 -0
- package/dist/pipelines/index.js +2 -0
- package/dist/pipelines/pipeline.d.ts +142 -0
- package/dist/pipelines/pipeline.js +140 -0
- package/dist/pipelines/use-case-step.d.ts +24 -0
- package/dist/pipelines/use-case-step.js +34 -0
- package/dist/schedule/schedule.d.ts +40 -0
- package/dist/schedule/schedule.js +135 -0
- package/dist/use-cases/index.d.ts +3 -0
- package/dist/use-cases/index.js +3 -0
- package/dist/use-cases/run-use-case.d.ts +33 -0
- package/dist/use-cases/run-use-case.js +117 -0
- package/dist/use-cases/use-case-consumer.d.ts +44 -0
- package/dist/use-cases/use-case-consumer.js +98 -0
- package/dist/use-cases/use-case.d.ts +116 -0
- package/dist/use-cases/use-case.js +75 -0
- package/dist/webhooks/create-webhook-handler.d.ts +63 -0
- package/dist/webhooks/create-webhook-handler.js +253 -0
- package/dist/webhooks/dead-letter-consumer.d.ts +14 -0
- package/dist/webhooks/dead-letter-consumer.js +42 -0
- package/dist/webhooks/define-registry.d.ts +42 -0
- package/dist/webhooks/define-registry.js +56 -0
- package/dist/webhooks/once.d.ts +15 -0
- package/dist/webhooks/once.js +29 -0
- package/dist/webhooks/redact-customer-bodies.d.ts +29 -0
- package/dist/webhooks/redact-customer-bodies.js +45 -0
- package/dist/webhooks/replay-webhooks.d.ts +39 -0
- package/dist/webhooks/replay-webhooks.js +82 -0
- package/dist/webhooks/sign-shopify-webhook.d.ts +14 -0
- package/dist/webhooks/sign-shopify-webhook.js +31 -0
- package/dist/webhooks/sweep-stuck-tasks.d.ts +32 -0
- package/dist/webhooks/sweep-stuck-tasks.js +73 -0
- package/dist/webhooks/webhook-consumer.d.ts +52 -0
- package/dist/webhooks/webhook-consumer.js +132 -0
- package/migrations/0001_create_ledger_claims.sql +23 -0
- package/migrations/0002_create_journal.sql +76 -0
- package/migrations/0003_create_shopify_sessions.sql +30 -0
- package/migrations/0004_create_journal_steps.sql +34 -0
- package/migrations/0007_create_job_tables.sql +71 -0
- package/migrations/0008_add_ledger_completed_at.sql +7 -0
- package/migrations/0009_create_webhook_receipts.sql +34 -0
- package/migrations/0010_index_webhook_deliveries_received_at.sql +2 -0
- package/migrations/0011_add_webhook_task_reason_code.sql +4 -0
- package/migrations/0012_rename_webhook_task_action_to_use_case.sql +11 -0
- package/migrations/0013_rename_job_tables_to_pipeline.sql +24 -0
- package/migrations/0016_add_webhook_task_requeues.sql +3 -0
- package/migrations/postgres/0001_create_ledger_claims.sql +13 -0
- package/migrations/postgres/0002_create_journal.sql +48 -0
- package/migrations/postgres/0003_create_shopify_sessions.sql +12 -0
- package/migrations/postgres/0004_create_journal_steps.sql +19 -0
- package/migrations/postgres/0005_create_cache_entries.sql +14 -0
- package/migrations/postgres/0006_create_queue.sql +42 -0
- package/migrations/postgres/0007_create_job_tables.sql +40 -0
- package/migrations/postgres/0008_add_ledger_completed_at.sql +3 -0
- package/migrations/postgres/0009_create_webhook_receipts.sql +23 -0
- package/migrations/postgres/0010_index_webhook_deliveries_received_at.sql +2 -0
- package/migrations/postgres/0011_add_webhook_task_reason_code.sql +3 -0
- package/migrations/postgres/0012_rename_webhook_task_action_to_use_case.sql +5 -0
- package/migrations/postgres/0013_rename_job_tables_to_pipeline.sql +22 -0
- package/migrations/postgres/0016_add_webhook_task_requeues.sql +3 -0
- package/package.json +144 -4
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Ledger } from '../contracts/index.js';
|
|
2
|
+
export interface OnceOptions {
|
|
3
|
+
readonly ledger: Ledger;
|
|
4
|
+
readonly source: string;
|
|
5
|
+
readonly useCase: string;
|
|
6
|
+
/** How long a run may take before another attempt can claim the key. */
|
|
7
|
+
readonly leaseSeconds: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Runs a use case's side effect once per key: claim, run, complete.
|
|
11
|
+
* `'already-done'` does not carry the first run's result; it is not stored.
|
|
12
|
+
* `'in-progress'` means another run holds the lease; retry the task later.
|
|
13
|
+
*/
|
|
14
|
+
export type Once = <T>(key: string, ttlSeconds: number, fn: () => Promise<T>) => Promise<T | 'already-done' | 'in-progress'>;
|
|
15
|
+
export declare function createOnce({ ledger, source, useCase, leaseSeconds, }: OnceOptions): Once;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export function createOnce({ ledger, source, useCase, leaseSeconds, }) {
|
|
2
|
+
// A ':' in either would let two use cases build the same ledger key.
|
|
3
|
+
if (`${source}${useCase}`.includes(':')) {
|
|
4
|
+
throw new TypeError(`source and useCase must not contain ':'`);
|
|
5
|
+
}
|
|
6
|
+
return async (key, ttlSeconds, fn) => {
|
|
7
|
+
const ledgerKey = `${source}:${useCase}:${key}`;
|
|
8
|
+
const claim = await ledger.claim(ledgerKey, leaseSeconds);
|
|
9
|
+
if (claim.status === 'completed') {
|
|
10
|
+
return 'already-done';
|
|
11
|
+
}
|
|
12
|
+
if (claim.status === 'in-progress') {
|
|
13
|
+
return 'in-progress';
|
|
14
|
+
}
|
|
15
|
+
let result;
|
|
16
|
+
try {
|
|
17
|
+
result = await fn();
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
// shortcut: claims carry no owner token, so a run that outlives its lease
|
|
21
|
+
// and then throws releases the next holder's claim; add a token to Ledger if that matters.
|
|
22
|
+
// The lease frees the key if release fails; keep fn's error.
|
|
23
|
+
await ledger.release(ledgerKey).catch(() => { });
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
await ledger.complete(ledgerKey, ttlSeconds);
|
|
27
|
+
return result;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { BlobStore } from '../contracts/index.js';
|
|
2
|
+
/** The fields of a `customers/redact` body this reads. */
|
|
3
|
+
export type CustomersRedactPayload = {
|
|
4
|
+
readonly shop_domain: string;
|
|
5
|
+
readonly customer: {
|
|
6
|
+
readonly id: number | string;
|
|
7
|
+
readonly email?: string | null;
|
|
8
|
+
};
|
|
9
|
+
readonly orders_to_redact?: readonly (number | string)[];
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Decision 0010: a handled `customers/redact` deletes, at once, each raw body
|
|
13
|
+
* of that shop that names the customer: its customer id or one of
|
|
14
|
+
* `orders_to_redact` as a whole number, or its email in any case. A false match
|
|
15
|
+
* deletes a body early, never keeps one.
|
|
16
|
+
*
|
|
17
|
+
* It wins over the retention sweep's rule that a task not yet final keeps its
|
|
18
|
+
* body: that task's retry then finds no body, a fault, and goes dead. `keepKey`
|
|
19
|
+
* spares the redact delivery's own body, so its own retry can still read it;
|
|
20
|
+
* the retention sweep deletes it later. Returns how many bodies it deleted.
|
|
21
|
+
*
|
|
22
|
+
* shortcut: reads every body the shop has in one call, which on Workers can
|
|
23
|
+
* pass the per-invocation operation limit for a very busy shop; make it a paged
|
|
24
|
+
* Pipeline when one does.
|
|
25
|
+
*/
|
|
26
|
+
export declare function redactCustomerBodies(blobStore: BlobStore, payload: CustomersRedactPayload, options: {
|
|
27
|
+
readonly source: string;
|
|
28
|
+
readonly keepKey?: string;
|
|
29
|
+
}): Promise<number>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const numericId = (id) => {
|
|
2
|
+
const s = String(id);
|
|
3
|
+
if (!/^\d+$/.test(s)) {
|
|
4
|
+
throw new TypeError(`redactCustomerBodies: id ${s} is not numeric`);
|
|
5
|
+
}
|
|
6
|
+
return s;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Decision 0010: a handled `customers/redact` deletes, at once, each raw body
|
|
10
|
+
* of that shop that names the customer: its customer id or one of
|
|
11
|
+
* `orders_to_redact` as a whole number, or its email in any case. A false match
|
|
12
|
+
* deletes a body early, never keeps one.
|
|
13
|
+
*
|
|
14
|
+
* It wins over the retention sweep's rule that a task not yet final keeps its
|
|
15
|
+
* body: that task's retry then finds no body, a fault, and goes dead. `keepKey`
|
|
16
|
+
* spares the redact delivery's own body, so its own retry can still read it;
|
|
17
|
+
* the retention sweep deletes it later. Returns how many bodies it deleted.
|
|
18
|
+
*
|
|
19
|
+
* shortcut: reads every body the shop has in one call, which on Workers can
|
|
20
|
+
* pass the per-invocation operation limit for a very busy shop; make it a paged
|
|
21
|
+
* Pipeline when one does.
|
|
22
|
+
*/
|
|
23
|
+
export async function redactCustomerBodies(blobStore, payload, options) {
|
|
24
|
+
const shop = payload.shop_domain;
|
|
25
|
+
if (shop === '' || shop.includes('/')) {
|
|
26
|
+
throw new TypeError(`redactCustomerBodies: shop ${shop} is not a domain`);
|
|
27
|
+
}
|
|
28
|
+
const ids = [payload.customer.id, ...(payload.orders_to_redact ?? [])].map(numericId);
|
|
29
|
+
const id = new RegExp(`(?<!\\d)(?:${ids.join('|')})(?!\\d)`);
|
|
30
|
+
const email = payload.customer.email?.toLowerCase() || undefined;
|
|
31
|
+
const names = (body) => id.test(body) || (email !== undefined && body.toLowerCase().includes(email));
|
|
32
|
+
const keys = await blobStore.list(`webhooks/${options.source}/${shop}/`);
|
|
33
|
+
let deleted = 0;
|
|
34
|
+
for (const key of keys) {
|
|
35
|
+
if (key === options.keepKey) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const body = await blobStore.getText(key);
|
|
39
|
+
if (body !== undefined && names(body)) {
|
|
40
|
+
await blobStore.delete(key);
|
|
41
|
+
deleted++;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return deleted;
|
|
45
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Toolkit } from '../contracts/index.js';
|
|
2
|
+
import type { WebhookMessage } from './create-webhook-handler.js';
|
|
3
|
+
import type { WebhookRegistry } from './define-registry.js';
|
|
4
|
+
/** What replay did with one task. Anything but `replayed` left it as it was. */
|
|
5
|
+
export type ReplayOutcome = 'replayed'
|
|
6
|
+
/** The raw body is gone: swept past retention, or redacted. */
|
|
7
|
+
| 'body-missing'
|
|
8
|
+
/** The registry has no such use case for the delivery's topic. */
|
|
9
|
+
| 'not-registered'
|
|
10
|
+
/** Not `dead` or `replayed`: done, or still in flight. */
|
|
11
|
+
| 'not-replayable';
|
|
12
|
+
export type ReplayResult = {
|
|
13
|
+
readonly found: false;
|
|
14
|
+
} | {
|
|
15
|
+
readonly found: true;
|
|
16
|
+
readonly tasks: readonly {
|
|
17
|
+
readonly useCase: string;
|
|
18
|
+
readonly outcome: ReplayOutcome;
|
|
19
|
+
}[];
|
|
20
|
+
};
|
|
21
|
+
export type ReplayDeps = Pick<Toolkit<unknown, WebhookMessage>, 'journal' | 'clock' | 'logger' | 'blobStore' | 'webhookQueue'>;
|
|
22
|
+
/**
|
|
23
|
+
* Re-queues a delivery's tasks: each `dead` one moves to `replayed`, and a
|
|
24
|
+
* use case registered since the delivery arrived gets its task, which backfills
|
|
25
|
+
* it. Each sends one message, and the consumer reads the raw body again from
|
|
26
|
+
* the blob store. `useCases` narrows it, `[name]` to one task; the default is
|
|
27
|
+
* every use case the registry has for the delivery's topic.
|
|
28
|
+
*
|
|
29
|
+
* Journaled as a `replay` run with one unit per task, `actor` in its
|
|
30
|
+
* `externalIds` (docs/operator-ledger.md §4). A use case that already did its
|
|
31
|
+
* work is kept from doing it twice by its own `once()` keys, not by replay.
|
|
32
|
+
*
|
|
33
|
+
* A send that rejects leaves the task `replayed` and rejects; replaying it
|
|
34
|
+
* again sends it again.
|
|
35
|
+
*/
|
|
36
|
+
export declare function replayWebhooks<Deps>(deps: ReplayDeps, registry: WebhookRegistry<Deps>, deliveryId: string, options: {
|
|
37
|
+
readonly useCases?: readonly string[];
|
|
38
|
+
readonly actor: string;
|
|
39
|
+
}): Promise<ReplayResult>;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { runUnit, withRun } from '../use-cases/run-use-case.js';
|
|
2
|
+
import { skipped, written } from '../use-cases/use-case.js';
|
|
3
|
+
/**
|
|
4
|
+
* Re-queues a delivery's tasks: each `dead` one moves to `replayed`, and a
|
|
5
|
+
* use case registered since the delivery arrived gets its task, which backfills
|
|
6
|
+
* it. Each sends one message, and the consumer reads the raw body again from
|
|
7
|
+
* the blob store. `useCases` narrows it, `[name]` to one task; the default is
|
|
8
|
+
* every use case the registry has for the delivery's topic.
|
|
9
|
+
*
|
|
10
|
+
* Journaled as a `replay` run with one unit per task, `actor` in its
|
|
11
|
+
* `externalIds` (docs/operator-ledger.md §4). A use case that already did its
|
|
12
|
+
* work is kept from doing it twice by its own `once()` keys, not by replay.
|
|
13
|
+
*
|
|
14
|
+
* A send that rejects leaves the task `replayed` and rejects; replaying it
|
|
15
|
+
* again sends it again.
|
|
16
|
+
*/
|
|
17
|
+
export async function replayWebhooks(deps, registry, deliveryId, options) {
|
|
18
|
+
const { journal, clock, blobStore, webhookQueue } = deps;
|
|
19
|
+
const found = await journal.delivery(deliveryId);
|
|
20
|
+
const externalIds = { actor: options.actor };
|
|
21
|
+
return withRun(deps, { job: 'webhooks.replay', trigger: 'replay', mode: 'live' }, async (scope) => {
|
|
22
|
+
if (found === undefined) {
|
|
23
|
+
await runUnit(REPLAY_UNIT, deliveryId, scope, null, async () => skipped('not-found', { externalIds }));
|
|
24
|
+
return { found: false };
|
|
25
|
+
}
|
|
26
|
+
const { delivery, tasks } = found;
|
|
27
|
+
const registered = registry
|
|
28
|
+
.useCasesFor(delivery.source, delivery.topic)
|
|
29
|
+
.map((r) => r.useCase.name);
|
|
30
|
+
// Checked before any task moves, so a swept body moves none. A sweep
|
|
31
|
+
// racing this leaves a replayed task whose run fails visibly (#141).
|
|
32
|
+
const hasBody = (await blobStore.get(delivery.bodyKey)) !== undefined;
|
|
33
|
+
const results = [];
|
|
34
|
+
for (const useCase of options.useCases ?? registered) {
|
|
35
|
+
const outcome = await replayOne(useCase);
|
|
36
|
+
results.push({ useCase, outcome });
|
|
37
|
+
await runUnit(REPLAY_UNIT, `${deliveryId}:${useCase}`, scope, null, async () => outcome === 'replayed'
|
|
38
|
+
? written({ externalIds })
|
|
39
|
+
: skipped(outcome, { externalIds }));
|
|
40
|
+
}
|
|
41
|
+
return { found: true, tasks: results };
|
|
42
|
+
async function replayOne(useCase) {
|
|
43
|
+
if (!registered.includes(useCase)) {
|
|
44
|
+
return 'not-registered';
|
|
45
|
+
}
|
|
46
|
+
if (!hasBody) {
|
|
47
|
+
return 'body-missing';
|
|
48
|
+
}
|
|
49
|
+
const task = tasks.find((t) => t.useCase === useCase);
|
|
50
|
+
if (task === undefined) {
|
|
51
|
+
await journal.recordDelivery(delivery, [useCase]);
|
|
52
|
+
}
|
|
53
|
+
else if (
|
|
54
|
+
// Already `replayed`: an earlier replay's send may have failed, and
|
|
55
|
+
// nothing else re-sends it. A second message is safe: once() keys.
|
|
56
|
+
task.state !== 'replayed' &&
|
|
57
|
+
!(await journal.moveTask(deliveryId, useCase, 'replayed', clock.now()))) {
|
|
58
|
+
return 'not-replayable';
|
|
59
|
+
}
|
|
60
|
+
const { source, topic, shop, webhookId, bodyKey } = delivery;
|
|
61
|
+
await webhookQueue.send({
|
|
62
|
+
source,
|
|
63
|
+
topic,
|
|
64
|
+
shop,
|
|
65
|
+
webhookId,
|
|
66
|
+
bodyKey,
|
|
67
|
+
useCase,
|
|
68
|
+
});
|
|
69
|
+
return 'replayed';
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* A replay unit, keyed by its task, whose decision is made before it is
|
|
75
|
+
* recorded. Not a use case: replay records many of them in one run, which a
|
|
76
|
+
* use case's `execute` never does (decision 0013).
|
|
77
|
+
*/
|
|
78
|
+
const REPLAY_UNIT = {
|
|
79
|
+
name: 'webhooks.replay',
|
|
80
|
+
unitType: 'webhook-task',
|
|
81
|
+
key: (unitKey) => unitKey,
|
|
82
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface SignShopifyWebhookOptions {
|
|
2
|
+
url: string | URL;
|
|
3
|
+
secret: string;
|
|
4
|
+
topic: string;
|
|
5
|
+
shop: string;
|
|
6
|
+
/** An object is serialized once; the signature covers the bytes sent. */
|
|
7
|
+
body: string | Uint8Array<ArrayBuffer> | ArrayBuffer | object;
|
|
8
|
+
deliveryId?: string;
|
|
9
|
+
/** Omitted when unset, because only `Clock` reads the time. */
|
|
10
|
+
triggeredAt?: string;
|
|
11
|
+
apiVersion?: string;
|
|
12
|
+
}
|
|
13
|
+
/** A POST signed the way Shopify signs a webhook delivery: base64 HMAC-SHA256 of the raw body. */
|
|
14
|
+
export declare function signShopifyWebhook({ url, secret, topic, shop, body, deliveryId, triggeredAt, apiVersion, }: SignShopifyWebhookOptions): Promise<Request>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Self-contained, with no relative imports, so plain `.mjs` scripts can load
|
|
2
|
+
// it through Node's type stripping (#129).
|
|
3
|
+
/** A POST signed the way Shopify signs a webhook delivery: base64 HMAC-SHA256 of the raw body. */
|
|
4
|
+
export async function signShopifyWebhook({ url, secret, topic, shop, body, deliveryId = crypto.randomUUID(), triggeredAt, apiVersion = '2026-10', }) {
|
|
5
|
+
// JSON.stringify turns these into "{}", which would be signed and sent silently.
|
|
6
|
+
if (body instanceof Blob || body instanceof ReadableStream) {
|
|
7
|
+
throw new TypeError('body must be a string, bytes or a JSON object');
|
|
8
|
+
}
|
|
9
|
+
const encoder = new TextEncoder();
|
|
10
|
+
const bytes = typeof body === 'string'
|
|
11
|
+
? encoder.encode(body)
|
|
12
|
+
: body instanceof Uint8Array
|
|
13
|
+
? body
|
|
14
|
+
: body instanceof ArrayBuffer
|
|
15
|
+
? new Uint8Array(body)
|
|
16
|
+
: encoder.encode(JSON.stringify(body));
|
|
17
|
+
const key = await crypto.subtle.importKey('raw', encoder.encode(secret), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
|
|
18
|
+
const mac = new Uint8Array(await crypto.subtle.sign('HMAC', key, bytes));
|
|
19
|
+
const headers = new Headers({
|
|
20
|
+
'Content-Type': 'application/json',
|
|
21
|
+
'X-Shopify-Hmac-Sha256': btoa(String.fromCharCode(...mac)),
|
|
22
|
+
'X-Shopify-Topic': topic,
|
|
23
|
+
'X-Shopify-Shop-Domain': shop,
|
|
24
|
+
'X-Shopify-API-Version': apiVersion,
|
|
25
|
+
'X-Shopify-Webhook-Id': deliveryId,
|
|
26
|
+
});
|
|
27
|
+
if (triggeredAt !== undefined) {
|
|
28
|
+
headers.set('X-Shopify-Triggered-At', triggeredAt);
|
|
29
|
+
}
|
|
30
|
+
return new Request(url, { method: 'POST', headers, body: bytes });
|
|
31
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type Toolkit } from '../contracts/index.js';
|
|
2
|
+
import type { WebhookMessage } from './create-webhook-handler.js';
|
|
3
|
+
export type SweepStuckTasksDeps = Pick<Toolkit<unknown, WebhookMessage>, 'journal' | 'clock' | 'logger' | 'webhookQueue'>;
|
|
4
|
+
export type SweepStuckTasksOptions = {
|
|
5
|
+
/** How long a task may sit in `received`, `processing` or `replayed` before it counts as stuck. */
|
|
6
|
+
readonly stuckAfterSeconds?: number;
|
|
7
|
+
/** Re-sends before a stuck task is marked `dead` with reason `stuck`. */
|
|
8
|
+
readonly maxRequeues?: number;
|
|
9
|
+
/** Tasks per sweep. A sweep that hits it leaves the rest to the next one. */
|
|
10
|
+
readonly limit?: number;
|
|
11
|
+
};
|
|
12
|
+
export type SweepStuckTasksResult = {
|
|
13
|
+
readonly requeued: number;
|
|
14
|
+
readonly dead: number;
|
|
15
|
+
};
|
|
16
|
+
/** Longer than a queue's backlog under normal load, so a waiting task is not re-sent. */
|
|
17
|
+
export declare const DEFAULT_STUCK_AFTER_SECONDS: number;
|
|
18
|
+
export declare const DEFAULT_MAX_REQUEUES = 3;
|
|
19
|
+
/**
|
|
20
|
+
* Finds tasks stuck in `received`, `processing` or `replayed` (a lost send, a
|
|
21
|
+
* consumer that died, a partial `sendBatch`) and re-sends each one's message,
|
|
22
|
+
* or marks it `dead` with reason `stuck` once it has been re-sent
|
|
23
|
+
* `maxRequeues` times. So no task is left silent.
|
|
24
|
+
*
|
|
25
|
+
* Each task is claimed with `reclaimStuckTask` before it is sent, so two
|
|
26
|
+
* overlapping sweeps send it once, and a task that moved since it was read is
|
|
27
|
+
* left alone. A send that rejects rejects the sweep, and the task, already
|
|
28
|
+
* counted, is sent again by a later sweep.
|
|
29
|
+
*
|
|
30
|
+
* Journaled as a `webhooks.sweep-stuck` run with one unit per task it claimed.
|
|
31
|
+
*/
|
|
32
|
+
export declare function sweepStuckTasks(deps: SweepStuckTasksDeps, { stuckAfterSeconds, maxRequeues, limit, }?: SweepStuckTasksOptions): Promise<SweepStuckTasksResult>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { plusSeconds } from '../contracts/index.js';
|
|
2
|
+
import { runUnit, withRun } from '../use-cases/run-use-case.js';
|
|
3
|
+
import { skipped, written } from '../use-cases/use-case.js';
|
|
4
|
+
/** Longer than a queue's backlog under normal load, so a waiting task is not re-sent. */
|
|
5
|
+
export const DEFAULT_STUCK_AFTER_SECONDS = 15 * 60;
|
|
6
|
+
export const DEFAULT_MAX_REQUEUES = 3;
|
|
7
|
+
/**
|
|
8
|
+
* Finds tasks stuck in `received`, `processing` or `replayed` (a lost send, a
|
|
9
|
+
* consumer that died, a partial `sendBatch`) and re-sends each one's message,
|
|
10
|
+
* or marks it `dead` with reason `stuck` once it has been re-sent
|
|
11
|
+
* `maxRequeues` times. So no task is left silent.
|
|
12
|
+
*
|
|
13
|
+
* Each task is claimed with `reclaimStuckTask` before it is sent, so two
|
|
14
|
+
* overlapping sweeps send it once, and a task that moved since it was read is
|
|
15
|
+
* left alone. A send that rejects rejects the sweep, and the task, already
|
|
16
|
+
* counted, is sent again by a later sweep.
|
|
17
|
+
*
|
|
18
|
+
* Journaled as a `webhooks.sweep-stuck` run with one unit per task it claimed.
|
|
19
|
+
*/
|
|
20
|
+
export async function sweepStuckTasks(deps, { stuckAfterSeconds = DEFAULT_STUCK_AFTER_SECONDS, maxRequeues = DEFAULT_MAX_REQUEUES, limit = 100, } = {}) {
|
|
21
|
+
// At zero or below, a claim could rewrite updated_at to the value it matched, and a second sweep would send again.
|
|
22
|
+
if (!(stuckAfterSeconds > 0)) {
|
|
23
|
+
throw new RangeError(`stuckAfterSeconds must be positive, not ${stuckAfterSeconds}`);
|
|
24
|
+
}
|
|
25
|
+
const { journal, clock, webhookQueue } = deps;
|
|
26
|
+
const stuck = await journal.stuckTasks(plusSeconds(clock.now(), -stuckAfterSeconds), limit);
|
|
27
|
+
const result = { requeued: 0, dead: 0 };
|
|
28
|
+
if (stuck.length === 0) {
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
async function send(task) {
|
|
32
|
+
const found = await journal.delivery(task.deliveryId);
|
|
33
|
+
if (found === undefined) {
|
|
34
|
+
throw new Error(`webhook delivery missing: ${task.deliveryId}`);
|
|
35
|
+
}
|
|
36
|
+
const { source, topic, shop, webhookId, bodyKey } = found.delivery;
|
|
37
|
+
await webhookQueue.send({
|
|
38
|
+
source,
|
|
39
|
+
topic,
|
|
40
|
+
shop,
|
|
41
|
+
webhookId,
|
|
42
|
+
bodyKey,
|
|
43
|
+
useCase: task.useCase,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return withRun(deps, { job: 'webhooks.sweep-stuck', trigger: 'schedule', mode: 'live' }, async (scope) => {
|
|
47
|
+
for (const task of stuck) {
|
|
48
|
+
const to = task.requeues < maxRequeues ? 'requeued' : 'dead';
|
|
49
|
+
if (!(await journal.reclaimStuckTask(task, clock.now(), to))) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
result[to]++;
|
|
53
|
+
// Claimed already, so the unit's decision is made; it only sends.
|
|
54
|
+
await runUnit(SWEEP_UNIT, task, scope, null, async ({ track }) => {
|
|
55
|
+
if (to === 'dead') {
|
|
56
|
+
return skipped('stuck');
|
|
57
|
+
}
|
|
58
|
+
await track('queue', 'send', async () => send(task));
|
|
59
|
+
return written();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A sweep unit, keyed by its task. Not a use case: a sweep records many of
|
|
67
|
+
* them in one run, which a use case's `execute` never does (decision 0013).
|
|
68
|
+
*/
|
|
69
|
+
const SWEEP_UNIT = {
|
|
70
|
+
name: 'webhooks.sweep-stuck',
|
|
71
|
+
unitType: 'webhook-task',
|
|
72
|
+
key: (task) => `${task.deliveryId}:${task.useCase}`,
|
|
73
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { QueueBatch, QueueConsumer, QueueMessage, Toolkit } from '../contracts/index.js';
|
|
2
|
+
import type { UseCase } from '../use-cases/use-case.js';
|
|
3
|
+
import type { JournalDeps } from '../use-cases/run-use-case.js';
|
|
4
|
+
import { type UseCaseConsumerOptions } from '../use-cases/use-case-consumer.js';
|
|
5
|
+
import { type WebhookMessage } from './create-webhook-handler.js';
|
|
6
|
+
import type { WebhookRegistry } from './define-registry.js';
|
|
7
|
+
export type WebhookConsumerOptions<Deps> = Omit<UseCaseConsumerOptions<Deps>, 'job' | 'toolkit'> & {
|
|
8
|
+
/** Also reads each raw body from `blobStore`, where the edge wrote it. */
|
|
9
|
+
readonly toolkit: JournalDeps & Pick<Toolkit, 'blobStore'>;
|
|
10
|
+
/** The same registry the edge takes. Its entries may override `retryDelaySeconds` and `maxAttempts`. */
|
|
11
|
+
readonly registry: WebhookRegistry<Deps>;
|
|
12
|
+
};
|
|
13
|
+
/** The consumer's options, with one use case's retry settings applied. */
|
|
14
|
+
export type WebhookSettleOptions<Deps> = Omit<WebhookConsumerOptions<Deps>, 'registry'> & {
|
|
15
|
+
/** The queue's own limit; `maxAttempts` is the use case's, at most this. */
|
|
16
|
+
readonly queueMaxAttempts: number;
|
|
17
|
+
};
|
|
18
|
+
/** One topic's use case, with the parse that turns a raw body into its input. */
|
|
19
|
+
export type WebhookUseCase<Deps> = {
|
|
20
|
+
/** The use case's name, which names its task in the delivery's receipt. */
|
|
21
|
+
readonly name: string;
|
|
22
|
+
readonly settle: (batch: QueueBatch<WebhookMessage>, message: QueueMessage<WebhookMessage>, options: WebhookSettleOptions<Deps>) => Promise<void>;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Binds `useCase` to its `parse`. A missing body or a throw from `parse` is a
|
|
26
|
+
* fault: the message is retried and, after the last attempt, dead-lettered
|
|
27
|
+
* with no unit.
|
|
28
|
+
*
|
|
29
|
+
* Moves the delivery's task in the journal around the run: `processing`
|
|
30
|
+
* first, then `done` when the message is acked, `failed` when it is retried,
|
|
31
|
+
* or `dead` on the use case's last attempt, the latter two with the reason:
|
|
32
|
+
* the last retryable reason code, or `fault` for a throw. A dead task's message is retried
|
|
33
|
+
* only on the queue's last attempt too, so the queue dead-letters it, and is
|
|
34
|
+
* acked before that. These are required writes, so one
|
|
35
|
+
* that throws retries the message rather than ack it. A task that cannot move
|
|
36
|
+
* to `processing`, because it is already `done` or was never receipted, is
|
|
37
|
+
* logged and acked without running.
|
|
38
|
+
*/
|
|
39
|
+
export declare function webhookUseCase<Input, Reason extends string, Deps>(useCase: UseCase<Input, Reason, Deps>, parse: (body: string, message: WebhookMessage) => Input): WebhookUseCase<Deps>;
|
|
40
|
+
/**
|
|
41
|
+
* The consumer of `toolkit.webhookQueue`: each message runs the one use case it
|
|
42
|
+
* names and is acked or retried on its own (`settleUseCaseMessage`), so one
|
|
43
|
+
* use case failing does not hold back another for the same delivery. A message
|
|
44
|
+
* whose use case is not registered is logged and acked, never retried: design
|
|
45
|
+
* §7.1. Throws when a use case's `maxAttempts` is above the queue's.
|
|
46
|
+
*/
|
|
47
|
+
export declare function createWebhookConsumer<Deps>({ registry, ...options }: WebhookConsumerOptions<Deps>): QueueConsumer<WebhookMessage>;
|
|
48
|
+
export type { WebhookMessage } from './create-webhook-handler.js';
|
|
49
|
+
export { defineRegistry, WEBHOOK_MAX_USE_CASES_PER_TOPIC, type WebhookRegistration, type WebhookRegistry, type WebhookTopicSettings, } from './define-registry.js';
|
|
50
|
+
export { createDeadLetterConsumer } from './dead-letter-consumer.js';
|
|
51
|
+
export { replayWebhooks, type ReplayDeps, type ReplayOutcome, type ReplayResult, } from './replay-webhooks.js';
|
|
52
|
+
export { DEFAULT_MAX_REQUEUES, DEFAULT_STUCK_AFTER_SECONDS, sweepStuckTasks, type SweepStuckTasksDeps, type SweepStuckTasksOptions, type SweepStuckTasksResult, } from './sweep-stuck-tasks.js';
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { retryDelayFor, settleUseCaseMessage, } from '../use-cases/use-case-consumer.js';
|
|
2
|
+
import { webhookDeliveryId, } from './create-webhook-handler.js';
|
|
3
|
+
/**
|
|
4
|
+
* Binds `useCase` to its `parse`. A missing body or a throw from `parse` is a
|
|
5
|
+
* fault: the message is retried and, after the last attempt, dead-lettered
|
|
6
|
+
* with no unit.
|
|
7
|
+
*
|
|
8
|
+
* Moves the delivery's task in the journal around the run: `processing`
|
|
9
|
+
* first, then `done` when the message is acked, `failed` when it is retried,
|
|
10
|
+
* or `dead` on the use case's last attempt, the latter two with the reason:
|
|
11
|
+
* the last retryable reason code, or `fault` for a throw. A dead task's message is retried
|
|
12
|
+
* only on the queue's last attempt too, so the queue dead-letters it, and is
|
|
13
|
+
* acked before that. These are required writes, so one
|
|
14
|
+
* that throws retries the message rather than ack it. A task that cannot move
|
|
15
|
+
* to `processing`, because it is already `done` or was never receipted, is
|
|
16
|
+
* logged and acked without running.
|
|
17
|
+
*/
|
|
18
|
+
export function webhookUseCase(useCase, parse) {
|
|
19
|
+
return {
|
|
20
|
+
name: useCase.name,
|
|
21
|
+
settle: async (batch, message, options) => {
|
|
22
|
+
const { journal, clock, logger } = options.toolkit;
|
|
23
|
+
const deliveryId = webhookDeliveryId(message.body);
|
|
24
|
+
const context = {
|
|
25
|
+
deliveryId,
|
|
26
|
+
useCase: useCase.name,
|
|
27
|
+
messageId: message.id,
|
|
28
|
+
};
|
|
29
|
+
const move = async (to, reasonCode) => journal.moveTask(deliveryId, useCase.name, to, clock.now(), reasonCode);
|
|
30
|
+
const retry = () => {
|
|
31
|
+
batch.retry(message.id, {
|
|
32
|
+
delaySeconds: retryDelayFor(message.attempt, options.retryDelaySeconds),
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
try {
|
|
36
|
+
if (!(await move('processing'))) {
|
|
37
|
+
logger.warn('webhook task not runnable', context);
|
|
38
|
+
batch.ack(message.id);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
logger.error('webhook receipt failed', {
|
|
44
|
+
...context,
|
|
45
|
+
error: String(error),
|
|
46
|
+
});
|
|
47
|
+
retry();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
// Held until the receipt says how the message was settled.
|
|
51
|
+
let settled;
|
|
52
|
+
const reason = await settleUseCaseMessage({
|
|
53
|
+
messages: batch.messages,
|
|
54
|
+
ack: () => {
|
|
55
|
+
settled = {};
|
|
56
|
+
},
|
|
57
|
+
retry: (_id, retryOptions) => {
|
|
58
|
+
settled = { retry: retryOptions };
|
|
59
|
+
},
|
|
60
|
+
}, message, useCase, async (body) => {
|
|
61
|
+
const bytes = await options.toolkit.blobStore.get(body.bodyKey);
|
|
62
|
+
if (bytes === undefined) {
|
|
63
|
+
throw new Error(`webhook body missing: ${body.bodyKey}`);
|
|
64
|
+
}
|
|
65
|
+
return parse(new TextDecoder().decode(bytes), body);
|
|
66
|
+
}, options);
|
|
67
|
+
const acked = settled !== undefined && !('retry' in settled);
|
|
68
|
+
const dead = !acked && message.attempt >= options.maxAttempts;
|
|
69
|
+
try {
|
|
70
|
+
await move(acked ? 'done' : dead ? 'dead' : 'failed', reason);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
logger.error('webhook receipt failed', {
|
|
74
|
+
...context,
|
|
75
|
+
error: String(error),
|
|
76
|
+
});
|
|
77
|
+
retry();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (acked || (dead && message.attempt < options.queueMaxAttempts)) {
|
|
81
|
+
batch.ack(message.id);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
batch.retry(message.id, settled?.retry);
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The consumer of `toolkit.webhookQueue`: each message runs the one use case it
|
|
91
|
+
* names and is acked or retried on its own (`settleUseCaseMessage`), so one
|
|
92
|
+
* use case failing does not hold back another for the same delivery. A message
|
|
93
|
+
* whose use case is not registered is logged and acked, never retried: design
|
|
94
|
+
* §7.1. Throws when a use case's `maxAttempts` is above the queue's.
|
|
95
|
+
*/
|
|
96
|
+
export function createWebhookConsumer({ registry, ...options }) {
|
|
97
|
+
for (const { source, topic, useCase, maxAttempts } of registry.useCases) {
|
|
98
|
+
if (maxAttempts !== undefined && maxAttempts > options.maxAttempts) {
|
|
99
|
+
throw new TypeError(`webhooks: ${source} ${topic} ${useCase.name} maxAttempts (${maxAttempts}) is above the queue's (${options.maxAttempts})`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return async (batch) => {
|
|
103
|
+
for (const message of batch.messages) {
|
|
104
|
+
const { source, topic, shop, webhookId, useCase } = message.body;
|
|
105
|
+
const registration = registry
|
|
106
|
+
.useCasesFor(source, topic)
|
|
107
|
+
.find((entry) => entry.useCase.name === useCase);
|
|
108
|
+
if (registration !== undefined) {
|
|
109
|
+
await registration.useCase.settle(batch, message, {
|
|
110
|
+
...options,
|
|
111
|
+
retryDelaySeconds: registration.retryDelaySeconds ?? options.retryDelaySeconds,
|
|
112
|
+
maxAttempts: registration.maxAttempts ?? options.maxAttempts,
|
|
113
|
+
queueMaxAttempts: options.maxAttempts,
|
|
114
|
+
});
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
options.toolkit.logger.warn('webhook use case not registered', {
|
|
118
|
+
source,
|
|
119
|
+
topic,
|
|
120
|
+
useCase,
|
|
121
|
+
shop,
|
|
122
|
+
webhookId,
|
|
123
|
+
messageId: message.id,
|
|
124
|
+
});
|
|
125
|
+
batch.ack(message.id);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
export { defineRegistry, WEBHOOK_MAX_USE_CASES_PER_TOPIC, } from './define-registry.js';
|
|
130
|
+
export { createDeadLetterConsumer } from './dead-letter-consumer.js';
|
|
131
|
+
export { replayWebhooks, } from './replay-webhooks.js';
|
|
132
|
+
export { DEFAULT_MAX_REQUEUES, DEFAULT_STUCK_AFTER_SECONDS, sweepStuckTasks, } from './sweep-stuck-tasks.js';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
-- The idempotency ledger. One row per claimed key, and nothing else: there is
|
|
2
|
+
-- no get(), no list() and no query surface, because adding one turns this into
|
|
3
|
+
-- the run journal. See src/contracts/ledger.ts.
|
|
4
|
+
--
|
|
5
|
+
-- `key` is the PRIMARY KEY, and that is the whole concurrency story. Two
|
|
6
|
+
-- concurrent claims on one key cannot both insert, so the winner is decided by
|
|
7
|
+
-- the schema rather than by anything the adapter remembers to do.
|
|
8
|
+
--
|
|
9
|
+
-- Both timestamps are epoch milliseconds, written by the caller from the
|
|
10
|
+
-- injected Clock. Deliberately NOT `DEFAULT CURRENT_TIMESTAMP`: the conformance
|
|
11
|
+
-- suite advances a test clock by 11 seconds and expects a reclaim, and a
|
|
12
|
+
-- schema-side clock cannot be advanced.
|
|
13
|
+
CREATE TABLE ledger_claims (
|
|
14
|
+
key TEXT PRIMARY KEY,
|
|
15
|
+
claimed_at INTEGER NOT NULL,
|
|
16
|
+
expires_at INTEGER NOT NULL
|
|
17
|
+
) STRICT;
|
|
18
|
+
|
|
19
|
+
-- Serves nothing the Ledger interface exposes — there is no query that reads by
|
|
20
|
+
-- expiry. It exists for the operational sweep of rows whose lease ended long
|
|
21
|
+
-- ago, which is not part of this interface and should not need a table scan
|
|
22
|
+
-- when somebody writes it.
|
|
23
|
+
CREATE INDEX ledger_claims_expires_at ON ledger_claims (expires_at);
|