jsdomain-parser 1.0.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 +151 -0
- package/dist/jsdomain-parser.cjs.js +9885 -0
- package/dist/jsdomain-parser.cjs.min.js +1 -0
- package/dist/jsdomain-parser.esm.js +9880 -0
- package/dist/jsdomain-parser.esm.min.js +1 -0
- package/dist/jsdomain-parser.js +9890 -0
- package/dist/jsdomain-parser.min.js +1 -0
- package/package.json +33 -0
- package/rollup.config.js +40 -0
- package/src/index.js +46 -0
- package/src/parseTld.js +56 -0
- package/src/tlds.json +9781 -0
- package/test/test.html +51 -0
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "jsdomain-parser",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Parse URLs to extract TLDs, domain, protocols and more",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "rollup -c"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/ToolsOverflow/jsdomain-parser"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"url",
|
|
16
|
+
"parser",
|
|
17
|
+
"domain",
|
|
18
|
+
"parser",
|
|
19
|
+
"tld",
|
|
20
|
+
"extractor",
|
|
21
|
+
"extract",
|
|
22
|
+
"tld"
|
|
23
|
+
],
|
|
24
|
+
"author": "ToolsOverflow",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
28
|
+
"rollup": "^2.79.2",
|
|
29
|
+
"rollup-plugin-commonjs": "^10.1.0",
|
|
30
|
+
"rollup-plugin-node-resolve": "^5.2.0",
|
|
31
|
+
"rollup-plugin-terser": "^7.0.2"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import resolve from "rollup-plugin-node-resolve";
|
|
2
|
+
import commonjs from "rollup-plugin-commonjs";
|
|
3
|
+
import { terser } from "rollup-plugin-terser";
|
|
4
|
+
import json from "@rollup/plugin-json";
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
input: "src/index.js",
|
|
8
|
+
output: [
|
|
9
|
+
{
|
|
10
|
+
file: "dist/jsdomain-parser.cjs.js",
|
|
11
|
+
format: "cjs",
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
file: "dist/jsdomain-parser.cjs.min.js",
|
|
15
|
+
format: "cjs",
|
|
16
|
+
plugins: [terser()],
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
file: "dist/jsdomain-parser.esm.js",
|
|
20
|
+
format: "esm",
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
file: "dist/jsdomain-parser.esm.min.js",
|
|
24
|
+
format: "esm",
|
|
25
|
+
plugins: [terser()],
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
file: "dist/jsdomain-parser.min.js",
|
|
29
|
+
format: "iife",
|
|
30
|
+
name: "DomainParser",
|
|
31
|
+
plugins: [terser()],
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
file: "dist/jsdomain-parser.js",
|
|
35
|
+
format: "iife",
|
|
36
|
+
name: "DomainParser",
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
plugins: [resolve(), commonjs(), json()],
|
|
40
|
+
};
|
package/src/index.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { parseTld } from "./parseTld.js";
|
|
2
|
+
|
|
3
|
+
function parse(url, options = {}) {
|
|
4
|
+
try {
|
|
5
|
+
if (!url.startsWith("http")) url = `http://${url}`;
|
|
6
|
+
const urlObject = new URL(url);
|
|
7
|
+
|
|
8
|
+
const tldData = parseTld(urlObject.hostname, options);
|
|
9
|
+
|
|
10
|
+
let domain = urlObject.hostname;
|
|
11
|
+
const hostnameParts = urlObject.hostname.split(".");
|
|
12
|
+
for (let i = hostnameParts.length - 1; i >= 0; i--) {
|
|
13
|
+
const extended = hostnameParts.slice(i);
|
|
14
|
+
|
|
15
|
+
if (extended.join(".") === tldData.name) {
|
|
16
|
+
domain = hostnameParts[i - 1] + "." + extended;
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const query = {};
|
|
22
|
+
for (const [key, value] of urlObject.searchParams.entries()) {
|
|
23
|
+
query[key] = value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
tld: tldData,
|
|
28
|
+
url: {
|
|
29
|
+
domain: domain,
|
|
30
|
+
origin: urlObject.origin,
|
|
31
|
+
protocol: urlObject.protocol,
|
|
32
|
+
host: urlObject.host,
|
|
33
|
+
hostname: urlObject.hostname,
|
|
34
|
+
port: urlObject.port,
|
|
35
|
+
pathname: urlObject.pathname,
|
|
36
|
+
search: urlObject.search,
|
|
37
|
+
hash: urlObject.hash,
|
|
38
|
+
query,
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
} catch (e) {
|
|
42
|
+
throw new Error(`Invalid URL: ${e}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { parse, parseTld };
|
package/src/parseTld.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import tlds from "./tlds.json";
|
|
2
|
+
|
|
3
|
+
const parseTld = (hostname, options = {}) => {
|
|
4
|
+
const {
|
|
5
|
+
allowUnknown = false,
|
|
6
|
+
allowPrivate = true,
|
|
7
|
+
extendedTlds = [],
|
|
8
|
+
} = options;
|
|
9
|
+
|
|
10
|
+
if (!Array.isArray(extendedTlds)) {
|
|
11
|
+
throw new Error("customTlds must be an array");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const parts = hostname.split(".");
|
|
15
|
+
|
|
16
|
+
const customTlds = extendedTlds.map((tld) => [tld, tld.split(".").length]);
|
|
17
|
+
const icann = { ...tlds.icann, ...Object.fromEntries(customTlds) };
|
|
18
|
+
const privateTlds = tlds.private;
|
|
19
|
+
|
|
20
|
+
let detected = [];
|
|
21
|
+
|
|
22
|
+
for (let p = parts.length - 1; p >= 0; p--) {
|
|
23
|
+
const extended = parts.slice(p);
|
|
24
|
+
const prevPart = parts[p - 1];
|
|
25
|
+
|
|
26
|
+
if (icann[extended.join(".")] && prevPart !== undefined) {
|
|
27
|
+
detected = extended;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (allowPrivate) {
|
|
31
|
+
if (privateTlds[extended.join(".")] && prevPart !== undefined) {
|
|
32
|
+
detected = extended;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (allowUnknown) {
|
|
38
|
+
if (detected.length == 0) {
|
|
39
|
+
detected = parts.slice(-1);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (detected.length == 0) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
"Could not detect TLD. You can set allowUnknown to true for allowing unknown TLDs."
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
name: detected.join("."),
|
|
51
|
+
length: detected.length,
|
|
52
|
+
parts: detected,
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export { parseTld };
|