From 91559d20ca24add3bee0d1fd51b1676d656595a4 Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Wed, 25 Oct 2023 18:22:55 +0700 Subject: [PATCH 1/3] fix: check for value before manipulation --- src/helpers/metrics.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/helpers/metrics.ts b/src/helpers/metrics.ts index 500a62d9..b3bbe00f 100644 --- a/src/helpers/metrics.ts +++ b/src/helpers/metrics.ts @@ -58,9 +58,13 @@ export default function initMetrics(app: Express) { if (query && operationName) { const definition = parse(query).definitions.find( // @ts-ignore - def => def.name.value === operationName + def => def.name?.value === operationName ); + if (!definition) { + return; + } + // @ts-ignore const types = definition.selectionSet.selections.map(sel => sel.name.value); From 1b053c25a60fdeb47c3edc60f33ff70f45db6e2f Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Fri, 27 Oct 2023 13:15:57 +0700 Subject: [PATCH 2/3] fix: run background refresher on demand --- src/api.ts | 4 +++- src/helpers/moderation.ts | 4 +--- src/helpers/spaces.ts | 4 +--- src/index.ts | 3 ++- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/api.ts b/src/api.ts index aeb30df6..44707627 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,6 +1,6 @@ import express from 'express'; import { capture } from '@snapshot-labs/snapshot-sentry'; -import { getSpace } from './helpers/spaces'; +import refreshSpacesCache, { getSpace } from './helpers/spaces'; import { name, version } from '../package.json'; import { sendError } from './helpers/utils'; @@ -8,6 +8,8 @@ const router = express.Router(); const network = process.env.NETWORK || 'testnet'; const SEQUENCER_URL = process.env.SEQUENCER_URL || ''; +refreshSpacesCache(); + router.post('/message', async (req, res) => { return sendError(res, 'personal sign is not supported anymore'); }); diff --git a/src/helpers/moderation.ts b/src/helpers/moderation.ts index cd7e9141..315d8ec6 100644 --- a/src/helpers/moderation.ts +++ b/src/helpers/moderation.ts @@ -13,7 +13,7 @@ async function loadModerationData() { flaggedLinks = res?.flaggedLinks; } -async function run() { +export default async function run() { try { await loadModerationData(); consecutiveFailsCount = 0; @@ -28,5 +28,3 @@ async function run() { await snapshot.utils.sleep(5e3); run(); } - -run(); diff --git a/src/helpers/spaces.ts b/src/helpers/spaces.ts index 40fd3c2e..5e9beb0d 100644 --- a/src/helpers/spaces.ts +++ b/src/helpers/spaces.ts @@ -176,7 +176,7 @@ export async function getSpace(id: string) { }; } -async function run() { +export default async function run() { try { await loadSpaces(); await loadSpacesMetrics(); @@ -187,5 +187,3 @@ async function run() { await snapshot.utils.sleep(360e3); run(); } - -run(); diff --git a/src/index.ts b/src/index.ts index 6e5d5cda..7ca0571e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,7 @@ import rateLimit from './helpers/rateLimit'; import log from './helpers/log'; import initMetrics from './helpers/metrics'; import { checkKeycard } from './helpers/keycard'; -import './helpers/moderation'; +import refreshModerationData from './helpers/moderation'; import './helpers/strategies'; const app = express(); @@ -16,6 +16,7 @@ const PORT = process.env.PORT || 3000; initLogger(app); initMetrics(app); +refreshModerationData(); app.disable('x-powered-by'); app.use(express.json({ limit: '20mb' })); From 47e4ac0ad7ff1c05f6b0042b15d276a993657f67 Mon Sep 17 00:00:00 2001 From: Wan Qi Chen <495709+wa0x6e@users.noreply.github.com> Date: Sat, 28 Oct 2023 12:20:48 +0700 Subject: [PATCH 3/3] refactor: move all refresher init in same place --- src/api.ts | 4 +--- src/index.ts | 2 ++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/api.ts b/src/api.ts index 44707627..aeb30df6 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,6 +1,6 @@ import express from 'express'; import { capture } from '@snapshot-labs/snapshot-sentry'; -import refreshSpacesCache, { getSpace } from './helpers/spaces'; +import { getSpace } from './helpers/spaces'; import { name, version } from '../package.json'; import { sendError } from './helpers/utils'; @@ -8,8 +8,6 @@ const router = express.Router(); const network = process.env.NETWORK || 'testnet'; const SEQUENCER_URL = process.env.SEQUENCER_URL || ''; -refreshSpacesCache(); - router.post('/message', async (req, res) => { return sendError(res, 'personal sign is not supported anymore'); }); diff --git a/src/index.ts b/src/index.ts index 7ca0571e..a8aff851 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ import log from './helpers/log'; import initMetrics from './helpers/metrics'; import { checkKeycard } from './helpers/keycard'; import refreshModerationData from './helpers/moderation'; +import refreshSpacesCache from './helpers/spaces'; import './helpers/strategies'; const app = express(); @@ -17,6 +18,7 @@ const PORT = process.env.PORT || 3000; initLogger(app); initMetrics(app); refreshModerationData(); +refreshSpacesCache(); app.disable('x-powered-by'); app.use(express.json({ limit: '20mb' }));