1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.decodePayload = exports.decodePacket = exports.encodePayload = exports.encodePacket = exports.protocol = void 0;
|
---|
4 | const encodePacket_js_1 = require("./encodePacket.js");
|
---|
5 | exports.encodePacket = encodePacket_js_1.default;
|
---|
6 | const decodePacket_js_1 = require("./decodePacket.js");
|
---|
7 | exports.decodePacket = decodePacket_js_1.default;
|
---|
8 | const SEPARATOR = String.fromCharCode(30); // see https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text
|
---|
9 | const encodePayload = (packets, callback) => {
|
---|
10 | // some packets may be added to the array while encoding, so the initial length must be saved
|
---|
11 | const length = packets.length;
|
---|
12 | const encodedPackets = new Array(length);
|
---|
13 | let count = 0;
|
---|
14 | packets.forEach((packet, i) => {
|
---|
15 | // force base64 encoding for binary packets
|
---|
16 | (0, encodePacket_js_1.default)(packet, false, encodedPacket => {
|
---|
17 | encodedPackets[i] = encodedPacket;
|
---|
18 | if (++count === length) {
|
---|
19 | callback(encodedPackets.join(SEPARATOR));
|
---|
20 | }
|
---|
21 | });
|
---|
22 | });
|
---|
23 | };
|
---|
24 | exports.encodePayload = encodePayload;
|
---|
25 | const decodePayload = (encodedPayload, binaryType) => {
|
---|
26 | const encodedPackets = encodedPayload.split(SEPARATOR);
|
---|
27 | const packets = [];
|
---|
28 | for (let i = 0; i < encodedPackets.length; i++) {
|
---|
29 | const decodedPacket = (0, decodePacket_js_1.default)(encodedPackets[i], binaryType);
|
---|
30 | packets.push(decodedPacket);
|
---|
31 | if (decodedPacket.type === "error") {
|
---|
32 | break;
|
---|
33 | }
|
---|
34 | }
|
---|
35 | return packets;
|
---|
36 | };
|
---|
37 | exports.decodePayload = decodePayload;
|
---|
38 | exports.protocol = 4;
|
---|