* Decode a packet String (JSON data) * * @param {String} str * @return {Object} packet
(str)
| 223 | * @return {Object} packet |
| 224 | */ |
| 225 | private decodeString(str): Packet { |
| 226 | let i = 0; |
| 227 | // look up type |
| 228 | const p: any = { |
| 229 | type: Number(str.charAt(0)), |
| 230 | }; |
| 231 | |
| 232 | if (PacketType[p.type] === undefined) { |
| 233 | throw new Error("unknown packet type " + p.type); |
| 234 | } |
| 235 | |
| 236 | // look up attachments if type binary |
| 237 | if ( |
| 238 | p.type === PacketType.BINARY_EVENT || |
| 239 | p.type === PacketType.BINARY_ACK |
| 240 | ) { |
| 241 | const start = i + 1; |
| 242 | while (str.charAt(++i) !== "-" && i != str.length) {} |
| 243 | const buf = str.substring(start, i); |
| 244 | if (buf != Number(buf) || str.charAt(i) !== "-") { |
| 245 | throw new Error("Illegal attachments"); |
| 246 | } |
| 247 | const n = Number(buf); |
| 248 | if (!isInteger(n) || n < 0) { |
| 249 | throw new Error("Illegal attachments"); |
| 250 | } else if (n > this.opts.maxAttachments) { |
| 251 | throw new Error("too many attachments"); |
| 252 | } |
| 253 | p.attachments = n; |
| 254 | } |
| 255 | |
| 256 | // look up namespace (if any) |
| 257 | if ("/" === str.charAt(i + 1)) { |
| 258 | const start = i + 1; |
| 259 | while (++i) { |
| 260 | const c = str.charAt(i); |
| 261 | if ("," === c) break; |
| 262 | if (i === str.length) break; |
| 263 | } |
| 264 | p.nsp = str.substring(start, i); |
| 265 | } else { |
| 266 | p.nsp = "/"; |
| 267 | } |
| 268 | |
| 269 | // look up id |
| 270 | const next = str.charAt(i + 1); |
| 271 | if ("" !== next && Number(next) == next) { |
| 272 | const start = i + 1; |
| 273 | while (++i) { |
| 274 | const c = str.charAt(i); |
| 275 | if (null == c || Number(c) != c) { |
| 276 | --i; |
| 277 | break; |
| 278 | } |
| 279 | if (i === str.length) break; |
| 280 | } |
| 281 | p.id = Number(str.substring(start, i + 1)); |
| 282 | } |
no test coverage detected