web-manager 4.3.2 → 4.3.3

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.
@@ -1,347 +0,0 @@
1
- class ServiceWorker {
2
- constructor(manager) {
3
- this.manager = manager;
4
- this._registration = null;
5
- this._updateCallbacks = [];
6
- this._messageHandlers = new Map();
7
- }
8
-
9
- // Check if service workers are supported
10
- isSupported() {
11
- return 'serviceWorker' in navigator;
12
- }
13
-
14
- // Register service worker
15
- async register(options = {}) {
16
- try {
17
- if (!this.isSupported()) {
18
- console.warn('Service Workers are not supported');
19
- return null;
20
- }
21
-
22
- const swPath = options.path || this.manager.config.serviceWorker?.config?.path || '/service-worker.js';
23
- const scope = options.scope || '/';
24
-
25
- // Build config object to pass to service worker
26
- const config = {
27
- app: this.manager.config.brand?.id,
28
- environment: this.manager.config.environment,
29
- buildTime: this.manager.config.buildTime,
30
- firebase: this.manager.config.firebase?.app?.config || null
31
- };
32
-
33
- // Get service worker URL with just cache breaker
34
- const cacheBreaker = config.buildTime || Date.now();
35
- const swUrl = `${swPath}?cb=${cacheBreaker}`;
36
-
37
- // Get existing registrations
38
- const registrations = await navigator.serviceWorker.getRegistrations();
39
-
40
- // Check if service worker is already registered for this scope
41
- let registration = registrations.find(reg =>
42
- reg.scope === new URL(scope, window.location.href).href
43
- );
44
-
45
- // Always register/re-register to ensure we have the correct URL with config
46
- console.log('----WM SW 1');
47
-
48
- if (registration) {
49
- console.log('----WM SW 2');
50
- console.log('Unregistering existing service worker to update config');
51
- await registration.unregister();
52
- // Wait a bit for unregistration to complete
53
- await new Promise(resolve => setTimeout(resolve, 100));
54
- }
55
- console.log('----WM SW 3');
56
-
57
- console.log('Registering service worker with cache breaker');
58
- registration = await navigator.serviceWorker.register(swUrl, {
59
- scope,
60
- updateViaCache: options.updateViaCache || 'imports'
61
- });
62
-
63
- // Wait for the service worker to be ready
64
- await navigator.serviceWorker.ready;
65
-
66
- // Send the full config via postMessage after registration
67
- console.log('----WM SW 4');
68
- if (registration.active) {
69
- console.log('----WM SW 5');
70
- try {
71
- this.postMessage({
72
- command: 'update-config',
73
- payload: config
74
- });
75
- } catch (error) {
76
- console.warn('Could not send config to service worker:', error);
77
- }
78
- }
79
-
80
- this._registration = registration;
81
- this.manager.state.serviceWorker = registration;
82
- console.log('----WM SW 6');
83
-
84
- // Set up update handlers
85
- this._setupUpdateHandlers(registration);
86
- console.log('----WM SW 7');
87
-
88
- // Check for updates
89
- if (options.checkForUpdate !== false) {
90
- registration.update();
91
- }
92
- console.log('----WM SW 8');
93
-
94
- // Set up message channel
95
- if (registration.active) {
96
- this._setupMessageChannel();
97
- }
98
- console.log('----WM SW 9');
99
-
100
- return registration;
101
- } catch (error) {
102
- console.error('Service Worker registration failed:', error);
103
- throw error;
104
- }
105
- }
106
-
107
- // Unregister service worker
108
- async unregister() {
109
- try {
110
- if (!this._registration) {
111
- const registrations = await navigator.serviceWorker.getRegistrations();
112
- for (const registration of registrations) {
113
- await registration.unregister();
114
- }
115
- } else {
116
- await this._registration.unregister();
117
- }
118
-
119
- this._registration = null;
120
- this.manager.state.serviceWorker = null;
121
-
122
- return true;
123
- } catch (error) {
124
- console.error('Service Worker unregistration failed:', error);
125
- return false;
126
- }
127
- }
128
-
129
- // Get current registration
130
- getRegistration() {
131
- return this._registration;
132
- }
133
-
134
- // Check for updates
135
- async update() {
136
- try {
137
- if (!this._registration) {
138
- throw new Error('No service worker registered');
139
- }
140
-
141
- await this._registration.update();
142
- return true;
143
- } catch (error) {
144
- console.error('Service Worker update failed:', error);
145
- return false;
146
- }
147
- }
148
-
149
- // Post message to service worker
150
- postMessage(message, options = {}) {
151
- return new Promise((resolve, reject) => {
152
- if (!this.isSupported()) {
153
- return reject(new Error('Service Workers not supported'));
154
- }
155
-
156
- const controller = this._registration?.active || navigator.serviceWorker.controller;
157
-
158
- if (!controller) {
159
- return reject(new Error('No active service worker'));
160
- }
161
-
162
- const messageChannel = new MessageChannel();
163
- const timeout = options.timeout || 5000;
164
- let timeoutId;
165
-
166
- // Set up timeout
167
- if (timeout > 0) {
168
- timeoutId = setTimeout(() => {
169
- messageChannel.port1.close();
170
- reject(new Error('Service worker message timeout'));
171
- }, timeout);
172
- }
173
-
174
- // Listen for response
175
- messageChannel.port1.onmessage = (event) => {
176
- clearTimeout(timeoutId);
177
-
178
- if (event.data.error) {
179
- reject(new Error(event.data.error));
180
- } else {
181
- resolve(event.data);
182
- }
183
- };
184
-
185
- // Send message
186
- controller.postMessage(message, [messageChannel.port2]);
187
- });
188
- }
189
-
190
- // Listen for messages from service worker
191
- onMessage(type, handler) {
192
- if (!this.isSupported()) {
193
- return () => {};
194
- }
195
-
196
- // Store handler
197
- if (!this._messageHandlers.has(type)) {
198
- this._messageHandlers.set(type, new Set());
199
- }
200
- this._messageHandlers.get(type).add(handler);
201
-
202
- // Set up global message listener if not already done
203
- if (this._messageHandlers.size === 1) {
204
- navigator.serviceWorker.addEventListener('message', this._handleMessage.bind(this));
205
- }
206
-
207
- // Return unsubscribe function
208
- return () => {
209
- const handlers = this._messageHandlers.get(type);
210
- if (handlers) {
211
- handlers.delete(handler);
212
- if (handlers.size === 0) {
213
- this._messageHandlers.delete(type);
214
- }
215
- }
216
- };
217
- }
218
-
219
- // Skip waiting and activate new service worker
220
- async skipWaiting() {
221
- try {
222
- if (!this._registration?.waiting) {
223
- throw new Error('No service worker waiting');
224
- }
225
-
226
- // Post message to skip waiting
227
- await this.postMessage({ action: 'skipWaiting' });
228
-
229
- // Reload page after activation
230
- let refreshing = false;
231
- navigator.serviceWorker.addEventListener('controllerchange', () => {
232
- if (!refreshing) {
233
- refreshing = true;
234
- window.location.reload();
235
- }
236
- });
237
-
238
- return true;
239
- } catch (error) {
240
- console.error('Skip waiting failed:', error);
241
- return false;
242
- }
243
- }
244
-
245
- // Listen for update events
246
- onUpdateFound(callback) {
247
- this._updateCallbacks.push(callback);
248
-
249
- return () => {
250
- const index = this._updateCallbacks.indexOf(callback);
251
- if (index > -1) {
252
- this._updateCallbacks.splice(index, 1);
253
- }
254
- };
255
- }
256
-
257
- // Get service worker state
258
- getState() {
259
- if (!this._registration) {
260
- return 'none';
261
- }
262
-
263
- if (this._registration.installing) {
264
- return 'installing';
265
- } else if (this._registration.waiting) {
266
- return 'waiting';
267
- } else if (this._registration.active) {
268
- return 'active';
269
- }
270
-
271
- return 'unknown';
272
- }
273
-
274
- // Private: Set up update handlers
275
- _setupUpdateHandlers(registration) {
276
- // Listen for updates
277
- registration.addEventListener('updatefound', () => {
278
- const newWorker = registration.installing;
279
-
280
- if (!newWorker) return;
281
-
282
- newWorker.addEventListener('statechange', () => {
283
- if (newWorker.state === 'installed' && navigator.serviceWorker.controller) {
284
- // New service worker available
285
- this._notifyUpdateCallbacks({
286
- type: 'update-available',
287
- worker: newWorker
288
- });
289
-
290
- // Automatically skip waiting and activate new worker
291
- if (this.manager.config.serviceWorker?.autoUpdate !== false) {
292
- this.skipWaiting();
293
- }
294
- }
295
- });
296
- });
297
-
298
- // Listen for controller changes
299
- let refreshing = false;
300
- navigator.serviceWorker.addEventListener('controllerchange', () => {
301
- if (!refreshing) {
302
- this._notifyUpdateCallbacks({
303
- type: 'controller-change'
304
- });
305
- }
306
- });
307
- }
308
-
309
- // Private: Notify update callbacks
310
- _notifyUpdateCallbacks(event) {
311
- this._updateCallbacks.forEach(callback => {
312
- try {
313
- callback(event);
314
- } catch (error) {
315
- console.error('Update callback error:', error);
316
- }
317
- });
318
- }
319
-
320
- // Private: Handle incoming messages
321
- _handleMessage(event) {
322
- const { type, ...data } = event.data || {};
323
-
324
- if (!type) return;
325
-
326
- const handlers = this._messageHandlers.get(type);
327
- if (handlers) {
328
- handlers.forEach(handler => {
329
- try {
330
- handler(data, event);
331
- } catch (error) {
332
- console.error('Message handler error:', error);
333
- }
334
- });
335
- }
336
- }
337
-
338
- // Private: Set up message channel
339
- _setupMessageChannel() {
340
- // This ensures we can communicate with the service worker
341
- navigator.serviceWorker.ready.then(() => {
342
- console.log('Service Worker ready for messaging');
343
- });
344
- }
345
- }
346
-
347
- export default ServiceWorker;
@@ -1,158 +0,0 @@
1
- const package = require('../package.json');
2
- const assert = require('assert');
3
-
4
- beforeEach(() => {
5
- });
6
-
7
- before(() => {
8
- });
9
-
10
- after(() => {
11
- });
12
-
13
- /*
14
- * ============
15
- * Test Cases
16
- * ============
17
- */
18
- describe(`${package.name}`, () => {
19
- const _ = require('../lib/utilities.js');
20
- const lodash = require('lodash');
21
-
22
- // Utilities
23
- describe('.utilities()', () => {
24
- const sample = {
25
- main: {
26
- true: true,
27
- false: false,
28
- zero: 0,
29
- one: 1,
30
- string: 'string',
31
- stringEmpty: '',
32
- object: {},
33
- array: [],
34
- null: null,
35
- undefined: undefined,
36
- nan: NaN,
37
- infinity: Infinity,
38
- negativeInfinity: -Infinity,
39
- function: function () {},
40
- }
41
- }
42
-
43
- // Normal Cases
44
- describe('.get()', () => {
45
- it('should return true', () => {
46
- // console.log('---lodash', lodash.get(sample, 'main.true'));
47
- // console.log('---_.get', _.get(sample, 'main.true'));
48
- // console.log('---_.get2', _.get2(sample, 'main.true'));
49
-
50
- assert.strictEqual(_.get(sample, 'main.true'), true);
51
- });
52
-
53
- it('should return false', () => {
54
- assert.strictEqual(_.get(sample, 'main.false'), false);
55
- });
56
-
57
- it('should return 0', () => {
58
- assert.strictEqual(_.get(sample, 'main.zero'), 0);
59
- });
60
-
61
- it('should return 1', () => {
62
- assert.strictEqual(_.get(sample, 'main.one'), 1);
63
- });
64
-
65
- it('should return a string', () => {
66
- assert.strictEqual(_.get(sample, 'main.string'), 'string');
67
- });
68
-
69
- it('should return an empty string', () => {
70
- assert.strictEqual(_.get(sample, 'main.stringEmpty'), '');
71
- });
72
-
73
- it('should return an object', () => {
74
- assert.deepStrictEqual(_.get(sample, 'main.object'), {});
75
- });
76
-
77
- it('should return an array', () => {
78
- assert.deepStrictEqual(_.get(sample, 'main.array'), []);
79
- });
80
-
81
- it('should return null', () => {
82
- assert.strictEqual(_.get(sample, 'main.null'), null);
83
- });
84
-
85
- it('should return undefined', () => {
86
- assert.strictEqual(_.get(sample, 'main.undefined'), undefined);
87
- });
88
-
89
- it('should return NaN', () => {
90
- assert.strictEqual(Number.isNaN(_.get(sample, 'main.nan')), true);
91
- });
92
-
93
- it('should return Infinity', () => {
94
- assert.strictEqual(_.get(sample, 'main.infinity'), Infinity);
95
- });
96
-
97
- it('should return -Infinity', () => {
98
- assert.strictEqual(_.get(sample, 'main.negativeInfinity'), -Infinity);
99
- });
100
-
101
- it('should return a function', () => {
102
- assert.strictEqual(typeof _.get(sample, 'main.function'), 'function');
103
- });
104
-
105
- // Non-existent
106
- it('should return undefined', () => {
107
- assert.strictEqual(_.get(sample, 'main.nonexistent'), undefined);
108
- });
109
-
110
- // No path
111
- it('should return undefined', () => {
112
- assert.strictEqual(_.get(sample), undefined);
113
- });
114
-
115
- // Empty path
116
- it('should return undefined', () => {
117
- assert.strictEqual(_.get(sample, ''), undefined);
118
- });
119
-
120
- // Default value
121
- it('should return default value', () => {
122
- assert.strictEqual(_.get(sample, 'main.nonexistent', 'default'), 'default');
123
- });
124
-
125
- it('should return actual value', () => {
126
- assert.strictEqual(_.get(sample, 'main.false', 'default'), false);
127
- });
128
-
129
- it('should return actual value', () => {
130
- assert.strictEqual(_.get(sample, 'main.null', 'default'), null);
131
- });
132
-
133
- it('should return default value', () => {
134
- assert.strictEqual(_.get(sample, 'main.undefined', 'default'), 'default');
135
- });
136
-
137
- // Non-object for object
138
- it('should return undefined', () => {
139
- assert.strictEqual(_.get(undefined, 'main.true'), undefined);
140
- });
141
-
142
- it('should return undefined', () => {
143
- assert.strictEqual(_.get(null, 'main.true'), undefined);
144
- });
145
-
146
- it('should return undefined', () => {
147
- assert.strictEqual(_.get(false, 'main.true'), undefined);
148
- });
149
-
150
- it('should return undefined', () => {
151
- assert.strictEqual(_.get('', 'main.true'), undefined);
152
- });
153
-
154
- });
155
-
156
- });
157
-
158
- })