@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.mjs CHANGED
@@ -8,30 +8,6 @@ import ClientSocket from 'isomorphic-ws';
8
8
  var __defProp = Object.defineProperty;
9
9
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
10
10
 
11
- // src/http/interceptorWorker/errors/InvalidJSONError.ts
12
- var InvalidJSONError = class extends SyntaxError {
13
- static {
14
- __name(this, "InvalidJSONError");
15
- }
16
- constructor(value) {
17
- super(`Failed to parse value as JSON: ${value}`);
18
- this.name = "InvalidJSONError";
19
- }
20
- };
21
- var InvalidJSONError_default = InvalidJSONError;
22
-
23
- // src/http/interceptorWorker/errors/InvalidFormDataError.ts
24
- var InvalidFormDataError = class extends SyntaxError {
25
- static {
26
- __name(this, "InvalidFormDataError");
27
- }
28
- constructor(value) {
29
- super(`Failed to parse value as form data: ${value}`);
30
- this.name = "InvalidFormDataError";
31
- }
32
- };
33
- var InvalidFormDataError_default = InvalidFormDataError;
34
-
35
11
  // src/http/interceptor/errors/RunningHttpInterceptorError.ts
36
12
  var RunningHttpInterceptorError = class extends Error {
37
13
  static {
@@ -84,6 +60,50 @@ var UnknownHttpInterceptorTypeError = class extends TypeError {
84
60
  };
85
61
  var UnknownHttpInterceptorTypeError_default = UnknownHttpInterceptorTypeError;
86
62
 
63
+ // src/http/interceptor/errors/RequestSavingSafeLimitExceededError.ts
64
+ var RequestSavingSafeLimitExceededError = class extends TypeError {
65
+ static {
66
+ __name(this, "RequestSavingSafeLimitExceededError");
67
+ }
68
+ constructor(numberOfSavedRequests, safeLimit) {
69
+ super(
70
+ `The number of intercepted requests saved in memory (${numberOfSavedRequests}) exceeded the safe limit of ${safeLimit}. Did you forget to call \`interceptor.clear()\`?
71
+
72
+ 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.
73
+
74
+ If you do not need to save requests, consider setting \`requestSaving.enabled: false\` in your interceptor.
75
+
76
+ Learn more: https://github.com/zimicjs/zimic/wiki/api\u2010zimic\u2010interceptor\u2010http#saving-requests`
77
+ );
78
+ this.name = "RequestSavingSafeLimitExceededError";
79
+ }
80
+ };
81
+ var RequestSavingSafeLimitExceededError_default = RequestSavingSafeLimitExceededError;
82
+
83
+ // src/http/interceptorWorker/errors/InvalidFormDataError.ts
84
+ var InvalidFormDataError = class extends SyntaxError {
85
+ static {
86
+ __name(this, "InvalidFormDataError");
87
+ }
88
+ constructor(value) {
89
+ super(`Failed to parse value as form data: ${value}`);
90
+ this.name = "InvalidFormDataError";
91
+ }
92
+ };
93
+ var InvalidFormDataError_default = InvalidFormDataError;
94
+
95
+ // src/http/interceptorWorker/errors/InvalidJSONError.ts
96
+ var InvalidJSONError = class extends SyntaxError {
97
+ static {
98
+ __name(this, "InvalidJSONError");
99
+ }
100
+ constructor(value) {
101
+ super(`Failed to parse value as JSON: ${value}`);
102
+ this.name = "InvalidJSONError";
103
+ }
104
+ };
105
+ var InvalidJSONError_default = InvalidJSONError;
106
+
87
107
  // src/cli/browser/shared/constants.ts
88
108
  var SERVICE_WORKER_FILE_NAME = "mockServiceWorker.js";
89
109
 
@@ -115,7 +135,7 @@ var DisabledRequestSavingError = class extends TypeError {
115
135
  }
116
136
  constructor() {
117
137
  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"
138
+ "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
139
  );
120
140
  this.name = "DisabledRequestSavingError";
121
141
  }
@@ -259,9 +279,9 @@ Requests evaluated by this handler:
259
279
  ].filter((part) => part !== false).join("");
260
280
  }
261
281
  __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.";
282
+ function createMessageDiffs({ requestSaving, unmatchedRequestGroups }) {
283
+ if (!requestSaving.enabled) {
284
+ return "Tip: use `requestSaving.enabled: true` in your interceptor for more details about the unmatched requests.";
265
285
  }
266
286
  return unmatchedRequestGroups.map(({ request, diff }, index) => {
267
287
  const requestNumber = index + 1;
@@ -476,7 +496,7 @@ var HttpRequestHandlerClient = class {
476
496
  newThis.createResponseDeclaration = this.isResponseDeclarationFactory(declaration) ? declaration : () => declaration;
477
497
  newThis.numberOfMatchedRequests = 0;
478
498
  newThis.unmatchedRequestGroups.length = 0;
479
- newThis._requests.length = 0;
499
+ newThis.clearInterceptedRequests();
480
500
  this.interceptor.registerRequestHandler(this.handler);
481
501
  return newThis;
482
502
  }
@@ -500,7 +520,7 @@ var HttpRequestHandlerClient = class {
500
520
  declarationPointer: this.timesDeclarationPointer,
501
521
  unmatchedRequestGroups: this.unmatchedRequestGroups,
502
522
  hasRestrictions: this.restrictions.length > 0,
503
- hasSavedRequests: this.interceptor.saveRequests
523
+ requestSaving: this.interceptor.requestSaving
504
524
  });
505
525
  }
506
526
  }
@@ -512,7 +532,7 @@ var HttpRequestHandlerClient = class {
512
532
  this.timesDeclarationPointer = void 0;
513
533
  this.numberOfMatchedRequests = 0;
514
534
  this.unmatchedRequestGroups.length = 0;
515
- this._requests.length = 0;
535
+ this.clearInterceptedRequests();
516
536
  this.createResponseDeclaration = void 0;
517
537
  return this;
518
538
  }
@@ -525,7 +545,7 @@ var HttpRequestHandlerClient = class {
525
545
  if (restrictionsMatch.value) {
526
546
  this.numberOfMatchedRequests++;
527
547
  } else {
528
- const shouldSaveUnmatchedGroup = this.interceptor.saveRequests && this.restrictions.length > 0 && this.timesDeclarationPointer !== void 0;
548
+ const shouldSaveUnmatchedGroup = this.interceptor.requestSaving.enabled && this.restrictions.length > 0 && this.timesDeclarationPointer !== void 0;
529
549
  if (shouldSaveUnmatchedGroup) {
530
550
  this.unmatchedRequestGroups.push({ request, diff: restrictionsMatch.diff });
531
551
  }
@@ -659,6 +679,11 @@ var HttpRequestHandlerClient = class {
659
679
  saveInterceptedRequest(request, response) {
660
680
  const interceptedRequest = this.createInterceptedRequest(request, response);
661
681
  this._requests.push(interceptedRequest);
682
+ this.interceptor.incrementNumberOfSavedRequests(1);
683
+ }
684
+ clearInterceptedRequests() {
685
+ this.interceptor.incrementNumberOfSavedRequests(-this._requests.length);
686
+ this._requests.length = 0;
662
687
  }
663
688
  createInterceptedRequest(request, response) {
664
689
  const interceptedRequest = request;
@@ -671,7 +696,7 @@ var HttpRequestHandlerClient = class {
671
696
  return interceptedRequest;
672
697
  }
673
698
  get requests() {
674
- if (!this.interceptor.saveRequests) {
699
+ if (!this.interceptor.requestSaving.enabled) {
675
700
  throw new DisabledRequestSavingError_default();
676
701
  }
677
702
  return this._requests;
@@ -1459,6 +1484,7 @@ var RemoteHttpRequestHandler_default = RemoteHttpRequestHandler;
1459
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,10 +1510,16 @@ 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 ?? this.getDefaultRequestSavingEnabled(),
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
  }
1520
+ getDefaultRequestSavingEnabled() {
1521
+ return isServerSide() ? process.env.NODE_ENV === "test" : false;
1522
+ }
1490
1523
  get baseURL() {
1491
1524
  return this._baseURL;
1492
1525
  }
@@ -1506,15 +1539,6 @@ var HttpInterceptorClient = class {
1506
1539
  }
1507
1540
  return this.baseURL.href;
1508
1541
  }
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
1542
  get platform() {
1519
1543
  return this.worker.platform;
1520
1544
  }
@@ -1612,13 +1636,21 @@ var HttpInterceptorClient = class {
1612
1636
  }
1613
1637
  const responseDeclaration = await matchedHandler.applyResponseDeclaration(parsedRequest);
1614
1638
  const response = HttpInterceptorWorker_default.createResponseFromDeclaration(request, responseDeclaration);
1615
- if (this.saveRequests) {
1639
+ if (this.requestSaving.enabled) {
1616
1640
  const responseClone = response.clone();
1617
1641
  const parsedResponse = await HttpInterceptorWorker_default.parseRawResponse(responseClone);
1618
1642
  matchedHandler.saveInterceptedRequest(parsedRequest, parsedResponse);
1619
1643
  }
1620
1644
  return response;
1621
1645
  }
1646
+ incrementNumberOfSavedRequests(increment) {
1647
+ this.numberOfSavedRequests = Math.max(this.numberOfSavedRequests + increment, 0);
1648
+ const exceedsSafeLimit = this.numberOfSavedRequests > this.requestSaving.safeLimit;
1649
+ if (increment > 0 && exceedsSafeLimit) {
1650
+ const error = new RequestSavingSafeLimitExceededError_default(this.numberOfSavedRequests, this.requestSaving.safeLimit);
1651
+ console.warn(error);
1652
+ }
1653
+ }
1622
1654
  async findMatchedHandler(method, path, parsedRequest) {
1623
1655
  const handlersByPath = this.handlerClientsByMethod[method].get(path) ?? [];
1624
1656
  for (let handlerIndex = handlersByPath.length - 1; handlerIndex >= 0; handlerIndex--) {
@@ -2367,7 +2399,7 @@ var LocalHttpInterceptor = class {
2367
2399
  baseURL,
2368
2400
  Handler: LocalHttpRequestHandler_default,
2369
2401
  onUnhandledRequest: options.onUnhandledRequest,
2370
- saveRequests: options.saveRequests
2402
+ requestSaving: options.requestSaving
2371
2403
  });
2372
2404
  }
2373
2405
  get type() {
@@ -2379,11 +2411,11 @@ var LocalHttpInterceptor = class {
2379
2411
  set baseURL(baseURL) {
2380
2412
  this.client.baseURL = new URL(baseURL);
2381
2413
  }
2382
- get saveRequests() {
2383
- return this.client.saveRequests;
2414
+ get requestSaving() {
2415
+ return this.client.requestSaving;
2384
2416
  }
2385
- set saveRequests(saveRequests) {
2386
- this.client.saveRequests = saveRequests;
2417
+ set requestSaving(requestSaving) {
2418
+ this.client.requestSaving = requestSaving;
2387
2419
  }
2388
2420
  get onUnhandledRequest() {
2389
2421
  return this.client.onUnhandledRequest;
@@ -2457,7 +2489,7 @@ var RemoteHttpInterceptor = class {
2457
2489
  baseURL,
2458
2490
  Handler: RemoteHttpRequestHandler_default,
2459
2491
  onUnhandledRequest: options.onUnhandledRequest,
2460
- saveRequests: options.saveRequests
2492
+ requestSaving: options.requestSaving
2461
2493
  });
2462
2494
  }
2463
2495
  get type() {
@@ -2469,11 +2501,11 @@ var RemoteHttpInterceptor = class {
2469
2501
  set baseURL(baseURL) {
2470
2502
  this.client.baseURL = new URL(baseURL);
2471
2503
  }
2472
- get saveRequests() {
2473
- return this.client.saveRequests;
2504
+ get requestSaving() {
2505
+ return this.client.requestSaving;
2474
2506
  }
2475
- set saveRequests(saveRequests) {
2476
- this.client.saveRequests = saveRequests;
2507
+ set requestSaving(requestSaving) {
2508
+ this.client.requestSaving = requestSaving;
2477
2509
  }
2478
2510
  get onUnhandledRequest() {
2479
2511
  return this.client.onUnhandledRequest;
@@ -2576,6 +2608,6 @@ __name(createHttpInterceptor, "createHttpInterceptor");
2576
2608
  * Reply listeners are always present when notified in normal conditions. If they were not present, the request
2577
2609
  * would reach a timeout and not be responded. The empty set serves as a fallback. */
2578
2610
 
2579
- export { DisabledRequestSavingError_default as DisabledRequestSavingError, InvalidFormDataError_default as InvalidFormDataError, InvalidJSONError_default as InvalidJSONError, NotRunningHttpInterceptorError_default as NotRunningHttpInterceptorError, RunningHttpInterceptorError_default as RunningHttpInterceptorError, TimesCheckError_default as TimesCheckError, UnknownHttpInterceptorPlatformError_default as UnknownHttpInterceptorPlatformError, UnknownHttpInterceptorTypeError_default as UnknownHttpInterceptorTypeError, UnregisteredBrowserServiceWorkerError_default as UnregisteredBrowserServiceWorkerError, createHttpInterceptor };
2611
+ 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 };
2580
2612
  //# sourceMappingURL=http.mjs.map
2581
2613
  //# sourceMappingURL=http.mjs.map