"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const stream_1 = require("stream");
const cbor = require("cbor");
const encoders = {
    'cbor-definite': cbor.encode,
    json: JSON.stringify,
};
class ConfigurableEncode extends stream_1.Transform {
    constructor(encoder = 'json') {
        super({ writableObjectMode: true });
        this.encoder = encoder;
    }
    canAcceptRawBuffers() {
        return this.encoder === 'cbor-definite';
    }
    setEncoder(encoder) {
        this.encoder = encoder;
    }
    _transform(chunk, encoding, callback) {
        let encodedChunk;
        try {
            encodedChunk = encoders[this.encoder](chunk);
        }
        catch (e) {
            callback(e);
            return;
        }
        callback(undefined, encodedChunk);
    }
}
exports.default = ConfigurableEncode;
//# sourceMappingURL=ConfigurableEncode.js.map