@zimic/interceptor 0.16.0-canary.4 → 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.mjs CHANGED
@@ -115,7 +115,7 @@ var DisabledRequestSavingError = class extends TypeError {
115
115
  }
116
116
  constructor() {
117
117
  super(
118
- "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"
118
+ "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"
119
119
  );
120
120
  this.name = "DisabledRequestSavingError";
121
121
  }
@@ -259,9 +259,9 @@ Requests evaluated by this handler:
259
259
  ].filter((part) => part !== false).join("");
260
260
  }
261
261
  __name(createMessageHeader, "createMessageHeader");
262
- function createMessageDiffs({ hasSavedRequests, unmatchedRequestGroups }) {
263
- if (!hasSavedRequests) {
264
- return "Tip: enable `saveRequests: true` in your interceptor for more details about the unmatched requests.";
262
+ function createMessageDiffs({ requestSaving, unmatchedRequestGroups }) {
263
+ if (!requestSaving.enabled) {
264
+ return "Tip: use `requestSaving.enabled: true` in your interceptor for more details about the unmatched requests.";
265
265
  }
266
266
  return unmatchedRequestGroups.map(({ request, diff }, index) => {
267
267
  const requestNumber = index + 1;
@@ -476,7 +476,7 @@ var HttpRequestHandlerClient = class {
476
476
  newThis.createResponseDeclaration = this.isResponseDeclarationFactory(declaration) ? declaration : () => declaration;
477
477
  newThis.numberOfMatchedRequests = 0;
478
478
  newThis.unmatchedRequestGroups.length = 0;
479
- newThis._requests.length = 0;
479
+ newThis.clearInterceptedRequests();
480
480
  this.interceptor.registerRequestHandler(this.handler);
481
481
  return newThis;
482
482
  }
@@ -500,7 +500,7 @@ var HttpRequestHandlerClient = class {
500
500
  declarationPointer: this.timesDeclarationPointer,
501
501
  unmatchedRequestGroups: this.unmatchedRequestGroups,
502
502
  hasRestrictions: this.restrictions.length > 0,
503
- hasSavedRequests: this.interceptor.saveRequests
503
+ requestSaving: this.interceptor.requestSaving
504
504
  });
505
505
  }
506
506
  }
@@ -512,7 +512,7 @@ var HttpRequestHandlerClient = class {
512
512
  this.timesDeclarationPointer = void 0;
513
513
  this.numberOfMatchedRequests = 0;
514
514
  this.unmatchedRequestGroups.length = 0;
515
- this._requests.length = 0;
515
+ this.clearInterceptedRequests();
516
516
  this.createResponseDeclaration = void 0;
517
517
  return this;
518
518
  }
@@ -525,7 +525,7 @@ var HttpRequestHandlerClient = class {
525
525
  if (restrictionsMatch.value) {
526
526
  this.numberOfMatchedRequests++;
527
527
  } else {
528
- const shouldSaveUnmatchedGroup = this.interceptor.saveRequests && this.restrictions.length > 0 && this.timesDeclarationPointer !== void 0;
528
+ const shouldSaveUnmatchedGroup = this.interceptor.requestSaving.enabled && this.restrictions.length > 0 && this.timesDeclarationPointer !== void 0;
529
529
  if (shouldSaveUnmatchedGroup) {
530
530
  this.unmatchedRequestGroups.push({ request, diff: restrictionsMatch.diff });
531
531
  }
@@ -659,6 +659,11 @@ var HttpRequestHandlerClient = class {
659
659
  saveInterceptedRequest(request, response) {
660
660
  const interceptedRequest = this.createInterceptedRequest(request, response);
661
661
  this._requests.push(interceptedRequest);
662
+ this.interceptor.incrementNumberOfSavedRequests(1);
663
+ }
664
+ clearInterceptedRequests() {
665
+ this.interceptor.incrementNumberOfSavedRequests(-this._requests.length);
666
+ this._requests.length = 0;
662
667
  }
663
668
  createInterceptedRequest(request, response) {
664
669
  const interceptedRequest = request;
@@ -671,7 +676,7 @@ var HttpRequestHandlerClient = class {
671
676
  return interceptedRequest;
672
677
  }
673
678
  get requests() {
674
- if (!this.interceptor.saveRequests) {
679
+ if (!this.interceptor.requestSaving.enabled) {
675
680
  throw new DisabledRequestSavingError_default();
676
681
  }
677
682
  return this._requests;
@@ -1457,8 +1462,29 @@ var RemoteHttpRequestHandler = class {
1457
1462
  };
1458
1463
  var RemoteHttpRequestHandler_default = RemoteHttpRequestHandler;
1459
1464
 
1465
+ // src/http/interceptor/errors/RequestSavingSafeLimitExceededError.ts
1466
+ var RequestSavingSafeLimitExceededError = class extends TypeError {
1467
+ static {
1468
+ __name(this, "RequestSavingSafeLimitExceededError");
1469
+ }
1470
+ constructor(numberOfSavedRequests, safeLimit) {
1471
+ super(
1472
+ `The number of intercepted requests saved in memory (${numberOfSavedRequests}) exceeded the safe limit of ${safeLimit}. Did you forget to call \`interceptor.clear()\`?
1473
+
1474
+ 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.
1475
+
1476
+ If you do not need to save requests, consider setting \`requestSaving.enabled: false\` in your interceptor.
1477
+
1478
+ Learn more: https://github.com/zimicjs/zimic/wiki/api\u2010zimic\u2010interceptor\u2010http#saving-requests`
1479
+ );
1480
+ this.name = "RequestSavingSafeLimitExceededError";
1481
+ }
1482
+ };
1483
+ var RequestSavingSafeLimitExceededError_default = RequestSavingSafeLimitExceededError;
1484
+
1460
1485
  // src/http/interceptor/HttpInterceptorClient.ts
1461
1486
  var SUPPORTED_BASE_URL_PROTOCOLS = Object.freeze(["http", "https"]);
1487
+ var DEFAULT_REQUEST_SAVING_SAFE_LIMIT = 1e3;
1462
1488
  var HttpInterceptorClient = class {
1463
1489
  static {
1464
1490
  __name(this, "HttpInterceptorClient");
@@ -1466,7 +1492,8 @@ var HttpInterceptorClient = class {
1466
1492
  worker;
1467
1493
  store;
1468
1494
  _baseURL;
1469
- _saveRequests;
1495
+ requestSaving;
1496
+ numberOfSavedRequests = 0;
1470
1497
  onUnhandledRequest;
1471
1498
  isRunning = false;
1472
1499
  Handler;
@@ -1483,7 +1510,10 @@ var HttpInterceptorClient = class {
1483
1510
  this.worker = options.worker;
1484
1511
  this.store = options.store;
1485
1512
  this.baseURL = options.baseURL;
1486
- this._saveRequests = options.saveRequests;
1513
+ this.requestSaving = {
1514
+ enabled: options.requestSaving?.enabled ?? (isServerSide() ? process.env.NODE_ENV === "test" : false),
1515
+ safeLimit: options.requestSaving?.safeLimit ?? DEFAULT_REQUEST_SAVING_SAFE_LIMIT
1516
+ };
1487
1517
  this.onUnhandledRequest = options.onUnhandledRequest;
1488
1518
  this.Handler = options.Handler;
1489
1519
  }
@@ -1506,15 +1536,6 @@ var HttpInterceptorClient = class {
1506
1536
  }
1507
1537
  return this.baseURL.href;
1508
1538
  }
1509
- get saveRequests() {
1510
- if (this._saveRequests === void 0) {
1511
- return isServerSide() ? process.env.NODE_ENV === "test" : false;
1512
- }
1513
- return this._saveRequests;
1514
- }
1515
- set saveRequests(saveRequests) {
1516
- this._saveRequests = saveRequests;
1517
- }
1518
1539
  get platform() {
1519
1540
  return this.worker.platform;
1520
1541
  }
@@ -1612,13 +1633,21 @@ var HttpInterceptorClient = class {
1612
1633
  }
1613
1634
  const responseDeclaration = await matchedHandler.applyResponseDeclaration(parsedRequest);
1614
1635
  const response = HttpInterceptorWorker_default.createResponseFromDeclaration(request, responseDeclaration);
1615
- if (this.saveRequests) {
1636
+ if (this.requestSaving.enabled) {
1616
1637
  const responseClone = response.clone();
1617
1638
  const parsedResponse = await HttpInterceptorWorker_default.parseRawResponse(responseClone);
1618
1639
  matchedHandler.saveInterceptedRequest(parsedRequest, parsedResponse);
1619
1640
  }
1620
1641
  return response;
1621
1642
  }
1643
+ incrementNumberOfSavedRequests(increment) {
1644
+ this.numberOfSavedRequests = Math.max(this.numberOfSavedRequests + increment, 0);
1645
+ const exceedsSafeLimit = this.numberOfSavedRequests > this.requestSaving.safeLimit;
1646
+ if (increment > 0 && exceedsSafeLimit) {
1647
+ const error = new RequestSavingSafeLimitExceededError_default(this.numberOfSavedRequests, this.requestSaving.safeLimit);
1648
+ console.warn(error);
1649
+ }
1650
+ }
1622
1651
  async findMatchedHandler(method, path, parsedRequest) {
1623
1652
  const handlersByPath = this.handlerClientsByMethod[method].get(path) ?? [];
1624
1653
  for (let handlerIndex = handlersByPath.length - 1; handlerIndex >= 0; handlerIndex--) {
@@ -2367,7 +2396,7 @@ var LocalHttpInterceptor = class {
2367
2396
  baseURL,
2368
2397
  Handler: LocalHttpRequestHandler_default,
2369
2398
  onUnhandledRequest: options.onUnhandledRequest,
2370
- saveRequests: options.saveRequests
2399
+ requestSaving: options.requestSaving
2371
2400
  });
2372
2401
  }
2373
2402
  get type() {
@@ -2379,11 +2408,11 @@ var LocalHttpInterceptor = class {
2379
2408
  set baseURL(baseURL) {
2380
2409
  this.client.baseURL = new URL(baseURL);
2381
2410
  }
2382
- get saveRequests() {
2383
- return this.client.saveRequests;
2411
+ get requestSaving() {
2412
+ return this.client.requestSaving;
2384
2413
  }
2385
- set saveRequests(saveRequests) {
2386
- this.client.saveRequests = saveRequests;
2414
+ set requestSaving(requestSaving) {
2415
+ this.client.requestSaving = requestSaving;
2387
2416
  }
2388
2417
  get onUnhandledRequest() {
2389
2418
  return this.client.onUnhandledRequest;
@@ -2457,7 +2486,7 @@ var RemoteHttpInterceptor = class {
2457
2486
  baseURL,
2458
2487
  Handler: RemoteHttpRequestHandler_default,
2459
2488
  onUnhandledRequest: options.onUnhandledRequest,
2460
- saveRequests: options.saveRequests
2489
+ requestSaving: options.requestSaving
2461
2490
  });
2462
2491
  }
2463
2492
  get type() {
@@ -2469,11 +2498,11 @@ var RemoteHttpInterceptor = class {
2469
2498
  set baseURL(baseURL) {
2470
2499
  this.client.baseURL = new URL(baseURL);
2471
2500
  }
2472
- get saveRequests() {
2473
- return this.client.saveRequests;
2501
+ get requestSaving() {
2502
+ return this.client.requestSaving;
2474
2503
  }
2475
- set saveRequests(saveRequests) {
2476
- this.client.saveRequests = saveRequests;
2504
+ set requestSaving(requestSaving) {
2505
+ this.client.requestSaving = requestSaving;
2477
2506
  }
2478
2507
  get onUnhandledRequest() {
2479
2508
  return this.client.onUnhandledRequest;