Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shaggy-dogs-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-pdf/pdfkit": patch
---

refactor: align pdfkit/reference.js with upstream with modern Buffer polyfill
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@rollup/plugin-alias": "^5.1.0",
"@rollup/plugin-babel": "^6.0.0",
"@rollup/plugin-commonjs": "^25.0.0",
"@rollup/plugin-inject": "^5.0.4",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.0",
"@rollup/plugin-replace": "^5.0.0",
Expand Down
5 changes: 3 additions & 2 deletions packages/pdfkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@
],
"dependencies": {
"@babel/runtime": "^7.20.13",
"browserify-zlib": "^0.2.0",
"@noble/ciphers": "^1.0.0",
"@noble/hashes": "^1.6.0",
"browserify-zlib": "^0.2.0",
"fontkit": "^2.0.2",
"js-md5": "^0.8.3",
"linebreak": "^1.1.0",
"png-js": "^2.0.0",
"vite-compatible-readable-stream": "^3.6.1"
},
"devDependencies": {
"iconv-lite": "^0.4.13"
"iconv-lite": "^0.4.13",
"node-stdlib-browser": "^1.3.1"
}
}
43 changes: 27 additions & 16 deletions packages/pdfkit/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import replace from '@rollup/plugin-replace';
import nodeResolve from '@rollup/plugin-node-resolve';
import ignore from 'rollup-plugin-ignore';
import alias from '@rollup/plugin-alias';
import nodePolyfills from 'rollup-plugin-polyfill-node';
import inject from '@rollup/plugin-inject';
import commonjs from '@rollup/plugin-commonjs';
import { createRequire } from 'module';

import pkg from './package.json' with { type: 'json' };

const require = createRequire(import.meta.url);
const stdLibBrowser = require('node-stdlib-browser');

const input = 'src/index.js';

const babelConfig = () => ({
Expand All @@ -31,30 +35,37 @@ const getExternal = ({ browser }) => [
...(browser ? [] : ['fs'])
];

// node-stdlib-browser entries are absolute paths (they point at installed
// packages); these get bundled inline so the absolute paths are fine. Aliases
// for externalized modules (pako/*) MUST stay as bare specifiers so the
// consumer's bundler can resolve them.
const browserAliasEntries = {
...stdLibBrowser,
// Override stdLibBrowser's stream-browserify with the vite-compatible variant
// (bundled inline; nodeResolve will pick it up).
stream: 'vite-compatible-readable-stream',
// pako sub-paths fail to resolve without explicit .js suffix; see
// https://github.com/browserify/browserify-zlib/pull/45
'pako/lib/zlib/zstream': 'pako/lib/zlib/zstream.js',
'pako/lib/zlib/constants': 'pako/lib/zlib/constants.js',
};

const getPlugins = ({ browser }) => [
json(),
...(browser
? [
ignore(['fs']),
alias({
entries: [
// See https://github.com/browserify/browserify-zlib/pull/45
{
find: 'pako/lib/zlib/zstream',
replacement: 'pako/lib/zlib/zstream.js'
},
{
find: 'pako/lib/zlib/constants',
replacement: 'pako/lib/zlib/constants.js'
},
{ find: 'stream', replacement: 'vite-compatible-readable-stream' },
{ find: 'zlib', replacement: 'browserify-zlib' }
]
entries: Object.entries(browserAliasEntries).map(([find, replacement]) => ({
find,
replacement
}))
}),
commonjs(),
nodeResolve({ browser, preferBuiltins: !browser }),
nodePolyfills({
include: [/node_modules\/.+\.js/, /pdfkit\/src\/.*\.js/]
inject({
Buffer: [stdLibBrowser.buffer, 'Buffer'],
process: stdLibBrowser.process
})
]
: [nodeResolve({ browser, preferBuiltins: !browser })]),
Expand Down
85 changes: 30 additions & 55 deletions packages/pdfkit/src/reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,22 @@ By Devon Govett
*/

import zlib from 'zlib';
import stream from 'stream';
import PDFAbstractReference from './abstract_reference';
import PDFObject from './object';

class PDFReference extends stream.Writable {
constructor(document, id, data) {
super({ decodeStrings: false });

this.finalize = this.finalize.bind(this);
class PDFReference extends PDFAbstractReference {
constructor(document, id, data = {}) {
super();
this.document = document;
this.id = id;
if (data == null) {
data = {};
}
this.data = data;

this.gen = 0;
this.deflate = null;
this.compress = this.document.compress && !this.data.Filter;
this.uncompressedLength = 0;
this.chunks = [];
this.buffer = [];
}

initDeflate() {
this.data.Filter = 'FlateDecode';

this.deflate = zlib.createDeflate();
this.deflate.on('data', (chunk) => {
this.chunks.push(chunk);
return (this.data.Length += chunk.length);
});

return this.deflate.on('end', this.finalize);
}

_write(chunk, encoding, callback) {
write(chunk) {
if (!(chunk instanceof Uint8Array)) {
chunk = Buffer.from(chunk + '\n', 'binary');
}
Expand All @@ -47,28 +28,18 @@ class PDFReference extends stream.Writable {
if (this.data.Length == null) {
this.data.Length = 0;
}

this.buffer.push(chunk);
this.data.Length += chunk.length;
if (this.compress) {
if (!this.deflate) {
this.initDeflate();
}
this.deflate.write(chunk);
} else {
this.chunks.push(chunk);
this.data.Length += chunk.length;
this.data.Filter = 'FlateDecode';
}

return callback();
}

end() {
super.end(...arguments);

if (this.deflate) {
return this.deflate.end();
end(chunk) {
if (chunk) {
this.write(chunk);
}

return this.finalize();
this.finalize();
}

finalize() {
Expand All @@ -78,29 +49,33 @@ class PDFReference extends stream.Writable {
? this.document._security.getEncryptFn(this.id, this.gen)
: null;

if (this.chunks.length) {
let buffer = Buffer.concat(this.chunks);
if (this.buffer.length) {
this.buffer = Buffer.concat(this.buffer);
if (this.compress) {
this.buffer = zlib.deflateSync(this.buffer);
}

if (encryptFn) {
buffer = encryptFn(buffer);
this.buffer = encryptFn(this.buffer);
}

this.data.Length = buffer.length;
this.document._write(`${this.id} ${this.gen} obj`);
this.document._write(PDFObject.convert(this.data, encryptFn));
this.data.Length = this.buffer.length;
}

this.document._write(`${this.id} ${this.gen} obj`);
this.document._write(PDFObject.convert(this.data, encryptFn));

if (this.buffer.length) {
this.document._write('stream');
this.document._write(buffer);
this.chunks.length = 0;
this.document._write(this.buffer);

this.buffer = []; // free up memory
this.document._write('\nendstream');
} else {
this.document._write(`${this.id} ${this.gen} obj`);
this.document._write(PDFObject.convert(this.data, encryptFn));
}

this.document._write('endobj');
return this.document._refEnd(this);
this.document._refEnd(this);
}

toString() {
return `${this.id} ${this.gen} R`;
}
Expand Down
Loading
Loading