@cocreate/notification 1.0.0 → 1.0.1

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 CHANGED
@@ -1,3 +1,10 @@
1
+ ## [1.0.1](https://github.com/CoCreate-app/CoCreate-notification/compare/v1.0.0...v1.0.1) (2023-09-24)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * add web-push in order to generate Vapid keys ([cffd320](https://github.com/CoCreate-app/CoCreate-notification/commit/cffd3202b28722cb930b2be18370b1c835c6e2b2))
7
+
1
8
  # 1.0.0 (2023-09-24)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/notification",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Simple HTML5 & JavaScript component add, update & remove values in element's notification from input, select or js api. Easily configured using HTML5 notification and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "cocreate",
@@ -58,6 +58,7 @@
58
58
  },
59
59
  "dependencies": {
60
60
  "@cocreate/actions": "^1.11.2",
61
- "@cocreate/socket-client": "^1.28.0"
61
+ "@cocreate/socket-client": "^1.28.0",
62
+ "web-push": "^3.6.6"
62
63
  }
63
64
  }
package/src/client.js CHANGED
@@ -1,14 +1,6 @@
1
1
  import socket from '@cocreate/socket-client'
2
2
  // import actions from '@cocreate/actions'
3
3
 
4
- // Request push notification permission
5
- const permission = await Notification.requestPermission();
6
- if (permission === 'granted') {
7
- // User granted push notification permission, handle it here
8
- subscribeToPushNotifications();
9
- }
10
-
11
-
12
4
  async function handlePushPermission() {
13
5
  // Request push notification permission
14
6
  const permission = await Notification.requestPermission();
@@ -77,3 +69,4 @@ document.querySelector('[actions*="notification.subscribe"]').addEventListener('
77
69
  // endEvent: "notification",
78
70
  // callback: (action) => handlePushPermission
79
71
  // });
72
+ export default {}
package/src/server.js CHANGED
@@ -1,9 +1,10 @@
1
1
  const crypto = require('crypto');
2
2
  const https = require('https');
3
+ const webpush = require('web-push');
3
4
 
4
5
  class CoCreateNotification {
5
6
  constructor(crud) {
6
- this.wsManager = crud.wsManager
7
+ this.socket = crud.wsManager
7
8
  this.crud = crud
8
9
  this.newKeyMap = new Map()
9
10
  this.subscriptions = new Map()
@@ -11,10 +12,10 @@ class CoCreateNotification {
11
12
  }
12
13
 
13
14
  init() {
14
- if (this.wsManager) {
15
- this.wsManager.on('notification.publicKey', (data) => this.publicKey(data));
16
- this.wsManager.on('notification.subscription', (data) => this.subscription(data));
17
- this.wsManager.on('notification.send', (data) => this.send(data));
15
+ if (this.socket) {
16
+ this.socket.on('notification.publicKey', (data) => this.publicKey(data));
17
+ this.socket.on('notification.subscription', (data) => this.subscription(data));
18
+ this.socket.on('notification.send', (data) => this.send(data));
18
19
  }
19
20
  }
20
21
 
@@ -22,14 +23,29 @@ class CoCreateNotification {
22
23
  publicKey(data) {
23
24
  let subscription = this.subscriptions.has(data.clientId)
24
25
  if (!subscription) {
25
- let newKeys = this.generateVapidKeys()
26
- this.newKeyMap.set(data.clientId, newKeys)
27
- data.publicKey = newKeyMap.publicKey
26
+ // let newKeys = this.generateVapidKeys()
27
+ let newKeys = webpush.generateVAPIDKeys()
28
+
29
+ console.log('Before: ', newKeys.publicKey)
30
+ webpush.setVapidDetails(
31
+ 'mailto:contact@cocreate.app',
32
+ newKeys.publicKey,
33
+ newKeys.privateKey
34
+ );
35
+
36
+ // Request a VAPID key
37
+ const vapidRequest = webpush.generateRequestDetails();
38
+ console.log('vapidRequest: ', vapidRequest)
39
+
40
+ data.publicKey = this.base64URLEncode(base64URLEncode(vapidRequest.keys.p256dh))
41
+ this.newKeyMap.set(data.clientId, { publicKey: data.publicKey, privateKey: this.base64URLEncode(vapidRequest.keys.auth) })
28
42
  } else {
29
43
  data.publicKey = subscription.publicKey
30
44
  }
31
45
 
32
- this.socket.send(data)
46
+ console.log('After: ', data.publicKey)
47
+ if (data.socket)
48
+ this.socket.send(data)
33
49
 
34
50
  }
35
51
 
@@ -78,7 +94,7 @@ class CoCreateNotification {
78
94
  if (data.payload && !data.payload.timestamp)
79
95
  data.payload.timestamp = Date.now()
80
96
 
81
- let payload = {...subscription, ...data.payload}
97
+ let payload = { ...subscription, ...data.payload }
82
98
  delete payload.privateKey
83
99
  delete payload.publicKey
84
100
  payload = JSON.stringify(payload);
@@ -138,10 +154,11 @@ class CoCreateNotification {
138
154
 
139
155
  // Function to base64 URL encode
140
156
  base64URLEncode(value) {
141
- return value.toString('base64')
157
+ const base64 = Buffer.from(value).toString('base64');
158
+ return base64
142
159
  .replace(/\+/g, '-')
143
160
  .replace(/\//g, '_')
144
- .replace(/=/g, '');
161
+ .replace(/=+$/, ''); // Remove trailing equal signs
145
162
  }
146
163
 
147
164
 
@@ -157,7 +174,7 @@ const payloadExample = {
157
174
  { action: 'action-2', title: 'Action 2' },
158
175
  ],
159
176
  tag: 'notification-tag', // A unique identifier for the notification
160
- data,
177
+ data: {},
161
178
  vibrate: [100, 50, 100], // Vibration pattern (milliseconds)
162
179
  image: '/path/to/notification-image.jpg', // An image to display within the notification
163
180
  requireInteraction: true, // Requires user interaction to close the notification