@theholocron/react-template 0.4.0 → 0.4.1
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/dist/mockServiceWorker.js +286 -301
- package/package.json +1 -1
|
@@ -7,114 +7,111 @@
|
|
|
7
7
|
* - Please do NOT modify this file.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
const PACKAGE_VERSION =
|
|
11
|
-
const INTEGRITY_CHECKSUM =
|
|
12
|
-
const IS_MOCKED_RESPONSE = Symbol(
|
|
13
|
-
const activeClientIds = new Set()
|
|
14
|
-
|
|
15
|
-
addEventListener(
|
|
16
|
-
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
addEventListener(
|
|
20
|
-
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
addEventListener(
|
|
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
|
-
addEventListener(
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const requestId = crypto.randomUUID()
|
|
116
|
-
event.respondWith(handleRequest(event, requestId, requestInterceptedAt))
|
|
117
|
-
})
|
|
10
|
+
const PACKAGE_VERSION = "2.15.0";
|
|
11
|
+
const INTEGRITY_CHECKSUM = "03cb67ac84128e63d7cd722a6e5b7f1e";
|
|
12
|
+
const IS_MOCKED_RESPONSE = Symbol("isMockedResponse");
|
|
13
|
+
const activeClientIds = new Set();
|
|
14
|
+
|
|
15
|
+
addEventListener("install", function () {
|
|
16
|
+
self.skipWaiting();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
addEventListener("activate", function (event) {
|
|
20
|
+
event.waitUntil(self.clients.claim());
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
addEventListener("message", async function (event) {
|
|
24
|
+
const clientId = Reflect.get(event.source || {}, "id");
|
|
25
|
+
|
|
26
|
+
if (!clientId || !self.clients) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const client = await self.clients.get(clientId);
|
|
31
|
+
|
|
32
|
+
if (!client) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const allClients = await self.clients.matchAll({
|
|
37
|
+
type: "window",
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
switch (event.data) {
|
|
41
|
+
case "KEEPALIVE_REQUEST": {
|
|
42
|
+
sendToClient(client, {
|
|
43
|
+
type: "KEEPALIVE_RESPONSE",
|
|
44
|
+
});
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
case "INTEGRITY_CHECK_REQUEST": {
|
|
49
|
+
sendToClient(client, {
|
|
50
|
+
type: "INTEGRITY_CHECK_RESPONSE",
|
|
51
|
+
payload: {
|
|
52
|
+
packageVersion: PACKAGE_VERSION,
|
|
53
|
+
checksum: INTEGRITY_CHECKSUM,
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
case "MOCK_ACTIVATE": {
|
|
60
|
+
activeClientIds.add(clientId);
|
|
61
|
+
|
|
62
|
+
sendToClient(client, {
|
|
63
|
+
type: "MOCKING_ENABLED",
|
|
64
|
+
payload: {
|
|
65
|
+
client: {
|
|
66
|
+
id: client.id,
|
|
67
|
+
frameType: client.frameType,
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
case "CLIENT_CLOSED": {
|
|
75
|
+
activeClientIds.delete(clientId);
|
|
76
|
+
|
|
77
|
+
const remainingClients = allClients.filter((client) => {
|
|
78
|
+
return client.id !== clientId;
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Unregister itself when there are no more clients
|
|
82
|
+
if (remainingClients.length === 0) {
|
|
83
|
+
self.registration.unregister();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
addEventListener("fetch", function (event) {
|
|
92
|
+
const requestInterceptedAt = Date.now();
|
|
93
|
+
|
|
94
|
+
// Bypass navigation requests.
|
|
95
|
+
if (event.request.mode === "navigate") {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Opening the DevTools triggers the "only-if-cached" request
|
|
100
|
+
// that cannot be handled by the worker. Bypass such requests.
|
|
101
|
+
if (event.request.cache === "only-if-cached" && event.request.mode !== "same-origin") {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Bypass all requests when there are no active clients.
|
|
106
|
+
// Prevents the self-unregistered worked from handling requests
|
|
107
|
+
// after it's been terminated (still remains active until the next reload).
|
|
108
|
+
if (activeClientIds.size === 0) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const requestId = crypto.randomUUID();
|
|
113
|
+
event.respondWith(handleRequest(event, requestId, requestInterceptedAt));
|
|
114
|
+
});
|
|
118
115
|
|
|
119
116
|
/**
|
|
120
117
|
* @param {FetchEvent} event
|
|
@@ -122,60 +119,53 @@ addEventListener('fetch', function (event) {
|
|
|
122
119
|
* @param {number} requestInterceptedAt
|
|
123
120
|
*/
|
|
124
121
|
async function handleRequest(event, requestId, requestInterceptedAt) {
|
|
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
|
-
responseClone && responseClone.body
|
|
173
|
-
? [serializedRequest.body, responseClone.body]
|
|
174
|
-
: [],
|
|
175
|
-
)
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return response
|
|
122
|
+
const client = await resolveMainClient(event);
|
|
123
|
+
const requestCloneForEvents = event.request.clone();
|
|
124
|
+
const response = await getResponse(event, client, requestId, requestInterceptedAt);
|
|
125
|
+
|
|
126
|
+
// Send back the response clone for the "response:*" life-cycle events.
|
|
127
|
+
// Ensure MSW is active and ready to handle the message, otherwise
|
|
128
|
+
// this message will pend indefinitely.
|
|
129
|
+
if (client && activeClientIds.has(client.id)) {
|
|
130
|
+
const serializedRequest = await serializeRequest(requestCloneForEvents);
|
|
131
|
+
|
|
132
|
+
// Omit the body of server-sent event stream responses.
|
|
133
|
+
// Cloning such responses would prevent client-side stream cancelations
|
|
134
|
+
// from reaching the original stream (a teed stream only cancels its
|
|
135
|
+
// source once both of its branches cancel) and would buffer the
|
|
136
|
+
// entire stream into the unconsumed clone indefinitely.
|
|
137
|
+
const isEventStreamResponse = response.headers
|
|
138
|
+
.get("content-type")
|
|
139
|
+
?.toLowerCase()
|
|
140
|
+
.startsWith("text/event-stream");
|
|
141
|
+
|
|
142
|
+
// Clone the response so both the client and the library could consume it.
|
|
143
|
+
const responseClone = isEventStreamResponse ? null : response.clone();
|
|
144
|
+
|
|
145
|
+
sendToClient(
|
|
146
|
+
client,
|
|
147
|
+
{
|
|
148
|
+
type: "RESPONSE",
|
|
149
|
+
payload: {
|
|
150
|
+
isMockedResponse: IS_MOCKED_RESPONSE in response,
|
|
151
|
+
request: {
|
|
152
|
+
id: requestId,
|
|
153
|
+
...serializedRequest,
|
|
154
|
+
},
|
|
155
|
+
response: {
|
|
156
|
+
type: response.type,
|
|
157
|
+
status: response.status,
|
|
158
|
+
statusText: response.statusText,
|
|
159
|
+
headers: Object.fromEntries(response.headers.entries()),
|
|
160
|
+
body: responseClone ? responseClone.body : null,
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
responseClone && responseClone.body ? [serializedRequest.body, responseClone.body] : []
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return response;
|
|
179
169
|
}
|
|
180
170
|
|
|
181
171
|
/**
|
|
@@ -187,30 +177,30 @@ async function handleRequest(event, requestId, requestInterceptedAt) {
|
|
|
187
177
|
* @returns {Promise<Client | undefined>}
|
|
188
178
|
*/
|
|
189
179
|
async function resolveMainClient(event) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
180
|
+
const client = await self.clients.get(event.clientId);
|
|
181
|
+
|
|
182
|
+
if (activeClientIds.has(event.clientId)) {
|
|
183
|
+
return client;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (client?.frameType === "top-level") {
|
|
187
|
+
return client;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const allClients = await self.clients.matchAll({
|
|
191
|
+
type: "window",
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
return allClients
|
|
195
|
+
.filter((client) => {
|
|
196
|
+
// Get only those clients that are currently visible.
|
|
197
|
+
return client.visibilityState === "visible";
|
|
198
|
+
})
|
|
199
|
+
.find((client) => {
|
|
200
|
+
// Find the client ID that's recorded in the
|
|
201
|
+
// set of clients that have registered the worker.
|
|
202
|
+
return activeClientIds.has(client.id);
|
|
203
|
+
});
|
|
214
204
|
}
|
|
215
205
|
|
|
216
206
|
/**
|
|
@@ -221,74 +211,72 @@ async function resolveMainClient(event) {
|
|
|
221
211
|
* @returns {Promise<Response>}
|
|
222
212
|
*/
|
|
223
213
|
async function getResponse(event, client, requestId, requestInterceptedAt) {
|
|
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
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
return passthrough()
|
|
214
|
+
// Clone the request because it might've been already used
|
|
215
|
+
// (i.e. its body has been read and sent to the client).
|
|
216
|
+
const requestClone = event.request.clone();
|
|
217
|
+
|
|
218
|
+
function passthrough() {
|
|
219
|
+
// Cast the request headers to a new Headers instance
|
|
220
|
+
// so the headers can be manipulated with.
|
|
221
|
+
const headers = new Headers(requestClone.headers);
|
|
222
|
+
|
|
223
|
+
// Remove the "accept" header value that marked this request as passthrough.
|
|
224
|
+
// This prevents request alteration and also keeps it compliant with the
|
|
225
|
+
// user-defined CORS policies.
|
|
226
|
+
const acceptHeader = headers.get("accept");
|
|
227
|
+
if (acceptHeader) {
|
|
228
|
+
const values = acceptHeader.split(",").map((value) => value.trim());
|
|
229
|
+
const filteredValues = values.filter((value) => value !== "msw/passthrough");
|
|
230
|
+
|
|
231
|
+
if (filteredValues.length > 0) {
|
|
232
|
+
headers.set("accept", filteredValues.join(", "));
|
|
233
|
+
} else {
|
|
234
|
+
headers.delete("accept");
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return fetch(requestClone, { headers });
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Bypass mocking when the client is not active.
|
|
242
|
+
if (!client) {
|
|
243
|
+
return passthrough();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Bypass initial page load requests (i.e. static assets).
|
|
247
|
+
// The absence of the immediate/parent client in the map of the active clients
|
|
248
|
+
// means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet
|
|
249
|
+
// and is not ready to handle requests.
|
|
250
|
+
if (!activeClientIds.has(client.id)) {
|
|
251
|
+
return passthrough();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Notify the client that a request has been intercepted.
|
|
255
|
+
const serializedRequest = await serializeRequest(event.request);
|
|
256
|
+
const clientMessage = await sendToClient(
|
|
257
|
+
client,
|
|
258
|
+
{
|
|
259
|
+
type: "REQUEST",
|
|
260
|
+
payload: {
|
|
261
|
+
id: requestId,
|
|
262
|
+
interceptedAt: requestInterceptedAt,
|
|
263
|
+
...serializedRequest,
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
[serializedRequest.body]
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
switch (clientMessage.type) {
|
|
270
|
+
case "MOCK_RESPONSE": {
|
|
271
|
+
return respondWithMock(clientMessage.data);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
case "PASSTHROUGH": {
|
|
275
|
+
return passthrough();
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return passthrough();
|
|
292
280
|
}
|
|
293
281
|
|
|
294
282
|
/**
|
|
@@ -298,22 +286,19 @@ async function getResponse(event, client, requestId, requestInterceptedAt) {
|
|
|
298
286
|
* @returns {Promise<any>}
|
|
299
287
|
*/
|
|
300
288
|
function sendToClient(client, message, transferrables = []) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
...transferrables.filter(Boolean),
|
|
315
|
-
])
|
|
316
|
-
})
|
|
289
|
+
return new Promise((resolve, reject) => {
|
|
290
|
+
const channel = new MessageChannel();
|
|
291
|
+
|
|
292
|
+
channel.port1.onmessage = (event) => {
|
|
293
|
+
if (event.data && event.data.error) {
|
|
294
|
+
return reject(event.data.error);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
resolve(event.data);
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
client.postMessage(message, [channel.port2, ...transferrables.filter(Boolean)]);
|
|
301
|
+
});
|
|
317
302
|
}
|
|
318
303
|
|
|
319
304
|
/**
|
|
@@ -321,41 +306,41 @@ function sendToClient(client, message, transferrables = []) {
|
|
|
321
306
|
* @returns {Response}
|
|
322
307
|
*/
|
|
323
308
|
function respondWithMock(response) {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
309
|
+
// Setting response status code to 0 is a no-op.
|
|
310
|
+
// However, when responding with a "Response.error()", the produced Response
|
|
311
|
+
// instance will have status code set to 0. Since it's not possible to create
|
|
312
|
+
// a Response instance with status code 0, handle that use-case separately.
|
|
313
|
+
if (response.status === 0) {
|
|
314
|
+
return Response.error();
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const mockedResponse = new Response(response.body, response);
|
|
318
|
+
|
|
319
|
+
Reflect.defineProperty(mockedResponse, IS_MOCKED_RESPONSE, {
|
|
320
|
+
value: true,
|
|
321
|
+
enumerable: true,
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
return mockedResponse;
|
|
340
325
|
}
|
|
341
326
|
|
|
342
327
|
/**
|
|
343
328
|
* @param {Request} request
|
|
344
329
|
*/
|
|
345
330
|
async function serializeRequest(request) {
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
331
|
+
return {
|
|
332
|
+
url: request.url,
|
|
333
|
+
mode: request.mode,
|
|
334
|
+
method: request.method,
|
|
335
|
+
headers: Object.fromEntries(request.headers.entries()),
|
|
336
|
+
cache: request.cache,
|
|
337
|
+
credentials: request.credentials,
|
|
338
|
+
destination: request.destination,
|
|
339
|
+
integrity: request.integrity,
|
|
340
|
+
redirect: request.redirect,
|
|
341
|
+
referrer: request.referrer,
|
|
342
|
+
referrerPolicy: request.referrerPolicy,
|
|
343
|
+
body: await request.arrayBuffer(),
|
|
344
|
+
keepalive: request.keepalive,
|
|
345
|
+
};
|
|
361
346
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theholocron/react-template",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "A modern React template with pre-configured tools, best practices, and CI/CD setup for rapid project development.",
|
|
5
5
|
"homepage": "https://docs.theholocron.dev/react-template/",
|
|
6
6
|
"bugs": "https://github.com/theholocron/react-template/issues",
|