UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

38 lines 1.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebSocketWritable = void 0; const stream_1 = require("stream"); const constants_1 = require("./constants"); class WebSocketWritable extends stream_1.Writable { constructor(ws, maxBuffer = constants_1.MAX_WS_BUFFER) { super(); this.ws = ws; this.maxBuffer = maxBuffer; } _write(chunk, encoding, callback) { // Send data over the WebSocket with a callback. this.ws.send(chunk, { fin: false, binary: true }, (err) => { if (err) { return callback(err); } // Check if the buffer is over the threshold. If so, poll until it drains if (this.ws.bufferedAmount > this.maxBuffer) { const interval = setInterval(() => { if (this.ws.bufferedAmount < this.maxBuffer) { clearInterval(interval); callback(); } }, 10); } else { callback(); } }); } _final(callback) { // Signal the end of transmission. this.ws.send(Buffer.from([]), { fin: true, binary: true }, callback); } } exports.WebSocketWritable = WebSocketWritable; //# sourceMappingURL=ws-writable.js.map