Skip to content

Commit

Permalink
dgram: make _createSocketHandle() internal only
Browse files Browse the repository at this point in the history
_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>
  • Loading branch information
cjihrig authored and targos committed Jul 31, 2018
1 parent ae17d18 commit 98ef8cf
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 64 deletions.
65 changes: 5 additions & 60 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@

'use strict';

const assert = require('assert');
const errors = require('internal/errors');
const { kStateSymbol } = require('internal/dgram');
const {
kStateSymbol,
_createSocketHandle,
newHandle
} = require('internal/dgram');
const {
ERR_INVALID_ARG_TYPE,
ERR_MISSING_ARGS,
ERR_SOCKET_ALREADY_BOUND,
ERR_SOCKET_BAD_BUFFER_SIZE,
ERR_SOCKET_BAD_PORT,
ERR_SOCKET_BAD_TYPE,
ERR_SOCKET_BUFFER_SIZE,
ERR_SOCKET_CANNOT_SEND,
ERR_SOCKET_DGRAM_NOT_RUNNING
Expand All @@ -47,9 +49,6 @@ const { UV_UDP_REUSEADDR } = process.binding('constants').os;

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

// Lazy load for startup performance.
let dns;

const BIND_STATE_UNBOUND = 0;
const BIND_STATE_BINDING = 1;
const BIND_STATE_BOUND = 2;
Expand All @@ -64,59 +63,6 @@ const errnoException = errors.errnoException;
const exceptionWithHostPort = errors.exceptionWithHostPort;


function lookup4(lookup, address, callback) {
return lookup(address || '127.0.0.1', 4, callback);
}


function lookup6(lookup, address, callback) {
return lookup(address || '::1', 6, callback);
}


function newHandle(type, lookup) {
if (lookup === undefined) {
if (dns === undefined) dns = require('dns');
lookup = dns.lookup;
} else if (typeof lookup !== 'function')
throw new ERR_INVALID_ARG_TYPE('lookup', 'Function', lookup);

if (type === 'udp4') {
const handle = new UDP();
handle.lookup = lookup4.bind(handle, lookup);
return handle;
}

if (type === 'udp6') {
const handle = new UDP();
handle.lookup = lookup6.bind(handle, lookup);
handle.bind = handle.bind6;
handle.send = handle.send6;
return handle;
}

throw new ERR_SOCKET_BAD_TYPE();
}


function _createSocketHandle(address, port, addressType, fd, flags) {
// Opening an existing fd is not supported for UDP handles.
assert(typeof fd !== 'number' || fd < 0);

var handle = newHandle(addressType);

if (port || address) {
var err = handle.bind(address, port || 0, flags);
if (err) {
handle.close();
return err;
}
}

return handle;
}


function Socket(type, listener) {
EventEmitter.call(this);
var lookup;
Expand Down Expand Up @@ -739,7 +685,6 @@ Socket.prototype.getSendBufferSize = function() {


module.exports = {
_createSocketHandle,
createSocket,
Socket
};
2 changes: 1 addition & 1 deletion lib/internal/cluster/shared_handle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
const assert = require('assert');
const dgram = require('dgram');
const dgram = require('internal/dgram');
const net = require('net');

module.exports = SharedHandle;
Expand Down
68 changes: 67 additions & 1 deletion lib/internal/dgram.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,70 @@
'use strict';
const assert = require('assert');
const { codes } = require('internal/errors');
const { UDP } = process.binding('udp_wrap');
const { ERR_INVALID_ARG_TYPE, ERR_SOCKET_BAD_TYPE } = codes;
const kStateSymbol = Symbol('state symbol');
let dns; // Lazy load for startup performance.

module.exports = { kStateSymbol };

function lookup4(lookup, address, callback) {
return lookup(address || '127.0.0.1', 4, callback);
}


function lookup6(lookup, address, callback) {
return lookup(address || '::1', 6, callback);
}


function newHandle(type, lookup) {
if (lookup === undefined) {
if (dns === undefined) {
dns = require('dns');
}

lookup = dns.lookup;
} else if (typeof lookup !== 'function') {
throw new ERR_INVALID_ARG_TYPE('lookup', 'Function', lookup);
}

if (type === 'udp4') {
const handle = new UDP();

handle.lookup = lookup4.bind(handle, lookup);
return handle;
}

if (type === 'udp6') {
const handle = new UDP();

handle.lookup = lookup6.bind(handle, lookup);
handle.bind = handle.bind6;
handle.send = handle.send6;
return handle;
}

throw new ERR_SOCKET_BAD_TYPE();
}


function _createSocketHandle(address, port, addressType, fd, flags) {
// Opening an existing fd is not supported for UDP handles.
assert(typeof fd !== 'number' || fd < 0);

const handle = newHandle(addressType);

if (port || address) {
const err = handle.bind(address, port || 0, flags);

if (err) {
handle.close();
return err;
}
}

return handle;
}


module.exports = { kStateSymbol, _createSocketHandle, newHandle };
4 changes: 2 additions & 2 deletions test/parallel/test-dgram-create-socket-handle.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');
const { _createSocketHandle } = require('internal/dgram');
const UDP = process.binding('udp_wrap').UDP;
const _createSocketHandle = dgram._createSocketHandle;

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

0 comments on commit 98ef8cf

Please sign in to comment.