tldts-icann 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
@@ -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
@@ -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
  }
@@ -1000,7 +1092,7 @@ const exceptionsRoot = 240;
1000
1092
  // only fast when the typed arrays are module-scope monomorphic globals —
1001
1093
  // closing over them (a shared factory) measured ~20% slower. This ICANN build
1002
1094
  // specializes: every terminal node is an ICANN rule, so the mask is constant
1003
- // and isIcann/isPrivate are never written. Keep the two in sync.
1095
+ // and a match is always ICANN. Keep the two in sync.
1004
1096
  const ICANN = 1;
1005
1097
  // `edgeOffset` (where each label starts in `labelText`), `edgeHash` (djb2 of
1006
1098
  // each label) and `wildcardEdge` (each node's '*' edge, or -1) are derived once
@@ -1084,6 +1176,7 @@ function findEdge(node, hash, hostname, start, length) {
1084
1176
  * Walk `hostname`'s labels right-to-left from `root`, recording the deepest
1085
1177
  * ICANN node (with the label boundaries of that match in
1086
1178
  * `matchStart`/`matchEnd`). Returns whether any match was found.
1179
+ * An empty label (leading dot or `..`) never matches a `*` edge.
1087
1180
  */
1088
1181
  function walk(hostname, root) {
1089
1182
  let node = root;
@@ -1095,7 +1188,7 @@ function walk(hostname, root) {
1095
1188
  if (code === 46 /* '.' */) {
1096
1189
  const start = i + 1;
1097
1190
  let edge = findEdge(node, hash >>> 0, hostname, start, end - start);
1098
- if (edge === -1) {
1191
+ if (edge === -1 && end !== start) {
1099
1192
  edge = wildcardEdge[node];
1100
1193
  }
1101
1194
  if (edge === -1) {
@@ -1118,7 +1211,7 @@ function walk(hostname, root) {
1118
1211
  // duplicated rather than folded into the loop (via `i >= -1`) because that
1119
1212
  // extra per-character branch measured slightly slower on the hot path.
1120
1213
  let edge = findEdge(node, hash >>> 0, hostname, 0, end);
1121
- if (edge === -1) {
1214
+ if (edge === -1 && end !== 0) {
1122
1215
  edge = wildcardEdge[node];
1123
1216
  }
1124
1217
  if (edge !== -1) {
@@ -1136,21 +1229,28 @@ function walk(hostname, root) {
1136
1229
  */
1137
1230
  function suffixLookup(hostname, options, out) {
1138
1231
  if (fastPathLookup(hostname, options, out)) {
1139
- return;
1232
+ return false;
1140
1233
  }
1141
1234
  // Exceptions have priority and strip their own left-most label (e.g. the
1142
1235
  // rule '!www.ck' makes the suffix of 'www.ck' be 'ck').
1143
- if (walk(hostname, exceptionsRoot)) {
1236
+ if (options.allowIcannDomains && walk(hostname, exceptionsRoot)) {
1237
+ out.isIcann = true;
1238
+ out.isPrivate = false;
1144
1239
  out.publicSuffix = hostname.slice(matchEnd + 1);
1145
- return;
1240
+ return true;
1146
1241
  }
1147
- if (walk(hostname, rulesRoot)) {
1242
+ if (options.allowIcannDomains && walk(hostname, rulesRoot)) {
1243
+ out.isIcann = true;
1244
+ out.isPrivate = false;
1148
1245
  out.publicSuffix = hostname.slice(matchStart);
1149
- return;
1246
+ return false;
1150
1247
  }
1151
1248
  // No match: the prevailing '*' rule makes the right-most label the suffix.
1249
+ out.isIcann = false;
1250
+ out.isPrivate = false;
1152
1251
  const lastDot = hostname.lastIndexOf('.');
1153
1252
  out.publicSuffix = lastDot === -1 ? hostname : hostname.slice(lastDot + 1);
1253
+ return false;
1154
1254
  }
1155
1255
 
1156
1256
  // For all methods but 'parse', it does not make sense to allocate an object