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