msw 0.39.1 → 0.39.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/node/lib/index.js CHANGED
@@ -2047,207 +2047,6 @@ function resetHandlers(initialHandlers, ...nextHandlers) {
2047
2047
  return nextHandlers.length > 0 ? [...nextHandlers] : [...initialHandlers];
2048
2048
  }
2049
2049
 
2050
- /*!
2051
- * cookie
2052
- * Copyright(c) 2012-2014 Roman Shtylman
2053
- * Copyright(c) 2015 Douglas Christopher Wilson
2054
- * MIT Licensed
2055
- */
2056
-
2057
- /**
2058
- * Module exports.
2059
- * @public
2060
- */
2061
-
2062
- var parse_1 = parse$2;
2063
- var serialize_1 = serialize;
2064
-
2065
- /**
2066
- * Module variables.
2067
- * @private
2068
- */
2069
-
2070
- var decode = decodeURIComponent;
2071
- var encode = encodeURIComponent;
2072
-
2073
- /**
2074
- * RegExp to match field-content in RFC 7230 sec 3.2
2075
- *
2076
- * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
2077
- * field-vchar = VCHAR / obs-text
2078
- * obs-text = %x80-FF
2079
- */
2080
-
2081
- var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
2082
-
2083
- /**
2084
- * Parse a cookie header.
2085
- *
2086
- * Parse the given cookie header string into an object
2087
- * The object has the various cookies as keys(names) => values
2088
- *
2089
- * @param {string} str
2090
- * @param {object} [options]
2091
- * @return {object}
2092
- * @public
2093
- */
2094
-
2095
- function parse$2(str, options) {
2096
- if (typeof str !== 'string') {
2097
- throw new TypeError('argument str must be a string');
2098
- }
2099
-
2100
- var obj = {};
2101
- var opt = options || {};
2102
- var pairs = str.split(';');
2103
- var dec = opt.decode || decode;
2104
-
2105
- for (var i = 0; i < pairs.length; i++) {
2106
- var pair = pairs[i];
2107
- var index = pair.indexOf('=');
2108
-
2109
- // skip things that don't look like key=value
2110
- if (index < 0) {
2111
- continue;
2112
- }
2113
-
2114
- var key = pair.substring(0, index).trim();
2115
-
2116
- // only assign once
2117
- if (undefined == obj[key]) {
2118
- var val = pair.substring(index + 1, pair.length).trim();
2119
-
2120
- // quoted values
2121
- if (val[0] === '"') {
2122
- val = val.slice(1, -1);
2123
- }
2124
-
2125
- obj[key] = tryDecode(val, dec);
2126
- }
2127
- }
2128
-
2129
- return obj;
2130
- }
2131
-
2132
- /**
2133
- * Serialize data into a cookie header.
2134
- *
2135
- * Serialize the a name value pair into a cookie string suitable for
2136
- * http headers. An optional options object specified cookie parameters.
2137
- *
2138
- * serialize('foo', 'bar', { httpOnly: true })
2139
- * => "foo=bar; httpOnly"
2140
- *
2141
- * @param {string} name
2142
- * @param {string} val
2143
- * @param {object} [options]
2144
- * @return {string}
2145
- * @public
2146
- */
2147
-
2148
- function serialize(name, val, options) {
2149
- var opt = options || {};
2150
- var enc = opt.encode || encode;
2151
-
2152
- if (typeof enc !== 'function') {
2153
- throw new TypeError('option encode is invalid');
2154
- }
2155
-
2156
- if (!fieldContentRegExp.test(name)) {
2157
- throw new TypeError('argument name is invalid');
2158
- }
2159
-
2160
- var value = enc(val);
2161
-
2162
- if (value && !fieldContentRegExp.test(value)) {
2163
- throw new TypeError('argument val is invalid');
2164
- }
2165
-
2166
- var str = name + '=' + value;
2167
-
2168
- if (null != opt.maxAge) {
2169
- var maxAge = opt.maxAge - 0;
2170
-
2171
- if (isNaN(maxAge) || !isFinite(maxAge)) {
2172
- throw new TypeError('option maxAge is invalid')
2173
- }
2174
-
2175
- str += '; Max-Age=' + Math.floor(maxAge);
2176
- }
2177
-
2178
- if (opt.domain) {
2179
- if (!fieldContentRegExp.test(opt.domain)) {
2180
- throw new TypeError('option domain is invalid');
2181
- }
2182
-
2183
- str += '; Domain=' + opt.domain;
2184
- }
2185
-
2186
- if (opt.path) {
2187
- if (!fieldContentRegExp.test(opt.path)) {
2188
- throw new TypeError('option path is invalid');
2189
- }
2190
-
2191
- str += '; Path=' + opt.path;
2192
- }
2193
-
2194
- if (opt.expires) {
2195
- if (typeof opt.expires.toUTCString !== 'function') {
2196
- throw new TypeError('option expires is invalid');
2197
- }
2198
-
2199
- str += '; Expires=' + opt.expires.toUTCString();
2200
- }
2201
-
2202
- if (opt.httpOnly) {
2203
- str += '; HttpOnly';
2204
- }
2205
-
2206
- if (opt.secure) {
2207
- str += '; Secure';
2208
- }
2209
-
2210
- if (opt.sameSite) {
2211
- var sameSite = typeof opt.sameSite === 'string'
2212
- ? opt.sameSite.toLowerCase() : opt.sameSite;
2213
-
2214
- switch (sameSite) {
2215
- case true:
2216
- str += '; SameSite=Strict';
2217
- break;
2218
- case 'lax':
2219
- str += '; SameSite=Lax';
2220
- break;
2221
- case 'strict':
2222
- str += '; SameSite=Strict';
2223
- break;
2224
- case 'none':
2225
- str += '; SameSite=None';
2226
- break;
2227
- default:
2228
- throw new TypeError('option sameSite is invalid');
2229
- }
2230
- }
2231
-
2232
- return str;
2233
- }
2234
-
2235
- /**
2236
- * Try decoding a string using a decoding function.
2237
- *
2238
- * @param {string} str
2239
- * @param {function} decode
2240
- * @private
2241
- */
2242
-
2243
- function tryDecode(str, decode) {
2244
- try {
2245
- return decode(str);
2246
- } catch (e) {
2247
- return str;
2248
- }
2249
- }
2250
-
2251
2050
  /**
2252
2051
  * Parses a given value into a JSON.
2253
2052
  * Does not throw an exception on an invalid JSON string.
@@ -2888,6 +2687,207 @@ function parseBody(body, headers) {
2888
2687
  return body;
2889
2688
  }
2890
2689
 
2690
+ /*!
2691
+ * cookie
2692
+ * Copyright(c) 2012-2014 Roman Shtylman
2693
+ * Copyright(c) 2015 Douglas Christopher Wilson
2694
+ * MIT Licensed
2695
+ */
2696
+
2697
+ /**
2698
+ * Module exports.
2699
+ * @public
2700
+ */
2701
+
2702
+ var parse_1 = parse$2;
2703
+ var serialize_1 = serialize;
2704
+
2705
+ /**
2706
+ * Module variables.
2707
+ * @private
2708
+ */
2709
+
2710
+ var decode = decodeURIComponent;
2711
+ var encode = encodeURIComponent;
2712
+
2713
+ /**
2714
+ * RegExp to match field-content in RFC 7230 sec 3.2
2715
+ *
2716
+ * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
2717
+ * field-vchar = VCHAR / obs-text
2718
+ * obs-text = %x80-FF
2719
+ */
2720
+
2721
+ var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
2722
+
2723
+ /**
2724
+ * Parse a cookie header.
2725
+ *
2726
+ * Parse the given cookie header string into an object
2727
+ * The object has the various cookies as keys(names) => values
2728
+ *
2729
+ * @param {string} str
2730
+ * @param {object} [options]
2731
+ * @return {object}
2732
+ * @public
2733
+ */
2734
+
2735
+ function parse$2(str, options) {
2736
+ if (typeof str !== 'string') {
2737
+ throw new TypeError('argument str must be a string');
2738
+ }
2739
+
2740
+ var obj = {};
2741
+ var opt = options || {};
2742
+ var pairs = str.split(';');
2743
+ var dec = opt.decode || decode;
2744
+
2745
+ for (var i = 0; i < pairs.length; i++) {
2746
+ var pair = pairs[i];
2747
+ var index = pair.indexOf('=');
2748
+
2749
+ // skip things that don't look like key=value
2750
+ if (index < 0) {
2751
+ continue;
2752
+ }
2753
+
2754
+ var key = pair.substring(0, index).trim();
2755
+
2756
+ // only assign once
2757
+ if (undefined == obj[key]) {
2758
+ var val = pair.substring(index + 1, pair.length).trim();
2759
+
2760
+ // quoted values
2761
+ if (val[0] === '"') {
2762
+ val = val.slice(1, -1);
2763
+ }
2764
+
2765
+ obj[key] = tryDecode(val, dec);
2766
+ }
2767
+ }
2768
+
2769
+ return obj;
2770
+ }
2771
+
2772
+ /**
2773
+ * Serialize data into a cookie header.
2774
+ *
2775
+ * Serialize the a name value pair into a cookie string suitable for
2776
+ * http headers. An optional options object specified cookie parameters.
2777
+ *
2778
+ * serialize('foo', 'bar', { httpOnly: true })
2779
+ * => "foo=bar; httpOnly"
2780
+ *
2781
+ * @param {string} name
2782
+ * @param {string} val
2783
+ * @param {object} [options]
2784
+ * @return {string}
2785
+ * @public
2786
+ */
2787
+
2788
+ function serialize(name, val, options) {
2789
+ var opt = options || {};
2790
+ var enc = opt.encode || encode;
2791
+
2792
+ if (typeof enc !== 'function') {
2793
+ throw new TypeError('option encode is invalid');
2794
+ }
2795
+
2796
+ if (!fieldContentRegExp.test(name)) {
2797
+ throw new TypeError('argument name is invalid');
2798
+ }
2799
+
2800
+ var value = enc(val);
2801
+
2802
+ if (value && !fieldContentRegExp.test(value)) {
2803
+ throw new TypeError('argument val is invalid');
2804
+ }
2805
+
2806
+ var str = name + '=' + value;
2807
+
2808
+ if (null != opt.maxAge) {
2809
+ var maxAge = opt.maxAge - 0;
2810
+
2811
+ if (isNaN(maxAge) || !isFinite(maxAge)) {
2812
+ throw new TypeError('option maxAge is invalid')
2813
+ }
2814
+
2815
+ str += '; Max-Age=' + Math.floor(maxAge);
2816
+ }
2817
+
2818
+ if (opt.domain) {
2819
+ if (!fieldContentRegExp.test(opt.domain)) {
2820
+ throw new TypeError('option domain is invalid');
2821
+ }
2822
+
2823
+ str += '; Domain=' + opt.domain;
2824
+ }
2825
+
2826
+ if (opt.path) {
2827
+ if (!fieldContentRegExp.test(opt.path)) {
2828
+ throw new TypeError('option path is invalid');
2829
+ }
2830
+
2831
+ str += '; Path=' + opt.path;
2832
+ }
2833
+
2834
+ if (opt.expires) {
2835
+ if (typeof opt.expires.toUTCString !== 'function') {
2836
+ throw new TypeError('option expires is invalid');
2837
+ }
2838
+
2839
+ str += '; Expires=' + opt.expires.toUTCString();
2840
+ }
2841
+
2842
+ if (opt.httpOnly) {
2843
+ str += '; HttpOnly';
2844
+ }
2845
+
2846
+ if (opt.secure) {
2847
+ str += '; Secure';
2848
+ }
2849
+
2850
+ if (opt.sameSite) {
2851
+ var sameSite = typeof opt.sameSite === 'string'
2852
+ ? opt.sameSite.toLowerCase() : opt.sameSite;
2853
+
2854
+ switch (sameSite) {
2855
+ case true:
2856
+ str += '; SameSite=Strict';
2857
+ break;
2858
+ case 'lax':
2859
+ str += '; SameSite=Lax';
2860
+ break;
2861
+ case 'strict':
2862
+ str += '; SameSite=Strict';
2863
+ break;
2864
+ case 'none':
2865
+ str += '; SameSite=None';
2866
+ break;
2867
+ default:
2868
+ throw new TypeError('option sameSite is invalid');
2869
+ }
2870
+ }
2871
+
2872
+ return str;
2873
+ }
2874
+
2875
+ /**
2876
+ * Try decoding a string using a decoding function.
2877
+ *
2878
+ * @param {string} str
2879
+ * @param {function} decode
2880
+ * @private
2881
+ */
2882
+
2883
+ function tryDecode(str, decode) {
2884
+ try {
2885
+ return decode(str);
2886
+ } catch (e) {
2887
+ return str;
2888
+ }
2889
+ }
2890
+
2891
2891
  function getAllCookies() {
2892
2892
  return parse_1(document.cookie);
2893
2893
  }
@@ -2917,13 +2917,36 @@ function getRequestCookies(request) {
2917
2917
  }
2918
2918
  }
2919
2919
 
2920
+ /**
2921
+ * Sets relevant cookies on the request.
2922
+ * Request cookies are taken from the following sources:
2923
+ * - Immediate (own) request cookies (those in the "Cookie" request header);
2924
+ * - From the `document.cookie` based on the request's `credentials` value;
2925
+ * - From the internal cookie store that persists/hydrates cookies in Node.js
2926
+ */
2920
2927
  function setRequestCookies(request) {
2921
2928
  var _a;
2929
+ // Set mocked request cookies from the `cookie` header of the original request.
2930
+ // No need to take `credentials` into account, because in Node.js requests are intercepted
2931
+ // _after_ they happen. Request issuer should have already taken care of sending relevant cookies.
2932
+ // Unlike browser, where interception is on the worker level, _before_ the request happens.
2933
+ const requestCookiesString = request.headers.get('cookie');
2922
2934
  cookies.store.hydrate();
2923
- request.cookies = Object.assign(Object.assign({}, getRequestCookies(request)), Array.from((_a = cookies.store.get(Object.assign(Object.assign({}, request), { url: request.url.toString() }))) === null || _a === void 0 ? void 0 : _a.entries()).reduce((cookies, [name, { value }]) => Object.assign(cookies, { [name]: value }), {}));
2924
- request.headers.set('cookie', Object.entries(request.cookies)
2925
- .map(([name, value]) => `${name}=${value}`)
2926
- .join('; '));
2935
+ const cookiesFromStore = Array.from((_a = cookies.store.get(Object.assign(Object.assign({}, request), { url: request.url.toString() }))) === null || _a === void 0 ? void 0 : _a.entries()).reduce((cookies, [name, { value }]) => {
2936
+ return Object.assign(cookies, { [name.trim()]: value });
2937
+ }, {});
2938
+ const cookiesFromDocument = getRequestCookies(request);
2939
+ const forwardedCookies = Object.assign(Object.assign({}, cookiesFromDocument), cookiesFromStore);
2940
+ // Ensure the persisted (document) cookies are propagated to the request.
2941
+ // Propagated the cookies persisted in the Cookuie Store to the request headers.
2942
+ // This forwards relevant request cookies based on the request's credentials.
2943
+ for (const [name, value] of Object.entries(forwardedCookies)) {
2944
+ request.headers.append('cookie', `${name}=${value}`);
2945
+ }
2946
+ const ownCookies = requestCookiesString
2947
+ ? parse_1(requestCookiesString)
2948
+ : {};
2949
+ request.cookies = Object.assign(Object.assign(Object.assign({}, request.cookies), forwardedCookies), ownCookies);
2927
2950
  }
2928
2951
 
2929
2952
  /**
@@ -2935,6 +2958,7 @@ function parseIsomorphicRequest(request) {
2935
2958
  url: request.url,
2936
2959
  method: request.method,
2937
2960
  body: parseBody(request.body, request.headers),
2961
+ credentials: request.credentials || 'same-origin',
2938
2962
  headers: request.headers,
2939
2963
  cookies: {},
2940
2964
  redirect: 'manual',
@@ -2946,21 +2970,9 @@ function parseIsomorphicRequest(request) {
2946
2970
  integrity: '',
2947
2971
  destination: 'document',
2948
2972
  bodyUsed: false,
2949
- credentials: 'same-origin',
2950
2973
  };
2951
- // Set mocked request cookies from the `cookie` header of the original request.
2952
- // No need to take `credentials` into account, because in Node.js requests are intercepted
2953
- // _after_ they happen. Request issuer should have already taken care of sending relevant cookies.
2954
- // Unlike browser, where interception is on the worker level, _before_ the request happens.
2955
- const requestCookiesString = request.headers.get('cookie');
2956
2974
  // Attach all the cookies from the virtual cookie store.
2957
2975
  setRequestCookies(mockedRequest);
2958
- const requestCookies = requestCookiesString
2959
- ? parse_1(requestCookiesString)
2960
- : {};
2961
- // Merge both direct request cookies and the cookies inherited
2962
- // from other same-origin requests in the cookie store.
2963
- mockedRequest.cookies = Object.assign(Object.assign({}, mockedRequest.cookies), requestCookies);
2964
2976
  return mockedRequest;
2965
2977
  }
2966
2978
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msw",
3
- "version": "0.39.1",
3
+ "version": "0.39.2",
4
4
  "description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",
5
5
  "main": "lib/umd/index.js",
6
6
  "module": "lib/esm/index.js",
@@ -9,13 +9,15 @@
9
9
  "msw": "cli/index.js"
10
10
  },
11
11
  "engines": {
12
- "node": ">=16.x"
12
+ "node": ">=14"
13
13
  },
14
14
  "scripts": {
15
15
  "start": "cross-env NODE_ENV=development rollup -c rollup.config.ts -w",
16
16
  "clean": "rimraf lib {native,node}/lib",
17
17
  "lint": "eslint \"{cli,config,src,test}/**/*.ts\"",
18
- "build": "yarn clean && yarn lint && cross-env NODE_ENV=production rollup -c rollup.config.ts && yarn test:ts",
18
+ "prebuild": "yarn clean && yarn lint",
19
+ "build": "cross-env NODE_ENV=production rollup -c rollup.config.ts",
20
+ "postbuild": "yarn test:ts",
19
21
  "test": "yarn test:unit && yarn test:integration",
20
22
  "test:unit": "cross-env BABEL_ENV=test jest --maxWorkers=3",
21
23
  "test:integration": "jest --config=test/jest.config.js --maxWorkers=1",
@@ -67,8 +69,8 @@
67
69
  ],
68
70
  "sideEffects": false,
69
71
  "dependencies": {
70
- "@mswjs/cookies": "^0.1.7",
71
- "@mswjs/interceptors": "^0.14.1",
72
+ "@mswjs/cookies": "^0.2.0",
73
+ "@mswjs/interceptors": "^0.15.1",
72
74
  "@open-draft/until": "^1.0.3",
73
75
  "@types/cookie": "^0.4.1",
74
76
  "@types/js-levenshtein": "^1.1.1",
@@ -98,6 +100,7 @@
98
100
  "@rollup/plugin-json": "^4.1.0",
99
101
  "@rollup/plugin-node-resolve": "^13.1.3",
100
102
  "@rollup/plugin-replace": "^3.1.0",
103
+ "@semantic-release/git": "^10.0.1",
101
104
  "@types/fs-extra": "^9.0.13",
102
105
  "@types/jest": "26",
103
106
  "@types/json-bigint": "^1.0.1",