fluent-object-stream
Version:
Facilitate transformation on nodeJS streams
47 lines (46 loc) • 1.65 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTransform = void 0;
const stream_1 = require("stream");
const stream_error_1 = __importDefault(require("./stream-error"));
/**
* Utility function to create {@link Transform} stream. It handles error to propagate them.
*
* @param objectTransform {@link ObjectTransform} representing the transformation to apply.
* @param options options to create the {@link Transform} stream.
* @return the created {@link Transform} stream.
*/
function createTransform(objectTransform, options) {
return new stream_1.Transform({
objectMode: true,
highWaterMark: options?.highWaterMark,
transform: async function (value, encoding, callback) {
try {
await objectTransform.transformElement(value, (data) => this.push(data));
callback();
}
catch (e) {
if (e instanceof Error)
callback(e);
else
callback(new stream_error_1.default(e));
}
},
flush(callback) {
try {
objectTransform.onEnd?.((data) => this.push(data));
callback();
}
catch (e) {
if (e instanceof Error)
callback(e);
else
callback(new stream_error_1.default(e));
}
},
});
}
exports.createTransform = createTransform;