Skip to content

Commit dcc82b3

Browse files
BridgeARaddaleax
authored andcommitted
lib: remove inherits() usage
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 <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 6ccc80c commit dcc82b3

26 files changed

+50
-72
lines changed

lib/_http_agent.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ function Agent(options) {
105105
}
106106
});
107107
}
108-
109-
util.inherits(Agent, EventEmitter);
108+
Object.setPrototypeOf(Agent.prototype, EventEmitter.prototype);
110109

111110
Agent.defaultMaxSockets = Infinity;
112111

lib/_http_client.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,7 @@ function ClientRequest(input, options, cb) {
279279

280280
this._deferToConnect(null, null, () => this._flush());
281281
}
282-
283-
util.inherits(ClientRequest, OutgoingMessage);
284-
282+
Object.setPrototypeOf(ClientRequest.prototype, OutgoingMessage.prototype);
285283

286284
ClientRequest.prototype._finish = function _finish() {
287285
DTRACE_HTTP_CLIENT_REQUEST(this, this.connection);

lib/_http_incoming.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
'use strict';
2323

24-
const util = require('util');
2524
const Stream = require('stream');
2625

2726
function readStart(socket) {
@@ -72,8 +71,7 @@ function IncomingMessage(socket) {
7271
// read by the user, so there's no point continuing to handle it.
7372
this._dumped = false;
7473
}
75-
util.inherits(IncomingMessage, Stream.Readable);
76-
74+
Object.setPrototypeOf(IncomingMessage.prototype, Stream.Readable.prototype);
7775

7876
IncomingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
7977
if (callback)

lib/_http_outgoing.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function OutgoingMessage() {
106106

107107
this._onPendingData = noopPendingOutput;
108108
}
109-
util.inherits(OutgoingMessage, Stream);
109+
Object.setPrototypeOf(OutgoingMessage.prototype, Stream.prototype);
110110

111111

112112
Object.defineProperty(OutgoingMessage.prototype, '_headers', {

lib/_http_server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function ServerResponse(req) {
137137
this.shouldKeepAlive = false;
138138
}
139139
}
140-
util.inherits(ServerResponse, OutgoingMessage);
140+
Object.setPrototypeOf(ServerResponse.prototype, OutgoingMessage.prototype);
141141

142142
ServerResponse.prototype._finish = function _finish() {
143143
DTRACE_HTTP_SERVER_RESPONSE(this.connection);

lib/_stream_duplex.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@
2828

2929
module.exports = Duplex;
3030

31-
const util = require('util');
3231
const Readable = require('_stream_readable');
3332
const Writable = require('_stream_writable');
3433

35-
util.inherits(Duplex, Readable);
34+
Object.setPrototypeOf(Duplex.prototype, Readable.prototype);
3635

3736
{
3837
// Allow the keys array to be GC'ed.

lib/_stream_passthrough.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
module.exports = PassThrough;
2929

3030
const Transform = require('_stream_transform');
31-
const util = require('util');
32-
util.inherits(PassThrough, Transform);
31+
Object.setPrototypeOf(PassThrough.prototype, Transform.prototype);
3332

3433
function PassThrough(options) {
3534
if (!(this instanceof PassThrough))

lib/_stream_readable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const { emitExperimentalWarning } = require('internal/util');
4444
let StringDecoder;
4545
let createReadableStreamAsyncIterator;
4646

47-
util.inherits(Readable, Stream);
47+
Object.setPrototypeOf(Readable.prototype, Stream.prototype);
4848

4949
const { errorOrDestroy } = destroyImpl;
5050
const kProxyEvents = ['error', 'close', 'destroy', 'pause', 'resume'];

lib/_stream_transform.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ const {
7171
ERR_TRANSFORM_WITH_LENGTH_0
7272
} = require('internal/errors').codes;
7373
const Duplex = require('_stream_duplex');
74-
const util = require('util');
75-
util.inherits(Transform, Duplex);
74+
Object.setPrototypeOf(Transform.prototype, Duplex.prototype);
7675

7776

7877
function afterTransform(er, data) {

lib/_stream_writable.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
module.exports = Writable;
2929
Writable.WritableState = WritableState;
3030

31-
const util = require('util');
3231
const internalUtil = require('internal/util');
3332
const Stream = require('stream');
3433
const { Buffer } = require('buffer');
@@ -47,7 +46,7 @@ const {
4746

4847
const { errorOrDestroy } = destroyImpl;
4948

50-
util.inherits(Writable, Stream);
49+
Object.setPrototypeOf(Writable.prototype, Stream.prototype);
5150

5251
function nop() {}
5352

lib/dgram.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function Socket(type, listener) {
108108
sendBufferSize
109109
};
110110
}
111-
util.inherits(Socket, EventEmitter);
111+
Object.setPrototypeOf(Socket.prototype, EventEmitter.prototype);
112112

113113

114114
function createSocket(type, listener) {

lib/https.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function Agent(options) {
149149
list: []
150150
};
151151
}
152-
inherits(Agent, HttpAgent);
152+
Object.setPrototypeOf(Agent.prototype, HttpAgent.prototype);
153153
Agent.prototype.createConnection = createConnection;
154154

155155
Agent.prototype.getName = function getName(options) {

lib/internal/child_process.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ function ChildProcess() {
265265
maybeClose(this);
266266
};
267267
}
268-
util.inherits(ChildProcess, EventEmitter);
268+
Object.setPrototypeOf(ChildProcess.prototype, EventEmitter.prototype);
269269

270270

271271
function flushStdio(subprocess) {

lib/internal/cluster/worker.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22
const EventEmitter = require('events');
3-
const util = require('util');
43

54
module.exports = Worker;
65

@@ -30,7 +29,7 @@ function Worker(options) {
3029
}
3130
}
3231

33-
util.inherits(Worker, EventEmitter);
32+
Object.setPrototypeOf(Worker.prototype, EventEmitter.prototype);
3433

3534
Worker.prototype.kill = function() {
3635
this.destroy.apply(this, arguments);

lib/internal/crypto/cipher.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const {
3232
const assert = require('assert');
3333
const LazyTransform = require('internal/streams/lazy_transform');
3434

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

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

127-
inherits(Cipher, LazyTransform);
126+
Object.setPrototypeOf(Cipher.prototype, LazyTransform.prototype);
128127

129128
Cipher.prototype._transform = function _transform(chunk, encoding, callback) {
130129
this.push(this[kHandle].update(chunk, encoding));
@@ -254,7 +253,7 @@ function addCipherPrototypeFunctions(constructor) {
254253
constructor.prototype.setAAD = Cipher.prototype.setAAD;
255254
}
256255

257-
inherits(Cipheriv, LazyTransform);
256+
Object.setPrototypeOf(Cipheriv.prototype, LazyTransform.prototype);
258257
addCipherPrototypeFunctions(Cipheriv);
259258
legacyNativeHandle(Cipheriv);
260259

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

268-
inherits(Decipher, LazyTransform);
267+
Object.setPrototypeOf(Decipher.prototype, LazyTransform.prototype);
269268
addCipherPrototypeFunctions(Decipher);
270269
legacyNativeHandle(Decipher);
271270

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

280-
inherits(Decipheriv, LazyTransform);
279+
Object.setPrototypeOf(Decipheriv.prototype, LazyTransform.prototype);
281280
addCipherPrototypeFunctions(Decipheriv);
282281
legacyNativeHandle(Decipheriv);
283282

lib/internal/crypto/hash.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const {
2121
ERR_INVALID_ARG_TYPE
2222
} = require('internal/errors').codes;
2323
const { validateString } = require('internal/validators');
24-
const { inherits } = require('util');
2524
const { normalizeEncoding } = require('internal/util');
2625
const { isArrayBufferView } = require('internal/util/types');
2726
const LazyTransform = require('internal/streams/lazy_transform');
@@ -39,7 +38,7 @@ function Hash(algorithm, options) {
3938
LazyTransform.call(this, options);
4039
}
4140

42-
inherits(Hash, LazyTransform);
41+
Object.setPrototypeOf(Hash.prototype, LazyTransform.prototype);
4342

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

103-
inherits(Hmac, LazyTransform);
102+
Object.setPrototypeOf(Hmac.prototype, LazyTransform.prototype);
104103

105104
Hmac.prototype.update = Hash.prototype.update;
106105

lib/internal/crypto/sig.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const {
1818
validateArrayBufferView,
1919
} = require('internal/crypto/util');
2020
const { Writable } = require('stream');
21-
const { inherits } = require('util');
2221

2322
function Sign(algorithm, options) {
2423
if (!(this instanceof Sign))
@@ -30,7 +29,7 @@ function Sign(algorithm, options) {
3029
Writable.call(this, options);
3130
}
3231

33-
inherits(Sign, Writable);
32+
Object.setPrototypeOf(Sign.prototype, Writable.prototype);
3433

3534
Sign.prototype._write = function _write(chunk, encoding, callback) {
3635
this.update(chunk, encoding);
@@ -101,7 +100,7 @@ function Verify(algorithm, options) {
101100
Writable.call(this, options);
102101
}
103102

104-
inherits(Verify, Writable);
103+
Object.setPrototypeOf(Verify.prototype, Writable.prototype);
105104

106105
Verify.prototype._write = Sign.prototype._write;
107106
Verify.prototype.update = Sign.prototype.update;

lib/internal/fs/streams.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const {
1717
} = require('internal/fs/utils');
1818
const { Readable, Writable } = require('stream');
1919
const { toPathIfFileURL } = require('internal/url');
20-
const util = require('util');
2120

2221
const kMinPoolSpace = 128;
2322

@@ -119,7 +118,7 @@ function ReadStream(path, options) {
119118
}
120119
});
121120
}
122-
util.inherits(ReadStream, Readable);
121+
Object.setPrototypeOf(ReadStream.prototype, Readable.prototype);
123122

124123
ReadStream.prototype.open = function() {
125124
fs.open(this.path, this.flags, this.mode, (er, fd) => {
@@ -273,7 +272,7 @@ function WriteStream(path, options) {
273272
if (typeof this.fd !== 'number')
274273
this.open();
275274
}
276-
util.inherits(WriteStream, Writable);
275+
Object.setPrototypeOf(WriteStream.prototype, Writable.prototype);
277276

278277
WriteStream.prototype._final = function(callback) {
279278
if (this.autoClose) {

lib/internal/fs/sync_write_stream.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const { Writable } = require('stream');
4-
const { inherits } = require('util');
54
const { closeSync, writeSync } = require('fs');
65

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

19-
inherits(SyncWriteStream, Writable);
18+
Object.setPrototypeOf(SyncWriteStream.prototype, Writable.prototype);
2019

2120
SyncWriteStream.prototype._write = function(chunk, encoding, cb) {
2221
writeSync(this.fd, chunk, 0, chunk.length);

lib/internal/fs/watchers.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const {
1919
const { toNamespacedPath } = require('path');
2020
const { validateUint32 } = require('internal/validators');
2121
const { toPathIfFileURL } = require('internal/url');
22-
const util = require('util');
2322
const assert = require('assert');
2423

2524
const kOldStatus = Symbol('kOldStatus');
@@ -36,7 +35,7 @@ function StatWatcher(bigint) {
3635
this[kOldStatus] = -1;
3736
this[kUseBigint] = bigint;
3837
}
39-
util.inherits(StatWatcher, EventEmitter);
38+
Object.setPrototypeOf(StatWatcher.prototype, EventEmitter.prototype);
4039

4140
function onchange(newStatus, stats) {
4241
const self = this[owner_symbol];
@@ -132,7 +131,7 @@ function FSWatcher() {
132131
}
133132
};
134133
}
135-
util.inherits(FSWatcher, EventEmitter);
134+
Object.setPrototypeOf(FSWatcher.prototype, EventEmitter.prototype);
136135

137136

138137
// FIXME(joyeecheung): this method is not documented.

lib/internal/streams/legacy.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict';
22

33
const EE = require('events');
4-
const util = require('util');
54

65
function Stream() {
76
EE.call(this);
87
}
9-
util.inherits(Stream, EE);
8+
Object.setPrototypeOf(Stream.prototype, EE.prototype);
109

1110
Stream.prototype.pipe = function(dest, options) {
1211
var source = this;

lib/net.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ function Server(options, connectionListener) {
11451145
this.allowHalfOpen = options.allowHalfOpen || false;
11461146
this.pauseOnConnect = !!options.pauseOnConnect;
11471147
}
1148-
util.inherits(Server, EventEmitter);
1148+
Object.setPrototypeOf(Server.prototype, EventEmitter.prototype);
11491149

11501150

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

lib/perf_hooks.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ const {
3333
const { AsyncResource } = require('async_hooks');
3434
const L = require('internal/linkedlist');
3535
const kInspect = require('internal/util').customInspectSymbol;
36-
const { inherits } = require('util');
3736

3837
const kCallback = Symbol('callback');
3938
const kTypes = Symbol('types');
@@ -208,10 +207,8 @@ class PerformanceNodeTiming {
208207
};
209208
}
210209
}
211-
// Use this instead of Extends because we want PerformanceEntry in the
212-
// prototype chain but we do not want to use the PerformanceEntry
213-
// constructor for this.
214-
inherits(PerformanceNodeTiming, PerformanceEntry);
210+
Object.setPrototypeOf(
211+
PerformanceNodeTiming.prototype, PerformanceEntry.prototype);
215212

216213
const nodeTiming = new PerformanceNodeTiming();
217214

lib/readline.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const {
3232
ERR_INVALID_CURSOR_POS,
3333
ERR_INVALID_OPT_VALUE
3434
} = require('internal/errors').codes;
35-
const { debug, inherits } = require('util');
35+
const { debug } = require('util');
3636
const { emitExperimentalWarning } = require('internal/util');
3737
const { Buffer } = require('buffer');
3838
const EventEmitter = require('events');
@@ -245,7 +245,7 @@ function Interface(input, output, completer, terminal) {
245245
input.resume();
246246
}
247247

248-
inherits(Interface, EventEmitter);
248+
Object.setPrototypeOf(Interface.prototype, EventEmitter.prototype);
249249

250250
Object.defineProperty(Interface.prototype, 'columns', {
251251
configurable: true,

0 commit comments

Comments
 (0)