mozilla/pdf.js · error · JpxError

Invalid data format, must be a TypedArray.

Error message

Invalid data format, must be a TypedArray.

What it means

Thrown by the static JpxImage.parseImageProperties(stream) but ONLY in the IMAGE_DECODERS build (guarded by PDFJSDev.test('IMAGE_DECODERS')). In that standalone build the method accepts an ArrayBuffer or TypedArray and wraps it in a Stream; anything else is rejected. In the normal PDF.js worker build this branch is compiled out.

Source

Thrown at src/core/jpx.js:88

        throw new JpxError("Unknown error");
      }
      const { imageData } = module;
      module.imageData = null;

      return imageData;
    } finally {
      if (ptr) {
        module._free(ptr);
      }
    }
  }

  static parseImageProperties(stream) {
    if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("IMAGE_DECODERS")) {
      if (stream instanceof ArrayBuffer || ArrayBuffer.isView(stream)) {
        stream = new Stream(stream);
      } else {
        throw new JpxError("Invalid data format, must be a TypedArray.");
      }
    }
    // No need to use OpenJPEG here since we're only getting very basic
    // information which are located in the first bytes of the file.
    let newByte = stream.getByte();
    while (newByte >= 0) {
      const oldByte = newByte;
      newByte = stream.getByte();
      const code = (oldByte << 8) | newByte;
      // Image and tile size (SIZ)
      if (code === 0xff51) {
        stream.skip(4);
        const Xsiz = stream.getInt32() >>> 0; // Byte 4
        const Ysiz = stream.getInt32() >>> 0; // Byte 8
        const XOsiz = stream.getInt32() >>> 0; // Byte 12
        const YOsiz = stream.getInt32() >>> 0; // Byte 16
        stream.skip(16);
        const Csiz = stream.getUint16(); // Byte 36

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Pass a Uint8Array (or ArrayBuffer) to parseImageProperties in the IMAGE_DECODERS build.
  2. Add a type check before calling: if (!(bytes instanceof Uint8Array)) bytes = new Uint8Array(bytes).
  3. Confirm you're using the correct build variant for your integration.

Example fix

// before
const props = JpxImage.parseImageProperties(someStream);

// after
const props = JpxImage.parseImageProperties(new Uint8Array(someBuffer));
Defensive patterns

Strategy: type-guard

Validate before calling

function toBytes(input) {
  if (input instanceof ArrayBuffer) return new Uint8Array(input);
  if (ArrayBuffer.isView(input)) return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
  throw new TypeError('Expected ArrayBuffer or TypedArray');
}
const props = JpxImage.parseImageProperties(toBytes(stream));

Type guard

function isArrayBufferLike(x) {
  return x instanceof ArrayBuffer || ArrayBuffer.isView(x);
}

Prevention

When it happens

Trigger: In the IMAGE_DECODERS build, calling JpxImage.parseImageProperties(x) where x is not an ArrayBuffer and not an ArrayBuffer.isView (e.g. a plain object, a number, a string).

Common situations: Using the standalone pdf.js components build (image_decoders) and passing the wrong type — e.g. a Stream object or a raw object — instead of a TypedArray/ArrayBuffer. Won't occur in the standard worker path.

Related errors


AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13). Data as JSON: /api/errors/247f79966558a5df. Report an issue: GitHub.