diff --git a/src/index.ts b/src/index.ts index b50fe17..a8320d9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,7 +10,9 @@ import { const PORT = Number(process.env.PORT) || 4040; // Initialize database -initDb("collabd.db"); +const DB_PATH = process.env.COLLABD_DB || "collabd.db"; +initDb(DB_PATH); +console.debug(`database initialized: ${DB_PATH}`); function isValidRoomName(name: unknown): name is string { if (typeof name !== "string") return false; diff --git a/src/session.ts b/src/session.ts index 5e710f9..6f60d5a 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1,7 +1,7 @@ import type { ServerWebSocket } from "bun"; import * as Y from "yjs"; +import { getUpdates, saveUpdate } from "./db"; import { type AwarenessState, encode } from "./protocol"; -import { saveUpdate, getUpdates } from "./db"; export interface WsData { room: string | null; @@ -16,7 +16,7 @@ export class Session { doc: Y.Doc; clients: Set = new Set(); - constructor(public name: string) { + constructor(public readonly name: string) { this.doc = new Y.Doc(); } @@ -50,8 +50,13 @@ export class Session { applyUpdate(update: Uint8Array, from: Client) { try { Y.applyUpdate(this.doc, update); - // Persist the update - saveUpdate(this.name, update); + // Persist the update (log but don't fail on db errors) + try { + saveUpdate(this.name, update); + } catch (err) { + console.error(`failed to persist update in room ${this.name}:`, err); + // Continue - memory state is still updated + } // broadcast to others for (const client of this.clients) { if (client !== from) { @@ -114,6 +119,13 @@ export function getOrCreateSession(name: string): Session { for (const update of updates) { Y.applyUpdate(session.doc, update); } + + // Double-check pattern - another request may have created session while we loaded + const existing = sessions.get(name); + if (existing) { + return existing; + } + sessions.set(name, session); console.debug(`session created: ${name}`); }