msw 2.11.2 → 2.11.4
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/lib/browser/index.js +129 -114
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +129 -114
- package/lib/browser/index.mjs.map +1 -1
- package/lib/core/{HttpResponse-BbwAqLE_.d.ts → HttpResponse-BpT5A_2L.d.ts} +1 -0
- package/lib/core/{HttpResponse-B4YmE-GJ.d.mts → HttpResponse-DGUjNQHG.d.mts} +1 -0
- package/lib/core/HttpResponse.d.mts +1 -1
- package/lib/core/HttpResponse.d.ts +1 -1
- package/lib/core/HttpResponse.js.map +1 -1
- package/lib/core/HttpResponse.mjs.map +1 -1
- package/lib/core/SetupApi.d.mts +1 -1
- package/lib/core/SetupApi.d.ts +1 -1
- package/lib/core/getResponse.d.mts +1 -1
- package/lib/core/getResponse.d.ts +1 -1
- package/lib/core/graphql.d.mts +1 -1
- package/lib/core/graphql.d.ts +1 -1
- package/lib/core/handlers/GraphQLHandler.d.mts +1 -1
- package/lib/core/handlers/GraphQLHandler.d.ts +1 -1
- package/lib/core/handlers/HttpHandler.d.mts +1 -1
- package/lib/core/handlers/HttpHandler.d.ts +1 -1
- package/lib/core/handlers/HttpHandler.js +0 -5
- package/lib/core/handlers/HttpHandler.js.map +1 -1
- package/lib/core/handlers/HttpHandler.mjs +1 -6
- package/lib/core/handlers/HttpHandler.mjs.map +1 -1
- package/lib/core/handlers/RequestHandler.d.mts +1 -1
- package/lib/core/handlers/RequestHandler.d.ts +1 -1
- package/lib/core/http.d.mts +1 -1
- package/lib/core/http.d.ts +1 -1
- package/lib/core/index.d.mts +1 -1
- package/lib/core/index.d.ts +1 -1
- package/lib/core/passthrough.d.mts +1 -1
- package/lib/core/passthrough.d.ts +1 -1
- package/lib/core/utils/HttpResponse/decorators.d.mts +1 -1
- package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
- package/lib/core/utils/HttpResponse/decorators.js +1 -1
- package/lib/core/utils/HttpResponse/decorators.js.map +1 -1
- package/lib/core/utils/HttpResponse/decorators.mjs +1 -1
- package/lib/core/utils/HttpResponse/decorators.mjs.map +1 -1
- package/lib/core/utils/executeHandlers.d.mts +1 -1
- package/lib/core/utils/executeHandlers.d.ts +1 -1
- package/lib/core/utils/handleRequest.d.mts +1 -1
- package/lib/core/utils/handleRequest.d.ts +1 -1
- package/lib/core/utils/handleRequest.js +8 -8
- package/lib/core/utils/handleRequest.js.map +1 -1
- package/lib/core/utils/handleRequest.mjs +8 -8
- package/lib/core/utils/handleRequest.mjs.map +1 -1
- package/lib/core/utils/internal/isHandlerKind.d.mts +1 -1
- package/lib/core/utils/internal/isHandlerKind.d.ts +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.d.mts +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.d.ts +1 -1
- package/lib/core/utils/internal/parseMultipartData.d.mts +1 -1
- package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
- package/lib/core/utils/internal/requestHandlerUtils.d.mts +1 -1
- package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
- package/lib/core/utils/logging/serializeResponse.js +1 -1
- package/lib/core/utils/logging/serializeResponse.js.map +1 -1
- package/lib/core/utils/logging/serializeResponse.mjs +1 -1
- package/lib/core/utils/logging/serializeResponse.mjs.map +1 -1
- package/lib/core/utils/request/getRequestCookies.js +3 -13
- package/lib/core/utils/request/getRequestCookies.js.map +1 -1
- package/lib/core/utils/request/getRequestCookies.mjs +6 -3
- package/lib/core/utils/request/getRequestCookies.mjs.map +1 -1
- package/lib/core/utils/url/cleanUrl.d.mts +1 -2
- package/lib/core/utils/url/cleanUrl.d.ts +1 -2
- package/lib/core/utils/url/cleanUrl.js +1 -5
- package/lib/core/utils/url/cleanUrl.js.map +1 -1
- package/lib/core/utils/url/cleanUrl.mjs +1 -5
- package/lib/core/utils/url/cleanUrl.mjs.map +1 -1
- package/lib/core/ws/WebSocketIndexedDBClientStore.d.mts +1 -2
- package/lib/core/ws/WebSocketIndexedDBClientStore.d.ts +1 -2
- package/lib/core/ws/WebSocketIndexedDBClientStore.js.map +1 -1
- package/lib/core/ws/WebSocketIndexedDBClientStore.mjs.map +1 -1
- package/lib/core/ws/WebSocketMemoryClientStore.d.mts +1 -2
- package/lib/core/ws/WebSocketMemoryClientStore.d.ts +1 -2
- package/lib/core/ws/WebSocketMemoryClientStore.js.map +1 -1
- package/lib/core/ws/WebSocketMemoryClientStore.mjs.map +1 -1
- package/lib/core/ws/handleWebSocketEvent.d.mts +2 -3
- package/lib/core/ws/handleWebSocketEvent.d.ts +2 -3
- package/lib/core/ws/handleWebSocketEvent.js.map +1 -1
- package/lib/core/ws/handleWebSocketEvent.mjs.map +1 -1
- package/lib/core/ws/utils/getMessageLength.d.mts +1 -1
- package/lib/core/ws/utils/getMessageLength.d.ts +1 -1
- package/lib/core/ws/utils/getMessageLength.js.map +1 -1
- package/lib/core/ws/utils/getMessageLength.mjs.map +1 -1
- package/lib/iife/index.js +595 -636
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +2 -1
- package/lib/shims/cookie.d.mts +7 -0
- package/lib/shims/cookie.d.ts +7 -0
- package/lib/shims/cookie.js +204 -0
- package/lib/shims/cookie.mjs +196 -0
- package/lib/shims/statuses.d.mts +6 -0
- package/lib/shims/statuses.d.ts +6 -0
- package/lib/shims/statuses.js +195 -0
- package/lib/shims/statuses.mjs +187 -0
- package/package.json +7 -8
- package/src/browser/setupWorker/glossary.ts +1 -5
- package/src/browser/setupWorker/setupWorker.ts +4 -9
- package/src/browser/setupWorker/start/createRequestListener.ts +4 -1
- package/src/browser/setupWorker/start/utils/getWorkerInstance.ts +17 -16
- package/src/browser/utils/{supportsReadableStreamTransfer.ts → supports.ts} +13 -0
- package/src/browser/utils/workerChannel.ts +13 -0
- package/src/core/HttpResponse.ts +1 -0
- package/src/core/handlers/HttpHandler.ts +1 -8
- package/src/core/utils/HttpResponse/decorators.ts +1 -1
- package/src/core/utils/handleRequest.ts +8 -8
- package/src/core/utils/logging/serializeResponse.ts +1 -1
- package/src/core/utils/request/getRequestCookies.ts +6 -3
- package/src/core/utils/url/cleanUrl.ts +0 -4
- package/src/core/ws/WebSocketIndexedDBClientStore.ts +1 -1
- package/src/core/ws/WebSocketMemoryClientStore.ts +1 -1
- package/src/core/ws/handleWebSocketEvent.ts +1 -1
- package/src/core/ws/utils/getMessageLength.ts +1 -1
- package/src/mockServiceWorker.js +1 -0
- package/src/shims/cookie.ts +7 -0
- package/src/shims/statuses.ts +6 -0
- package/src/tsconfig.src.json +1 -6
- package/src/browser/utils/deferNetworkRequestsUntil.test.ts +0 -48
- package/src/browser/utils/deferNetworkRequestsUntil.ts +0 -29
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
8
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
19
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
20
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
21
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
22
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
23
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
24
|
+
mod
|
|
25
|
+
));
|
|
26
|
+
|
|
27
|
+
// node_modules/.pnpm/statuses@2.0.2/node_modules/statuses/codes.json
|
|
28
|
+
var require_codes = __commonJS({
|
|
29
|
+
"node_modules/.pnpm/statuses@2.0.2/node_modules/statuses/codes.json"(exports, module) {
|
|
30
|
+
module.exports = {
|
|
31
|
+
"100": "Continue",
|
|
32
|
+
"101": "Switching Protocols",
|
|
33
|
+
"102": "Processing",
|
|
34
|
+
"103": "Early Hints",
|
|
35
|
+
"200": "OK",
|
|
36
|
+
"201": "Created",
|
|
37
|
+
"202": "Accepted",
|
|
38
|
+
"203": "Non-Authoritative Information",
|
|
39
|
+
"204": "No Content",
|
|
40
|
+
"205": "Reset Content",
|
|
41
|
+
"206": "Partial Content",
|
|
42
|
+
"207": "Multi-Status",
|
|
43
|
+
"208": "Already Reported",
|
|
44
|
+
"226": "IM Used",
|
|
45
|
+
"300": "Multiple Choices",
|
|
46
|
+
"301": "Moved Permanently",
|
|
47
|
+
"302": "Found",
|
|
48
|
+
"303": "See Other",
|
|
49
|
+
"304": "Not Modified",
|
|
50
|
+
"305": "Use Proxy",
|
|
51
|
+
"307": "Temporary Redirect",
|
|
52
|
+
"308": "Permanent Redirect",
|
|
53
|
+
"400": "Bad Request",
|
|
54
|
+
"401": "Unauthorized",
|
|
55
|
+
"402": "Payment Required",
|
|
56
|
+
"403": "Forbidden",
|
|
57
|
+
"404": "Not Found",
|
|
58
|
+
"405": "Method Not Allowed",
|
|
59
|
+
"406": "Not Acceptable",
|
|
60
|
+
"407": "Proxy Authentication Required",
|
|
61
|
+
"408": "Request Timeout",
|
|
62
|
+
"409": "Conflict",
|
|
63
|
+
"410": "Gone",
|
|
64
|
+
"411": "Length Required",
|
|
65
|
+
"412": "Precondition Failed",
|
|
66
|
+
"413": "Payload Too Large",
|
|
67
|
+
"414": "URI Too Long",
|
|
68
|
+
"415": "Unsupported Media Type",
|
|
69
|
+
"416": "Range Not Satisfiable",
|
|
70
|
+
"417": "Expectation Failed",
|
|
71
|
+
"418": "I'm a Teapot",
|
|
72
|
+
"421": "Misdirected Request",
|
|
73
|
+
"422": "Unprocessable Entity",
|
|
74
|
+
"423": "Locked",
|
|
75
|
+
"424": "Failed Dependency",
|
|
76
|
+
"425": "Too Early",
|
|
77
|
+
"426": "Upgrade Required",
|
|
78
|
+
"428": "Precondition Required",
|
|
79
|
+
"429": "Too Many Requests",
|
|
80
|
+
"431": "Request Header Fields Too Large",
|
|
81
|
+
"451": "Unavailable For Legal Reasons",
|
|
82
|
+
"500": "Internal Server Error",
|
|
83
|
+
"501": "Not Implemented",
|
|
84
|
+
"502": "Bad Gateway",
|
|
85
|
+
"503": "Service Unavailable",
|
|
86
|
+
"504": "Gateway Timeout",
|
|
87
|
+
"505": "HTTP Version Not Supported",
|
|
88
|
+
"506": "Variant Also Negotiates",
|
|
89
|
+
"507": "Insufficient Storage",
|
|
90
|
+
"508": "Loop Detected",
|
|
91
|
+
"509": "Bandwidth Limit Exceeded",
|
|
92
|
+
"510": "Not Extended",
|
|
93
|
+
"511": "Network Authentication Required"
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// node_modules/.pnpm/statuses@2.0.2/node_modules/statuses/index.js
|
|
99
|
+
var require_statuses = __commonJS({
|
|
100
|
+
"node_modules/.pnpm/statuses@2.0.2/node_modules/statuses/index.js"(exports, module) {
|
|
101
|
+
"use strict";
|
|
102
|
+
var codes = require_codes();
|
|
103
|
+
module.exports = status;
|
|
104
|
+
status.message = codes;
|
|
105
|
+
status.code = createMessageToStatusCodeMap(codes);
|
|
106
|
+
status.codes = createStatusCodeList(codes);
|
|
107
|
+
status.redirect = {
|
|
108
|
+
300: true,
|
|
109
|
+
301: true,
|
|
110
|
+
302: true,
|
|
111
|
+
303: true,
|
|
112
|
+
305: true,
|
|
113
|
+
307: true,
|
|
114
|
+
308: true
|
|
115
|
+
};
|
|
116
|
+
status.empty = {
|
|
117
|
+
204: true,
|
|
118
|
+
205: true,
|
|
119
|
+
304: true
|
|
120
|
+
};
|
|
121
|
+
status.retry = {
|
|
122
|
+
502: true,
|
|
123
|
+
503: true,
|
|
124
|
+
504: true
|
|
125
|
+
};
|
|
126
|
+
function createMessageToStatusCodeMap(codes2) {
|
|
127
|
+
var map = {};
|
|
128
|
+
Object.keys(codes2).forEach(function forEachCode(code) {
|
|
129
|
+
var message2 = codes2[code];
|
|
130
|
+
var status2 = Number(code);
|
|
131
|
+
map[message2.toLowerCase()] = status2;
|
|
132
|
+
});
|
|
133
|
+
return map;
|
|
134
|
+
}
|
|
135
|
+
function createStatusCodeList(codes2) {
|
|
136
|
+
return Object.keys(codes2).map(function mapCode(code) {
|
|
137
|
+
return Number(code);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
function getStatusCode(message2) {
|
|
141
|
+
var msg = message2.toLowerCase();
|
|
142
|
+
if (!Object.prototype.hasOwnProperty.call(status.code, msg)) {
|
|
143
|
+
throw new Error('invalid status message: "' + message2 + '"');
|
|
144
|
+
}
|
|
145
|
+
return status.code[msg];
|
|
146
|
+
}
|
|
147
|
+
function getStatusMessage(code) {
|
|
148
|
+
if (!Object.prototype.hasOwnProperty.call(status.message, code)) {
|
|
149
|
+
throw new Error("invalid status code: " + code);
|
|
150
|
+
}
|
|
151
|
+
return status.message[code];
|
|
152
|
+
}
|
|
153
|
+
function status(code) {
|
|
154
|
+
if (typeof code === "number") {
|
|
155
|
+
return getStatusMessage(code);
|
|
156
|
+
}
|
|
157
|
+
if (typeof code !== "string") {
|
|
158
|
+
throw new TypeError("code must be a number or string");
|
|
159
|
+
}
|
|
160
|
+
var n = parseInt(code, 10);
|
|
161
|
+
if (!isNaN(n)) {
|
|
162
|
+
return getStatusMessage(n);
|
|
163
|
+
}
|
|
164
|
+
return getStatusCode(code);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// src/shims/statuses.ts
|
|
170
|
+
var allStatuses = __toESM(require_statuses(), 1);
|
|
171
|
+
var statuses = allStatuses.default || allStatuses;
|
|
172
|
+
var message = statuses.message;
|
|
173
|
+
var statuses_default = statuses;
|
|
174
|
+
export {
|
|
175
|
+
statuses_default as default,
|
|
176
|
+
message
|
|
177
|
+
};
|
|
178
|
+
/*! Bundled license information:
|
|
179
|
+
|
|
180
|
+
statuses/index.js:
|
|
181
|
+
(*!
|
|
182
|
+
* statuses
|
|
183
|
+
* Copyright(c) 2014 Jonathan Ong
|
|
184
|
+
* Copyright(c) 2016 Douglas Christopher Wilson
|
|
185
|
+
* MIT Licensed
|
|
186
|
+
*)
|
|
187
|
+
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "msw",
|
|
3
|
-
"version": "2.11.
|
|
3
|
+
"version": "2.11.4",
|
|
4
4
|
"description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./lib/core/index.js",
|
|
@@ -214,14 +214,11 @@
|
|
|
214
214
|
],
|
|
215
215
|
"sideEffects": false,
|
|
216
216
|
"dependencies": {
|
|
217
|
-
"@bundled-es-modules/cookie": "^2.0.1",
|
|
218
|
-
"@bundled-es-modules/statuses": "^1.0.1",
|
|
219
217
|
"@inquirer/confirm": "^5.0.0",
|
|
220
218
|
"@mswjs/interceptors": "^0.39.1",
|
|
221
219
|
"@open-draft/deferred-promise": "^2.2.0",
|
|
222
|
-
"@open-draft/until": "^2.1.0",
|
|
223
|
-
"@types/cookie": "^0.6.0",
|
|
224
220
|
"@types/statuses": "^2.0.4",
|
|
221
|
+
"cookie": "^1.0.2",
|
|
225
222
|
"graphql": "^16.8.1",
|
|
226
223
|
"headers-polyfill": "^4.0.2",
|
|
227
224
|
"is-node-process": "^1.2.0",
|
|
@@ -229,9 +226,11 @@
|
|
|
229
226
|
"path-to-regexp": "^6.3.0",
|
|
230
227
|
"picocolors": "^1.1.1",
|
|
231
228
|
"rettime": "^0.7.0",
|
|
229
|
+
"statuses": "^2.0.2",
|
|
232
230
|
"strict-event-emitter": "^0.5.1",
|
|
233
231
|
"tough-cookie": "^6.0.0",
|
|
234
232
|
"type-fest": "^4.26.1",
|
|
233
|
+
"until-async": "^3.0.2",
|
|
235
234
|
"yargs": "^17.7.2"
|
|
236
235
|
},
|
|
237
236
|
"devDependencies": {
|
|
@@ -242,7 +241,7 @@
|
|
|
242
241
|
"@fastify/websocket": "^8.3.1",
|
|
243
242
|
"@graphql-typed-document-node/core": "^3.2.0",
|
|
244
243
|
"@open-draft/test-server": "^0.4.2",
|
|
245
|
-
"@ossjs/release": "^0.
|
|
244
|
+
"@ossjs/release": "^0.10.0",
|
|
246
245
|
"@playwright/test": "^1.48.0",
|
|
247
246
|
"@swc/core": "^1.13.5",
|
|
248
247
|
"@types/eslint__js": "^8.42.3",
|
|
@@ -283,11 +282,11 @@
|
|
|
283
282
|
"typescript-eslint": "^8.42.0",
|
|
284
283
|
"undici": "^6.20.0",
|
|
285
284
|
"url-loader": "^4.1.1",
|
|
286
|
-
"vitest": "^3.
|
|
285
|
+
"vitest": "^3.2.4",
|
|
287
286
|
"vitest-environment-miniflare": "^2.14.4",
|
|
288
287
|
"webpack": "^5.95.0",
|
|
289
288
|
"webpack-http-server": "^0.5.0",
|
|
290
|
-
"msw": "2.11.
|
|
289
|
+
"msw": "2.11.4"
|
|
291
290
|
},
|
|
292
291
|
"peerDependencies": {
|
|
293
292
|
"typescript": ">= 4.8.x"
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Emitter } from 'strict-event-emitter'
|
|
2
|
+
import type { HttpRequestEventMap, Interceptor } from '@mswjs/interceptors'
|
|
2
3
|
import type { DeferredPromise } from '@open-draft/deferred-promise'
|
|
3
4
|
import {
|
|
4
5
|
LifeCycleEventEmitter,
|
|
@@ -6,7 +7,6 @@ import {
|
|
|
6
7
|
SharedOptions,
|
|
7
8
|
} from '~/core/sharedOptions'
|
|
8
9
|
import { RequestHandler } from '~/core/handlers/RequestHandler'
|
|
9
|
-
import type { HttpRequestEventMap, Interceptor } from '@mswjs/interceptors'
|
|
10
10
|
import type { RequiredDeep } from '~/core/typeUtils'
|
|
11
11
|
import type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'
|
|
12
12
|
import type { WorkerChannel } from '../utils/workerChannel'
|
|
@@ -25,10 +25,6 @@ export type SetupWorkerInternalContext = {
|
|
|
25
25
|
emitter: Emitter<LifeCycleEventsMap>
|
|
26
26
|
keepAliveInterval?: number
|
|
27
27
|
workerChannel: WorkerChannel
|
|
28
|
-
supports: {
|
|
29
|
-
serviceWorkerApi: boolean
|
|
30
|
-
readableStreamTransfer: boolean
|
|
31
|
-
}
|
|
32
28
|
fallbackInterceptor?: Interceptor<HttpRequestEventMap>
|
|
33
29
|
}
|
|
34
30
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { invariant } from 'outvariant'
|
|
2
2
|
import { isNodeProcess } from 'is-node-process'
|
|
3
|
+
import { DeferredPromise } from '@open-draft/deferred-promise'
|
|
3
4
|
import type {
|
|
4
5
|
SetupWorkerInternalContext,
|
|
5
6
|
StartReturnType,
|
|
@@ -14,15 +15,14 @@ import { SetupApi } from '~/core/SetupApi'
|
|
|
14
15
|
import { mergeRight } from '~/core/utils/internal/mergeRight'
|
|
15
16
|
import type { LifeCycleEventsMap } from '~/core/sharedOptions'
|
|
16
17
|
import type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'
|
|
17
|
-
import { supportsReadableStreamTransfer } from '../utils/supportsReadableStreamTransfer'
|
|
18
18
|
import { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'
|
|
19
19
|
import { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'
|
|
20
20
|
import { attachWebSocketLogger } from '~/core/ws/utils/attachWebSocketLogger'
|
|
21
21
|
import { WorkerChannel } from '../utils/workerChannel'
|
|
22
|
-
import { DeferredPromise } from '@open-draft/deferred-promise'
|
|
23
22
|
import { createFallbackRequestListener } from './start/createFallbackRequestListener'
|
|
24
23
|
import { printStartMessage } from './start/utils/printStartMessage'
|
|
25
24
|
import { printStopMessage } from './stop/utils/printStopMessage'
|
|
25
|
+
import { supportsServiceWorker } from '../utils/supports'
|
|
26
26
|
|
|
27
27
|
export class SetupWorkerApi
|
|
28
28
|
extends SetupApi<LifeCycleEventsMap>
|
|
@@ -60,11 +60,6 @@ export class SetupWorkerApi
|
|
|
60
60
|
workerChannel: new WorkerChannel({
|
|
61
61
|
worker: workerPromise,
|
|
62
62
|
}),
|
|
63
|
-
supports: {
|
|
64
|
-
serviceWorkerApi:
|
|
65
|
-
'serviceWorker' in navigator && location.protocol !== 'file:',
|
|
66
|
-
readableStreamTransfer: supportsReadableStreamTransfer(),
|
|
67
|
-
},
|
|
68
63
|
}
|
|
69
64
|
}
|
|
70
65
|
|
|
@@ -117,7 +112,7 @@ export class SetupWorkerApi
|
|
|
117
112
|
|
|
118
113
|
// Use a fallback interception algorithm in the environments
|
|
119
114
|
// where the Service Worker API isn't supported.
|
|
120
|
-
if (!
|
|
115
|
+
if (!supportsServiceWorker()) {
|
|
121
116
|
const fallbackInterceptor = createFallbackRequestListener(
|
|
122
117
|
this.context,
|
|
123
118
|
this.context.startOptions,
|
|
@@ -159,7 +154,7 @@ export class SetupWorkerApi
|
|
|
159
154
|
this.context.workerStoppedAt = Date.now()
|
|
160
155
|
this.context.emitter.removeAllListeners()
|
|
161
156
|
|
|
162
|
-
if (
|
|
157
|
+
if (supportsServiceWorker()) {
|
|
163
158
|
this.context.workerChannel.removeAllListeners('RESPONSE')
|
|
164
159
|
window.clearInterval(this.context.keepAliveInterval)
|
|
165
160
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Emitter } from 'rettime'
|
|
2
2
|
import { StartOptions, SetupWorkerInternalContext } from '../glossary'
|
|
3
3
|
import { deserializeRequest } from '../../utils/deserializeRequest'
|
|
4
|
+
import { supportsReadableStreamTransfer } from '../../utils/supports'
|
|
4
5
|
import { RequestHandler } from '~/core/handlers/RequestHandler'
|
|
5
6
|
import { handleRequest } from '~/core/utils/handleRequest'
|
|
6
7
|
import { RequiredDeep } from '~/core/typeUtils'
|
|
@@ -8,6 +9,8 @@ import { devUtils } from '~/core/utils/internal/devUtils'
|
|
|
8
9
|
import { toResponseInit } from '~/core/utils/toResponseInit'
|
|
9
10
|
import { isHandlerKind } from '~/core/utils/internal/isHandlerKind'
|
|
10
11
|
|
|
12
|
+
const SUPPORTS_READABLE_STREAM_TRANSFER = supportsReadableStreamTransfer()
|
|
13
|
+
|
|
11
14
|
export const createRequestListener = (
|
|
12
15
|
context: SetupWorkerInternalContext,
|
|
13
16
|
options: RequiredDeep<StartOptions>,
|
|
@@ -58,7 +61,7 @@ export const createRequestListener = (
|
|
|
58
61
|
* @note Safari doesn't support transferring a "ReadableStream".
|
|
59
62
|
* Check that the browser supports that before sending it to the worker.
|
|
60
63
|
*/
|
|
61
|
-
if (
|
|
64
|
+
if (SUPPORTS_READABLE_STREAM_TRANSFER) {
|
|
62
65
|
const responseStreamOrNull = response.body
|
|
63
66
|
|
|
64
67
|
event.postMessage(
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { until } from '
|
|
1
|
+
import { until } from 'until-async'
|
|
2
2
|
import { devUtils } from '~/core/utils/internal/devUtils'
|
|
3
3
|
import { getAbsoluteWorkerUrl } from '../../../utils/getAbsoluteWorkerUrl'
|
|
4
4
|
import { getWorkerByRegistration } from './getWorkerByRegistration'
|
|
@@ -52,21 +52,22 @@ export const getWorkerInstance = async (
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
// When the Service Worker wasn't found, register it anew and return the reference.
|
|
55
|
-
const registrationResult = await until<
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
55
|
+
const [registrationError, registrationResult] = await until<
|
|
56
|
+
Error,
|
|
57
|
+
ServiceWorkerInstanceTuple
|
|
58
|
+
>(async () => {
|
|
59
|
+
const registration = await navigator.serviceWorker.register(url, options)
|
|
60
|
+
return [
|
|
61
|
+
// Compare existing worker registration by its worker URL,
|
|
62
|
+
// to prevent irrelevant workers to resolve here (such as Codesandbox worker).
|
|
63
|
+
getWorkerByRegistration(registration, absoluteWorkerUrl, findWorker),
|
|
64
|
+
registration,
|
|
65
|
+
]
|
|
66
|
+
})
|
|
66
67
|
|
|
67
68
|
// Handle Service Worker registration errors.
|
|
68
|
-
if (
|
|
69
|
-
const isWorkerMissing =
|
|
69
|
+
if (registrationError) {
|
|
70
|
+
const isWorkerMissing = registrationError.message.includes('(404)')
|
|
70
71
|
|
|
71
72
|
// Produce a custom error message when given a non-existing Service Worker url.
|
|
72
73
|
// Suggest developers to check their setup.
|
|
@@ -87,10 +88,10 @@ Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/i
|
|
|
87
88
|
throw new Error(
|
|
88
89
|
devUtils.formatMessage(
|
|
89
90
|
'Failed to register the Service Worker:\n\n%s',
|
|
90
|
-
|
|
91
|
+
registrationError.message,
|
|
91
92
|
),
|
|
92
93
|
)
|
|
93
94
|
}
|
|
94
95
|
|
|
95
|
-
return registrationResult
|
|
96
|
+
return registrationResult
|
|
96
97
|
}
|
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the Service Worker API is supproted and available
|
|
3
|
+
* in the current browsing context.
|
|
4
|
+
*/
|
|
5
|
+
export function supportsServiceWorker(): boolean {
|
|
6
|
+
return (
|
|
7
|
+
typeof navigator !== 'undefined' &&
|
|
8
|
+
'serviceWorker' in navigator &&
|
|
9
|
+
typeof location !== 'undefined' &&
|
|
10
|
+
location.protocol !== 'file:'
|
|
11
|
+
)
|
|
12
|
+
}
|
|
13
|
+
|
|
1
14
|
/**
|
|
2
15
|
* Returns a boolean indicating whether the current browser
|
|
3
16
|
* supports `ReadableStream` as a `Transferable` when posting
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { invariant } from 'outvariant'
|
|
1
2
|
import { Emitter, TypedEvent } from 'rettime'
|
|
2
3
|
import { isObject } from '~/core/utils/internal/isObject'
|
|
3
4
|
import type { StringifiedResponse } from '../setupWorker/glossary'
|
|
5
|
+
import { supportsServiceWorker } from '../utils/supports'
|
|
4
6
|
|
|
5
7
|
export interface WorkerChannelOptions {
|
|
6
8
|
worker: Promise<ServiceWorker>
|
|
@@ -65,6 +67,8 @@ export type WorkerEventResponse = {
|
|
|
65
67
|
PASSTHROUGH: []
|
|
66
68
|
}
|
|
67
69
|
|
|
70
|
+
const SUPPORTS_SERVICE_WORKER = supportsServiceWorker()
|
|
71
|
+
|
|
68
72
|
export class WorkerEvent<
|
|
69
73
|
DataType,
|
|
70
74
|
ReturnType = any,
|
|
@@ -121,6 +125,10 @@ export class WorkerChannel extends Emitter<WorkerChannelEventMap> {
|
|
|
121
125
|
constructor(protected readonly options: WorkerChannelOptions) {
|
|
122
126
|
super()
|
|
123
127
|
|
|
128
|
+
if (!SUPPORTS_SERVICE_WORKER) {
|
|
129
|
+
return
|
|
130
|
+
}
|
|
131
|
+
|
|
124
132
|
navigator.serviceWorker.addEventListener('message', async (event) => {
|
|
125
133
|
const worker = await this.options.worker
|
|
126
134
|
|
|
@@ -139,6 +147,11 @@ export class WorkerChannel extends Emitter<WorkerChannelEventMap> {
|
|
|
139
147
|
* This triggers the `message` event listener on ServiceWorkerGlobalScope.
|
|
140
148
|
*/
|
|
141
149
|
public postMessage(type: OutgoingWorkerEvents): void {
|
|
150
|
+
invariant(
|
|
151
|
+
SUPPORTS_SERVICE_WORKER,
|
|
152
|
+
'Failed to post message on a WorkerChannel: the Service Worker API is unavailable in this context. This is likely an issue with MSW. Please report it on GitHub: https://github.com/mswjs/msw/issues',
|
|
153
|
+
)
|
|
154
|
+
|
|
142
155
|
this.options.worker.then((worker) => {
|
|
143
156
|
worker.postMessage(type)
|
|
144
157
|
})
|
package/src/core/HttpResponse.ts
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
} from '../utils/matching/matchRequestUrl'
|
|
14
14
|
import { toPublicUrl } from '../utils/request/toPublicUrl'
|
|
15
15
|
import { getAllRequestCookies } from '../utils/request/getRequestCookies'
|
|
16
|
-
import { cleanUrl
|
|
16
|
+
import { cleanUrl } from '../utils/url/cleanUrl'
|
|
17
17
|
import {
|
|
18
18
|
RequestHandler,
|
|
19
19
|
RequestHandlerDefaultInfo,
|
|
@@ -115,13 +115,6 @@ export class HttpHandler extends RequestHandler<
|
|
|
115
115
|
return
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
const searchParams = getSearchParams(path)
|
|
119
|
-
const queryParams: Array<string> = []
|
|
120
|
-
|
|
121
|
-
searchParams.forEach((_, paramName) => {
|
|
122
|
-
queryParams.push(paramName)
|
|
123
|
-
})
|
|
124
|
-
|
|
125
118
|
devUtils.warn(
|
|
126
119
|
`Found a redundant usage of query parameters in the request handler URL for "${method} ${path}". Please match against a path instead and access query parameters using "new URL(request.url).searchParams" instead. Learn more: https://mswjs.io/docs/http/intercepting-requests#querysearch-parameters`,
|
|
127
120
|
)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { until } from '
|
|
1
|
+
import { until } from 'until-async'
|
|
2
2
|
import { Emitter } from 'strict-event-emitter'
|
|
3
3
|
import { LifeCycleEventsMap, SharedOptions } from '../sharedOptions'
|
|
4
4
|
import { RequiredDeep } from '../typeUtils'
|
|
@@ -54,7 +54,7 @@ export async function handleRequest(
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
// Resolve a mocked response from the list of request handlers.
|
|
57
|
-
const lookupResult = await until(() => {
|
|
57
|
+
const [lookupError, lookupResult] = await until(() => {
|
|
58
58
|
return executeHandlers({
|
|
59
59
|
request,
|
|
60
60
|
requestId,
|
|
@@ -63,19 +63,19 @@ export async function handleRequest(
|
|
|
63
63
|
})
|
|
64
64
|
})
|
|
65
65
|
|
|
66
|
-
if (
|
|
66
|
+
if (lookupError) {
|
|
67
67
|
// Allow developers to react to unhandled exceptions in request handlers.
|
|
68
68
|
emitter.emit('unhandledException', {
|
|
69
|
-
error:
|
|
69
|
+
error: lookupError,
|
|
70
70
|
request,
|
|
71
71
|
requestId,
|
|
72
72
|
})
|
|
73
|
-
throw
|
|
73
|
+
throw lookupError
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
// If the handler lookup returned nothing, no request handler was found
|
|
77
77
|
// matching this request. Report the request as unhandled.
|
|
78
|
-
if (!lookupResult
|
|
78
|
+
if (!lookupResult) {
|
|
79
79
|
await onUnhandledRequest(request, options.onUnhandledRequest)
|
|
80
80
|
emitter.emit('request:unhandled', { request, requestId })
|
|
81
81
|
emitter.emit('request:end', { request, requestId })
|
|
@@ -83,7 +83,7 @@ export async function handleRequest(
|
|
|
83
83
|
return
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
const { response } = lookupResult
|
|
86
|
+
const { response } = lookupResult
|
|
87
87
|
|
|
88
88
|
// When the handled request returned no mocked response, warn the developer,
|
|
89
89
|
// as it may be an oversight on their part. Perform the request as-is.
|
|
@@ -110,7 +110,7 @@ export async function handleRequest(
|
|
|
110
110
|
emitter.emit('request:match', { request, requestId })
|
|
111
111
|
|
|
112
112
|
const requiredLookupResult =
|
|
113
|
-
lookupResult
|
|
113
|
+
lookupResult as RequiredDeep<HandlersExecutionResult>
|
|
114
114
|
|
|
115
115
|
handleRequestOptions?.onMockedResponse?.(response, requiredLookupResult)
|
|
116
116
|
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
parse as parseCookie,
|
|
3
|
+
serialize as serializeCookie,
|
|
4
|
+
} from '../../../shims/cookie'
|
|
2
5
|
import { cookieStore } from '../cookieStore'
|
|
3
6
|
|
|
4
7
|
function parseCookies(input: string): Record<string, string> {
|
|
5
|
-
const parsedCookies =
|
|
8
|
+
const parsedCookies = parseCookie(input)
|
|
6
9
|
const cookies: Record<string, string> = {}
|
|
7
10
|
|
|
8
11
|
for (const cookieName in parsedCookies) {
|
|
@@ -62,7 +65,7 @@ export function getAllRequestCookies(request: Request): Record<string, string> {
|
|
|
62
65
|
for (const name in cookiesFromDocument) {
|
|
63
66
|
request.headers.append(
|
|
64
67
|
'cookie',
|
|
65
|
-
|
|
68
|
+
serializeCookie(name, cookiesFromDocument[name]),
|
|
66
69
|
)
|
|
67
70
|
}
|
|
68
71
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DeferredPromise } from '@open-draft/deferred-promise'
|
|
2
|
-
import { WebSocketClientConnectionProtocol } from '@mswjs/interceptors/
|
|
2
|
+
import { WebSocketClientConnectionProtocol } from '@mswjs/interceptors/WebSocket'
|
|
3
3
|
import {
|
|
4
4
|
type SerializedWebSocketClient,
|
|
5
5
|
WebSocketClientStore,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { WebSocketConnectionData } from '@mswjs/interceptors/
|
|
1
|
+
import type { WebSocketConnectionData } from '@mswjs/interceptors/WebSocket'
|
|
2
2
|
import { RequestHandler } from '../handlers/RequestHandler'
|
|
3
3
|
import { WebSocketHandler } from '../handlers/WebSocketHandler'
|
|
4
4
|
import { webSocketInterceptor } from './webSocketInterceptor'
|
package/src/mockServiceWorker.js
CHANGED
|
@@ -205,6 +205,7 @@ async function resolveMainClient(event) {
|
|
|
205
205
|
* @param {FetchEvent} event
|
|
206
206
|
* @param {Client | undefined} client
|
|
207
207
|
* @param {string} requestId
|
|
208
|
+
* @param {number} requestInterceptedAt
|
|
208
209
|
* @returns {Promise<Response>}
|
|
209
210
|
*/
|
|
210
211
|
async function getResponse(event, client, requestId, requestInterceptedAt) {
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import * as allCookie from 'cookie'
|
|
2
|
+
const cookie = (allCookie as any).default || allCookie
|
|
3
|
+
|
|
4
|
+
export const parse = cookie.parse as typeof import('cookie').parse
|
|
5
|
+
export const serialize = cookie.serialize as typeof import('cookie').serialize
|
|
6
|
+
|
|
7
|
+
export default cookie
|