trousse 3.6.0 → 3.8.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
@@ -1,5 +1,4 @@
1
1
  import { omitEmpty } from "./arrays.js";
2
- import { AnyOf, DeepOmit, IsPlainObject, MaybePromise, NormalizeOptions, Nullish, ParamOptions, ParamSource, PartialNullish, Pattern, RegistrableDomainOptions, WeightedMimeType } from "./types.js";
3
2
  import { coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, coerceString } from "./coercions.js";
4
3
  import { archiveExtensions, audioExtensions, codeExtensions, documentExtensions, flashExtensions, fontExtensions, imageExtensions, installerExtensions, subtitleExtensions, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions } from "./files.js";
5
4
  import { isBoolean, isFiniteNumber, isFunction, isInteger, isNonEmptyString, isNullish, isNumber, isObject, isPlainObject, isPresent, isString, isValidDate } from "./is.js";
@@ -9,5 +8,6 @@ import { anyWordMatchesAnyOf, endsWithAnyOf, escapeRegex, getAnyOf, includesAnyO
9
8
  import { createAcceptHeader, feedMimeTypes, genericFeedMimeTypes, genericHtmlMimeTypes, htmlMimeTypes } from "./mimes.js";
10
9
  import { omit, pick, toMap, trimObject } from "./objects.js";
11
10
  import { sleep } from "./timers.js";
11
+ import { AnyOf, DeepOmit, IsPlainObject, MaybePromise, NormalizeOptions, Nullish, ParamOptions, ParamSource, PartialNullish, Pattern, RegistrableDomainOptions, WeightedMimeType } from "./types.js";
12
12
  import { addMissingProtocol, addMissingScheme, composeFragment, composeQuery, createUrlSpellings, createUrlVariants, decodePercent, decodeSegment, encodePathSegment, fixMalformedProtocol, fixMalformedQuery, fixMalformedScheme, getFragmentParam, getFragmentParamValues, getFragmentParams, getOrigin, getPathSegments, getQueryParam, getQueryParamValues, getQueryParams, getRegistrableDomain, getSubdomain, hasFragmentParam, hasQueryParam, isHostOf, isHostOrSubdomainOf, isHttpUrl, isIpAddress, isSubdomainOf, normalizeUrl, parseUrl, resolveFeedProtocol, resolveFeedScheme, resolveUrl, stripWww, trimUrlValues, upgradeProtocol, upgradeScheme } from "./urls.js";
13
13
  export { type AnyOf, type DeepOmit, type IsPlainObject, 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, decodePercent, decodeSegment, documentExtensions, encodePathSegment, endsWithAnyOf, escapeHtml, escapeRegex, feedMimeTypes, 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, isHttpUrl, isInteger, isIpAddress, isJsonLike, isNonEmptyString, isNullish, isNumber, isObject, isParseableJson, isPlainObject, isPresent, isSameText, isString, isSubdomainOf, 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/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import { isBoolean, isFiniteNumber, isFunction, isInteger, isNonEmptyString, isNullish, isNumber, isObject, isPlainObject, isPresent, isString, isValidDate } from "./is.js";
2
1
  import { omitEmpty } from "./arrays.js";
3
2
  import { coerceArray, coerceBoolean, coerceDate, coerceNumber, coerceSingular, coerceString } from "./coercions.js";
4
3
  import { archiveExtensions, audioExtensions, codeExtensions, documentExtensions, flashExtensions, fontExtensions, imageExtensions, installerExtensions, subtitleExtensions, videoExtensions, webAudioExtensions, webImageExtensions, webVideoExtensions } from "./files.js";
4
+ import { isBoolean, isFiniteNumber, isFunction, isInteger, isNonEmptyString, isNullish, isNumber, isObject, isPlainObject, isPresent, isString, isValidDate } from "./is.js";
5
5
  import { isJsonLike, isParseableJson, parseJson } from "./json.js";
6
6
  import { escapeHtml, t, tx } from "./locales.js";
7
7
  import { anyWordMatchesAnyOf, endsWithAnyOf, escapeRegex, getAnyOf, includesAnyOf, isAnyOf, isSameText, startsWithAnyOf } from "./matching.js";
package/dist/matching.js CHANGED
@@ -83,4 +83,4 @@ const anyWordMatchesAnyOf = (value, patterns) => {
83
83
  }
84
84
  return false;
85
85
  };
86
- export { anyWordMatchesAnyOf, endsWithAnyOf, escapeRegex, getAnyOf, includesAnyOf, isAnyOf, isSameText, startsWithAnyOf };
86
+ export { anyWordMatchesAnyOf, endsWithAnyOf, escapeRegex, getAnyOf, includesAnyOf, isAnyOf, isSameText, startsWithAnyOf, toPatternList };
package/dist/urls.d.ts CHANGED
@@ -1,12 +1,12 @@
1
- import { NormalizeOptions, Nullish, ParamOptions, ParamSource, RegistrableDomainOptions } from "./types.js";
1
+ import { NormalizeOptions, Nullish, ParamOptions, ParamSource, Pattern, RegistrableDomainOptions } from "./types.js";
2
2
  export declare const parseUrl: (value: Nullish<string | URL>, base?: string | URL) => URL | undefined;
3
3
  export declare const getPathSegments: (value: string | URL) => Array<string>;
4
4
  export declare const getOrigin: (value: Nullish<string | URL>) => string | undefined;
5
- export declare const isHostOf: (url: string | URL, hosts: string | ReadonlyArray<string>) => boolean;
6
- export declare const isSubdomainOf: (url: string | URL, domains: string | ReadonlyArray<string>) => boolean;
5
+ export declare const isHostOf: (url: string | URL, hosts: Pattern | ReadonlyArray<Pattern>) => boolean;
6
+ export declare const isSubdomainOf: (url: string | URL, domains: Pattern | ReadonlyArray<Pattern>) => boolean;
7
7
  export declare const isHttpUrl: (url: string | URL) => boolean;
8
- export declare const isHostOrSubdomainOf: (url: string | URL, domains: string | ReadonlyArray<string>) => boolean;
9
- export declare const getSubdomain: (url: string | URL, domains: string | ReadonlyArray<string>) => string | undefined;
8
+ export declare const isHostOrSubdomainOf: (url: string | URL, domains: Pattern | ReadonlyArray<Pattern>) => boolean;
9
+ export declare const getSubdomain: (url: string | URL, domains: Pattern | ReadonlyArray<Pattern>) => string | undefined;
10
10
  export declare const decodeSegment: (segment: string | undefined) => string | undefined;
11
11
  export declare const decodePercent: (value: string) => string;
12
12
  export declare const encodePathSegment: (value: string) => string;
package/dist/urls.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { isNullish } from "./is.js";
2
- import { endsWithAnyOf, isAnyOf } from "./matching.js";
2
+ import { isAnyOf, toPatternList } from "./matching.js";
3
3
  import { decodeHTMLStrict } from "entities";
4
4
  // src/urls.ts
5
5
  const httpSchemes = ["http:", "https:"];
@@ -31,15 +31,24 @@ const normalizeDomain = (domain) => {
31
31
  if (!nonAsciiRegex.test(bare)) return bare;
32
32
  return parseUrl(`http://${bare}`)?.hostname ?? bare;
33
33
  };
34
+ const normalizeHostPattern = (pattern) => {
35
+ return pattern instanceof RegExp ? pattern : normalizeDomain(pattern);
36
+ };
37
+ const getParentDomains = (hostname) => {
38
+ const parents = [];
39
+ for (let index = hostname.indexOf("."); index !== -1; index = hostname.indexOf(".", index + 1)) parents.push(hostname.slice(index + 1));
40
+ return parents;
41
+ };
34
42
  const isHostOf = (url, hosts) => {
35
43
  const hostname = getHostname(url);
36
44
  if (!hostname) return false;
37
- return isAnyOf(hostname, (typeof hosts === "string" ? [hosts] : hosts).map(normalizeDomain));
45
+ return isAnyOf(hostname, toPatternList(hosts).map(normalizeHostPattern));
38
46
  };
39
47
  const isSubdomainOf = (url, domains) => {
40
48
  const hostname = getHostname(url);
41
49
  if (!hostname) return false;
42
- return endsWithAnyOf(hostname, (typeof domains === "string" ? [domains] : domains).map((domain) => `.${normalizeDomain(domain)}`));
50
+ const list = toPatternList(domains).map(normalizeHostPattern);
51
+ return getParentDomains(hostname).some((parent) => isAnyOf(parent, list));
43
52
  };
44
53
  const isHttpUrl = (url) => {
45
54
  const scheme = parseUrl(url)?.protocol;
@@ -52,10 +61,11 @@ const isHostOrSubdomainOf = (url, domains) => {
52
61
  const getSubdomain = (url, domains) => {
53
62
  const hostname = getHostname(url)?.toLowerCase();
54
63
  if (!hostname) return;
55
- const list = typeof domains === "string" ? [domains] : domains;
56
- for (const domain of list) {
57
- const suffix = `.${normalizeDomain(domain).toLowerCase()}`;
58
- if (hostname.endsWith(suffix) && hostname.length > suffix.length) return hostname.slice(0, -suffix.length);
64
+ const parents = getParentDomains(hostname);
65
+ for (const domain of toPatternList(domains)) {
66
+ const pattern = normalizeHostPattern(domain);
67
+ const parent = parents.find((candidate) => isAnyOf(candidate, pattern));
68
+ if (parent) return hostname.slice(0, -parent.length - 1);
59
69
  }
60
70
  };
61
71
  const decodeSegment = (segment) => {
@@ -427,6 +437,18 @@ const resolveUrl = (url, base) => {
427
437
  if (!parsed || !isHttpUrl(parsed)) return;
428
438
  return parsed.href;
429
439
  };
440
+ const valueShapeEscapeRegex = /(%(?:26|23|25|2B|3B))/gi;
441
+ const nameShapeEscapeRegex = /(%(?:26|23|25|2B|3B|3D))/gi;
442
+ const decodeQueryPart = (part, shapeRegex) => {
443
+ return part.replace(plusRegex, "%20").split(shapeRegex).map((chunk, index) => index % 2 ? chunk.toUpperCase() : decodePercent(chunk)).join("");
444
+ };
445
+ const canonicalizeQuery = (search) => {
446
+ return `?${search.slice(1).split("&").map((pair) => {
447
+ const at = pair.indexOf("=");
448
+ if (at === -1) return decodeQueryPart(pair, nameShapeEscapeRegex);
449
+ return `${decodeQueryPart(pair.slice(0, at), nameShapeEscapeRegex)}=${decodeQueryPart(pair.slice(at + 1), valueShapeEscapeRegex)}`;
450
+ }).join("&")}`;
451
+ };
430
452
  const decodeAndNormalizeEncoding = (value) => {
431
453
  if (!value.includes("%")) return value;
432
454
  return value.replace(percentEscapeRegex, (hex) => {
@@ -471,7 +493,10 @@ const normalizeUrl = (url, options) => {
471
493
  if (options.collapseSlashes) pathname = pathname.replace(repeatedSlashesRegex, "/");
472
494
  if (options.stripTrailingSlash && pathname.length > 1 && pathname.endsWith("/")) pathname = pathname.slice(0, -1);
473
495
  parsed.pathname = pathname;
474
- if (options.normalizeEncoding && parsed.search.includes("%")) parsed.search = decodeAndNormalizeEncoding(parsed.search);
496
+ if (options.normalizeEncoding && parsed.search.length > 1) {
497
+ parsed.search = canonicalizeQuery(parsed.search);
498
+ parsed.search = parsed.search.replace(percentEscapeRegex, (hex) => hex.toUpperCase());
499
+ }
475
500
  if (options.normalizeEncoding && parsed.hash.includes("%")) parsed.hash = decodeAndNormalizeEncoding(parsed.hash);
476
501
  if (options.stripQuery) parsed.search = "";
477
502
  if (parsed.search && (options.stripQueryParams || options.lowercaseQuery)) {
@@ -522,7 +547,15 @@ const createUrlVariants = (url) => {
522
547
  const sameQuerySpellings = [
523
548
  ["+", "%20"],
524
549
  [":", "%3A"],
525
- ["@", "%40"]
550
+ ["@", "%40"],
551
+ ["/", "%2F"],
552
+ [",", "%2C"],
553
+ ["(", "%28"],
554
+ [")", "%29"],
555
+ ["*", "%2A"],
556
+ ["!", "%21"],
557
+ ["'", "%27"],
558
+ ["$", "%24"]
526
559
  ];
527
560
  const percentEscapeRegex = /%[0-9a-f]{2}/gi;
528
561
  const addLowercaseHexSpellings = (url, spellings) => {
@@ -532,25 +565,34 @@ const addLowercaseHexSpellings = (url, spellings) => {
532
565
  });
533
566
  return [.../* @__PURE__ */ new Set([...spellings, ...lowercaseHexSpellings])];
534
567
  };
568
+ const maxMixedQuerySpellings = 4;
569
+ const respellQuery = (spelling, queryIndex, pairs, toEncoded) => {
570
+ const fragmentIndex = spelling.indexOf("#");
571
+ const queryEnd = fragmentIndex === -1 ? spelling.length : fragmentIndex;
572
+ let query = spelling.slice(queryIndex, queryEnd);
573
+ for (const [written, encoded] of pairs) query = toEncoded ? query.replaceAll(written, encoded) : query.replaceAll(encoded, written);
574
+ return `${spelling.slice(0, queryIndex)}${query}${spelling.slice(queryEnd)}`;
575
+ };
535
576
  const createUrlSpellings = (url) => {
536
577
  const uppercaseHexUrl = url.replace(percentEscapeRegex, (hex) => hex.toUpperCase());
537
- let spellings = [.../* @__PURE__ */ new Set([url, uppercaseHexUrl])];
538
578
  const hashIndex = url.indexOf("#");
539
579
  const queryEnd = hashIndex === -1 ? url.length : hashIndex;
540
580
  const queryIndex = url.indexOf("?");
541
- if (queryIndex === -1 || queryIndex > queryEnd) return addLowercaseHexSpellings(url, spellings);
542
- for (const [written, encoded] of sameQuerySpellings) {
543
- const swapped = spellings.flatMap((spelling) => {
544
- const fragmentIndex = spelling.indexOf("#");
545
- const spellingQueryEnd = fragmentIndex === -1 ? spelling.length : fragmentIndex;
546
- const head = spelling.slice(0, queryIndex);
547
- const query = spelling.slice(queryIndex, spellingQueryEnd);
548
- const fragment = spelling.slice(spellingQueryEnd);
549
- return [`${head}${query.replaceAll(encoded, written)}${fragment}`, `${head}${query.replaceAll(written, encoded)}${fragment}`];
550
- });
551
- spellings = [...new Set(swapped)];
552
- }
553
- return addLowercaseHexSpellings(url, [url, ...spellings]);
581
+ if (queryIndex === -1 || queryIndex > queryEnd) return addLowercaseHexSpellings(url, [.../* @__PURE__ */ new Set([url, uppercaseHexUrl])]);
582
+ const query = uppercaseHexUrl.slice(queryIndex, queryEnd);
583
+ const present = sameQuerySpellings.filter(([written, encoded]) => {
584
+ return query.includes(written) || query.includes(encoded);
585
+ });
586
+ const bases = [.../* @__PURE__ */ new Set([url, uppercaseHexUrl])];
587
+ const throughout = bases.flatMap((base) => [respellQuery(base, queryIndex, present, false), respellQuery(base, queryIndex, present, true)]);
588
+ let mixes = bases;
589
+ if (present.length <= maxMixedQuerySpellings) for (const pair of present) mixes = mixes.flatMap((spelling) => [respellQuery(spelling, queryIndex, [pair], false), respellQuery(spelling, queryIndex, [pair], true)]);
590
+ return addLowercaseHexSpellings(url, [.../* @__PURE__ */ new Set([
591
+ url,
592
+ uppercaseHexUrl,
593
+ ...throughout,
594
+ ...mixes
595
+ ])]);
554
596
  };
555
597
  const genericSecondLevels = [
556
598
  "ac",
package/package.json CHANGED
@@ -39,12 +39,12 @@
39
39
  "test": "bun test"
40
40
  },
41
41
  "devDependencies": {
42
- "@types/bun": "^1.4.2",
43
- "kvalita": "^1.31.0",
44
- "tsdown": "0.23.1"
42
+ "@types/bun": "^1.4.4",
43
+ "kvalita": "^1.35.0",
44
+ "tsdown": "0.23.7"
45
45
  },
46
46
  "dependencies": {
47
- "entities": "^8.1.0"
47
+ "entities": "^9.0.0"
48
48
  },
49
- "version": "3.6.0"
49
+ "version": "3.8.0"
50
50
  }