From b044570f726523a96f74b4d93fb72d7fce94e158 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 14 Feb 2026 13:56:15 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20optimize:=20replace=20blocking=20sy?= =?UTF-8?q?nc=20I/O=20in=20dashboard=20request=20handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Converted the dashboard HTTP request handler to an async function and replaced synchronous `fs.existsSync` and `fs.statSync` calls with non-blocking `fs.promises.stat`. This improves server responsiveness and concurrency by ensuring the event loop is not blocked during file system checks. Also added robust error handling to the asynchronous handler to prevent unhandled promise rejections and ensure consistent 500 responses on internal errors. Co-authored-by: PR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> --- src/lib.mjs | 129 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 55 deletions(-) diff --git a/src/lib.mjs b/src/lib.mjs index d36b5d8..75eada6 100644 --- a/src/lib.mjs +++ b/src/lib.mjs @@ -486,65 +486,84 @@ export async function cmdDashboard(port = DEFAULT_DASHBOARD_PORT) { // this file is in /src/lib.mjs, so '..' goes to const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); - const server = http.createServer((req, res) => { - // URL parsing - const url = new URL(req.url, `http://${req.headers.host}`); - let pathname = url.pathname; + const server = http.createServer(async (req, res) => { + try { + // URL parsing + const url = new URL(req.url, `http://${req.headers.host}`); + let pathname = url.pathname; - // Redirect / to /dashboard/ - if (pathname === '/' || pathname === '/dashboard') { - res.writeHead(302, { 'Location': '/dashboard/' }); - res.end(); - return; - } - - // Special case: /torch-config.json - // Priority: User's CWD > Package default - if (pathname === '/torch-config.json') { - const userConfigPath = path.resolve(process.cwd(), 'torch-config.json'); - if (fs.existsSync(userConfigPath)) { - res.writeHead(200, { 'Content-Type': 'application/json' }); - fs.createReadStream(userConfigPath).pipe(res); + // Redirect / to /dashboard/ + if (pathname === '/' || pathname === '/dashboard') { + res.writeHead(302, { 'Location': '/dashboard/' }); + res.end(); return; } - // If not found in CWD, fall through to serve from packageRoot (if it exists there) - // or return empty object if missing? - // Falling through means it looks for packageRoot/torch-config.json + + // Special case: /torch-config.json + // Priority: User's CWD > Package default + if (pathname === '/torch-config.json') { + const userConfigPath = path.resolve(process.cwd(), 'torch-config.json'); + try { + const stats = await fs.promises.stat(userConfigPath); + if (stats.isFile()) { + res.writeHead(200, { 'Content-Type': 'application/json' }); + fs.createReadStream(userConfigPath).pipe(res); + return; + } + } catch { + // If not found in CWD, fall through to serve from packageRoot (if it exists there) + } + } + + // Security check: prevent directory traversal + const safePath = path.normalize(pathname).replace(new RegExp('^(\\.\\.[\\/\\\\])+'), ''); + let filePath = path.join(packageRoot, safePath); + + let stats; + try { + stats = await fs.promises.stat(filePath); + // If directory, try index.html + if (stats.isDirectory()) { + filePath = path.join(filePath, 'index.html'); + stats = await fs.promises.stat(filePath); + } + } catch { + res.writeHead(404); + res.end('Not Found'); + return; + } + + // Check if file exists and is a file + if (!stats.isFile()) { + res.writeHead(404); + res.end('Not Found'); + return; + } + + // MIME types + const extname = path.extname(filePath); + let contentType = 'text/plain'; + switch (extname) { + case '.html': contentType = 'text/html'; break; + case '.js': contentType = 'text/javascript'; break; + case '.css': contentType = 'text/css'; break; + case '.json': contentType = 'application/json'; break; + case '.png': contentType = 'image/png'; break; + case '.jpg': contentType = 'image/jpeg'; break; + case '.svg': contentType = 'image/svg+xml'; break; + case '.ico': contentType = 'image/x-icon'; break; + case '.md': contentType = 'text/markdown'; break; + } + + res.writeHead(200, { 'Content-Type': contentType }); + fs.createReadStream(filePath).pipe(res); + } catch (err) { + console.error('Dashboard error:', err); + if (!res.headersSent) { + res.writeHead(500); + res.end('Internal Server Error'); + } } - - // Security check: prevent directory traversal - const safePath = path.normalize(pathname).replace(new RegExp('^(\\.\\.[\\/\\\\])+'), ''); - let filePath = path.join(packageRoot, safePath); - - // If directory, try index.html - if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) { - filePath = path.join(filePath, 'index.html'); - } - - // Check if file exists and is a file - if (!fs.existsSync(filePath) || !fs.statSync(filePath).isFile()) { - res.writeHead(404); - res.end('Not Found'); - return; - } - - // MIME types - const extname = path.extname(filePath); - let contentType = 'text/plain'; - switch (extname) { - case '.html': contentType = 'text/html'; break; - case '.js': contentType = 'text/javascript'; break; - case '.css': contentType = 'text/css'; break; - case '.json': contentType = 'application/json'; break; - case '.png': contentType = 'image/png'; break; - case '.jpg': contentType = 'image/jpeg'; break; - case '.svg': contentType = 'image/svg+xml'; break; - case '.ico': contentType = 'image/x-icon'; break; - case '.md': contentType = 'text/markdown'; break; - } - - res.writeHead(200, { 'Content-Type': contentType }); - fs.createReadStream(filePath).pipe(res); }); server.listen(port, () => {