Skip to content

feat: Implement EventSource#2608

Merged
KhafraDev merged 63 commits into
nodejs:mainfrom
Uzlopak:eventsource
Jan 24, 2024
Merged

feat: Implement EventSource#2608
KhafraDev merged 63 commits into
nodejs:mainfrom
Uzlopak:eventsource

Conversation

@Uzlopak

@Uzlopak Uzlopak commented Jan 11, 2024

Copy link
Copy Markdown
Contributor

This is my attempt to implement eventsource into undici and thus into nodejs.

I am currently working on it, of course. EventSourceStream is not working perfect, so that would be my priority. After that I would focus on spec compliance.

But my biggest issue still needs clarification:

EventSource in a Browser UserAgent expects that you can use "withCredentials" option to send cookies with the request. So this is kind of limiting as I assume that node native fetch is not storing any cookies. So maybe we should deviate from the spec and allow maybe like npm:eventsource to pass headers via the options parameter. Thus you could also use the authorization header to connect to a EventSource server.change

@KhafraDev
I am looking forward for some hints regarding expected coding style in undici. E.g. should I replace the private properties to symbols?

Should I integrate wpt tests? Tbh. they are not working out of the box, and needs python.

Looking forward for your feedback.

This relates to...

Rationale

Changes

Features

Bug Fixes

Breaking Changes and Deprecations

Status

@codecov-commenter

codecov-commenter commented Jan 11, 2024

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.03150% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.44%. Comparing base (e39a632) to head (42fbe93).
⚠️ Report is 1190 commits behind head on main.

Files with missing lines Patch % Lines
lib/eventsource/eventsource.js 96.09% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2608      +/-   ##
==========================================
- Coverage   85.54%   85.44%   -0.11%     
==========================================
  Files          76       84       +8     
  Lines        6858     7544     +686     
==========================================
+ Hits         5867     6446     +579     
- Misses        991     1098     +107     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@KhafraDev KhafraDev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

private properties are fine and preferred :)

regarding the WPTs we have our own test runner, but it's not as easy to use (you'd have to implement the routes for yourself, for example). There is also a subsequent section of the contributing guide that may help. I'd be willing to do this for you if you'd prefer, it'd save you a lot of work 😅.

@Uzlopak

Uzlopak commented Jan 11, 2024

Copy link
Copy Markdown
Contributor Author

@KhafraDev

I would be very grateful if you would add the wpt tests :). I invited you to my fork. So should be easier for you to directly pushing to it.

It feels strange, that some attributes are private and some are symbols. Also comparing to other parts of the codebase looks not very tidy.

Anyway: Looking forward for every nitpick.

@KhafraDev

Copy link
Copy Markdown
Member

added the tests, you can run them by doing node test/wpt/start-eventsource.mjs

Comment thread test/wpt/runner/worker.mjs Fixed
@KhafraDev

Copy link
Copy Markdown
Member

I can start reviewing now if you'd like, but currently the WPTs are failing because the WPTs use relative urls but you're not passing a base URL, for example:

const baseURL = getGlobalOrigin()
// 1. Let urlRecord be the result of applying the URL parser to url with baseURL.
let urlRecord
try {
urlRecord = new URL(url, baseURL)

@Uzlopak

Uzlopak commented Jan 11, 2024

Copy link
Copy Markdown
Contributor Author

@KhafraDev

Awesome! Thanks for the snippet with getGlobalOrigin. Now 3 only wpt tests fail, which are related to the EventSourceStream, which I already said is not up to standard.

Please review the code, so that I can tackle them asap you write that you finished the review :).

Please be nitpicky as possible :)

Thank you!

Comment thread lib/eventsource/eventsource.js
Comment thread lib/eventsource/eventsource.js Outdated
Comment thread lib/eventsource/eventsource.js Outdated
Comment thread lib/eventsource/eventsource.js Outdated
Comment thread lib/eventsource/eventsource.js Outdated
Comment thread lib/eventsource/eventsource.js Outdated
Comment thread lib/eventsource/eventsource.js Outdated
@Uzlopak

Uzlopak commented Jan 11, 2024

Copy link
Copy Markdown
Contributor Author

I will continue tonight to work on this. It feels much cleaner to implement it in undici than in node core itself.

Stay tuned ;)

Comment thread lib/eventsource/eventsource.js Outdated

@metcoder95 metcoder95 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So far LGTM, nice work @Uzlopak 🚀
I'd like to go on a second review later this weekend, to also have the time to read the spec a bit more; if that's ok?

Comment thread lib/eventsource/eventsource-stream.js
@Uzlopak

Uzlopak commented Jan 12, 2024

Copy link
Copy Markdown
Contributor Author

I have following tasks to do:

  • Make it more like the fetch implementation where every step is commented implemented according to the spec
  • Make the parser feature complete
  • Check if the state parameter, shared between the EventSource and EventSourceStream is complete, or if it should be modified
  • 100% test coverage, if possible

But you are invited to review and nitpick as much as possible as early as possible ;)

@Uzlopak

Uzlopak commented Jan 14, 2024

Copy link
Copy Markdown
Contributor Author

Ok, I figured now out how to it like you did it for fetch. Obviously I have to implement it differently. My local code is not working, so I wont push. Tomorrow/Sunday I willl take care of it.

Comment thread lib/eventsource/events.js Outdated
Comment thread lib/eventsource/index.js
Comment thread lib/eventsource/index.js Outdated
Comment thread lib/eventsource/index.js Outdated
Comment thread lib/eventsource/index.js Outdated
Comment thread lib/eventsource/index.js Outdated
@Uzlopak

Uzlopak commented Jan 20, 2024

Copy link
Copy Markdown
Contributor Author

I think for a first implementation this is good enough. Maybe the Buffer.subarray calls can be reduced. But it is currently working. So would be happy if you do a review now ;).

@Uzlopak Uzlopak changed the title feat: implement eventsource WIP feat: implement eventsource Jan 20, 2024
@Uzlopak Uzlopak changed the title feat: implement eventsource feat: implement EventSource Jan 20, 2024

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you add TODOs to remove subarrays and concats for performance reasons?

Comment thread lib/eventsource/eventsource-stream.js
Comment thread lib/eventsource/eventsource-stream.js
Comment thread lib/eventsource/eventsource-stream.js
@KhafraDev

Copy link
Copy Markdown
Member

@Uzlopak we already drop those headers on cross-origin redirect

@Uzlopak

Uzlopak commented Jan 20, 2024

Copy link
Copy Markdown
Contributor Author

@KhafraDev
perfect!

@mcollina
Added the TODOs and created an issue with some more information regarding it. Maybe #2630 can be then labelled good-first-issue?

@Uzlopak Uzlopak changed the title feat: implement EventSource feat: Implement EventSource Jan 20, 2024
@mcollina

Copy link
Copy Markdown
Member

I don't think it'd be so easy ;).

@mcollina

Copy link
Copy Markdown
Member

I'd prefer to wait until the new v21.6.1 release is out so we can test this there too.

@metcoder95 metcoder95 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice work 🚀

@Uzlopak

Uzlopak commented Jan 22, 2024

Copy link
Copy Markdown
Contributor Author

@mcollina
@KhafraDev
@metcoder95
The CI/CD pipeline is green now ;).

@KhafraDev
KhafraDev merged commit e2652b7 into nodejs:main Jan 24, 2024
@Uzlopak
Uzlopak deleted the eventsource branch January 24, 2024 08:13
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.

6 participants