Skip to content

Commit

Permalink
lib: remove internal util.inherits() usage
Browse files Browse the repository at this point in the history
It is recommended not to use this function anymore. Therefore all
internal usage is removed and switched to `Object.setPrototypeOf()`.
  • Loading branch information
BridgeAR committed Jan 15, 2019
1 parent 1c7b5db commit 43fa556
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

'use strict';

const util = require('util');
const net = require('net');
const assert = require('assert').ok;
const {
Expand Down Expand Up @@ -309,7 +308,8 @@ function Server(options, requestListener) {
this.maxHeadersCount = null;
this.headersTimeout = 40 * 1000; // 40 seconds
}
util.inherits(Server, net.Server);
Object.setPrototypeOf(Server, net.Server);
Object.setPrototypeOf(Server.prototype, net.Server.prototype);


Server.prototype.setTimeout = function setTimeout(msecs, callback) {
Expand Down
8 changes: 6 additions & 2 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,9 @@ function TLSSocket(socket, opts) {
// Read on next tick so the caller has a chance to setup listeners
process.nextTick(initRead, this, socket);
}
util.inherits(TLSSocket, net.Socket);
Object.setPrototypeOf(TLSSocket, net.Socket);
Object.setPrototypeOf(TLSSocket.prototype, net.Socket.prototype);

exports.TLSSocket = TLSSocket;

var proxiedMethods = [
Expand Down Expand Up @@ -881,7 +883,9 @@ function Server(options, listener) {
}
}

util.inherits(Server, net.Server);
Object.setPrototypeOf(Server, net.Server);
Object.setPrototypeOf(Server.prototype, net.Server.prototype);

exports.Server = Server;
exports.createServer = function createServer(options, listener) {
return new Server(options, listener);
Expand Down
4 changes: 2 additions & 2 deletions lib/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const {
kServerResponse
} = require('_http_server');
const { ClientRequest } = require('_http_client');
const { inherits } = util;
const debug = util.debuglog('https');
const { URL, urlToOptions, searchParamsSymbol } = require('internal/url');
const { IncomingMessage, ServerResponse } = require('http');
Expand Down Expand Up @@ -76,7 +75,8 @@ function Server(opts, requestListener) {
this.maxHeadersCount = null;
this.headersTimeout = 40 * 1000; // 40 seconds
}
inherits(Server, tls.Server);
Object.setPrototypeOf(Server, tls.Server);
Object.setPrototypeOf(Server.prototype, tls.Server.prototype);

Server.prototype.setTimeout = HttpServer.prototype.setTimeout;

Expand Down
4 changes: 2 additions & 2 deletions lib/internal/streams/lazy_transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
'use strict';

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

const {
getDefaultEncoding
Expand All @@ -17,7 +16,8 @@ function LazyTransform(options) {
this.writable = true;
this.readable = true;
}
util.inherits(LazyTransform, stream.Transform);
Object.setPrototypeOf(LazyTransform, stream.Transform);
Object.setPrototypeOf(LazyTransform.prototype, stream.Transform.prototype);

function makeGetter(name) {
return function() {
Expand Down
3 changes: 2 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ function Socket(options) {
this[kBytesRead] = 0;
this[kBytesWritten] = 0;
}
util.inherits(Socket, stream.Duplex);
Object.setPrototypeOf(Socket, stream.Duplex);
Object.setPrototypeOf(Socket.prototype, stream.Duplex.prototype);

// Refresh existing timeouts.
Socket.prototype._unrefTimer = function _unrefTimer() {
Expand Down
7 changes: 4 additions & 3 deletions lib/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

'use strict';

const { inherits } = require('util');
const net = require('net');
const { TTY, isTTY } = internalBinding('tty_wrap');
const errors = require('internal/errors');
Expand Down Expand Up @@ -58,7 +57,8 @@ function ReadStream(fd, options) {
this.isRaw = false;
this.isTTY = true;
}
inherits(ReadStream, net.Socket);
Object.setPrototypeOf(ReadStream, net.Socket);
Object.setPrototypeOf(ReadStream.prototype, net.Socket.prototype);

ReadStream.prototype.setRawMode = function(flag) {
flag = !!flag;
Expand Down Expand Up @@ -103,7 +103,8 @@ function WriteStream(fd) {
this.rows = winSize[1];
}
}
inherits(WriteStream, net.Socket);
Object.setPrototypeOf(WriteStream, net.Socket);
Object.setPrototypeOf(WriteStream.prototype, net.Socket.prototype);

WriteStream.prototype.isTTY = true;

Expand Down
6 changes: 4 additions & 2 deletions test/parallel/test-net-access-byteswritten.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ const tty = require('tty');
// Check that the bytesWritten getter doesn't crash if object isn't
// constructed.
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(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.WriteStream.prototype.bytesWritten, undefined);

0 comments on commit 43fa556

Please sign in to comment.