⚡ Bolt: Async stream optimization for static files#93
⚡ Bolt: Async stream optimization for static files#93Dexploarer wants to merge 1 commit intomainfrom
Conversation
Replaces synchronous `fs.statSync` and `fs.readFileSync` with asynchronous `fs.promises.stat` and `fs.createReadStream` in `serveStaticUi` to prevent blocking the event loop during static asset serving. This improves server responsiveness under load. - Adds `streamStaticResponse` helper for piping streams to response - Updates `handleRequest` to await `serveStaticUi` - Ensures proper error handling for file streams
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @Dexploarer, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the performance and responsiveness of static file serving by migrating from synchronous, blocking file I/O operations to asynchronous streaming. This change offloads file reading to the thread pool and allows the server to handle more concurrent requests without degrading performance, particularly for larger assets. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request optimizes static file serving by switching from synchronous file reads to asynchronous streams. This is a great performance improvement that avoids blocking the Node.js event loop. The changes in src/api/server.ts correctly replace fs.readFileSync with fs.createReadStream and make the serving function asynchronous.
I've added one comment to make the new streaming logic more robust by handling client disconnects and improving error handling, which will prevent potential resource leaks.
| stream.pipe(res); | ||
| stream.on("error", (err) => { | ||
| logger.error( | ||
| `[milady-api] Static stream error: ${err instanceof Error ? err.message : err}`, | ||
| ); | ||
| if (!res.headersSent) { | ||
| res.statusCode = 500; | ||
| res.end("Internal Server Error"); | ||
| } | ||
| }); |
There was a problem hiding this comment.
The current implementation for streaming static responses can be made more robust.
- Error Handling Order: It's safer to attach error handlers before piping the stream to the response to catch any potential synchronous errors.
- Client Disconnect: If a client disconnects while the file is being streamed, the read stream may continue to read the file from disk, leading to a resource leak. You should listen for the
closeevent on the response object and destroy the file stream. - Error After Headers Sent: If a stream error occurs after headers have been sent, the response cannot be changed to a 500 error. In this case, it's best to destroy the response socket to signal an error to the client.
Here is a suggested improvement that addresses these points:
stream.on("error", (err) => {
logger.error(
`[milady-api] Static stream error: ${err instanceof Error ? err.message : err}`,
);
if (!res.headersSent) {
res.statusCode = 500;
res.end("Internal Server Error");
} else {
res.destroy();
}
});
res.on("close", () => {
stream.destroy();
});
stream.pipe(res);
⚡ Bolt: Optimized static file serving to use async streams.
💡 What: Replaced synchronous
fs.readFileSyncwithfs.createReadStreamandfs.statSyncwithfs.promises.statinsrc/api/server.ts.🎯 Why: Reading files synchronously blocks the Node.js event loop, which degrades performance and responsiveness, especially for larger assets or under concurrent load.
📊 Impact: Improves server throughput and responsiveness by offloading file I/O to the thread pool and streaming content.
🔬 Measurement: Verified logic correctness; manual verification test created (but not included due to env constraints). The change aligns with Node.js best practices for static file serving.
PR created automatically by Jules for task 6346858301705708063 started by @Dexploarer