@classytic/notifications 2.7.0 → 2.9.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 +75 -0
- package/README.md +90 -1
- 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 +5 -4
- package/dist/index.mjs +3 -3
- package/dist/providers/index.d.mts +13 -0
- package/dist/providers/index.mjs +33 -7
- package/dist/push/index.d.mts +310 -0
- package/dist/push/index.mjs +1004 -0
- package/dist/{status-webhook-D7Hrr71A.mjs → status-webhook-D1lNssTZ.mjs} +20 -2
- package/dist/{status-webhook-BlhPvRnD.d.mts → status-webhook-Pg_jRXwp.d.mts} +8 -38
- package/dist/{telegram.channel-DnVdyq8_.mjs → telegram.channel-0XRkIXmt.mjs} +37 -3
- package/dist/{telegram.channel-CzQH7Wa8.d.mts → telegram.channel-8DKyn1og.d.mts} +1 -1
- package/dist/{types-DyUMfkyr.d.mts → types-BNjSzlcY.d.mts} +58 -18
- package/dist/utils/index.d.mts +4 -3
- package/dist/utils/index.mjs +2 -2
- package/package.json +9 -3
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { C as PushProviderMessage, S as PushProvider, b as PushInterruptionLevel, x as PushPlatform } from "../types-BNjSzlcY.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`).
|
|
72
|
+
*/
|
|
73
|
+
export declare class TransientPushError extends NotificationError {
|
|
74
|
+
readonly retryable: true;
|
|
75
|
+
readonly provider: PushProviderName;
|
|
76
|
+
/** HTTP status, or 0 for a network-level failure. */
|
|
77
|
+
readonly status: number;
|
|
78
|
+
readonly reason: string;
|
|
79
|
+
readonly retryAfterMs?: number;
|
|
80
|
+
constructor(provider: PushProviderName, status: number, reason: string, options?: {
|
|
81
|
+
retryAfterMs?: number;
|
|
82
|
+
cause?: Error;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/** Structural check: is this an invalid-token error (from any copy of this package)? */
|
|
86
|
+
export declare function isInvalidPushTokenError(err: unknown): err is InvalidPushTokenError;
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/push/credentials.d.ts
|
|
89
|
+
/** A credential value, or a (possibly async) function returning the current one. */
|
|
90
|
+
type CredentialsSource<T> = T | (() => T | Promise<T>);
|
|
91
|
+
/** Google service-account credentials for FCM HTTP v1. */
|
|
92
|
+
interface FcmCredentials {
|
|
93
|
+
/** Firebase / Google Cloud project id (`project_id`). */
|
|
94
|
+
projectId: string;
|
|
95
|
+
/** Service-account email (`client_email`). */
|
|
96
|
+
clientEmail: string;
|
|
97
|
+
/** PKCS#8 RSA private key, PEM (`private_key`). Literal `\n` escapes are accepted. */
|
|
98
|
+
privateKey: string;
|
|
99
|
+
/** `private_key_id`: sent as the JWT `kid` when present. */
|
|
100
|
+
privateKeyId?: string;
|
|
101
|
+
/** OAuth2 token endpoint (`token_uri`). Default `https://oauth2.googleapis.com/token`. */
|
|
102
|
+
tokenUri?: string;
|
|
103
|
+
/** Opaque version/etag. When it changes, the provider re-imports the key. */
|
|
104
|
+
version?: string;
|
|
105
|
+
}
|
|
106
|
+
/** APNs token-based authentication (`.p8` signing key). */
|
|
107
|
+
interface ApnsCredentials {
|
|
108
|
+
/** 10-character key id from the Apple developer account. */
|
|
109
|
+
keyId: string;
|
|
110
|
+
/** 10-character team id. */
|
|
111
|
+
teamId: string;
|
|
112
|
+
/** The `.p8` file contents (PKCS#8 EC P-256 private key, PEM). Literal `\n` escapes are accepted. */
|
|
113
|
+
privateKey: string;
|
|
114
|
+
/** The app's bundle id, sent as `apns-topic`. */
|
|
115
|
+
bundleId: string;
|
|
116
|
+
/** Which APNs host: `production` (default) or `sandbox` (development builds). */
|
|
117
|
+
environment?: 'production' | 'sandbox';
|
|
118
|
+
/** Opaque version/etag. When it changes, the provider re-signs and reconnects if the host moved. */
|
|
119
|
+
version?: string;
|
|
120
|
+
}
|
|
121
|
+
export declare const GOOGLE_TOKEN_URI = "https://oauth2.googleapis.com/token";
|
|
122
|
+
/** `\n` escapes (an env var or a JSON copy) become real newlines; surrounding space is dropped. */
|
|
123
|
+
export declare function normalizePem(pem: string): string;
|
|
124
|
+
/**
|
|
125
|
+
* Accept a Google service-account file in any of the forms hosts store it: the JSON object
|
|
126
|
+
* (snake_case as downloaded, or camelCase), its JSON text, or the base64 of that text.
|
|
127
|
+
*/
|
|
128
|
+
export declare function parseFcmServiceAccount(input: string | Record<string, unknown>, version?: string): FcmCredentials;
|
|
129
|
+
/** Check shape and key type; returns a normalized copy. Throws {@link PushCredentialsError}. */
|
|
130
|
+
export declare function validateFcmCredentials(creds: FcmCredentials): FcmCredentials;
|
|
131
|
+
/** Check shape and key type (EC P-256); returns a normalized copy. Throws {@link PushCredentialsError}. */
|
|
132
|
+
export declare function validateApnsCredentials(creds: ApnsCredentials): ApnsCredentials;
|
|
133
|
+
/** A stable cache key for a credential value: its `version`, else a hash of every field. */
|
|
134
|
+
export declare function credentialsFingerprint(creds: object): string;
|
|
135
|
+
//#endregion
|
|
136
|
+
//#region src/push/internal.d.ts
|
|
137
|
+
/** Called when a token is declared dead: delete the device row here. Errors are swallowed. */
|
|
138
|
+
type InvalidTokenHandler = (token: string, reason: InvalidTokenReason) => void | Promise<void>;
|
|
139
|
+
/**
|
|
140
|
+
* One structured record per send attempt, for an opt-in delivery log or metrics. Holds only what
|
|
141
|
+
* is safe to persist: a token SUFFIX and HASH (never the token), status codes, latency. No title,
|
|
142
|
+
* body, or data.
|
|
143
|
+
*/
|
|
144
|
+
interface PushDeliveryEvent {
|
|
145
|
+
readonly provider: PushProviderName;
|
|
146
|
+
readonly platform?: PushPlatform;
|
|
147
|
+
readonly outcome: 'sent' | 'invalid-token' | 'rejected' | 'transient' | 'credentials';
|
|
148
|
+
/** HTTP status (0 = network failure or no request made). */
|
|
149
|
+
readonly status: number;
|
|
150
|
+
/** Provider reason code (`Unregistered`, `UNAVAILABLE`, ...). */
|
|
151
|
+
readonly reason?: string;
|
|
152
|
+
/** Provider message id on success (`apns-id`, FCM message name). */
|
|
153
|
+
readonly messageId?: string;
|
|
154
|
+
/** Last 6 characters of the device token. */
|
|
155
|
+
readonly tokenSuffix: string;
|
|
156
|
+
/** sha256 prefix of the device token: joins events for one device without storing it. */
|
|
157
|
+
readonly tokenHash: string;
|
|
158
|
+
readonly latencyMs: number;
|
|
159
|
+
readonly retryAfterMs?: number;
|
|
160
|
+
}
|
|
161
|
+
type PushDeliveryListener = (event: PushDeliveryEvent) => void;
|
|
162
|
+
/** Last 6 characters, for messages and logs. */
|
|
163
|
+
export declare function tokenSuffix(token: string): string;
|
|
164
|
+
/** Non-reversible token hash for joining log rows. */
|
|
165
|
+
export declare function tokenHash(token: string): string;
|
|
166
|
+
/**
|
|
167
|
+
* `Retry-After` as milliseconds: delta-seconds or an HTTP-date. Unparseable or negative values
|
|
168
|
+
* are ignored. Capped at one hour so a hostile or broken header cannot park a queue for days.
|
|
169
|
+
*/
|
|
170
|
+
export declare function parseRetryAfter(value: string | string[] | null | undefined, now: number): number | undefined;
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region src/push/fcm.d.ts
|
|
173
|
+
export declare const FCM_ENDPOINT = "https://fcm.googleapis.com";
|
|
174
|
+
export declare const FCM_SCOPE = "https://www.googleapis.com/auth/firebase.messaging";
|
|
175
|
+
interface FcmProviderOptions extends ApsOptions {
|
|
176
|
+
/** Service-account credentials, or a function returning the current ones (re-read per send). */
|
|
177
|
+
readonly credentials: CredentialsSource<FcmCredentials>;
|
|
178
|
+
/** Dead-token hook (delete the device row). */
|
|
179
|
+
readonly onInvalidToken?: InvalidTokenHandler;
|
|
180
|
+
/** One structured, redacted event per send (opt-in delivery log / metrics). */
|
|
181
|
+
readonly onDelivery?: PushDeliveryListener;
|
|
182
|
+
/** FCM origin. Default `https://fcm.googleapis.com` (tests point it at a local server). */
|
|
183
|
+
readonly endpoint?: string;
|
|
184
|
+
/** Request timeout per HTTP call. Default 10 000 ms. */
|
|
185
|
+
readonly timeoutMs?: number;
|
|
186
|
+
/** Max sends in flight from this provider instance. Default 100. */
|
|
187
|
+
readonly maxConcurrency?: number;
|
|
188
|
+
/** Ask FCM to validate without delivering (`validate_only`). For a "send test" dry run. */
|
|
189
|
+
readonly validateOnly?: boolean;
|
|
190
|
+
/** Injected `fetch` (tests, proxies). Default `globalThis.fetch`. */
|
|
191
|
+
readonly fetch?: typeof fetch;
|
|
192
|
+
/** Clock (ms). Default `Date.now`. */
|
|
193
|
+
readonly now?: () => number;
|
|
194
|
+
}
|
|
195
|
+
/** Build the FCM v1 `message` object. Exported for tests and for hosts that inspect payloads. */
|
|
196
|
+
export declare function buildFcmMessage(message: PushProviderMessage, options: ApsOptions, nowMs: number): Record<string, unknown>;
|
|
197
|
+
export declare function fcmProvider(options: FcmProviderOptions): PushProvider;
|
|
198
|
+
//#endregion
|
|
199
|
+
//#region src/push/apns.d.ts
|
|
200
|
+
export declare const APNS_HOSTS: {
|
|
201
|
+
readonly production: 'https://api.push.apple.com';
|
|
202
|
+
readonly sandbox: 'https://api.sandbox.push.apple.com';
|
|
203
|
+
};
|
|
204
|
+
/** Default provider-token lifetime: inside Apple's 20 to 60 minute window with margin. */
|
|
205
|
+
export declare const APNS_TOKEN_TTL_MS: number;
|
|
206
|
+
/** The part of `http2.ClientHttp2Session` the provider uses (injectable for tests). */
|
|
207
|
+
interface ApnsSession {
|
|
208
|
+
request(headers: http2.OutgoingHttpHeaders): http2.ClientHttp2Stream;
|
|
209
|
+
close(): void;
|
|
210
|
+
ref(): void;
|
|
211
|
+
unref(): void;
|
|
212
|
+
readonly closed: boolean;
|
|
213
|
+
readonly destroyed: boolean;
|
|
214
|
+
on(event: 'error' | 'goaway' | 'close', listener: (...args: unknown[]) => void): unknown;
|
|
215
|
+
}
|
|
216
|
+
interface ApnsProviderOptions extends ApsOptions {
|
|
217
|
+
/** Signing key + ids + bundle id + environment, or a function returning the current ones. */
|
|
218
|
+
readonly credentials: CredentialsSource<ApnsCredentials>;
|
|
219
|
+
/** Dead-token hook (delete the device row). */
|
|
220
|
+
readonly onInvalidToken?: InvalidTokenHandler;
|
|
221
|
+
/** One structured, redacted event per send (opt-in delivery log / metrics). */
|
|
222
|
+
readonly onDelivery?: PushDeliveryListener;
|
|
223
|
+
/** Provider-token reuse window. Default 50 min; must be within 20 to 60 min. */
|
|
224
|
+
readonly tokenTtlMs?: number;
|
|
225
|
+
/** Per-request timeout. Default 10 000 ms. */
|
|
226
|
+
readonly requestTimeoutMs?: number;
|
|
227
|
+
/** Max streams in flight from this provider instance. Default 500 (Apple allows ~1000 per connection). */
|
|
228
|
+
readonly maxConcurrency?: number;
|
|
229
|
+
/** Override the APNs origin (tests). Wins over `credentials.environment`. */
|
|
230
|
+
readonly host?: string;
|
|
231
|
+
/** Permit an `http://` (h2c) host. Tests only. */
|
|
232
|
+
readonly allowPlaintext?: boolean;
|
|
233
|
+
/** Extra TLS / session options for `http2.connect` (a test CA, a proxy agent). */
|
|
234
|
+
readonly connectOptions?: http2.SecureClientSessionOptions;
|
|
235
|
+
/** Injected connector (tests). Default `http2.connect`. */
|
|
236
|
+
readonly connect?: (authority: string, options?: http2.SecureClientSessionOptions) => ApnsSession;
|
|
237
|
+
/** Clock (ms). Default `Date.now`. */
|
|
238
|
+
readonly now?: () => number;
|
|
239
|
+
}
|
|
240
|
+
/** The APNs JSON payload for a message. Exported for tests and payload inspection. */
|
|
241
|
+
export declare function buildApnsPayload(message: PushProviderMessage, options: ApsOptions): Record<string, unknown>;
|
|
242
|
+
export declare function apnsProvider(options: ApnsProviderOptions): PushProvider & {
|
|
243
|
+
close(): Promise<void>;
|
|
244
|
+
};
|
|
245
|
+
//#endregion
|
|
246
|
+
//#region src/push/router.d.ts
|
|
247
|
+
interface PushRouterOptions {
|
|
248
|
+
readonly android?: PushProvider;
|
|
249
|
+
readonly ios?: PushProvider;
|
|
250
|
+
readonly web?: PushProvider;
|
|
251
|
+
/** Used when a message carries no `platform`. Omit to reject such messages. */
|
|
252
|
+
readonly defaultPlatform?: PushPlatform;
|
|
253
|
+
}
|
|
254
|
+
interface PushRouter extends PushProvider {
|
|
255
|
+
/** Platforms with a provider. */
|
|
256
|
+
readonly platforms: readonly PushPlatform[];
|
|
257
|
+
/** Close every distinct provider that has `close()` (APNs sessions). */
|
|
258
|
+
close(): Promise<void>;
|
|
259
|
+
}
|
|
260
|
+
export declare function pushRouter(options: PushRouterOptions): PushRouter;
|
|
261
|
+
//#endregion
|
|
262
|
+
//#region src/push/payload.d.ts
|
|
263
|
+
/**
|
|
264
|
+
* Opaque id or enum: letters, digits, `_ - . :`, 1 to 64 characters (`task_offer`,
|
|
265
|
+
* `order.created`, an ObjectId, a UUID). No spaces, so no sentence or name fits.
|
|
266
|
+
*/
|
|
267
|
+
export declare const OPAQUE_ID: RegExp;
|
|
268
|
+
interface ContentFreeOptions {
|
|
269
|
+
/** Data keys a push may carry besides `type` (ids and enums, never personal data). */
|
|
270
|
+
readonly allowedKeys: readonly string[];
|
|
271
|
+
/** Allowed `type` values. Omit to accept any value matching `valuePattern`. */
|
|
272
|
+
readonly types?: readonly string[];
|
|
273
|
+
/** Pattern every data value must match. Default {@link OPAQUE_ID}. */
|
|
274
|
+
readonly valuePattern?: RegExp;
|
|
275
|
+
}
|
|
276
|
+
/** What the builder returns: spread it into a `PushProviderMessage` with the device token. */
|
|
277
|
+
interface ContentFreeMessage {
|
|
278
|
+
readonly title: string;
|
|
279
|
+
readonly body: '';
|
|
280
|
+
readonly data: Record<string, string>;
|
|
281
|
+
/** True when there is no title: a data-only (background) push. */
|
|
282
|
+
readonly contentAvailable: boolean;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Build content-free messages. `title` should be GENERIC ("New task offered to you"), already in
|
|
286
|
+
* the device's language; omit it for a silent data-only push.
|
|
287
|
+
*/
|
|
288
|
+
export declare function createContentFreeBuilder(options: ContentFreeOptions): (input: {
|
|
289
|
+
type: string;
|
|
290
|
+
refs?: Record<string, string | undefined>;
|
|
291
|
+
title?: string;
|
|
292
|
+
}) => ContentFreeMessage;
|
|
293
|
+
/**
|
|
294
|
+
* Wrap a provider so ONLY content-free messages pass: empty body, no image, data limited to the
|
|
295
|
+
* allowed keys and opaque values. Anything else is a permanent `PushRejectedError` before any
|
|
296
|
+
* network call.
|
|
297
|
+
*/
|
|
298
|
+
export declare function contentFreeOnly(provider: PushProvider, options: ContentFreeOptions): PushProvider;
|
|
299
|
+
//#endregion
|
|
300
|
+
//#region src/push/jwt.d.ts
|
|
301
|
+
type JwtAlgorithm = 'RS256' | 'ES256';
|
|
302
|
+
/**
|
|
303
|
+
* Sign `header.claims` with `key`. ES256 signatures are emitted in the JOSE `r||s` form
|
|
304
|
+
* (`ieee-p1363`), which is what Apple requires; DER would be rejected.
|
|
305
|
+
*/
|
|
306
|
+
export declare function signJwt(alg: JwtAlgorithm, key: KeyObject, claims: Record<string, unknown>, headerExtra?: Record<string, unknown>): string;
|
|
307
|
+
/** Short, non-reversible fingerprint (hex) of any string: for cache keys and safe log fields. */
|
|
308
|
+
export declare function fingerprint(value: string, length?: number): string;
|
|
309
|
+
//#endregion
|
|
310
|
+
export type { ApnsCredentials, ApnsProviderOptions, ApnsSession, ApsOptions, ContentFreeMessage, ContentFreeOptions, CredentialsSource, FcmCredentials, FcmProviderOptions, InvalidTokenHandler, InvalidTokenReason, JwtAlgorithm, PushDeliveryEvent, PushDeliveryListener, PushInterruptionLevel, PushPlatform, PushProvider, PushProviderMessage, PushProviderName, PushRouter, PushRouterOptions };
|