Skip to content

Commit

Permalink
lib: remove inherits() usage
Browse files Browse the repository at this point in the history
This switches all `util.inherits()` calls to use
`Object.setPrototypeOf()` instead. In fact, `util.inherits()` is
mainly a small wrapper around exactly this function while adding
the `_super` property on the object as well.

Refs: #24395

PR-URL: #24755
Refs: #24395
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
BridgeAR authored and addaleax committed Dec 5, 2018
1 parent 6ccc80c commit dcc82b3
Show file tree
Hide file tree
Showing 26 changed files with 50 additions and 72 deletions.
3 changes: 1 addition & 2 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ function Agent(options) {
}
});
}

util.inherits(Agent, EventEmitter);
Object.setPrototypeOf(Agent.prototype, EventEmitter.prototype);

Agent.defaultMaxSockets = Infinity;

Expand Down
4 changes: 1 addition & 3 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,7 @@ function ClientRequest(input, options, cb) {

this._deferToConnect(null, null, () => this._flush());
}

util.inherits(ClientRequest, OutgoingMessage);

Object.setPrototypeOf(ClientRequest.prototype, OutgoingMessage.prototype);

ClientRequest.prototype._finish = function _finish() {
DTRACE_HTTP_CLIENT_REQUEST(this, this.connection);
Expand Down
4 changes: 1 addition & 3 deletions lib/_http_incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

'use strict';

const util = require('util');
const Stream = require('stream');

function readStart(socket) {
Expand Down Expand Up @@ -72,8 +71,7 @@ function IncomingMessage(socket) {
// read by the user, so there's no point continuing to handle it.
this._dumped = false;
}
util.inherits(IncomingMessage, Stream.Readable);

Object.setPrototypeOf(IncomingMessage.prototype, Stream.Readable.prototype);

IncomingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
if (callback)
Expand Down
2 changes: 1 addition & 1 deletion lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function OutgoingMessage() {

this._onPendingData = noopPendingOutput;
}
util.inherits(OutgoingMessage, Stream);
Object.setPrototypeOf(OutgoingMessage.prototype, Stream.prototype);


Object.defineProperty(OutgoingMessage.prototype, '_headers', {
Expand Down
2 changes: 1 addition & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function ServerResponse(req) {
this.shouldKeepAlive = false;
}
}
util.inherits(ServerResponse, OutgoingMessage);
Object.setPrototypeOf(ServerResponse.prototype, OutgoingMessage.prototype);

ServerResponse.prototype._finish = function _finish() {
DTRACE_HTTP_SERVER_RESPONSE(this.connection);
Expand Down
3 changes: 1 addition & 2 deletions lib/_stream_duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@

module.exports = Duplex;

const util = require('util');
const Readable = require('_stream_readable');
const Writable = require('_stream_writable');

util.inherits(Duplex, Readable);
Object.setPrototypeOf(Duplex.prototype, Readable.prototype);

{
// Allow the keys array to be GC'ed.
Expand Down
3 changes: 1 addition & 2 deletions lib/_stream_passthrough.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
module.exports = PassThrough;

const Transform = require('_stream_transform');
const util = require('util');
util.inherits(PassThrough, Transform);
Object.setPrototypeOf(PassThrough.prototype, Transform.prototype);

function PassThrough(options) {
if (!(this instanceof PassThrough))
Expand Down
2 changes: 1 addition & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const { emitExperimentalWarning } = require('internal/util');
let StringDecoder;
let createReadableStreamAsyncIterator;

util.inherits(Readable, Stream);
Object.setPrototypeOf(Readable.prototype, Stream.prototype);

const { errorOrDestroy } = destroyImpl;
const kProxyEvents = ['error', 'close', 'destroy', 'pause', 'resume'];
Expand Down
3 changes: 1 addition & 2 deletions lib/_stream_transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ const {
ERR_TRANSFORM_WITH_LENGTH_0
} = require('internal/errors').codes;
const Duplex = require('_stream_duplex');
const util = require('util');
util.inherits(Transform, Duplex);
Object.setPrototypeOf(Transform.prototype, Duplex.prototype);


function afterTransform(er, data) {
Expand Down
3 changes: 1 addition & 2 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
module.exports = Writable;
Writable.WritableState = WritableState;

const util = require('util');
const internalUtil = require('internal/util');
const Stream = require('stream');
const { Buffer } = require('buffer');
Expand All @@ -47,7 +46,7 @@ const {

const { errorOrDestroy } = destroyImpl;

util.inherits(Writable, Stream);
Object.setPrototypeOf(Writable.prototype, Stream.prototype);

function nop() {}

Expand Down
2 changes: 1 addition & 1 deletion lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function Socket(type, listener) {
sendBufferSize
};
}
util.inherits(Socket, EventEmitter);
Object.setPrototypeOf(Socket.prototype, EventEmitter.prototype);


function createSocket(type, listener) {
Expand Down
2 changes: 1 addition & 1 deletion lib/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function Agent(options) {
list: []
};
}
inherits(Agent, HttpAgent);
Object.setPrototypeOf(Agent.prototype, HttpAgent.prototype);
Agent.prototype.createConnection = createConnection;

Agent.prototype.getName = function getName(options) {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function ChildProcess() {
maybeClose(this);
};
}
util.inherits(ChildProcess, EventEmitter);
Object.setPrototypeOf(ChildProcess.prototype, EventEmitter.prototype);


function flushStdio(subprocess) {
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/cluster/worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
const EventEmitter = require('events');
const util = require('util');

module.exports = Worker;

Expand Down Expand Up @@ -30,7 +29,7 @@ function Worker(options) {
}
}

util.inherits(Worker, EventEmitter);
Object.setPrototypeOf(Worker.prototype, EventEmitter.prototype);

Worker.prototype.kill = function() {
this.destroy.apply(this, arguments);
Expand Down
9 changes: 4 additions & 5 deletions lib/internal/crypto/cipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const {
const assert = require('assert');
const LazyTransform = require('internal/streams/lazy_transform');

const { inherits } = require('util');
const { deprecate, normalizeEncoding } = require('internal/util');

// Lazy loaded for startup performance.
Expand Down Expand Up @@ -124,7 +123,7 @@ function Cipher(cipher, password, options) {
createCipher.call(this, cipher, password, options, true);
}

inherits(Cipher, LazyTransform);
Object.setPrototypeOf(Cipher.prototype, LazyTransform.prototype);

Cipher.prototype._transform = function _transform(chunk, encoding, callback) {
this.push(this[kHandle].update(chunk, encoding));
Expand Down Expand Up @@ -254,7 +253,7 @@ function addCipherPrototypeFunctions(constructor) {
constructor.prototype.setAAD = Cipher.prototype.setAAD;
}

inherits(Cipheriv, LazyTransform);
Object.setPrototypeOf(Cipheriv.prototype, LazyTransform.prototype);
addCipherPrototypeFunctions(Cipheriv);
legacyNativeHandle(Cipheriv);

Expand All @@ -265,7 +264,7 @@ function Decipher(cipher, password, options) {
createCipher.call(this, cipher, password, options, false);
}

inherits(Decipher, LazyTransform);
Object.setPrototypeOf(Decipher.prototype, LazyTransform.prototype);
addCipherPrototypeFunctions(Decipher);
legacyNativeHandle(Decipher);

Expand All @@ -277,7 +276,7 @@ function Decipheriv(cipher, key, iv, options) {
createCipherWithIV.call(this, cipher, key, options, false, iv);
}

inherits(Decipheriv, LazyTransform);
Object.setPrototypeOf(Decipheriv.prototype, LazyTransform.prototype);
addCipherPrototypeFunctions(Decipheriv);
legacyNativeHandle(Decipheriv);

Expand Down
5 changes: 2 additions & 3 deletions lib/internal/crypto/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const {
ERR_INVALID_ARG_TYPE
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');
const { inherits } = require('util');
const { normalizeEncoding } = require('internal/util');
const { isArrayBufferView } = require('internal/util/types');
const LazyTransform = require('internal/streams/lazy_transform');
Expand All @@ -39,7 +38,7 @@ function Hash(algorithm, options) {
LazyTransform.call(this, options);
}

inherits(Hash, LazyTransform);
Object.setPrototypeOf(Hash.prototype, LazyTransform.prototype);

Hash.prototype._transform = function _transform(chunk, encoding, callback) {
this[kHandle].update(chunk, encoding);
Expand Down Expand Up @@ -100,7 +99,7 @@ function Hmac(hmac, key, options) {
LazyTransform.call(this, options);
}

inherits(Hmac, LazyTransform);
Object.setPrototypeOf(Hmac.prototype, LazyTransform.prototype);

Hmac.prototype.update = Hash.prototype.update;

Expand Down
5 changes: 2 additions & 3 deletions lib/internal/crypto/sig.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const {
validateArrayBufferView,
} = require('internal/crypto/util');
const { Writable } = require('stream');
const { inherits } = require('util');

function Sign(algorithm, options) {
if (!(this instanceof Sign))
Expand All @@ -30,7 +29,7 @@ function Sign(algorithm, options) {
Writable.call(this, options);
}

inherits(Sign, Writable);
Object.setPrototypeOf(Sign.prototype, Writable.prototype);

Sign.prototype._write = function _write(chunk, encoding, callback) {
this.update(chunk, encoding);
Expand Down Expand Up @@ -101,7 +100,7 @@ function Verify(algorithm, options) {
Writable.call(this, options);
}

inherits(Verify, Writable);
Object.setPrototypeOf(Verify.prototype, Writable.prototype);

Verify.prototype._write = Sign.prototype._write;
Verify.prototype.update = Sign.prototype.update;
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const {
} = require('internal/fs/utils');
const { Readable, Writable } = require('stream');
const { toPathIfFileURL } = require('internal/url');
const util = require('util');

const kMinPoolSpace = 128;

Expand Down Expand Up @@ -119,7 +118,7 @@ function ReadStream(path, options) {
}
});
}
util.inherits(ReadStream, Readable);
Object.setPrototypeOf(ReadStream.prototype, Readable.prototype);

ReadStream.prototype.open = function() {
fs.open(this.path, this.flags, this.mode, (er, fd) => {
Expand Down Expand Up @@ -273,7 +272,7 @@ function WriteStream(path, options) {
if (typeof this.fd !== 'number')
this.open();
}
util.inherits(WriteStream, Writable);
Object.setPrototypeOf(WriteStream.prototype, Writable.prototype);

WriteStream.prototype._final = function(callback) {
if (this.autoClose) {
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/fs/sync_write_stream.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const { Writable } = require('stream');
const { inherits } = require('util');
const { closeSync, writeSync } = require('fs');

function SyncWriteStream(fd, options) {
Expand All @@ -16,7 +15,7 @@ function SyncWriteStream(fd, options) {
this.on('end', () => this._destroy());
}

inherits(SyncWriteStream, Writable);
Object.setPrototypeOf(SyncWriteStream.prototype, Writable.prototype);

SyncWriteStream.prototype._write = function(chunk, encoding, cb) {
writeSync(this.fd, chunk, 0, chunk.length);
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/fs/watchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const {
const { toNamespacedPath } = require('path');
const { validateUint32 } = require('internal/validators');
const { toPathIfFileURL } = require('internal/url');
const util = require('util');
const assert = require('assert');

const kOldStatus = Symbol('kOldStatus');
Expand All @@ -36,7 +35,7 @@ function StatWatcher(bigint) {
this[kOldStatus] = -1;
this[kUseBigint] = bigint;
}
util.inherits(StatWatcher, EventEmitter);
Object.setPrototypeOf(StatWatcher.prototype, EventEmitter.prototype);

function onchange(newStatus, stats) {
const self = this[owner_symbol];
Expand Down Expand Up @@ -132,7 +131,7 @@ function FSWatcher() {
}
};
}
util.inherits(FSWatcher, EventEmitter);
Object.setPrototypeOf(FSWatcher.prototype, EventEmitter.prototype);


// FIXME(joyeecheung): this method is not documented.
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/streams/legacy.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';

const EE = require('events');
const util = require('util');

function Stream() {
EE.call(this);
}
util.inherits(Stream, EE);
Object.setPrototypeOf(Stream.prototype, EE.prototype);

Stream.prototype.pipe = function(dest, options) {
var source = this;
Expand Down
2 changes: 1 addition & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ function Server(options, connectionListener) {
this.allowHalfOpen = options.allowHalfOpen || false;
this.pauseOnConnect = !!options.pauseOnConnect;
}
util.inherits(Server, EventEmitter);
Object.setPrototypeOf(Server.prototype, EventEmitter.prototype);


function toNumber(x) { return (x = Number(x)) >= 0 ? x : false; }
Expand Down
7 changes: 2 additions & 5 deletions lib/perf_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const {
const { AsyncResource } = require('async_hooks');
const L = require('internal/linkedlist');
const kInspect = require('internal/util').customInspectSymbol;
const { inherits } = require('util');

const kCallback = Symbol('callback');
const kTypes = Symbol('types');
Expand Down Expand Up @@ -208,10 +207,8 @@ class PerformanceNodeTiming {
};
}
}
// Use this instead of Extends because we want PerformanceEntry in the
// prototype chain but we do not want to use the PerformanceEntry
// constructor for this.
inherits(PerformanceNodeTiming, PerformanceEntry);
Object.setPrototypeOf(
PerformanceNodeTiming.prototype, PerformanceEntry.prototype);

const nodeTiming = new PerformanceNodeTiming();

Expand Down
4 changes: 2 additions & 2 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const {
ERR_INVALID_CURSOR_POS,
ERR_INVALID_OPT_VALUE
} = require('internal/errors').codes;
const { debug, inherits } = require('util');
const { debug } = require('util');
const { emitExperimentalWarning } = require('internal/util');
const { Buffer } = require('buffer');
const EventEmitter = require('events');
Expand Down Expand Up @@ -245,7 +245,7 @@ function Interface(input, output, completer, terminal) {
input.resume();
}

inherits(Interface, EventEmitter);
Object.setPrototypeOf(Interface.prototype, EventEmitter.prototype);

Object.defineProperty(Interface.prototype, 'columns', {
configurable: true,
Expand Down
Loading

0 comments on commit dcc82b3

Please sign in to comment.