diff --git a/lib/dgram.js b/lib/dgram.js index 93c94c71cfaa02..a8de95111f8412 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -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 }; diff --git a/lib/internal/cluster/shared_handle.js b/lib/internal/cluster/shared_handle.js index fe6042142e31f6..0bb8c44f5d630b 100644 --- a/lib/internal/cluster/shared_handle.js +++ b/lib/internal/cluster/shared_handle.js @@ -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; diff --git a/lib/internal/dgram.js b/lib/internal/dgram.js index 0f333178e87378..82b65294c21da5 100644 --- a/lib/internal/dgram.js +++ b/lib/internal/dgram.js @@ -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 }; diff --git a/test/parallel/test-dgram-create-socket-handle.js b/test/parallel/test-dgram-create-socket-handle.js index 57480f7d2cfaa6..e49e3e8dd6c4b4 100644 --- a/test/parallel/test-dgram-create-socket-handle.js +++ b/test/parallel/test-dgram-create-socket-handle.js @@ -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(() => {