Repository navigation
feat: implement the internal ws runtime - #581
Merged
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add an internal ws runtime so a client and a server handler can connect, exchange text, and close their connection through objects stored in memory. This provides the runtime implementation for #580, following the usage and type contract already defined in #555.
Connection and message delivery
src/ws/registry.tsstores server registrations separately from Socket.IO. It matches connections by host, port, and path, assigns a virtual port forport: 0, and handles the local aliaseslocalhost,127.0.0.1, and::1. Closing a server removes its registration so the address can be used again.src/ws/runtime.tsimplements the internalWebSocketandWebSocketServerconstructors. A connection creates a socket on the server and pairs it with the client through object references. The server receives basic request information, including the URL, method, and automatic headers. The constructor callback receives a separate object exposingaddress().Connection and message events are scheduled through microtasks. The client receives
openbefore an immediate server greeting. Text sent throughsend()becomes a Buffer and reaches an ordinary message listener withisBinary=false; anonmessagehandler receives the text as a string. Normal close updates both sockets, delivers the close code and reason, and removes the socket from the server's internal set. The mock creates no HTTP server or TCP connection.Event APIs and input validation
src/ws/emitter.tsreuses the existingNodeEmitterwhile adapting registration aliases and listener removal to ws behavior. The comparisons coveron(),once(),off(), listener inspection, dispatch order, duplicate listeners, and the wrappers returned byrawListeners()on the server and both socket sides.src/ws/event-target.tsandsrc/ws/events.tsadd event handler properties,addEventListener(), andremoveEventListener(). They preserve callback receivers, listener identity, replacement order, and event fields. Listener metadata uses WeakMaps associated with the registered callbacks, so removal through EventEmitter is reflected in event property lookups.URL handling normalizes supported protocols and snapshots the URL before the asynchronous connection attempt. Close validation follows the observable state changes and exception order of real ws, including allowed code ranges, reason byte limits, and argument handling while a socket is connecting, closing, or closed.
Tests and documentation
The same application tests run against real ws and the internal mock. Separate mock tests cover operation without network access, virtual host selection, and isolation from Socket.IO. Compile checks exercise the actual runtime's callback and event types. The conformance generator now reports ws comparisons separately from Socket.IO comparisons, and the review documents record the guarantees and remaining limits.
Remaining scope
Public
smocket/wsexports, generated package declarations, and independent package consumers remain in #562. Client tracking and applications with several clients remain in #539. Byte inputs, send callbacks, and detailed comparisons for failures, pending work, and concurrent close remain in #540. Shared core extraction remains in #541. This PR closes the internal runtime task while #538 continues to track the related work.Related issue
Closes #580
Checklist