@vllnt/convex-push 0.0.0-stage → 0.1.0-canary.4a1ca91

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.
Files changed (152) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/LICENSE +21 -0
  3. package/README.md +415 -2
  4. package/dist/client/index.d.ts +180 -0
  5. package/dist/client/index.d.ts.map +1 -0
  6. package/dist/client/index.js +152 -0
  7. package/dist/client/index.js.map +1 -0
  8. package/dist/component/_generated/api.d.ts +80 -0
  9. package/dist/component/_generated/api.d.ts.map +1 -0
  10. package/dist/component/_generated/api.js +31 -0
  11. package/dist/component/_generated/api.js.map +1 -0
  12. package/dist/component/_generated/component.d.ts +274 -0
  13. package/dist/component/_generated/component.d.ts.map +1 -0
  14. package/dist/component/_generated/component.js +11 -0
  15. package/dist/component/_generated/component.js.map +1 -0
  16. package/dist/component/_generated/dataModel.d.ts +46 -0
  17. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  18. package/dist/component/_generated/dataModel.js +11 -0
  19. package/dist/component/_generated/dataModel.js.map +1 -0
  20. package/dist/component/_generated/server.d.ts +142 -0
  21. package/dist/component/_generated/server.d.ts.map +1 -0
  22. package/dist/component/_generated/server.js +80 -0
  23. package/dist/component/_generated/server.js.map +1 -0
  24. package/dist/component/actions.d.ts +29 -0
  25. package/dist/component/actions.d.ts.map +1 -0
  26. package/dist/component/actions.js +29 -0
  27. package/dist/component/actions.js.map +1 -0
  28. package/dist/component/checks.d.ts +27 -0
  29. package/dist/component/checks.d.ts.map +1 -0
  30. package/dist/component/checks.js +107 -0
  31. package/dist/component/checks.js.map +1 -0
  32. package/dist/component/convex.config.d.ts +18 -0
  33. package/dist/component/convex.config.d.ts.map +1 -0
  34. package/dist/component/convex.config.js +30 -0
  35. package/dist/component/convex.config.js.map +1 -0
  36. package/dist/component/crons.d.ts +8 -0
  37. package/dist/component/crons.d.ts.map +1 -0
  38. package/dist/component/crons.js +15 -0
  39. package/dist/component/crons.js.map +1 -0
  40. package/dist/component/crypto/codec.d.ts +64 -0
  41. package/dist/component/crypto/codec.d.ts.map +1 -0
  42. package/dist/component/crypto/codec.js +102 -0
  43. package/dist/component/crypto/codec.js.map +1 -0
  44. package/dist/component/crypto/jwt.d.ts +46 -0
  45. package/dist/component/crypto/jwt.d.ts.map +1 -0
  46. package/dist/component/crypto/jwt.js +82 -0
  47. package/dist/component/crypto/jwt.js.map +1 -0
  48. package/dist/component/crypto/token_seal.d.ts +31 -0
  49. package/dist/component/crypto/token_seal.d.ts.map +1 -0
  50. package/dist/component/crypto/token_seal.js +44 -0
  51. package/dist/component/crypto/token_seal.js.map +1 -0
  52. package/dist/component/crypto/web_push_crypto.d.ts +42 -0
  53. package/dist/component/crypto/web_push_crypto.d.ts.map +1 -0
  54. package/dist/component/crypto/web_push_crypto.js +49 -0
  55. package/dist/component/crypto/web_push_crypto.js.map +1 -0
  56. package/dist/component/internal_mutations.d.ts +48 -0
  57. package/dist/component/internal_mutations.d.ts.map +1 -0
  58. package/dist/component/internal_mutations.js +82 -0
  59. package/dist/component/internal_mutations.js.map +1 -0
  60. package/dist/component/mutations.d.ts +223 -0
  61. package/dist/component/mutations.d.ts.map +1 -0
  62. package/dist/component/mutations.js +244 -0
  63. package/dist/component/mutations.js.map +1 -0
  64. package/dist/component/providers/apns.d.ts +46 -0
  65. package/dist/component/providers/apns.d.ts.map +1 -0
  66. package/dist/component/providers/apns.js +105 -0
  67. package/dist/component/providers/apns.js.map +1 -0
  68. package/dist/component/providers/fcm.d.ts +36 -0
  69. package/dist/component/providers/fcm.d.ts.map +1 -0
  70. package/dist/component/providers/fcm.js +116 -0
  71. package/dist/component/providers/fcm.js.map +1 -0
  72. package/dist/component/providers/http.d.ts +45 -0
  73. package/dist/component/providers/http.d.ts.map +1 -0
  74. package/dist/component/providers/http.js +74 -0
  75. package/dist/component/providers/http.js.map +1 -0
  76. package/dist/component/providers/keys.d.ts +70 -0
  77. package/dist/component/providers/keys.d.ts.map +1 -0
  78. package/dist/component/providers/keys.js +89 -0
  79. package/dist/component/providers/keys.js.map +1 -0
  80. package/dist/component/providers/payload.d.ts +83 -0
  81. package/dist/component/providers/payload.d.ts.map +1 -0
  82. package/dist/component/providers/payload.js +162 -0
  83. package/dist/component/providers/payload.js.map +1 -0
  84. package/dist/component/providers/token_cache.d.ts +56 -0
  85. package/dist/component/providers/token_cache.d.ts.map +1 -0
  86. package/dist/component/providers/token_cache.js +42 -0
  87. package/dist/component/providers/token_cache.js.map +1 -0
  88. package/dist/component/providers/web_push.d.ts +40 -0
  89. package/dist/component/providers/web_push.d.ts.map +1 -0
  90. package/dist/component/providers/web_push.js +94 -0
  91. package/dist/component/providers/web_push.js.map +1 -0
  92. package/dist/component/queries.d.ts +132 -0
  93. package/dist/component/queries.d.ts.map +1 -0
  94. package/dist/component/queries.js +156 -0
  95. package/dist/component/queries.js.map +1 -0
  96. package/dist/component/queue.d.ts +66 -0
  97. package/dist/component/queue.d.ts.map +1 -0
  98. package/dist/component/queue.js +356 -0
  99. package/dist/component/queue.js.map +1 -0
  100. package/dist/component/registry.d.ts +46 -0
  101. package/dist/component/registry.d.ts.map +1 -0
  102. package/dist/component/registry.js +128 -0
  103. package/dist/component/registry.js.map +1 -0
  104. package/dist/component/schema.d.ts +286 -0
  105. package/dist/component/schema.d.ts.map +1 -0
  106. package/dist/component/schema.js +70 -0
  107. package/dist/component/schema.js.map +1 -0
  108. package/dist/component/senders.d.ts +67 -0
  109. package/dist/component/senders.d.ts.map +1 -0
  110. package/dist/component/senders.js +123 -0
  111. package/dist/component/senders.js.map +1 -0
  112. package/dist/component/validators.d.ts +784 -0
  113. package/dist/component/validators.d.ts.map +1 -0
  114. package/dist/component/validators.js +193 -0
  115. package/dist/component/validators.js.map +1 -0
  116. package/dist/shared.d.ts +109 -0
  117. package/dist/shared.d.ts.map +1 -0
  118. package/dist/shared.js +131 -0
  119. package/dist/shared.js.map +1 -0
  120. package/docs/API.md +258 -0
  121. package/llms.txt +33 -0
  122. package/package.json +125 -4
  123. package/src/client/index.ts +276 -0
  124. package/src/component/_generated/api.ts +96 -0
  125. package/src/component/_generated/component.ts +263 -0
  126. package/src/component/_generated/dataModel.ts +60 -0
  127. package/src/component/_generated/server.ts +178 -0
  128. package/src/component/actions.ts +31 -0
  129. package/src/component/checks.ts +117 -0
  130. package/src/component/convex.config.ts +32 -0
  131. package/src/component/crons.ts +17 -0
  132. package/src/component/crypto/codec.ts +113 -0
  133. package/src/component/crypto/jwt.ts +109 -0
  134. package/src/component/crypto/token_seal.ts +56 -0
  135. package/src/component/crypto/web_push_crypto.ts +75 -0
  136. package/src/component/internal_mutations.ts +88 -0
  137. package/src/component/mutations.ts +257 -0
  138. package/src/component/providers/apns.ts +116 -0
  139. package/src/component/providers/fcm.ts +132 -0
  140. package/src/component/providers/http.ts +78 -0
  141. package/src/component/providers/keys.ts +133 -0
  142. package/src/component/providers/payload.ts +200 -0
  143. package/src/component/providers/token_cache.ts +75 -0
  144. package/src/component/providers/web_push.ts +101 -0
  145. package/src/component/queries.ts +182 -0
  146. package/src/component/queue.ts +425 -0
  147. package/src/component/registry.ts +144 -0
  148. package/src/component/schema.ts +82 -0
  149. package/src/component/senders.ts +152 -0
  150. package/src/component/validators.ts +285 -0
  151. package/src/shared.ts +155 -0
  152. package/src/test.ts +25 -0
@@ -0,0 +1,36 @@
1
+ import type { SendResult } from "../validators.js";
2
+ import { type FcmKey, type PushEnv } from "./keys.js";
3
+ import { type PushContent } from "./payload.js";
4
+ import type { Minted, TokenCache } from "./token_cache.js";
5
+ /** One FCM delivery. */
6
+ export interface FcmJob {
7
+ token: string;
8
+ content: PushContent;
9
+ expiresAt: number;
10
+ }
11
+ /**
12
+ * Exchange a signed RS256 assertion for a Google OAuth access token.
13
+ * @param key - Service-account key.
14
+ * @param now - Clock.
15
+ * @returns The token, or the sender result to return.
16
+ */
17
+ export declare function mintGoogleToken(key: FcmKey, now: () => number): Promise<Minted>;
18
+ /**
19
+ * Map an FCM HTTP v1 response to a sender result.
20
+ * @param status - HTTP status.
21
+ * @param headers - Response headers.
22
+ * @param body - Parsed JSON body.
23
+ * @param now - Clock.
24
+ * @returns The result. A 401 is a retry; the caller drops the cached token.
25
+ */
26
+ export declare function classifyFcm(status: number, headers: Headers, body: unknown, now: number): SendResult;
27
+ /**
28
+ * Send one FCM HTTP v1 message to one registration token.
29
+ * @param job - The delivery.
30
+ * @param env - Component env with the service-account key.
31
+ * @param cache - Provider-token cache.
32
+ * @param now - Clock.
33
+ * @returns The sender result.
34
+ */
35
+ export declare function sendFcm(job: FcmJob, env: PushEnv, cache: TokenCache, now: () => number): Promise<SendResult>;
36
+ //# sourceMappingURL=fcm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fcm.d.ts","sourceRoot":"","sources":["../../../src/component/providers/fcm.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,OAAO,EAAE,KAAK,MAAM,EAAE,KAAK,OAAO,EAAW,MAAM,WAAW,CAAC;AAC/D,OAAO,EAAkD,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAChG,OAAO,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAU3D,wBAAwB;AACxB,MAAM,WAAW,MAAM;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,WAAW,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;;;GAKG;AACH,wBAAsB,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CA8BrF;AAoBD;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,UAAU,CAepG;AAED;;;;;;;GAOG;AACH,wBAAsB,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAmBlH"}
@@ -0,0 +1,116 @@
1
+ import { RETRY_BASE_DELAY_MS } from "../../shared.js";
2
+ import { importSigningKey, signJwt } from "../crypto/jwt.js";
3
+ import { parseRetryAfter, providerFetch, readJson } from "./http.js";
4
+ import { readFcm } from "./keys.js";
5
+ import { buildFcmMessage, isJsonObject } from "./payload.js";
6
+ const FCM_SCOPE = "https://www.googleapis.com/auth/firebase.messaging";
7
+ /**
8
+ * Google access tokens live 3600 seconds. Reuse for 50 minutes, or for the
9
+ * lifetime less 10 minutes (at least half of it) when Google grants less.
10
+ */
11
+ const FCM_TOKEN_REUSE_MS = 3_000_000;
12
+ const FCM_TOKEN_MARGIN_MS = 600_000;
13
+ /**
14
+ * Exchange a signed RS256 assertion for a Google OAuth access token.
15
+ * @param key - Service-account key.
16
+ * @param now - Clock.
17
+ * @returns The token, or the sender result to return.
18
+ */
19
+ export async function mintGoogleToken(key, now) {
20
+ const signingKey = await importSigningKey(key.privateKey, "RS256");
21
+ if (!signingKey)
22
+ return { ok: false, result: { status: "failed", code: "CREDENTIALS_INVALID" } };
23
+ const issuedAt = Math.floor(now() / 1000);
24
+ const assertion = await signJwt({}, { iss: key.clientEmail, scope: FCM_SCOPE, aud: key.tokenUri, iat: issuedAt, exp: issuedAt + 3600 }, signingKey, "RS256");
25
+ const http = await providerFetch(key.tokenUri, {
26
+ method: "POST",
27
+ headers: { "content-type": "application/x-www-form-urlencoded" },
28
+ body: new URLSearchParams({ grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer", assertion }).toString(),
29
+ });
30
+ if (!http.ok)
31
+ return { ok: false, result: http.result };
32
+ const { status } = http.response;
33
+ const body = await readJson(http.response);
34
+ if (status === 200 && isJsonObject(body) && typeof body.access_token === "string") {
35
+ const lifetimeMs = typeof body.expires_in === "number" ? body.expires_in * 1000 : 3_600_000;
36
+ const minted = now();
37
+ return {
38
+ ok: true,
39
+ token: body.access_token,
40
+ refreshAfter: minted + Math.min(FCM_TOKEN_REUSE_MS, Math.max(lifetimeMs - FCM_TOKEN_MARGIN_MS, lifetimeMs / 2)),
41
+ expiresAt: minted + lifetimeMs,
42
+ };
43
+ }
44
+ if (status === 429 || status >= 500)
45
+ return { ok: false, result: { status: "retry", code: "TOKEN_EXCHANGE_UNAVAILABLE", httpStatus: status } };
46
+ return { ok: false, result: { status: "failed", code: "CREDENTIALS_INVALID", httpStatus: status } };
47
+ }
48
+ function fcmErrorCode(body, status) {
49
+ const error = isJsonObject(body) && isJsonObject(body.error) ? body.error : {};
50
+ const details = Array.isArray(error.details) ? error.details.filter(isJsonObject) : [];
51
+ const fcmDetail = details.find((d) => typeof d.errorCode === "string");
52
+ const code = typeof fcmDetail?.errorCode === "string"
53
+ ? fcmDetail.errorCode
54
+ : typeof error.status === "string"
55
+ ? error.status
56
+ : `HTTP_${String(status)}`;
57
+ const tokenField = details.some((d) => Array.isArray(d.fieldViolations) &&
58
+ d.fieldViolations.some((f) => isJsonObject(f) && f.field === "message.token"));
59
+ return { code, tokenField };
60
+ }
61
+ /**
62
+ * Map an FCM HTTP v1 response to a sender result.
63
+ * @param status - HTTP status.
64
+ * @param headers - Response headers.
65
+ * @param body - Parsed JSON body.
66
+ * @param now - Clock.
67
+ * @returns The result. A 401 is a retry; the caller drops the cached token.
68
+ */
69
+ export function classifyFcm(status, headers, body, now) {
70
+ if (status === 200) {
71
+ const name = isJsonObject(body) && typeof body.name === "string" ? body.name : undefined;
72
+ return { status: "sent", providerMessageId: name, httpStatus: 200 };
73
+ }
74
+ const { code, tokenField } = fcmErrorCode(body, status);
75
+ if (code === "UNREGISTERED" || (status === 400 && code === "INVALID_ARGUMENT" && tokenField)) {
76
+ return { status: "invalid", code, httpStatus: status };
77
+ }
78
+ if (status === 401)
79
+ return { status: "retry", code, httpStatus: status };
80
+ if (status === 429) {
81
+ return { status: "retry", code, httpStatus: status, delayMs: parseRetryAfter(headers.get("retry-after"), now) ?? RETRY_BASE_DELAY_MS };
82
+ }
83
+ if (status >= 500)
84
+ return { status: "retry", code, httpStatus: status, delayMs: parseRetryAfter(headers.get("retry-after"), now) };
85
+ return { status: "failed", code, httpStatus: status };
86
+ }
87
+ /**
88
+ * Send one FCM HTTP v1 message to one registration token.
89
+ * @param job - The delivery.
90
+ * @param env - Component env with the service-account key.
91
+ * @param cache - Provider-token cache.
92
+ * @param now - Clock.
93
+ * @returns The sender result.
94
+ */
95
+ export async function sendFcm(job, env, cache, now) {
96
+ const key = await readFcm(env);
97
+ if (!key.ok)
98
+ return { status: "failed", code: key.code };
99
+ const slot = { credentialRef: key.value.credentialRef };
100
+ const access = await cache.get(slot, key.value.privateKey, () => mintGoogleToken(key.value, now));
101
+ if (!access.ok)
102
+ return access.result;
103
+ const ttlSeconds = job.content.options.ttlSeconds === 0 ? 0 : Math.max(Math.floor((job.expiresAt - now()) / 1000), 0);
104
+ const http = await providerFetch(`https://fcm.googleapis.com/v1/projects/${encodeURIComponent(key.value.projectId)}/messages:send`, {
105
+ method: "POST",
106
+ headers: { authorization: `Bearer ${access.token}`, "content-type": "application/json" },
107
+ body: JSON.stringify({ message: buildFcmMessage(job.content, job.token, ttlSeconds) }),
108
+ });
109
+ if (!http.ok)
110
+ return http.result;
111
+ const result = classifyFcm(http.response.status, http.response.headers, await readJson(http.response), now());
112
+ if (result.httpStatus === 401)
113
+ await cache.drop(slot);
114
+ return result;
115
+ }
116
+ //# sourceMappingURL=fcm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fcm.js","sourceRoot":"","sources":["../../../src/component/providers/fcm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE7D,OAAO,EAAE,eAAe,EA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,IAAI,CAAC,MAAM,CAAC;IACjC,MAAM,MAAM,GAAG,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;IAC9G,IAAI,MAAM,CAAC,UAAU,KAAK,GAAG;QAAE,MAAM,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACtD,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,45 @@
1
+ import type { SendResult } from "../validators.js";
2
+ /**
3
+ * Read a Retry-After header: delay seconds or an HTTP date.
4
+ * @param value - The header value.
5
+ * @param now - Current time in ms.
6
+ * @returns The delay in ms, clamped to one second..one day, or `undefined`.
7
+ */
8
+ export declare function parseRetryAfter(value: null | string, now: number): number | undefined;
9
+ /**
10
+ * Bound a provider- or sender-supplied delay to one second..one day.
11
+ * @param ms - The asked delay.
12
+ * @returns The delay used.
13
+ */
14
+ export declare function clampDelay(ms: number): number;
15
+ /**
16
+ * Backoff before the next attempt: one minute, doubling per attempt, capped.
17
+ * @param attempts - Attempts made so far (1 after the first).
18
+ * @returns The delay in ms.
19
+ */
20
+ export declare function backoffDelay(attempts: number): number;
21
+ /** A response, or the retry result a network failure maps to. */
22
+ export type HttpOutcome = {
23
+ ok: false;
24
+ result: SendResult;
25
+ } | {
26
+ ok: true;
27
+ response: Response;
28
+ };
29
+ /**
30
+ * POST to a provider with a time limit. Redirects are not followed (the 3xx is
31
+ * returned as the answer), so a request and its credentials only go to the URL
32
+ * given. A network error or timeout becomes a retry result; nothing about the
33
+ * request is logged.
34
+ * @param url - The provider URL.
35
+ * @param init - Method, headers and body.
36
+ * @returns The response, or a retry result.
37
+ */
38
+ export declare function providerFetch(url: string, init: RequestInit): Promise<HttpOutcome>;
39
+ /**
40
+ * Read a response body as JSON without throwing.
41
+ * @param response - A fetch response.
42
+ * @returns The parsed value, or `null`.
43
+ */
44
+ export declare function readJson(response: Response): Promise<unknown>;
45
+ //# sourceMappingURL=http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../../src/component/providers/http.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAMnD;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,IAAI,GAAG,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAMrF;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,iEAAiE;AACjE,MAAM,MAAM,WAAW,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,UAAU,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC;AAE/F;;;;;;;;GAQG;AACH,wBAAsB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC,CAaxF;AAED;;;;GAIG;AACH,wBAAsB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,CAMnE"}
@@ -0,0 +1,74 @@
1
+ import { PROVIDER_REQUEST_TIMEOUT_MS, RETRY_BASE_DELAY_MS, RETRY_MAX_DELAY_MS } from "../../shared.js";
2
+ /** Bounds applied to any provider-supplied delay: one second to one day. */
3
+ const MIN_DELAY_MS = 1000;
4
+ const MAX_DELAY_MS = 86_400_000;
5
+ /**
6
+ * Read a Retry-After header: delay seconds or an HTTP date.
7
+ * @param value - The header value.
8
+ * @param now - Current time in ms.
9
+ * @returns The delay in ms, clamped to one second..one day, or `undefined`.
10
+ */
11
+ export function parseRetryAfter(value, now) {
12
+ if (!value)
13
+ return undefined;
14
+ const trimmed = value.trim();
15
+ const ms = /^\d+$/.test(trimmed) ? Number(trimmed) * 1000 : Date.parse(trimmed) - now;
16
+ if (Number.isNaN(ms))
17
+ return undefined;
18
+ return clampDelay(ms);
19
+ }
20
+ /**
21
+ * Bound a provider- or sender-supplied delay to one second..one day.
22
+ * @param ms - The asked delay.
23
+ * @returns The delay used.
24
+ */
25
+ export function clampDelay(ms) {
26
+ return Math.min(Math.max(ms, MIN_DELAY_MS), MAX_DELAY_MS);
27
+ }
28
+ /**
29
+ * Backoff before the next attempt: one minute, doubling per attempt, capped.
30
+ * @param attempts - Attempts made so far (1 after the first).
31
+ * @returns The delay in ms.
32
+ */
33
+ export function backoffDelay(attempts) {
34
+ return Math.min(RETRY_BASE_DELAY_MS * 2 ** Math.max(attempts - 1, 0), RETRY_MAX_DELAY_MS);
35
+ }
36
+ /**
37
+ * POST to a provider with a time limit. Redirects are not followed (the 3xx is
38
+ * returned as the answer), so a request and its credentials only go to the URL
39
+ * given. A network error or timeout becomes a retry result; nothing about the
40
+ * request is logged.
41
+ * @param url - The provider URL.
42
+ * @param init - Method, headers and body.
43
+ * @returns The response, or a retry result.
44
+ */
45
+ export async function providerFetch(url, init) {
46
+ const controller = new AbortController();
47
+ const timer = setTimeout(() => {
48
+ controller.abort();
49
+ }, PROVIDER_REQUEST_TIMEOUT_MS);
50
+ try {
51
+ return { ok: true, response: await fetch(url, { ...init, redirect: "manual", signal: controller.signal }) };
52
+ }
53
+ catch (error) {
54
+ const timedOut = error instanceof Error && error.name === "AbortError";
55
+ return { ok: false, result: { status: "retry", code: timedOut ? "TIMEOUT" : "NETWORK_ERROR" } };
56
+ }
57
+ finally {
58
+ clearTimeout(timer);
59
+ }
60
+ }
61
+ /**
62
+ * Read a response body as JSON without throwing.
63
+ * @param response - A fetch response.
64
+ * @returns The parsed value, or `null`.
65
+ */
66
+ export async function readJson(response) {
67
+ try {
68
+ return JSON.parse(await response.text());
69
+ }
70
+ catch {
71
+ return null;
72
+ }
73
+ }
74
+ //# sourceMappingURL=http.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.js","sourceRoot":"","sources":["../../../src/component/providers/http.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAGvG,4EAA4E;AAC5E,MAAM,YAAY,GAAG,IAAI,CAAC;AAC1B,MAAM,YAAY,GAAG,UAAU,CAAC;AAEhC;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,KAAoB,EAAE,GAAW;IAC/D,IAAI,CAAC,KAAK;QAAE,OAAO,SAAS,CAAC;IAC7B,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7B,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,GAAG,CAAC;IACtF,IAAI,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;QAAE,OAAO,SAAS,CAAC;IACvC,OAAO,UAAU,CAAC,EAAE,CAAC,CAAC;AACxB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,EAAU;IACnC,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,YAAY,CAAC,EAAE,YAAY,CAAC,CAAC;AAC5D,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,QAAgB;IAC3C,OAAO,IAAI,CAAC,GAAG,CAAC,mBAAmB,GAAG,CAAC,IAAI,IAAI,CAAC,GAAG,CAAC,QAAQ,GAAG,CAAC,EAAE,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAC;AAC5F,CAAC;AAKD;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,GAAW,EAAE,IAAiB;IAChE,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;IACzC,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE;QAC5B,UAAU,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC,EAAE,2BAA2B,CAAC,CAAC;IAChC,IAAI,CAAC;QACH,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,KAAK,CAAC,GAAG,EAAE,EAAE,GAAG,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC;IAC9G,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,QAAQ,GAAG,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,IAAI,KAAK,YAAY,CAAC;QACvE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,eAAe,EAAE,EAAE,CAAC;IAClG,CAAC;YAAS,CAAC;QACT,YAAY,CAAC,KAAK,CAAC,CAAC;IACtB,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,QAAkB;IAC/C,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;IAC3C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,70 @@
1
+ /** The component's environment variables, as bound at mount. Every one is optional. */
2
+ export interface PushEnv {
3
+ readonly APNS_KEY_P8?: string;
4
+ readonly APNS_KEY_ID?: string;
5
+ readonly APNS_TEAM_ID?: string;
6
+ readonly APNS_SANDBOX_KEY_P8?: string;
7
+ readonly APNS_SANDBOX_KEY_ID?: string;
8
+ readonly FCM_SERVICE_ACCOUNT_JSON?: string;
9
+ readonly VAPID_PUBLIC_KEY?: string;
10
+ readonly VAPID_PRIVATE_KEY?: string;
11
+ readonly VAPID_SUBJECT?: string;
12
+ }
13
+ /** Apple token-auth key for one environment. */
14
+ export interface ApnsKey {
15
+ pemText: string;
16
+ keyId: string;
17
+ teamId: string;
18
+ credentialRef: string;
19
+ }
20
+ /** The fields of a Google service-account key file this package uses. */
21
+ export interface FcmKey {
22
+ projectId: string;
23
+ clientEmail: string;
24
+ privateKey: string;
25
+ tokenUri: string;
26
+ credentialRef: string;
27
+ }
28
+ /** VAPID key pair and contact. */
29
+ export interface VapidKey {
30
+ publicKey: string;
31
+ privateKey: string;
32
+ subject: string;
33
+ credentialRef: string;
34
+ }
35
+ /** Result of reading one provider's keys. */
36
+ export type KeyRead<T> = {
37
+ ok: true;
38
+ value: T;
39
+ } | {
40
+ ok: false;
41
+ code: "CREDENTIALS_INVALID" | "PROVIDER_NOT_CONFIGURED";
42
+ };
43
+ /**
44
+ * Whether a provider has its keys set at all. Cheap, used at fan-out.
45
+ * @param env - Component env.
46
+ * @param platform - A built-in platform.
47
+ * @returns Whether every required variable is present.
48
+ */
49
+ export declare function isConfigured(env: PushEnv, platform: "apns" | "fcm" | "webpush"): boolean;
50
+ /**
51
+ * Read the Apple key for one environment. The sandbox pair is used for sandbox
52
+ * when set; otherwise the main key signs for both hosts.
53
+ * @param env - Component env.
54
+ * @param environment - Which Apple host the delivery targets.
55
+ * @returns The key, or why it is unusable.
56
+ */
57
+ export declare function readApns(env: PushEnv, environment: "production" | "sandbox"): Promise<KeyRead<ApnsKey>>;
58
+ /**
59
+ * Read the Google service-account key.
60
+ * @param env - Component env.
61
+ * @returns The key, or why it is unusable.
62
+ */
63
+ export declare function readFcm(env: PushEnv): Promise<KeyRead<FcmKey>>;
64
+ /**
65
+ * Read the VAPID key pair.
66
+ * @param env - Component env.
67
+ * @returns The key, or why it is unusable.
68
+ */
69
+ export declare function readVapid(env: PushEnv): Promise<KeyRead<VapidKey>>;
70
+ //# sourceMappingURL=keys.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keys.d.ts","sourceRoot":"","sources":["../../../src/component/providers/keys.ts"],"names":[],"mappings":"AAGA,uFAAuF;AACvF,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,mBAAmB,CAAC,EAAE,MAAM,CAAC;IACtC,QAAQ,CAAC,mBAAmB,CAAC,EAAE,MAAM,CAAC;IACtC,QAAQ,CAAC,wBAAwB,CAAC,EAAE,MAAM,CAAC;IAC3C,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IACnC,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAC;IACpC,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;CACjC;AAED,gDAAgD;AAChD,MAAM,WAAW,OAAO;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,yEAAyE;AACzE,MAAM,WAAW,MAAM;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,kCAAkC;AAClC,MAAM,WAAW,QAAQ;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,6CAA6C;AAC7C,MAAM,MAAM,OAAO,CAAC,CAAC,IACjB;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,CAAC,CAAA;CAAE,GACtB;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,IAAI,EAAE,qBAAqB,GAAG,yBAAyB,CAAA;CAAE,CAAC;AAQ3E;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,GAAG,KAAK,GAAG,SAAS,GAAG,OAAO,CAQxF;AAED;;;;;;GAMG;AACH,wBAAsB,QAAQ,CAAC,GAAG,EAAE,OAAO,EAAE,WAAW,EAAE,YAAY,GAAG,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAS7G;AAeD;;;;GAIG;AACH,wBAAsB,OAAO,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAapE;AAED;;;;GAIG;AACH,wBAAsB,SAAS,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAQxE"}
@@ -0,0 +1,89 @@
1
+ import { sha256Hex } from "../crypto/codec.js";
2
+ import { isJsonObject } from "./payload.js";
3
+ const GOOGLE_TOKEN_URI = "https://oauth2.googleapis.com/token";
4
+ /** Variable names of the main and the sandbox-only Apple key. */
5
+ const APNS_MAIN_VARS = { pem: "APNS_KEY_P8", id: "APNS_KEY_ID" };
6
+ const APNS_SANDBOX_VARS = { pem: "APNS_SANDBOX_KEY_P8", id: "APNS_SANDBOX_KEY_ID" };
7
+ /**
8
+ * Whether a provider has its keys set at all. Cheap, used at fan-out.
9
+ * @param env - Component env.
10
+ * @param platform - A built-in platform.
11
+ * @returns Whether every required variable is present.
12
+ */
13
+ export function isConfigured(env, platform) {
14
+ if (platform === "apns") {
15
+ const main = Boolean(env.APNS_KEY_P8 && env.APNS_KEY_ID);
16
+ const sandbox = Boolean(env.APNS_SANDBOX_KEY_P8 && env.APNS_SANDBOX_KEY_ID);
17
+ return Boolean(env.APNS_TEAM_ID) && (main || sandbox);
18
+ }
19
+ if (platform === "fcm")
20
+ return Boolean(env.FCM_SERVICE_ACCOUNT_JSON);
21
+ return Boolean(env.VAPID_PUBLIC_KEY && env.VAPID_PRIVATE_KEY && env.VAPID_SUBJECT);
22
+ }
23
+ /**
24
+ * Read the Apple key for one environment. The sandbox pair is used for sandbox
25
+ * when set; otherwise the main key signs for both hosts.
26
+ * @param env - Component env.
27
+ * @param environment - Which Apple host the delivery targets.
28
+ * @returns The key, or why it is unusable.
29
+ */
30
+ export async function readApns(env, environment) {
31
+ const useSandboxKey = environment === "sandbox" && Boolean(env.APNS_SANDBOX_KEY_P8 && env.APNS_SANDBOX_KEY_ID);
32
+ const names = useSandboxKey ? APNS_SANDBOX_VARS : APNS_MAIN_VARS;
33
+ const pemText = env[names.pem];
34
+ const keyId = env[names.id];
35
+ const teamId = env.APNS_TEAM_ID;
36
+ if (!pemText || !keyId || !teamId)
37
+ return { ok: false, code: "PROVIDER_NOT_CONFIGURED" };
38
+ const credentialRef = await sha256Hex(`apns:${teamId}:${keyId}`);
39
+ return { ok: true, value: { pemText, keyId, teamId, credentialRef } };
40
+ }
41
+ function stringField(object, key) {
42
+ const value = object[key];
43
+ return typeof value === "string" && value.length > 0 ? value : undefined;
44
+ }
45
+ function parseJson(text) {
46
+ try {
47
+ return JSON.parse(text);
48
+ }
49
+ catch {
50
+ return null;
51
+ }
52
+ }
53
+ /**
54
+ * Read the Google service-account key.
55
+ * @param env - Component env.
56
+ * @returns The key, or why it is unusable.
57
+ */
58
+ export async function readFcm(env) {
59
+ if (!env.FCM_SERVICE_ACCOUNT_JSON)
60
+ return { ok: false, code: "PROVIDER_NOT_CONFIGURED" };
61
+ const parsed = parseJson(env.FCM_SERVICE_ACCOUNT_JSON);
62
+ if (!isJsonObject(parsed))
63
+ return { ok: false, code: "CREDENTIALS_INVALID" };
64
+ const projectId = stringField(parsed, "project_id");
65
+ const clientEmail = stringField(parsed, "client_email");
66
+ const privateKey = stringField(parsed, "private_key");
67
+ if (!projectId || !clientEmail || !privateKey) {
68
+ return { ok: false, code: "CREDENTIALS_INVALID" };
69
+ }
70
+ const keyId = stringField(parsed, "private_key_id") ?? "";
71
+ const credentialRef = await sha256Hex(`fcm:${clientEmail}:${keyId}`);
72
+ return { ok: true, value: { projectId, clientEmail, privateKey, tokenUri: GOOGLE_TOKEN_URI, credentialRef } };
73
+ }
74
+ /**
75
+ * Read the VAPID key pair.
76
+ * @param env - Component env.
77
+ * @returns The key, or why it is unusable.
78
+ */
79
+ export async function readVapid(env) {
80
+ const { VAPID_PUBLIC_KEY: publicKey, VAPID_PRIVATE_KEY: privateKey, VAPID_SUBJECT: subject } = env;
81
+ if (!publicKey || !privateKey || !subject)
82
+ return { ok: false, code: "PROVIDER_NOT_CONFIGURED" };
83
+ if (!subject.startsWith("mailto:") && !subject.startsWith("https:")) {
84
+ return { ok: false, code: "CREDENTIALS_INVALID" };
85
+ }
86
+ const credentialRef = await sha256Hex(`vapid:${publicKey}`);
87
+ return { ok: true, value: { publicKey, privateKey, subject, credentialRef } };
88
+ }
89
+ //# sourceMappingURL=keys.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keys.js","sourceRoot":"","sources":["../../../src/component/providers/keys.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AA6C5C,MAAM,gBAAgB,GAAG,qCAAqC,CAAC;AAE/D,iEAAiE;AACjE,MAAM,cAAc,GAAG,EAAE,GAAG,EAAE,aAAa,EAAE,EAAE,EAAE,aAAa,EAAW,CAAC;AAC1E,MAAM,iBAAiB,GAAG,EAAE,GAAG,EAAE,qBAAqB,EAAE,EAAE,EAAE,qBAAqB,EAAW,CAAC;AAE7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AAC;QAC9C,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,qBAAqB,EAAE,CAAC;IACpD,CAAC;IACD,MAAM,KAAK,GAAG,WAAW,CAAC,MAAM,EAAE,gBAAgB,CAAC,IAAI,EAAE,CAAC;IAC1D,MAAM,aAAa,GAAG,MAAM,SAAS,CAAC,OAAO,WAAW,IAAI,KAAK,EAAE,CAAC,CAAC;IACrE,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,SAAS,EAAE,WAAW,EAAE,UAAU,EAAE,QAAQ,EAAE,gBAAgB,EAAE,aAAa,EAAE,EAAE,CAAC;AAChH,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAAC,GAAY;IAC1C,MAAM,EAAE,gBAAgB,EAAE,SAAS,EAAE,iBAAiB,EAAE,UAAU,EAAE,aAAa,EAAE,OAAO,EAAE,GAAG,GAAG,CAAC;IACnG,IAAI,CAAC,SAAS,IAAI,CAAC,UAAU,IAAI,CAAC,OAAO;QAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,yBAAyB,EAAE,CAAC;IACjG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;QACpE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,qBAAqB,EAAE,CAAC;IACpD,CAAC;IACD,MAAM,aAAa,GAAG,MAAM,SAAS,CAAC,SAAS,SAAS,EAAE,CAAC,CAAC;IAC5D,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,SAAS,EAAE,UAAU,EAAE,OAAO,EAAE,aAAa,EAAE,EAAE,CAAC;AAChF,CAAC"}
@@ -0,0 +1,83 @@
1
+ import type { MessageOptions, Overrides, PushData } from "../validators.js";
2
+ /** The provider-neutral content of one message. */
3
+ export interface PushContent {
4
+ title?: string;
5
+ body?: string;
6
+ data: PushData;
7
+ options: MessageOptions;
8
+ overrides?: Overrides;
9
+ }
10
+ /** A JSON object. */
11
+ export type JsonObject = {
12
+ [key: string]: unknown;
13
+ };
14
+ /**
15
+ * Narrow an unknown value to a plain JSON object.
16
+ * @param value - Any value.
17
+ * @returns Whether it is a non-array object.
18
+ */
19
+ export declare function isJsonObject(value: unknown): value is JsonObject;
20
+ /**
21
+ * A message with neither title nor body is silent: a background push on APNs,
22
+ * a data-only message on FCM, and refused on Web Push.
23
+ * @param content - The message.
24
+ * @returns Whether it is silent.
25
+ */
26
+ export declare function isSilent(content: Pick<PushContent, "body" | "title">): boolean;
27
+ /**
28
+ * Parse a `rawJson` override.
29
+ * @param raw - The string, or undefined.
30
+ * @returns The object, `undefined` when absent, or `null` when it is too long, not JSON, or not an object.
31
+ */
32
+ export declare function parseRawJson(raw: string | undefined): JsonObject | null | undefined;
33
+ /**
34
+ * Merge `source` into `target`: objects merge key by key, anything else replaces.
35
+ * @param target - The built payload.
36
+ * @param source - The override.
37
+ * @returns A new merged object.
38
+ */
39
+ export declare function deepMerge(target: JsonObject, source: JsonObject): JsonObject;
40
+ /**
41
+ * Build the APNs JSON payload: `aps` plus the data keys beside it.
42
+ * @param content - The message.
43
+ * @returns The payload object, with `rawJson` merged last.
44
+ */
45
+ export declare function buildApnsPayload(content: PushContent): JsonObject;
46
+ /**
47
+ * Build the FCM HTTP v1 `message` object without its target. Data values become
48
+ * strings, as FCM requires. Topic and condition targets set by `rawJson` are
49
+ * removed and the token is added afterwards, so an override can never change
50
+ * who receives the message.
51
+ * @param content - The message.
52
+ * @param ttlSeconds - Remaining time to live.
53
+ * @returns The `message` object minus `token`.
54
+ */
55
+ export declare function buildFcmBody(content: PushContent, ttlSeconds: number): JsonObject;
56
+ /**
57
+ * The FCM `message` for one token: {@link buildFcmBody} plus the token.
58
+ * @param content - The message.
59
+ * @param token - The device's FCM registration token.
60
+ * @param ttlSeconds - Remaining time to live.
61
+ * @returns The `message` object.
62
+ */
63
+ export declare function buildFcmMessage(content: PushContent, token: string, ttlSeconds: number): JsonObject;
64
+ /**
65
+ * Build the JSON the service worker receives for a Web Push message.
66
+ * @param content - The message.
67
+ * @returns The payload object, with `rawJson` merged last.
68
+ */
69
+ export declare function buildWebPushPayload(content: PushContent): JsonObject;
70
+ /** A platform whose payload is over its limit, with the measured size. */
71
+ export interface Oversize {
72
+ platform: "apns" | "fcm" | "webpush";
73
+ bytes: number;
74
+ limit: number;
75
+ }
76
+ /**
77
+ * Check each built-in provider's payload against its limit, after `rawJson` is merged.
78
+ * @param content - The message.
79
+ * @param platforms - The platforms this message fans out to.
80
+ * @returns The first platform over its limit, or `null`.
81
+ */
82
+ export declare function findOversize(content: PushContent, platforms: ReadonlySet<string>): Oversize | null;
83
+ //# sourceMappingURL=payload.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"payload.d.ts","sourceRoot":"","sources":["../../../src/component/providers/payload.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5E,mDAAmD;AACnD,MAAM,WAAW,WAAW;IAC1B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,QAAQ,CAAC;IACf,OAAO,EAAE,cAAc,CAAC;IACxB,SAAS,CAAC,EAAE,SAAS,CAAC;CACvB;AAED,qBAAqB;AACrB,MAAM,MAAM,UAAU,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAAC;AAEpD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,UAAU,CAEhE;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,OAAO,CAE9E;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