All files / lib/wrapper duplex.js

64.29% Statements 18/28
30% Branches 6/20
40% Functions 2/5
65.38% Lines 17/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 459x 9x 9x               1x 1x 1x 1x 1x 2x     2x   1x 1x 1x             1x                 1x 1x         9x
const {Duplex} = require('stream');
const {DataWrapper} = require('./data_wrapper.js');
const {Manager} = require('../manager/manager.js');
 
/**
 * Wrapper for _Duplex_ streams that knows what to do with {@link DataWrapper} messages.
 * @extends Duplex
 */
class DuplexWrapper extends Duplex {
    constructor(options) {
        options = {...options, readableObjectMode: true, writableObjectMode: true};
        super(options);
        this.options = options;
        this.__real__push = this.push;
        this.push = (payload) => {
            Iif(payload != null && this.goal) {
                payload = payload instanceof DataWrapper?payload.getChild(this):new DataWrapper(payload, this);
            }
            this.__real__push(payload);
        };
        Eif(this._write) {
            this.__real__write = this._write;
            this._write = (payload, encoding, cb) => {
                if(this.goal) {
                    payload = payload instanceof DataWrapper?payload.getChild(this):new DataWrapper(payload, this);
                }
                this.__real__write(payload, encoding, cb);
            };
        }
        Iif(this._writev) {
            this.__real__writev = this._writev;
            this._writev = (payloads, cb) => {
                if(this.goal) {
                    payloads = payloads.forEach(payload=>payload.chunk = payload.chunk instanceof DataWrapper?payload.chunk.getChild(this):new DataWrapper(payload.chunk, this));
                }
                this.__real__writev(payloads, cb);
            };
        }
        this.type = 'Duplex';
        Eif(this.options.name) Manager.set(this.type, this);
    }
 
}
 
module.exports.DuplexWrapper = DuplexWrapper;