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

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
@@ -142,7 +142,7 @@ var DisabledRequestSavingError = class extends TypeError {
142
142
  }
143
143
  constructor() {
144
144
  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"
145
+ "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
146
  );
147
147
  this.name = "DisabledRequestSavingError";
148
148
  }
@@ -286,9 +286,9 @@ Requests evaluated by this handler:
286
286
  ].filter((part) => part !== false).join("");
287
287
  }
288
288
  __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.";
289
+ function createMessageDiffs({ requestSaving, unmatchedRequestGroups }) {
290
+ if (!requestSaving.enabled) {
291
+ return "Tip: use `requestSaving.enabled: true` in your interceptor for more details about the unmatched requests.";
292
292
  }
293
293
  return unmatchedRequestGroups.map(({ request, diff }, index) => {
294
294
  const requestNumber = index + 1;
@@ -503,7 +503,7 @@ var HttpRequestHandlerClient = class {
503
503
  newThis.createResponseDeclaration = this.isResponseDeclarationFactory(declaration) ? declaration : () => declaration;
504
504
  newThis.numberOfMatchedRequests = 0;
505
505
  newThis.unmatchedRequestGroups.length = 0;
506
- newThis._requests.length = 0;
506
+ newThis.clearInterceptedRequests();
507
507
  this.interceptor.registerRequestHandler(this.handler);
508
508
  return newThis;
509
509
  }
@@ -527,7 +527,7 @@ var HttpRequestHandlerClient = class {
527
527
  declarationPointer: this.timesDeclarationPointer,
528
528
  unmatchedRequestGroups: this.unmatchedRequestGroups,
529
529
  hasRestrictions: this.restrictions.length > 0,
530
- hasSavedRequests: this.interceptor.saveRequests
530
+ requestSaving: this.interceptor.requestSaving
531
531
  });
532
532
  }
533
533
  }
@@ -539,7 +539,7 @@ var HttpRequestHandlerClient = class {
539
539
  this.timesDeclarationPointer = void 0;
540
540
  this.numberOfMatchedRequests = 0;
541
541
  this.unmatchedRequestGroups.length = 0;
542
- this._requests.length = 0;
542
+ this.clearInterceptedRequests();
543
543
  this.createResponseDeclaration = void 0;
544
544
  return this;
545
545
  }
@@ -552,7 +552,7 @@ var HttpRequestHandlerClient = class {
552
552
  if (restrictionsMatch.value) {
553
553
  this.numberOfMatchedRequests++;
554
554
  } else {
555
- const shouldSaveUnmatchedGroup = this.interceptor.saveRequests && this.restrictions.length > 0 && this.timesDeclarationPointer !== void 0;
555
+ const shouldSaveUnmatchedGroup = this.interceptor.requestSaving.enabled && this.restrictions.length > 0 && this.timesDeclarationPointer !== void 0;
556
556
  if (shouldSaveUnmatchedGroup) {
557
557
  this.unmatchedRequestGroups.push({ request, diff: restrictionsMatch.diff });
558
558
  }
@@ -686,6 +686,11 @@ var HttpRequestHandlerClient = class {
686
686
  saveInterceptedRequest(request, response) {
687
687
  const interceptedRequest = this.createInterceptedRequest(request, response);
688
688
  this._requests.push(interceptedRequest);
689
+ this.interceptor.incrementNumberOfSavedRequests(1);
690
+ }
691
+ clearInterceptedRequests() {
692
+ this.interceptor.incrementNumberOfSavedRequests(-this._requests.length);
693
+ this._requests.length = 0;
689
694
  }
690
695
  createInterceptedRequest(request, response) {
691
696
  const interceptedRequest = request;
@@ -698,7 +703,7 @@ var HttpRequestHandlerClient = class {
698
703
  return interceptedRequest;
699
704
  }
700
705
  get requests() {
701
- if (!this.interceptor.saveRequests) {
706
+ if (!this.interceptor.requestSaving.enabled) {
702
707
  throw new DisabledRequestSavingError_default();
703
708
  }
704
709
  return this._requests;
@@ -1484,8 +1489,29 @@ var RemoteHttpRequestHandler = class {
1484
1489
  };
1485
1490
  var RemoteHttpRequestHandler_default = RemoteHttpRequestHandler;
1486
1491
 
1492
+ // src/http/interceptor/errors/RequestSavingSafeLimitExceededError.ts
1493
+ var RequestSavingSafeLimitExceededError = class extends TypeError {
1494
+ static {
1495
+ __name(this, "RequestSavingSafeLimitExceededError");
1496
+ }
1497
+ constructor(numberOfSavedRequests, safeLimit) {
1498
+ super(
1499
+ `The number of intercepted requests saved in memory (${numberOfSavedRequests}) exceeded the safe limit of ${safeLimit}. Did you forget to call \`interceptor.clear()\`?
1500
+
1501
+ 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.
1502
+
1503
+ If you do not need to save requests, consider setting \`requestSaving.enabled: false\` in your interceptor.
1504
+
1505
+ Learn more: https://github.com/zimicjs/zimic/wiki/api\u2010zimic\u2010interceptor\u2010http#saving-requests`
1506
+ );
1507
+ this.name = "RequestSavingSafeLimitExceededError";
1508
+ }
1509
+ };
1510
+ var RequestSavingSafeLimitExceededError_default = RequestSavingSafeLimitExceededError;
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,7 +1537,10 @@ 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 ?? (isServerSide() ? process.env.NODE_ENV === "test" : false),
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
  }
@@ -1533,15 +1563,6 @@ var HttpInterceptorClient = class {
1533
1563
  }
1534
1564
  return this.baseURL.href;
1535
1565
  }
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
1566
  get platform() {
1546
1567
  return this.worker.platform;
1547
1568
  }
@@ -1639,13 +1660,21 @@ var HttpInterceptorClient = class {
1639
1660
  }
1640
1661
  const responseDeclaration = await matchedHandler.applyResponseDeclaration(parsedRequest);
1641
1662
  const response = HttpInterceptorWorker_default.createResponseFromDeclaration(request, responseDeclaration);
1642
- if (this.saveRequests) {
1663
+ if (this.requestSaving.enabled) {
1643
1664
  const responseClone = response.clone();
1644
1665
  const parsedResponse = await HttpInterceptorWorker_default.parseRawResponse(responseClone);
1645
1666
  matchedHandler.saveInterceptedRequest(parsedRequest, parsedResponse);
1646
1667
  }
1647
1668
  return response;
1648
1669
  }
1670
+ incrementNumberOfSavedRequests(increment) {
1671
+ this.numberOfSavedRequests = Math.max(this.numberOfSavedRequests + increment, 0);
1672
+ const exceedsSafeLimit = this.numberOfSavedRequests > this.requestSaving.safeLimit;
1673
+ if (increment > 0 && exceedsSafeLimit) {
1674
+ const error = new RequestSavingSafeLimitExceededError_default(this.numberOfSavedRequests, this.requestSaving.safeLimit);
1675
+ console.warn(error);
1676
+ }
1677
+ }
1649
1678
  async findMatchedHandler(method, path, parsedRequest) {
1650
1679
  const handlersByPath = this.handlerClientsByMethod[method].get(path) ?? [];
1651
1680
  for (let handlerIndex = handlersByPath.length - 1; handlerIndex >= 0; handlerIndex--) {
@@ -2394,7 +2423,7 @@ var LocalHttpInterceptor = class {
2394
2423
  baseURL,
2395
2424
  Handler: LocalHttpRequestHandler_default,
2396
2425
  onUnhandledRequest: options.onUnhandledRequest,
2397
- saveRequests: options.saveRequests
2426
+ requestSaving: options.requestSaving
2398
2427
  });
2399
2428
  }
2400
2429
  get type() {
@@ -2406,11 +2435,11 @@ var LocalHttpInterceptor = class {
2406
2435
  set baseURL(baseURL) {
2407
2436
  this.client.baseURL = new URL(baseURL);
2408
2437
  }
2409
- get saveRequests() {
2410
- return this.client.saveRequests;
2438
+ get requestSaving() {
2439
+ return this.client.requestSaving;
2411
2440
  }
2412
- set saveRequests(saveRequests) {
2413
- this.client.saveRequests = saveRequests;
2441
+ set requestSaving(requestSaving) {
2442
+ this.client.requestSaving = requestSaving;
2414
2443
  }
2415
2444
  get onUnhandledRequest() {
2416
2445
  return this.client.onUnhandledRequest;
@@ -2484,7 +2513,7 @@ var RemoteHttpInterceptor = class {
2484
2513
  baseURL,
2485
2514
  Handler: RemoteHttpRequestHandler_default,
2486
2515
  onUnhandledRequest: options.onUnhandledRequest,
2487
- saveRequests: options.saveRequests
2516
+ requestSaving: options.requestSaving
2488
2517
  });
2489
2518
  }
2490
2519
  get type() {
@@ -2496,11 +2525,11 @@ var RemoteHttpInterceptor = class {
2496
2525
  set baseURL(baseURL) {
2497
2526
  this.client.baseURL = new URL(baseURL);
2498
2527
  }
2499
- get saveRequests() {
2500
- return this.client.saveRequests;
2528
+ get requestSaving() {
2529
+ return this.client.requestSaving;
2501
2530
  }
2502
- set saveRequests(saveRequests) {
2503
- this.client.saveRequests = saveRequests;
2531
+ set requestSaving(requestSaving) {
2532
+ this.client.requestSaving = requestSaving;
2504
2533
  }
2505
2534
  get onUnhandledRequest() {
2506
2535
  return this.client.onUnhandledRequest;