@hellotext/hellotext 2.5.7 → 2.5.8
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/README.md +0 -25
- package/dist/hellotext.js +1 -1
- package/index.d.ts +0 -6
- package/lib/api/acks.cjs +17 -31
- package/lib/api/acks.js +17 -42
- package/lib/api/businesses.cjs +14 -28
- package/lib/api/businesses.js +13 -36
- package/lib/api/events.cjs +26 -40
- package/lib/api/events.js +28 -52
- package/lib/api/forms.cjs +24 -40
- package/lib/api/forms.js +24 -57
- package/lib/api/identifications.cjs +15 -29
- package/lib/api/identifications.js +15 -40
- package/lib/api/index.cjs +25 -51
- package/lib/api/index.js +27 -54
- package/lib/api/push/identities.cjs +30 -46
- package/lib/api/push/identities.js +30 -63
- package/lib/api/response.cjs +30 -52
- package/lib/api/response.js +31 -60
- package/lib/api/submissions.cjs +18 -34
- package/lib/api/submissions.js +18 -48
- package/lib/api/webchat/messages.cjs +44 -64
- package/lib/api/webchat/messages.js +44 -80
- package/lib/api/webchats.cjs +37 -56
- package/lib/api/webchats.js +37 -65
- package/lib/api/whatsapp_widgets.cjs +49 -72
- package/lib/api/whatsapp_widgets.js +48 -91
- package/lib/builders/input_builder.cjs +36 -48
- package/lib/builders/input_builder.js +36 -48
- package/lib/builders/logo_builder.cjs +10 -29
- package/lib/builders/logo_builder.js +20 -28
- package/lib/channels/application_channel.cjs +136 -178
- package/lib/channels/application_channel.js +137 -180
- package/lib/channels/webchat_channel.cjs +125 -170
- package/lib/channels/webchat_channel.js +125 -170
- package/lib/controllers/form_controller.cjs +82 -121
- package/lib/controllers/form_controller.js +82 -130
- package/lib/controllers/message_controller.cjs +211 -282
- package/lib/controllers/message_controller.js +209 -284
- package/lib/controllers/mixins/usePopover.cjs +3 -6
- package/lib/controllers/mixins/usePopover.js +17 -22
- package/lib/controllers/webchat/emoji_picker_controller.cjs +106 -150
- package/lib/controllers/webchat/emoji_picker_controller.js +105 -170
- package/lib/controllers/webchat/useBehaviour.js +5 -5
- package/lib/controllers/webchat/useOpeningSequence.js +6 -6
- package/lib/controllers/webchat/useTeaser.js +9 -9
- package/lib/controllers/webchat_controller.cjs +916 -1088
- package/lib/controllers/webchat_controller.js +926 -1152
- package/lib/core/configuration/forms.cjs +21 -35
- package/lib/core/configuration/forms.js +20 -35
- package/lib/core/configuration/locale.cjs +36 -71
- package/lib/core/configuration/locale.js +36 -70
- package/lib/core/configuration/push.cjs +9 -21
- package/lib/core/configuration/push.js +8 -20
- package/lib/core/configuration/webchat.cjs +142 -182
- package/lib/core/configuration/webchat.js +145 -189
- package/lib/core/configuration/whatsapp.cjs +76 -102
- package/lib/core/configuration/whatsapp.js +76 -105
- package/lib/core/configuration.cjs +60 -78
- package/lib/core/configuration.js +59 -78
- package/lib/core/event.cjs +36 -59
- package/lib/core/event.js +35 -61
- package/lib/core/sanitize_html.js +4 -4
- package/lib/errors/invalid_event.cjs +5 -26
- package/lib/errors/invalid_event.js +5 -26
- package/lib/errors/not_initialized_error.cjs +5 -26
- package/lib/errors/not_initialized_error.js +5 -26
- package/lib/hellotext.cjs +185 -223
- package/lib/hellotext.js +188 -247
- package/lib/index.js +1 -1
- package/lib/models/business.cjs +114 -152
- package/lib/models/business.js +117 -162
- package/lib/models/cookies.cjs +34 -51
- package/lib/models/cookies.js +34 -51
- package/lib/models/fingerprint.cjs +8 -23
- package/lib/models/fingerprint.js +25 -58
- package/lib/models/form.cjs +106 -142
- package/lib/models/form.js +105 -153
- package/lib/models/form_collection.cjs +63 -101
- package/lib/models/form_collection.js +61 -107
- package/lib/models/page.cjs +106 -129
- package/lib/models/page.js +106 -131
- package/lib/models/push.cjs +278 -322
- package/lib/models/push.js +284 -357
- package/lib/models/query.cjs +21 -41
- package/lib/models/query.js +21 -41
- package/lib/models/session.cjs +33 -65
- package/lib/models/session.js +30 -63
- package/lib/models/user.cjs +29 -51
- package/lib/models/user.js +29 -51
- package/lib/models/utm.cjs +15 -27
- package/lib/models/utm.js +17 -32
- package/lib/models/webchat.cjs +52 -76
- package/lib/models/webchat.js +52 -90
- package/lib/models/whatsapp_widget.cjs +41 -59
- package/lib/models/whatsapp_widget.js +41 -73
- package/lib/vanilla.cjs +4 -7
- package/lib/vanilla.js +2 -1
- package/package.json +1 -9
- package/src/index.bundle.js +0 -1
- package/src/vanilla.js +2 -1
- package/styles/index.css +0 -30
package/lib/models/push.js
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
2
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
3
|
-
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
4
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
5
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
6
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
7
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
8
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
9
|
-
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
10
|
-
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
11
1
|
import API from '../api';
|
|
12
2
|
import { Configuration } from '../core';
|
|
13
3
|
|
|
@@ -16,13 +6,12 @@ import { Configuration } from '../core';
|
|
|
16
6
|
*
|
|
17
7
|
* @property {Promise<void>|null} ready - Initialization promise, available after initialize().
|
|
18
8
|
*/
|
|
19
|
-
|
|
9
|
+
class Push {
|
|
20
10
|
/**
|
|
21
11
|
* @param {Object} data - Push configuration from the business response.
|
|
22
12
|
* @param {String} data.public_key - Base64url-encoded VAPID public key.
|
|
23
13
|
*/
|
|
24
|
-
|
|
25
|
-
_classCallCheck(this, Push);
|
|
14
|
+
constructor(data) {
|
|
26
15
|
this.publicKey = data.public_key;
|
|
27
16
|
this.serviceWorkerUrl = Configuration.push.serviceWorkerUrl;
|
|
28
17
|
this.channelId = Configuration.push.channelId;
|
|
@@ -42,371 +31,309 @@ var Push = /*#__PURE__*/function () {
|
|
|
42
31
|
*
|
|
43
32
|
* @returns {Promise<void>}
|
|
44
33
|
*/
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
34
|
+
initialize() {
|
|
35
|
+
this.ready = this.restoreSubscription();
|
|
36
|
+
return this.ready;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Registers an existing Hellotext subscription with the server without prompting.
|
|
41
|
+
*
|
|
42
|
+
* @private
|
|
43
|
+
* @returns {Promise<void>}
|
|
44
|
+
*/
|
|
45
|
+
async restoreSubscription() {
|
|
46
|
+
const registration = await this.getRegistration();
|
|
47
|
+
const subscription = await registration.pushManager.getSubscription();
|
|
48
|
+
if (this.disposed || this.unsubscribePromise || !subscription || !this.owns(subscription)) {
|
|
49
|
+
return;
|
|
50
50
|
}
|
|
51
|
+
this.subscription = subscription;
|
|
52
|
+
await this.sync();
|
|
53
|
+
}
|
|
51
54
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (this.disposed || this.unsubscribePromise || !subscription || !this.owns(subscription)) {
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
this.subscription = subscription;
|
|
68
|
-
yield this.sync();
|
|
69
|
-
});
|
|
70
|
-
function restoreSubscription() {
|
|
71
|
-
return _restoreSubscription.apply(this, arguments);
|
|
72
|
-
}
|
|
73
|
-
return restoreSubscription;
|
|
74
|
-
}()
|
|
75
|
-
/**
|
|
76
|
-
* Creates or reuses a Push subscription. Call from a user click handler.
|
|
77
|
-
* Concurrent calls share the same pending request.
|
|
78
|
-
*
|
|
79
|
-
* @returns {Promise<import('../api/response').Response|void>} Server registration response,
|
|
80
|
-
* or no value when disposed.
|
|
81
|
-
* Rejects when permission is denied or a browser or network operation fails.
|
|
82
|
-
*/
|
|
83
|
-
}, {
|
|
84
|
-
key: "subscribe",
|
|
85
|
-
value: function subscribe() {
|
|
86
|
-
if (this.disposed) return Promise.resolve();
|
|
87
|
-
if (this.unsubscribePromise) return Promise.reject(new Error('Push unsubscribe is in progress'));
|
|
88
|
-
if (this.subscribePromise) return this.subscribePromise;
|
|
55
|
+
/**
|
|
56
|
+
* Creates or reuses a Push subscription. Call from a user click handler.
|
|
57
|
+
* Concurrent calls share the same pending request.
|
|
58
|
+
*
|
|
59
|
+
* @returns {Promise<import('../api/response').Response|void>} Server registration response,
|
|
60
|
+
* or no value when disposed.
|
|
61
|
+
* Rejects when permission is denied or a browser or network operation fails.
|
|
62
|
+
*/
|
|
63
|
+
subscribe() {
|
|
64
|
+
if (this.disposed) return Promise.resolve();
|
|
65
|
+
if (this.unsubscribePromise) return Promise.reject(new Error('Push unsubscribe is in progress'));
|
|
66
|
+
if (this.subscribePromise) return this.subscribePromise;
|
|
89
67
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
68
|
+
// Request permission before awaiting worker readiness to retain the click's user activation.
|
|
69
|
+
const permission = Notification.permission === 'default' ? Notification.requestPermission() : Promise.resolve(Notification.permission);
|
|
70
|
+
this.subscribePromise = this.createSubscription(permission).finally(() => {
|
|
71
|
+
this.subscribePromise = null;
|
|
72
|
+
});
|
|
73
|
+
return this.subscribePromise;
|
|
74
|
+
}
|
|
97
75
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (this.disposed) return;
|
|
114
|
-
if (subscription && !this.owns(subscription)) {
|
|
115
|
-
throw new Error('The existing Push subscription belongs to a different application');
|
|
116
|
-
}
|
|
117
|
-
subscription || (subscription = yield registration.pushManager.subscribe({
|
|
118
|
-
userVisibleOnly: true,
|
|
119
|
-
applicationServerKey: this.applicationServerKey
|
|
120
|
-
}));
|
|
121
|
-
if (this.disposed) return;
|
|
122
|
-
this.subscription = subscription;
|
|
123
|
-
return this.sync();
|
|
124
|
-
});
|
|
125
|
-
function createSubscription(_x) {
|
|
126
|
-
return _createSubscription.apply(this, arguments);
|
|
127
|
-
}
|
|
128
|
-
return createSubscription;
|
|
129
|
-
}()
|
|
130
|
-
/**
|
|
131
|
-
* Disables the server identity and removes the browser subscription.
|
|
132
|
-
* Keeps the subscription when the server request fails so the caller can retry.
|
|
133
|
-
*
|
|
134
|
-
* @returns {Promise<import('../api/response').Response|null|void>} Server response, null when
|
|
135
|
-
* no subscription exists, or no value when disposed. Rejects when a browser or network
|
|
136
|
-
* operation fails.
|
|
137
|
-
*/
|
|
138
|
-
}, {
|
|
139
|
-
key: "unsubscribe",
|
|
140
|
-
value: function unsubscribe() {
|
|
141
|
-
if (this.disposed) return Promise.resolve();
|
|
142
|
-
if (this.unsubscribePromise) return this.unsubscribePromise;
|
|
143
|
-
this.clearRetryTimeout();
|
|
144
|
-
this.unsubscribePromise = this.removeSubscription().finally(() => {
|
|
145
|
-
this.unsubscribePromise = null;
|
|
146
|
-
});
|
|
147
|
-
return this.unsubscribePromise;
|
|
76
|
+
/**
|
|
77
|
+
* Waits for permission and worker readiness, then subscribes and registers with Hellotext.
|
|
78
|
+
*
|
|
79
|
+
* @private
|
|
80
|
+
* @param {Promise<NotificationPermission>} permission - Pending notification permission result.
|
|
81
|
+
* @returns {Promise<import('../api/response').Response|void>}
|
|
82
|
+
*/
|
|
83
|
+
async createSubscription(permission) {
|
|
84
|
+
if ((await permission) !== 'granted') throw new Error('Push permission was not granted');
|
|
85
|
+
const registration = await this.getRegistration();
|
|
86
|
+
if (this.disposed) return;
|
|
87
|
+
let subscription = await registration.pushManager.getSubscription();
|
|
88
|
+
if (this.disposed) return;
|
|
89
|
+
if (subscription && !this.owns(subscription)) {
|
|
90
|
+
throw new Error('The existing Push subscription belongs to a different application');
|
|
148
91
|
}
|
|
92
|
+
subscription ||= await registration.pushManager.subscribe({
|
|
93
|
+
userVisibleOnly: true,
|
|
94
|
+
applicationServerKey: this.applicationServerKey
|
|
95
|
+
});
|
|
96
|
+
if (this.disposed) return;
|
|
97
|
+
this.subscription = subscription;
|
|
98
|
+
return this.sync();
|
|
99
|
+
}
|
|
149
100
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
return removeSubscription;
|
|
186
|
-
}()
|
|
187
|
-
/**
|
|
188
|
-
* Registers the current subscription, sharing any request already in progress.
|
|
189
|
-
*
|
|
190
|
-
* @private
|
|
191
|
-
* @returns {Promise<import('../api/response').Response|void>}
|
|
192
|
-
*/
|
|
193
|
-
}, {
|
|
194
|
-
key: "sync",
|
|
195
|
-
value: function sync() {
|
|
196
|
-
if (this.disposed) return Promise.resolve();
|
|
197
|
-
if (this.syncPromise) return this.syncPromise;
|
|
198
|
-
this.clearRetryTimeout();
|
|
199
|
-
this.syncPromise = this.registerIdentity().finally(() => {
|
|
200
|
-
this.syncPromise = null;
|
|
201
|
-
});
|
|
202
|
-
return this.syncPromise;
|
|
101
|
+
/**
|
|
102
|
+
* Disables the server identity and removes the browser subscription.
|
|
103
|
+
* Keeps the subscription when the server request fails so the caller can retry.
|
|
104
|
+
*
|
|
105
|
+
* @returns {Promise<import('../api/response').Response|null|void>} Server response, null when
|
|
106
|
+
* no subscription exists, or no value when disposed. Rejects when a browser or network
|
|
107
|
+
* operation fails.
|
|
108
|
+
*/
|
|
109
|
+
unsubscribe() {
|
|
110
|
+
if (this.disposed) return Promise.resolve();
|
|
111
|
+
if (this.unsubscribePromise) return this.unsubscribePromise;
|
|
112
|
+
this.clearRetryTimeout();
|
|
113
|
+
this.unsubscribePromise = this.removeSubscription().finally(() => {
|
|
114
|
+
this.unsubscribePromise = null;
|
|
115
|
+
});
|
|
116
|
+
return this.unsubscribePromise;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Finishes pending registration before disabling and removing the subscription.
|
|
121
|
+
*
|
|
122
|
+
* @private
|
|
123
|
+
* @returns {Promise<import('../api/response').Response|null|void>}
|
|
124
|
+
*/
|
|
125
|
+
async removeSubscription() {
|
|
126
|
+
await this.ready?.catch(() => {});
|
|
127
|
+
await this.subscribePromise?.catch(() => {});
|
|
128
|
+
await this.syncPromise?.catch(() => {});
|
|
129
|
+
const registration = await this.getRegistration();
|
|
130
|
+
const subscription = (await registration.pushManager.getSubscription()) || this.subscription;
|
|
131
|
+
if (this.disposed) return;
|
|
132
|
+
if (!subscription) return null;
|
|
133
|
+
if (!this.owns(subscription)) {
|
|
134
|
+
throw new Error('The existing Push subscription belongs to a different application');
|
|
203
135
|
}
|
|
136
|
+
this.subscription = subscription;
|
|
137
|
+
const response = await API.pushIdentities.destroy({
|
|
138
|
+
subscription: subscription.toJSON()
|
|
139
|
+
});
|
|
140
|
+
if (response.failed) return response;
|
|
141
|
+
if (this.disposed) return;
|
|
142
|
+
await subscription.unsubscribe();
|
|
143
|
+
this.subscription = null;
|
|
144
|
+
this.clearRetryTimeout();
|
|
145
|
+
return response;
|
|
146
|
+
}
|
|
204
147
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
148
|
+
/**
|
|
149
|
+
* Registers the current subscription, sharing any request already in progress.
|
|
150
|
+
*
|
|
151
|
+
* @private
|
|
152
|
+
* @returns {Promise<import('../api/response').Response|void>}
|
|
153
|
+
*/
|
|
154
|
+
sync() {
|
|
155
|
+
if (this.disposed) return Promise.resolve();
|
|
156
|
+
if (this.syncPromise) return this.syncPromise;
|
|
157
|
+
this.clearRetryTimeout();
|
|
158
|
+
this.syncPromise = this.registerIdentity().finally(() => {
|
|
159
|
+
this.syncPromise = null;
|
|
160
|
+
});
|
|
161
|
+
return this.syncPromise;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Sends the subscription to Hellotext and schedules a retry on failure.
|
|
166
|
+
*
|
|
167
|
+
* @private
|
|
168
|
+
* @returns {Promise<import('../api/response').Response>}
|
|
169
|
+
*/
|
|
170
|
+
async registerIdentity() {
|
|
171
|
+
try {
|
|
172
|
+
const response = await API.pushIdentities.create({
|
|
173
|
+
subscription: this.subscription.toJSON(),
|
|
174
|
+
...(this.channelId ? {
|
|
175
|
+
channel_id: this.channelId
|
|
176
|
+
} : {})
|
|
231
177
|
});
|
|
232
|
-
|
|
233
|
-
|
|
178
|
+
if (response.succeeded) {
|
|
179
|
+
this.retryAttempts = 0;
|
|
180
|
+
} else {
|
|
181
|
+
this.scheduleRetry();
|
|
234
182
|
}
|
|
235
|
-
return
|
|
236
|
-
}()
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
*
|
|
240
|
-
* @private
|
|
241
|
-
* @returns {void}
|
|
242
|
-
*/
|
|
243
|
-
}, {
|
|
244
|
-
key: "scheduleRetry",
|
|
245
|
-
value: function scheduleRetry() {
|
|
246
|
-
if (this.disposed || this.unsubscribePromise || this.retryTimeout || this.retryAttempts >= 3) return;
|
|
247
|
-
this.retryTimeout = setTimeout(() => {
|
|
248
|
-
this.retryTimeout = null;
|
|
249
|
-
this.sync().catch(() => {});
|
|
250
|
-
}, 1000 * 2 ** this.retryAttempts);
|
|
251
|
-
this.retryAttempts += 1;
|
|
183
|
+
return response;
|
|
184
|
+
} catch (error) {
|
|
185
|
+
this.scheduleRetry();
|
|
186
|
+
throw error;
|
|
252
187
|
}
|
|
188
|
+
}
|
|
253
189
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
clearTimeout(this.retryTimeout);
|
|
190
|
+
/**
|
|
191
|
+
* Schedules up to three registration retries with increasing delays.
|
|
192
|
+
*
|
|
193
|
+
* @private
|
|
194
|
+
* @returns {void}
|
|
195
|
+
*/
|
|
196
|
+
scheduleRetry() {
|
|
197
|
+
if (this.disposed || this.unsubscribePromise || this.retryTimeout || this.retryAttempts >= 3) return;
|
|
198
|
+
this.retryTimeout = setTimeout(() => {
|
|
264
199
|
this.retryTimeout = null;
|
|
265
|
-
|
|
200
|
+
this.sync().catch(() => {});
|
|
201
|
+
}, 1000 * 2 ** this.retryAttempts);
|
|
202
|
+
this.retryAttempts += 1;
|
|
203
|
+
}
|
|
266
204
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
}
|
|
205
|
+
/**
|
|
206
|
+
* Cancels a pending registration retry.
|
|
207
|
+
*
|
|
208
|
+
* @private
|
|
209
|
+
* @returns {void}
|
|
210
|
+
*/
|
|
211
|
+
clearRetryTimeout() {
|
|
212
|
+
clearTimeout(this.retryTimeout);
|
|
213
|
+
this.retryTimeout = null;
|
|
214
|
+
}
|
|
278
215
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
}
|
|
216
|
+
/**
|
|
217
|
+
* Stops registration retries and prevents further use of this instance.
|
|
218
|
+
*
|
|
219
|
+
* @returns {void}
|
|
220
|
+
*/
|
|
221
|
+
dispose() {
|
|
222
|
+
this.disposed = true;
|
|
223
|
+
this.clearRetryTimeout();
|
|
224
|
+
}
|
|
289
225
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
226
|
+
/**
|
|
227
|
+
* Whether this instance has a browser subscription, regardless of server registration.
|
|
228
|
+
*
|
|
229
|
+
* @returns {Boolean}
|
|
230
|
+
*/
|
|
231
|
+
get subscribed() {
|
|
232
|
+
return !!this.subscription;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Decodes the public key into the bytes expected by PushManager.subscribe().
|
|
237
|
+
*
|
|
238
|
+
* @private
|
|
239
|
+
* @returns {Uint8Array}
|
|
240
|
+
*/
|
|
241
|
+
get applicationServerKey() {
|
|
242
|
+
const base64 = this.publicKey.replace(/-/g, '+').replace(/_/g, '/');
|
|
243
|
+
return Uint8Array.from(atob(base64.padEnd(Math.ceil(base64.length / 4) * 4, '=')), char => char.charCodeAt(0));
|
|
244
|
+
}
|
|
302
245
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
key
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
246
|
+
/**
|
|
247
|
+
* Checks whether a subscription uses this instance's public key.
|
|
248
|
+
*
|
|
249
|
+
* @private
|
|
250
|
+
* @param {PushSubscription} subscription - Browser subscription to check.
|
|
251
|
+
* @returns {Boolean}
|
|
252
|
+
*/
|
|
253
|
+
owns(subscription) {
|
|
254
|
+
const key = subscription.options?.applicationServerKey;
|
|
255
|
+
if (!key) return false;
|
|
256
|
+
const actual = new Uint8Array(key);
|
|
257
|
+
const expected = this.applicationServerKey;
|
|
258
|
+
return actual.length === expected.length && actual.every((value, index) => value === expected[index]);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Gets the worker registration, sharing any pending lookup.
|
|
263
|
+
* Failed lookups can be retried by a later call.
|
|
264
|
+
*
|
|
265
|
+
* @private
|
|
266
|
+
* @returns {Promise<ServiceWorkerRegistration>}
|
|
267
|
+
*/
|
|
268
|
+
getRegistration() {
|
|
269
|
+
if (!this.registrationPromise) {
|
|
270
|
+
this.registrationPromise = this.loadRegistration().catch(error => {
|
|
271
|
+
this.registrationPromise = null;
|
|
272
|
+
throw error;
|
|
273
|
+
});
|
|
319
274
|
}
|
|
275
|
+
return this.registrationPromise;
|
|
276
|
+
}
|
|
320
277
|
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
if (!
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
278
|
+
/**
|
|
279
|
+
* Registers the configured worker or waits for the page's existing registration.
|
|
280
|
+
*
|
|
281
|
+
* @private
|
|
282
|
+
* @returns {Promise<ServiceWorkerRegistration>} An active registration.
|
|
283
|
+
* Rejects if registration fails or the worker readiness wait times out.
|
|
284
|
+
*/
|
|
285
|
+
async loadRegistration() {
|
|
286
|
+
if (this.serviceWorkerUrl) {
|
|
287
|
+
const registration = await navigator.serviceWorker.register(this.serviceWorkerUrl);
|
|
288
|
+
if (registration.active && !registration.installing && !registration.waiting) {
|
|
289
|
+
// A returning visitor may have an older worker that does not include our Push handlers.
|
|
290
|
+
// Calling register() with the same URL can return that existing registration without
|
|
291
|
+
// checking whether the script served at that URL has changed since their last visit.
|
|
292
|
+
//
|
|
293
|
+
// If no replacement is already installing or waiting, explicitly check for an update
|
|
294
|
+
// before treating the active worker as ready. Awaiting update() completes the update
|
|
295
|
+
// check, but does not wait for a replacement worker to activate. The code below selects
|
|
296
|
+
// that replacement, if one was found, and waits for its activation before subscribing.
|
|
297
|
+
await registration.update();
|
|
336
298
|
}
|
|
337
|
-
|
|
299
|
+
const worker = registration.installing || registration.waiting || registration.active;
|
|
300
|
+
if (worker?.state === 'activated') return registration;
|
|
301
|
+
return new Promise((resolve, reject) => {
|
|
302
|
+
const timeout = setTimeout(() => finish(new Error('Push service worker did not become active')), 10000);
|
|
303
|
+
const finish = error => {
|
|
304
|
+
clearTimeout(timeout);
|
|
305
|
+
worker?.removeEventListener('statechange', changed);
|
|
306
|
+
error ? reject(error) : resolve(registration);
|
|
307
|
+
};
|
|
308
|
+
const changed = () => {
|
|
309
|
+
if (worker?.state === 'activated') finish();
|
|
310
|
+
if (worker?.state === 'redundant') finish(new Error('Push service worker installation failed'));
|
|
311
|
+
};
|
|
312
|
+
worker?.addEventListener('statechange', changed);
|
|
313
|
+
changed();
|
|
314
|
+
});
|
|
338
315
|
}
|
|
339
316
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
value: function () {
|
|
350
|
-
var _loadRegistration = _asyncToGenerator(function* () {
|
|
351
|
-
if (this.serviceWorkerUrl) {
|
|
352
|
-
var registration = yield navigator.serviceWorker.register(this.serviceWorkerUrl);
|
|
353
|
-
if (registration.active && !registration.installing && !registration.waiting) {
|
|
354
|
-
// A returning visitor may have an older worker that does not include our Push handlers.
|
|
355
|
-
// Calling register() with the same URL can return that existing registration without
|
|
356
|
-
// checking whether the script served at that URL has changed since their last visit.
|
|
357
|
-
//
|
|
358
|
-
// If no replacement is already installing or waiting, explicitly check for an update
|
|
359
|
-
// before treating the active worker as ready. Awaiting update() completes the update
|
|
360
|
-
// check, but does not wait for a replacement worker to activate. The code below selects
|
|
361
|
-
// that replacement, if one was found, and waits for its activation before subscribing.
|
|
362
|
-
yield registration.update();
|
|
363
|
-
}
|
|
364
|
-
var worker = registration.installing || registration.waiting || registration.active;
|
|
365
|
-
if ((worker === null || worker === void 0 ? void 0 : worker.state) === 'activated') return registration;
|
|
366
|
-
return new Promise((resolve, reject) => {
|
|
367
|
-
var timeout = setTimeout(() => finish(new Error('Push service worker did not become active')), 10000);
|
|
368
|
-
var finish = error => {
|
|
369
|
-
clearTimeout(timeout);
|
|
370
|
-
worker === null || worker === void 0 ? void 0 : worker.removeEventListener('statechange', changed);
|
|
371
|
-
error ? reject(error) : resolve(registration);
|
|
372
|
-
};
|
|
373
|
-
var changed = () => {
|
|
374
|
-
if ((worker === null || worker === void 0 ? void 0 : worker.state) === 'activated') finish();
|
|
375
|
-
if ((worker === null || worker === void 0 ? void 0 : worker.state) === 'redundant') finish(new Error('Push service worker installation failed'));
|
|
376
|
-
};
|
|
377
|
-
worker === null || worker === void 0 ? void 0 : worker.addEventListener('statechange', changed);
|
|
378
|
-
changed();
|
|
379
|
-
});
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
// Reuse the service worker already registered for this page.
|
|
383
|
-
return new Promise((resolve, reject) => {
|
|
384
|
-
var timeout = setTimeout(() => reject(new Error('Push service worker is not available')), 10000);
|
|
385
|
-
navigator.serviceWorker.ready.then(registration => {
|
|
386
|
-
clearTimeout(timeout);
|
|
387
|
-
resolve(registration);
|
|
388
|
-
}, error => {
|
|
389
|
-
clearTimeout(timeout);
|
|
390
|
-
reject(error);
|
|
391
|
-
});
|
|
392
|
-
});
|
|
317
|
+
// Reuse the service worker already registered for this page.
|
|
318
|
+
return new Promise((resolve, reject) => {
|
|
319
|
+
const timeout = setTimeout(() => reject(new Error('Push service worker is not available')), 10000);
|
|
320
|
+
navigator.serviceWorker.ready.then(registration => {
|
|
321
|
+
clearTimeout(timeout);
|
|
322
|
+
resolve(registration);
|
|
323
|
+
}, error => {
|
|
324
|
+
clearTimeout(timeout);
|
|
325
|
+
reject(error);
|
|
393
326
|
});
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
get: function get() {
|
|
407
|
-
return typeof window !== 'undefined' && window.isSecureContext === true && typeof navigator !== 'undefined' && 'serviceWorker' in navigator && typeof PushManager !== 'undefined' && typeof Notification !== 'undefined';
|
|
408
|
-
}
|
|
409
|
-
}]);
|
|
410
|
-
return Push;
|
|
411
|
-
}();
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Whether the current page provides the browser APIs required for Push.
|
|
332
|
+
*
|
|
333
|
+
* @returns {Boolean}
|
|
334
|
+
*/
|
|
335
|
+
static get supported() {
|
|
336
|
+
return typeof window !== 'undefined' && window.isSecureContext === true && typeof navigator !== 'undefined' && 'serviceWorker' in navigator && typeof PushManager !== 'undefined' && typeof Notification !== 'undefined';
|
|
337
|
+
}
|
|
338
|
+
}
|
|
412
339
|
export { Push };
|