@zimic/interceptor 0.14.0-canary.17 → 0.14.0-canary.20

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.
Files changed (55) hide show
  1. package/dist/{chunk-3XWHITKR.mjs → chunk-5ESJW32X.mjs} +90 -165
  2. package/dist/chunk-5ESJW32X.mjs.map +1 -0
  3. package/dist/{chunk-SWLQ2IF6.js → chunk-JIUQ7G6N.js} +90 -165
  4. package/dist/chunk-JIUQ7G6N.js.map +1 -0
  5. package/dist/cli.js +7 -7
  6. package/dist/cli.js.map +1 -1
  7. package/dist/cli.mjs +3 -3
  8. package/dist/cli.mjs.map +1 -1
  9. package/dist/http.d.ts +1 -12
  10. package/dist/http.js +182 -180
  11. package/dist/http.js.map +1 -1
  12. package/dist/http.mjs +182 -180
  13. package/dist/http.mjs.map +1 -1
  14. package/dist/scripts/postinstall.js.map +1 -1
  15. package/dist/scripts/postinstall.mjs.map +1 -1
  16. package/dist/server.js +5 -5
  17. package/dist/server.mjs +1 -1
  18. package/package.json +4 -3
  19. package/src/http/interceptor/HttpInterceptorClient.ts +13 -7
  20. package/src/http/interceptor/HttpInterceptorStore.ts +4 -6
  21. package/src/http/interceptor/LocalHttpInterceptor.ts +6 -7
  22. package/src/http/interceptor/RemoteHttpInterceptor.ts +7 -11
  23. package/src/http/interceptor/types/options.ts +1 -1
  24. package/src/http/interceptor/types/requests.ts +1 -2
  25. package/src/http/interceptorWorker/HttpInterceptorWorker.ts +4 -5
  26. package/src/http/interceptorWorker/LocalHttpInterceptorWorker.ts +5 -5
  27. package/src/http/interceptorWorker/RemoteHttpInterceptorWorker.ts +8 -8
  28. package/src/http/interceptorWorker/types/options.ts +1 -3
  29. package/src/http/interceptorWorker/types/requests.ts +1 -2
  30. package/src/http/requestHandler/HttpRequestHandlerClient.ts +3 -2
  31. package/src/http/requestHandler/LocalHttpRequestHandler.ts +1 -2
  32. package/src/http/requestHandler/RemoteHttpRequestHandler.ts +1 -2
  33. package/src/http/requestHandler/errors/TimesCheckError.ts +2 -2
  34. package/src/http/requestHandler/types/public.ts +1 -2
  35. package/src/http/requestHandler/types/requests.ts +1 -2
  36. package/src/http/requestHandler/types/restrictions.ts +1 -2
  37. package/src/server/InterceptorServer.ts +6 -4
  38. package/src/utils/console.ts +3 -8
  39. package/src/utils/crypto.ts +1 -1
  40. package/src/utils/data.ts +0 -20
  41. package/src/utils/fetch.ts +0 -20
  42. package/src/utils/files.ts +5 -18
  43. package/src/utils/processes.ts +1 -1
  44. package/src/webSocket/WebSocketClient.ts +3 -5
  45. package/src/webSocket/WebSocketHandler.ts +1 -1
  46. package/src/webSocket/types.ts +1 -2
  47. package/dist/chunk-3XWHITKR.mjs.map +0 -1
  48. package/dist/chunk-SWLQ2IF6.js.map +0 -1
  49. package/src/types/arrays.d.ts +0 -4
  50. package/src/types/objects.d.ts +0 -14
  51. package/src/types/strings.d.ts +0 -9
  52. package/src/types/utils.ts +0 -28
  53. package/src/utils/imports.ts +0 -12
  54. package/src/utils/time.ts +0 -5
  55. package/src/utils/urls.ts +0 -138
@@ -5,6 +5,41 @@ import { HttpHeaders, HttpSearchParams, HttpFormData, HTTP_METHODS, HTTP_METHODS
5
5
  import chalk2 from 'chalk';
6
6
  import ClientSocket from 'isomorphic-ws';
7
7
 
8
+ // ../zimic-utils/dist/chunk-PAWJFY3S.mjs
9
+ var __defProp = Object.defineProperty;
10
+ var __name2 = /* @__PURE__ */ __name((target, value) => __defProp(target, "name", { value, configurable: true }), "__name");
11
+
12
+ // ../zimic-utils/dist/chunk-7MZYQWWC.mjs
13
+ var URL_PATH_PARAM_REGEX = /\/:([^/]+)/g;
14
+ function createRegExpFromURL(url) {
15
+ URL_PATH_PARAM_REGEX.lastIndex = 0;
16
+ const urlWithReplacedPathParams = encodeURI(url).replace(/([.()*?+$\\])/g, "\\$1").replace(URL_PATH_PARAM_REGEX, "/(?<$1>[^/]+)").replace(/(\/+)$/, "(?:/+)?");
17
+ return new RegExp(`^${urlWithReplacedPathParams}$`);
18
+ }
19
+ __name(createRegExpFromURL, "createRegExpFromURL");
20
+ __name2(createRegExpFromURL, "createRegExpFromURL");
21
+ var createRegExpFromURL_default = createRegExpFromURL;
22
+
23
+ // ../zimic-utils/dist/url/excludeURLParams.mjs
24
+ function excludeURLParams(url) {
25
+ url.hash = "";
26
+ url.search = "";
27
+ url.username = "";
28
+ url.password = "";
29
+ return url;
30
+ }
31
+ __name(excludeURLParams, "excludeURLParams");
32
+ __name2(excludeURLParams, "excludeURLParams");
33
+ var excludeURLParams_default = excludeURLParams;
34
+
35
+ // ../zimic-utils/dist/chunk-3O5CS47X.mjs
36
+ function isDefined(value) {
37
+ return value !== undefined && value !== null;
38
+ }
39
+ __name(isDefined, "isDefined");
40
+ __name2(isDefined, "isDefined");
41
+ var isDefined_default = isDefined;
42
+
8
43
  // src/utils/arrays.ts
9
44
  function removeArrayIndex(array, index) {
10
45
  if (index >= 0 && index < array.length) {
@@ -19,42 +54,36 @@ function removeArrayElement(array, element) {
19
54
  }
20
55
  __name(removeArrayElement, "removeArrayElement");
21
56
 
22
- // src/utils/environment.ts
23
- function isClientSide() {
24
- return typeof window !== "undefined";
25
- }
26
- __name(isClientSide, "isClientSide");
27
-
28
- // src/utils/imports.ts
57
+ // ../zimic-utils/dist/import/createCachedDynamicImport.mjs
29
58
  function createCachedDynamicImport(importModuleDynamically) {
30
59
  let cachedImportResult;
31
- return /* @__PURE__ */ __name(async function importModuleDynamicallyWithCache() {
60
+ return /* @__PURE__ */ __name2(/* @__PURE__ */ __name(async function importModuleDynamicallyWithCache() {
32
61
  if (cachedImportResult === undefined) {
33
62
  cachedImportResult = await importModuleDynamically();
34
63
  }
35
64
  return cachedImportResult;
36
- }, "importModuleDynamicallyWithCache");
65
+ }, "importModuleDynamicallyWithCache"), "importModuleDynamicallyWithCache");
37
66
  }
38
67
  __name(createCachedDynamicImport, "createCachedDynamicImport");
68
+ __name2(createCachedDynamicImport, "createCachedDynamicImport");
69
+ var createCachedDynamicImport_default = createCachedDynamicImport;
70
+
71
+ // src/utils/environment.ts
72
+ function isClientSide() {
73
+ return typeof window !== "undefined";
74
+ }
75
+ __name(isClientSide, "isClientSide");
39
76
 
40
77
  // src/utils/files.ts
41
- var importBuffer = createCachedDynamicImport(
78
+ var importFile = createCachedDynamicImport_default(
42
79
  /* istanbul ignore next -- @preserve
43
- * Ignoring as Node.js >=20 provides a global file and the buffer import won't run. */
44
- () => import('buffer')
80
+ * Ignoring as Node.js >=20 provides a global File and the import fallback won't run. */
81
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
82
+ async () => globalThis.File ?? (await import('buffer')).File
45
83
  );
46
- var FileSingleton;
47
- async function importFile() {
48
- if (FileSingleton) {
49
- return FileSingleton;
50
- }
51
- FileSingleton = globalThis.File ?? (await importBuffer()).File;
52
- return FileSingleton;
53
- }
54
- __name(importFile, "importFile");
55
84
 
56
85
  // src/utils/console.ts
57
- var importUtil = createCachedDynamicImport(() => import('util'));
86
+ var importUtil = createCachedDynamicImport_default(() => import('util'));
58
87
  async function formatValueToLog(value, options = {}) {
59
88
  if (isClientSide()) {
60
89
  return value;
@@ -75,40 +104,9 @@ __name(formatValueToLog, "formatValueToLog");
75
104
  function logWithPrefix(messageOrMessages, options = {}) {
76
105
  const { method = "log" } = options;
77
106
  const messages = Array.isArray(messageOrMessages) ? messageOrMessages : [messageOrMessages];
78
- console[method](chalk2.cyan("[zimic]"), ...messages);
107
+ console[method](chalk2.cyan("[@zimic/interceptor]"), ...messages);
79
108
  }
80
109
  __name(logWithPrefix, "logWithPrefix");
81
-
82
- // src/utils/data.ts
83
- function convertArrayBufferToBase64(buffer) {
84
- if (isClientSide()) {
85
- const bufferBytes = new Uint8Array(buffer);
86
- const bufferAsStringArray = [];
87
- for (const byte of bufferBytes) {
88
- const byteCode = String.fromCharCode(byte);
89
- bufferAsStringArray.push(byteCode);
90
- }
91
- const bufferAsString = bufferAsStringArray.join("");
92
- return btoa(bufferAsString);
93
- } else {
94
- return Buffer.from(buffer).toString("base64");
95
- }
96
- }
97
- __name(convertArrayBufferToBase64, "convertArrayBufferToBase64");
98
- function convertBase64ToArrayBuffer(base64Value) {
99
- if (isClientSide()) {
100
- const bufferAsString = atob(base64Value);
101
- const array = Uint8Array.from(bufferAsString, (character) => character.charCodeAt(0));
102
- return array.buffer;
103
- } else {
104
- return Buffer.from(base64Value, "base64");
105
- }
106
- }
107
- __name(convertBase64ToArrayBuffer, "convertBase64ToArrayBuffer");
108
- function isDefined(value) {
109
- return value !== undefined && value !== null;
110
- }
111
- __name(isDefined, "isDefined");
112
110
  var HttpServerTimeoutError = class extends Error {
113
111
  static {
114
112
  __name(this, "HttpServerTimeoutError");
@@ -193,105 +191,6 @@ function methodCanHaveResponseBody(method) {
193
191
  }
194
192
  __name(methodCanHaveResponseBody, "methodCanHaveResponseBody");
195
193
 
196
- // src/utils/urls.ts
197
- var InvalidURLError = class extends TypeError {
198
- static {
199
- __name(this, "InvalidURLError");
200
- }
201
- constructor(url) {
202
- super(`Invalid URL: '${url}'`);
203
- this.name = "InvalidURL";
204
- }
205
- };
206
- var UnsupportedURLProtocolError = class extends TypeError {
207
- static {
208
- __name(this, "UnsupportedURLProtocolError");
209
- }
210
- constructor(protocol, availableProtocols) {
211
- super(
212
- `Unsupported URL protocol: '${protocol}'. The available options are ${availableProtocols.map((protocol2) => `'${protocol2}'`).join(", ")}`
213
- );
214
- this.name = "UnsupportedURLProtocolError";
215
- }
216
- };
217
- function createURLOrThrow(rawURL) {
218
- try {
219
- const url = new URL(rawURL);
220
- Object.defineProperty(url, "raw", {
221
- value: rawURL.toString(),
222
- writable: false,
223
- enumerable: true,
224
- configurable: false
225
- });
226
- return url;
227
- } catch {
228
- throw new InvalidURLError(rawURL);
229
- }
230
- }
231
- __name(createURLOrThrow, "createURLOrThrow");
232
- function excludeNonPathParams(url) {
233
- url.hash = "";
234
- url.search = "";
235
- url.username = "";
236
- url.password = "";
237
- return url;
238
- }
239
- __name(excludeNonPathParams, "excludeNonPathParams");
240
- var DuplicatedPathParamError = class extends Error {
241
- static {
242
- __name(this, "DuplicatedPathParamError");
243
- }
244
- constructor(url, paramName) {
245
- super(
246
- `The path parameter '${paramName}' appears more than once in the URL '${url}'. This is not supported. Please make sure that each parameter is unique.`
247
- );
248
- this.name = "DuplicatedPathParamError";
249
- }
250
- };
251
- var URL_PATH_PARAM_REGEX = /\/:([^/]+)/g;
252
- function ensureUniquePathParams(url) {
253
- const matches = url.matchAll(URL_PATH_PARAM_REGEX);
254
- const uniqueParamNames = /* @__PURE__ */ new Set();
255
- for (const match of matches) {
256
- const paramName = match[1];
257
- if (uniqueParamNames.has(paramName)) {
258
- throw new DuplicatedPathParamError(url, paramName);
259
- }
260
- uniqueParamNames.add(paramName);
261
- }
262
- }
263
- __name(ensureUniquePathParams, "ensureUniquePathParams");
264
- function createURL(rawURL, options = {}) {
265
- const {
266
- protocols,
267
- excludeNonPathParams: shouldExcludeNonPathParams = false,
268
- ensureUniquePathParams: shouldEnsureUniquePathParams = false
269
- } = options;
270
- const url = createURLOrThrow(rawURL);
271
- const protocol = url.protocol.replace(/:$/, "");
272
- if (protocols && !protocols.includes(protocol)) {
273
- throw new UnsupportedURLProtocolError(protocol, protocols);
274
- }
275
- if (shouldExcludeNonPathParams) {
276
- excludeNonPathParams(url);
277
- }
278
- if (shouldEnsureUniquePathParams) {
279
- ensureUniquePathParams(url.toString());
280
- }
281
- return url;
282
- }
283
- __name(createURL, "createURL");
284
- function prepareURLForRegex(url) {
285
- const encodedURL = encodeURI(url);
286
- return encodedURL.replace(/([.()*?+$\\])/g, "\\$1");
287
- }
288
- __name(prepareURLForRegex, "prepareURLForRegex");
289
- function createRegexFromURL(url) {
290
- const urlWithReplacedPathParams = prepareURLForRegex(url).replace(URL_PATH_PARAM_REGEX, "/(?<$1>[^/]+)").replace(/(\/+)$/, "(?:/+)?");
291
- return new RegExp(`^${urlWithReplacedPathParams}$`);
292
- }
293
- __name(createRegexFromURL, "createRegexFromURL");
294
-
295
194
  // src/http/requestHandler/types/requests.ts
296
195
  var HTTP_INTERCEPTOR_REQUEST_HIDDEN_PROPERTIES = Object.freeze(
297
196
  /* @__PURE__ */ new Set([
@@ -461,7 +360,7 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
461
360
  this.getInterceptorUnhandledRequestStrategy(requestClone, interceptor)
462
361
  ]);
463
362
  const candidatesOrPromises = [interceptorStrategy, defaultStrategy, globalDefaultStrategy];
464
- const candidateStrategies = await Promise.all(candidatesOrPromises.filter(isDefined));
363
+ const candidateStrategies = await Promise.all(candidatesOrPromises.filter(isDefined_default));
465
364
  return candidateStrategies;
466
365
  } catch (error) {
467
366
  console.error(error);
@@ -477,7 +376,7 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
477
376
  }
478
377
  findInterceptorByRequestBaseURL(request) {
479
378
  const interceptor = this.runningInterceptors.findLast((interceptor2) => {
480
- const baseURL = interceptor2.baseURL().toString();
379
+ const baseURL = interceptor2.baseURL();
481
380
  return request.url.startsWith(baseURL);
482
381
  });
483
382
  return interceptor;
@@ -524,7 +423,7 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
524
423
  const parsedBody = await this.parseRawBody(rawRequest);
525
424
  const headers = new HttpHeaders(rawRequest.headers);
526
425
  const pathParams = options.urlRegex ? this.parseRawPathParams(options.urlRegex, rawRequest) : {};
527
- const parsedURL = createURL(rawRequest.url);
426
+ const parsedURL = new URL(rawRequest.url);
528
427
  const searchParams = new HttpSearchParams(parsedURL.searchParams);
529
428
  const parsedRequest = new Proxy(rawRequest, {
530
429
  has(target, property) {
@@ -725,6 +624,33 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
725
624
  };
726
625
  var HttpInterceptorWorker_default = HttpInterceptorWorker;
727
626
 
627
+ // src/utils/data.ts
628
+ function convertArrayBufferToBase64(buffer) {
629
+ if (isClientSide()) {
630
+ const bufferBytes = new Uint8Array(buffer);
631
+ const bufferAsStringArray = [];
632
+ for (const byte of bufferBytes) {
633
+ const byteCode = String.fromCharCode(byte);
634
+ bufferAsStringArray.push(byteCode);
635
+ }
636
+ const bufferAsString = bufferAsStringArray.join("");
637
+ return btoa(bufferAsString);
638
+ } else {
639
+ return Buffer.from(buffer).toString("base64");
640
+ }
641
+ }
642
+ __name(convertArrayBufferToBase64, "convertArrayBufferToBase64");
643
+ function convertBase64ToArrayBuffer(base64Value) {
644
+ if (isClientSide()) {
645
+ const bufferAsString = atob(base64Value);
646
+ const array = Uint8Array.from(bufferAsString, (character) => character.charCodeAt(0));
647
+ return array.buffer;
648
+ } else {
649
+ return Buffer.from(base64Value, "base64");
650
+ }
651
+ }
652
+ __name(convertBase64ToArrayBuffer, "convertBase64ToArrayBuffer");
653
+
728
654
  // src/utils/fetch.ts
729
655
  async function serializeRequest(request) {
730
656
  const requestClone = request.clone();
@@ -877,7 +803,7 @@ async function closeServerSocket(socket, options = {}) {
877
803
  __name(closeServerSocket, "closeServerSocket");
878
804
 
879
805
  // src/utils/crypto.ts
880
- var importCrypto = createCachedDynamicImport(async () => {
806
+ var importCrypto = createCachedDynamicImport_default(async () => {
881
807
  const globalCrypto = globalThis.crypto;
882
808
  return globalCrypto ?? await import('crypto');
883
809
  });
@@ -1357,12 +1283,13 @@ var InterceptorServer = class {
1357
1283
  }, "resetWorker");
1358
1284
  registerHttpHandler({ id, url, method }, socket) {
1359
1285
  const handlerGroups = this.httpHandlerGroups[method];
1360
- const fullURL = createURL(url.full, { excludeNonPathParams: true });
1286
+ const fullURL = new URL(url.full);
1287
+ excludeURLParams_default(fullURL);
1361
1288
  handlerGroups.push({
1362
1289
  id,
1363
1290
  url: {
1364
1291
  base: url.base,
1365
- fullRegex: createRegexFromURL(fullURL.toString())
1292
+ fullRegex: createRegExpFromURL_default(fullURL.toString())
1366
1293
  },
1367
1294
  socket
1368
1295
  });
@@ -1436,7 +1363,7 @@ var InterceptorServer = class {
1436
1363
  async createResponseForRequest(request) {
1437
1364
  const webSocketServer = this.webSocketServer();
1438
1365
  const methodHandlers = this.httpHandlerGroups[request.method];
1439
- const requestURL = createURL(request.url, { excludeNonPathParams: true }).toString();
1366
+ const requestURL = excludeURLParams_default(new URL(request.url)).toString();
1440
1367
  let matchedSomeInterceptor = false;
1441
1368
  for (let index = methodHandlers.length - 1; index >= 0; index--) {
1442
1369
  const handler = methodHandlers[index];
@@ -1528,8 +1455,6 @@ var InterceptorServerNamespace_default = InterceptorServerNamespace;
1528
1455
 
1529
1456
  // src/server/index.ts
1530
1457
  var interceptorServer = Object.freeze(new InterceptorServerNamespace_default());
1531
- /* istanbul ignore next -- @preserve
1532
- * Ignoring as Node.js >=20 provides a global File and the import fallback won't run. */
1533
1458
  /* istanbul ignore if -- @preserve
1534
1459
  * This is expected not to happen since the servers are not stopped unless they are running. */
1535
1460
  /* istanbul ignore if -- @preserve
@@ -1556,6 +1481,6 @@ var interceptorServer = Object.freeze(new InterceptorServerNamespace_default());
1556
1481
  * This try..catch is for the case when the remote interceptor web socket client is closed before responding.
1557
1482
  * Since simulating this scenario is difficult, we are ignoring this branch fow now. */
1558
1483
 
1559
- export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotStartedInterceptorServerError_default, createCachedDynamicImport, interceptorServer, logWithPrefix };
1560
- //# sourceMappingURL=chunk-3XWHITKR.mjs.map
1561
- //# sourceMappingURL=chunk-3XWHITKR.mjs.map
1484
+ export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotStartedInterceptorServerError_default, createCachedDynamicImport_default, interceptorServer, logWithPrefix };
1485
+ //# sourceMappingURL=chunk-5ESJW32X.mjs.map
1486
+ //# sourceMappingURL=chunk-5ESJW32X.mjs.map