Skip to content

feat: implement the internal ws runtime - #581

Merged
electrohyun merged 9 commits into
mainfrom
feat/ws-basic-runtime
Nov 11, 2026
Merged

electrohyun merged 9 commits into
mainfrom
feat/ws-basic-runtime

Conversation

@electrohyun

@electrohyun electrohyun commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

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.ts stores server registrations separately from Socket.IO. It matches connections by host, port, and path, assigns a virtual port for port: 0, and handles the local aliases localhost, 127.0.0.1, and ::1. Closing a server removes its registration so the address can be used again.

src/ws/runtime.ts implements the internal WebSocket and WebSocketServer constructors. 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 exposing address().

Connection and message events are scheduled through microtasks. The client receives open before an immediate server greeting. Text sent through send() becomes a Buffer and reaches an ordinary message listener with isBinary=false; an onmessage handler 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.ts reuses the existing NodeEmitter while adapting registration aliases and listener removal to ws behavior. The comparisons cover on(), once(), off(), listener inspection, dispatch order, duplicate listeners, and the wrappers returned by rawListeners() on the server and both socket sides.

src/ws/event-target.ts and src/ws/events.ts add event handler properties, addEventListener(), and removeEventListener(). 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/ws exports, 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

  • Tests added or updated
  • Behavior verified against real ws; existing Socket.IO comparisons pass

@electrohyun electrohyun self-assigned this Nov 11, 2026
@vercel

vercel Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
smocket Ready Ready Preview Nov 11, 2026 8:33am UTC

@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

Pre-merge checks: Inconclusive 2

Failed Failed checks: 2 inconclusive

Check name Status Explanation Resolution
Title check Pending This assessment has not completed. The check will be retried on the next pre-merge check run.
Description check Pending This assessment has not completed. The check will be retried on the next pre-merge check run.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions github-actions Bot added enhancement New feature or request 📏 xl Pull request changes at least 1,000 lines labels Nov 11, 2026
@codecov

codecov Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@electrohyun
electrohyun merged commit ad5b05b into main Nov 11, 2026
18 checks passed
@electrohyun
electrohyun deleted the feat/ws-basic-runtime branch November 11, 2026 08:42

This branch was successfully deployed

1 active deployment
Preview — f8f3fe67 Deployed Nov 11, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request 📏 xl Pull request changes at least 1,000 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: implement the internal ws runtime

1 participant