-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.dist.js
260 lines (221 loc) · 8.51 KB
/
index.dist.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.decodeKey = decodeKey;
exports.encodeKey = encodeKey;
exports.getPushSubscriptionPayload = getPushSubscriptionPayload;
exports.getPushSubscriptionFlow = getPushSubscriptionFlow;
exports.isSupported = void 0;
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
/**
* @type {boolean}
*/
var isSupported = 'serviceWorker' in navigator && 'PushManager' in window && 'showNotification' in ServiceWorkerRegistration.prototype;
/**
* @param {string} type
* @param {PushSubscriptionLogger|null} logger
*
* @return {PushSubscriptionLogger}
*/
exports.isSupported = isSupported;
function getLogger(type, logger) {
return ['debug', 'error'].reduce(function (accumulator, method) {
accumulator[method] = function () {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return logger && logger[method].apply(logger, ["Push API -> [".concat(type, "]:")].concat(args));
};
return accumulator;
}, {});
}
/**
* @param {string} encodedString
*
* @return {Uint8Array}
*/
function decodeKey(encodedString) {
var padding = '='.repeat((4 - encodedString.length % 4) % 4);
var decoded = atob((encodedString + padding).replace(/-/g, '+').replace(/_/g, '/'));
return new Uint8Array(_toConsumableArray(decoded).map(function (char) {
return char.charCodeAt(0);
}));
}
/**
* @param {ArrayBuffer} buffer
*
* @return {string}
*/
function encodeKey(buffer) {
return btoa(String.fromCharCode.apply(String, _toConsumableArray(new Uint8Array(buffer))));
}
/**
* @param {PushSubscription} pushSubscription
*
* @return {PushSubscriptionPayload}
*/
function getPushSubscriptionPayload(pushSubscription) {
return ['p256dh', 'auth'].reduce(function (accumulator, key) {
var value = pushSubscription.getKey(key);
accumulator[key] = value ? encodeKey(value) : null;
return accumulator;
}, {
user_agent: navigator.userAgent,
utc_offset: new Date().getTimezoneOffset() / 60,
encoding: (PushManager.supportedContentEncodings || ['aesgcm'])[0],
endpoint: pushSubscription.endpoint
});
}
/**
* @param {function(method: PushSubscriptionHttpMethod, subscription: PushSubscription): void} sync
* The function to synchronize the subscription stored in a browser with servers that are going to
* be used as notifications dispatchers. This callback MUST NOT THROW exceptions since they will
* force the workflow to behave incorrectly.
* @param {PushSubscriptionLogger} logger
*
* @return {PushSubscriptionFlow}
*
* @example
* The flow with a sync function that correctly handles errors and doesn't let them break the flow.
* @code
* import { getPushSubscriptionFlow, getPushSubscriptionPayload } from 'web-push-api';
*
* const flow = getPushSubscriptionFlow((method, pushSubscription) => {
* request(method, 'web-push-api/subscription', getPushSubscriptionPayload(pushSubscription))
* .then(({ errors }) => errors.map(console.error))
* .catch(console.error);
* });
* @code
*/
function getPushSubscriptionFlow(sync) {
var logger = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : console;
/** @type {NotificationPermission} */
var permission = Notification.permission;
/** @type {PushSubscription|undefined|null} */
var currentSubscription;
return {
getPermission: function getPermission() {
return new Promise(function ($return, $error) {
var _getLogger, debug;
_getLogger = getLogger('getPermission', logger), debug = _getLogger.debug;
debug(permission);
if (permission === 'default') {
return Promise.resolve(Notification.requestPermission()).then(function ($await_4) {
try {
permission = $await_4;
debug(permission);
return $If_1.call(this);
} catch ($boundEx) {
return $error($boundEx);
}
}.bind(this), $error);
}
function $If_1() {
return $return(permission);
}
return $If_1.call(this);
});
},
getSubscription: function getSubscription() {
return new Promise(function ($return, $error) {
var _getLogger2, debug, error;
_getLogger2 = getLogger('getSubscription', logger), debug = _getLogger2.debug, error = _getLogger2.error;
// Force to call "getPermission()".
if (permission !== 'granted') {
error('no permission');
return $error(new Error('Notifications permission is not granted.'));
}
if (currentSubscription instanceof PushSubscription) {
debug('reuse');
return $return(currentSubscription);
}
debug('retrieve');
return Promise.resolve(navigator.serviceWorker.ready).then(function ($await_5) {
try {
return Promise.resolve($await_5.pushManager.getSubscription()).then(function ($await_6) {
try {
currentSubscription = $await_6;
if (currentSubscription instanceof PushSubscription) {
debug('update');
sync('PATCH', currentSubscription);
}
return $return(currentSubscription);
} catch ($boundEx) {
return $error($boundEx);
}
}, $error);
} catch ($boundEx) {
return $error($boundEx);
}
}, $error);
});
},
subscribe: function subscribe(applicationServerKey) {
return new Promise(function ($return, $error) {
var _getLogger3, debug, error;
_getLogger3 = getLogger('subscribe', logger), debug = _getLogger3.debug, error = _getLogger3.error;
// Force to call "getSubscription()".
if (currentSubscription === undefined) {
error('not checked');
return $error(new Error('Check for the existing subscription before requesting a new one.'));
}
if (currentSubscription === null) {
debug('request');
return Promise.resolve(navigator.serviceWorker.ready).then(function ($await_7) {
try {
return Promise.resolve($await_7.pushManager.subscribe({
applicationServerKey: decodeKey(applicationServerKey),
userVisibleOnly: true
})).then(function ($await_8) {
try {
currentSubscription = $await_8;
debug('create');
sync('POST', currentSubscription);
return $If_2.call(this);
} catch ($boundEx) {
return $error($boundEx);
}
}.bind(this), $error);
} catch ($boundEx) {
return $error($boundEx);
}
}.bind(this), $error);
}
function $If_2() {
return $return(currentSubscription);
}
return $If_2.call(this);
});
},
unsubscribe: function unsubscribe() {
return new Promise(function ($return, $error) {
var _getLogger4, debug, error;
_getLogger4 = getLogger('unsubscribe', logger), debug = _getLogger4.debug, error = _getLogger4.error;
// Force to call "subscribe()" or "getSubscription()".
if (currentSubscription instanceof PushSubscription) {
debug('request');
return Promise.resolve(currentSubscription.unsubscribe()).then(function ($await_9) {
try {
if ($await_9) {
debug('delete');
sync('DELETE', currentSubscription);
currentSubscription = null;
return $return(null);
}
error('failed');
return $error(new Error('Unable to unsubscribe.'));
} catch ($boundEx) {
return $error($boundEx);
}
}, $error);
}
error('logic violation');
return $error(new Error('Unsubscription cannot be performed while there is no subscription.'));
});
}
};
}