Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
perf: convert sync file I/O to async in route handlers
Adam Eivydb1c36b0491e
diff --git a/.changelog/v0.17.x.md b/.changelog/v0.17.x.mdindex dfae68b8b6..94a726c51b 100644--- a/.changelog/v0.17.x.md+++ b/.changelog/v0.17.x.md@@ -18,3 +18,9 @@ JIRA integration improvements and token lifecycle management.### CityHud scan line: CSS animation instead of setInterval- Replaced 50ms `setInterval` (20 React re-renders/sec) with a pure CSS `@keyframes` animation- Eliminates unnecessary React state updates and re-renders for a visual-only effect++### Convert sync file I/O to async in server route handlers+- `attachments.js` GET /: replaced `readdirSync` + `statSync` loop with async `readdir` + `Promise.all(stat)`+- `detect.js` POST /repo: replaced `statSync` with async `stat`+- `visionTest.js` runVisionTestSuite: replaced `readdirSync` with async `readdir`+- Prevents event loop blocking when listing large directoriesdiff --git a/server/routes/attachments.js b/server/routes/attachments.jsindex 146081c0c5..958d9573d4 100644--- a/server/routes/attachments.js+++ b/server/routes/attachments.js@@ -4,7 +4,7 @@*/import { Router } from 'express';-import { writeFile, mkdir, unlink } from 'fs/promises';+import { writeFile, mkdir, unlink, readdir, stat } from 'fs/promises';import { existsSync } from 'fs';import { join, dirname, resolve, basename, extname } from 'path';import { fileURLToPath } from 'url';@@ -191,16 +191,14 @@ router.delete('/:filename', asyncHandler(async (req, res) => {// GET /api/attachments - List all attachments (for debugging)router.get('/', asyncHandler(async (req, res) => {- const { readdirSync, statSync } = await import('fs');-if (!existsSync(ATTACHMENTS_DIR)) {return res.json({ attachments: [] });}- const files = readdirSync(ATTACHMENTS_DIR);- const attachments = files.map(filename => {+ const files = await readdir(ATTACHMENTS_DIR);+ const attachments = await Promise.all(files.map(async filename => {const filepath = join(ATTACHMENTS_DIR, filename);- const stats = statSync(filepath);+ const stats = await stat(filepath);const ext = getExtension(filename);return {filename,@@ -209,7 +207,7 @@ router.get('/', asyncHandler(async (req, res) => {mimeType: ALLOWED_EXTENSIONS[ext] || 'application/octet-stream',createdAt: stats.birthtime};- });+ }));res.json({ attachments });}));diff --git a/server/routes/detect.js b/server/routes/detect.jsindex 391401b085..d745e816df 100644--- a/server/routes/detect.js+++ b/server/routes/detect.js@@ -1,6 +1,6 @@import { Router } from 'express';-import { existsSync, statSync } from 'fs';-import { readFile } from 'fs/promises';+import { existsSync } from 'fs';+import { readFile, stat } from 'fs/promises';import { join } from 'path';import { exec } from 'child_process';import { promisify } from 'util';@@ -28,7 +28,7 @@ router.post('/repo', asyncHandler(async (req, res) => {}// Check if it's a directory- const stats = statSync(path);+ const stats = await stat(path);if (!stats.isDirectory()) {return res.json({valid: false,diff --git a/server/services/visionTest.js b/server/services/visionTest.jsindex fdd2fb487f..2f27fc11ad 100644--- a/server/services/visionTest.js+++ b/server/services/visionTest.js@@ -5,7 +5,7 @@* and verifying the model can correctly interpret them.*/-import { readFile } from 'fs/promises';+import { readFile, readdir } from 'fs/promises';import { existsSync } from 'fs';import { join, dirname, resolve, extname } from 'path';import { fileURLToPath } from 'url';@@ -206,10 +206,9 @@ export async function testVision({ imagePath, prompt, expectedContent, providerI*/export async function runVisionTestSuite(providerId = 'lmstudio', model) {const results = [];- const screenshotFiles = await import('fs').then(fs =>- fs.readdirSync(SCREENSHOTS_DIR).filter(f =>- /\.(png|jpg|jpeg|gif|webp)$/i.test(f)- )+ const allFiles = await readdir(SCREENSHOTS_DIR).catch(() => []);+ const screenshotFiles = allFiles.filter(f =>+ /\.(png|jpg|jpeg|gif|webp)$/i.test(f));if (screenshotFiles.length === 0) {diff --git a/server/services/visionTest.test.js b/server/services/visionTest.test.jsindex ddb043c2d8..8a60100150 100644--- a/server/services/visionTest.test.js+++ b/server/services/visionTest.test.js@@ -6,21 +6,21 @@ vi.mock('./providers.js', () => ({getProviderById: vi.fn()}));-// Mock fs/promises for image loading+// Mock fs/promises for image loading and directory listingvi.mock('fs/promises', () => ({- readFile: vi.fn()+ readFile: vi.fn(),+ readdir: vi.fn()}));// Mock fs for existsSyncvi.mock('fs', () => ({- existsSync: vi.fn(),- readdirSync: vi.fn()+ existsSync: vi.fn()}));// Import mocked modulesimport { getProviderById } from './providers.js';-import { readFile } from 'fs/promises';-import { existsSync, readdirSync } from 'fs';+import { readFile, readdir } from 'fs/promises';+import { existsSync } from 'fs';describe('Vision Test Service', () => {const mockProvider = {@@ -218,7 +218,7 @@ describe('Vision Test Service', () => {describe('runVisionTestSuite', () => {it('should return error when no screenshots available', async () => {- readdirSync.mockReturnValue([]);+ readdir.mockResolvedValue([]);const result = await runVisionTestSuite('lmstudio');@@ -227,7 +227,7 @@ describe('Vision Test Service', () => {});it('should run multiple tests on available screenshots', async () => {- readdirSync.mockReturnValue(['test1.png', 'test2.jpg']);+ readdir.mockResolvedValue(['test1.png', 'test2.jpg']);getProviderById.mockResolvedValue(mockProvider);existsSync.mockReturnValue(true);readFile.mockResolvedValue(mockImageBuffer);