Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: remove inherits() usage #24755

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ Inherit the prototype methods from one [constructor][] into another. The
prototype of `constructor` will be set to a new object created from
`superConstructor`.

This mainly adds some input validation on top of
`Object.setPrototypeOf(constructor.prototype, superConstructor.prototype)`.
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
As an additional convenience, `superConstructor` will be accessible
through the `constructor.super_` property.

Expand Down
4 changes: 2 additions & 2 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ function Agent(options) {
}
});
}

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

Agent.defaultMaxSockets = Infinity;

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

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

util.inherits(ClientRequest, OutgoingMessage);

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

ClientRequest.prototype._finish = function _finish() {
DTRACE_HTTP_CLIENT_REQUEST(this, this.connection);
Expand Down
5 changes: 2 additions & 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,8 @@ 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);
Object.setPrototypeOf(IncomingMessage, Stream.Readable);

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

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


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

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

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);
Object.setPrototypeOf(Duplex, Readable);

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

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

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

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

const { errorOrDestroy } = destroyImpl;
const kProxyEvents = ['error', 'close', 'destroy', 'pause', 'resume'];
Expand Down
4 changes: 2 additions & 2 deletions lib/_stream_transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ 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);
Object.setPrototypeOf(Transform, Duplex);


function afterTransform(er, data) {
Expand Down
4 changes: 2 additions & 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,8 @@ const {

const { errorOrDestroy } = destroyImpl;

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

function nop() {}

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


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

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


function flushStdio(subprocess) {
Expand Down
4 changes: 2 additions & 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,8 @@ function Worker(options) {
}
}

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

Worker.prototype.kill = function() {
this.destroy.apply(this, arguments);
Expand Down
13 changes: 8 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,8 @@ function Cipher(cipher, password, options) {
createCipher.call(this, cipher, password, options, true);
}

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

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

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

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

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

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

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

Expand Down
7 changes: 4 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,8 @@ function Hash(algorithm, options) {
LazyTransform.call(this, options);
}

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

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

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

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

Expand Down
7 changes: 4 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,8 @@ function Sign(algorithm, options) {
Writable.call(this, options);
}

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

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

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

Verify.prototype._write = Sign.prototype._write;
Verify.prototype.update = Sign.prototype.update;
Expand Down
7 changes: 4 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,8 @@ function ReadStream(path, options) {
}
});
}
util.inherits(ReadStream, Readable);
Object.setPrototypeOf(ReadStream.prototype, Readable.prototype);
Object.setPrototypeOf(ReadStream, Readable);

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

WriteStream.prototype._final = function(callback) {
if (this.autoClose) {
Expand Down
4 changes: 2 additions & 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,8 @@ function SyncWriteStream(fd, options) {
this.on('end', () => this._destroy());
}

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

SyncWriteStream.prototype._write = function(chunk, encoding, cb) {
writeSync(this.fd, chunk, 0, chunk.length);
Expand Down
7 changes: 4 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,8 @@ function StatWatcher(bigint) {
this[kOldStatus] = -1;
this[kUseBigint] = bigint;
}
util.inherits(StatWatcher, EventEmitter);
Object.setPrototypeOf(StatWatcher.prototype, EventEmitter.prototype);
Object.setPrototypeOf(StatWatcher, EventEmitter);

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


// FIXME(joyeecheung): this method is not documented.
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/streams/legacy.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'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);
Object.setPrototypeOf(Stream, EE);

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


function toNumber(x) { return (x = Number(x)) >= 0 ? x : false; }
Expand Down
Loading