pdf-lite 1.6.2 → 1.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,6 +16,9 @@ export class PdfDictionary extends PdfObject {
16
16
  }
17
17
  }
18
18
  }
19
+ keys() {
20
+ return Array.from(this.#entries.keys());
21
+ }
19
22
  get(key) {
20
23
  if (key instanceof PdfName) {
21
24
  return this.#entries.get(key);
@@ -131,6 +134,20 @@ export class PdfDictionary extends PdfObject {
131
134
  new PdfEndDictionaryToken(),
132
135
  ];
133
136
  }
137
+ /** Factory-style type conversion: constructs a new instance passing `this` as the first argument */
138
+ becomes(cls, ...args) {
139
+ if (this instanceof cls)
140
+ return this;
141
+ const donor = new cls(this, ...args);
142
+ Object.setPrototypeOf(this, cls.prototype);
143
+ for (const key of Object.getOwnPropertyNames(donor)) {
144
+ if (!Object.prototype.hasOwnProperty.call(this, key)) {
145
+ ;
146
+ this[key] = donor[key];
147
+ }
148
+ }
149
+ return this;
150
+ }
134
151
  copyFrom(other) {
135
152
  for (const [key, value] of other.#entries) {
136
153
  this.#entries.set(key, value);
@@ -70,8 +70,13 @@ export declare abstract class IncrementalParser<I, O> extends Parser<I, O> {
70
70
  */
71
71
  protected compact(): void;
72
72
  /**
73
- * Override to customize when to compact the buffer
74
- * By default, compacts when more than 1000 items have been consumed
73
+ * Override to customize when to compact the buffer.
74
+ * Compacts when more than 1000 items have been consumed AND we've consumed
75
+ * at least half the buffer. The second condition prevents O(n²) behaviour
76
+ * when a single large chunk is fed all at once: without it, compact() is
77
+ * called O(n/1000) times, each copying O(n) elements → O(n²) total.
78
+ * By requiring bufferIndex ≥ buffer.length/2 we ensure at most O(log n)
79
+ * compactions, for O(n) total copies.
75
80
  *
76
81
  * @returns boolean indicating whether to compact the buffer
77
82
  */
@@ -139,13 +139,19 @@ export class IncrementalParser extends Parser {
139
139
  }
140
140
  }
141
141
  /**
142
- * Override to customize when to compact the buffer
143
- * By default, compacts when more than 1000 items have been consumed
142
+ * Override to customize when to compact the buffer.
143
+ * Compacts when more than 1000 items have been consumed AND we've consumed
144
+ * at least half the buffer. The second condition prevents O(n²) behaviour
145
+ * when a single large chunk is fed all at once: without it, compact() is
146
+ * called O(n/1000) times, each copying O(n) elements → O(n²) total.
147
+ * By requiring bufferIndex ≥ buffer.length/2 we ensure at most O(log n)
148
+ * compactions, for O(n) total copies.
144
149
  *
145
150
  * @returns boolean indicating whether to compact the buffer
146
151
  */
147
152
  canCompact() {
148
- return this.bufferIndex > 1000;
153
+ return (this.bufferIndex >= 1000 &&
154
+ this.bufferIndex >= this.buffer.length >> 1);
149
155
  }
150
156
  /**
151
157
  * Generates parsed output items from the buffer.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdf-lite",
3
- "version": "1.6.2",
3
+ "version": "1.6.4",
4
4
  "main": "dist/index.js",
5
5
  "type": "module",
6
6
  "exports": {