Skip to content

Commit

Permalink
chore: update deps, refactor to use @upstash/redis
Browse files Browse the repository at this point in the history
  • Loading branch information
dallen4 committed Jan 5, 2025
1 parent b988342 commit 2d08801
Show file tree
Hide file tree
Showing 11 changed files with 2,089 additions and 2,589 deletions.
1 change: 0 additions & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"open": "^10.1.0",
"ora": "^6.1.2",
"semver": "^7.5.4",
"ws": "^8.11.0",
"yaml": "^2.5.0"
},
"devDependencies": {
Expand Down
4 changes: 0 additions & 4 deletions cli/pkg.json

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@clerk/clerk-js": "^5.22.3",
"@clerk/clerk-sdk-node": "^5.0.42",
"@libsql/client": "^0.14.0",
"@upstash/redis": "^1.34.3",
"@xstate/react": "^3.0.1",
"drizzle-kit": "^0.24.0",
"drizzle-orm": "^0.33.0",
Expand All @@ -51,15 +52,15 @@
"devDependencies": {
"@types/nanoid-dictionary": "^4.2.3",
"@types/node": "^20",
"@types/qrcode": "^1.5.5",
"@vitest/coverage-istanbul": "^2.1.8",
"eslint": "^8.31.0",
"jscpd": "^4.0.5",
"postcss": "^8.4.49",
"prettier": "^2.8.2",
"ts-node": "^10.7.0",
"ts-prune": "^0.10.3",
"tsx": "^4.19.2",
"typescript": "^4.0.3",
"typescript": "^5.7.2",
"vitest": "^2.1.8"
},
"browser": {
Expand Down
2 changes: 1 addition & 1 deletion shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"private": true,
"peerDependencies": {
"peerjs": "^1",
"typescript": "^4",
"typescript": "^5",
"ws": "*"
}
}
52 changes: 26 additions & 26 deletions web/api/drops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,57 @@ import { generateDateTotalId } from './util';
import { DropDetails } from '@shared/types/common';

export const getDailyDropCount = async (target: Date) => {
const client = getRedis();
const client = getRedis();

const dateId = generateDateTotalId(target);
const userDropCount = await client.get(dateId);
const dateId = generateDateTotalId(target);
const userDropCount = await client.get<number>(dateId);

return userDropCount ? parseInt(userDropCount) : 0;
return userDropCount ? userDropCount : 0;
};

export const incrementDailyDropCount = async () => {
const client = getRedis();
const client = getRedis();

const dateId = generateDateTotalId();
const dateId = generateDateTotalId();

const dailyDropCount = await client.get(dateId);
const dailyDropCount = await client.get(dateId);

let dailyCount = 1;
let dailyCount = 1;

if (!dailyDropCount) await client.set(dateId, dailyCount);
else dailyCount = await client.incr(dateId);
if (!dailyDropCount) await client.set(dateId, dailyCount);
else dailyCount = await client.incr(dateId);

return dailyCount;
return dailyCount;
};

const FIVE_MINS_IN_SEC = 10 * 60;

export const createDrop = async (peerId: string) => {
const client = getRedis();
const client = getRedis();

const dropId = nanoid();
const nonce = generateIV();
const dropId = nanoid();
const nonce = generateIV();

const key = formatDropKey(dropId);
await client.hset(key, { peerId, nonce });
await client.expire(key, FIVE_MINS_IN_SEC);
const key = formatDropKey(dropId);
await client.hset(key, { peerId, nonce });
await client.expire(key, FIVE_MINS_IN_SEC);

await incrementDailyDropCount();
await incrementDailyDropCount();

return { dropId, nonce };
return { dropId, nonce };
};

export const getDrop = async (id: string): Promise<DropDetails> => {
const client = getRedis();
const dropItem = await client.hgetall(formatDropKey(id));
const client = getRedis();
const dropItem = await client.hgetall(formatDropKey(id));

return dropItem as DropDetails;
return dropItem as DropDetails;
};

export const deleteDrop = async (id: string): Promise<boolean> => {
const client = getRedis();
const key = formatDropKey(id);
await client.del(key);
const client = getRedis();
const key = formatDropKey(id);
await client.del(key);

return true;
return true;
};
27 changes: 14 additions & 13 deletions web/api/limiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ import { DAILY_DROP_LIMIT_COOKIE } from '@config/cookies';

const DAY_IN_SEC = 60 * 60 * 24;

export const checkAndIncrementDropCount = async (ipAddress: string) => {
const userIpHash = await hashRaw(ipAddress);
export const checkAndIncrementDropCount = async (
ipAddress: string,
) => {
const userIpHash = await hashRaw(ipAddress);

const client = getRedis();
const client = getRedis();

const userDropCount = await client.get(userIpHash);
const userDropCount = await client.get<number>(userIpHash);

if (!userDropCount) {
await client.setex(userIpHash, DAY_IN_SEC, 1);
} else {
const dailyDropLimit = await get<number>(DAILY_DROP_LIMIT_COOKIE);
if (!userDropCount) {
await client.setex(userIpHash, DAY_IN_SEC, 1);
} else {
const dailyDropLimit = await get<number>(DAILY_DROP_LIMIT_COOKIE);

if (parseInt(userDropCount) >= dailyDropLimit!)
return false;
else await client.incr(userIpHash);
}
if (userDropCount >= dailyDropLimit!) return false;
else await client.incr(userIpHash);
}

return true;
return true;
};
15 changes: 6 additions & 9 deletions web/api/redis.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import Redis from 'ioredis';
import { Redis } from '@upstash/redis';

let client: Redis;

export const getRedis = () =>
client || (client = new Redis(process.env.REDIS_URL!));

export const cleanupRedis = async () => {
if (client) {
await client.quit();
console.log('Redis connection closed...');
}
};
client ||
(client = new Redis({
url: process.env.REDIS_REST_URL!,
token: process.env.REDIS_REST_TOKEN!,
}));
3 changes: 0 additions & 3 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"cookies-next": "^2.0.4",
"cors": "^2.8.5",
"eslint-config-next": "^14.2.13",
"ioredis": "^5.0.4",
"js-cookie": "^3.0.1",
"next": "^14.2.13",
"next-pwa": "^5.6.0",
Expand All @@ -60,15 +59,13 @@
"@types/cors": "^2.8.13",
"@types/js-cookie": "^3.0.2",
"@types/mdx": "^2.0.13",
"@types/qrcode": "^1.5.0",
"@types/randomcolor": "^0.5.6",
"@types/react": "^18",
"@types/react-dom": "^18",
"@types/request-ip": "^0.0.37",
"@types/ua-parser-js": "^0.7.36",
"@typescript-eslint/eslint-plugin": "^5.48.0",
"@typescript-eslint/parser": "^5.48.0",
"eslint": "^8.31.0",
"eslint-plugin-react": "^7.31.11",
"next-transpile-modules": "^10.0.1"
},
Expand Down
2 changes: 1 addition & 1 deletion web/tests/e2e/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const test = base.extend<TestOptions>({

let testToken: string | null = null;

const getTestToken = async () => getRedis().get(testTokenKey);
const getTestToken = async () => getRedis().get<string>(testTokenKey);

export const getOrCreateTestToken = async () => {
const client = getRedis();
Expand Down
4 changes: 1 addition & 3 deletions worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20231121.0",
"typescript": "^5.0.4",
"wrangler": "^3.99.0"
},
"dependencies": {
"@hono/clerk-auth": "^2.0.0",
"@hono/zod-validator": "^0.4.1",
"@upstash/redis": "^1.34.3"
"@hono/zod-validator": "^0.4.1"
}
}
Loading

0 comments on commit 2d08801

Please sign in to comment.