BabylonJS/Babylon.js · error

deflate: invalid code length repeat

Error message

deflate: invalid code length repeat

What it means

Code-length symbol 16 repeats the previous code length 3-6 times, which is illegal when there is no previous length (it would be the very first decoded entry). This throw means the dynamic block's code-length sequence starts (or resumes) with a repeat-before-any-value pattern, indicating corrupt bitstream or misaligned reading.

Source

Thrown at packages/dev/loaders/src/FBX/parsers/zlibInflate.ts:378

        distanceTree: new HuffmanTree(distanceLengths, { allowEmpty: true }),
    };
}

function readCodeLengths(reader: BitReader, codeLengthTree: HuffmanTree, count: number): number[] {
    const lengths: number[] = [];
    while (lengths.length < count) {
        const symbol = codeLengthTree.decode(reader);
        if (symbol <= 15) {
            lengths.push(symbol);
            continue;
        }

        let repeatLength: number;
        let repeatedValue: number;
        switch (symbol) {
            case 16:
                if (lengths.length === 0) {
                    throw new Error("deflate: invalid code length repeat");
                }
                repeatedValue = lengths[lengths.length - 1];
                repeatLength = reader.readBits(2) + 3;
                break;
            case 17:
                repeatedValue = 0;
                repeatLength = reader.readBits(3) + 3;
                break;
            case 18:
                repeatedValue = 0;
                repeatLength = reader.readBits(7) + 11;
                break;
            default:
                throw new Error("deflate: invalid code length symbol");
        }

        if (lengths.length + repeatLength > count) {
            throw new Error("deflate: invalid code length repeat");

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Test the extracted payload with python zlib.decompress — if it fails there too, the file is corrupt and must be re-exported/re-downloaded
  2. Verify the property-header parsing produces the correct payload offset (payload must begin at the zlib header, immediately after the array length/encoding fields)
  3. Check the file's byte size against the exporter's output to detect truncation
  4. Compare behavior against the same asset exported without compression (FBX Encoding=0) to isolate the inflater
Defensive patterns

Strategy: validation

Validate before calling

// Verify payload is well-formed zlib before custom inflation
const ref = decompressSync(payload); // throws if corrupt
if (ref.byteLength !== expectedLength) throw new Error("Decoded size mismatch");

Try / catch

try {
  const out = inflateZlib(payload, expectedLength);
} catch (e) {
  if (e instanceof Error && e.message === "deflate: invalid code length repeat") {
    throw new Error("FBX compressed array corrupt (bad code-length repeat)", { cause: e });
  }
  throw e;
}

Prevention

When it happens

Trigger: readCodeLengths (called from readDynamicTrees during inflateZlib) decodes symbol 16 from the codeLengthTree when lengths.length === 0 — no prior code length exists to repeat.

Common situations: Corrupt FBX files; payload sliced from the wrong offset so the code-length tree is decoded from garbage; truncated downloads losing part of the dynamic header.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/48b33b05fd47e28c. Report an issue: GitHub.