refactor: centralize domain values and enforce numeric lint
This commit is contained in:
@@ -1,3 +1,8 @@
|
||||
const DEFAULT_PORT = 3000;
|
||||
const DEFAULT_TBANK_RATE_LIMIT_PER_SECOND = 5;
|
||||
const MAX_PORT = 65_535;
|
||||
const MIN_JWT_SECRET_LENGTH = 32;
|
||||
const TOKEN_KEY_BYTES = 32;
|
||||
export interface Environment {
|
||||
DATABASE_URL: string;
|
||||
FRONTEND_ORIGIN: string;
|
||||
@@ -13,12 +18,14 @@ export interface Environment {
|
||||
export function validateEnv(config: Record<string, unknown>): Environment {
|
||||
const databaseUrl = config['DATABASE_URL'];
|
||||
const frontendOrigin = config['FRONTEND_ORIGIN'];
|
||||
const port = config['PORT'] ?? 3000;
|
||||
const port = config['PORT'] ?? DEFAULT_PORT;
|
||||
const jwtSecret = config['JWT_SECRET'];
|
||||
const tokenEncryptionKey = config['TOKEN_ENCRYPTION_KEY'];
|
||||
const tbankEndpoint =
|
||||
config['TBANK_GRPC_ENDPOINT'] ?? 'invest-public-api.tbank.ru:443';
|
||||
const tbankRateLimit = config['TBANK_RATE_LIMIT_PER_SECOND'] ?? 5;
|
||||
const tbankRateLimit =
|
||||
config['TBANK_RATE_LIMIT_PER_SECOND'] ??
|
||||
DEFAULT_TBANK_RATE_LIMIT_PER_SECOND;
|
||||
const tbankCaCertPath = config['TBANK_CA_CERT_PATH'];
|
||||
const logLevel = config['LOG_LEVEL'] ?? 'info';
|
||||
|
||||
@@ -47,18 +54,25 @@ export function validateEnv(config: Record<string, unknown>): Environment {
|
||||
}
|
||||
|
||||
const parsedPort = Number(port);
|
||||
if (!Number.isInteger(parsedPort) || parsedPort < 1 || parsedPort > 65535) {
|
||||
if (
|
||||
!Number.isInteger(parsedPort) ||
|
||||
parsedPort < 1 ||
|
||||
parsedPort > MAX_PORT
|
||||
) {
|
||||
throw new Error('PORT must be a valid port number');
|
||||
}
|
||||
|
||||
if (typeof jwtSecret !== 'string' || jwtSecret.length < 32) {
|
||||
if (
|
||||
typeof jwtSecret !== 'string' ||
|
||||
jwtSecret.length < MIN_JWT_SECRET_LENGTH
|
||||
) {
|
||||
throw new Error('JWT_SECRET must be at least 32 characters long');
|
||||
}
|
||||
|
||||
if (
|
||||
typeof tokenEncryptionKey !== 'string' ||
|
||||
!/^[A-Za-z0-9+/]+={0,2}$/.test(tokenEncryptionKey) ||
|
||||
Buffer.from(tokenEncryptionKey, 'base64').length !== 32
|
||||
Buffer.from(tokenEncryptionKey, 'base64').length !== TOKEN_KEY_BYTES
|
||||
) {
|
||||
throw new Error(
|
||||
'TOKEN_ENCRYPTION_KEY must be a base64-encoded 32-byte key',
|
||||
|
||||
Reference in New Issue
Block a user