tldts-icann 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 +19 -1
- package/dist/cjs/index.js +103 -9
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/src/suffix-trie.js +13 -6
- package/dist/cjs/src/suffix-trie.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/es6/src/suffix-trie.js +13 -6
- package/dist/es6/src/suffix-trie.js.map +1 -1
- package/dist/es6/tsconfig.bundle.tsbuildinfo +1 -1
- package/dist/index.cjs.min.js +1 -1
- package/dist/index.cjs.min.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/dist/types/src/suffix-trie.d.ts +1 -1
- package/package.json +4 -4
- package/src/suffix-trie.ts +14 -7
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
|
-
//
|
|
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
|
-
|
|
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
|
}
|
|
@@ -1000,7 +1087,7 @@ const exceptionsRoot = 240;
|
|
|
1000
1087
|
// only fast when the typed arrays are module-scope monomorphic globals —
|
|
1001
1088
|
// closing over them (a shared factory) measured ~20% slower. This ICANN build
|
|
1002
1089
|
// specializes: every terminal node is an ICANN rule, so the mask is constant
|
|
1003
|
-
// and
|
|
1090
|
+
// and a match is always ICANN. Keep the two in sync.
|
|
1004
1091
|
const ICANN = 1;
|
|
1005
1092
|
// `edgeOffset` (where each label starts in `labelText`), `edgeHash` (djb2 of
|
|
1006
1093
|
// each label) and `wildcardEdge` (each node's '*' edge, or -1) are derived once
|
|
@@ -1136,21 +1223,28 @@ function walk(hostname, root) {
|
|
|
1136
1223
|
*/
|
|
1137
1224
|
function suffixLookup(hostname, options, out) {
|
|
1138
1225
|
if (fastPathLookup(hostname, options, out)) {
|
|
1139
|
-
return;
|
|
1226
|
+
return false;
|
|
1140
1227
|
}
|
|
1141
1228
|
// Exceptions have priority and strip their own left-most label (e.g. the
|
|
1142
1229
|
// rule '!www.ck' makes the suffix of 'www.ck' be 'ck').
|
|
1143
|
-
if (walk(hostname, exceptionsRoot)) {
|
|
1230
|
+
if (options.allowIcannDomains && walk(hostname, exceptionsRoot)) {
|
|
1231
|
+
out.isIcann = true;
|
|
1232
|
+
out.isPrivate = false;
|
|
1144
1233
|
out.publicSuffix = hostname.slice(matchEnd + 1);
|
|
1145
|
-
return;
|
|
1234
|
+
return true;
|
|
1146
1235
|
}
|
|
1147
|
-
if (walk(hostname, rulesRoot)) {
|
|
1236
|
+
if (options.allowIcannDomains && walk(hostname, rulesRoot)) {
|
|
1237
|
+
out.isIcann = true;
|
|
1238
|
+
out.isPrivate = false;
|
|
1148
1239
|
out.publicSuffix = hostname.slice(matchStart);
|
|
1149
|
-
return;
|
|
1240
|
+
return false;
|
|
1150
1241
|
}
|
|
1151
1242
|
// No match: the prevailing '*' rule makes the right-most label the suffix.
|
|
1243
|
+
out.isIcann = false;
|
|
1244
|
+
out.isPrivate = false;
|
|
1152
1245
|
const lastDot = hostname.lastIndexOf('.');
|
|
1153
1246
|
out.publicSuffix = lastDot === -1 ? hostname : hostname.slice(lastDot + 1);
|
|
1247
|
+
return false;
|
|
1154
1248
|
}
|
|
1155
1249
|
|
|
1156
1250
|
// For all methods but 'parse', it does not make sense to allocate an object
|