net: add experimental net/promises API#63965
Conversation
Add an experimental `net/promises` namespace, accessible via
`require('node:net/promises')` or `net.promises`, mirroring the existing
`fs/promises` and `dns/promises` modules.
It provides promise-based variants of net's one-shot lifecycle
operations:
- `connect()` returns a promise that fulfills with a connected Socket
once the 'connect' event fires, and rejects on connection failure or
when an optional AbortSignal is aborted, destroying the socket.
- `listen()` creates a server, begins listening, and returns a promise
that fulfills with the listening Server once the 'listening' event
fires. It rejects if the server fails to bind or an optional
AbortSignal is aborted, closing the server.
The functions are named for the actions they await, forming a parallel
`connect()`/`listen()` pair. This is intentionally not the callback
API's factory taxonomy: there `createConnection()` is canonical because
it both creates a socket and initiates connecting, whereas
`createServer()` is a pure factory that does not listen. A
`createConnection`/`createServer` pairing would be inconsistent here
because `createServer` has no completion to await, so no
`createConnection` alias is provided.
Refs: nodejs#21482
Assisted-by: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Ethan Arrowood <ethan@arrowood.dev>
|
Review requested:
|
pimterry
left a comment
There was a problem hiding this comment.
One of the existing tests is unhappy, but otherwise this looks great to me ❤️
I'd be OK to ship experimental as-is, one note to consider: normal netServer.listen({ signal }) takes a signal which can be used to abort the server any time, but netPromises.listen({ signal }) takes a signal that can be used to cancel only the initial listen, and does nothing later on. The signal is linked only to the once(server, 'listening'... and is dropped from the server itself.
There's a specific comment confirming this is intentional, but then the docs here slightly disagree... I can see arguments both ways: this approach is more clearly linked to the promise flow, but then it does create an inconsistency between the APIs.
I'm happy either way tbh, just food for thought.
Adds an experimental
net/promisesnamespace that provides promise-based variants ofnet's connection and listening operations, mirroring the existingfs/promisesanddns/promisesmodules. It is accessible viarequire('node:net/promises')orrequire('node:net').promises.Motivation
nethas no promise-based API. To await a connected socket or a listening server today you have to hand-wire the'connect'/'listening'and'error'events into anew Promise, or reach forevents.once. There is long-standing demand for a first-class promise API for these one-shot lifecycle moments (Refs: #21482).netis stream- and event-oriented, so only its one-shot async moments map cleanly to promises (establishing a connection and binding a server). The rest of the api remains the same.What's added
netPromises.connect(options)/connect(path)/connect(port[, host])resolves with a connectednet.Socketonce its'connect'event fires; rejects on connection failure or abort, destroying the socket.netPromises.listen([options])creates a server, begins listening, and resolves with the listeningnet.Serveronce its'listening'event fires; rejects if it fails to bind or is aborted, closing the server. Accepts aconnectionListeneroption and the usualnet.createServer/server.listenoptions.isIP,isIPv4,isIPv6,BlockList,SocketAddress) are re-exported for convenience.Both functions accept an optional
AbortSignaland reject with anAbortErrorwhen it is aborted, consistent withtimers/promisesandreadline/promises.Example:
A note on naming (
connect/listen, notcreateConnection/createServer)The naming here is intentional. This API promisifies actions that complete:
connect()resolves once the connection is established, andlisten()resolves once the server is listening. The function name is the action being awaited, and the two form a parallel pair.That is deliberately not the factory taxonomy of the callback API. There,
createConnection()is the canonical name because it both creates a socket and initiates connecting. Creation and the action are fused, so the factory name also implies the action.createServer(), by contrast, is a pure factory: it does not listen; listening is the separateserver.listen()step. So acreateConnection/createServerpairing would be inconsistent in a promise API.createServerhas no completion to await. Naming the functions for their actions (connect/listen) keeps the pair coherent, and there is no factory counterpart to alias, socreateConnectionis intentionally omitted.Notes
events.once(emitter, name, { signal }), so error and abort handling match the rest of core.Refs: #21482
Assisted-by: Claude Opus 4.8 noreply@anthropic.com