pdfium-native 0.3.0 → 0.4.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.
- package/CHANGELOG.md +200 -0
- package/README.md +20 -0
- package/dist/concurrency.d.ts +9 -0
- package/dist/concurrency.d.ts.map +1 -0
- package/dist/concurrency.js +22 -0
- package/dist/concurrency.js.map +1 -0
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +3 -2
- package/dist/document.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/page.d.ts.map +1 -1
- package/dist/page.js +15 -8
- package/dist/page.js.map +1 -1
- package/lib/concurrency.ts +24 -0
- package/lib/document.ts +3 -2
- package/lib/index.ts +3 -1
- package/lib/page.ts +18 -8
- package/package.json +15 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/), and this project adheres to [Semantic Versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## [0.4.0] - 2026-04-04
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- `concurrency()` — get/set max concurrent native operations dispatched to the libuv thread pool (default: CPU cores, powered by p-limit)
|
|
12
|
+
- Husky + lint-staged pre-commit hook (Prettier + ESLint on staged files)
|
|
13
|
+
|
|
14
|
+
## [0.3.0] - 2026-03-31
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- `ImagePageObject.render()` — extract and render embedded PDF images as PNG, JPEG, or raw stream bytes
|
|
19
|
+
- Three render modes: intrinsic bitmap, rendered bitmap (with mask/matrix), and raw encoded stream
|
|
20
|
+
- File output support for image rendering via `output` option
|
|
21
|
+
- `objects()` async generator on `PDFiumPage` for iterating all page objects
|
|
22
|
+
- Image dimension cap (`MAX_IMAGE_PIXELS = 256 MP`) to prevent allocation failures on huge embedded images
|
|
23
|
+
- Negative object index guard in `RenderImageWorker`
|
|
24
|
+
|
|
25
|
+
### Changed
|
|
26
|
+
|
|
27
|
+
- **Breaking:** Default render format changed from JPEG to PNG for both `page.render()` and `image.render()` — use `{ format: 'jpeg' }` to restore previous behavior
|
|
28
|
+
- Image `render()` binding moved from TypeScript layer to native C++ (`GetObjectWorker::OnOK`) for cleaner API
|
|
29
|
+
- `PDFiumPage.getObject()` is now a direct passthrough to the native addon
|
|
30
|
+
|
|
31
|
+
## [0.3.0] - 2026-03-31
|
|
32
|
+
|
|
33
|
+
### Added
|
|
34
|
+
|
|
35
|
+
- `ImagePageObject.render()` — extract and render embedded PDF images as PNG, JPEG, or raw stream bytes
|
|
36
|
+
- Three render modes: intrinsic bitmap, rendered bitmap (with mask/matrix), and raw encoded stream
|
|
37
|
+
- File output support for image rendering via `output` option
|
|
38
|
+
- `objects()` async generator on `PDFiumPage` for iterating all page objects
|
|
39
|
+
- Image dimension cap (`MAX_IMAGE_PIXELS = 256 MP`) to prevent allocation failures on huge embedded images
|
|
40
|
+
- Negative object index guard in `RenderImageWorker`
|
|
41
|
+
|
|
42
|
+
### Changed
|
|
43
|
+
|
|
44
|
+
- **Breaking:** Default render format changed from JPEG to PNG for both `page.render()` and `image.render()` — use `{ format: 'jpeg' }` to restore previous behavior
|
|
45
|
+
- Image `render()` binding moved from TypeScript layer to native C++ (`GetObjectWorker::OnOK`) for cleaner API
|
|
46
|
+
- `PDFiumPage.getObject()` is now a direct passthrough to the native addon
|
|
47
|
+
|
|
48
|
+
## [0.2.4] - 2026-03-30
|
|
49
|
+
|
|
50
|
+
### Fixed
|
|
51
|
+
|
|
52
|
+
- Bumped minimum Node.js version to 20.11.0 (`import.meta.dirname` requirement)
|
|
53
|
+
- Wrapped web `ReadableStream` with `Readable.fromWeb()` in install script
|
|
54
|
+
- Updated README Node version range from 18–24 to 20–24
|
|
55
|
+
- Used static import for `Readable` instead of dynamic `import()`
|
|
56
|
+
|
|
57
|
+
### Added
|
|
58
|
+
|
|
59
|
+
- npm publish provenance (`--provenance` flag)
|
|
60
|
+
- Build provenance attestation for prebuilt binaries via `actions/attest-build-provenance`
|
|
61
|
+
|
|
62
|
+
## [0.2.3] - 2026-03-28
|
|
63
|
+
|
|
64
|
+
### Added
|
|
65
|
+
|
|
66
|
+
- `getFormFields()` for reading PDF form fields (text fields, checkboxes, radio buttons, combo boxes, list boxes, signatures)
|
|
67
|
+
- Bookmark sibling count limit (`MAX_SIBLINGS_PER_LEVEL = 10000`) to prevent DoS from malicious PDFs
|
|
68
|
+
|
|
69
|
+
### Changed
|
|
70
|
+
|
|
71
|
+
- Extracted shared UTF-16 read/write helpers (`ReadU16`, `SetU16`, `SetU16IfPresent`, `ToNapiString`, `ActionTypeString`) to `napi_helpers.h`, reducing boilerplate across all workers
|
|
72
|
+
- Split monolithic `page_workers.h` (1500 lines) into 7 individual worker files
|
|
73
|
+
- Moved `CHECK_ALIVE` macro to `napi_helpers.h`
|
|
74
|
+
|
|
75
|
+
### Fixed
|
|
76
|
+
|
|
77
|
+
- `ReadU16` now rejects odd byte lengths to prevent buffer overflow on malformed PDFs
|
|
78
|
+
- `SearchWorker` and `GetFormFieldsWorker` now properly reject (instead of silently resolving empty) when PDFium fails to load text page or initialize form environment
|
|
79
|
+
- Form fill environment cleanup is now guarded against null handle
|
|
80
|
+
- Render worker validates bitmap buffer and stride before pixel conversion
|
|
81
|
+
- Render worker pixel buffer allocation casts both dimensions to `size_t` to prevent integer overflow
|
|
82
|
+
- `stb_write_callback` guards against negative size parameter
|
|
83
|
+
- `LoadDocumentWorker` rejects buffers larger than `INT_MAX`
|
|
84
|
+
- Annotation handles are now closed before `push_back` to prevent resource leaks on allocation failure
|
|
85
|
+
- Text and search workers guard against `INT_MAX` overflow in buffer sizing
|
|
86
|
+
- Removed unused `#include <algorithm>` from `page.h`
|
|
87
|
+
|
|
88
|
+
## [0.2.2] - 2026-03-28
|
|
89
|
+
|
|
90
|
+
### Changed
|
|
91
|
+
|
|
92
|
+
- README overhaul: benefit-driven headline, Quick Start moved above features, grouped "Why" section (Performance / Developer experience / Reliability), use cases section, competitor comparison table with actual packages (@hyzyla/pdfium, pdfjs-dist, pdf-parse, pdf2json)
|
|
93
|
+
- Added npm badges (version, node, license, platform)
|
|
94
|
+
- Expanded npm keywords for discoverability
|
|
95
|
+
- Added homepage and bugs URLs to package.json
|
|
96
|
+
|
|
97
|
+
## [0.2.1] - 2026-03-28
|
|
98
|
+
|
|
99
|
+
### Changed
|
|
100
|
+
|
|
101
|
+
- Cross-compile `darwin-x64` on `macos-latest` (arm64) and `win32-arm64` on `windows-latest` (x64), eliminating dedicated runners
|
|
102
|
+
- Moved `linux-arm64` prebuild from native `ubuntu-24.04-arm` runner to QEMU Docker cross-compile
|
|
103
|
+
- Added `linux-musl-x64` and `linux-musl-arm64` prebuilds via Alpine Docker
|
|
104
|
+
|
|
105
|
+
### Fixed
|
|
106
|
+
|
|
107
|
+
- `install.mjs` now detects musl libc and downloads the correct prebuilt tarball on Alpine/musl systems
|
|
108
|
+
- Cache page dimensions in C++ to avoid reading them without mutex in `Render()`
|
|
109
|
+
- `g_initialized` is now `std::atomic<bool>` for thread safety
|
|
110
|
+
- Consistent alive flag ordering in `PDFiumPage::CleanUp()` (set under mutex, matching `PDFiumDocument`)
|
|
111
|
+
|
|
112
|
+
### Performance
|
|
113
|
+
|
|
114
|
+
- Binary size reduced ~37% via LTO, `-Os`, `-fvisibility=hidden`, dead code stripping, and link-time symbol stripping
|
|
115
|
+
|
|
116
|
+
## [0.2.0] - 2026-03-28
|
|
117
|
+
|
|
118
|
+
### Changed
|
|
119
|
+
|
|
120
|
+
- **BREAKING**: `getText()`, `getObject()`, `getLinks()`, `search()`, `getAnnotations()`, and `getBookmarks()` now return Promises instead of synchronous values
|
|
121
|
+
- `objects()` generator is now an async generator (`AsyncGenerator<PageObject>`)
|
|
122
|
+
- GetPage bounds check moved off the main thread into the async worker
|
|
123
|
+
- Alive flags in `destroy()`/`close()` are now set under the global mutex
|
|
124
|
+
|
|
125
|
+
### Added
|
|
126
|
+
|
|
127
|
+
- GC Finalize callbacks on `PDFiumPage` and `PDFiumDocument` as safety net for unreleased resources
|
|
128
|
+
- `FPDF_DestroyLibrary` cleanup hook on environment teardown
|
|
129
|
+
- `page_workers.h` with dedicated AsyncWorker classes for all page/document operations
|
|
130
|
+
|
|
131
|
+
### Fixed
|
|
132
|
+
|
|
133
|
+
- SSRF vulnerability in install script (CodeQL critical)
|
|
134
|
+
- Missing `permissions: contents: read` in CI/release workflow jobs (CodeQL medium)
|
|
135
|
+
|
|
136
|
+
### Removed
|
|
137
|
+
|
|
138
|
+
- `ia32` platform support (macOS, Linux musl, Windows)
|
|
139
|
+
|
|
140
|
+
## [0.1.7] - 2026-03-28
|
|
141
|
+
|
|
142
|
+
### Fixed
|
|
143
|
+
|
|
144
|
+
- MSVC ARM64 build: compile `stb_image_write` as C++ to fix const initializer error
|
|
145
|
+
|
|
146
|
+
## [0.1.6] - 2026-03-28
|
|
147
|
+
|
|
148
|
+
### Fixed
|
|
149
|
+
|
|
150
|
+
- Windows prebuild: use bash shell for tarball creation
|
|
151
|
+
|
|
152
|
+
## [0.1.5] - 2026-03-28
|
|
153
|
+
|
|
154
|
+
### Changed
|
|
155
|
+
|
|
156
|
+
- Merged publish and prebuild into single `release.yml` workflow
|
|
157
|
+
|
|
158
|
+
## [0.1.4] - 2026-03-28
|
|
159
|
+
|
|
160
|
+
### Changed
|
|
161
|
+
|
|
162
|
+
- Release triggered by tag push (`v*`) instead of GitHub release event
|
|
163
|
+
- Auto-create GitHub release with prebuilt tarballs
|
|
164
|
+
|
|
165
|
+
## [0.1.3] - 2026-03-28
|
|
166
|
+
|
|
167
|
+
### Fixed
|
|
168
|
+
|
|
169
|
+
- npm publish authentication with OIDC trusted publishing (Node 24 / npm 11.5.1+)
|
|
170
|
+
|
|
171
|
+
## [0.1.2] - 2026-03-28
|
|
172
|
+
|
|
173
|
+
### Fixed
|
|
174
|
+
|
|
175
|
+
- Prebuild tarball naming and `contents: write` permission for release uploads
|
|
176
|
+
|
|
177
|
+
## [0.1.1] - 2026-03-28
|
|
178
|
+
|
|
179
|
+
### Fixed
|
|
180
|
+
|
|
181
|
+
- Prebuild workflow failures
|
|
182
|
+
- Added `@types/node` dev dependency
|
|
183
|
+
|
|
184
|
+
## [0.1.0] - 2026-03-27
|
|
185
|
+
|
|
186
|
+
### Added
|
|
187
|
+
|
|
188
|
+
- Async document loading from `Buffer` or file path (with optional password)
|
|
189
|
+
- Page rendering to JPEG/PNG (`Buffer` or file output) with scale/quality options
|
|
190
|
+
- Text extraction via `getText()`
|
|
191
|
+
- Text search with bounding rectangles via `search()`
|
|
192
|
+
- Page object inspection with discriminated union types (text/image/path/etc.)
|
|
193
|
+
- Document metadata as a cached readonly property
|
|
194
|
+
- Bookmark tree traversal via `getBookmarks()`
|
|
195
|
+
- Link enumeration via `getLinks()`
|
|
196
|
+
- Annotation reading via `getAnnotations()`
|
|
197
|
+
- Support for macOS (arm64, x64), Linux glibc (x64, arm64, arm, ia32, ppc64), Linux musl (x64, arm64, ia32), and Windows (x64, arm64, ia32)
|
|
198
|
+
- ESLint + Prettier configuration
|
|
199
|
+
- GitHub Actions publish workflow with test gate
|
|
200
|
+
- TypeScript type declarations for JS consumers
|
package/README.md
CHANGED
|
@@ -365,6 +365,26 @@ interface DocumentMetadata {
|
|
|
365
365
|
|
|
366
366
|
---
|
|
367
367
|
|
|
368
|
+
## ⚙️ Concurrency
|
|
369
|
+
|
|
370
|
+
### `concurrency(value?): number`
|
|
371
|
+
|
|
372
|
+
Gets or sets the maximum number of concurrent native operations dispatched to the thread pool.
|
|
373
|
+
|
|
374
|
+
The default is the number of CPU cores (`os.availableParallelism()`). A value of `0` resets to the default.
|
|
375
|
+
|
|
376
|
+
PDFium is single-threaded internally — all operations are serialized through a global mutex. The concurrency limiter prevents excess libuv worker threads from being blocked waiting on that mutex.
|
|
377
|
+
|
|
378
|
+
```typescript
|
|
379
|
+
import { loadDocument, concurrency } from 'pdfium-native';
|
|
380
|
+
|
|
381
|
+
concurrency(); // 8 (CPU cores)
|
|
382
|
+
concurrency(2); // limit to 2 concurrent operations
|
|
383
|
+
concurrency(0); // reset to default
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
---
|
|
387
|
+
|
|
368
388
|
## 🙏 Acknowledgements
|
|
369
389
|
|
|
370
390
|
This project uses prebuilt PDFium binaries from [bblanchon/pdfium-binaries](https://github.com/bblanchon/pdfium-binaries), which provides automated builds of the PDFium library for multiple platforms. Thanks to [@bblanchon](https://github.com/bblanchon) for maintaining this invaluable resource.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets or sets the maximum number of concurrent native operations.
|
|
3
|
+
*
|
|
4
|
+
* The default value is the number of CPU cores (`os.availableParallelism()`).
|
|
5
|
+
* A value of `0` resets to the default.
|
|
6
|
+
*/
|
|
7
|
+
export declare function concurrency(value?: number): number;
|
|
8
|
+
export declare function withConcurrency<T>(fn: () => Promise<T>): Promise<T>;
|
|
9
|
+
//# sourceMappingURL=concurrency.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"concurrency.d.ts","sourceRoot":"","sources":["../lib/concurrency.ts"],"names":[],"mappings":"AAKA;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAQlD;AAED,wBAAgB,eAAe,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAEnE"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { availableParallelism } from 'node:os';
|
|
2
|
+
import pLimit from 'p-limit';
|
|
3
|
+
let limit = pLimit(availableParallelism());
|
|
4
|
+
/**
|
|
5
|
+
* Gets or sets the maximum number of concurrent native operations.
|
|
6
|
+
*
|
|
7
|
+
* The default value is the number of CPU cores (`os.availableParallelism()`).
|
|
8
|
+
* A value of `0` resets to the default.
|
|
9
|
+
*/
|
|
10
|
+
export function concurrency(value) {
|
|
11
|
+
if (value !== undefined) {
|
|
12
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
13
|
+
throw new TypeError('Concurrency must be a non-negative integer');
|
|
14
|
+
}
|
|
15
|
+
limit = pLimit(value === 0 ? availableParallelism() : value);
|
|
16
|
+
}
|
|
17
|
+
return limit.concurrency;
|
|
18
|
+
}
|
|
19
|
+
export function withConcurrency(fn) {
|
|
20
|
+
return limit(fn);
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=concurrency.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"concurrency.js","sourceRoot":"","sources":["../lib/concurrency.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,MAA8B,MAAM,SAAS,CAAC;AAErD,IAAI,KAAK,GAAkB,MAAM,CAAC,oBAAoB,EAAE,CAAC,CAAC;AAE1D;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc;IACxC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;YAC1C,MAAM,IAAI,SAAS,CAAC,4CAA4C,CAAC,CAAC;QACpE,CAAC;QACD,KAAK,GAAG,MAAM,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,oBAAoB,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;IAC/D,CAAC;IACD,OAAO,KAAK,CAAC,WAAW,CAAC;AAC3B,CAAC;AAED,MAAM,UAAU,eAAe,CAAI,EAAoB;IACrD,OAAO,KAAK,CAAC,EAAE,CAAC,CAAC;AACnB,CAAC"}
|
package/dist/document.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"document.d.ts","sourceRoot":"","sources":["../lib/document.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"document.d.ts","sourceRoot":"","sources":["../lib/document.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,KAAK,EAAE,QAAQ,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE7E;;GAEG;AACH,qBAAa,cAAc;IAOb,OAAO,CAAC,MAAM;IAN1B,6BAA6B;IAC7B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,sDAAsD;IACtD,QAAQ,CAAC,QAAQ,EAAE,gBAAgB,CAAC;IAEpC,gBAAgB;gBACI,MAAM,EAAE,cAAc;IAK1C,oCAAoC;IAC9B,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAIjD,4EAA4E;IACrE,KAAK,IAAI,cAAc,CAAC,UAAU,CAAC;IAM1C,mDAAmD;IACnD,OAAO,IAAI,IAAI;IAIf,yCAAyC;IACzC,YAAY,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;CAGpC"}
|
package/dist/document.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { withConcurrency } from './concurrency.js';
|
|
1
2
|
import { PDFiumPage } from './page.js';
|
|
2
3
|
/**
|
|
3
4
|
* A loaded PDF document.
|
|
@@ -16,7 +17,7 @@ export class PDFiumDocument {
|
|
|
16
17
|
}
|
|
17
18
|
/** Gets a page by 0-based index. */
|
|
18
19
|
async getPage(index) {
|
|
19
|
-
return new PDFiumPage(await this.native.getPage(index));
|
|
20
|
+
return new PDFiumPage(await withConcurrency(() => this.native.getPage(index)));
|
|
20
21
|
}
|
|
21
22
|
/** Iterates over all pages. Caller is responsible for closing each page. */
|
|
22
23
|
async *pages() {
|
|
@@ -30,7 +31,7 @@ export class PDFiumDocument {
|
|
|
30
31
|
}
|
|
31
32
|
/** Returns the bookmark/outline tree. */
|
|
32
33
|
getBookmarks() {
|
|
33
|
-
return this.native.getBookmarks();
|
|
34
|
+
return withConcurrency(() => this.native.getBookmarks());
|
|
34
35
|
}
|
|
35
36
|
}
|
|
36
37
|
//# sourceMappingURL=document.js.map
|
package/dist/document.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"document.js","sourceRoot":"","sources":["../lib/document.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAGvC;;GAEG;AACH,MAAM,OAAO,cAAc;IAOL;IANpB,6BAA6B;IACpB,SAAS,CAAS;IAC3B,sDAAsD;IAC7C,QAAQ,CAAmB;IAEpC,gBAAgB;IAChB,YAAoB,MAAsB;QAAtB,WAAM,GAAN,MAAM,CAAgB;QACxC,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;QAClC,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;IAClC,CAAC;IAED,oCAAoC;IACpC,KAAK,CAAC,OAAO,CAAC,KAAa;QACzB,OAAO,IAAI,UAAU,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"document.js","sourceRoot":"","sources":["../lib/document.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAGvC;;GAEG;AACH,MAAM,OAAO,cAAc;IAOL;IANpB,6BAA6B;IACpB,SAAS,CAAS;IAC3B,sDAAsD;IAC7C,QAAQ,CAAmB;IAEpC,gBAAgB;IAChB,YAAoB,MAAsB;QAAtB,WAAM,GAAN,MAAM,CAAgB;QACxC,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;QAClC,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;IAClC,CAAC;IAED,oCAAoC;IACpC,KAAK,CAAC,OAAO,CAAC,KAAa;QACzB,OAAO,IAAI,UAAU,CAAC,MAAM,eAAe,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACjF,CAAC;IAED,4EAA4E;IAC5E,KAAK,CAAC,CAAC,KAAK;QACV,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC,EAAE,EAAE,CAAC;YACxC,MAAM,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QACxB,CAAC;IACH,CAAC;IAED,mDAAmD;IACnD,OAAO;QACL,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAED,yCAAyC;IACzC,YAAY;QACV,OAAO,eAAe,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,YAAY,EAAE,CAAC,CAAC;IAC3D,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ type PdfInput = Buffer | string;
|
|
|
4
4
|
* Opens a PDF document from a Buffer or file path.
|
|
5
5
|
*/
|
|
6
6
|
export declare function loadDocument(input: PdfInput, password?: string): Promise<PDFiumDocument>;
|
|
7
|
+
export { concurrency } from './concurrency.js';
|
|
7
8
|
export { PDFiumDocument } from './document.js';
|
|
8
9
|
export { PDFiumError, PDFiumFileError, PDFiumFormatError, PDFiumPasswordError, PDFiumSecurityError, } from './errors.js';
|
|
9
10
|
export { PDFiumPage } from './page.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAO/C,KAAK,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;AAchC;;GAEG;AACH,wBAAsB,YAAY,CAAC,KAAK,EAAE,QAAQ,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAM9F;AAED,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EACL,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,YAAY,EACV,UAAU,EACV,gBAAgB,EAChB,cAAc,EACd,QAAQ,EACR,gBAAgB,EAChB,mBAAmB,EACnB,SAAS,EACT,eAAe,EACf,aAAa,EACb,eAAe,EACf,eAAe,EACf,kBAAkB,EAClB,IAAI,EACJ,cAAc,EACd,eAAe,EACf,UAAU,EACV,gBAAgB,EAChB,iBAAiB,EACjB,UAAU,EACV,IAAI,EACJ,WAAW,EACX,aAAa,EACb,UAAU,EACV,cAAc,EACd,cAAc,GACf,MAAM,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createRequire } from 'node:module';
|
|
2
2
|
import { dirname, resolve } from 'node:path';
|
|
3
3
|
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { withConcurrency } from './concurrency.js';
|
|
4
5
|
import { PDFiumDocument } from './document.js';
|
|
5
6
|
import { parseNativeError } from './errors.js';
|
|
6
7
|
const require = createRequire(import.meta.url);
|
|
@@ -21,12 +22,13 @@ const addon = require('../build/Release/pdfium.node');
|
|
|
21
22
|
*/
|
|
22
23
|
export async function loadDocument(input, password) {
|
|
23
24
|
try {
|
|
24
|
-
return new PDFiumDocument(await addon.loadDocument(input, password));
|
|
25
|
+
return new PDFiumDocument(await withConcurrency(() => addon.loadDocument(input, password)));
|
|
25
26
|
}
|
|
26
27
|
catch (err) {
|
|
27
28
|
throw parseNativeError(err);
|
|
28
29
|
}
|
|
29
30
|
}
|
|
31
|
+
export { concurrency } from './concurrency.js';
|
|
30
32
|
export { PDFiumDocument } from './document.js';
|
|
31
33
|
export { PDFiumError, PDFiumFileError, PDFiumFormatError, PDFiumPasswordError, PDFiumSecurityError, } from './errors.js';
|
|
32
34
|
export { PDFiumPage } from './page.js';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAG/C,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC/C,MAAM,SAAS,GAAG,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAI1D,oEAAoE;AACpE,uEAAuE;AACvE,kEAAkE;AAClE,MAAM,QAAQ,GAAG,OAAO,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;AAC9D,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;IACjC,OAAO,CAAC,GAAG,CAAC,IAAI,GAAG,GAAG,QAAQ,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,EAAE,EAAE,CAAC;AAC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I,IAAI,EAAE,EAAE,CAAC;AAC7D,CAAC;KAAM,CAAC;IACN,OAAO,CAAC,GAAG,CAAC,eAAe,GAAG,GAAG,QAAQ,IAAI,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,EAAE,EAAE,CAAC;AACnF,CAAC;AAED,MAAM,KAAK,GAAgB,OAAO,CAAC,8BAA8B,CAAC,CAAC;AAEnE;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,KAAe,EAAE,QAAiB;IACnE,IAAI,CAAC;QACH,OAAO,IAAI,cAAc,CAAC,MAAM,eAAe,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC9F,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,MAAM,gBAAgB,CAAC,GAAG,CAAC,CAAC;IAC9B,CAAC;AACH,CAAC;AAED,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EACL,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/page.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"page.d.ts","sourceRoot":"","sources":["../lib/page.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"page.d.ts","sourceRoot":"","sources":["../lib/page.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,UAAU,EACV,SAAS,EAGT,IAAI,EACJ,UAAU,EACV,UAAU,EACV,gBAAgB,EAChB,iBAAiB,EACjB,WAAW,EACX,aAAa,EACd,MAAM,YAAY,CAAC;AAEpB;;GAEG;AACH,qBAAa,UAAU;IAqBT,OAAO,CAAC,MAAM;IApB1B,+CAA+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3C,cAAc,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;IAIvC,2CAA2C;IAC3C,aAAa,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;CAGtC"}
|
package/dist/page.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { withConcurrency } from './concurrency.js';
|
|
1
2
|
/**
|
|
2
3
|
* A page in a PDF document.
|
|
3
4
|
*/
|
|
@@ -39,14 +40,20 @@ export class PDFiumPage {
|
|
|
39
40
|
}
|
|
40
41
|
/** Extracts all text from the page. */
|
|
41
42
|
getText() {
|
|
42
|
-
return this.native.getText();
|
|
43
|
+
return withConcurrency(() => this.native.getText());
|
|
43
44
|
}
|
|
44
45
|
render(options) {
|
|
45
|
-
return this.native.render(options);
|
|
46
|
+
return withConcurrency(() => this.native.render(options));
|
|
46
47
|
}
|
|
47
48
|
/** Returns the page object at the given index with type and bounds. */
|
|
48
|
-
getObject(index) {
|
|
49
|
-
|
|
49
|
+
async getObject(index) {
|
|
50
|
+
const obj = await withConcurrency(() => this.native.getObject(index));
|
|
51
|
+
// wrap the native render function on image objects with the concurrency semaphore
|
|
52
|
+
if (obj.type === 'image') {
|
|
53
|
+
const nativeRender = obj.render.bind(obj);
|
|
54
|
+
obj.render = ((opts) => withConcurrency(() => nativeRender(opts)));
|
|
55
|
+
}
|
|
56
|
+
return obj;
|
|
50
57
|
}
|
|
51
58
|
/** Iterates over all page objects (text spans, paths, images, etc.). */
|
|
52
59
|
async *objects() {
|
|
@@ -60,19 +67,19 @@ export class PDFiumPage {
|
|
|
60
67
|
}
|
|
61
68
|
/** Returns all links on the page. */
|
|
62
69
|
getLinks() {
|
|
63
|
-
return this.native.getLinks();
|
|
70
|
+
return withConcurrency(() => this.native.getLinks());
|
|
64
71
|
}
|
|
65
72
|
/** Searches for text on the page. Returns matches with positions and bounding rects. */
|
|
66
73
|
search(text, options) {
|
|
67
|
-
return this.native.search(text, options);
|
|
74
|
+
return withConcurrency(() => this.native.search(text, options));
|
|
68
75
|
}
|
|
69
76
|
/** Returns all annotations on the page. */
|
|
70
77
|
getAnnotations() {
|
|
71
|
-
return this.native.getAnnotations();
|
|
78
|
+
return withConcurrency(() => this.native.getAnnotations());
|
|
72
79
|
}
|
|
73
80
|
/** Returns all form fields on the page. */
|
|
74
81
|
getFormFields() {
|
|
75
|
-
return this.native.getFormFields();
|
|
82
|
+
return withConcurrency(() => this.native.getFormFields());
|
|
76
83
|
}
|
|
77
84
|
}
|
|
78
85
|
//# sourceMappingURL=page.js.map
|
package/dist/page.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"page.js","sourceRoot":"","sources":["../lib/page.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"page.js","sourceRoot":"","sources":["../lib/page.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAenD;;GAEG;AACH,MAAM,OAAO,UAAU;IAqBD;IApBpB,+CAA+C;IACtC,KAAK,CAAS;IACvB,gDAAgD;IACvC,MAAM,CAAS;IACxB,0BAA0B;IACjB,MAAM,CAAS;IACxB,gEAAgE;IACvD,WAAW,CAAS;IAC7B,kEAAkE;IACzD,QAAQ,CAAS;IAC1B,yCAAyC;IAChC,eAAe,CAAU;IAClC,2FAA2F;IAClF,KAAK,CAAU;IACxB,oDAAoD;IAC3C,OAAO,CAAoB;IACpC,iEAAiE;IACxD,OAAO,CAAoB;IAEpC,gBAAgB;IAChB,YAAoB,MAAkB;QAAlB,WAAM,GAAN,MAAM,CAAY;QACpC,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC5B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC5B,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;QACtC,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,Q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CAAC,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC,EAAE,EAAE,CAAC;YAC1C,MAAM,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QAC1B,CAAC;IACH,CAAC;IAED,2CAA2C;IAC3C,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;IAED,qCAAqC;IACrC,QAAQ;QACN,OAAO,eAAe,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;IACvD,CAAC;IAED,wFAAwF;IACxF,MAAM,CAAC,IAAY,EAAE,OAAuB;QAC1C,OAAO,eAAe,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;IAClE,CAAC;IAED,2CAA2C;IAC3C,cAAc;QACZ,OAAO,eAAe,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,cAAc,EAAE,CAAC,CAAC;IAC7D,CAAC;IAED,2CAA2C;IAC3C,aAAa;QACX,OAAO,eAAe,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC,CAAC;IAC5D,CAAC;CACF"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { availableParallelism } from 'node:os';
|
|
2
|
+
import pLimit, { type LimitFunction } from 'p-limit';
|
|
3
|
+
|
|
4
|
+
let limit: LimitFunction = pLimit(availableParallelism());
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Gets or sets the maximum number of concurrent native operations.
|
|
8
|
+
*
|
|
9
|
+
* The default value is the number of CPU cores (`os.availableParallelism()`).
|
|
10
|
+
* A value of `0` resets to the default.
|
|
11
|
+
*/
|
|
12
|
+
export function concurrency(value?: number): number {
|
|
13
|
+
if (value !== undefined) {
|
|
14
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
15
|
+
throw new TypeError('Concurrency must be a non-negative integer');
|
|
16
|
+
}
|
|
17
|
+
limit = pLimit(value === 0 ? availableParallelism() : value);
|
|
18
|
+
}
|
|
19
|
+
return limit.concurrency;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function withConcurrency<T>(fn: () => Promise<T>): Promise<T> {
|
|
23
|
+
return limit(fn);
|
|
24
|
+
}
|
package/lib/document.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { withConcurrency } from './concurrency.js';
|
|
1
2
|
import { PDFiumPage } from './page.js';
|
|
2
3
|
import type { Bookmark, DocumentMetadata, NativeDocument } from './types.js';
|
|
3
4
|
|
|
@@ -18,7 +19,7 @@ export class PDFiumDocument {
|
|
|
18
19
|
|
|
19
20
|
/** Gets a page by 0-based index. */
|
|
20
21
|
async getPage(index: number): Promise<PDFiumPage> {
|
|
21
|
-
return new PDFiumPage(await this.native.getPage(index));
|
|
22
|
+
return new PDFiumPage(await withConcurrency(() => this.native.getPage(index)));
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
/** Iterates over all pages. Caller is responsible for closing each page. */
|
|
@@ -35,6 +36,6 @@ export class PDFiumDocument {
|
|
|
35
36
|
|
|
36
37
|
/** Returns the bookmark/outline tree. */
|
|
37
38
|
getBookmarks(): Promise<Bookmark[]> {
|
|
38
|
-
return this.native.getBookmarks();
|
|
39
|
+
return withConcurrency(() => this.native.getBookmarks());
|
|
39
40
|
}
|
|
40
41
|
}
|
package/lib/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createRequire } from 'node:module';
|
|
2
2
|
import { dirname, resolve } from 'node:path';
|
|
3
3
|
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { withConcurrency } from './concurrency.js';
|
|
4
5
|
import { PDFiumDocument } from './document.js';
|
|
5
6
|
import { parseNativeError } from './errors.js';
|
|
6
7
|
import type { NativeAddon } from './types.js';
|
|
@@ -27,12 +28,13 @@ const addon: NativeAddon = require('../build/Release/pdfium.node');
|
|
|
27
28
|
*/
|
|
28
29
|
export async function loadDocument(input: PdfInput, password?: string): Promise<PDFiumDocument> {
|
|
29
30
|
try {
|
|
30
|
-
return new PDFiumDocument(await addon.loadDocument(input, password));
|
|
31
|
+
return new PDFiumDocument(await withConcurrency(() => addon.loadDocument(input, password)));
|
|
31
32
|
} catch (err) {
|
|
32
33
|
throw parseNativeError(err);
|
|
33
34
|
}
|
|
34
35
|
}
|
|
35
36
|
|
|
37
|
+
export { concurrency } from './concurrency.js';
|
|
36
38
|
export { PDFiumDocument } from './document.js';
|
|
37
39
|
export {
|
|
38
40
|
PDFiumError,
|
package/lib/page.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import { withConcurrency } from './concurrency.js';
|
|
1
2
|
import type {
|
|
2
3
|
Annotation,
|
|
3
4
|
FormField,
|
|
5
|
+
ImagePageObject,
|
|
6
|
+
ImageRenderOptions,
|
|
4
7
|
Link,
|
|
5
8
|
NativePage,
|
|
6
9
|
PageObject,
|
|
@@ -48,7 +51,7 @@ export class PDFiumPage {
|
|
|
48
51
|
|
|
49
52
|
/** Extracts all text from the page. */
|
|
50
53
|
getText(): Promise<string> {
|
|
51
|
-
return this.native.getText();
|
|
54
|
+
return withConcurrency(() => this.native.getText());
|
|
52
55
|
}
|
|
53
56
|
|
|
54
57
|
/** Renders the page to a file. */
|
|
@@ -56,12 +59,19 @@ export class PDFiumPage {
|
|
|
56
59
|
/** Renders the page to an encoded image buffer (JPEG or PNG). */
|
|
57
60
|
render(options?: PageRenderOptions): Promise<Buffer>;
|
|
58
61
|
render(options?: PageRenderOptions): Promise<Buffer | void> {
|
|
59
|
-
return this.native.render(options);
|
|
62
|
+
return withConcurrency(() => this.native.render(options));
|
|
60
63
|
}
|
|
61
64
|
|
|
62
65
|
/** Returns the page object at the given index with type and bounds. */
|
|
63
|
-
getObject(index: number): Promise<PageObject> {
|
|
64
|
-
|
|
66
|
+
async getObject(index: number): Promise<PageObject> {
|
|
67
|
+
const obj = await withConcurrency(() => this.native.getObject(index));
|
|
68
|
+
// wrap the native render function on image objects with the concurrency semaphore
|
|
69
|
+
if (obj.type === 'image') {
|
|
70
|
+
const nativeRender = (obj as ImagePageObject).render.bind(obj);
|
|
71
|
+
(obj as ImagePageObject).render = ((opts?: ImageRenderOptions) =>
|
|
72
|
+
withConcurrency(() => nativeRender(opts))) as ImagePageObject['render'];
|
|
73
|
+
}
|
|
74
|
+
return obj;
|
|
65
75
|
}
|
|
66
76
|
|
|
67
77
|
/** Iterates over all page objects (text spans, paths, images, etc.). */
|
|
@@ -78,21 +88,21 @@ export class PDFiumPage {
|
|
|
78
88
|
|
|
79
89
|
/** Returns all links on the page. */
|
|
80
90
|
getLinks(): Promise<Link[]> {
|
|
81
|
-
return this.native.getLinks();
|
|
91
|
+
return withConcurrency(() => this.native.getLinks());
|
|
82
92
|
}
|
|
83
93
|
|
|
84
94
|
/** Searches for text on the page. Returns matches with positions and bounding rects. */
|
|
85
95
|
search(text: string, options?: SearchOptions): Promise<SearchMatch[]> {
|
|
86
|
-
return this.native.search(text, options);
|
|
96
|
+
return withConcurrency(() => this.native.search(text, options));
|
|
87
97
|
}
|
|
88
98
|
|
|
89
99
|
/** Returns all annotations on the page. */
|
|
90
100
|
getAnnotations(): Promise<Annotation[]> {
|
|
91
|
-
return this.native.getAnnotations();
|
|
101
|
+
return withConcurrency(() => this.native.getAnnotations());
|
|
92
102
|
}
|
|
93
103
|
|
|
94
104
|
/** Returns all form fields on the page. */
|
|
95
105
|
getFormFields(): Promise<FormField[]> {
|
|
96
|
-
return this.native.getFormFields();
|
|
106
|
+
return withConcurrency(() => this.native.getFormFields());
|
|
97
107
|
}
|
|
98
108
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pdfium-native",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Native Node.js bindings for PDFium",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -39,7 +39,10 @@
|
|
|
39
39
|
"src",
|
|
40
40
|
"lib",
|
|
41
41
|
"scripts",
|
|
42
|
-
"binding.gyp"
|
|
42
|
+
"binding.gyp",
|
|
43
|
+
"README.md",
|
|
44
|
+
"CHANGELOG.md",
|
|
45
|
+
"LICENSE"
|
|
43
46
|
],
|
|
44
47
|
"repository": {
|
|
45
48
|
"type": "git",
|
|
@@ -73,10 +76,16 @@
|
|
|
73
76
|
"lint": "eslint --fix .",
|
|
74
77
|
"lint:check": "eslint .",
|
|
75
78
|
"format": "prettier --write .",
|
|
76
|
-
"format:check": "prettier --check ."
|
|
79
|
+
"format:check": "prettier --check .",
|
|
80
|
+
"prepare": "husky"
|
|
81
|
+
},
|
|
82
|
+
"lint-staged": {
|
|
83
|
+
"*.{ts,js,json,md,yaml,yml}": "prettier --write",
|
|
84
|
+
"*.{ts,js}": "eslint --max-warnings 0"
|
|
77
85
|
},
|
|
78
86
|
"dependencies": {
|
|
79
|
-
"node-addon-api": "^8.0.0"
|
|
87
|
+
"node-addon-api": "^8.0.0",
|
|
88
|
+
"p-limit": "^7.3.0"
|
|
80
89
|
},
|
|
81
90
|
"devDependencies": {
|
|
82
91
|
"@eslint/js": "^10.0.1",
|
|
@@ -84,6 +93,8 @@
|
|
|
84
93
|
"eslint": "^10.1.0",
|
|
85
94
|
"eslint-config-prettier": "^10.1.8",
|
|
86
95
|
"eslint-plugin-prettier": "^5.5.5",
|
|
96
|
+
"husky": "^9.1.7",
|
|
97
|
+
"lint-staged": "^16.4.0",
|
|
87
98
|
"node-gyp": "^11.0.0",
|
|
88
99
|
"prettier": "^3.8.1",
|
|
89
100
|
"typescript": "^5.9.3",
|