Skip to content

fix: do not use ReadableStream async iterator in SSE stream parsing#311

Merged
ishymko merged 5 commits intoa2aproject:mainfrom
onfabric:main
Jan 23, 2026
Merged

fix: do not use ReadableStream async iterator in SSE stream parsing#311
ishymko merged 5 commits intoa2aproject:mainfrom
onfabric:main

Conversation

@ilbertt
Copy link
Copy Markdown
Contributor

@ilbertt ilbertt commented Jan 22, 2026

Description

The async iterator in ReadableStream is not available in browsers like Safari: MDN. This PR changes it to a lower-level API which has better support across different environments.

Fixes #308.

@ilbertt ilbertt requested a review from a team as a code owner January 22, 2026 13:25
@ilbertt ilbertt changed the title fix: ensure body stream has an async iterator fix: ensure SSE body stream has an async iterator Jan 22, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @ilbertt, 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 resolves a critical browser compatibility issue related to ReadableStream objects lacking native Symbol.asyncIterator support in environments like Safari. By introducing a custom pipeThrough utility, it ensures that Server-Sent Events (SSE) parsing can consistently leverage for await...of syntax, thereby enhancing the robustness and cross-browser functionality of stream processing.

Highlights

  • Browser Compatibility Fix: Implemented a fallback for ReadableStream to ensure Symbol.asyncIterator is available, addressing compatibility issues in browsers like Safari where it might be missing.
  • New pipeThrough Utility: Introduced a pipeThrough function that wraps TextDecoderStream and conditionally adds a custom Symbol.asyncIterator to the stream if it's not natively present, ensuring for await...of loops work consistently.
  • Custom Async Iterator Implementation: Added a createAsyncIterator helper function to provide a manual async iterator for ReadableStream instances, which is used by the new pipeThrough utility.
  • Enhanced Test Coverage: Included new test cases to validate the async iterator fallback mechanism and confirm proper resource cleanup (e.g., reader.releaseLock()) when iterating streams is terminated early.
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.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 22, 2026

🧪 Code Coverage

⬇️ Download Full Report

Main PR Delta
src/sse_utils.ts 90.9% 93.22% 🟢 +2.32%
Total 80% 80.06% 🟢 +0.06%

Generated by coverage-comment.yml

Copy link
Copy Markdown
Contributor

@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 pull request introduces a polyfill for "Symbol.asyncIterator" on "ReadableStream" to enhance compatibility across different browser environments, particularly Safari. This is a valuable improvement for robust SSE stream parsing. The new "createAsyncIterator" and "pipeThrough" functions are well-structured, and the "try...finally" block in "createAsyncIterator" ensures proper resource cleanup. The added test cases effectively validate the new fallback mechanism and early termination scenarios. The identified type inaccuracies should be addressed for improved type safety and clarity.

Comment thread src/sse_utils.ts Outdated
Comment thread src/sse_utils.ts Outdated
Copy link
Copy Markdown
Member

@ishymko ishymko left a comment

Choose a reason for hiding this comment

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

Thank you for the prompt fix!

BTW were you able to test it on a real iOS? We don't have this kind of integration tests running on a real iOS so if you were able to confirm it it'd be great.

Comment thread src/sse_utils.ts Outdated
@ilbertt
Copy link
Copy Markdown
Contributor Author

ilbertt commented Jan 22, 2026

BTW were you able to test it on a real iOS? We don't have this kind of integration tests running on a real iOS so if you were able to confirm it it'd be great.

I tested the workaround from my comment, and it was working. If you can release a beta/patch version, I'm happy to pull it in and test it

Copy link
Copy Markdown
Member

@ishymko ishymko left a comment

Choose a reason for hiding this comment

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

I tested the workaround from #308 (comment)

Sounds good, thank you!

A few minor comments and should be good to go.

Comment thread src/sse_utils.ts Outdated
Comment thread test/sse_utils.spec.ts Outdated
Comment thread test/sse_utils.spec.ts Outdated
Comment thread test/sse_utils.spec.ts Outdated
@ishymko ishymko changed the title fix: ensure SSE body stream has an async iterator fix: add async iterator fallback for SSE stream parsing Jan 23, 2026
@ishymko ishymko changed the title fix: add async iterator fallback for SSE stream parsing fix: do not use ReadableStream async iterator in SSE stream parsing Jan 23, 2026
@ilbertt
Copy link
Copy Markdown
Contributor Author

ilbertt commented Jan 23, 2026

@ishymko I've pushed some updates, let me know if I need to do anything else!

@ilbertt ilbertt requested a review from ishymko January 23, 2026 09:41
Comment thread test/sse_utils.spec.ts Outdated
Copy link
Copy Markdown
Member

@ishymko ishymko left a comment

Choose a reason for hiding this comment

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

Thank you for the contribution!

Should be released next week or the week after next at the latest.

@ishymko ishymko merged commit 5359fa8 into a2aproject:main Jan 23, 2026
16 checks passed
ishymko pushed a commit that referenced this pull request Feb 3, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.3.10](v0.3.9...v0.3.10)
(2026-01-27)


### Bug Fixes

* do not use ReadableStream async iterator in SSE stream parsing
([#311](#311))
([5359fa8](5359fa8))
* preserve typed error as cause in SSE errors
([#315](#315))
([d39544e](d39544e))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
@ilbertt
Copy link
Copy Markdown
Contributor Author

ilbertt commented Feb 3, 2026

@ishymko to close the loop, I've just bumped the @a2a-js/sdk to v0.3.10 for our production app and everything works.

Thanks again for the help!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: value of response.body.pipeThrough is undefined (SSE)

2 participants