From 20f2a00c882841c7c14c078e458325e8203355ea Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Thu, 8 Oct 2026 11:49:55 +0200 Subject: [PATCH] tls: add tlsSocket.socket, to access the raw stream beneath TLS Signed-off-by: Tim Perry --- doc/api/tls.md | 23 ++++++++ lib/internal/tls/wrap.js | 2 + .../test-tls-socket-underlying-socket.js | 57 +++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 test/parallel/test-tls-socket-underlying-socket.js diff --git a/doc/api/tls.md b/doc/api/tls.md index 7df2ab938606..d0027f8d83df 100644 --- a/doc/api/tls.md +++ b/doc/api/tls.md @@ -1668,6 +1668,27 @@ and their processing can be delayed due to packet loss or reordering. However, smaller fragments add extra TLS framing bytes and CPU overhead, which may decrease overall server throughput. +### `tlsSocket.socket` + + + +* Type: {net.Socket|stream.Duplex|null} + +The underlying socket or stream that this socket reads and writes TLS data +through. This is the `socket` passed to [`new tls.TLSSocket()`][] or +[`tls.connect()`][], or the connection given to a [`tls.Server`][] by its +`'connection'` event. + +This is only `null` for client sockets that manage their own connection, +created by [`tls.connect()`][] with a `port` or `path` rather than a `socket`. +Connection details such as `remoteAddress` are then available directly on this +socket. Server sockets always have an underlying socket. + +This remains available after the socket has been destroyed, for example to +identify the underlying connection from a [`'tlsClientError'`][] listener. + ## `tls.checkServerIdentity(hostname, cert)`