How to use the streamx.Duplex function in streamx

To help you get started, we’ve selected a few streamx examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github automerge / hypermerge / dist / Multiplex.js View on Github external
* automatically.
     */
    MsgType[MsgType["End"] = 2] = "End";
    /**
     * Signal to the other side that we are completely closing this channel and will not read or
     * write any more data.
     *
     * Message body is an empty Buffer.
     *
     */
    MsgType[MsgType["Destroy"] = 3] = "Destroy";
})(MsgType = exports.MsgType || (exports.MsgType = {}));
/**
 * Allows many Duplex streams to be sent over a single Duplex.
 */
class Multiplex extends streamx_1.Duplex {
    constructor() {
        super();
        /**
         * Called when a remote frame is decoded by SMC.
         */
        this.onReceivedMsg = (remoteId, type, data) => {
            switch (type) {
                case MsgType.Start: {
                    const name = data.toString();
                    const channel = this.getOrCreateChannel(name);
                    this.remoteChannels.set(remoteId, channel);
                    break;
                }
                case MsgType.Data:
                    this.getChannelByRemoteId(remoteId).push(data);
                    break;
github automerge / hypermerge / dist / Multiplex.js View on Github external
if (!channel)
            throw new Error(`Unknown remote channelId: ${id}`);
        return channel;
    }
    getOrCreateChannel(name) {
        return Misc_1.getOrCreate(this.channels, name, () => {
            const id = this.getNextId();
            return new Channel(name, id, (type, msg) => this.sendMsg(id, name, type, msg));
        });
    }
    getNextId() {
        return this.nextId++;
    }
}
exports.default = Multiplex;
class Channel extends streamx_1.Duplex {
    constructor(name, id, send) {
        super({
            highWaterMark: 0,
            mapWritable: (data) => (typeof data === 'string' ? Buffer.from(data) : data),
        });
        this.name = name;
        this.id = id;
        this.send = send;
        this.send(MsgType.Start, Buffer.from(this.name));
    }
    /**
     * Calls .end() and returns a promise that resolves when the channel is fully closed. Channels
     * are fully closed when both sides have called .end()
     */
    close() {
        return new Promise((res) => {

streamx

An iteration of the Node.js core streams with a series of improvements

MIT
Latest version published 2 months ago

Package Health Score

83 / 100
Full package analysis

Similar packages