jsdomain-parser 1.0.6 → 1.0.7
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 +1 -0
- package/dist/jsdomain-parser.cjs.js +65 -21
- package/dist/jsdomain-parser.cjs.min.js +1 -1
- package/dist/jsdomain-parser.esm.js +65 -21
- package/dist/jsdomain-parser.esm.min.js +1 -1
- package/dist/jsdomain-parser.js +65 -21
- package/dist/jsdomain-parser.min.js +1 -1
- package/{test/test.html → example/example.html} +1 -1
- package/index.js +1 -42
- package/package.json +4 -2
- package/src/parse.js +65 -0
- package/src/parseTld.js +12 -2
- package/src/parseUrl.js +15 -0
- package/src/tlds.json +1 -0
- package/test/parse.test.js +152 -0
package/README.md
CHANGED
|
@@ -8,6 +8,7 @@ JS Domain Parser is a lightweight JavaScript library designed to parse and extra
|
|
|
8
8
|
- Detects TLDs using a comprehensive list of ICANN and private TLDs.
|
|
9
9
|
- Supports custom TLDs via configuration.
|
|
10
10
|
- Allows toggling support for private and unknown TLDs.
|
|
11
|
+
- Supports all protocols (e.g., `http`, `https`, `ssh`, `ftp`, `telnet`, etc.).
|
|
11
12
|
|
|
12
13
|
## Installation
|
|
13
14
|
|
|
@@ -2,6 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
const parseUrl = (url) => {
|
|
6
|
+
if (!url) throw new Error("Invalid domain name");
|
|
7
|
+
|
|
8
|
+
const haveProtocol = /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(url);
|
|
9
|
+
if (!haveProtocol) url = `http://${url}`;
|
|
10
|
+
const urlObject = new URL(url);
|
|
11
|
+
|
|
12
|
+
// check for hostname validity
|
|
13
|
+
if (urlObject.hostname.split(".").findIndex((it) => it.trim() == "") >= 0)
|
|
14
|
+
throw new Error("Invalid domain name");
|
|
15
|
+
|
|
16
|
+
return urlObject;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
var parseUrl_1 = parseUrl;
|
|
20
|
+
|
|
5
21
|
var icann = {
|
|
6
22
|
ac: 1,
|
|
7
23
|
"com.ac": 2,
|
|
@@ -9781,6 +9797,7 @@ var tlds$1 = {
|
|
|
9781
9797
|
"meinforum.net": 2,
|
|
9782
9798
|
"affinitylottery.org.uk": 3,
|
|
9783
9799
|
"raffleentry.org.uk": 3,
|
|
9800
|
+
localhost: 1,
|
|
9784
9801
|
"weeklylottery.org.uk": 3
|
|
9785
9802
|
}
|
|
9786
9803
|
};
|
|
@@ -9797,7 +9814,7 @@ function getCjsExportFromNamespace (n) {
|
|
|
9797
9814
|
|
|
9798
9815
|
var tlds = getCjsExportFromNamespace(tlds$2);
|
|
9799
9816
|
|
|
9800
|
-
const parseTld = (
|
|
9817
|
+
const parseTld = (url, options = {}) => {
|
|
9801
9818
|
const {
|
|
9802
9819
|
allowUnknown = false,
|
|
9803
9820
|
allowPrivate = true,
|
|
@@ -9808,6 +9825,12 @@ const parseTld = (hostname, options = {}) => {
|
|
|
9808
9825
|
throw new Error("customTlds must be an array");
|
|
9809
9826
|
}
|
|
9810
9827
|
|
|
9828
|
+
const { hostname } = parseUrl_1(url);
|
|
9829
|
+
|
|
9830
|
+
// handle localhost as a special case
|
|
9831
|
+
if (hostname == "localhost" && allowPrivate)
|
|
9832
|
+
return { name: "localhost", length: 1, parts: ["localhost"] };
|
|
9833
|
+
|
|
9811
9834
|
const parts = hostname.split(".");
|
|
9812
9835
|
|
|
9813
9836
|
const customTlds = extendedTlds.map((tld) => [tld, tld.split(".").length]);
|
|
@@ -9837,7 +9860,9 @@ const parseTld = (hostname, options = {}) => {
|
|
|
9837
9860
|
}
|
|
9838
9861
|
}
|
|
9839
9862
|
|
|
9840
|
-
|
|
9863
|
+
const isIP = /^\d{1,3}(\.\d{1,3}){3}$/.test(hostname);
|
|
9864
|
+
|
|
9865
|
+
if (detected.length == 0 && !isIP) {
|
|
9841
9866
|
throw new Error(
|
|
9842
9867
|
"Could not detect TLD. You can set allowUnknown to true for allowing unknown TLDs."
|
|
9843
9868
|
);
|
|
@@ -9854,17 +9879,29 @@ var parseTld_1 = parseTld;
|
|
|
9854
9879
|
|
|
9855
9880
|
function parse(url, options = {}) {
|
|
9856
9881
|
try {
|
|
9857
|
-
|
|
9858
|
-
const urlObject = new URL(url);
|
|
9882
|
+
const urlObject = parseUrl_1(url);
|
|
9859
9883
|
|
|
9860
|
-
|
|
9884
|
+
if (
|
|
9885
|
+
urlObject.hostname.split(".").filter((i) => i.trim()).length <= 1 &&
|
|
9886
|
+
urlObject.hostname !== "localhost"
|
|
9887
|
+
) {
|
|
9888
|
+
throw new Error(
|
|
9889
|
+
`Invalid domain name: "${urlObject.hostname}" is not a valid domain.`
|
|
9890
|
+
);
|
|
9891
|
+
}
|
|
9892
|
+
|
|
9893
|
+
const tldData = parseTld_1(url, options);
|
|
9861
9894
|
|
|
9862
9895
|
let domain = urlObject.hostname;
|
|
9863
9896
|
const hostnameParts = urlObject.hostname.split(".");
|
|
9864
9897
|
for (let i = hostnameParts.length - 1; i >= 0; i--) {
|
|
9865
9898
|
const extended = hostnameParts.slice(i);
|
|
9899
|
+
|
|
9866
9900
|
if (extended.join(".") === tldData.name) {
|
|
9867
|
-
|
|
9901
|
+
if (hostnameParts[i - 1]) {
|
|
9902
|
+
domain = hostnameParts[i - 1] + "." + extended.join(".");
|
|
9903
|
+
}
|
|
9904
|
+
|
|
9868
9905
|
break;
|
|
9869
9906
|
}
|
|
9870
9907
|
}
|
|
@@ -9874,29 +9911,36 @@ function parse(url, options = {}) {
|
|
|
9874
9911
|
query[key] = value;
|
|
9875
9912
|
}
|
|
9876
9913
|
|
|
9914
|
+
let urlData = {
|
|
9915
|
+
domain: domain,
|
|
9916
|
+
origin: urlObject.origin,
|
|
9917
|
+
protocol: urlObject.protocol,
|
|
9918
|
+
host: urlObject.host,
|
|
9919
|
+
hostname: urlObject.hostname,
|
|
9920
|
+
port: urlObject.port,
|
|
9921
|
+
pathname: urlObject.pathname,
|
|
9922
|
+
search: urlObject.search,
|
|
9923
|
+
hash: urlObject.hash,
|
|
9924
|
+
query,
|
|
9925
|
+
};
|
|
9926
|
+
|
|
9927
|
+
// handle for protocols that aren't supported by URL constructor
|
|
9928
|
+
if (urlObject.origin === "null") {
|
|
9929
|
+
urlData.origin = urlObject.protocol + "//" + urlObject.hostname;
|
|
9930
|
+
}
|
|
9931
|
+
|
|
9877
9932
|
return {
|
|
9878
9933
|
tld: tldData,
|
|
9879
|
-
url:
|
|
9880
|
-
domain: domain,
|
|
9881
|
-
origin: urlObject.origin,
|
|
9882
|
-
protocol: urlObject.protocol,
|
|
9883
|
-
host: urlObject.host,
|
|
9884
|
-
hostname: urlObject.hostname,
|
|
9885
|
-
port: urlObject.port,
|
|
9886
|
-
pathname: urlObject.pathname,
|
|
9887
|
-
search: urlObject.search,
|
|
9888
|
-
hash: urlObject.hash,
|
|
9889
|
-
query,
|
|
9890
|
-
},
|
|
9934
|
+
url: urlData,
|
|
9891
9935
|
};
|
|
9892
9936
|
} catch (e) {
|
|
9893
9937
|
throw new Error(`Invalid URL: ${e}`);
|
|
9894
9938
|
}
|
|
9895
9939
|
}
|
|
9896
9940
|
|
|
9897
|
-
|
|
9898
|
-
|
|
9899
|
-
var jsdomainParser = { parse, parseTld: parseTld_1 };
|
|
9941
|
+
var parse_1 = parse;
|
|
9942
|
+
|
|
9943
|
+
var jsdomainParser = { parse: parse_1, parseTld: parseTld_1 };
|
|
9900
9944
|
var jsdomainParser_1 = jsdomainParser.parse;
|
|
9901
9945
|
var jsdomainParser_2 = jsdomainParser.parseTld;
|
|
9902
9946
|
|