flecks/packages/core/build/stream.js
2024-02-14 06:42:46 -06:00

97 lines
2.2 KiB
JavaScript

// eslint-disable-next-line max-classes-per-file, import/no-extraneous-dependencies
const {Buffer} = require('buffer');
const {dump: dumpYml, load: loadYml} = require('js-yaml');
const JsonParse = require('jsonparse');
const {Transform, Writable} = require('stream');
exports.JsonStream = class JsonStream extends Transform {
constructor(decorator) {
super();
this.parser = new JsonParse();
const self = this;
this.parser.onValue = function onValue(O) {
if (0 === this.stack.length) {
self.transformed = JSON.stringify(decorator(O));
}
};
this.transformed = undefined;
}
// eslint-disable-next-line no-underscore-dangle
_flush(done) {
this.push(this.transformed);
done();
}
// eslint-disable-next-line no-underscore-dangle
_transform(chunk, encoding, done) {
this.parser.write(chunk);
done();
}
};
exports.JsonStream.PrettyPrint = class extends exports.JsonStream {
constructor(decorator, indent = 2) {
super(decorator);
const self = this;
this.parser.onValue = function onValue(O) {
if (0 === this.stack.length) {
self.transformed = JSON.stringify(O, null, indent);
}
};
}
};
exports.pipesink = (stream) => {
class Sink extends Writable {
constructor() {
super();
this.buffers = [];
}
// eslint-disable-next-line no-underscore-dangle
_write(chunk, encoding, done) {
this.buffers.push(chunk);
done();
}
}
const sink = new Sink();
return new Promise((resolve, reject) => {
stream.pipe(sink);
stream.on('error', reject);
stream.on('end', () => {
resolve(Buffer.concat(sink.buffers));
});
});
};
exports.YamlStream = class YamlStream extends Transform {
constructor(decorator, options = {dump: {}, load: {}}) {
super();
this.buffers = [];
this.decorator = decorator;
this.options = options;
}
// eslint-disable-next-line no-underscore-dangle
_flush(done) {
const yml = loadYml(Buffer.concat(this.buffers).toString(), this.options.load);
this.push(dumpYml(this.decorator(yml), this.options.dump));
done();
}
// eslint-disable-next-line no-underscore-dangle
_transform(chunk, encoding, done) {
this.buffers.push(chunk);
done();
}
};