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
This commit is contained in:
2025-12-12 22:56:55 +01:00
parent 53a576670e
commit 3c0d1c8411
2 changed files with 9 additions and 3 deletions

View File

@@ -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

View File

@@ -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<string, RpcHandler> = {
// 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(