Skip to content

Comments

⚡ Bolt: Optimize static asset serving with async streams#99

Draft
Dexploarer wants to merge 1 commit intomainfrom
perf/static-ui-async-13203585560154505481
Draft

⚡ Bolt: Optimize static asset serving with async streams#99
Dexploarer wants to merge 1 commit intomainfrom
perf/static-ui-async-13203585560154505481

Conversation

@Dexploarer
Copy link
Owner

Refactored serveStaticUi in src/api/server.ts to use asynchronous filesystem operations (fs.promises.stat and fs.createReadStream with pipeline). This prevents the Node.js event loop from blocking while serving static UI assets in production mode, improving concurrency and responsiveness under load.

Included src/api/server.static.test.ts to verify the new async behavior and edge cases (e.g. file not found falling back to SPA index). Exported serveStaticUi and added _resetUiDirForTest to facilitate testing.


PR created automatically by Jules for task 13203585560154505481 started by @Dexploarer

@google-labs-jules
Copy link

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Comment on lines +73 to +76
setTimeout(() => {
mockFileStream.push(Buffer.from("css content"));
mockFileStream.push(null); // End of stream
}, 10);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nondeterministic test due to setTimeout for stream completion

The use of setTimeout to push data to the mock file stream in the test may cause nondeterministic behavior and flakiness:

setTimeout(() => {
  mockFileStream.push(Buffer.from("css content"));
  mockFileStream.push(null); // End of stream
}, 10);

If the test runner resolves the promise before the stream is fully pushed, the test may intermittently fail. Replace setTimeout with synchronous stream manipulation or use proper async handling (e.g., await events.once(mockFileStream, 'end')) to ensure reliable test execution.

@github-actions github-actions bot added the tests label Feb 19, 2026
@coderabbitai
Copy link

coderabbitai bot commented Feb 19, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch perf/static-ui-async-13203585560154505481

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link

Summary of Changes

Hello @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 scalability of static asset serving within the application. By transitioning from synchronous to asynchronous file operations and leveraging Node.js streams, the server can now handle static content requests more efficiently, reducing event loop blocking and improving overall responsiveness, especially in production environments. The changes are thoroughly validated with new dedicated tests.

Highlights

  • Asynchronous Static Asset Serving: The serveStaticUi function has been refactored to utilize asynchronous filesystem operations (fs.promises.stat and fs.createReadStream with pipeline). This change prevents the Node.js event loop from blocking when serving static UI assets in production, significantly improving concurrency and responsiveness under heavy load.
  • Improved Test Coverage: A new test file, src/api/server.static.test.ts, has been added to thoroughly verify the new asynchronous behavior of serveStaticUi, including edge cases like file not found scenarios that trigger the Single Page Application (SPA) index fallback. A new _resetUiDirForTest function was also exported to facilitate testing.
  • HEAD Request Optimization: Specific handling for HEAD requests has been introduced within serveStaticUi to ensure that only headers are sent, without streaming the file body, further optimizing network traffic for such requests.

🧠 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
  • src/api/server.static.test.ts
    • Added new test file for serveStaticUi.
    • Implemented tests for serving existing files with async streams.
    • Included tests for non-production environment handling.
    • Added tests for SPA fallback when a file is not found.
    • Mocked node:fs module for isolated testing of file system interactions.
  • src/api/server.ts
    • Imported pipeline from node:stream/promises for efficient stream handling.
    • Exported _resetUiDirForTest to allow clearing cached UI directory information in tests.
    • Modified serveStaticUi to be an export async function, returning a Promise<boolean>.
    • Replaced synchronous fs.statSync with asynchronous fs.promises.stat for file status checks.
    • Replaced synchronous fs.readFileSync with fs.createReadStream and pipeline for streaming file content.
    • Added logic to handle HEAD requests by sending only headers and ending the response without a body.
    • Updated the handleRequest function to await the serveStaticUi call.
Activity
  • No human activity (comments, reviews) has been recorded on this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The refactoring of serveStaticUi to use asynchronous filesystem operations and streams is a significant improvement for the server's performance and responsiveness. By using fs.promises.stat and pipeline with fs.createReadStream, the Node.js event loop is no longer blocked during file I/O, which is critical for serving static assets under load. The addition of unit tests and the _resetUiDirForTest helper ensures the new logic is verified and maintainable. I have one recommendation regarding error handling during streaming to prevent potential 'Headers already sent' errors.

await pipeline(fs.createReadStream(candidatePath), res);
return true;
}
} catch {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current try-catch block is too broad. If the pipeline fails after res.writeHead has already been called (for example, if the client disconnects prematurely), the execution will enter the catch block and proceed to the SPA fallback logic at line 1767. This will attempt to call res.writeHead again via sendStaticResponse, resulting in a ERR_HTTP_HEADERS_SENT error and potentially crashing the request handler. We should check if headers have already been sent before allowing the fallback to proceed.

  } catch (err) {
    if (res.headersSent) {
      return true;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant