msw 2.11.0 → 2.11.2
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.d.mts +0 -3
- package/lib/browser/index.d.ts +0 -3
- package/lib/browser/index.js +503 -294
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +503 -294
- package/lib/browser/index.mjs.map +1 -1
- package/lib/core/{HttpResponse-DiuKTgC7.d.mts → HttpResponse-B4YmE-GJ.d.mts} +1 -1
- package/lib/core/{HttpResponse-DlQEvD4q.d.ts → HttpResponse-BbwAqLE_.d.ts} +1 -1
- 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/GraphQLHandler.js.map +1 -1
- package/lib/core/handlers/GraphQLHandler.mjs.map +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.map +1 -1
- 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/cookieStore.d.mts +10 -2
- package/lib/core/utils/cookieStore.d.ts +10 -2
- package/lib/core/utils/cookieStore.js +49 -146
- package/lib/core/utils/cookieStore.js.map +1 -1
- package/lib/core/utils/cookieStore.mjs +53 -136
- package/lib/core/utils/cookieStore.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 +1 -1
- package/lib/core/utils/handleRequest.js.map +1 -1
- package/lib/core/utils/handleRequest.mjs +1 -1
- package/lib/core/utils/handleRequest.mjs.map +1 -1
- package/lib/core/utils/internal/devUtils.js.map +1 -1
- package/lib/core/utils/internal/devUtils.mjs.map +1 -1
- package/lib/core/utils/internal/getCallFrame.js +2 -2
- package/lib/core/utils/internal/getCallFrame.js.map +1 -1
- package/lib/core/utils/internal/getCallFrame.mjs +2 -2
- package/lib/core/utils/internal/getCallFrame.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/parseGraphQLRequest.js +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.js.map +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.mjs +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.mjs.map +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/matching/matchRequestUrl.js +1 -1
- package/lib/core/utils/matching/matchRequestUrl.js.map +1 -1
- package/lib/core/utils/matching/matchRequestUrl.mjs +1 -1
- package/lib/core/utils/matching/matchRequestUrl.mjs.map +1 -1
- package/lib/core/utils/request/getRequestCookies.js +1 -1
- package/lib/core/utils/request/getRequestCookies.js.map +1 -1
- package/lib/core/utils/request/getRequestCookies.mjs +1 -1
- package/lib/core/utils/request/getRequestCookies.mjs.map +1 -1
- package/lib/core/utils/request/storeResponseCookies.d.mts +1 -1
- package/lib/core/utils/request/storeResponseCookies.d.ts +1 -1
- package/lib/core/utils/request/storeResponseCookies.js +2 -2
- package/lib/core/utils/request/storeResponseCookies.js.map +1 -1
- package/lib/core/utils/request/storeResponseCookies.mjs +2 -2
- package/lib/core/utils/request/storeResponseCookies.mjs.map +1 -1
- package/lib/core/utils/url/cleanUrl.js +1 -1
- package/lib/core/utils/url/cleanUrl.js.map +1 -1
- package/lib/core/utils/url/cleanUrl.mjs +1 -1
- package/lib/core/utils/url/cleanUrl.mjs.map +1 -1
- package/lib/core/utils/url/isAbsoluteUrl.js +1 -1
- package/lib/core/utils/url/isAbsoluteUrl.js.map +1 -1
- package/lib/core/utils/url/isAbsoluteUrl.mjs +1 -1
- package/lib/core/utils/url/isAbsoluteUrl.mjs.map +1 -1
- package/lib/core/ws/WebSocketIndexedDBClientStore.js.map +1 -1
- package/lib/core/ws/WebSocketIndexedDBClientStore.mjs.map +1 -1
- package/lib/core/ws/handleWebSocketEvent.d.mts +1 -1
- package/lib/core/ws/handleWebSocketEvent.d.ts +1 -1
- package/lib/core/ws/utils/attachWebSocketLogger.js.map +1 -1
- package/lib/core/ws/utils/attachWebSocketLogger.mjs.map +1 -1
- package/lib/iife/index.js +6885 -16153
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +16 -12
- package/lib/node/index.js.map +1 -1
- package/lib/node/index.mjs.map +1 -1
- package/package.json +22 -15
- package/src/browser/setupWorker/glossary.ts +9 -114
- package/src/browser/setupWorker/setupWorker.ts +83 -120
- package/src/browser/setupWorker/start/createRequestListener.ts +20 -24
- package/src/browser/setupWorker/start/createResponseListener.ts +15 -22
- package/src/browser/setupWorker/start/createStartHandler.ts +24 -18
- package/src/browser/setupWorker/start/utils/enableMocking.ts +18 -21
- package/src/browser/setupWorker/start/utils/printStartMessage.ts +0 -2
- package/src/browser/utils/checkWorkerIntegrity.ts +22 -14
- package/src/browser/utils/workerChannel.ts +146 -0
- package/src/core/HttpResponse.ts +3 -3
- package/src/core/handlers/GraphQLHandler.test.ts +1 -1
- package/src/core/handlers/GraphQLHandler.ts +0 -3
- package/src/core/handlers/HttpHandler.ts +0 -2
- package/src/core/utils/cookieStore.ts +56 -198
- package/src/core/utils/handleRequest.ts +1 -1
- package/src/core/utils/internal/devUtils.ts +0 -2
- package/src/core/utils/internal/getCallFrame.ts +2 -2
- package/src/core/utils/internal/parseGraphQLRequest.ts +1 -0
- package/src/core/utils/matching/matchRequestUrl.ts +2 -2
- package/src/core/utils/request/getRequestCookies.ts +1 -1
- package/src/core/utils/request/onUnhandledRequest.test.ts +1 -1
- package/src/core/utils/request/storeResponseCookies.ts +3 -3
- package/src/core/utils/request/toPublicUrl.test.ts +1 -1
- package/src/core/utils/url/cleanUrl.ts +1 -1
- package/src/core/utils/url/isAbsoluteUrl.ts +1 -1
- package/src/core/ws/WebSocketClientManager.test.ts +1 -1
- package/src/core/ws/WebSocketIndexedDBClientStore.ts +0 -4
- package/src/core/ws/utils/attachWebSocketLogger.ts +0 -14
- package/src/mockServiceWorker.js +14 -10
- package/src/node/SetupServerApi.ts +1 -1
- package/src/browser/setupWorker/start/createFallbackStart.ts +0 -21
- package/src/browser/setupWorker/start/utils/createMessageChannel.ts +0 -32
- package/src/browser/setupWorker/stop/createFallbackStop.ts +0 -11
- package/src/browser/setupWorker/stop/createStop.ts +0 -35
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,171 +15,76 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
var cookieStore_exports = {};
|
|
30
20
|
__export(cookieStore_exports, {
|
|
31
21
|
cookieStore: () => cookieStore
|
|
32
22
|
});
|
|
33
23
|
module.exports = __toCommonJS(cookieStore_exports);
|
|
34
|
-
var import_outvariant = require("outvariant");
|
|
35
24
|
var import_is_node_process = require("is-node-process");
|
|
36
|
-
var
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
storageKey;
|
|
25
|
+
var import_outvariant = require("outvariant");
|
|
26
|
+
var import_tough_cookie = require("tough-cookie");
|
|
27
|
+
var import_jsonParse = require("./internal/jsonParse");
|
|
28
|
+
class CookieStore {
|
|
29
|
+
#storageKey = "__msw-cookie-store__";
|
|
30
|
+
#jar;
|
|
31
|
+
#memoryStore;
|
|
41
32
|
constructor() {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
this.synchronous = true;
|
|
48
|
-
this.storage = localStorage;
|
|
49
|
-
this.storageKey = "__msw-cookie-store__";
|
|
50
|
-
}
|
|
51
|
-
findCookie(domain, path, key, callback) {
|
|
52
|
-
try {
|
|
53
|
-
const store2 = this.getStore();
|
|
54
|
-
const cookies = this.filterCookiesFromList(store2, { domain, path, key });
|
|
55
|
-
callback(null, cookies[0] || null);
|
|
56
|
-
} catch (error) {
|
|
57
|
-
if (error instanceof Error) {
|
|
58
|
-
callback(error, null);
|
|
59
|
-
}
|
|
33
|
+
if (!(0, import_is_node_process.isNodeProcess)()) {
|
|
34
|
+
(0, import_outvariant.invariant)(
|
|
35
|
+
typeof localStorage !== "undefined",
|
|
36
|
+
"Failed to create a CookieStore: `localStorage` is not available in this environment. This is likely an issue with your environment, which has been detected as browser (or browser-like) environment and must implement global browser APIs correctly."
|
|
37
|
+
);
|
|
60
38
|
}
|
|
39
|
+
this.#memoryStore = new import_tough_cookie.MemoryCookieStore();
|
|
40
|
+
this.#memoryStore.idx = this.getCookieStoreIndex();
|
|
41
|
+
this.#jar = new import_tough_cookie.CookieJar(this.#memoryStore);
|
|
61
42
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
callback(null, []);
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
try {
|
|
68
|
-
const store2 = this.getStore();
|
|
69
|
-
const results = this.filterCookiesFromList(store2, {
|
|
70
|
-
domain,
|
|
71
|
-
path
|
|
72
|
-
});
|
|
73
|
-
callback(null, results);
|
|
74
|
-
} catch (error) {
|
|
75
|
-
if (error instanceof Error) {
|
|
76
|
-
callback(error, []);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
43
|
+
getCookies(url) {
|
|
44
|
+
return this.#jar.getCookiesSync(url);
|
|
79
45
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
const store2 = this.getStore();
|
|
86
|
-
store2.push(cookie);
|
|
87
|
-
this.updateStore(store2);
|
|
88
|
-
} catch (error) {
|
|
89
|
-
if (error instanceof Error) {
|
|
90
|
-
callback(error);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
46
|
+
async setCookie(cookieName, url) {
|
|
47
|
+
await this.#jar.setCookie(cookieName, url);
|
|
48
|
+
this.persist();
|
|
93
49
|
}
|
|
94
|
-
|
|
95
|
-
if (
|
|
96
|
-
|
|
97
|
-
newCookie.domain || "",
|
|
98
|
-
newCookie.path || "",
|
|
99
|
-
newCookie.key,
|
|
100
|
-
callback
|
|
101
|
-
);
|
|
102
|
-
return;
|
|
50
|
+
getCookieStoreIndex() {
|
|
51
|
+
if (typeof localStorage === "undefined") {
|
|
52
|
+
return {};
|
|
103
53
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
try {
|
|
108
|
-
const store2 = this.getStore();
|
|
109
|
-
const nextStore = this.deleteCookiesFromList(store2, { domain, path, key });
|
|
110
|
-
this.updateStore(nextStore);
|
|
111
|
-
callback(null);
|
|
112
|
-
} catch (error) {
|
|
113
|
-
if (error instanceof Error) {
|
|
114
|
-
callback(error);
|
|
115
|
-
}
|
|
54
|
+
const cookiesString = localStorage.getItem(this.#storageKey);
|
|
55
|
+
if (cookiesString == null) {
|
|
56
|
+
return {};
|
|
116
57
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const store2 = this.getStore();
|
|
121
|
-
const nextStore = this.deleteCookiesFromList(store2, { domain, path });
|
|
122
|
-
this.updateStore(nextStore);
|
|
123
|
-
callback(null);
|
|
124
|
-
} catch (error) {
|
|
125
|
-
if (error instanceof Error) {
|
|
126
|
-
callback(error);
|
|
127
|
-
}
|
|
58
|
+
const rawCookies = (0, import_jsonParse.jsonParse)(cookiesString);
|
|
59
|
+
if (rawCookies == null) {
|
|
60
|
+
return {};
|
|
128
61
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
62
|
+
const cookies = {};
|
|
63
|
+
for (const rawCookie of rawCookies) {
|
|
64
|
+
const cookie = import_tough_cookie.Cookie.fromJSON(rawCookie);
|
|
65
|
+
if (cookie != null && cookie.domain != null && cookie.path != null) {
|
|
66
|
+
cookies[cookie.domain] ||= {};
|
|
67
|
+
cookies[cookie.domain][cookie.path] ||= {};
|
|
68
|
+
cookies[cookie.domain][cookie.path][cookie.key] = cookie;
|
|
136
69
|
}
|
|
137
70
|
}
|
|
71
|
+
return cookies;
|
|
138
72
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
if (json == null) {
|
|
143
|
-
return [];
|
|
144
|
-
}
|
|
145
|
-
const rawCookies = JSON.parse(json);
|
|
146
|
-
const cookies = [];
|
|
147
|
-
for (const rawCookie of rawCookies) {
|
|
148
|
-
const cookie = Cookie.fromJSON(rawCookie);
|
|
149
|
-
if (cookie != null) {
|
|
150
|
-
cookies.push(cookie);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
return cookies;
|
|
154
|
-
} catch {
|
|
155
|
-
return [];
|
|
73
|
+
persist() {
|
|
74
|
+
if (typeof localStorage === "undefined") {
|
|
75
|
+
return;
|
|
156
76
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
filterCookiesFromList(cookies, matches) {
|
|
165
|
-
const result = [];
|
|
166
|
-
for (const cookie of cookies) {
|
|
167
|
-
if (matches.domain && !domainMatch(matches.domain, cookie.domain || "")) {
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
if (matches.path && !pathMatch(matches.path, cookie.path || "")) {
|
|
171
|
-
continue;
|
|
172
|
-
}
|
|
173
|
-
if (matches.key && cookie.key !== matches.key) {
|
|
174
|
-
continue;
|
|
77
|
+
const data = [];
|
|
78
|
+
const { idx } = this.#memoryStore;
|
|
79
|
+
for (const domain in idx) {
|
|
80
|
+
for (const path in idx[domain]) {
|
|
81
|
+
for (const key in idx[domain][path]) {
|
|
82
|
+
data.push(idx[domain][path][key].toJSON());
|
|
83
|
+
}
|
|
175
84
|
}
|
|
176
|
-
result.push(cookie);
|
|
177
85
|
}
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
deleteCookiesFromList(cookies, matches) {
|
|
181
|
-
const matchingCookies = this.filterCookiesFromList(cookies, matches);
|
|
182
|
-
return cookies.filter((cookie) => !matchingCookies.includes(cookie));
|
|
86
|
+
localStorage.setItem(this.#storageKey, JSON.stringify(data));
|
|
183
87
|
}
|
|
184
88
|
}
|
|
185
|
-
const
|
|
186
|
-
const cookieStore = new CookieJar(store);
|
|
89
|
+
const cookieStore = new CookieStore();
|
|
187
90
|
//# sourceMappingURL=cookieStore.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/core/utils/cookieStore.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../../src/core/utils/cookieStore.ts"],"sourcesContent":["import { isNodeProcess } from 'is-node-process'\nimport { invariant } from 'outvariant'\nimport {\n Cookie,\n CookieJar,\n MemoryCookieStore,\n type MemoryCookieStoreIndex,\n} from 'tough-cookie'\nimport { jsonParse } from './internal/jsonParse'\n\nclass CookieStore {\n #storageKey = '__msw-cookie-store__'\n #jar: CookieJar\n #memoryStore: MemoryCookieStore\n\n constructor() {\n if (!isNodeProcess()) {\n invariant(\n typeof localStorage !== 'undefined',\n 'Failed to create a CookieStore: `localStorage` is not available in this environment. This is likely an issue with your environment, which has been detected as browser (or browser-like) environment and must implement global browser APIs correctly.',\n )\n }\n\n this.#memoryStore = new MemoryCookieStore()\n this.#memoryStore.idx = this.getCookieStoreIndex()\n this.#jar = new CookieJar(this.#memoryStore)\n }\n\n public getCookies(url: string): Array<Cookie> {\n return this.#jar.getCookiesSync(url)\n }\n\n public async setCookie(cookieName: string, url: string): Promise<void> {\n await this.#jar.setCookie(cookieName, url)\n this.persist()\n }\n\n private getCookieStoreIndex(): MemoryCookieStoreIndex {\n if (typeof localStorage === 'undefined') {\n return {}\n }\n\n const cookiesString = localStorage.getItem(this.#storageKey)\n if (cookiesString == null) {\n return {}\n }\n\n const rawCookies = jsonParse<Array<Record<string, unknown>>>(cookiesString)\n if (rawCookies == null) {\n return {}\n }\n\n const cookies: MemoryCookieStoreIndex = {}\n\n for (const rawCookie of rawCookies) {\n const cookie = Cookie.fromJSON(rawCookie)\n\n if (cookie != null && cookie.domain != null && cookie.path != null) {\n cookies[cookie.domain] ||= {}\n cookies[cookie.domain][cookie.path] ||= {}\n cookies[cookie.domain][cookie.path][cookie.key] = cookie\n }\n }\n\n return cookies\n }\n\n private persist(): void {\n if (typeof localStorage === 'undefined') {\n return\n }\n\n const data = []\n const { idx } = this.#memoryStore\n\n for (const domain in idx) {\n for (const path in idx[domain]) {\n for (const key in idx[domain][path]) {\n data.push(idx[domain][path][key].toJSON())\n }\n }\n }\n\n localStorage.setItem(this.#storageKey, JSON.stringify(data))\n }\n}\n\nexport const cookieStore = new CookieStore()\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,6BAA8B;AAC9B,wBAA0B;AAC1B,0BAKO;AACP,uBAA0B;AAE1B,MAAM,YAAY;AAAA,EAChB,cAAc;AAAA,EACd;AAAA,EACA;AAAA,EAEA,cAAc;AACZ,QAAI,KAAC,sCAAc,GAAG;AACpB;AAAA,QACE,OAAO,iBAAiB;AAAA,QACxB;AAAA,MACF;AAAA,IACF;AAEA,SAAK,eAAe,IAAI,sCAAkB;AAC1C,SAAK,aAAa,MAAM,KAAK,oBAAoB;AACjD,SAAK,OAAO,IAAI,8BAAU,KAAK,YAAY;AAAA,EAC7C;AAAA,EAEO,WAAW,KAA4B;AAC5C,WAAO,KAAK,KAAK,eAAe,GAAG;AAAA,EACrC;AAAA,EAEA,MAAa,UAAU,YAAoB,KAA4B;AACrE,UAAM,KAAK,KAAK,UAAU,YAAY,GAAG;AACzC,SAAK,QAAQ;AAAA,EACf;AAAA,EAEQ,sBAA8C;AACpD,QAAI,OAAO,iBAAiB,aAAa;AACvC,aAAO,CAAC;AAAA,IACV;AAEA,UAAM,gBAAgB,aAAa,QAAQ,KAAK,WAAW;AAC3D,QAAI,iBAAiB,MAAM;AACzB,aAAO,CAAC;AAAA,IACV;AAEA,UAAM,iBAAa,4BAA0C,aAAa;AAC1E,QAAI,cAAc,MAAM;AACtB,aAAO,CAAC;AAAA,IACV;AAEA,UAAM,UAAkC,CAAC;AAEzC,eAAW,aAAa,YAAY;AAClC,YAAM,SAAS,2BAAO,SAAS,SAAS;AAExC,UAAI,UAAU,QAAQ,OAAO,UAAU,QAAQ,OAAO,QAAQ,MAAM;AAClE,gBAAQ,OAAO,MAAM,MAAM,CAAC;AAC5B,gBAAQ,OAAO,MAAM,EAAE,OAAO,IAAI,MAAM,CAAC;AACzC,gBAAQ,OAAO,MAAM,EAAE,OAAO,IAAI,EAAE,OAAO,GAAG,IAAI;AAAA,MACpD;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEQ,UAAgB;AACtB,QAAI,OAAO,iBAAiB,aAAa;AACvC;AAAA,IACF;AAEA,UAAM,OAAO,CAAC;AACd,UAAM,EAAE,IAAI,IAAI,KAAK;AAErB,eAAW,UAAU,KAAK;AACxB,iBAAW,QAAQ,IAAI,MAAM,GAAG;AAC9B,mBAAW,OAAO,IAAI,MAAM,EAAE,IAAI,GAAG;AACnC,eAAK,KAAK,IAAI,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC;AAAA,QAC3C;AAAA,MACF;AAAA,IACF;AAEA,iBAAa,QAAQ,KAAK,aAAa,KAAK,UAAU,IAAI,CAAC;AAAA,EAC7D;AACF;AAEO,MAAM,cAAc,IAAI,YAAY;","names":[]}
|
|
@@ -1,156 +1,73 @@
|
|
|
1
|
-
import { invariant } from "outvariant";
|
|
2
1
|
import { isNodeProcess } from "is-node-process";
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
import { invariant } from "outvariant";
|
|
3
|
+
import {
|
|
4
|
+
Cookie,
|
|
5
|
+
CookieJar,
|
|
6
|
+
MemoryCookieStore
|
|
7
|
+
} from "tough-cookie";
|
|
8
|
+
import { jsonParse } from './internal/jsonParse.mjs';
|
|
9
|
+
class CookieStore {
|
|
10
|
+
#storageKey = "__msw-cookie-store__";
|
|
11
|
+
#jar;
|
|
12
|
+
#memoryStore;
|
|
8
13
|
constructor() {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
this.synchronous = true;
|
|
15
|
-
this.storage = localStorage;
|
|
16
|
-
this.storageKey = "__msw-cookie-store__";
|
|
17
|
-
}
|
|
18
|
-
findCookie(domain, path, key, callback) {
|
|
19
|
-
try {
|
|
20
|
-
const store2 = this.getStore();
|
|
21
|
-
const cookies = this.filterCookiesFromList(store2, { domain, path, key });
|
|
22
|
-
callback(null, cookies[0] || null);
|
|
23
|
-
} catch (error) {
|
|
24
|
-
if (error instanceof Error) {
|
|
25
|
-
callback(error, null);
|
|
26
|
-
}
|
|
14
|
+
if (!isNodeProcess()) {
|
|
15
|
+
invariant(
|
|
16
|
+
typeof localStorage !== "undefined",
|
|
17
|
+
"Failed to create a CookieStore: `localStorage` is not available in this environment. This is likely an issue with your environment, which has been detected as browser (or browser-like) environment and must implement global browser APIs correctly."
|
|
18
|
+
);
|
|
27
19
|
}
|
|
20
|
+
this.#memoryStore = new MemoryCookieStore();
|
|
21
|
+
this.#memoryStore.idx = this.getCookieStoreIndex();
|
|
22
|
+
this.#jar = new CookieJar(this.#memoryStore);
|
|
28
23
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
callback(null, []);
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
try {
|
|
35
|
-
const store2 = this.getStore();
|
|
36
|
-
const results = this.filterCookiesFromList(store2, {
|
|
37
|
-
domain,
|
|
38
|
-
path
|
|
39
|
-
});
|
|
40
|
-
callback(null, results);
|
|
41
|
-
} catch (error) {
|
|
42
|
-
if (error instanceof Error) {
|
|
43
|
-
callback(error, []);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
24
|
+
getCookies(url) {
|
|
25
|
+
return this.#jar.getCookiesSync(url);
|
|
46
26
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
const store2 = this.getStore();
|
|
53
|
-
store2.push(cookie);
|
|
54
|
-
this.updateStore(store2);
|
|
55
|
-
} catch (error) {
|
|
56
|
-
if (error instanceof Error) {
|
|
57
|
-
callback(error);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
27
|
+
async setCookie(cookieName, url) {
|
|
28
|
+
await this.#jar.setCookie(cookieName, url);
|
|
29
|
+
this.persist();
|
|
60
30
|
}
|
|
61
|
-
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
newCookie.domain || "",
|
|
65
|
-
newCookie.path || "",
|
|
66
|
-
newCookie.key,
|
|
67
|
-
callback
|
|
68
|
-
);
|
|
69
|
-
return;
|
|
31
|
+
getCookieStoreIndex() {
|
|
32
|
+
if (typeof localStorage === "undefined") {
|
|
33
|
+
return {};
|
|
70
34
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
try {
|
|
75
|
-
const store2 = this.getStore();
|
|
76
|
-
const nextStore = this.deleteCookiesFromList(store2, { domain, path, key });
|
|
77
|
-
this.updateStore(nextStore);
|
|
78
|
-
callback(null);
|
|
79
|
-
} catch (error) {
|
|
80
|
-
if (error instanceof Error) {
|
|
81
|
-
callback(error);
|
|
82
|
-
}
|
|
35
|
+
const cookiesString = localStorage.getItem(this.#storageKey);
|
|
36
|
+
if (cookiesString == null) {
|
|
37
|
+
return {};
|
|
83
38
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const store2 = this.getStore();
|
|
88
|
-
const nextStore = this.deleteCookiesFromList(store2, { domain, path });
|
|
89
|
-
this.updateStore(nextStore);
|
|
90
|
-
callback(null);
|
|
91
|
-
} catch (error) {
|
|
92
|
-
if (error instanceof Error) {
|
|
93
|
-
callback(error);
|
|
94
|
-
}
|
|
39
|
+
const rawCookies = jsonParse(cookiesString);
|
|
40
|
+
if (rawCookies == null) {
|
|
41
|
+
return {};
|
|
95
42
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
43
|
+
const cookies = {};
|
|
44
|
+
for (const rawCookie of rawCookies) {
|
|
45
|
+
const cookie = Cookie.fromJSON(rawCookie);
|
|
46
|
+
if (cookie != null && cookie.domain != null && cookie.path != null) {
|
|
47
|
+
cookies[cookie.domain] ||= {};
|
|
48
|
+
cookies[cookie.domain][cookie.path] ||= {};
|
|
49
|
+
cookies[cookie.domain][cookie.path][cookie.key] = cookie;
|
|
103
50
|
}
|
|
104
51
|
}
|
|
52
|
+
return cookies;
|
|
105
53
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if (json == null) {
|
|
110
|
-
return [];
|
|
111
|
-
}
|
|
112
|
-
const rawCookies = JSON.parse(json);
|
|
113
|
-
const cookies = [];
|
|
114
|
-
for (const rawCookie of rawCookies) {
|
|
115
|
-
const cookie = Cookie.fromJSON(rawCookie);
|
|
116
|
-
if (cookie != null) {
|
|
117
|
-
cookies.push(cookie);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return cookies;
|
|
121
|
-
} catch {
|
|
122
|
-
return [];
|
|
54
|
+
persist() {
|
|
55
|
+
if (typeof localStorage === "undefined") {
|
|
56
|
+
return;
|
|
123
57
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
filterCookiesFromList(cookies, matches) {
|
|
132
|
-
const result = [];
|
|
133
|
-
for (const cookie of cookies) {
|
|
134
|
-
if (matches.domain && !domainMatch(matches.domain, cookie.domain || "")) {
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
if (matches.path && !pathMatch(matches.path, cookie.path || "")) {
|
|
138
|
-
continue;
|
|
139
|
-
}
|
|
140
|
-
if (matches.key && cookie.key !== matches.key) {
|
|
141
|
-
continue;
|
|
58
|
+
const data = [];
|
|
59
|
+
const { idx } = this.#memoryStore;
|
|
60
|
+
for (const domain in idx) {
|
|
61
|
+
for (const path in idx[domain]) {
|
|
62
|
+
for (const key in idx[domain][path]) {
|
|
63
|
+
data.push(idx[domain][path][key].toJSON());
|
|
64
|
+
}
|
|
142
65
|
}
|
|
143
|
-
result.push(cookie);
|
|
144
66
|
}
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
deleteCookiesFromList(cookies, matches) {
|
|
148
|
-
const matchingCookies = this.filterCookiesFromList(cookies, matches);
|
|
149
|
-
return cookies.filter((cookie) => !matchingCookies.includes(cookie));
|
|
67
|
+
localStorage.setItem(this.#storageKey, JSON.stringify(data));
|
|
150
68
|
}
|
|
151
69
|
}
|
|
152
|
-
const
|
|
153
|
-
const cookieStore = new CookieJar(store);
|
|
70
|
+
const cookieStore = new CookieStore();
|
|
154
71
|
export {
|
|
155
72
|
cookieStore
|
|
156
73
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/core/utils/cookieStore.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../../src/core/utils/cookieStore.ts"],"sourcesContent":["import { isNodeProcess } from 'is-node-process'\nimport { invariant } from 'outvariant'\nimport {\n Cookie,\n CookieJar,\n MemoryCookieStore,\n type MemoryCookieStoreIndex,\n} from 'tough-cookie'\nimport { jsonParse } from './internal/jsonParse'\n\nclass CookieStore {\n #storageKey = '__msw-cookie-store__'\n #jar: CookieJar\n #memoryStore: MemoryCookieStore\n\n constructor() {\n if (!isNodeProcess()) {\n invariant(\n typeof localStorage !== 'undefined',\n 'Failed to create a CookieStore: `localStorage` is not available in this environment. This is likely an issue with your environment, which has been detected as browser (or browser-like) environment and must implement global browser APIs correctly.',\n )\n }\n\n this.#memoryStore = new MemoryCookieStore()\n this.#memoryStore.idx = this.getCookieStoreIndex()\n this.#jar = new CookieJar(this.#memoryStore)\n }\n\n public getCookies(url: string): Array<Cookie> {\n return this.#jar.getCookiesSync(url)\n }\n\n public async setCookie(cookieName: string, url: string): Promise<void> {\n await this.#jar.setCookie(cookieName, url)\n this.persist()\n }\n\n private getCookieStoreIndex(): MemoryCookieStoreIndex {\n if (typeof localStorage === 'undefined') {\n return {}\n }\n\n const cookiesString = localStorage.getItem(this.#storageKey)\n if (cookiesString == null) {\n return {}\n }\n\n const rawCookies = jsonParse<Array<Record<string, unknown>>>(cookiesString)\n if (rawCookies == null) {\n return {}\n }\n\n const cookies: MemoryCookieStoreIndex = {}\n\n for (const rawCookie of rawCookies) {\n const cookie = Cookie.fromJSON(rawCookie)\n\n if (cookie != null && cookie.domain != null && cookie.path != null) {\n cookies[cookie.domain] ||= {}\n cookies[cookie.domain][cookie.path] ||= {}\n cookies[cookie.domain][cookie.path][cookie.key] = cookie\n }\n }\n\n return cookies\n }\n\n private persist(): void {\n if (typeof localStorage === 'undefined') {\n return\n }\n\n const data = []\n const { idx } = this.#memoryStore\n\n for (const domain in idx) {\n for (const path in idx[domain]) {\n for (const key in idx[domain][path]) {\n data.push(idx[domain][path][key].toJSON())\n }\n }\n }\n\n localStorage.setItem(this.#storageKey, JSON.stringify(data))\n }\n}\n\nexport const cookieStore = new CookieStore()\n"],"mappings":"AAAA,SAAS,qBAAqB;AAC9B,SAAS,iBAAiB;AAC1B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP,SAAS,iBAAiB;AAE1B,MAAM,YAAY;AAAA,EAChB,cAAc;AAAA,EACd;AAAA,EACA;AAAA,EAEA,cAAc;AACZ,QAAI,CAAC,cAAc,GAAG;AACpB;AAAA,QACE,OAAO,iBAAiB;AAAA,QACxB;AAAA,MACF;AAAA,IACF;AAEA,SAAK,eAAe,IAAI,kBAAkB;AAC1C,SAAK,aAAa,MAAM,KAAK,oBAAoB;AACjD,SAAK,OAAO,IAAI,UAAU,KAAK,YAAY;AAAA,EAC7C;AAAA,EAEO,WAAW,KAA4B;AAC5C,WAAO,KAAK,KAAK,eAAe,GAAG;AAAA,EACrC;AAAA,EAEA,MAAa,UAAU,YAAoB,KAA4B;AACrE,UAAM,KAAK,KAAK,UAAU,YAAY,GAAG;AACzC,SAAK,QAAQ;AAAA,EACf;AAAA,EAEQ,sBAA8C;AACpD,QAAI,OAAO,iBAAiB,aAAa;AACvC,aAAO,CAAC;AAAA,IACV;AAEA,UAAM,gBAAgB,aAAa,QAAQ,KAAK,WAAW;AAC3D,QAAI,iBAAiB,MAAM;AACzB,aAAO,CAAC;AAAA,IACV;AAEA,UAAM,aAAa,UAA0C,aAAa;AAC1E,QAAI,cAAc,MAAM;AACtB,aAAO,CAAC;AAAA,IACV;AAEA,UAAM,UAAkC,CAAC;AAEzC,eAAW,aAAa,YAAY;AAClC,YAAM,SAAS,OAAO,SAAS,SAAS;AAExC,UAAI,UAAU,QAAQ,OAAO,UAAU,QAAQ,OAAO,QAAQ,MAAM;AAClE,gBAAQ,OAAO,MAAM,MAAM,CAAC;AAC5B,gBAAQ,OAAO,MAAM,EAAE,OAAO,IAAI,MAAM,CAAC;AACzC,gBAAQ,OAAO,MAAM,EAAE,OAAO,IAAI,EAAE,OAAO,GAAG,IAAI;AAAA,MACpD;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEQ,UAAgB;AACtB,QAAI,OAAO,iBAAiB,aAAa;AACvC;AAAA,IACF;AAEA,UAAM,OAAO,CAAC;AACd,UAAM,EAAE,IAAI,IAAI,KAAK;AAErB,eAAW,UAAU,KAAK;AACxB,iBAAW,QAAQ,IAAI,MAAM,GAAG;AAC9B,mBAAW,OAAO,IAAI,MAAM,EAAE,IAAI,GAAG;AACnC,eAAK,KAAK,IAAI,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC;AAAA,QAC3C;AAAA,MACF;AAAA,IACF;AAEA,iBAAa,QAAQ,KAAK,aAAa,KAAK,UAAU,IAAI,CAAC;AAAA,EAC7D;AACF;AAEO,MAAM,cAAc,IAAI,YAAY;","names":[]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { t as HandlersExecutionResult, p as ResponseResolutionContext, u as executeHandlers } from '../HttpResponse-
|
|
1
|
+
export { t as HandlersExecutionResult, p as ResponseResolutionContext, u as executeHandlers } from '../HttpResponse-B4YmE-GJ.mjs';
|
|
2
2
|
import '@mswjs/interceptors';
|
|
3
3
|
import './internal/isIterable.mjs';
|
|
4
4
|
import '../typeUtils.mjs';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { t as HandlersExecutionResult, p as ResponseResolutionContext, u as executeHandlers } from '../HttpResponse-
|
|
1
|
+
export { t as HandlersExecutionResult, p as ResponseResolutionContext, u as executeHandlers } from '../HttpResponse-BbwAqLE_.js';
|
|
2
2
|
import '@mswjs/interceptors';
|
|
3
3
|
import './internal/isIterable.js';
|
|
4
4
|
import '../typeUtils.js';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Emitter } from 'strict-event-emitter';
|
|
2
2
|
import { SharedOptions, LifeCycleEventsMap } from '../sharedOptions.mjs';
|
|
3
3
|
import { RequiredDeep } from '../typeUtils.mjs';
|
|
4
|
-
import { t as HandlersExecutionResult, R as RequestHandler } from '../HttpResponse-
|
|
4
|
+
import { t as HandlersExecutionResult, R as RequestHandler } from '../HttpResponse-B4YmE-GJ.mjs';
|
|
5
5
|
import './request/onUnhandledRequest.mjs';
|
|
6
6
|
import '@mswjs/interceptors';
|
|
7
7
|
import './internal/isIterable.mjs';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Emitter } from 'strict-event-emitter';
|
|
2
2
|
import { SharedOptions, LifeCycleEventsMap } from '../sharedOptions.js';
|
|
3
3
|
import { RequiredDeep } from '../typeUtils.js';
|
|
4
|
-
import { t as HandlersExecutionResult, R as RequestHandler } from '../HttpResponse-
|
|
4
|
+
import { t as HandlersExecutionResult, R as RequestHandler } from '../HttpResponse-BbwAqLE_.js';
|
|
5
5
|
import './request/onUnhandledRequest.js';
|
|
6
6
|
import '@mswjs/interceptors';
|
|
7
7
|
import './internal/isIterable.js';
|
|
@@ -66,7 +66,7 @@ async function handleRequest(request, requestId, handlers, options, emitter, han
|
|
|
66
66
|
handleRequestOptions?.onPassthroughResponse?.(request);
|
|
67
67
|
return;
|
|
68
68
|
}
|
|
69
|
-
(0, import_storeResponseCookies.storeResponseCookies)(request, response);
|
|
69
|
+
await (0, import_storeResponseCookies.storeResponseCookies)(request, response);
|
|
70
70
|
emitter.emit("request:match", { request, requestId });
|
|
71
71
|
const requiredLookupResult = lookupResult.data;
|
|
72
72
|
handleRequestOptions?.onMockedResponse?.(response, requiredLookupResult);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/core/utils/handleRequest.ts"],"sourcesContent":["import { until } from '@open-draft/until'\nimport { Emitter } from 'strict-event-emitter'\nimport { LifeCycleEventsMap, SharedOptions } from '../sharedOptions'\nimport { RequiredDeep } from '../typeUtils'\nimport type { RequestHandler } from '../handlers/RequestHandler'\nimport { HandlersExecutionResult, executeHandlers } from './executeHandlers'\nimport { onUnhandledRequest } from './request/onUnhandledRequest'\nimport { storeResponseCookies } from './request/storeResponseCookies'\n\nexport interface HandleRequestOptions {\n /**\n * `resolutionContext` is not part of the general public api\n * but is exposed to aid in creating extensions like\n * `@mswjs/http-middleware`.\n */\n resolutionContext?: {\n /**\n * A base url to use when resolving relative urls.\n * @note This is primarily used by the `@mswjs/http-middleware`\n * to resolve relative urls in the context of the running server\n */\n baseUrl?: string\n }\n\n /**\n * Invoked whenever a request is performed as-is.\n */\n onPassthroughResponse?(request: Request): void\n\n /**\n * Invoked when the mocked response is ready to be sent.\n */\n onMockedResponse?(\n response: Response,\n handler: RequiredDeep<HandlersExecutionResult>,\n ): void\n}\n\nexport async function handleRequest(\n request: Request,\n requestId: string,\n handlers: Array<RequestHandler>,\n options: RequiredDeep<SharedOptions>,\n emitter: Emitter<LifeCycleEventsMap>,\n handleRequestOptions?: HandleRequestOptions,\n): Promise<Response | undefined> {\n emitter.emit('request:start', { request, requestId })\n\n // Perform requests wrapped in \"bypass()\" as-is.\n if (request.headers.get('accept')?.includes('msw/passthrough')) {\n emitter.emit('request:end', { request, requestId })\n handleRequestOptions?.onPassthroughResponse?.(request)\n return\n }\n\n // Resolve a mocked response from the list of request handlers.\n const lookupResult = await until(() => {\n return executeHandlers({\n request,\n requestId,\n handlers,\n resolutionContext: handleRequestOptions?.resolutionContext,\n })\n })\n\n if (lookupResult.error) {\n // Allow developers to react to unhandled exceptions in request handlers.\n emitter.emit('unhandledException', {\n error: lookupResult.error,\n request,\n requestId,\n })\n throw lookupResult.error\n }\n\n // If the handler lookup returned nothing, no request handler was found\n // matching this request. Report the request as unhandled.\n if (!lookupResult.data) {\n await onUnhandledRequest(request, options.onUnhandledRequest)\n emitter.emit('request:unhandled', { request, requestId })\n emitter.emit('request:end', { request, requestId })\n handleRequestOptions?.onPassthroughResponse?.(request)\n return\n }\n\n const { response } = lookupResult.data\n\n // When the handled request returned no mocked response, warn the developer,\n // as it may be an oversight on their part. Perform the request as-is.\n if (!response) {\n emitter.emit('request:end', { request, requestId })\n handleRequestOptions?.onPassthroughResponse?.(request)\n return\n }\n\n // Perform the request as-is when the developer explicitly returned \"req.passthrough()\".\n // This produces no warning as the request was handled.\n if (\n response.status === 302 &&\n response.headers.get('x-msw-intention') === 'passthrough'\n ) {\n emitter.emit('request:end', { request, requestId })\n handleRequestOptions?.onPassthroughResponse?.(request)\n return\n }\n\n // Store all the received response cookies in the cookie jar.\n storeResponseCookies(request, response)\n\n emitter.emit('request:match', { request, requestId })\n\n const requiredLookupResult =\n lookupResult.data as RequiredDeep<HandlersExecutionResult>\n\n handleRequestOptions?.onMockedResponse?.(response, requiredLookupResult)\n\n emitter.emit('request:end', { request, requestId })\n\n return response\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAsB;AAKtB,6BAAyD;AACzD,gCAAmC;AACnC,kCAAqC;AA+BrC,eAAsB,cACpB,SACA,WACA,UACA,SACA,SACA,sBAC+B;AAC/B,UAAQ,KAAK,iBAAiB,EAAE,SAAS,UAAU,CAAC;AAGpD,MAAI,QAAQ,QAAQ,IAAI,QAAQ,GAAG,SAAS,iBAAiB,GAAG;AAC9D,YAAQ,KAAK,eAAe,EAAE,SAAS,UAAU,CAAC;AAClD,0BAAsB,wBAAwB,OAAO;AACrD;AAAA,EACF;AAGA,QAAM,eAAe,UAAM,oBAAM,MAAM;AACrC,eAAO,wCAAgB;AAAA,MACrB;AAAA,MACA;AAAA,MACA;AAAA,MACA,mBAAmB,sBAAsB;AAAA,IAC3C,CAAC;AAAA,EACH,CAAC;AAED,MAAI,aAAa,OAAO;AAEtB,YAAQ,KAAK,sBAAsB;AAAA,MACjC,OAAO,aAAa;AAAA,MACpB;AAAA,MACA;AAAA,IACF,CAAC;AACD,UAAM,aAAa;AAAA,EACrB;AAIA,MAAI,CAAC,aAAa,MAAM;AACtB,cAAM,8CAAmB,SAAS,QAAQ,kBAAkB;AAC5D,YAAQ,KAAK,qBAAqB,EAAE,SAAS,UAAU,CAAC;AACxD,YAAQ,KAAK,eAAe,EAAE,SAAS,UAAU,CAAC;AAClD,0BAAsB,wBAAwB,OAAO;AACrD;AAAA,EACF;AAEA,QAAM,EAAE,SAAS,IAAI,aAAa;AAIlC,MAAI,CAAC,UAAU;AACb,YAAQ,KAAK,eAAe,EAAE,SAAS,UAAU,CAAC;AAClD,0BAAsB,wBAAwB,OAAO;AACrD;AAAA,EACF;AAIA,MACE,SAAS,WAAW,OACpB,SAAS,QAAQ,IAAI,iBAAiB,MAAM,eAC5C;AACA,YAAQ,KAAK,eAAe,EAAE,SAAS,UAAU,CAAC;AAClD,0BAAsB,wBAAwB,OAAO;AACrD;AAAA,EACF;AAGA,
|
|
1
|
+
{"version":3,"sources":["../../../src/core/utils/handleRequest.ts"],"sourcesContent":["import { until } from '@open-draft/until'\nimport { Emitter } from 'strict-event-emitter'\nimport { LifeCycleEventsMap, SharedOptions } from '../sharedOptions'\nimport { RequiredDeep } from '../typeUtils'\nimport type { RequestHandler } from '../handlers/RequestHandler'\nimport { HandlersExecutionResult, executeHandlers } from './executeHandlers'\nimport { onUnhandledRequest } from './request/onUnhandledRequest'\nimport { storeResponseCookies } from './request/storeResponseCookies'\n\nexport interface HandleRequestOptions {\n /**\n * `resolutionContext` is not part of the general public api\n * but is exposed to aid in creating extensions like\n * `@mswjs/http-middleware`.\n */\n resolutionContext?: {\n /**\n * A base url to use when resolving relative urls.\n * @note This is primarily used by the `@mswjs/http-middleware`\n * to resolve relative urls in the context of the running server\n */\n baseUrl?: string\n }\n\n /**\n * Invoked whenever a request is performed as-is.\n */\n onPassthroughResponse?(request: Request): void\n\n /**\n * Invoked when the mocked response is ready to be sent.\n */\n onMockedResponse?(\n response: Response,\n handler: RequiredDeep<HandlersExecutionResult>,\n ): void\n}\n\nexport async function handleRequest(\n request: Request,\n requestId: string,\n handlers: Array<RequestHandler>,\n options: RequiredDeep<SharedOptions>,\n emitter: Emitter<LifeCycleEventsMap>,\n handleRequestOptions?: HandleRequestOptions,\n): Promise<Response | undefined> {\n emitter.emit('request:start', { request, requestId })\n\n // Perform requests wrapped in \"bypass()\" as-is.\n if (request.headers.get('accept')?.includes('msw/passthrough')) {\n emitter.emit('request:end', { request, requestId })\n handleRequestOptions?.onPassthroughResponse?.(request)\n return\n }\n\n // Resolve a mocked response from the list of request handlers.\n const lookupResult = await until(() => {\n return executeHandlers({\n request,\n requestId,\n handlers,\n resolutionContext: handleRequestOptions?.resolutionContext,\n })\n })\n\n if (lookupResult.error) {\n // Allow developers to react to unhandled exceptions in request handlers.\n emitter.emit('unhandledException', {\n error: lookupResult.error,\n request,\n requestId,\n })\n throw lookupResult.error\n }\n\n // If the handler lookup returned nothing, no request handler was found\n // matching this request. Report the request as unhandled.\n if (!lookupResult.data) {\n await onUnhandledRequest(request, options.onUnhandledRequest)\n emitter.emit('request:unhandled', { request, requestId })\n emitter.emit('request:end', { request, requestId })\n handleRequestOptions?.onPassthroughResponse?.(request)\n return\n }\n\n const { response } = lookupResult.data\n\n // When the handled request returned no mocked response, warn the developer,\n // as it may be an oversight on their part. Perform the request as-is.\n if (!response) {\n emitter.emit('request:end', { request, requestId })\n handleRequestOptions?.onPassthroughResponse?.(request)\n return\n }\n\n // Perform the request as-is when the developer explicitly returned \"req.passthrough()\".\n // This produces no warning as the request was handled.\n if (\n response.status === 302 &&\n response.headers.get('x-msw-intention') === 'passthrough'\n ) {\n emitter.emit('request:end', { request, requestId })\n handleRequestOptions?.onPassthroughResponse?.(request)\n return\n }\n\n // Store all the received response cookies in the cookie jar.\n await storeResponseCookies(request, response)\n\n emitter.emit('request:match', { request, requestId })\n\n const requiredLookupResult =\n lookupResult.data as RequiredDeep<HandlersExecutionResult>\n\n handleRequestOptions?.onMockedResponse?.(response, requiredLookupResult)\n\n emitter.emit('request:end', { request, requestId })\n\n return response\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAsB;AAKtB,6BAAyD;AACzD,gCAAmC;AACnC,kCAAqC;AA+BrC,eAAsB,cACpB,SACA,WACA,UACA,SACA,SACA,sBAC+B;AAC/B,UAAQ,KAAK,iBAAiB,EAAE,SAAS,UAAU,CAAC;AAGpD,MAAI,QAAQ,QAAQ,IAAI,QAAQ,GAAG,SAAS,iBAAiB,GAAG;AAC9D,YAAQ,KAAK,eAAe,EAAE,SAAS,UAAU,CAAC;AAClD,0BAAsB,wBAAwB,OAAO;AACrD;AAAA,EACF;AAGA,QAAM,eAAe,UAAM,oBAAM,MAAM;AACrC,eAAO,wCAAgB;AAAA,MACrB;AAAA,MACA;AAAA,MACA;AAAA,MACA,mBAAmB,sBAAsB;AAAA,IAC3C,CAAC;AAAA,EACH,CAAC;AAED,MAAI,aAAa,OAAO;AAEtB,YAAQ,KAAK,sBAAsB;AAAA,MACjC,OAAO,aAAa;AAAA,MACpB;AAAA,MACA;AAAA,IACF,CAAC;AACD,UAAM,aAAa;AAAA,EACrB;AAIA,MAAI,CAAC,aAAa,MAAM;AACtB,cAAM,8CAAmB,SAAS,QAAQ,kBAAkB;AAC5D,YAAQ,KAAK,qBAAqB,EAAE,SAAS,UAAU,CAAC;AACxD,YAAQ,KAAK,eAAe,EAAE,SAAS,UAAU,CAAC;AAClD,0BAAsB,wBAAwB,OAAO;AACrD;AAAA,EACF;AAEA,QAAM,EAAE,SAAS,IAAI,aAAa;AAIlC,MAAI,CAAC,UAAU;AACb,YAAQ,KAAK,eAAe,EAAE,SAAS,UAAU,CAAC;AAClD,0BAAsB,wBAAwB,OAAO;AACrD;AAAA,EACF;AAIA,MACE,SAAS,WAAW,OACpB,SAAS,QAAQ,IAAI,iBAAiB,MAAM,eAC5C;AACA,YAAQ,KAAK,eAAe,EAAE,SAAS,UAAU,CAAC;AAClD,0BAAsB,wBAAwB,OAAO;AACrD;AAAA,EACF;AAGA,YAAM,kDAAqB,SAAS,QAAQ;AAE5C,UAAQ,KAAK,iBAAiB,EAAE,SAAS,UAAU,CAAC;AAEpD,QAAM,uBACJ,aAAa;AAEf,wBAAsB,mBAAmB,UAAU,oBAAoB;AAEvE,UAAQ,KAAK,eAAe,EAAE,SAAS,UAAU,CAAC;AAElD,SAAO;AACT;","names":[]}
|
|
@@ -43,7 +43,7 @@ async function handleRequest(request, requestId, handlers, options, emitter, han
|
|
|
43
43
|
handleRequestOptions?.onPassthroughResponse?.(request);
|
|
44
44
|
return;
|
|
45
45
|
}
|
|
46
|
-
storeResponseCookies(request, response);
|
|
46
|
+
await storeResponseCookies(request, response);
|
|
47
47
|
emitter.emit("request:match", { request, requestId });
|
|
48
48
|
const requiredLookupResult = lookupResult.data;
|
|
49
49
|
handleRequestOptions?.onMockedResponse?.(response, requiredLookupResult);
|