@zimic/interceptor 1.1.2-canary.3 → 1.1.2-canary.4

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
@@ -856,28 +856,31 @@ var LocalHttpRequestHandler = class {
856
856
  };
857
857
  var LocalHttpRequestHandler_default = LocalHttpRequestHandler;
858
858
 
859
- // ../zimic-utils/dist/chunk-46M3OXFU.mjs
860
- function getExtraPatternsToEscape() {
859
+ // ../zimic-utils/dist/chunk-TYSDS3LQ.mjs
860
+ function createPathCharactersToEscapeRegex() {
861
861
  return /([.(){}+$])/g;
862
862
  }
863
- function getURIEncodedBackSlashPattern() {
864
- return /%5C/g;
863
+ function preparePathForRegex(path) {
864
+ const pathURLPrefix = `data:${path.startsWith("/") ? "" : "/"}`;
865
+ const pathAsURL = new URL(`${pathURLPrefix}${path}`);
866
+ const encodedPath = pathAsURL.href.replace(pathURLPrefix, "");
867
+ return encodedPath.replace(/^\/+/g, "").replace(/\/+$/g, "").replace(createPathCharactersToEscapeRegex(), "\\$1");
865
868
  }
866
- function getPathParamPattern() {
869
+ function createPathParamRegex() {
867
870
  return /(?<escape>\\)?:(?<identifier>[$_\p{ID_Start}][$\p{ID_Continue}]+)/gu;
868
871
  }
869
- function getRepeatingPathParamPattern() {
872
+ function createRepeatingPathParamRegex() {
870
873
  return /(?<escape>\\)?:(?<identifier>[$_\p{ID_Start}][$\p{ID_Continue}]+)\\+/gu;
871
874
  }
872
- function getOptionalPathParamPattern() {
875
+ function createOptionalPathParamRegex() {
873
876
  return /(?<leadingSlash>\/)?(?<escape>\\)?:(?<identifier>[$_\p{ID_Start}][$\p{ID_Continue}]+)\?(?<trailingSlash>\/)?/gu;
874
877
  }
875
- function getOptionalRepeatingPathParamPattern() {
878
+ function createOptionalRepeatingPathParamRegex() {
876
879
  return /(?<leadingSlash>\/)?(?<escape>\\)?:(?<identifier>[$_\p{ID_Start}][$\p{ID_Continue}]+)\*(?<trailingSlash>\/)?/gu;
877
880
  }
878
- function createParametrizedPathPattern(path) {
879
- const replacedURL = encodeURI(path).replace(/^\/+/g, "").replace(/\/+$/g, "").replace(getExtraPatternsToEscape(), "\\$1").replace(getURIEncodedBackSlashPattern(), "\\").replace(
880
- getOptionalRepeatingPathParamPattern(),
881
+ function createRegexFromPath(path) {
882
+ const pathRegexContent = preparePathForRegex(path).replace(
883
+ createOptionalRepeatingPathParamRegex(),
881
884
  (_match, leadingSlash, escape, identifier, trailingSlash) => {
882
885
  if (escape) {
883
886
  return `:${identifier}`;
@@ -896,10 +899,10 @@ function createParametrizedPathPattern(path) {
896
899
  return `(?<${identifier}>.+?)?`;
897
900
  }
898
901
  }
899
- ).replace(getRepeatingPathParamPattern(), (_match, escape, identifier) => {
902
+ ).replace(createRepeatingPathParamRegex(), (_match, escape, identifier) => {
900
903
  return escape ? `:${identifier}` : `(?<${identifier}>.+)`;
901
904
  }).replace(
902
- getOptionalPathParamPattern(),
905
+ createOptionalPathParamRegex(),
903
906
  (_match, leadingSlash, escape, identifier, trailingSlash) => {
904
907
  if (escape) {
905
908
  return `:${identifier}`;
@@ -918,12 +921,12 @@ function createParametrizedPathPattern(path) {
918
921
  return `(?<${identifier}>[^\\/]+?)?`;
919
922
  }
920
923
  }
921
- ).replace(getPathParamPattern(), (_match, escape, identifier) => {
924
+ ).replace(createPathParamRegex(), (_match, escape, identifier) => {
922
925
  return escape ? `:${identifier}` : `(?<${identifier}>[^\\/]+?)`;
923
926
  });
924
- return new RegExp(`^/?${replacedURL}/?$`);
927
+ return new RegExp(`^/?${pathRegexContent}/?$`);
925
928
  }
926
- var createParametrizedPathPattern_default = createParametrizedPathPattern;
929
+ var createRegexFromPath_default = createRegexFromPath;
927
930
 
928
931
  // ../zimic-utils/dist/url/excludeURLParams.mjs
929
932
  function excludeURLParams(url) {
@@ -1220,7 +1223,7 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
1220
1223
  }
1221
1224
  static parseRawPathParams(request, options) {
1222
1225
  const requestPath = request.url.replace(options?.baseURL ?? "", "");
1223
- const paramsMatch = options?.pathPattern.exec(requestPath);
1226
+ const paramsMatch = options?.pathRegex.exec(requestPath);
1224
1227
  const params = {};
1225
1228
  for (const [paramName, paramValue] of Object.entries(paramsMatch?.groups ?? {})) {
1226
1229
  params[paramName] = typeof paramValue === "string" ? decodeURIComponent(paramValue) : void 0;
@@ -1572,12 +1575,12 @@ var HttpInterceptorClient = class {
1572
1575
  return;
1573
1576
  }
1574
1577
  this.handlerClientsByMethod[handler.method].set(handler.path, handlerClients);
1575
- const pathPattern = createParametrizedPathPattern_default(handler.path);
1578
+ const pathRegex = createRegexFromPath_default(handler.path);
1576
1579
  const registrationResult = this.workerOrThrow.use(this, handler.method, handler.path, async (context) => {
1577
1580
  const response = await this.handleInterceptedRequest(
1578
1581
  handler.method,
1579
1582
  handler.path,
1580
- pathPattern,
1583
+ pathRegex,
1581
1584
  context
1582
1585
  );
1583
1586
  return response;
@@ -1586,10 +1589,10 @@ var HttpInterceptorClient = class {
1586
1589
  handler.registerSyncPromise(registrationResult);
1587
1590
  }
1588
1591
  }
1589
- async handleInterceptedRequest(method, path, pathPattern, { request }) {
1592
+ async handleInterceptedRequest(method, path, pathRegex, { request }) {
1590
1593
  const parsedRequest = await HttpInterceptorWorker_default.parseRawRequest(request, {
1591
1594
  baseURL: this.baseURLAsString,
1592
- pathPattern
1595
+ pathRegex
1593
1596
  });
1594
1597
  const matchedHandler = await this.findMatchedHandler(method, path, parsedRequest);
1595
1598
  if (!matchedHandler) {
@@ -1671,7 +1674,7 @@ var DuplicatedPathParamError = class extends Error {
1671
1674
  }
1672
1675
  };
1673
1676
  function validatePathParams(path) {
1674
- const pathParamMatches = path.toString().matchAll(getPathParamPattern());
1677
+ const pathParamMatches = path.toString().matchAll(createPathParamRegex());
1675
1678
  const uniqueParamNames = /* @__PURE__ */ new Set();
1676
1679
  for (const paramMatch of pathParamMatches) {
1677
1680
  const paramName = paramMatch.groups?.identifier;
@@ -1795,7 +1798,7 @@ var LocalHttpInterceptorWorker = class extends HttpInterceptorWorker_default {
1795
1798
  const handler = {
1796
1799
  baseURL: interceptor.baseURLAsString,
1797
1800
  method,
1798
- pathPattern: createParametrizedPathPattern_default(path),
1801
+ pathRegex: createRegexFromPath_default(path),
1799
1802
  interceptor,
1800
1803
  createResponse: async (context) => {
1801
1804
  const request = context.request;
@@ -1832,7 +1835,7 @@ var LocalHttpInterceptorWorker = class extends HttpInterceptorWorker_default {
1832
1835
  continue;
1833
1836
  }
1834
1837
  const requestPath = requestURLAsString.replace(handler.baseURL, "");
1835
- const matchesPath = handler.pathPattern.test(requestPath);
1838
+ const matchesPath = handler.pathRegex.test(requestPath);
1836
1839
  if (!matchesPath) {
1837
1840
  continue;
1838
1841
  }