@classytic/notifications 2.8.0 → 2.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +89 -0
- package/README.md +96 -0
- package/dist/channels/index.d.mts +1 -1
- package/dist/channels/index.mjs +1 -1
- package/dist/errors-DCOaQvhV.d.mts +37 -0
- package/dist/index.d.mts +18 -4
- package/dist/index.mjs +103 -12
- package/dist/push/index.d.mts +414 -0
- package/dist/push/index.mjs +1133 -0
- package/dist/{status-webhook-BlhPvRnD.d.mts → status-webhook-DCU8KQLR.d.mts} +13 -38
- package/dist/{status-webhook-D7Hrr71A.mjs → status-webhook-DN3bdqKs.mjs} +37 -2
- package/dist/{telegram.channel-DnVdyq8_.mjs → telegram.channel-0XRkIXmt.mjs} +37 -3
- package/dist/{telegram.channel-CzQH7Wa8.d.mts → telegram.channel-DcqdfWmV.d.mts} +1 -1
- package/dist/{types-DyUMfkyr.d.mts → types-D3g8xK2h.d.mts} +78 -19
- package/dist/utils/index.d.mts +4 -3
- package/dist/utils/index.mjs +2 -2
- package/package.json +7 -2
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
import { C as PushProviderMessage, S as PushProvider, b as PushInterruptionLevel, x as PushPlatform } from "../types-D3g8xK2h.mjs";
|
|
2
|
+
import { n as NotificationError, r as PermanentDeliveryError } from "../errors-DCOaQvhV.mjs";
|
|
3
|
+
import { KeyObject } from "node:crypto";
|
|
4
|
+
import http2 from "node:http2";
|
|
5
|
+
//#region src/push/aps.d.ts
|
|
6
|
+
interface ApsOptions {
|
|
7
|
+
/**
|
|
8
|
+
* The iOS build carries Apple's critical-alerts entitlement. Without it a `critical` level is
|
|
9
|
+
* downgraded to `time-sensitive`, because Apple rejects or silently drops critical alerts from
|
|
10
|
+
* apps that lack the entitlement. Default false.
|
|
11
|
+
*/
|
|
12
|
+
readonly allowCriticalAlerts?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** The effective interruption level after the entitlement check. */
|
|
15
|
+
export declare function effectiveLevel(level: PushInterruptionLevel | undefined, options: ApsOptions): PushInterruptionLevel | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Build `aps` for a message. Visible pushes get an `alert` and a sound (`default` unless the
|
|
18
|
+
* message says otherwise; `null` = silent). Data-only pushes get only `content-available: 1`.
|
|
19
|
+
*/
|
|
20
|
+
export declare function buildAps(message: PushProviderMessage, options: ApsOptions): Record<string, unknown>;
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/push/errors.d.ts
|
|
23
|
+
/** Which provider produced an error or event. */
|
|
24
|
+
type PushProviderName = 'fcm' | 'apns' | 'router';
|
|
25
|
+
/**
|
|
26
|
+
* Why a token was declared dead. `unregistered` (FCM UNREGISTERED, APNs 410) is the certain
|
|
27
|
+
* signal: the app was uninstalled or the token rotated. `invalid-token` (FCM INVALID_ARGUMENT
|
|
28
|
+
* naming the token) and `bad-device-token` (APNs 400 BadDeviceToken) also fire when the token
|
|
29
|
+
* was minted for ANOTHER environment or project, so a host unsure its configuration matches the
|
|
30
|
+
* app build may choose to delete only on `unregistered`.
|
|
31
|
+
*/
|
|
32
|
+
type InvalidTokenReason = 'unregistered' | 'invalid-token' | 'bad-device-token';
|
|
33
|
+
/**
|
|
34
|
+
* The device token will never work again. Permanent (`retryable: false`): the service does not
|
|
35
|
+
* retry it, and the host should delete the device row (the providers' `onInvalidToken` hook and
|
|
36
|
+
* arc-notifications' device channel do).
|
|
37
|
+
*/
|
|
38
|
+
export declare class InvalidPushTokenError extends PermanentDeliveryError {
|
|
39
|
+
readonly reason: InvalidTokenReason;
|
|
40
|
+
readonly provider: PushProviderName;
|
|
41
|
+
/** HTTP status the provider answered. */
|
|
42
|
+
readonly status: number;
|
|
43
|
+
/** Last 6 characters of the token: enough to correlate, useless to an attacker. */
|
|
44
|
+
readonly tokenSuffix: string;
|
|
45
|
+
/** Structural marker (survives bundling / duplicate copies of this package). */
|
|
46
|
+
readonly invalidToken: true;
|
|
47
|
+
constructor(provider: PushProviderName, reason: InvalidTokenReason, status: number, tokenSuffix: string);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A failure no retry fixes that is NOT about the token: a payload the provider rejects, a
|
|
51
|
+
* misconfigured topic or sender, a platform with no provider. The device row is kept.
|
|
52
|
+
*/
|
|
53
|
+
export declare class PushRejectedError extends PermanentDeliveryError {
|
|
54
|
+
readonly provider: PushProviderName;
|
|
55
|
+
readonly status: number;
|
|
56
|
+
/** The provider's reason code (`BadTopic`, `INVALID_ARGUMENT`, ...), or a local one. */
|
|
57
|
+
readonly reason: string;
|
|
58
|
+
constructor(provider: PushProviderName, status: number, reason: string, detail?: string);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Credentials are missing, malformed, or refused by the provider. Permanent: retrying with the
|
|
62
|
+
* same credentials cannot succeed (a rotated credential is picked up on the next send anyway).
|
|
63
|
+
* The message names FIELDS, never values.
|
|
64
|
+
*/
|
|
65
|
+
export declare class PushCredentialsError extends PermanentDeliveryError {
|
|
66
|
+
readonly provider: PushProviderName;
|
|
67
|
+
constructor(provider: PushProviderName, message: string);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* A transient failure (429, 5xx, timeout, connection reset). Retryable; `retryAfterMs` carries the
|
|
71
|
+
* provider's `Retry-After`, which the service's retry loop honours (`retryAfterHint`), and
|
|
72
|
+
* `retryFloorMs` the provider's minimum wait (FCM 10 s, FCM 429 60 s, APNs 5xx 15 min, APNs 429 60 s;
|
|
73
|
+
* `PUSH_RETRY_DEFAULTS`), which nothing may retry sooner than (2.10).
|
|
74
|
+
*/
|
|
75
|
+
export declare class TransientPushError extends NotificationError {
|
|
76
|
+
readonly retryable: true;
|
|
77
|
+
readonly provider: PushProviderName;
|
|
78
|
+
/** HTTP status, or 0 for a network-level failure. */
|
|
79
|
+
readonly status: number;
|
|
80
|
+
readonly reason: string;
|
|
81
|
+
readonly retryAfterMs?: number;
|
|
82
|
+
/**
|
|
83
|
+
* The provider's minimum wait before a retry (ms). Defaults from {@link pushRetryFloorMs}
|
|
84
|
+
* (provider + status); a throw site may pass its own. `withRetry` and `planPushRetry` never
|
|
85
|
+
* retry sooner.
|
|
86
|
+
*/
|
|
87
|
+
readonly retryFloorMs: number;
|
|
88
|
+
constructor(provider: PushProviderName, status: number, reason: string, options?: {
|
|
89
|
+
retryAfterMs?: number;
|
|
90
|
+
retryFloorMs?: number;
|
|
91
|
+
cause?: Error;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
/** Structural check: is this an invalid-token error (from any copy of this package)? */
|
|
95
|
+
export declare function isInvalidPushTokenError(err: unknown): err is InvalidPushTokenError;
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/push/credentials.d.ts
|
|
98
|
+
/** A credential value, or a (possibly async) function returning the current one. */
|
|
99
|
+
type CredentialsSource<T> = T | (() => T | Promise<T>);
|
|
100
|
+
/** Google service-account credentials for FCM HTTP v1. */
|
|
101
|
+
interface FcmCredentials {
|
|
102
|
+
/** Firebase / Google Cloud project id (`project_id`). */
|
|
103
|
+
projectId: string;
|
|
104
|
+
/** Service-account email (`client_email`). */
|
|
105
|
+
clientEmail: string;
|
|
106
|
+
/** PKCS#8 RSA private key, PEM (`private_key`). Literal `\n` escapes are accepted. */
|
|
107
|
+
privateKey: string;
|
|
108
|
+
/** `private_key_id`: sent as the JWT `kid` when present. */
|
|
109
|
+
privateKeyId?: string;
|
|
110
|
+
/** OAuth2 token endpoint (`token_uri`). Default `https://oauth2.googleapis.com/token`. */
|
|
111
|
+
tokenUri?: string;
|
|
112
|
+
/** Opaque version/etag. When it changes, the provider re-imports the key. */
|
|
113
|
+
version?: string;
|
|
114
|
+
}
|
|
115
|
+
/** APNs token-based authentication (`.p8` signing key). */
|
|
116
|
+
interface ApnsCredentials {
|
|
117
|
+
/** 10-character key id from the Apple developer account. */
|
|
118
|
+
keyId: string;
|
|
119
|
+
/** 10-character team id. */
|
|
120
|
+
teamId: string;
|
|
121
|
+
/** The `.p8` file contents (PKCS#8 EC P-256 private key, PEM). Literal `\n` escapes are accepted. */
|
|
122
|
+
privateKey: string;
|
|
123
|
+
/** The app's bundle id, sent as `apns-topic`. */
|
|
124
|
+
bundleId: string;
|
|
125
|
+
/** Which APNs host: `production` (default) or `sandbox` (development builds). */
|
|
126
|
+
environment?: 'production' | 'sandbox';
|
|
127
|
+
/** Opaque version/etag. When it changes, the provider re-signs and reconnects if the host moved. */
|
|
128
|
+
version?: string;
|
|
129
|
+
}
|
|
130
|
+
export declare const GOOGLE_TOKEN_URI = "https://oauth2.googleapis.com/token";
|
|
131
|
+
/** `\n` escapes (an env var or a JSON copy) become real newlines; surrounding space is dropped. */
|
|
132
|
+
export declare function normalizePem(pem: string): string;
|
|
133
|
+
/**
|
|
134
|
+
* Accept a Google service-account file in any of the forms hosts store it: the JSON object
|
|
135
|
+
* (snake_case as downloaded, or camelCase), its JSON text, or the base64 of that text.
|
|
136
|
+
*/
|
|
137
|
+
export declare function parseFcmServiceAccount(input: string | Record<string, unknown>, version?: string): FcmCredentials;
|
|
138
|
+
/** Check shape and key type; returns a normalized copy. Throws {@link PushCredentialsError}. */
|
|
139
|
+
export declare function validateFcmCredentials(creds: FcmCredentials): FcmCredentials;
|
|
140
|
+
/** Check shape and key type (EC P-256); returns a normalized copy. Throws {@link PushCredentialsError}. */
|
|
141
|
+
export declare function validateApnsCredentials(creds: ApnsCredentials): ApnsCredentials;
|
|
142
|
+
/** A stable cache key for a credential value: its `version`, else a hash of every field. */
|
|
143
|
+
export declare function credentialsFingerprint(creds: object): string;
|
|
144
|
+
//#endregion
|
|
145
|
+
//#region src/push/internal.d.ts
|
|
146
|
+
/** Called when a token is declared dead: delete the device row here. Errors are swallowed. */
|
|
147
|
+
type InvalidTokenHandler = (token: string, reason: InvalidTokenReason) => void | Promise<void>;
|
|
148
|
+
/**
|
|
149
|
+
* One structured record per send attempt, for an opt-in delivery log or metrics. Holds only what
|
|
150
|
+
* is safe to persist: a token SUFFIX and HASH (never the token), status codes, latency. No title,
|
|
151
|
+
* body, or data.
|
|
152
|
+
*/
|
|
153
|
+
interface PushDeliveryEvent {
|
|
154
|
+
readonly provider: PushProviderName;
|
|
155
|
+
readonly platform?: PushPlatform;
|
|
156
|
+
readonly outcome: 'sent' | 'invalid-token' | 'rejected' | 'transient' | 'credentials';
|
|
157
|
+
/** HTTP status (0 = network failure or no request made). */
|
|
158
|
+
readonly status: number;
|
|
159
|
+
/** Provider reason code (`Unregistered`, `UNAVAILABLE`, ...). */
|
|
160
|
+
readonly reason?: string;
|
|
161
|
+
/** Provider message id on success (`apns-id`, FCM message name). */
|
|
162
|
+
readonly messageId?: string;
|
|
163
|
+
/** Last 6 characters of the device token. */
|
|
164
|
+
readonly tokenSuffix: string;
|
|
165
|
+
/** sha256 prefix of the device token: joins events for one device without storing it. */
|
|
166
|
+
readonly tokenHash: string;
|
|
167
|
+
readonly latencyMs: number;
|
|
168
|
+
readonly retryAfterMs?: number;
|
|
169
|
+
}
|
|
170
|
+
type PushDeliveryListener = (event: PushDeliveryEvent) => void;
|
|
171
|
+
/** Last 6 characters, for messages and logs. */
|
|
172
|
+
export declare function tokenSuffix(token: string): string;
|
|
173
|
+
/** Non-reversible token hash for joining log rows. */
|
|
174
|
+
export declare function tokenHash(token: string): string;
|
|
175
|
+
/**
|
|
176
|
+
* `Retry-After` as milliseconds: delta-seconds or an HTTP-date. Unparseable or negative values
|
|
177
|
+
* are ignored. Capped at one hour so a hostile or broken header cannot park a queue for days.
|
|
178
|
+
*/
|
|
179
|
+
export declare function parseRetryAfter(value: string | string[] | null | undefined, now: number): number | undefined;
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region src/push/fcm.d.ts
|
|
182
|
+
export declare const FCM_ENDPOINT = "https://fcm.googleapis.com";
|
|
183
|
+
export declare const FCM_SCOPE = "https://www.googleapis.com/auth/firebase.messaging";
|
|
184
|
+
interface FcmProviderOptions extends ApsOptions {
|
|
185
|
+
/** Service-account credentials, or a function returning the current ones (re-read per send). */
|
|
186
|
+
readonly credentials: CredentialsSource<FcmCredentials>;
|
|
187
|
+
/** Dead-token hook (delete the device row). */
|
|
188
|
+
readonly onInvalidToken?: InvalidTokenHandler;
|
|
189
|
+
/** One structured, redacted event per send (opt-in delivery log / metrics). */
|
|
190
|
+
readonly onDelivery?: PushDeliveryListener;
|
|
191
|
+
/** FCM origin. Default `https://fcm.googleapis.com` (tests point it at a local server). */
|
|
192
|
+
readonly endpoint?: string;
|
|
193
|
+
/** Request timeout per HTTP call. Default 10 000 ms. */
|
|
194
|
+
readonly timeoutMs?: number;
|
|
195
|
+
/** Max sends in flight from this provider instance. Default 100. */
|
|
196
|
+
readonly maxConcurrency?: number;
|
|
197
|
+
/** Ask FCM to validate without delivering (`validate_only`). For a "send test" dry run. */
|
|
198
|
+
readonly validateOnly?: boolean;
|
|
199
|
+
/** Injected `fetch` (tests, proxies). Default `globalThis.fetch`. */
|
|
200
|
+
readonly fetch?: typeof fetch;
|
|
201
|
+
/** Clock (ms). Default `Date.now`. */
|
|
202
|
+
readonly now?: () => number;
|
|
203
|
+
}
|
|
204
|
+
/** Build the FCM v1 `message` object. Exported for tests and for hosts that inspect payloads. */
|
|
205
|
+
export declare function buildFcmMessage(message: PushProviderMessage, options: ApsOptions, nowMs: number): Record<string, unknown>;
|
|
206
|
+
export declare function fcmProvider(options: FcmProviderOptions): PushProvider;
|
|
207
|
+
//#endregion
|
|
208
|
+
//#region src/push/apns.d.ts
|
|
209
|
+
export declare const APNS_HOSTS: {
|
|
210
|
+
readonly production: 'https://api.push.apple.com';
|
|
211
|
+
readonly sandbox: 'https://api.sandbox.push.apple.com';
|
|
212
|
+
};
|
|
213
|
+
/** Default provider-token lifetime: inside Apple's 20 to 60 minute window with margin. */
|
|
214
|
+
export declare const APNS_TOKEN_TTL_MS: number;
|
|
215
|
+
/** The part of `http2.ClientHttp2Session` the provider uses (injectable for tests). */
|
|
216
|
+
interface ApnsSession {
|
|
217
|
+
request(headers: http2.OutgoingHttpHeaders): http2.ClientHttp2Stream;
|
|
218
|
+
close(): void;
|
|
219
|
+
ref(): void;
|
|
220
|
+
unref(): void;
|
|
221
|
+
readonly closed: boolean;
|
|
222
|
+
readonly destroyed: boolean;
|
|
223
|
+
on(event: 'error' | 'goaway' | 'close', listener: (...args: unknown[]) => void): unknown;
|
|
224
|
+
}
|
|
225
|
+
interface ApnsProviderOptions extends ApsOptions {
|
|
226
|
+
/** Signing key + ids + bundle id + environment, or a function returning the current ones. */
|
|
227
|
+
readonly credentials: CredentialsSource<ApnsCredentials>;
|
|
228
|
+
/** Dead-token hook (delete the device row). */
|
|
229
|
+
readonly onInvalidToken?: InvalidTokenHandler;
|
|
230
|
+
/** One structured, redacted event per send (opt-in delivery log / metrics). */
|
|
231
|
+
readonly onDelivery?: PushDeliveryListener;
|
|
232
|
+
/** Provider-token reuse window. Default 50 min; must be within 20 to 60 min. */
|
|
233
|
+
readonly tokenTtlMs?: number;
|
|
234
|
+
/** Per-request timeout. Default 10 000 ms. */
|
|
235
|
+
readonly requestTimeoutMs?: number;
|
|
236
|
+
/** Max streams in flight from this provider instance. Default 500 (Apple allows ~1000 per connection). */
|
|
237
|
+
readonly maxConcurrency?: number;
|
|
238
|
+
/** Override the APNs origin (tests). Wins over `credentials.environment`. */
|
|
239
|
+
readonly host?: string;
|
|
240
|
+
/** Permit an `http://` (h2c) host. Tests only. */
|
|
241
|
+
readonly allowPlaintext?: boolean;
|
|
242
|
+
/** Extra TLS / session options for `http2.connect` (a test CA, a proxy agent). */
|
|
243
|
+
readonly connectOptions?: http2.SecureClientSessionOptions;
|
|
244
|
+
/** Injected connector (tests). Default `http2.connect`. */
|
|
245
|
+
readonly connect?: (authority: string, options?: http2.SecureClientSessionOptions) => ApnsSession;
|
|
246
|
+
/** Clock (ms). Default `Date.now`. */
|
|
247
|
+
readonly now?: () => number;
|
|
248
|
+
}
|
|
249
|
+
/** The APNs JSON payload for a message. Exported for tests and payload inspection. */
|
|
250
|
+
export declare function buildApnsPayload(message: PushProviderMessage, options: ApsOptions): Record<string, unknown>;
|
|
251
|
+
export declare function apnsProvider(options: ApnsProviderOptions): PushProvider & {
|
|
252
|
+
close(): Promise<void>;
|
|
253
|
+
};
|
|
254
|
+
//#endregion
|
|
255
|
+
//#region src/push/router.d.ts
|
|
256
|
+
interface PushRouterOptions {
|
|
257
|
+
readonly android?: PushProvider;
|
|
258
|
+
readonly ios?: PushProvider;
|
|
259
|
+
readonly web?: PushProvider;
|
|
260
|
+
/** Used when a message carries no `platform`. Omit to reject such messages. */
|
|
261
|
+
readonly defaultPlatform?: PushPlatform;
|
|
262
|
+
}
|
|
263
|
+
interface PushRouter extends PushProvider {
|
|
264
|
+
/** Platforms with a provider. */
|
|
265
|
+
readonly platforms: readonly PushPlatform[];
|
|
266
|
+
/** Close every distinct provider that has `close()` (APNs sessions). */
|
|
267
|
+
close(): Promise<void>;
|
|
268
|
+
}
|
|
269
|
+
export declare function pushRouter(options: PushRouterOptions): PushRouter;
|
|
270
|
+
//#endregion
|
|
271
|
+
//#region src/push/payload.d.ts
|
|
272
|
+
/**
|
|
273
|
+
* Opaque id or enum: letters, digits, `_ - . :`, 1 to 64 characters (`task_offer`,
|
|
274
|
+
* `order.created`, an ObjectId, a UUID). No spaces, so no sentence or name fits.
|
|
275
|
+
*/
|
|
276
|
+
export declare const OPAQUE_ID: RegExp;
|
|
277
|
+
interface ContentFreeOptions {
|
|
278
|
+
/** Data keys a push may carry besides `type` (ids and enums, never personal data). */
|
|
279
|
+
readonly allowedKeys: readonly string[];
|
|
280
|
+
/** Allowed `type` values. Omit to accept any value matching `valuePattern`. */
|
|
281
|
+
readonly types?: readonly string[];
|
|
282
|
+
/** Pattern every data value must match. Default {@link OPAQUE_ID}. */
|
|
283
|
+
readonly valuePattern?: RegExp;
|
|
284
|
+
}
|
|
285
|
+
/** What the builder returns: spread it into a `PushProviderMessage` with the device token. */
|
|
286
|
+
interface ContentFreeMessage {
|
|
287
|
+
readonly title: string;
|
|
288
|
+
readonly body: '';
|
|
289
|
+
readonly data: Record<string, string>;
|
|
290
|
+
/** True when there is no title: a data-only (background) push. */
|
|
291
|
+
readonly contentAvailable: boolean;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Build content-free messages. `title` should be GENERIC ("New task offered to you"), already in
|
|
295
|
+
* the device's language; omit it for a silent data-only push.
|
|
296
|
+
*/
|
|
297
|
+
export declare function createContentFreeBuilder(options: ContentFreeOptions): (input: {
|
|
298
|
+
type: string;
|
|
299
|
+
refs?: Record<string, string | undefined>;
|
|
300
|
+
title?: string;
|
|
301
|
+
}) => ContentFreeMessage;
|
|
302
|
+
/**
|
|
303
|
+
* Wrap a provider so ONLY content-free messages pass: empty body, no image, data limited to the
|
|
304
|
+
* allowed keys and opaque values. Anything else is a permanent `PushRejectedError` before any
|
|
305
|
+
* network call.
|
|
306
|
+
*/
|
|
307
|
+
export declare function contentFreeOnly(provider: PushProvider, options: ContentFreeOptions): PushProvider;
|
|
308
|
+
//#endregion
|
|
309
|
+
//#region src/push/retry.d.ts
|
|
310
|
+
/**
|
|
311
|
+
* Push retry timing: provider minimums, `Retry-After`, exponential backoff with jitter, a give-up
|
|
312
|
+
* window and the notification's own expiry. Pure functions: nothing here sleeps. The caller (a job
|
|
313
|
+
* queue, arc-notifications' per-device retry job) schedules the next attempt at the returned delay.
|
|
314
|
+
*
|
|
315
|
+
* Sources (ADR 0001 section 5.3, arc-ecosystem `docs/adr/0001-notifications-and-push.md`):
|
|
316
|
+
* - FCM "Scale FCM" [S3]: first retry at least 10 s; on 429 wait `Retry-After`, else 60 s;
|
|
317
|
+
* exponential after that; stop after 60 min of continuous failure; drop requests that are no
|
|
318
|
+
* longer timely. FCM ErrorCode [S2]: "minimum initial delay of 1 minute" for quota (429).
|
|
319
|
+
* - APNs "Handling notification responses" [A3]: 5xx "After 15 minutes, you can retry";
|
|
320
|
+
* 429 `TooManyRequests` "You can retry with a delay" (the 60 s floor is our value).
|
|
321
|
+
* - Network errors and timeouts: 10 s floor (our value, same as FCM's minimum).
|
|
322
|
+
*
|
|
323
|
+
* @module @classytic/notifications/push
|
|
324
|
+
*/
|
|
325
|
+
/** The numbers behind every push retry decision. Exported so hosts and docs quote one table. */
|
|
326
|
+
export declare const PUSH_RETRY_DEFAULTS: {
|
|
327
|
+
/** FCM: first retry not sooner than 10 s (5xx, network, timeout) [S3]. */
|
|
328
|
+
readonly fcmMinDelayMs: 10000;
|
|
329
|
+
/** FCM 429: `Retry-After`, else 60 s; quota errors need a 1 minute initial delay [S2][S3]. */
|
|
330
|
+
readonly fcmQuotaDelayMs: 60000;
|
|
331
|
+
/** FCM: stop after 60 minutes of continuous failure [S3]. */
|
|
332
|
+
readonly fcmGiveUpAfterMs: 3600000;
|
|
333
|
+
/** APNs 500 / 503: retry after 15 minutes [A3]. */
|
|
334
|
+
readonly apnsServerErrorDelayMs: 900000;
|
|
335
|
+
/** APNs 429 (`TooManyRequests`, `TooManyProviderTokenUpdates`): 60 s (our value) [A3]. */
|
|
336
|
+
readonly apnsTooManyRequestsDelayMs: 60000;
|
|
337
|
+
/** Network failure, timeout, credentials store down, unknown provider: 10 s (our value). */
|
|
338
|
+
readonly networkDelayMs: 10000;
|
|
339
|
+
/** Attempts per device, the first send included (ADR 5.3). */
|
|
340
|
+
readonly maxAttempts: 5;
|
|
341
|
+
/** Backoff growth stops here (a floor or Retry-After above it still wins). */
|
|
342
|
+
readonly maxDelayMs: 3600000;
|
|
343
|
+
/** Upward jitter: a delay is stretched by up to this fraction, never shortened. */
|
|
344
|
+
readonly jitter: 0.2;
|
|
345
|
+
};
|
|
346
|
+
/**
|
|
347
|
+
* The provider's minimum wait before retrying a transient failure with this HTTP status
|
|
348
|
+
* (`0` = network failure / timeout / no request made). `TransientPushError` fills
|
|
349
|
+
* `retryFloorMs` from this table unless the throw site passes its own.
|
|
350
|
+
*/
|
|
351
|
+
export declare function pushRetryFloorMs(provider: string, status: number): number;
|
|
352
|
+
interface PushRetryDelayOptions {
|
|
353
|
+
/** `[0, 1)` source for jitter. Default `Math.random` (tests pass a constant). */
|
|
354
|
+
readonly random?: () => number;
|
|
355
|
+
/** Cap on exponential growth. Default 60 min. A floor or `Retry-After` above it still wins. */
|
|
356
|
+
readonly maxDelayMs?: number;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* How long to wait before retry number `attempt` (1 = the first retry, after one failed send):
|
|
360
|
+
* `max(Retry-After, provider floor) x 2^(attempt - 1)`, capped at `maxDelayMs`, then stretched by
|
|
361
|
+
* up to 20 % jitter. Jitter only ever ADDS: the result is never below the provider minimum or the
|
|
362
|
+
* provider's `Retry-After`.
|
|
363
|
+
*/
|
|
364
|
+
export declare function pushRetryDelay(error: unknown, attempt: number, options?: PushRetryDelayOptions): number;
|
|
365
|
+
interface PushRetryInput extends PushRetryDelayOptions {
|
|
366
|
+
/** The error the last attempt threw. */
|
|
367
|
+
readonly error: unknown;
|
|
368
|
+
/** Attempts made so far for this message and device (1 after the first failed send). */
|
|
369
|
+
readonly attempt: number;
|
|
370
|
+
/** When the first attempt failed (ms epoch): the FCM 60 min window counts from here. */
|
|
371
|
+
readonly firstFailureAt: number;
|
|
372
|
+
/** Now (ms epoch). */
|
|
373
|
+
readonly now: number;
|
|
374
|
+
/** The notification's expiry (ms epoch). A retry that would land at or after it is dropped. */
|
|
375
|
+
readonly expiresAt?: number;
|
|
376
|
+
/** Total attempts allowed, the first send included. Default 5. */
|
|
377
|
+
readonly maxAttempts?: number;
|
|
378
|
+
/**
|
|
379
|
+
* Stop once this long has passed since `firstFailureAt`. Default: 60 min for FCM errors [S3],
|
|
380
|
+
* none for APNs (bounded by `expiresAt` and `maxAttempts`).
|
|
381
|
+
*/
|
|
382
|
+
readonly giveUpAfterMs?: number;
|
|
383
|
+
}
|
|
384
|
+
type PushRetryDecision = {
|
|
385
|
+
readonly retry: true;
|
|
386
|
+
readonly delayMs: number;
|
|
387
|
+
readonly at: number;
|
|
388
|
+
} | {
|
|
389
|
+
readonly retry: false;
|
|
390
|
+
/**
|
|
391
|
+
* `permanent`: not retryable (dead token, rejected payload, credentials);
|
|
392
|
+
* `attempts`: `maxAttempts` used; `expired`: the next attempt would be past `expiresAt`;
|
|
393
|
+
* `gave-up`: past the provider's continuous-failure window (FCM 60 min).
|
|
394
|
+
*/
|
|
395
|
+
readonly reason: 'permanent' | 'attempts' | 'expired' | 'gave-up';
|
|
396
|
+
};
|
|
397
|
+
/**
|
|
398
|
+
* Decide whether, and when, to retry one push to one device. Never sleeps; the caller schedules a
|
|
399
|
+
* job at `delayMs`. Order: permanent errors stop, then the attempt budget, then the timing check
|
|
400
|
+
* against the give-up window and the notification's expiry.
|
|
401
|
+
*/
|
|
402
|
+
export declare function planPushRetry(input: PushRetryInput): PushRetryDecision;
|
|
403
|
+
//#endregion
|
|
404
|
+
//#region src/push/jwt.d.ts
|
|
405
|
+
type JwtAlgorithm = 'RS256' | 'ES256';
|
|
406
|
+
/**
|
|
407
|
+
* Sign `header.claims` with `key`. ES256 signatures are emitted in the JOSE `r||s` form
|
|
408
|
+
* (`ieee-p1363`), which is what Apple requires; DER would be rejected.
|
|
409
|
+
*/
|
|
410
|
+
export declare function signJwt(alg: JwtAlgorithm, key: KeyObject, claims: Record<string, unknown>, headerExtra?: Record<string, unknown>): string;
|
|
411
|
+
/** Short, non-reversible fingerprint (hex) of any string: for cache keys and safe log fields. */
|
|
412
|
+
export declare function fingerprint(value: string, length?: number): string;
|
|
413
|
+
//#endregion
|
|
414
|
+
export type { ApnsCredentials, ApnsProviderOptions, ApnsSession, ApsOptions, ContentFreeMessage, ContentFreeOptions, CredentialsSource, FcmCredentials, FcmProviderOptions, InvalidTokenHandler, InvalidTokenReason, JwtAlgorithm, PushDeliveryEvent, PushDeliveryListener, PushInterruptionLevel, PushPlatform, PushProvider, PushProviderMessage, PushProviderName, PushRetryDecision, PushRetryDelayOptions, PushRetryInput, PushRouter, PushRouterOptions };
|