tldts-experimental 7.4.28 → 7.5.1

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/README.md CHANGED
@@ -20,6 +20,10 @@ _probabilistic data-structure_. It is:
20
20
 
21
21
  The drawback is that there might be some _unlikely_ false positive (think bloom filters).
22
22
 
23
+ Single-label rules (e.g. `uk`) are not stored, so with `extraSuffixes` a
24
+ single-label extra rule equal to a built-in one reports the extra rule's
25
+ section.
26
+
23
27
  For more details, check the documentation from the following files:
24
28
 
25
29
  - [building](https://github.com/remusao/tldts/blob/master/bin/builders/hashes.ts)
package/dist/cjs/index.js CHANGED
@@ -772,12 +772,96 @@ function isValidHostname (hostname) {
772
772
  lastCharCode !== 45);
773
773
  }
774
774
 
775
- function setDefaultsImpl({ allowIcannDomains = true, allowPrivateDomains = false, detectIp = true, detectSpecialUse = false, extractHostname = true, mixedInputs = true, validHosts = null, validateHostname = true, }) {
775
+ /**
776
+ * Apply `options.extraSuffixes` to the built-in lookup result in `out`, as if
777
+ * they were part of the list (`isException`: the built-in winner is an
778
+ * exception rule). As in browsers, the longest match prevails and an exception
779
+ * beats a rule of the same length. Remaining ties go to the built-in match
780
+ * (unless it matched no rule: both flags falsy), then to `icann`.
781
+ */
782
+ function extraSuffixLookup(hostname, options, isException, out) {
783
+ const suffix = out.publicSuffix;
784
+ if (suffix === null) {
785
+ return;
786
+ }
787
+ // Best extra exception and rule, as start indices in `hostname` (smaller is
788
+ // longer; `none` if unmatched), and whether each is ICANN.
789
+ const none = hostname.length + 1;
790
+ let exception = none;
791
+ let exceptionIsIcann = false;
792
+ let rule = none;
793
+ let ruleIsIcann = false;
794
+ const extra = options.extraSuffixes;
795
+ for (let section = 0; section < 2; section += 1) {
796
+ const isIcann = section === 0;
797
+ const rules = isIcann
798
+ ? options.allowIcannDomains && extra.icann
799
+ : options.allowPrivateDomains && extra.private;
800
+ if (!Array.isArray(rules)) {
801
+ continue;
802
+ }
803
+ for (const r of rules) {
804
+ // Sparse or untyped lists may hold non-strings.
805
+ if (typeof r !== 'string') {
806
+ continue;
807
+ }
808
+ const exceptionRule = r.startsWith('!');
809
+ const wildcard = r.startsWith('*.');
810
+ const body = exceptionRule ? r.slice(1) : wildcard ? r.slice(2) : r;
811
+ // Skip empty rules, and exceptions without a parent ('!p').
812
+ if (body === '' ||
813
+ (exceptionRule && !body.includes('.')) ||
814
+ !shareSameDomainSuffix(hostname, body)) {
815
+ continue;
816
+ }
817
+ let start = hostname.length - body.length;
818
+ // Strict `<`: on a tie, the first match (`icann` first) wins.
819
+ if (exceptionRule) {
820
+ if (start < exception) {
821
+ exception = start;
822
+ exceptionIsIcann = isIcann;
823
+ }
824
+ continue;
825
+ }
826
+ // `*.p` also matches `p`, else one more non-empty label.
827
+ if (wildcard && start > 1 && hostname.charCodeAt(start - 2) !== 46) {
828
+ start = hostname.lastIndexOf('.', start - 2) + 1;
829
+ }
830
+ if (start < rule) {
831
+ rule = start;
832
+ ruleIsIcann = isIcann;
833
+ }
834
+ }
835
+ }
836
+ // Start of the built-in match: an exception starts one label left of its
837
+ // suffix.
838
+ const suffixStart = hostname.length - suffix.length;
839
+ const builtin = isException
840
+ ? hostname.lastIndexOf('.', suffixStart - 2) + 1
841
+ : suffixStart;
842
+ // An exception wins ties with rules; a built-in exception wins ties with
843
+ // an extra one.
844
+ if (exception <= rule &&
845
+ (isException ? exception < builtin : exception <= builtin)) {
846
+ out.publicSuffix = hostname.slice(hostname.indexOf('.', exception) + 1);
847
+ out.isIcann = exceptionIsIcann;
848
+ out.isPrivate = !exceptionIsIcann;
849
+ }
850
+ else if (rule < builtin ||
851
+ (rule === builtin && !out.isIcann && !out.isPrivate)) {
852
+ out.publicSuffix = hostname.slice(rule);
853
+ out.isIcann = ruleIsIcann;
854
+ out.isPrivate = !ruleIsIcann;
855
+ }
856
+ }
857
+
858
+ function setDefaultsImpl({ allowIcannDomains = true, allowPrivateDomains = false, detectIp = true, detectSpecialUse = false, extraSuffixes = null, extractHostname = true, mixedInputs = true, validHosts = null, validateHostname = true, }) {
776
859
  return {
777
860
  allowIcannDomains,
778
861
  allowPrivateDomains,
779
862
  detectIp,
780
863
  detectSpecialUse,
864
+ extraSuffixes,
781
865
  extractHostname,
782
866
  mixedInputs,
783
867
  validHosts,
@@ -855,7 +939,12 @@ function parseImpl(url, step, suffixLookup, partialOptions, result) {
855
939
  // extractHostname returned `url` unchanged (same reference).
856
940
  let urlIsValid = false;
857
941
  if (!options.extractHostname) {
858
- result.hostname = url;
942
+ // Trim trailing dots as extractHostname does (`new URL()` keeps them).
943
+ let end = url.length;
944
+ while (end > 1 && url.charCodeAt(end - 1) === 46 /* '.' */) {
945
+ end -= 1;
946
+ }
947
+ result.hostname = end === url.length ? url : url.slice(0, end);
859
948
  }
860
949
  else if (options.mixedInputs) {
861
950
  urlIsValid = isValidHostname(url);
@@ -898,8 +987,11 @@ function parseImpl(url, step, suffixLookup, partialOptions, result) {
898
987
  if (step === 5 /* FLAG.ALL */ && options.detectSpecialUse) {
899
988
  result.isSpecialUse = isSpecialUse(result.hostname);
900
989
  }
901
- // Extract public suffix
902
- suffixLookup(result.hostname, options, result);
990
+ // Extract public suffix (`isException`: the winner is an exception rule)
991
+ const isException = suffixLookup(result.hostname, options, result);
992
+ if (options.extraSuffixes !== null) {
993
+ extraSuffixLookup(result.hostname, options, isException === true, result);
994
+ }
903
995
  if (step === 2 /* FLAG.PUBLIC_SUFFIX */ || result.publicSuffix === null) {
904
996
  return result;
905
997
  }
@@ -1059,7 +1151,7 @@ function hashHostnameLabelsBackward(hostname, maximumNumberOfLabels) {
1059
1151
  */
1060
1152
  function suffixLookup(hostname, options, out) {
1061
1153
  if (fastPathLookup(hostname, options, out)) {
1062
- return;
1154
+ return false;
1063
1155
  }
1064
1156
  const { allowIcannDomains, allowPrivateDomains } = options;
1065
1157
  // Keep track of longest match
@@ -1161,6 +1253,12 @@ function suffixLookup(hostname, options, out) {
1161
1253
  // important to make sure that we always keep the longest match if there
1162
1254
  // are both a wildcard and a normal rule matching).
1163
1255
  if (match !== 0 /* Result.NO_MATCH */) {
1256
+ // '*' never matches an empty label ('.S', '..S'): `*.S` then only
1257
+ // implies `S`.
1258
+ if ((match & 16 /* Result.WILDCARD_MATCH */) !== 0 &&
1259
+ (labelStart === 1 || hostname.charCodeAt(labelStart - 2) === 46)) {
1260
+ match = (match & -17 /* Result.WILDCARD_MATCH */) | 8 /* Result.NORMAL_MATCH */;
1261
+ }
1164
1262
  matchKind = match;
1165
1263
  matchLabels = label + ((match & 16 /* Result.WILDCARD_MATCH */) !== 0 ? 2 : 1);
1166
1264
  matchIndex = labelStart;
@@ -1172,14 +1270,14 @@ function suffixLookup(hostname, options, out) {
1172
1270
  if (matchIndex === -1) {
1173
1271
  out.publicSuffix =
1174
1272
  numberOfHashes === 1 ? hostname : hostname.slice(BUFFER[1]);
1175
- return;
1273
+ return false;
1176
1274
  }
1177
1275
  // If match is an exception, this means that we need to count less label.
1178
1276
  // For example, exception rule !foo.com would yield suffix 'com', so we need
1179
1277
  // to locate the next dot and slice from there.
1180
1278
  if ((matchKind & 4 /* Result.EXCEPTION_MATCH */) !== 0) {
1181
1279
  out.publicSuffix = hostname.slice(BUFFER[((matchLabels - 2) << 1) + 1]);
1182
- return;
1280
+ return true;
1183
1281
  }
1184
1282
  // If match is a wildcard, we need to match one more label. If wildcard rule
1185
1283
  // was *.com, we would have stored only 'com' in the packed structure and we
@@ -1187,7 +1285,7 @@ function suffixLookup(hostname, options, out) {
1187
1285
  if ((matchKind & 16 /* Result.WILDCARD_MATCH */) !== 0) {
1188
1286
  if (matchLabels < numberOfHashes) {
1189
1287
  out.publicSuffix = hostname.slice(BUFFER[((matchLabels - 1) << 1) + 1]);
1190
- return;
1288
+ return false;
1191
1289
  }
1192
1290
  // matchLabels >= numberOfHashes: the suffix may extend past the labels we
1193
1291
  // tracked in BUFFER, so locate its start by scanning backward for the
@@ -1205,11 +1303,12 @@ function suffixLookup(hostname, options, out) {
1205
1303
  }
1206
1304
  }
1207
1305
  out.publicSuffix = hostname.slice(suffixStart);
1208
- return;
1306
+ return false;
1209
1307
  }
1210
1308
  // if ((matchKind & Result.NORMAL_MATCH) !== 0)
1211
1309
  // For normal match, we just slice the hostname at the beginning of suffix.
1212
1310
  out.publicSuffix = hostname.slice(matchIndex);
1311
+ return false;
1213
1312
  }
1214
1313
 
1215
1314
  // For all methods but 'parse', it does not make sense to allocate an object