@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.
- package/CHANGELOG.md +41 -0
- package/LICENSE +21 -0
- package/README.md +415 -2
- package/dist/client/index.d.ts +180 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +152 -0
- package/dist/client/index.js.map +1 -0
- package/dist/component/_generated/api.d.ts +80 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +274 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +142 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +80 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/actions.d.ts +29 -0
- package/dist/component/actions.d.ts.map +1 -0
- package/dist/component/actions.js +29 -0
- package/dist/component/actions.js.map +1 -0
- package/dist/component/checks.d.ts +27 -0
- package/dist/component/checks.d.ts.map +1 -0
- package/dist/component/checks.js +107 -0
- package/dist/component/checks.js.map +1 -0
- package/dist/component/convex.config.d.ts +18 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +30 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/crons.d.ts +8 -0
- package/dist/component/crons.d.ts.map +1 -0
- package/dist/component/crons.js +15 -0
- package/dist/component/crons.js.map +1 -0
- package/dist/component/crypto/codec.d.ts +64 -0
- package/dist/component/crypto/codec.d.ts.map +1 -0
- package/dist/component/crypto/codec.js +102 -0
- package/dist/component/crypto/codec.js.map +1 -0
- package/dist/component/crypto/jwt.d.ts +46 -0
- package/dist/component/crypto/jwt.d.ts.map +1 -0
- package/dist/component/crypto/jwt.js +82 -0
- package/dist/component/crypto/jwt.js.map +1 -0
- package/dist/component/crypto/token_seal.d.ts +31 -0
- package/dist/component/crypto/token_seal.d.ts.map +1 -0
- package/dist/component/crypto/token_seal.js +44 -0
- package/dist/component/crypto/token_seal.js.map +1 -0
- package/dist/component/crypto/web_push_crypto.d.ts +42 -0
- package/dist/component/crypto/web_push_crypto.d.ts.map +1 -0
- package/dist/component/crypto/web_push_crypto.js +49 -0
- package/dist/component/crypto/web_push_crypto.js.map +1 -0
- package/dist/component/internal_mutations.d.ts +48 -0
- package/dist/component/internal_mutations.d.ts.map +1 -0
- package/dist/component/internal_mutations.js +82 -0
- package/dist/component/internal_mutations.js.map +1 -0
- package/dist/component/mutations.d.ts +223 -0
- package/dist/component/mutations.d.ts.map +1 -0
- package/dist/component/mutations.js +244 -0
- package/dist/component/mutations.js.map +1 -0
- package/dist/component/providers/apns.d.ts +46 -0
- package/dist/component/providers/apns.d.ts.map +1 -0
- package/dist/component/providers/apns.js +105 -0
- package/dist/component/providers/apns.js.map +1 -0
- package/dist/component/providers/fcm.d.ts +36 -0
- package/dist/component/providers/fcm.d.ts.map +1 -0
- package/dist/component/providers/fcm.js +116 -0
- package/dist/component/providers/fcm.js.map +1 -0
- package/dist/component/providers/http.d.ts +45 -0
- package/dist/component/providers/http.d.ts.map +1 -0
- package/dist/component/providers/http.js +74 -0
- package/dist/component/providers/http.js.map +1 -0
- package/dist/component/providers/keys.d.ts +70 -0
- package/dist/component/providers/keys.d.ts.map +1 -0
- package/dist/component/providers/keys.js +89 -0
- package/dist/component/providers/keys.js.map +1 -0
- package/dist/component/providers/payload.d.ts +83 -0
- package/dist/component/providers/payload.d.ts.map +1 -0
- package/dist/component/providers/payload.js +162 -0
- package/dist/component/providers/payload.js.map +1 -0
- package/dist/component/providers/token_cache.d.ts +56 -0
- package/dist/component/providers/token_cache.d.ts.map +1 -0
- package/dist/component/providers/token_cache.js +42 -0
- package/dist/component/providers/token_cache.js.map +1 -0
- package/dist/component/providers/web_push.d.ts +40 -0
- package/dist/component/providers/web_push.d.ts.map +1 -0
- package/dist/component/providers/web_push.js +94 -0
- package/dist/component/providers/web_push.js.map +1 -0
- package/dist/component/queries.d.ts +132 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +156 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/queue.d.ts +66 -0
- package/dist/component/queue.d.ts.map +1 -0
- package/dist/component/queue.js +356 -0
- package/dist/component/queue.js.map +1 -0
- package/dist/component/registry.d.ts +46 -0
- package/dist/component/registry.d.ts.map +1 -0
- package/dist/component/registry.js +128 -0
- package/dist/component/registry.js.map +1 -0
- package/dist/component/schema.d.ts +286 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +70 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/senders.d.ts +67 -0
- package/dist/component/senders.d.ts.map +1 -0
- package/dist/component/senders.js +123 -0
- package/dist/component/senders.js.map +1 -0
- package/dist/component/validators.d.ts +784 -0
- package/dist/component/validators.d.ts.map +1 -0
- package/dist/component/validators.js +193 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/shared.d.ts +109 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +131 -0
- package/dist/shared.js.map +1 -0
- package/docs/API.md +258 -0
- package/llms.txt +33 -0
- package/package.json +125 -4
- package/src/client/index.ts +276 -0
- package/src/component/_generated/api.ts +96 -0
- package/src/component/_generated/component.ts +263 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +178 -0
- package/src/component/actions.ts +31 -0
- package/src/component/checks.ts +117 -0
- package/src/component/convex.config.ts +32 -0
- package/src/component/crons.ts +17 -0
- package/src/component/crypto/codec.ts +113 -0
- package/src/component/crypto/jwt.ts +109 -0
- package/src/component/crypto/token_seal.ts +56 -0
- package/src/component/crypto/web_push_crypto.ts +75 -0
- package/src/component/internal_mutations.ts +88 -0
- package/src/component/mutations.ts +257 -0
- package/src/component/providers/apns.ts +116 -0
- package/src/component/providers/fcm.ts +132 -0
- package/src/component/providers/http.ts +78 -0
- package/src/component/providers/keys.ts +133 -0
- package/src/component/providers/payload.ts +200 -0
- package/src/component/providers/token_cache.ts +75 -0
- package/src/component/providers/web_push.ts +101 -0
- package/src/component/queries.ts +182 -0
- package/src/component/queue.ts +425 -0
- package/src/component/registry.ts +144 -0
- package/src/component/schema.ts +82 -0
- package/src/component/senders.ts +152 -0
- package/src/component/validators.ts +285 -0
- package/src/shared.ts +155 -0
- package/src/test.ts +25 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { APNS_MAX_PAYLOAD_BYTES, FCM_MAX_PAYLOAD_BYTES, MAX_RAW_JSON_LENGTH, WEBPUSH_MAX_PLAINTEXT_BYTES, } from "../../shared.js";
|
|
2
|
+
import { byteLength } from "../crypto/codec.js";
|
|
3
|
+
/**
|
|
4
|
+
* Narrow an unknown value to a plain JSON object.
|
|
5
|
+
* @param value - Any value.
|
|
6
|
+
* @returns Whether it is a non-array object.
|
|
7
|
+
*/
|
|
8
|
+
export function isJsonObject(value) {
|
|
9
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A message with neither title nor body is silent: a background push on APNs,
|
|
13
|
+
* a data-only message on FCM, and refused on Web Push.
|
|
14
|
+
* @param content - The message.
|
|
15
|
+
* @returns Whether it is silent.
|
|
16
|
+
*/
|
|
17
|
+
export function isSilent(content) {
|
|
18
|
+
return !content.title && !content.body;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Parse a `rawJson` override.
|
|
22
|
+
* @param raw - The string, or undefined.
|
|
23
|
+
* @returns The object, `undefined` when absent, or `null` when it is too long, not JSON, or not an object.
|
|
24
|
+
*/
|
|
25
|
+
export function parseRawJson(raw) {
|
|
26
|
+
if (raw === undefined)
|
|
27
|
+
return undefined;
|
|
28
|
+
if (raw.length > MAX_RAW_JSON_LENGTH)
|
|
29
|
+
return null;
|
|
30
|
+
try {
|
|
31
|
+
const parsed = JSON.parse(raw);
|
|
32
|
+
return isJsonObject(parsed) ? parsed : null;
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Merge `source` into `target`: objects merge key by key, anything else replaces.
|
|
40
|
+
* @param target - The built payload.
|
|
41
|
+
* @param source - The override.
|
|
42
|
+
* @returns A new merged object.
|
|
43
|
+
*/
|
|
44
|
+
export function deepMerge(target, source) {
|
|
45
|
+
const out = { ...target };
|
|
46
|
+
for (const [key, value] of Object.entries(source)) {
|
|
47
|
+
const current = out[key];
|
|
48
|
+
out[key] = isJsonObject(current) && isJsonObject(value) ? deepMerge(current, value) : value;
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
function withRaw(built, raw) {
|
|
53
|
+
const parsed = parseRawJson(raw);
|
|
54
|
+
return parsed ? deepMerge(built, parsed) : built;
|
|
55
|
+
}
|
|
56
|
+
function compact(object) {
|
|
57
|
+
return Object.fromEntries(Object.entries(object).filter(([, value]) => value !== undefined));
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Build the APNs JSON payload: `aps` plus the data keys beside it.
|
|
61
|
+
* @param content - The message.
|
|
62
|
+
* @returns The payload object, with `rawJson` merged last.
|
|
63
|
+
*/
|
|
64
|
+
export function buildApnsPayload(content) {
|
|
65
|
+
const apns = content.overrides?.apns;
|
|
66
|
+
const silent = isSilent(content);
|
|
67
|
+
const aps = compact({
|
|
68
|
+
alert: silent ? undefined : compact({ title: content.title, body: content.body }),
|
|
69
|
+
badge: content.options.badge,
|
|
70
|
+
sound: content.options.sound,
|
|
71
|
+
category: content.options.category,
|
|
72
|
+
"thread-id": apns?.threadId,
|
|
73
|
+
"interruption-level": apns?.interruptionLevel,
|
|
74
|
+
"mutable-content": apns?.mutableContent ? 1 : undefined,
|
|
75
|
+
"content-available": silent ? 1 : undefined,
|
|
76
|
+
});
|
|
77
|
+
return withRaw({ ...content.data, aps }, apns?.rawJson);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Build the FCM HTTP v1 `message` object without its target. Data values become
|
|
81
|
+
* strings, as FCM requires. Topic and condition targets set by `rawJson` are
|
|
82
|
+
* removed and the token is added afterwards, so an override can never change
|
|
83
|
+
* who receives the message.
|
|
84
|
+
* @param content - The message.
|
|
85
|
+
* @param ttlSeconds - Remaining time to live.
|
|
86
|
+
* @returns The `message` object minus `token`.
|
|
87
|
+
*/
|
|
88
|
+
export function buildFcmBody(content, ttlSeconds) {
|
|
89
|
+
const fcm = content.overrides?.fcm;
|
|
90
|
+
const silent = isSilent(content);
|
|
91
|
+
const dataEntries = Object.entries(content.data);
|
|
92
|
+
const channelId = fcm?.channelId ?? content.options.category;
|
|
93
|
+
const androidNotification = silent ? undefined : compact({ sound: content.options.sound, channel_id: channelId });
|
|
94
|
+
const built = compact({
|
|
95
|
+
notification: silent ? undefined : compact({ title: content.title, body: content.body }),
|
|
96
|
+
data: dataEntries.length > 0 ? Object.fromEntries(dataEntries.map(([k, v]) => [k, String(v)])) : undefined,
|
|
97
|
+
android: compact({
|
|
98
|
+
priority: content.options.priority === "high" ? "HIGH" : "NORMAL",
|
|
99
|
+
ttl: `${String(ttlSeconds)}s`,
|
|
100
|
+
collapse_key: content.options.collapseKey,
|
|
101
|
+
notification: androidNotification && Object.keys(androidNotification).length > 0 ? androidNotification : undefined,
|
|
102
|
+
}),
|
|
103
|
+
});
|
|
104
|
+
const { topic: _topic, condition: _condition, ...merged } = withRaw(built, fcm?.rawJson);
|
|
105
|
+
return merged;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The FCM `message` for one token: {@link buildFcmBody} plus the token.
|
|
109
|
+
* @param content - The message.
|
|
110
|
+
* @param token - The device's FCM registration token.
|
|
111
|
+
* @param ttlSeconds - Remaining time to live.
|
|
112
|
+
* @returns The `message` object.
|
|
113
|
+
*/
|
|
114
|
+
export function buildFcmMessage(content, token, ttlSeconds) {
|
|
115
|
+
return { ...buildFcmBody(content, ttlSeconds), token };
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Build the JSON the service worker receives for a Web Push message.
|
|
119
|
+
* @param content - The message.
|
|
120
|
+
* @returns The payload object, with `rawJson` merged last.
|
|
121
|
+
*/
|
|
122
|
+
export function buildWebPushPayload(content) {
|
|
123
|
+
const webpush = content.overrides?.webpush;
|
|
124
|
+
return withRaw(compact({
|
|
125
|
+
title: content.title,
|
|
126
|
+
body: content.body,
|
|
127
|
+
data: content.data,
|
|
128
|
+
icon: webpush?.icon,
|
|
129
|
+
url: webpush?.url,
|
|
130
|
+
badge: content.options.badge,
|
|
131
|
+
tag: content.options.collapseKey,
|
|
132
|
+
}), webpush?.rawJson);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Check each built-in provider's payload against its limit, after `rawJson` is merged.
|
|
136
|
+
* @param content - The message.
|
|
137
|
+
* @param platforms - The platforms this message fans out to.
|
|
138
|
+
* @returns The first platform over its limit, or `null`.
|
|
139
|
+
*/
|
|
140
|
+
export function findOversize(content, platforms) {
|
|
141
|
+
const ttl = content.options.ttlSeconds ?? 0;
|
|
142
|
+
const checks = [];
|
|
143
|
+
if (platforms.has("apns")) {
|
|
144
|
+
checks.push({ platform: "apns", bytes: byteLength(JSON.stringify(buildApnsPayload(content))), limit: APNS_MAX_PAYLOAD_BYTES });
|
|
145
|
+
}
|
|
146
|
+
if (platforms.has("fcm")) {
|
|
147
|
+
checks.push({
|
|
148
|
+
platform: "fcm",
|
|
149
|
+
bytes: byteLength(JSON.stringify(buildFcmBody(content, ttl))),
|
|
150
|
+
limit: FCM_MAX_PAYLOAD_BYTES,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
if (platforms.has("webpush")) {
|
|
154
|
+
checks.push({
|
|
155
|
+
platform: "webpush",
|
|
156
|
+
bytes: byteLength(JSON.stringify(buildWebPushPayload(content))),
|
|
157
|
+
limit: WEBPUSH_MAX_PLAINTEXT_BYTES,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
return checks.find((check) => check.bytes > check.limit) ?? null;
|
|
161
|
+
}
|
|
162
|
+
//# sourceMappingURL=payload.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payload.js","sourceRoot":"","sources":["../../../src/component/providers/payload.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EACtB,qBAAqB,EACrB,mBAAmB,EACnB,2BAA2B,GAC5B,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAehD;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,OAA4C;IACnE,OAAO,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC;AACzC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,GAAuB;IAClD,IAAI,GAAG,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IACxC,IAAI,GAAG,CAAC,MAAM,GAAG,mBAAmB;QAAE,OAAO,IAAI,CAAC;IAClD,IAAI,CAAC;QACH,MAAM,MAAM,GAAY,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACxC,OAAO,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC;IAC9C,CAAC;IAAC,MAA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AQ,EAAE,SAAS;YACnB,KAAK,EAAE,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC,CAAC;YAC/D,KAAK,EAAE,2BAA2B;SACnC,CAAC,CAAC;IACL,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC;AACnE,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type Sealed } from "../crypto/token_seal.js";
|
|
2
|
+
import type { SendResult } from "../validators.js";
|
|
3
|
+
/** A freshly minted provider token and when to stop using it. */
|
|
4
|
+
export type Minted = {
|
|
5
|
+
ok: false;
|
|
6
|
+
result: SendResult;
|
|
7
|
+
} | {
|
|
8
|
+
ok: true;
|
|
9
|
+
token: string;
|
|
10
|
+
refreshAfter: number;
|
|
11
|
+
expiresAt: number;
|
|
12
|
+
};
|
|
13
|
+
/** A token from the cache, or the sender result to return when none can be had. */
|
|
14
|
+
export type TokenRead = {
|
|
15
|
+
ok: false;
|
|
16
|
+
result: SendResult;
|
|
17
|
+
} | {
|
|
18
|
+
ok: true;
|
|
19
|
+
token: string;
|
|
20
|
+
};
|
|
21
|
+
/** A stored cache row, as the storage callbacks see it. */
|
|
22
|
+
export interface StoredToken extends Sealed {
|
|
23
|
+
refreshAfter: number;
|
|
24
|
+
expiresAt: number;
|
|
25
|
+
}
|
|
26
|
+
/** Which cached token: a credential fingerprint, plus the push service origin for VAPID. */
|
|
27
|
+
export interface TokenSlot {
|
|
28
|
+
credentialRef: string;
|
|
29
|
+
audience?: string;
|
|
30
|
+
}
|
|
31
|
+
/** Storage the cache runs on. The action wires these to internal queries and mutations. */
|
|
32
|
+
export interface TokenStorage {
|
|
33
|
+
load(slot: TokenSlot): Promise<StoredToken | null>;
|
|
34
|
+
/** Store a minted token. Keeps a fresher row written by a concurrent sender and returns the row that won. */
|
|
35
|
+
store(slot: TokenSlot, row: StoredToken & {
|
|
36
|
+
mintedAt: number;
|
|
37
|
+
}): Promise<Sealed>;
|
|
38
|
+
drop(slot: TokenSlot): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
/** The shared provider-token cache used by every built-in sender. */
|
|
41
|
+
export interface TokenCache {
|
|
42
|
+
get(slot: TokenSlot, secret: string, mint: () => Promise<Minted>): Promise<TokenRead>;
|
|
43
|
+
drop(slot: TokenSlot): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Build the cache. Tokens are stored encrypted under a key derived from the
|
|
47
|
+
* long-lived private key; a stored token is reused until `refreshAfter`. When
|
|
48
|
+
* a refresh fails, a stored token that has not expired is still used. A stored
|
|
49
|
+
* row that cannot be opened is dropped before minting, so it cannot win the
|
|
50
|
+
* store and force a new token on every send.
|
|
51
|
+
* @param storage - Load, store and drop callbacks.
|
|
52
|
+
* @param now - Clock.
|
|
53
|
+
* @returns The cache.
|
|
54
|
+
*/
|
|
55
|
+
export declare function createTokenCache(storage: TokenStorage, now: () => number): TokenCache;
|
|
56
|
+
//# sourceMappingURL=token_cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token_cache.d.ts","sourceRoot":"","sources":["../../../src/component/providers/token_cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,MAAM,EAAQ,MAAM,yBAAyB,CAAC;AAClF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,iEAAiE;AACjE,MAAM,MAAM,MAAM,GACd;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,UAAU,CAAA;CAAE,GACjC;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAAC;AAEzE,mFAAmF;AACnF,MAAM,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
|
3
|
+
return `${slot.credentialRef}|${slot.audience ?? ""}`;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Build the cache. Tokens are stored encrypted under a key derived from the
|
|
7
|
+
* long-lived private key; a stored token is reused until `refreshAfter`. When
|
|
8
|
+
* a refresh fails, a stored token that has not expired is still used. A stored
|
|
9
|
+
* row that cannot be opened is dropped before minting, so it cannot win the
|
|
10
|
+
* store and force a new token on every send.
|
|
11
|
+
* @param storage - Load, store and drop callbacks.
|
|
12
|
+
* @param now - Clock.
|
|
13
|
+
* @returns The cache.
|
|
14
|
+
*/
|
|
15
|
+
export function createTokenCache(storage, now) {
|
|
16
|
+
return {
|
|
17
|
+
async get(slot, secret, mint) {
|
|
18
|
+
const key = await deriveCacheKey(secret, slot.credentialRef);
|
|
19
|
+
const aad = aadFor(slot);
|
|
20
|
+
const row = await storage.load(slot);
|
|
21
|
+
const live = row !== null && now() < row.expiresAt;
|
|
22
|
+
const cached = live ? await open(key, row, aad) : null;
|
|
23
|
+
if (cached && row && now() < row.refreshAfter)
|
|
24
|
+
return { ok: true, token: cached };
|
|
25
|
+
if (live && !cached)
|
|
26
|
+
await storage.drop(slot);
|
|
27
|
+
const minted = await mint();
|
|
28
|
+
if (!minted.ok)
|
|
29
|
+
return cached ? { ok: true, token: cached } : minted;
|
|
30
|
+
const sealed = await seal(key, minted.token, aad);
|
|
31
|
+
const winner = await storage.store(slot, {
|
|
32
|
+
...sealed,
|
|
33
|
+
mintedAt: now(),
|
|
34
|
+
refreshAfter: minted.refreshAfter,
|
|
35
|
+
expiresAt: minted.expiresAt,
|
|
36
|
+
});
|
|
37
|
+
return { ok: true, token: (await open(key, winner, aad)) ?? minted.token };
|
|
38
|
+
},
|
|
39
|
+
drop: (slot) => storage.drop(slot),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=token_cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token_cache.js","sourceRoot":"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,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;YACrE,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;YAClD,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE;gBACvC,GAAG,MAAM;gBACT,QAAQ,EAAE,GAAG,EAAE;gBACf,YAAY,EAAE,MAAM,CAAC,YAAY;gBACjC,SAAS,EAAE,MAAM,CAAC,SAAS;aAC5B,CAAC,CAAC;YACH,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;QAC7E,CAAC;QACD,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC;KACnC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { SendResult } from "../validators.js";
|
|
2
|
+
import { type PushEnv } from "./keys.js";
|
|
3
|
+
import { type PushContent } from "./payload.js";
|
|
4
|
+
import type { TokenCache } from "./token_cache.js";
|
|
5
|
+
/** One Web Push delivery. */
|
|
6
|
+
export interface WebPushJob {
|
|
7
|
+
messageId: string;
|
|
8
|
+
endpoint: string;
|
|
9
|
+
p256dh: string;
|
|
10
|
+
auth: string;
|
|
11
|
+
content: PushContent;
|
|
12
|
+
expiresAt: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The Topic header: the collapse key (or the message id) hashed to 32 URL-safe
|
|
16
|
+
* characters, the most Web Push allows.
|
|
17
|
+
* @param job - The delivery.
|
|
18
|
+
* @returns The header value.
|
|
19
|
+
*/
|
|
20
|
+
export declare function webPushTopic(job: Pick<WebPushJob, "content" | "messageId">): Promise<string>;
|
|
21
|
+
/**
|
|
22
|
+
* Map a push service response to a sender result. 401 and 403 mean the push
|
|
23
|
+
* service refused the VAPID token: the sender drops it and the retry signs a
|
|
24
|
+
* new one.
|
|
25
|
+
* @param status - HTTP status.
|
|
26
|
+
* @param headers - Response headers.
|
|
27
|
+
* @param now - Clock.
|
|
28
|
+
* @returns The result.
|
|
29
|
+
*/
|
|
30
|
+
export declare function classifyWebPush(status: number, headers: Headers, now: number): SendResult;
|
|
31
|
+
/**
|
|
32
|
+
* Encrypt and send one Web Push message, signed with VAPID (RFC 8292).
|
|
33
|
+
* @param job - The delivery.
|
|
34
|
+
* @param env - Component env with the VAPID keys.
|
|
35
|
+
* @param cache - Provider-token cache; one VAPID JWT per push service origin.
|
|
36
|
+
* @param now - Clock.
|
|
37
|
+
* @returns The sender result.
|
|
38
|
+
*/
|
|
39
|
+
export declare function sendWebPush(job: WebPushJob, env: PushEnv, cache: TokenCache, now: () => number): Promise<SendResult>;
|
|
40
|
+
//# sourceMappingURL=web_push.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web_push.d.ts","sourceRoot":"","sources":["../../../src/component/providers/web_push.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,OAAO,EAAE,KAAK,OAAO,EAAa,MAAM,WAAW,CAAC;AACpD,OAAO,EAAuB,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AACrE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAMnD,6BAA6B;AAC7B,MAAM,WAAW,UAAU;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,WAAW,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,GAAG,EAAE,IAAI,CAAC,UAAU,EAAE,SAAS,GAAG,WAAW,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAElG;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,UAAU,CASzF;AAED;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAuC1H"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { base64Decode, sha256Base64Url, utf8 } from "../crypto/codec.js";
|
|
2
|
+
import { importRawP256PrivateKey, signJwt } from "../crypto/jwt.js";
|
|
3
|
+
import { encryptWebPush } from "../crypto/web_push_crypto.js";
|
|
4
|
+
import { parseRetryAfter, providerFetch } from "./http.js";
|
|
5
|
+
import { readVapid } from "./keys.js";
|
|
6
|
+
import { buildWebPushPayload } from "./payload.js";
|
|
7
|
+
/** A VAPID JWT may live 24 hours. Sign for 24 hours minus a minute, reuse for 12 hours. */
|
|
8
|
+
const VAPID_LIFETIME_MS = 86_340_000;
|
|
9
|
+
const VAPID_REUSE_MS = 43_200_000;
|
|
10
|
+
/**
|
|
11
|
+
* The Topic header: the collapse key (or the message id) hashed to 32 URL-safe
|
|
12
|
+
* characters, the most Web Push allows.
|
|
13
|
+
* @param job - The delivery.
|
|
14
|
+
* @returns The header value.
|
|
15
|
+
*/
|
|
16
|
+
export async function webPushTopic(job) {
|
|
17
|
+
return (await sha256Base64Url(job.content.options.collapseKey ?? job.messageId)).slice(0, 32);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Map a push service response to a sender result. 401 and 403 mean the push
|
|
21
|
+
* service refused the VAPID token: the sender drops it and the retry signs a
|
|
22
|
+
* new one.
|
|
23
|
+
* @param status - HTTP status.
|
|
24
|
+
* @param headers - Response headers.
|
|
25
|
+
* @param now - Clock.
|
|
26
|
+
* @returns The result.
|
|
27
|
+
*/
|
|
28
|
+
export function classifyWebPush(status, headers, now) {
|
|
29
|
+
if (status >= 200 && status < 300) {
|
|
30
|
+
return { status: "sent", providerMessageId: headers.get("location") ?? undefined, httpStatus: status };
|
|
31
|
+
}
|
|
32
|
+
if (status === 404 || status === 410)
|
|
33
|
+
return { status: "invalid", code: status === 404 ? "NOT_FOUND" : "GONE", httpStatus: status };
|
|
34
|
+
if (status === 401 || status === 403 || status === 429 || status >= 500) {
|
|
35
|
+
return { status: "retry", code: `HTTP_${String(status)}`, httpStatus: status, delayMs: parseRetryAfter(headers.get("retry-after"), now) };
|
|
36
|
+
}
|
|
37
|
+
return { status: "failed", code: status === 413 ? "PAYLOAD_TOO_LARGE" : `HTTP_${String(status)}`, httpStatus: status };
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Encrypt and send one Web Push message, signed with VAPID (RFC 8292).
|
|
41
|
+
* @param job - The delivery.
|
|
42
|
+
* @param env - Component env with the VAPID keys.
|
|
43
|
+
* @param cache - Provider-token cache; one VAPID JWT per push service origin.
|
|
44
|
+
* @param now - Clock.
|
|
45
|
+
* @returns The sender result.
|
|
46
|
+
*/
|
|
47
|
+
export async function sendWebPush(job, env, cache, now) {
|
|
48
|
+
const key = await readVapid(env);
|
|
49
|
+
if (!key.ok)
|
|
50
|
+
return { status: "failed", code: key.code };
|
|
51
|
+
const audience = new URL(job.endpoint).origin;
|
|
52
|
+
const jwt = await cache.get({ credentialRef: key.value.credentialRef, audience }, key.value.privateKey, async () => {
|
|
53
|
+
const signingKey = await importRawP256PrivateKey(key.value.privateKey, key.value.publicKey, "sign");
|
|
54
|
+
if (!signingKey)
|
|
55
|
+
return { ok: false, result: { status: "failed", code: "CREDENTIALS_INVALID" } };
|
|
56
|
+
const issuedAt = now();
|
|
57
|
+
const expiresAt = issuedAt + VAPID_LIFETIME_MS;
|
|
58
|
+
const token = await signJwt({}, { aud: audience, exp: Math.floor(expiresAt / 1000), sub: key.value.subject }, signingKey, "ES256");
|
|
59
|
+
return { ok: true, token, refreshAfter: issuedAt + VAPID_REUSE_MS, expiresAt };
|
|
60
|
+
});
|
|
61
|
+
if (!jwt.ok)
|
|
62
|
+
return jwt.result;
|
|
63
|
+
const uaPublic = base64Decode(job.p256dh);
|
|
64
|
+
const authSecret = base64Decode(job.auth);
|
|
65
|
+
if (!uaPublic || !authSecret)
|
|
66
|
+
return { status: "invalid", code: "BAD_SUBSCRIPTION_KEYS" };
|
|
67
|
+
let body;
|
|
68
|
+
try {
|
|
69
|
+
body = await encryptWebPush({ plaintext: utf8(JSON.stringify(buildWebPushPayload(job.content))), uaPublic, authSecret });
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return { status: "invalid", code: "BAD_SUBSCRIPTION_KEYS" };
|
|
73
|
+
}
|
|
74
|
+
const ttl = job.content.options.ttlSeconds === 0 ? 0 : Math.max(Math.floor((job.expiresAt - now()) / 1000), 0);
|
|
75
|
+
const http = await providerFetch(job.endpoint, {
|
|
76
|
+
method: "POST",
|
|
77
|
+
headers: {
|
|
78
|
+
authorization: `vapid t=${jwt.token}, k=${key.value.publicKey}`,
|
|
79
|
+
"content-encoding": "aes128gcm",
|
|
80
|
+
"content-type": "application/octet-stream",
|
|
81
|
+
ttl: String(ttl),
|
|
82
|
+
urgency: job.content.options.priority === "high" ? "high" : "normal",
|
|
83
|
+
topic: await webPushTopic(job),
|
|
84
|
+
},
|
|
85
|
+
body,
|
|
86
|
+
});
|
|
87
|
+
if (!http.ok)
|
|
88
|
+
return http.result;
|
|
89
|
+
const { status } = http.response;
|
|
90
|
+
if (status === 401 || status === 403)
|
|
91
|
+
await cache.drop({ credentialRef: key.value.credentialRef, audience });
|
|
92
|
+
return classifyWebPush(status, http.response.headers, now());
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=web_push.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":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AAC;AAC/D,CAAC"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/** A message and the state of each delivery. Never returns title, body, data or device addresses. */
|
|
2
|
+
export declare const status: import("convex/server").RegisteredQuery<"public", {
|
|
3
|
+
messageId: string;
|
|
4
|
+
}, Promise<{
|
|
5
|
+
messageId: import("convex/values").GenericId<"messages">;
|
|
6
|
+
createdAt: number;
|
|
7
|
+
expiresAt: number;
|
|
8
|
+
scrubbedAt: number | undefined;
|
|
9
|
+
deliveries: {
|
|
10
|
+
attempts: number;
|
|
11
|
+
deliveryId: string;
|
|
12
|
+
deviceId: string;
|
|
13
|
+
errorCode?: string | undefined;
|
|
14
|
+
httpStatus?: number | undefined;
|
|
15
|
+
nextAttemptAt?: number | undefined;
|
|
16
|
+
platform: "apns" | "custom" | "fcm" | "webpush";
|
|
17
|
+
provider?: string | undefined;
|
|
18
|
+
providerMessageId?: string | undefined;
|
|
19
|
+
sentAt?: number | undefined;
|
|
20
|
+
status: "failed" | "invalid" | "queued" | "sent";
|
|
21
|
+
updatedAt: number;
|
|
22
|
+
}[];
|
|
23
|
+
} | null>>;
|
|
24
|
+
/** A subject's devices, optionally filtered by status. Addresses are never returned. */
|
|
25
|
+
export declare const listDevices: import("convex/server").RegisteredQuery<"public", {
|
|
26
|
+
limit?: number | undefined;
|
|
27
|
+
status?: "active" | "disabled" | "invalid" | undefined;
|
|
28
|
+
subjectRef: string;
|
|
29
|
+
}, Promise<{
|
|
30
|
+
appRef: string;
|
|
31
|
+
deviceId: string;
|
|
32
|
+
installRef?: string | undefined;
|
|
33
|
+
invalidCode?: string | undefined;
|
|
34
|
+
lastSeenAt: number;
|
|
35
|
+
locale?: string | undefined;
|
|
36
|
+
platform: "apns" | "custom" | "fcm" | "webpush";
|
|
37
|
+
provider?: string | undefined;
|
|
38
|
+
status: "active" | "disabled" | "invalid";
|
|
39
|
+
subjectRef: string;
|
|
40
|
+
}[]>>;
|
|
41
|
+
/** A subject's recent messages, newest first, with delivery counts. */
|
|
42
|
+
export declare const listBySubject: import("convex/server").RegisteredQuery<"public", {
|
|
43
|
+
limit?: number | undefined;
|
|
44
|
+
subjectRef: string;
|
|
45
|
+
}, Promise<{
|
|
46
|
+
messageId: import("convex/values").GenericId<"messages">;
|
|
47
|
+
createdAt: number;
|
|
48
|
+
expiresAt: number;
|
|
49
|
+
scrubbedAt: number | undefined;
|
|
50
|
+
queued: number;
|
|
51
|
+
sent: number;
|
|
52
|
+
failed: number;
|
|
53
|
+
invalid: number;
|
|
54
|
+
}[]>>;
|
|
55
|
+
/**
|
|
56
|
+
* Everything the send action needs for one queued delivery. Internal: the host
|
|
57
|
+
* cannot call it, and it is the only function that reads content and addresses.
|
|
58
|
+
*/
|
|
59
|
+
export declare const loadJob: import("convex/server").RegisteredQuery<"internal", {
|
|
60
|
+
deliveryId: import("convex/values").GenericId<"deliveries">;
|
|
61
|
+
}, Promise<{
|
|
62
|
+
deliveryId: import("convex/values").GenericId<"deliveries">;
|
|
63
|
+
messageId: import("convex/values").GenericId<"messages">;
|
|
64
|
+
platform: "apns" | "custom" | "fcm" | "webpush";
|
|
65
|
+
provider: string | undefined;
|
|
66
|
+
senderHandle: string | undefined;
|
|
67
|
+
apns: {
|
|
68
|
+
environment: "production" | "sandbox";
|
|
69
|
+
topic: string;
|
|
70
|
+
} | undefined;
|
|
71
|
+
attempts: number;
|
|
72
|
+
expiresAt: number;
|
|
73
|
+
title: string | undefined;
|
|
74
|
+
body: string | undefined;
|
|
75
|
+
data: Record<string, string | number | boolean>;
|
|
76
|
+
options: {
|
|
77
|
+
badge?: number | undefined;
|
|
78
|
+
category?: string | undefined;
|
|
79
|
+
collapseKey?: string | undefined;
|
|
80
|
+
priority: "high" | "normal";
|
|
81
|
+
sound?: string | undefined;
|
|
82
|
+
ttlSeconds?: number | undefined;
|
|
83
|
+
};
|
|
84
|
+
overrides: {
|
|
85
|
+
apns?: {
|
|
86
|
+
interruptionLevel?: "active" | "critical" | "passive" | "time-sensitive" | undefined;
|
|
87
|
+
mutableContent?: boolean | undefined;
|
|
88
|
+
rawJson?: string | undefined;
|
|
89
|
+
threadId?: string | undefined;
|
|
90
|
+
} | undefined;
|
|
91
|
+
fcm?: {
|
|
92
|
+
channelId?: string | undefined;
|
|
93
|
+
rawJson?: string | undefined;
|
|
94
|
+
} | undefined;
|
|
95
|
+
webpush?: {
|
|
96
|
+
icon?: string | undefined;
|
|
97
|
+
rawJson?: string | undefined;
|
|
98
|
+
url?: string | undefined;
|
|
99
|
+
} | undefined;
|
|
100
|
+
} | undefined;
|
|
101
|
+
device: {
|
|
102
|
+
status: "active" | "disabled" | "invalid";
|
|
103
|
+
address: {
|
|
104
|
+
platform: "apns";
|
|
105
|
+
token: string;
|
|
106
|
+
} | {
|
|
107
|
+
platform: "fcm";
|
|
108
|
+
token: string;
|
|
109
|
+
} | {
|
|
110
|
+
auth: string;
|
|
111
|
+
endpoint: string;
|
|
112
|
+
p256dh: string;
|
|
113
|
+
platform: "webpush";
|
|
114
|
+
} | {
|
|
115
|
+
address: string;
|
|
116
|
+
platform: "custom";
|
|
117
|
+
provider: string;
|
|
118
|
+
};
|
|
119
|
+
reassigned: boolean;
|
|
120
|
+
} | null;
|
|
121
|
+
} | null>>;
|
|
122
|
+
/** A cached provider token row (ciphertext). Internal. */
|
|
123
|
+
export declare const loadToken: import("convex/server").RegisteredQuery<"internal", {
|
|
124
|
+
audience?: string | undefined;
|
|
125
|
+
credentialRef: string;
|
|
126
|
+
}, Promise<{
|
|
127
|
+
cipher: ArrayBuffer;
|
|
128
|
+
iv: ArrayBuffer;
|
|
129
|
+
refreshAfter: number;
|
|
130
|
+
expiresAt: number;
|
|
131
|
+
} | null>>;
|
|
132
|
+
//# sourceMappingURL=queries.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queries.d.ts","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AA4CA,qGAAqG;AACrG,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;UAmBjB,CAAC;AAEH,wFAAwF;AACxF,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;KActB,CAAC;AAEH,uEAAuE;AACvE,eAAO,MAAM,aAAa;;;;;;;;;;;;KA+BxB,CAAC;AAEH;;;GAGG;AACH,eAAO,MAAM,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UA4ClB,CAAC;AAEH,0DAA0D;AAC1D,eAAO,MAAM,SAAS;;;;;;;;UAapB,CAAC"}
|