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