mirror of
https://github.com/nodejs/node.git
synced 2024-11-30 07:27:22 +01:00
8d901bb44e
It does not make too much sense to have modules unrelated to TTY load the TTY binding just to use this method. Put this in the util binding instead. PR-URL: https://github.com/nodejs/node/pull/27289 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
82 lines
1.8 KiB
JavaScript
82 lines
1.8 KiB
JavaScript
'use strict';
|
|
const { codes } = require('internal/errors');
|
|
const { UDP } = internalBinding('udp_wrap');
|
|
const { guessHandleType } = internalBinding('util');
|
|
const { isInt32 } = require('internal/validators');
|
|
const { UV_EINVAL } = internalBinding('uv');
|
|
const { ERR_INVALID_ARG_TYPE, ERR_SOCKET_BAD_TYPE } = codes;
|
|
const kStateSymbol = Symbol('state symbol');
|
|
let dns; // Lazy load for startup performance.
|
|
|
|
|
|
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.connect = handle.connect6;
|
|
handle.send = handle.send6;
|
|
return handle;
|
|
}
|
|
|
|
throw new ERR_SOCKET_BAD_TYPE();
|
|
}
|
|
|
|
|
|
function _createSocketHandle(address, port, addressType, fd, flags) {
|
|
const handle = newHandle(addressType);
|
|
let err;
|
|
|
|
if (isInt32(fd) && fd > 0) {
|
|
const type = guessHandleType(fd);
|
|
if (type !== 'UDP') {
|
|
err = UV_EINVAL;
|
|
} else {
|
|
err = handle.open(fd);
|
|
}
|
|
} else if (port || address) {
|
|
err = handle.bind(address, port || 0, flags);
|
|
}
|
|
|
|
if (err) {
|
|
handle.close();
|
|
return err;
|
|
}
|
|
|
|
return handle;
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
kStateSymbol,
|
|
_createSocketHandle,
|
|
newHandle
|
|
};
|