From ba2c323d2c21f31e06cbe708169123e4009009d3 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Wed, 15 Aug 2018 17:14:22 -0700 Subject: [PATCH] streams: move process.binding('stream_wrap') to internalBinding --- benchmark/net/tcp-raw-c2s.js | 114 +++++++++--------- benchmark/net/tcp-raw-pipe.js | 21 ++-- benchmark/net/tcp-raw-s2c.js | 81 +++++++------ lib/internal/bootstrap/node.js | 7 +- lib/internal/child_process.js | 2 +- lib/internal/http2/core.js | 2 +- lib/internal/stream_base_commons.js | 3 +- lib/net.js | 2 +- src/stream_wrap.cc | 4 +- ...ocess-binding-internalbinding-whitelist.js | 1 + test/parallel/test-stream-wrap.js | 6 +- test/parallel/test-tcp-wrap-connect.js | 6 +- test/parallel/test-tcp-wrap-listen.js | 4 +- test/parallel/test-tls-close-notify.js | 4 +- test/pseudo-tty/test-tty-wrap.js | 6 +- test/sequential/test-async-wrap-getasyncid.js | 4 +- 16 files changed, 145 insertions(+), 122 deletions(-) diff --git a/benchmark/net/tcp-raw-c2s.js b/benchmark/net/tcp-raw-c2s.js index 4b6dd9c3f2b145..d6666011d5601d 100644 --- a/benchmark/net/tcp-raw-c2s.js +++ b/benchmark/net/tcp-raw-c2s.js @@ -12,14 +12,15 @@ const bench = common.createBenchmark(main, { len: [102400, 1024 * 1024 * 16], type: ['utf', 'asc', 'buf'], dur: [5] -}); - -const { TCP, constants: TCPConstants } = process.binding('tcp_wrap'); -const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap; -const WriteWrap = process.binding('stream_wrap').WriteWrap; -const PORT = common.PORT; +}, { flags: [ '--expose-internals', '--no-warnings' ] }); function main({ dur, len, type }) { + const { internalBinding } = require('internal/test/binding'); + const { TCP, constants: TCPConstants } = process.binding('tcp_wrap'); + const { TCPConnectWrap } = process.binding('tcp_wrap'); + const { WriteWrap } = internalBinding('stream_wrap'); + const PORT = common.PORT; + const serverHandle = new TCP(TCPConstants.SERVER); var err = serverHandle.bind('127.0.0.1', PORT); if (err) @@ -58,71 +59,70 @@ function main({ dur, len, type }) { }; client(type, len); -} - -function fail(err, syscall) { - throw util._errnoException(err, syscall); -} - -function client(type, len) { - var chunk; - switch (type) { - case 'buf': - chunk = Buffer.alloc(len, 'x'); - break; - case 'utf': - chunk = 'ü'.repeat(len / 2); - break; - case 'asc': - chunk = 'x'.repeat(len); - break; - default: - throw new Error(`invalid type: ${type}`); + function fail(err, syscall) { + throw util._errnoException(err, syscall); } - const clientHandle = new TCP(TCPConstants.SOCKET); - const connectReq = new TCPConnectWrap(); - const err = clientHandle.connect(connectReq, '127.0.0.1', PORT); - - if (err) - fail(err, 'connect'); - - clientHandle.readStart(); - - connectReq.oncomplete = function(err) { - if (err) - fail(err, 'connect'); - - while (clientHandle.writeQueueSize === 0) - write(); - }; - - function write() { - const writeReq = new WriteWrap(); - writeReq.oncomplete = afterWrite; - var err; + function client(type, len) { + var chunk; switch (type) { case 'buf': - err = clientHandle.writeBuffer(writeReq, chunk); + chunk = Buffer.alloc(len, 'x'); break; case 'utf': - err = clientHandle.writeUtf8String(writeReq, chunk); + chunk = 'ü'.repeat(len / 2); break; case 'asc': - err = clientHandle.writeAsciiString(writeReq, chunk); + chunk = 'x'.repeat(len); break; + default: + throw new Error(`invalid type: ${type}`); } - if (err) - fail(err, 'write'); - } + const clientHandle = new TCP(TCPConstants.SOCKET); + const connectReq = new TCPConnectWrap(); + const err = clientHandle.connect(connectReq, '127.0.0.1', PORT); - function afterWrite(err, handle) { if (err) - fail(err, 'write'); + fail(err, 'connect'); + + clientHandle.readStart(); + + connectReq.oncomplete = function(err) { + if (err) + fail(err, 'connect'); - while (clientHandle.writeQueueSize === 0) - write(); + while (clientHandle.writeQueueSize === 0) + write(); + }; + + function write() { + const writeReq = new WriteWrap(); + writeReq.oncomplete = afterWrite; + var err; + switch (type) { + case 'buf': + err = clientHandle.writeBuffer(writeReq, chunk); + break; + case 'utf': + err = clientHandle.writeUtf8String(writeReq, chunk); + break; + case 'asc': + err = clientHandle.writeAsciiString(writeReq, chunk); + break; + } + + if (err) + fail(err, 'write'); + } + + function afterWrite(err, handle) { + if (err) + fail(err, 'write'); + + while (clientHandle.writeQueueSize === 0) + write(); + } } } diff --git a/benchmark/net/tcp-raw-pipe.js b/benchmark/net/tcp-raw-pipe.js index dfde3d40b50b55..8203abca6e92b9 100644 --- a/benchmark/net/tcp-raw-pipe.js +++ b/benchmark/net/tcp-raw-pipe.js @@ -12,18 +12,21 @@ const bench = common.createBenchmark(main, { len: [102400, 1024 * 1024 * 16], type: ['utf', 'asc', 'buf'], dur: [5] +}, { + flags: [ '--expose-internals', '--no-warnings' ] }); -function fail(err, syscall) { - throw util._errnoException(err, syscall); -} - -const { TCP, constants: TCPConstants } = process.binding('tcp_wrap'); -const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap; -const WriteWrap = process.binding('stream_wrap').WriteWrap; -const PORT = common.PORT; - function main({ dur, len, type }) { + const { internalBinding } = require('internal/test/binding'); + const { TCP, constants: TCPConstants } = process.binding('tcp_wrap'); + const { TCPConnectWrap } = process.binding('tcp_wrap'); + const { WriteWrap } = internalBinding('stream_wrap'); + const PORT = common.PORT; + + function fail(err, syscall) { + throw util._errnoException(err, syscall); + } + // Server const serverHandle = new TCP(TCPConstants.SERVER); var err = serverHandle.bind('127.0.0.1', PORT); diff --git a/benchmark/net/tcp-raw-s2c.js b/benchmark/net/tcp-raw-s2c.js index fe0bffd8127ec7..1e42b311ad7b2a 100644 --- a/benchmark/net/tcp-raw-s2c.js +++ b/benchmark/net/tcp-raw-s2c.js @@ -12,14 +12,17 @@ const bench = common.createBenchmark(main, { len: [102400, 1024 * 1024 * 16], type: ['utf', 'asc', 'buf'], dur: [5] +}, { + flags: [ '--expose-internals', '--no-warnings' ] }); -const { TCP, constants: TCPConstants } = process.binding('tcp_wrap'); -const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap; -const WriteWrap = process.binding('stream_wrap').WriteWrap; -const PORT = common.PORT; - function main({ dur, len, type }) { + const { internalBinding } = require('internal/test/binding'); + const { TCP, constants: TCPConstants } = process.binding('tcp_wrap'); + const { TCPConnectWrap } = process.binding('tcp_wrap'); + const { WriteWrap } = internalBinding('stream_wrap'); + const PORT = common.PORT; + const serverHandle = new TCP(TCPConstants.SERVER); var err = serverHandle.bind('127.0.0.1', PORT); if (err) @@ -89,42 +92,42 @@ function main({ dur, len, type }) { }; client(dur); -} -function fail(err, syscall) { - throw util._errnoException(err, syscall); -} + function fail(err, syscall) { + throw util._errnoException(err, syscall); + } -function client(dur) { - const clientHandle = new TCP(TCPConstants.SOCKET); - const connectReq = new TCPConnectWrap(); - const err = clientHandle.connect(connectReq, '127.0.0.1', PORT); - - if (err) - fail(err, 'connect'); - - connectReq.oncomplete = function() { - var bytes = 0; - clientHandle.onread = function(nread, buffer) { - // we're not expecting to ever get an EOF from the client. - // just lots of data forever. - if (nread < 0) - fail(nread, 'read'); - - // don't slice the buffer. the point of this is to isolate, not - // simulate real traffic. - bytes += buffer.length; - }; + function client(dur) { + const clientHandle = new TCP(TCPConstants.SOCKET); + const connectReq = new TCPConnectWrap(); + const err = clientHandle.connect(connectReq, '127.0.0.1', PORT); - clientHandle.readStart(); - - // the meat of the benchmark is right here: - bench.start(); + if (err) + fail(err, 'connect'); - setTimeout(function() { - // report in Gb/sec - bench.end((bytes * 8) / (1024 * 1024 * 1024)); - process.exit(0); - }, dur * 1000); - }; + connectReq.oncomplete = function() { + var bytes = 0; + clientHandle.onread = function(nread, buffer) { + // we're not expecting to ever get an EOF from the client. + // just lots of data forever. + if (nread < 0) + fail(nread, 'read'); + + // don't slice the buffer. the point of this is to isolate, not + // simulate real traffic. + bytes += buffer.length; + }; + + clientHandle.readStart(); + + // the meat of the benchmark is right here: + bench.start(); + + setTimeout(function() { + // report in Gb/sec + bench.end((bytes * 8) / (1024 * 1024 * 1024)); + process.exit(0); + }, dur * 1000); + }; + } } diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 32a62171da98ed..661b1eb3803be6 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -344,7 +344,12 @@ // that are whitelisted for access via process.binding()... this is used // to provide a transition path for modules that are being moved over to // internalBinding. - const internalBindingWhitelist = new SafeSet(['uv', 'http_parser', 'v8']); + const internalBindingWhitelist = + new SafeSet([ + 'uv', + 'http_parser', + 'v8', + 'stream_wrap']); process.binding = function binding(name) { return internalBindingWhitelist.has(name) ? internalBinding(name) : diff --git a/lib/internal/child_process.js b/lib/internal/child_process.js index d212bbeaa7bca2..cf7e23bf7a9248 100644 --- a/lib/internal/child_process.js +++ b/lib/internal/child_process.js @@ -24,7 +24,7 @@ const assert = require('assert'); const { internalBinding } = require('internal/bootstrap/loaders'); const { Process } = process.binding('process_wrap'); -const { WriteWrap } = process.binding('stream_wrap'); +const { WriteWrap } = internalBinding('stream_wrap'); const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap'); const { TTY } = process.binding('tty_wrap'); const { TCP } = process.binding('tcp_wrap'); diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 299949481350e1..c0c452a35f175f 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -115,7 +115,7 @@ const { isArrayBufferView } = require('internal/util/types'); const { FileHandle } = process.binding('fs'); const binding = internalBinding('http2'); -const { ShutdownWrap } = process.binding('stream_wrap'); +const { ShutdownWrap } = internalBinding('stream_wrap'); const { UV_EOF } = internalBinding('uv'); const { StreamPipe } = internalBinding('stream_pipe'); diff --git a/lib/internal/stream_base_commons.js b/lib/internal/stream_base_commons.js index b252b1d8ff7670..9bd2dd90bc119e 100644 --- a/lib/internal/stream_base_commons.js +++ b/lib/internal/stream_base_commons.js @@ -2,7 +2,8 @@ const { Buffer } = require('buffer'); const errors = require('internal/errors'); -const { WriteWrap } = process.binding('stream_wrap'); +const { internalBinding } = require('internal/bootstrap/loaders'); +const { WriteWrap } = internalBinding('stream_wrap'); const errnoException = errors.errnoException; diff --git a/lib/net.js b/lib/net.js index b7b91a71f17659..428675173f3e3e 100644 --- a/lib/net.js +++ b/lib/net.js @@ -43,7 +43,7 @@ const { const { Buffer } = require('buffer'); const TTYWrap = process.binding('tty_wrap'); -const { ShutdownWrap } = process.binding('stream_wrap'); +const { ShutdownWrap } = internalBinding('stream_wrap'); const { TCP, TCPConnectWrap, diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index 60a17545427b16..59f7eb9503f4b0 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -374,5 +374,5 @@ void LibuvStreamWrap::AfterUvWrite(uv_write_t* req, int status) { } // namespace node -NODE_BUILTIN_MODULE_CONTEXT_AWARE(stream_wrap, - node::LibuvStreamWrap::Initialize) +NODE_MODULE_CONTEXT_AWARE_INTERNAL(stream_wrap, + node::LibuvStreamWrap::Initialize) diff --git a/test/parallel/test-process-binding-internalbinding-whitelist.js b/test/parallel/test-process-binding-internalbinding-whitelist.js index e8c3b76214fa7a..9c8cca25035031 100644 --- a/test/parallel/test-process-binding-internalbinding-whitelist.js +++ b/test/parallel/test-process-binding-internalbinding-whitelist.js @@ -9,3 +9,4 @@ const assert = require('assert'); assert(process.binding('uv')); assert(process.binding('http_parser')); assert(process.binding('v8')); +assert(process.binding('stream_wrap')); diff --git a/test/parallel/test-stream-wrap.js b/test/parallel/test-stream-wrap.js index 5312596afac40d..9a279790d8aceb 100644 --- a/test/parallel/test-stream-wrap.js +++ b/test/parallel/test-stream-wrap.js @@ -1,10 +1,12 @@ +// Flags: --expose-internals 'use strict'; const common = require('../common'); const assert = require('assert'); +const { internalBinding } = require('internal/test/binding'); const StreamWrap = require('_stream_wrap'); -const Duplex = require('stream').Duplex; -const ShutdownWrap = process.binding('stream_wrap').ShutdownWrap; +const { Duplex } = require('stream'); +const { ShutdownWrap } = internalBinding('stream_wrap'); function testShutdown(callback) { const stream = new Duplex({ diff --git a/test/parallel/test-tcp-wrap-connect.js b/test/parallel/test-tcp-wrap-connect.js index 36f87d1863ef36..a903307e5b6cca 100644 --- a/test/parallel/test-tcp-wrap-connect.js +++ b/test/parallel/test-tcp-wrap-connect.js @@ -1,9 +1,11 @@ +// Flags: --expose-internals 'use strict'; require('../common'); const assert = require('assert'); +const { internalBinding } = require('internal/test/binding'); const { TCP, constants: TCPConstants } = process.binding('tcp_wrap'); -const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap; -const ShutdownWrap = process.binding('stream_wrap').ShutdownWrap; +const { TCPConnectWrap } = process.binding('tcp_wrap'); +const { ShutdownWrap } = internalBinding('stream_wrap'); function makeConnection() { const client = new TCP(TCPConstants.SOCKET); diff --git a/test/parallel/test-tcp-wrap-listen.js b/test/parallel/test-tcp-wrap-listen.js index 8203a4771b861f..7df5a11ff1861e 100644 --- a/test/parallel/test-tcp-wrap-listen.js +++ b/test/parallel/test-tcp-wrap-listen.js @@ -1,9 +1,11 @@ +// Flags: --expose-internals 'use strict'; const common = require('../common'); const assert = require('assert'); +const { internalBinding } = require('internal/test/binding'); const { TCP, constants: TCPConstants } = process.binding('tcp_wrap'); -const WriteWrap = process.binding('stream_wrap').WriteWrap; +const { WriteWrap } = internalBinding('stream_wrap'); const server = new TCP(TCPConstants.SOCKET); diff --git a/test/parallel/test-tls-close-notify.js b/test/parallel/test-tls-close-notify.js index 75b35c5f3c5b3c..808727b12739b2 100644 --- a/test/parallel/test-tls-close-notify.js +++ b/test/parallel/test-tls-close-notify.js @@ -19,15 +19,17 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. +// Flags: --expose-internals 'use strict'; const common = require('../common'); if (!common.hasCrypto) common.skip('missing crypto'); +const { internalBinding } = require('internal/test/binding'); const tls = require('tls'); const fixtures = require('../common/fixtures'); -const { ShutdownWrap } = process.binding('stream_wrap'); +const { ShutdownWrap } = internalBinding('stream_wrap'); const server = tls.createServer({ key: fixtures.readKey('agent1-key.pem'), diff --git a/test/pseudo-tty/test-tty-wrap.js b/test/pseudo-tty/test-tty-wrap.js index 6212d655d355fc..cb961d04033b6b 100644 --- a/test/pseudo-tty/test-tty-wrap.js +++ b/test/pseudo-tty/test-tty-wrap.js @@ -1,8 +1,10 @@ +// Flags: --expose-internals --no-warnings 'use strict'; require('../common'); -const TTY = process.binding('tty_wrap').TTY; -const WriteWrap = process.binding('stream_wrap').WriteWrap; +const { internalBinding } = require('internal/test/binding'); +const { TTY } = process.binding('tty_wrap'); +const { WriteWrap } = internalBinding('stream_wrap'); const handle = new TTY(1); const req = new WriteWrap(); diff --git a/test/sequential/test-async-wrap-getasyncid.js b/test/sequential/test-async-wrap-getasyncid.js index 9ee32b646cd955..917f84bd821150 100644 --- a/test/sequential/test-async-wrap-getasyncid.js +++ b/test/sequential/test-async-wrap-getasyncid.js @@ -201,12 +201,12 @@ if (common.hasCrypto) { // eslint-disable-line node-core/crypto-check } { - const binding = process.binding('stream_wrap'); + const binding = internalBinding('stream_wrap'); testUninitialized(new binding.WriteWrap(), 'WriteWrap'); } { - const stream_wrap = process.binding('stream_wrap'); + const stream_wrap = internalBinding('stream_wrap'); const tcp_wrap = process.binding('tcp_wrap'); const server = net.createServer(common.mustCall((socket) => { server.close();