Files
node/lib/internal/net/promises.js
T
Ethan Arrowood 6d8baeabb0 net: add experimental net/promises API
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: https://github.com/nodejs/node/issues/21482
Assisted-by: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Ethan Arrowood <ethan@arrowood.dev>
PR-URL: https://github.com/nodejs/node/pull/63965
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-07-28 19:29:48 +00:00

89 lines
3.0 KiB
JavaScript

'use strict';
const { once } = require('events');
const {
validateAbortSignal,
validateObject,
} = require('internal/validators');
const { kEmptyObject } = require('internal/util');
// Lazily loaded to avoid a require cycle with the `net` module, which exposes
// this namespace through its `promises` getter.
let net;
function lazyNet() {
net ??= require('net');
return net;
}
// Resolves with a connected `net.Socket` once the `'connect'` event fires, and
// rejects if the connection fails or the optional `signal` is aborted.
async function connect(...args) {
const lazy = lazyNet();
const options = lazy._normalizeArgs(args)[0];
const { signal } = options;
if (signal !== undefined) {
validateAbortSignal(signal, 'options.signal');
signal.throwIfAborted();
}
// Strip the signal so the socket does not also install its own abort
// handling; rejecting and destroying below fully tears the socket down.
const socket = lazy.connect({ ...options, signal: undefined });
try {
await once(socket, 'connect', signal !== undefined ? { signal } : kEmptyObject);
} catch (err) {
socket.destroy();
throw err;
}
return socket;
}
// Creates a server and resolves with it once it is listening, rejecting if it
// fails to bind or the optional `signal` is aborted.
async function listen(options = kEmptyObject) {
validateObject(options, 'options');
const { signal, connectionListener } = options;
if (signal !== undefined) {
validateAbortSignal(signal, 'options.signal');
signal.throwIfAborted();
}
const lazy = lazyNet();
const server = lazy.createServer(options, connectionListener);
try {
// Default to an ephemeral port when no listen target is supplied, matching
// `server.listen()` with no arguments; passing a target-less options object
// (the `{}` default, or e.g. `{ signal }`) straight to listen() would throw
// ERR_INVALID_ARG_VALUE. `signal` is passed through so net installs its own
// close-on-abort handler: the signal aborts the server for its entire
// lifetime, not just the pending listen.
const hasListenTarget = options.port !== undefined ||
options.path !== undefined ||
options.fd !== undefined ||
options.handle !== undefined;
server.listen(hasListenTarget ? options : { ...options, port: 0 });
await once(server, 'listening', signal !== undefined ? { signal } : kEmptyObject);
} catch (err) {
// On abort, net's signal handler already closes the server, so closing
// again would be redundant; on other failures (e.g. a bind error) there
// is no such handler, so close it here.
if (!signal?.aborted) {
server.close();
}
throw err;
}
return server;
}
module.exports = {
connect,
listen,
get isIP() { return lazyNet().isIP; },
get isIPv4() { return lazyNet().isIPv4; },
get isIPv6() { return lazyNet().isIPv6; },
get BlockList() { return lazyNet().BlockList; },
get SocketAddress() { return lazyNet().SocketAddress; },
};