UNPKG

process-tunnel

Version:
74 lines (73 loc) 3.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypedReadableStream = void 0; var queued_message_1 = require("./interface/queued-message"); var Dissolve = require('dissolve'); var TypedReadableStream = /** @class */ (function () { function TypedReadableStream(channel, queue) { var _this = this; this.stream = new Dissolve(); this.stream.loop(function () { _this.stream.int8('type') .tap(function () { switch (_this.stream.vars.type) { case queued_message_1.MessageType.ACK: { _this.stream.int32('length'); } break; case queued_message_1.MessageType.REQ: { _this.stream.string('callback', 36); _this.stream.int32('length'); } break; case queued_message_1.MessageType.ERR: { _this.stream.string('callback', 36); _this.stream.int32('length'); } break; } _this.stream.tap(function () { _this.stream.string('args', _this.stream.vars.length); }); }) .tap(function () { _this.stream.vars.args = JSON.parse(_this.stream.vars.args); _this.stream.push(_this.stream.vars); _this.stream.vars = {}; }); }); this.stream.on('readable', function () { var packet; while (packet = _this.stream.read()) { console.log(packet); switch (packet.type) { case queued_message_1.MessageType.ACK: { queue.approve(packet.args.type, packet.args.name, packet.args.prefix); } break; case queued_message_1.MessageType.REQ: { channel.emit('REQ', packet.callback, packet.args); } break; case queued_message_1.MessageType.ERR: { channel.emit('ERR', packet.callback, packet.args); } break; default: channel.destroy(new Error("Unhandled MessageType (" + packet.type + ")")); } } }); channel.pipe(this.stream); } TypedReadableStream.prototype.destroy = function () { this.stream.end(); }; return TypedReadableStream; }()); exports.TypedReadableStream = TypedReadableStream;