Spaces:
Sleeping
Sleeping
Commit ·
9e8da7a
1
Parent(s): bb750d6
refactor: prepare server module for testing
Browse files- Guard server startup behind the START_SERVER environment flag
- Export the request handler and server instance for test injection
- .env.example +3 -0
- .env.prod.example +3 -0
- .env.test +3 -0
- README.md +1 -0
- src/config.js +5 -0
- src/server.js +9 -6
.env.example
CHANGED
|
@@ -33,6 +33,9 @@ REDIS_DB=0
|
|
| 33 |
# Override to use a different data source for seeding
|
| 34 |
# SEED_API_BASE_URL=https://jsonplaceholder.typicode.com
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
# Debug: log all SQL queries to stderr
|
| 37 |
# DEBUG_SQL=true
|
| 38 |
|
|
|
|
| 33 |
# Override to use a different data source for seeding
|
| 34 |
# SEED_API_BASE_URL=https://jsonplaceholder.typicode.com
|
| 35 |
|
| 36 |
+
# Request body size limit (bytes, default 1 MB)
|
| 37 |
+
# MAX_BODY_SIZE=1048576
|
| 38 |
+
|
| 39 |
# Debug: log all SQL queries to stderr
|
| 40 |
# DEBUG_SQL=true
|
| 41 |
|
.env.prod.example
CHANGED
|
@@ -23,6 +23,9 @@
|
|
| 23 |
# Override to use a different data source for seeding
|
| 24 |
# SEED_API_BASE_URL=https://jsonplaceholder.typicode.com
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
# Debug: log all SQL queries to stderr
|
| 27 |
# DEBUG_SQL=false
|
| 28 |
|
|
|
|
| 23 |
# Override to use a different data source for seeding
|
| 24 |
# SEED_API_BASE_URL=https://jsonplaceholder.typicode.com
|
| 25 |
|
| 26 |
+
# Request body size limit (bytes, default 1 MB)
|
| 27 |
+
# MAX_BODY_SIZE=1048576
|
| 28 |
+
|
| 29 |
# Debug: log all SQL queries to stderr
|
| 30 |
# DEBUG_SQL=false
|
| 31 |
|
.env.test
CHANGED
|
@@ -22,6 +22,9 @@ DB_PATH=./storage/test.db
|
|
| 22 |
# Override to use a different data source for seeding
|
| 23 |
# SEED_API_BASE_URL=https://jsonplaceholder.typicode.com
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
# Debug: log all SQL queries to stderr
|
| 26 |
DEBUG_SQL=false
|
| 27 |
|
|
|
|
| 22 |
# Override to use a different data source for seeding
|
| 23 |
# SEED_API_BASE_URL=https://jsonplaceholder.typicode.com
|
| 24 |
|
| 25 |
+
# Request body size limit (bytes, default 1 MB)
|
| 26 |
+
# MAX_BODY_SIZE=1048576
|
| 27 |
+
|
| 28 |
# Debug: log all SQL queries to stderr
|
| 29 |
DEBUG_SQL=false
|
| 30 |
|
README.md
CHANGED
|
@@ -52,6 +52,7 @@ Environment files are loaded by `src/load-env.js` (auto-run via `src/config.js`)
|
|
| 52 |
| `DEBUG_SQL` | `false` | Log all SQL queries to stderr (`true`/`false`) |
|
| 53 |
| `RATE_LIMIT_WINDOW_MS` | `60000` | Time window in milliseconds (default 1 min) |
|
| 54 |
| `SEED_API_BASE_URL` | `https://jsonplaceholder.typicode.com` | Base URL for seed data API |
|
|
|
|
| 55 |
|
| 56 |
---
|
| 57 |
|
|
|
|
| 52 |
| `DEBUG_SQL` | `false` | Log all SQL queries to stderr (`true`/`false`) |
|
| 53 |
| `RATE_LIMIT_WINDOW_MS` | `60000` | Time window in milliseconds (default 1 min) |
|
| 54 |
| `SEED_API_BASE_URL` | `https://jsonplaceholder.typicode.com` | Base URL for seed data API |
|
| 55 |
+
| `MAX_BODY_SIZE` | `1048576` | Max request body size in bytes (minimum 1) |
|
| 56 |
|
| 57 |
---
|
| 58 |
|
src/config.js
CHANGED
|
@@ -27,6 +27,10 @@ const RATE_LIMIT_MAX = parseInt(process.env.RATE_LIMIT_MAX || '100', 10);
|
|
| 27 |
const RATE_LIMIT_WINDOW_MS = parseInt(process.env.RATE_LIMIT_WINDOW_MS || '60000', 10);
|
| 28 |
const RATE_LIMIT_WINDOW_SEC = Math.ceil(RATE_LIMIT_WINDOW_MS / 1000);
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
module.exports = {
|
| 31 |
port: PORT,
|
| 32 |
dbPath: DB_PATH,
|
|
@@ -36,4 +40,5 @@ module.exports = {
|
|
| 36 |
rateLimitMax: RATE_LIMIT_MAX,
|
| 37 |
rateLimitWindowMs: RATE_LIMIT_WINDOW_MS,
|
| 38 |
rateLimitWindowSec: RATE_LIMIT_WINDOW_SEC,
|
|
|
|
| 39 |
};
|
|
|
|
| 27 |
const RATE_LIMIT_WINDOW_MS = parseInt(process.env.RATE_LIMIT_WINDOW_MS || '60000', 10);
|
| 28 |
const RATE_LIMIT_WINDOW_SEC = Math.ceil(RATE_LIMIT_WINDOW_MS / 1000);
|
| 29 |
|
| 30 |
+
const DEFAULT_MAX_BODY_SIZE = 1048576;
|
| 31 |
+
const parsedMaxBodySize = parseInt(process.env.MAX_BODY_SIZE || String(DEFAULT_MAX_BODY_SIZE), 10);
|
| 32 |
+
const MAX_BODY_SIZE = isNaN(parsedMaxBodySize) || parsedMaxBodySize < 1 ? DEFAULT_MAX_BODY_SIZE : parsedMaxBodySize;
|
| 33 |
+
|
| 34 |
module.exports = {
|
| 35 |
port: PORT,
|
| 36 |
dbPath: DB_PATH,
|
|
|
|
| 40 |
rateLimitMax: RATE_LIMIT_MAX,
|
| 41 |
rateLimitWindowMs: RATE_LIMIT_WINDOW_MS,
|
| 42 |
rateLimitWindowSec: RATE_LIMIT_WINDOW_SEC,
|
| 43 |
+
maxBodySize: MAX_BODY_SIZE,
|
| 44 |
};
|
src/server.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
| 3 |
const http = require('http');
|
| 4 |
|
| 5 |
const { port, redisOpts, rateLimitMax, rateLimitEnabled,
|
| 6 |
-
rateLimitWindowMs, rateLimitWindowSec } = require('./config');
|
| 7 |
const RedisClient = require('./redis');
|
| 8 |
const { createRateLimiter } = require('./rate-limiter');
|
| 9 |
const db = require('./database');
|
|
@@ -43,7 +43,6 @@ function badRequest(res, msg = 'Bad Request') {
|
|
| 43 |
json(res, 400, { error: msg });
|
| 44 |
}
|
| 45 |
|
| 46 |
-
const MAX_BODY_SIZE = 1_048_576;
|
| 47 |
const BODY_TOO_LARGE = Symbol('BODY_TOO_LARGE');
|
| 48 |
|
| 49 |
function readBody(req, res) {
|
|
@@ -51,8 +50,8 @@ function readBody(req, res) {
|
|
| 51 |
let body = '';
|
| 52 |
req.on('data', (chunk) => {
|
| 53 |
body += chunk;
|
| 54 |
-
if (body.length >
|
| 55 |
-
json(res, 413, { error: 'Request body too large', message: `Body exceeds ${
|
| 56 |
req.destroy();
|
| 57 |
reject(BODY_TOO_LARGE);
|
| 58 |
}
|
|
@@ -174,7 +173,7 @@ function handleHealth(res) {
|
|
| 174 |
async function requestHandler(req, res) {
|
| 175 |
const parsed = new URL(req.url, 'http://localhost');
|
| 176 |
const pathname = parsed.pathname;
|
| 177 |
-
const query = parsed.searchParams;
|
| 178 |
const method = req.method.toUpperCase();
|
| 179 |
|
| 180 |
if (method === 'OPTIONS') {
|
|
@@ -254,8 +253,12 @@ function printLog() {
|
|
| 254 |
}
|
| 255 |
|
| 256 |
function startServer() {
|
| 257 |
-
|
|
|
|
|
|
|
| 258 |
}
|
| 259 |
|
| 260 |
process.on('SIGINT', () => { server.close(); redis.quit().catch(() => { }); process.exit(0); });
|
| 261 |
process.on('SIGTERM', () => { server.close(); redis.quit().catch(() => { }); process.exit(0); });
|
|
|
|
|
|
|
|
|
| 3 |
const http = require('http');
|
| 4 |
|
| 5 |
const { port, redisOpts, rateLimitMax, rateLimitEnabled,
|
| 6 |
+
rateLimitWindowMs, rateLimitWindowSec, maxBodySize } = require('./config');
|
| 7 |
const RedisClient = require('./redis');
|
| 8 |
const { createRateLimiter } = require('./rate-limiter');
|
| 9 |
const db = require('./database');
|
|
|
|
| 43 |
json(res, 400, { error: msg });
|
| 44 |
}
|
| 45 |
|
|
|
|
| 46 |
const BODY_TOO_LARGE = Symbol('BODY_TOO_LARGE');
|
| 47 |
|
| 48 |
function readBody(req, res) {
|
|
|
|
| 50 |
let body = '';
|
| 51 |
req.on('data', (chunk) => {
|
| 52 |
body += chunk;
|
| 53 |
+
if (body.length > maxBodySize) {
|
| 54 |
+
json(res, 413, { error: 'Request body too large', message: `Body exceeds ${maxBodySize} bytes limit` });
|
| 55 |
req.destroy();
|
| 56 |
reject(BODY_TOO_LARGE);
|
| 57 |
}
|
|
|
|
| 173 |
async function requestHandler(req, res) {
|
| 174 |
const parsed = new URL(req.url, 'http://localhost');
|
| 175 |
const pathname = parsed.pathname;
|
| 176 |
+
const query = Object.fromEntries(parsed.searchParams);
|
| 177 |
const method = req.method.toUpperCase();
|
| 178 |
|
| 179 |
if (method === 'OPTIONS') {
|
|
|
|
| 253 |
}
|
| 254 |
|
| 255 |
function startServer() {
|
| 256 |
+
if (process.env.START_SERVER !== 'false') { // for testing
|
| 257 |
+
server.listen(port, printLog);
|
| 258 |
+
}
|
| 259 |
}
|
| 260 |
|
| 261 |
process.on('SIGINT', () => { server.close(); redis.quit().catch(() => { }); process.exit(0); });
|
| 262 |
process.on('SIGTERM', () => { server.close(); redis.quit().catch(() => { }); process.exit(0); });
|
| 263 |
+
|
| 264 |
+
module.exports = { server, requestHandler };
|