Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit 5f5c2d1

Browse files
committed
feat: impl rate limit
1 parent f7560ea commit 5f5c2d1

32 files changed

+823
-75
lines changed

.github/workflows/test-all.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ jobs:
4040

4141
# Run tests on all modules in the registry
4242
- name: Run Tests for all modules
43-
run: cd ./opengb-registry/tests/basic && opengb test
43+
run: cd ./opengb-registry/tests/basic && opengb test --strict-schemas --force-deploy-migrations

modules/auth/scripts/auth_email_passwordless.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export async function run(
1515
ctx: ScriptContext,
1616
req: Request,
1717
): Promise<Response> {
18-
if (!ctx.userConfig.email) throw new RuntimeError("PROVIDER_DISABLED");
18+
await ctx.modules.rateLimit.throttlePublic({});
1919

20-
await ctx.modules.rateLimit.throttle({});
20+
if (!ctx.userConfig.email) throw new RuntimeError("PROVIDER_DISABLED");
2121

2222
// Fetch existing user if session token is provided
2323
let userId: string | undefined;

modules/auth/scripts/verify_email_passwordless.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export async function run(
1818
ctx: ScriptContext,
1919
req: Request,
2020
): Promise<Response> {
21-
await ctx.modules.rateLimit.throttle({});
21+
await ctx.modules.rateLimit.throttlePublic({});
2222

2323
const code = req.code.toUpperCase();
2424

modules/currency/scripts/deposit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export async function run(
1616
ctx: ScriptContext,
1717
req: Request,
1818
): Promise<Response> {
19-
await ctx.modules.rateLimit.throttle({ requests: 25 });
19+
await ctx.modules.rateLimit.throttlePublic({ requests: 25 });
2020

2121
if (req.amount < 0 || !Number.isFinite(req.amount)) {
2222
throw new RuntimeError("INVALID_AMOUNT");

modules/currency/scripts/get_balance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export async function run(
1414
ctx: ScriptContext,
1515
req: Request,
1616
): Promise<Response> {
17-
await ctx.modules.rateLimit.throttle({ requests: 25 });
17+
await ctx.modules.rateLimit.throttlePublic({ requests: 25 });
1818

1919
return {
2020
balance: await getBalance(ctx.db, req.userId),

modules/currency/scripts/get_balance_by_token.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export async function run(
1313
ctx: ScriptContext,
1414
req: Request,
1515
): Promise<Response> {
16-
await ctx.modules.rateLimit.throttle({ requests: 25 });
16+
await ctx.modules.rateLimit.throttlePublic({ requests: 25 });
1717

1818
const { userId } = await ctx.modules.users.validateUserToken({
1919
userToken: req.userToken,

modules/currency/scripts/set_balance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export async function run(
1414
ctx: ScriptContext,
1515
req: Request,
1616
): Promise<Response> {
17-
await ctx.modules.rateLimit.throttle({ requests: 25 });
17+
await ctx.modules.rateLimit.throttlePublic({ requests: 25 });
1818

1919
if (req.balance < 0 || !Number.isFinite(req.balance)) {
2020
throw new RuntimeError("INVALID_AMOUNT");

modules/currency/scripts/withdraw.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export async function run(
1616
ctx: ScriptContext,
1717
req: Request,
1818
): Promise<Response> {
19-
await ctx.modules.rateLimit.throttle({ requests: 25 });
19+
await ctx.modules.rateLimit.throttlePublic({ requests: 25 });
2020

2121
if (req.amount < 0 || !Number.isFinite(req.amount)) {
2222
throw new RuntimeError("INVALID_AMOUNT");

modules/currency/tests/e2e.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import { faker } from "https://deno.land/x/[email protected]/mod.ts";
55
test(
66
"e2e transaction",
77
async (ctx: TestContext) => {
8-
const { user: user, token: token } = await ctx.modules.users.register({
8+
const { user: user } = await ctx.modules.users.createUser({
99
username: faker.internet.userName(),
10-
identity: { guest: {} },
10+
});
11+
const { token } = await ctx.modules.users.createUserToken({
12+
userId: user.id,
1113
});
1214

1315
const { updatedBalance } = await ctx.modules.currency.deposit({

modules/currency/tests/errors.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ test(
1919
test(
2020
"withdraw more than balance",
2121
async (ctx: TestContext) => {
22-
const { user: user, token: _token } = await ctx.modules.users.register({
22+
const { user: user } = await ctx.modules.users.createUser({
2323
username: faker.internet.userName(),
24-
identity: { guest: {} },
2524
});
2625

2726
const { updatedBalance: _ } = await ctx.modules.currency.deposit({
@@ -39,9 +38,8 @@ test(
3938
test(
4039
"withdraw negative amount",
4140
async (ctx: TestContext) => {
42-
const { user: user, token: _token } = await ctx.modules.users.register({
41+
const { user: user } = await ctx.modules.users.createUser({
4342
username: faker.internet.userName(),
44-
identity: { guest: {} },
4543
});
4644

4745
const error = await assertRejects(async () => {
@@ -54,9 +52,8 @@ test(
5452
test(
5553
"withdraw Infinity",
5654
async (ctx: TestContext) => {
57-
const { user: user, token: _token } = await ctx.modules.users.register({
55+
const { user: user } = await ctx.modules.users.createUser({
5856
username: faker.internet.userName(),
59-
identity: { guest: {} },
6057
});
6158

6259
const error = await assertRejects(async () => {
@@ -72,9 +69,8 @@ test(
7269
test(
7370
"withdraw NaN",
7471
async (ctx: TestContext) => {
75-
const { user: user, token: _token } = await ctx.modules.users.register({
72+
const { user: user } = await ctx.modules.users.createUser({
7673
username: faker.internet.userName(),
77-
identity: { guest: {} },
7874
});
7975

8076
const error = await assertRejects(async () => {
@@ -87,9 +83,8 @@ test(
8783
test(
8884
"deposit Infinity",
8985
async (ctx: TestContext) => {
90-
const { user: user, token: _token } = await ctx.modules.users.register({
86+
const { user: user } = await ctx.modules.users.createUser({
9187
username: faker.internet.userName(),
92-
identity: { guest: {} },
9388
});
9489

9590
const error = await assertRejects(async () => {
@@ -102,9 +97,8 @@ test(
10297
test(
10398
"deposit NaN",
10499
async (ctx: TestContext) => {
105-
const { user: user, token: _token } = await ctx.modules.users.register({
100+
const { user: user } = await ctx.modules.users.createUser({
106101
username: faker.internet.userName(),
107-
identity: { guest: {} },
108102
});
109103

110104
const error = await assertRejects(async () => {
@@ -117,9 +111,8 @@ test(
117111
test(
118112
"deposit negative amount",
119113
async (ctx: TestContext) => {
120-
const { user: user, token: _token } = await ctx.modules.users.register({
114+
const { user: user } = await ctx.modules.users.createUser({
121115
username: faker.internet.userName(),
122-
identity: { guest: {} },
123116
});
124117

125118
const error = await assertRejects(async () => {
@@ -132,9 +125,8 @@ test(
132125
test(
133126
"set balance to negative",
134127
async (ctx: TestContext) => {
135-
const { user: user, token: _token } = await ctx.modules.users.register({
128+
const { user: user } = await ctx.modules.users.createUser({
136129
username: faker.internet.userName(),
137-
identity: { guest: {} },
138130
});
139131

140132
const error = await assertRejects(async () => {
@@ -147,9 +139,8 @@ test(
147139
test(
148140
"set balance to NaN",
149141
async (ctx: TestContext) => {
150-
const { user: user, token: _token } = await ctx.modules.users.register({
142+
const { user: user } = await ctx.modules.users.createUser({
151143
username: faker.internet.userName(),
152-
identity: { guest: {} },
153144
});
154145

155146
const error = await assertRejects(async () => {
@@ -163,9 +154,8 @@ test(
163154
test(
164155
"set balance to infinity",
165156
async (ctx: TestContext) => {
166-
const { user: user, token: _token } = await ctx.modules.users.register({
157+
const { user: user } = await ctx.modules.users.createUser({
167158
username: faker.internet.userName(),
168-
identity: { guest: {} },
169159
});
170160

171161
const error = await assertRejects(async () => {

0 commit comments

Comments
 (0)