msw 2.11.3 → 2.11.5
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 +96 -90
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +96 -90
- 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 +2 -2
- package/lib/core/index.d.ts +2 -2
- package/lib/core/index.js +2 -0
- package/lib/core/index.js.map +1 -1
- package/lib/core/index.mjs +4 -0
- package/lib/core/index.mjs.map +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/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 +556 -605
- 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 +5 -6
- 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/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/index.ts +5 -0
- package/src/core/utils/HttpResponse/decorators.ts +1 -1
- 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.5",
|
|
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,13 +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
|
-
"@types/cookie": "^0.6.0",
|
|
223
220
|
"@types/statuses": "^2.0.4",
|
|
221
|
+
"cookie": "^1.0.2",
|
|
224
222
|
"graphql": "^16.8.1",
|
|
225
223
|
"headers-polyfill": "^4.0.2",
|
|
226
224
|
"is-node-process": "^1.2.0",
|
|
@@ -228,6 +226,7 @@
|
|
|
228
226
|
"path-to-regexp": "^6.3.0",
|
|
229
227
|
"picocolors": "^1.1.1",
|
|
230
228
|
"rettime": "^0.7.0",
|
|
229
|
+
"statuses": "^2.0.2",
|
|
231
230
|
"strict-event-emitter": "^0.5.1",
|
|
232
231
|
"tough-cookie": "^6.0.0",
|
|
233
232
|
"type-fest": "^4.26.1",
|
|
@@ -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",
|
|
@@ -287,7 +286,7 @@
|
|
|
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.5"
|
|
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,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
|
)
|
package/src/core/index.ts
CHANGED
|
@@ -20,6 +20,11 @@ export {
|
|
|
20
20
|
/* Utils */
|
|
21
21
|
export { matchRequestUrl } from './utils/matching/matchRequestUrl'
|
|
22
22
|
export * from './utils/handleRequest'
|
|
23
|
+
export {
|
|
24
|
+
onUnhandledRequest,
|
|
25
|
+
type UnhandledRequestStrategy,
|
|
26
|
+
type UnhandledRequestCallback,
|
|
27
|
+
} from './utils/request/onUnhandledRequest'
|
|
23
28
|
export { getResponse } from './getResponse'
|
|
24
29
|
export { cleanUrl } from './utils/url/cleanUrl'
|
|
25
30
|
|
|
@@ -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
|
package/src/tsconfig.src.json
CHANGED
|
@@ -3,12 +3,7 @@
|
|
|
3
3
|
// living in the "src" directory.
|
|
4
4
|
"extends": "../tsconfig.base.json",
|
|
5
5
|
"compilerOptions": {
|
|
6
|
-
"composite": true
|
|
7
|
-
"baseUrl": "./",
|
|
8
|
-
"paths": {
|
|
9
|
-
"~/core": ["core"],
|
|
10
|
-
"~/core/*": ["core/*"]
|
|
11
|
-
}
|
|
6
|
+
"composite": true
|
|
12
7
|
},
|
|
13
8
|
"include": ["../global.d.ts", "./**/*.ts"],
|
|
14
9
|
"exclude": ["./**/*.test.ts"]
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @vitest-environment jsdom
|
|
3
|
-
*/
|
|
4
|
-
import { deferNetworkRequestsUntil } from './deferNetworkRequestsUntil'
|
|
5
|
-
|
|
6
|
-
beforeAll(() => {
|
|
7
|
-
// Stubs native `fetch` function to remove any external
|
|
8
|
-
// asynchronicity from this test suite.
|
|
9
|
-
window.fetch = vi.fn().mockImplementation(() => {
|
|
10
|
-
return Promise.resolve({
|
|
11
|
-
json: () => ({
|
|
12
|
-
response: 'body',
|
|
13
|
-
}),
|
|
14
|
-
})
|
|
15
|
-
})
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
afterAll(() => {
|
|
19
|
-
vi.restoreAllMocks()
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
test('defers any requests that happen while a given promise is pending', async () => {
|
|
23
|
-
const events: string[] = []
|
|
24
|
-
|
|
25
|
-
// Emulate a Service Worker registration Promise.
|
|
26
|
-
const workerPromise = new Promise<null>((resolve) => {
|
|
27
|
-
setTimeout(resolve, 1000)
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
workerPromise.then(() => {
|
|
31
|
-
events.push('promise resolved')
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
// Calling this functions intercepts all requests that happen while
|
|
35
|
-
// the given promise is pending, and defers their execution
|
|
36
|
-
// until the promise is resolved.
|
|
37
|
-
deferNetworkRequestsUntil(workerPromise)
|
|
38
|
-
|
|
39
|
-
// Perform a request.
|
|
40
|
-
const requestPromise = fetch('/user').then(() => {
|
|
41
|
-
events.push('response received')
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
await Promise.all([workerPromise, requestPromise])
|
|
45
|
-
|
|
46
|
-
// Assert the order of resolved events.
|
|
47
|
-
expect(events).toEqual(['promise resolved', 'response received'])
|
|
48
|
-
})
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { until } from 'until-async'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Intercepts and defers any requests on the page
|
|
5
|
-
* until the Service Worker instance is ready.
|
|
6
|
-
* Must only be used in a browser.
|
|
7
|
-
*/
|
|
8
|
-
export function deferNetworkRequestsUntil(predicatePromise: Promise<any>) {
|
|
9
|
-
// Defer any `XMLHttpRequest` requests until the Service Worker is ready.
|
|
10
|
-
const originalXhrSend = window.XMLHttpRequest.prototype.send
|
|
11
|
-
window.XMLHttpRequest.prototype.send = function (
|
|
12
|
-
...args: Parameters<XMLHttpRequest['send']>
|
|
13
|
-
) {
|
|
14
|
-
// Keep this function synchronous to comply with `XMLHttpRequest.prototype.send`,
|
|
15
|
-
// because that method is always synchronous.
|
|
16
|
-
until(() => predicatePromise).then(() => {
|
|
17
|
-
window.XMLHttpRequest.prototype.send = originalXhrSend
|
|
18
|
-
this.send(...args)
|
|
19
|
-
})
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// Defer any `fetch` requests until the Service Worker is ready.
|
|
23
|
-
const originalFetch = window.fetch
|
|
24
|
-
window.fetch = async (...args) => {
|
|
25
|
-
await until(() => predicatePromise)
|
|
26
|
-
window.fetch = originalFetch
|
|
27
|
-
return window.fetch(...args)
|
|
28
|
-
}
|
|
29
|
-
}
|