initial commit
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
||||
'use strict';
|
||||
|
||||
var _ = require('./');
|
||||
|
||||
var _2 = _interopRequireDefault(_);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
process.stdin.pipe(new _2.default(5)).on('error', err => {
|
||||
console.error('Read more than 5 bytes from stdin');
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
}).pipe(process.stdout); /* eslint-disable no-console */
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _stream = require('stream');
|
||||
|
||||
class OverflowError extends Error {}
|
||||
|
||||
class MeterSream extends _stream.Transform {
|
||||
constructor() {
|
||||
let maxSize = arguments.length <= 0 || arguments[0] === undefined ? Infinity : arguments[0];
|
||||
|
||||
super();
|
||||
this.maxSize = maxSize;
|
||||
this.byteCount = 0;
|
||||
}
|
||||
_transform(chunk, encoding, cb) {
|
||||
// if this is called, _flush isn't called
|
||||
if (this.err) return cb(this.err);
|
||||
if (this.maxSize === Infinity) return cb(null, chunk);
|
||||
this.byteCount += chunk.length;
|
||||
if (this.byteCount > this.maxSize) {
|
||||
const remainingBytes = this.maxSize - this.byteCount;
|
||||
const lastChunk = chunk.slice(0, remainingBytes);
|
||||
this.err = new OverflowError(`Stream exceeded specified max of ${ this.maxSize } bytes.`);
|
||||
// flush remaining chunk and wait until next
|
||||
// _transform or _flush call to emit error
|
||||
return cb(null, lastChunk);
|
||||
}
|
||||
cb(null, chunk);
|
||||
}
|
||||
// if error detected on last call to _transform
|
||||
// flush will be called with the error
|
||||
_flush(cb) {
|
||||
cb(this.err);
|
||||
}
|
||||
}
|
||||
MeterSream.OverflowError = OverflowError;
|
||||
|
||||
exports.default = MeterSream;
|
||||
Reference in New Issue
Block a user