@molecule/api-push-notifications-web-push 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +115 -0
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +19 -0
- package/dist/browser-guard.js.map +1 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +39 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +29 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +145 -0
- package/dist/provider.js.map +1 -0
- package/dist/secrets.d.ts +16 -0
- package/dist/secrets.d.ts.map +1 -0
- package/dist/secrets.js +34 -0
- package/dist/secrets.js.map +1 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.d.ts","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser guard — `@molecule/api-push-notifications-web-push` is SERVER-ONLY.
|
|
3
|
+
*
|
|
4
|
+
* Generated by scripts/gen-browser-guards.mjs (workspace root) — edit THAT, not this.
|
|
5
|
+
* Evaluating a server package in a browser bundle is always an import-graph mistake
|
|
6
|
+
* (node APIs, secrets); without this guard it surfaces as a cryptic downstream crash
|
|
7
|
+
* ("Buffer is not defined") far from the culprit. Throwing here names the package and
|
|
8
|
+
* the fix at the exact moment the client bundle evaluates it. jsdom tests and SSR are
|
|
9
|
+
* unaffected: the throw requires browser globals AND the absence of a node runtime.
|
|
10
|
+
*/
|
|
11
|
+
const g = globalThis;
|
|
12
|
+
if (g.window !== undefined && g.document !== undefined && !g.process?.versions?.node) {
|
|
13
|
+
throw new Error('@molecule/api-push-notifications-web-push is SERVER-ONLY: it was bundled into browser/client code. Import it only ' +
|
|
14
|
+
'from server code (a server route/function or your API), or dynamic-import it inside ' +
|
|
15
|
+
'the server handler — never from components or shared client modules, and never ' +
|
|
16
|
+
'polyfill Buffer/process to silence this.');
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=browser-guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.js","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,CAAC,GAAG,UAIT,CAAA;AACD,IAAI,CAAC,CAAC,MAAM,KAAK,SAAS,IAAI,CAAC,CAAC,QAAQ,KAAK,SAAS,IAAI,CAAC,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;IACrF,MAAM,IAAI,KAAK,CACb,oHAAoH;QAClH,sFAAsF;QACtF,iFAAiF;QACjF,0CAA0C,CAC7C,CAAA;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Push provider for molecule.dev push notifications.
|
|
3
|
+
*
|
|
4
|
+
* Provides push notification delivery using the Web Push protocol (VAPID)
|
|
5
|
+
* via the `web-push` library.
|
|
6
|
+
*
|
|
7
|
+
* @see https://www.npmjs.com/package/web-push
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { setProvider } from '@molecule/api-push-notifications'
|
|
12
|
+
* import { provider } from '@molecule/api-push-notifications-web-push'
|
|
13
|
+
*
|
|
14
|
+
* setProvider(provider) // VAPID config is read from env on first send
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* Configuration is lazy and env-driven: `configure()` — called automatically on
|
|
19
|
+
* the first `send()` if you never call it — reads `VAPID_EMAIL`,
|
|
20
|
+
* `VAPID_PUBLIC_KEY`, and `VAPID_PRIVATE_KEY` unless an explicit `VapidConfig`
|
|
21
|
+
* is passed. With any of the three missing, wiring/boot does NOT fail:
|
|
22
|
+
* `configure()` logs a warning ("Push notifications disabled: missing …") and
|
|
23
|
+
* every subsequent `send()`/`sendMany()` THROWS "Push notifications not
|
|
24
|
+
* configured" — so a missing env var surfaces at first send, not at startup.
|
|
25
|
+
* `VAPID_EMAIL` accepts a bare address or the `mailto:`/`https:` form (a bare
|
|
26
|
+
* address is normalized to `mailto:…` — don't prepend `mailto:` to a value that
|
|
27
|
+
* already has it). `getPublicKey()` serves the key browsers need to subscribe
|
|
28
|
+
* (configured key first, `VAPID_PUBLIC_KEY` fallback); `generateVapidKeys()`
|
|
29
|
+
* mints a fresh pair — scaffolds auto-generate these secrets, so it's only
|
|
30
|
+
* needed for manual provisioning or rotation. `sendMany()` uses
|
|
31
|
+
* `Promise.allSettled`: one dead subscription never aborts the batch (check
|
|
32
|
+
* each result's `error`).
|
|
33
|
+
*
|
|
34
|
+
* @module
|
|
35
|
+
*/
|
|
36
|
+
export * from './browser-guard.js';
|
|
37
|
+
export * from './provider.js';
|
|
38
|
+
export * from './secrets.js';
|
|
39
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Push provider for molecule.dev push notifications.
|
|
3
|
+
*
|
|
4
|
+
* Provides push notification delivery using the Web Push protocol (VAPID)
|
|
5
|
+
* via the `web-push` library.
|
|
6
|
+
*
|
|
7
|
+
* @see https://www.npmjs.com/package/web-push
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { setProvider } from '@molecule/api-push-notifications'
|
|
12
|
+
* import { provider } from '@molecule/api-push-notifications-web-push'
|
|
13
|
+
*
|
|
14
|
+
* setProvider(provider) // VAPID config is read from env on first send
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* Configuration is lazy and env-driven: `configure()` — called automatically on
|
|
19
|
+
* the first `send()` if you never call it — reads `VAPID_EMAIL`,
|
|
20
|
+
* `VAPID_PUBLIC_KEY`, and `VAPID_PRIVATE_KEY` unless an explicit `VapidConfig`
|
|
21
|
+
* is passed. With any of the three missing, wiring/boot does NOT fail:
|
|
22
|
+
* `configure()` logs a warning ("Push notifications disabled: missing …") and
|
|
23
|
+
* every subsequent `send()`/`sendMany()` THROWS "Push notifications not
|
|
24
|
+
* configured" — so a missing env var surfaces at first send, not at startup.
|
|
25
|
+
* `VAPID_EMAIL` accepts a bare address or the `mailto:`/`https:` form (a bare
|
|
26
|
+
* address is normalized to `mailto:…` — don't prepend `mailto:` to a value that
|
|
27
|
+
* already has it). `getPublicKey()` serves the key browsers need to subscribe
|
|
28
|
+
* (configured key first, `VAPID_PUBLIC_KEY` fallback); `generateVapidKeys()`
|
|
29
|
+
* mints a fresh pair — scaffolds auto-generate these secrets, so it's only
|
|
30
|
+
* needed for manual provisioning or rotation. `sendMany()` uses
|
|
31
|
+
* `Promise.allSettled`: one dead subscription never aborts the batch (check
|
|
32
|
+
* each result's `error`).
|
|
33
|
+
*
|
|
34
|
+
* @module
|
|
35
|
+
*/
|
|
36
|
+
export * from './browser-guard.js';
|
|
37
|
+
export * from './provider.js';
|
|
38
|
+
export * from './secrets.js';
|
|
39
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Push provider implementation for molecule.dev push notifications.
|
|
3
|
+
*
|
|
4
|
+
* Provides push notification delivery using the Web Push protocol via the
|
|
5
|
+
* web-push library. Supports VAPID authentication and batch sending.
|
|
6
|
+
*
|
|
7
|
+
* @see https://www.npmjs.com/package/web-push
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import './secrets.js';
|
|
12
|
+
import type { PushNotificationProvider } from '@molecule/api-push-notifications';
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new `WebPushProvider` instance. VAPID credentials are configured lazily on first send.
|
|
15
|
+
* @returns A `PushNotificationProvider` backed by the `web-push` library.
|
|
16
|
+
*/
|
|
17
|
+
export declare function createProvider(): PushNotificationProvider;
|
|
18
|
+
/**
|
|
19
|
+
* Lazily-initialized push notification provider using the `web-push` library.
|
|
20
|
+
* Created on first property access via a `Proxy` so no work is done at import time.
|
|
21
|
+
*
|
|
22
|
+
* The `set` trap is REQUIRED, not defensive: methods reached through the proxy
|
|
23
|
+
* run with `this` bound to the proxy, so an instance-state write like
|
|
24
|
+
* `this.configured = true` would otherwise land on the dummy `{}` target while
|
|
25
|
+
* every read passes through to the real instance — `configure()` could then
|
|
26
|
+
* never take effect and every send would throw "not configured".
|
|
27
|
+
*/
|
|
28
|
+
export declare const provider: PushNotificationProvider;
|
|
29
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAKH,OAAO,cAAc,CAAA;AAIrB,OAAO,KAAK,EAEV,wBAAwB,EAKzB,MAAM,kCAAkC,CAAA;AAoHzC;;;GAGG;AACH,wBAAgB,cAAc,IAAI,wBAAwB,CAEzD;AAID;;;;;;;;;GASG;AACH,eAAO,MAAM,QAAQ,EAAE,wBASrB,CAAA"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Push provider implementation for molecule.dev push notifications.
|
|
3
|
+
*
|
|
4
|
+
* Provides push notification delivery using the Web Push protocol via the
|
|
5
|
+
* web-push library. Supports VAPID authentication and batch sending.
|
|
6
|
+
*
|
|
7
|
+
* @see https://www.npmjs.com/package/web-push
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
// Side-effect import: registers this bond's secret definitions so the
|
|
12
|
+
// runtime registry is populated even when provider.js is imported directly
|
|
13
|
+
// (not through the package barrel).
|
|
14
|
+
import './secrets.js';
|
|
15
|
+
import webPush from 'web-push';
|
|
16
|
+
import { getLogger } from '@molecule/api-bond';
|
|
17
|
+
/**
|
|
18
|
+
* Web Push implementation of `PushNotificationProvider`. Uses the `web-push` library
|
|
19
|
+
* for VAPID-authenticated push notification delivery over the Web Push protocol.
|
|
20
|
+
*/
|
|
21
|
+
const logger = getLogger();
|
|
22
|
+
/**
|
|
23
|
+
* Web Push implementation of `PushNotificationProvider` using the `web-push` library.
|
|
24
|
+
*/
|
|
25
|
+
class WebPushProvider {
|
|
26
|
+
configured = false;
|
|
27
|
+
publicKey;
|
|
28
|
+
/**
|
|
29
|
+
* Configures VAPID credentials for Web Push. Falls back to `VAPID_EMAIL`, `VAPID_PUBLIC_KEY`,
|
|
30
|
+
* and `VAPID_PRIVATE_KEY` environment variables if no config is provided.
|
|
31
|
+
*
|
|
32
|
+
* Accepts the contact address with or without a `mailto:` prefix — the secrets
|
|
33
|
+
* registry teaches the `mailto:you@example.com` form, so blindly prepending
|
|
34
|
+
* would silently ship a malformed `mailto:mailto:…` VAPID subject that some
|
|
35
|
+
* push services reject.
|
|
36
|
+
*
|
|
37
|
+
* @param config - Optional VAPID configuration with email, public key, and private key.
|
|
38
|
+
*/
|
|
39
|
+
configure(config) {
|
|
40
|
+
const email = config?.email ?? process.env.VAPID_EMAIL;
|
|
41
|
+
const publicKey = config?.publicKey ?? process.env.VAPID_PUBLIC_KEY;
|
|
42
|
+
const privateKey = config?.privateKey ?? process.env.VAPID_PRIVATE_KEY;
|
|
43
|
+
if (email && publicKey && privateKey) {
|
|
44
|
+
const subject = email.startsWith('mailto:') || email.startsWith('https:') ? email : `mailto:${email}`;
|
|
45
|
+
webPush.setVapidDetails(subject, publicKey, privateKey);
|
|
46
|
+
this.configured = true;
|
|
47
|
+
this.publicKey = publicKey;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
const missing = [
|
|
51
|
+
!email && 'VAPID_EMAIL',
|
|
52
|
+
!publicKey && 'VAPID_PUBLIC_KEY',
|
|
53
|
+
!privateKey && 'VAPID_PRIVATE_KEY',
|
|
54
|
+
]
|
|
55
|
+
.filter(Boolean)
|
|
56
|
+
.join(', ');
|
|
57
|
+
logger.warn(`Push notifications disabled: missing ${missing}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Sends a push notification to a single subscription endpoint.
|
|
62
|
+
* Auto-configures VAPID on first call if not already configured.
|
|
63
|
+
* @param subscription - The browser push subscription (endpoint + keys).
|
|
64
|
+
* @param payload - The notification payload (title, body, icon, etc.) serialized as JSON.
|
|
65
|
+
* @returns The HTTP status code, headers, and body from the push service response.
|
|
66
|
+
*/
|
|
67
|
+
async send(subscription, payload) {
|
|
68
|
+
if (!this.configured)
|
|
69
|
+
this.configure();
|
|
70
|
+
if (!this.configured) {
|
|
71
|
+
throw new Error('Push notifications not configured. Set VAPID_EMAIL, VAPID_PUBLIC_KEY, and VAPID_PRIVATE_KEY.');
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const result = await webPush.sendNotification(subscription, JSON.stringify(payload));
|
|
75
|
+
return { statusCode: result.statusCode, headers: result.headers, body: result.body };
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
logger.error('WebPush send error:', error);
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Sends a push notification to multiple subscriptions in parallel using `Promise.allSettled`.
|
|
84
|
+
* @param subscriptions - An array of browser push subscriptions.
|
|
85
|
+
* @param payload - The notification payload to send to all subscriptions.
|
|
86
|
+
* @returns An array of results, each containing the subscription, send result (if successful), and error (if failed).
|
|
87
|
+
*/
|
|
88
|
+
async sendMany(subscriptions, payload) {
|
|
89
|
+
const results = await Promise.allSettled(subscriptions.map((sub) => this.send(sub, payload)));
|
|
90
|
+
return results.map((result, i) => ({
|
|
91
|
+
subscription: subscriptions[i],
|
|
92
|
+
result: result.status === 'fulfilled' ? result.value : undefined,
|
|
93
|
+
error: result.status === 'rejected' ? result.reason : undefined,
|
|
94
|
+
}));
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Generates a new VAPID key pair for Web Push authentication.
|
|
98
|
+
* @returns An object with `publicKey` and `privateKey` as URL-safe base64 strings.
|
|
99
|
+
*/
|
|
100
|
+
generateVapidKeys() {
|
|
101
|
+
return webPush.generateVAPIDKeys();
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Returns the VAPID public key clients need to create push subscriptions.
|
|
105
|
+
* Prefers the key passed to `configure()` (env-only lookup here broke every
|
|
106
|
+
* app that configured with an explicit `VapidConfig` — the subscribe route
|
|
107
|
+
* served `undefined` while sends worked), falling back to the
|
|
108
|
+
* `VAPID_PUBLIC_KEY` environment variable.
|
|
109
|
+
* @returns The VAPID public key string, or `undefined` if not configured.
|
|
110
|
+
*/
|
|
111
|
+
getPublicKey() {
|
|
112
|
+
return this.publicKey ?? process.env.VAPID_PUBLIC_KEY;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Creates a new `WebPushProvider` instance. VAPID credentials are configured lazily on first send.
|
|
117
|
+
* @returns A `PushNotificationProvider` backed by the `web-push` library.
|
|
118
|
+
*/
|
|
119
|
+
export function createProvider() {
|
|
120
|
+
return new WebPushProvider();
|
|
121
|
+
}
|
|
122
|
+
let _instance;
|
|
123
|
+
/**
|
|
124
|
+
* Lazily-initialized push notification provider using the `web-push` library.
|
|
125
|
+
* Created on first property access via a `Proxy` so no work is done at import time.
|
|
126
|
+
*
|
|
127
|
+
* The `set` trap is REQUIRED, not defensive: methods reached through the proxy
|
|
128
|
+
* run with `this` bound to the proxy, so an instance-state write like
|
|
129
|
+
* `this.configured = true` would otherwise land on the dummy `{}` target while
|
|
130
|
+
* every read passes through to the real instance — `configure()` could then
|
|
131
|
+
* never take effect and every send would throw "not configured".
|
|
132
|
+
*/
|
|
133
|
+
export const provider = new Proxy({}, {
|
|
134
|
+
get(_, prop, receiver) {
|
|
135
|
+
if (!_instance)
|
|
136
|
+
_instance = createProvider();
|
|
137
|
+
return Reflect.get(_instance, prop, receiver);
|
|
138
|
+
},
|
|
139
|
+
set(_, prop, value) {
|
|
140
|
+
if (!_instance)
|
|
141
|
+
_instance = createProvider();
|
|
142
|
+
return Reflect.set(_instance, prop, value);
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
//# sourceMappingURL=provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,sEAAsE;AACtE,2EAA2E;AAC3E,oCAAoC;AACpC,OAAO,cAAc,CAAA;AACrB,OAAO,OAAO,MAAM,UAAU,CAAA;AAE9B,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAU9C;;;GAGG;AACH,MAAM,MAAM,GAAG,SAAS,EAAE,CAAA;AAE1B;;GAEG;AACH,MAAM,eAAe;IACX,UAAU,GAAG,KAAK,CAAA;IAClB,SAAS,CAAoB;IAErC;;;;;;;;;;OAUG;IACH,SAAS,CAAC,MAAoB;QAC5B,MAAM,KAAK,GAAG,MAAM,EAAE,KAAK,IAAI,OAAO,CAAC,GAAG,CAAC,WAAW,CAAA;QACtD,MAAM,SAAS,GAAG,MAAM,EAAE,SAAS,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAA;QACnE,MAAM,UAAU,GAAG,MAAM,EAAE,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAA;QAEtE,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AE,CAAA;QAC5C,OAAO,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAA;IAC/C,CAAC;IACD,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK;QAChB,IAAI,CAAC,SAAS;YAAE,SAAS,GAAG,cAAc,EAAE,CAAA;QAC5C,OAAO,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,IAAI,EAAE,KAAK,CAAC,CAAA;IAC5C,CAAC;CACF,CAAC,CAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Push secret definitions — self-registered at import time so the
|
|
3
|
+
* runtime secrets registry (`@molecule/api-secrets`) can drive boot-time
|
|
4
|
+
* configuration reports and actionable "not configured" errors.
|
|
5
|
+
*
|
|
6
|
+
* Content is derived MECHANICALLY from this package's mlcl registry
|
|
7
|
+
* secrets entry (label/instructions/setupUrl/example) via the fleet
|
|
8
|
+
* formula, so packages sharing a key register byte-identical definitions
|
|
9
|
+
* and registration order never matters.
|
|
10
|
+
*
|
|
11
|
+
* @module
|
|
12
|
+
*/
|
|
13
|
+
import type { SecretDefinition } from '@molecule/api-secrets';
|
|
14
|
+
/** Secret definitions required by the Web Push notifications bond. */
|
|
15
|
+
export declare const pushNotificationsWebPushSecretDefinitions: SecretDefinition[];
|
|
16
|
+
//# sourceMappingURL=secrets.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secrets.d.ts","sourceRoot":"","sources":["../src/secrets.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAG7D,sEAAsE;AACtE,eAAO,MAAM,yCAAyC,EAAE,gBAAgB,EAoBvE,CAAA"}
|
package/dist/secrets.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Push secret definitions — self-registered at import time so the
|
|
3
|
+
* runtime secrets registry (`@molecule/api-secrets`) can drive boot-time
|
|
4
|
+
* configuration reports and actionable "not configured" errors.
|
|
5
|
+
*
|
|
6
|
+
* Content is derived MECHANICALLY from this package's mlcl registry
|
|
7
|
+
* secrets entry (label/instructions/setupUrl/example) via the fleet
|
|
8
|
+
* formula, so packages sharing a key register byte-identical definitions
|
|
9
|
+
* and registration order never matters.
|
|
10
|
+
*
|
|
11
|
+
* @module
|
|
12
|
+
*/
|
|
13
|
+
import { registerSecrets } from '@molecule/api-secrets';
|
|
14
|
+
/** Secret definitions required by the Web Push notifications bond. */
|
|
15
|
+
export const pushNotificationsWebPushSecretDefinitions = [
|
|
16
|
+
{
|
|
17
|
+
key: 'VAPID_PUBLIC_KEY',
|
|
18
|
+
description: 'Web Push VAPID public key — VAPID public key for Web Push. Auto-generated at scaffold — no manual setup needed.',
|
|
19
|
+
required: true,
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
key: 'VAPID_PRIVATE_KEY',
|
|
23
|
+
description: 'Web Push VAPID private key — VAPID private key matching VAPID_PUBLIC_KEY. Auto-generated at scaffold — no manual setup needed.',
|
|
24
|
+
required: true,
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
key: 'VAPID_EMAIL',
|
|
28
|
+
description: 'Web Push contact email — Contact address sent to push services with each request (mailto: form).',
|
|
29
|
+
required: true,
|
|
30
|
+
example: 'mailto:you@example.com',
|
|
31
|
+
},
|
|
32
|
+
];
|
|
33
|
+
registerSecrets(pushNotificationsWebPushSecretDefinitions);
|
|
34
|
+
//# sourceMappingURL=secrets.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secrets.js","sourceRoot":"","sources":["../src/secrets.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAEvD,sEAAsE;AACtE,MAAM,CAAC,MAAM,yCAAyC,GAAuB;IAC3E;QACE,GAAG,EAAE,kBAAkB;QACvB,WAAW,EACT,iHAAiH;QACnH,QAAQ,EAAE,IAAI;KACf;IACD;QACE,GAAG,EAAE,mBAAmB;QACxB,WAAW,EACT,gIAAgI;QAClI,QAAQ,EAAE,IAAI;KACf;IACD;QACE,GAAG,EAAE,aAAa;QAClB,WAAW,EACT,kGAAkG;QACpG,QAAQ,EAAE,IAAI;QACd,OAAO,EAAE,wBAAwB;KAClC;CACF,CAAA;AAED,eAAe,CAAC,yCAAyC,CAAC,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/api-push-notifications-web-push",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Web Push provider for molecule.dev push notifications",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"molecule",
|
|
24
|
+
"push-notifications",
|
|
25
|
+
"web-push"
|
|
26
|
+
],
|
|
27
|
+
"license": "Apache-2.0",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"web-push": "3.6.7"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@molecule/api-bond": "^1.0.0",
|
|
33
|
+
"@molecule/api-push-notifications": "^1.0.0",
|
|
34
|
+
"@molecule/api-secrets": "^1.0.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@molecule/api-bond": "1.0.0",
|
|
38
|
+
"@molecule/api-push-notifications": "1.0.0",
|
|
39
|
+
"@types/node": "26.1.2",
|
|
40
|
+
"@types/web-push": "3.6.4",
|
|
41
|
+
"typescript": "6.0.3",
|
|
42
|
+
"vitest": "4.1.10"
|
|
43
|
+
},
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
47
|
+
"directory": "packages/api/bonds/push-notifications/web-push"
|
|
48
|
+
},
|
|
49
|
+
"homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/api/bonds/push-notifications/web-push",
|
|
50
|
+
"bugs": "https://github.com/molecule-dev/molecule/issues",
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
}
|
|
54
|
+
}
|