@zimic/interceptor 1.1.3-canary.0 → 1.1.3-canary.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/http.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var color2 = require('picocolors');
4
3
  var http = require('@zimic/http');
4
+ var color2 = require('picocolors');
5
5
  var msw = require('msw');
6
6
  var mswBrowser = require('msw/browser');
7
7
  var mswNode = require('msw/node');
@@ -32,6 +32,8 @@ var mswBrowser__namespace = /*#__PURE__*/_interopNamespace(mswBrowser);
32
32
  var mswNode__namespace = /*#__PURE__*/_interopNamespace(mswNode);
33
33
  var ClientSocket__default = /*#__PURE__*/_interopDefault(ClientSocket);
34
34
 
35
+ // src/http/index.ts
36
+
35
37
  // src/http/interceptor/errors/RunningHttpInterceptorError.ts
36
38
  var RunningHttpInterceptorError = class extends Error {
37
39
  constructor(additionalMessage) {
@@ -89,24 +91,6 @@ Learn more: https://zimic.dev/docs/interceptor/api/create-http-interceptor`
89
91
  };
90
92
  var RequestSavingSafeLimitExceededError_default = RequestSavingSafeLimitExceededError;
91
93
 
92
- // src/http/interceptorWorker/errors/InvalidFormDataError.ts
93
- var InvalidFormDataError = class extends SyntaxError {
94
- constructor(value) {
95
- super(`Failed to parse value as form data: ${value}`);
96
- this.name = "InvalidFormDataError";
97
- }
98
- };
99
- var InvalidFormDataError_default = InvalidFormDataError;
100
-
101
- // src/http/interceptorWorker/errors/InvalidJSONError.ts
102
- var InvalidJSONError = class extends SyntaxError {
103
- constructor(value) {
104
- super(`Failed to parse value as JSON: ${value}`);
105
- this.name = "InvalidJSONError";
106
- }
107
- };
108
- var InvalidJSONError_default = InvalidJSONError;
109
-
110
94
  // src/cli/browser/shared/constants.ts
111
95
  var SERVICE_WORKER_FILE_NAME = "mockServiceWorker.js";
112
96
 
@@ -955,15 +939,15 @@ function createRegexFromPath(path) {
955
939
  }
956
940
  var createRegexFromPath_default = createRegexFromPath;
957
941
 
958
- // ../zimic-utils/dist/url/excludeURLParams.mjs
959
- function excludeURLParams(url) {
942
+ // ../zimic-utils/dist/url/excludeNonPathParams.mjs
943
+ function excludeNonPathParams(url) {
960
944
  url.hash = "";
961
945
  url.search = "";
962
946
  url.username = "";
963
947
  url.password = "";
964
948
  return url;
965
949
  }
966
- var excludeURLParams_default = excludeURLParams;
950
+ var excludeNonPathParams_default = excludeNonPathParams;
967
951
 
968
952
  // ../zimic-utils/dist/url/validateURLProtocol.mjs
969
953
  var UnsupportedURLProtocolError = class extends TypeError {
@@ -1152,7 +1136,10 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
1152
1136
  static async parseRawRequest(originalRawRequest, options) {
1153
1137
  const rawRequest = originalRawRequest.clone();
1154
1138
  const rawRequestClone = rawRequest.clone();
1155
- const parsedBody = await this.parseRawBody(rawRequest);
1139
+ const parsedBody = await http.parseHttpBody(rawRequest).catch((error) => {
1140
+ logger.error("Failed to parse request body:", error);
1141
+ return null;
1142
+ });
1156
1143
  const headers = new http.HttpHeaders(rawRequest.headers);
1157
1144
  const pathParams = this.parseRawPathParams(rawRequest, options);
1158
1145
  const parsedURL = new URL(rawRequest.url);
@@ -1209,7 +1196,10 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
1209
1196
  static async parseRawResponse(originalRawResponse) {
1210
1197
  const rawResponse = originalRawResponse.clone();
1211
1198
  const rawResponseClone = rawResponse.clone();
1212
- const parsedBody = await this.parseRawBody(rawResponse);
1199
+ const parsedBody = await http.parseHttpBody(rawResponse).catch((error) => {
1200
+ logger.error("Failed to parse response body:", error);
1201
+ return null;
1202
+ });
1213
1203
  const headers = new http.HttpHeaders(rawResponse.headers);
1214
1204
  const parsedRequest = new Proxy(rawResponse, {
1215
1205
  has(target, property) {
@@ -1257,82 +1247,6 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
1257
1247
  }
1258
1248
  return params;
1259
1249
  }
1260
- static async parseRawBody(resource) {
1261
- const contentType = resource.headers.get("content-type");
1262
- try {
1263
- if (contentType) {
1264
- if (contentType.startsWith("application/json")) {
1265
- return await this.parseRawBodyAsJSON(resource);
1266
- }
1267
- if (contentType.startsWith("multipart/form-data")) {
1268
- return await this.parseRawBodyAsFormData(resource);
1269
- }
1270
- if (contentType.startsWith("application/x-www-form-urlencoded")) {
1271
- return await this.parseRawBodyAsSearchParams(resource);
1272
- }
1273
- if (contentType.startsWith("text/") || contentType.startsWith("application/xml")) {
1274
- return await this.parseRawBodyAsText(resource);
1275
- }
1276
- if (contentType.startsWith("application/") || contentType.startsWith("image/") || contentType.startsWith("audio/") || contentType.startsWith("font/") || contentType.startsWith("video/") || contentType.startsWith("multipart/")) {
1277
- return await this.parseRawBodyAsBlob(resource);
1278
- }
1279
- }
1280
- const resourceClone = resource.clone();
1281
- try {
1282
- return await this.parseRawBodyAsJSON(resource);
1283
- } catch {
1284
- return await this.parseRawBodyAsBlob(resourceClone);
1285
- }
1286
- } catch (error) {
1287
- console.error(error);
1288
- return null;
1289
- }
1290
- }
1291
- static async parseRawBodyAsJSON(resource) {
1292
- const bodyAsText = await resource.text();
1293
- if (!bodyAsText.trim()) {
1294
- return null;
1295
- }
1296
- try {
1297
- const bodyAsJSON = JSON.parse(bodyAsText);
1298
- return bodyAsJSON;
1299
- } catch {
1300
- throw new InvalidJSONError_default(bodyAsText);
1301
- }
1302
- }
1303
- static async parseRawBodyAsSearchParams(resource) {
1304
- const bodyAsText = await resource.text();
1305
- if (!bodyAsText.trim()) {
1306
- return null;
1307
- }
1308
- const bodyAsSearchParams = new http.HttpSearchParams(bodyAsText);
1309
- return bodyAsSearchParams;
1310
- }
1311
- static async parseRawBodyAsFormData(resource) {
1312
- const resourceClone = resource.clone();
1313
- try {
1314
- const bodyAsRawFormData = await resource.formData();
1315
- const bodyAsFormData = new http.HttpFormData();
1316
- for (const [key, value] of bodyAsRawFormData) {
1317
- bodyAsFormData.append(key, value);
1318
- }
1319
- return bodyAsFormData;
1320
- } catch {
1321
- const bodyAsText = await resourceClone.text();
1322
- if (!bodyAsText.trim()) {
1323
- return null;
1324
- }
1325
- throw new InvalidFormDataError_default(bodyAsText);
1326
- }
1327
- }
1328
- static async parseRawBodyAsBlob(resource) {
1329
- const bodyAsBlob = await resource.blob();
1330
- return bodyAsBlob;
1331
- }
1332
- static async parseRawBodyAsText(resource) {
1333
- const bodyAsText = await resource.text();
1334
- return bodyAsText || null;
1335
- }
1336
1250
  static async logUnhandledRequestWarning(rawRequest, action) {
1337
1251
  const request = await this.parseRawRequest(rawRequest);
1338
1252
  const [formattedHeaders, formattedSearchParams, formattedBody] = await Promise.all([
@@ -1504,7 +1418,7 @@ var HttpInterceptorClient = class {
1504
1418
  );
1505
1419
  }
1506
1420
  validateURLProtocol_default(newBaseURL, SUPPORTED_BASE_URL_PROTOCOLS);
1507
- excludeURLParams_default(newBaseURL);
1421
+ excludeNonPathParams_default(newBaseURL);
1508
1422
  this._baseURL = newBaseURL;
1509
1423
  }
1510
1424
  get baseURLAsString() {
@@ -1853,7 +1767,7 @@ var LocalHttpInterceptorWorker = class extends HttpInterceptorWorker_default {
1853
1767
  }
1854
1768
  async createResponseForRequest(request) {
1855
1769
  const methodHandlers = this.httpHandlersByMethod[request.method];
1856
- const requestURL = excludeURLParams_default(new URL(request.url));
1770
+ const requestURL = excludeNonPathParams_default(new URL(request.url));
1857
1771
  const requestURLAsString = requestURL.href === `${requestURL.origin}/` ? requestURL.origin : requestURL.href;
1858
1772
  for (let handlerIndex = methodHandlers.length - 1; handlerIndex >= 0; handlerIndex--) {
1859
1773
  const handler = methodHandlers[handlerIndex];
@@ -2816,6 +2730,12 @@ function createHttpInterceptor(options) {
2816
2730
  }
2817
2731
  throw new UnknownHttpInterceptorTypeError_default(type);
2818
2732
  }
2733
+
2734
+ // src/http/index.ts
2735
+ var InvalidFormDataError = class extends http.InvalidFormDataError {
2736
+ };
2737
+ var InvalidJSONError = class extends http.InvalidJSONError {
2738
+ };
2819
2739
  /* istanbul ignore next -- @preserve
2820
2740
  * Ignoring because there will always be a handler for the given method and path at this point. */
2821
2741
  /* istanbul ignore else -- @preserve */
@@ -2836,8 +2756,8 @@ function createHttpInterceptor(options) {
2836
2756
  * would reach a timeout and not be responded. The empty set serves as a fallback. */
2837
2757
 
2838
2758
  exports.DisabledRequestSavingError = DisabledRequestSavingError_default;
2839
- exports.InvalidFormDataError = InvalidFormDataError_default;
2840
- exports.InvalidJSONError = InvalidJSONError_default;
2759
+ exports.InvalidFormDataError = InvalidFormDataError;
2760
+ exports.InvalidJSONError = InvalidJSONError;
2841
2761
  exports.NotRunningHttpInterceptorError = NotRunningHttpInterceptorError_default;
2842
2762
  exports.RequestSavingSafeLimitExceededError = RequestSavingSafeLimitExceededError_default;
2843
2763
  exports.RunningHttpInterceptorError = RunningHttpInterceptorError_default;