@fungi.computer/carrier 0.2.7 → 0.2.9
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 +10 -4
- package/dist/cloudflare.js +11 -6
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/internal/owner.d.ts +1 -0
- package/dist/internal/owner.js +36 -3
- package/dist/internal/policy.d.ts +1 -0
- package/dist/internal/policy.js +1 -0
- package/docs/api/index.md +4 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -147,7 +147,9 @@ acceptance. It does not mean the provider accepted the email or delivered it.
|
|
|
147
147
|
**Replay.** Use a stable source ID across handoff retries. A repeated ID with
|
|
148
148
|
identical content returns the existing receipt. Changing its content, envelope,
|
|
149
149
|
headers or expiry produces `CarrierError` with `code: "conflict"`. `get()`
|
|
150
|
-
returns `undefined` when the ID is absent.
|
|
150
|
+
returns `undefined` when the ID is absent. Replay protection lasts while the
|
|
151
|
+
receipt is retained; producers must not retry a terminal ID after its retention
|
|
152
|
+
window.
|
|
151
153
|
|
|
152
154
|
**Dispatch evidence.** Carrier records dispatch intent before it invokes the
|
|
153
155
|
provider. A successful response records `provider_accepted` and its
|
|
@@ -161,7 +163,12 @@ delivery. Work interrupted before dispatch intent recovers safely.
|
|
|
161
163
|
**Statuses.** A receipt's `status` is one of `queued`, `dispatching`,
|
|
162
164
|
`provider_accepted`, `outcome_unknown`, `rejected`, `refused`, `expired` or
|
|
163
165
|
`superseded`. Terminal receipts keep identities and dispatch evidence. Carrier
|
|
164
|
-
removes their stored content.
|
|
166
|
+
removes their stored content. Terminal receipts and recipient Messages expire 30
|
|
167
|
+
days after settlement. Each tick deletes at most 64 expired Emails and their
|
|
168
|
+
Messages; queued and dispatching Emails are never selected. Native alarms retain
|
|
169
|
+
an expiry wake even when no delivery is pending. Other hosts must schedule
|
|
170
|
+
`nextRetentionAt()` as well as dispatch wakes. Existing terminal receipts whose
|
|
171
|
+
settlement time is unknown receive a full 30-day window from migration.
|
|
165
172
|
|
|
166
173
|
**Provider rejections and retries.** The Cloudflare transport decodes the
|
|
167
174
|
binding's documented error `code`. A whole-request refusal before acceptance is
|
|
@@ -484,8 +491,7 @@ No email is sent by these fixtures.
|
|
|
484
491
|
- There is no exactly-once delivery promise. `outcome_unknown` is final, and a
|
|
485
492
|
new ID for the same email can duplicate it.
|
|
486
493
|
- Recipient Messages stay `unconfirmed`. Carrier does not ingest provider
|
|
487
|
-
delivery events or correlate them with receipts
|
|
488
|
-
terminal receipts.
|
|
494
|
+
delivery events or correlate them with receipts.
|
|
489
495
|
- Run exactly one owner per store at a time. Simultaneous owners of the same
|
|
490
496
|
store are unsupported.
|
|
491
497
|
- The test suite does not exercise real mail delivery, provider MIME generation
|
package/dist/cloudflare.js
CHANGED
|
@@ -29,8 +29,16 @@ export async function createCloudflareCarrier(options) {
|
|
|
29
29
|
const carrier = await createCarrier(options.canSend === undefined
|
|
30
30
|
? configuration
|
|
31
31
|
: { ...configuration, canSend: options.canSend });
|
|
32
|
-
|
|
33
|
-
await
|
|
32
|
+
const schedule = async () => {
|
|
33
|
+
if (await carrier.hasPendingWork())
|
|
34
|
+
return arm();
|
|
35
|
+
const retentionAt = await carrier.nextRetentionAt();
|
|
36
|
+
if (retentionAt === null)
|
|
37
|
+
await storage.deleteAlarm();
|
|
38
|
+
else
|
|
39
|
+
await storage.setAlarm(Math.max(Date.now() + 5_000, retentionAt));
|
|
40
|
+
};
|
|
41
|
+
await schedule();
|
|
34
42
|
let active;
|
|
35
43
|
const alarm = () => {
|
|
36
44
|
if (active !== undefined)
|
|
@@ -40,10 +48,7 @@ export async function createCloudflareCarrier(options) {
|
|
|
40
48
|
await arm();
|
|
41
49
|
await carrier.tick();
|
|
42
50
|
await storage.transaction(async () => {
|
|
43
|
-
|
|
44
|
-
await arm();
|
|
45
|
-
else
|
|
46
|
-
await storage.deleteAlarm();
|
|
51
|
+
await schedule();
|
|
47
52
|
});
|
|
48
53
|
})();
|
|
49
54
|
active = operation;
|
package/dist/index.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export type Carrier = Readonly<{
|
|
|
12
12
|
tick: () => Promise<void>;
|
|
13
13
|
/** Report whether the runtime must retain a recovery wake. */
|
|
14
14
|
hasPendingWork: () => Promise<boolean>;
|
|
15
|
+
/** Earliest terminal-receipt expiry; hosts retain a wake even without pending dispatch. */
|
|
16
|
+
nextRetentionAt: () => Promise<number | null>;
|
|
15
17
|
}>;
|
|
16
18
|
/**
|
|
17
19
|
* Construct exactly one owner per store/runtime lifetime. A replacement may be
|
package/dist/index.js
CHANGED
package/dist/internal/owner.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export declare function openOwner(options: Options): {
|
|
|
13
13
|
messages: readonly import("./contracts.js").Message[];
|
|
14
14
|
}>, CarrierError, never>;
|
|
15
15
|
tick: Effect.Effect<void, CarrierError, never>;
|
|
16
|
+
nextRetentionAt: Effect.Effect<number | null, CarrierError, never>;
|
|
16
17
|
get: (id: string) => Effect.Effect<Readonly<{
|
|
17
18
|
id: string;
|
|
18
19
|
status: Schema.Schema.Type<typeof CarrierStatusSchema>;
|
package/dist/internal/owner.js
CHANGED
|
@@ -128,7 +128,7 @@ export function openOwner(options) {
|
|
|
128
128
|
});
|
|
129
129
|
};
|
|
130
130
|
const finish = (id, status, providerId = null) => {
|
|
131
|
-
sql.exec("UPDATE carrier_emails SET status=?,provider_id=?,content=NULL,retry_at=NULL WHERE id=?", status, providerId, id);
|
|
131
|
+
sql.exec("UPDATE carrier_emails SET status=?,provider_id=?,content=NULL,retry_at=NULL,terminal_at=? WHERE id=?", status, providerId, options.now(), id);
|
|
132
132
|
};
|
|
133
133
|
const retryDelay = (attempt) => Math.min(carrierPolicy.retry.baseDelayMs * 2 ** (attempt - 1), carrierPolicy.retry.maxDelayMs);
|
|
134
134
|
const execute = (job) => Effect.gen(function* () {
|
|
@@ -244,6 +244,14 @@ export function openOwner(options) {
|
|
|
244
244
|
yield* sync(() => {
|
|
245
245
|
options.storage.transactionSync(() => {
|
|
246
246
|
sql.exec("CREATE TABLE IF NOT EXISTS carrier_emails (id TEXT PRIMARY KEY,content TEXT,fingerprint TEXT NOT NULL,status TEXT NOT NULL,job_id TEXT NOT NULL UNIQUE,provider_id TEXT,accepted_at INTEGER NOT NULL,attempts INTEGER NOT NULL DEFAULT 0,retry_at INTEGER,error_code TEXT,error_class TEXT) STRICT");
|
|
247
|
+
sql.exec("CREATE INDEX IF NOT EXISTS carrier_emails_open ON carrier_emails(id) WHERE status IN ('queued','dispatching')");
|
|
248
|
+
const columns = sql.exec("PRAGMA table_info(carrier_emails)").toArray();
|
|
249
|
+
if (!columns.some((column) => column.name === "terminal_at")) {
|
|
250
|
+
sql.exec("ALTER TABLE carrier_emails ADD COLUMN terminal_at INTEGER");
|
|
251
|
+
// Historical settlement time is unknown. Preserve a full window from migration.
|
|
252
|
+
sql.exec("UPDATE carrier_emails SET terminal_at=? WHERE status IN ('provider_accepted','outcome_unknown','rejected','refused','expired','superseded')", options.now());
|
|
253
|
+
}
|
|
254
|
+
sql.exec("CREATE INDEX IF NOT EXISTS carrier_emails_terminal ON carrier_emails(terminal_at,id) WHERE status IN ('provider_accepted','outcome_unknown','rejected','refused','expired','superseded')");
|
|
247
255
|
sql.exec("CREATE TABLE IF NOT EXISTS carrier_messages (id TEXT PRIMARY KEY,email_id TEXT NOT NULL REFERENCES carrier_emails(id),position INTEGER NOT NULL,recipient TEXT NOT NULL,recipient_key TEXT NOT NULL,roles TEXT NOT NULL,UNIQUE(email_id,recipient),UNIQUE(email_id,position)) STRICT");
|
|
248
256
|
sql.exec("CREATE INDEX IF NOT EXISTS carrier_messages_recipient_key ON carrier_messages(recipient_key)");
|
|
249
257
|
});
|
|
@@ -326,7 +334,7 @@ export function openOwner(options) {
|
|
|
326
334
|
// A refusal is durable: replaying this ID cannot bypass the cap.
|
|
327
335
|
// Its unused job identity only satisfies the row's key.
|
|
328
336
|
const jobId = ids.work.generate();
|
|
329
|
-
sql.exec("INSERT INTO carrier_emails (id,content,fingerprint,status,job_id,accepted_at) VALUES (
|
|
337
|
+
sql.exec("INSERT INTO carrier_emails (id,content,fingerprint,status,job_id,accepted_at,terminal_at) VALUES (?,?,?,?,?,?,?)", submission.id, capped ? null : content, fingerprint, capped ? "refused" : "queued", jobId, acceptedAt, capped ? acceptedAt : null);
|
|
330
338
|
let position = 0;
|
|
331
339
|
for (const [recipient, roles] of recipients) {
|
|
332
340
|
sql.exec("INSERT INTO carrier_messages (id,email_id,position,recipient,recipient_key,roles) VALUES (?,?,?,?,?,?)", ids.message.generate(), submission.id, position++, recipient, recipientKey(recipient), JSON.stringify(roles));
|
|
@@ -395,11 +403,35 @@ export function openOwner(options) {
|
|
|
395
403
|
const last = rows.at(-1);
|
|
396
404
|
repairCursor = rows.length === 64 && last !== undefined ? last.id : "";
|
|
397
405
|
});
|
|
406
|
+
const prune = sync(() => {
|
|
407
|
+
options.storage.transactionSync(() => {
|
|
408
|
+
const expired = sql
|
|
409
|
+
.exec("SELECT id FROM carrier_emails WHERE status IN ('provider_accepted','outcome_unknown','rejected','refused','expired','superseded') AND terminal_at<? ORDER BY terminal_at,id LIMIT 64", options.now() - carrierPolicy.terminalRetentionMs)
|
|
410
|
+
.toArray()
|
|
411
|
+
.map((row) => Schema.decodeUnknownSync(Schema.Struct({ id: Schema.String }))(row)
|
|
412
|
+
.id);
|
|
413
|
+
if (expired.length === 0)
|
|
414
|
+
return;
|
|
415
|
+
const placeholders = expired.map(() => "?").join(",");
|
|
416
|
+
sql.exec(`DELETE FROM carrier_messages WHERE email_id IN (${placeholders})`, ...expired);
|
|
417
|
+
sql.exec(`DELETE FROM carrier_emails WHERE id IN (${placeholders})`, ...expired);
|
|
418
|
+
});
|
|
419
|
+
});
|
|
420
|
+
const nextRetentionAt = sync(() => {
|
|
421
|
+
const row = sql
|
|
422
|
+
.exec("SELECT terminal_at FROM carrier_emails WHERE status IN ('provider_accepted','outcome_unknown','rejected','refused','expired','superseded') AND terminal_at IS NOT NULL ORDER BY terminal_at,id LIMIT 1")
|
|
423
|
+
.toArray()[0];
|
|
424
|
+
return row === undefined
|
|
425
|
+
? null
|
|
426
|
+
: Schema.decodeUnknownSync(Schema.Struct({ terminal_at: Schema.Number }))(row).terminal_at +
|
|
427
|
+
carrierPolicy.terminalRetentionMs +
|
|
428
|
+
1;
|
|
429
|
+
});
|
|
398
430
|
const tick = Effect.suspend(() => {
|
|
399
431
|
if (ticking)
|
|
400
432
|
return Effect.void;
|
|
401
433
|
ticking = true;
|
|
402
|
-
return watchdog.tick().pipe(Effect.mapError(unavailable), Effect.andThen(repair), Effect.asVoid, Effect.ensuring(Effect.sync(() => {
|
|
434
|
+
return watchdog.tick().pipe(Effect.mapError(unavailable), Effect.andThen(repair), Effect.andThen(prune), Effect.asVoid, Effect.ensuring(Effect.sync(() => {
|
|
403
435
|
ticking = false;
|
|
404
436
|
})));
|
|
405
437
|
});
|
|
@@ -407,6 +439,7 @@ export function openOwner(options) {
|
|
|
407
439
|
initialize,
|
|
408
440
|
send,
|
|
409
441
|
tick,
|
|
442
|
+
nextRetentionAt,
|
|
410
443
|
get: (id) => sync(() => receipt(id)),
|
|
411
444
|
hasPendingWork: Effect.gen(function* () {
|
|
412
445
|
const domainPending = yield* sync(() => sql
|
package/dist/internal/policy.js
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
* provider invocations have been rejected; then the Email is `rejected`.
|
|
14
14
|
*/
|
|
15
15
|
export const carrierPolicy = {
|
|
16
|
+
terminalRetentionMs: 30 * 24 * 60 * 60 * 1000,
|
|
16
17
|
recipientCap: { messages: 5, windowMs: 60 * 60 * 1000 },
|
|
17
18
|
retry: { maxAttempts: 6, baseDelayMs: 30_000, maxDelayMs: 15 * 60 * 1000 },
|
|
18
19
|
};
|
package/docs/api/index.md
CHANGED
|
@@ -82,6 +82,7 @@ type Carrier = Readonly<{
|
|
|
82
82
|
get: (id) => Promise<Receipt | undefined>;
|
|
83
83
|
tick: () => Promise<void>;
|
|
84
84
|
hasPendingWork: () => Promise<boolean>;
|
|
85
|
+
nextRetentionAt: () => Promise<number | null>;
|
|
85
86
|
}>;
|
|
86
87
|
```
|
|
87
88
|
|
|
@@ -312,6 +313,7 @@ function createCarrier(options): Promise<Readonly<{
|
|
|
312
313
|
| undefined>;
|
|
313
314
|
tick: () => Promise<void>;
|
|
314
315
|
hasPendingWork: () => Promise<boolean>;
|
|
316
|
+
nextRetentionAt: () => Promise<number | null>;
|
|
315
317
|
}>>;
|
|
316
318
|
```
|
|
317
319
|
|
|
@@ -340,4 +342,5 @@ store are unsupported. Native adapters supply their runtime ownership.
|
|
|
340
342
|
`providerRejection?`: `Readonly`\<\{ `code`: ...; `class`: ...; \}\>;
|
|
341
343
|
`messages`: readonly `Readonly`\<\{ `id`: ...; `recipient`: ...; `roles`: ...;
|
|
342
344
|
`status`: ...; \}\>[]; \}\> \| `undefined`\>; `tick`: () => `Promise`\<`void`\>;
|
|
343
|
-
`hasPendingWork`: () => `Promise`\<`boolean`\>;
|
|
345
|
+
`hasPendingWork`: () => `Promise`\<`boolean`\>; `nextRetentionAt`: () =>
|
|
346
|
+
`Promise`\<`number` \| `null`\>; \}\>\>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fungi.computer/carrier",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"author": "Levi <levi@technomancy.dev>",
|
|
5
5
|
"description": "Durable email submission and recipient delivery tracking",
|
|
6
6
|
"type": "module",
|
|
@@ -45,14 +45,14 @@
|
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@cloudflare/workers-types": "5.20260828.1",
|
|
47
47
|
"@fungi.computer/almanac": "0.2.1",
|
|
48
|
-
"@fungi.computer/cairn": "0.1.
|
|
49
|
-
"@fungi.computer/watchdog": "0.1.
|
|
48
|
+
"@fungi.computer/cairn": "0.1.1",
|
|
49
|
+
"@fungi.computer/watchdog": "0.1.7",
|
|
50
50
|
"effect": "4.0.2",
|
|
51
51
|
"pg": "8.23.0",
|
|
52
52
|
"@types/pg": "8.23.1"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@cloudflare/vitest-plugin": "1.1
|
|
55
|
+
"@cloudflare/vitest-plugin": "1.6.1",
|
|
56
56
|
"@types/node": "24.13.3",
|
|
57
57
|
"typescript": "5.9.3",
|
|
58
58
|
"vitest": "4.1.11"
|