From f2108fa51dc47999b5d7f8fdb1498719ea72c6a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Fri, 10 Nov 2017 19:18:12 +0100 Subject: [PATCH] test: use ES6 classes instead of util.inherits Backport-PR-URL: https://github.com/nodejs/node/pull/17068 PR-URL: https://github.com/nodejs/node/pull/16938 Reviewed-By: Refael Ackermann Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Gireesh Punathil --- .../test-crypto-lazy-transform-writable.js | 11 +- test/parallel/test-crypto-stream.js | 34 +++--- test/parallel/test-event-emitter-listeners.js | 4 +- .../test-http-client-read-in-error.js | 46 ++++---- test/parallel/test-http-client-readable.js | 58 +++++----- test/parallel/test-readline-interface.js | 13 +-- test/parallel/test-readline-keys.js | 7 +- test/parallel/test-stream-big-packet.js | 33 +++--- test/parallel/test-stream-events-prepend.js | 29 +++-- test/parallel/test-stream-pipe-after-end.js | 49 ++++----- test/parallel/test-stream-push-strings.js | 46 ++++---- ...stream-writable-change-default-encoding.js | 21 ++-- .../test-stream-writable-decoded-encoding.js | 21 ++-- test/parallel/test-stream-writable-null.js | 19 ++-- test/parallel/test-stream2-basic.js | 101 +++++++++--------- test/parallel/test-stream2-compatibility.js | 48 ++++----- .../test-stream2-pipe-error-once-listener.js | 30 ++---- test/parallel/test-stream2-set-encoding.js | 59 +++++----- test/parallel/test-stream2-unpipe-drain.js | 33 +++--- test/parallel/test-stream2-unpipe-leak.js | 30 +++--- test/parallel/test-stream2-writable.js | 31 +++--- test/parallel/test-util-format.js | 17 +-- test/pummel/test-tls-server-large-request.js | 26 ++--- test/sequential/test-stream2-fs.js | 35 +++--- 24 files changed, 373 insertions(+), 428 deletions(-) diff --git a/test/parallel/test-crypto-lazy-transform-writable.js b/test/parallel/test-crypto-lazy-transform-writable.js index f12243b9f4d17c..94240321bd755e 100644 --- a/test/parallel/test-crypto-lazy-transform-writable.js +++ b/test/parallel/test-crypto-lazy-transform-writable.js @@ -7,7 +7,6 @@ if (!common.hasCrypto) const assert = require('assert'); const crypto = require('crypto'); const Stream = require('stream'); -const util = require('util'); const hasher1 = crypto.createHash('sha256'); const hasher2 = crypto.createHash('sha256'); @@ -18,12 +17,12 @@ hasher1.end(); const expected = hasher1.read().toString('hex'); -function OldStream() { - Stream.call(this); - - this.readable = true; +class OldStream extends Stream { + constructor() { + super(); + this.readable = true; + } } -util.inherits(OldStream, Stream); const stream = new OldStream(); diff --git a/test/parallel/test-crypto-stream.js b/test/parallel/test-crypto-stream.js index fe218504267538..6eac641c93fbba 100644 --- a/test/parallel/test-crypto-stream.js +++ b/test/parallel/test-crypto-stream.js @@ -5,26 +5,26 @@ if (!common.hasCrypto) const assert = require('assert'); const stream = require('stream'); -const util = require('util'); const crypto = require('crypto'); -// Small stream to buffer converter -function Stream2buffer(callback) { - stream.Writable.call(this); - - this._buffers = []; - this.once('finish', function() { - callback(null, Buffer.concat(this._buffers)); - }); -} -util.inherits(Stream2buffer, stream.Writable); - -Stream2buffer.prototype._write = function(data, encodeing, done) { - this._buffers.push(data); - return done(null); -}; - if (!common.hasFipsCrypto) { + // Small stream to buffer converter + class Stream2buffer extends stream.Writable { + constructor(callback) { + super(); + + this._buffers = []; + this.once('finish', function() { + callback(null, Buffer.concat(this._buffers)); + }); + } + + _write(data, encodeing, done) { + this._buffers.push(data); + return done(null); + } + } + // Create an md5 hash of "Hallo world" const hasher1 = crypto.createHash('md5'); hasher1.pipe(new Stream2buffer(common.mustCall(function end(err, hash) { diff --git a/test/parallel/test-event-emitter-listeners.js b/test/parallel/test-event-emitter-listeners.js index 11c1ed49824cd2..fb60b71c6ec9cf 100644 --- a/test/parallel/test-event-emitter-listeners.js +++ b/test/parallel/test-event-emitter-listeners.js @@ -3,12 +3,9 @@ require('../common'); const assert = require('assert'); const events = require('events'); -const util = require('util'); function listener() {} function listener2() {} -class TestStream { constructor() { } } -util.inherits(TestStream, events.EventEmitter); { const ee = new events.EventEmitter(); @@ -47,6 +44,7 @@ util.inherits(TestStream, events.EventEmitter); } { + class TestStream extends events.EventEmitter {} const s = new TestStream(); assert.deepStrictEqual(s.listeners('foo'), []); } diff --git a/test/parallel/test-http-client-read-in-error.js b/test/parallel/test-http-client-read-in-error.js index 69b24f73771d41..dbf103013175d3 100644 --- a/test/parallel/test-http-client-read-in-error.js +++ b/test/parallel/test-http-client-read-in-error.js @@ -2,38 +2,34 @@ require('../common'); const net = require('net'); const http = require('http'); -const util = require('util'); -function Agent() { - http.Agent.call(this); -} -util.inherits(Agent, http.Agent); - -Agent.prototype.createConnection = function() { - const self = this; - const socket = new net.Socket(); +class Agent extends http.Agent { + createConnection() { + const socket = new net.Socket(); - socket.on('error', function() { - socket.push('HTTP/1.1 200\r\n\r\n'); - }); + socket.on('error', function() { + socket.push('HTTP/1.1 200\r\n\r\n'); + }); - socket.on('newListener', function onNewListener(name) { - if (name !== 'error') - return; - socket.removeListener('newListener', onNewListener); + let onNewListener; + socket.on('newListener', onNewListener = (name) => { + if (name !== 'error') + return; + socket.removeListener('newListener', onNewListener); - // Let other listeners to be set up too - process.nextTick(function() { - self.breakSocket(socket); + // Let other listeners to be set up too + process.nextTick(() => { + this.breakSocket(socket); + }); }); - }); - return socket; -}; + return socket; + } -Agent.prototype.breakSocket = function breakSocket(socket) { - socket.emit('error', new Error('Intentional error')); -}; + breakSocket(socket) { + socket.emit('error', new Error('Intentional error')); + } +} const agent = new Agent(); diff --git a/test/parallel/test-http-client-readable.js b/test/parallel/test-http-client-readable.js index bc6421692b305b..dd7c371f1894e6 100644 --- a/test/parallel/test-http-client-readable.js +++ b/test/parallel/test-http-client-readable.js @@ -2,41 +2,37 @@ const common = require('../common'); const assert = require('assert'); const http = require('http'); -const util = require('util'); const Duplex = require('stream').Duplex; -function FakeAgent() { - http.Agent.call(this); +class FakeAgent extends http.Agent { + createConnection() { + const s = new Duplex(); + let once = false; + + s._read = function() { + if (once) + return this.push(null); + once = true; + + this.push('HTTP/1.1 200 Ok\r\nTransfer-Encoding: chunked\r\n\r\n'); + this.push('b\r\nhello world\r\n'); + this.readable = false; + this.push('0\r\n\r\n'); + }; + + // Blackhole + s._write = function(data, enc, cb) { + cb(); + }; + + s.destroy = s.destroySoon = function() { + this.writable = false; + }; + + return s; + } } -util.inherits(FakeAgent, http.Agent); - -FakeAgent.prototype.createConnection = function() { - const s = new Duplex(); - let once = false; - - s._read = function() { - if (once) - return this.push(null); - once = true; - - this.push('HTTP/1.1 200 Ok\r\nTransfer-Encoding: chunked\r\n\r\n'); - this.push('b\r\nhello world\r\n'); - this.readable = false; - this.push('0\r\n\r\n'); - }; - - // Blackhole - s._write = function(data, enc, cb) { - cb(); - }; - - s.destroy = s.destroySoon = function() { - this.writable = false; - }; - - return s; -}; let received = ''; diff --git a/test/parallel/test-readline-interface.js b/test/parallel/test-readline-interface.js index ea47009bcd76f8..8da5982cff9ada 100644 --- a/test/parallel/test-readline-interface.js +++ b/test/parallel/test-readline-interface.js @@ -3,17 +3,14 @@ const common = require('../common'); const assert = require('assert'); const readline = require('readline'); const EventEmitter = require('events').EventEmitter; -const inherits = require('util').inherits; const { Writable, Readable } = require('stream'); -function FakeInput() { - EventEmitter.call(this); +class FakeInput extends EventEmitter { + resume() {} + pause() {} + write() {} + end() {} } -inherits(FakeInput, EventEmitter); -FakeInput.prototype.resume = () => {}; -FakeInput.prototype.pause = () => {}; -FakeInput.prototype.write = () => {}; -FakeInput.prototype.end = () => {}; function isWarned(emitter) { for (const name in emitter) { diff --git a/test/parallel/test-readline-keys.js b/test/parallel/test-readline-keys.js index 68beb89281cb53..f3e5f8befaf034 100644 --- a/test/parallel/test-readline-keys.js +++ b/test/parallel/test-readline-keys.js @@ -2,14 +2,9 @@ const common = require('../common'); const PassThrough = require('stream').PassThrough; const assert = require('assert'); -const inherits = require('util').inherits; const Interface = require('readline').Interface; - -function FakeInput() { - PassThrough.call(this); -} -inherits(FakeInput, PassThrough); +class FakeInput extends PassThrough {} function extend(k) { return Object.assign({ ctrl: false, meta: false, shift: false }, k); diff --git a/test/parallel/test-stream-big-packet.js b/test/parallel/test-stream-big-packet.js index 72c4526973a9ad..09ed825036f2e4 100644 --- a/test/parallel/test-stream-big-packet.js +++ b/test/parallel/test-stream-big-packet.js @@ -1,31 +1,26 @@ 'use strict'; require('../common'); const assert = require('assert'); -const util = require('util'); const stream = require('stream'); let passed = false; -function PassThrough() { - stream.Transform.call(this); -} -util.inherits(PassThrough, stream.Transform); -PassThrough.prototype._transform = function(chunk, encoding, done) { - this.push(chunk); - done(); -}; - -function TestStream() { - stream.Transform.call(this); +class PassThrough extends stream.Transform { + _transform(chunk, encoding, done) { + this.push(chunk); + done(); + } } -util.inherits(TestStream, stream.Transform); -TestStream.prototype._transform = function(chunk, encoding, done) { - if (!passed) { - // Char 'a' only exists in the last write - passed = chunk.toString().includes('a'); + +class TestStream extends stream.Transform { + _transform(chunk, encoding, done) { + if (!passed) { + // Char 'a' only exists in the last write + passed = chunk.toString().includes('a'); + } + done(); } - done(); -}; +} const s1 = new PassThrough(); const s2 = new PassThrough(); diff --git a/test/parallel/test-stream-events-prepend.js b/test/parallel/test-stream-events-prepend.js index 254d9a3690aee5..80fedf8faee570 100644 --- a/test/parallel/test-stream-events-prepend.js +++ b/test/parallel/test-stream-events-prepend.js @@ -1,26 +1,23 @@ 'use strict'; const common = require('../common'); const stream = require('stream'); -const util = require('util'); -function Writable() { - this.writable = true; - stream.Writable.call(this); - this.prependListener = undefined; +class Writable extends stream.Writable { + constructor() { + super(); + this.prependListener = undefined; + } + + _write(chunk, end, cb) { + cb(); + } } -util.inherits(Writable, stream.Writable); -Writable.prototype._write = function(chunk, end, cb) { - cb(); -}; -function Readable() { - this.readable = true; - stream.Readable.call(this); +class Readable extends stream.Readable { + _read() { + this.push(null); + } } -util.inherits(Readable, stream.Readable); -Readable.prototype._read = function() { - this.push(null); -}; const w = new Writable(); w.on('pipe', common.mustCall()); diff --git a/test/parallel/test-stream-pipe-after-end.js b/test/parallel/test-stream-pipe-after-end.js index fe41d54cabc5f0..af62f619938e14 100644 --- a/test/parallel/test-stream-pipe-after-end.js +++ b/test/parallel/test-stream-pipe-after-end.js @@ -3,35 +3,32 @@ const common = require('../common'); const assert = require('assert'); const Readable = require('_stream_readable'); const Writable = require('_stream_writable'); -const util = require('util'); - -util.inherits(TestReadable, Readable); -function TestReadable(opt) { - if (!(this instanceof TestReadable)) - return new TestReadable(opt); - Readable.call(this, opt); - this._ended = false; -} -TestReadable.prototype._read = function() { - if (this._ended) - this.emit('error', new Error('_read called twice')); - this._ended = true; - this.push(null); -}; - -util.inherits(TestWritable, Writable); -function TestWritable(opt) { - if (!(this instanceof TestWritable)) - return new TestWritable(opt); - Writable.call(this, opt); - this._written = []; +class TestReadable extends Readable { + constructor(opt) { + super(opt); + this._ended = false; + } + + _read() { + if (this._ended) + this.emit('error', new Error('_read called twice')); + this._ended = true; + this.push(null); + } } -TestWritable.prototype._write = function(chunk, encoding, cb) { - this._written.push(chunk); - cb(); -}; +class TestWritable extends Writable { + constructor(opt) { + super(opt); + this._written = []; + } + + _write(chunk, encoding, cb) { + this._written.push(chunk); + cb(); + } +} // this one should not emit 'end' until we read() from it later. const ender = new TestReadable(); diff --git a/test/parallel/test-stream-push-strings.js b/test/parallel/test-stream-push-strings.js index 7ccda9246c30ed..c8fe92a0bdbf02 100644 --- a/test/parallel/test-stream-push-strings.js +++ b/test/parallel/test-stream-push-strings.js @@ -3,32 +3,32 @@ require('../common'); const assert = require('assert'); const Readable = require('stream').Readable; -const util = require('util'); -util.inherits(MyStream, Readable); -function MyStream(options) { - Readable.call(this, options); - this._chunks = 3; -} +class MyStream extends Readable { + constructor(options) { + super(options); + this._chunks = 3; + } -MyStream.prototype._read = function(n) { - switch (this._chunks--) { - case 0: - return this.push(null); - case 1: - return setTimeout(function() { - this.push('last chunk'); - }.bind(this), 100); - case 2: - return this.push('second to last chunk'); - case 3: - return process.nextTick(function() { - this.push('first chunk'); - }.bind(this)); - default: - throw new Error('?'); + _read(n) { + switch (this._chunks--) { + case 0: + return this.push(null); + case 1: + return setTimeout(function() { + this.push('last chunk'); + }.bind(this), 100); + case 2: + return this.push('second to last chunk'); + case 3: + return process.nextTick(function() { + this.push('first chunk'); + }.bind(this)); + default: + throw new Error('?'); + } } -}; +} const ms = new MyStream(); const results = []; diff --git a/test/parallel/test-stream-writable-change-default-encoding.js b/test/parallel/test-stream-writable-change-default-encoding.js index baf343de2d8279..ca8824bb5910dd 100644 --- a/test/parallel/test-stream-writable-change-default-encoding.js +++ b/test/parallel/test-stream-writable-change-default-encoding.js @@ -3,19 +3,18 @@ require('../common'); const assert = require('assert'); const stream = require('stream'); -const util = require('util'); -function MyWritable(fn, options) { - stream.Writable.call(this, options); - this.fn = fn; -} - -util.inherits(MyWritable, stream.Writable); +class MyWritable extends stream.Writable { + constructor(fn, options) { + super(options); + this.fn = fn; + } -MyWritable.prototype._write = function(chunk, encoding, callback) { - this.fn(Buffer.isBuffer(chunk), typeof chunk, encoding); - callback(); -}; + _write(chunk, encoding, callback) { + this.fn(Buffer.isBuffer(chunk), typeof chunk, encoding); + callback(); + } +} (function defaultCondingIsUtf8() { const m = new MyWritable(function(isBuffer, type, enc) { diff --git a/test/parallel/test-stream-writable-decoded-encoding.js b/test/parallel/test-stream-writable-decoded-encoding.js index 4c903a0b012e3e..c859362305aa63 100644 --- a/test/parallel/test-stream-writable-decoded-encoding.js +++ b/test/parallel/test-stream-writable-decoded-encoding.js @@ -3,19 +3,18 @@ require('../common'); const assert = require('assert'); const stream = require('stream'); -const util = require('util'); -function MyWritable(fn, options) { - stream.Writable.call(this, options); - this.fn = fn; -} - -util.inherits(MyWritable, stream.Writable); +class MyWritable extends stream.Writable { + constructor(fn, options) { + super(options); + this.fn = fn; + } -MyWritable.prototype._write = function(chunk, encoding, callback) { - this.fn(Buffer.isBuffer(chunk), typeof chunk, encoding); - callback(); -}; + _write(chunk, encoding, callback) { + this.fn(Buffer.isBuffer(chunk), typeof chunk, encoding); + callback(); + } +} { const m = new MyWritable(function(isBuffer, type, enc) { diff --git a/test/parallel/test-stream-writable-null.js b/test/parallel/test-stream-writable-null.js index 0bb8aa7bbc1c10..da58710a84df68 100644 --- a/test/parallel/test-stream-writable-null.js +++ b/test/parallel/test-stream-writable-null.js @@ -3,18 +3,17 @@ require('../common'); const assert = require('assert'); const stream = require('stream'); -const util = require('util'); -function MyWritable(options) { - stream.Writable.call(this, options); -} - -util.inherits(MyWritable, stream.Writable); +class MyWritable extends stream.Writable { + constructor(opt) { + super(opt); + } -MyWritable.prototype._write = function(chunk, encoding, callback) { - assert.notStrictEqual(chunk, null); - callback(); -}; + _write(chunk, encoding, callback) { + assert.notStrictEqual(chunk, null); + callback(); + } +} assert.throws(() => { const m = new MyWritable({objectMode: true}); diff --git a/test/parallel/test-stream2-basic.js b/test/parallel/test-stream2-basic.js index 58ac2bd1033659..928ac33ddfe807 100644 --- a/test/parallel/test-stream2-basic.js +++ b/test/parallel/test-stream2-basic.js @@ -3,67 +3,66 @@ const common = require('../common'); const R = require('_stream_readable'); const assert = require('assert'); -const util = require('util'); const EE = require('events').EventEmitter; -function TestReader(n) { - R.apply(this); - this._buffer = Buffer.alloc(n || 100, 'x'); - this._pos = 0; - this._bufs = 10; -} +class TestReader extends R { + constructor(n) { + super(); + this._buffer = Buffer.alloc(n || 100, 'x'); + this._pos = 0; + this._bufs = 10; + } -util.inherits(TestReader, R); + _read(n) { + const max = this._buffer.length - this._pos; + n = Math.max(n, 0); + const toRead = Math.min(n, max); + if (toRead === 0) { + // simulate the read buffer filling up with some more bytes some time + // in the future. + setTimeout(() => { + this._pos = 0; + this._bufs -= 1; + if (this._bufs <= 0) { + // read them all! + if (!this.ended) + this.push(null); + } else { + // now we have more. + // kinda cheating by calling _read, but whatever, + // it's just fake anyway. + this._read(n); + } + }, 10); + return; + } -TestReader.prototype._read = function(n) { - const max = this._buffer.length - this._pos; - n = Math.max(n, 0); - const toRead = Math.min(n, max); - if (toRead === 0) { - // simulate the read buffer filling up with some more bytes some time - // in the future. - setTimeout(function() { - this._pos = 0; - this._bufs -= 1; - if (this._bufs <= 0) { - // read them all! - if (!this.ended) - this.push(null); - } else { - // now we have more. - // kinda cheating by calling _read, but whatever, - // it's just fake anyway. - this._read(n); - } - }.bind(this), 10); - return; + const ret = this._buffer.slice(this._pos, this._pos + toRead); + this._pos += toRead; + this.push(ret); } - - const ret = this._buffer.slice(this._pos, this._pos + toRead); - this._pos += toRead; - this.push(ret); -}; +} ///// -function TestWriter() { - EE.apply(this); - this.received = []; - this.flush = false; -} - -util.inherits(TestWriter, EE); +class TestWriter extends EE { + constructor() { + super(); + this.received = []; + this.flush = false; + } -TestWriter.prototype.write = function(c) { - this.received.push(c.toString()); - this.emit('write', c); - return true; -}; + write(c) { + this.received.push(c.toString()); + this.emit('write', c); + return true; + } -TestWriter.prototype.end = function(c) { - if (c) this.write(c); - this.emit('end', this.received); -}; + end(c) { + if (c) this.write(c); + this.emit('end', this.received); + } +} { // Test basic functionality diff --git a/test/parallel/test-stream2-compatibility.js b/test/parallel/test-stream2-compatibility.js index a353da8dede305..758d019f0879eb 100644 --- a/test/parallel/test-stream2-compatibility.js +++ b/test/parallel/test-stream2-compatibility.js @@ -4,25 +4,23 @@ const R = require('_stream_readable'); const W = require('_stream_writable'); const assert = require('assert'); -const util = require('util'); - let ondataCalled = 0; -function TestReader() { - R.apply(this); - this._buffer = Buffer.alloc(100, 'x'); - - this.on('data', function() { - ondataCalled++; - }); -} +class TestReader extends R { + constructor() { + super(); + this._buffer = Buffer.alloc(100, 'x'); -util.inherits(TestReader, R); + this.on('data', () => { + ondataCalled++; + }); + } -TestReader.prototype._read = function(n) { - this.push(this._buffer); - this._buffer = Buffer.alloc(0); -}; + _read(n) { + this.push(this._buffer); + this._buffer = Buffer.alloc(0); + } +} const reader = new TestReader(); setImmediate(function() { @@ -31,17 +29,17 @@ setImmediate(function() { reader.push(null); }); -function TestWriter() { - W.apply(this); - this.write('foo'); - this.end(); -} - -util.inherits(TestWriter, W); +class TestWriter extends W { + constructor() { + super(); + this.write('foo'); + this.end(); + } -TestWriter.prototype._write = function(chunk, enc, cb) { - cb(); -}; + _write(chunk, enc, cb) { + cb(); + } +} const writer = new TestWriter(); diff --git a/test/parallel/test-stream2-pipe-error-once-listener.js b/test/parallel/test-stream2-pipe-error-once-listener.js index c4230619526d79..14543b701b8c48 100644 --- a/test/parallel/test-stream2-pipe-error-once-listener.js +++ b/test/parallel/test-stream2-pipe-error-once-listener.js @@ -1,30 +1,20 @@ 'use strict'; require('../common'); - -const util = require('util'); const stream = require('stream'); - -function Read() { - stream.Readable.call(this); +class Read extends stream.Readable { + _read(size) { + this.push('x'); + this.push(null); + } } -util.inherits(Read, stream.Readable); - -Read.prototype._read = function(size) { - this.push('x'); - this.push(null); -}; - -function Write() { - stream.Writable.call(this); +class Write extends stream.Writable { + _write(buffer, encoding, cb) { + this.emit('error', new Error('boom')); + this.emit('alldone'); + } } -util.inherits(Write, stream.Writable); - -Write.prototype._write = function(buffer, encoding, cb) { - this.emit('error', new Error('boom')); - this.emit('alldone'); -}; const read = new Read(); const write = new Write(); diff --git a/test/parallel/test-stream2-set-encoding.js b/test/parallel/test-stream2-set-encoding.js index 45bc59a5250d02..4215b068888cb8 100644 --- a/test/parallel/test-stream2-set-encoding.js +++ b/test/parallel/test-stream2-set-encoding.js @@ -2,40 +2,37 @@ const common = require('../common'); const assert = require('assert'); const R = require('_stream_readable'); -const util = require('util'); -util.inherits(TestReader, R); - -function TestReader(n, opts) { - R.call(this, opts); - - this.pos = 0; - this.len = n || 100; +class TestReader extends R { + constructor(n, opts) { + super(opts); + this.pos = 0; + this.len = n || 100; + } + + _read(n) { + setTimeout(() => { + if (this.pos >= this.len) { + // double push(null) to test eos handling + this.push(null); + return this.push(null); + } + + n = Math.min(n, this.len - this.pos); + if (n <= 0) { + // double push(null) to test eos handling + this.push(null); + return this.push(null); + } + + this.pos += n; + const ret = Buffer.alloc(n, 'a'); + + return this.push(ret); + }, 1); + } } -TestReader.prototype._read = function(n) { - setTimeout(function() { - - if (this.pos >= this.len) { - // double push(null) to test eos handling - this.push(null); - return this.push(null); - } - - n = Math.min(n, this.len - this.pos); - if (n <= 0) { - // double push(null) to test eos handling - this.push(null); - return this.push(null); - } - - this.pos += n; - const ret = Buffer.alloc(n, 'a'); - - return this.push(ret); - }.bind(this), 1); -}; - { // Verify utf8 encoding const tr = new TestReader(100); diff --git a/test/parallel/test-stream2-unpipe-drain.js b/test/parallel/test-stream2-unpipe-drain.js index b546e269c8c272..a09705d4ab6e1f 100644 --- a/test/parallel/test-stream2-unpipe-drain.js +++ b/test/parallel/test-stream2-unpipe-drain.js @@ -3,30 +3,27 @@ require('../common'); const assert = require('assert'); const stream = require('stream'); -const util = require('util'); -function TestWriter() { - stream.Writable.call(this); +class TestWriter extends stream.Writable { + _write(buffer, encoding, callback) { + console.log('write called'); + // super slow write stream (callback never called) + } } -util.inherits(TestWriter, stream.Writable); - -TestWriter.prototype._write = function(buffer, encoding, callback) { - console.log('write called'); - // super slow write stream (callback never called) -}; const dest = new TestWriter(); -function TestReader(id) { - stream.Readable.call(this); - this.reads = 0; -} -util.inherits(TestReader, stream.Readable); +class TestReader extends stream.Readable { + constructor() { + super(); + this.reads = 0; + } -TestReader.prototype._read = function(size) { - this.reads += 1; - this.push(Buffer.alloc(size)); -}; + _read(size) { + this.reads += 1; + this.push(Buffer.alloc(size)); + } +} const src1 = new TestReader(); const src2 = new TestReader(); diff --git a/test/parallel/test-stream2-unpipe-leak.js b/test/parallel/test-stream2-unpipe-leak.js index 8800a2bbc68e45..5e98e9bc73807a 100644 --- a/test/parallel/test-stream2-unpipe-leak.js +++ b/test/parallel/test-stream2-unpipe-leak.js @@ -5,29 +5,27 @@ const stream = require('stream'); const chunk = Buffer.from('hallo'); -const util = require('util'); - -function TestWriter() { - stream.Writable.call(this); +class TestWriter extends stream.Writable { + _write(buffer, encoding, callback) { + callback(null); + } } -util.inherits(TestWriter, stream.Writable); - -TestWriter.prototype._write = function(buffer, encoding, callback) { - callback(null); -}; const dest = new TestWriter(); // Set this high so that we'd trigger a nextTick warning // and/or RangeError if we do maybeReadMore wrong. -function TestReader() { - stream.Readable.call(this, { highWaterMark: 0x10000 }); +class TestReader extends stream.Readable { + constructor() { + super({ + highWaterMark: 0x10000 + }); + } + + _read(size) { + this.push(chunk); + } } -util.inherits(TestReader, stream.Readable); - -TestReader.prototype._read = function(size) { - this.push(chunk); -}; const src = new TestReader(); diff --git a/test/parallel/test-stream2-writable.js b/test/parallel/test-stream2-writable.js index d244f06ef0a292..03282a27ad9c03 100644 --- a/test/parallel/test-stream2-writable.js +++ b/test/parallel/test-stream2-writable.js @@ -4,24 +4,23 @@ const W = require('_stream_writable'); const D = require('_stream_duplex'); const assert = require('assert'); -const util = require('util'); -util.inherits(TestWriter, W); - -function TestWriter() { - W.apply(this, arguments); - this.buffer = []; - this.written = 0; +class TestWriter extends W { + constructor(opts) { + super(opts); + this.buffer = []; + this.written = 0; + } + + _write(chunk, encoding, cb) { + // simulate a small unpredictable latency + setTimeout(() => { + this.buffer.push(chunk.toString()); + this.written += chunk.length; + cb(); + }, Math.floor(Math.random() * 10)); + } } -TestWriter.prototype._write = function(chunk, encoding, cb) { - // simulate a small unpredictable latency - setTimeout(function() { - this.buffer.push(chunk.toString()); - this.written += chunk.length; - cb(); - }.bind(this), Math.floor(Math.random() * 10)); -}; - const chunks = new Array(50); for (let i = 0; i < chunks.length; i++) { chunks[i] = new Array(i + 1).join('x'); diff --git a/test/parallel/test-util-format.js b/test/parallel/test-util-format.js index 96856de7bf1b5c..b88bf9754e42c8 100644 --- a/test/parallel/test-util-format.js +++ b/test/parallel/test-util-format.js @@ -68,15 +68,16 @@ assert.strictEqual(util.format('o: %j, a: %j'), 'o: %j, a: %j'); // Errors const err = new Error('foo'); assert.strictEqual(util.format(err), err.stack); -function CustomError(msg) { - Error.call(this); - Object.defineProperty(this, 'message', - { value: msg, enumerable: false }); - Object.defineProperty(this, 'name', - { value: 'CustomError', enumerable: false }); - Error.captureStackTrace(this, CustomError); +class CustomError extends Error { + constructor(msg) { + super(); + Object.defineProperty(this, 'message', + { value: msg, enumerable: false }); + Object.defineProperty(this, 'name', + { value: 'CustomError', enumerable: false }); + Error.captureStackTrace(this, CustomError); + } } -util.inherits(CustomError, Error); const customError = new CustomError('bar'); assert.strictEqual(util.format(customError), customError.stack); // Doesn't capture stack trace diff --git a/test/pummel/test-tls-server-large-request.js b/test/pummel/test-tls-server-large-request.js index e048defe39155c..c8140a3ef93ad0 100644 --- a/test/pummel/test-tls-server-large-request.js +++ b/test/pummel/test-tls-server-large-request.js @@ -7,7 +7,6 @@ const assert = require('assert'); const tls = require('tls'); const fs = require('fs'); const stream = require('stream'); -const util = require('util'); const request = Buffer.from(new Array(1024 * 256).join('ABCD')); // 1mb @@ -16,21 +15,22 @@ const options = { cert: fs.readFileSync(`${common.fixturesDir}/keys/agent1-cert.pem`) }; -function Mediator() { - stream.Writable.call(this); - this.buf = ''; -} -util.inherits(Mediator, stream.Writable); +class Mediator extends stream.Writable { + constructor() { + super(); + this.buf = ''; + } -Mediator.prototype._write = function _write(data, enc, cb) { - this.buf += data; - setTimeout(cb, 0); + _write(data, enc, cb) { + this.buf += data; + setTimeout(cb, 0); - if (this.buf.length >= request.length) { - assert.strictEqual(this.buf, request.toString()); - server.close(); + if (this.buf.length >= request.length) { + assert.strictEqual(this.buf, request.toString()); + server.close(); + } } -}; +} const mediator = new Mediator(); diff --git a/test/sequential/test-stream2-fs.js b/test/sequential/test-stream2-fs.js index ba35176a778f3b..bdb474422f49e3 100644 --- a/test/sequential/test-stream2-fs.js +++ b/test/sequential/test-stream2-fs.js @@ -13,28 +13,27 @@ const size = fs.statSync(file).size; const expectLengths = [1024]; -const util = require('util'); const Stream = require('stream'); -util.inherits(TestWriter, Stream); - -function TestWriter() { - Stream.apply(this); - this.buffer = []; - this.length = 0; +class TestWriter extends Stream { + constructor() { + super(); + this.buffer = []; + this.length = 0; + } + + write(c) { + this.buffer.push(c.toString()); + this.length += c.length; + return true; + } + + end(c) { + if (c) this.buffer.push(c.toString()); + this.emit('results', this.buffer); + } } -TestWriter.prototype.write = function(c) { - this.buffer.push(c.toString()); - this.length += c.length; - return true; -}; - -TestWriter.prototype.end = function(c) { - if (c) this.buffer.push(c.toString()); - this.emit('results', this.buffer); -}; - const r = new FSReadable(file); const w = new TestWriter();