Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Stream to use eventstream-parser library #214

Merged
merged 5 commits into from
Mar 12, 2024
Merged

Conversation

aron
Copy link
Contributor

@aron aron commented Mar 6, 2024

Based on top of the unit tests added in #211 and taking inspiration from #202 this PR aims to simplify our EventSource parsing logic to use a standardized library, eventsource-parser, designed to work across different environments including browsers, Node and Deno.

This is bundled into the repository under the vendor/eventsource-parser/stream.js file, bundlejs was used to create the file and there is documentation in the README about how to regenerate it.

The stream is now built on top of the ReadableStream primitive. Which is available in Node >= 18.

I've also removed the fetch shim from the tests as it's outdated and doesn't support streaming the body property. This just required updating nock to use a v14 beta which supports fetch.

I've tested it on Cloudflare workers, but unfortunately we've added a bunch more node dependencies that need to be fixed before it'll work there. See #215 for more details on what needs to be done next.

@aron aron force-pushed the refactor-stream branch from 09c1ffe to 6142099 Compare March 6, 2024 13:38
@aron aron marked this pull request as ready for review March 6, 2024 13:43
@aron aron requested a review from mattt March 6, 2024 13:48
@aron aron changed the base branch from fix-stream to main March 6, 2024 13:48
@aron aron force-pushed the refactor-stream branch 3 times, most recently from 7e68022 to 18a182d Compare March 6, 2024 13:52
@aron aron changed the title WIP: Refactor stream Refactor Stream to use eventstream-parser library Mar 6, 2024
@aron aron force-pushed the refactor-stream branch 3 times, most recently from 0515a03 to 63ec753 Compare March 7, 2024 18:28
@aron
Copy link
Contributor Author

aron commented Mar 7, 2024

@mattt I've now bundled the eventsource-parser/stream module, and added some documentation about how to update it in the future if needed. It's less than 200 lines so I think we're okay.

@aron aron force-pushed the refactor-stream branch from 63ec753 to 901745a Compare March 7, 2024 18:31
@aron aron force-pushed the refactor-stream branch from 901745a to da11489 Compare March 8, 2024 10:07
@aron
Copy link
Contributor Author

aron commented Mar 11, 2024

@mattt I'm pretty confident this one is in good shape. I'll merge it today unless you have any further concerns.

@mattt mattt merged commit 6de744f into main Mar 12, 2024
10 checks passed
@mattt mattt deleted the refactor-stream branch March 12, 2024 09:16
@mattt mattt mentioned this pull request Mar 12, 2024
aron added a commit that referenced this pull request Mar 12, 2024
This fixes a regression introduced with #214 where we were not exiting correctly when getting the `"done"` event from the server. This was picked up by the introduction of the CloudFlare integration tests added in #217 which uses the streaming API.

Once the fix was added it turns out that the `nock()` tests were incorrectly passing due to some internal weirdness when using `respondWith` and a `Readable` object. I wasn't able to get this working without hitting a different error:

    TypeError: Invalid state: Controller is already closed

It looks like nock is retaining some global state somewhere in it's implementation and streams are being retained across requests. No combination of resetting mocks seemed to fix it.

In the end I just mocked out the fetch function passed into the `createReadableStream` library and returned a `Response`. I think we should probably do this everywhere rather than use `nock()` as the Request/Response APIs provided by fetch are much better now than the old node http lib.
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.

2 participants