replace progress-stream with our implementation

upstream is mostly unmaintained
This commit is contained in:
Girish Ramakrishnan
2022-11-06 10:17:14 +01:00
parent 5af1bbfb3c
commit 962d7030bb
5 changed files with 50 additions and 69 deletions
+3 -3
View File
@@ -14,7 +14,7 @@ const assert = require('assert'),
{ DecryptStream, EncryptStream } = require('../hush.js'),
once = require('../once.js'),
path = require('path'),
progressStream = require('progress-stream'),
ProgressStream = require('../progress-stream.js'),
storage = require('../storage.js'),
tar = require('tar-fs'),
zlib = require('zlib');
@@ -51,7 +51,7 @@ function tarPack(dataLayout, encryption) {
});
const gzip = zlib.createGzip({});
const ps = progressStream({ time: 10000 }); // emit 'progress' every 10 seconds
const ps = new ProgressStream({ interval: 10000 }); // emit 'progress' every 10 seconds
pack.on('error', function (error) {
debug('tarPack: tar stream error.', error);
@@ -84,7 +84,7 @@ function tarExtract(inStream, dataLayout, encryption) {
assert.strictEqual(typeof encryption, 'object');
const gunzip = zlib.createGunzip({});
const ps = progressStream({ time: 10000 }); // display a progress every 10 seconds
const ps = new ProgressStream({ interval: 10000 }); // display a progress every 10 seconds
const extract = tar.extract('/', {
map: function (header) {
header.name = dataLayout.toLocalPath(header.name);
+3 -3
View File
@@ -5,7 +5,7 @@ const assert = require('assert'),
crypto = require('crypto'),
debug = require('debug')('box:hush'),
fs = require('fs'),
progressStream = require('progress-stream'),
ProgressStream = require('./progress-stream.js'),
TransformStream = require('stream').Transform;
class EncryptStream extends TransformStream {
@@ -157,7 +157,7 @@ function createReadStream(sourceFile, encryption) {
assert.strictEqual(typeof encryption, 'object');
const stream = fs.createReadStream(sourceFile);
const ps = progressStream({ time: 10000 }); // display a progress every 10 seconds
const ps = new ProgressStream({ interval: 10000 }); // display a progress every 10 seconds
stream.on('error', function (error) {
debug(`createReadStream: read stream error at ${sourceFile}`, error);
@@ -185,7 +185,7 @@ function createWriteStream(destFile, encryption) {
assert.strictEqual(typeof encryption, 'object');
const stream = fs.createWriteStream(destFile);
const ps = progressStream({ time: 10000 }); // display a progress every 10 seconds
const ps = new ProgressStream({ interval: 10000 }); // display a progress every 10 seconds
stream.on('error', function (error) {
debug(`createWriteStream: write stream error ${destFile}`, error);
+44
View File
@@ -0,0 +1,44 @@
'use strict';
exports = module.exports = ProgressStream;
const stream = require('stream'),
TransformStream = stream.Transform;
class ProgressStream extends TransformStream {
constructor(options) {
super();
this._options = Object.assign({ interval: 10 * 1000 }, options);
this._transferred = 0;
this._delta = 0;
this._started = false;
this._startTime = null;
this._interval = null;
}
_start() {
this._startTime = Date.now();
this._started = true;
this._interval = setInterval(() => {
const speed = this._delta * 1000 / this._options.interval;
this._delta = 0;
this.emit('progress', { speed, transferred: this._transferred });
}, this._options.interval);
}
_stop() {
clearInterval(this._interval);
}
_transform(chunk, encoding, callback) {
if (!this._started) this._start();
this._transferred += chunk.length;
this._delta += chunk.length;
callback(null, chunk);
}
_flush(callback) {
this._stop();
callback(null);
}
}