'use strict';

var node_stream = require('node:stream');

/**
 * Simple writable buffer stream
 * @docs: https://nodejs.org/api/stream.html#stream_writable_streams
 */
class WritableBufferStream extends node_stream.Writable {
    _chunks;
    constructor(options) {
        super(options);
        this._chunks = [];
    }
    _write(chunk, encoding, callback) {
        this._chunks.push(chunk);
        callback();
    }
    _destroy(error, callback) {
        this._chunks = [];
        callback(error);
    }
    toBuffer() {
        return Buffer.concat(this._chunks);
    }
}
function streamToPromise(stream) {
    return new Promise((resolve, reject) => {
        stream.once('finish', () => resolve(stream.toBuffer()));
        stream.once('error', reject);
    });
}

exports.WritableBufferStream = WritableBufferStream;
exports.streamToPromise = streamToPromise;
