| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1357,6 +1357,9 @@ being issued by trusted CA (`options.ca`). | |||
| 1357 | 1357 | <!-- YAML | |
| 1358 | 1358 | added: v0.11.3 | |
| 1359 | 1359 | changes: | |
| 1360 | + - version: REPLACEME | ||
| 1361 | + pr-url: https://github.com/nodejs/node/pull/35753 | ||
| 1362 | + description: Added `onread` option. | ||
| 1360 | 1363 | - version: v14.1.0 | |
| 1361 | 1364 | pr-url: https://github.com/nodejs/node/pull/32786 | |
| 1362 | 1365 | description: The `highWaterMark` option is accepted now. | |
@@ -1468,6 +1471,10 @@ changes: | |||
| 1468 | 1471 | [`tls.createSecureContext()`][]. If a `secureContext` is _not_ provided, one | |
| 1469 | 1472 | will be created by passing the entire `options` object to | |
| 1470 | 1473 | `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. | ||
| 1471 | 1478 | * ...: [`tls.createSecureContext()`][] options that are used if the | |
| 1472 | 1479 | `secureContext` option is missing, otherwise they are ignored. | |
| 1473 | 1480 | * ...: Any [`socket.connect()`][] option not already listed. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -502,6 +502,7 @@ function TLSSocket(socket, opts) { | |||
| 502 | 502 | pauseOnCreate: tlsOptions.pauseOnConnect, | |
| 503 | 503 | manualStart: true, | |
| 504 | 504 | highWaterMark: tlsOptions.highWaterMark, | |
| 505 | + onread: !socket ? tlsOptions.onread : null, | ||
| 505 | 506 | }); | |
| 506 | 507 | ||
| 507 | 508 | // Proxy for API compatibility | |
@@ -1617,6 +1618,7 @@ exports.connect = function connect(...args) { | |||
| 1617 | 1618 | enableTrace: options.enableTrace, | |
| 1618 | 1619 | pskCallback: options.pskCallback, | |
| 1619 | 1620 | highWaterMark: options.highWaterMark, | |
| 1621 | + onread: options.onread, | ||
| 1620 | 1622 | }); | |
| 1621 | 1623 | ||
| 1622 | 1624 | // rejectUnauthorized property can be explicitly defined as `undefined` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -307,55 +307,54 @@ function Socket(options) { | |||
| 307 | 307 | if (options.handle) { | |
| 308 | 308 | this._handle = options.handle; // private | |
| 309 | 309 | 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; | ||
| 326 | 313 | ||
| 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'); | ||
| 330 | 326 | ||
| 331 | - err = this._handle.open(fd); | ||
| 327 | + this[async_id_symbol] = this._handle.getAsyncId(); | ||
| 332 | 328 | ||
| 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); | ||
| 337 | 333 | 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; | ||
| 358 | 356 | } | |
| 357 | + this[kBufferCb] = onread.callback; | ||
| 359 | 358 | } | |
| 360 | 359 | ||
| 361 | 360 | // Shut down the socket when we're finished with it. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments