Add nonblocking/asyn I/O operations
This commit is contained in:
@@ -1,25 +1,38 @@
|
||||
import { Router } from 'express';
|
||||
import { getAllNotes } from '../db/notes.dao.js';
|
||||
import { pipeline } from 'node:stream/promises';
|
||||
import { getAllNotes, streamAllNotes } from '../db/notes.dao.js';
|
||||
import { clusterNotes } from '../services/clustering.service.js';
|
||||
import { jsonArray } from '../lib/streams.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const notes = await getAllNotes();
|
||||
res.json(notes);
|
||||
const rows = await streamAllNotes();
|
||||
res.type('application/json');
|
||||
await pipeline(rows, jsonArray(), res);
|
||||
} catch (err) {
|
||||
console.error(`Error loading notes: ${err}`);
|
||||
if (res.headersSent) {
|
||||
res.destroy(err);
|
||||
return;
|
||||
}
|
||||
res.status(500).json({ error: 'Failed to load notes' });
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/cluster', async (req, res) => {
|
||||
const controller = new AbortController();
|
||||
res.on('close', () => {
|
||||
if (!res.writableEnded) controller.abort();
|
||||
});
|
||||
|
||||
try {
|
||||
const notes = await getAllNotes();
|
||||
const result = await clusterNotes(notes);
|
||||
const result = await clusterNotes(notes, { signal: controller.signal });
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
if (controller.signal.aborted) return;
|
||||
console.error(`Clustering failed: ${err}`);
|
||||
res.status(500).json({ error: 'Clustering failed' });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user