initial commit
This commit is contained in:
+34
@@ -0,0 +1,34 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _stream = require('stream');
|
||||
|
||||
// Discards "bytesToDiscard" bytes
|
||||
class DiscardStream extends _stream.Transform {
|
||||
constructor() {
|
||||
let bytesToDiscard = arguments.length <= 0 || arguments[0] === undefined ? 0 : arguments[0];
|
||||
|
||||
super();
|
||||
this.bytesToDiscard = bytesToDiscard;
|
||||
this.byteCount = 0;
|
||||
this.done = false;
|
||||
}
|
||||
_transform(chunk, encoding, cb) {
|
||||
if (this.done) return cb(null, chunk);
|
||||
|
||||
this.byteCount += chunk.length;
|
||||
|
||||
if (this.byteCount > this.bytesToDiscard) {
|
||||
this.done = true;
|
||||
const bytesToKeep = this.byteCount - this.bytesToDiscard;
|
||||
const firstChunk = chunk.slice(-bytesToKeep);
|
||||
return cb(null, firstChunk);
|
||||
}
|
||||
cb(null);
|
||||
}
|
||||
}
|
||||
|
||||
exports.default = DiscardStream;
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _stream = require('stream');
|
||||
|
||||
var _crypto = require('crypto');
|
||||
|
||||
class HashStream extends _stream.Transform {
|
||||
constructor(algo) {
|
||||
var _ref = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
||||
|
||||
var _ref$encoding = _ref.encoding;
|
||||
let encoding = _ref$encoding === undefined ? 'hex' : _ref$encoding;
|
||||
|
||||
super({ objectMode: true });
|
||||
this.algo = algo;
|
||||
this.digestEncoding = encoding;
|
||||
this.hashSum = (0, _crypto.createHash)(algo);
|
||||
}
|
||||
|
||||
_transform(chunk, encoding, cb) {
|
||||
// Update hash sum objects
|
||||
this.hashSum.update(chunk);
|
||||
cb();
|
||||
}
|
||||
|
||||
_flush(cb) {
|
||||
// Compute hash digest
|
||||
const digest = this.hashSum.digest(this.digestEncoding);
|
||||
// this.emit('hash', hash)
|
||||
this.push({ digest });
|
||||
cb(null);
|
||||
}
|
||||
}
|
||||
exports.default = HashStream;
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _stream = require('stream');
|
||||
|
||||
var _crypto = require('crypto');
|
||||
|
||||
class RandomStream extends _stream.Readable {
|
||||
constructor(max) {
|
||||
super();
|
||||
this.remainingBytes = max;
|
||||
}
|
||||
_read(requestedSize) {
|
||||
if (this.remainingBytes === 0) return this.push(null);
|
||||
|
||||
const size = Math.min(this.remainingBytes, requestedSize);
|
||||
this.remainingBytes -= size;
|
||||
|
||||
(0, _crypto.randomBytes)(size, (err, buf) => {
|
||||
if (err) return this.emit('error', err);
|
||||
this.push(buf);
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.default = RandomStream;
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _stream = require('stream');
|
||||
|
||||
class SizeStream extends _stream.Transform {
|
||||
constructor(cb) {
|
||||
super({ objectMode: true });
|
||||
this.streamSize = 0;
|
||||
this.cb = cb;
|
||||
}
|
||||
|
||||
_transform(chunk, encoding, cb) {
|
||||
if (this.cb) return this._transformCb(chunk, encoding, cb);
|
||||
// Update hash sum objects
|
||||
this.streamSize = this.streamSize + chunk.length;
|
||||
cb();
|
||||
}
|
||||
|
||||
_flush(cb) {
|
||||
if (this.cb) return this._flushCb(cb);
|
||||
const size = this.streamSize;
|
||||
this.push({ size });
|
||||
cb(null);
|
||||
}
|
||||
|
||||
_transformCb(chunk, encoding, cb) {
|
||||
this.streamSize = this.streamSize + chunk.length;
|
||||
cb(null, chunk);
|
||||
}
|
||||
_flushCb(cb) {
|
||||
const size = this.streamSize;
|
||||
this.cb(size);
|
||||
cb(null);
|
||||
}
|
||||
}
|
||||
exports.default = SizeStream;
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _HashStream = require('./HashStream');
|
||||
|
||||
Object.defineProperty(exports, 'HashStream', {
|
||||
enumerable: true,
|
||||
get: function get() {
|
||||
return _interopRequireDefault(_HashStream).default;
|
||||
}
|
||||
});
|
||||
|
||||
var _SizeStream = require('./SizeStream');
|
||||
|
||||
Object.defineProperty(exports, 'SizeStream', {
|
||||
enumerable: true,
|
||||
get: function get() {
|
||||
return _interopRequireDefault(_SizeStream).default;
|
||||
}
|
||||
});
|
||||
|
||||
var _RandomStream = require('./RandomStream');
|
||||
|
||||
Object.defineProperty(exports, 'RandomStream', {
|
||||
enumerable: true,
|
||||
get: function get() {
|
||||
return _interopRequireDefault(_RandomStream).default;
|
||||
}
|
||||
});
|
||||
|
||||
var _DiscardStream = require('./DiscardStream');
|
||||
|
||||
Object.defineProperty(exports, 'DiscardStream', {
|
||||
enumerable: true,
|
||||
get: function get() {
|
||||
return _interopRequireDefault(_DiscardStream).default;
|
||||
}
|
||||
});
|
||||
|
||||
var _meterstream = require('meterstream');
|
||||
|
||||
Object.defineProperty(exports, 'MeterStream', {
|
||||
enumerable: true,
|
||||
get: function get() {
|
||||
return _interopRequireDefault(_meterstream).default;
|
||||
}
|
||||
});
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
Reference in New Issue
Block a user