"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeQueryStream = void 0;
const stream_1 = require("stream");
const apiStream_1 = require("./apiStream");
class NodeQueryStream extends stream_1.Readable {
    constructor(queries, handler, args) {
        super({ objectMode: true });
        this.queries = queries;
        this.handler = handler;
        this.transform = args?.transform; // Function to transform results data
    }
    async _read() {
        try {
            if (this.queries.length === 0) {
                this.push(null); // No more data, end the stream
                return;
            }
            // Retrieve and remove the first query from the list
            const query = this.queries.shift();
            // Get node response
            let data = await this.handler(query);
            // Transform data before pushing
            if (this.transform) {
                data = this.transform(data);
            }
            if (typeof data !== 'string') {
                data = JSON.stringify(data);
            }
            this.push(data); // Push the result of the handler into the stream
        }
        catch (error) {
            this.emit('error', error); // Emit error event in case of failure
        }
    }
    static onStream(stream, req, res) {
        return apiStream_1.ExternalApiStream.onStream(stream, req, res);
    }
    static mergeStreams(streams, destination) {
        return apiStream_1.ExternalApiStream.mergeStreams(streams, destination);
    }
}
exports.NodeQueryStream = NodeQueryStream;
//# sourceMappingURL=nodeStream.js.map