BabylonJS/Babylon.js · error

zlib: unexpected end of input

Error message

zlib: unexpected end of input

What it means

inflateZlib requires at least 6 bytes (2-byte zlib header + 4-byte Adler-32 trailer) and throws 'zlib: unexpected end of input' when the compressed buffer is shorter. The compressed payload passed from parseArrayProperty is too small to be a valid zlib stream.

Source

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

const LENGTH_BASE = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258];
const LENGTH_EXTRA_BITS = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0];
const DISTANCE_BASE = [1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577];
const DISTANCE_EXTRA_BITS = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13];
const CODE_LENGTH_ORDER = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];

/**
 * Inflate a zlib-wrapped deflate stream.
 *
 * This implementation is intentionally scoped to FBX binary array payloads: one-shot,
 * synchronous zlib streams with the exact uncompressed length known up front.
 */
export function inflateZlib(input: Uint8Array, expectedLength: number): Uint8Array {
    if (!Number.isInteger(expectedLength) || expectedLength < 0) {
        throw new Error("zlib: invalid expected length");
    }
    if (input.byteLength < 6) {
        throw new Error("zlib: unexpected end of input");
    }

    const cmf = input[0];
    const flg = input[1];
    if ((cmf & 0x0f) !== 8 || cmf >> 4 > 7 || ((cmf << 8) + flg) % 31 !== 0) {
        throw new Error("zlib: invalid header");
    }
    if ((flg & 0x20) !== 0) {
        throw new Error("zlib: preset dictionary not supported");
    }

    const reader = new BitReader(input, 2, input.byteLength - 4);
    const output = new OutputWriter(expectedLength);

    let isFinalBlock = false;
    while (!isFinalBlock) {
        isFinalBlock = reader.readBits(1) === 1;
        const blockType = reader.readBits(2);

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Verify the FBX file is complete (size/hash) and re-download/re-export if truncated
  2. Check that parseArrayProperty slices the compressed data from the correct offset with the declared compressedLength
  3. Validate the file with a reference FBX reader to confirm the compressed record is intact
  4. Catch this error and report a corrupt-file error instead of a raw parser failure

Example fix

// before
const compressed = bytes.slice(offset, offset + len); inflateZlib(compressed, expected);
// after
if (len < 6) throw new Error(`Compressed FBX array truncated (${len} bytes)`);
const compressed = bytes.slice(offset, offset + len); inflateZlib(compressed, expected);
Defensive patterns

Strategy: validation

Validate before calling

if (compressed.byteLength < 6) {
  throw new Error(`FBX compressed array too small: ${compressed.byteLength} bytes`);
}
inflateZlib(compressed, expectedLength);

Type guard

function isPlausibleZlibBuffer(b: Uint8Array): boolean {
  return b.byteLength >= 6;
}

Try / catch

try {
  return inflateZlib(compressed, expectedLength);
} catch (e) {
  if ((e as Error).message === 'zlib: unexpected end of input') {
    throw new Error('Compressed FBX array payload truncated', { cause: e });
  }
  throw e;
}

Prevention

When it happens

Trigger: Compressed array property whose compressedLength is < 6 (e.g., 0-5 bytes) after the FBX record header claimed compression (encoding 1); a slice taken at the wrong offset yielding a tiny buffer.

Common situations: Truncated FBX files where the compressed array was cut off; wrong byte offset when slicing the compressed region; a file where an array is marked compressed but contains only a stub.

Understand the failure class

Related errors


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