Skip to content

Commit f6c7978

Browse files
puzpuzpuztargos
authored andcommitted
tls: allow reading data into a static buffer
Refs: #25436 PR-URL: #35753 Refs: #25436 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent 9c5912c commit f6c7978

File tree

6 files changed

+362
-44
lines changed

6 files changed

+362
-44
lines changed
File renamed without changes.

benchmark/tls/throughput-s2c.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const bench = common.createBenchmark(main, {
4+
dur: [5],
5+
type: ['buf', 'asc', 'utf'],
6+
sendchunklen: [256, 32 * 1024, 128 * 1024, 16 * 1024 * 1024],
7+
recvbuflen: [0, 64 * 1024, 1024 * 1024],
8+
recvbufgenfn: ['true', 'false']
9+
});
10+
11+
const fixtures = require('../../test/common/fixtures');
12+
let options;
13+
let recvbuf;
14+
let received = 0;
15+
const tls = require('tls');
16+
17+
function main({ dur, type, sendchunklen, recvbuflen, recvbufgenfn }) {
18+
if (isFinite(recvbuflen) && recvbuflen > 0)
19+
recvbuf = Buffer.alloc(recvbuflen);
20+
21+
let encoding;
22+
let chunk;
23+
switch (type) {
24+
case 'buf':
25+
chunk = Buffer.alloc(sendchunklen, 'b');
26+
break;
27+
case 'asc':
28+
chunk = 'a'.repeat(sendchunklen);
29+
encoding = 'ascii';
30+
break;
31+
case 'utf':
32+
chunk = 'ü'.repeat(sendchunklen / 2);
33+
encoding = 'utf8';
34+
break;
35+
default:
36+
throw new Error('invalid type');
37+
}
38+
39+
options = {
40+
key: fixtures.readKey('rsa_private.pem'),
41+
cert: fixtures.readKey('rsa_cert.crt'),
42+
ca: fixtures.readKey('rsa_ca.crt'),
43+
ciphers: 'AES256-GCM-SHA384'
44+
};
45+
46+
let socketOpts;
47+
if (recvbuf === undefined) {
48+
socketOpts = { port: common.PORT, rejectUnauthorized: false };
49+
} else {
50+
let buffer = recvbuf;
51+
if (recvbufgenfn === 'true') {
52+
let bufidx = -1;
53+
const bufpool = [
54+
recvbuf,
55+
Buffer.from(recvbuf),
56+
Buffer.from(recvbuf),
57+
];
58+
buffer = () => {
59+
bufidx = (bufidx + 1) % bufpool.length;
60+
return bufpool[bufidx];
61+
};
62+
}
63+
socketOpts = {
64+
port: common.PORT,
65+
rejectUnauthorized: false,
66+
onread: {
67+
buffer,
68+
callback: function(nread, buf) {
69+
received += nread;
70+
}
71+
}
72+
};
73+
}
74+
75+
const server = tls.createServer(options, (socket) => {
76+
socket.on('data', (buf) => {
77+
socket.on('drain', write);
78+
write();
79+
});
80+
81+
function write() {
82+
while (false !== socket.write(chunk, encoding));
83+
}
84+
});
85+
86+
let conn;
87+
server.listen(common.PORT, () => {
88+
conn = tls.connect(socketOpts, () => {
89+
setTimeout(done, dur * 1000);
90+
bench.start();
91+
conn.write('hello');
92+
});
93+
94+
conn.on('data', (chunk) => {
95+
received += chunk.length;
96+
});
97+
});
98+
99+
function done() {
100+
const mbits = (received * 8) / (1024 * 1024);
101+
bench.end(mbits);
102+
process.exit(0);
103+
}
104+
}

doc/api/tls.md

+7
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,9 @@ being issued by trusted CA (`options.ca`).
13571357
<!-- YAML
13581358
added: v0.11.3
13591359
changes:
1360+
- version: REPLACEME
1361+
pr-url: https://github.com/nodejs/node/pull/35753
1362+
description: Added `onread` option.
13601363
- version: v14.1.0
13611364
pr-url: https://github.com/nodejs/node/pull/32786
13621365
description: The `highWaterMark` option is accepted now.
@@ -1468,6 +1471,10 @@ changes:
14681471
[`tls.createSecureContext()`][]. If a `secureContext` is _not_ provided, one
14691472
will be created by passing the entire `options` object to
14701473
`tls.createSecureContext()`.
1474+
* `onread` {Object} If the `socket` option is missing, incoming data is
1475+
stored in a single `buffer` and passed to the supplied `callback` when
1476+
data arrives on the socket, otherwise the option is ignored. See the
1477+
`onread` option of [`net.Socket`][] for details.
14711478
* ...: [`tls.createSecureContext()`][] options that are used if the
14721479
`secureContext` option is missing, otherwise they are ignored.
14731480
* ...: Any [`socket.connect()`][] option not already listed.

lib/_tls_wrap.js

+2
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ function TLSSocket(socket, opts) {
502502
pauseOnCreate: tlsOptions.pauseOnConnect,
503503
manualStart: true,
504504
highWaterMark: tlsOptions.highWaterMark,
505+
onread: !socket ? tlsOptions.onread : null,
505506
});
506507

507508
// Proxy for API compatibility
@@ -1609,6 +1610,7 @@ exports.connect = function connect(...args) {
16091610
enableTrace: options.enableTrace,
16101611
pskCallback: options.pskCallback,
16111612
highWaterMark: options.highWaterMark,
1613+
onread: options.onread,
16121614
});
16131615

16141616
tlssock[kConnectOptions] = options;

lib/net.js

+43-44
Original file line numberDiff line numberDiff line change
@@ -307,55 +307,54 @@ function Socket(options) {
307307
if (options.handle) {
308308
this._handle = options.handle; // private
309309
this[async_id_symbol] = getNewAsyncId(this._handle);
310-
} else {
311-
const onread = options.onread;
312-
if (onread !== null && typeof onread === 'object' &&
313-
(isUint8Array(onread.buffer) || typeof onread.buffer === 'function') &&
314-
typeof onread.callback === 'function') {
315-
if (typeof onread.buffer === 'function') {
316-
this[kBuffer] = true;
317-
this[kBufferGen] = onread.buffer;
318-
} else {
319-
this[kBuffer] = onread.buffer;
320-
}
321-
this[kBufferCb] = onread.callback;
322-
}
323-
if (options.fd !== undefined) {
324-
const { fd } = options;
325-
let err;
310+
} else if (options.fd !== undefined) {
311+
const { fd } = options;
312+
let err;
326313

327-
// createHandle will throw ERR_INVALID_FD_TYPE if `fd` is not
328-
// a valid `PIPE` or `TCP` descriptor
329-
this._handle = createHandle(fd, false);
314+
// createHandle will throw ERR_INVALID_FD_TYPE if `fd` is not
315+
// a valid `PIPE` or `TCP` descriptor
316+
this._handle = createHandle(fd, false);
317+
318+
err = this._handle.open(fd);
319+
320+
// While difficult to fabricate, in some architectures
321+
// `open` may return an error code for valid file descriptors
322+
// which cannot be opened. This is difficult to test as most
323+
// un-openable fds will throw on `createHandle`
324+
if (err)
325+
throw errnoException(err, 'open');
330326

331-
err = this._handle.open(fd);
327+
this[async_id_symbol] = this._handle.getAsyncId();
332328

333-
// While difficult to fabricate, in some architectures
334-
// `open` may return an error code for valid file descriptors
335-
// which cannot be opened. This is difficult to test as most
336-
// un-openable fds will throw on `createHandle`
329+
if ((fd === 1 || fd === 2) &&
330+
(this._handle instanceof Pipe) && isWindows) {
331+
// Make stdout and stderr blocking on Windows
332+
err = this._handle.setBlocking(true);
337333
if (err)
338-
throw errnoException(err, 'open');
339-
340-
this[async_id_symbol] = this._handle.getAsyncId();
341-
342-
if ((fd === 1 || fd === 2) &&
343-
(this._handle instanceof Pipe) && isWindows) {
344-
// Make stdout and stderr blocking on Windows
345-
err = this._handle.setBlocking(true);
346-
if (err)
347-
throw errnoException(err, 'setBlocking');
348-
349-
this._writev = null;
350-
this._write = makeSyncWrite(fd);
351-
// makeSyncWrite adjusts this value like the original handle would, so
352-
// we need to let it do that by turning it into a writable, own
353-
// property.
354-
ObjectDefineProperty(this._handle, 'bytesWritten', {
355-
value: 0, writable: true
356-
});
357-
}
334+
throw errnoException(err, 'setBlocking');
335+
336+
this._writev = null;
337+
this._write = makeSyncWrite(fd);
338+
// makeSyncWrite adjusts this value like the original handle would, so
339+
// we need to let it do that by turning it into a writable, own
340+
// property.
341+
ObjectDefineProperty(this._handle, 'bytesWritten', {
342+
value: 0, writable: true
343+
});
344+
}
345+
}
346+
347+
const onread = options.onread;
348+
if (onread !== null && typeof onread === 'object' &&
349+
(isUint8Array(onread.buffer) || typeof onread.buffer === 'function') &&
350+
typeof onread.callback === 'function') {
351+
if (typeof onread.buffer === 'function') {
352+
this[kBuffer] = true;
353+
this[kBufferGen] = onread.buffer;
354+
} else {
355+
this[kBuffer] = onread.buffer;
358356
}
357+
this[kBufferCb] = onread.callback;
359358
}
360359

361360
// Shut down the socket when we're finished with it.

0 commit comments

Comments
 (0)