From 3c0d1c8411a8c458f66e66e11e488436873bbe7c Mon Sep 17 00:00:00 2001 From: Bas van den Aakster Date: Fri, 12 Dec 2025 22:56:55 +0100 Subject: [PATCH] Replace magic numbers with named constants in server Refactoring: Extract magic numbers to named constants - MAX_BATCH_SIZE = 100 (batch request limit) - MAX_PAGE_SIZE = 100 (pagination limit) Replaced in: - app.ts: Batch size validation (line 68-69) - rpc.ts: Page size limit (line 184) Impact: Improves code clarity and makes limits configurable --- src/app.ts | 7 +++++-- src/rpc.ts | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/app.ts b/src/app.ts index e1f6ad9..70547be 100644 --- a/src/app.ts +++ b/src/app.ts @@ -4,6 +4,9 @@ import { Storage } from './storage/types.ts'; import { Config } from './config.ts'; import { handleRpc, RpcRequest } from './rpc.ts'; +// Constants +const MAX_BATCH_SIZE = 100; + /** * Creates the Hono application with RPC interface */ @@ -62,8 +65,8 @@ export function createApp(storage: Storage, config: Config) { return c.json({ error: 'Empty request array' }, 400); } - if (requests.length > 100) { - return c.json({ error: 'Too many requests in batch (max 100)' }, 400); + if (requests.length > MAX_BATCH_SIZE) { + return c.json({ error: `Too many requests in batch (max ${MAX_BATCH_SIZE})` }, 400); } // Handle RPC diff --git a/src/rpc.ts b/src/rpc.ts index 3b4b1e4..00714c8 100644 --- a/src/rpc.ts +++ b/src/rpc.ts @@ -12,6 +12,9 @@ import { validateUsername, } from './crypto.ts'; +// Constants +const MAX_PAGE_SIZE = 100; + /** * RPC request format */ @@ -178,7 +181,7 @@ const handlers: Record = { // Paginated discovery mode if (limit !== undefined) { - const pageLimit = Math.min(Math.max(1, limit), 100); + const pageLimit = Math.min(Math.max(1, limit), MAX_PAGE_SIZE); const pageOffset = Math.max(0, offset || 0); const allServices = await storage.getServicesByName(