mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00

PR-URL: https://github.com/nodejs/node/pull/22474 Refs: https://github.com/nodejs/node/issues/22160 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
58 lines
1.5 KiB
JavaScript
58 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
const Buffer = require('buffer').Buffer;
|
|
const { internalBinding } = require('internal/bootstrap/loaders');
|
|
const { isIPv6 } = internalBinding('cares_wrap');
|
|
const { writeBuffer } = process.binding('fs');
|
|
const errors = require('internal/errors');
|
|
|
|
const octet = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
|
|
const re = new RegExp(`^${octet}[.]${octet}[.]${octet}[.]${octet}$`);
|
|
|
|
function isIPv4(s) {
|
|
return re.test(s);
|
|
}
|
|
|
|
function isIP(s) {
|
|
if (isIPv4(s)) return 4;
|
|
if (isIPv6(s)) return 6;
|
|
return 0;
|
|
}
|
|
|
|
// Check that the port number is not NaN when coerced to a number,
|
|
// is an integer and that it falls within the legal range of port numbers.
|
|
function isLegalPort(port) {
|
|
if ((typeof port !== 'number' && typeof port !== 'string') ||
|
|
(typeof port === 'string' && port.trim().length === 0))
|
|
return false;
|
|
return +port === (+port >>> 0) && port <= 0xFFFF;
|
|
}
|
|
|
|
function makeSyncWrite(fd) {
|
|
return function(chunk, enc, cb) {
|
|
if (enc !== 'buffer')
|
|
chunk = Buffer.from(chunk, enc);
|
|
|
|
this._handle.bytesWritten += chunk.length;
|
|
|
|
const ctx = {};
|
|
writeBuffer(fd, chunk, 0, chunk.length, null, undefined, ctx);
|
|
if (ctx.errno !== undefined) {
|
|
const ex = errors.uvException(ctx);
|
|
// Legacy: net writes have .code === .errno, whereas writeBuffer gives the
|
|
// raw errno number in .errno.
|
|
ex.errno = ex.code;
|
|
return cb(ex);
|
|
}
|
|
cb();
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
isIP,
|
|
isIPv4,
|
|
isIPv6,
|
|
isLegalPort,
|
|
makeSyncWrite,
|
|
normalizedArgsSymbol: Symbol('normalizedArgs')
|
|
};
|