@ztimson/utils 0.28.5 → 0.28.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/dist/index.mjs CHANGED
@@ -1503,6 +1503,15 @@ class NotAcceptableError extends CustomError {
1503
1503
  return err.constructor.code == this.code;
1504
1504
  }
1505
1505
  }
1506
+ class TooManyRequestsError extends CustomError {
1507
+ static code = 429;
1508
+ constructor(message = "Rate Limit Reached") {
1509
+ super(message);
1510
+ }
1511
+ static instanceof(err) {
1512
+ return err.constructor.code == this.code;
1513
+ }
1514
+ }
1506
1515
  class InternalServerError extends CustomError {
1507
1516
  static code = 500;
1508
1517
  constructor(message = "Internal Server Error") {
@@ -1850,6 +1859,15 @@ function gravatar(email, def = "mp") {
1850
1859
  if (!email) return "";
1851
1860
  return `https://www.gravatar.com/avatar/${md5(email)}?d=${def}`;
1852
1861
  }
1862
+ function matchesCidr(ip, cidr) {
1863
+ if (!cidr) return true;
1864
+ if (!ip) return false;
1865
+ if (!cidr?.includes("/")) return ip === cidr;
1866
+ const [range, bits] = cidr.split("/");
1867
+ const mask = ~(2 ** (32 - parseInt(bits)) - 1);
1868
+ const ipToInt = (str) => str.split(".").reduce((int, octet) => (int << 8) + parseInt(octet), 0) >>> 0;
1869
+ return (ipToInt(ip) & mask) === (ipToInt(range) & mask);
1870
+ }
1853
1871
  function ipV6ToV4(ip) {
1854
1872
  if (!ip) return null;
1855
1873
  const ipv4 = ip.split(":").splice(-1)[0];
@@ -2734,6 +2752,7 @@ export {
2734
2752
  ServiceUnavailableError,
2735
2753
  Table,
2736
2754
  TemplateError,
2755
+ TooManyRequestsError,
2737
2756
  TypedEmitter,
2738
2757
  UnauthorizedError,
2739
2758
  addUnique,
@@ -2785,6 +2804,7 @@ export {
2785
2804
  makeArray,
2786
2805
  makeUnique,
2787
2806
  matchAll,
2807
+ matchesCidr,
2788
2808
  md5,
2789
2809
  mixin,
2790
2810
  month,