UNPKG

node-noise

Version:
199 lines 7.28 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createPbStream = exports.PbStreamImpl = void 0; var streams = __importStar(require("stream")); var uint8arraylist_1 = require("./uint8arraylist"); var encoder_1 = require("./encoder"); var duplexify_1 = __importDefault(require("duplexify")); var RingBuffer = /** @class */ (function () { function RingBuffer() { this.buffers = []; this.pendingReaders = []; } RingBuffer.prototype.push = function (data) { var pendingReader = this.pendingReaders.shift(); if (pendingReader) { pendingReader.resolve(data); } else { this.buffers.push(data); } }; RingBuffer.prototype.poll = function () { var _this = this; return new Promise(function (resolve, reject) { var buffered = _this.buffers.shift(); if (buffered) { resolve(buffered); } else { _this.pendingReaders.push({ resolve: resolve, reject: reject }); } }); }; RingBuffer.prototype.pop = function () { return this.buffers.shift(); }; RingBuffer.prototype.close = function (err) { var item; while (!!(item = this.pendingReaders.shift())) { item.reject(err); } }; return RingBuffer; }()); var PbStreamImpl = /** @class */ (function (_super) { __extends(PbStreamImpl, _super); function PbStreamImpl(options) { var _this = _super.call(this, __assign({ autoDestroy: true }, options)) || this; _this.ringBuffer = new RingBuffer(); _this.receiveBuffer = new uint8arraylist_1.Uint8ArrayList(); _this.unwrapped = null; _this.unwrappedOutbound = null; _this.unwrappedInbound = null; _this.nextWrite = null; return _this; } PbStreamImpl.prototype.writeLP = function (input) { this.push((0, encoder_1.uint16BEEncode)(input.length)); this.push(input); }; PbStreamImpl.prototype.readLP = function () { return this.ringBuffer.poll(); }; PbStreamImpl.prototype.unwrap = function () { var _this = this; var self = this; if (this.unwrapped) { return this.unwrapped; } this.unwrappedOutbound = new streams.Writable({ autoDestroy: true, write: function (chunk, encoding, callback) { var ret = self.push(chunk); if (ret) { callback(); } else { if (self.nextWrite) { callback(new Error('Writing')); } else { self.nextWrite = callback; } } return; } }); this.unwrappedInbound = new streams.PassThrough(); this.unwrapped = new duplexify_1.default(this.unwrappedOutbound, this.unwrappedInbound); var next = function (err) { if (err) { return; } var item = _this.ringBuffer.pop(); if (item) { _this.unwrappedInbound.write(item, next); } else { var bufferedLength = _this.receiveBuffer.length; if (bufferedLength > 0) { _this.unwrappedInbound.write(_this.receiveBuffer.slice(0, bufferedLength)); _this.receiveBuffer.consume(bufferedLength); } } }; next(); return this.unwrapped; }; PbStreamImpl.prototype._read = function (size) { var nextWrite = this.nextWrite; this.nextWrite = null; if (nextWrite) { nextWrite(); } }; PbStreamImpl.prototype._write = function (chunk, encoding, callback) { var _this = this; if (this.unwrapped) { this.unwrappedInbound.write(chunk, encoding, callback); return; } this.receiveBuffer.append(chunk); var next = function () { if (_this.receiveBuffer.length >= 2) { var length_1 = (0, encoder_1.uint16BEDecode)(_this.receiveBuffer); if (_this.receiveBuffer.length >= (2 + length_1)) { var data = _this.receiveBuffer.slice(2, 2 + length_1); _this.ringBuffer.push(data); _this.receiveBuffer.consume(2 + length_1); return next(); } } callback(); }; next(); }; return PbStreamImpl; }(streams.Duplex)); exports.PbStreamImpl = PbStreamImpl; function createPbStream() { return new PbStreamImpl(); } exports.createPbStream = createPbStream; //# sourceMappingURL=pb-stream.js.map