diff --git a/server/package.json b/server/package.json index 9f206bf..886bbb6 100644 --- a/server/package.json +++ b/server/package.json @@ -4,7 +4,7 @@ "private": true, "type": "module", "engines": { - "node": ">=22" + "node": ">=22.13" }, "scripts": { "dev": "node --watch src/index.js", diff --git a/server/src/admin-crud.js b/server/src/admin-crud.js index 6fa3164..a56f75d 100644 --- a/server/src/admin-crud.js +++ b/server/src/admin-crud.js @@ -304,7 +304,7 @@ export function createRow(db, entity, payload) { ) .run(...Object.values(values)) : db.prepare(`INSERT INTO ${entity.table} DEFAULT VALUES`).run(); - // better-sqlite3 and node:sqlite disagree about BigInt here. + // node:sqlite may hand back a BigInt here. newId = Number(result.lastInsertRowid); } else { const names = [entity.idColumn, ...Object.keys(values)]; diff --git a/server/src/db.js b/server/src/db.js index d5c926c..e7c5f3d 100644 --- a/server/src/db.js +++ b/server/src/db.js @@ -2,15 +2,12 @@ DATABASE One SQLite file, opened once at boot and held for the life of - the process. Node's own sqlite module is used when it's there - (Node 24+), better-sqlite3 otherwise. Their APIs overlap enough - that everything below works against either, as long as you: + the process, through Node's own node:sqlite (Node 22.13+). + House rules: • use positional ? parameters, never named ones • pass 0/1 for booleans, never true/false - • use tx() rather than db.transaction() - - Those three rules are the whole compatibility story. + • use tx(): node:sqlite has no db.transaction() ═══════════════════════════════════════════════════════════════ */ import { readdirSync, readFileSync } from "node:fs"; @@ -20,18 +17,6 @@ import { fileURLToPath } from "node:url"; const HERE = dirname(fileURLToPath(import.meta.url)); const MIGRATIONS_DIR = join(HERE, "migrations"); -/* ── Driver selection ──────────────────────────────────────── */ - -async function loadDriver() { - try { - const { DatabaseSync } = await import("node:sqlite"); - return { Driver: DatabaseSync, name: "node:sqlite" }; - } catch { - const { default: BetterSqlite3 } = await import("better-sqlite3"); - return { Driver: BetterSqlite3, name: "better-sqlite3" }; - } -} - /* ── Open ────────────────────────────────────────────────────── WAL readers never block the writer, which matters the moment a feedback POST lands mid page-load @@ -42,15 +27,15 @@ async function loadDriver() { ───────────────────────────────────────────────────────────── */ export async function openDatabase(path) { - const { Driver, name } = await loadDriver(); - const db = new Driver(path); + const { DatabaseSync } = await import("node:sqlite"); + const db = new DatabaseSync(path); db.exec("PRAGMA journal_mode = WAL"); db.exec("PRAGMA foreign_keys = ON"); db.exec("PRAGMA synchronous = NORMAL"); db.exec("PRAGMA busy_timeout = 5000"); - db.driverName = name; + db.driverName = "node:sqlite"; return db; } diff --git a/server/src/routes/feedback.js b/server/src/routes/feedback.js index 6a09822..beb5335 100644 --- a/server/src/routes/feedback.js +++ b/server/src/routes/feedback.js @@ -10,13 +10,12 @@ length caps rejected before anything touches the database no HTML stored verbatim, escaped at render time - The write is a single INSERT with positional parameters, so it - works on node:sqlite and better-sqlite3 alike. No transaction: - one statement is already atomic. + The write is a single INSERT. No transaction: one statement is + already atomic. ═══════════════════════════════════════════════════════════════ */ import { Hono } from "hono"; -import { createHash, randomBytes } from "node:crypto"; +import { clientIp, hashIp } from "../auth.js"; const feedback = new Hono(); @@ -35,25 +34,6 @@ const MIN_MESSAGE = 10; // a renamed option shouldn't lose someone's submission. const TYPES = ["broken", "confusing", "outdated", "request", "praise", "other"]; -/* ── IP hashing ──────────────────────────────────────────────── - Stored so repeat abuse from one source is visible during - triage, hashed so the table never holds an address. Without - IP_SALT the salt is regenerated each boot, which makes hashes - incomparable across restarts — fine for dev, set it in - /etc/ngu/api.env for production. - ───────────────────────────────────────────────────────────── */ - -const IP_SALT = process.env.IP_SALT ?? randomBytes(16).toString("hex"); - -if (!process.env.IP_SALT) { - console.warn("IP_SALT unset — feedback ip_hash values reset on restart"); -} - -function hashIp(ip) { - if (!ip) return null; - return createHash("sha256").update(`${IP_SALT}:${ip}`).digest("hex").slice(0, 32); -} - /* ── Input cleaning ──────────────────────────────────────────── */ function clean(value, max) { @@ -118,7 +98,7 @@ feedback.post("/", async (c) => { pagePath || null, sectionId || null, clean(c.req.header("user-agent"), 500) || null, - hashIp(c.req.header("x-forwarded-for")?.split(",")[0].trim()), + hashIp(clientIp(c)), ); // created_at and status come from column defaults. diff --git a/src/components/PeopleTiles.tsx b/src/components/PeopleTiles.tsx index ab1b12d..5a89801 100644 --- a/src/components/PeopleTiles.tsx +++ b/src/components/PeopleTiles.tsx @@ -12,6 +12,7 @@ import { Link } from "react-router-dom"; import { get } from "../lib/api.ts"; import { isBadId, personHref } from "../lib/hrefs.ts"; +import { personPhoto } from "../lib/media.ts"; import "./PeopleTiles.css"; /** @@ -547,7 +548,7 @@ function Tile({ const content = ( - + {person.name} @@ -760,13 +761,6 @@ function hasDetails(person: Person): boolean { ); } -/* Database rows carry a bare filename; a hand-written entry may - give a path or a full URL. Both should work. */ -function photoSrc(photo?: string | null): string | null { - if (!photo) return null; - if (/^(https?:|\/|data:)/.test(photo)) return photo; - return `/people/${photo}`; -} function initials(name = ""): string { return name diff --git a/src/lib/api.ts b/src/lib/api.ts index 5eeca0a..9d3c693 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -38,8 +38,8 @@ export class ApiError extends Error { async function request(path, options: RequestInit = {}) { const response = await fetch(`${BASE}${path}`, { - headers: { Accept: "application/json", ...options.headers }, ...options, + headers: { Accept: "application/json", ...options.headers }, }); if (response.status === 204) return null;