@upstash/ratelimit 2.0.3 → 2.0.4

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
@@ -26,10 +26,10 @@ var Analytics = class {
26
26
  * @returns
27
27
  */
28
28
  extractGeo(req) {
29
- if (typeof req.geo !== "undefined") {
29
+ if (req.geo !== void 0) {
30
30
  return req.geo;
31
31
  }
32
- if (typeof req.cf !== "undefined") {
32
+ if (req.cf !== void 0) {
33
33
  return req.cf;
34
34
  }
35
35
  return {};
@@ -118,18 +118,24 @@ function ms(d) {
118
118
  const time = Number.parseInt(match[1]);
119
119
  const unit = match[2];
120
120
  switch (unit) {
121
- case "ms":
121
+ case "ms": {
122
122
  return time;
123
- case "s":
123
+ }
124
+ case "s": {
124
125
  return time * 1e3;
125
- case "m":
126
+ }
127
+ case "m": {
126
128
  return time * 1e3 * 60;
127
- case "h":
129
+ }
130
+ case "h": {
128
131
  return time * 1e3 * 60 * 60;
129
- case "d":
132
+ }
133
+ case "d": {
130
134
  return time * 1e3 * 60 * 60 * 24;
131
- default:
135
+ }
136
+ default: {
132
137
  throw new Error(`Unable to parse window size: ${d}`);
138
+ }
133
139
  }
134
140
  }
135
141
 
@@ -571,7 +577,7 @@ var updateIpDenyList = async (redis, prefix, threshold, ttl) => {
571
577
  const transaction = redis.multi();
572
578
  transaction.sdiffstore(allDenyLists, allDenyLists, ipDenyList);
573
579
  transaction.del(ipDenyList);
574
- transaction.sadd(ipDenyList, ...allIps);
580
+ transaction.sadd(ipDenyList, allIps.at(0), ...allIps.slice(1));
575
581
  transaction.sdiffstore(ipDenyList, ipDenyList, allDenyLists);
576
582
  transaction.sunionstore(allDenyLists, allDenyLists, ipDenyList);
577
583
  transaction.set(statusKey, "valid", { px: ttl ?? getIpListTTL() });
@@ -673,7 +679,7 @@ var Ratelimit = class {
673
679
  }) : void 0;
674
680
  if (config.ephemeralCache instanceof Map) {
675
681
  this.ctx.cache = new Cache(config.ephemeralCache);
676
- } else if (typeof config.ephemeralCache === "undefined") {
682
+ } else if (config.ephemeralCache === void 0) {
677
683
  this.ctx.cache = new Cache(/* @__PURE__ */ new Map());
678
684
  }
679
685
  }
@@ -804,15 +810,10 @@ var Ratelimit = class {
804
810
  const key = this.getKey(identifier);
805
811
  const definedMembers = this.getDefinedMembers(identifier, req);
806
812
  const deniedValue = checkDenyListCache(definedMembers);
807
- let result;
808
- if (deniedValue) {
809
- result = [defaultDeniedResponse(deniedValue), { deniedValue, invalidIpDenyList: false }];
810
- } else {
811
- result = await Promise.all([
812
- this.limiter().limit(this.ctx, key, req?.rate),
813
- this.enableProtection ? checkDenyList(this.primaryRedis, this.prefix, definedMembers) : { deniedValue: void 0, invalidIpDenyList: false }
814
- ]);
815
- }
813
+ const result = deniedValue ? [defaultDeniedResponse(deniedValue), { deniedValue, invalidIpDenyList: false }] : await Promise.all([
814
+ this.limiter().limit(this.ctx, key, req?.rate),
815
+ this.enableProtection ? checkDenyList(this.primaryRedis, this.prefix, definedMembers) : { deniedValue: void 0, invalidIpDenyList: false }
816
+ ]);
816
817
  return resolveLimitPayload(this.primaryRedis, this.prefix, result, this.denyListThreshold);
817
818
  };
818
819
  /**
@@ -862,9 +863,9 @@ var Ratelimit = class {
862
863
  time: Date.now(),
863
864
  success: ratelimitResponse.reason === "denyList" ? "denied" : ratelimitResponse.success,
864
865
  ...geo
865
- }).catch((err) => {
866
+ }).catch((error) => {
866
867
  let errorMessage = "Failed to record analytics";
867
- if (`${err}`.includes("WRONGTYPE")) {
868
+ if (`${error}`.includes("WRONGTYPE")) {
868
869
  errorMessage = `
869
870
  Failed to record analytics. See the information below:
870
871
 
@@ -877,11 +878,11 @@ var Ratelimit = class {
877
878
 
878
879
  `;
879
880
  }
880
- console.warn(errorMessage, err);
881
+ console.warn(errorMessage, error);
881
882
  });
882
883
  ratelimitResponse.pending = Promise.all([ratelimitResponse.pending, analyticsP]);
883
- } catch (err) {
884
- console.warn("Failed to record analytics", err);
884
+ } catch (error) {
885
+ console.warn("Failed to record analytics", error);
885
886
  }
886
887
  ;
887
888
  }
@@ -992,18 +993,17 @@ var MultiRegionRatelimit = class extends Ratelimit {
992
993
  const remaining = tokens - usedTokens;
993
994
  async function sync() {
994
995
  const individualIDs = await Promise.all(dbs.map((s) => s.request));
995
- const allIDs = Array.from(
996
- new Set(
997
- individualIDs.flatMap((_) => _).reduce((acc, curr, index) => {
998
- if (index % 2 === 0) {
999
- acc.push(curr);
1000
- }
1001
- return acc;
1002
- }, [])
1003
- ).values()
1004
- );
996
+ const allIDs = [...new Set(
997
+ individualIDs.flat().reduce((acc, curr, index) => {
998
+ if (index % 2 === 0) {
999
+ acc.push(curr);
1000
+ }
1001
+ return acc;
1002
+ }, [])
1003
+ ).values()];
1005
1004
  for (const db of dbs) {
1006
- const usedDbTokens = (await db.request).reduce(
1005
+ const usedDbTokensRequest = await db.request;
1006
+ const usedDbTokens = usedDbTokensRequest.reduce(
1007
1007
  (accTokens, usedToken, index) => {
1008
1008
  let parsedToken = 0;
1009
1009
  if (index % 2) {
@@ -1013,7 +1013,8 @@ var MultiRegionRatelimit = class extends Ratelimit {
1013
1013
  },
1014
1014
  0
1015
1015
  );
1016
- const dbIds = (await db.request).reduce((ids, currentId, index) => {
1016
+ const dbIdsRequest = await db.request;
1017
+ const dbIds = dbIdsRequest.reduce((ids, currentId, index) => {
1017
1018
  if (index % 2 === 0) {
1018
1019
  ids.push(currentId);
1019
1020
  }
@@ -1160,16 +1161,14 @@ var MultiRegionRatelimit = class extends Ratelimit {
1160
1161
  const remaining = tokens - usedTokens;
1161
1162
  async function sync() {
1162
1163
  const res = await Promise.all(dbs.map((s) => s.request));
1163
- const allCurrentIds = Array.from(
1164
- new Set(
1165
- res.flatMap(([current2]) => current2).reduce((acc, curr, index) => {
1166
- if (index % 2 === 0) {
1167
- acc.push(curr);
1168
- }
1169
- return acc;
1170
- }, [])
1171
- ).values()
1172
- );
1164
+ const allCurrentIds = [...new Set(
1165
+ res.flatMap(([current2]) => current2).reduce((acc, curr, index) => {
1166
+ if (index % 2 === 0) {
1167
+ acc.push(curr);
1168
+ }
1169
+ return acc;
1170
+ }, [])
1171
+ ).values()];
1173
1172
  for (const db of dbs) {
1174
1173
  const [current2, _previous, _success] = await db.request;
1175
1174
  const dbIds = current2.reduce((ids, currentId, index) => {