@factoringplus/pl-components-pack-v3 0.5.81 → 0.5.82

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.
@@ -12,158 +12,158 @@ const INTEGRITY_CHECKSUM = 'c9450df6e4dc5e45740c3b0b640727a2';
12
12
  const activeClientIds = new Set();
13
13
 
14
14
  self.addEventListener('install', function () {
15
- self.skipWaiting();
15
+ self.skipWaiting();
16
16
  });
17
17
 
18
18
  self.addEventListener('activate', function (event) {
19
- event.waitUntil(self.clients.claim());
19
+ event.waitUntil(self.clients.claim());
20
20
  });
21
21
 
22
22
  self.addEventListener('message', async function (event) {
23
- const clientId = event.source.id;
24
-
25
- if (!clientId || !self.clients) {
26
- return;
27
- }
28
-
29
- const client = await self.clients.get(clientId);
30
-
31
- if (!client) {
32
- return;
33
- }
34
-
35
- const allClients = await self.clients.matchAll({
36
- type: 'window',
37
- });
38
-
39
- switch (event.data) {
40
- case 'KEEPALIVE_REQUEST': {
41
- sendToClient(client, {
42
- type: 'KEEPALIVE_RESPONSE',
43
- });
44
- break;
45
- }
46
-
47
- case 'INTEGRITY_CHECK_REQUEST': {
48
- sendToClient(client, {
49
- type: 'INTEGRITY_CHECK_RESPONSE',
50
- payload: INTEGRITY_CHECKSUM,
51
- });
52
- break;
53
- }
54
-
55
- case 'MOCK_ACTIVATE': {
56
- activeClientIds.add(clientId);
57
-
58
- sendToClient(client, {
59
- type: 'MOCKING_ENABLED',
60
- payload: true,
61
- });
62
- break;
63
- }
64
-
65
- case 'MOCK_DEACTIVATE': {
66
- activeClientIds.delete(clientId);
67
- break;
68
- }
69
-
70
- case 'CLIENT_CLOSED': {
71
- activeClientIds.delete(clientId);
72
-
73
- const remainingClients = allClients.filter((client) => {
74
- return client.id !== clientId;
75
- });
76
-
77
- // Unregister itself when there are no more clients
78
- if (remainingClients.length === 0) {
79
- self.registration.unregister();
80
- }
81
-
82
- break;
83
- }
84
- }
23
+ const clientId = event.source.id;
24
+
25
+ if (!clientId || !self.clients) {
26
+ return;
27
+ }
28
+
29
+ const client = await self.clients.get(clientId);
30
+
31
+ if (!client) {
32
+ return;
33
+ }
34
+
35
+ const allClients = await self.clients.matchAll({
36
+ type: 'window',
37
+ });
38
+
39
+ switch (event.data) {
40
+ case 'KEEPALIVE_REQUEST': {
41
+ sendToClient(client, {
42
+ type: 'KEEPALIVE_RESPONSE',
43
+ });
44
+ break;
45
+ }
46
+
47
+ case 'INTEGRITY_CHECK_REQUEST': {
48
+ sendToClient(client, {
49
+ type: 'INTEGRITY_CHECK_RESPONSE',
50
+ payload: INTEGRITY_CHECKSUM,
51
+ });
52
+ break;
53
+ }
54
+
55
+ case 'MOCK_ACTIVATE': {
56
+ activeClientIds.add(clientId);
57
+
58
+ sendToClient(client, {
59
+ type: 'MOCKING_ENABLED',
60
+ payload: true,
61
+ });
62
+ break;
63
+ }
64
+
65
+ case 'MOCK_DEACTIVATE': {
66
+ activeClientIds.delete(clientId);
67
+ break;
68
+ }
69
+
70
+ case 'CLIENT_CLOSED': {
71
+ activeClientIds.delete(clientId);
72
+
73
+ const remainingClients = allClients.filter((client) => {
74
+ return client.id !== clientId;
75
+ });
76
+
77
+ // Unregister itself when there are no more clients
78
+ if (remainingClients.length === 0) {
79
+ self.registration.unregister();
80
+ }
81
+
82
+ break;
83
+ }
84
+ }
85
85
  });
86
86
 
87
87
  self.addEventListener('fetch', function (event) {
88
- const { request } = event;
89
- const accept = request.headers.get('accept') || '';
90
-
91
- // Bypass server-sent events.
92
- if (accept.includes('text/event-stream')) {
93
- return;
94
- }
95
-
96
- // Bypass navigation requests.
97
- if (request.mode === 'navigate') {
98
- return;
99
- }
100
-
101
- // Opening the DevTools triggers the "only-if-cached" request
102
- // that cannot be handled by the worker. Bypass such requests.
103
- if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') {
104
- return;
105
- }
106
-
107
- // Bypass all requests when there are no active clients.
108
- // Prevents the self-unregistered worked from handling requests
109
- // after it's been deleted (still remains active until the next reload).
110
- if (activeClientIds.size === 0) {
111
- return;
112
- }
113
-
114
- // Generate unique request ID.
115
- const requestId = Math.random().toString(16).slice(2);
116
-
117
- event.respondWith(
118
- handleRequest(event, requestId).catch((error) => {
119
- if (error.name === 'NetworkError') {
120
- console.warn(
121
- '[MSW] Successfully emulated a network error for the "%s %s" request.',
122
- request.method,
123
- request.url,
124
- );
125
- return;
126
- }
127
-
128
- // At this point, any exception indicates an issue with the original request/response.
129
- console.error(
130
- `\
88
+ const { request } = event;
89
+ const accept = request.headers.get('accept') || '';
90
+
91
+ // Bypass server-sent events.
92
+ if (accept.includes('text/event-stream')) {
93
+ return;
94
+ }
95
+
96
+ // Bypass navigation requests.
97
+ if (request.mode === 'navigate') {
98
+ return;
99
+ }
100
+
101
+ // Opening the DevTools triggers the "only-if-cached" request
102
+ // that cannot be handled by the worker. Bypass such requests.
103
+ if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') {
104
+ return;
105
+ }
106
+
107
+ // Bypass all requests when there are no active clients.
108
+ // Prevents the self-unregistered worked from handling requests
109
+ // after it's been deleted (still remains active until the next reload).
110
+ if (activeClientIds.size === 0) {
111
+ return;
112
+ }
113
+
114
+ // Generate unique request ID.
115
+ const requestId = Math.random().toString(16).slice(2);
116
+
117
+ event.respondWith(
118
+ handleRequest(event, requestId).catch((error) => {
119
+ if (error.name === 'NetworkError') {
120
+ console.warn(
121
+ '[MSW] Successfully emulated a network error for the "%s %s" request.',
122
+ request.method,
123
+ request.url,
124
+ );
125
+ return;
126
+ }
127
+
128
+ // At this point, any exception indicates an issue with the original request/response.
129
+ console.error(
130
+ `\
131
131
  [MSW] Caught an exception from the "%s %s" request (%s). This is probably not a problem with Mock Service Worker. There is likely an additional logging output above.`,
132
- request.method,
133
- request.url,
134
- `${error.name}: ${error.message}`,
135
- );
136
- }),
137
- );
132
+ request.method,
133
+ request.url,
134
+ `${error.name}: ${error.message}`,
135
+ );
136
+ }),
137
+ );
138
138
  });
139
139
 
140
140
  async function handleRequest(event, requestId) {
141
- const client = await resolveMainClient(event);
142
- const response = await getResponse(event, client, requestId);
143
-
144
- // Send back the response clone for the "response:*" life-cycle events.
145
- // Ensure MSW is active and ready to handle the message, otherwise
146
- // this message will pend indefinitely.
147
- if (client && activeClientIds.has(client.id)) {
148
- (async function () {
149
- const clonedResponse = response.clone();
150
- sendToClient(client, {
151
- type: 'RESPONSE',
152
- payload: {
153
- requestId,
154
- type: clonedResponse.type,
155
- ok: clonedResponse.ok,
156
- status: clonedResponse.status,
157
- statusText: clonedResponse.statusText,
158
- body: clonedResponse.body === null ? null : await clonedResponse.text(),
159
- headers: Object.fromEntries(clonedResponse.headers.entries()),
160
- redirected: clonedResponse.redirected,
161
- },
162
- });
163
- })();
164
- }
165
-
166
- return response;
141
+ const client = await resolveMainClient(event);
142
+ const response = await getResponse(event, client, requestId);
143
+
144
+ // Send back the response clone for the "response:*" life-cycle events.
145
+ // Ensure MSW is active and ready to handle the message, otherwise
146
+ // this message will pend indefinitely.
147
+ if (client && activeClientIds.has(client.id)) {
148
+ (async function () {
149
+ const clonedResponse = response.clone();
150
+ sendToClient(client, {
151
+ type: 'RESPONSE',
152
+ payload: {
153
+ requestId,
154
+ type: clonedResponse.type,
155
+ ok: clonedResponse.ok,
156
+ status: clonedResponse.status,
157
+ statusText: clonedResponse.statusText,
158
+ body: clonedResponse.body === null ? null : await clonedResponse.text(),
159
+ headers: Object.fromEntries(clonedResponse.headers.entries()),
160
+ redirected: clonedResponse.redirected,
161
+ },
162
+ });
163
+ })();
164
+ }
165
+
166
+ return response;
167
167
  }
168
168
 
169
169
  // Resolve the main client for the given event.
@@ -171,194 +171,194 @@ async function handleRequest(event, requestId) {
171
171
  // that registered the worker. It's with the latter the worker should
172
172
  // communicate with during the response resolving phase.
173
173
  async function resolveMainClient(event) {
174
- const client = await self.clients.get(event.clientId);
175
-
176
- if (client.frameType === 'top-level') {
177
- return client;
178
- }
179
-
180
- const allClients = await self.clients.matchAll({
181
- type: 'window',
182
- });
183
-
184
- return allClients
185
- .filter((client) => {
186
- // Get only those clients that are currently visible.
187
- return client.visibilityState === 'visible';
188
- })
189
- .find((client) => {
190
- // Find the client ID that's recorded in the
191
- // set of clients that have registered the worker.
192
- return activeClientIds.has(client.id);
193
- });
174
+ const client = await self.clients.get(event.clientId);
175
+
176
+ if (client.frameType === 'top-level') {
177
+ return client;
178
+ }
179
+
180
+ const allClients = await self.clients.matchAll({
181
+ type: 'window',
182
+ });
183
+
184
+ return allClients
185
+ .filter((client) => {
186
+ // Get only those clients that are currently visible.
187
+ return client.visibilityState === 'visible';
188
+ })
189
+ .find((client) => {
190
+ // Find the client ID that's recorded in the
191
+ // set of clients that have registered the worker.
192
+ return activeClientIds.has(client.id);
193
+ });
194
194
  }
195
195
 
196
196
  async function getResponse(event, client, requestId) {
197
- const { request } = event;
198
- const clonedRequest = request.clone();
199
-
200
- function passthrough() {
201
- // Clone the request because it might've been already used
202
- // (i.e. its body has been read and sent to the cilent).
203
- const headers = Object.fromEntries(clonedRequest.headers.entries());
204
-
205
- // Remove MSW-specific request headers so the bypassed requests
206
- // comply with the server's CORS preflight check.
207
- // Operate with the headers as an object because request "Headers"
208
- // are immutable.
209
- delete headers['x-msw-bypass'];
210
-
211
- return fetch(clonedRequest, { headers });
212
- }
213
-
214
- // Bypass mocking when the client is not active.
215
- if (!client) {
216
- return passthrough();
217
- }
218
-
219
- // Bypass initial page load requests (i.e. static assets).
220
- // The absence of the immediate/parent client in the map of the active clients
221
- // means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet
222
- // and is not ready to handle requests.
223
- if (!activeClientIds.has(client.id)) {
224
- return passthrough();
225
- }
226
-
227
- // Bypass requests with the explicit bypass header.
228
- // Such requests can be issued by "ctx.fetch()".
229
- if (request.headers.get('x-msw-bypass') === 'true') {
230
- return passthrough();
231
- }
232
-
233
- // Create a communication channel scoped to the current request.
234
- // This way events can be exchanged outside of the worker's global
235
- // "message" event listener (i.e. abstracted into functions).
236
- const operationChannel = new BroadcastChannel(`msw-response-stream-${requestId}`);
237
-
238
- // Notify the client that a request has been intercepted.
239
- const clientMessage = await sendToClient(client, {
240
- type: 'REQUEST',
241
- payload: {
242
- id: requestId,
243
- url: request.url,
244
- method: request.method,
245
- headers: Object.fromEntries(request.headers.entries()),
246
- cache: request.cache,
247
- mode: request.mode,
248
- credentials: request.credentials,
249
- destination: request.destination,
250
- integrity: request.integrity,
251
- redirect: request.redirect,
252
- referrer: request.referrer,
253
- referrerPolicy: request.referrerPolicy,
254
- body: await request.text(),
255
- bodyUsed: request.bodyUsed,
256
- keepalive: request.keepalive,
257
- },
258
- });
259
-
260
- switch (clientMessage.type) {
261
- case 'MOCK_RESPONSE': {
262
- return respondWithMock(clientMessage.payload);
263
- }
264
-
265
- case 'MOCK_RESPONSE_START': {
266
- return respondWithMockStream(operationChannel, clientMessage.payload);
267
- }
268
-
269
- case 'MOCK_NOT_FOUND': {
270
- return passthrough();
271
- }
272
-
273
- case 'NETWORK_ERROR': {
274
- const { name, message } = clientMessage.payload;
275
- const networkError = new Error(message);
276
- networkError.name = name;
277
-
278
- // Rejecting a "respondWith" promise emulates a network error.
279
- throw networkError;
280
- }
281
-
282
- case 'INTERNAL_ERROR': {
283
- const parsedBody = JSON.parse(clientMessage.payload.body);
284
-
285
- console.error(
286
- `\
197
+ const { request } = event;
198
+ const clonedRequest = request.clone();
199
+
200
+ function passthrough() {
201
+ // Clone the request because it might've been already used
202
+ // (i.e. its body has been read and sent to the cilent).
203
+ const headers = Object.fromEntries(clonedRequest.headers.entries());
204
+
205
+ // Remove MSW-specific request headers so the bypassed requests
206
+ // comply with the server's CORS preflight check.
207
+ // Operate with the headers as an object because request "Headers"
208
+ // are immutable.
209
+ delete headers['x-msw-bypass'];
210
+
211
+ return fetch(clonedRequest, { headers });
212
+ }
213
+
214
+ // Bypass mocking when the client is not active.
215
+ if (!client) {
216
+ return passthrough();
217
+ }
218
+
219
+ // Bypass initial page load requests (i.e. static assets).
220
+ // The absence of the immediate/parent client in the map of the active clients
221
+ // means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet
222
+ // and is not ready to handle requests.
223
+ if (!activeClientIds.has(client.id)) {
224
+ return passthrough();
225
+ }
226
+
227
+ // Bypass requests with the explicit bypass header.
228
+ // Such requests can be issued by "ctx.fetch()".
229
+ if (request.headers.get('x-msw-bypass') === 'true') {
230
+ return passthrough();
231
+ }
232
+
233
+ // Create a communication channel scoped to the current request.
234
+ // This way events can be exchanged outside of the worker's global
235
+ // "message" event listener (i.e. abstracted into functions).
236
+ const operationChannel = new BroadcastChannel(`msw-response-stream-${requestId}`);
237
+
238
+ // Notify the client that a request has been intercepted.
239
+ const clientMessage = await sendToClient(client, {
240
+ type: 'REQUEST',
241
+ payload: {
242
+ id: requestId,
243
+ url: request.url,
244
+ method: request.method,
245
+ headers: Object.fromEntries(request.headers.entries()),
246
+ cache: request.cache,
247
+ mode: request.mode,
248
+ credentials: request.credentials,
249
+ destination: request.destination,
250
+ integrity: request.integrity,
251
+ redirect: request.redirect,
252
+ referrer: request.referrer,
253
+ referrerPolicy: request.referrerPolicy,
254
+ body: await request.text(),
255
+ bodyUsed: request.bodyUsed,
256
+ keepalive: request.keepalive,
257
+ },
258
+ });
259
+
260
+ switch (clientMessage.type) {
261
+ case 'MOCK_RESPONSE': {
262
+ return respondWithMock(clientMessage.payload);
263
+ }
264
+
265
+ case 'MOCK_RESPONSE_START': {
266
+ return respondWithMockStream(operationChannel, clientMessage.payload);
267
+ }
268
+
269
+ case 'MOCK_NOT_FOUND': {
270
+ return passthrough();
271
+ }
272
+
273
+ case 'NETWORK_ERROR': {
274
+ const { name, message } = clientMessage.payload;
275
+ const networkError = new Error(message);
276
+ networkError.name = name;
277
+
278
+ // Rejecting a "respondWith" promise emulates a network error.
279
+ throw networkError;
280
+ }
281
+
282
+ case 'INTERNAL_ERROR': {
283
+ const parsedBody = JSON.parse(clientMessage.payload.body);
284
+
285
+ console.error(
286
+ `\
287
287
  [MSW] Uncaught exception in the request handler for "%s %s":
288
288
 
289
289
  ${parsedBody.location}
290
290
 
291
291
  This exception has been gracefully handled as a 500 response, however, it's strongly recommended to resolve this error, as it indicates a mistake in your code. If you wish to mock an error response, please see this guide: https://mswjs.io/docs/recipes/mocking-error-responses\
292
292
  `,
293
- request.method,
294
- request.url,
295
- );
293
+ request.method,
294
+ request.url,
295
+ );
296
296
 
297
- return respondWithMock(clientMessage.payload);
298
- }
299
- }
297
+ return respondWithMock(clientMessage.payload);
298
+ }
299
+ }
300
300
 
301
- return passthrough();
301
+ return passthrough();
302
302
  }
303
303
 
304
304
  function sendToClient(client, message) {
305
- return new Promise((resolve, reject) => {
306
- const channel = new MessageChannel();
305
+ return new Promise((resolve, reject) => {
306
+ const channel = new MessageChannel();
307
307
 
308
- channel.port1.onmessage = (event) => {
309
- if (event.data && event.data.error) {
310
- return reject(event.data.error);
311
- }
308
+ channel.port1.onmessage = (event) => {
309
+ if (event.data && event.data.error) {
310
+ return reject(event.data.error);
311
+ }
312
312
 
313
- resolve(event.data);
314
- };
313
+ resolve(event.data);
314
+ };
315
315
 
316
- client.postMessage(JSON.stringify(message), [channel.port2]);
317
- });
316
+ client.postMessage(JSON.stringify(message), [channel.port2]);
317
+ });
318
318
  }
319
319
 
320
320
  function sleep(timeMs) {
321
- return new Promise((resolve) => {
322
- setTimeout(resolve, timeMs);
323
- });
321
+ return new Promise((resolve) => {
322
+ setTimeout(resolve, timeMs);
323
+ });
324
324
  }
325
325
 
326
326
  async function respondWithMock(response) {
327
- await sleep(response.delay);
328
- return new Response(response.body, response);
327
+ await sleep(response.delay);
328
+ return new Response(response.body, response);
329
329
  }
330
330
 
331
331
  function respondWithMockStream(operationChannel, mockResponse) {
332
- let streamCtrl;
333
- const stream = new ReadableStream({
334
- start: (controller) => (streamCtrl = controller),
335
- });
336
-
337
- return new Promise(async (resolve, reject) => {
338
- operationChannel.onmessageerror = (event) => {
339
- operationChannel.close();
340
- return reject(event.data.error);
341
- };
342
-
343
- operationChannel.onmessage = (event) => {
344
- if (!event.data) {
345
- return;
346
- }
347
-
348
- switch (event.data.type) {
349
- case 'MOCK_RESPONSE_CHUNK': {
350
- streamCtrl.enqueue(event.data.payload);
351
- break;
352
- }
353
-
354
- case 'MOCK_RESPONSE_END': {
355
- streamCtrl.close();
356
- operationChannel.close();
357
- }
358
- }
359
- };
360
-
361
- await sleep(mockResponse.delay);
362
- return resolve(new Response(stream, mockResponse));
363
- });
332
+ let streamCtrl;
333
+ const stream = new ReadableStream({
334
+ start: (controller) => (streamCtrl = controller),
335
+ });
336
+
337
+ return new Promise(async (resolve, reject) => {
338
+ operationChannel.onmessageerror = (event) => {
339
+ operationChannel.close();
340
+ return reject(event.data.error);
341
+ };
342
+
343
+ operationChannel.onmessage = (event) => {
344
+ if (!event.data) {
345
+ return;
346
+ }
347
+
348
+ switch (event.data.type) {
349
+ case 'MOCK_RESPONSE_CHUNK': {
350
+ streamCtrl.enqueue(event.data.payload);
351
+ break;
352
+ }
353
+
354
+ case 'MOCK_RESPONSE_END': {
355
+ streamCtrl.close();
356
+ operationChannel.close();
357
+ }
358
+ }
359
+ };
360
+
361
+ await sleep(mockResponse.delay);
362
+ return resolve(new Response(stream, mockResponse));
363
+ });
364
364
  }