Last change
on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | const encodePacket = require("./encodePacket");
|
---|
| 2 | const decodePacket = require("./decodePacket");
|
---|
| 3 |
|
---|
| 4 | const SEPARATOR = String.fromCharCode(30); // see https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text
|
---|
| 5 |
|
---|
| 6 | const encodePayload = (packets, callback) => {
|
---|
| 7 | // some packets may be added to the array while encoding, so the initial length must be saved
|
---|
| 8 | const length = packets.length;
|
---|
| 9 | const encodedPackets = new Array(length);
|
---|
| 10 | let count = 0;
|
---|
| 11 |
|
---|
| 12 | packets.forEach((packet, i) => {
|
---|
| 13 | // force base64 encoding for binary packets
|
---|
| 14 | encodePacket(packet, false, encodedPacket => {
|
---|
| 15 | encodedPackets[i] = encodedPacket;
|
---|
| 16 | if (++count === length) {
|
---|
| 17 | callback(encodedPackets.join(SEPARATOR));
|
---|
| 18 | }
|
---|
| 19 | });
|
---|
| 20 | });
|
---|
| 21 | };
|
---|
| 22 |
|
---|
| 23 | const decodePayload = (encodedPayload, binaryType) => {
|
---|
| 24 | const encodedPackets = encodedPayload.split(SEPARATOR);
|
---|
| 25 | const packets = [];
|
---|
| 26 | for (let i = 0; i < encodedPackets.length; i++) {
|
---|
| 27 | const decodedPacket = decodePacket(encodedPackets[i], binaryType);
|
---|
| 28 | packets.push(decodedPacket);
|
---|
| 29 | if (decodedPacket.type === "error") {
|
---|
| 30 | break;
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 | return packets;
|
---|
| 34 | };
|
---|
| 35 |
|
---|
| 36 | module.exports = {
|
---|
| 37 | protocol: 4,
|
---|
| 38 | encodePacket,
|
---|
| 39 | encodePayload,
|
---|
| 40 | decodePacket,
|
---|
| 41 | decodePayload
|
---|
| 42 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.