2016-01-26 14:12:41 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-30 15:55:29 +00:00
|
|
|
const {
|
2020-01-06 02:48:14 +00:00
|
|
|
RegExp,
|
2020-11-17 23:19:12 +00:00
|
|
|
RegExpPrototypeTest,
|
2019-11-30 15:55:29 +00:00
|
|
|
Symbol,
|
|
|
|
} = primordials;
|
|
|
|
|
2017-12-30 23:27:56 +00:00
|
|
|
const Buffer = require('buffer').Buffer;
|
2018-08-23 14:29:40 +00:00
|
|
|
const { writeBuffer } = internalBinding('fs');
|
2024-04-23 17:05:38 +00:00
|
|
|
const {
|
|
|
|
UVException,
|
|
|
|
} = require('internal/errors');
|
2017-12-30 23:27:56 +00:00
|
|
|
|
2018-09-03 12:28:31 +00:00
|
|
|
// IPv4 Segment
|
2023-09-13 20:01:36 +00:00
|
|
|
const v4Seg = '(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])';
|
|
|
|
const v4Str = `(?:${v4Seg}\\.){3}${v4Seg}`;
|
2018-09-03 12:28:31 +00:00
|
|
|
const IPv4Reg = new RegExp(`^${v4Str}$`);
|
|
|
|
|
|
|
|
// IPv6 Segment
|
|
|
|
const v6Seg = '(?:[0-9a-fA-F]{1,4})';
|
2023-09-13 20:01:36 +00:00
|
|
|
const IPv6Reg = new RegExp('^(?:' +
|
2018-09-03 12:28:31 +00:00
|
|
|
`(?:${v6Seg}:){7}(?:${v6Seg}|:)|` +
|
|
|
|
`(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` +
|
2023-09-13 20:01:36 +00:00
|
|
|
`(?:${v6Seg}:){5}(?::${v4Str}|(?::${v6Seg}){1,2}|:)|` +
|
|
|
|
`(?:${v6Seg}:){4}(?:(?::${v6Seg}){0,1}:${v4Str}|(?::${v6Seg}){1,3}|:)|` +
|
|
|
|
`(?:${v6Seg}:){3}(?:(?::${v6Seg}){0,2}:${v4Str}|(?::${v6Seg}){1,4}|:)|` +
|
|
|
|
`(?:${v6Seg}:){2}(?:(?::${v6Seg}){0,3}:${v4Str}|(?::${v6Seg}){1,5}|:)|` +
|
|
|
|
`(?:${v6Seg}:){1}(?:(?::${v6Seg}){0,4}:${v4Str}|(?::${v6Seg}){1,6}|:)|` +
|
|
|
|
`(?::(?:(?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` +
|
|
|
|
')(?:%[0-9a-zA-Z-.:]{1,})?$');
|
2018-01-26 17:39:10 +00:00
|
|
|
|
|
|
|
function isIPv4(s) {
|
2022-06-17 17:12:42 +00:00
|
|
|
// TODO(aduh95): Replace RegExpPrototypeTest with RegExpPrototypeExec when it
|
|
|
|
// no longer creates a perf regression in the dns benchmark.
|
|
|
|
// eslint-disable-next-line node-core/avoid-prototype-pollution
|
2020-11-17 23:19:12 +00:00
|
|
|
return RegExpPrototypeTest(IPv4Reg, s);
|
2018-09-03 12:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isIPv6(s) {
|
2022-06-17 17:12:42 +00:00
|
|
|
// TODO(aduh95): Replace RegExpPrototypeTest with RegExpPrototypeExec when it
|
|
|
|
// no longer creates a perf regression in the dns benchmark.
|
|
|
|
// eslint-disable-next-line node-core/avoid-prototype-pollution
|
2020-11-17 23:19:12 +00:00
|
|
|
return RegExpPrototypeTest(IPv6Reg, s);
|
2018-01-26 17:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isIP(s) {
|
|
|
|
if (isIPv4(s)) return 4;
|
|
|
|
if (isIPv6(s)) return 6;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-30 23:27:56 +00:00
|
|
|
function makeSyncWrite(fd) {
|
|
|
|
return function(chunk, enc, cb) {
|
|
|
|
if (enc !== 'buffer')
|
|
|
|
chunk = Buffer.from(chunk, enc);
|
|
|
|
|
2018-03-17 16:52:57 +00:00
|
|
|
this._handle.bytesWritten += chunk.length;
|
2017-12-30 23:27:56 +00:00
|
|
|
|
2018-03-03 07:43:14 +00:00
|
|
|
const ctx = {};
|
|
|
|
writeBuffer(fd, chunk, 0, chunk.length, null, undefined, ctx);
|
|
|
|
if (ctx.errno !== undefined) {
|
2024-04-23 17:05:38 +00:00
|
|
|
const ex = new UVException(ctx);
|
2019-06-09 13:04:51 +00:00
|
|
|
ex.errno = ctx.errno;
|
2017-12-30 23:27:56 +00:00
|
|
|
return cb(ex);
|
|
|
|
}
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-15 22:29:00 +00:00
|
|
|
module.exports = {
|
2023-02-23 09:55:04 +00:00
|
|
|
kReinitializeHandle: Symbol('kReinitializeHandle'),
|
2018-01-26 17:39:10 +00:00
|
|
|
isIP,
|
|
|
|
isIPv4,
|
|
|
|
isIPv6,
|
2017-12-30 23:27:56 +00:00
|
|
|
makeSyncWrite,
|
2023-02-26 10:34:02 +00:00
|
|
|
normalizedArgsSymbol: Symbol('normalizedArgs'),
|
2017-02-15 22:29:00 +00:00
|
|
|
};
|