Full document, spreadsheet, slideshow, and diagram tooling
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request 'fix: make snapshot save handler robust against silent failures' (#149) from fix/save-handler-robustness into main

scott 1f5b3df7 41528b41

+16 -2
+16 -2
server/index.ts
··· 270 270 271 271 // Accept both PUT (normal save) and POST (sendBeacon — which can only POST) 272 272 const snapshotHandler = (req: Request<{ id: string }>, res: Response): void => { 273 - stmts.putSnapshot.run(req.body, req.params.id); 274 - res.json({ ok: true }); 273 + if (!req.body || !Buffer.isBuffer(req.body) || req.body.length === 0) { 274 + res.status(400).json({ error: 'Empty or missing snapshot body' }); 275 + return; 276 + } 277 + try { 278 + const result = stmts.putSnapshot.run(req.body, req.params.id); 279 + if (result.changes === 0) { 280 + // Document doesn't exist — auto-create it so saves never silently fail 281 + db.prepare("INSERT OR IGNORE INTO documents (id, type, name_encrypted) VALUES (?, 'doc', NULL)").run(req.params.id); 282 + stmts.putSnapshot.run(req.body, req.params.id); 283 + } 284 + res.json({ ok: true }); 285 + } catch (err: unknown) { 286 + console.error('Snapshot save failed:', err); 287 + res.status(500).json({ error: 'Failed to save snapshot' }); 288 + } 275 289 }; 276 290 const snapshotMiddleware = express.raw({ limit: '50mb', type: '*/*' }); 277 291 app.put('/api/documents/:id/snapshot', snapshotMiddleware, snapshotHandler);