@superjackfruitlabs/treaty-outbox 0.0.0-stage → 0.0.5
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 +21 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +13 -0
- package/dist/postgres.d.ts +28 -0
- package/dist/postgres.js +101 -0
- package/dist/sqlite.d.ts +38 -0
- package/dist/sqlite.js +89 -0
- package/dist/types.d.ts +31 -0
- package/dist/types.js +22 -0
- package/package.json +27 -4
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rakesh Gangwar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A transactional outbox and a deduplicating inbox for CloudEvents (treaty plan step 5).
|
|
3
|
+
*
|
|
4
|
+
* A producer writes an event in the same transaction as the change it records, and a drain sends
|
|
5
|
+
* it at least once. A consumer handles each CloudEvents (source, id) once, in one transaction
|
|
6
|
+
* with the record that it did, so replaying an event changes nothing.
|
|
7
|
+
*
|
|
8
|
+
* `sqlite` is shaped for a Durable Object's `ctx.storage`; `postgres` for any client with
|
|
9
|
+
* `query(text, params)`.
|
|
10
|
+
*/
|
|
11
|
+
export * as sqlite from "./sqlite.ts";
|
|
12
|
+
export * as postgres from "./postgres.ts";
|
|
13
|
+
export { DEFAULT_BACKOFF, type Backoff, type Delivery, type OutboxEvent, type PendingEvent, } from "./types.ts";
|
|
14
|
+
export type { DrainOptions } from "./sqlite.ts";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A transactional outbox and a deduplicating inbox for CloudEvents (treaty plan step 5).
|
|
3
|
+
*
|
|
4
|
+
* A producer writes an event in the same transaction as the change it records, and a drain sends
|
|
5
|
+
* it at least once. A consumer handles each CloudEvents (source, id) once, in one transaction
|
|
6
|
+
* with the record that it did, so replaying an event changes nothing.
|
|
7
|
+
*
|
|
8
|
+
* `sqlite` is shaped for a Durable Object's `ctx.storage`; `postgres` for any client with
|
|
9
|
+
* `query(text, params)`.
|
|
10
|
+
*/
|
|
11
|
+
export * as sqlite from "./sqlite.js";
|
|
12
|
+
export * as postgres from "./postgres.js";
|
|
13
|
+
export { DEFAULT_BACKOFF, } from "./types.js";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The outbox and inbox on Postgres, through the smallest async client shape: `query(text, params)`
|
|
3
|
+
* returning `{ rows }`, which pg, PGlite and a thin wrapper over postgres.js all provide.
|
|
4
|
+
*
|
|
5
|
+
* - `enqueue` takes a client that is already inside the caller's transaction.
|
|
6
|
+
* - `drain` claims due rows with `FOR UPDATE SKIP LOCKED` and a lease, so several drains can run
|
|
7
|
+
* at once without sending the same row twice while it is leased, and sends outside any
|
|
8
|
+
* transaction. Delivery is at least once.
|
|
9
|
+
* - `handleOnce` takes the caller's way of running a transaction, and records the event in the
|
|
10
|
+
* same transaction as the handler's writes.
|
|
11
|
+
*/
|
|
12
|
+
import { type Delivery, type OutboxEvent, type PendingEvent } from "./types.ts";
|
|
13
|
+
import type { DrainOptions } from "./sqlite.ts";
|
|
14
|
+
export interface PgLike {
|
|
15
|
+
query(text: string, params?: unknown[]): Promise<{
|
|
16
|
+
rows: Record<string, unknown>[];
|
|
17
|
+
}>;
|
|
18
|
+
}
|
|
19
|
+
export declare const POSTGRES_SCHEMA: readonly ["CREATE TABLE IF NOT EXISTS treaty_outbox (\n seq BIGSERIAL PRIMARY KEY,\n source TEXT NOT NULL,\n id TEXT NOT NULL,\n subject TEXT NOT NULL,\n attributes JSONB NOT NULL,\n data BYTEA,\n attempts INTEGER NOT NULL DEFAULT 0,\n next_attempt_at BIGINT NOT NULL,\n last_error TEXT,\n dead BOOLEAN NOT NULL DEFAULT FALSE,\n UNIQUE (source, id)\n )", "CREATE INDEX IF NOT EXISTS treaty_outbox_due ON treaty_outbox (dead, next_attempt_at)", "CREATE TABLE IF NOT EXISTS treaty_inbox (\n source TEXT NOT NULL,\n id TEXT NOT NULL,\n received_at BIGINT NOT NULL,\n PRIMARY KEY (source, id)\n )"];
|
|
20
|
+
export declare function migrate(db: PgLike): Promise<void>;
|
|
21
|
+
/** Call with the client of the transaction that makes the change the event records. Enqueuing the same event again does nothing. */
|
|
22
|
+
export declare function enqueue(tx: PgLike, event: OutboxEvent, subject: string, nowMs?: number): Promise<void>;
|
|
23
|
+
export declare function drain(db: PgLike, send: (pending: PendingEvent) => Promise<void>, options?: DrainOptions): Promise<{
|
|
24
|
+
sent: number;
|
|
25
|
+
failed: number;
|
|
26
|
+
}>;
|
|
27
|
+
/** Runs `handler` once per (source, id), in one transaction with the record that it ran. */
|
|
28
|
+
export declare function handleOnce(transaction: <T>(fn: (tx: PgLike) => Promise<T>) => Promise<T>, event: OutboxEvent, handler: (tx: PgLike) => Promise<void>, nowMs?: number): Promise<Delivery>;
|
package/dist/postgres.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The outbox and inbox on Postgres, through the smallest async client shape: `query(text, params)`
|
|
3
|
+
* returning `{ rows }`, which pg, PGlite and a thin wrapper over postgres.js all provide.
|
|
4
|
+
*
|
|
5
|
+
* - `enqueue` takes a client that is already inside the caller's transaction.
|
|
6
|
+
* - `drain` claims due rows with `FOR UPDATE SKIP LOCKED` and a lease, so several drains can run
|
|
7
|
+
* at once without sending the same row twice while it is leased, and sends outside any
|
|
8
|
+
* transaction. Delivery is at least once.
|
|
9
|
+
* - `handleOnce` takes the caller's way of running a transaction, and records the event in the
|
|
10
|
+
* same transaction as the handler's writes.
|
|
11
|
+
*/
|
|
12
|
+
import { DEFAULT_BACKOFF, decodeEvent, encodeEvent, } from "./types.js";
|
|
13
|
+
export const POSTGRES_SCHEMA = [
|
|
14
|
+
`CREATE TABLE IF NOT EXISTS treaty_outbox (
|
|
15
|
+
seq BIGSERIAL PRIMARY KEY,
|
|
16
|
+
source TEXT NOT NULL,
|
|
17
|
+
id TEXT NOT NULL,
|
|
18
|
+
subject TEXT NOT NULL,
|
|
19
|
+
attributes JSONB NOT NULL,
|
|
20
|
+
data BYTEA,
|
|
21
|
+
attempts INTEGER NOT NULL DEFAULT 0,
|
|
22
|
+
next_attempt_at BIGINT NOT NULL,
|
|
23
|
+
last_error TEXT,
|
|
24
|
+
dead BOOLEAN NOT NULL DEFAULT FALSE,
|
|
25
|
+
UNIQUE (source, id)
|
|
26
|
+
)`,
|
|
27
|
+
`CREATE INDEX IF NOT EXISTS treaty_outbox_due ON treaty_outbox (dead, next_attempt_at)`,
|
|
28
|
+
`CREATE TABLE IF NOT EXISTS treaty_inbox (
|
|
29
|
+
source TEXT NOT NULL,
|
|
30
|
+
id TEXT NOT NULL,
|
|
31
|
+
received_at BIGINT NOT NULL,
|
|
32
|
+
PRIMARY KEY (source, id)
|
|
33
|
+
)`,
|
|
34
|
+
];
|
|
35
|
+
export async function migrate(db) {
|
|
36
|
+
for (const statement of POSTGRES_SCHEMA)
|
|
37
|
+
await db.query(statement);
|
|
38
|
+
}
|
|
39
|
+
/** Call with the client of the transaction that makes the change the event records. Enqueuing the same event again does nothing. */
|
|
40
|
+
export async function enqueue(tx, event, subject, nowMs = Date.now()) {
|
|
41
|
+
const { attributes, data } = encodeEvent(event);
|
|
42
|
+
await tx.query(`INSERT INTO treaty_outbox (source, id, subject, attributes, data, next_attempt_at) VALUES ($1, $2, $3, $4, $5, $6)
|
|
43
|
+
ON CONFLICT (source, id) DO NOTHING`, [
|
|
44
|
+
event.attributes["source"],
|
|
45
|
+
event.attributes["id"],
|
|
46
|
+
subject,
|
|
47
|
+
attributes,
|
|
48
|
+
data,
|
|
49
|
+
nowMs,
|
|
50
|
+
]);
|
|
51
|
+
}
|
|
52
|
+
export async function drain(db, send, options = {}) {
|
|
53
|
+
const now = options.nowMs ?? Date.now();
|
|
54
|
+
const backoff = options.backoff ?? DEFAULT_BACKOFF;
|
|
55
|
+
const { rows } = await db.query(`UPDATE treaty_outbox SET next_attempt_at = $1 WHERE seq IN (
|
|
56
|
+
SELECT seq FROM treaty_outbox WHERE NOT dead AND next_attempt_at <= $2 ORDER BY seq LIMIT $3 FOR UPDATE SKIP LOCKED
|
|
57
|
+
) RETURNING seq, subject, attributes, data, attempts`, [now + (options.leaseMs ?? 60_000), now, options.limit ?? 100]);
|
|
58
|
+
rows.sort((a, b) => Number(a["seq"]) - Number(b["seq"]));
|
|
59
|
+
let sent = 0;
|
|
60
|
+
let failed = 0;
|
|
61
|
+
for (const row of rows) {
|
|
62
|
+
const attempts = Number(row["attempts"]);
|
|
63
|
+
const attributes = typeof row["attributes"] === "string"
|
|
64
|
+
? row["attributes"]
|
|
65
|
+
: JSON.stringify(row["attributes"]);
|
|
66
|
+
try {
|
|
67
|
+
await send({
|
|
68
|
+
event: decodeEvent(attributes, row["data"] ?? null),
|
|
69
|
+
subject: String(row["subject"]),
|
|
70
|
+
attempts,
|
|
71
|
+
});
|
|
72
|
+
await db.query(`DELETE FROM treaty_outbox WHERE seq = $1`, [row["seq"]]);
|
|
73
|
+
sent++;
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
const next = attempts + 1;
|
|
77
|
+
await db.query(`UPDATE treaty_outbox SET attempts = $1, next_attempt_at = $2, last_error = $3, dead = $4 WHERE seq = $5`, [
|
|
78
|
+
next,
|
|
79
|
+
now + backoff.delayMs(next),
|
|
80
|
+
String(error),
|
|
81
|
+
next >= backoff.maxAttempts,
|
|
82
|
+
row["seq"],
|
|
83
|
+
]);
|
|
84
|
+
failed++;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return { sent, failed };
|
|
88
|
+
}
|
|
89
|
+
/** Runs `handler` once per (source, id), in one transaction with the record that it ran. */
|
|
90
|
+
export async function handleOnce(transaction, event, handler, nowMs = Date.now()) {
|
|
91
|
+
const { source, id } = event.attributes;
|
|
92
|
+
if (!source || !id)
|
|
93
|
+
throw new Error("an event needs its id and source attributes");
|
|
94
|
+
return transaction(async (tx) => {
|
|
95
|
+
const { rows } = await tx.query(`INSERT INTO treaty_inbox (source, id, received_at) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING RETURNING id`, [source, id, nowMs]);
|
|
96
|
+
if (rows.length === 0)
|
|
97
|
+
return "duplicate";
|
|
98
|
+
await handler(tx);
|
|
99
|
+
return "handled";
|
|
100
|
+
});
|
|
101
|
+
}
|
package/dist/sqlite.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The outbox and inbox on SQLite, in the shape of a Durable Object's `ctx.storage`: `sql.exec`
|
|
3
|
+
* is synchronous and `transactionSync` makes a group of writes atomic.
|
|
4
|
+
*
|
|
5
|
+
* - `enqueue` writes an event in the caller's transaction, beside the change it records.
|
|
6
|
+
* - `drain` sends due events. Delivery is at least once: a crash after sending and before the
|
|
7
|
+
* row is cleared sends it again, which is why consumers use `handleOnce`.
|
|
8
|
+
* - `handleOnce` runs a handler once per CloudEvents (source, id), in one transaction with the
|
|
9
|
+
* record that it ran, so a replay changes nothing and a failed handler can be retried.
|
|
10
|
+
*/
|
|
11
|
+
import { type Backoff, type Delivery, type OutboxEvent, type PendingEvent } from "./types.ts";
|
|
12
|
+
export interface SqliteLike {
|
|
13
|
+
exec(query: string, ...bindings: unknown[]): {
|
|
14
|
+
toArray(): Record<string, unknown>[];
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export interface SqliteStorage {
|
|
18
|
+
sql: SqliteLike;
|
|
19
|
+
transactionSync<T>(fn: () => T): T;
|
|
20
|
+
}
|
|
21
|
+
export declare const SQLITE_SCHEMA: readonly ["CREATE TABLE IF NOT EXISTS treaty_outbox (\n seq INTEGER PRIMARY KEY AUTOINCREMENT,\n source TEXT NOT NULL,\n id TEXT NOT NULL,\n subject TEXT NOT NULL,\n attributes TEXT NOT NULL,\n data BLOB,\n attempts INTEGER NOT NULL DEFAULT 0,\n next_attempt_at INTEGER NOT NULL,\n last_error TEXT,\n dead INTEGER NOT NULL DEFAULT 0,\n UNIQUE (source, id)\n )", "CREATE INDEX IF NOT EXISTS treaty_outbox_due ON treaty_outbox (dead, next_attempt_at)", "CREATE TABLE IF NOT EXISTS treaty_inbox (\n source TEXT NOT NULL,\n id TEXT NOT NULL,\n received_at INTEGER NOT NULL,\n PRIMARY KEY (source, id)\n )"];
|
|
22
|
+
export declare function migrate(sql: SqliteLike): void;
|
|
23
|
+
/** Call inside the transaction that makes the change the event records. Enqueuing the same event again does nothing. */
|
|
24
|
+
export declare function enqueue(sql: SqliteLike, event: OutboxEvent, subject: string, nowMs?: number): void;
|
|
25
|
+
export interface DrainOptions {
|
|
26
|
+
nowMs?: number;
|
|
27
|
+
limit?: number;
|
|
28
|
+
/** How long a claimed event is hidden from another drain while it is being sent. */
|
|
29
|
+
leaseMs?: number;
|
|
30
|
+
backoff?: Backoff;
|
|
31
|
+
}
|
|
32
|
+
/** Sends due events through `send`; a throw from `send` schedules a retry. Returns how many were sent and failed. */
|
|
33
|
+
export declare function drain(sql: SqliteLike, send: (pending: PendingEvent) => Promise<void>, options?: DrainOptions): Promise<{
|
|
34
|
+
sent: number;
|
|
35
|
+
failed: number;
|
|
36
|
+
}>;
|
|
37
|
+
/** Runs `handler` once per (source, id). A throw from `handler` rolls back, so the event can be handled on a later delivery. */
|
|
38
|
+
export declare function handleOnce(storage: SqliteStorage, event: OutboxEvent, handler: () => void, nowMs?: number): Delivery;
|
package/dist/sqlite.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The outbox and inbox on SQLite, in the shape of a Durable Object's `ctx.storage`: `sql.exec`
|
|
3
|
+
* is synchronous and `transactionSync` makes a group of writes atomic.
|
|
4
|
+
*
|
|
5
|
+
* - `enqueue` writes an event in the caller's transaction, beside the change it records.
|
|
6
|
+
* - `drain` sends due events. Delivery is at least once: a crash after sending and before the
|
|
7
|
+
* row is cleared sends it again, which is why consumers use `handleOnce`.
|
|
8
|
+
* - `handleOnce` runs a handler once per CloudEvents (source, id), in one transaction with the
|
|
9
|
+
* record that it ran, so a replay changes nothing and a failed handler can be retried.
|
|
10
|
+
*/
|
|
11
|
+
import { DEFAULT_BACKOFF, decodeEvent, encodeEvent, } from "./types.js";
|
|
12
|
+
export const SQLITE_SCHEMA = [
|
|
13
|
+
`CREATE TABLE IF NOT EXISTS treaty_outbox (
|
|
14
|
+
seq INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
15
|
+
source TEXT NOT NULL,
|
|
16
|
+
id TEXT NOT NULL,
|
|
17
|
+
subject TEXT NOT NULL,
|
|
18
|
+
attributes TEXT NOT NULL,
|
|
19
|
+
data BLOB,
|
|
20
|
+
attempts INTEGER NOT NULL DEFAULT 0,
|
|
21
|
+
next_attempt_at INTEGER NOT NULL,
|
|
22
|
+
last_error TEXT,
|
|
23
|
+
dead INTEGER NOT NULL DEFAULT 0,
|
|
24
|
+
UNIQUE (source, id)
|
|
25
|
+
)`,
|
|
26
|
+
`CREATE INDEX IF NOT EXISTS treaty_outbox_due ON treaty_outbox (dead, next_attempt_at)`,
|
|
27
|
+
`CREATE TABLE IF NOT EXISTS treaty_inbox (
|
|
28
|
+
source TEXT NOT NULL,
|
|
29
|
+
id TEXT NOT NULL,
|
|
30
|
+
received_at INTEGER NOT NULL,
|
|
31
|
+
PRIMARY KEY (source, id)
|
|
32
|
+
)`,
|
|
33
|
+
];
|
|
34
|
+
export function migrate(sql) {
|
|
35
|
+
for (const statement of SQLITE_SCHEMA)
|
|
36
|
+
sql.exec(statement);
|
|
37
|
+
}
|
|
38
|
+
/** Call inside the transaction that makes the change the event records. Enqueuing the same event again does nothing. */
|
|
39
|
+
export function enqueue(sql, event, subject, nowMs = Date.now()) {
|
|
40
|
+
const { attributes, data } = encodeEvent(event);
|
|
41
|
+
sql.exec(`INSERT INTO treaty_outbox (source, id, subject, attributes, data, next_attempt_at) VALUES (?, ?, ?, ?, ?, ?)
|
|
42
|
+
ON CONFLICT (source, id) DO NOTHING`, event.attributes["source"], event.attributes["id"], subject, attributes, data, nowMs);
|
|
43
|
+
}
|
|
44
|
+
/** Sends due events through `send`; a throw from `send` schedules a retry. Returns how many were sent and failed. */
|
|
45
|
+
export async function drain(sql, send, options = {}) {
|
|
46
|
+
const now = options.nowMs ?? Date.now();
|
|
47
|
+
const backoff = options.backoff ?? DEFAULT_BACKOFF;
|
|
48
|
+
const claimed = sql
|
|
49
|
+
.exec(`UPDATE treaty_outbox SET next_attempt_at = ? WHERE seq IN (
|
|
50
|
+
SELECT seq FROM treaty_outbox WHERE dead = 0 AND next_attempt_at <= ? ORDER BY seq LIMIT ?
|
|
51
|
+
) RETURNING seq, subject, attributes, data, attempts`, now + (options.leaseMs ?? 60_000), now, options.limit ?? 100)
|
|
52
|
+
.toArray()
|
|
53
|
+
.sort((a, b) => Number(a["seq"]) - Number(b["seq"]));
|
|
54
|
+
let sent = 0;
|
|
55
|
+
let failed = 0;
|
|
56
|
+
for (const row of claimed) {
|
|
57
|
+
const attempts = Number(row["attempts"]);
|
|
58
|
+
try {
|
|
59
|
+
await send({
|
|
60
|
+
event: decodeEvent(String(row["attributes"]), row["data"] ?? null),
|
|
61
|
+
subject: String(row["subject"]),
|
|
62
|
+
attempts,
|
|
63
|
+
});
|
|
64
|
+
sql.exec(`DELETE FROM treaty_outbox WHERE seq = ?`, row["seq"]);
|
|
65
|
+
sent++;
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
const next = attempts + 1;
|
|
69
|
+
sql.exec(`UPDATE treaty_outbox SET attempts = ?, next_attempt_at = ?, last_error = ?, dead = ? WHERE seq = ?`, next, now + backoff.delayMs(next), String(error), next >= backoff.maxAttempts ? 1 : 0, row["seq"]);
|
|
70
|
+
failed++;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return { sent, failed };
|
|
74
|
+
}
|
|
75
|
+
/** Runs `handler` once per (source, id). A throw from `handler` rolls back, so the event can be handled on a later delivery. */
|
|
76
|
+
export function handleOnce(storage, event, handler, nowMs = Date.now()) {
|
|
77
|
+
const { source, id } = event.attributes;
|
|
78
|
+
if (!source || !id)
|
|
79
|
+
throw new Error("an event needs its id and source attributes");
|
|
80
|
+
return storage.transactionSync(() => {
|
|
81
|
+
const fresh = storage.sql
|
|
82
|
+
.exec(`INSERT INTO treaty_inbox (source, id, received_at) VALUES (?, ?, ?) ON CONFLICT DO NOTHING RETURNING id`, source, id, nowMs)
|
|
83
|
+
.toArray();
|
|
84
|
+
if (fresh.length === 0)
|
|
85
|
+
return "duplicate";
|
|
86
|
+
handler();
|
|
87
|
+
return "handled";
|
|
88
|
+
});
|
|
89
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An event as the outbox stores it. Structurally the same as `CloudEvent` from
|
|
3
|
+
* `@superjackfruitlabs/treaty-cloudevents` (attributes as canonical strings, data as bytes), so
|
|
4
|
+
* either can be passed where the other is expected, without this package depending on that one.
|
|
5
|
+
*/
|
|
6
|
+
export interface OutboxEvent {
|
|
7
|
+
attributes: Record<string, string>;
|
|
8
|
+
data?: Uint8Array;
|
|
9
|
+
}
|
|
10
|
+
/** A row the drain hands to `send`. */
|
|
11
|
+
export interface PendingEvent {
|
|
12
|
+
event: OutboxEvent;
|
|
13
|
+
/** The NATS subject (or other address) it goes to. */
|
|
14
|
+
subject: string;
|
|
15
|
+
attempts: number;
|
|
16
|
+
}
|
|
17
|
+
/** What `handleOnce` did. */
|
|
18
|
+
export type Delivery = "handled" | "duplicate";
|
|
19
|
+
/** Retry policy for the drain: when to try a failed event again. */
|
|
20
|
+
export interface Backoff {
|
|
21
|
+
/** Milliseconds to wait after the `attempts`th failure. */
|
|
22
|
+
delayMs(attempts: number): number;
|
|
23
|
+
/** After this many attempts an event is parked as dead and not tried again. */
|
|
24
|
+
maxAttempts: number;
|
|
25
|
+
}
|
|
26
|
+
export declare const DEFAULT_BACKOFF: Backoff;
|
|
27
|
+
export declare function encodeEvent(event: OutboxEvent): {
|
|
28
|
+
attributes: string;
|
|
29
|
+
data: Uint8Array | null;
|
|
30
|
+
};
|
|
31
|
+
export declare function decodeEvent(attributes: string, data: Uint8Array | ArrayBuffer | null): OutboxEvent;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const DEFAULT_BACKOFF = {
|
|
2
|
+
delayMs: (attempts) => Math.min(60_000 * 2 ** Math.max(0, attempts - 1), 3_600_000),
|
|
3
|
+
maxAttempts: 12,
|
|
4
|
+
};
|
|
5
|
+
export function encodeEvent(event) {
|
|
6
|
+
if (!event.attributes["id"] || !event.attributes["source"]) {
|
|
7
|
+
throw new Error("an outbox event needs its id and source attributes");
|
|
8
|
+
}
|
|
9
|
+
return {
|
|
10
|
+
attributes: JSON.stringify(event.attributes),
|
|
11
|
+
data: event.data ?? null,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export function decodeEvent(attributes, data) {
|
|
15
|
+
const parsed = JSON.parse(attributes);
|
|
16
|
+
if (data === null || data.byteLength === 0)
|
|
17
|
+
return { attributes: parsed };
|
|
18
|
+
return {
|
|
19
|
+
attributes: parsed,
|
|
20
|
+
data: data instanceof Uint8Array ? data : new Uint8Array(data),
|
|
21
|
+
};
|
|
22
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@superjackfruitlabs/treaty-outbox",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.5",
|
|
4
|
+
"description": "Transactional outbox and deduplicating inbox for CloudEvents, on Durable Object SQLite and on Postgres.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/SuperJackfruitLabs/treaty.git",
|
|
10
|
+
"directory": "packages/outbox"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public",
|
|
27
|
+
"registry": "https://registry.npmjs.org/"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/README.md
DELETED