From 52cbe89f7ff400448d09040bf3b0ec47f70fe70d Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 18 Nov 2020 00:19:12 +0100 Subject: [PATCH] net: refactor to use more primordials PR-URL: https://github.com/nodejs/node/pull/36303 Reviewed-By: Rich Trott Reviewed-By: James M Snell --- lib/internal/net.js | 5 +++-- lib/net.js | 26 ++++++++++++++++---------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/internal/net.js b/lib/internal/net.js index 2df1d5d5f613d2..8ae3170228dc32 100644 --- a/lib/internal/net.js +++ b/lib/internal/net.js @@ -2,6 +2,7 @@ const { RegExp, + RegExpPrototypeTest, Symbol, } = primordials; @@ -28,11 +29,11 @@ const IPv6Reg = new RegExp('^(' + ')(%[0-9a-zA-Z-.:]{1,})?$'); function isIPv4(s) { - return IPv4Reg.test(s); + return RegExpPrototypeTest(IPv4Reg, s); } function isIPv6(s) { - return IPv6Reg.test(s); + return RegExpPrototypeTest(IPv6Reg, s); } function isIP(s) { diff --git a/lib/net.js b/lib/net.js index 48e97ffd713951..639395bcf162a8 100644 --- a/lib/net.js +++ b/lib/net.js @@ -23,13 +23,19 @@ const { ArrayIsArray, + ArrayPrototypeIndexOf, + ArrayPrototypePush, + ArrayPrototypeSplice, Boolean, Error, + FunctionPrototype, + FunctionPrototypeCall, Number, NumberIsNaN, NumberParseInt, ObjectDefineProperty, ObjectSetPrototypeOf, + ReflectApply, Symbol, } = primordials; @@ -123,7 +129,7 @@ const DEFAULT_IPV6_ADDR = '::'; const isWindows = process.platform === 'win32'; -function noop() {} +const noop = FunctionPrototype; function getFlags(ipv6Only) { return ipv6Only === true ? TCPConstants.UV_TCP_IPV6ONLY : 0; @@ -298,7 +304,7 @@ function Socket(options) { options.autoDestroy = false; // Handle strings directly. options.decodeStrings = false; - stream.Duplex.call(this, options); + ReflectApply(stream.Duplex, this, [options]); // Default to *not* allowing half open sockets. this.allowHalfOpen = Boolean(allowHalfOpen); @@ -588,7 +594,7 @@ Socket.prototype._read = function(n) { Socket.prototype.end = function(data, encoding, callback) { - stream.Duplex.prototype.end.call(this, data, encoding, callback); + ReflectApply(stream.Duplex.prototype.end, this, [data, encoding, callback]); DTRACE_NET_STREAM_END(this); return this; }; @@ -604,7 +610,7 @@ Socket.prototype.pause = function() { this.destroy(errnoException(err, 'read')); } } - return stream.Duplex.prototype.pause.call(this); + return FunctionPrototypeCall(stream.Duplex.prototype.pause, this); }; @@ -613,7 +619,7 @@ Socket.prototype.resume = function() { !this._handle.reading) { tryReadStart(this); } - return stream.Duplex.prototype.resume.call(this); + return FunctionPrototypeCall(stream.Duplex.prototype.resume, this); }; @@ -622,7 +628,7 @@ Socket.prototype.read = function(n) { !this._handle.reading) { tryReadStart(this); } - return stream.Duplex.prototype.read.call(this, n); + return ReflectApply(stream.Duplex.prototype.read, this, [n]); }; @@ -1161,7 +1167,7 @@ function Server(options, connectionListener) { if (!(this instanceof Server)) return new Server(options, connectionListener); - EventEmitter.call(this); + FunctionPrototypeCall(EventEmitter, this); if (typeof options === 'function') { connectionListener = options; @@ -1687,10 +1693,10 @@ ObjectDefineProperty(Socket.prototype, '_handle', { Server.prototype._setupWorker = function(socketList) { this._usingWorkers = true; - this._workers.push(socketList); + ArrayPrototypePush(this._workers, socketList); socketList.once('exit', (socketList) => { - const index = this._workers.indexOf(socketList); - this._workers.splice(index, 1); + const index = ArrayPrototypeIndexOf(this._workers, socketList); + ArrayPrototypeSplice(this._workers, index, 1); }); };