(data)
| 230 | } |
| 231 | |
| 232 | function decode(data) { |
| 233 | let i = 1; // skip "4" prefix |
| 234 | |
| 235 | const packet = { |
| 236 | type: parseInt(data.charAt(i++), 10), |
| 237 | }; |
| 238 | |
| 239 | if (data.charAt(i)) { |
| 240 | packet.data = JSON.parse(data.substring(i)); |
| 241 | } |
| 242 | |
| 243 | if (!isPacketValid(packet)) { |
| 244 | throw new Error("invalid format"); |
| 245 | } |
| 246 | |
| 247 | return packet; |
| 248 | } |
| 249 | |
| 250 | function isPacketValid(packet) { |
| 251 | switch (packet.type) { |
no test coverage detected