Skip to content

Commit 52cbe89

Browse files
aduh95targos
authored andcommitted
net: refactor to use more primordials
PR-URL: #36303 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 5daeac6 commit 52cbe89

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

lib/internal/net.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
RegExp,
5+
RegExpPrototypeTest,
56
Symbol,
67
} = primordials;
78

@@ -28,11 +29,11 @@ const IPv6Reg = new RegExp('^(' +
2829
')(%[0-9a-zA-Z-.:]{1,})?$');
2930

3031
function isIPv4(s) {
31-
return IPv4Reg.test(s);
32+
return RegExpPrototypeTest(IPv4Reg, s);
3233
}
3334

3435
function isIPv6(s) {
35-
return IPv6Reg.test(s);
36+
return RegExpPrototypeTest(IPv6Reg, s);
3637
}
3738

3839
function isIP(s) {

lib/net.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@
2323

2424
const {
2525
ArrayIsArray,
26+
ArrayPrototypeIndexOf,
27+
ArrayPrototypePush,
28+
ArrayPrototypeSplice,
2629
Boolean,
2730
Error,
31+
FunctionPrototype,
32+
FunctionPrototypeCall,
2833
Number,
2934
NumberIsNaN,
3035
NumberParseInt,
3136
ObjectDefineProperty,
3237
ObjectSetPrototypeOf,
38+
ReflectApply,
3339
Symbol,
3440
} = primordials;
3541

@@ -123,7 +129,7 @@ const DEFAULT_IPV6_ADDR = '::';
123129

124130
const isWindows = process.platform === 'win32';
125131

126-
function noop() {}
132+
const noop = FunctionPrototype;
127133

128134
function getFlags(ipv6Only) {
129135
return ipv6Only === true ? TCPConstants.UV_TCP_IPV6ONLY : 0;
@@ -298,7 +304,7 @@ function Socket(options) {
298304
options.autoDestroy = false;
299305
// Handle strings directly.
300306
options.decodeStrings = false;
301-
stream.Duplex.call(this, options);
307+
ReflectApply(stream.Duplex, this, [options]);
302308

303309
// Default to *not* allowing half open sockets.
304310
this.allowHalfOpen = Boolean(allowHalfOpen);
@@ -588,7 +594,7 @@ Socket.prototype._read = function(n) {
588594

589595

590596
Socket.prototype.end = function(data, encoding, callback) {
591-
stream.Duplex.prototype.end.call(this, data, encoding, callback);
597+
ReflectApply(stream.Duplex.prototype.end, this, [data, encoding, callback]);
592598
DTRACE_NET_STREAM_END(this);
593599
return this;
594600
};
@@ -604,7 +610,7 @@ Socket.prototype.pause = function() {
604610
this.destroy(errnoException(err, 'read'));
605611
}
606612
}
607-
return stream.Duplex.prototype.pause.call(this);
613+
return FunctionPrototypeCall(stream.Duplex.prototype.pause, this);
608614
};
609615

610616

@@ -613,7 +619,7 @@ Socket.prototype.resume = function() {
613619
!this._handle.reading) {
614620
tryReadStart(this);
615621
}
616-
return stream.Duplex.prototype.resume.call(this);
622+
return FunctionPrototypeCall(stream.Duplex.prototype.resume, this);
617623
};
618624

619625

@@ -622,7 +628,7 @@ Socket.prototype.read = function(n) {
622628
!this._handle.reading) {
623629
tryReadStart(this);
624630
}
625-
return stream.Duplex.prototype.read.call(this, n);
631+
return ReflectApply(stream.Duplex.prototype.read, this, [n]);
626632
};
627633

628634

@@ -1161,7 +1167,7 @@ function Server(options, connectionListener) {
11611167
if (!(this instanceof Server))
11621168
return new Server(options, connectionListener);
11631169

1164-
EventEmitter.call(this);
1170+
FunctionPrototypeCall(EventEmitter, this);
11651171

11661172
if (typeof options === 'function') {
11671173
connectionListener = options;
@@ -1687,10 +1693,10 @@ ObjectDefineProperty(Socket.prototype, '_handle', {
16871693

16881694
Server.prototype._setupWorker = function(socketList) {
16891695
this._usingWorkers = true;
1690-
this._workers.push(socketList);
1696+
ArrayPrototypePush(this._workers, socketList);
16911697
socketList.once('exit', (socketList) => {
1692-
const index = this._workers.indexOf(socketList);
1693-
this._workers.splice(index, 1);
1698+
const index = ArrayPrototypeIndexOf(this._workers, socketList);
1699+
ArrayPrototypeSplice(this._workers, index, 1);
16941700
});
16951701
};
16961702

0 commit comments

Comments
 (0)