Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ae17d18

Browse filesBrowse 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 <apapirovski@mac.com> Reviewed-By: Wyatt Preul <wpreul@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent b5b7438 commit ae17d18
Copy full SHA for ae17d18

File tree

Expand file treeCollapse file tree

10 files changed

+141
-89
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

10 files changed

+141
-89
lines changed
Open diff view settings
Collapse file

‎lib/dgram.js‎

Copy file name to clipboardExpand all lines: lib/dgram.js
+94-66Lines changed: 94 additions & 66 deletions
Large diffs are not rendered by default.
Collapse file

‎lib/internal/child_process.js‎

Copy file name to clipboardExpand all lines: lib/internal/child_process.js
+2-1Lines changed: 2 additions & 1 deletion
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) {
Collapse file

‎lib/internal/dgram.js‎

Copy file name to clipboard
+4Lines changed: 4 additions & 0 deletions
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 };
Collapse file

‎node.gyp‎

Copy file name to clipboardExpand all lines: node.gyp
+1Lines changed: 1 addition & 0 deletions
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',
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-dgram-close-during-bind.js
+6-3Lines changed: 6 additions & 3 deletions
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
}));
Collapse file

‎test/parallel/test-dgram-close.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-dgram-close.js
+3-1Lines changed: 3 additions & 1 deletion
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')
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-dgram-recv-error.js
+4-1Lines changed: 4 additions & 1 deletion
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)));
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-dgram-send-error.js
+4-2Lines changed: 4 additions & 2 deletions
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

Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-handle-wrap-isrefed.js
+18-12Lines changed: 18 additions & 12 deletions
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

Collapse file

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

Copy file name to clipboardExpand all lines: test/sequential/test-dgram-implicit-bind-failure.js
+5-3Lines changed: 5 additions & 3 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.