Skip to content

Commit 1a84005

Browse files
addaleaxMylesBorins
authored andcommitted
doc,test: mention Duplex support for TLS
Document and test the existing support for generic Duplex streams in the TLS module. PR-URL: #17599 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent bc7dc65 commit 1a84005

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

doc/api/tls.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,10 @@ changes:
455455
description: ALPN options are supported now.
456456
-->
457457

458-
* `socket` {net.Socket} An instance of [`net.Socket`][]
458+
* `socket` {net.Socket|stream.Duplex}
459+
On the server side, any `Duplex` stream. On the client side, any
460+
instance of [`net.Socket`][] (for generic `Duplex` stream support
461+
on the client side, [`tls.connect()`][] must be used).
459462
* `options` {Object}
460463
* `isServer`: The SSL/TLS protocol is asymmetrical, TLSSockets must know if
461464
they are to behave as a server or a client. If `true` the TLS socket will be
@@ -815,10 +818,12 @@ changes:
815818
* `port` {number} Port the client should connect to.
816819
* `path` {string} Creates unix socket connection to path. If this option is
817820
specified, `host` and `port` are ignored.
818-
* `socket` {net.Socket} Establish secure connection on a given socket rather
819-
than creating a new socket. If this option is specified, `path`, `host` and
820-
`port` are ignored. Usually, a socket is already connected when passed to
821-
`tls.connect()`, but it can be connected later. Note that
821+
* `socket` {stream.Duplex} Establish secure connection on a given socket
822+
rather than creating a new socket. Typically, this is an instance of
823+
[`net.Socket`][], but any `Duplex` stream is allowed.
824+
If this option is specified, `path`, `host` and `port` are ignored,
825+
except for certificate validation. Usually, a socket is already connected
826+
when passed to `tls.connect()`, but it can be connected later. Note that
822827
connection/disconnection/destruction of `socket` is the user's
823828
responsibility, calling `tls.connect()` will not cause `net.connect()` to be
824829
called.
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
const common = require('../common');
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
6+
const fixtures = require('../common/fixtures');
7+
const makeDuplexPair = require('../common/duplexpair');
8+
const assert = require('assert');
9+
const { TLSSocket, connect } = require('tls');
10+
11+
const key = fixtures.readKey('agent1-key.pem');
12+
const cert = fixtures.readKey('agent1-cert.pem');
13+
const ca = fixtures.readKey('ca1-cert.pem');
14+
15+
const { clientSide, serverSide } = makeDuplexPair();
16+
17+
const clientTLS = connect({
18+
socket: clientSide,
19+
ca,
20+
host: 'agent1' // Hostname from certificate
21+
});
22+
const serverTLS = new TLSSocket(serverSide, {
23+
isServer: true,
24+
key,
25+
cert,
26+
ca
27+
});
28+
29+
assert.strictEqual(clientTLS.connecting, false);
30+
assert.strictEqual(serverTLS.connecting, false);
31+
32+
clientTLS.on('secureConnect', common.mustCall(() => {
33+
clientTLS.write('foobar', common.mustCall(() => {
34+
assert.strictEqual(serverTLS.read().toString(), 'foobar');
35+
assert.strictEqual(clientTLS._handle.writeQueueSize, 0);
36+
}));
37+
assert.ok(clientTLS._handle.writeQueueSize > 0);
38+
}));

tools/doc/type-parser.js

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const typeMap = {
4949
'Stream': 'stream.html#stream_stream',
5050
'stream.Readable': 'stream.html#stream_class_stream_readable',
5151
'stream.Writable': 'stream.html#stream_class_stream_writable',
52+
'stream.Duplex': 'stream.html#stream_class_stream_duplex',
5253

5354
'tls.TLSSocket': 'tls.html#tls_class_tls_tlssocket',
5455

0 commit comments

Comments
 (0)