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

implement WebSocketStream #2713

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

KhafraDev
Copy link
Member

@KhafraDev KhafraDev commented Feb 7, 2024

https://whatpr.org/websockets/48/7b748d3...7b81f79.html#the-websocketstream-interface

Now that the spec has been written, it's time to add it.

const ws = new WebSocketStream(`wss://my.websocket.server`)

ws.opened.then((init) => {
  const writer = init.writable.getWriter()

  writer.ready.then(() => writer.write(Buffer.from([65, 66, 67])))
})

@codecov-commenter
Copy link

codecov-commenter commented Feb 7, 2024

Codecov Report

Attention: Patch coverage is 45.62079% with 565 lines in your changes are missing coverage. Please review.

Project coverage is 91.63%. Comparing base (03a2d43) to head (13187d0).
Report is 1 commits behind head on main.

Files Patch % Lines
lib/websocket/stream/websocketstream.js 0.00% 359 Missing ⚠️
lib/web/websocket/stream/websocketstream.js 71.98% 100 Missing ⚠️
lib/websocket/stream/websocketerror.js 0.00% 76 Missing ⚠️
lib/web/websocket/util.js 82.92% 28 Missing ⚠️
lib/web/fetch/webidl.js 60.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2713      +/-   ##
==========================================
- Coverage   93.64%   91.63%   -2.01%     
==========================================
  Files          86       90       +4     
  Lines       23862    24897    +1035     
==========================================
+ Hits        22345    22815     +470     
- Misses       1517     2082     +565     

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

@KhafraDev KhafraDev marked this pull request as ready for review February 8, 2024 23:00
@KhafraDev
Copy link
Member Author

@nodejs/undici this is ready to review now, although this should not be merged until WebSocketStream is added into the spec. whatwg/websockets#48

@ronag
Copy link
Member

ronag commented Feb 25, 2024

@KhafraDev I don't have so much opinion on this one. I'm not interested in the Web API stuff. Which is why I'd like to separate them out more from undici core.

If you can get one more approving review it should be mergeable. @mcollina

@KhafraDev
Copy link
Member Author

Maybe we could start pinging the web standards team for reviews?

lib/websocket/stream/websocketstream.js Show resolved Hide resolved
// 12. Let writable be a new WritableStream .
// 13. Set up writable with writeAlgorithm , closeAlgorithm , and abortAlgorithm .
const writable = new WritableStream({
write: (chunk) => this.#write(chunk),
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is respecting backpressure.

Copy link
Member Author

@KhafraDev KhafraDev Mar 6, 2024

Choose a reason for hiding this comment

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

WritableStream should have backpressure built into it, at least according to mdn https://developer.mozilla.org/en-US/docs/Web/API/WritableStream

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but you are not forwarding the information of the underlining socket being busy up.
https://github.com/nodejs/undici/pull/2713/files#diff-c1287dec52a96e140d7231c268bfa35a6f1e0390c46ccaf15b3ba5834f2a7342R322 will return false if the socket is "full", and then emit a 'drain' event when free again. This is not connected in any way to the WritableStream.

lib/websocket/stream/websocketstream.js Show resolved Hide resolved
@@ -8,5 +8,6 @@ module.exports = {
kBinaryType: Symbol('binary type'),
kSentClose: Symbol('sent close'),
kReceivedClose: Symbol('received close'),
kByteParser: Symbol('byte parser')
kByteParser: Symbol('byte parser'),
kPromises: Symbol('kPromises')
Copy link
Contributor

Choose a reason for hiding this comment

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

keep the style?

Suggested change
kPromises: Symbol('kPromises')
kPromises: Symbol('promises')

@KhafraDev KhafraDev force-pushed the websocket-stream branch 2 times, most recently from ae7a600 to 1abf223 Compare March 6, 2024 02:07
enumerable

implement some more

implement close

finish open

run WPTs

update wpts and fix 9/19 failures

fix most remaining failures

fix remaining test failures

fix writing

fix: implement writing completely

fixup

fixup
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.

None yet

5 participants