index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.decodePayload = exports.decodePacket = exports.encodePayload = exports.encodePacket = exports.protocol = exports.createPacketDecoderStream = exports.createPacketEncoderStream = void 0;
  4. const encodePacket_js_1 = require("./encodePacket.js");
  5. Object.defineProperty(exports, "encodePacket", { enumerable: true, get: function () { return encodePacket_js_1.encodePacket; } });
  6. const decodePacket_js_1 = require("./decodePacket.js");
  7. Object.defineProperty(exports, "decodePacket", { enumerable: true, get: function () { return decodePacket_js_1.decodePacket; } });
  8. const commons_js_1 = require("./commons.js");
  9. const SEPARATOR = String.fromCharCode(30); // see https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text
  10. const encodePayload = (packets, callback) => {
  11. // some packets may be added to the array while encoding, so the initial length must be saved
  12. const length = packets.length;
  13. const encodedPackets = new Array(length);
  14. let count = 0;
  15. packets.forEach((packet, i) => {
  16. // force base64 encoding for binary packets
  17. (0, encodePacket_js_1.encodePacket)(packet, false, encodedPacket => {
  18. encodedPackets[i] = encodedPacket;
  19. if (++count === length) {
  20. callback(encodedPackets.join(SEPARATOR));
  21. }
  22. });
  23. });
  24. };
  25. exports.encodePayload = encodePayload;
  26. const decodePayload = (encodedPayload, binaryType) => {
  27. const encodedPackets = encodedPayload.split(SEPARATOR);
  28. const packets = [];
  29. for (let i = 0; i < encodedPackets.length; i++) {
  30. const decodedPacket = (0, decodePacket_js_1.decodePacket)(encodedPackets[i], binaryType);
  31. packets.push(decodedPacket);
  32. if (decodedPacket.type === "error") {
  33. break;
  34. }
  35. }
  36. return packets;
  37. };
  38. exports.decodePayload = decodePayload;
  39. function createPacketEncoderStream() {
  40. return new TransformStream({
  41. transform(packet, controller) {
  42. (0, encodePacket_js_1.encodePacketToBinary)(packet, encodedPacket => {
  43. const payloadLength = encodedPacket.length;
  44. let header;
  45. // inspired by the WebSocket format: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#decoding_payload_length
  46. if (payloadLength < 126) {
  47. header = new Uint8Array(1);
  48. new DataView(header.buffer).setUint8(0, payloadLength);
  49. }
  50. else if (payloadLength < 65536) {
  51. header = new Uint8Array(3);
  52. const view = new DataView(header.buffer);
  53. view.setUint8(0, 126);
  54. view.setUint16(1, payloadLength);
  55. }
  56. else {
  57. header = new Uint8Array(9);
  58. const view = new DataView(header.buffer);
  59. view.setUint8(0, 127);
  60. view.setBigUint64(1, BigInt(payloadLength));
  61. }
  62. // first bit indicates whether the payload is plain text (0) or binary (1)
  63. if (packet.data && typeof packet.data !== "string") {
  64. header[0] |= 0x80;
  65. }
  66. controller.enqueue(header);
  67. controller.enqueue(encodedPacket);
  68. });
  69. }
  70. });
  71. }
  72. exports.createPacketEncoderStream = createPacketEncoderStream;
  73. let TEXT_DECODER;
  74. function totalLength(chunks) {
  75. return chunks.reduce((acc, chunk) => acc + chunk.length, 0);
  76. }
  77. function concatChunks(chunks, size) {
  78. if (chunks[0].length === size) {
  79. return chunks.shift();
  80. }
  81. const buffer = new Uint8Array(size);
  82. let j = 0;
  83. for (let i = 0; i < size; i++) {
  84. buffer[i] = chunks[0][j++];
  85. if (j === chunks[0].length) {
  86. chunks.shift();
  87. j = 0;
  88. }
  89. }
  90. if (chunks.length && j < chunks[0].length) {
  91. chunks[0] = chunks[0].slice(j);
  92. }
  93. return buffer;
  94. }
  95. function createPacketDecoderStream(maxPayload, binaryType) {
  96. if (!TEXT_DECODER) {
  97. TEXT_DECODER = new TextDecoder();
  98. }
  99. const chunks = [];
  100. let state = 0 /* READ_HEADER */;
  101. let expectedLength = -1;
  102. let isBinary = false;
  103. return new TransformStream({
  104. transform(chunk, controller) {
  105. chunks.push(chunk);
  106. while (true) {
  107. if (state === 0 /* READ_HEADER */) {
  108. if (totalLength(chunks) < 1) {
  109. break;
  110. }
  111. const header = concatChunks(chunks, 1);
  112. isBinary = (header[0] & 0x80) === 0x80;
  113. expectedLength = header[0] & 0x7f;
  114. if (expectedLength < 126) {
  115. state = 3 /* READ_PAYLOAD */;
  116. }
  117. else if (expectedLength === 126) {
  118. state = 1 /* READ_EXTENDED_LENGTH_16 */;
  119. }
  120. else {
  121. state = 2 /* READ_EXTENDED_LENGTH_64 */;
  122. }
  123. }
  124. else if (state === 1 /* READ_EXTENDED_LENGTH_16 */) {
  125. if (totalLength(chunks) < 2) {
  126. break;
  127. }
  128. const headerArray = concatChunks(chunks, 2);
  129. expectedLength = new DataView(headerArray.buffer, headerArray.byteOffset, headerArray.length).getUint16(0);
  130. state = 3 /* READ_PAYLOAD */;
  131. }
  132. else if (state === 2 /* READ_EXTENDED_LENGTH_64 */) {
  133. if (totalLength(chunks) < 8) {
  134. break;
  135. }
  136. const headerArray = concatChunks(chunks, 8);
  137. const view = new DataView(headerArray.buffer, headerArray.byteOffset, headerArray.length);
  138. const n = view.getUint32(0);
  139. if (n > Math.pow(2, 53 - 32) - 1) {
  140. // the maximum safe integer in JavaScript is 2^53 - 1
  141. controller.enqueue(commons_js_1.ERROR_PACKET);
  142. break;
  143. }
  144. expectedLength = n * Math.pow(2, 32) + view.getUint32(4);
  145. state = 3 /* READ_PAYLOAD */;
  146. }
  147. else {
  148. if (totalLength(chunks) < expectedLength) {
  149. break;
  150. }
  151. const data = concatChunks(chunks, expectedLength);
  152. controller.enqueue((0, decodePacket_js_1.decodePacket)(isBinary ? data : TEXT_DECODER.decode(data), binaryType));
  153. state = 0 /* READ_HEADER */;
  154. }
  155. if (expectedLength === 0 || expectedLength > maxPayload) {
  156. controller.enqueue(commons_js_1.ERROR_PACKET);
  157. break;
  158. }
  159. }
  160. }
  161. });
  162. }
  163. exports.createPacketDecoderStream = createPacketDecoderStream;
  164. exports.protocol = 4;