@serfab/cadre-core 0.7.1 → 0.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/README.md +11 -10
- package/dist/cadre-node.d.ts +818 -19
- package/dist/cadre-node.js +1952 -99
- package/dist/cadre-node.js.map +1 -1
- package/dist/canonical-datetime.d.ts +33 -0
- package/dist/canonical-datetime.js +38 -0
- package/dist/canonical-datetime.js.map +1 -0
- package/dist/canonical-json.d.ts +12 -0
- package/dist/canonical-json.js +33 -0
- package/dist/canonical-json.js.map +1 -0
- package/dist/control-cohort.d.ts +63 -0
- package/dist/control-cohort.js +80 -0
- package/dist/control-cohort.js.map +1 -0
- package/dist/control-database.d.ts +279 -8
- package/dist/control-database.js +563 -134
- package/dist/control-database.js.map +1 -1
- package/dist/control-formation-recorder.d.ts +89 -0
- package/dist/control-formation-recorder.js +139 -0
- package/dist/control-formation-recorder.js.map +1 -0
- package/dist/control-schema.d.ts +12 -0
- package/dist/control-schema.js +223 -0
- package/dist/control-schema.js.map +1 -0
- package/dist/control-stream.d.ts +54 -0
- package/dist/control-stream.js +104 -0
- package/dist/control-stream.js.map +1 -0
- package/dist/device-token.d.ts +50 -0
- package/dist/device-token.js +66 -0
- package/dist/device-token.js.map +1 -0
- package/dist/diagnostics/connection-path.d.ts +118 -0
- package/dist/diagnostics/connection-path.js +204 -0
- package/dist/diagnostics/connection-path.js.map +1 -0
- package/dist/diagnostics/webrtc-turn-tracker.d.ts +50 -0
- package/dist/diagnostics/webrtc-turn-tracker.js +196 -0
- package/dist/diagnostics/webrtc-turn-tracker.js.map +1 -0
- package/dist/ed25519-key.d.ts +42 -0
- package/dist/ed25519-key.js +51 -0
- package/dist/ed25519-key.js.map +1 -0
- package/dist/hibernation-manager.d.ts +79 -0
- package/dist/hibernation-manager.js +173 -23
- package/dist/hibernation-manager.js.map +1 -1
- package/dist/index.d.ts +25 -4
- package/dist/index.js +59 -4
- package/dist/index.js.map +1 -1
- package/dist/key-store-file.d.ts +52 -0
- package/dist/key-store-file.js +194 -0
- package/dist/key-store-file.js.map +1 -0
- package/dist/key-store.d.ts +75 -0
- package/dist/key-store.js +66 -0
- package/dist/key-store.js.map +1 -0
- package/dist/peer-authorization.d.ts +48 -0
- package/dist/peer-authorization.js +66 -0
- package/dist/peer-authorization.js.map +1 -0
- package/dist/peer-record.d.ts +89 -0
- package/dist/peer-record.js +117 -0
- package/dist/peer-record.js.map +1 -0
- package/dist/push-credentials.d.ts +34 -0
- package/dist/push-credentials.js +81 -0
- package/dist/push-credentials.js.map +1 -0
- package/dist/push-fanout.d.ts +137 -0
- package/dist/push-fanout.js +197 -0
- package/dist/push-fanout.js.map +1 -0
- package/dist/push-node.d.ts +38 -0
- package/dist/push-node.js +48 -0
- package/dist/push-node.js.map +1 -0
- package/dist/push-notifier-apns.d.ts +61 -0
- package/dist/push-notifier-apns.js +199 -0
- package/dist/push-notifier-apns.js.map +1 -0
- package/dist/push-notifier-fcm.d.ts +44 -0
- package/dist/push-notifier-fcm.js +182 -0
- package/dist/push-notifier-fcm.js.map +1 -0
- package/dist/push-notifier-shared.d.ts +18 -0
- package/dist/push-notifier-shared.js +25 -0
- package/dist/push-notifier-shared.js.map +1 -0
- package/dist/push-notifier.d.ts +52 -0
- package/dist/push-notifier.js +22 -0
- package/dist/push-notifier.js.map +1 -0
- package/dist/schema-verification.d.ts +11 -2
- package/dist/schema-verification.js +15 -4
- package/dist/schema-verification.js.map +1 -1
- package/dist/seed-bootstrap.d.ts +228 -21
- package/dist/seed-bootstrap.js +489 -168
- package/dist/seed-bootstrap.js.map +1 -1
- package/dist/seed-trust-policy.d.ts +63 -0
- package/dist/seed-trust-policy.js +56 -0
- package/dist/seed-trust-policy.js.map +1 -0
- package/dist/strand-addr-protocol.d.ts +132 -0
- package/dist/strand-addr-protocol.js +313 -0
- package/dist/strand-addr-protocol.js.map +1 -0
- package/dist/strand-cohort.d.ts +54 -0
- package/dist/strand-cohort.js +38 -0
- package/dist/strand-cohort.js.map +1 -0
- package/dist/strand-database.d.ts +54 -6
- package/dist/strand-database.js +88 -100
- package/dist/strand-database.js.map +1 -1
- package/dist/strand-formation-manager.d.ts +79 -19
- package/dist/strand-formation-manager.js +196 -104
- package/dist/strand-formation-manager.js.map +1 -1
- package/dist/strand-formation-protocol.d.ts +162 -0
- package/dist/strand-formation-protocol.js +279 -0
- package/dist/strand-formation-protocol.js.map +1 -0
- package/dist/strand-instance-manager.d.ts +73 -4
- package/dist/strand-instance-manager.js +167 -41
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/strand-member-key.d.ts +29 -0
- package/dist/strand-member-key.js +38 -0
- package/dist/strand-member-key.js.map +1 -0
- package/dist/strand-member-registry.d.ts +98 -0
- package/dist/strand-member-registry.js +129 -0
- package/dist/strand-member-registry.js.map +1 -0
- package/dist/strand-membership-writer.d.ts +361 -0
- package/dist/strand-membership-writer.js +449 -0
- package/dist/strand-membership-writer.js.map +1 -0
- package/dist/strand-solicitation.d.ts +77 -2
- package/dist/strand-solicitation.js +25 -4
- package/dist/strand-solicitation.js.map +1 -1
- package/dist/strand-wake-payload.d.ts +30 -0
- package/dist/strand-wake-payload.js +20 -0
- package/dist/strand-wake-payload.js.map +1 -0
- package/dist/strand-wake-protocol.d.ts +114 -0
- package/dist/strand-wake-protocol.js +257 -0
- package/dist/strand-wake-protocol.js.map +1 -0
- package/dist/strand-watcher.d.ts +6 -2
- package/dist/strand-watcher.js +57 -17
- package/dist/strand-watcher.js.map +1 -1
- package/dist/types.d.ts +436 -33
- package/dist/types.js +22 -5
- package/dist/types.js.map +1 -1
- package/package.json +18 -8
- package/src/cadre-node.ts +2981 -948
- package/src/canonical-datetime.ts +39 -0
- package/src/canonical-json.ts +30 -0
- package/src/control-cohort.ts +103 -0
- package/src/control-database.ts +948 -383
- package/src/control-formation-recorder.ts +151 -0
- package/src/control-schema.ts +222 -0
- package/src/control-stream.ts +128 -0
- package/src/device-token.ts +89 -0
- package/src/diagnostics/connection-path.ts +277 -0
- package/src/diagnostics/webrtc-turn-tracker.ts +222 -0
- package/src/ed25519-key.ts +69 -0
- package/src/hibernation-manager.ts +197 -28
- package/src/index.ts +275 -79
- package/src/key-store-file.ts +203 -0
- package/src/key-store.ts +99 -0
- package/src/peer-authorization.ts +81 -0
- package/src/peer-record.ts +146 -0
- package/src/push-credentials.ts +84 -0
- package/src/push-fanout.ts +272 -0
- package/src/push-node.ts +64 -0
- package/src/push-notifier-apns.ts +247 -0
- package/src/push-notifier-fcm.ts +220 -0
- package/src/push-notifier-shared.ts +28 -0
- package/src/push-notifier.ts +52 -0
- package/src/schema-verification.ts +18 -4
- package/src/seed-bootstrap.ts +563 -223
- package/src/seed-trust-policy.ts +107 -0
- package/src/strand-addr-protocol.ts +410 -0
- package/src/strand-cohort.ts +77 -0
- package/src/strand-database.ts +113 -141
- package/src/strand-formation-manager.ts +233 -142
- package/src/strand-formation-protocol.ts +391 -0
- package/src/strand-instance-manager.ts +207 -56
- package/src/strand-member-key.ts +39 -0
- package/src/strand-member-registry.ts +177 -0
- package/src/strand-membership-writer.ts +640 -0
- package/src/strand-solicitation.ts +93 -4
- package/src/strand-wake-payload.ts +32 -0
- package/src/strand-wake-protocol.ts +331 -0
- package/src/strand-watcher.ts +61 -17
- package/src/types.ts +1032 -582
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* push-node.ts — Node-only subpath entry for platform push **construction**.
|
|
3
|
+
*
|
|
4
|
+
* Import from the dedicated subpath, never the package root:
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { createPushNotifier } from '@serfab/cadre-core/push-node';
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* The FCM/APNs implementations reach for `node:crypto` / `node:http2`, so they
|
|
11
|
+
* are quarantined here — unreachable from the cross-platform `./index.js` graph
|
|
12
|
+
* (same isolation pattern as `./key-store-file`). A React Native / browser entry
|
|
13
|
+
* never resolves this path, so the Node builtins never reach a bundler that
|
|
14
|
+
* cannot satisfy them. The core references only the {@link PushNotifier}
|
|
15
|
+
* *interface* (`push-notifier.ts`) and a Node host injects the instance built
|
|
16
|
+
* here into `CadreNodeConfig.push.notifier`.
|
|
17
|
+
*/
|
|
18
|
+
import { createFcmPushNotifier } from './push-notifier-fcm.js';
|
|
19
|
+
import { createApnsPushNotifier } from './push-notifier-apns.js';
|
|
20
|
+
/**
|
|
21
|
+
* Build a {@link PushNotifier} router over the configured credentials. Only the
|
|
22
|
+
* platforms whose credentials are present get a backing implementation; a `send`
|
|
23
|
+
* for an unconfigured platform returns a best-effort `no <platform> credentials`
|
|
24
|
+
* failure rather than throwing.
|
|
25
|
+
*/
|
|
26
|
+
export function createPushNotifier(creds, deps = {}) {
|
|
27
|
+
const fcm = creds.fcm ? createFcmPushNotifier(creds.fcm, deps.fcm) : undefined;
|
|
28
|
+
const apns = creds.apns ? createApnsPushNotifier(creds.apns, deps.apns) : undefined;
|
|
29
|
+
async function send(msg) {
|
|
30
|
+
if (msg.platform === 'fcm')
|
|
31
|
+
return fcm ? fcm.send(msg) : noCreds('fcm');
|
|
32
|
+
if (msg.platform === 'apns')
|
|
33
|
+
return apns ? apns.send(msg) : noCreds('apns');
|
|
34
|
+
return { ok: false, unregistered: false, error: `unknown platform ${String(msg.platform)}` };
|
|
35
|
+
}
|
|
36
|
+
async function close() {
|
|
37
|
+
await Promise.all([fcm?.close(), apns?.close()]);
|
|
38
|
+
}
|
|
39
|
+
return { send, close };
|
|
40
|
+
}
|
|
41
|
+
/** Best-effort no-op result for a platform with no configured credentials. */
|
|
42
|
+
function noCreds(platform) {
|
|
43
|
+
return { ok: false, unregistered: false, error: `no ${platform} credentials` };
|
|
44
|
+
}
|
|
45
|
+
// Per-platform builders + their injected-dep types, for a host that wants a
|
|
46
|
+
// single-platform notifier or to inject a fake transport directly.
|
|
47
|
+
export { createFcmPushNotifier, createApnsPushNotifier };
|
|
48
|
+
//# sourceMappingURL=push-node.js.map
|
|
@@ -0,0 +1 @@
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,EAAE,CAAC,CAAC,CAAC;IACnD,CAAC;IAED,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AACzB,CAAC;AAED,8EAA8E;AAC9E,SAAS,OAAO,CAAC,QAAsB;IACrC,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,QAAQ,cAAc,EAAE,CAAC;AACjF,CAAC;AAED,4EAA4E;AAC5E,mEAAmE;AACnE,OAAO,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* push-notifier-apns.ts — APNs HTTP/2 strand-wake delivery.
|
|
3
|
+
*
|
|
4
|
+
* Node-only: signs a provider JWT (ES256, JOSE raw r‖s via `node:crypto`) and
|
|
5
|
+
* sends a background data push over an HTTP/2 session to Apple's gateway. APNs
|
|
6
|
+
* mandates HTTP/2, so `node:http2` is unavoidable here — one reason the push
|
|
7
|
+
* path is injected rather than rebuilt on WebCrypto/`fetch`.
|
|
8
|
+
*
|
|
9
|
+
* Reachable only through the `@serfab/cadre-core/push-node` subpath — never from
|
|
10
|
+
* the cross-platform `./index.js` graph — so the `node:crypto` / `node:http2`
|
|
11
|
+
* imports below are safe: no RN/browser bundler ever resolves this module. (Plain
|
|
12
|
+
* named imports; the old namespace-import-to-only-warn dance is obsolete now that
|
|
13
|
+
* the module is unreachable from a browser build by construction.)
|
|
14
|
+
*
|
|
15
|
+
* The HTTP/2 call is behind an injected `Http2Requester` seam (bundled with a
|
|
16
|
+
* `close` into an {@link ApnsTransport}) so unit tests assert the request shape
|
|
17
|
+
* and map every documented response code with no real network and no credentials.
|
|
18
|
+
* The default transport owns a single lazily-(re)established `node:http2` session.
|
|
19
|
+
*
|
|
20
|
+
* Two single-shot retries guard transient failure without a retry storm: a
|
|
21
|
+
* thrown transport error (session death / GOAWAY mid-send) re-establishes the
|
|
22
|
+
* session and retries the one request once; a 403 `ExpiredProviderToken`
|
|
23
|
+
* re-mints the provider JWT and retries once. A second failure is returned as-is.
|
|
24
|
+
*
|
|
25
|
+
* Secret hygiene: the `.p8` `privateKey`, the minted JWT, and full device tokens
|
|
26
|
+
* are never logged — failure log lines carry only a status/reason and a redacted
|
|
27
|
+
* token prefix.
|
|
28
|
+
*/
|
|
29
|
+
import type { ApnsCredentials } from './types.js';
|
|
30
|
+
import type { PushNotifier } from './push-notifier.js';
|
|
31
|
+
/** Raw HTTP/2 response: APNs `:status` and the (possibly empty) JSON body. */
|
|
32
|
+
export interface ApnsResponse {
|
|
33
|
+
status: number;
|
|
34
|
+
body: string;
|
|
35
|
+
}
|
|
36
|
+
/** A single APNs HTTP/2 request — `:path`, headers, and the JSON body. */
|
|
37
|
+
export interface ApnsRequest {
|
|
38
|
+
/** `:path`, e.g. `/3/device/{token}`. */
|
|
39
|
+
path: string;
|
|
40
|
+
headers: Record<string, string>;
|
|
41
|
+
body: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Low-level HTTP/2 request seam. Resolves with the `(status, body)` pair; rejects
|
|
45
|
+
* (throws) on a session/transport failure so the caller can re-establish + retry.
|
|
46
|
+
*/
|
|
47
|
+
export type Http2Requester = (req: ApnsRequest) => Promise<ApnsResponse>;
|
|
48
|
+
/** The HTTP/2 request seam plus its session-teardown. */
|
|
49
|
+
export interface ApnsTransport {
|
|
50
|
+
request: Http2Requester;
|
|
51
|
+
close(): Promise<void>;
|
|
52
|
+
}
|
|
53
|
+
export interface ApnsPushDeps {
|
|
54
|
+
/** HTTP/2 transport. Defaults to a real `node:http2` session manager. */
|
|
55
|
+
transport?: ApnsTransport;
|
|
56
|
+
/** Monotonic clock (ms). Defaults to `Date.now`. Injected for token-cache tests. */
|
|
57
|
+
now?: () => number;
|
|
58
|
+
/** Failure log seam (default: `debug('sereus:cadre:push:apns')`). */
|
|
59
|
+
log?: (line: string) => void;
|
|
60
|
+
}
|
|
61
|
+
export declare function createApnsPushNotifier(creds: ApnsCredentials, deps?: ApnsPushDeps): PushNotifier;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* push-notifier-apns.ts — APNs HTTP/2 strand-wake delivery.
|
|
3
|
+
*
|
|
4
|
+
* Node-only: signs a provider JWT (ES256, JOSE raw r‖s via `node:crypto`) and
|
|
5
|
+
* sends a background data push over an HTTP/2 session to Apple's gateway. APNs
|
|
6
|
+
* mandates HTTP/2, so `node:http2` is unavoidable here — one reason the push
|
|
7
|
+
* path is injected rather than rebuilt on WebCrypto/`fetch`.
|
|
8
|
+
*
|
|
9
|
+
* Reachable only through the `@serfab/cadre-core/push-node` subpath — never from
|
|
10
|
+
* the cross-platform `./index.js` graph — so the `node:crypto` / `node:http2`
|
|
11
|
+
* imports below are safe: no RN/browser bundler ever resolves this module. (Plain
|
|
12
|
+
* named imports; the old namespace-import-to-only-warn dance is obsolete now that
|
|
13
|
+
* the module is unreachable from a browser build by construction.)
|
|
14
|
+
*
|
|
15
|
+
* The HTTP/2 call is behind an injected `Http2Requester` seam (bundled with a
|
|
16
|
+
* `close` into an {@link ApnsTransport}) so unit tests assert the request shape
|
|
17
|
+
* and map every documented response code with no real network and no credentials.
|
|
18
|
+
* The default transport owns a single lazily-(re)established `node:http2` session.
|
|
19
|
+
*
|
|
20
|
+
* Two single-shot retries guard transient failure without a retry storm: a
|
|
21
|
+
* thrown transport error (session death / GOAWAY mid-send) re-establishes the
|
|
22
|
+
* session and retries the one request once; a 403 `ExpiredProviderToken`
|
|
23
|
+
* re-mints the provider JWT and retries once. A second failure is returned as-is.
|
|
24
|
+
*
|
|
25
|
+
* Secret hygiene: the `.p8` `privateKey`, the minted JWT, and full device tokens
|
|
26
|
+
* are never logged — failure log lines carry only a status/reason and a redacted
|
|
27
|
+
* token prefix.
|
|
28
|
+
*/
|
|
29
|
+
import { sign } from 'node:crypto';
|
|
30
|
+
import { connect } from 'node:http2';
|
|
31
|
+
import debug from 'debug';
|
|
32
|
+
import { MAX_REASON_LEN, b64urlJson, errText, redact } from './push-notifier-shared.js';
|
|
33
|
+
const APNS_PROD_HOST = 'https://api.push.apple.com';
|
|
34
|
+
const APNS_SANDBOX_HOST = 'https://api.sandbox.push.apple.com';
|
|
35
|
+
/** Apple requires the provider token be 20–60 min old; refresh well inside that. */
|
|
36
|
+
const PROVIDER_TOKEN_TTL_MS = 45 * 60000;
|
|
37
|
+
const debugApns = debug('sereus:cadre:push:apns');
|
|
38
|
+
export function createApnsPushNotifier(creds, deps = {}) {
|
|
39
|
+
const host = creds.production ? APNS_PROD_HOST : APNS_SANDBOX_HOST;
|
|
40
|
+
const transport = deps.transport ?? createHttp2Transport(host);
|
|
41
|
+
const now = deps.now ?? (() => Date.now());
|
|
42
|
+
const log = deps.log ?? ((line) => debugApns(line));
|
|
43
|
+
let cached = null;
|
|
44
|
+
async function send(msg) {
|
|
45
|
+
let jwt;
|
|
46
|
+
try {
|
|
47
|
+
jwt = providerToken(false);
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
log(`provider-token mint failed: ${errText(err)}`);
|
|
51
|
+
return { ok: false, unregistered: false, error: `apns jwt mint failed: ${errText(err)}` };
|
|
52
|
+
}
|
|
53
|
+
let res = await tryRequest(msg, jwt);
|
|
54
|
+
if (!res)
|
|
55
|
+
return { ok: false, unregistered: false, error: `apns transport failed (token=${redact(msg.token)})` };
|
|
56
|
+
// Provider token expired mid-flight — re-mint once and retry the single send.
|
|
57
|
+
if (res.status === 403 && /ExpiredProviderToken/i.test(res.body)) {
|
|
58
|
+
try {
|
|
59
|
+
jwt = providerToken(true);
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
log(`provider-token re-mint failed: ${errText(err)}`);
|
|
63
|
+
return { ok: false, unregistered: false, error: `apns jwt re-mint failed: ${errText(err)}` };
|
|
64
|
+
}
|
|
65
|
+
res = await tryRequest(msg, jwt);
|
|
66
|
+
if (!res)
|
|
67
|
+
return { ok: false, unregistered: false, error: `apns transport failed (token=${redact(msg.token)})` };
|
|
68
|
+
}
|
|
69
|
+
return mapResult(res, msg.token);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Send one request with a single re-establish-on-throw retry. A thrown error is
|
|
73
|
+
* a session death / GOAWAY; the default transport recreates its session lazily
|
|
74
|
+
* on the next call, so a second attempt rides a fresh session. Returns `null`
|
|
75
|
+
* when both attempts throw.
|
|
76
|
+
*/
|
|
77
|
+
async function tryRequest(msg, jwt) {
|
|
78
|
+
const req = buildRequest(msg, jwt);
|
|
79
|
+
try {
|
|
80
|
+
return await transport.request(req);
|
|
81
|
+
}
|
|
82
|
+
catch (err1) {
|
|
83
|
+
log(`http2 request failed, re-establishing once: ${errText(err1)}`);
|
|
84
|
+
try {
|
|
85
|
+
return await transport.request(req);
|
|
86
|
+
}
|
|
87
|
+
catch (err2) {
|
|
88
|
+
log(`http2 request failed after re-establish: ${errText(err2)}`);
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function mapResult(res, deviceToken) {
|
|
94
|
+
if (res.status === 200)
|
|
95
|
+
return { ok: true };
|
|
96
|
+
const reason = apnsReason(res.body);
|
|
97
|
+
const summary = `apns ${res.status}${reason ? ` ${reason}` : ''}`;
|
|
98
|
+
if (res.status === 410 || reason === 'Unregistered' || (res.status === 400 && reason === 'BadDeviceToken')) {
|
|
99
|
+
log(`unregistered token=${redact(deviceToken)} (${summary})`);
|
|
100
|
+
return { ok: false, unregistered: true, error: summary };
|
|
101
|
+
}
|
|
102
|
+
log(`send failed token=${redact(deviceToken)} (${summary})`);
|
|
103
|
+
return { ok: false, unregistered: false, error: summary };
|
|
104
|
+
}
|
|
105
|
+
function buildRequest(msg, jwt) {
|
|
106
|
+
const body = JSON.stringify({
|
|
107
|
+
aps: { 'content-available': 1 },
|
|
108
|
+
type: msg.payload.type,
|
|
109
|
+
strandId: msg.payload.strandId,
|
|
110
|
+
reason: msg.payload.reason.slice(0, MAX_REASON_LEN),
|
|
111
|
+
});
|
|
112
|
+
return {
|
|
113
|
+
path: `/3/device/${msg.token}`,
|
|
114
|
+
headers: {
|
|
115
|
+
authorization: `bearer ${jwt}`,
|
|
116
|
+
'apns-topic': creds.bundleId,
|
|
117
|
+
'apns-push-type': 'background',
|
|
118
|
+
'apns-priority': '5',
|
|
119
|
+
'apns-expiration': '0',
|
|
120
|
+
},
|
|
121
|
+
body,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function providerToken(forceRefresh) {
|
|
125
|
+
if (!forceRefresh && cached && cached.expiresAt > now())
|
|
126
|
+
return cached.value;
|
|
127
|
+
const iat = Math.floor(now() / 1000);
|
|
128
|
+
const header = { alg: 'ES256', kid: creds.keyId };
|
|
129
|
+
const claims = { iss: creds.teamId, iat };
|
|
130
|
+
const signingInput = `${b64urlJson(header)}.${b64urlJson(claims)}`;
|
|
131
|
+
const sig = sign('SHA256', Buffer.from(signingInput), {
|
|
132
|
+
key: creds.privateKey,
|
|
133
|
+
dsaEncoding: 'ieee-p1363',
|
|
134
|
+
}).toString('base64url');
|
|
135
|
+
const jwt = `${signingInput}.${sig}`;
|
|
136
|
+
cached = { value: jwt, expiresAt: now() + PROVIDER_TOKEN_TTL_MS };
|
|
137
|
+
return jwt;
|
|
138
|
+
}
|
|
139
|
+
async function close() {
|
|
140
|
+
cached = null;
|
|
141
|
+
await transport.close();
|
|
142
|
+
}
|
|
143
|
+
return { send, close };
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Default {@link ApnsTransport}: a single lazily-(re)established `node:http2`
|
|
147
|
+
* session to Apple's gateway. A session that errors / receives GOAWAY / closes is
|
|
148
|
+
* dropped so the next request re-establishes it. Not exercised by unit tests (no
|
|
149
|
+
* network) — tests inject a fake transport; the real path is validated out-of-band.
|
|
150
|
+
*/
|
|
151
|
+
function createHttp2Transport(host) {
|
|
152
|
+
let session = null;
|
|
153
|
+
function ensureSession() {
|
|
154
|
+
if (session && !session.closed && !session.destroyed)
|
|
155
|
+
return session;
|
|
156
|
+
const s = connect(host);
|
|
157
|
+
// Drop a dead session so ensureSession recreates it on the next request.
|
|
158
|
+
s.on('error', () => { if (session === s)
|
|
159
|
+
session = null; });
|
|
160
|
+
s.on('goaway', () => { s.destroy(); });
|
|
161
|
+
s.on('close', () => { if (session === s)
|
|
162
|
+
session = null; });
|
|
163
|
+
session = s;
|
|
164
|
+
return s;
|
|
165
|
+
}
|
|
166
|
+
async function request(req) {
|
|
167
|
+
const s = ensureSession();
|
|
168
|
+
return await new Promise((resolve, reject) => {
|
|
169
|
+
const stream = s.request({ ':method': 'POST', ':path': req.path, ...req.headers });
|
|
170
|
+
const chunks = [];
|
|
171
|
+
let status = 0;
|
|
172
|
+
stream.on('response', (headers) => { status = Number(headers[':status'] ?? 0); });
|
|
173
|
+
stream.on('data', (chunk) => chunks.push(chunk));
|
|
174
|
+
stream.on('end', () => resolve({ status, body: Buffer.concat(chunks).toString('utf8') }));
|
|
175
|
+
stream.on('error', (err) => reject(err));
|
|
176
|
+
stream.end(req.body);
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
async function close() {
|
|
180
|
+
const s = session;
|
|
181
|
+
session = null;
|
|
182
|
+
if (s)
|
|
183
|
+
await new Promise((resolve) => s.close(() => resolve()));
|
|
184
|
+
}
|
|
185
|
+
return { request, close };
|
|
186
|
+
}
|
|
187
|
+
/** Extract the APNs error `reason` field from a response body, if present. */
|
|
188
|
+
function apnsReason(body) {
|
|
189
|
+
if (!body)
|
|
190
|
+
return null;
|
|
191
|
+
try {
|
|
192
|
+
const parsed = JSON.parse(body);
|
|
193
|
+
return typeof parsed.reason === 'string' ? parsed.reason : null;
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
//# sourceMappingURL=push-notifier-apns.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"push-notifier-apns.js","sourceR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,CAAC;QAClB,OAAO,GAAG,IAAI,CAAC;QACf,IAAI,CAAC;YAAE,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;IACxE,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;AAC5B,CAAC;AAED,8EAA8E;AAC9E,SAAS,UAAU,CAAC,IAAY;IAC9B,IAAI,CAAC,IAAI;QAAE,OAAO,IAAI,CAAC;IACvB,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAyB,CAAC;QACxD,OAAO,OAAO,MAAM,CAAC,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC;IAClE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* push-notifier-fcm.ts — FCM HTTP v1 strand-wake delivery.
|
|
3
|
+
*
|
|
4
|
+
* Node-only: mints a Google OAuth2 access token (RS256 JWT bearer grant signed
|
|
5
|
+
* with `node:crypto`) and POSTs a data message to the FCM v1 endpoint. The legacy
|
|
6
|
+
* server-key `fcm.googleapis.com/fcm/send` API is deprecated, so we use HTTP v1.
|
|
7
|
+
*
|
|
8
|
+
* Reachable only through the `@serfab/cadre-core/push-node` subpath — never from
|
|
9
|
+
* the cross-platform `./index.js` graph — so the `node:crypto` import below is
|
|
10
|
+
* safe: no RN/browser bundler ever resolves this module. (Plain named import;
|
|
11
|
+
* the old namespace-import-to-only-warn dance is obsolete now that the module is
|
|
12
|
+
* unreachable from a browser build by construction.)
|
|
13
|
+
*
|
|
14
|
+
* The network call is behind an injected `fetch`-like seam so unit tests assert
|
|
15
|
+
* the request shape and map every documented response code with no real network
|
|
16
|
+
* and no credentials. Failures are returned as {@link PushSendResult} values.
|
|
17
|
+
*
|
|
18
|
+
* Secret hygiene: the service-account `privateKey`, the minted JWT, and full
|
|
19
|
+
* device tokens are never logged — failure log lines carry only a status/error
|
|
20
|
+
* code and a redacted token prefix.
|
|
21
|
+
*/
|
|
22
|
+
import type { FcmCredentials } from './types.js';
|
|
23
|
+
import type { PushNotifier } from './push-notifier.js';
|
|
24
|
+
/** Minimal `fetch` Response shape the FCM sender consumes. */
|
|
25
|
+
export interface FcmResponseLike {
|
|
26
|
+
status: number;
|
|
27
|
+
json(): Promise<unknown>;
|
|
28
|
+
text(): Promise<string>;
|
|
29
|
+
}
|
|
30
|
+
/** Injected `fetch`-like transport (default: global `fetch`). */
|
|
31
|
+
export type FcmFetch = (url: string, init: {
|
|
32
|
+
method: string;
|
|
33
|
+
headers: Record<string, string>;
|
|
34
|
+
body: string;
|
|
35
|
+
}) => Promise<FcmResponseLike>;
|
|
36
|
+
export interface FcmPushDeps {
|
|
37
|
+
/** Network transport. Defaults to global `fetch`. */
|
|
38
|
+
fetch?: FcmFetch;
|
|
39
|
+
/** Monotonic clock (ms). Defaults to `Date.now`. Injected for cache tests. */
|
|
40
|
+
now?: () => number;
|
|
41
|
+
/** Failure log seam (default: `debug('sereus:cadre:push:fcm')`). */
|
|
42
|
+
log?: (line: string) => void;
|
|
43
|
+
}
|
|
44
|
+
export declare function createFcmPushNotifier(creds: FcmCredentials, deps?: FcmPushDeps): PushNotifier;
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* push-notifier-fcm.ts — FCM HTTP v1 strand-wake delivery.
|
|
3
|
+
*
|
|
4
|
+
* Node-only: mints a Google OAuth2 access token (RS256 JWT bearer grant signed
|
|
5
|
+
* with `node:crypto`) and POSTs a data message to the FCM v1 endpoint. The legacy
|
|
6
|
+
* server-key `fcm.googleapis.com/fcm/send` API is deprecated, so we use HTTP v1.
|
|
7
|
+
*
|
|
8
|
+
* Reachable only through the `@serfab/cadre-core/push-node` subpath — never from
|
|
9
|
+
* the cross-platform `./index.js` graph — so the `node:crypto` import below is
|
|
10
|
+
* safe: no RN/browser bundler ever resolves this module. (Plain named import;
|
|
11
|
+
* the old namespace-import-to-only-warn dance is obsolete now that the module is
|
|
12
|
+
* unreachable from a browser build by construction.)
|
|
13
|
+
*
|
|
14
|
+
* The network call is behind an injected `fetch`-like seam so unit tests assert
|
|
15
|
+
* the request shape and map every documented response code with no real network
|
|
16
|
+
* and no credentials. Failures are returned as {@link PushSendResult} values.
|
|
17
|
+
*
|
|
18
|
+
* Secret hygiene: the service-account `privateKey`, the minted JWT, and full
|
|
19
|
+
* device tokens are never logged — failure log lines carry only a status/error
|
|
20
|
+
* code and a redacted token prefix.
|
|
21
|
+
*/
|
|
22
|
+
import { sign } from 'node:crypto';
|
|
23
|
+
import debug from 'debug';
|
|
24
|
+
import { MAX_REASON_LEN, b64urlJson, errText, redact } from './push-notifier-shared.js';
|
|
25
|
+
const TOKEN_ENDPOINT = 'https://oauth2.googleapis.com/token';
|
|
26
|
+
const FCM_SCOPE = 'https://www.googleapis.com/auth/firebase.messaging';
|
|
27
|
+
const JWT_BEARER_GRANT = 'urn:ietf:params:oauth:grant-type:jwt-bearer';
|
|
28
|
+
/** Re-mint the access token this many ms before its stated expiry. */
|
|
29
|
+
const TOKEN_REFRESH_SKEW_MS = 60000;
|
|
30
|
+
/** OAuth2 access tokens are short-lived (≤1h); assume 1h when unstated. */
|
|
31
|
+
const DEFAULT_TOKEN_TTL_MS = 3600000;
|
|
32
|
+
const debugFcm = debug('sereus:cadre:push:fcm');
|
|
33
|
+
export function createFcmPushNotifier(creds, deps = {}) {
|
|
34
|
+
const doFetch = deps.fetch ?? ((url, init) => fetch(url, init));
|
|
35
|
+
const now = deps.now ?? (() => Date.now());
|
|
36
|
+
const log = deps.log ?? ((line) => debugFcm(line));
|
|
37
|
+
const sendUrl = `https://fcm.googleapis.com/v1/projects/${creds.projectId}/messages:send`;
|
|
38
|
+
let cached = null;
|
|
39
|
+
async function send(msg) {
|
|
40
|
+
let token;
|
|
41
|
+
try {
|
|
42
|
+
token = await accessToken(false);
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
log(`oauth mint failed: ${errText(err)}`);
|
|
46
|
+
return { ok: false, unregistered: false, error: `fcm oauth mint failed: ${errText(err)}` };
|
|
47
|
+
}
|
|
48
|
+
const first = await postSend(msg, token);
|
|
49
|
+
if (first.status !== 401)
|
|
50
|
+
return mapResult(first, msg.token);
|
|
51
|
+
// The cached access token was rejected (expired/revoked mid-flight). Re-mint
|
|
52
|
+
// once and retry the single send; a second failure is returned as-is.
|
|
53
|
+
let retryToken;
|
|
54
|
+
try {
|
|
55
|
+
retryToken = await accessToken(true);
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
log(`oauth re-mint failed: ${errText(err)}`);
|
|
59
|
+
return { ok: false, unregistered: false, error: `fcm oauth re-mint failed: ${errText(err)}` };
|
|
60
|
+
}
|
|
61
|
+
return mapResult(await postSend(msg, retryToken), msg.token);
|
|
62
|
+
}
|
|
63
|
+
async function postSend(msg, token) {
|
|
64
|
+
const body = JSON.stringify({
|
|
65
|
+
message: {
|
|
66
|
+
token: msg.token,
|
|
67
|
+
data: {
|
|
68
|
+
type: msg.payload.type,
|
|
69
|
+
strandId: msg.payload.strandId,
|
|
70
|
+
reason: msg.payload.reason.slice(0, MAX_REASON_LEN),
|
|
71
|
+
},
|
|
72
|
+
android: { priority: 'high' },
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
const res = await doFetch(sendUrl, {
|
|
76
|
+
method: 'POST',
|
|
77
|
+
headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' },
|
|
78
|
+
body,
|
|
79
|
+
});
|
|
80
|
+
return { status: res.status, body: await safeText(res) };
|
|
81
|
+
}
|
|
82
|
+
function mapResult(res, deviceToken) {
|
|
83
|
+
if (res.status === 200)
|
|
84
|
+
return { ok: true };
|
|
85
|
+
const summary = errorSummary(res.status, res.body);
|
|
86
|
+
if (isUnregistered(res.status, res.body)) {
|
|
87
|
+
log(`unregistered token=${redact(deviceToken)} (${summary})`);
|
|
88
|
+
return { ok: false, unregistered: true, error: summary };
|
|
89
|
+
}
|
|
90
|
+
log(`send failed token=${redact(deviceToken)} (${summary})`);
|
|
91
|
+
return { ok: false, unregistered: false, error: summary };
|
|
92
|
+
}
|
|
93
|
+
async function accessToken(forceRefresh) {
|
|
94
|
+
if (!forceRefresh && cached && cached.expiresAt > now())
|
|
95
|
+
return cached.value;
|
|
96
|
+
cached = null;
|
|
97
|
+
const minted = await mintAccessToken();
|
|
98
|
+
cached = { value: minted.value, expiresAt: now() + minted.ttlMs - TOKEN_REFRESH_SKEW_MS };
|
|
99
|
+
return minted.value;
|
|
100
|
+
}
|
|
101
|
+
async function mintAccessToken() {
|
|
102
|
+
const jwt = signOauthJwt();
|
|
103
|
+
const res = await doFetch(TOKEN_ENDPOINT, {
|
|
104
|
+
method: 'POST',
|
|
105
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
106
|
+
body: `grant_type=${encodeURIComponent(JWT_BEARER_GRANT)}&assertion=${encodeURIComponent(jwt)}`,
|
|
107
|
+
});
|
|
108
|
+
if (res.status !== 200) {
|
|
109
|
+
throw new Error(`token endpoint ${res.status}: ${await safeText(res)}`);
|
|
110
|
+
}
|
|
111
|
+
const parsed = (await res.json());
|
|
112
|
+
if (typeof parsed.access_token !== 'string') {
|
|
113
|
+
throw new Error('token endpoint returned no access_token');
|
|
114
|
+
}
|
|
115
|
+
const ttlMs = typeof parsed.expires_in === 'number' ? parsed.expires_in * 1000 : DEFAULT_TOKEN_TTL_MS;
|
|
116
|
+
return { value: parsed.access_token, ttlMs };
|
|
117
|
+
}
|
|
118
|
+
function signOauthJwt() {
|
|
119
|
+
const iat = Math.floor(now() / 1000);
|
|
120
|
+
const header = { alg: 'RS256', typ: 'JWT' };
|
|
121
|
+
const claims = {
|
|
122
|
+
iss: creds.clientEmail,
|
|
123
|
+
sub: creds.clientEmail,
|
|
124
|
+
scope: FCM_SCOPE,
|
|
125
|
+
aud: TOKEN_ENDPOINT,
|
|
126
|
+
iat,
|
|
127
|
+
exp: iat + 3600,
|
|
128
|
+
};
|
|
129
|
+
const signingInput = `${b64urlJson(header)}.${b64urlJson(claims)}`;
|
|
130
|
+
const sig = sign('RSA-SHA256', Buffer.from(signingInput), creds.privateKey).toString('base64url');
|
|
131
|
+
return `${signingInput}.${sig}`;
|
|
132
|
+
}
|
|
133
|
+
async function close() {
|
|
134
|
+
cached = null;
|
|
135
|
+
}
|
|
136
|
+
return { send, close };
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Whether an FCM v1 error response names a permanently-invalid registration
|
|
140
|
+
* token: a 404 (`UNREGISTERED`), or a 400 `INVALID_ARGUMENT` whose message names
|
|
141
|
+
* the registration token. A generic 400 (malformed message) is NOT unregistered.
|
|
142
|
+
*/
|
|
143
|
+
function isUnregistered(status, body) {
|
|
144
|
+
if (status === 404)
|
|
145
|
+
return true;
|
|
146
|
+
if (status !== 400)
|
|
147
|
+
return false;
|
|
148
|
+
const code = fcmErrorCode(body);
|
|
149
|
+
if (code === 'UNREGISTERED')
|
|
150
|
+
return true;
|
|
151
|
+
if (code === 'INVALID_ARGUMENT' && /registration[ -]token|not a valid fcm registration/i.test(body))
|
|
152
|
+
return true;
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
/** Pull the FCM `errorCode` (FcmError detail) or RPC `status` out of the body. */
|
|
156
|
+
function fcmErrorCode(body) {
|
|
157
|
+
try {
|
|
158
|
+
const parsed = JSON.parse(body);
|
|
159
|
+
const err = parsed.error;
|
|
160
|
+
if (!err)
|
|
161
|
+
return null;
|
|
162
|
+
const detailCode = err.details?.find((d) => typeof d.errorCode === 'string')?.errorCode;
|
|
163
|
+
return detailCode ?? err.status ?? null;
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/** A secret-free one-line error summary (status + error code, never the token). */
|
|
170
|
+
function errorSummary(status, body) {
|
|
171
|
+
const code = fcmErrorCode(body);
|
|
172
|
+
return code ? `fcm ${status} ${code}` : `fcm ${status}`;
|
|
173
|
+
}
|
|
174
|
+
async function safeText(res) {
|
|
175
|
+
try {
|
|
176
|
+
return await res.text();
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
return '';
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=push-notifier-fcm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"push-notifier-fcm.js","sourceRoot":"","sources":["../src/push-notifier-fcm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAC;AAExF,MAAM,cAAc,GAAG,qCAAqC,CAAC;AAC7D,MAAM,SAAS,GAAG,oDAAoD,CAAC;AACvE,MAAM,gBAAgB,GAAG,6CAA6C,CAAC;AACvE,sEAAsE;AACtE,MAAM,qBAAqB,GAAG,KAAM,CAAC;AACrC,2EAA2E;AAC3E,MAAM,oBAAoB,GAAG,OAAS,CAAC;AAEvC,MAAM,QAAQ,GAAG,KAAK,CAAC,uBAAuB,CAAC,CAAC;AA8BhD,MAAM,UAAU,qBAAqB,CAAC,KAAqB,EAAE,OAAoB,EAAE;IACjF,MAAM,OAAO,GAAa,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC;IAC1E,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,IAAI,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M,GAAG,CAAC,IAAI,EAAE,CAAC;IAC1B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* push-notifier-shared.ts — internal helpers shared by the FCM and APNs
|
|
3
|
+
* push-notifier implementations. Server-only (used only by modules that already
|
|
4
|
+
* reach for `node:crypto` / `node:http2`); never re-exported from the package
|
|
5
|
+
* entry. Centralized so the secret-redaction and payload-bounding rules have a
|
|
6
|
+
* single, hardenable source rather than drifting between the two transports.
|
|
7
|
+
*/
|
|
8
|
+
/** Defensive cap on the free-form `reason` carried in a strand-wake payload. */
|
|
9
|
+
export declare const MAX_REASON_LEN = 256;
|
|
10
|
+
/** Base64url-encode the JSON form of an object (JWT header/claims segments). */
|
|
11
|
+
export declare function b64urlJson(obj: unknown): string;
|
|
12
|
+
/** A short, secret-free string for an unknown thrown value. */
|
|
13
|
+
export declare function errText(err: unknown): string;
|
|
14
|
+
/**
|
|
15
|
+
* Redact a device token to a short, non-reversible prefix for debug lines — the
|
|
16
|
+
* full FCM/APNs token is a secret and must never reach a log.
|
|
17
|
+
*/
|
|
18
|
+
export declare function redact(token: string): string;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* push-notifier-shared.ts — internal helpers shared by the FCM and APNs
|
|
3
|
+
* push-notifier implementations. Server-only (used only by modules that already
|
|
4
|
+
* reach for `node:crypto` / `node:http2`); never re-exported from the package
|
|
5
|
+
* entry. Centralized so the secret-redaction and payload-bounding rules have a
|
|
6
|
+
* single, hardenable source rather than drifting between the two transports.
|
|
7
|
+
*/
|
|
8
|
+
/** Defensive cap on the free-form `reason` carried in a strand-wake payload. */
|
|
9
|
+
export const MAX_REASON_LEN = 256;
|
|
10
|
+
/** Base64url-encode the JSON form of an object (JWT header/claims segments). */
|
|
11
|
+
export function b64urlJson(obj) {
|
|
12
|
+
return Buffer.from(JSON.stringify(obj)).toString('base64url');
|
|
13
|
+
}
|
|
14
|
+
/** A short, secret-free string for an unknown thrown value. */
|
|
15
|
+
export function errText(err) {
|
|
16
|
+
return err instanceof Error ? err.message : String(err);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Redact a device token to a short, non-reversible prefix for debug lines — the
|
|
20
|
+
* full FCM/APNs token is a secret and must never reach a log.
|
|
21
|
+
*/
|
|
22
|
+
export function redact(token) {
|
|
23
|
+
return token.length <= 8 ? '…' : `${token.slice(0, 6)}…`;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=push-notifier-shared.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"push-notifier-shared.js","sourceRoot":"","sources":["../src/push-notifier-shared.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,gFAAgF;AAChF,MAAM,CAAC,MAAM,cAAc,GAAG,GAAG,CAAC;AAElC,gFAAgF;AAChF,MAAM,UAAU,UAAU,CAAC,GAAY;IACrC,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;AAChE,CAAC;AAED,+DAA+D;AAC/D,MAAM,UAAU,OAAO,CAAC,GAAY;IAClC,OAAO,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AAC1D,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,MAAM,CAAC,KAAa;IAClC,OAAO,KAAK,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC;AAC3D,CAAC"}
|