trousse 3.13.0 → 3.14.0

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/index.d.ts CHANGED
@@ -8,6 +8,6 @@ import { anyWordMatchesAnyOf, endsWithAnyOf, escapeRegex, getAnyOf, includesAnyO
8
8
  import { createAcceptHeader, feedMimeTypes, genericFeedMimeTypes, genericHtmlMimeTypes, htmlMimeTypes } from "./mimes.js";
9
9
  import { omit, pick, toMap, trimObject } from "./objects.js";
10
10
  import { sleep } from "./timers.js";
11
- import { AnyOf, DeepOmit, IsPlainObject, LocaleSegmentOptions, MaybePromise, NormalizeOptions, Nullish, ParamOptions, ParamSource, PartialNullish, Pattern, RegistrableDomainOptions, WeightedMimeType } from "./types.js";
11
+ import { AnyOf, DeepOmit, IsPlainObject, LocaleSegmentOptions, MaybePromise, NormalizeOptions, Nullish, ParamOptions, ParamSource, PartialNullish, Pattern, QueryParamPredicate, RegistrableDomainOptions, WeightedMimeType } from "./types.js";
12
12
  import { addMissingProtocol, addMissingScheme, composeFragment, composeQuery, createUrlSpellings, createUrlVariants, decodeBase64, decodeBase64Binary, decodeBase64Url, decodeBase64UrlBinary, decodePercent, decodeSegment, encodePathSegment, fixMalformedHost, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isIpAddress, isSubdomainOf, isUtf8Binary, normalizeUrl, parseUrl, resolveFeedProtocol, resolveFeedScheme, resolveUrl, stripWww, trimUrlValues, upgradeProtocol, upgradeScheme } from "./urls.js";
13
- export { type AnyOf, type DeepOmit, type IsPlainObject, type LocaleSegmentOptions, type MaybePromise, type NormalizeOptions, type Nullish, type ParamOptions, type ParamSource, type PartialNullish, type Pattern, type RegistrableDomainOptions, type WeightedMimeType, addMissingProtocol, addMissingScheme, anyWordMatchesAnyOf, archiveExtensions, audioExtensions, codeExtensions, coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, coerceString, composeFragment, composeQuery, createAcceptHeader, createUrlSpellings, createUrlVariants, decodeBase64, decodeBase64Binary, decodeBase64Url, decodeBase64UrlBinary, decodePercent, decodeSegment, documentExtensions, encodePathSegment, endsWithAnyOf, escapeHtml, escapeRegex, feedMimeTypes, fixMalformedHost, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, flashExtensions, fontExtensions, genericFeedMimeTypes, genericHtmlMimeTypes, getAnyOf, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, htmlMimeTypes, imageExtensions, includesAnyOf, installerExtensions, isAnyOf, isBoolean, isFiniteNumber, isFunction, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isInteger, isIpAddress, isJsonLike, isLocaleSegment, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isRegionSegment, isSameText, isString, isSubdomainOf, isUtf8Binary, isValidDate, normalizeUrl, omit, omitEmpty, parseJson, parseUrl, pick, resolveFeedProtocol, resolveFeedScheme, resolveUrl, sleep, startsWithAnyOf, stripWww, subtitleExtensions, t, toMap, trimObject, trimUrlValues, tx, upgradeProtocol, upgradeScheme, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions };
13
+ export { type AnyOf, type DeepOmit, type IsPlainObject, type LocaleSegmentOptions, type MaybePromise, type NormalizeOptions, type Nullish, type ParamOptions, type ParamSource, type PartialNullish, type Pattern, type QueryParamPredicate, type RegistrableDomainOptions, type WeightedMimeType, addMissingProtocol, addMissingScheme, anyWordMatchesAnyOf, archiveExtensions, audioExtensions, codeExtensions, coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, coerceString, composeFragment, composeQuery, createAcceptHeader, createUrlSpellings, createUrlVariants, decodeBase64, decodeBase64Binary, decodeBase64Url, decodeBase64UrlBinary, decodePercent, decodeSegment, documentExtensions, encodePathSegment, endsWithAnyOf, escapeHtml, escapeRegex, feedMimeTypes, fixMalformedHost, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, flashExtensions, fontExtensions, genericFeedMimeTypes, genericHtmlMimeTypes, getAnyOf, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, htmlMimeTypes, imageExtensions, includesAnyOf, installerExtensions, isAnyOf, isBoolean, isFiniteNumber, isFunction, isHostOf, isHostOrSubdomainOf, isHostname, isHttpUrl, isInteger, isIpAddress, isJsonLike, isLocaleSegment, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isRegionSegment, isSameText, isString, isSubdomainOf, isUtf8Binary, isValidDate, normalizeUrl, omit, omitEmpty, parseJson, parseUrl, pick, resolveFeedProtocol, resolveFeedScheme, resolveUrl, sleep, startsWithAnyOf, stripWww, subtitleExtensions, t, toMap, trimObject, trimUrlValues, tx, upgradeProtocol, upgradeScheme, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions };
package/dist/types.d.ts CHANGED
@@ -17,6 +17,7 @@ export type LocaleSegmentOptions = {
17
17
  export type RegistrableDomainOptions = {
18
18
  suffixes?: ReadonlyArray<string>;
19
19
  };
20
+ export type QueryParamPredicate = (name: string, value: string | undefined, url: URL) => boolean;
20
21
  export type NormalizeOptions = {
21
22
  stripScheme?: boolean;
22
23
  /** @deprecated Use `stripScheme` instead, which wins when both are set. */
@@ -29,7 +30,7 @@ export type NormalizeOptions = {
29
30
  collapseSlashes?: boolean;
30
31
  stripHash?: boolean;
31
32
  sortQueryParams?: boolean;
32
- stripQueryParams?: ReadonlyArray<string>;
33
+ stripQueryParams?: ReadonlyArray<string> | QueryParamPredicate;
33
34
  stripQuery?: boolean;
34
35
  stripEmptyQuery?: boolean;
35
36
  lowercaseQuery?: boolean;
package/dist/urls.js CHANGED
@@ -1,4 +1,4 @@
1
- import { isNullish, isString } from "./is.js";
1
+ import { isFunction, isNullish, isString } from "./is.js";
2
2
  import { isAnyOf, toPatternList } from "./matching.js";
3
3
  import { decodeHTMLStrict } from "entities";
4
4
  // src/urls.ts
@@ -286,7 +286,7 @@ const ipv4Regex = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
286
286
  const ipv6Regex = /^([0-9a-f]{0,4}:){2,7}[0-9a-f]{0,4}$/i;
287
287
  const wwwPrefixRegex = /^(?:www\.)+(?=.*\.)/;
288
288
  const ipv6BracketsRegex = /^\[|\]$/g;
289
- const hostnameLabelRegex = /^[a-z0-9_](?:[a-z0-9_-]*[a-z0-9_])?$/i;
289
+ const hostnameLabelRegex = /^[a-z0-9_-]+$/i;
290
290
  const fileExtensionRegex = /\.[a-z][a-z0-9]{0,5}$/i;
291
291
  const unreservedCharsRegex = /[a-zA-Z0-9._~-]/;
292
292
  const httpsLetterRegex = /s/i;
@@ -342,7 +342,7 @@ const fixMalformedScheme = (url) => {
342
342
  return url;
343
343
  };
344
344
  const authorityRegex = /^(?:[a-z][a-z\d+.-]*:)?\/\/[^/?#\\]*/i;
345
- const dotRunRegex = /\.{2,}/g;
345
+ const doubledDotRegex = /(?<!\.)\.\.(?!\.)/g;
346
346
  const trailingHostJunkChars = [
347
347
  ")",
348
348
  ",",
@@ -356,8 +356,9 @@ const fixMalformedHost = (url) => {
356
356
  const host = authority.slice(hostStart);
357
357
  let hostEnd = host.length;
358
358
  while (hostEnd > 0 && trailingHostJunkChars.includes(host.charAt(hostEnd - 1))) hostEnd--;
359
- let fixedHost = host.slice(0, hostEnd).replace(dotRunRegex, ".");
360
- if (fixedHost.startsWith(".")) fixedHost = fixedHost.slice(1);
359
+ let fixedHost = host.slice(0, hostEnd).replace(doubledDotRegex, ".");
360
+ if (fixedHost.startsWith(".") && !fixedHost.startsWith("..")) fixedHost = fixedHost.slice(1);
361
+ if (fixedHost.endsWith(".")) return url;
361
362
  if (!fixedHost || fixedHost.startsWith(",")) return url;
362
363
  return `${url.slice(0, hostStart)}${fixedHost}${url.slice(authority.length)}`;
363
364
  };
@@ -545,14 +546,22 @@ const decodeAndNormalizeEncoding = (value) => {
545
546
  return hex.toUpperCase();
546
547
  });
547
548
  };
548
- const decodeQueryKey = (pair) => {
549
- const key = pair.split("=")[0].replace(plusRegex, " ");
549
+ const decodeQueryComponent = (component) => {
550
+ const spaced = component.replace(plusRegex, " ");
550
551
  try {
551
- return decodeURIComponent(key);
552
+ return decodeURIComponent(spaced);
552
553
  } catch {
553
- return key;
554
+ return spaced;
554
555
  }
555
556
  };
557
+ const decodeQueryKey = (pair) => {
558
+ return decodeQueryComponent(pair.split("=")[0]);
559
+ };
560
+ const decodeQueryValue = (pair) => {
561
+ const separatorIndex = pair.indexOf("=");
562
+ if (separatorIndex === -1) return;
563
+ return decodeQueryComponent(pair.slice(separatorIndex + 1));
564
+ };
556
565
  const compareQueryPairs = (a, b) => {
557
566
  const keyA = decodeQueryKey(a);
558
567
  const keyB = decodeQueryKey(b);
@@ -588,7 +597,12 @@ const normalizeUrl = (url, options) => {
588
597
  if (options.stripQuery) parsed.search = "";
589
598
  if (parsed.search && (options.stripQueryParams || options.lowercaseQuery)) {
590
599
  let pairs = parsed.search.slice(1).split("&");
591
- if (options.stripQueryParams) {
600
+ if (isFunction(options.stripQueryParams)) {
601
+ const isStripped = options.stripQueryParams;
602
+ pairs = pairs.filter((pair) => {
603
+ return pair && !isStripped(decodeQueryKey(pair), decodeQueryValue(pair), parsed);
604
+ });
605
+ } else if (options.stripQueryParams) {
592
606
  const strippedSet = getStrippedParamsSet(options.stripQueryParams);
593
607
  pairs = pairs.filter((pair) => pair && !strippedSet.has(decodeQueryKey(pair).toLowerCase()));
594
608
  }
package/package.json CHANGED
@@ -46,5 +46,5 @@
46
46
  "dependencies": {
47
47
  "entities": "^9.0.0"
48
48
  },
49
- "version": "3.13.0"
49
+ "version": "3.14.0"
50
50
  }