Skip to content

Commit c667b63

Browse files
authored
Merge pull request #127 from upstash/hotfix-bump-redis
Bump Upstash Redis
2 parents 235762b + 0dfb86b commit c667b63

File tree

11 files changed

+77
-32
lines changed

11 files changed

+77
-32
lines changed

.github/workflows/tests.yaml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ jobs:
3939
run: bun run test
4040

4141
cloudflare-workers-local:
42-
needs:
43-
- test
4442
runs-on: ubuntu-latest
4543
steps:
4644
- name: Setup repo
@@ -127,8 +125,6 @@ jobs:
127125
DEPLOYMENT_URL: https://upstash-ratelimit.upsdev.workers.dev
128126

129127
nextjs-local:
130-
needs:
131-
- test
132128
runs-on: ubuntu-latest
133129
steps:
134130
- name: Setup repo
@@ -195,8 +191,8 @@ jobs:
195191
VERCEL_PROJECT_ID: "prj_NSOSq2ZawugKhtZGb3ViHX4b56hx"
196192
working-directory: examples/nextjs
197193

198-
- name: Install dependencies
199-
run: bun install
194+
- name: Install @upstash/ratelimit canary version
195+
run: npm install @upstash/ratelimit@${{needs.release.outputs.version}}
200196
working-directory: examples/nextjs
201197

202198
- name: Build Project

bun.lockb

324 Bytes
Binary file not shown.

examples/cloudflare-workers/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"deploy": "wrangler publish"
1515
},
1616
"dependencies": {
17-
"@upstash/ratelimit": "^1.0.3",
18-
"@upstash/redis": "^1.20.6"
17+
"@upstash/ratelimit": "latest",
18+
"@upstash/redis": "latest"
1919
}
2020
}

examples/nextjs/app/api/route.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const redis = new Redis({
1313

1414
// Create a new ratelimiter
1515
const ratelimit = new Ratelimit({
16-
// @ts-ignore
1716
redis,
1817
limiter: Ratelimit.slidingWindow(10, "10 s"),
1918
prefix: "@upstash/ratelimit",

examples/nextjs/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"lint": "next lint"
1010
},
1111
"dependencies": {
12-
"@upstash/ratelimit": "^1.1.3",
13-
"@upstash/redis": "^1.34.2",
12+
"@upstash/ratelimit": "latest",
13+
"@upstash/redis": "latest",
1414
"@vercel/functions": "^1.0.2",
1515
"next": "14.2.3",
1616
"react": "^18",
@@ -30,4 +30,4 @@
3030
},
3131
"module": "index.ts",
3232
"type": "module"
33-
}
33+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"devDependencies": {
1616
"@typescript-eslint/eslint-plugin": "^8.4.0",
17-
"@upstash/redis": "^1.31.5",
17+
"@upstash/redis": "^1.34.3",
1818
"bun-types": "latest",
1919
"eslint": "^9.10.0",
2020
"eslint-plugin-unicorn": "^55.0.0",

src/cache.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Ratelimit } from "./index";
55
test("ephemeral cache", async () => {
66
const maxTokens = 10;
77
const redis = Redis.fromEnv();
8+
await redis.scriptFlush()
89

910
const metrics: Record<string | symbol, number> = {};
1011

@@ -37,7 +38,7 @@ test("ephemeral cache", async () => {
3738
}
3839

3940
expect(passes).toBeLessThanOrEqual(10);
40-
expect(metrics.evalsha).toBe(11);
41+
expect(metrics.evalsha).toBe(12);
4142
expect(reasons).toContain("cacheBlock")
4243

4344
await new Promise((r) => setTimeout(r, 5000));

src/deny-list/ip-deny-list.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { beforeEach, describe, expect, test } from "bun:test";
33
import { checkDenyList } from "./deny-list";
44
import { disableIpDenyList, updateIpDenyList } from "./ip-deny-list";
55
import { DenyListExtension, IpDenyListKey, IpDenyListStatusKey } from "../types";
6+
import { Ratelimit } from "..";
67

78
describe("should update ip deny list status", async () => {
89
const redis = Redis.fromEnv();
@@ -139,6 +140,37 @@ describe("should update ip deny list status", async () => {
139140
expect(newStatus).toBe("valid")
140141
expect(newStatusTTL).toBeGreaterThan(1000)
141142
})
143+
144+
test("should be able to use ratelimit with deny list", async () => {
145+
146+
/**
147+
* When enableProtection is set to true, there was an error in the
148+
* @upstash/ratelimit 2.0.3 release. Because the @upstash/redis
149+
* uses auto-pipelining by default, the client would send the
150+
* EVALSHA and the protection EVAL at the same time.
151+
*
152+
* When the db loses its scripts after a SCRIPT FLUSH or when
153+
* ratelimit is used in a new DB, the EVALSHA will fail. But
154+
* because since the EVAL and EVALSHA are pipelined, they will
155+
* both fail and EVAL will get the EVALSHA's error.
156+
*/
157+
const redis = Redis.fromEnv({ enableAutoPipelining: true });
158+
159+
// flush the db to make sure
160+
await redis.scriptFlush()
161+
// sleep for two secs
162+
await new Promise(r => setTimeout(r, 2000));
163+
164+
const ratelimit = new Ratelimit({
165+
limiter: Ratelimit.fixedWindow(2, "1s"),
166+
redis,
167+
enableProtection: true
168+
})
169+
170+
const { success, remaining } = await ratelimit.limit("ip-deny-list")
171+
expect(success).toBeTrue()
172+
expect(remaining).toBe(1)
173+
})
142174
})
143175

144176
describe("should only allow threshold values from 1 to 8", async () => {
@@ -177,4 +209,4 @@ describe("should only allow threshold values from 1 to 8", async () => {
177209
expect(error.name).toEqual("ThresholdError")
178210
}
179211
})
180-
})
212+
})

src/deny-list/ip-deny-list.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ export const updateIpDenyList = async (
8383

8484
// delete the old ip deny list and create new one
8585
transaction.del(ipDenyList)
86-
transaction.sadd(ipDenyList, ...allIps)
86+
87+
transaction.sadd(ipDenyList, allIps.at(0), ...allIps.slice(1))
8788

8889
// make all deny list and ip deny list disjoint by removing duplicate
8990
// ones from ip deny list

src/hash.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Redis } from "@upstash/redis";
2+
import { describe, test } from "bun:test";
3+
import { safeEval } from "./hash";
4+
import { SCRIPTS } from "./lua-scripts/hash";
5+
6+
const redis = Redis.fromEnv();
7+
8+
describe("should set hash correctly", () => {
9+
test("should set hash in new db correctly", async () => {
10+
await redis.scriptFlush()
11+
12+
// sleep for two secs
13+
await new Promise(r => setTimeout(r, 2000));
14+
15+
await safeEval(
16+
{
17+
redis
18+
},
19+
SCRIPTS.singleRegion.fixedWindow.limit,
20+
["id"],
21+
[10, 1]
22+
)
23+
})
24+
})

0 commit comments

Comments
 (0)