( encodedPacket: RawData, binaryType?: BinaryType, )
| 7 | } from "./commons.js"; |
| 8 | |
| 9 | export const decodePacket = ( |
| 10 | encodedPacket: RawData, |
| 11 | binaryType?: BinaryType, |
| 12 | ): Packet => { |
| 13 | if (typeof encodedPacket !== "string") { |
| 14 | return { |
| 15 | type: "message", |
| 16 | data: mapBinary(encodedPacket, binaryType), |
| 17 | }; |
| 18 | } |
| 19 | const type = encodedPacket.charAt(0); |
| 20 | if (type === "b") { |
| 21 | const buffer = Buffer.from(encodedPacket.substring(1), "base64"); |
| 22 | return { |
| 23 | type: "message", |
| 24 | data: mapBinary(buffer, binaryType), |
| 25 | }; |
| 26 | } |
| 27 | if (!PACKET_TYPES_REVERSE[type]) { |
| 28 | return ERROR_PACKET; |
| 29 | } |
| 30 | return encodedPacket.length > 1 |
| 31 | ? { |
| 32 | type: PACKET_TYPES_REVERSE[type], |
| 33 | data: encodedPacket.substring(1), |
| 34 | } |
| 35 | : { |
| 36 | type: PACKET_TYPES_REVERSE[type], |
| 37 | }; |
| 38 | }; |
| 39 | |
| 40 | const mapBinary = (data: RawData, binaryType?: BinaryType) => { |
| 41 | switch (binaryType) { |
no test coverage detected