From b8234eb6ab25b3862c83ae62297fe098720d0300 Mon Sep 17 00:00:00 2001 From: Zicklag Date: Tue, 3 Dec 2024 16:47:21 -0600 Subject: [PATCH] fix: remove keyv dependency and use redis instead. --- package.json | 2 - pnpm-lock.yaml | 88 ------------------------------------ src/lib/discord_bot/index.ts | 20 ++++---- 3 files changed, 10 insertions(+), 100 deletions(-) diff --git a/package.json b/package.json index c72949fc..7ab7ea35 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,6 @@ "@dicebear/core": "^9.2.2", "@floating-ui/dom": "^1.6.12", "@iconify/svelte": "^4.0.2", - "@keyv/redis": "^3.0.1", "@lezer/highlight": "^1.2.1", "@rodrigodagostino/svelte-sortable-list": "^0.10.8", "@skeletonlabs/skeleton": "^2.10.3", @@ -52,7 +51,6 @@ "eslint-config-prettier": "^9.1.0", "eslint-plugin-svelte": "^2.46.0", "fast-xml-parser": "^4.5.0", - "keyv": "^5.2.1", "leaf-proto": "workspace:*", "linkedom": "^0.18.5", "linktree-parser": "^1.5.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e4f4f908..77511c5a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -41,9 +41,6 @@ importers: '@iconify/svelte': specifier: ^4.0.2 version: 4.0.2(svelte@5.2.9) - '@keyv/redis': - specifier: ^3.0.1 - version: 3.0.1 '@lezer/highlight': specifier: ^1.2.1 version: 1.2.1 @@ -137,9 +134,6 @@ importers: fast-xml-parser: specifier: ^4.5.0 version: 4.5.0 - keyv: - specifier: ^5.2.1 - version: 5.2.1 leaf-proto: specifier: workspace:* version: link:leaf/ts @@ -846,9 +840,6 @@ packages: cpu: [x64] os: [win32] - '@ioredis/commands@1.2.0': - resolution: {integrity: sha512-Sx1pU8EM64o2BrqNpEO1CNLtKQwyhuXuqyfH7oGKCk+1a33d2r5saW8zNwm3j6BTExtjrv2BxTgzzkMwts6vGg==} - '@isaacs/cliui@8.0.2': resolution: {integrity: sha512-O8jcjabXaleOG9DQ0+ARXWZBTfnP4WNAqzuiJK7ll44AmxGKv/J2M4TPjxjY3znBCfvBXFzucm1twdyFybFqEA==} engines: {node: '>=12'} @@ -871,13 +862,6 @@ packages: '@jridgewell/trace-mapping@0.3.25': resolution: {integrity: sha512-vNk6aEwybGtawWmy/PzwnGDOjCkLWSD2wqvjGGAgOAwCGWySYXfYoxt00IJkTF+8Lb57DwOb3Aa0o9CApepiYQ==} - '@keyv/redis@3.0.1': - resolution: {integrity: sha512-eyqzomQC76LjUOEkPP8rdR2Fk4eZBSS0Ma47i7CNiQuv8NCw3trZvghx8L5Xruk7XPEj/eRAMrAxP//xQFOPdQ==} - engines: {node: '>= 18'} - - '@keyv/serialize@1.0.1': - resolution: {integrity: sha512-kKXeynfORDGPUEEl2PvTExM2zs+IldC6ZD8jPcfvI351MDNtfMlw9V9s4XZXuJNDK2qR5gbEKxRyoYx3quHUVQ==} - '@leichtgewicht/ip-codec@2.0.5': resolution: {integrity: sha512-Vo+PSpZG2/fmgmiNzYK9qWRh8h/CHrwD0mo1h1DzL4yzHNSfWYujGTYsWGreD000gcgmZ7K4Ys6Tx9TxtsKdDw==} @@ -1694,10 +1678,6 @@ packages: resolution: {integrity: sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==} engines: {node: '>=0.4.0'} - denque@2.1.0: - resolution: {integrity: sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==} - engines: {node: '>=0.10'} - dequal@2.0.3: resolution: {integrity: sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA==} engines: {node: '>=6'} @@ -2070,10 +2050,6 @@ packages: inherits@2.0.4: resolution: {integrity: sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==} - ioredis@5.4.1: - resolution: {integrity: sha512-2YZsvl7jopIa1gaePkeMtd9rAcSjOOjPtpcLlOeusyO+XH2SK5ZcT+UCrElPP+WVIInh2TzeI4XW9ENaSLVVHA==} - engines: {node: '>=12.22.0'} - ip-address@9.0.5: resolution: {integrity: sha512-zHtQzGojZXTwZTHQqra+ETKd4Sn3vgi7uBmlPoXVWZqYvuKmtI0l/VZTjqGmJY9x88GGOaZ9+G9ES8hC4T4X8g==} engines: {node: '>= 12'} @@ -2169,9 +2145,6 @@ packages: keyv@4.5.4: resolution: {integrity: sha512-oxVHkHR/EJf2CNXnWxRLW6mg7JyCCUcG0DtEGmL2ctUo1PNTin1PUil+r/+4r5MpVgC/fn1kjsx7mjSujKqIpw==} - keyv@5.2.1: - resolution: {integrity: sha512-tpIgCaY02VCW2Pz0zAn4guyct+IeH6Mb5wZdOvpe4oqXeQOJO0C3Wo8fTnf7P3ZD83Vr9kghbkNmzG3lTOhy/A==} - kleur@4.1.5: resolution: {integrity: sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ==} engines: {node: '>=6'} @@ -2215,12 +2188,6 @@ packages: lodash.castarray@4.4.0: resolution: {integrity: sha512-aVx8ztPv7/2ULbArGJ2Y42bG1mEQ5mGjpdvrbJcJFU3TbYybe+QlLS4pst9zV52ymy2in1KpFPiZnAOATxD4+Q==} - lodash.defaults@4.2.0: - resolution: {integrity: sha512-qjxPLHd3r5DnsdGacqOMU6pb/avJzdh9tFX2ymgoZE27BmjXrNy/y4LoaiTeAb+O3gL8AfpJGtqfX/ae2leYYQ==} - - lodash.isarguments@3.1.0: - resolution: {integrity: sha512-chi4NHZlZqZD18a0imDHnZPrDeBbTtVN7GXMwuGdRH9qotxAjYs3aVLKc7zNOG9eddR5Ksd8rvFEBc9SsggPpg==} - lodash.isplainobject@4.0.6: resolution: {integrity: sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==} @@ -2693,14 +2660,6 @@ packages: reconnecting-websocket@4.4.0: resolution: {integrity: sha512-D2E33ceRPga0NvTDhJmphEgJ7FUYF0v4lr1ki0csq06OdlxKfugGzN0dSkxM/NfqCxYELK4KcaTOUOjTV6Dcng==} - redis-errors@1.2.0: - resolution: {integrity: sha512-1qny3OExCf0UvUV/5wpYKf2YwPcOqXzkwKKSmKHiE6ZMQs5heeE/c8eXK+PNllPvmjgAbfnsbpkGZWy8cBpn9w==} - engines: {node: '>=4'} - - redis-parser@3.0.0: - resolution: {integrity: sha512-DJnGAeenTdpMEH6uAJRK/uiyEIH9WVsUmoLwzudwGJUwZPp80PDBWPHXSAGNPwNvIXAbe7MSUB1zQFugFml66A==} - engines: {node: '>=4'} - redis@4.7.0: resolution: {integrity: sha512-zvmkHEAdGMn+hMRXuMBtu4Vo5P6rHQjLoHftu+lBqq8ZTA3RCVC/WzD790bkKKiNFp7d5/9PcSD19fJyyRvOdQ==} @@ -2838,9 +2797,6 @@ packages: stackback@0.0.2: resolution: {integrity: sha512-1XMJE5fQo1jGH6Y/7ebnwPOBEkIEnT4QF32d5R1+VXdXveM0IBMJt8zfaxX1P3QhVwrYe+576+jkANtSS2mBbw==} - standard-as-callback@2.1.0: - resolution: {integrity: sha512-qoRRSyROncaz1z0mvYqIE4lCd9p2R90i6GxW3uZv5ucSu8tU7B5HXUP1gG8pVZsYNVaXjk8ClXHPttLyxAL48A==} - std-env@3.8.0: resolution: {integrity: sha512-Bc3YwwCB+OzldMxOXJIIvC6cPRWr/LxOp48CdQTOkPyk/t4JWWJbrilwBd7RJzKV8QW7tJkcgAmeuLLJugl5/w==} @@ -3830,8 +3786,6 @@ snapshots: '@img/sharp-win32-x64@0.33.5': optional: true - '@ioredis/commands@1.2.0': {} - '@isaacs/cliui@8.0.2': dependencies: string-width: 5.1.2 @@ -3858,16 +3812,6 @@ snapshots: '@jridgewell/resolve-uri': 3.1.2 '@jridgewell/sourcemap-codec': 1.5.0 - '@keyv/redis@3.0.1': - dependencies: - ioredis: 5.4.1 - transitivePeerDependencies: - - supports-color - - '@keyv/serialize@1.0.1': - dependencies: - buffer: 6.0.3 - '@leichtgewicht/ip-codec@2.0.5': {} '@lezer/common@1.2.3': {} @@ -4684,8 +4628,6 @@ snapshots: delayed-stream@1.0.0: optional: true - denque@2.1.0: {} - dequal@2.0.3: {} detect-indent@6.1.0: {} @@ -5150,20 +5092,6 @@ snapshots: inherits@2.0.4: {} - ioredis@5.4.1: - dependencies: - '@ioredis/commands': 1.2.0 - cluster-key-slot: 1.1.2 - debug: 4.3.7 - denque: 2.1.0 - lodash.defaults: 4.2.0 - lodash.isarguments: 3.1.0 - redis-errors: 1.2.0 - redis-parser: 3.0.0 - standard-as-callback: 2.1.0 - transitivePeerDependencies: - - supports-color - ip-address@9.0.5: dependencies: jsbn: 1.1.0 @@ -5269,10 +5197,6 @@ snapshots: dependencies: json-buffer: 3.0.1 - keyv@5.2.1: - dependencies: - '@keyv/serialize': 1.0.1 - kleur@4.1.5: {} known-css-properties@0.35.0: {} @@ -5313,10 +5237,6 @@ snapshots: lodash.castarray@4.4.0: {} - lodash.defaults@4.2.0: {} - - lodash.isarguments@3.1.0: {} - lodash.isplainobject@4.0.6: {} lodash.merge@4.6.2: {} @@ -5746,12 +5666,6 @@ snapshots: reconnecting-websocket@4.4.0: {} - redis-errors@1.2.0: {} - - redis-parser@3.0.0: - dependencies: - redis-errors: 1.2.0 - redis@4.7.0: dependencies: '@redis/bloom': 1.2.0(@redis/client@1.6.0) @@ -5937,8 +5851,6 @@ snapshots: stackback@0.0.2: {} - standard-as-callback@2.1.0: {} - std-env@3.8.0: {} string-width@4.2.3: diff --git a/src/lib/discord_bot/index.ts b/src/lib/discord_bot/index.ts index b7df82ed..07351e7b 100644 --- a/src/lib/discord_bot/index.ts +++ b/src/lib/discord_bot/index.ts @@ -12,24 +12,24 @@ import { WebLinks } from '$lib/leaf/profile'; import { getDiscordUserRauthyId } from '$lib/leaf/discord'; -import Keyv from 'keyv'; import { leafClient } from '$lib/leaf'; import { Name } from 'leaf-proto/components'; +import { redis } from '$lib/redis'; +import { usernames } from '$lib/usernames'; -// TODO: allow using Redis for key-value storage so that it can be clustered properly. -const discordLoginLinkIds = new Keyv({ namespace: 'discord-login-links' }); +const REDIS_PREFIX = 'weird:discord-login-links:'; export const createDiscordLoginLinkId = async (discordId: string): Promise => { const linkid = crypto.randomUUID(); // Create a login link that is valid for 10 minutes - await discordLoginLinkIds.set(linkid, discordId, 10 * 60 * 1000); + await redis.set(REDIS_PREFIX + linkid, discordId, { EX: 10 * 60 * 1000 }); return linkid; }; -export const getDiscordIdForLoginLink = async (loginLink: string): Promise => { - const discordId = await discordLoginLinkIds.get(loginLink); - if (discordId) await discordLoginLinkIds.delete(loginLink); - return discordId; +export const getDiscordIdForLoginLink = async (linkId: string): Promise => { + const discordId = await redis.get(REDIS_PREFIX + linkId); + if (discordId) await redis.del(linkId); + return discordId || undefined; }; const LOGIN_CMD = 'weird-login'; @@ -104,7 +104,7 @@ client.on('interactionCreate', async function (interaction) { }); return; } - const profileLink = getProfileLinkById(userId); + const profileLink = await getProfileLinkById(userId); const profile = await getProfile(profileLink); if (!profile) { interaction.reply({ @@ -127,7 +127,7 @@ client.on('interactionCreate', async function (interaction) { ]); interaction.reply({ - content: `Links imported successfully (http://${PublicEnv.PUBLIC_DOMAIN}/${profile.username}/discord-links):\n${links.join('\n')}`, + content: `Links imported successfully (http://${PublicEnv.PUBLIC_DOMAIN}/${await usernames.getByRauthyId(userId)}/discord-links):\n${links.join('\n')}`, ephemeral: true }); }