@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/errors.d.ts CHANGED
@@ -43,6 +43,11 @@ export declare class NotAcceptableError extends CustomError {
43
43
  constructor(message?: string);
44
44
  static instanceof(err: Error): boolean;
45
45
  }
46
+ export declare class TooManyRequestsError extends CustomError {
47
+ static code: number;
48
+ constructor(message?: string);
49
+ static instanceof(err: Error): boolean;
50
+ }
46
51
  export declare class InternalServerError extends CustomError {
47
52
  static code: number;
48
53
  constructor(message?: string);
package/dist/index.cjs CHANGED
@@ -1507,6 +1507,15 @@ ${opts.message || this.desc}`;
1507
1507
  return err.constructor.code == this.code;
1508
1508
  }
1509
1509
  }
1510
+ class TooManyRequestsError extends CustomError {
1511
+ static code = 429;
1512
+ constructor(message = "Rate Limit Reached") {
1513
+ super(message);
1514
+ }
1515
+ static instanceof(err) {
1516
+ return err.constructor.code == this.code;
1517
+ }
1518
+ }
1510
1519
  class InternalServerError extends CustomError {
1511
1520
  static code = 500;
1512
1521
  constructor(message = "Internal Server Error") {
@@ -1854,6 +1863,15 @@ ${opts.message || this.desc}`;
1854
1863
  if (!email) return "";
1855
1864
  return `https://www.gravatar.com/avatar/${md5(email)}?d=${def}`;
1856
1865
  }
1866
+ function matchesCidr(ip, cidr) {
1867
+ if (!cidr) return true;
1868
+ if (!ip) return false;
1869
+ if (!cidr?.includes("/")) return ip === cidr;
1870
+ const [range, bits] = cidr.split("/");
1871
+ const mask = ~(2 ** (32 - parseInt(bits)) - 1);
1872
+ const ipToInt = (str) => str.split(".").reduce((int, octet) => (int << 8) + parseInt(octet), 0) >>> 0;
1873
+ return (ipToInt(ip) & mask) === (ipToInt(range) & mask);
1874
+ }
1857
1875
  function ipV6ToV4(ip) {
1858
1876
  if (!ip) return null;
1859
1877
  const ipv4 = ip.split(":").splice(-1)[0];
@@ -2737,6 +2755,7 @@ ${err.message || err.toString()}`);
2737
2755
  exports2.ServiceUnavailableError = ServiceUnavailableError;
2738
2756
  exports2.Table = Table;
2739
2757
  exports2.TemplateError = TemplateError;
2758
+ exports2.TooManyRequestsError = TooManyRequestsError;
2740
2759
  exports2.TypedEmitter = TypedEmitter;
2741
2760
  exports2.UnauthorizedError = UnauthorizedError;
2742
2761
  exports2.addUnique = addUnique;
@@ -2788,6 +2807,7 @@ ${err.message || err.toString()}`);
2788
2807
  exports2.makeArray = makeArray;
2789
2808
  exports2.makeUnique = makeUnique;
2790
2809
  exports2.matchAll = matchAll;
2810
+ exports2.matchesCidr = matchesCidr;
2791
2811
  exports2.md5 = md5;
2792
2812
  exports2.mixin = mixin;
2793
2813
  exports2.month = month;