1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.receiveOne = receiveOne;
|
7 | exports.receiveAndParse = receiveAndParse;
|
8 |
|
9 | var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
10 |
|
11 | var _message_decoder = require("./protobuf/message_decoder.js");
|
12 |
|
13 | var _protobufjsOldFixedWebpack = require("protobufjs-old-fixed-webpack");
|
14 |
|
15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
16 |
|
17 | function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
18 |
|
19 | function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
20 |
|
21 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
22 |
|
23 | function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
24 |
|
25 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
26 |
|
27 | var MESSAGE_HEADER_BYTE = 0x23;
|
28 |
|
29 | var PartiallyParsedInput =
|
30 |
|
31 | function () {
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function PartiallyParsedInput(typeNumber, length) {
|
37 | _classCallCheck(this, PartiallyParsedInput);
|
38 |
|
39 | this.typeNumber = typeNumber;
|
40 | this.expectedLength = length;
|
41 | this.buffer = new _protobufjsOldFixedWebpack.ByteBuffer(length);
|
42 | }
|
43 |
|
44 | _createClass(PartiallyParsedInput, [{
|
45 | key: "isDone",
|
46 | value: function isDone() {
|
47 | return this.buffer.offset >= this.expectedLength;
|
48 | }
|
49 | }, {
|
50 | key: "append",
|
51 | value: function append(buffer) {
|
52 | this.buffer.append(buffer);
|
53 | }
|
54 | }, {
|
55 | key: "arrayBuffer",
|
56 | value: function arrayBuffer() {
|
57 | var byteBuffer = this.buffer;
|
58 | byteBuffer.reset();
|
59 | return byteBuffer.toArrayBuffer();
|
60 | }
|
61 | }]);
|
62 |
|
63 | return PartiallyParsedInput;
|
64 | }();
|
65 |
|
66 |
|
67 | function parseFirstInput(bytes) {
|
68 |
|
69 | var byteBuffer = _protobufjsOldFixedWebpack.ByteBuffer.concat([bytes]);
|
70 |
|
71 |
|
72 | var sharp1 = byteBuffer.readByte();
|
73 | var sharp2 = byteBuffer.readByte();
|
74 |
|
75 | if (sharp1 !== MESSAGE_HEADER_BYTE || sharp2 !== MESSAGE_HEADER_BYTE) {
|
76 | throw new Error("Didn't receive expected header signature.");
|
77 | }
|
78 |
|
79 |
|
80 | var type = byteBuffer.readUint16();
|
81 | var length = byteBuffer.readUint32();
|
82 |
|
83 | var res = new PartiallyParsedInput(type, length);
|
84 | res.append(byteBuffer);
|
85 | return res;
|
86 | }
|
87 |
|
88 |
|
89 |
|
90 | function receiveRest(_x, _x2) {
|
91 | return _receiveRest.apply(this, arguments);
|
92 | }
|
93 |
|
94 |
|
95 | function _receiveRest() {
|
96 | _receiveRest = _asyncToGenerator(
|
97 |
|
98 | _regenerator["default"].mark(function _callee(parsedInput, receiver) {
|
99 | var data;
|
100 | return _regenerator["default"].wrap(function _callee$(_context) {
|
101 | while (1) {
|
102 | switch (_context.prev = _context.next) {
|
103 | case 0:
|
104 | if (!parsedInput.isDone()) {
|
105 | _context.next = 2;
|
106 | break;
|
107 | }
|
108 |
|
109 | return _context.abrupt("return");
|
110 |
|
111 | case 2:
|
112 | _context.next = 4;
|
113 | return receiver();
|
114 |
|
115 | case 4:
|
116 | data = _context.sent;
|
117 |
|
118 | if (!(data == null)) {
|
119 | _context.next = 7;
|
120 | break;
|
121 | }
|
122 |
|
123 | throw new Error("Received no data.");
|
124 |
|
125 | case 7:
|
126 | parsedInput.append(data);
|
127 | return _context.abrupt("return", receiveRest(parsedInput, receiver));
|
128 |
|
129 | case 9:
|
130 | case "end":
|
131 | return _context.stop();
|
132 | }
|
133 | }
|
134 | }, _callee);
|
135 | }));
|
136 | return _receiveRest.apply(this, arguments);
|
137 | }
|
138 |
|
139 | function receiveBuffer(_x3) {
|
140 | return _receiveBuffer.apply(this, arguments);
|
141 | }
|
142 |
|
143 | function _receiveBuffer() {
|
144 | _receiveBuffer = _asyncToGenerator(
|
145 |
|
146 | _regenerator["default"].mark(function _callee2(receiver) {
|
147 | var data, partialInput;
|
148 | return _regenerator["default"].wrap(function _callee2$(_context2) {
|
149 | while (1) {
|
150 | switch (_context2.prev = _context2.next) {
|
151 | case 0:
|
152 | _context2.next = 2;
|
153 | return receiver();
|
154 |
|
155 | case 2:
|
156 | data = _context2.sent;
|
157 | partialInput = parseFirstInput(data);
|
158 | _context2.next = 6;
|
159 | return receiveRest(partialInput, receiver);
|
160 |
|
161 | case 6:
|
162 | return _context2.abrupt("return", partialInput);
|
163 |
|
164 | case 7:
|
165 | case "end":
|
166 | return _context2.stop();
|
167 | }
|
168 | }
|
169 | }, _callee2);
|
170 | }));
|
171 | return _receiveBuffer.apply(this, arguments);
|
172 | }
|
173 |
|
174 | function receiveOne(messages, data) {
|
175 | var byteBuffer = _protobufjsOldFixedWebpack.ByteBuffer.concat([data]);
|
176 |
|
177 | var typeId = byteBuffer.readUint16();
|
178 | byteBuffer.readUint32();
|
179 |
|
180 | var decoder = new _message_decoder.MessageDecoder(messages, typeId, byteBuffer.toArrayBuffer());
|
181 | return {
|
182 | message: decoder.decodedJSON(),
|
183 | type: decoder.messageName()
|
184 | };
|
185 | }
|
186 |
|
187 |
|
188 | function receiveAndParse(_x4, _x5) {
|
189 | return _receiveAndParse.apply(this, arguments);
|
190 | }
|
191 |
|
192 | function _receiveAndParse() {
|
193 | _receiveAndParse = _asyncToGenerator(
|
194 |
|
195 | _regenerator["default"].mark(function _callee3(messages, receiver) {
|
196 | var received, typeId, buffer, decoder;
|
197 | return _regenerator["default"].wrap(function _callee3$(_context3) {
|
198 | while (1) {
|
199 | switch (_context3.prev = _context3.next) {
|
200 | case 0:
|
201 | _context3.next = 2;
|
202 | return receiveBuffer(receiver);
|
203 |
|
204 | case 2:
|
205 | received = _context3.sent;
|
206 | typeId = received.typeNumber;
|
207 | buffer = received.arrayBuffer();
|
208 | decoder = new _message_decoder.MessageDecoder(messages, typeId, buffer);
|
209 | return _context3.abrupt("return", {
|
210 | message: decoder.decodedJSON(),
|
211 | type: decoder.messageName()
|
212 | });
|
213 |
|
214 | case 7:
|
215 | case "end":
|
216 | return _context3.stop();
|
217 | }
|
218 | }
|
219 | }, _callee3);
|
220 | }));
|
221 | return _receiveAndParse.apply(this, arguments);
|
222 | } |
\ | No newline at end of file |