1'use strict'; 2 3const { 4 RegExp, 5 Symbol, 6} = primordials; 7 8const Buffer = require('buffer').Buffer; 9const { writeBuffer } = internalBinding('fs'); 10const errors = require('internal/errors'); 11 12// IPv4 Segment 13const v4Seg = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'; 14const v4Str = `(${v4Seg}[.]){3}${v4Seg}`; 15const IPv4Reg = new RegExp(`^${v4Str}$`); 16 17// IPv6 Segment 18const v6Seg = '(?:[0-9a-fA-F]{1,4})'; 19const IPv6Reg = new RegExp('^(' + 20 `(?:${v6Seg}:){7}(?:${v6Seg}|:)|` + 21 `(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` + 22 `(?:${v6Seg}:){5}(?::${v4Str}|(:${v6Seg}){1,2}|:)|` + 23 `(?:${v6Seg}:){4}(?:(:${v6Seg}){0,1}:${v4Str}|(:${v6Seg}){1,3}|:)|` + 24 `(?:${v6Seg}:){3}(?:(:${v6Seg}){0,2}:${v4Str}|(:${v6Seg}){1,4}|:)|` + 25 `(?:${v6Seg}:){2}(?:(:${v6Seg}){0,3}:${v4Str}|(:${v6Seg}){1,5}|:)|` + 26 `(?:${v6Seg}:){1}(?:(:${v6Seg}){0,4}:${v4Str}|(:${v6Seg}){1,6}|:)|` + 27 `(?::((?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` + 28')(%[0-9a-zA-Z-.:]{1,})?$'); 29 30function isIPv4(s) { 31 return IPv4Reg.test(s); 32} 33 34function isIPv6(s) { 35 return IPv6Reg.test(s); 36} 37 38function isIP(s) { 39 if (isIPv4(s)) return 4; 40 if (isIPv6(s)) return 6; 41 return 0; 42} 43 44function makeSyncWrite(fd) { 45 return function(chunk, enc, cb) { 46 if (enc !== 'buffer') 47 chunk = Buffer.from(chunk, enc); 48 49 this._handle.bytesWritten += chunk.length; 50 51 const ctx = {}; 52 writeBuffer(fd, chunk, 0, chunk.length, null, undefined, ctx); 53 if (ctx.errno !== undefined) { 54 const ex = errors.uvException(ctx); 55 // Legacy: net writes have .code === .errno, whereas writeBuffer gives the 56 // raw errno number in .errno. 57 ex.errno = ex.code; 58 return cb(ex); 59 } 60 cb(); 61 }; 62} 63 64module.exports = { 65 isIP, 66 isIPv4, 67 isIPv6, 68 makeSyncWrite, 69 normalizedArgsSymbol: Symbol('normalizedArgs') 70}; 71