Files
webspeak3/gateway/src/activity-log.ts
T
MoepchiandClaude Sonnet 5 da4f416e32 Extract feedback/connection logging into a swappable module
Same pattern as the recent store.ts extraction: feedback-log writes and
connection-event logging move from inline file/console I/O in index.ts
into activity-log.ts, called via logFeedback()/logConnectionEvent(). The
public default behavior is unchanged (JSON-lines file with rotation,
console logging gated by LOG_CONNECTIONS).

This is the piece of the private SQLite overlay's persistence swap that's
a clean drop-in replacement - the private VPS gateway now imports the same
two function names from its own activity-log.ts (SQLite-backed) instead of
patching these call sites into a forked index.ts.

Note: the private overlay's live host-ban enforcement (blocked_hosts
table, heartbeat-driven disconnect) is NOT extracted here - it has no
public equivalent and touches the WS connection/heartbeat control flow
directly, not just a persistence call. Left as-is for now.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
2026-09-18 11:09:37 +00:00

70 lines
2.8 KiB
TypeScript

// --- Activity logging (feedback + connection events) -----------------------
//
// Narrow interface so the private VPS overlay can swap in SQLite persistence
// (see mem:reference_webspeak3_private_sqlite_persistence) without forking
// index.ts - only this module differs between public and private; index.ts's
// two call sites just call logFeedback/logConnectionEvent unconditionally.
// This file itself has zero private modifications, same as store.ts.
import { appendFile, rename, stat } from "node:fs/promises";
import path from "node:path";
// Plain JSON-lines file. Defaults under the container's WORKDIR (/app in
// the Docker image); mount a volume over it if you want submissions to
// survive a container recreate.
const FEEDBACK_LOG_FILE = process.env.FEEDBACK_LOG_FILE ?? path.resolve(process.cwd(), "feedback.log");
// Simple size-based rotation: once the log crosses this size, the current
// file is moved to feedback.log.1 (overwriting any previous one) and a
// fresh file is started. Keeps disk usage bounded on a plain JSON-lines
// file with no other retention policy.
const FEEDBACK_LOG_MAX_BYTES = 5 * 1024 * 1024;
async function rotateFeedbackLogIfNeeded(): Promise<void> {
try {
const { size } = await stat(FEEDBACK_LOG_FILE);
if (size < FEEDBACK_LOG_MAX_BYTES) return;
await rename(FEEDBACK_LOG_FILE, `${FEEDBACK_LOG_FILE}.1`);
} catch (err) {
// ENOENT just means there's no log yet - nothing to rotate.
if ((err as NodeJS.ErrnoException).code !== "ENOENT") {
console.error("[feedback] Failed to rotate feedback log:", err);
}
}
}
export interface FeedbackEntry {
at: string;
category: string;
message: string;
email?: string;
}
export async function logFeedback(entry: FeedbackEntry): Promise<void> {
try {
await rotateFeedbackLogIfNeeded();
await appendFile(FEEDBACK_LOG_FILE, JSON.stringify(entry) + "\n", "utf-8");
} catch (err) {
console.error("[feedback] Failed to write feedback log:", err);
}
}
// Opt-in only, off by default: this is an open-source, self-hostable image,
// and other instances shouldn't get connection logging just because it's in
// the codebase. Set LOG_CONNECTIONS=1 in the .env of the instance you want it
// on. Logs only host/server-name/timestamp — no nickname, no IP.
export const LOG_CONNECTIONS = process.env.LOG_CONNECTIONS === "1";
export interface ConnectionEvent {
type: "connected" | "disconnected";
host: string;
server?: string;
}
export function logConnectionEvent(event: ConnectionEvent): void {
if (!LOG_CONNECTIONS) return;
if (event.type === "connected") {
console.log(`[connections] connected host=${event.host} server=${event.server} at=${new Date().toISOString()}`);
} else {
console.log(`[connections] disconnected host=${event.host} at=${new Date().toISOString()}`);
}
}