net: remove usage of require('util')

Use `require('internal/util/inspect').inspect`,
`require('internal/util/debuglog').debuglog`,
`require('internal/util').deprecate` and `Object.setPrototypeOf` instead
of `require('util')`.
Fix test in `test/parallel/test-net-access-byteswritten.js` to do not
check the `super_` property that was set when using
`require('util').inherits`.

Refs: https://github.com/nodejs/node/issues/26546
Refs: https://github.com/nodejs/node/pull/26896

PR-URL: https://github.com/nodejs/node/pull/26920
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
dnlup 2019-03-26 12:28:25 +01:00 committed by Michaël Zasso
parent e54f237afe
commit f0b3855a90
No known key found for this signature in database
GPG Key ID: 770F7A9A5AE15600
2 changed files with 16 additions and 14 deletions

View File

@ -23,8 +23,9 @@
const EventEmitter = require('events'); const EventEmitter = require('events');
const stream = require('stream'); const stream = require('stream');
const util = require('util'); const { inspect } = require('internal/util/inspect');
const internalUtil = require('internal/util'); const debug = require('internal/util/debuglog').debuglog('net');
const { deprecate } = require('internal/util');
const { const {
isIP, isIP,
isIPv4, isIPv4,
@ -130,8 +131,6 @@ function getNewAsyncId(handle) {
} }
const debug = util.debuglog('net');
function isPipeName(s) { function isPipeName(s) {
return typeof s === 'string' && toNumber(s) === false; return typeof s === 'string' && toNumber(s) === false;
} }
@ -335,7 +334,8 @@ function Socket(options) {
this[kBytesRead] = 0; this[kBytesRead] = 0;
this[kBytesWritten] = 0; this[kBytesWritten] = 0;
} }
util.inherits(Socket, stream.Duplex); Object.setPrototypeOf(Socket.prototype, stream.Duplex.prototype);
Object.setPrototypeOf(Socket, stream.Duplex);
// Refresh existing timeouts. // Refresh existing timeouts.
Socket.prototype._unrefTimer = function _unrefTimer() { Socket.prototype._unrefTimer = function _unrefTimer() {
@ -1094,7 +1094,7 @@ function Server(options, connectionListener) {
this._connections = 0; this._connections = 0;
Object.defineProperty(this, 'connections', { Object.defineProperty(this, 'connections', {
get: internalUtil.deprecate(() => { get: deprecate(() => {
if (this._usingWorkers) { if (this._usingWorkers) {
return null; return null;
@ -1102,9 +1102,9 @@ function Server(options, connectionListener) {
return this._connections; return this._connections;
}, 'Server.connections property is deprecated. ' + }, 'Server.connections property is deprecated. ' +
'Use Server.getConnections method instead.', 'DEP0020'), 'Use Server.getConnections method instead.', 'DEP0020'),
set: internalUtil.deprecate((val) => (this._connections = val), set: deprecate((val) => (this._connections = val),
'Server.connections property is deprecated.', 'Server.connections property is deprecated.',
'DEP0020'), 'DEP0020'),
configurable: true, enumerable: false configurable: true, enumerable: false
}); });
@ -1406,7 +1406,7 @@ Server.prototype.listen = function(...args) {
'must have the property "port" or "path"'); 'must have the property "port" or "path"');
} }
throw new ERR_INVALID_OPT_VALUE('options', util.inspect(options)); throw new ERR_INVALID_OPT_VALUE('options', inspect(options));
}; };
function lookupAndListen(self, port, address, backlog, exclusive, flags) { function lookupAndListen(self, port, address, backlog, exclusive, flags) {
@ -1590,10 +1590,10 @@ Object.defineProperty(Socket.prototype, '_handle', {
}); });
Server.prototype.listenFD = internalUtil.deprecate(function(fd, type) { Server.prototype.listenFD = deprecate(function(fd, type) {
return this.listen({ fd: fd }); return this.listen({ fd: fd });
}, 'Server.listenFD is deprecated. Use Server.listen({fd: <number>}) instead.', }, 'Server.listenFD is deprecated. Use Server.listen({fd: <number>}) instead.',
'DEP0021'); 'DEP0021');
Server.prototype._setupWorker = function(socketList) { Server.prototype._setupWorker = function(socketList) {
this._usingWorkers = true; this._usingWorkers = true;

View File

@ -12,8 +12,10 @@ const tty = require('tty');
// Check that the bytesWritten getter doesn't crash if object isn't // Check that the bytesWritten getter doesn't crash if object isn't
// constructed. // constructed.
assert.strictEqual(net.Socket.prototype.bytesWritten, undefined); assert.strictEqual(net.Socket.prototype.bytesWritten, undefined);
assert.strictEqual(tls.TLSSocket.super_.prototype.bytesWritten, undefined); assert.strictEqual(Object.getPrototypeOf(tls.TLSSocket).prototype.bytesWritten,
undefined);
assert.strictEqual(tls.TLSSocket.prototype.bytesWritten, undefined); assert.strictEqual(tls.TLSSocket.prototype.bytesWritten, undefined);
assert.strictEqual(tty.ReadStream.super_.prototype.bytesWritten, undefined); assert.strictEqual(Object.getPrototypeOf(tty.ReadStream).prototype.bytesWritten,
undefined);
assert.strictEqual(tty.ReadStream.prototype.bytesWritten, undefined); assert.strictEqual(tty.ReadStream.prototype.bytesWritten, undefined);
assert.strictEqual(tty.WriteStream.prototype.bytesWritten, undefined); assert.strictEqual(tty.WriteStream.prototype.bytesWritten, undefined);