Skip to content

Commit 98ef8cf

Browse files
cjihrigtargos
authored andcommitted
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 <apapirovski@mac.com> Reviewed-By: Wyatt Preul <wpreul@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent ae17d18 commit 98ef8cf

4 files changed

Lines changed: 75 additions & 64 deletions

File tree

‎lib/dgram.js‎

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@
2121

2222
'use strict';
2323

24-
constassert=require('assert');
2524
consterrors=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-
letdns;
52-
5352
constBIND_STATE_UNBOUND=0;
5453
constBIND_STATE_BINDING=1;
5554
constBIND_STATE_BOUND=2;
@@ -64,59 +63,6 @@ const errnoException = errors.errnoException;
6463
constexceptionWithHostPort=errors.exceptionWithHostPort;
6564

6665

67-
functionlookup4(lookup,address,callback){
68-
returnlookup(address||'127.0.0.1',4,callback);
69-
}
70-
71-
72-
functionlookup6(lookup,address,callback){
73-
returnlookup(address||'::1',6,callback);
74-
}
75-
76-
77-
functionnewHandle(type,lookup){
78-
if(lookup===undefined){
79-
if(dns===undefined)dns=require('dns');
80-
lookup=dns.lookup;
81-
}elseif(typeoflookup!=='function')
82-
thrownewERR_INVALID_ARG_TYPE('lookup','Function',lookup);
83-
84-
if(type==='udp4'){
85-
consthandle=newUDP();
86-
handle.lookup=lookup4.bind(handle,lookup);
87-
returnhandle;
88-
}
89-
90-
if(type==='udp6'){
91-
consthandle=newUDP();
92-
handle.lookup=lookup6.bind(handle,lookup);
93-
handle.bind=handle.bind6;
94-
handle.send=handle.send6;
95-
returnhandle;
96-
}
97-
98-
thrownewERR_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(typeoffd!=='number'||fd<0);
105-
106-
varhandle=newHandle(addressType);
107-
108-
if(port||address){
109-
varerr=handle.bind(address,port||0,flags);
110-
if(err){
111-
handle.close();
112-
returnerr;
113-
}
114-
}
115-
116-
returnhandle;
117-
}
118-
119-
12066
functionSocket(type,listener){
12167
EventEmitter.call(this);
12268
varlookup;
@@ -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‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
constassert=require('assert');
3-
constdgram=require('dgram');
3+
constdgram=require('internal/dgram');
44
constnet=require('net');
55

66
module.exports=SharedHandle;

‎lib/internal/dgram.js‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,70 @@
11
'use strict';
2+
constassert=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
constkStateSymbol=Symbol('state symbol');
7+
letdns;// Lazy load for startup performance.
38

4-
module.exports={ kStateSymbol };
9+
10+
functionlookup4(lookup,address,callback){
11+
returnlookup(address||'127.0.0.1',4,callback);
12+
}
13+
14+
15+
functionlookup6(lookup,address,callback){
16+
returnlookup(address||'::1',6,callback);
17+
}
18+
19+
20+
functionnewHandle(type,lookup){
21+
if(lookup===undefined){
22+
if(dns===undefined){
23+
dns=require('dns');
24+
}
25+
26+
lookup=dns.lookup;
27+
}elseif(typeoflookup!=='function'){
28+
thrownewERR_INVALID_ARG_TYPE('lookup','Function',lookup);
29+
}
30+
31+
if(type==='udp4'){
32+
consthandle=newUDP();
33+
34+
handle.lookup=lookup4.bind(handle,lookup);
35+
returnhandle;
36+
}
37+
38+
if(type==='udp6'){
39+
consthandle=newUDP();
40+
41+
handle.lookup=lookup6.bind(handle,lookup);
42+
handle.bind=handle.bind6;
43+
handle.send=handle.send6;
44+
returnhandle;
45+
}
46+
47+
thrownewERR_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(typeoffd!=='number'||fd<0);
54+
55+
consthandle=newHandle(addressType);
56+
57+
if(port||address){
58+
consterr=handle.bind(address,port||0,flags);
59+
60+
if(err){
61+
handle.close();
62+
returnerr;
63+
}
64+
}
65+
66+
returnhandle;
67+
}
68+
69+
70+
module.exports={ kStateSymbol, _createSocketHandle, newHandle };

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
// Flags: --expose-internals
12
'use strict';
23
constcommon=require('../common');
34
constassert=require('assert');
4-
constdgram=require('dgram');
5+
const{ _createSocketHandle }=require('internal/dgram');
56
constUDP=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)