Idea — Use runtime-provided Socket API, if available #8
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.
A thought — would this have the highest impact if implemented as a polyfill?
Right now, this implementation assumes a Node.js environment, and wraps
node:net
andnode:tls
. It is effectively "an implementation of theconnect()
API that works in Node.js".Library maintainers who need their library to work across runtimes would benefit most from a polyfill, where, if
connect()
is available, it is used directly, and runtime-specific APIs are never imported. That way, maintainers could just pull in this polyfill, and trust that it'd work across both Node.js and other runtimes that already provide the sockets API natively.I can clean this up, and handle moving the Node.js imports to be conditional (to ensure that this can run in non-Node.js environments) — but I first wanted to submit the idea to get on the same page. Could also extend this to have logic that covered checking if you're in a Vercel Edge Functions environment, or any other runtime that makes the sockets available directly.
Thoughts? cc @jasnell — thinking back to the
fetch()
API — being able to pull in a polyfill that'd work across environments seemed at the time to me like it really helped libraries start adopting it, even as it took time to become available everywhere. But maybe there are dragons and downsides I'm not seeing of taking this approach :)