jsdomain-parser 1.0.5 → 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.
@@ -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,9 @@ const parseTld = (hostname, options = {}) => {
9833
9856
  }
9834
9857
  }
9835
9858
 
9836
- if (detected.length == 0) {
9859
+ const isIP = /^\d{1,3}(\.\d{1,3}){3}$/.test(hostname);
9860
+
9861
+ if (detected.length == 0 && !isIP) {
9837
9862
  throw new Error(
9838
9863
  "Could not detect TLD. You can set allowUnknown to true for allowing unknown TLDs."
9839
9864
  );
@@ -9850,17 +9875,29 @@ var parseTld_1 = parseTld;
9850
9875
 
9851
9876
  function parse(url, options = {}) {
9852
9877
  try {
9853
- if (!url.startsWith("http")) url = `http://${url}`;
9854
- const urlObject = new URL(url);
9878
+ const urlObject = parseUrl_1(url);
9855
9879
 
9856
- const tldData = parseTld_1(urlObject.hostname, options);
9880
+ if (
9881
+ urlObject.hostname.split(".").filter((i) => i.trim()).length <= 1 &&
9882
+ urlObject.hostname !== "localhost"
9883
+ ) {
9884
+ throw new Error(
9885
+ `Invalid domain name: "${urlObject.hostname}" is not a valid domain.`
9886
+ );
9887
+ }
9888
+
9889
+ const tldData = parseTld_1(url, options);
9857
9890
 
9858
9891
  let domain = urlObject.hostname;
9859
9892
  const hostnameParts = urlObject.hostname.split(".");
9860
9893
  for (let i = hostnameParts.length - 1; i >= 0; i--) {
9861
9894
  const extended = hostnameParts.slice(i);
9895
+
9862
9896
  if (extended.join(".") === tldData.name) {
9863
- domain = hostnameParts[i - 1] + "." + extended.join(".");
9897
+ if (hostnameParts[i - 1]) {
9898
+ domain = hostnameParts[i - 1] + "." + extended.join(".");
9899
+ }
9900
+
9864
9901
  break;
9865
9902
  }
9866
9903
  }
@@ -9870,29 +9907,36 @@ function parse(url, options = {}) {
9870
9907
  query[key] = value;
9871
9908
  }
9872
9909
 
9910
+ let urlData = {
9911
+ domain: domain,
9912
+ origin: urlObject.origin,
9913
+ protocol: urlObject.protocol,
9914
+ host: urlObject.host,
9915
+ hostname: urlObject.hostname,
9916
+ port: urlObject.port,
9917
+ pathname: urlObject.pathname,
9918
+ search: urlObject.search,
9919
+ hash: urlObject.hash,
9920
+ query,
9921
+ };
9922
+
9923
+ // handle for protocols that aren't supported by URL constructor
9924
+ if (urlObject.origin === "null") {
9925
+ urlData.origin = urlObject.protocol + "//" + urlObject.hostname;
9926
+ }
9927
+
9873
9928
  return {
9874
9929
  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
- },
9930
+ url: urlData,
9887
9931
  };
9888
9932
  } catch (e) {
9889
9933
  throw new Error(`Invalid URL: ${e}`);
9890
9934
  }
9891
9935
  }
9892
9936
 
9893
- console.log(parse("google.co.in"));
9894
-
9895
- var jsdomainParser = { parse, parseTld: parseTld_1 };
9937
+ var parse_1 = parse;
9938
+
9939
+ var jsdomainParser = { parse: parse_1, parseTld: parseTld_1 };
9896
9940
  var jsdomainParser_1 = jsdomainParser.parse;
9897
9941
  var jsdomainParser_2 = jsdomainParser.parseTld;
9898
9942