@ttt-productions/notification-core 0.15.17 → 0.15.18
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 +9 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -3
- package/dist/index.js.map +1 -1
- package/dist/react/components/NotificationEmptyState.d.ts +0 -3
- package/dist/react/components/NotificationEmptyState.d.ts.map +1 -1
- package/dist/react/components/NotificationEmptyState.js +0 -3
- package/dist/react/components/NotificationEmptyState.js.map +1 -1
- package/dist/react/components/NotificationHistoryList.d.ts +0 -6
- package/dist/react/components/NotificationHistoryList.d.ts.map +1 -1
- package/dist/react/components/NotificationHistoryList.js +0 -8
- package/dist/react/components/NotificationHistoryList.js.map +1 -1
- package/dist/react/components/NotificationList.d.ts +0 -3
- package/dist/react/components/NotificationList.d.ts.map +1 -1
- package/dist/react/components/NotificationList.js +2 -29
- package/dist/react/components/NotificationList.js.map +1 -1
- package/dist/react/components/NotificationUnreadBadge.d.ts +0 -5
- package/dist/react/components/NotificationUnreadBadge.d.ts.map +1 -1
- package/dist/react/components/NotificationUnreadBadge.js +0 -5
- package/dist/react/components/NotificationUnreadBadge.js.map +1 -1
- package/dist/react/components/relative-time.d.ts +0 -3
- package/dist/react/components/relative-time.d.ts.map +1 -1
- package/dist/react/components/relative-time.js +0 -3
- package/dist/react/components/relative-time.js.map +1 -1
- package/dist/react/hooks/useActiveNotifications.d.ts +0 -4
- package/dist/react/hooks/useActiveNotifications.d.ts.map +1 -1
- package/dist/react/hooks/useActiveNotifications.js +2 -6
- package/dist/react/hooks/useActiveNotifications.js.map +1 -1
- package/dist/react/hooks/useArchiveAllNotifications.d.ts +0 -30
- package/dist/react/hooks/useArchiveAllNotifications.d.ts.map +1 -1
- package/dist/react/hooks/useArchiveAllNotifications.js +1 -43
- package/dist/react/hooks/useArchiveAllNotifications.js.map +1 -1
- package/dist/react/hooks/useArchiveNotification.d.ts +0 -8
- package/dist/react/hooks/useArchiveNotification.d.ts.map +1 -1
- package/dist/react/hooks/useArchiveNotification.js +0 -8
- package/dist/react/hooks/useArchiveNotification.js.map +1 -1
- package/dist/react/hooks/useNotificationHistory.d.ts +0 -14
- package/dist/react/hooks/useNotificationHistory.d.ts.map +1 -1
- package/dist/react/hooks/useNotificationHistory.js +2 -18
- package/dist/react/hooks/useNotificationHistory.js.map +1 -1
- package/dist/react/hooks/useUnreadCount.d.ts +0 -22
- package/dist/react/hooks/useUnreadCount.d.ts.map +1 -1
- package/dist/react/hooks/useUnreadCount.js +1 -22
- package/dist/react/hooks/useUnreadCount.js.map +1 -1
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +0 -2
- package/dist/react/index.js.map +1 -1
- package/dist/react/types.d.ts +0 -42
- package/dist/react/types.d.ts.map +1 -1
- package/dist/react/types.js.map +1 -1
- package/dist/schemas.d.ts +0 -22
- package/dist/schemas.d.ts.map +1 -1
- package/dist/schemas.js +0 -25
- package/dist/schemas.js.map +1 -1
- package/dist/server/activeNotificationId.d.ts +1 -22
- package/dist/server/activeNotificationId.d.ts.map +1 -1
- package/dist/server/activeNotificationId.js +0 -16
- package/dist/server/activeNotificationId.js.map +1 -1
- package/dist/server/delivery-ledger.d.ts +1 -73
- package/dist/server/delivery-ledger.d.ts.map +1 -1
- package/dist/server/delivery-ledger.js +7 -59
- package/dist/server/delivery-ledger.js.map +1 -1
- package/dist/server/errors.d.ts +0 -9
- package/dist/server/errors.d.ts.map +1 -1
- package/dist/server/errors.js +1 -11
- package/dist/server/errors.js.map +1 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +0 -3
- package/dist/server/index.js.map +1 -1
- package/dist/server/observed-generation.d.ts +1 -41
- package/dist/server/observed-generation.d.ts.map +1 -1
- package/dist/server/observed-generation.js +1 -31
- package/dist/server/observed-generation.js.map +1 -1
- package/dist/server/processBatchHelper.d.ts +0 -19
- package/dist/server/processBatchHelper.d.ts.map +1 -1
- package/dist/server/processBatchHelper.js +2 -46
- package/dist/server/processBatchHelper.js.map +1 -1
- package/dist/server/types.d.ts +0 -9
- package/dist/server/types.d.ts.map +1 -1
- package/dist/server/types.js +0 -4
- package/dist/server/types.js.map +1 -1
- package/dist/types.d.ts +2 -196
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +0 -6
- package/dist/types.js.map +1 -1
- package/package.json +5 -5
- package/src/styles/notifications.css +0 -14
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"activeNotificationId.js","sourceRoot":"","sources":["../../src/server/activeNotificationId.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"activeNotificationId.js","sourceRoot":"","sources":["../../src/server/activeNotificationId.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,MAAM,UAAU,4BAA4B,CAAC,MAO5C;IACC,MAAM,KAAK,GACT,MAAM,CAAC,YAAY,KAAK,UAAU,CAAC,CAAC,CAAC,QAAQ,MAAM,CAAC,YAAY,IAAI,MAAM,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC;IAC1F,MAAM,KAAK,GACT,MAAM,CAAC,gBAAgB,KAAK,SAAS;QACnC,CAAC,CAAC,gBAAgB,MAAM,CAAC,gBAAgB,IAAI,KAAK,IAAI,MAAM,CAAC,QAAQ,EAAE;QACvE,CAAC,CAAC,GAAG,MAAM,CAAC,QAAQ,IAAI,KAAK,IAAI,MAAM,CAAC,QAAQ,EAAE,CAAC;IACvD,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AACvE,CAAC","sourcesContent":["import { createHash } from 'node:crypto';\n\nexport function buildActiveNotificationDocId(params: {\n category: string;\n audienceType: 'personal' | 'shared';\n targetUserId: string | null | undefined;\n dedupKey: string;\n /** Omit for processBatchHelper's id, which must stay byte-identical: existing active docs live at it. */\n notificationType?: string;\n}): string {\n const scope =\n params.audienceType === 'personal' ? `user:${params.targetUserId ?? 'null'}` : 'shared';\n const input =\n params.notificationType !== undefined\n ? `notif-active ${params.notificationType} ${scope} ${params.dedupKey}`\n : `${params.category} ${scope} ${params.dedupKey}`;\n return createHash('sha256').update(input).digest('hex').slice(0, 40);\n}\n"]}
|
|
@@ -1,68 +1,22 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generic notification delivery ledger (notification redesign — P1).
|
|
3
|
-
*
|
|
4
|
-
* One Firestore doc per (recipient|shared, type, occurrence) is BOTH the queue
|
|
5
|
-
* row AND the idempotency ledger. This package owns the GENERIC mechanism;
|
|
6
|
-
* the consuming app owns the concrete collection name, the deterministic
|
|
7
|
-
* `deliveryId`/`eventId`/`aggregationKey` construction, and the worker that
|
|
8
|
-
* selects rows per `materializationClass`. The app passes fully-formed
|
|
9
|
-
* `DeliveryRowInput`s; this module never invents domain ids.
|
|
10
|
-
*
|
|
11
|
-
* Key invariants (FROZEN — this module is their single source; the consuming app's
|
|
12
|
-
* notification design doc explains the product rationale):
|
|
13
|
-
* - Enqueue = create-if-absent. `ALREADY_EXISTS` is a per-row duplicate no-op,
|
|
14
|
-
* never a failure — there is no marker-without-payload state, so the
|
|
15
|
-
* "marker created, payload lost" crash window cannot exist.
|
|
16
|
-
* - Materialize = ONE transaction that reads the delivery row + active card,
|
|
17
|
-
* applies the aggregation exactly once (per strategy + caps), rotates the
|
|
18
|
-
* opaque `activityGeneration`, resets `seenAt`, and flips the row to
|
|
19
|
-
* `materialized` — so the "applied but not marked" double-apply window is gone.
|
|
20
|
-
* Retrying a `materialized` row is a successful no-op.
|
|
21
|
-
* - The same transaction asks the app whether the recipient may still receive
|
|
22
|
-
* (`options.isRecipientEligible`); an ineligible recipient gets no card and the
|
|
23
|
-
* row turns terminal `skipped`, so a row queued before (or retried after) the
|
|
24
|
-
* recipient became ineligible can never recreate a card. Re-running it is a no-op.
|
|
25
|
-
* - TTL (`expireAt`, a real Firestore Timestamp via `config.timestampFromMillis`)
|
|
26
|
-
* is set ONLY at `materialized` / `skipped`; a `queued` or `deadLetter` row is NEVER TTL'd
|
|
27
|
-
* so an unresolved delivery can't be silently deleted before replay.
|
|
28
|
-
*/
|
|
29
1
|
import type { NotificationSystemConfig, NotificationDoc } from '../types.js';
|
|
30
2
|
import type { ServerFirestore, ServerTransaction } from './types.js';
|
|
31
|
-
/**
|
|
32
|
-
* `skipped` is terminal: the recipient was not eligible when the row was materialized, so no card
|
|
33
|
-
* was written. It is TTL'd like `materialized` and never retried.
|
|
34
|
-
*/
|
|
35
3
|
export type DeliveryState = 'queued' | 'materialized' | 'skipped' | 'deadLetter';
|
|
36
|
-
/** Why a row was terminalized as `skipped`. */
|
|
37
4
|
export type DeliverySkipReason = 'recipientIneligible';
|
|
38
|
-
/**
|
|
39
|
-
* Ledger options the consuming app must decide.
|
|
40
|
-
*/
|
|
41
5
|
export interface DeliveryLedgerOptions {
|
|
42
|
-
/**
|
|
43
|
-
* Whether `recipientUid` may still receive a notification. Called INSIDE the materialize
|
|
44
|
-
* transaction, after the delivery row is read and before anything is written, so the answer
|
|
45
|
-
* and the card write commit together (BACKEND-116): read through `tx` only. A recipient that
|
|
46
|
-
* answers false gets no card, and the row becomes `skipped`. Not called for a shared
|
|
47
|
-
* (recipient-less) row. A consumer with no eligibility rule answers true.
|
|
48
|
-
*/
|
|
6
|
+
/** Runs inside the materialize transaction before any write: read through `tx` only (RULE-STD-BACKEND-116). */
|
|
49
7
|
isRecipientEligible: (recipientUid: string, tx: ServerTransaction) => Promise<boolean>;
|
|
50
8
|
}
|
|
51
9
|
export type AggregationStrategy = 'increment' | 'staticRelight';
|
|
52
10
|
export type MaterializationClass = 'directQueued' | 'realtimeFallback' | 'fanoutOrphan' | 'retry';
|
|
53
|
-
/** The materialization payload carried on every delivery row. */
|
|
54
11
|
export interface DeliveryPayload {
|
|
55
|
-
/** The acting user's uid, or `null`/`''` for a system / no-actor occurrence (N-M2). */
|
|
56
12
|
actorId: string | null;
|
|
57
13
|
metadata: Record<string, unknown>;
|
|
58
14
|
occurrenceAt: number;
|
|
59
15
|
}
|
|
60
|
-
/** App-supplied, fully-formed delivery row (the app owns all id construction). */
|
|
61
16
|
export interface DeliveryRowInput {
|
|
62
17
|
deliveryId: string;
|
|
63
18
|
notificationType: string;
|
|
64
19
|
eventId: string;
|
|
65
|
-
/** `null` for shared-admin occurrences (the deliveryId hash uses the literal `'shared'`). */
|
|
66
20
|
recipientUid: string | null;
|
|
67
21
|
aggregationKey: string;
|
|
68
22
|
strategy: AggregationStrategy;
|
|
@@ -83,53 +37,27 @@ export type EnqueueRowResult = {
|
|
|
83
37
|
};
|
|
84
38
|
export interface EnqueueResult {
|
|
85
39
|
results: EnqueueRowResult[];
|
|
86
|
-
/** True iff EVERY row is now represented by a created-or-pre-existing doc (page-cursor invariant input). */
|
|
87
40
|
allResolved: boolean;
|
|
88
41
|
}
|
|
89
42
|
export type MaterializeOutcome = 'materialized' | 'already-materialized' | 'skipped-ineligible-recipient' | 'missing' | 'skipped-non-queued';
|
|
90
43
|
export interface MaterializeManyOptions {
|
|
91
|
-
/** Rows materialized at once (default 10). */
|
|
92
44
|
concurrency?: number;
|
|
93
|
-
/**
|
|
94
|
-
* Called for a row whose materialize threw and whose failure could not then be recorded either —
|
|
95
|
-
* `error` is the recording's own failure and `details.cause` the materialize failure it was
|
|
96
|
-
* recording. The row stays as it was (queued, attempt count
|
|
97
|
-
* unchanged), so a later run selects it again; the rest of the batch proceeds. The app's capture
|
|
98
|
-
* goes here; without it the ledger writes the failure to `console.error`.
|
|
99
|
-
*/
|
|
100
45
|
onUnrecordedFailure?: (deliveryId: string, error: unknown, details: {
|
|
101
46
|
cause: unknown;
|
|
102
47
|
}) => void;
|
|
103
48
|
}
|
|
104
|
-
/** What a `replay` did: re-queued the row, found no row, or found a row that is not dead-lettered. */
|
|
105
49
|
export type ReplayOutcome = 'replayed' | 'missing' | 'not-dead-lettered';
|
|
106
50
|
export interface ReplayOptions {
|
|
107
|
-
/**
|
|
108
|
-
* The caller's own write (its audit event), composed into the replay transaction and awaited
|
|
109
|
-
* there on the `replayed` path only. A hook that throws or rejects fails the replay, so the
|
|
110
|
-
* reset never commits without it.
|
|
111
|
-
*/
|
|
112
51
|
auditWrite?: (txn: ServerTransaction) => void | Promise<void>;
|
|
113
52
|
}
|
|
114
53
|
export interface DeliveryLedger {
|
|
115
54
|
enqueue(rows: DeliveryRowInput[]): Promise<EnqueueResult>;
|
|
116
55
|
materialize(deliveryId: string): Promise<MaterializeOutcome>;
|
|
117
|
-
/**
|
|
118
|
-
* Materialize many rows with bounded concurrency. A row whose materialize throws has the failure
|
|
119
|
-
* recorded (a retry or a dead letter); each row is isolated, so one row's failure — even one
|
|
120
|
-
* whose recording also fails — never fails the batch or stops the rows behind it.
|
|
121
|
-
*/
|
|
122
56
|
materializeMany(deliveryIds: string[], options?: MaterializeManyOptions): Promise<Record<MaterializeOutcome, number>>;
|
|
123
57
|
recordTransientFailure(deliveryId: string, error: unknown): Promise<void>;
|
|
124
58
|
deadLetter(deliveryId: string, error: unknown): Promise<void>;
|
|
125
|
-
/** Re-queue a dead-lettered row. A missing or non-dead-lettered row is answered, never written. */
|
|
126
59
|
replay(deliveryId: string, options?: ReplayOptions): Promise<ReplayOutcome>;
|
|
127
60
|
}
|
|
128
|
-
/**
|
|
129
|
-
* Apply the aggregation strategy to produce the active-doc field delta.
|
|
130
|
-
* PURE — exported for direct unit testing. `existing` is the current active doc
|
|
131
|
-
* (or null to create). Always rotates `activityGeneration` and resets `seenAt`.
|
|
132
|
-
*/
|
|
133
61
|
export declare function applyAggregation(params: {
|
|
134
62
|
strategy: AggregationStrategy;
|
|
135
63
|
existing: Pick<NotificationDoc, 'count' | 'latestActorIds'> | null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"delivery-ledger.d.ts","sourceRoot":"","sources":["../../src/server/delivery-ledger.ts"],"names":[],"mappings":"
|
|
1
|
+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E,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC9D,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;CAC7E;AAYD,wBAAgB,gBAAgB,CAAC,MAAM,EAAE;IACvC,QAAQ,EAAE,mBAAmB,CAAC;IAC9B,QAAQ,EAAE,IAAI,CAAC,eAAe,EAAE,OAAO,GAAG,gBAAgB,CAAC,GAAG,IAAI,CAAC;IACnE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;IACxC,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,EAAE,MAAM,CAAC;CACpB,GAAG;IACF,KAAK,EAAE,MAAM,CAAC;IACd,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CACnB,CAeA;AAED,wBAAgB,oBAAoB,CAClC,EAAE,EAAE,eAAe,EACnB,MAAM,EAAE,wBAAwB,EAChC,OAAO,EAAE,qBAAqB,GAC7B,cAAc,CAkThB"}
|
|
@@ -1,64 +1,26 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generic notification delivery ledger (notification redesign — P1).
|
|
3
|
-
*
|
|
4
|
-
* One Firestore doc per (recipient|shared, type, occurrence) is BOTH the queue
|
|
5
|
-
* row AND the idempotency ledger. This package owns the GENERIC mechanism;
|
|
6
|
-
* the consuming app owns the concrete collection name, the deterministic
|
|
7
|
-
* `deliveryId`/`eventId`/`aggregationKey` construction, and the worker that
|
|
8
|
-
* selects rows per `materializationClass`. The app passes fully-formed
|
|
9
|
-
* `DeliveryRowInput`s; this module never invents domain ids.
|
|
10
|
-
*
|
|
11
|
-
* Key invariants (FROZEN — this module is their single source; the consuming app's
|
|
12
|
-
* notification design doc explains the product rationale):
|
|
13
|
-
* - Enqueue = create-if-absent. `ALREADY_EXISTS` is a per-row duplicate no-op,
|
|
14
|
-
* never a failure — there is no marker-without-payload state, so the
|
|
15
|
-
* "marker created, payload lost" crash window cannot exist.
|
|
16
|
-
* - Materialize = ONE transaction that reads the delivery row + active card,
|
|
17
|
-
* applies the aggregation exactly once (per strategy + caps), rotates the
|
|
18
|
-
* opaque `activityGeneration`, resets `seenAt`, and flips the row to
|
|
19
|
-
* `materialized` — so the "applied but not marked" double-apply window is gone.
|
|
20
|
-
* Retrying a `materialized` row is a successful no-op.
|
|
21
|
-
* - The same transaction asks the app whether the recipient may still receive
|
|
22
|
-
* (`options.isRecipientEligible`); an ineligible recipient gets no card and the
|
|
23
|
-
* row turns terminal `skipped`, so a row queued before (or retried after) the
|
|
24
|
-
* recipient became ineligible can never recreate a card. Re-running it is a no-op.
|
|
25
|
-
* - TTL (`expireAt`, a real Firestore Timestamp via `config.timestampFromMillis`)
|
|
26
|
-
* is set ONLY at `materialized` / `skipped`; a `queued` or `deadLetter` row is NEVER TTL'd
|
|
27
|
-
* so an unresolved delivery can't be silently deleted before replay.
|
|
28
|
-
*/
|
|
29
1
|
import { randomUUID } from 'node:crypto';
|
|
30
2
|
import { buildActiveNotificationDocId } from './activeNotificationId.js';
|
|
31
3
|
const DEFAULT_DELIVERIES_PATH = 'notificationDeliveries';
|
|
32
|
-
const DEFAULT_DELIVERY_TTL_MS = 90 * 24 * 60 * 60 * 1000;
|
|
4
|
+
const DEFAULT_DELIVERY_TTL_MS = 90 * 24 * 60 * 60 * 1000;
|
|
33
5
|
const DEFAULT_MAX_ATTEMPTS = 8;
|
|
34
6
|
const DEFAULT_COUNT_CAP = 5000;
|
|
35
7
|
const DEFAULT_ACTOR_CAP = 5;
|
|
36
|
-
const BACKOFF_BASE_MS = 60 * 1000;
|
|
37
|
-
const BACKOFF_MAX_MS = 60 * 60 * 1000;
|
|
38
|
-
/** gRPC ALREADY_EXISTS (6) — the create-if-absent duplicate signal. */
|
|
8
|
+
const BACKOFF_BASE_MS = 60 * 1000;
|
|
9
|
+
const BACKOFF_MAX_MS = 60 * 60 * 1000;
|
|
39
10
|
function isAlreadyExists(error) {
|
|
40
11
|
const e = error;
|
|
41
12
|
return e?.code === 6 || /already.?exists/i.test(e?.message ?? '');
|
|
42
13
|
}
|
|
43
14
|
function backoffMs(attemptCount) {
|
|
44
15
|
const exp = Math.min(BACKOFF_BASE_MS * 2 ** Math.max(0, attemptCount - 1), BACKOFF_MAX_MS);
|
|
45
|
-
// Full jitter — spread retries so a burst of failures doesn't synchronize.
|
|
46
16
|
return Math.floor(exp / 2 + Math.random() * (exp / 2));
|
|
47
17
|
}
|
|
48
|
-
/**
|
|
49
|
-
* Apply the aggregation strategy to produce the active-doc field delta.
|
|
50
|
-
* PURE — exported for direct unit testing. `existing` is the current active doc
|
|
51
|
-
* (or null to create). Always rotates `activityGeneration` and resets `seenAt`.
|
|
52
|
-
*/
|
|
53
18
|
export function applyAggregation(params) {
|
|
54
19
|
const { strategy, existing, actorId, countCap, actorCap, buildMessage, now, generation } = params;
|
|
55
20
|
const prevActors = existing?.latestActorIds ?? [];
|
|
56
|
-
// N-M2: a system / no-actor occurrence (null or '') is never injected into the actor
|
|
57
|
-
// list — it would render as a blank avatar / unresolvable uid in `latestActorIds`.
|
|
58
21
|
const latestActorIds = actorId
|
|
59
22
|
? [actorId, ...prevActors.filter((id) => id !== actorId)].slice(0, actorCap)
|
|
60
23
|
: prevActors.slice(0, actorCap);
|
|
61
|
-
// staticRelight never shows a count (stays 1); increment counts up to the cap.
|
|
62
24
|
const count = strategy === 'staticRelight' ? 1 : Math.min((existing?.count ?? 0) + 1, countCap);
|
|
63
25
|
return {
|
|
64
26
|
count,
|
|
@@ -78,8 +40,7 @@ export function createDeliveryLedger(db, config, options) {
|
|
|
78
40
|
if (!configTimestampFromMillis) {
|
|
79
41
|
throw new Error('[notification-core] createDeliveryLedger requires config.timestampFromMillis (Firestore TTL needs a real Timestamp for expireAt).');
|
|
80
42
|
}
|
|
81
|
-
//
|
|
82
|
-
// (called later) see it as defined, not `... | undefined`.
|
|
43
|
+
// An explicitly non-optional binding: TS does not carry the narrowing into closures called later.
|
|
83
44
|
const toTimestamp = configTimestampFromMillis;
|
|
84
45
|
const deliveries = db.collection(deliveriesPath);
|
|
85
46
|
function deliveryRef(deliveryId) {
|
|
@@ -103,7 +64,6 @@ export function createDeliveryLedger(db, config, options) {
|
|
|
103
64
|
createdAt: now,
|
|
104
65
|
materializedAt: null,
|
|
105
66
|
deadLetteredAt: null,
|
|
106
|
-
// No expireAt while queued.
|
|
107
67
|
};
|
|
108
68
|
}
|
|
109
69
|
function getTypeConfig(type) {
|
|
@@ -149,8 +109,6 @@ export function createDeliveryLedger(db, config, options) {
|
|
|
149
109
|
const notificationType = d.notificationType;
|
|
150
110
|
const aggregationKey = d.aggregationKey;
|
|
151
111
|
const recipientUid = d.recipientUid ?? null;
|
|
152
|
-
// The recipient is checked in this transaction, so a recipient that became ineligible
|
|
153
|
-
// (e.g. an erased account) never gets a card from a row queued before, or retried after.
|
|
154
112
|
if (recipientUid !== null && !(await isRecipientEligible(recipientUid, tx))) {
|
|
155
113
|
const skippedAt = Date.now();
|
|
156
114
|
tx.update(dRef, {
|
|
@@ -175,19 +133,14 @@ export function createDeliveryLedger(db, config, options) {
|
|
|
175
133
|
dedupKey: aggregationKey,
|
|
176
134
|
notificationType,
|
|
177
135
|
}));
|
|
178
|
-
const activeSnap = await tx.get(activeRef); //
|
|
136
|
+
const activeSnap = await tx.get(activeRef); // Firestore transactions take every read before any write.
|
|
179
137
|
const now = Date.now();
|
|
180
138
|
const generation = randomUUID();
|
|
181
139
|
const countCap = typeConfig.countCap ?? DEFAULT_COUNT_CAP;
|
|
182
140
|
const actorCap = typeConfig.actorCap ?? DEFAULT_ACTOR_CAP;
|
|
183
141
|
const buildMessage = (count) => typeConfig.messagePattern(payload.metadata, count);
|
|
184
|
-
// Latest-occurrence-derived fields — recomputed every materialize so an EXISTING
|
|
185
|
-
// aggregate card reflects the newest activity (title/targetPath/metadata), not the
|
|
186
|
-
// first occurrence's stale values (N-I4).
|
|
187
142
|
const title = typeConfig.titlePattern(payload.metadata);
|
|
188
|
-
//
|
|
189
|
-
// entirely (Firestore rejects `undefined` values). A pre-existing doc written
|
|
190
|
-
// before a type went linkless keeps its stale targetPath until archived.
|
|
143
|
+
// Firestore rejects `undefined` values, so a linkless type omits the field.
|
|
191
144
|
const targetPath = typeConfig.defaultTargetPath === undefined
|
|
192
145
|
? undefined
|
|
193
146
|
: typeof typeConfig.defaultTargetPath === 'function'
|
|
@@ -266,11 +219,6 @@ export function createDeliveryLedger(db, config, options) {
|
|
|
266
219
|
});
|
|
267
220
|
}
|
|
268
221
|
else {
|
|
269
|
-
// n1: re-stamp the row into the reserved `retry` materialization lane on backoff. Without
|
|
270
|
-
// this the row keeps its original class and competes for that lane's capacity while backed
|
|
271
|
-
// off; the worker's reserved `retry` slice (separate CLASS_CAPS capacity, drained in
|
|
272
|
-
// DRAIN_ORDER) would always select 0 rows. Re-stamping moves a backed-off row into the
|
|
273
|
-
// anti-starvation lane so it drains there instead of starving in its original lane.
|
|
274
222
|
tx.update(dRef, {
|
|
275
223
|
attemptCount,
|
|
276
224
|
nextAttemptAt: now + backoffMs(attemptCount),
|
|
@@ -312,7 +260,7 @@ export function createDeliveryLedger(db, config, options) {
|
|
|
312
260
|
nextAttemptAt: Date.now(),
|
|
313
261
|
lastError: null,
|
|
314
262
|
deadLetteredAt: null,
|
|
315
|
-
expireAt: null,
|
|
263
|
+
expireAt: null,
|
|
316
264
|
});
|
|
317
265
|
await options?.auditWrite?.(tx);
|
|
318
266
|
return 'replayed';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"delivery-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AK,CAAC;IACf,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,sBAAsB,EAAE,UAAU,EAAE,MAAM,EAAE,CAAC;AAC/F,CAAC","sourcesContent":["/**\n * Generic notification delivery ledger (notification redesign — P1).\n *\n * One Firestore doc per (recipient|shared, type, occurrence) is BOTH the queue\n * row AND the idempotency ledger. This package owns the GENERIC mechanism;\n * the consuming app owns the concrete collection name, the deterministic\n * `deliveryId`/`eventId`/`aggregationKey` construction, and the worker that\n * selects rows per `materializationClass`. The app passes fully-formed\n * `DeliveryRowInput`s; this module never invents domain ids.\n *\n * Key invariants (FROZEN — this module is their single source; the consuming app's\n * notification design doc explains the product rationale):\n * - Enqueue = create-if-absent. `ALREADY_EXISTS` is a per-row duplicate no-op,\n * never a failure — there is no marker-without-payload state, so the\n * \"marker created, payload lost\" crash window cannot exist.\n * - Materialize = ONE transaction that reads the delivery row + active card,\n * applies the aggregation exactly once (per strategy + caps), rotates the\n * opaque `activityGeneration`, resets `seenAt`, and flips the row to\n * `materialized` — so the \"applied but not marked\" double-apply window is gone.\n * Retrying a `materialized` row is a successful no-op.\n * - The same transaction asks the app whether the recipient may still receive\n * (`options.isRecipientEligible`); an ineligible recipient gets no card and the\n * row turns terminal `skipped`, so a row queued before (or retried after) the\n * recipient became ineligible can never recreate a card. Re-running it is a no-op.\n * - TTL (`expireAt`, a real Firestore Timestamp via `config.timestampFromMillis`)\n * is set ONLY at `materialized` / `skipped`; a `queued` or `deadLetter` row is NEVER TTL'd\n * so an unresolved delivery can't be silently deleted before replay.\n */\n\nimport { randomUUID } from 'node:crypto';\nimport type { NotificationSystemConfig, NotificationDoc } from '../types.js';\nimport type { ServerFirestore, ServerDocRef, ServerTransaction } from './types.js';\nimport { buildActiveNotificationDocId } from './activeNotificationId.js';\n\nconst DEFAULT_DELIVERIES_PATH = 'notificationDeliveries';\nconst DEFAULT_DELIVERY_TTL_MS = 90 * 24 * 60 * 60 * 1000; // 90 days\nconst DEFAULT_MAX_ATTEMPTS = 8;\nconst DEFAULT_COUNT_CAP = 5000;\nconst DEFAULT_ACTOR_CAP = 5;\nconst BACKOFF_BASE_MS = 60 * 1000; // 1 min\nconst BACKOFF_MAX_MS = 60 * 60 * 1000; // 1 hour\n\n/**\n * `skipped` is terminal: the recipient was not eligible when the row was materialized, so no card\n * was written. It is TTL'd like `materialized` and never retried.\n */\nexport type DeliveryState = 'queued' | 'materialized' | 'skipped' | 'deadLetter';\n\n/** Why a row was terminalized as `skipped`. */\nexport type DeliverySkipReason = 'recipientIneligible';\n\n/**\n * Ledger options the consuming app must decide.\n */\nexport interface DeliveryLedgerOptions {\n /**\n * Whether `recipientUid` may still receive a notification. Called INSIDE the materialize\n * transaction, after the delivery row is read and before anything is written, so the answer\n * and the card write commit together (BACKEND-116): read through `tx` only. A recipient that\n * answers false gets no card, and the row becomes `skipped`. Not called for a shared\n * (recipient-less) row. A consumer with no eligibility rule answers true.\n */\n isRecipientEligible: (recipientUid: string, tx: ServerTransaction) => Promise<boolean>;\n}\nexport type AggregationStrategy = 'increment' | 'staticRelight';\nexport type MaterializationClass = 'directQueued' | 'realtimeFallback' | 'fanoutOrphan' | 'retry';\n\n/** The materialization payload carried on every delivery row. */\nexport interface DeliveryPayload {\n /** The acting user's uid, or `null`/`''` for a system / no-actor occurrence (N-M2). */\n actorId: string | null;\n metadata: Record<string, unknown>;\n occurrenceAt: number;\n}\n\n/** App-supplied, fully-formed delivery row (the app owns all id construction). */\nexport interface DeliveryRowInput {\n deliveryId: string;\n notificationType: string;\n eventId: string;\n /** `null` for shared-admin occurrences (the deliveryId hash uses the literal `'shared'`). */\n recipientUid: string | null;\n aggregationKey: string;\n strategy: AggregationStrategy;\n payload: DeliveryPayload;\n payloadVersion: number;\n materializationClass: MaterializationClass;\n}\n\nexport type EnqueueRowResult =\n | { deliveryId: string; outcome: 'created' }\n | { deliveryId: string; outcome: 'duplicate' }\n | { deliveryId: string; outcome: 'failed'; error: unknown };\n\nexport interface EnqueueResult {\n results: EnqueueRowResult[];\n /** True iff EVERY row is now represented by a created-or-pre-existing doc (page-cursor invariant input). */\n allResolved: boolean;\n}\n\nexport type MaterializeOutcome =\n | 'materialized'\n | 'already-materialized'\n | 'skipped-ineligible-recipient'\n | 'missing'\n | 'skipped-non-queued';\n\nexport interface MaterializeManyOptions {\n /** Rows materialized at once (default 10). */\n concurrency?: number;\n /**\n * Called for a row whose materialize threw and whose failure could not then be recorded either —\n * `error` is the recording's own failure and `details.cause` the materialize failure it was\n * recording. The row stays as it was (queued, attempt count\n * unchanged), so a later run selects it again; the rest of the batch proceeds. The app's capture\n * goes here; without it the ledger writes the failure to `console.error`.\n */\n onUnrecordedFailure?: (deliveryId: string, error: unknown, details: { cause: unknown }) => void;\n}\n\n/** What a `replay` did: re-queued the row, found no row, or found a row that is not dead-lettered. */\nexport type ReplayOutcome = 'replayed' | 'missing' | 'not-dead-lettered';\n\nexport interface ReplayOptions {\n /**\n * The caller's own write (its audit event), composed into the replay transaction and awaited\n * there on the `replayed` path only. A hook that throws or rejects fails the replay, so the\n * reset never commits without it.\n */\n auditWrite?: (txn: ServerTransaction) => void | Promise<void>;\n}\n\nexport interface DeliveryLedger {\n enqueue(rows: DeliveryRowInput[]): Promise<EnqueueResult>;\n materialize(deliveryId: string): Promise<MaterializeOutcome>;\n /**\n * Materialize many rows with bounded concurrency. A row whose materialize throws has the failure\n * recorded (a retry or a dead letter); each row is isolated, so one row's failure — even one\n * whose recording also fails — never fails the batch or stops the rows behind it.\n */\n materializeMany(\n deliveryIds: string[],\n options?: MaterializeManyOptions,\n ): Promise<Record<MaterializeOutcome, number>>;\n recordTransientFailure(deliveryId: string, error: unknown): Promise<void>;\n deadLetter(deliveryId: string, error: unknown): Promise<void>;\n /** Re-queue a dead-lettered row. A missing or non-dead-lettered row is answered, never written. */\n replay(deliveryId: string, options?: ReplayOptions): Promise<ReplayOutcome>;\n}\n\n/** gRPC ALREADY_EXISTS (6) — the create-if-absent duplicate signal. */\nfunction isAlreadyExists(error: unknown): boolean {\n const e = error as { code?: number; message?: string } | undefined;\n return e?.code === 6 || /already.?exists/i.test(e?.message ?? '');\n}\n\nfunction backoffMs(attemptCount: number): number {\n const exp = Math.min(BACKOFF_BASE_MS * 2 ** Math.max(0, attemptCount - 1), BACKOFF_MAX_MS);\n // Full jitter — spread retries so a burst of failures doesn't synchronize.\n return Math.floor(exp / 2 + Math.random() * (exp / 2));\n}\n\n/**\n * Apply the aggregation strategy to produce the active-doc field delta.\n * PURE — exported for direct unit testing. `existing` is the current active doc\n * (or null to create). Always rotates `activityGeneration` and resets `seenAt`.\n */\nexport function applyAggregation(params: {\n strategy: AggregationStrategy;\n existing: Pick<NotificationDoc, 'count' | 'latestActorIds'> | null;\n actorId: string | null;\n countCap: number;\n actorCap: number;\n buildMessage: (count: number) => string;\n now: number;\n generation: string;\n}): {\n count: number;\n latestActorIds: string[];\n message: string;\n activityGeneration: string;\n seenAt: number;\n updatedAt: number;\n} {\n const { strategy, existing, actorId, countCap, actorCap, buildMessage, now, generation } = params;\n const prevActors = existing?.latestActorIds ?? [];\n // N-M2: a system / no-actor occurrence (null or '') is never injected into the actor\n // list — it would render as a blank avatar / unresolvable uid in `latestActorIds`.\n const latestActorIds = actorId\n ? [actorId, ...prevActors.filter((id) => id !== actorId)].slice(0, actorCap)\n : prevActors.slice(0, actorCap);\n // staticRelight never shows a count (stays 1); increment counts up to the cap.\n const count = strategy === 'staticRelight' ? 1 : Math.min((existing?.count ?? 0) + 1, countCap);\n return {\n count,\n latestActorIds,\n message: buildMessage(count),\n activityGeneration: generation,\n seenAt: 0,\n updatedAt: now,\n };\n}\n\nexport function createDeliveryLedger(\n db: ServerFirestore,\n config: NotificationSystemConfig,\n options: DeliveryLedgerOptions,\n): DeliveryLedger {\n const { isRecipientEligible } = options;\n const deliveriesPath = config.deliveriesCollectionPath ?? DEFAULT_DELIVERIES_PATH;\n const ttlMs = config.deliveryTtlMs ?? DEFAULT_DELIVERY_TTL_MS;\n const maxAttempts = config.maxDeliveryAttempts ?? DEFAULT_MAX_ATTEMPTS;\n const configTimestampFromMillis = config.timestampFromMillis;\n if (!configTimestampFromMillis) {\n throw new Error(\n '[notification-core] createDeliveryLedger requires config.timestampFromMillis (Firestore TTL needs a real Timestamp for expireAt).',\n );\n }\n // Bind to an explicitly non-optional type so the nested transaction closures\n // (called later) see it as defined, not `... | undefined`.\n const toTimestamp: (ms: number) => unknown = configTimestampFromMillis;\n\n const deliveries = db.collection(deliveriesPath);\n\n function deliveryRef(deliveryId: string): ServerDocRef {\n return deliveries.doc(deliveryId);\n }\n\n function buildDeliveryDoc(row: DeliveryRowInput, now: number): Record<string, unknown> {\n return {\n deliveryId: row.deliveryId,\n state: 'queued' as DeliveryState,\n notificationType: row.notificationType,\n eventId: row.eventId,\n recipientUid: row.recipientUid,\n aggregationKey: row.aggregationKey,\n strategy: row.strategy,\n payload: row.payload,\n payloadVersion: row.payloadVersion,\n materializationClass: row.materializationClass,\n attemptCount: 0,\n nextAttemptAt: now,\n lastError: null,\n createdAt: now,\n materializedAt: null,\n deadLetteredAt: null,\n // No expireAt while queued.\n };\n }\n\n function getTypeConfig(type: string) {\n const typeConfig = config.types[type];\n if (!typeConfig) throw new Error(`[notification-core] Unknown notification type: ${type}`);\n const categoryConfig = config.categories[typeConfig.category];\n if (!categoryConfig)\n throw new Error(`[notification-core] Unknown category: ${typeConfig.category}`);\n return { typeConfig, categoryConfig };\n }\n\n async function enqueue(rows: DeliveryRowInput[]): Promise<EnqueueResult> {\n if (rows.length === 0) return { results: [], allResolved: true };\n const now = Date.now();\n const settled = await Promise.allSettled(\n rows.map((row) =>\n deliveryRef(row.deliveryId)\n .create(buildDeliveryDoc(row, now))\n .then(() => ({ deliveryId: row.deliveryId, outcome: 'created' as const })),\n ),\n );\n const results: EnqueueRowResult[] = settled.map((s, i) => {\n const deliveryId = rows[i].deliveryId;\n if (s.status === 'fulfilled') return s.value;\n if (isAlreadyExists(s.reason)) return { deliveryId, outcome: 'duplicate' };\n return { deliveryId, outcome: 'failed', error: s.reason };\n });\n return { results, allResolved: results.every((r) => r.outcome !== 'failed') };\n }\n\n async function materialize(deliveryId: string): Promise<MaterializeOutcome> {\n return db.runTransaction(async (tx) => {\n const dRef = deliveryRef(deliveryId);\n const dSnap = await tx.get(dRef);\n if (!dSnap.exists) return 'missing';\n const d = (dSnap.data() ?? {}) as Record<string, unknown>;\n const state = d.state as DeliveryState;\n if (state === 'materialized') return 'already-materialized';\n if (state === 'skipped') return 'skipped-ineligible-recipient';\n if (state !== 'queued') return 'skipped-non-queued';\n\n const notificationType = d.notificationType as string;\n const aggregationKey = d.aggregationKey as string;\n const recipientUid = (d.recipientUid as string | null) ?? null;\n\n // The recipient is checked in this transaction, so a recipient that became ineligible\n // (e.g. an erased account) never gets a card from a row queued before, or retried after.\n if (recipientUid !== null && !(await isRecipientEligible(recipientUid, tx))) {\n const skippedAt = Date.now();\n tx.update(dRef, {\n state: 'skipped' as DeliveryState,\n skipReason: 'recipientIneligible' as DeliverySkipReason,\n skippedAt,\n expireAt: toTimestamp(skippedAt + ttlMs),\n });\n return 'skipped-ineligible-recipient';\n }\n const strategy = (d.strategy as AggregationStrategy) ?? 'increment';\n const payload = (d.payload as DeliveryPayload) ?? {\n actorId: '',\n metadata: {},\n occurrenceAt: Date.now(),\n };\n const { typeConfig, categoryConfig } = getTypeConfig(notificationType);\n\n const activeRef = db.collection(categoryConfig.activePath).doc(\n buildActiveNotificationDocId({\n category: typeConfig.category,\n audienceType: categoryConfig.audienceType,\n targetUserId: recipientUid,\n dedupKey: aggregationKey,\n notificationType,\n }),\n );\n const activeSnap = await tx.get(activeRef); // all reads before writes\n\n const now = Date.now();\n const generation = randomUUID();\n const countCap = typeConfig.countCap ?? DEFAULT_COUNT_CAP;\n const actorCap = typeConfig.actorCap ?? DEFAULT_ACTOR_CAP;\n const buildMessage = (count: number) => typeConfig.messagePattern(payload.metadata, count);\n // Latest-occurrence-derived fields — recomputed every materialize so an EXISTING\n // aggregate card reflects the newest activity (title/targetPath/metadata), not the\n // first occurrence's stale values (N-I4).\n const title = typeConfig.titlePattern(payload.metadata);\n // A type with no defaultTargetPath is linkless: the doc must OMIT the field\n // entirely (Firestore rejects `undefined` values). A pre-existing doc written\n // before a type went linkless keeps its stale targetPath until archived.\n const targetPath =\n typeConfig.defaultTargetPath === undefined\n ? undefined\n : typeof typeConfig.defaultTargetPath === 'function'\n ? typeConfig.defaultTargetPath(payload.metadata)\n : typeConfig.defaultTargetPath;\n const targetPathField = targetPath === undefined ? {} : { targetPath };\n\n if (activeSnap.exists) {\n const existing = (activeSnap.data() ?? {}) as Pick<\n NotificationDoc,\n 'count' | 'latestActorIds'\n >;\n const delta = applyAggregation({\n strategy,\n existing,\n actorId: payload.actorId,\n countCap,\n actorCap,\n buildMessage,\n now,\n generation,\n });\n tx.update(activeRef, { ...delta, title, ...targetPathField, metadata: payload.metadata });\n } else {\n const delta = applyAggregation({\n strategy,\n existing: null,\n actorId: payload.actorId,\n countCap,\n actorCap,\n buildMessage,\n now,\n generation,\n });\n const newDoc: Omit<NotificationDoc, 'id'> = {\n type: notificationType,\n dedupKey: aggregationKey,\n category: typeConfig.category,\n targetUserId: recipientUid,\n title,\n message: delta.message,\n count: delta.count,\n latestActorIds: delta.latestActorIds,\n ...targetPathField,\n metadata: payload.metadata,\n seenAt: 0,\n activityGeneration: generation,\n createdAt: now,\n updatedAt: now,\n };\n tx.set(activeRef, newDoc as Record<string, unknown>);\n }\n\n tx.update(dRef, {\n state: 'materialized' as DeliveryState,\n materializedAt: now,\n expireAt: toTimestamp(now + ttlMs),\n });\n return 'materialized';\n });\n }\n\n async function recordTransientFailure(deliveryId: string, error: unknown): Promise<void> {\n await db.runTransaction(async (tx) => {\n const dRef = deliveryRef(deliveryId);\n const dSnap = await tx.get(dRef);\n if (!dSnap.exists) return;\n const d = (dSnap.data() ?? {}) as Record<string, unknown>;\n if ((d.state as DeliveryState) !== 'queued') return;\n const attemptCount = ((d.attemptCount as number) ?? 0) + 1;\n const now = Date.now();\n const message = error instanceof Error ? error.message : String(error);\n if (attemptCount >= maxAttempts) {\n tx.update(dRef, {\n state: 'deadLetter' as DeliveryState,\n attemptCount,\n lastError: message,\n deadLetteredAt: now,\n });\n } else {\n // n1: re-stamp the row into the reserved `retry` materialization lane on backoff. Without\n // this the row keeps its original class and competes for that lane's capacity while backed\n // off; the worker's reserved `retry` slice (separate CLASS_CAPS capacity, drained in\n // DRAIN_ORDER) would always select 0 rows. Re-stamping moves a backed-off row into the\n // anti-starvation lane so it drains there instead of starving in its original lane.\n tx.update(dRef, {\n attemptCount,\n nextAttemptAt: now + backoffMs(attemptCount),\n lastError: message,\n materializationClass: 'retry' as MaterializationClass,\n });\n }\n });\n }\n\n async function deadLetter(deliveryId: string, error: unknown): Promise<void> {\n await db.runTransaction(async (tx) => {\n const dRef = deliveryRef(deliveryId);\n const dSnap = await tx.get(dRef);\n if (!dSnap.exists) return;\n const d = (dSnap.data() ?? {}) as Record<string, unknown>;\n if ((d.state as DeliveryState) !== 'queued') return;\n tx.update(dRef, {\n state: 'deadLetter' as DeliveryState,\n attemptCount: ((d.attemptCount as number) ?? 0) + 1,\n lastError: error instanceof Error ? error.message : String(error),\n deadLetteredAt: Date.now(),\n });\n });\n }\n\n async function replay(deliveryId: string, options?: ReplayOptions): Promise<ReplayOutcome> {\n return db.runTransaction(async (tx): Promise<ReplayOutcome> => {\n const dRef = deliveryRef(deliveryId);\n const dSnap = await tx.get(dRef);\n if (!dSnap.exists) return 'missing';\n const d = (dSnap.data() ?? {}) as Record<string, unknown>;\n if ((d.state as DeliveryState) !== 'deadLetter') return 'not-dead-lettered';\n tx.update(dRef, {\n state: 'queued' as DeliveryState,\n attemptCount: 0,\n nextAttemptAt: Date.now(),\n lastError: null,\n deadLetteredAt: null,\n expireAt: null, // clear any TTL set on a prior terminal\n });\n await options?.auditWrite?.(tx);\n return 'replayed';\n });\n }\n\n function reportUnrecordedFailure(\n options: MaterializeManyOptions | undefined,\n deliveryId: string,\n error: unknown,\n cause: unknown,\n ): void {\n try {\n if (options?.onUnrecordedFailure) {\n options.onUnrecordedFailure(deliveryId, error, { cause });\n return;\n }\n } catch (handlerError) {\n console.error(\n '[notification-core] onUnrecordedFailure threw for delivery',\n deliveryId,\n handlerError,\n );\n }\n console.error(\n '[notification-core] A delivery failure could not be recorded',\n deliveryId,\n error,\n { cause },\n );\n }\n\n async function materializeMany(\n deliveryIds: string[],\n options?: MaterializeManyOptions,\n ): Promise<Record<MaterializeOutcome, number>> {\n const concurrency = Math.max(1, options?.concurrency ?? 10);\n const tally: Record<MaterializeOutcome, number> = {\n materialized: 0,\n 'already-materialized': 0,\n 'skipped-ineligible-recipient': 0,\n missing: 0,\n 'skipped-non-queued': 0,\n };\n let cursor = 0;\n async function worker(): Promise<void> {\n while (cursor < deliveryIds.length) {\n const id = deliveryIds[cursor++];\n try {\n const outcome = await materialize(id);\n tally[outcome] += 1;\n } catch (error) {\n try {\n await recordTransientFailure(id, error);\n } catch (recordError) {\n reportUnrecordedFailure(options, id, recordError, error);\n }\n }\n }\n }\n await Promise.all(Array.from({ length: Math.min(concurrency, deliveryIds.length) }, worker));\n return tally;\n }\n\n return { enqueue, materialize, materializeMany, recordTransientFailure, deadLetter, replay };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"delivery-ledger.js","sourceRoot":"","sources":["../../src/server/delivery-ledger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,OAAO,EAAE,4BAA4B,EAAE,MAAM,2BAA2B,CAAC;AAEzE,MAAM,uBAAuB,GAAG,wBAAwB,CAAC;AACzD,MAAM,uBAAuB,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AACzD,MAAM,oBAAoB,GAAG,CAAC,CAAC;AAC/B,MAAM,iBAAiB,GAAG,IAAI,CAAC;AAC/B,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAC5B,MAAM,e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AM,sBAAsB,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;oBAC1C,CAAC;oBAAC,OAAO,WAAW,EAAE,CAAC;wBACrB,uBAAuB,CAAC,OAAO,EAAE,EAAE,EAAE,WAAW,EAAE,KAAK,CAAC,CAAC;oBAC3D,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;QACD,MAAM,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,WAAW,EAAE,WAAW,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC;QAC7F,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,sBAAsB,EAAE,UAAU,EAAE,MAAM,EAAE,CAAC;AAC/F,CAAC","sourcesContent":["import { randomUUID } from 'node:crypto';\nimport type { NotificationSystemConfig, NotificationDoc } from '../types.js';\nimport type { ServerFirestore, ServerDocRef, ServerTransaction } from './types.js';\nimport { buildActiveNotificationDocId } from './activeNotificationId.js';\n\nconst DEFAULT_DELIVERIES_PATH = 'notificationDeliveries';\nconst DEFAULT_DELIVERY_TTL_MS = 90 * 24 * 60 * 60 * 1000;\nconst DEFAULT_MAX_ATTEMPTS = 8;\nconst DEFAULT_COUNT_CAP = 5000;\nconst DEFAULT_ACTOR_CAP = 5;\nconst BACKOFF_BASE_MS = 60 * 1000;\nconst BACKOFF_MAX_MS = 60 * 60 * 1000;\n\nexport type DeliveryState = 'queued' | 'materialized' | 'skipped' | 'deadLetter';\n\nexport type DeliverySkipReason = 'recipientIneligible';\n\nexport interface DeliveryLedgerOptions {\n /** Runs inside the materialize transaction before any write: read through `tx` only (RULE-STD-BACKEND-116). */\n isRecipientEligible: (recipientUid: string, tx: ServerTransaction) => Promise<boolean>;\n}\nexport type AggregationStrategy = 'increment' | 'staticRelight';\nexport type MaterializationClass = 'directQueued' | 'realtimeFallback' | 'fanoutOrphan' | 'retry';\n\nexport interface DeliveryPayload {\n actorId: string | null;\n metadata: Record<string, unknown>;\n occurrenceAt: number;\n}\n\nexport interface DeliveryRowInput {\n deliveryId: string;\n notificationType: string;\n eventId: string;\n recipientUid: string | null;\n aggregationKey: string;\n strategy: AggregationStrategy;\n payload: DeliveryPayload;\n payloadVersion: number;\n materializationClass: MaterializationClass;\n}\n\nexport type EnqueueRowResult =\n | { deliveryId: string; outcome: 'created' }\n | { deliveryId: string; outcome: 'duplicate' }\n | { deliveryId: string; outcome: 'failed'; error: unknown };\n\nexport interface EnqueueResult {\n results: EnqueueRowResult[];\n allResolved: boolean;\n}\n\nexport type MaterializeOutcome =\n | 'materialized'\n | 'already-materialized'\n | 'skipped-ineligible-recipient'\n | 'missing'\n | 'skipped-non-queued';\n\nexport interface MaterializeManyOptions {\n concurrency?: number;\n onUnrecordedFailure?: (deliveryId: string, error: unknown, details: { cause: unknown }) => void;\n}\n\nexport type ReplayOutcome = 'replayed' | 'missing' | 'not-dead-lettered';\n\nexport interface ReplayOptions {\n auditWrite?: (txn: ServerTransaction) => void | Promise<void>;\n}\n\nexport interface DeliveryLedger {\n enqueue(rows: DeliveryRowInput[]): Promise<EnqueueResult>;\n materialize(deliveryId: string): Promise<MaterializeOutcome>;\n materializeMany(\n deliveryIds: string[],\n options?: MaterializeManyOptions,\n ): Promise<Record<MaterializeOutcome, number>>;\n recordTransientFailure(deliveryId: string, error: unknown): Promise<void>;\n deadLetter(deliveryId: string, error: unknown): Promise<void>;\n replay(deliveryId: string, options?: ReplayOptions): Promise<ReplayOutcome>;\n}\n\nfunction isAlreadyExists(error: unknown): boolean {\n const e = error as { code?: number; message?: string } | undefined;\n return e?.code === 6 || /already.?exists/i.test(e?.message ?? '');\n}\n\nfunction backoffMs(attemptCount: number): number {\n const exp = Math.min(BACKOFF_BASE_MS * 2 ** Math.max(0, attemptCount - 1), BACKOFF_MAX_MS);\n return Math.floor(exp / 2 + Math.random() * (exp / 2));\n}\n\nexport function applyAggregation(params: {\n strategy: AggregationStrategy;\n existing: Pick<NotificationDoc, 'count' | 'latestActorIds'> | null;\n actorId: string | null;\n countCap: number;\n actorCap: number;\n buildMessage: (count: number) => string;\n now: number;\n generation: string;\n}): {\n count: number;\n latestActorIds: string[];\n message: string;\n activityGeneration: string;\n seenAt: number;\n updatedAt: number;\n} {\n const { strategy, existing, actorId, countCap, actorCap, buildMessage, now, generation } = params;\n const prevActors = existing?.latestActorIds ?? [];\n const latestActorIds = actorId\n ? [actorId, ...prevActors.filter((id) => id !== actorId)].slice(0, actorCap)\n : prevActors.slice(0, actorCap);\n const count = strategy === 'staticRelight' ? 1 : Math.min((existing?.count ?? 0) + 1, countCap);\n return {\n count,\n latestActorIds,\n message: buildMessage(count),\n activityGeneration: generation,\n seenAt: 0,\n updatedAt: now,\n };\n}\n\nexport function createDeliveryLedger(\n db: ServerFirestore,\n config: NotificationSystemConfig,\n options: DeliveryLedgerOptions,\n): DeliveryLedger {\n const { isRecipientEligible } = options;\n const deliveriesPath = config.deliveriesCollectionPath ?? DEFAULT_DELIVERIES_PATH;\n const ttlMs = config.deliveryTtlMs ?? DEFAULT_DELIVERY_TTL_MS;\n const maxAttempts = config.maxDeliveryAttempts ?? DEFAULT_MAX_ATTEMPTS;\n const configTimestampFromMillis = config.timestampFromMillis;\n if (!configTimestampFromMillis) {\n throw new Error(\n '[notification-core] createDeliveryLedger requires config.timestampFromMillis (Firestore TTL needs a real Timestamp for expireAt).',\n );\n }\n // An explicitly non-optional binding: TS does not carry the narrowing into closures called later.\n const toTimestamp: (ms: number) => unknown = configTimestampFromMillis;\n\n const deliveries = db.collection(deliveriesPath);\n\n function deliveryRef(deliveryId: string): ServerDocRef {\n return deliveries.doc(deliveryId);\n }\n\n function buildDeliveryDoc(row: DeliveryRowInput, now: number): Record<string, unknown> {\n return {\n deliveryId: row.deliveryId,\n state: 'queued' as DeliveryState,\n notificationType: row.notificationType,\n eventId: row.eventId,\n recipientUid: row.recipientUid,\n aggregationKey: row.aggregationKey,\n strategy: row.strategy,\n payload: row.payload,\n payloadVersion: row.payloadVersion,\n materializationClass: row.materializationClass,\n attemptCount: 0,\n nextAttemptAt: now,\n lastError: null,\n createdAt: now,\n materializedAt: null,\n deadLetteredAt: null,\n };\n }\n\n function getTypeConfig(type: string) {\n const typeConfig = config.types[type];\n if (!typeConfig) throw new Error(`[notification-core] Unknown notification type: ${type}`);\n const categoryConfig = config.categories[typeConfig.category];\n if (!categoryConfig)\n throw new Error(`[notification-core] Unknown category: ${typeConfig.category}`);\n return { typeConfig, categoryConfig };\n }\n\n async function enqueue(rows: DeliveryRowInput[]): Promise<EnqueueResult> {\n if (rows.length === 0) return { results: [], allResolved: true };\n const now = Date.now();\n const settled = await Promise.allSettled(\n rows.map((row) =>\n deliveryRef(row.deliveryId)\n .create(buildDeliveryDoc(row, now))\n .then(() => ({ deliveryId: row.deliveryId, outcome: 'created' as const })),\n ),\n );\n const results: EnqueueRowResult[] = settled.map((s, i) => {\n const deliveryId = rows[i].deliveryId;\n if (s.status === 'fulfilled') return s.value;\n if (isAlreadyExists(s.reason)) return { deliveryId, outcome: 'duplicate' };\n return { deliveryId, outcome: 'failed', error: s.reason };\n });\n return { results, allResolved: results.every((r) => r.outcome !== 'failed') };\n }\n\n async function materialize(deliveryId: string): Promise<MaterializeOutcome> {\n return db.runTransaction(async (tx) => {\n const dRef = deliveryRef(deliveryId);\n const dSnap = await tx.get(dRef);\n if (!dSnap.exists) return 'missing';\n const d = (dSnap.data() ?? {}) as Record<string, unknown>;\n const state = d.state as DeliveryState;\n if (state === 'materialized') return 'already-materialized';\n if (state === 'skipped') return 'skipped-ineligible-recipient';\n if (state !== 'queued') return 'skipped-non-queued';\n\n const notificationType = d.notificationType as string;\n const aggregationKey = d.aggregationKey as string;\n const recipientUid = (d.recipientUid as string | null) ?? null;\n\n if (recipientUid !== null && !(await isRecipientEligible(recipientUid, tx))) {\n const skippedAt = Date.now();\n tx.update(dRef, {\n state: 'skipped' as DeliveryState,\n skipReason: 'recipientIneligible' as DeliverySkipReason,\n skippedAt,\n expireAt: toTimestamp(skippedAt + ttlMs),\n });\n return 'skipped-ineligible-recipient';\n }\n const strategy = (d.strategy as AggregationStrategy) ?? 'increment';\n const payload = (d.payload as DeliveryPayload) ?? {\n actorId: '',\n metadata: {},\n occurrenceAt: Date.now(),\n };\n const { typeConfig, categoryConfig } = getTypeConfig(notificationType);\n\n const activeRef = db.collection(categoryConfig.activePath).doc(\n buildActiveNotificationDocId({\n category: typeConfig.category,\n audienceType: categoryConfig.audienceType,\n targetUserId: recipientUid,\n dedupKey: aggregationKey,\n notificationType,\n }),\n );\n const activeSnap = await tx.get(activeRef); // Firestore transactions take every read before any write.\n\n const now = Date.now();\n const generation = randomUUID();\n const countCap = typeConfig.countCap ?? DEFAULT_COUNT_CAP;\n const actorCap = typeConfig.actorCap ?? DEFAULT_ACTOR_CAP;\n const buildMessage = (count: number) => typeConfig.messagePattern(payload.metadata, count);\n const title = typeConfig.titlePattern(payload.metadata);\n // Firestore rejects `undefined` values, so a linkless type omits the field.\n const targetPath =\n typeConfig.defaultTargetPath === undefined\n ? undefined\n : typeof typeConfig.defaultTargetPath === 'function'\n ? typeConfig.defaultTargetPath(payload.metadata)\n : typeConfig.defaultTargetPath;\n const targetPathField = targetPath === undefined ? {} : { targetPath };\n\n if (activeSnap.exists) {\n const existing = (activeSnap.data() ?? {}) as Pick<\n NotificationDoc,\n 'count' | 'latestActorIds'\n >;\n const delta = applyAggregation({\n strategy,\n existing,\n actorId: payload.actorId,\n countCap,\n actorCap,\n buildMessage,\n now,\n generation,\n });\n tx.update(activeRef, { ...delta, title, ...targetPathField, metadata: payload.metadata });\n } else {\n const delta = applyAggregation({\n strategy,\n existing: null,\n actorId: payload.actorId,\n countCap,\n actorCap,\n buildMessage,\n now,\n generation,\n });\n const newDoc: Omit<NotificationDoc, 'id'> = {\n type: notificationType,\n dedupKey: aggregationKey,\n category: typeConfig.category,\n targetUserId: recipientUid,\n title,\n message: delta.message,\n count: delta.count,\n latestActorIds: delta.latestActorIds,\n ...targetPathField,\n metadata: payload.metadata,\n seenAt: 0,\n activityGeneration: generation,\n createdAt: now,\n updatedAt: now,\n };\n tx.set(activeRef, newDoc as Record<string, unknown>);\n }\n\n tx.update(dRef, {\n state: 'materialized' as DeliveryState,\n materializedAt: now,\n expireAt: toTimestamp(now + ttlMs),\n });\n return 'materialized';\n });\n }\n\n async function recordTransientFailure(deliveryId: string, error: unknown): Promise<void> {\n await db.runTransaction(async (tx) => {\n const dRef = deliveryRef(deliveryId);\n const dSnap = await tx.get(dRef);\n if (!dSnap.exists) return;\n const d = (dSnap.data() ?? {}) as Record<string, unknown>;\n if ((d.state as DeliveryState) !== 'queued') return;\n const attemptCount = ((d.attemptCount as number) ?? 0) + 1;\n const now = Date.now();\n const message = error instanceof Error ? error.message : String(error);\n if (attemptCount >= maxAttempts) {\n tx.update(dRef, {\n state: 'deadLetter' as DeliveryState,\n attemptCount,\n lastError: message,\n deadLetteredAt: now,\n });\n } else {\n tx.update(dRef, {\n attemptCount,\n nextAttemptAt: now + backoffMs(attemptCount),\n lastError: message,\n materializationClass: 'retry' as MaterializationClass,\n });\n }\n });\n }\n\n async function deadLetter(deliveryId: string, error: unknown): Promise<void> {\n await db.runTransaction(async (tx) => {\n const dRef = deliveryRef(deliveryId);\n const dSnap = await tx.get(dRef);\n if (!dSnap.exists) return;\n const d = (dSnap.data() ?? {}) as Record<string, unknown>;\n if ((d.state as DeliveryState) !== 'queued') return;\n tx.update(dRef, {\n state: 'deadLetter' as DeliveryState,\n attemptCount: ((d.attemptCount as number) ?? 0) + 1,\n lastError: error instanceof Error ? error.message : String(error),\n deadLetteredAt: Date.now(),\n });\n });\n }\n\n async function replay(deliveryId: string, options?: ReplayOptions): Promise<ReplayOutcome> {\n return db.runTransaction(async (tx): Promise<ReplayOutcome> => {\n const dRef = deliveryRef(deliveryId);\n const dSnap = await tx.get(dRef);\n if (!dSnap.exists) return 'missing';\n const d = (dSnap.data() ?? {}) as Record<string, unknown>;\n if ((d.state as DeliveryState) !== 'deadLetter') return 'not-dead-lettered';\n tx.update(dRef, {\n state: 'queued' as DeliveryState,\n attemptCount: 0,\n nextAttemptAt: Date.now(),\n lastError: null,\n deadLetteredAt: null,\n expireAt: null,\n });\n await options?.auditWrite?.(tx);\n return 'replayed';\n });\n }\n\n function reportUnrecordedFailure(\n options: MaterializeManyOptions | undefined,\n deliveryId: string,\n error: unknown,\n cause: unknown,\n ): void {\n try {\n if (options?.onUnrecordedFailure) {\n options.onUnrecordedFailure(deliveryId, error, { cause });\n return;\n }\n } catch (handlerError) {\n console.error(\n '[notification-core] onUnrecordedFailure threw for delivery',\n deliveryId,\n handlerError,\n );\n }\n console.error(\n '[notification-core] A delivery failure could not be recorded',\n deliveryId,\n error,\n { cause },\n );\n }\n\n async function materializeMany(\n deliveryIds: string[],\n options?: MaterializeManyOptions,\n ): Promise<Record<MaterializeOutcome, number>> {\n const concurrency = Math.max(1, options?.concurrency ?? 10);\n const tally: Record<MaterializeOutcome, number> = {\n materialized: 0,\n 'already-materialized': 0,\n 'skipped-ineligible-recipient': 0,\n missing: 0,\n 'skipped-non-queued': 0,\n };\n let cursor = 0;\n async function worker(): Promise<void> {\n while (cursor < deliveryIds.length) {\n const id = deliveryIds[cursor++];\n try {\n const outcome = await materialize(id);\n tally[outcome] += 1;\n } catch (error) {\n try {\n await recordTransientFailure(id, error);\n } catch (recordError) {\n reportUnrecordedFailure(options, id, recordError, error);\n }\n }\n }\n }\n await Promise.all(Array.from({ length: Math.min(concurrency, deliveryIds.length) }, worker));\n return tally;\n }\n\n return { enqueue, materialize, materializeMany, recordTransientFailure, deadLetter, replay };\n}\n"]}
|
package/dist/server/errors.d.ts
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Typed errors thrown by notification-core server helpers.
|
|
3
|
-
*/
|
|
4
|
-
/**
|
|
5
|
-
* Thrown when a caller is not allowed to perform a notification action — e.g.
|
|
6
|
-
* archiving a personal notification they do not own, or a non-admin archiving a
|
|
7
|
-
* shared notification. Consuming callables should catch this and map it to a
|
|
8
|
-
* `permission-denied` error code rather than surfacing a generic `internal`.
|
|
9
|
-
*/
|
|
10
1
|
export declare class NotificationPermissionError extends Error {
|
|
11
2
|
constructor(message: string);
|
|
12
3
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/server/errors.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/server/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,2BAA4B,SAAQ,KAAK;gBACxC,OAAO,EAAE,MAAM;CAM5B"}
|
package/dist/server/errors.js
CHANGED
|
@@ -1,18 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Typed errors thrown by notification-core server helpers.
|
|
3
|
-
*/
|
|
4
|
-
/**
|
|
5
|
-
* Thrown when a caller is not allowed to perform a notification action — e.g.
|
|
6
|
-
* archiving a personal notification they do not own, or a non-admin archiving a
|
|
7
|
-
* shared notification. Consuming callables should catch this and map it to a
|
|
8
|
-
* `permission-denied` error code rather than surfacing a generic `internal`.
|
|
9
|
-
*/
|
|
10
1
|
export class NotificationPermissionError extends Error {
|
|
11
2
|
constructor(message) {
|
|
12
3
|
super(message);
|
|
13
4
|
this.name = 'NotificationPermissionError';
|
|
14
|
-
//
|
|
15
|
-
// target (extending built-ins can break it otherwise).
|
|
5
|
+
// Restores the prototype chain, which extending a built-in can break under the transpile target.
|
|
16
6
|
Object.setPrototypeOf(this, NotificationPermissionError.prototype);
|
|
17
7
|
}
|
|
18
8
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/server/errors.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/server/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,2BAA4B,SAAQ,KAAK;IACpD,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,6BAA6B,CAAC;QAC1C,iGAAiG;QACjG,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,2BAA2B,CAAC,SAAS,CAAC,CAAC;IACrE,CAAC;CACF","sourcesContent":["export class NotificationPermissionError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'NotificationPermissionError';\n // Restores the prototype chain, which extending a built-in can break under the transpile target.\n Object.setPrototypeOf(this, NotificationPermissionError.prototype);\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,2BAA2B,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,4BAA4B,EAAE,MAAM,2BAA2B,CAAC;AAEzE,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC9E,YAAY,EACV,cAAc,EACd,gBAAgB,EAChB,eAAe,EACf,aAAa,EACb,kBAAkB,EAClB,qBAAqB,EACrB,mBAAmB,EACnB,oBAAoB,EACpB,aAAa,EACb,gBAAgB,EAChB,kBAAkB,EAClB,sBAAsB,EACtB,aAAa,EACb,aAAa,GACd,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EACL,kCAAkC,EAClC,iCAAiC,GAClC,MAAM,0BAA0B,CAAC;AAClC,YAAY,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAEhF,YAAY,EACV,eAAe,EACf,mBAAmB,EACnB,WAAW,EACX,mBAAmB,EACnB,iBAAiB,EACjB,YAAY,EACZ,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,YAAY,CAAC"}
|
package/dist/server/index.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
// Server-side helpers (for Cloud Functions)
|
|
2
1
|
export { processBatchHelper } from './processBatchHelper.js';
|
|
3
2
|
export { NotificationPermissionError } from './errors.js';
|
|
4
3
|
export { buildActiveNotificationDocId } from './activeNotificationId.js';
|
|
5
|
-
// Delivery ledger
|
|
6
4
|
export { createDeliveryLedger, applyAggregation } from './delivery-ledger.js';
|
|
7
|
-
// Observed-generation seen/archive protocol
|
|
8
5
|
export { markNotificationSeenWithGeneration, archiveNotificationWithGeneration, } from './observed-generation.js';
|
|
9
6
|
//# sourceMappingURL=index.js.map
|
package/dist/server/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,2BAA2B,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,4BAA4B,EAAE,MAAM,2BAA2B,CAAC;AAEzE,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAkB9E,OAAO,EACL,kCAAkC,EAClC,iCAAiC,GAClC,MAAM,0BAA0B,CAAC","sourcesContent":["export { processBatchHelper } from './processBatchHelper.js';\nexport { NotificationPermissionError } from './errors.js';\nexport { buildActiveNotificationDocId } from './activeNotificationId.js';\n\nexport { createDeliveryLedger, applyAggregation } from './delivery-ledger.js';\nexport type {\n DeliveryLedger,\n DeliveryRowInput,\n DeliveryPayload,\n DeliveryState,\n DeliverySkipReason,\n DeliveryLedgerOptions,\n AggregationStrategy,\n MaterializationClass,\n EnqueueResult,\n EnqueueRowResult,\n MaterializeOutcome,\n MaterializeManyOptions,\n ReplayOutcome,\n ReplayOptions,\n} from './delivery-ledger.js';\n\nexport {\n markNotificationSeenWithGeneration,\n archiveNotificationWithGeneration,\n} from './observed-generation.js';\nexport type { MarkSeenOutcome, ArchiveOutcome } from './observed-generation.js';\n\nexport type {\n ServerFirestore,\n ServerCollectionRef,\n ServerQuery,\n ServerQuerySnapshot,\n ServerDocSnapshot,\n ServerDocRef,\n ServerWriteBatch,\n ServerTransaction,\n} from './types.js';\n"]}
|
|
@@ -1,62 +1,22 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Observed-generation seen/archive protocol (notification redesign — P1).
|
|
3
|
-
*
|
|
4
|
-
* The SEEN/ARCHIVE precondition compares the card's current opaque
|
|
5
|
-
* `activityGeneration` against the generation the client actually observed when
|
|
6
|
-
* it rendered the row — so activity arriving after render (or a delete+recreate
|
|
7
|
-
* that rotated the token) is never silently swallowed. Archive additionally
|
|
8
|
-
* carries a retry-stable `requestId`; the deterministic history doc id makes a
|
|
9
|
-
* replay return the stored result and touch nothing.
|
|
10
|
-
*
|
|
11
|
-
* GENERIC: the app constructs the deterministic ids (`historyRef`, `payloadHash`)
|
|
12
|
-
* via its own canonical hash (in the consuming app's core package) and passes the
|
|
13
|
-
* refs in; this module owns only the transactional state machine, not the id formulas.
|
|
14
|
-
*/
|
|
15
1
|
import type { ServerFirestore, ServerDocRef, ServerTransaction } from './types.js';
|
|
16
2
|
export type MarkSeenOutcome = 'seen' | 'generation-mismatch' | 'missing';
|
|
17
|
-
/**
|
|
18
|
-
* Set `seenAt`/`seenAtGeneration` ONLY if the card's current `activityGeneration`
|
|
19
|
-
* still matches the observed one; otherwise no-op (the row stays unseen so newer
|
|
20
|
-
* activity is never swallowed).
|
|
21
|
-
*/
|
|
22
3
|
export declare function markNotificationSeenWithGeneration(db: ServerFirestore, activeRef: ServerDocRef, params: {
|
|
23
4
|
observedActivityGeneration: string;
|
|
24
5
|
now?: number;
|
|
25
6
|
}): Promise<MarkSeenOutcome>;
|
|
26
7
|
export type ArchiveOutcome = 'archived' | 'replayed' | 'conflict' | 'generation-mismatch' | 'missing';
|
|
27
|
-
/**
|
|
28
|
-
* Archive one active card into history, idempotently and replay-safely.
|
|
29
|
-
*
|
|
30
|
-
* - history doc already exists + same `payloadHash` ⇒ `replayed` (touch NOTHING).
|
|
31
|
-
* - history doc already exists + DIFFERENT `payloadHash` ⇒ `conflict` (caller alerts).
|
|
32
|
-
* - first-seen: archive only if the active card's `activityGeneration` still
|
|
33
|
-
* matches the observed generation; else `generation-mismatch` (leave it active).
|
|
34
|
-
*/
|
|
35
8
|
export declare function archiveNotificationWithGeneration(db: ServerFirestore, params: {
|
|
36
9
|
activeRef: ServerDocRef;
|
|
37
|
-
/** Deterministic history doc id = hash('notification-archive', category, audienceScope, requestId) (app-built). */
|
|
38
10
|
historyRef: ServerDocRef;
|
|
39
11
|
requestId: string;
|
|
40
12
|
observedActivityGeneration: string;
|
|
41
|
-
/** Deterministic payload hash (app-built) — the replay-vs-conflict discriminator. */
|
|
42
13
|
payloadHash: string;
|
|
43
14
|
category: string;
|
|
44
15
|
audienceScope: string;
|
|
45
|
-
/** Native-TTL expiry for the history doc; converted to a Timestamp via the factory. */
|
|
46
16
|
expireAtMs: number;
|
|
47
17
|
timestampFromMillis: (ms: number) => unknown;
|
|
48
|
-
/** Admin history quick-access field (admin category only). */
|
|
49
18
|
handledBy?: string;
|
|
50
|
-
/**
|
|
51
|
-
* Optional hook to compose additional writes (e.g. an audit event) INTO the
|
|
52
|
-
* same archive transaction, so they commit atomically with the active→history
|
|
53
|
-
* move. Invoked in the write phase ONLY on the successful 'archived' path —
|
|
54
|
-
* never on replay/conflict/generation-mismatch/missing — so the audit is
|
|
55
|
-
* written iff the archive actually happens. Honor reads-before-writes: do not
|
|
56
|
-
* read inside the hook. The hook is awaited inside the transaction body, so a
|
|
57
|
-
* throw or a rejected promise fails the archive instead of committing it
|
|
58
|
-
* without its write.
|
|
59
|
-
*/
|
|
19
|
+
/** Runs in the write phase, so it must not read (Firestore reads-before-writes). */
|
|
60
20
|
auditWrite?: (txn: ServerTransaction) => void | Promise<void>;
|
|
61
21
|
}): Promise<ArchiveOutcome>;
|
|
62
22
|
//# sourceMappingURL=observed-generation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"observed-generation.d.ts","sourceRoot":"","sources":["../../src/server/observed-generation.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"observed-generation.d.ts","sourceRoot":"","sources":["../../src/server/observed-generation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEnF,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,qBAAqB,GAAG,SAAS,CAAC;AAEzE,wBAAsB,kCAAkC,CACtD,EAAE,EAAE,eAAe,EACnB,SAAS,EAAE,YAAY,EACvB,MAAM,EAAE;IAAE,0BAA0B,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,GAC3D,OAAO,CAAC,eAAe,CAAC,CAc1B;AAED,MAAM,MAAM,cAAc,GACxB,UAAU,GAAG,UAAU,GAAG,UAAU,GAAG,qBAAqB,GAAG,SAAS,CAAC;AAE3E,wBAAsB,iCAAiC,CACrD,EAAE,EAAE,eAAe,EACnB,MAAM,EAAE;IACN,SAAS,EAAE,YAAY,CAAC;IACxB,UAAU,EAAE,YAAY,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,0BAA0B,EAAE,MAAM,CAAC;IACnC,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,UAAU,EAAE,MAAM,CAAC;IACnB,mBAAmB,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC;IAC7C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,oFAAoF;IACpF,UAAU,CAAC,EAAE,CAAC,GAAG,EAAE,iBAAiB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/D,GACA,OAAO,CAAC,cAAc,CAAC,CAiDzB"}
|