{"id":"7596c6e483c692fb","repo":"socketio/socket.io","slug":"unknown-packet-type-p-type","errorCode":null,"errorMessage":"unknown packet type ${p.type}","messagePattern":"unknown packet type (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/socket.io-parser/lib/index.ts","lineNumber":228,"sourceCode":"      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\n   */\n  private decodeString(str): Packet {\n    let i = 0;\n    // look up type\n    const p: any = {\n      type: Number(str.charAt(0)),\n    };\n\n    if (PacketType[p.type] === undefined) {\n      throw new Error(\"unknown packet type \" + p.type);\n    }\n\n    // look up attachments if type binary\n    if (\n      p.type === PacketType.BINARY_EVENT ||\n      p.type === PacketType.BINARY_ACK\n    ) {\n      const start = i + 1;\n      while (str.charAt(++i) !== \"-\" && i != str.length) {}\n      const buf = str.substring(start, i);\n      if (buf != Number(buf) || str.charAt(i) !== \"-\") {\n        throw new Error(\"Illegal attachments\");\n      }\n      const n = Number(buf);\n      if (!isInteger(n) || n < 1) {\n        throw new Error(\"Illegal attachments\");\n      } else if (n > this.opts.maxAttachments) {\n        throw new Error(\"too many attachments\");","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/socketio/socket.io/blob/ae7fb46e08c5ed964b4a1ea8b1703e816511598e/packages/socket.io-parser/lib/index.ts#L210-L246","documentation":"Thrown by decodeString() when the first character of an encoded packet does not map to a known PacketType (CONNECT=0, DISCONNECT=1, EVENT=2, ACK=3, CONNECT_ERROR=4, BINARY_EVENT=5, BINARY_ACK=6). The leading byte must be a valid type id; anything else is an unknown packet type.","triggerScenarios":"Feeding the decoder a string whose first character is not 0-6, e.g. '9...', 'x...', or an empty/garbled frame where Number(str.charAt(0)) has no matching PacketType enum entry.","commonSituations":"A transport delivering truncated or corrupted frames; an Engine.IO version mismatch where the leading byte means something different; or a custom producer emitting raw JSON without the type prefix.","solutions":["Verify the encoder and decoder use the same Socket.IO protocol version so the leading type byte is consistent.","Strip any Engine.IO wrapping/prefix correctly before handing the inner packet string to the Socket.IO decoder.","Guard against empty/garbled frames at the transport layer and drop them before decoding."],"exampleFix":"// before\ndecoder.add(raw); // raw has bad type byte -> 'unknown packet type 9'\n\n// after\nif (!/^[0-6]/.test(raw)) {\n  return; // drop invalid frame\n}\ndecoder.add(raw);","handlingStrategy":"validation","validationCode":"const VALID_TYPES = new Set([0,1,2,3,4,5,6]);\nfunction validTypeByte(s){ return VALID_TYPES.has(Number(s.charAt(0))); }","typeGuard":"function isKnownPacketType(t){ return [0,1,2,3,4,5,6].includes(Number(t)); }","tryCatchPattern":"try { decoder.add(raw); }\ncatch(e){ if(/unknown packet type/.test(e.message)){ return; } throw e; }","preventionTips":["Match encoder/decoder Socket.IO protocol versions.","Strip Engine.IO framing before handing the inner string to the decoder.","Drop empty/garbled frames at the transport layer."],"tags":["parser","decoder","protocol","packet-type"],"analyzedSha":"ae7fb46e08c5ed964b4a1ea8b1703e816511598e","analyzedAt":"2026-08-03T19:08:14.127Z","schemaVersion":2}