Skip to content

Commit 494459f

Browse files
committed
Update reference.js with upstream version
Syncs up with the upstream version at: https://github.com/foliojs/pdfkit/blob/5635f8a02135acae1a6c1b904af55afe05d0ab7e/lib/security.js
1 parent 370fade commit 494459f

File tree

1 file changed

+29
-49
lines changed

1 file changed

+29
-49
lines changed

packages/pdfkit/src/reference.js

+29-49
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,37 @@
11
import zlib from 'zlib';
2-
import stream from 'stream';
32
import PDFObject from './object';
43

5-
class PDFReference extends stream.Writable {
6-
constructor(document, id, data) {
7-
super({ decodeStrings: false });
8-
9-
this.finalize = this.finalize.bind(this);
4+
class PDFReference {
5+
constructor(document, id, data = {}) {
106
this.document = document;
117
this.id = id;
12-
if (data == null) {
13-
data = {};
14-
}
158
this.data = data;
16-
179
this.gen = 0;
18-
this.deflate = null;
1910
this.compress = this.document.compress && !this.data.Filter;
2011
this.uncompressedLength = 0;
21-
this.chunks = [];
12+
this.buffer = [];
2213
}
2314

24-
initDeflate() {
25-
this.data.Filter = 'FlateDecode';
26-
27-
this.deflate = zlib.createDeflate();
28-
this.deflate.on('data', (chunk) => {
29-
this.chunks.push(chunk);
30-
return (this.data.Length += chunk.length);
31-
});
32-
33-
return this.deflate.on('end', this.finalize);
34-
}
35-
36-
_write(chunk, encoding, callback) {
37-
if (!(chunk instanceof Uint8Array)) {
15+
write(chunk) {
16+
if (!Buffer.isBuffer(chunk)) {
3817
chunk = Buffer.from(chunk + '\n', 'binary');
3918
}
4019

4120
this.uncompressedLength += chunk.length;
4221
if (this.data.Length == null) {
4322
this.data.Length = 0;
4423
}
45-
24+
this.buffer.push(chunk);
25+
this.data.Length += chunk.length;
4626
if (this.compress) {
47-
if (!this.deflate) {
48-
this.initDeflate();
49-
}
50-
this.deflate.write(chunk);
51-
} else {
52-
this.chunks.push(chunk);
53-
this.data.Length += chunk.length;
27+
return (this.data.Filter = 'FlateDecode');
5428
}
55-
56-
return callback();
5729
}
5830

59-
end() {
60-
super.end(...arguments);
61-
62-
if (this.deflate) {
63-
return this.deflate.end();
31+
end(chunk) {
32+
if (chunk) {
33+
this.write(chunk);
6434
}
65-
6635
return this.finalize();
6736
}
6837

@@ -72,23 +41,34 @@ class PDFReference extends stream.Writable {
7241
const encryptFn = this.document._security
7342
? this.document._security.getEncryptFn(this.id, this.gen)
7443
: null;
44+
45+
if (this.buffer.length) {
46+
this.buffer = Buffer.concat(this.buffer);
47+
if (this.compress) {
48+
this.buffer = zlib.deflateSync(this.buffer);
49+
}
50+
51+
if (encryptFn) {
52+
this.buffer = encryptFn(this.buffer);
53+
}
54+
55+
this.data.Length = this.buffer.length;
56+
}
57+
7558
this.document._write(`${this.id} ${this.gen} obj`);
7659
this.document._write(PDFObject.convert(this.data, encryptFn));
7760

78-
if (this.chunks.length) {
61+
if (this.buffer.length) {
7962
this.document._write('stream');
80-
for (let chunk of Array.from(this.chunks)) {
81-
this.document._write(chunk);
82-
}
63+
this.document._write(this.buffer);
8364

84-
this.chunks.length = 0; // free up memory
65+
this.buffer = []; // free up memory
8566
this.document._write('\nendstream');
8667
}
8768

8869
this.document._write('endobj');
89-
return this.document._refEnd(this);
70+
this.document._refEnd(this);
9071
}
91-
9272
toString() {
9373
return `${this.id} ${this.gen} R`;
9474
}

0 commit comments

Comments
 (0)