Skip to content

Commit 884b23d

Browse files
committed
stream: move process.binding('stream_wrap') to internalBinding
PR-URL: #22345 Refs: #22160 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 7108893 commit 884b23d

16 files changed

+145
-122
lines changed

benchmark/net/tcp-raw-c2s.js

+57-57
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ const bench = common.createBenchmark(main, {
1212
len: [102400, 1024 * 1024 * 16],
1313
type: ['utf', 'asc', 'buf'],
1414
dur: [5]
15-
});
16-
17-
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
18-
const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
19-
const WriteWrap = process.binding('stream_wrap').WriteWrap;
20-
const PORT = common.PORT;
15+
}, { flags: [ '--expose-internals', '--no-warnings' ] });
2116

2217
function main({ dur, len, type }) {
18+
const { internalBinding } = require('internal/test/binding');
19+
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
20+
const { TCPConnectWrap } = process.binding('tcp_wrap');
21+
const { WriteWrap } = internalBinding('stream_wrap');
22+
const PORT = common.PORT;
23+
2324
const serverHandle = new TCP(TCPConstants.SERVER);
2425
var err = serverHandle.bind('127.0.0.1', PORT);
2526
if (err)
@@ -58,71 +59,70 @@ function main({ dur, len, type }) {
5859
};
5960

6061
client(type, len);
61-
}
62-
6362

64-
function fail(err, syscall) {
65-
throw util._errnoException(err, syscall);
66-
}
67-
68-
function client(type, len) {
69-
var chunk;
70-
switch (type) {
71-
case 'buf':
72-
chunk = Buffer.alloc(len, 'x');
73-
break;
74-
case 'utf':
75-
chunk = 'ü'.repeat(len / 2);
76-
break;
77-
case 'asc':
78-
chunk = 'x'.repeat(len);
79-
break;
80-
default:
81-
throw new Error(`invalid type: ${type}`);
63+
function fail(err, syscall) {
64+
throw util._errnoException(err, syscall);
8265
}
8366

84-
const clientHandle = new TCP(TCPConstants.SOCKET);
85-
const connectReq = new TCPConnectWrap();
86-
const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);
87-
88-
if (err)
89-
fail(err, 'connect');
90-
91-
clientHandle.readStart();
92-
93-
connectReq.oncomplete = function(err) {
94-
if (err)
95-
fail(err, 'connect');
96-
97-
while (clientHandle.writeQueueSize === 0)
98-
write();
99-
};
100-
101-
function write() {
102-
const writeReq = new WriteWrap();
103-
writeReq.oncomplete = afterWrite;
104-
var err;
67+
function client(type, len) {
68+
var chunk;
10569
switch (type) {
10670
case 'buf':
107-
err = clientHandle.writeBuffer(writeReq, chunk);
71+
chunk = Buffer.alloc(len, 'x');
10872
break;
10973
case 'utf':
110-
err = clientHandle.writeUtf8String(writeReq, chunk);
74+
chunk = 'ü'.repeat(len / 2);
11175
break;
11276
case 'asc':
113-
err = clientHandle.writeAsciiString(writeReq, chunk);
77+
chunk = 'x'.repeat(len);
11478
break;
79+
default:
80+
throw new Error(`invalid type: ${type}`);
11581
}
11682

117-
if (err)
118-
fail(err, 'write');
119-
}
83+
const clientHandle = new TCP(TCPConstants.SOCKET);
84+
const connectReq = new TCPConnectWrap();
85+
const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);
12086

121-
function afterWrite(err, handle) {
12287
if (err)
123-
fail(err, 'write');
88+
fail(err, 'connect');
89+
90+
clientHandle.readStart();
91+
92+
connectReq.oncomplete = function(err) {
93+
if (err)
94+
fail(err, 'connect');
12495

125-
while (clientHandle.writeQueueSize === 0)
126-
write();
96+
while (clientHandle.writeQueueSize === 0)
97+
write();
98+
};
99+
100+
function write() {
101+
const writeReq = new WriteWrap();
102+
writeReq.oncomplete = afterWrite;
103+
var err;
104+
switch (type) {
105+
case 'buf':
106+
err = clientHandle.writeBuffer(writeReq, chunk);
107+
break;
108+
case 'utf':
109+
err = clientHandle.writeUtf8String(writeReq, chunk);
110+
break;
111+
case 'asc':
112+
err = clientHandle.writeAsciiString(writeReq, chunk);
113+
break;
114+
}
115+
116+
if (err)
117+
fail(err, 'write');
118+
}
119+
120+
function afterWrite(err, handle) {
121+
if (err)
122+
fail(err, 'write');
123+
124+
while (clientHandle.writeQueueSize === 0)
125+
write();
126+
}
127127
}
128128
}

benchmark/net/tcp-raw-pipe.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@ const bench = common.createBenchmark(main, {
1212
len: [102400, 1024 * 1024 * 16],
1313
type: ['utf', 'asc', 'buf'],
1414
dur: [5]
15+
}, {
16+
flags: [ '--expose-internals', '--no-warnings' ]
1517
});
1618

17-
function fail(err, syscall) {
18-
throw util._errnoException(err, syscall);
19-
}
20-
21-
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
22-
const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
23-
const WriteWrap = process.binding('stream_wrap').WriteWrap;
24-
const PORT = common.PORT;
25-
2619
function main({ dur, len, type }) {
20+
const { internalBinding } = require('internal/test/binding');
21+
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
22+
const { TCPConnectWrap } = process.binding('tcp_wrap');
23+
const { WriteWrap } = internalBinding('stream_wrap');
24+
const PORT = common.PORT;
25+
26+
function fail(err, syscall) {
27+
throw util._errnoException(err, syscall);
28+
}
29+
2730
// Server
2831
const serverHandle = new TCP(TCPConstants.SERVER);
2932
var err = serverHandle.bind('127.0.0.1', PORT);

benchmark/net/tcp-raw-s2c.js

+42-39
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ const bench = common.createBenchmark(main, {
1212
len: [102400, 1024 * 1024 * 16],
1313
type: ['utf', 'asc', 'buf'],
1414
dur: [5]
15+
}, {
16+
flags: [ '--expose-internals', '--no-warnings' ]
1517
});
1618

17-
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
18-
const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
19-
const WriteWrap = process.binding('stream_wrap').WriteWrap;
20-
const PORT = common.PORT;
21-
2219
function main({ dur, len, type }) {
20+
const { internalBinding } = require('internal/test/binding');
21+
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
22+
const { TCPConnectWrap } = process.binding('tcp_wrap');
23+
const { WriteWrap } = internalBinding('stream_wrap');
24+
const PORT = common.PORT;
25+
2326
const serverHandle = new TCP(TCPConstants.SERVER);
2427
var err = serverHandle.bind('127.0.0.1', PORT);
2528
if (err)
@@ -89,42 +92,42 @@ function main({ dur, len, type }) {
8992
};
9093

9194
client(dur);
92-
}
9395

94-
function fail(err, syscall) {
95-
throw util._errnoException(err, syscall);
96-
}
96+
function fail(err, syscall) {
97+
throw util._errnoException(err, syscall);
98+
}
9799

98-
function client(dur) {
99-
const clientHandle = new TCP(TCPConstants.SOCKET);
100-
const connectReq = new TCPConnectWrap();
101-
const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);
102-
103-
if (err)
104-
fail(err, 'connect');
105-
106-
connectReq.oncomplete = function() {
107-
var bytes = 0;
108-
clientHandle.onread = function(nread, buffer) {
109-
// we're not expecting to ever get an EOF from the client.
110-
// just lots of data forever.
111-
if (nread < 0)
112-
fail(nread, 'read');
113-
114-
// don't slice the buffer. the point of this is to isolate, not
115-
// simulate real traffic.
116-
bytes += buffer.length;
117-
};
100+
function client(dur) {
101+
const clientHandle = new TCP(TCPConstants.SOCKET);
102+
const connectReq = new TCPConnectWrap();
103+
const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);
118104

119-
clientHandle.readStart();
120-
121-
// the meat of the benchmark is right here:
122-
bench.start();
105+
if (err)
106+
fail(err, 'connect');
123107

124-
setTimeout(function() {
125-
// report in Gb/sec
126-
bench.end((bytes * 8) / (1024 * 1024 * 1024));
127-
process.exit(0);
128-
}, dur * 1000);
129-
};
108+
connectReq.oncomplete = function() {
109+
var bytes = 0;
110+
clientHandle.onread = function(nread, buffer) {
111+
// we're not expecting to ever get an EOF from the client.
112+
// just lots of data forever.
113+
if (nread < 0)
114+
fail(nread, 'read');
115+
116+
// don't slice the buffer. the point of this is to isolate, not
117+
// simulate real traffic.
118+
bytes += buffer.length;
119+
};
120+
121+
clientHandle.readStart();
122+
123+
// the meat of the benchmark is right here:
124+
bench.start();
125+
126+
setTimeout(function() {
127+
// report in Gb/sec
128+
bench.end((bytes * 8) / (1024 * 1024 * 1024));
129+
process.exit(0);
130+
}, dur * 1000);
131+
};
132+
}
130133
}

lib/internal/bootstrap/node.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,12 @@
344344
// that are whitelisted for access via process.binding()... this is used
345345
// to provide a transition path for modules that are being moved over to
346346
// internalBinding.
347-
const internalBindingWhitelist = new SafeSet(['uv', 'http_parser', 'v8']);
347+
const internalBindingWhitelist =
348+
new SafeSet([
349+
'uv',
350+
'http_parser',
351+
'v8',
352+
'stream_wrap']);
348353
process.binding = function binding(name) {
349354
return internalBindingWhitelist.has(name) ?
350355
internalBinding(name) :

lib/internal/child_process.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const assert = require('assert');
2424
const { internalBinding } = require('internal/bootstrap/loaders');
2525

2626
const { Process } = process.binding('process_wrap');
27-
const { WriteWrap } = process.binding('stream_wrap');
27+
const { WriteWrap } = internalBinding('stream_wrap');
2828
const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap');
2929
const { TTY } = process.binding('tty_wrap');
3030
const { TCP } = process.binding('tcp_wrap');

lib/internal/http2/core.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const { isArrayBufferView } = require('internal/util/types');
115115

116116
const { FileHandle } = process.binding('fs');
117117
const binding = internalBinding('http2');
118-
const { ShutdownWrap } = process.binding('stream_wrap');
118+
const { ShutdownWrap } = internalBinding('stream_wrap');
119119
const { UV_EOF } = internalBinding('uv');
120120

121121
const { StreamPipe } = internalBinding('stream_pipe');

lib/internal/stream_base_commons.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const { Buffer } = require('buffer');
44
const errors = require('internal/errors');
5-
const { WriteWrap } = process.binding('stream_wrap');
5+
const { internalBinding } = require('internal/bootstrap/loaders');
6+
const { WriteWrap } = internalBinding('stream_wrap');
67

78
const errnoException = errors.errnoException;
89

lib/net.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const {
4343

4444
const { Buffer } = require('buffer');
4545
const TTYWrap = process.binding('tty_wrap');
46-
const { ShutdownWrap } = process.binding('stream_wrap');
46+
const { ShutdownWrap } = internalBinding('stream_wrap');
4747
const {
4848
TCP,
4949
TCPConnectWrap,

src/stream_wrap.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -374,5 +374,5 @@ void LibuvStreamWrap::AfterUvWrite(uv_write_t* req, int status) {
374374

375375
} // namespace node
376376

377-
NODE_BUILTIN_MODULE_CONTEXT_AWARE(stream_wrap,
378-
node::LibuvStreamWrap::Initialize)
377+
NODE_MODULE_CONTEXT_AWARE_INTERNAL(stream_wrap,
378+
node::LibuvStreamWrap::Initialize)

test/parallel/test-process-binding-internalbinding-whitelist.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ const assert = require('assert');
99
assert(process.binding('uv'));
1010
assert(process.binding('http_parser'));
1111
assert(process.binding('v8'));
12+
assert(process.binding('stream_wrap'));

test/parallel/test-stream-wrap.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
// Flags: --expose-internals
12
'use strict';
23
const common = require('../common');
34
const assert = require('assert');
45

6+
const { internalBinding } = require('internal/test/binding');
57
const StreamWrap = require('_stream_wrap');
6-
const Duplex = require('stream').Duplex;
7-
const ShutdownWrap = process.binding('stream_wrap').ShutdownWrap;
8+
const { Duplex } = require('stream');
9+
const { ShutdownWrap } = internalBinding('stream_wrap');
810

911
function testShutdown(callback) {
1012
const stream = new Duplex({

test/parallel/test-tcp-wrap-connect.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// Flags: --expose-internals
12
'use strict';
23
require('../common');
34
const assert = require('assert');
5+
const { internalBinding } = require('internal/test/binding');
46
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
5-
const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
6-
const ShutdownWrap = process.binding('stream_wrap').ShutdownWrap;
7+
const { TCPConnectWrap } = process.binding('tcp_wrap');
8+
const { ShutdownWrap } = internalBinding('stream_wrap');
79

810
function makeConnection() {
911
const client = new TCP(TCPConstants.SOCKET);

test/parallel/test-tcp-wrap-listen.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// Flags: --expose-internals
12
'use strict';
23
const common = require('../common');
34
const assert = require('assert');
45

6+
const { internalBinding } = require('internal/test/binding');
57
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
6-
const WriteWrap = process.binding('stream_wrap').WriteWrap;
8+
const { WriteWrap } = internalBinding('stream_wrap');
79

810
const server = new TCP(TCPConstants.SOCKET);
911

test/parallel/test-tls-close-notify.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
// Flags: --expose-internals
2223
'use strict';
2324
const common = require('../common');
2425

2526
if (!common.hasCrypto)
2627
common.skip('missing crypto');
2728

29+
const { internalBinding } = require('internal/test/binding');
2830
const tls = require('tls');
2931
const fixtures = require('../common/fixtures');
30-
const { ShutdownWrap } = process.binding('stream_wrap');
32+
const { ShutdownWrap } = internalBinding('stream_wrap');
3133

3234
const server = tls.createServer({
3335
key: fixtures.readKey('agent1-key.pem'),

0 commit comments

Comments
 (0)