Skip to content

refactor: use Uint8Array instead of Buffer - #1764

Open
diegomura wants to merge 1 commit into
masterfrom
diegomura/replace-buffer-with-uint8array
Open

refactor: use Uint8Array instead of Buffer#1764
diegomura wants to merge 1 commit into
masterfrom
diegomura/replace-buffer-with-uint8array

Conversation

@diegomura

@diegomura diegomura commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

What kind of change does this PR introduce?

Nothing under lib/ uses Node's Buffer anymore.

Latin1 and UTF-16BE encoding, hex and base64 conversion, concatenation and fixed-size allocation all go through Uint8Array and a few helpers in lib/binary.js now.

This is part of modernizing the library, and follows the fflate and base64 changes. Buffer is a Node builtin that bundlers no longer polyfill by default, and it was the last one left in code that has to run in a browser.

It is not a breaking change. Buffer is a Uint8Array subclass, so passing one to image(), file() or registerFont() works as before, and Readable.push() converts Uint8Array to Buffer zero-copy, so Node consumers still read Buffer chunks off the document stream.

pdfkit/virtual-fs now returns a Uint8Array from readFileSync(). Encoded reads still return strings.

A dict value that is a plain Uint8Array now serializes to a hex string. The check was Buffer.isBuffer(), so it used to fall through to String(obj) and write 1,2,3.

TextDecoder('latin1') is not a valid replacement for the String.fromCharCode loops. Node maps latin1, iso-8859-1 and windows-1252 all to ISO-8859-1, but the WHATWG Encoding Standard makes all three windows-1252, where 0x80 decodes to U+20AC. Browsers follow the spec, so it would corrupt 0x80-0x9F in the browser build.

@diegomura diegomura changed the title Use Uint8Array instead of Buffer refactor: use Uint8Array instead of Buffer Aug 12, 2026
@diegomura
diegomura requested a review from blikblum August 12, 2026 23:13
@diegomura diegomura self-assigned this Aug 12, 2026
diegomura added a commit to diegomura/react-pdf that referenced this pull request Aug 12, 2026
…t#1764

Adopts upstream's naming and shapes so the files stay mergeable: concat
instead of concatBytes, toUTF16BE writing its own BOM, fromBinaryString
masking with 0xff, and the restructured encrypt branch in object.js.

Restores src/virtual-fs.js with upstream's Uint8Array version. It is
unreachable in this fork (not imported, not exported, absent from both
bundles), but keeping it avoids a needless divergence.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Buffer is a Uint8Array subclass, and Readable.push() converts Uint8Array
to Buffer zero-copy, so Buffer inputs and Buffer output chunks keep
working on Node.
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.

1 participant