@zimic/interceptor 1.1.2-canary.2 → 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) {
@@ -1219,8 +1222,13 @@ var HttpInterceptorWorker = class _HttpInterceptorWorker {
1219
1222
  return HTTP_INTERCEPTOR_RESPONSE_HIDDEN_PROPERTIES.has(property);
1220
1223
  }
1221
1224
  static parseRawPathParams(request, options) {
1222
- const match = options?.pathPattern.exec(request.url.replace(options.baseURL, ""));
1223
- return { ...match?.groups };
1225
+ const requestPath = request.url.replace(options?.baseURL ?? "", "");
1226
+ const paramsMatch = options?.pathRegex.exec(requestPath);
1227
+ const params = {};
1228
+ for (const [paramName, paramValue] of Object.entries(paramsMatch?.groups ?? {})) {
1229
+ params[paramName] = typeof paramValue === "string" ? decodeURIComponent(paramValue) : void 0;
1230
+ }
1231
+ return params;
1224
1232
  }
1225
1233
  static async parseRawBody(resource) {
1226
1234
  const contentType = resource.headers.get("content-type");
@@ -1567,12 +1575,12 @@ var HttpInterceptorClient = class {
1567
1575
  return;
1568
1576
  }
1569
1577
  this.handlerClientsByMethod[handler.method].set(handler.path, handlerClients);
1570
- const pathPattern = createParametrizedPathPattern_default(handler.path);
1578
+ const pathRegex = createRegexFromPath_default(handler.path);
1571
1579
  const registrationResult = this.workerOrThrow.use(this, handler.method, handler.path, async (context) => {
1572
1580
  const response = await this.handleInterceptedRequest(
1573
1581
  handler.method,
1574
1582
  handler.path,
1575
- pathPattern,
1583
+ pathRegex,
1576
1584
  context
1577
1585
  );
1578
1586
  return response;
@@ -1581,10 +1589,10 @@ var HttpInterceptorClient = class {
1581
1589
  handler.registerSyncPromise(registrationResult);
1582
1590
  }
1583
1591
  }
1584
- async handleInterceptedRequest(method, path, pathPattern, { request }) {
1592
+ async handleInterceptedRequest(method, path, pathRegex, { request }) {
1585
1593
  const parsedRequest = await HttpInterceptorWorker_default.parseRawRequest(request, {
1586
1594
  baseURL: this.baseURLAsString,
1587
- pathPattern
1595
+ pathRegex
1588
1596
  });
1589
1597
  const matchedHandler = await this.findMatchedHandler(method, path, parsedRequest);
1590
1598
  if (!matchedHandler) {
@@ -1666,7 +1674,7 @@ var DuplicatedPathParamError = class extends Error {
1666
1674
  }
1667
1675
  };
1668
1676
  function validatePathParams(path) {
1669
- const pathParamMatches = path.toString().matchAll(getPathParamPattern());
1677
+ const pathParamMatches = path.toString().matchAll(createPathParamRegex());
1670
1678
  const uniqueParamNames = /* @__PURE__ */ new Set();
1671
1679
  for (const paramMatch of pathParamMatches) {
1672
1680
  const paramName = paramMatch.groups?.identifier;
@@ -1790,7 +1798,7 @@ var LocalHttpInterceptorWorker = class extends HttpInterceptorWorker_default {
1790
1798
  const handler = {
1791
1799
  baseURL: interceptor.baseURLAsString,
1792
1800
  method,
1793
- pathPattern: createParametrizedPathPattern_default(path),
1801
+ pathRegex: createRegexFromPath_default(path),
1794
1802
  interceptor,
1795
1803
  createResponse: async (context) => {
1796
1804
  const request = context.request;
@@ -1827,7 +1835,7 @@ var LocalHttpInterceptorWorker = class extends HttpInterceptorWorker_default {
1827
1835
  continue;
1828
1836
  }
1829
1837
  const requestPath = requestURLAsString.replace(handler.baseURL, "");
1830
- const matchesPath = handler.pathPattern.test(requestPath);
1838
+ const matchesPath = handler.pathRegex.test(requestPath);
1831
1839
  if (!matchesPath) {
1832
1840
  continue;
1833
1841
  }