Skip to content

Commit 98ef8cf

Browse files
cjihrigtargos
authored andcommittedJul 31, 2018
dgram: make _createSocketHandle() internal only
_createSocketHandle() is used internally by the cluster module. This commit makes it internal only API. PR-URL: #21923 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Wyatt Preul <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent ae17d18 commit 98ef8cf

File tree

4 files changed

+75
-64
lines changed

4 files changed

+75
-64
lines changed
 

‎lib/dgram.js

+5-60
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@
2121

2222
'use strict';
2323

24-
const assert = require('assert');
2524
const errors = require('internal/errors');
26-
const { kStateSymbol } = require('internal/dgram');
25+
const {
26+
kStateSymbol,
27+
_createSocketHandle,
28+
newHandle
29+
} = require('internal/dgram');
2730
const {
2831
ERR_INVALID_ARG_TYPE,
2932
ERR_MISSING_ARGS,
3033
ERR_SOCKET_ALREADY_BOUND,
3134
ERR_SOCKET_BAD_BUFFER_SIZE,
3235
ERR_SOCKET_BAD_PORT,
33-
ERR_SOCKET_BAD_TYPE,
3436
ERR_SOCKET_BUFFER_SIZE,
3537
ERR_SOCKET_CANNOT_SEND,
3638
ERR_SOCKET_DGRAM_NOT_RUNNING
@@ -47,9 +49,6 @@ const { UV_UDP_REUSEADDR } = process.binding('constants').os;
4749

4850
const { UDP, SendWrap } = process.binding('udp_wrap');
4951

50-
// Lazy load for startup performance.
51-
let dns;
52-
5352
const BIND_STATE_UNBOUND = 0;
5453
const BIND_STATE_BINDING = 1;
5554
const BIND_STATE_BOUND = 2;
@@ -64,59 +63,6 @@ const errnoException = errors.errnoException;
6463
const exceptionWithHostPort = errors.exceptionWithHostPort;
6564

6665

67-
function lookup4(lookup, address, callback) {
68-
return lookup(address || '127.0.0.1', 4, callback);
69-
}
70-
71-
72-
function lookup6(lookup, address, callback) {
73-
return lookup(address || '::1', 6, callback);
74-
}
75-
76-
77-
function newHandle(type, lookup) {
78-
if (lookup === undefined) {
79-
if (dns === undefined) dns = require('dns');
80-
lookup = dns.lookup;
81-
} else if (typeof lookup !== 'function')
82-
throw new ERR_INVALID_ARG_TYPE('lookup', 'Function', lookup);
83-
84-
if (type === 'udp4') {
85-
const handle = new UDP();
86-
handle.lookup = lookup4.bind(handle, lookup);
87-
return handle;
88-
}
89-
90-
if (type === 'udp6') {
91-
const handle = new UDP();
92-
handle.lookup = lookup6.bind(handle, lookup);
93-
handle.bind = handle.bind6;
94-
handle.send = handle.send6;
95-
return handle;
96-
}
97-
98-
throw new ERR_SOCKET_BAD_TYPE();
99-
}
100-
101-
102-
function _createSocketHandle(address, port, addressType, fd, flags) {
103-
// Opening an existing fd is not supported for UDP handles.
104-
assert(typeof fd !== 'number' || fd < 0);
105-
106-
var handle = newHandle(addressType);
107-
108-
if (port || address) {
109-
var err = handle.bind(address, port || 0, flags);
110-
if (err) {
111-
handle.close();
112-
return err;
113-
}
114-
}
115-
116-
return handle;
117-
}
118-
119-
12066
function Socket(type, listener) {
12167
EventEmitter.call(this);
12268
var lookup;
@@ -739,7 +685,6 @@ Socket.prototype.getSendBufferSize = function() {
739685

740686

741687
module.exports = {
742-
_createSocketHandle,
743688
createSocket,
744689
Socket
745690
};

‎lib/internal/cluster/shared_handle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
const assert = require('assert');
3-
const dgram = require('dgram');
3+
const dgram = require('internal/dgram');
44
const net = require('net');
55

66
module.exports = SharedHandle;

‎lib/internal/dgram.js

+67-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,70 @@
11
'use strict';
2+
const assert = require('assert');
3+
const { codes } = require('internal/errors');
4+
const { UDP } = process.binding('udp_wrap');
5+
const { ERR_INVALID_ARG_TYPE, ERR_SOCKET_BAD_TYPE } = codes;
26
const kStateSymbol = Symbol('state symbol');
7+
let dns; // Lazy load for startup performance.
38

4-
module.exports = { kStateSymbol };
9+
10+
function lookup4(lookup, address, callback) {
11+
return lookup(address || '127.0.0.1', 4, callback);
12+
}
13+
14+
15+
function lookup6(lookup, address, callback) {
16+
return lookup(address || '::1', 6, callback);
17+
}
18+
19+
20+
function newHandle(type, lookup) {
21+
if (lookup === undefined) {
22+
if (dns === undefined) {
23+
dns = require('dns');
24+
}
25+
26+
lookup = dns.lookup;
27+
} else if (typeof lookup !== 'function') {
28+
throw new ERR_INVALID_ARG_TYPE('lookup', 'Function', lookup);
29+
}
30+
31+
if (type === 'udp4') {
32+
const handle = new UDP();
33+
34+
handle.lookup = lookup4.bind(handle, lookup);
35+
return handle;
36+
}
37+
38+
if (type === 'udp6') {
39+
const handle = new UDP();
40+
41+
handle.lookup = lookup6.bind(handle, lookup);
42+
handle.bind = handle.bind6;
43+
handle.send = handle.send6;
44+
return handle;
45+
}
46+
47+
throw new ERR_SOCKET_BAD_TYPE();
48+
}
49+
50+
51+
function _createSocketHandle(address, port, addressType, fd, flags) {
52+
// Opening an existing fd is not supported for UDP handles.
53+
assert(typeof fd !== 'number' || fd < 0);
54+
55+
const handle = newHandle(addressType);
56+
57+
if (port || address) {
58+
const err = handle.bind(address, port || 0, flags);
59+
60+
if (err) {
61+
handle.close();
62+
return err;
63+
}
64+
}
65+
66+
return handle;
67+
}
68+
69+
70+
module.exports = { kStateSymbol, _createSocketHandle, newHandle };

‎test/parallel/test-dgram-create-socket-handle.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
// Flags: --expose-internals
12
'use strict';
23
const common = require('../common');
34
const assert = require('assert');
4-
const dgram = require('dgram');
5+
const { _createSocketHandle } = require('internal/dgram');
56
const UDP = process.binding('udp_wrap').UDP;
6-
const _createSocketHandle = dgram._createSocketHandle;
77

88
// Throws if an "existing fd" is passed in.
99
common.expectsError(() => {

0 commit comments

Comments
 (0)
Please sign in to comment.