tldts-experimental 7.0.32 → 7.1.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/cjs/index.js CHANGED
@@ -440,11 +440,16 @@ function parseImpl(url, step, suffixLookup, partialOptions, result) {
440
440
  // set to `false` to speed-up parsing. If only URLs are expected then
441
441
  // `mixedInputs` can be set to `false`. The `mixedInputs` is only a hint
442
442
  // and will not change the behavior of the library.
443
+ // Whether `url` itself was already a valid hostname (only computed on the
444
+ // mixedInputs path). Lets us skip the post-extraction validation below when
445
+ // extractHostname returned `url` unchanged (same reference).
446
+ let urlIsValid = false;
443
447
  if (!options.extractHostname) {
444
448
  result.hostname = url;
445
449
  }
446
450
  else if (options.mixedInputs) {
447
- result.hostname = extractHostname(url, isValidHostname(url));
451
+ urlIsValid = isValidHostname(url);
452
+ result.hostname = extractHostname(url, urlIsValid);
448
453
  }
449
454
  else {
450
455
  result.hostname = extractHostname(url, false);
@@ -463,6 +468,9 @@ function parseImpl(url, step, suffixLookup, partialOptions, result) {
463
468
  if (options.validateHostname &&
464
469
  options.extractHostname &&
465
470
  result.hostname !== null &&
471
+ // Skip the re-scan when `url` was already validated and extractHostname
472
+ // returned it unchanged (same reference => identical string, still valid).
473
+ !(urlIsValid && result.hostname === url) &&
466
474
  !isValidHostname(result.hostname)) {
467
475
  result.hostname = null;
468
476
  return result;
@@ -759,11 +767,22 @@ function suffixLookup(hostname, options, out) {
759
767
  out.publicSuffix = hostname.slice(BUFFER[((matchLabels - 1) << 1) + 1]);
760
768
  return;
761
769
  }
762
- const parts = hostname.split('.');
763
- while (parts.length > matchLabels) {
764
- parts.shift();
770
+ // matchLabels >= numberOfHashes: the suffix may extend past the labels we
771
+ // tracked in BUFFER, so locate its start by scanning backward for the
772
+ // matchLabels-th dot from the end (no array alloc, no O(n^2) shift). Fewer
773
+ // dots than matchLabels means the whole hostname is the suffix.
774
+ let suffixStart = 0;
775
+ let seenDots = 0;
776
+ for (let i = hostname.length - 1; i >= 0; i -= 1) {
777
+ if (hostname.charCodeAt(i) === 46 /* '.' */) {
778
+ seenDots += 1;
779
+ if (seenDots === matchLabels) {
780
+ suffixStart = i + 1;
781
+ break;
782
+ }
783
+ }
765
784
  }
766
- out.publicSuffix = parts.join('.');
785
+ out.publicSuffix = hostname.slice(suffixStart);
767
786
  return;
768
787
  }
769
788
  // if ((matchKind & Result.NORMAL_MATCH) !== 0)
@@ -775,28 +794,28 @@ function suffixLookup(hostname, options, out) {
775
794
  // every single time to only return the value of a specific attribute. To avoid
776
795
  // this un-necessary allocation, we use a global object which is re-used.
777
796
  const RESULT = getEmptyResult();
778
- function parse(url, options = {}) {
797
+ function parse(url, options) {
779
798
  return parseImpl(url, 5 /* FLAG.ALL */, suffixLookup, options, getEmptyResult());
780
799
  }
781
- function getHostname(url, options = {}) {
800
+ function getHostname(url, options) {
782
801
  /*@__INLINE__*/ resetResult(RESULT);
783
802
  return parseImpl(url, 0 /* FLAG.HOSTNAME */, suffixLookup, options, RESULT).hostname;
784
803
  }
785
- function getPublicSuffix(url, options = {}) {
804
+ function getPublicSuffix(url, options) {
786
805
  /*@__INLINE__*/ resetResult(RESULT);
787
806
  return parseImpl(url, 2 /* FLAG.PUBLIC_SUFFIX */, suffixLookup, options, RESULT)
788
807
  .publicSuffix;
789
808
  }
790
- function getDomain(url, options = {}) {
809
+ function getDomain(url, options) {
791
810
  /*@__INLINE__*/ resetResult(RESULT);
792
811
  return parseImpl(url, 3 /* FLAG.DOMAIN */, suffixLookup, options, RESULT).domain;
793
812
  }
794
- function getSubdomain(url, options = {}) {
813
+ function getSubdomain(url, options) {
795
814
  /*@__INLINE__*/ resetResult(RESULT);
796
815
  return parseImpl(url, 4 /* FLAG.SUB_DOMAIN */, suffixLookup, options, RESULT)
797
816
  .subdomain;
798
817
  }
799
- function getDomainWithoutSuffix(url, options = {}) {
818
+ function getDomainWithoutSuffix(url, options) {
800
819
  /*@__INLINE__*/ resetResult(RESULT);
801
820
  return parseImpl(url, 5 /* FLAG.ALL */, suffixLookup, options, RESULT)
802
821
  .domainWithoutSuffix;