Skip to content

Commit ae17d18

Browse files
cjihrigtargos
authored andcommitted
dgram: hide underscored Socket properties
dgram sockets have a fair number of exposed private properties. This commit hides them all behind a single symbol property. PR-URL: #21923 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Wyatt Preul <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent b5b7438 commit ae17d18

10 files changed

+141
-89
lines changed

lib/dgram.js

+94-66
Large diffs are not rendered by default.

lib/internal/child_process.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const { isUint8Array } = require('internal/util/types');
3232
const spawn_sync = process.binding('spawn_sync');
3333
const { HTTPParser } = process.binding('http_parser');
3434
const { freeParser } = require('_http_common');
35+
const { kStateSymbol } = require('internal/dgram');
3536

3637
const {
3738
UV_EACCES,
@@ -181,7 +182,7 @@ const handleConversion = {
181182
send: function(message, socket, options) {
182183
message.dgramType = socket.type;
183184

184-
return socket._handle;
185+
return socket[kStateSymbol].handle;
185186
},
186187

187188
got: function(message, handle, emit) {

lib/internal/dgram.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
const kStateSymbol = Symbol('state symbol');
3+
4+
module.exports = { kStateSymbol };

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
'lib/internal/crypto/sig.js',
105105
'lib/internal/crypto/util.js',
106106
'lib/internal/constants.js',
107+
'lib/internal/dgram.js',
107108
'lib/internal/dns/promises.js',
108109
'lib/internal/dns/utils.js',
109110
'lib/internal/domexception.js',

test/parallel/test-dgram-close-during-bind.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
// Flags: --expose-internals
12
'use strict';
23
const common = require('../common');
34
const dgram = require('dgram');
5+
const { kStateSymbol } = require('internal/dgram');
46
const socket = dgram.createSocket('udp4');
5-
const lookup = socket._handle.lookup;
7+
const { handle } = socket[kStateSymbol];
8+
const lookup = handle.lookup;
69

710
// Test the scenario where the socket is closed during a bind operation.
8-
socket._handle.bind = common.mustNotCall('bind() should not be called.');
11+
handle.bind = common.mustNotCall('bind() should not be called.');
912

10-
socket._handle.lookup = common.mustCall(function(address, callback) {
13+
handle.lookup = common.mustCall(function(address, callback) {
1114
socket.close(common.mustCall(() => {
1215
lookup.call(this, address, callback);
1316
}));

test/parallel/test-dgram-close.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@
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
// Ensure that if a dgram socket is closed before the DNS lookup completes, it
2425
// won't crash.
2526

2627
const common = require('../common');
2728
const assert = require('assert');
2829
const dgram = require('dgram');
30+
const { kStateSymbol } = require('internal/dgram');
2931

3032
const buf = Buffer.alloc(1024, 42);
3133

3234
let socket = dgram.createSocket('udp4');
33-
const handle = socket._handle;
35+
const { handle } = socket[kStateSymbol];
3436

3537
// get a random port for send
3638
const portGetter = dgram.createSocket('udp4')

test/parallel/test-dgram-recv-error.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
// Flags: --expose-internals
12
'use strict';
23
const common = require('../common');
34
const assert = require('assert');
45
const dgram = require('dgram');
6+
const { kStateSymbol } = require('internal/dgram');
57
const s = dgram.createSocket('udp4');
8+
const { handle } = s[kStateSymbol];
69

710
s.on('error', common.mustCall((err) => {
811
s.close();
@@ -13,4 +16,4 @@ s.on('error', common.mustCall((err) => {
1316
}));
1417

1518
s.on('message', common.mustNotCall('no message should be received.'));
16-
s.bind(common.mustCall(() => s._handle.onmessage(-1, s._handle, null, null)));
19+
s.bind(common.mustCall(() => handle.onmessage(-1, handle, null, null)));

test/parallel/test-dgram-send-error.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
// Flags: --expose-internals
12
'use strict';
23
const common = require('../common');
34
const assert = require('assert');
45
const dgram = require('dgram');
6+
const { kStateSymbol } = require('internal/dgram');
57
const mockError = new Error('mock DNS error');
68

79
function getSocket(callback) {
810
const socket = dgram.createSocket('udp4');
911

1012
socket.on('message', common.mustNotCall('Should not receive any messages.'));
1113
socket.bind(common.mustCall(() => {
12-
socket._handle.lookup = function(address, callback) {
14+
socket[kStateSymbol].handle.lookup = function(address, callback) {
1315
process.nextTick(callback, mockError);
1416
};
1517

@@ -57,7 +59,7 @@ getSocket((socket) => {
5759
);
5860
});
5961

60-
socket._handle.send = function() {
62+
socket[kStateSymbol].handle.send = function() {
6163
return errCode;
6264
};
6365

test/parallel/test-handle-wrap-isrefed.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Flags: --expose-internals
12
'use strict';
23

34
const common = require('../common');
@@ -25,41 +26,46 @@ const strictEqual = require('assert').strictEqual;
2526

2627

2728
const dgram = require('dgram');
29+
const { kStateSymbol } = require('internal/dgram');
2830

2931
// dgram ipv4
3032
{
3133
const sock4 = dgram.createSocket('udp4');
32-
strictEqual(Object.getPrototypeOf(sock4._handle).hasOwnProperty('hasRef'),
34+
const handle = sock4[kStateSymbol].handle;
35+
36+
strictEqual(Object.getPrototypeOf(handle).hasOwnProperty('hasRef'),
3337
true, 'udp_wrap: ipv4: hasRef() missing');
34-
strictEqual(sock4._handle.hasRef(),
38+
strictEqual(handle.hasRef(),
3539
true, 'udp_wrap: ipv4: not initially refed');
3640
sock4.unref();
37-
strictEqual(sock4._handle.hasRef(),
41+
strictEqual(handle.hasRef(),
3842
false, 'udp_wrap: ipv4: unref() ineffective');
3943
sock4.ref();
40-
strictEqual(sock4._handle.hasRef(),
44+
strictEqual(handle.hasRef(),
4145
true, 'udp_wrap: ipv4: ref() ineffective');
42-
sock4._handle.close(common.mustCall(() =>
43-
strictEqual(sock4._handle.hasRef(),
46+
handle.close(common.mustCall(() =>
47+
strictEqual(handle.hasRef(),
4448
false, 'udp_wrap: ipv4: not unrefed on close')));
4549
}
4650

4751

4852
// dgram ipv6
4953
{
5054
const sock6 = dgram.createSocket('udp6');
51-
strictEqual(Object.getPrototypeOf(sock6._handle).hasOwnProperty('hasRef'),
55+
const handle = sock6[kStateSymbol].handle;
56+
57+
strictEqual(Object.getPrototypeOf(handle).hasOwnProperty('hasRef'),
5258
true, 'udp_wrap: ipv6: hasRef() missing');
53-
strictEqual(sock6._handle.hasRef(),
59+
strictEqual(handle.hasRef(),
5460
true, 'udp_wrap: ipv6: not initially refed');
5561
sock6.unref();
56-
strictEqual(sock6._handle.hasRef(),
62+
strictEqual(handle.hasRef(),
5763
false, 'udp_wrap: ipv6: unref() ineffective');
5864
sock6.ref();
59-
strictEqual(sock6._handle.hasRef(),
65+
strictEqual(handle.hasRef(),
6066
true, 'udp_wrap: ipv6: ref() ineffective');
61-
sock6._handle.close(common.mustCall(() =>
62-
strictEqual(sock6._handle.hasRef(),
67+
handle.close(common.mustCall(() =>
68+
strictEqual(handle.hasRef(),
6369
false, 'udp_wrap: ipv6: not unrefed on close')));
6470
}
6571

test/sequential/test-dgram-implicit-bind-failure.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
// Flags: --expose-internals
12
'use strict';
23
const common = require('../common');
34
const assert = require('assert');
45
const dgram = require('dgram');
56
const dns = require('dns');
7+
const { kStateSymbol } = require('internal/dgram');
68

79
// Monkey patch dns.lookup() so that it always fails.
810
dns.lookup = function(address, family, callback) {
@@ -25,8 +27,8 @@ socket.on('error', (err) => {
2527
// should also be two listeners - this function and the dgram internal one
2628
// time error handler.
2729
dnsFailures++;
28-
assert(Array.isArray(socket._queue));
29-
assert.strictEqual(socket._queue.length, 1);
30+
assert(Array.isArray(socket[kStateSymbol].queue));
31+
assert.strictEqual(socket[kStateSymbol].queue.length, 1);
3032
assert.strictEqual(socket.listenerCount('error'), 2);
3133
return;
3234
}
@@ -35,7 +37,7 @@ socket.on('error', (err) => {
3537
// On error, the queue should be destroyed and this function should be
3638
// the only listener.
3739
sendFailures++;
38-
assert.strictEqual(socket._queue, undefined);
40+
assert.strictEqual(socket[kStateSymbol].queue, undefined);
3941
assert.strictEqual(socket.listenerCount('error'), 1);
4042
return;
4143
}

0 commit comments

Comments
 (0)