2022-03-01 01:52:56 -06:00
|
|
|
// eslint-disable-next-line max-classes-per-file
|
2024-01-16 00:28:20 -06:00
|
|
|
const JsonParse = require('jsonparse');
|
|
|
|
const {Transform} = require('stream');
|
2022-03-01 01:52:56 -06:00
|
|
|
|
2024-01-16 00:28:20 -06:00
|
|
|
exports.JsonStream = class JsonStream extends Transform {
|
2022-03-01 01:52:56 -06:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
const self = this;
|
|
|
|
this.done = undefined;
|
|
|
|
this.parser = new JsonParse();
|
|
|
|
this.parser.onValue = function onValue(O) {
|
|
|
|
if (0 === this.stack.length) {
|
|
|
|
self.push(JSON.stringify(O));
|
|
|
|
self.done();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
_transform(chunk, encoding, done) {
|
|
|
|
this.done = done;
|
|
|
|
this.parser.write(chunk);
|
|
|
|
}
|
|
|
|
|
2024-01-16 00:28:20 -06:00
|
|
|
};
|
2022-03-01 01:52:56 -06:00
|
|
|
|
2024-01-16 00:28:20 -06:00
|
|
|
exports.JsonStream.PrettyPrint = class extends Transform {
|
2022-03-01 01:52:56 -06:00
|
|
|
|
|
|
|
constructor(indent = 2) {
|
|
|
|
super();
|
|
|
|
this.indent = indent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
async _transform(chunk, encoding, done) {
|
|
|
|
this.push(JSON.stringify(JSON.parse(chunk), null, this.indent));
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2024-01-16 00:28:20 -06:00
|
|
|
exports.transform = (fn, opts = {}) => {
|
2022-03-01 01:52:56 -06:00
|
|
|
class EasyTransform extends Transform {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-underscore-dangle, class-methods-use-this
|
|
|
|
_transform(chunk, encoding, done) {
|
|
|
|
fn(chunk, encoding, done, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return new EasyTransform();
|
|
|
|
};
|