jsdomain-parser 1.0.6 → 1.0.8
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 +7 -2
- package/dist/jsdomain-parser.cjs.js +67 -22
- package/dist/jsdomain-parser.cjs.min.js +1 -1
- package/dist/jsdomain-parser.esm.js +67 -22
- package/dist/jsdomain-parser.esm.min.js +1 -1
- package/dist/{jsdomain-parser.js → jsdomain-parser.umd.js} +73 -27
- package/dist/jsdomain-parser.umd.min.js +1 -0
- package/{test/test.html → example/example.html} +4 -4
- package/index.js +1 -42
- package/package.json +4 -2
- package/rollup.config.js +7 -7
- 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/dist/jsdomain-parser.min.js +0 -1
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
|
|
|
@@ -15,12 +16,16 @@ JS Domain Parser is a lightweight JavaScript library designed to parse and extra
|
|
|
15
16
|
npm install jsdomain-parser
|
|
16
17
|
```
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
or you can include `jsdomain-parser` directly in your project by adding the following `<script>` tag to your HTML file:
|
|
19
22
|
|
|
20
23
|
```html
|
|
21
|
-
<script src="./dist/jsdomain-parser.min.js"></script>
|
|
24
|
+
<script src="./dist/jsdomain-parser.umd.min.js"></script>
|
|
22
25
|
```
|
|
23
26
|
|
|
27
|
+
This will expose the `jsDomainParser` global object in the browser.
|
|
28
|
+
|
|
24
29
|
## Usage
|
|
25
30
|
|
|
26
31
|
### Example 1: Basic Parsing
|
|
@@ -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,10 @@ const parseTld = (hostname, options = {}) => {
|
|
|
9837
9860
|
}
|
|
9838
9861
|
}
|
|
9839
9862
|
|
|
9840
|
-
|
|
9863
|
+
// handle case where hostname is an IP address
|
|
9864
|
+
const isIP = /^\d{1,3}(\.\d{1,3}){3}$/.test(hostname);
|
|
9865
|
+
|
|
9866
|
+
if (detected.length == 0 && !isIP) {
|
|
9841
9867
|
throw new Error(
|
|
9842
9868
|
"Could not detect TLD. You can set allowUnknown to true for allowing unknown TLDs."
|
|
9843
9869
|
);
|
|
@@ -9854,17 +9880,29 @@ var parseTld_1 = parseTld;
|
|
|
9854
9880
|
|
|
9855
9881
|
function parse(url, options = {}) {
|
|
9856
9882
|
try {
|
|
9857
|
-
|
|
9858
|
-
const urlObject = new URL(url);
|
|
9883
|
+
const urlObject = parseUrl_1(url);
|
|
9859
9884
|
|
|
9860
|
-
|
|
9885
|
+
if (
|
|
9886
|
+
urlObject.hostname.split(".").filter((i) => i.trim()).length <= 1 &&
|
|
9887
|
+
urlObject.hostname !== "localhost"
|
|
9888
|
+
) {
|
|
9889
|
+
throw new Error(
|
|
9890
|
+
`Invalid domain name: "${urlObject.hostname}" is not a valid domain.`
|
|
9891
|
+
);
|
|
9892
|
+
}
|
|
9893
|
+
|
|
9894
|
+
const tldData = parseTld_1(url, options);
|
|
9861
9895
|
|
|
9862
9896
|
let domain = urlObject.hostname;
|
|
9863
9897
|
const hostnameParts = urlObject.hostname.split(".");
|
|
9864
9898
|
for (let i = hostnameParts.length - 1; i >= 0; i--) {
|
|
9865
9899
|
const extended = hostnameParts.slice(i);
|
|
9900
|
+
|
|
9866
9901
|
if (extended.join(".") === tldData.name) {
|
|
9867
|
-
|
|
9902
|
+
if (hostnameParts[i - 1]) {
|
|
9903
|
+
domain = hostnameParts[i - 1] + "." + extended.join(".");
|
|
9904
|
+
}
|
|
9905
|
+
|
|
9868
9906
|
break;
|
|
9869
9907
|
}
|
|
9870
9908
|
}
|
|
@@ -9874,29 +9912,36 @@ function parse(url, options = {}) {
|
|
|
9874
9912
|
query[key] = value;
|
|
9875
9913
|
}
|
|
9876
9914
|
|
|
9915
|
+
let urlData = {
|
|
9916
|
+
domain: domain,
|
|
9917
|
+
origin: urlObject.origin,
|
|
9918
|
+
protocol: urlObject.protocol,
|
|
9919
|
+
host: urlObject.host,
|
|
9920
|
+
hostname: urlObject.hostname,
|
|
9921
|
+
port: urlObject.port,
|
|
9922
|
+
pathname: urlObject.pathname,
|
|
9923
|
+
search: urlObject.search,
|
|
9924
|
+
hash: urlObject.hash,
|
|
9925
|
+
query,
|
|
9926
|
+
};
|
|
9927
|
+
|
|
9928
|
+
// handle for protocols that aren't supported by URL constructor
|
|
9929
|
+
if (urlObject.origin === "null") {
|
|
9930
|
+
urlData.origin = urlObject.protocol + "//" + urlObject.hostname;
|
|
9931
|
+
}
|
|
9932
|
+
|
|
9877
9933
|
return {
|
|
9878
9934
|
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
|
-
},
|
|
9935
|
+
url: urlData,
|
|
9891
9936
|
};
|
|
9892
9937
|
} catch (e) {
|
|
9893
|
-
throw new Error(`Invalid URL: ${e}`);
|
|
9938
|
+
throw new Error(`Invalid URL: ${e.message}`);
|
|
9894
9939
|
}
|
|
9895
9940
|
}
|
|
9896
9941
|
|
|
9897
|
-
|
|
9898
|
-
|
|
9899
|
-
var jsdomainParser = { parse, parseTld: parseTld_1 };
|
|
9942
|
+
var parse_1 = parse;
|
|
9943
|
+
|
|
9944
|
+
var jsdomainParser = { parse: parse_1, parseTld: parseTld_1 };
|
|
9900
9945
|
var jsdomainParser_1 = jsdomainParser.parse;
|
|
9901
9946
|
var jsdomainParser_2 = jsdomainParser.parseTld;
|
|
9902
9947
|
|