Accept Uint8Array (and other ArrayBuffer views) as image source#1745
Open
JamBalaya56562 wants to merge 1 commit into
Open
Accept Uint8Array (and other ArrayBuffer views) as image source#1745JamBalaya56562 wants to merge 1 commit into
JamBalaya56562 wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
doc.image()acceptsBuffer,ArrayBuffer, base64 data URIs and file paths, but a plainUint8Array(e.g. bytes from S3 /fetch().arrayBuffer()→new Uint8Array(...)) was not recognized and fell through tofs.readFileSync(src), throwing:This adds an
ArrayBuffer.isView(src)branch after the existingBuffer.isBuffer(src)check (so NodeBuffers are unaffected) that wraps typed arrays /DataViewviaBuffer.from(src.buffer, src.byteOffset, src.byteLength), preservingbyteOffset/byteLengthso subarray views work too.Fixes #1446
Tests
Added two regression tests in
tests/unit/image.spec.js:Uint8ArraysourceUint8Arrayview with a non-zerobyteOffsetAll unit tests pass (
yarn test:unit, 325 tests); eslint and prettier are clean.🤖 Generated with Claude Code