tldts-icann 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
@@ -80,8 +80,11 @@ your inputs.
80
80
  // If set to `false` we assume only URLs will be given as input, which
81
81
  // speed-ups processing.
82
82
  mixedInputs: boolean;
83
- // Specifies extra valid suffixes (default: null)
83
+ // Hostnames to treat as registrable domains, e.g. ['localhost'] (default: null)
84
84
  validHosts: string[] | null;
85
+ // Extra public suffix rules, in PSL syntax, per section (default: null)
86
+ // See "Adding public suffixes" below.
87
+ extraSuffixes: { icann?: readonly string[]; private?: readonly string[] } | null;
85
88
  }
86
89
  ```
87
90
 
@@ -282,6 +285,21 @@ tldts.getDomain('localhost', { validHosts: ['localhost'] }); // returns 'localho
282
285
  tldts.getSubdomain('vhost.localhost', { validHosts: ['localhost'] }); // returns 'vhost'
283
286
  ```
284
287
 
288
+ ## Adding public suffixes
289
+
290
+ Pass suffixes the Public Suffix List does not include with the `extraSuffixes`
291
+ option, in PSL syntax:
292
+
293
+ ```js
294
+ const { getDomain } = require('tldts-icann');
295
+
296
+ getDomain('foo.us.com'); // 'us.com'
297
+ getDomain('foo.us.com', { extraSuffixes: { icann: ['us.com'] } }); // 'foo.us.com'
298
+ ```
299
+
300
+ See [Adding public suffixes](https://github.com/remusao/tldts/tree/master/packages/tldts#adding-public-suffixes)
301
+ for the rule syntax and semantics.
302
+
285
303
  ## Updating the TLDs List
286
304
 
287
305
  `tldts` made the opinionated choice of shipping with a list of suffixes directly
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
  }
@@ -985,7 +1087,7 @@ const exceptionsRoot = 240;
985
1087
  // only fast when the typed arrays are module-scope monomorphic globals —
986
1088
  // closing over them (a shared factory) measured ~20% slower. This ICANN build
987
1089
  // specializes: every terminal node is an ICANN rule, so the mask is constant
988
- // and isIcann/isPrivate are never written. Keep the two in sync.
1090
+ // and a match is always ICANN. Keep the two in sync.
989
1091
  const ICANN = 1;
990
1092
  // `edgeOffset` (where each label starts in `labelText`), `edgeHash` (djb2 of
991
1093
  // each label) and `wildcardEdge` (each node's '*' edge, or -1) are derived once
@@ -1121,21 +1223,28 @@ function walk(hostname, root) {
1121
1223
  */
1122
1224
  function suffixLookup(hostname, options, out) {
1123
1225
  if (fastPathLookup(hostname, options, out)) {
1124
- return;
1226
+ return false;
1125
1227
  }
1126
1228
  // Exceptions have priority and strip their own left-most label (e.g. the
1127
1229
  // rule '!www.ck' makes the suffix of 'www.ck' be 'ck').
1128
- if (walk(hostname, exceptionsRoot)) {
1230
+ if (options.allowIcannDomains && walk(hostname, exceptionsRoot)) {
1231
+ out.isIcann = true;
1232
+ out.isPrivate = false;
1129
1233
  out.publicSuffix = hostname.slice(matchEnd + 1);
1130
- return;
1234
+ return true;
1131
1235
  }
1132
- if (walk(hostname, rulesRoot)) {
1236
+ if (options.allowIcannDomains && walk(hostname, rulesRoot)) {
1237
+ out.isIcann = true;
1238
+ out.isPrivate = false;
1133
1239
  out.publicSuffix = hostname.slice(matchStart);
1134
- return;
1240
+ return false;
1135
1241
  }
1136
1242
  // No match: the prevailing '*' rule makes the right-most label the suffix.
1243
+ out.isIcann = false;
1244
+ out.isPrivate = false;
1137
1245
  const lastDot = hostname.lastIndexOf('.');
1138
1246
  out.publicSuffix = lastDot === -1 ? hostname : hostname.slice(lastDot + 1);
1247
+ return false;
1139
1248
  }
1140
1249
 
1141
1250
  // For all methods but 'parse', it does not make sense to allocate an object