1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | const memoize = require("../util/memoize");
|
8 | const SerializerMiddleware = require("./SerializerMiddleware");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | const LAZY_HEADER = 0x0b;
|
71 | const TRUE_HEADER = 0x0c;
|
72 | const FALSE_HEADER = 0x0d;
|
73 | const BOOLEANS_HEADER = 0x0e;
|
74 | const NULL_HEADER = 0x10;
|
75 | const NULL2_HEADER = 0x11;
|
76 | const NULL3_HEADER = 0x12;
|
77 | const NULLS8_HEADER = 0x13;
|
78 | const NULLS32_HEADER = 0x14;
|
79 | const NULL_AND_I8_HEADER = 0x15;
|
80 | const NULL_AND_I32_HEADER = 0x16;
|
81 | const NULL_AND_TRUE_HEADER = 0x17;
|
82 | const NULL_AND_FALSE_HEADER = 0x18;
|
83 | const STRING_HEADER = 0x1e;
|
84 | const BUFFER_HEADER = 0x1f;
|
85 | const I8_HEADER = 0x60;
|
86 | const I32_HEADER = 0x40;
|
87 | const F64_HEADER = 0x20;
|
88 | const SHORT_STRING_HEADER = 0x80;
|
89 |
|
90 |
|
91 | const NUMBERS_HEADER_MASK = 0xe0;
|
92 | const NUMBERS_COUNT_MASK = 0x1f;
|
93 | const SHORT_STRING_LENGTH_MASK = 0x7f;
|
94 |
|
95 | const HEADER_SIZE = 1;
|
96 | const I8_SIZE = 1;
|
97 | const I32_SIZE = 4;
|
98 | const F64_SIZE = 8;
|
99 |
|
100 | const MEASURE_START_OPERATION = Symbol("MEASURE_START_OPERATION");
|
101 | const MEASURE_END_OPERATION = Symbol("MEASURE_END_OPERATION");
|
102 |
|
103 | const identifyNumber = n => {
|
104 | if (n === (n | 0)) {
|
105 | if (n <= 127 && n >= -128) return 0;
|
106 | if (n <= 2147483647 && n >= -2147483648) return 1;
|
107 | }
|
108 | return 2;
|
109 | };
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | class BinaryMiddleware extends SerializerMiddleware {
|
117 | |
118 |
|
119 |
|
120 |
|
121 |
|
122 | serialize(data, context) {
|
123 | return this._serialize(data, context);
|
124 | }
|
125 |
|
126 | |
127 |
|
128 |
|
129 |
|
130 |
|
131 | _serialize(data, context) {
|
132 |
|
133 | let currentBuffer = null;
|
134 |
|
135 | let leftOverBuffer = null;
|
136 | let currentPosition = 0;
|
137 |
|
138 | const buffers = [];
|
139 | let buffersTotalLength = 0;
|
140 | const allocate = bytesNeeded => {
|
141 | if (currentBuffer !== null) {
|
142 | if (currentBuffer.length - currentPosition >= bytesNeeded) return;
|
143 | flush();
|
144 | }
|
145 | if (leftOverBuffer && leftOverBuffer.length >= bytesNeeded) {
|
146 | currentBuffer = leftOverBuffer;
|
147 | leftOverBuffer = null;
|
148 | } else {
|
149 | currentBuffer = Buffer.allocUnsafeSlow(
|
150 | Math.max(bytesNeeded, buffersTotalLength, 16384)
|
151 | );
|
152 | }
|
153 | };
|
154 | const flush = () => {
|
155 | if (currentBuffer !== null) {
|
156 | buffers.push(currentBuffer.slice(0, currentPosition));
|
157 | if (
|
158 | !leftOverBuffer ||
|
159 | leftOverBuffer.length < currentBuffer.length - currentPosition
|
160 | )
|
161 | leftOverBuffer = currentBuffer.slice(currentPosition);
|
162 | currentBuffer = null;
|
163 | buffersTotalLength += currentPosition;
|
164 | currentPosition = 0;
|
165 | }
|
166 | };
|
167 | const writeU8 = byte => {
|
168 | currentBuffer.writeUInt8(byte, currentPosition++);
|
169 | };
|
170 | const writeU32 = ui32 => {
|
171 | currentBuffer.writeUInt32LE(ui32, currentPosition);
|
172 | currentPosition += 4;
|
173 | };
|
174 | const measureStack = [];
|
175 | const measureStart = () => {
|
176 | measureStack.push(buffers.length, currentPosition);
|
177 | };
|
178 | const measureEnd = () => {
|
179 | const oldPos = measureStack.pop();
|
180 | const buffersIndex = measureStack.pop();
|
181 | let size = currentPosition - oldPos;
|
182 | for (let i = buffersIndex; i < buffers.length; i++) {
|
183 | size += buffers[i].length;
|
184 | }
|
185 | return size;
|
186 | };
|
187 | const serializeData = data => {
|
188 | for (let i = 0; i < data.length; i++) {
|
189 | const thing = data[i];
|
190 | switch (typeof thing) {
|
191 | case "function": {
|
192 | if (!SerializerMiddleware.isLazy(thing))
|
193 | throw new Error("Unexpected function " + thing);
|
194 |
|
195 | let serializedData = SerializerMiddleware.getLazySerializedValue(
|
196 | thing
|
197 | );
|
198 | if (serializedData === undefined) {
|
199 | if (SerializerMiddleware.isLazy(thing, this)) {
|
200 | const data = this._serialize(thing(), context);
|
201 | SerializerMiddleware.setLazySerializedValue(thing, data);
|
202 | serializedData = data;
|
203 | } else {
|
204 | serializedData = SerializerMiddleware.serializeLazy(
|
205 | thing,
|
206 | data => this._serialize(data, context)
|
207 | );
|
208 | }
|
209 | }
|
210 | if (typeof serializedData === "function") {
|
211 | flush();
|
212 | buffers.push(serializedData);
|
213 | } else {
|
214 | const lengths = [];
|
215 | for (const item of serializedData) {
|
216 | let last;
|
217 | if (typeof item === "function") {
|
218 | lengths.push(0);
|
219 | } else if (item.length === 0) {
|
220 |
|
221 | } else if (
|
222 | lengths.length > 0 &&
|
223 | (last = lengths[lengths.length - 1]) !== 0
|
224 | ) {
|
225 | const remaining = 0xffffffff - last;
|
226 | if (remaining >= item.length) {
|
227 | lengths[lengths.length - 1] += item.length;
|
228 | } else {
|
229 | lengths.push(item.length - remaining);
|
230 | lengths[lengths.length - 2] = 0xffffffff;
|
231 | }
|
232 | } else {
|
233 | lengths.push(item.length);
|
234 | }
|
235 | }
|
236 | allocate(5 + lengths.length * 4);
|
237 | writeU8(LAZY_HEADER);
|
238 | writeU32(lengths.length);
|
239 | for (const l of lengths) {
|
240 | writeU32(l);
|
241 | }
|
242 | flush();
|
243 | for (const item of serializedData) {
|
244 | buffers.push(item);
|
245 | }
|
246 | }
|
247 | break;
|
248 | }
|
249 | case "string": {
|
250 | const len = Buffer.byteLength(thing);
|
251 | if (len >= 128 || len !== thing.length) {
|
252 | allocate(len + HEADER_SIZE + I32_SIZE);
|
253 | writeU8(STRING_HEADER);
|
254 | writeU32(len);
|
255 | currentBuffer.write(thing, currentPosition);
|
256 | } else {
|
257 | allocate(len + HEADER_SIZE);
|
258 | writeU8(SHORT_STRING_HEADER | len);
|
259 | currentBuffer.write(thing, currentPosition, "latin1");
|
260 | }
|
261 | currentPosition += len;
|
262 | break;
|
263 | }
|
264 | case "number": {
|
265 | const type = identifyNumber(thing);
|
266 | if (type === 0 && thing >= 0 && thing <= 10) {
|
267 |
|
268 | allocate(I8_SIZE);
|
269 | writeU8(thing);
|
270 | break;
|
271 | }
|
272 | |
273 |
|
274 |
|
275 |
|
276 | let n = 1;
|
277 | for (; n < 32 && i + n < data.length; n++) {
|
278 | const item = data[i + n];
|
279 | if (typeof item !== "number") break;
|
280 | if (identifyNumber(item) !== type) break;
|
281 | }
|
282 | switch (type) {
|
283 | case 0:
|
284 | allocate(HEADER_SIZE + I8_SIZE * n);
|
285 | writeU8(I8_HEADER | (n - 1));
|
286 | while (n > 0) {
|
287 | currentBuffer.writeInt8(
|
288 | (data[i]),
|
289 | currentPosition
|
290 | );
|
291 | currentPosition += I8_SIZE;
|
292 | n--;
|
293 | i++;
|
294 | }
|
295 | break;
|
296 | case 1:
|
297 | allocate(HEADER_SIZE + I32_SIZE * n);
|
298 | writeU8(I32_HEADER | (n - 1));
|
299 | while (n > 0) {
|
300 | currentBuffer.writeInt32LE(
|
301 | (data[i]),
|
302 | currentPosition
|
303 | );
|
304 | currentPosition += I32_SIZE;
|
305 | n--;
|
306 | i++;
|
307 | }
|
308 | break;
|
309 | case 2:
|
310 | allocate(HEADER_SIZE + F64_SIZE * n);
|
311 | writeU8(F64_HEADER | (n - 1));
|
312 | while (n > 0) {
|
313 | currentBuffer.writeDoubleLE(
|
314 | (data[i]),
|
315 | currentPosition
|
316 | );
|
317 | currentPosition += F64_SIZE;
|
318 | n--;
|
319 | i++;
|
320 | }
|
321 | break;
|
322 | }
|
323 |
|
324 | i--;
|
325 | break;
|
326 | }
|
327 | case "boolean": {
|
328 | let lastByte = thing === true ? 1 : 0;
|
329 | const bytes = [];
|
330 | let count = 1;
|
331 | let n;
|
332 | for (n = 1; n < 0xffffffff && i + n < data.length; n++) {
|
333 | const item = data[i + n];
|
334 | if (typeof item !== "boolean") break;
|
335 | const pos = count & 0x7;
|
336 | if (pos === 0) {
|
337 | bytes.push(lastByte);
|
338 | lastByte = item === true ? 1 : 0;
|
339 | } else if (item === true) {
|
340 | lastByte |= 1 << pos;
|
341 | }
|
342 | count++;
|
343 | }
|
344 | i += count - 1;
|
345 | if (count === 1) {
|
346 | allocate(HEADER_SIZE);
|
347 | writeU8(lastByte === 1 ? TRUE_HEADER : FALSE_HEADER);
|
348 | } else if (count === 2) {
|
349 | allocate(HEADER_SIZE * 2);
|
350 | writeU8(lastByte & 1 ? TRUE_HEADER : FALSE_HEADER);
|
351 | writeU8(lastByte & 2 ? TRUE_HEADER : FALSE_HEADER);
|
352 | } else if (count <= 6) {
|
353 | allocate(HEADER_SIZE + I8_SIZE);
|
354 | writeU8(BOOLEANS_HEADER);
|
355 | writeU8((1 << count) | lastByte);
|
356 | } else if (count <= 133) {
|
357 | allocate(
|
358 | HEADER_SIZE + I8_SIZE + I8_SIZE * bytes.length + I8_SIZE
|
359 | );
|
360 | writeU8(BOOLEANS_HEADER);
|
361 | writeU8(0x80 | (count - 7));
|
362 | for (const byte of bytes) writeU8(byte);
|
363 | writeU8(lastByte);
|
364 | } else {
|
365 | allocate(
|
366 | HEADER_SIZE +
|
367 | I8_SIZE +
|
368 | I32_SIZE +
|
369 | I8_SIZE * bytes.length +
|
370 | I8_SIZE
|
371 | );
|
372 | writeU8(BOOLEANS_HEADER);
|
373 | writeU8(0xff);
|
374 | writeU32(count);
|
375 | for (const byte of bytes) writeU8(byte);
|
376 | writeU8(lastByte);
|
377 | }
|
378 | break;
|
379 | }
|
380 | case "object": {
|
381 | if (thing === null) {
|
382 | let n;
|
383 | for (n = 1; n < 0x100000104 && i + n < data.length; n++) {
|
384 | const item = data[i + n];
|
385 | if (item !== null) break;
|
386 | }
|
387 | i += n - 1;
|
388 | if (n === 1) {
|
389 | if (i + 1 < data.length) {
|
390 | const next = data[i + 1];
|
391 | if (next === true) {
|
392 | allocate(HEADER_SIZE);
|
393 | writeU8(NULL_AND_TRUE_HEADER);
|
394 | i++;
|
395 | } else if (next === false) {
|
396 | allocate(HEADER_SIZE);
|
397 | writeU8(NULL_AND_FALSE_HEADER);
|
398 | i++;
|
399 | } else if (typeof next === "number") {
|
400 | const type = identifyNumber(next);
|
401 | if (type === 0) {
|
402 | allocate(HEADER_SIZE + I8_SIZE);
|
403 | writeU8(NULL_AND_I8_HEADER);
|
404 | currentBuffer.writeInt8(next, currentPosition);
|
405 | currentPosition += I8_SIZE;
|
406 | i++;
|
407 | } else if (type === 1) {
|
408 | allocate(HEADER_SIZE + I32_SIZE);
|
409 | writeU8(NULL_AND_I32_HEADER);
|
410 | currentBuffer.writeInt32LE(next, currentPosition);
|
411 | currentPosition += I32_SIZE;
|
412 | i++;
|
413 | } else {
|
414 | allocate(HEADER_SIZE);
|
415 | writeU8(NULL_HEADER);
|
416 | }
|
417 | } else {
|
418 | allocate(HEADER_SIZE);
|
419 | writeU8(NULL_HEADER);
|
420 | }
|
421 | } else {
|
422 | allocate(HEADER_SIZE);
|
423 | writeU8(NULL_HEADER);
|
424 | }
|
425 | } else if (n === 2) {
|
426 | allocate(HEADER_SIZE);
|
427 | writeU8(NULL2_HEADER);
|
428 | } else if (n === 3) {
|
429 | allocate(HEADER_SIZE);
|
430 | writeU8(NULL3_HEADER);
|
431 | } else if (n < 260) {
|
432 | allocate(HEADER_SIZE + I8_SIZE);
|
433 | writeU8(NULLS8_HEADER);
|
434 | writeU8(n - 4);
|
435 | } else {
|
436 | allocate(HEADER_SIZE + I32_SIZE);
|
437 | writeU8(NULLS32_HEADER);
|
438 | writeU32(n - 260);
|
439 | }
|
440 | } else if (Buffer.isBuffer(thing)) {
|
441 | if (thing.length < 8192) {
|
442 | allocate(HEADER_SIZE + I32_SIZE + thing.length);
|
443 | writeU8(BUFFER_HEADER);
|
444 | writeU32(thing.length);
|
445 | thing.copy(currentBuffer, currentPosition);
|
446 | currentPosition += thing.length;
|
447 | } else {
|
448 | allocate(HEADER_SIZE + I32_SIZE);
|
449 | writeU8(BUFFER_HEADER);
|
450 | writeU32(thing.length);
|
451 | flush();
|
452 | buffers.push(thing);
|
453 | }
|
454 | }
|
455 | break;
|
456 | }
|
457 | case "symbol": {
|
458 | if (thing === MEASURE_START_OPERATION) {
|
459 | measureStart();
|
460 | } else if (thing === MEASURE_END_OPERATION) {
|
461 | const size = measureEnd();
|
462 | allocate(HEADER_SIZE + I32_SIZE);
|
463 | writeU8(I32_HEADER);
|
464 | currentBuffer.writeInt32LE(size, currentPosition);
|
465 | currentPosition += I32_SIZE;
|
466 | }
|
467 | break;
|
468 | }
|
469 | }
|
470 | }
|
471 | };
|
472 | serializeData(data);
|
473 | flush();
|
474 | return buffers;
|
475 | }
|
476 |
|
477 | |
478 |
|
479 |
|
480 |
|
481 |
|
482 | deserialize(data, context) {
|
483 | return this._deserialize(data, context);
|
484 | }
|
485 |
|
486 | |
487 |
|
488 |
|
489 |
|
490 |
|
491 | _deserialize(data, context) {
|
492 | let currentDataItem = 0;
|
493 | let currentBuffer = data[0];
|
494 | let currentIsBuffer = Buffer.isBuffer(currentBuffer);
|
495 | let currentPosition = 0;
|
496 |
|
497 | const checkOverflow = () => {
|
498 | if (currentPosition >= currentBuffer.length) {
|
499 | currentPosition = 0;
|
500 | currentDataItem++;
|
501 | currentBuffer =
|
502 | currentDataItem < data.length ? data[currentDataItem] : null;
|
503 | currentIsBuffer = Buffer.isBuffer(currentBuffer);
|
504 | }
|
505 | };
|
506 | const isInCurrentBuffer = n => {
|
507 | return currentIsBuffer && n + currentPosition <= currentBuffer.length;
|
508 | };
|
509 | |
510 |
|
511 |
|
512 |
|
513 |
|
514 | const read = n => {
|
515 | if (!currentIsBuffer) {
|
516 | throw new Error(
|
517 | currentBuffer === null
|
518 | ? "Unexpected end of stream"
|
519 | : "Unexpected lazy element in stream"
|
520 | );
|
521 | }
|
522 | const rem = currentBuffer.length - currentPosition;
|
523 | if (rem < n) {
|
524 | return Buffer.concat([read(rem), read(n - rem)]);
|
525 | }
|
526 | const res = (currentBuffer).slice(
|
527 | currentPosition,
|
528 | currentPosition + n
|
529 | );
|
530 | currentPosition += n;
|
531 | checkOverflow();
|
532 | return res;
|
533 | };
|
534 | |
535 |
|
536 |
|
537 |
|
538 |
|
539 | const readUpTo = n => {
|
540 | if (!currentIsBuffer) {
|
541 | throw new Error(
|
542 | currentBuffer === null
|
543 | ? "Unexpected end of stream"
|
544 | : "Unexpected lazy element in stream"
|
545 | );
|
546 | }
|
547 | const rem = currentBuffer.length - currentPosition;
|
548 | if (rem < n) {
|
549 | n = rem;
|
550 | }
|
551 | const res = (currentBuffer).slice(
|
552 | currentPosition,
|
553 | currentPosition + n
|
554 | );
|
555 | currentPosition += n;
|
556 | checkOverflow();
|
557 | return res;
|
558 | };
|
559 | const readU8 = () => {
|
560 | if (!currentIsBuffer) {
|
561 | throw new Error(
|
562 | currentBuffer === null
|
563 | ? "Unexpected end of stream"
|
564 | : "Unexpected lazy element in stream"
|
565 | );
|
566 | }
|
567 | |
568 |
|
569 |
|
570 |
|
571 | const byte = (currentBuffer).readUInt8(
|
572 | currentPosition
|
573 | );
|
574 | currentPosition += I8_SIZE;
|
575 | checkOverflow();
|
576 | return byte;
|
577 | };
|
578 | const readU32 = () => {
|
579 | return read(I32_SIZE).readUInt32LE(0);
|
580 | };
|
581 | const readBits = (data, n) => {
|
582 | let mask = 1;
|
583 | while (n !== 0) {
|
584 | result.push((data & mask) !== 0);
|
585 | mask = mask << 1;
|
586 | n--;
|
587 | }
|
588 | };
|
589 | const dispatchTable = Array.from({ length: 256 }).map((_, header) => {
|
590 | switch (header) {
|
591 | case LAZY_HEADER:
|
592 | return () => {
|
593 | const count = readU32();
|
594 | const lengths = Array.from({ length: count }).map(() => readU32());
|
595 | const content = [];
|
596 | for (let l of lengths) {
|
597 | if (l === 0) {
|
598 | if (typeof currentBuffer !== "function") {
|
599 | throw new Error("Unexpected non-lazy element in stream");
|
600 | }
|
601 | content.push(currentBuffer);
|
602 | currentDataItem++;
|
603 | currentBuffer =
|
604 | currentDataItem < data.length ? data[currentDataItem] : null;
|
605 | currentIsBuffer = Buffer.isBuffer(currentBuffer);
|
606 | } else {
|
607 | do {
|
608 | const buf = readUpTo(l);
|
609 | l -= buf.length;
|
610 | content.push(buf);
|
611 | } while (l > 0);
|
612 | }
|
613 | }
|
614 | result.push(
|
615 | SerializerMiddleware.createLazy(
|
616 | memoize(() => this._deserialize(content, context)),
|
617 | this,
|
618 | undefined,
|
619 | content
|
620 | )
|
621 | );
|
622 | };
|
623 | case BUFFER_HEADER:
|
624 | return () => {
|
625 | const len = readU32();
|
626 | result.push(read(len));
|
627 | };
|
628 | case TRUE_HEADER:
|
629 | return () => result.push(true);
|
630 | case FALSE_HEADER:
|
631 | return () => result.push(false);
|
632 | case NULL3_HEADER:
|
633 | return () => result.push(null, null, null);
|
634 | case NULL2_HEADER:
|
635 | return () => result.push(null, null);
|
636 | case NULL_HEADER:
|
637 | return () => result.push(null);
|
638 | case NULL_AND_TRUE_HEADER:
|
639 | return () => result.push(null, true);
|
640 | case NULL_AND_FALSE_HEADER:
|
641 | return () => result.push(null, false);
|
642 | case NULL_AND_I8_HEADER:
|
643 | return () => {
|
644 | if (currentIsBuffer) {
|
645 | result.push(
|
646 | null,
|
647 | (currentBuffer).readInt8(currentPosition)
|
648 | );
|
649 | currentPosition += I8_SIZE;
|
650 | checkOverflow();
|
651 | } else {
|
652 | result.push(null, read(I8_SIZE).readInt8(0));
|
653 | }
|
654 | };
|
655 | case NULL_AND_I32_HEADER:
|
656 | return () => {
|
657 | result.push(null);
|
658 | if (isInCurrentBuffer(I32_SIZE)) {
|
659 | result.push(
|
660 | (currentBuffer).readInt32LE(
|
661 | currentPosition
|
662 | )
|
663 | );
|
664 | currentPosition += I32_SIZE;
|
665 | checkOverflow();
|
666 | } else {
|
667 | result.push(read(I32_SIZE).readInt32LE(0));
|
668 | }
|
669 | };
|
670 | case NULLS8_HEADER:
|
671 | return () => {
|
672 | const len = readU8() + 4;
|
673 | for (let i = 0; i < len; i++) {
|
674 | result.push(null);
|
675 | }
|
676 | };
|
677 | case NULLS32_HEADER:
|
678 | return () => {
|
679 | const len = readU32() + 260;
|
680 | for (let i = 0; i < len; i++) {
|
681 | result.push(null);
|
682 | }
|
683 | };
|
684 | case BOOLEANS_HEADER:
|
685 | return () => {
|
686 | const innerHeader = readU8();
|
687 | if ((innerHeader & 0xf0) === 0) {
|
688 | readBits(innerHeader, 3);
|
689 | } else if ((innerHeader & 0xe0) === 0) {
|
690 | readBits(innerHeader, 4);
|
691 | } else if ((innerHeader & 0xc0) === 0) {
|
692 | readBits(innerHeader, 5);
|
693 | } else if ((innerHeader & 0x80) === 0) {
|
694 | readBits(innerHeader, 6);
|
695 | } else if (innerHeader !== 0xff) {
|
696 | let count = (innerHeader & 0x7f) + 7;
|
697 | while (count > 8) {
|
698 | readBits(readU8(), 8);
|
699 | count -= 8;
|
700 | }
|
701 | readBits(readU8(), count);
|
702 | } else {
|
703 | let count = readU32();
|
704 | while (count > 8) {
|
705 | readBits(readU8(), 8);
|
706 | count -= 8;
|
707 | }
|
708 | readBits(readU8(), count);
|
709 | }
|
710 | };
|
711 | case STRING_HEADER:
|
712 | return () => {
|
713 | const len = readU32();
|
714 | if (isInCurrentBuffer(len)) {
|
715 | result.push(
|
716 | currentBuffer.toString(
|
717 | undefined,
|
718 | currentPosition,
|
719 | currentPosition + len
|
720 | )
|
721 | );
|
722 | currentPosition += len;
|
723 | checkOverflow();
|
724 | } else {
|
725 | result.push(read(len).toString());
|
726 | }
|
727 | };
|
728 | case SHORT_STRING_HEADER:
|
729 | return () => result.push("");
|
730 | case SHORT_STRING_HEADER | 1:
|
731 | return () => {
|
732 | if (currentIsBuffer) {
|
733 | result.push(
|
734 | currentBuffer.toString(
|
735 | "latin1",
|
736 | currentPosition,
|
737 | currentPosition + 1
|
738 | )
|
739 | );
|
740 | currentPosition++;
|
741 | checkOverflow();
|
742 | } else {
|
743 | result.push(read(1).toString("latin1"));
|
744 | }
|
745 | };
|
746 | case I8_HEADER:
|
747 | return () => {
|
748 | if (currentIsBuffer) {
|
749 | result.push(
|
750 | (currentBuffer).readInt8(currentPosition)
|
751 | );
|
752 | currentPosition++;
|
753 | checkOverflow();
|
754 | } else {
|
755 | result.push(read(1).readInt8(0));
|
756 | }
|
757 | };
|
758 | default:
|
759 | if (header <= 10) {
|
760 | return () => result.push(header);
|
761 | } else if ((header & SHORT_STRING_HEADER) === SHORT_STRING_HEADER) {
|
762 | const len = header & SHORT_STRING_LENGTH_MASK;
|
763 | return () => {
|
764 | if (isInCurrentBuffer(len)) {
|
765 | result.push(
|
766 | currentBuffer.toString(
|
767 | "latin1",
|
768 | currentPosition,
|
769 | currentPosition + len
|
770 | )
|
771 | );
|
772 | currentPosition += len;
|
773 | checkOverflow();
|
774 | } else {
|
775 | result.push(read(len).toString("latin1"));
|
776 | }
|
777 | };
|
778 | } else if ((header & NUMBERS_HEADER_MASK) === F64_HEADER) {
|
779 | const len = (header & NUMBERS_COUNT_MASK) + 1;
|
780 | return () => {
|
781 | const need = F64_SIZE * len;
|
782 | if (isInCurrentBuffer(need)) {
|
783 | for (let i = 0; i < len; i++) {
|
784 | result.push(
|
785 | (currentBuffer).readDoubleLE(
|
786 | currentPosition
|
787 | )
|
788 | );
|
789 | currentPosition += F64_SIZE;
|
790 | }
|
791 | checkOverflow();
|
792 | } else {
|
793 | const buf = read(need);
|
794 | for (let i = 0; i < len; i++) {
|
795 | result.push(buf.readDoubleLE(i * F64_SIZE));
|
796 | }
|
797 | }
|
798 | };
|
799 | } else if ((header & NUMBERS_HEADER_MASK) === I32_HEADER) {
|
800 | const len = (header & NUMBERS_COUNT_MASK) + 1;
|
801 | return () => {
|
802 | const need = I32_SIZE * len;
|
803 | if (isInCurrentBuffer(need)) {
|
804 | for (let i = 0; i < len; i++) {
|
805 | result.push(
|
806 | (currentBuffer).readInt32LE(
|
807 | currentPosition
|
808 | )
|
809 | );
|
810 | currentPosition += I32_SIZE;
|
811 | }
|
812 | checkOverflow();
|
813 | } else {
|
814 | const buf = read(need);
|
815 | for (let i = 0; i < len; i++) {
|
816 | result.push(buf.readInt32LE(i * I32_SIZE));
|
817 | }
|
818 | }
|
819 | };
|
820 | } else if ((header & NUMBERS_HEADER_MASK) === I8_HEADER) {
|
821 | const len = (header & NUMBERS_COUNT_MASK) + 1;
|
822 | return () => {
|
823 | const need = I8_SIZE * len;
|
824 | if (isInCurrentBuffer(need)) {
|
825 | for (let i = 0; i < len; i++) {
|
826 | result.push(
|
827 | (currentBuffer).readInt8(
|
828 | currentPosition
|
829 | )
|
830 | );
|
831 | currentPosition += I8_SIZE;
|
832 | }
|
833 | checkOverflow();
|
834 | } else {
|
835 | const buf = read(need);
|
836 | for (let i = 0; i < len; i++) {
|
837 | result.push(buf.readInt8(i * I8_SIZE));
|
838 | }
|
839 | }
|
840 | };
|
841 | } else {
|
842 | return () => {
|
843 | throw new Error(
|
844 | `Unexpected header byte 0x${header.toString(16)}`
|
845 | );
|
846 | };
|
847 | }
|
848 | }
|
849 | });
|
850 |
|
851 |
|
852 | const result = [];
|
853 | while (currentBuffer !== null) {
|
854 | if (typeof currentBuffer === "function") {
|
855 | result.push(
|
856 | SerializerMiddleware.deserializeLazy(currentBuffer, data =>
|
857 | this._deserialize(data, context)
|
858 | )
|
859 | );
|
860 | currentDataItem++;
|
861 | currentBuffer =
|
862 | currentDataItem < data.length ? data[currentDataItem] : null;
|
863 | currentIsBuffer = Buffer.isBuffer(currentBuffer);
|
864 | } else {
|
865 | const header = readU8();
|
866 | dispatchTable[header]();
|
867 | }
|
868 | }
|
869 | return result;
|
870 | }
|
871 | }
|
872 |
|
873 | module.exports = BinaryMiddleware;
|
874 |
|
875 | module.exports.MEASURE_START_OPERATION = MEASURE_START_OPERATION;
|
876 | module.exports.MEASURE_END_OPERATION = MEASURE_END_OPERATION;
|