tldts-experimental 7.4.28 → 7.5.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/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 label (possibly empty: '.p').
827
+ if (wildcard && start !== 0) {
828
+ start = start === 1 ? 0 : 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,
@@ -898,8 +982,11 @@ function parseImpl(url, step, suffixLookup, partialOptions, result) {
898
982
  if (step === 5 /* FLAG.ALL */ && options.detectSpecialUse) {
899
983
  result.isSpecialUse = isSpecialUse(result.hostname);
900
984
  }
901
- // Extract public suffix
902
- suffixLookup(result.hostname, options, result);
985
+ // Extract public suffix (`isException`: the winner is an exception rule)
986
+ const isException = suffixLookup(result.hostname, options, result);
987
+ if (options.extraSuffixes !== null) {
988
+ extraSuffixLookup(result.hostname, options, isException === true, result);
989
+ }
903
990
  if (step === 2 /* FLAG.PUBLIC_SUFFIX */ || result.publicSuffix === null) {
904
991
  return result;
905
992
  }
@@ -1059,7 +1146,7 @@ function hashHostnameLabelsBackward(hostname, maximumNumberOfLabels) {
1059
1146
  */
1060
1147
  function suffixLookup(hostname, options, out) {
1061
1148
  if (fastPathLookup(hostname, options, out)) {
1062
- return;
1149
+ return false;
1063
1150
  }
1064
1151
  const { allowIcannDomains, allowPrivateDomains } = options;
1065
1152
  // Keep track of longest match
@@ -1172,14 +1259,14 @@ function suffixLookup(hostname, options, out) {
1172
1259
  if (matchIndex === -1) {
1173
1260
  out.publicSuffix =
1174
1261
  numberOfHashes === 1 ? hostname : hostname.slice(BUFFER[1]);
1175
- return;
1262
+ return false;
1176
1263
  }
1177
1264
  // If match is an exception, this means that we need to count less label.
1178
1265
  // For example, exception rule !foo.com would yield suffix 'com', so we need
1179
1266
  // to locate the next dot and slice from there.
1180
1267
  if ((matchKind & 4 /* Result.EXCEPTION_MATCH */) !== 0) {
1181
1268
  out.publicSuffix = hostname.slice(BUFFER[((matchLabels - 2) << 1) + 1]);
1182
- return;
1269
+ return true;
1183
1270
  }
1184
1271
  // If match is a wildcard, we need to match one more label. If wildcard rule
1185
1272
  // was *.com, we would have stored only 'com' in the packed structure and we
@@ -1187,7 +1274,7 @@ function suffixLookup(hostname, options, out) {
1187
1274
  if ((matchKind & 16 /* Result.WILDCARD_MATCH */) !== 0) {
1188
1275
  if (matchLabels < numberOfHashes) {
1189
1276
  out.publicSuffix = hostname.slice(BUFFER[((matchLabels - 1) << 1) + 1]);
1190
- return;
1277
+ return false;
1191
1278
  }
1192
1279
  // matchLabels >= numberOfHashes: the suffix may extend past the labels we
1193
1280
  // tracked in BUFFER, so locate its start by scanning backward for the
@@ -1205,11 +1292,12 @@ function suffixLookup(hostname, options, out) {
1205
1292
  }
1206
1293
  }
1207
1294
  out.publicSuffix = hostname.slice(suffixStart);
1208
- return;
1295
+ return false;
1209
1296
  }
1210
1297
  // if ((matchKind & Result.NORMAL_MATCH) !== 0)
1211
1298
  // For normal match, we just slice the hostname at the beginning of suffix.
1212
1299
  out.publicSuffix = hostname.slice(matchIndex);
1300
+ return false;
1213
1301
  }
1214
1302
 
1215
1303
  // For all methods but 'parse', it does not make sense to allocate an object