( packets: Packet[], callback: (encodedPayload: string) => void, )
| 11 | const SEPARATOR = String.fromCharCode(30); // see https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text |
| 12 | |
| 13 | const encodePayload = ( |
| 14 | packets: Packet[], |
| 15 | callback: (encodedPayload: string) => void, |
| 16 | ) => { |
| 17 | // some packets may be added to the array while encoding, so the initial length must be saved |
| 18 | const length = packets.length; |
| 19 | const encodedPackets = new Array(length); |
| 20 | let count = 0; |
| 21 | |
| 22 | packets.forEach((packet, i) => { |
| 23 | // force base64 encoding for binary packets |
| 24 | encodePacket(packet, false, (encodedPacket) => { |
| 25 | encodedPackets[i] = encodedPacket; |
| 26 | if (++count === length) { |
| 27 | callback(encodedPackets.join(SEPARATOR)); |
| 28 | } |
| 29 | }); |
| 30 | }); |
| 31 | }; |
| 32 | |
| 33 | const decodePayload = ( |
| 34 | encodedPayload: string, |
no test coverage detected