@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,102 @@
1
+ /** Byte and text encodings shared by the signers and the encryption code. Web APIs only. */
2
+ const encoder = new TextEncoder();
3
+ const decoder = new TextDecoder();
4
+ /**
5
+ * Encode text as UTF-8.
6
+ * @param text - Any string.
7
+ * @returns Its UTF-8 bytes.
8
+ */
9
+ export function utf8(text) {
10
+ return encoder.encode(text);
11
+ }
12
+ /**
13
+ * Decode UTF-8 bytes.
14
+ * @param bytes - UTF-8 bytes.
15
+ * @returns The decoded string.
16
+ */
17
+ export function fromUtf8(bytes) {
18
+ return decoder.decode(bytes);
19
+ }
20
+ /**
21
+ * Concatenate byte arrays.
22
+ * @param parts - Arrays to join, in order.
23
+ * @returns One array.
24
+ */
25
+ export function concat(...parts) {
26
+ const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
27
+ let offset = 0;
28
+ for (const part of parts) {
29
+ out.set(part, offset);
30
+ offset += part.length;
31
+ }
32
+ return out;
33
+ }
34
+ /**
35
+ * Copy any byte view into a fresh ArrayBuffer-backed array.
36
+ * @param input - An ArrayBuffer or byte view.
37
+ * @returns A copy.
38
+ */
39
+ export function toBytes(input) {
40
+ return new Uint8Array(input instanceof Uint8Array ? input : new Uint8Array(input));
41
+ }
42
+ /**
43
+ * Encode bytes as unpadded base64url (RFC 4648 section 5).
44
+ * @param bytes - Bytes to encode.
45
+ * @returns The base64url string.
46
+ */
47
+ export function base64UrlEncode(bytes) {
48
+ let binary = "";
49
+ for (const byte of bytes)
50
+ binary += String.fromCodePoint(byte);
51
+ return btoa(binary).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
52
+ }
53
+ /**
54
+ * Decode base64 or base64url, padded or not.
55
+ * @param text - The encoded string. Whitespace is ignored.
56
+ * @returns The bytes, or `null` when the input is not base64.
57
+ */
58
+ export function base64Decode(text) {
59
+ const normalized = text.replaceAll(/\s+/g, "").replaceAll("-", "+").replaceAll("_", "/");
60
+ if (!/^[A-Za-z0-9+/]*={0,2}$/.test(normalized) || normalized.replace(/=+$/, "").length % 4 === 1) {
61
+ return null;
62
+ }
63
+ const padded = normalized.padEnd(Math.ceil(normalized.length / 4) * 4, "=");
64
+ const binary = atob(padded);
65
+ const out = new Uint8Array(binary.length);
66
+ for (let i = 0; i < binary.length; i++)
67
+ out[i] = binary.charCodeAt(i);
68
+ return out;
69
+ }
70
+ /**
71
+ * Encode bytes as lowercase hex.
72
+ * @param bytes - Bytes to encode.
73
+ * @returns The hex string.
74
+ */
75
+ export function toHex(bytes) {
76
+ return [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
77
+ }
78
+ /**
79
+ * SHA-256 of a string, as hex.
80
+ * @param text - Input text, hashed as UTF-8.
81
+ * @returns 64 hex characters.
82
+ */
83
+ export async function sha256Hex(text) {
84
+ return toHex(new Uint8Array(await crypto.subtle.digest("SHA-256", utf8(text))));
85
+ }
86
+ /**
87
+ * SHA-256 of a string, as base64url.
88
+ * @param text - Input text, hashed as UTF-8.
89
+ * @returns 43 base64url characters.
90
+ */
91
+ export async function sha256Base64Url(text) {
92
+ return base64UrlEncode(new Uint8Array(await crypto.subtle.digest("SHA-256", utf8(text))));
93
+ }
94
+ /**
95
+ * Size of a string in UTF-8 bytes.
96
+ * @param text - Any string.
97
+ * @returns The byte length.
98
+ */
99
+ export function byteLength(text) {
100
+ return utf8(text).length;
101
+ }
102
+ //# sourceMappingURL=codec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codec.js","sourceRoot":"","sources":["../../../src/component/crypto/codec.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAK5F,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAClC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAElC;;;;GAIG;AACH,MAAM,UAAU,IAAI,CAAC,IAAY;IAC/B,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AAC9B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAiB;IACxC,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC/B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,MAAM,CAAC,GAAG,KAAmB;IAC3C,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;IACpE,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,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for Google. */
3
+ export type JwtAlgorithm = "ES256" | "RS256";
4
+ /**
5
+ * Read the DER bytes of a PEM private key. Accepts the PEM armour or bare
6
+ * base64, and literal `\n` escapes as environment variables often carry them.
7
+ * @param pem - The key text.
8
+ * @returns The DER bytes, or `null` when the text is not a key.
9
+ */
10
+ export declare function pemToDer(pem: string): Bytes | null;
11
+ /**
12
+ * Import a PKCS#8 private key for signing.
13
+ * @param pem - PEM text of the key (an Apple .p8 file or a Google service-account key).
14
+ * @param algorithm - Which signature it makes.
15
+ * @returns The signing key, or `null` when the key cannot be imported.
16
+ */
17
+ export declare function importSigningKey(pem: string, algorithm: JwtAlgorithm): Promise<CryptoKey | null>;
18
+ /**
19
+ * Split an uncompressed P-256 public point (65 bytes, leading 0x04) into JWK coordinates.
20
+ * @param point - The raw public key.
21
+ * @returns `x` and `y` as base64url, or `null` when the point is malformed.
22
+ */
23
+ export declare function pointToCoordinates(point: Uint8Array): {
24
+ x: string;
25
+ y: string;
26
+ } | null;
27
+ /**
28
+ * Import a raw P-256 private scalar plus its public point, the format VAPID
29
+ * keys and RFC 8291 test vectors use.
30
+ * @param privateKey - base64url of the 32-byte scalar.
31
+ * @param publicKey - base64url of the 65-byte uncompressed point.
32
+ * @param usage - `sign` for ECDSA, `deriveBits` for ECDH.
33
+ * @returns The key, or `null` when the input does not form a valid key pair.
34
+ */
35
+ export declare function importRawP256PrivateKey(privateKey: string, publicKey: string, usage: "sign" | "deriveBits"): Promise<CryptoKey | null>;
36
+ /**
37
+ * Make a compact JWS. ES256 signatures come out of Web Crypto as raw r||s,
38
+ * which is the JWS encoding (RFC 7518 section 3.4).
39
+ * @param header - JOSE header; `alg` always comes from `algorithm`, never from it.
40
+ * @param claims - JWT claims.
41
+ * @param key - A signing key from {@link importSigningKey} or {@link importRawP256PrivateKey}.
42
+ * @param algorithm - ES256 or RS256.
43
+ * @returns `header.claims.signature`, each part base64url.
44
+ */
45
+ export declare function signJwt(header: Record<string, string>, claims: Record<string, number | string>, key: CryptoKey, algorithm: JwtAlgorithm): Promise<string>;
46
+ //# sourceMappingURL=jwt.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwt.d.ts","sourceRoot":"","sources":["../../../src/component/crypto/jwt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiC,KAAK,KAAK,EAAQ,MAAM,YAAY,CAAC;AAE7E,8FAA8F;AAC9F,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,OAAO,CAAC;AAK7C;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,KAAK,GAAG,IAAI,CAOlD;AAED;;;;;GAKG;AACH,wBAAsB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,YAAY,GAAG,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,CActG;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,UAAU,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAGrF;AAED;;;;;;;GAOG;AACH,wBAAsB,uBAAuB,CAC3C,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,MAAM,GAAG,YAAY,GAC3B,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,CAiB3B;AAED;;;;;;;;GAQG;AACH,wBAAsB,OAAO,CAC3B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC9B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,EACvC,GAAG,EAAE,SAAS,EACd,SAAS,EAAE,YAAY,GACtB,OAAO,CAAC,MAAM,CAAC,CAQjB"}
@@ -0,0 +1,82 @@
1
+ import { base64Decode, base64UrlEncode, utf8 } from "./codec.js";
2
+ const ECDSA_P256 = { name: "ECDSA", namedCurve: "P-256" };
3
+ const RSA_SHA256 = { name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" };
4
+ /**
5
+ * Read the DER bytes of a PEM private key. Accepts the PEM armour or bare
6
+ * base64, and literal `\n` escapes as environment variables often carry them.
7
+ * @param pem - The key text.
8
+ * @returns The DER bytes, or `null` when the text is not a key.
9
+ */
10
+ export function pemToDer(pem) {
11
+ const body = pem
12
+ .replaceAll(String.raw `\n`, "\n")
13
+ .replaceAll(/-----(BEGIN|END) [A-Z ]+-----/g, "")
14
+ .trim();
15
+ if (body.length === 0)
16
+ return null;
17
+ return base64Decode(body);
18
+ }
19
+ /**
20
+ * Import a PKCS#8 private key for signing.
21
+ * @param pem - PEM text of the key (an Apple .p8 file or a Google service-account key).
22
+ * @param algorithm - Which signature it makes.
23
+ * @returns The signing key, or `null` when the key cannot be imported.
24
+ */
25
+ export async function importSigningKey(pem, algorithm) {
26
+ const der = pemToDer(pem);
27
+ if (!der)
28
+ return null;
29
+ try {
30
+ return await crypto.subtle.importKey("pkcs8", der, algorithm === "ES256" ? ECDSA_P256 : RSA_SHA256, false, ["sign"]);
31
+ }
32
+ catch {
33
+ return null;
34
+ }
35
+ }
36
+ /**
37
+ * Split an uncompressed P-256 public point (65 bytes, leading 0x04) into JWK coordinates.
38
+ * @param point - The raw public key.
39
+ * @returns `x` and `y` as base64url, or `null` when the point is malformed.
40
+ */
41
+ export function pointToCoordinates(point) {
42
+ if (point.length !== 65 || point[0] !== 4)
43
+ return null;
44
+ return { x: base64UrlEncode(point.slice(1, 33)), y: base64UrlEncode(point.slice(33)) };
45
+ }
46
+ /**
47
+ * Import a raw P-256 private scalar plus its public point, the format VAPID
48
+ * keys and RFC 8291 test vectors use.
49
+ * @param privateKey - base64url of the 32-byte scalar.
50
+ * @param publicKey - base64url of the 65-byte uncompressed point.
51
+ * @param usage - `sign` for ECDSA, `deriveBits` for ECDH.
52
+ * @returns The key, or `null` when the input does not form a valid key pair.
53
+ */
54
+ export async function importRawP256PrivateKey(privateKey, publicKey, usage) {
55
+ const d = base64Decode(privateKey);
56
+ const point = base64Decode(publicKey);
57
+ const coordinates = point ? pointToCoordinates(point) : null;
58
+ if (d?.length !== 32 || !coordinates)
59
+ return null;
60
+ const jwk = { kty: "EC", crv: "P-256", d: base64UrlEncode(d), ...coordinates, ext: false };
61
+ try {
62
+ return await crypto.subtle.importKey("jwk", jwk, usage === "sign" ? ECDSA_P256 : { name: "ECDH", namedCurve: "P-256" }, false, [usage]);
63
+ }
64
+ catch {
65
+ return null;
66
+ }
67
+ }
68
+ /**
69
+ * Make a compact JWS. ES256 signatures come out of Web Crypto as raw r||s,
70
+ * which is the JWS encoding (RFC 7518 section 3.4).
71
+ * @param header - JOSE header; `alg` always comes from `algorithm`, never from it.
72
+ * @param claims - JWT claims.
73
+ * @param key - A signing key from {@link importSigningKey} or {@link importRawP256PrivateKey}.
74
+ * @param algorithm - ES256 or RS256.
75
+ * @returns `header.claims.signature`, each part base64url.
76
+ */
77
+ export async function signJwt(header, claims, key, algorithm) {
78
+ const input = `${base64UrlEncode(utf8(JSON.stringify({ typ: "JWT", ...header, alg: algorithm })))}.${base64UrlEncode(utf8(JSON.stringify(claims)))}`;
79
+ const signature = await crypto.subtle.sign(algorithm === "ES256" ? { name: "ECDSA", hash: "SHA-256" } : RSA_SHA256, key, utf8(input));
80
+ return `${input}.${base64UrlEncode(new Uint8Array(signature))}`;
81
+ }
82
+ //# sourceMappingURL=jwt.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwt.js","sourceRoot":"","sources":["../../../src/component/crypto/jwt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,eAAe,EAAc,IAAI,EAAE,MAAM,YAAY,CAAC;AAK7E,MAAM,UAAU,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAW,CAAC;AACnE,MAAM,UAAU,GAAG,EAAE,IAAI,EAAE,mBAAmB,EAAE,IAAI,EAAE,SAAS,EAAW,CAAC;AAE3E;;;;;GAKG;AACH,MAAM,UAAU,QAAQ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,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,UAAU,EACvE,GAAG,EACH,IAAI,CAAC,KAAK,CAAC,CACZ,CAAC;IACF,OAAO,GAAG,KAAK,IAAI,eAAe,CAAC,IAAI,UAAU,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC;AAClE,CAAC"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Derive the AES-256-GCM key that protects cached provider tokens. It comes
3
+ * from the long-lived private key, inside the action, and is never stored, so a
4
+ * database export holds no usable token.
5
+ * @param secret - The long-lived private key text.
6
+ * @param credentialRef - The credential fingerprint, bound into the key.
7
+ * @returns A non-extractable AES-GCM key.
8
+ */
9
+ export declare function deriveCacheKey(secret: string, credentialRef: string): Promise<CryptoKey>;
10
+ /** A sealed token as stored in `providerTokens`. */
11
+ export interface Sealed {
12
+ cipher: ArrayBuffer;
13
+ iv: ArrayBuffer;
14
+ }
15
+ /**
16
+ * Encrypt a token with a fresh 12-byte IV.
17
+ * @param key - From {@link deriveCacheKey}.
18
+ * @param token - The provider token.
19
+ * @param aad - Bound associated data (credential and audience), so a row cannot be swapped.
20
+ * @returns Ciphertext and IV.
21
+ */
22
+ export declare function seal(key: CryptoKey, token: string, aad: string): Promise<Sealed>;
23
+ /**
24
+ * Decrypt a sealed token.
25
+ * @param key - From {@link deriveCacheKey}.
26
+ * @param sealed - Stored ciphertext and IV.
27
+ * @param aad - The associated data used at sealing.
28
+ * @returns The token, or `null` when the key or data do not match.
29
+ */
30
+ export declare function open(key: CryptoKey, sealed: Sealed, aad: string): Promise<string | null>;
31
+ //# sourceMappingURL=token_seal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token_seal.d.ts","sourceRoot":"","sources":["../../../src/component/crypto/token_seal.ts"],"names":[],"mappings":"AAKA;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,CAG9F;AAED,oDAAoD;AACpD,MAAM,WAAW,MAAM;IACrB,MAAM,EAAE,WAAW,CAAC;IACpB,EAAE,EAAE,WAAW,CAAC;CACjB;AAED;;;;;;GAMG;AACH,wBAAsB,IAAI,CAAC,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAItF;AAED;;;;;;GAMG;AACH,wBAAsB,IAAI,CAAC,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAW9F"}
@@ -0,0 +1,44 @@
1
+ import { fromUtf8, toBytes, utf8 } from "./codec.js";
2
+ import { hkdf } from "./web_push_crypto.js";
3
+ const CACHE_SALT = utf8("@vllnt/convex-push token cache v1");
4
+ /**
5
+ * Derive the AES-256-GCM key that protects cached provider tokens. It comes
6
+ * from the long-lived private key, inside the action, and is never stored, so a
7
+ * database export holds no usable token.
8
+ * @param secret - The long-lived private key text.
9
+ * @param credentialRef - The credential fingerprint, bound into the key.
10
+ * @returns A non-extractable AES-GCM key.
11
+ */
12
+ export async function deriveCacheKey(secret, credentialRef) {
13
+ const raw = await hkdf(CACHE_SALT, utf8(secret), utf8(credentialRef), 32);
14
+ return crypto.subtle.importKey("raw", raw, "AES-GCM", false, ["encrypt", "decrypt"]);
15
+ }
16
+ /**
17
+ * Encrypt a token with a fresh 12-byte IV.
18
+ * @param key - From {@link deriveCacheKey}.
19
+ * @param token - The provider token.
20
+ * @param aad - Bound associated data (credential and audience), so a row cannot be swapped.
21
+ * @returns Ciphertext and IV.
22
+ */
23
+ export async function seal(key, token, aad) {
24
+ const iv = crypto.getRandomValues(new Uint8Array(12));
25
+ const cipher = await crypto.subtle.encrypt({ name: "AES-GCM", iv, additionalData: utf8(aad) }, key, utf8(token));
26
+ return { cipher, iv: iv.buffer };
27
+ }
28
+ /**
29
+ * Decrypt a sealed token.
30
+ * @param key - From {@link deriveCacheKey}.
31
+ * @param sealed - Stored ciphertext and IV.
32
+ * @param aad - The associated data used at sealing.
33
+ * @returns The token, or `null` when the key or data do not match.
34
+ */
35
+ export async function open(key, sealed, aad) {
36
+ try {
37
+ const plain = await crypto.subtle.decrypt({ name: "AES-GCM", iv: toBytes(sealed.iv), additionalData: utf8(aad) }, key, toBytes(sealed.cipher));
38
+ return fromUtf8(new Uint8Array(plain));
39
+ }
40
+ catch {
41
+ return null;
42
+ }
43
+ }
44
+ //# sourceMappingURL=token_seal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token_seal.js","sourceRoot":"","sources":["../../../src/component/crypto/token_seal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AACjE,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAE5C,MAAM,UAAU,GAAG,IAAI,CAAC,mCAAmC,CAAC,CAAC;AAE7D;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,MAAc,EAAE,aAAqB;IACxE,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,aAAa,CAAC,EAAE,EAAE,CAAC,CAAC;IAC1E,OAAO,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC;AACvF,CAAC;AAQD;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,GAAc,EAAE,KAAa,EAAE,GAAW;IACnE,MAAM,EAAE,GAAU,MAAM,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC;IAC7D,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,cAAc,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;IACjH,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,CAAC,MAAM,EAAE,CAAC;AACnC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,GAAc,EAAE,MAAc,EAAE,GAAW;IACpE,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,OAAO,CACvC,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,cAAc,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,EACtE,GAAG,EACH,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CACvB,CAAC;QACF,OAAO,QAAQ,CAAC,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;IACzC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,42 @@
1
+ import { type Bytes } from "./codec.js";
2
+ /**
3
+ * HKDF-SHA-256 (RFC 5869).
4
+ * @param salt - Salt bytes.
5
+ * @param ikm - Input keying material.
6
+ * @param info - Context info.
7
+ * @param length - Output length in bytes.
8
+ * @returns The derived bytes.
9
+ */
10
+ export declare function hkdf(salt: Bytes, ikm: Bytes, info: Bytes, length: number): Promise<Bytes>;
11
+ /** The sender's ephemeral ECDH key pair. Tests inject the RFC 8291 key; production generates one per message. */
12
+ export interface SenderKeys {
13
+ privateKey: CryptoKey;
14
+ publicKey: Bytes;
15
+ }
16
+ /**
17
+ * Make a fresh ephemeral ECDH key pair.
18
+ * @returns The private key and its raw public point.
19
+ */
20
+ export declare function generateSenderKeys(): Promise<SenderKeys>;
21
+ /** Inputs of one Web Push encryption. */
22
+ export interface EncryptInput {
23
+ plaintext: Bytes;
24
+ /** The subscription's `p256dh` key: the browser's public point. */
25
+ uaPublic: Bytes;
26
+ /** The subscription's `auth` secret: 16 bytes. */
27
+ authSecret: Bytes;
28
+ /** Defaults to 16 random bytes. */
29
+ salt?: Bytes;
30
+ /** Defaults to a fresh key pair. */
31
+ senderKeys?: SenderKeys;
32
+ }
33
+ /**
34
+ * Encrypt a push message with the aes128gcm content coding (RFC 8188) as Web
35
+ * Push uses it (RFC 8291): ECDH with the browser key, HKDF with the auth secret,
36
+ * then AES-128-GCM over one record ending in the 0x02 padding delimiter.
37
+ * @param input - Plaintext and subscription keys.
38
+ * @returns The request body: header (salt, record size, sender key) then ciphertext.
39
+ * @throws When the browser key is not a valid P-256 point.
40
+ */
41
+ export declare function encryptWebPush(input: EncryptInput): Promise<Bytes>;
42
+ //# sourceMappingURL=web_push_crypto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web_push_crypto.d.ts","sourceRoot":"","sources":["../../../src/component/crypto/web_push_crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,KAAK,EAAyB,MAAM,YAAY,CAAC;AAO/D;;;;;;;GAOG;AACH,wBAAsB,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,CAG/F;AAED,iHAAiH;AACjH,MAAM,WAAW,UAAU;IACzB,UAAU,EAAE,SAAS,CAAC;IACtB,SAAS,EAAE,KAAK,CAAC;CAClB;AAED;;;GAGG;AACH,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,UAAU,CAAC,CAG9D;AAED,yCAAyC;AACzC,MAAM,WAAW,YAAY;IAC3B,SAAS,EAAE,KAAK,CAAC;IACjB,mEAAmE;IACnE,QAAQ,EAAE,KAAK,CAAC;IAChB,kDAAkD;IAClD,UAAU,EAAE,KAAK,CAAC;IAClB,mCAAmC;IACnC,IAAI,CAAC,EAAE,KAAK,CAAC;IACb,oCAAoC;IACpC,UAAU,CAAC,EAAE,UAAU,CAAC;CACzB;AAED;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC,CAkBxE"}
@@ -0,0 +1,49 @@
1
+ import { concat, toBytes, utf8 } from "./codec.js";
2
+ /** Record size written in the aes128gcm header. One record is always enough for a push. */
3
+ const RECORD_SIZE = 4096;
4
+ /** Length of an uncompressed P-256 public point. */
5
+ const POINT_LENGTH = 65;
6
+ /**
7
+ * HKDF-SHA-256 (RFC 5869).
8
+ * @param salt - Salt bytes.
9
+ * @param ikm - Input keying material.
10
+ * @param info - Context info.
11
+ * @param length - Output length in bytes.
12
+ * @returns The derived bytes.
13
+ */
14
+ export async function hkdf(salt, ikm, info, length) {
15
+ const key = await crypto.subtle.importKey("raw", ikm, "HKDF", false, ["deriveBits"]);
16
+ return toBytes(await crypto.subtle.deriveBits({ name: "HKDF", hash: "SHA-256", salt, info }, key, length * 8));
17
+ }
18
+ /**
19
+ * Make a fresh ephemeral ECDH key pair.
20
+ * @returns The private key and its raw public point.
21
+ */
22
+ export async function generateSenderKeys() {
23
+ const pair = await crypto.subtle.generateKey({ name: "ECDH", namedCurve: "P-256" }, false, ["deriveBits"]);
24
+ return { privateKey: pair.privateKey, publicKey: toBytes(await crypto.subtle.exportKey("raw", pair.publicKey)) };
25
+ }
26
+ /**
27
+ * Encrypt a push message with the aes128gcm content coding (RFC 8188) as Web
28
+ * Push uses it (RFC 8291): ECDH with the browser key, HKDF with the auth secret,
29
+ * then AES-128-GCM over one record ending in the 0x02 padding delimiter.
30
+ * @param input - Plaintext and subscription keys.
31
+ * @returns The request body: header (salt, record size, sender key) then ciphertext.
32
+ * @throws When the browser key is not a valid P-256 point.
33
+ */
34
+ export async function encryptWebPush(input) {
35
+ const salt = input.salt ?? crypto.getRandomValues(new Uint8Array(16));
36
+ const sender = input.senderKeys ?? (await generateSenderKeys());
37
+ const uaKey = await crypto.subtle.importKey("raw", input.uaPublic, { name: "ECDH", namedCurve: "P-256" }, false, []);
38
+ const ecdhSecret = toBytes(await crypto.subtle.deriveBits({ name: "ECDH", public: uaKey }, sender.privateKey, 256));
39
+ const keyInfo = concat(utf8("WebPush: info\0"), input.uaPublic, sender.publicKey);
40
+ const ikm = await hkdf(input.authSecret, ecdhSecret, keyInfo, 32);
41
+ const cek = await hkdf(salt, ikm, utf8("Content-Encoding: aes128gcm\0"), 16);
42
+ const nonce = await hkdf(salt, ikm, utf8("Content-Encoding: nonce\0"), 12);
43
+ const aesKey = await crypto.subtle.importKey("raw", cek, "AES-GCM", false, ["encrypt"]);
44
+ const ciphertext = toBytes(await crypto.subtle.encrypt({ name: "AES-GCM", iv: nonce }, aesKey, concat(input.plaintext, new Uint8Array([2]))));
45
+ const recordSize = new Uint8Array(4);
46
+ new DataView(recordSize.buffer).setUint32(0, RECORD_SIZE);
47
+ return concat(salt, recordSize, new Uint8Array([POINT_LENGTH]), sender.publicKey, ciphertext);
48
+ }
49
+ //# sourceMappingURL=web_push_crypto.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web_push_crypto.js","sourceRoot":"","sources":["../../../src/component/crypto/web_push_crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAE/D,2FAA2F;AAC3F,MAAM,WAAW,GAAG,IAAI,CAAC;AACzB,oDAAoD;AACpD,MAAM,YAAY,GAAG,EAAE,CAAC;AAExB;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,IAAW,EAAE,GAAU,EAAE,IAAW,EAAE,MAAc;IAC7E,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM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CAAC,CAAC;AAChG,CAAC"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Workpool completion callback for one send job. A success carries the send
3
+ * action's result (validated by the action's `returns`); a throw or timeout of
4
+ * the action counts as a retry; a cancelled job ends the delivery as failed.
5
+ */
6
+ export declare const onSendComplete: import("convex/server").RegisteredMutation<"internal", {
7
+ context: {
8
+ deliveryId: import("convex/values").GenericId<"deliveries">;
9
+ };
10
+ result: {
11
+ kind: "success";
12
+ returnValue: any;
13
+ } | {
14
+ errorData?: any;
15
+ kind: "failed";
16
+ error: string;
17
+ } | {
18
+ kind: "canceled";
19
+ };
20
+ workId: import("@convex-dev/workpool").WorkId;
21
+ }, Promise<null>>;
22
+ /** Erase a message's content once none of its deliveries is queued. Idempotent. */
23
+ export declare const scrub: import("convex/server").RegisteredMutation<"internal", {
24
+ messageId: import("convex/values").GenericId<"messages">;
25
+ }, Promise<null>>;
26
+ /**
27
+ * Store a freshly minted provider token. When another sender already stored a
28
+ * token that is still fresh, that one is kept and returned, so concurrent
29
+ * senders converge on one token (Apple rejects frequent token changes).
30
+ */
31
+ export declare const storeToken: import("convex/server").RegisteredMutation<"internal", {
32
+ audience?: string | undefined;
33
+ cipher: ArrayBuffer;
34
+ credentialRef: string;
35
+ expiresAt: number;
36
+ iv: ArrayBuffer;
37
+ mintedAt: number;
38
+ refreshAfter: number;
39
+ }, Promise<{
40
+ cipher: ArrayBuffer;
41
+ iv: ArrayBuffer;
42
+ }>>;
43
+ /** Forget a cached token the provider refused as expired. */
44
+ export declare const dropToken: import("convex/server").RegisteredMutation<"internal", {
45
+ audience?: string | undefined;
46
+ credentialRef: string;
47
+ }, Promise<null>>;
48
+ //# sourceMappingURL=internal_mutations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internal_mutations.d.ts","sourceRoot":"","sources":["../../src/component/internal_mutations.ts"],"names":[],"mappings":"AAMA;;;;GAIG;AACH,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;iBAazB,CAAC;AAEH,mFAAmF;AACnF,eAAO,MAAM,KAAK;;iBAOhB,CAAC;AAcH;;;;GAIG;AACH,eAAO,MAAM,UAAU;;;;;;;;;;;GAoBrB,CAAC;AAEH,6DAA6D;AAC7D,eAAO,MAAM,SAAS;;;iBAWpB,CAAC"}
@@ -0,0 +1,82 @@
1
+ import { vOnCompleteArgs } from "@convex-dev/workpool";
2
+ import { v } from "convex/values";
3
+ import { internalMutation } from "./_generated/server.js";
4
+ import { applyOutcome, scrubIfDone } from "./queue.js";
5
+ /**
6
+ * Workpool completion callback for one send job. A success carries the send
7
+ * action's result (validated by the action's `returns`); a throw or timeout of
8
+ * the action counts as a retry; a cancelled job ends the delivery as failed.
9
+ */
10
+ export const onSendComplete = internalMutation({
11
+ args: vOnCompleteArgs(v.object({ deliveryId: v.id("deliveries") })),
12
+ returns: v.null(),
13
+ handler: async (ctx, { context, result }) => {
14
+ const outcome = result.kind === "success"
15
+ ? parseOutcome(result.returnValue)
16
+ : result.kind === "failed"
17
+ ? { status: "retry", code: "SENDER_ERROR" }
18
+ : { status: "failed", code: "CANCELLED" };
19
+ await applyOutcome(ctx, context.deliveryId, outcome);
20
+ return null;
21
+ },
22
+ });
23
+ /** Erase a message's content once none of its deliveries is queued. Idempotent. */
24
+ export const scrub = internalMutation({
25
+ args: { messageId: v.id("messages") },
26
+ returns: v.null(),
27
+ handler: async (ctx, { messageId }) => {
28
+ await scrubIfDone(ctx, messageId);
29
+ return null;
30
+ },
31
+ });
32
+ function parseOutcome(value) {
33
+ return isOutcome(value) ? value : { status: "failed", code: "SENDER_BAD_RESULT" };
34
+ }
35
+ const OUTCOME_STATUSES = new Set(["sent", "retry", "invalid", "failed", "skip"]);
36
+ function isOutcome(value) {
37
+ return typeof value === "object" && value !== null && "status" in value && OUTCOME_STATUSES.has(value.status);
38
+ }
39
+ const slot = { credentialRef: v.string(), audience: v.optional(v.string()) };
40
+ /**
41
+ * Store a freshly minted provider token. When another sender already stored a
42
+ * token that is still fresh, that one is kept and returned, so concurrent
43
+ * senders converge on one token (Apple rejects frequent token changes).
44
+ */
45
+ export const storeToken = internalMutation({
46
+ args: {
47
+ ...slot,
48
+ cipher: v.bytes(),
49
+ iv: v.bytes(),
50
+ mintedAt: v.number(),
51
+ refreshAfter: v.number(),
52
+ expiresAt: v.number(),
53
+ },
54
+ returns: v.object({ cipher: v.bytes(), iv: v.bytes() }),
55
+ handler: async (ctx, args) => {
56
+ const existing = await ctx.db
57
+ .query("providerTokens")
58
+ .withIndex("by_credential_audience", (q) => q.eq("credentialRef", args.credentialRef).eq("audience", args.audience))
59
+ .first();
60
+ if (existing && existing.refreshAfter > Date.now())
61
+ return { cipher: existing.cipher, iv: existing.iv };
62
+ if (existing)
63
+ await ctx.db.delete("providerTokens", existing._id);
64
+ await ctx.db.insert("providerTokens", args);
65
+ return { cipher: args.cipher, iv: args.iv };
66
+ },
67
+ });
68
+ /** Forget a cached token the provider refused as expired. */
69
+ export const dropToken = internalMutation({
70
+ args: slot,
71
+ returns: v.null(),
72
+ handler: async (ctx, args) => {
73
+ const existing = await ctx.db
74
+ .query("providerTokens")
75
+ .withIndex("by_credential_audience", (q) => q.eq("credentialRef", args.credentialRef).eq("audience", args.audience))
76
+ .first();
77
+ if (existing)
78
+ await ctx.db.delete("providerTokens", existing._id);
79
+ return null;
80
+ },
81
+ });
82
+ //# sourceMappingURL=internal_mutations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internal_mutations.js","sourceRoot":"","sources":["../../src/component/internal_mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGvD;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,gBAAgB,CAAC;IAC7C,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