Skip to content

fix: return receivers from fluent methods - #256

Merged
electrohyun merged 2 commits into
mainfrom
fix/match-fluent-returns
Aug 12, 2026
Merged

electrohyun merged 2 commits into
mainfrom
fix/match-fluent-returns

Conversation

@electrohyun

Copy link
Copy Markdown
Owner

Summary

Smocket declared and implemented five Socket.IO methods as returning void even though each method is fluent in both Socket.IO's runtime and public TypeScript surface. Server.use() and Namespace.use() registered middleware correctly, server Socket.disconnect() performed the expected teardown, and client Socket.connect() and Socket.disconnect() changed connection state correctly, but application code could not use the receiver returned by the equivalent Socket.IO call. The mismatch also erased the event maps and socket data that should remain available after a fluent call.

This changes those five contracts to return this and makes every implementation path return the object on which it was called. The server and namespace middleware methods return their own registration receiver. Server socket disconnect returns the same socket after initiating its existing teardown. Client connect returns the same socket while already connected, while starting a reconnection, and after an invalid namespace result. Client disconnect returns the same socket after an active teardown and while already disconnected. The terminal missing-origin client remains inert under ADR 0005, but its connect and disconnect methods now return that same failed client instead of undefined.

No delivery, admission, or lifecycle rule changes with the return value. Middleware order and rejection remain unchanged, reconnect still creates a fresh server socket under ADR 0013, disconnect still uses the existing reasons and cleanup order, and callers that ignore a result continue to compile. Server.on() is deliberately untouched. Socket.IO declares that method as returning this while its runtime returns the root namespace, so issue #233 excludes it rather than treating every server method as uniformly fluent.

Socket.IO reference

Fresh runtime probes and strict consumer type fixtures were run independently against Socket.IO and socket.io-client 4.7.5 and 4.8.3 before Smocket changed. Both supported version pairs produced the same receiver identity and declared every covered method as returning this.

Observation Socket.IO 4.7.5 and 4.8.3
io.use(middleware) returns the same Server instance
io.of(name).use(middleware) returns that same Namespace instance
server socket.disconnect() returns that same server Socket instance
client socket.connect() before opening returns that same client Socket instance
client socket.connect() while connected returns that same client Socket instance
client socket.disconnect() while connected returns that same client Socket instance
client socket.disconnect() while disconnected returns that same client Socket instance
Public declarations for all five methods this

The two version fixtures also chained Server.use() twice with non-default client-to-server events, server-to-client events, inter-server events, and socket data. Middleware on both calls retained the incoming listener map, outgoing emitter map, and typed socket.data; namespace middleware retained the same four generic positions. Server and client socket assignments confirmed that disconnect and connect return their fully parameterized socket types rather than a broad untyped receiver.

Runtime identity and branches

The implementation returns from the concrete receivers instead of relying on delegated return values. Server.use() still delegates registration to the root namespace but returns the server, while Namespace.use() returns the namespace that stores the middleware. This distinction matters because returning the namespace from the server method would preserve chaining but fail the measured identity.

Client methods close every branch with the receiver. An already-connected connect remains a no-op that returns the client. A disconnected client with a registered namespace starts the same fresh pairing as before and then returns the client. A missing namespace emits the same Invalid namespace result and returns the client, allowing a later registration and retry. Active and inactive disconnect calls likewise preserve their existing teardown or cancellation behavior before returning the client. The missing-origin FailedClientSocket keeps its terminal no-retry behavior while exposing the same fluent shape as an unconnected Socket.IO client.

Server socket disconnect returns only after the existing synchronous peer state transition and asynchronous teardown have been initiated. It does not await teardown, change the close argument behavior, or broaden the connection-wide scope tracked separately by ADR 0028 and issue #254.

Public type surface

ServerContract, NamespaceContract, ServerSocketContract, and ClientSocketContract now express the five returns with polymorphic this, so a concrete Smocket Server or socket subtype is not widened by chaining. Existing calls that discard the result remain valid because TypeScript permits an ignored non-void return.

The structural Socket.IO parity projections use explicit recursive receiver types for these members. TypeScript does not allow a this type in the intersection type aliases used to omit Socket.IO's internal listener fallback declarations, so naming each projection's own parameterized type preserves the same check without weakening it to any. The Ensure guards continue to compile for default and concrete event maps against the installed Socket.IO declarations.

src/event-maps.typecheck.ts exercises all five Smocket returns and their real Socket.IO counterparts. It chains root middleware twice, registers typed namespace middleware, assigns the server socket disconnect result, and assigns both client connect and disconnect results. The callbacks read and write non-default event maps and socket data after each fluent boundary, so a return that lost generic information would fail typecheck.

Tests and documentation

src/emitter-returns.test.ts adds real-first identity cases for server and namespace middleware registration, client connect while connected and reconnecting, client disconnect while connected and already disconnected, and server socket disconnect. Existing namespace coverage now asserts the receiver on a rejected retry before registration and on the successful retry after registration. The missing-origin test asserts both inert client methods, covering the terminal client implementation without changing its intentional divergence.

docs/conformance.md is regenerated from the green dual run, records the four new real-first cases, and broadens the generated area from emitter-only returns to the full return-value surface. The fluent-return entry is removed from docs/differences.md section C because #233 is no longer an accidental fidelity gap. The disconnect(true) gap added by ADR 0028 remains present and is unaffected. Under ADR 0019 this observable correction toward measured Socket.IO behavior lands in the pre-1.0 patch scheduled for v0.4.3.

Locally, the Node dual run passes 454 tests across 50 files, the Chromium mock run passes 227 tests across 25 files, and the published dist run passes 196 tests across 22 files. Every executable line changed in src/mock-server.ts, including invalid-namespace and terminal missing-origin returns, has a non-zero hit count in the local coverage report. TypeScript, ESLint, Prettier, coverage, conformance drift, build, smoke, external-import inspection, publint, and Are the Types Wrong all pass.

Related issue

Closes #233

Checklist

  • Tests added or updated
  • Behavior verified against real socket.io

@github-actions github-actions Bot added bug Something isn't working 📏 m Pull request changes 50–199 lines labels Aug 12, 2026
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@electrohyun, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 36 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: bef632a0-af82-4555-a9cf-317e96ff098b

📥 Commits

Reviewing files that changed from the base of the PR and between 460adbe and 1fe7220.

📒 Files selected for processing (9)
  • docs/conformance.md
  • docs/differences.md
  • scripts/conformance-report.mjs
  • src/connect-url.test.ts
  • src/contract.ts
  • src/emitter-returns.test.ts
  • src/event-maps.typecheck.ts
  • src/mock-server.ts
  • src/namespace.test.ts

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

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Aug 12, 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 08795ce into main Aug 12, 2026
14 checks passed
@electrohyun
electrohyun deleted the fix/match-fluent-returns branch August 12, 2026 08:52
@electrohyun electrohyun self-assigned this Aug 12, 2026
@github-project-automation github-project-automation Bot moved this to Done in smocket Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working 📏 m Pull request changes 50–199 lines

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

fix: match fluent returns for use, connect, and disconnect

1 participant