{"id":"f5c27a231f017af7","repo":"socketio/socket.io","slug":"got-binary-data-when-not-reconstructing-a-packet","errorCode":null,"errorMessage":"got binary data when not reconstructing a packet","messagePattern":"got binary data when not reconstructing a packet","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/socket.io-parser/lib/index.ts","lineNumber":200,"sourceCode":"    let packet;\n    if (typeof obj === \"string\") {\n      if (this.reconstructor) {\n        throw new Error(\"got plaintext data when reconstructing a packet\");\n      }\n      packet = this.decodeString(obj);\n      const isBinaryEvent = packet.type === PacketType.BINARY_EVENT;\n      if (isBinaryEvent || packet.type === PacketType.BINARY_ACK) {\n        packet.type = isBinaryEvent ? PacketType.EVENT : PacketType.ACK;\n        // binary packet's json\n        this.reconstructor = new BinaryReconstructor(packet);\n      } else {\n        // non-binary full packet\n        super.emitReserved(\"decoded\", packet);\n      }\n    } else if (isBinary(obj) || obj.base64) {\n      // raw binary data\n      if (!this.reconstructor) {\n        throw new Error(\"got binary data when not reconstructing a packet\");\n      } else {\n        packet = this.reconstructor.takeBinaryData(obj);\n        if (packet) {\n          // received final buffer\n          this.reconstructor = null;\n          super.emitReserved(\"decoded\", packet);\n        }\n      }\n    } else {\n      throw new Error(\"Unknown type: \" + obj);\n    }\n  }\n\n  /**\n   * Decode a packet String (JSON data)\n   *\n   * @param {String} str\n   * @return {Object} packet","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/socketio/socket.io/blob/ae7fb46e08c5ed964b4a1ea8b1703e816511598e/packages/socket.io-parser/lib/index.ts#L182-L218","documentation":"Thrown by the parser Decoder.add() when a binary blob/buffer arrives but no BinaryReconstructor is active (this.reconstructor is null). Binary data is only meaningful while reconstructing a previously announced BINARY_EVENT/BINARY_ACK packet; unsolicited binary frames violate the protocol.","triggerScenarios":"Calling decoder.add(someArrayBuffer) (or any isBinary/base64 object) before any BINARY_EVENT/BINARY_ACK string packet has initialized a reconstructor.","commonSituations":"A transport that sends binary WebSocket frames outside the Socket.IO binary-attachment handshake (e.g. raw ping/pong data, or a misconfigured engine.io); or feeding test fixtures binary data without a preceding binary packet header.","solutions":["Confirm the server is emitting events with binary content via the Socket.IO parser so it always sends a BINARY_EVENT header first.","Make sure a string BINARY_EVENT packet is fed to add() before any binary buffers.","Filter out non-Socket.IO binary frames at the transport layer before they reach the decoder."],"exampleFix":"// before\ndecoder.add(arrayBuffer); // throws: no reconstructor\n\n// after\n// first emit/forward the BINARY_EVENT string packet that declares the attachments\ndecoder.add(binaryEventString); // initializes reconstructor\ndecoder.add(arrayBuffer); // now valid","handlingStrategy":"validation","validationCode":"function canAcceptBinary(decoder){ return !!decoder.reconstructor; }","typeGuard":"function isBinaryFrame(obj){ return obj instanceof ArrayBuffer || Buffer.isBuffer(obj) || obj instanceof Blob || (obj&&obj.base64); }","tryCatchPattern":"try { decoder.add(buf); }\ncatch(e){ if(/binary data when not reconstructing/.test(e.message)) return; else throw e; }","preventionTips":["Only forward binary frames after a BINARY_EVENT header initialized the reconstructor.","Filter raw non-Socket.IO binary frames at the transport layer.","Feed the binary packet string via add() before its buffers."],"tags":["parser","binary","protocol","decoder"],"analyzedSha":"ae7fb46e08c5ed964b4a1ea8b1703e816511598e","analyzedAt":"2026-08-03T19:08:14.127Z","schemaVersion":2}