(maxPayload, binaryType)
| 435 | return buffer; |
| 436 | } |
| 437 | function createPacketDecoderStream(maxPayload, binaryType) { |
| 438 | if (!TEXT_DECODER) { |
| 439 | TEXT_DECODER = new TextDecoder(); |
| 440 | } |
| 441 | var chunks = []; |
| 442 | var state = 0 /* State.READ_HEADER */; |
| 443 | var expectedLength = -1; |
| 444 | var isBinary = false; |
| 445 | return new TransformStream({ |
| 446 | transform: function transform(chunk, controller) { |
| 447 | chunks.push(chunk); |
| 448 | while (true) { |
| 449 | if (state === 0 /* State.READ_HEADER */) { |
| 450 | if (totalLength(chunks) < 1) { |
| 451 | break; |
| 452 | } |
| 453 | var header = concatChunks(chunks, 1); |
| 454 | isBinary = (header[0] & 0x80) === 0x80; |
| 455 | expectedLength = header[0] & 0x7f; |
| 456 | if (expectedLength < 126) { |
| 457 | state = 3 /* State.READ_PAYLOAD */; |
| 458 | } else if (expectedLength === 126) { |
| 459 | state = 1 /* State.READ_EXTENDED_LENGTH_16 */; |
| 460 | } else { |
| 461 | state = 2 /* State.READ_EXTENDED_LENGTH_64 */; |
| 462 | } |
| 463 | } else if (state === 1 /* State.READ_EXTENDED_LENGTH_16 */) { |
| 464 | if (totalLength(chunks) < 2) { |
| 465 | break; |
| 466 | } |
| 467 | var headerArray = concatChunks(chunks, 2); |
| 468 | expectedLength = new DataView(headerArray.buffer, headerArray.byteOffset, headerArray.length).getUint16(0); |
| 469 | state = 3 /* State.READ_PAYLOAD */; |
| 470 | } else if (state === 2 /* State.READ_EXTENDED_LENGTH_64 */) { |
| 471 | if (totalLength(chunks) < 8) { |
| 472 | break; |
| 473 | } |
| 474 | var _headerArray = concatChunks(chunks, 8); |
| 475 | var view = new DataView(_headerArray.buffer, _headerArray.byteOffset, _headerArray.length); |
| 476 | var n = view.getUint32(0); |
| 477 | if (n > Math.pow(2, 53 - 32) - 1) { |
| 478 | // the maximum safe integer in JavaScript is 2^53 - 1 |
| 479 | controller.enqueue(ERROR_PACKET); |
| 480 | break; |
| 481 | } |
| 482 | expectedLength = n * Math.pow(2, 32) + view.getUint32(4); |
| 483 | state = 3 /* State.READ_PAYLOAD */; |
| 484 | } else { |
| 485 | if (totalLength(chunks) < expectedLength) { |
| 486 | break; |
| 487 | } |
| 488 | var data = concatChunks(chunks, expectedLength); |
| 489 | controller.enqueue(decodePacket(isBinary ? data : TEXT_DECODER.decode(data), binaryType)); |
| 490 | state = 0 /* State.READ_HEADER */; |
| 491 | } |
| 492 | if (expectedLength === 0 || expectedLength > maxPayload) { |
| 493 | controller.enqueue(ERROR_PACKET); |
| 494 | break; |
no test coverage detected