@web-ts-toolkit/pdf-reader 0.38.0 → 0.39.0

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.
Files changed (3) hide show
  1. package/index.d.mts +4 -2
  2. package/index.mjs +20 -6
  3. package/package.json +3 -3
package/index.d.mts CHANGED
@@ -8,13 +8,15 @@ declare class PdfReaderError extends Error {
8
8
  constructor(code: PdfReaderErrorCode, message: string);
9
9
  }
10
10
 
11
+ type PdfDocumentInitParameters = NonNullable<Parameters<typeof pdfjs_dist.getDocument>[0]>;
12
+ type PdfTypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
11
13
  /**
12
14
  * PDF.js `getDocument(...)` source accepted by the package root.
13
15
  *
14
16
  * Prefer trusted in-memory bytes when possible. PDF.js may transfer typed-array
15
17
  * ownership to its worker during load.
16
18
  */
17
- type PdfSource = NonNullable<Parameters<typeof pdfjs_dist.getDocument>[0]>;
19
+ type PdfSource = string | URL | ArrayBuffer | PdfTypedArray | number[] | PdfDocumentInitParameters;
18
20
  type TransformMatrix = readonly [number, number, number, number, number, number];
19
21
  type PageRange = number | readonly [start: number, end: number];
20
22
  type ViewportScale = number | ((pageWidth: number, pageHeight: number) => number);
@@ -189,4 +191,4 @@ declare function pdfUrlSource(url: string | URL, options?: PdfUrlSourceOptions):
189
191
  */
190
192
  declare function configurePdfWorker(worker: string | Worker): void;
191
193
 
192
- export { type BlobPageImage, type ConvertOptions, type DataUrlPageImage, type ExtractedImage, type LoadOptions, type LoadedPdfDocument, type LoadedPdfPage, PDFReader, type PageImageMimeType, type PageImageOutputMode, type PageImageResult, type PageRange, type PageResult, PdfReaderError, type PdfReaderErrorCode, type PdfReaderLimits, type PdfReaderLogger, type PdfReaderOptions, type PdfReaderSourceInfo, type PdfReaderSourcePolicy, type PdfReaderState, type PdfSource, type PdfTextContent, type PdfUrlSource, type PdfUrlSourceOptions, type TransformMatrix, type ViewportScale, configurePdfWorker, pdfUrlSource };
194
+ export { type BlobPageImage, type ConvertOptions, type DataUrlPageImage, type ExtractedImage, type LoadOptions, type LoadedPdfDocument, type LoadedPdfPage, PDFReader, type PageImageMimeType, type PageImageOutputMode, type PageImageResult, type PageRange, type PageResult, type PdfDocumentInitParameters, PdfReaderError, type PdfReaderErrorCode, type PdfReaderLimits, type PdfReaderLogger, type PdfReaderOptions, type PdfReaderSourceInfo, type PdfReaderSourcePolicy, type PdfReaderState, type PdfSource, type PdfTextContent, type PdfTypedArray, type PdfUrlSource, type PdfUrlSourceOptions, type TransformMatrix, type ViewportScale, configurePdfWorker, pdfUrlSource };
package/index.mjs CHANGED
@@ -269,7 +269,7 @@ var defaultLimits = {
269
269
  maxCanvasPixels: 4e7,
270
270
  maxEmbeddedImagePixels: 25e6
271
271
  };
272
- var _source, _createCanvas, _logger, _limits, _sourcePolicy, _loadingState, _document, _destroyPromise, _activeRenderTasks, _activePageWorkCount, _destroyed, _lastLoadFailed, _PDFReader_instances, load_fn, processPage_fn, encodePageImage_fn, resolveViewport_fn, encodeCanvasToBlob_fn, allocateCanvas_fn, waitForRender_fn, releaseCanvas_fn, getOrCreateLoadState_fn, awaitWithSignal_fn, createAbortedError_fn, createDestroyedError_fn, createDeadlineExceededError_fn, createUnsupportedEnvironmentError_fn, rethrowLifecycleError_fn, throwIfDestroyed_fn, throwIfAborted_fn, resolveLoadOptions_fn, isAbortSignal_fn, enforceSourcePolicy_fn, isPromiseLike_fn, inspectSource_fn, knownByteLength_fn, readUrl_fn, readHttpHeaders_fn, validateLimits_fn;
272
+ var _source, _createCanvas, _logger, _limits, _sourcePolicy, _loadingState, _loadingTask, _document, _destroyPromise, _activeRenderTasks, _activePageWorkCount, _destroyed, _lastLoadFailed, _PDFReader_instances, load_fn, processPage_fn, encodePageImage_fn, resolveViewport_fn, encodeCanvasToBlob_fn, allocateCanvas_fn, waitForRender_fn, releaseCanvas_fn, getOrCreateLoadState_fn, awaitWithSignal_fn, createAbortedError_fn, createDestroyedError_fn, createDeadlineExceededError_fn, createUnsupportedEnvironmentError_fn, rethrowLifecycleError_fn, throwIfDestroyed_fn, throwIfAborted_fn, resolveLoadOptions_fn, isAbortSignal_fn, enforceSourcePolicy_fn, isPromiseLike_fn, inspectSource_fn, toPdfJsSource_fn, isPdfTypedArray_fn, knownByteLength_fn, readUrl_fn, readHttpHeaders_fn, validateLimits_fn;
273
273
  var PDFReader = class {
274
274
  constructor(source, options = {}) {
275
275
  __privateAdd(this, _PDFReader_instances);
@@ -279,6 +279,7 @@ var PDFReader = class {
279
279
  __privateAdd(this, _limits);
280
280
  __privateAdd(this, _sourcePolicy);
281
281
  __privateAdd(this, _loadingState);
282
+ __privateAdd(this, _loadingTask);
282
283
  __privateAdd(this, _document);
283
284
  __privateAdd(this, _destroyPromise);
284
285
  __privateAdd(this, _activeRenderTasks, /* @__PURE__ */ new Set());
@@ -356,14 +357,15 @@ var PDFReader = class {
356
357
  async destroy() {
357
358
  if (__privateGet(this, _destroyPromise)) return __privateGet(this, _destroyPromise);
358
359
  __privateSet(this, _destroyed, true);
359
- const documentProxy = __privateGet(this, _document);
360
+ const loadingTask = __privateGet(this, _loadingTask);
360
361
  const loadingState = __privateGet(this, _loadingState);
362
+ __privateSet(this, _loadingTask, void 0);
361
363
  __privateSet(this, _document, void 0);
362
364
  __privateSet(this, _loadingState, void 0);
363
365
  __privateSet(this, _destroyPromise, (async () => {
364
366
  for (const renderTask of __privateGet(this, _activeRenderTasks)) renderTask.cancel();
365
- if (documentProxy) {
366
- await documentProxy.destroy();
367
+ if (loadingTask) {
368
+ await loadingTask.destroy();
367
369
  return;
368
370
  }
369
371
  if (!loadingState) return;
@@ -380,6 +382,7 @@ _logger = new WeakMap();
380
382
  _limits = new WeakMap();
381
383
  _sourcePolicy = new WeakMap();
382
384
  _loadingState = new WeakMap();
385
+ _loadingTask = new WeakMap();
383
386
  _document = new WeakMap();
384
387
  _destroyPromise = new WeakMap();
385
388
  _activeRenderTasks = new WeakMap();
@@ -554,7 +557,7 @@ getOrCreateLoadState_fn = function() {
554
557
  if (state.destroyed || __privateGet(this, _destroyed)) {
555
558
  throw __privateMethod(this, _PDFReader_instances, createDestroyedError_fn).call(this);
556
559
  }
557
- const task = getDocument(__privateGet(this, _source));
560
+ const task = getDocument(__privateMethod(this, _PDFReader_instances, toPdfJsSource_fn).call(this, __privateGet(this, _source)));
558
561
  state.task = task;
559
562
  const documentProxy = await task.promise;
560
563
  if (state.destroyed || state.destroyPromise) {
@@ -562,13 +565,14 @@ getOrCreateLoadState_fn = function() {
562
565
  throw __privateMethod(this, _PDFReader_instances, createDestroyedError_fn).call(this);
563
566
  }
564
567
  if (documentProxy.numPages > __privateGet(this, _limits).maxDocumentPages) {
565
- await documentProxy.destroy();
568
+ await task.destroy();
566
569
  throw new PdfReaderError(
567
570
  "PAGE_LIMIT_EXCEEDED",
568
571
  `PDF has ${documentProxy.numPages} pages; limit is ${__privateGet(this, _limits).maxDocumentPages}.`
569
572
  );
570
573
  }
571
574
  __privateMethod(this, _PDFReader_instances, throwIfDestroyed_fn).call(this);
575
+ __privateSet(this, _loadingTask, task);
572
576
  __privateSet(this, _document, documentProxy);
573
577
  __privateSet(this, _lastLoadFailed, false);
574
578
  return documentProxy;
@@ -710,6 +714,16 @@ inspectSource_fn = function(source) {
710
714
  withCredentials: candidate.withCredentials === true
711
715
  };
712
716
  };
717
+ toPdfJsSource_fn = function(source) {
718
+ if (typeof source === "string") return { url: source };
719
+ if (source instanceof URL) return { url: source };
720
+ if (source instanceof ArrayBuffer || __privateMethod(this, _PDFReader_instances, isPdfTypedArray_fn).call(this, source) || Array.isArray(source))
721
+ return { data: source };
722
+ return source;
723
+ };
724
+ isPdfTypedArray_fn = function(source) {
725
+ return ArrayBuffer.isView(source) && !(source instanceof DataView);
726
+ };
713
727
  knownByteLength_fn = function(value) {
714
728
  if (value instanceof ArrayBuffer) return value.byteLength;
715
729
  if (ArrayBuffer.isView(value)) return value.byteLength;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@web-ts-toolkit/pdf-reader",
3
3
  "description": "Resource-safe browser PDF rendering, text extraction, and embedded-image extraction built on PDF.js",
4
4
  "homepage": "https://web-ts-toolkit.pages.dev/docs/packages/pdf-reader",
5
- "version": "0.38.0",
5
+ "version": "0.39.0",
6
6
  "sideEffects": false,
7
7
  "keywords": [
8
8
  "pdf",
@@ -23,10 +23,10 @@
23
23
  }
24
24
  },
25
25
  "engines": {
26
- "node": ">=22"
26
+ "node": ">=22.13.0"
27
27
  },
28
28
  "peerDependencies": {
29
- "pdfjs-dist": "~5.7.284"
29
+ "pdfjs-dist": "^6.2.108"
30
30
  },
31
31
  "files": [
32
32
  "**/*",