* Decodes an encoded packet string into packet JSON. * * @param {String} obj - encoded packet
(obj: any)
| 179 | */ |
| 180 | |
| 181 | public add(obj: any) { |
| 182 | let packet; |
| 183 | if (typeof obj === class="st">"string") { |
| 184 | if (this.reconstructor) { |
| 185 | throw new Error(class="st">"got plaintext data when reconstructing a packet"); |
| 186 | } |
| 187 | packet = this.decodeString(obj); |
| 188 | const isBinaryEvent = packet.type === PacketType.BINARY_EVENT; |
| 189 | if (isBinaryEvent || packet.type === PacketType.BINARY_ACK) { |
| 190 | packet.type = isBinaryEvent ? PacketType.EVENT : PacketType.ACK; |
| 191 | class="cm">// binary packet's json |
| 192 | this.reconstructor = new BinaryReconstructor(packet); |
| 193 | |
| 194 | class="cm">// no attachments, labeled binary but no binary data to follow |
| 195 | if (packet.attachments === 0) { |
| 196 | super.emitReserved(class="st">"decoded", packet); |
| 197 | } |
| 198 | } else { |
| 199 | class="cm">// non-binary full packet |
| 200 | super.emitReserved(class="st">"decoded", packet); |
| 201 | } |
| 202 | } else if (isBinary(obj) || obj.base64) { |
| 203 | class="cm">// raw binary data |
| 204 | if (!this.reconstructor) { |
| 205 | throw new Error(class="st">"got binary data when not reconstructing a packet"); |
| 206 | } else { |
| 207 | packet = this.reconstructor.takeBinaryData(obj); |
| 208 | if (packet) { |
| 209 | class="cm">// received final buffer |
| 210 | this.reconstructor = null; |
| 211 | super.emitReserved(class="st">"decoded", packet); |
| 212 | } |
| 213 | } |
| 214 | } else { |
| 215 | throw new Error(class="st">"Unknown type: " + obj); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Decode a packet String (JSON data) |