UNPKG

9.94 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7var _bl = _interopRequireDefault(require("bl"));
8var _stream = require("stream");
9var _packet = require("./packet");
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11class OutgoingMessageStream extends _stream.Duplex {
12 constructor(debug, {
13 packetSize
14 }) {
15 super({
16 writableObjectMode: true
17 });
18 this.packetSize = packetSize;
19 this.debug = debug;
20 this.bl = new _bl.default();
21
22 // When the writable side is ended, push `null`
23 // to also end the readable side.
24 this.on('finish', () => {
25 this.push(null);
26 });
27 }
28 _write(message, _encoding, callback) {
29 const length = this.packetSize - _packet.HEADER_LENGTH;
30 let packetNumber = 0;
31 this.currentMessage = message;
32 this.currentMessage.on('data', data => {
33 if (message.ignore) {
34 return;
35 }
36 this.bl.append(data);
37 while (this.bl.length > length) {
38 const data = this.bl.slice(0, length);
39 this.bl.consume(length);
40
41 // TODO: Get rid of creating `Packet` instances here.
42 const packet = new _packet.Packet(message.type);
43 packet.packetId(packetNumber += 1);
44 packet.resetConnection(message.resetConnection);
45 packet.addData(data);
46 this.debug.packet('Sent', packet);
47 this.debug.data(packet);
48 if (this.push(packet.buffer) === false) {
49 message.pause();
50 }
51 }
52 });
53 this.currentMessage.on('end', () => {
54 const data = this.bl.slice();
55 this.bl.consume(data.length);
56
57 // TODO: Get rid of creating `Packet` instances here.
58 const packet = new _packet.Packet(message.type);
59 packet.packetId(packetNumber += 1);
60 packet.resetConnection(message.resetConnection);
61 packet.last(true);
62 packet.ignore(message.ignore);
63 packet.addData(data);
64 this.debug.packet('Sent', packet);
65 this.debug.data(packet);
66 this.push(packet.buffer);
67 this.currentMessage = undefined;
68 callback();
69 });
70 }
71 _read(_size) {
72 // If we do have a message, resume it and get data flowing.
73 // Otherwise, there is nothing to do.
74 if (this.currentMessage) {
75 this.currentMessage.resume();
76 }
77 }
78}
79var _default = exports.default = OutgoingMessageStream;
80module.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