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

feat: add net.Socket-based interceptor #375

Closed
wants to merge 4 commits into from

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented May 7, 2023

Before continuing, read this #399

A proof of concept of a Socket-based interceptor.

Things I learned

  • net.createConnection() accepts a lookup function that we can use to resolve non-existing hosts to whatever we want. It won't automatically give us a successful connection (because that's done by socket._handler.connect()) but it will prevent DNS lookup issues.
  • net.creaetConnection() accepts a custom handler class instance. It's an internal API responsible for provisioning the connection to a resolved address, reading/writing socket chunks, and terminating the socket.

Links

Roadmap

  • Socket interceptor is sensitive to import order. If you import ./socket.impl after you import http, it stops working.
  • Should establish the actual socket connection instead of force-emitting connect and ready events. This will improve the underlying code's integrity and allow us to replay the connection errors (very similar to what we already do in ClientRequestInterceptor).
  • Support SMTP and other protocols (currently getting Unsupported fd type: UNKNOWN from createHandle).
  • Wrap this whole thing in the Interceptor (class SocketInterceptor extends Interceptor).

@kettanaito
Copy link
Member Author

Since this implementation is specific to the import order, I couldn't create the interceptor out of it. The interceptor is applied by calling interceptor.apply() in the beforeAll() test hook. At that point, the test has already imported http, which renders the current interceptor obsolete (again, import order sensitivity).

@kettanaito kettanaito changed the title feat: basic implementation of socket interceptor feat: add net.Socket-based interceptor May 7, 2023
@kettanaito
Copy link
Member Author

Closing in favor of #515.

@kettanaito kettanaito closed this Apr 15, 2024
@kettanaito kettanaito deleted the feat/socket-interceptor branch April 15, 2024 21:31
@kettanaito
Copy link
Member Author

Released: v0.32.0 🎉

This has been released in v0.32.0!

Make sure to always update to the latest version (npm i @mswjs/interceptors@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

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.

1 participant