tldts-experimental 7.4.27 → 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
@@ -229,7 +229,7 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
229
229
  start += 2;
230
230
  }
231
231
  else {
232
- const indexOfProtocol = url.indexOf(':/', start);
232
+ let indexOfProtocol = url.indexOf(':/', start);
233
233
  if (indexOfProtocol !== -1) {
234
234
  // "scheme://…". Classify the scheme, then position `start` at the host.
235
235
  const special = getSpecialScheme(url, start, indexOfProtocol);
@@ -272,25 +272,40 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
272
272
  code === 13 /* CR */) {
273
273
  return extractHostname(url.replace(CONTROL_CHARS, ''), urlIsValidHostname, validate);
274
274
  }
275
- return null;
275
+ // A '/', '\', '?' or '#' first puts this ':/' in a path, query or
276
+ // fragment ("a.com/r?u=https://x"): re-read the input without it.
277
+ for (let j = i; j < indexOfProtocol; j += 1) {
278
+ const c = url.charCodeAt(j);
279
+ if (c === 47 || c === 92 || c === 63 || c === 35) {
280
+ indexOfProtocol = -1;
281
+ break;
282
+ }
283
+ }
284
+ if (indexOfProtocol !== -1) {
285
+ return null;
286
+ }
287
+ break;
276
288
  }
277
289
  }
278
- // A non-special scheme has an authority only after "//" (else it is an
279
- // opaque path with no host). `indexOf(':/')` already gave the first '/'.
280
- if (url.charCodeAt(indexOfProtocol + 2) === 47 /* '/' */) {
281
- isSpecial = false;
282
- start = indexOfProtocol + 3;
283
- }
284
- else {
285
- return null;
290
+ if (indexOfProtocol !== -1) {
291
+ // A non-special scheme has an authority only after "//" (else it is
292
+ // an opaque path with no host). `indexOf(':/')` already gave the
293
+ // first '/'.
294
+ if (url.charCodeAt(indexOfProtocol + 2) === 47 /* '/' */) {
295
+ isSpecial = false;
296
+ start = indexOfProtocol + 3;
297
+ }
298
+ else {
299
+ return null;
300
+ }
286
301
  }
287
302
  }
288
303
  }
289
- else if (url.charCodeAt(start) !== 91 /* '[' */) {
290
- // Cold path: no scheme "://", and not a bare IPv6 literal (whose first
291
- // ':' would otherwise look like a scheme separator; "[…]" falls through
292
- // to the ipv6 handling below). May be a bare host, a host:port, a
293
- // user@host, a slash-less special scheme ("https:host"), or an opaque
304
+ if (indexOfProtocol === -1 && url.charCodeAt(start) !== 91 /* '[' */) {
305
+ // Cold path: no "scheme://" prefix, and not a bare IPv6 literal (whose
306
+ // first ':' would otherwise look like a scheme separator; "[…]" falls
307
+ // through to the ipv6 handling below). May be a bare host, a host:port,
308
+ // a user@host, a slash-less special scheme ("https:host"), or an opaque
294
309
  // URI ("mailto:", "tel:", "urn:…").
295
310
  let indexOfColon = -1;
296
311
  for (let i = start; i < end; i += 1) {
@@ -341,7 +356,7 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
341
356
  if (!allDigits) {
342
357
  const special = getSpecialScheme(url, start, indexOfColon);
343
358
  if (special === 0) {
344
- // No "://" anywhere on the cold path and not a special scheme.
359
+ // No "scheme://" on the cold path and not a special scheme.
345
360
  // A second ':' before the host's end marks a bare, unbracketed
346
361
  // IPv6 literal ("2a01:e35::1"): fall through and let the host
347
362
  // loop + isIp classify it. Without one this is an opaque path
@@ -757,12 +772,96 @@ function isValidHostname (hostname) {
757
772
  lastCharCode !== 45);
758
773
  }
759
774
 
760
- 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, }) {
761
859
  return {
762
860
  allowIcannDomains,
763
861
  allowPrivateDomains,
764
862
  detectIp,
765
863
  detectSpecialUse,
864
+ extraSuffixes,
766
865
  extractHostname,
767
866
  mixedInputs,
768
867
  validHosts,
@@ -883,8 +982,11 @@ function parseImpl(url, step, suffixLookup, partialOptions, result) {
883
982
  if (step === 5 /* FLAG.ALL */ && options.detectSpecialUse) {
884
983
  result.isSpecialUse = isSpecialUse(result.hostname);
885
984
  }
886
- // Extract public suffix
887
- 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
+ }
888
990
  if (step === 2 /* FLAG.PUBLIC_SUFFIX */ || result.publicSuffix === null) {
889
991
  return result;
890
992
  }
@@ -1044,7 +1146,7 @@ function hashHostnameLabelsBackward(hostname, maximumNumberOfLabels) {
1044
1146
  */
1045
1147
  function suffixLookup(hostname, options, out) {
1046
1148
  if (fastPathLookup(hostname, options, out)) {
1047
- return;
1149
+ return false;
1048
1150
  }
1049
1151
  const { allowIcannDomains, allowPrivateDomains } = options;
1050
1152
  // Keep track of longest match
@@ -1157,14 +1259,14 @@ function suffixLookup(hostname, options, out) {
1157
1259
  if (matchIndex === -1) {
1158
1260
  out.publicSuffix =
1159
1261
  numberOfHashes === 1 ? hostname : hostname.slice(BUFFER[1]);
1160
- return;
1262
+ return false;
1161
1263
  }
1162
1264
  // If match is an exception, this means that we need to count less label.
1163
1265
  // For example, exception rule !foo.com would yield suffix 'com', so we need
1164
1266
  // to locate the next dot and slice from there.
1165
1267
  if ((matchKind & 4 /* Result.EXCEPTION_MATCH */) !== 0) {
1166
1268
  out.publicSuffix = hostname.slice(BUFFER[((matchLabels - 2) << 1) + 1]);
1167
- return;
1269
+ return true;
1168
1270
  }
1169
1271
  // If match is a wildcard, we need to match one more label. If wildcard rule
1170
1272
  // was *.com, we would have stored only 'com' in the packed structure and we
@@ -1172,7 +1274,7 @@ function suffixLookup(hostname, options, out) {
1172
1274
  if ((matchKind & 16 /* Result.WILDCARD_MATCH */) !== 0) {
1173
1275
  if (matchLabels < numberOfHashes) {
1174
1276
  out.publicSuffix = hostname.slice(BUFFER[((matchLabels - 1) << 1) + 1]);
1175
- return;
1277
+ return false;
1176
1278
  }
1177
1279
  // matchLabels >= numberOfHashes: the suffix may extend past the labels we
1178
1280
  // tracked in BUFFER, so locate its start by scanning backward for the
@@ -1190,11 +1292,12 @@ function suffixLookup(hostname, options, out) {
1190
1292
  }
1191
1293
  }
1192
1294
  out.publicSuffix = hostname.slice(suffixStart);
1193
- return;
1295
+ return false;
1194
1296
  }
1195
1297
  // if ((matchKind & Result.NORMAL_MATCH) !== 0)
1196
1298
  // For normal match, we just slice the hostname at the beginning of suffix.
1197
1299
  out.publicSuffix = hostname.slice(matchIndex);
1300
+ return false;
1198
1301
  }
1199
1302
 
1200
1303
  // For all methods but 'parse', it does not make sense to allocate an object