fix: harden server per final review
- load static files at startup so a missing file fails fast instead of crashing on request - WAL + busy_timeout so a manual export cannot fail live submissions - exit cleanly on SIGTERM (node is PID 1 in the container) - honour PORT=0 - document a required ingress rate limit - square card corners and larger card titles Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfmHGGiYUjNcvJoyzdf
This commit is contained in:
1 parent
c98c6da2ab
commit
f75da6d928
4 files changed
+57
-15
No files matched your search
@@ -93,3 +93,40 @@ test('honeypot submission is accepted but not stored', async () => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
assert.strictEqual(rows().length, before);
|
||||
});
|
||||
|
||||
const { spawn } = require('node:child_process');
|
||||
|
||||
test('missing page file fails at startup, not on a request', () => {
|
||||
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'ins-nopage-'));
|
||||
fs.copyFileSync(path.join(__dirname, 'server.js'), path.join(tmp, 'server.js'));
|
||||
const { createServer: create } = require(path.join(tmp, 'server.js'));
|
||||
assert.throws(() => create(path.join(tmp, 'leads.db')), /ENOENT/);
|
||||
fs.rmSync(tmp, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
test('insert waits for a lock held by another process (e.g. an export)', async () => {
|
||||
const lock = spawn(process.execPath, ['--disable-warning=ExperimentalWarning', '-e', `
|
||||
const { DatabaseSync } = require('node:sqlite');
|
||||
const db = new DatabaseSync(${JSON.stringify(dbPath)});
|
||||
db.exec('BEGIN EXCLUSIVE');
|
||||
console.log('locked');
|
||||
setTimeout(() => { db.exec('COMMIT'); db.close(); }, 500);
|
||||
`]);
|
||||
await new Promise((r) => lock.stdout.once('data', r));
|
||||
const res = await post(valid);
|
||||
assert.strictEqual(res.status, 201);
|
||||
});
|
||||
|
||||
test('exits promptly on SIGTERM', async () => {
|
||||
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'ins-term-'));
|
||||
const child = spawn(process.execPath, ['--disable-warning=ExperimentalWarning', path.join(__dirname, 'server.js')], {
|
||||
env: { ...process.env, PORT: '0', DB_PATH: path.join(tmp, 'leads.db') },
|
||||
});
|
||||
await new Promise((r) => child.stdout.once('data', r));
|
||||
const start = Date.now();
|
||||
child.kill('SIGTERM');
|
||||
const code = await new Promise((r) => child.on('exit', (c) => r(c)));
|
||||
assert.strictEqual(code, 0);
|
||||
assert.ok(Date.now() - start < 2000);
|
||||
fs.rmSync(tmp, { recursive: true, force: true });
|
||||
});
|
||||
Reference in new issue
Block a user