@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.mjs CHANGED
@@ -1,10 +1,12 @@
1
+ import { InvalidFormDataError as InvalidFormDataError$1, InvalidJSONError as InvalidJSONError$1, HTTP_METHODS, HttpHeaders, HttpSearchParams, HttpFormData, parseHttpBody } from '@zimic/http';
1
2
  import color2 from 'picocolors';
2
- import { HTTP_METHODS, HttpHeaders, HttpSearchParams, HttpFormData } from '@zimic/http';
3
3
  import { http, passthrough } from 'msw';
4
4
  import * as mswBrowser from 'msw/browser';
5
5
  import * as mswNode from 'msw/node';
6
6
  import ClientSocket from 'isomorphic-ws';
7
7
 
8
+ // src/http/index.ts
9
+
8
10
  // src/http/interceptor/errors/RunningHttpInterceptorError.ts
9
11
  var RunningHttpInterceptorError = class extends Error {
10
12
  constructor(additionalMessage) {
@@ -62,24 +64,6 @@ Learn more: https://zimic.dev/docs/interceptor/api/create-http-interceptor`
62
64
  };
63
65
  var RequestSavingSafeLimitExceededError_default = RequestSavingSafeLimitExceededError;
64
66
 
65
- // src/http/interceptorWorker/errors/InvalidFormDataError.ts
66
- var InvalidFormDataError = class extends SyntaxError {
67
- constructor(value) {
68
- super(`Failed to parse value as form data: ${value}`);
69
- this.name = "InvalidFormDataError";
70
- }
71
- };
72
- var InvalidFormDataError_default = InvalidFormDataError;
73
-
74
- // src/http/interceptorWorker/errors/InvalidJSONError.ts
75
- var InvalidJSONError = class extends SyntaxError {
76
- constructor(value) {
77
- super(`Failed to parse value as JSON: ${value}`);
78
- this.name = "InvalidJSONError";
79
- }
80
- };
81
- var InvalidJSONError_default = InvalidJSONError;
82
-
83
67
  // src/cli/browser/shared/constants.ts
84
68
  var SERVICE_WORKER_FILE_NAME = "mockServiceWorker.js";
85
69
 
@@ -928,15 +912,15 @@ function createRegexFromPath(path) {
928
912
  }
929
913
  var createRegexFromPath_default = createRegexFromPath;
930
914
 
931
- // ../zimic-utils/dist/url/excludeURLParams.mjs
932
- function excludeURLParams(url) {
915
+ // ../zimic-utils/dist/url/excludeNonPathParams.mjs
916
+ function excludeNonPathParams(url) {
933
917
  url.hash = "";
934
918
  url.search = "";
935
919
  url.username = "";
936
920
  url.password = "";
937
921
  return url;
938
922
  }
939
- var excludeURLParams_default = excludeURLParams;
923
+ var excludeNonPathParams_default = excludeNonPathParams;
940
924
 
941
925
  // ../zimic-utils/dist/url/validateURLProtocol.mjs
942
926
  var UnsupportedURLProtocolError = class extends TypeError {
@@ -1125,7 +1109,10 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
1125
1109
  static async parseRawRequest(originalRawRequest, options) {
1126
1110
  const rawRequest = originalRawRequest.clone();
1127
1111
  const rawRequestClone = rawRequest.clone();
1128
- const parsedBody = await this.parseRawBody(rawRequest);
1112
+ const parsedBody = await parseHttpBody(rawRequest).catch((error) => {
1113
+ logger.error("Failed to parse request body:", error);
1114
+ return null;
1115
+ });
1129
1116
  const headers = new HttpHeaders(rawRequest.headers);
1130
1117
  const pathParams = this.parseRawPathParams(rawRequest, options);
1131
1118
  const parsedURL = new URL(rawRequest.url);
@@ -1182,7 +1169,10 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
1182
1169
  static async parseRawResponse(originalRawResponse) {
1183
1170
  const rawResponse = originalRawResponse.clone();
1184
1171
  const rawResponseClone = rawResponse.clone();
1185
- const parsedBody = await this.parseRawBody(rawResponse);
1172
+ const parsedBody = await parseHttpBody(rawResponse).catch((error) => {
1173
+ logger.error("Failed to parse response body:", error);
1174
+ return null;
1175
+ });
1186
1176
  const headers = new HttpHeaders(rawResponse.headers);
1187
1177
  const parsedRequest = new Proxy(rawResponse, {
1188
1178
  has(target, property) {
@@ -1230,82 +1220,6 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
1230
1220
  }
1231
1221
  return params;
1232
1222
  }
1233
- static async parseRawBody(resource) {
1234
- const contentType = resource.headers.get("content-type");
1235
- try {
1236
- if (contentType) {
1237
- if (contentType.startsWith("application/json")) {
1238
- return await this.parseRawBodyAsJSON(resource);
1239
- }
1240
- if (contentType.startsWith("multipart/form-data")) {
1241
- return await this.parseRawBodyAsFormData(resource);
1242
- }
1243
- if (contentType.startsWith("application/x-www-form-urlencoded")) {
1244
- return await this.parseRawBodyAsSearchParams(resource);
1245
- }
1246
- if (contentType.startsWith("text/") || contentType.startsWith("application/xml")) {
1247
- return await this.parseRawBodyAsText(resource);
1248
- }
1249
- if (contentType.startsWith("application/") || contentType.startsWith("image/") || contentType.startsWith("audio/") || contentType.startsWith("font/") || contentType.startsWith("video/") || contentType.startsWith("multipart/")) {
1250
- return await this.parseRawBodyAsBlob(resource);
1251
- }
1252
- }
1253
- const resourceClone = resource.clone();
1254
- try {
1255
- return await this.parseRawBodyAsJSON(resource);
1256
- } catch {
1257
- return await this.parseRawBodyAsBlob(resourceClone);
1258
- }
1259
- } catch (error) {
1260
- console.error(error);
1261
- return null;
1262
- }
1263
- }
1264
- static async parseRawBodyAsJSON(resource) {
1265
- const bodyAsText = await resource.text();
1266
- if (!bodyAsText.trim()) {
1267
- return null;
1268
- }
1269
- try {
1270
- const bodyAsJSON = JSON.parse(bodyAsText);
1271
- return bodyAsJSON;
1272
- } catch {
1273
- throw new InvalidJSONError_default(bodyAsText);
1274
- }
1275
- }
1276
- static async parseRawBodyAsSearchParams(resource) {
1277
- const bodyAsText = await resource.text();
1278
- if (!bodyAsText.trim()) {
1279
- return null;
1280
- }
1281
- const bodyAsSearchParams = new HttpSearchParams(bodyAsText);
1282
- return bodyAsSearchParams;
1283
- }
1284
- static async parseRawBodyAsFormData(resource) {
1285
- const resourceClone = resource.clone();
1286
- try {
1287
- const bodyAsRawFormData = await resource.formData();
1288
- const bodyAsFormData = new HttpFormData();
1289
- for (const [key, value] of bodyAsRawFormData) {
1290
- bodyAsFormData.append(key, value);
1291
- }
1292
- return bodyAsFormData;
1293
- } catch {
1294
- const bodyAsText = await resourceClone.text();
1295
- if (!bodyAsText.trim()) {
1296
- return null;
1297
- }
1298
- throw new InvalidFormDataError_default(bodyAsText);
1299
- }
1300
- }
1301
- static async parseRawBodyAsBlob(resource) {
1302
- const bodyAsBlob = await resource.blob();
1303
- return bodyAsBlob;
1304
- }
1305
- static async parseRawBodyAsText(resource) {
1306
- const bodyAsText = await resource.text();
1307
- return bodyAsText || null;
1308
- }
1309
1223
  static async logUnhandledRequestWarning(rawRequest, action) {
1310
1224
  const request = await this.parseRawRequest(rawRequest);
1311
1225
  const [formattedHeaders, formattedSearchParams, formattedBody] = await Promise.all([
@@ -1477,7 +1391,7 @@ var HttpInterceptorClient = class {
1477
1391
  );
1478
1392
  }
1479
1393
  validateURLProtocol_default(newBaseURL, SUPPORTED_BASE_URL_PROTOCOLS);
1480
- excludeURLParams_default(newBaseURL);
1394
+ excludeNonPathParams_default(newBaseURL);
1481
1395
  this._baseURL = newBaseURL;
1482
1396
  }
1483
1397
  get baseURLAsString() {
@@ -1826,7 +1740,7 @@ var LocalHttpInterceptorWorker = class extends HttpInterceptorWorker_default {
1826
1740
  }
1827
1741
  async createResponseForRequest(request) {
1828
1742
  const methodHandlers = this.httpHandlersByMethod[request.method];
1829
- const requestURL = excludeURLParams_default(new URL(request.url));
1743
+ const requestURL = excludeNonPathParams_default(new URL(request.url));
1830
1744
  const requestURLAsString = requestURL.href === `${requestURL.origin}/` ? requestURL.origin : requestURL.href;
1831
1745
  for (let handlerIndex = methodHandlers.length - 1; handlerIndex >= 0; handlerIndex--) {
1832
1746
  const handler = methodHandlers[handlerIndex];
@@ -2789,6 +2703,12 @@ function createHttpInterceptor(options) {
2789
2703
  }
2790
2704
  throw new UnknownHttpInterceptorTypeError_default(type);
2791
2705
  }
2706
+
2707
+ // src/http/index.ts
2708
+ var InvalidFormDataError = class extends InvalidFormDataError$1 {
2709
+ };
2710
+ var InvalidJSONError = class extends InvalidJSONError$1 {
2711
+ };
2792
2712
  /* istanbul ignore next -- @preserve
2793
2713
  * Ignoring because there will always be a handler for the given method and path at this point. */
2794
2714
  /* istanbul ignore else -- @preserve */
@@ -2808,6 +2728,6 @@ function createHttpInterceptor(options) {
2808
2728
  * Reply listeners are always present when notified in normal conditions. If they were not present, the request
2809
2729
  * would reach a timeout and not be responded. The empty set serves as a fallback. */
2810
2730
 
2811
- export { DisabledRequestSavingError_default as DisabledRequestSavingError, InvalidFormDataError_default as InvalidFormDataError, InvalidJSONError_default as InvalidJSONError, NotRunningHttpInterceptorError_default as NotRunningHttpInterceptorError, RequestSavingSafeLimitExceededError_default as RequestSavingSafeLimitExceededError, RunningHttpInterceptorError_default as RunningHttpInterceptorError, TimesCheckError_default as TimesCheckError, UnknownHttpInterceptorPlatformError_default as UnknownHttpInterceptorPlatformError, UnknownHttpInterceptorTypeError_default as UnknownHttpInterceptorTypeError, UnregisteredBrowserServiceWorkerError_default as UnregisteredBrowserServiceWorkerError, createHttpInterceptor };
2731
+ export { DisabledRequestSavingError_default as DisabledRequestSavingError, InvalidFormDataError, InvalidJSONError, NotRunningHttpInterceptorError_default as NotRunningHttpInterceptorError, RequestSavingSafeLimitExceededError_default as RequestSavingSafeLimitExceededError, RunningHttpInterceptorError_default as RunningHttpInterceptorError, TimesCheckError_default as TimesCheckError, UnknownHttpInterceptorPlatformError_default as UnknownHttpInterceptorPlatformError, UnknownHttpInterceptorTypeError_default as UnknownHttpInterceptorTypeError, UnregisteredBrowserServiceWorkerError_default as UnregisteredBrowserServiceWorkerError, createHttpInterceptor };
2812
2732
  //# sourceMappingURL=http.mjs.map
2813
2733
  //# sourceMappingURL=http.mjs.map