(maxPayload, binaryType)
| 522 | return buffer; |
| 523 | } |
| 524 | function createPacketDecoderStream(maxPayload, binaryType) { |
| 525 | if (!TEXT_DECODER) { |
| 526 | TEXT_DECODER = new TextDecoder(); |
| 527 | } |
| 528 | var chunks = []; |
| 529 | var state = 0 /* READ_HEADER */; |
| 530 | var expectedLength = -1; |
| 531 | var isBinary = false; |
| 532 | return new TransformStream({ |
| 533 | transform: function transform(chunk, controller) { |
| 534 | chunks.push(chunk); |
| 535 | while (true) { |
| 536 | if (state === 0 /* READ_HEADER */) { |
| 537 | if (totalLength(chunks) < 1) { |
| 538 | break; |
| 539 | } |
| 540 | var header = concatChunks(chunks, 1); |
| 541 | isBinary = (header[0] & 0x80) === 0x80; |
| 542 | expectedLength = header[0] & 0x7f; |
| 543 | if (expectedLength < 126) { |
| 544 | state = 3 /* READ_PAYLOAD */; |
| 545 | } else if (expectedLength === 126) { |
| 546 | state = 1 /* READ_EXTENDED_LENGTH_16 */; |
| 547 | } else { |
| 548 | state = 2 /* READ_EXTENDED_LENGTH_64 */; |
| 549 | } |
| 550 | } else if (state === 1 /* READ_EXTENDED_LENGTH_16 */) { |
| 551 | if (totalLength(chunks) < 2) { |
| 552 | break; |
| 553 | } |
| 554 | var headerArray = concatChunks(chunks, 2); |
| 555 | expectedLength = new DataView(headerArray.buffer, headerArray.byteOffset, headerArray.length).getUint16(0); |
| 556 | state = 3 /* READ_PAYLOAD */; |
| 557 | } else if (state === 2 /* READ_EXTENDED_LENGTH_64 */) { |
| 558 | if (totalLength(chunks) < 8) { |
| 559 | break; |
| 560 | } |
| 561 | var _headerArray = concatChunks(chunks, 8); |
| 562 | var view = new DataView(_headerArray.buffer, _headerArray.byteOffset, _headerArray.length); |
| 563 | var n = view.getUint32(0); |
| 564 | if (n > Math.pow(2, 53 - 32) - 1) { |
| 565 | // the maximum safe integer in JavaScript is 2^53 - 1 |
| 566 | controller.enqueue(ERROR_PACKET); |
| 567 | break; |
| 568 | } |
| 569 | expectedLength = n * Math.pow(2, 32) + view.getUint32(4); |
| 570 | state = 3 /* READ_PAYLOAD */; |
| 571 | } else { |
| 572 | if (totalLength(chunks) < expectedLength) { |
| 573 | break; |
| 574 | } |
| 575 | var data = concatChunks(chunks, expectedLength); |
| 576 | controller.enqueue(decodePacket(isBinary ? data : TEXT_DECODER.decode(data), binaryType)); |
| 577 | state = 0 /* READ_HEADER */; |
| 578 | } |
| 579 | |
| 580 | if (expectedLength === 0 || expectedLength > maxPayload) { |
| 581 | controller.enqueue(ERROR_PACKET); |
no test coverage detected