Dm/drop node dependencies#3397
Draft
diegomura wants to merge 4 commits into
Draft
Conversation
Introduce src/binary.js with Uint8Array-native helpers and port the mechanical callers (object, document._write, png, attachments, metadata, security) off Node's Buffer. image.js/jpeg.js/reference.js still use Buffer and will be migrated in a follow-up pass. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
… reference Convert image.js, image/jpeg.js, and reference.js to use binary.js helpers instead of Node Buffer methods. Removes another chunk of Buffer dependency on the path to a Node-free pdfkit core. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Replace Node's zlib with pako in png.js and reference.js. Reference.js now accumulates raw chunks and deflates once at finalize, dropping the streaming zlib.Deflate pipeline. line_wrapper.js gains a tiny inline emitter so it no longer imports Node's 'events' module while keeping its on/once/emit public API intact. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
PDFDocument now extends a local MiniReadable class that implements only what the renderer relies on (on/once/emit/push/pipe). PDFReference no longer extends stream.Writable — write/end are direct methods. Drops the vite-compatible-readable-stream dep and its rollup alias. Co-Authored-By: Claude Opus 4.7 <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.
Upstream pdfkit PR plan
Incremental, backwards-compatible PRs to propose to
foliojs/pdfkit. Each PR stands alone
and delivers value independently — none touch the public class hierarchy
(
PDFDocument extends stream.Readable,PDFReference extends stream.Writable).Scoped out (stays in fork)
stream.Readable/stream.Writablewith a local shim — changespublic class hierarchy and breaks
ref.write(chunk, encoding, cb)callers.ref._write(chunk, encoding, cb)→ref.write(chunk)— same reason.PR 1 — Refactor JPEG/EXIF parser to work on raw bytes
Value: EXIF parser currently uses
Buffer.readUInt16BE/LE,subarray().toString('binary'|'ascii'). Abstracting to byte-level helpersmakes the parser work on any
Uint8Array, with clearer intent and nointermediate string allocations.
Adds:
src/binary.jswithreadUInt16BE/LE,readUInt32BE/LE,toBinaryString— each helper justified by this PR's use sites.Changes:
image/jpeg.jsRisk: zero — byte-identical output.
PR 2 — Drop
Buffer.alloc/Buffer.frominpng.jsinternalsValue:
new Uint8Array(n)matches what's being built (raw byteallocations).
zlib.deflateSyncacceptsUint8Arrayin modern Node. Smallersurface area.
Changes:
image/png.jsRisk: zero — Node
Bufferis aUint8Arraysubclass.PR 3 — Use byte-level PNG magic check in
image.jsValue: Replaces
data.toString('ascii', 1, 4) === 'PNG'with four bytecomparisons — avoids a string allocation per image open. Adds
fromBase64to
binary.jsfor the data-URI path (justified by use).Changes:
image.js; extendsbinary.jswithfromBase64.Risk: zero.
PR 4 — Remove redundant
Buffer.from()wrappers insecurity.jsValue:
@noble/ciphers,js-md5, andrc4already returnUint8Array.Buffer.from(uint8array)is a zero-copy view that adds nothing. Drops~a dozen wrappers; smaller and faster.
Changes:
security.jsRisk: low —
encDict.O/U/OE/UE/Permsvalues now stored asUint8Array.They flow into
PDFObject.convertwhich handlesUint8Arrayvia theBuffer.isBuffercheck — needs verification on upstream. Add aninstanceof Uint8Arrayco-equal test (purely additive) if needed.PR 5 — Simplify PDF hex/binary serialization with byte helpers
Value:
object.jshas scatteredBuffer.from(str, 'ascii'),Buffer.from(utf16str, 'utf16le') + swapBytes, and.toString('hex')calls.Replacing them with named helpers (
fromBinaryString,fromUtf16BEWithBOM,toHex) removes theswapByteshelper entirely and is moreself-documenting.
Adds:
fromBinaryString,fromUtf16BEWithBOM,toHex,fromUtf8Stringto
binary.js.Changes:
object.jsRisk: low — output byte-identical; needs test coverage for the UTF-16BE
path.
PR 6 — Replace
eventsimport inline_wrapper.jswith inline emitterValue:
LineWrapperonly useson/once/emitinternally. A ~20-lineinline emitter drops one Node-stdlib import and makes the event surface
explicit in the file rather than inherited.
Changes:
line_wrapper.jsRisk: low — public
on/once/emitAPI preserved.PR 7 — Swap
zlibforpakoValue: Pure JS, identical output, eliminates Node's
zlibfrom thebrowser bundle (no polyfill needed). Single dep swap.
Changes:
image/png.js,reference.js(streaming deflate → one-shot atfinalize; simpler),
package.json.Risk: medium —
reference.js's streaming pipeline becomes buffered. Forvery large streams, peak memory differs. Benchmark in the PR.