Skip to content

Bugfix: Handle TypedArrays and Buffers in deep-merge/clone#1744

Draft
ykw9263 wants to merge 5 commits into
foliojs:masterfrom
ykw9263:bugfix/table-deep-merged-typedarray
Draft

Bugfix: Handle TypedArrays and Buffers in deep-merge/clone#1744
ykw9263 wants to merge 5 commits into
foliojs:masterfrom
ykw9263:bugfix/table-deep-merged-typedarray

Conversation

@ykw9263

@ykw9263 ykw9263 commented Jun 25, 2026

Copy link
Copy Markdown

What kind of change does this PR introduce?
This fixes a bug mentioned in #1743 where providing font buffers crashes table generation.

In lib\table\utils.js, deepMerge and deepClone() copy properties key by key including Buffer and Uint8Array without inheriting the class. This causes the merged font buffer failing to be recognised by PDFFontFactory which relies on instanceof to determine the type of font sources.

This PR changes the behaviour of deepMerge() and deepClone() to clone TypedArrays and Buffers using .slice() and Buffer.from(buffer) respectively.

Additional unit tests (fonts in tables, deep-merging buffers) are added for the changes.

Checklist:

  • Unit Tests
  • Documentation
  • Update CHANGELOG.md
  • Ready to be merged

TODO:

@ykw9263 ykw9263 marked this pull request as draft June 25, 2026 17:33
Comment thread lib/table/utils.js
function deepClone(obj) {
let result = obj;
if (obj && typeof obj == 'object') {
if (isTypedArray(obj)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where deepClone is called in this code path?

I would like to avoid cloning buffer / UintArray in name of efficiency

Comment thread lib/table/utils.js
return (
isObject(item) &&
item.length !== undefined &&
(item instanceof Uint8Array ||

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK we only support Uint8Array

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.

2 participants