{"record":{"id":"247f79966558a5df","repo":"mozilla/pdf.js","slug":"invalid-data-format-must-be-a-typedarray","errorCode":null,"errorMessage":"Invalid data format, must be a TypedArray.","messagePattern":"Invalid data format, must be a TypedArray\\.","errorType":"exception","errorClass":"JpxError","httpStatus":null,"severity":"error","filePath":"src/core/jpx.js","lineNumber":88,"sourceCode":"        throw new JpxError(\"Unknown error\");\n      }\n      const { imageData } = module;\n      module.imageData = null;\n\n      return imageData;\n    } finally {\n      if (ptr) {\n        module._free(ptr);\n      }\n    }\n  }\n\n  static parseImageProperties(stream) {\n    if (typeof PDFJSDev !== \"undefined\" && PDFJSDev.test(\"IMAGE_DECODERS\")) {\n      if (stream instanceof ArrayBuffer || ArrayBuffer.isView(stream)) {\n        stream = new Stream(stream);\n      } else {\n        throw new JpxError(\"Invalid data format, must be a TypedArray.\");\n      }\n    }\n    // No need to use OpenJPEG here since we're only getting very basic\n    // information which are located in the first bytes of the file.\n    let newByte = stream.getByte();\n    while (newByte >= 0) {\n      const oldByte = newByte;\n      newByte = stream.getByte();\n      const code = (oldByte << 8) | newByte;\n      // Image and tile size (SIZ)\n      if (code === 0xff51) {\n        stream.skip(4);\n        const Xsiz = stream.getInt32() >>> 0; // Byte 4\n        const Ysiz = stream.getInt32() >>> 0; // Byte 8\n        const XOsiz = stream.getInt32() >>> 0; // Byte 12\n        const YOsiz = stream.getInt32() >>> 0; // Byte 16\n        stream.skip(16);\n        const Csiz = stream.getUint16(); // Byte 36","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/jpx.js#L70-L106","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Pass a Uint8Array (or ArrayBuffer) to parseImageProperties in the IMAGE_DECODERS build.","Add a type check before calling: if (!(bytes instanceof Uint8Array)) bytes = new Uint8Array(bytes).","Confirm you're using the correct build variant for your integration."],"exampleFix":"// before\nconst props = JpxImage.parseImageProperties(someStream);\n\n// after\nconst props = JpxImage.parseImageProperties(new Uint8Array(someBuffer));","handlingStrategy":"type-guard","validationCode":"function toBytes(input) {\n  if (input instanceof ArrayBuffer) return new Uint8Array(input);\n  if (ArrayBuffer.isView(input)) return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);\n  throw new TypeError('Expected ArrayBuffer or TypedArray');\n}\nconst props = JpxImage.parseImageProperties(toBytes(stream));","typeGuard":"function isArrayBufferLike(x) {\n  return x instanceof ArrayBuffer || ArrayBuffer.isView(x);\n}","tryCatchPattern":null,"preventionTips":["Always pass a Uint8Array/ArrayBuffer to parseImageProperties in the IMAGE_DECODERS build.","Confirm you're using the correct PDF.js build variant for your use case.","Add a type coercion step before the call to normalize input."],"tags":["jpeg2000","type-validation","image-decoding","jpx"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}