Skip to content

Accept Uint8Array (and other ArrayBuffer views) as image source#1745

Open
JamBalaya56562 wants to merge 1 commit into
foliojs:masterfrom
JamBalaya56562:fix-image-uint8array
Open

Accept Uint8Array (and other ArrayBuffer views) as image source#1745
JamBalaya56562 wants to merge 1 commit into
foliojs:masterfrom
JamBalaya56562:fix-image-uint8array

Conversation

@JamBalaya56562

Copy link
Copy Markdown

Summary

doc.image() accepts Buffer, ArrayBuffer, base64 data URIs and file paths, but a plain Uint8Array (e.g. bytes from S3 / fetch().arrayBuffer()new Uint8Array(...)) was not recognized and fell through to fs.readFileSync(src), throwing:

TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes.

This adds an ArrayBuffer.isView(src) branch after the existing Buffer.isBuffer(src) check (so Node Buffers are unaffected) that wraps typed arrays / DataView via Buffer.from(src.buffer, src.byteOffset, src.byteLength), preserving byteOffset/byteLength so subarray views work too.

Fixes #1446

Tests

Added two regression tests in tests/unit/image.spec.js:

  • a plain Uint8Array source
  • a Uint8Array view with a non-zero byteOffset

All unit tests pass (yarn test:unit, 325 tests); eslint and prettier are clean.

🤖 Generated with Claude Code

doc.image() handled Buffer, ArrayBuffer, base64 data URIs and file
paths, but a plain Uint8Array (e.g. bytes from S3 or fetch) fell
through to fs.readFileSync and threw ERR_INVALID_ARG_VALUE. Add an
ArrayBuffer.isView branch after the Buffer check so typed arrays and
DataView are wrapped via Buffer.from(buffer, byteOffset, byteLength),
preserving byteOffset/byteLength for subarray views.

Fixes foliojs#1446

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PDF image using Uint8Array throws an error

1 participant