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.
@@ -1,3 +1,19 @@
1
+ const parseUrl = (url) => {
2
+ if (!url) throw new Error("Invalid domain name");
3
+
4
+ const haveProtocol = /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(url);
5
+ if (!haveProtocol) url = `http://${url}`;
6
+ const urlObject = new URL(url);
7
+
8
+ // check for hostname validity
9
+ if (urlObject.hostname.split(".").findIndex((it) => it.trim() == "") >= 0)
10
+ throw new Error("Invalid domain name");
11
+
12
+ return urlObject;
13
+ };
14
+
15
+ var parseUrl_1 = parseUrl;
16
+
1
17
  var icann = {
2
18
  ac: 1,
3
19
  "com.ac": 2,
@@ -9777,6 +9793,7 @@ var tlds$1 = {
9777
9793
  "meinforum.net": 2,
9778
9794
  "affinitylottery.org.uk": 3,
9779
9795
  "raffleentry.org.uk": 3,
9796
+ localhost: 1,
9780
9797
  "weeklylottery.org.uk": 3
9781
9798
  }
9782
9799
  };
@@ -9793,7 +9810,7 @@ function getCjsExportFromNamespace (n) {
9793
9810
 
9794
9811
  var tlds = getCjsExportFromNamespace(tlds$2);
9795
9812
 
9796
- const parseTld = (hostname, options = {}) => {
9813
+ const parseTld = (url, options = {}) => {
9797
9814
  const {
9798
9815
  allowUnknown = false,
9799
9816
  allowPrivate = true,
@@ -9804,6 +9821,12 @@ const parseTld = (hostname, options = {}) => {
9804
9821
  throw new Error("customTlds must be an array");
9805
9822
  }
9806
9823
 
9824
+ const { hostname } = parseUrl_1(url);
9825
+
9826
+ // handle localhost as a special case
9827
+ if (hostname == "localhost" && allowPrivate)
9828
+ return { name: "localhost", length: 1, parts: ["localhost"] };
9829
+
9807
9830
  const parts = hostname.split(".");
9808
9831
 
9809
9832
  const customTlds = extendedTlds.map((tld) => [tld, tld.split(".").length]);
@@ -9833,7 +9856,10 @@ const parseTld = (hostname, options = {}) => {
9833
9856
  }
9834
9857
  }
9835
9858
 
9836
- if (detected.length == 0) {
9859
+ // handle case where hostname is an IP address
9860
+ const isIP = /^\d{1,3}(\.\d{1,3}){3}$/.test(hostname);
9861
+
9862
+ if (detected.length == 0 && !isIP) {
9837
9863
  throw new Error(
9838
9864
  "Could not detect TLD. You can set allowUnknown to true for allowing unknown TLDs."
9839
9865
  );
@@ -9850,17 +9876,29 @@ var parseTld_1 = parseTld;
9850
9876
 
9851
9877
  function parse(url, options = {}) {
9852
9878
  try {
9853
- if (!url.startsWith("http")) url = `http://${url}`;
9854
- const urlObject = new URL(url);
9879
+ const urlObject = parseUrl_1(url);
9855
9880
 
9856
- const tldData = parseTld_1(urlObject.hostname, options);
9881
+ if (
9882
+ urlObject.hostname.split(".").filter((i) => i.trim()).length <= 1 &&
9883
+ urlObject.hostname !== "localhost"
9884
+ ) {
9885
+ throw new Error(
9886
+ `Invalid domain name: "${urlObject.hostname}" is not a valid domain.`
9887
+ );
9888
+ }
9889
+
9890
+ const tldData = parseTld_1(url, options);
9857
9891
 
9858
9892
  let domain = urlObject.hostname;
9859
9893
  const hostnameParts = urlObject.hostname.split(".");
9860
9894
  for (let i = hostnameParts.length - 1; i >= 0; i--) {
9861
9895
  const extended = hostnameParts.slice(i);
9896
+
9862
9897
  if (extended.join(".") === tldData.name) {
9863
- domain = hostnameParts[i - 1] + "." + extended.join(".");
9898
+ if (hostnameParts[i - 1]) {
9899
+ domain = hostnameParts[i - 1] + "." + extended.join(".");
9900
+ }
9901
+
9864
9902
  break;
9865
9903
  }
9866
9904
  }
@@ -9870,29 +9908,36 @@ function parse(url, options = {}) {
9870
9908
  query[key] = value;
9871
9909
  }
9872
9910
 
9911
+ let urlData = {
9912
+ domain: domain,
9913
+ origin: urlObject.origin,
9914
+ protocol: urlObject.protocol,
9915
+ host: urlObject.host,
9916
+ hostname: urlObject.hostname,
9917
+ port: urlObject.port,
9918
+ pathname: urlObject.pathname,
9919
+ search: urlObject.search,
9920
+ hash: urlObject.hash,
9921
+ query,
9922
+ };
9923
+
9924
+ // handle for protocols that aren't supported by URL constructor
9925
+ if (urlObject.origin === "null") {
9926
+ urlData.origin = urlObject.protocol + "//" + urlObject.hostname;
9927
+ }
9928
+
9873
9929
  return {
9874
9930
  tld: tldData,
9875
- url: {
9876
- domain: domain,
9877
- origin: urlObject.origin,
9878
- protocol: urlObject.protocol,
9879
- host: urlObject.host,
9880
- hostname: urlObject.hostname,
9881
- port: urlObject.port,
9882
- pathname: urlObject.pathname,
9883
- search: urlObject.search,
9884
- hash: urlObject.hash,
9885
- query,
9886
- },
9931
+ url: urlData,
9887
9932
  };
9888
9933
  } catch (e) {
9889
- throw new Error(`Invalid URL: ${e}`);
9934
+ throw new Error(`Invalid URL: ${e.message}`);
9890
9935
  }
9891
9936
  }
9892
9937
 
9893
- console.log(parse("google.co.in"));
9894
-
9895
- var jsdomainParser = { parse, parseTld: parseTld_1 };
9938
+ var parse_1 = parse;
9939
+
9940
+ var jsdomainParser = { parse: parse_1, parseTld: parseTld_1 };
9896
9941
  var jsdomainParser_1 = jsdomainParser.parse;
9897
9942
  var jsdomainParser_2 = jsdomainParser.parseTld;
9898
9943