dgram: make _createSocketHandle() internal only

_createSocketHandle() is used internally by the cluster module.
This commit makes it internal only API.

PR-URL: https://github.com/nodejs/node/pull/21923
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Wyatt Preul <wpreul@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
cjihrig 2018-07-21 01:17:21 -04:00
parent 79e41cc7b0
commit 3f6ee75781
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5
4 changed files with 75 additions and 64 deletions

View File

@ -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
@ -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;
@ -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;
@ -739,7 +685,6 @@ Socket.prototype.getSendBufferSize = function() {
module.exports = {
_createSocketHandle,
createSocket,
Socket
};

View File

@ -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;

View File

@ -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 };

View File

@ -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(() => {