@zimic/interceptor 0.16.0-canary.5 → 0.16.0-canary.7

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
@@ -35,30 +35,6 @@ var ClientSocket__default = /*#__PURE__*/_interopDefault(ClientSocket);
35
35
  var __defProp = Object.defineProperty;
36
36
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
37
37
 
38
- // src/http/interceptorWorker/errors/InvalidJSONError.ts
39
- var InvalidJSONError = class extends SyntaxError {
40
- static {
41
- __name(this, "InvalidJSONError");
42
- }
43
- constructor(value) {
44
- super(`Failed to parse value as JSON: ${value}`);
45
- this.name = "InvalidJSONError";
46
- }
47
- };
48
- var InvalidJSONError_default = InvalidJSONError;
49
-
50
- // src/http/interceptorWorker/errors/InvalidFormDataError.ts
51
- var InvalidFormDataError = class extends SyntaxError {
52
- static {
53
- __name(this, "InvalidFormDataError");
54
- }
55
- constructor(value) {
56
- super(`Failed to parse value as form data: ${value}`);
57
- this.name = "InvalidFormDataError";
58
- }
59
- };
60
- var InvalidFormDataError_default = InvalidFormDataError;
61
-
62
38
  // src/http/interceptor/errors/RunningHttpInterceptorError.ts
63
39
  var RunningHttpInterceptorError = class extends Error {
64
40
  static {
@@ -111,6 +87,50 @@ var UnknownHttpInterceptorTypeError = class extends TypeError {
111
87
  };
112
88
  var UnknownHttpInterceptorTypeError_default = UnknownHttpInterceptorTypeError;
113
89
 
90
+ // src/http/interceptor/errors/RequestSavingSafeLimitExceededError.ts
91
+ var RequestSavingSafeLimitExceededError = class extends TypeError {
92
+ static {
93
+ __name(this, "RequestSavingSafeLimitExceededError");
94
+ }
95
+ constructor(numberOfSavedRequests, safeLimit) {
96
+ super(
97
+ `The number of intercepted requests saved in memory (${numberOfSavedRequests}) exceeded the safe limit of ${safeLimit}. Did you forget to call \`interceptor.clear()\`?
98
+
99
+ If you need to save requests, make sure to regularly call \`interceptor.clear()\`. Alternatively, you can hide this warning by increasing \`requestSaving.safeLimit\` in your interceptor. Note that saving too many requests in memory can lead to performance issues.
100
+
101
+ If you do not need to save requests, consider setting \`requestSaving.enabled: false\` in your interceptor.
102
+
103
+ Learn more: https://github.com/zimicjs/zimic/wiki/api\u2010zimic\u2010interceptor\u2010http#saving-requests`
104
+ );
105
+ this.name = "RequestSavingSafeLimitExceededError";
106
+ }
107
+ };
108
+ var RequestSavingSafeLimitExceededError_default = RequestSavingSafeLimitExceededError;
109
+
110
+ // src/http/interceptorWorker/errors/InvalidFormDataError.ts
111
+ var InvalidFormDataError = class extends SyntaxError {
112
+ static {
113
+ __name(this, "InvalidFormDataError");
114
+ }
115
+ constructor(value) {
116
+ super(`Failed to parse value as form data: ${value}`);
117
+ this.name = "InvalidFormDataError";
118
+ }
119
+ };
120
+ var InvalidFormDataError_default = InvalidFormDataError;
121
+
122
+ // src/http/interceptorWorker/errors/InvalidJSONError.ts
123
+ var InvalidJSONError = class extends SyntaxError {
124
+ static {
125
+ __name(this, "InvalidJSONError");
126
+ }
127
+ constructor(value) {
128
+ super(`Failed to parse value as JSON: ${value}`);
129
+ this.name = "InvalidJSONError";
130
+ }
131
+ };
132
+ var InvalidJSONError_default = InvalidJSONError;
133
+
114
134
  // src/cli/browser/shared/constants.ts
115
135
  var SERVICE_WORKER_FILE_NAME = "mockServiceWorker.js";
116
136
 
@@ -142,7 +162,7 @@ var DisabledRequestSavingError = class extends TypeError {
142
162
  }
143
163
  constructor() {
144
164
  super(
145
- "Intercepted requests are not being saved. Did you forget to use `saveRequests: true` when creating the interceptor?\n\nLearn more: https://github.com/zimicjs/zimic/wiki/api\u2010zimic\u2010interceptor\u2010http#saving-requests"
165
+ "Intercepted requests are not being saved. Did you forget to use `requestSaving.enabled: true` in your interceptor?\n\nLearn more: https://github.com/zimicjs/zimic/wiki/api\u2010zimic\u2010interceptor\u2010http#saving-requests"
146
166
  );
147
167
  this.name = "DisabledRequestSavingError";
148
168
  }
@@ -286,9 +306,9 @@ Requests evaluated by this handler:
286
306
  ].filter((part) => part !== false).join("");
287
307
  }
288
308
  __name(createMessageHeader, "createMessageHeader");
289
- function createMessageDiffs({ hasSavedRequests, unmatchedRequestGroups }) {
290
- if (!hasSavedRequests) {
291
- return "Tip: enable `saveRequests: true` in your interceptor for more details about the unmatched requests.";
309
+ function createMessageDiffs({ requestSaving, unmatchedRequestGroups }) {
310
+ if (!requestSaving.enabled) {
311
+ return "Tip: use `requestSaving.enabled: true` in your interceptor for more details about the unmatched requests.";
292
312
  }
293
313
  return unmatchedRequestGroups.map(({ request, diff }, index) => {
294
314
  const requestNumber = index + 1;
@@ -503,7 +523,7 @@ var HttpRequestHandlerClient = class {
503
523
  newThis.createResponseDeclaration = this.isResponseDeclarationFactory(declaration) ? declaration : () => declaration;
504
524
  newThis.numberOfMatchedRequests = 0;
505
525
  newThis.unmatchedRequestGroups.length = 0;
506
- newThis._requests.length = 0;
526
+ newThis.clearInterceptedRequests();
507
527
  this.interceptor.registerRequestHandler(this.handler);
508
528
  return newThis;
509
529
  }
@@ -527,7 +547,7 @@ var HttpRequestHandlerClient = class {
527
547
  declarationPointer: this.timesDeclarationPointer,
528
548
  unmatchedRequestGroups: this.unmatchedRequestGroups,
529
549
  hasRestrictions: this.restrictions.length > 0,
530
- hasSavedRequests: this.interceptor.saveRequests
550
+ requestSaving: this.interceptor.requestSaving
531
551
  });
532
552
  }
533
553
  }
@@ -539,7 +559,7 @@ var HttpRequestHandlerClient = class {
539
559
  this.timesDeclarationPointer = void 0;
540
560
  this.numberOfMatchedRequests = 0;
541
561
  this.unmatchedRequestGroups.length = 0;
542
- this._requests.length = 0;
562
+ this.clearInterceptedRequests();
543
563
  this.createResponseDeclaration = void 0;
544
564
  return this;
545
565
  }
@@ -552,7 +572,7 @@ var HttpRequestHandlerClient = class {
552
572
  if (restrictionsMatch.value) {
553
573
  this.numberOfMatchedRequests++;
554
574
  } else {
555
- const shouldSaveUnmatchedGroup = this.interceptor.saveRequests && this.restrictions.length > 0 && this.timesDeclarationPointer !== void 0;
575
+ const shouldSaveUnmatchedGroup = this.interceptor.requestSaving.enabled && this.restrictions.length > 0 && this.timesDeclarationPointer !== void 0;
556
576
  if (shouldSaveUnmatchedGroup) {
557
577
  this.unmatchedRequestGroups.push({ request, diff: restrictionsMatch.diff });
558
578
  }
@@ -686,6 +706,11 @@ var HttpRequestHandlerClient = class {
686
706
  saveInterceptedRequest(request, response) {
687
707
  const interceptedRequest = this.createInterceptedRequest(request, response);
688
708
  this._requests.push(interceptedRequest);
709
+ this.interceptor.incrementNumberOfSavedRequests(1);
710
+ }
711
+ clearInterceptedRequests() {
712
+ this.interceptor.incrementNumberOfSavedRequests(-this._requests.length);
713
+ this._requests.length = 0;
689
714
  }
690
715
  createInterceptedRequest(request, response) {
691
716
  const interceptedRequest = request;
@@ -698,7 +723,7 @@ var HttpRequestHandlerClient = class {
698
723
  return interceptedRequest;
699
724
  }
700
725
  get requests() {
701
- if (!this.interceptor.saveRequests) {
726
+ if (!this.interceptor.requestSaving.enabled) {
702
727
  throw new DisabledRequestSavingError_default();
703
728
  }
704
729
  return this._requests;
@@ -1486,6 +1511,7 @@ var RemoteHttpRequestHandler_default = RemoteHttpRequestHandler;
1486
1511
 
1487
1512
  // src/http/interceptor/HttpInterceptorClient.ts
1488
1513
  var SUPPORTED_BASE_URL_PROTOCOLS = Object.freeze(["http", "https"]);
1514
+ var DEFAULT_REQUEST_SAVING_SAFE_LIMIT = 1e3;
1489
1515
  var HttpInterceptorClient = class {
1490
1516
  static {
1491
1517
  __name(this, "HttpInterceptorClient");
@@ -1493,7 +1519,8 @@ var HttpInterceptorClient = class {
1493
1519
  worker;
1494
1520
  store;
1495
1521
  _baseURL;
1496
- _saveRequests;
1522
+ requestSaving;
1523
+ numberOfSavedRequests = 0;
1497
1524
  onUnhandledRequest;
1498
1525
  isRunning = false;
1499
1526
  Handler;
@@ -1510,10 +1537,16 @@ var HttpInterceptorClient = class {
1510
1537
  this.worker = options.worker;
1511
1538
  this.store = options.store;
1512
1539
  this.baseURL = options.baseURL;
1513
- this._saveRequests = options.saveRequests;
1540
+ this.requestSaving = {
1541
+ enabled: options.requestSaving?.enabled ?? this.getDefaultRequestSavingEnabled(),
1542
+ safeLimit: options.requestSaving?.safeLimit ?? DEFAULT_REQUEST_SAVING_SAFE_LIMIT
1543
+ };
1514
1544
  this.onUnhandledRequest = options.onUnhandledRequest;
1515
1545
  this.Handler = options.Handler;
1516
1546
  }
1547
+ getDefaultRequestSavingEnabled() {
1548
+ return isServerSide() ? process.env.NODE_ENV === "test" : false;
1549
+ }
1517
1550
  get baseURL() {
1518
1551
  return this._baseURL;
1519
1552
  }
@@ -1533,15 +1566,6 @@ var HttpInterceptorClient = class {
1533
1566
  }
1534
1567
  return this.baseURL.href;
1535
1568
  }
1536
- get saveRequests() {
1537
- if (this._saveRequests === void 0) {
1538
- return isServerSide() ? process.env.NODE_ENV === "test" : false;
1539
- }
1540
- return this._saveRequests;
1541
- }
1542
- set saveRequests(saveRequests) {
1543
- this._saveRequests = saveRequests;
1544
- }
1545
1569
  get platform() {
1546
1570
  return this.worker.platform;
1547
1571
  }
@@ -1639,13 +1663,21 @@ var HttpInterceptorClient = class {
1639
1663
  }
1640
1664
  const responseDeclaration = await matchedHandler.applyResponseDeclaration(parsedRequest);
1641
1665
  const response = HttpInterceptorWorker_default.createResponseFromDeclaration(request, responseDeclaration);
1642
- if (this.saveRequests) {
1666
+ if (this.requestSaving.enabled) {
1643
1667
  const responseClone = response.clone();
1644
1668
  const parsedResponse = await HttpInterceptorWorker_default.parseRawResponse(responseClone);
1645
1669
  matchedHandler.saveInterceptedRequest(parsedRequest, parsedResponse);
1646
1670
  }
1647
1671
  return response;
1648
1672
  }
1673
+ incrementNumberOfSavedRequests(increment) {
1674
+ this.numberOfSavedRequests = Math.max(this.numberOfSavedRequests + increment, 0);
1675
+ const exceedsSafeLimit = this.numberOfSavedRequests > this.requestSaving.safeLimit;
1676
+ if (increment > 0 && exceedsSafeLimit) {
1677
+ const error = new RequestSavingSafeLimitExceededError_default(this.numberOfSavedRequests, this.requestSaving.safeLimit);
1678
+ console.warn(error);
1679
+ }
1680
+ }
1649
1681
  async findMatchedHandler(method, path, parsedRequest) {
1650
1682
  const handlersByPath = this.handlerClientsByMethod[method].get(path) ?? [];
1651
1683
  for (let handlerIndex = handlersByPath.length - 1; handlerIndex >= 0; handlerIndex--) {
@@ -2394,7 +2426,7 @@ var LocalHttpInterceptor = class {
2394
2426
  baseURL,
2395
2427
  Handler: LocalHttpRequestHandler_default,
2396
2428
  onUnhandledRequest: options.onUnhandledRequest,
2397
- saveRequests: options.saveRequests
2429
+ requestSaving: options.requestSaving
2398
2430
  });
2399
2431
  }
2400
2432
  get type() {
@@ -2406,11 +2438,11 @@ var LocalHttpInterceptor = class {
2406
2438
  set baseURL(baseURL) {
2407
2439
  this.client.baseURL = new URL(baseURL);
2408
2440
  }
2409
- get saveRequests() {
2410
- return this.client.saveRequests;
2441
+ get requestSaving() {
2442
+ return this.client.requestSaving;
2411
2443
  }
2412
- set saveRequests(saveRequests) {
2413
- this.client.saveRequests = saveRequests;
2444
+ set requestSaving(requestSaving) {
2445
+ this.client.requestSaving = requestSaving;
2414
2446
  }
2415
2447
  get onUnhandledRequest() {
2416
2448
  return this.client.onUnhandledRequest;
@@ -2484,7 +2516,7 @@ var RemoteHttpInterceptor = class {
2484
2516
  baseURL,
2485
2517
  Handler: RemoteHttpRequestHandler_default,
2486
2518
  onUnhandledRequest: options.onUnhandledRequest,
2487
- saveRequests: options.saveRequests
2519
+ requestSaving: options.requestSaving
2488
2520
  });
2489
2521
  }
2490
2522
  get type() {
@@ -2496,11 +2528,11 @@ var RemoteHttpInterceptor = class {
2496
2528
  set baseURL(baseURL) {
2497
2529
  this.client.baseURL = new URL(baseURL);
2498
2530
  }
2499
- get saveRequests() {
2500
- return this.client.saveRequests;
2531
+ get requestSaving() {
2532
+ return this.client.requestSaving;
2501
2533
  }
2502
- set saveRequests(saveRequests) {
2503
- this.client.saveRequests = saveRequests;
2534
+ set requestSaving(requestSaving) {
2535
+ this.client.requestSaving = requestSaving;
2504
2536
  }
2505
2537
  get onUnhandledRequest() {
2506
2538
  return this.client.onUnhandledRequest;
@@ -2607,6 +2639,7 @@ exports.DisabledRequestSavingError = DisabledRequestSavingError_default;
2607
2639
  exports.InvalidFormDataError = InvalidFormDataError_default;
2608
2640
  exports.InvalidJSONError = InvalidJSONError_default;
2609
2641
  exports.NotRunningHttpInterceptorError = NotRunningHttpInterceptorError_default;
2642
+ exports.RequestSavingSafeLimitExceededError = RequestSavingSafeLimitExceededError_default;
2610
2643
  exports.RunningHttpInterceptorError = RunningHttpInterceptorError_default;
2611
2644
  exports.TimesCheckError = TimesCheckError_default;
2612
2645
  exports.UnknownHttpInterceptorPlatformError = UnknownHttpInterceptorPlatformError_default;