Skip to content

Commit

Permalink
refactor: use const, let
Browse files Browse the repository at this point in the history
BREAKING CHANGES: `const`, `let` not supported in node < 6
  • Loading branch information
fent committed Sep 24, 2018
1 parent 3dc04d5 commit 30f2fe6
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 73 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ A simple way to keep track of the speed of your readable streams.
```js
const StreamSpeed = require('streamspeed');

var rs = fs.createReadStream('somefile.avi');
var ss = new StreamSpeed();
let rs = fs.createReadStream('somefile.avi');
let ss = new StreamSpeed();
ss.add(rs);

// Listen for events emitted by streamspeed on the given stream.
Expand All @@ -24,7 +24,7 @@ ss.on('speed', (speed, avgSpeed) => {
Keep track of even a group of streams easily.

```js
var group = new Streamspeed();
let group = new Streamspeed();
group.add(stream1);
group.add(stream2);
group.add(stream3);
Expand Down Expand Up @@ -65,8 +65,8 @@ StreamSpeed.toHuman(1024 * 1024 * 20.5, 's') => 20.5MB/s
```

### Event: 'speed'
* `Number` - Speed at which streams in the group are being read.
* `Number` - Average speed.
* `number` - Speed at which streams in the group are being read.
* `number` - Average speed.

Will be emitted after the second time a stream is read and only if there is a change in speed.

Expand Down
40 changes: 18 additions & 22 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = class StreamSpeed extends EventEmitter {
*
* @constructor
* @extends {EventEmitter}
* @param {Number} per The time unit speed will be measured in.
* @param {number} per The time unit speed will be measured in.
*/
constructor(per) {
super();
Expand All @@ -24,8 +24,8 @@ module.exports = class StreamSpeed extends EventEmitter {
* Updates the group with the latest change in speed.
*
* @param {Object} meta
* @param {Number} speed
* @param {Number} avg
* @param {number} speed
* @param {number} avg
*/
_update(meta, speed, avg) {
meta.speed = speed;
Expand Down Expand Up @@ -62,38 +62,38 @@ module.exports = class StreamSpeed extends EventEmitter {
/**
* Add stream to group.
*
* @param {Stream} stream
* @param {Readable} stream
*/
add(origstream) {
// Check if stream is already in group.
if (this._streams.some(m => m.stream === origstream)) {
throw Error('Stream already in group');
}
var onReadable = () => {
var data = stream.read();

const onReadable = () => {
const data = stream.read();
if (data) {
reader.update(data, onUpdate);
}
};

var cleanup = () => {
const cleanup = () => {
stream.removeListener('readable', onReadable);
stream.removeListener('end', cleanup);
origstream.removeListener('error', cleanup);
this._streams.splice(this._streams.indexOf(meta), 1);
};

var meta = {
const meta = {
stream : origstream,
speed : 0,
avg : 0,
cleanup,
};
this._streams.push(meta);
var reader = new Speedometer(this.per);
var stream = origstream.pipe(new PassThrough());
var onUpdate = this._update.bind(this, meta);
const reader = new Speedometer(this.per);
const stream = origstream.pipe(new PassThrough());
const onUpdate = this._update.bind(this, meta);

stream.on('readable', onReadable);
stream.on('end', cleanup);
Expand All @@ -108,14 +108,10 @@ module.exports = class StreamSpeed extends EventEmitter {
*/
remove(stream) {
// Check if stream is in group.
var meta;
if (!this._streams.some(function(m) {
meta = m;
return m.stream === stream;
})) {
const meta = this._streams.find(m => m.stream === stream);
if (!meta) {
throw Error('Stream not found in group');
}

meta.cleanup();
}

Expand All @@ -124,14 +120,14 @@ module.exports = class StreamSpeed extends EventEmitter {
* Converts bytes to human readable unit.
* Thank you Amir from StackOverflow.
*
* @param {Number} bytes
* @return {String}
* @param {number} bytes
* @return {string}
*/
static toHuman(bytes, timeUnit) {
var units = ' KMGTPEZYXWVU';
const units = ' KMGTPEZYXWVU';
if (bytes <= 0) { return '0'; }
timeUnit = timeUnit ? '/' + timeUnit : '';
var t2 = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), 12);
const t2 = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), 12);
return (Math.round(bytes * 100 / Math.pow(1024, t2)) / 100) +
units.charAt(t2).replace(' ', '') + 'B' + timeUnit;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/speedometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module.exports = class Speedometer {
/**
* Helps count the number of bytes per data event in streams.
*
* @param {Number} per
* @constructor
* @param {number} per
*/
constructor(per) {
this.per = per;
Expand All @@ -18,11 +18,11 @@ module.exports = class Speedometer {
/**
* Called when new data arrives.
*
* @param {Buffer|String} data
* @param {Buffer|string} data
* @param {Function} callback
*/
update(data, callback) {
var now = Date.now();
const now = Date.now();

// Check if this is the first data event.
if (this.lasttime === null) {
Expand All @@ -31,15 +31,15 @@ module.exports = class Speedometer {
}

// Compare now to last time.
var speed = Math.round((data.length / (now - this.lasttime)) *
const speed = Math.round((data.length / (now - this.lasttime)) *
this.per);
this.lasttime = now;

// Get average speed.
this.total += speed;
var avg = Math.round(this.total / ++this.iteration);
const avg = Math.round(this.total / ++this.iteration);

var change = this.speed !== speed || this.avg !== avg;
const change = this.speed !== speed || this.avg !== avg;
this.speed = speed;
this.avg = avg;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"sinon": "^6.0.0"
},
"engines": {
"node": ">=4"
"node": ">=6"
},
"license": "MIT"
}
14 changes: 6 additions & 8 deletions test/group-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ const sinon = require('sinon');


describe('Create a group and write to it', () => {
var group = new StreamSpeed();
var s1 = new MockStream();
var s2 = new MockStream();
var s3 = new MockStream();
const group = new StreamSpeed();
const s1 = new MockStream();
const s2 = new MockStream();
const s3 = new MockStream();

group.add(s1);
group.add(s2);
group.add(s3);

var spy = sinon.spy();
const spy = sinon.spy();
group.on('speed', spy);

it('All streams added show up', () => {
Expand All @@ -30,11 +30,9 @@ describe('Create a group and write to it', () => {
});

it('Should emit `speed` event once for each stream', (done) => {
// read at 500 B/s on 2 streams, so 1000 B/s.
// Read at 500 B/s on 2 streams, 1000 B/s in total.
s1.interval(100, 6, 200, true, true);
s2.interval(100, 6, 200, true);
setTimeout(() => {
}, 1200);
s2.on('finish', () => {
assert.ok(spy.firstCall.calledWith(500, 500));
assert.ok(spy.secondCall.calledWith(1000, 1000));
Expand Down
18 changes: 9 additions & 9 deletions test/mockstream.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const PassThrough = require('stream').PassThrough;
const sinon = require('sinon');

var clock;
let clock;
before(() => { clock = sinon.useFakeTimers(); });
after(() => { clock.restore(); });

Expand All @@ -21,17 +21,17 @@ module.exports = class Mock extends PassThrough {
/**
* Runs the function run n times every interval.
*
* @param {Number} length
* @param {Number} n
* @param {Number} interval
* @param {Boolean} end
* @param {Boolean} skipTick
* @param {number} length
* @param {number} n
* @param {number} interval
* @param {boolean} end
* @param {boolean} skipTick
*/
interval(length, n, interval, end, skipTick) {
var callback = this.end.bind(this);
var i = 0;
const callback = this.end.bind(this);
let i = 0;

var iid = setInterval(() => {
const iid = setInterval(() => {
this.write(Buffer.alloc(length));
if (++i === n) {
clearInterval(iid);
Expand Down
13 changes: 6 additions & 7 deletions test/remove-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const sinon = require('sinon');


describe('Immediately remove a stream', () => {
var ss = new StreamSpeed();
var s = new PassThrough();
const ss = new StreamSpeed();
const s = new PassThrough();
ss.add(s);
ss.remove(s);

// Listen for things.
var spy = sinon.spy();
const spy = sinon.spy();
s.on('speed', spy);

it('Does not emit any events', (done) => {
Expand All @@ -31,17 +31,16 @@ describe('Immediately remove a stream', () => {


describe('Unwatch after several writes', () => {
var ss = new StreamSpeed(1);
var s = new MockStream();
const ss = new StreamSpeed(1);
const s = new MockStream();
ss.add(s);

var spy = sinon.spy();
const spy = sinon.spy();
ss.on('speed', spy);

it('Emits no events after calling remove', (done) => {
// Write at 1 bps for 0.5 seconds.
s.interval(100, 5, 100, true);

s.on('finish', done);
});

Expand Down
30 changes: 15 additions & 15 deletions test/single-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const sinon = require('sinon');

describe('Read from a stream', () => {
describe('with a unit', () => {
var ss = new StreamSpeed(1);
var rs = new MockStream();
const ss = new StreamSpeed(1);
const rs = new MockStream();
ss.add(rs);

var spy = sinon.spy();
const spy = sinon.spy();
ss.on('speed', spy);

it('Emitted one speed event', (done) => {
Expand All @@ -32,11 +32,11 @@ describe('Read from a stream', () => {
});

describe('with no unit', () => {
var ss = new StreamSpeed();
var rs = new MockStream();
const ss = new StreamSpeed();
const rs = new MockStream();
ss.add(rs);

var spy = sinon.spy();
const spy = sinon.spy();
ss.on('speed', spy);

it('Emited one speed event', (done) => {
Expand All @@ -55,11 +55,11 @@ describe('Read from a stream', () => {
});

describe('Written to at the rate of the unit', () => {
var ss = new StreamSpeed(100);
var rs = new MockStream();
const ss = new StreamSpeed(100);
const rs = new MockStream();
ss.add(rs);

var spy = sinon.spy();
const spy = sinon.spy();
ss.on('speed', spy);

it('Speed and avg speed are constant', (done) => {
Expand All @@ -74,11 +74,11 @@ describe('Read from a stream', () => {
});

describe('Read when stream speed is sporadic', () => {
var ss = new StreamSpeed();
var rs = new MockStream();
const ss = new StreamSpeed();
const rs = new MockStream();
ss.add(rs);

var spy = sinon.spy();
const spy = sinon.spy();
ss.on('speed', spy);

it('Speed and avg speed changes', () => {
Expand All @@ -95,11 +95,11 @@ describe('Read when stream speed is sporadic', () => {

describe('Stream being monitored has an error', () => {
it('Stream gets removed', () => {
var ss = new StreamSpeed();
var rs = new PassThrough();
const ss = new StreamSpeed();
const rs = new PassThrough();
rs.on('error', () => {});
ss.add(rs);
rs.emit('error', new Error('my error'));
rs.emit('error', Error('my error'));
assert.equal(ss.getStreams().length, 0);
});
});

0 comments on commit 30f2fe6

Please sign in to comment.