@d0paminedriven/pdfdown-ocr 0.9.9 → 0.10.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/README.md +79 -78
- package/index.d.ts +161 -139
- package/index.js +323 -45
- package/package.json +12 -14
package/README.md
CHANGED
|
@@ -62,50 +62,47 @@ This package exports everything from `@d0paminedriven/pdfdown` (text, images, an
|
|
|
62
62
|
export declare function extractTextWithOcrPerPage(
|
|
63
63
|
buffer: Buffer,
|
|
64
64
|
opts?: OcrOptions,
|
|
65
|
-
): Array<OcrPageText
|
|
65
|
+
): Array<OcrPageText>;
|
|
66
66
|
|
|
67
67
|
export declare function extractTextWithOcrPerPageAsync(
|
|
68
68
|
buffer: Buffer,
|
|
69
69
|
opts?: OcrOptions,
|
|
70
|
-
): Promise<Array<OcrPageText
|
|
70
|
+
): Promise<Array<OcrPageText>>;
|
|
71
71
|
|
|
72
72
|
// Full document extraction with OCR text fallback
|
|
73
|
-
export declare function pdfDocumentOcr(
|
|
74
|
-
buffer: Buffer,
|
|
75
|
-
opts?: OcrOptions,
|
|
76
|
-
): PdfDocumentOcr
|
|
73
|
+
export declare function pdfDocumentOcr(buffer: Buffer, opts?: OcrOptions): PdfDocumentOcr;
|
|
77
74
|
|
|
78
75
|
export declare function pdfDocumentOcrAsync(
|
|
79
76
|
buffer: Buffer,
|
|
80
77
|
opts?: OcrOptions,
|
|
81
|
-
): Promise<PdfDocumentOcr
|
|
78
|
+
): Promise<PdfDocumentOcr>;
|
|
82
79
|
```
|
|
83
80
|
|
|
84
81
|
### `PdfDown` class (includes OCR methods)
|
|
85
82
|
|
|
86
83
|
```typescript
|
|
87
84
|
export declare class PdfDown {
|
|
88
|
-
constructor(buffer: Buffer)
|
|
85
|
+
constructor(buffer: Buffer);
|
|
89
86
|
|
|
90
87
|
// ── Base methods ──
|
|
91
|
-
textPerPage(): Array<PageText
|
|
92
|
-
textPerPageAsync(): Promise<Array<PageText
|
|
93
|
-
imagesPerPage(): Array<PageImage
|
|
94
|
-
imagesPerPageAsync(): Promise<Array<PageImage
|
|
95
|
-
annotationsPerPage(): Array<PageAnnotation
|
|
96
|
-
annotationsPerPageAsync(): Promise<Array<PageAnnotation
|
|
97
|
-
structuredText(): Array<StructuredPageText
|
|
98
|
-
structuredTextAsync(): Promise<Array<StructuredPageText
|
|
99
|
-
metadata(): PdfMeta
|
|
100
|
-
metadataAsync(): Promise<PdfMeta
|
|
101
|
-
document(): PdfDocument
|
|
102
|
-
documentAsync(): Promise<PdfDocument
|
|
88
|
+
textPerPage(): Array<PageText>;
|
|
89
|
+
textPerPageAsync(): Promise<Array<PageText>>;
|
|
90
|
+
imagesPerPage(): Array<PageImage>;
|
|
91
|
+
imagesPerPageAsync(): Promise<Array<PageImage>>;
|
|
92
|
+
annotationsPerPage(): Array<PageAnnotation>;
|
|
93
|
+
annotationsPerPageAsync(): Promise<Array<PageAnnotation>>;
|
|
94
|
+
structuredText(): Array<StructuredPageText>;
|
|
95
|
+
structuredTextAsync(): Promise<Array<StructuredPageText>>;
|
|
96
|
+
metadata(): PdfMeta;
|
|
97
|
+
metadataAsync(): Promise<PdfMeta>;
|
|
98
|
+
document(): PdfDocument;
|
|
99
|
+
documentAsync(): Promise<PdfDocument>;
|
|
103
100
|
|
|
104
101
|
// ── OCR methods ──
|
|
105
|
-
textWithOcrPerPage(opts?: OcrOptions): Array<OcrPageText
|
|
106
|
-
textWithOcrPerPageAsync(opts?: OcrOptions): Promise<Array<OcrPageText
|
|
107
|
-
documentOcr(opts?: OcrOptions): PdfDocumentOcr
|
|
108
|
-
documentOcrAsync(opts?: OcrOptions): Promise<PdfDocumentOcr
|
|
102
|
+
textWithOcrPerPage(opts?: OcrOptions): Array<OcrPageText>;
|
|
103
|
+
textWithOcrPerPageAsync(opts?: OcrOptions): Promise<Array<OcrPageText>>;
|
|
104
|
+
documentOcr(opts?: OcrOptions): PdfDocumentOcr;
|
|
105
|
+
documentOcrAsync(opts?: OcrOptions): Promise<PdfDocumentOcr>;
|
|
109
106
|
}
|
|
110
107
|
```
|
|
111
108
|
|
|
@@ -113,46 +110,46 @@ export declare class PdfDown {
|
|
|
113
110
|
|
|
114
111
|
```typescript
|
|
115
112
|
export const enum TextSource {
|
|
116
|
-
Native =
|
|
117
|
-
Ocr =
|
|
113
|
+
Native = "Native",
|
|
114
|
+
Ocr = "Ocr",
|
|
118
115
|
}
|
|
119
116
|
|
|
120
117
|
export interface OcrPageText {
|
|
121
|
-
page: number
|
|
122
|
-
text: string
|
|
123
|
-
source: TextSource
|
|
118
|
+
page: number;
|
|
119
|
+
text: string;
|
|
120
|
+
source: TextSource;
|
|
124
121
|
}
|
|
125
122
|
|
|
126
123
|
export interface OcrStructuredPageText {
|
|
127
|
-
page: number
|
|
128
|
-
header: string
|
|
129
|
-
body: string
|
|
130
|
-
footer: string
|
|
131
|
-
source: TextSource
|
|
124
|
+
page: number;
|
|
125
|
+
header: string;
|
|
126
|
+
body: string;
|
|
127
|
+
footer: string;
|
|
128
|
+
source: TextSource;
|
|
132
129
|
}
|
|
133
130
|
|
|
134
131
|
export interface OcrOptions {
|
|
135
|
-
lang?: string
|
|
136
|
-
minTextLength?: number // non-whitespace char threshold before OCR fallback, default 1
|
|
137
|
-
maxThreads?: number
|
|
132
|
+
lang?: string; // Tesseract language code, default "eng"
|
|
133
|
+
minTextLength?: number; // non-whitespace char threshold before OCR fallback, default 1
|
|
134
|
+
maxThreads?: number; // cap on Rayon threads for OCR parallelism, default 4, clamped to [1, available CPUs]
|
|
138
135
|
}
|
|
139
136
|
|
|
140
137
|
export interface PdfDocumentOcr {
|
|
141
|
-
version: string
|
|
142
|
-
isLinearized: boolean
|
|
143
|
-
pageCount: number
|
|
144
|
-
creator?: string
|
|
145
|
-
producer?: string
|
|
146
|
-
creationDate?: string
|
|
147
|
-
modificationDate?: string
|
|
148
|
-
totalImages: number
|
|
149
|
-
totalAnnotations: number
|
|
150
|
-
imagePages: Array<number
|
|
151
|
-
annotationPages: Array<number
|
|
152
|
-
text: Array<OcrPageText
|
|
153
|
-
structuredText: Array<OcrStructuredPageText
|
|
154
|
-
images: Array<PageImage
|
|
155
|
-
annotations: Array<PageAnnotation
|
|
138
|
+
version: string;
|
|
139
|
+
isLinearized: boolean;
|
|
140
|
+
pageCount: number;
|
|
141
|
+
creator?: string;
|
|
142
|
+
producer?: string;
|
|
143
|
+
creationDate?: string;
|
|
144
|
+
modificationDate?: string;
|
|
145
|
+
totalImages: number;
|
|
146
|
+
totalAnnotations: number;
|
|
147
|
+
imagePages: Array<number>;
|
|
148
|
+
annotationPages: Array<number>;
|
|
149
|
+
text: Array<OcrPageText>;
|
|
150
|
+
structuredText: Array<OcrStructuredPageText>;
|
|
151
|
+
images: Array<PageImage>;
|
|
152
|
+
annotations: Array<PageAnnotation>;
|
|
156
153
|
}
|
|
157
154
|
```
|
|
158
155
|
|
|
@@ -163,41 +160,41 @@ export interface PdfDocumentOcr {
|
|
|
163
160
|
### Standalone
|
|
164
161
|
|
|
165
162
|
```typescript
|
|
166
|
-
import { readFile } from
|
|
167
|
-
import { extractTextWithOcrPerPageAsync } from
|
|
163
|
+
import { readFile } from "fs/promises";
|
|
164
|
+
import { extractTextWithOcrPerPageAsync } from "@d0paminedriven/pdfdown-ocr";
|
|
168
165
|
|
|
169
|
-
const pdf = await readFile(
|
|
170
|
-
const pages = await extractTextWithOcrPerPageAsync(pdf, { lang:
|
|
166
|
+
const pdf = await readFile("scanned-document.pdf");
|
|
167
|
+
const pages = await extractTextWithOcrPerPageAsync(pdf, { lang: "eng", minTextLength: 10 });
|
|
171
168
|
|
|
172
169
|
for (const { page, text, source } of pages) {
|
|
173
|
-
console.log(`Page ${page} [${source}]: ${text.slice(0, 100)}...`)
|
|
170
|
+
console.log(`Page ${page} [${source}]: ${text.slice(0, 100)}...`);
|
|
174
171
|
}
|
|
175
172
|
```
|
|
176
173
|
|
|
177
174
|
### Class-based (parse once, extract many)
|
|
178
175
|
|
|
179
176
|
```typescript
|
|
180
|
-
import { readFile } from
|
|
181
|
-
import { PdfDown } from
|
|
177
|
+
import { readFile } from "fs/promises";
|
|
178
|
+
import { PdfDown } from "@d0paminedriven/pdfdown-ocr";
|
|
182
179
|
|
|
183
|
-
const pdf = new PdfDown(await readFile(
|
|
180
|
+
const pdf = new PdfDown(await readFile("scanned-document.pdf"));
|
|
184
181
|
|
|
185
182
|
// OCR text extraction
|
|
186
|
-
const pages = await pdf.textWithOcrPerPageAsync({ lang:
|
|
183
|
+
const pages = await pdf.textWithOcrPerPageAsync({ lang: "eng", minTextLength: 10 });
|
|
187
184
|
|
|
188
185
|
// All base methods work too
|
|
189
|
-
const images = await pdf.imagesPerPageAsync()
|
|
190
|
-
const meta = pdf.metadata()
|
|
186
|
+
const images = await pdf.imagesPerPageAsync();
|
|
187
|
+
const meta = pdf.metadata();
|
|
191
188
|
```
|
|
192
189
|
|
|
193
190
|
### Extract everything with OCR in one call
|
|
194
191
|
|
|
195
192
|
```typescript
|
|
196
|
-
import { readFile } from
|
|
197
|
-
import { PdfDown } from
|
|
193
|
+
import { readFile } from "fs/promises";
|
|
194
|
+
import { PdfDown } from "@d0paminedriven/pdfdown-ocr";
|
|
198
195
|
|
|
199
|
-
const pdf = new PdfDown(await readFile(
|
|
200
|
-
const result = await pdf.documentOcrAsync({ minTextLength: 10 })
|
|
196
|
+
const pdf = new PdfDown(await readFile("scanned-document.pdf"));
|
|
197
|
+
const result = await pdf.documentOcrAsync({ minTextLength: 10 });
|
|
201
198
|
|
|
202
199
|
// result.text — OcrPageText[] (page, text, source per page)
|
|
203
200
|
// result.structuredText — OcrStructuredPageText[] (header/body/footer + source per page)
|
|
@@ -209,24 +206,24 @@ const result = await pdf.documentOcrAsync({ minTextLength: 10 })
|
|
|
209
206
|
### Combined: OCR text + images for multimodal pipelines
|
|
210
207
|
|
|
211
208
|
```typescript
|
|
212
|
-
import { readFile } from
|
|
213
|
-
import { PdfDown } from
|
|
209
|
+
import { readFile } from "fs/promises";
|
|
210
|
+
import { PdfDown } from "@d0paminedriven/pdfdown-ocr";
|
|
214
211
|
|
|
215
|
-
const pdf = new PdfDown(await readFile(
|
|
212
|
+
const pdf = new PdfDown(await readFile("scanned-document.pdf"));
|
|
216
213
|
|
|
217
214
|
const [ocrText, images] = await Promise.all([
|
|
218
215
|
pdf.textWithOcrPerPageAsync({ minTextLength: 10 }),
|
|
219
216
|
pdf.imagesPerPageAsync(),
|
|
220
|
-
])
|
|
217
|
+
]);
|
|
221
218
|
|
|
222
|
-
const imagesByPage = Map.groupBy(images, (img) => img.page)
|
|
219
|
+
const imagesByPage = Map.groupBy(images, (img) => img.page);
|
|
223
220
|
|
|
224
221
|
for (const { page, text, source } of ocrText) {
|
|
225
222
|
const pageImages = (imagesByPage.get(page) ?? []).map((img) => ({
|
|
226
|
-
dataUrl: `data:image/png;base64,${img.data.toString(
|
|
223
|
+
dataUrl: `data:image/png;base64,${img.data.toString("base64")}`,
|
|
227
224
|
width: img.width,
|
|
228
225
|
height: img.height,
|
|
229
|
-
}))
|
|
226
|
+
}));
|
|
230
227
|
// Send { page, text, source, images: pageImages } to your embedding pipeline
|
|
231
228
|
}
|
|
232
229
|
```
|
|
@@ -235,9 +232,9 @@ for (const { page, text, source } of ocrText) {
|
|
|
235
232
|
|
|
236
233
|
Both methods extract everything from a PDF in a single call. The difference is how text is extracted:
|
|
237
234
|
|
|
238
|
-
| Method
|
|
239
|
-
|
|
240
|
-
| `document()` / `documentAsync()`
|
|
235
|
+
| Method | Text extraction | Return type | Use when |
|
|
236
|
+
| -------------------------------------- | ------------------------ | ---------------- | ---------------------------------------- |
|
|
237
|
+
| `document()` / `documentAsync()` | Native PDF text only | `PdfDocument` | PDF has selectable text |
|
|
241
238
|
| `documentOcr()` / `documentOcrAsync()` | Native with OCR fallback | `PdfDocumentOcr` | PDF may contain scanned/image-only pages |
|
|
242
239
|
|
|
243
240
|
`PdfDocumentOcr` uses `OcrPageText` (with `source: 'Native' | 'Ocr'`) and `OcrStructuredPageText` (with header/body/footer split plus source) instead of the base `PageText` and `StructuredPageText` types. Images, annotations, and metadata are identical in both.
|
|
@@ -256,9 +253,13 @@ Both methods extract everything from a PDF in a single call. The difference is h
|
|
|
256
253
|
|
|
257
254
|
Prebuilt binaries are provided for:
|
|
258
255
|
|
|
259
|
-
- macOS (
|
|
256
|
+
- macOS (ARM64)
|
|
260
257
|
- Linux glibc (x64, ARM64)
|
|
261
258
|
|
|
259
|
+
Intel macOS is not built: the OCR package links the system Tesseract, and Homebrew no longer
|
|
260
|
+
ships Intel bottles, so there is no supported way to obtain the library on that platform.
|
|
261
|
+
The base `@d0paminedriven/pdfdown` package still ships an Intel macOS binary.
|
|
262
|
+
|
|
262
263
|
## Relationship to `@d0paminedriven/pdfdown`
|
|
263
264
|
|
|
264
265
|
Same Rust codebase, compiled with the `ocr` Cargo feature flag enabled. This package is a strict superset -- you can use it as a drop-in replacement for the base package if you need OCR capabilities.
|
package/index.d.ts
CHANGED
|
@@ -1,212 +1,234 @@
|
|
|
1
1
|
/* auto-generated by NAPI-RS */
|
|
2
2
|
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Which binding artifact the generated loader actually loaded: `'native'` for
|
|
6
|
+
* a native addon, otherwise the `platformArchABI` of the WASI flavor. Every
|
|
7
|
+
* flavor napi-rs can build is listed, because `NAPI_RS_NATIVE_LIBRARY_PATH`
|
|
8
|
+
* can point the loader at a WASI artifact this package does not build itself.
|
|
9
|
+
*/
|
|
10
|
+
export declare const __napiBindingTarget: "native" | "wasm32-wasi" | "wasm32-wasip1";
|
|
11
|
+
|
|
3
12
|
export declare class PdfDown {
|
|
4
|
-
constructor(buffer: Buffer)
|
|
5
|
-
textPerPage(): Array<PageText
|
|
6
|
-
imagesPerPage(): Array<PageImage
|
|
7
|
-
annotationsPerPage(): Array<PageAnnotation
|
|
8
|
-
metadata(): PdfMeta
|
|
9
|
-
textPerPageAsync(): Promise<Array<PageText
|
|
10
|
-
imagesPerPageAsync(): Promise<Array<PageImage
|
|
11
|
-
annotationsPerPageAsync(): Promise<Array<PageAnnotation
|
|
12
|
-
metadataAsync(): Promise<PdfMeta
|
|
13
|
-
document(): PdfDocument
|
|
14
|
-
documentAsync(): Promise<PdfDocument
|
|
15
|
-
structuredText(): Array<StructuredPageText
|
|
16
|
-
structuredTextAsync(): Promise<Array<StructuredPageText
|
|
17
|
-
textWithOcrPerPage(opts?: OcrOptions | undefined | null): Array<OcrPageText
|
|
18
|
-
textWithOcrPerPageAsync(opts?: OcrOptions | undefined | null): Promise<Array<OcrPageText
|
|
19
|
-
documentOcr(opts?: OcrOptions | undefined | null): PdfDocumentOcr
|
|
20
|
-
documentOcrAsync(opts?: OcrOptions | undefined | null): Promise<PdfDocumentOcr
|
|
21
|
-
renderPagesAsync(opts?: RenderOptions | undefined | null): Promise<Array<RenderedPage
|
|
13
|
+
constructor(buffer: Buffer);
|
|
14
|
+
textPerPage(): Array<PageText>;
|
|
15
|
+
imagesPerPage(): Array<PageImage>;
|
|
16
|
+
annotationsPerPage(): Array<PageAnnotation>;
|
|
17
|
+
metadata(): PdfMeta;
|
|
18
|
+
textPerPageAsync(): Promise<Array<PageText>>;
|
|
19
|
+
imagesPerPageAsync(): Promise<Array<PageImage>>;
|
|
20
|
+
annotationsPerPageAsync(): Promise<Array<PageAnnotation>>;
|
|
21
|
+
metadataAsync(): Promise<PdfMeta>;
|
|
22
|
+
document(): PdfDocument;
|
|
23
|
+
documentAsync(): Promise<PdfDocument>;
|
|
24
|
+
structuredText(): Array<StructuredPageText>;
|
|
25
|
+
structuredTextAsync(): Promise<Array<StructuredPageText>>;
|
|
26
|
+
textWithOcrPerPage(opts?: OcrOptions | undefined | null): Array<OcrPageText>;
|
|
27
|
+
textWithOcrPerPageAsync(opts?: OcrOptions | undefined | null): Promise<Array<OcrPageText>>;
|
|
28
|
+
documentOcr(opts?: OcrOptions | undefined | null): PdfDocumentOcr;
|
|
29
|
+
documentOcrAsync(opts?: OcrOptions | undefined | null): Promise<PdfDocumentOcr>;
|
|
30
|
+
renderPagesAsync(opts?: RenderOptions | undefined | null): Promise<Array<RenderedPage>>;
|
|
22
31
|
}
|
|
23
32
|
|
|
24
|
-
export type BoxType =
|
|
25
|
-
'MediaBox'|
|
|
26
|
-
'Unknown';
|
|
33
|
+
export type BoxType = "CropBox" | "MediaBox" | "Unknown";
|
|
27
34
|
|
|
28
|
-
export declare function capabilities(): Capabilities
|
|
35
|
+
export declare function capabilities(): Capabilities;
|
|
29
36
|
|
|
30
37
|
export interface Capabilities {
|
|
31
|
-
ocr: boolean
|
|
32
|
-
render: boolean
|
|
38
|
+
ocr: boolean;
|
|
39
|
+
render: boolean;
|
|
33
40
|
}
|
|
34
41
|
|
|
35
|
-
export declare function extractAnnotationsPerPage(buffer: Buffer): Array<PageAnnotation
|
|
42
|
+
export declare function extractAnnotationsPerPage(buffer: Buffer): Array<PageAnnotation>;
|
|
36
43
|
|
|
37
|
-
export declare function extractAnnotationsPerPageAsync(
|
|
44
|
+
export declare function extractAnnotationsPerPageAsync(
|
|
45
|
+
buffer: Buffer,
|
|
46
|
+
): Promise<Array<PageAnnotation>>;
|
|
38
47
|
|
|
39
|
-
export declare function extractImagesPerPage(buffer: Buffer): Array<PageImage
|
|
48
|
+
export declare function extractImagesPerPage(buffer: Buffer): Array<PageImage>;
|
|
40
49
|
|
|
41
|
-
export declare function extractImagesPerPageAsync(buffer: Buffer): Promise<Array<PageImage
|
|
50
|
+
export declare function extractImagesPerPageAsync(buffer: Buffer): Promise<Array<PageImage>>;
|
|
42
51
|
|
|
43
|
-
export declare function extractStructuredTextPerPage(buffer: Buffer): Array<StructuredPageText
|
|
52
|
+
export declare function extractStructuredTextPerPage(buffer: Buffer): Array<StructuredPageText>;
|
|
44
53
|
|
|
45
|
-
export declare function extractStructuredTextPerPageAsync(
|
|
54
|
+
export declare function extractStructuredTextPerPageAsync(
|
|
55
|
+
buffer: Buffer,
|
|
56
|
+
): Promise<Array<StructuredPageText>>;
|
|
46
57
|
|
|
47
|
-
export declare function extractTextPerPage(buffer: Buffer): Array<PageText
|
|
58
|
+
export declare function extractTextPerPage(buffer: Buffer): Array<PageText>;
|
|
48
59
|
|
|
49
|
-
export declare function extractTextPerPageAsync(buffer: Buffer): Promise<Array<PageText
|
|
60
|
+
export declare function extractTextPerPageAsync(buffer: Buffer): Promise<Array<PageText>>;
|
|
50
61
|
|
|
51
|
-
export declare function extractTextWithOcrPerPage(
|
|
62
|
+
export declare function extractTextWithOcrPerPage(
|
|
63
|
+
buffer: Buffer,
|
|
64
|
+
opts?: OcrOptions | undefined | null,
|
|
65
|
+
): Array<OcrPageText>;
|
|
52
66
|
|
|
53
|
-
export declare function extractTextWithOcrPerPageAsync(
|
|
67
|
+
export declare function extractTextWithOcrPerPageAsync(
|
|
68
|
+
buffer: Buffer,
|
|
69
|
+
opts?: OcrOptions | undefined | null,
|
|
70
|
+
): Promise<Array<OcrPageText>>;
|
|
54
71
|
|
|
55
72
|
export interface OcrOptions {
|
|
56
|
-
lang?: string
|
|
57
|
-
minTextLength?: number
|
|
58
|
-
maxThreads?: number
|
|
59
|
-
render?: RenderMode
|
|
60
|
-
renderDpi?: number
|
|
61
|
-
pdfiumPath?: string
|
|
73
|
+
lang?: string;
|
|
74
|
+
minTextLength?: number;
|
|
75
|
+
maxThreads?: number;
|
|
76
|
+
render?: RenderMode;
|
|
77
|
+
renderDpi?: number;
|
|
78
|
+
pdfiumPath?: string;
|
|
62
79
|
}
|
|
63
80
|
|
|
64
81
|
export interface OcrPageText {
|
|
65
|
-
page: number
|
|
66
|
-
text: string
|
|
67
|
-
source: TextSource
|
|
82
|
+
page: number;
|
|
83
|
+
text: string;
|
|
84
|
+
source: TextSource;
|
|
68
85
|
}
|
|
69
86
|
|
|
70
87
|
export interface OcrStructuredPageText {
|
|
71
|
-
page: number
|
|
72
|
-
header: string
|
|
73
|
-
body: string
|
|
74
|
-
footer: string
|
|
75
|
-
source: TextSource
|
|
88
|
+
page: number;
|
|
89
|
+
header: string;
|
|
90
|
+
body: string;
|
|
91
|
+
footer: string;
|
|
92
|
+
source: TextSource;
|
|
76
93
|
}
|
|
77
94
|
|
|
78
95
|
export interface PageAnnotation {
|
|
79
|
-
page: number
|
|
80
|
-
subtype: string
|
|
81
|
-
rect: Array<number
|
|
82
|
-
uri?: string
|
|
83
|
-
dest?: string
|
|
84
|
-
content?: string
|
|
96
|
+
page: number;
|
|
97
|
+
subtype: string;
|
|
98
|
+
rect: Array<number>;
|
|
99
|
+
uri?: string;
|
|
100
|
+
dest?: string;
|
|
101
|
+
content?: string;
|
|
85
102
|
}
|
|
86
103
|
|
|
87
104
|
export interface PageBox {
|
|
88
105
|
/** Number of pages that share these dimensions. */
|
|
89
|
-
pageCount: number
|
|
90
|
-
left: number
|
|
91
|
-
bottom: number
|
|
92
|
-
right: number
|
|
93
|
-
top: number
|
|
94
|
-
width: number
|
|
95
|
-
height: number
|
|
96
|
-
boxType: BoxType
|
|
106
|
+
pageCount: number;
|
|
107
|
+
left: number;
|
|
108
|
+
bottom: number;
|
|
109
|
+
right: number;
|
|
110
|
+
top: number;
|
|
111
|
+
width: number;
|
|
112
|
+
height: number;
|
|
113
|
+
boxType: BoxType;
|
|
97
114
|
/**
|
|
98
115
|
* Present only on non-dominant boxes — lists the specific pages with these
|
|
99
116
|
* dimensions. `None` on the first (most frequent) entry means "all pages
|
|
100
117
|
* not listed in any other entry's `pages` array."
|
|
101
118
|
*/
|
|
102
|
-
pages?: Array<number
|
|
119
|
+
pages?: Array<number>;
|
|
103
120
|
}
|
|
104
121
|
|
|
105
122
|
export interface PageImage {
|
|
106
|
-
page: number
|
|
107
|
-
imageIndex: number
|
|
108
|
-
width: number
|
|
109
|
-
height: number
|
|
110
|
-
data: Buffer
|
|
111
|
-
colorSpace: string
|
|
112
|
-
bitsPerComponent: number
|
|
113
|
-
filter: string
|
|
114
|
-
xobjectName: string
|
|
115
|
-
objectId: string
|
|
123
|
+
page: number;
|
|
124
|
+
imageIndex: number;
|
|
125
|
+
width: number;
|
|
126
|
+
height: number;
|
|
127
|
+
data: Buffer;
|
|
128
|
+
colorSpace: string;
|
|
129
|
+
bitsPerComponent: number;
|
|
130
|
+
filter: string;
|
|
131
|
+
xobjectName: string;
|
|
132
|
+
objectId: string;
|
|
116
133
|
}
|
|
117
134
|
|
|
118
135
|
export interface PageText {
|
|
119
|
-
page: number
|
|
120
|
-
text: string
|
|
136
|
+
page: number;
|
|
137
|
+
text: string;
|
|
121
138
|
}
|
|
122
139
|
|
|
123
|
-
export declare function pdfDocument(buffer: Buffer): PdfDocument
|
|
140
|
+
export declare function pdfDocument(buffer: Buffer): PdfDocument;
|
|
124
141
|
|
|
125
142
|
export interface PdfDocument {
|
|
126
|
-
version: string
|
|
127
|
-
isLinearized: boolean
|
|
128
|
-
pageCount: number
|
|
129
|
-
creator?: string
|
|
130
|
-
producer?: string
|
|
131
|
-
creationDate?: string
|
|
132
|
-
modificationDate?: string
|
|
133
|
-
pageBoxes: Array<PageBox
|
|
134
|
-
totalImages: number
|
|
135
|
-
totalAnnotations: number
|
|
136
|
-
imagePages: Array<number
|
|
137
|
-
annotationPages: Array<number
|
|
138
|
-
text: Array<PageText
|
|
139
|
-
structuredText: Array<StructuredPageText
|
|
140
|
-
images: Array<PageImage
|
|
141
|
-
annotations: Array<PageAnnotation
|
|
143
|
+
version: string;
|
|
144
|
+
isLinearized: boolean;
|
|
145
|
+
pageCount: number;
|
|
146
|
+
creator?: string;
|
|
147
|
+
producer?: string;
|
|
148
|
+
creationDate?: string;
|
|
149
|
+
modificationDate?: string;
|
|
150
|
+
pageBoxes: Array<PageBox>;
|
|
151
|
+
totalImages: number;
|
|
152
|
+
totalAnnotations: number;
|
|
153
|
+
imagePages: Array<number>;
|
|
154
|
+
annotationPages: Array<number>;
|
|
155
|
+
text: Array<PageText>;
|
|
156
|
+
structuredText: Array<StructuredPageText>;
|
|
157
|
+
images: Array<PageImage>;
|
|
158
|
+
annotations: Array<PageAnnotation>;
|
|
142
159
|
}
|
|
143
160
|
|
|
144
|
-
export declare function pdfDocumentAsync(buffer: Buffer): Promise<PdfDocument
|
|
161
|
+
export declare function pdfDocumentAsync(buffer: Buffer): Promise<PdfDocument>;
|
|
145
162
|
|
|
146
|
-
export declare function pdfDocumentOcr(
|
|
163
|
+
export declare function pdfDocumentOcr(
|
|
164
|
+
buffer: Buffer,
|
|
165
|
+
opts?: OcrOptions | undefined | null,
|
|
166
|
+
): PdfDocumentOcr;
|
|
147
167
|
|
|
148
168
|
export interface PdfDocumentOcr {
|
|
149
|
-
version: string
|
|
150
|
-
isLinearized: boolean
|
|
151
|
-
pageCount: number
|
|
152
|
-
creator?: string
|
|
153
|
-
producer?: string
|
|
154
|
-
creationDate?: string
|
|
155
|
-
modificationDate?: string
|
|
156
|
-
pageBoxes: Array<PageBox
|
|
157
|
-
totalImages: number
|
|
158
|
-
totalAnnotations: number
|
|
159
|
-
imagePages: Array<number
|
|
160
|
-
annotationPages: Array<number
|
|
161
|
-
text: Array<OcrPageText
|
|
162
|
-
structuredText: Array<OcrStructuredPageText
|
|
163
|
-
images: Array<PageImage
|
|
164
|
-
annotations: Array<PageAnnotation
|
|
169
|
+
version: string;
|
|
170
|
+
isLinearized: boolean;
|
|
171
|
+
pageCount: number;
|
|
172
|
+
creator?: string;
|
|
173
|
+
producer?: string;
|
|
174
|
+
creationDate?: string;
|
|
175
|
+
modificationDate?: string;
|
|
176
|
+
pageBoxes: Array<PageBox>;
|
|
177
|
+
totalImages: number;
|
|
178
|
+
totalAnnotations: number;
|
|
179
|
+
imagePages: Array<number>;
|
|
180
|
+
annotationPages: Array<number>;
|
|
181
|
+
text: Array<OcrPageText>;
|
|
182
|
+
structuredText: Array<OcrStructuredPageText>;
|
|
183
|
+
images: Array<PageImage>;
|
|
184
|
+
annotations: Array<PageAnnotation>;
|
|
165
185
|
}
|
|
166
186
|
|
|
167
|
-
export declare function pdfDocumentOcrAsync(
|
|
187
|
+
export declare function pdfDocumentOcrAsync(
|
|
188
|
+
buffer: Buffer,
|
|
189
|
+
opts?: OcrOptions | undefined | null,
|
|
190
|
+
): Promise<PdfDocumentOcr>;
|
|
168
191
|
|
|
169
192
|
export interface PdfMeta {
|
|
170
|
-
pageCount: number
|
|
171
|
-
version: string
|
|
172
|
-
isLinearized: boolean
|
|
173
|
-
creator?: string
|
|
174
|
-
producer?: string
|
|
175
|
-
creationDate?: string
|
|
176
|
-
modificationDate?: string
|
|
177
|
-
pageBoxes: Array<PageBox
|
|
193
|
+
pageCount: number;
|
|
194
|
+
version: string;
|
|
195
|
+
isLinearized: boolean;
|
|
196
|
+
creator?: string;
|
|
197
|
+
producer?: string;
|
|
198
|
+
creationDate?: string;
|
|
199
|
+
modificationDate?: string;
|
|
200
|
+
pageBoxes: Array<PageBox>;
|
|
178
201
|
}
|
|
179
202
|
|
|
180
|
-
export declare function pdfMetadata(buffer: Buffer): PdfMeta
|
|
203
|
+
export declare function pdfMetadata(buffer: Buffer): PdfMeta;
|
|
181
204
|
|
|
182
|
-
export declare function pdfMetadataAsync(buffer: Buffer): Promise<PdfMeta
|
|
205
|
+
export declare function pdfMetadataAsync(buffer: Buffer): Promise<PdfMeta>;
|
|
183
206
|
|
|
184
207
|
export interface RenderedPage {
|
|
185
|
-
page: number
|
|
186
|
-
width: number
|
|
187
|
-
height: number
|
|
188
|
-
dpi: number
|
|
189
|
-
data: Buffer
|
|
208
|
+
page: number;
|
|
209
|
+
width: number;
|
|
210
|
+
height: number;
|
|
211
|
+
dpi: number;
|
|
212
|
+
data: Buffer;
|
|
190
213
|
}
|
|
191
214
|
|
|
192
|
-
export type RenderMode =
|
|
193
|
-
'Never'|
|
|
194
|
-
'Always';
|
|
215
|
+
export type RenderMode = "Auto" | "Never" | "Always";
|
|
195
216
|
|
|
196
217
|
export interface RenderOptions {
|
|
197
|
-
dpi?: number
|
|
198
|
-
mode?: RenderMode
|
|
218
|
+
dpi?: number;
|
|
219
|
+
mode?: RenderMode;
|
|
199
220
|
}
|
|
200
221
|
|
|
201
|
-
export declare function renderPagesAsync(
|
|
222
|
+
export declare function renderPagesAsync(
|
|
223
|
+
buffer: Buffer,
|
|
224
|
+
opts?: RenderOptions | undefined | null,
|
|
225
|
+
): Promise<Array<RenderedPage>>;
|
|
202
226
|
|
|
203
227
|
export interface StructuredPageText {
|
|
204
|
-
page: number
|
|
205
|
-
header: string
|
|
206
|
-
body: string
|
|
207
|
-
footer: string
|
|
228
|
+
page: number;
|
|
229
|
+
header: string;
|
|
230
|
+
body: string;
|
|
231
|
+
footer: string;
|
|
208
232
|
}
|
|
209
233
|
|
|
210
|
-
export type TextSource =
|
|
211
|
-
'Ocr'|
|
|
212
|
-
'Rendered';
|
|
234
|
+
export type TextSource = "Native" | "Ocr" | "Rendered";
|
package/index.js
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
// prettier-ignore
|
|
2
1
|
/* eslint-disable */
|
|
3
2
|
// @ts-nocheck
|
|
4
3
|
/* auto-generated by NAPI-RS */
|
|
5
4
|
|
|
6
|
-
const {
|
|
7
|
-
require = createRequire(__filename)
|
|
8
|
-
|
|
9
|
-
const { readFileSync } = require('node:fs')
|
|
5
|
+
const { readFileSync } = require('fs')
|
|
10
6
|
let nativeBinding = null
|
|
7
|
+
// Which artifact actually loaded. The WASI fallback chain overwrites it with
|
|
8
|
+
// the flavor it resolved; the late native retry below leaves it alone because
|
|
9
|
+
// it only runs while no WASI candidate has been loaded. The take-over check
|
|
10
|
+
// after the load-failure block can also set it.
|
|
11
|
+
let __napiLoadedBindingTarget = 'native'
|
|
12
|
+
// What this loader's own `requireNative()` calls returned. The check after
|
|
13
|
+
// the load-failure block compares it with `nativeBinding` to tell a binding
|
|
14
|
+
// this loader loaded from one that code outside it put in its place.
|
|
15
|
+
let __napiRequiredNativeBinding = null
|
|
11
16
|
const loadErrors = []
|
|
12
17
|
|
|
13
18
|
const isMusl = () => {
|
|
@@ -36,7 +41,7 @@ const isMuslFromFilesystem = () => {
|
|
|
36
41
|
|
|
37
42
|
const isMuslFromReport = () => {
|
|
38
43
|
let report = null
|
|
39
|
-
if (typeof process.report
|
|
44
|
+
if (process.report && typeof process.report.getReport === 'function') {
|
|
40
45
|
process.report.excludeNetwork = true
|
|
41
46
|
report = process.report.getReport()
|
|
42
47
|
}
|
|
@@ -66,7 +71,16 @@ const isMuslFromChildProcess = () => {
|
|
|
66
71
|
function requireNative() {
|
|
67
72
|
if (process.env.NAPI_RS_NATIVE_LIBRARY_PATH) {
|
|
68
73
|
try {
|
|
69
|
-
|
|
74
|
+
const overrideBinding = require(process.env.NAPI_RS_NATIVE_LIBRARY_PATH)
|
|
75
|
+
// The override may be a generated WASI loader, which already reports its
|
|
76
|
+
// own flavor. Adopt it: `module.exports` aliases this object, so claiming
|
|
77
|
+
// 'native' would both misreport the artifact and overwrite the loader's
|
|
78
|
+
// marker through the alias.
|
|
79
|
+
__napiLoadedBindingTarget =
|
|
80
|
+
overrideBinding && typeof overrideBinding.__napiBindingTarget === 'string'
|
|
81
|
+
? overrideBinding.__napiBindingTarget
|
|
82
|
+
: 'native'
|
|
83
|
+
return overrideBinding
|
|
70
84
|
} catch (err) {
|
|
71
85
|
loadErrors.push(err)
|
|
72
86
|
}
|
|
@@ -108,20 +122,38 @@ function requireNative() {
|
|
|
108
122
|
}
|
|
109
123
|
} else if (process.platform === 'win32') {
|
|
110
124
|
if (process.arch === 'x64') {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
125
|
+
if ((process.config && process.config.variables && process.config.variables.shlib_suffix === 'dll.a') || (process.config && process.config.variables && process.config.variables.node_target_type === 'shared_library')) {
|
|
126
|
+
try {
|
|
127
|
+
return require('./pdfdown_ocr.win32-x64-gnu.node')
|
|
128
|
+
} catch (e) {
|
|
129
|
+
loadErrors.push(e)
|
|
130
|
+
}
|
|
131
|
+
try {
|
|
132
|
+
const binding = require('@d0paminedriven/pdfdown-ocr-win32-x64-gnu')
|
|
133
|
+
const bindingPackageVersion = require('@d0paminedriven/pdfdown-ocr-win32-x64-gnu/package.json').version
|
|
134
|
+
if (bindingPackageVersion !== '0.9.9' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
135
|
+
throw new Error(`Native binding package version mismatch, expected 0.9.9 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
136
|
+
}
|
|
137
|
+
return binding
|
|
138
|
+
} catch (e) {
|
|
139
|
+
loadErrors.push(e)
|
|
140
|
+
}
|
|
141
|
+
} else {
|
|
142
|
+
try {
|
|
143
|
+
return require('./pdfdown_ocr.win32-x64-msvc.node')
|
|
144
|
+
} catch (e) {
|
|
145
|
+
loadErrors.push(e)
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
const binding = require('@d0paminedriven/pdfdown-ocr-win32-x64-msvc')
|
|
149
|
+
const bindingPackageVersion = require('@d0paminedriven/pdfdown-ocr-win32-x64-msvc/package.json').version
|
|
150
|
+
if (bindingPackageVersion !== '0.9.9' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
151
|
+
throw new Error(`Native binding package version mismatch, expected 0.9.9 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
152
|
+
}
|
|
153
|
+
return binding
|
|
154
|
+
} catch (e) {
|
|
155
|
+
loadErrors.push(e)
|
|
121
156
|
}
|
|
122
|
-
return binding
|
|
123
|
-
} catch (e) {
|
|
124
|
-
loadErrors.push(e)
|
|
125
157
|
}
|
|
126
158
|
} else if (process.arch === 'ia32') {
|
|
127
159
|
try {
|
|
@@ -242,6 +274,22 @@ function requireNative() {
|
|
|
242
274
|
} catch (e) {
|
|
243
275
|
loadErrors.push(e)
|
|
244
276
|
}
|
|
277
|
+
} else if (process.arch === 'ppc64') {
|
|
278
|
+
try {
|
|
279
|
+
return require('./pdfdown_ocr.freebsd-ppc64.node')
|
|
280
|
+
} catch (e) {
|
|
281
|
+
loadErrors.push(e)
|
|
282
|
+
}
|
|
283
|
+
try {
|
|
284
|
+
const binding = require('@d0paminedriven/pdfdown-ocr-freebsd-ppc64')
|
|
285
|
+
const bindingPackageVersion = require('@d0paminedriven/pdfdown-ocr-freebsd-ppc64/package.json').version
|
|
286
|
+
if (bindingPackageVersion !== '0.9.9' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
287
|
+
throw new Error(`Native binding package version mismatch, expected 0.9.9 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
288
|
+
}
|
|
289
|
+
return binding
|
|
290
|
+
} catch (e) {
|
|
291
|
+
loadErrors.push(e)
|
|
292
|
+
}
|
|
245
293
|
} else {
|
|
246
294
|
loadErrors.push(new Error(`Unsupported architecture on FreeBSD: ${process.arch}`))
|
|
247
295
|
}
|
|
@@ -508,54 +556,284 @@ function requireNative() {
|
|
|
508
556
|
}
|
|
509
557
|
}
|
|
510
558
|
|
|
511
|
-
|
|
559
|
+
function createLoadErrorChain(errors) {
|
|
560
|
+
return errors.reduce((previous, current) => {
|
|
561
|
+
let message
|
|
562
|
+
try {
|
|
563
|
+
message =
|
|
564
|
+
current && typeof current.message === 'string'
|
|
565
|
+
? current.message
|
|
566
|
+
: String(current)
|
|
567
|
+
} catch {
|
|
568
|
+
message = 'Unknown error'
|
|
569
|
+
}
|
|
570
|
+
const error = new Error(message)
|
|
571
|
+
error.cause = previous
|
|
572
|
+
return error
|
|
573
|
+
}, null)
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// NAPI_RS_FORCE_WASI is a tri-state flag:
|
|
577
|
+
// unset / any other value → native binding preferred, WASI is only a fallback
|
|
578
|
+
// 'true' → prefer WASI, but retain native as a lazy fallback
|
|
579
|
+
// 'error' → require WASI without initializing a native fallback
|
|
580
|
+
// Treating any non-empty string as truthy (the historical behavior) meant
|
|
581
|
+
// NAPI_RS_FORCE_WASI=false, NAPI_RS_FORCE_WASI=0, etc. inadvertently triggered
|
|
582
|
+
// the WASI path, causing ENOENT for packages shipped without a .wasi.cjs file.
|
|
583
|
+
//
|
|
584
|
+
// NAPI_RS_WASI_FLAVOR selects one exact generated flavor and implies strict
|
|
585
|
+
// WASI loading. It never crosses into another flavor or falls back to native.
|
|
586
|
+
const __napiWasiFlavors = ['wasm32-wasi']
|
|
587
|
+
const __napiWasiFlavor = process.env.NAPI_RS_WASI_FLAVOR
|
|
588
|
+
const __napiWasiFlavorRequested =
|
|
589
|
+
typeof __napiWasiFlavor === 'string' && __napiWasiFlavor.length > 0
|
|
590
|
+
if (
|
|
591
|
+
__napiWasiFlavorRequested &&
|
|
592
|
+
__napiWasiFlavors.indexOf(__napiWasiFlavor) === -1
|
|
593
|
+
) {
|
|
594
|
+
throw new Error(
|
|
595
|
+
'Unsupported WASI flavor "' +
|
|
596
|
+
__napiWasiFlavor +
|
|
597
|
+
'". Available flavors: ' +
|
|
598
|
+
__napiWasiFlavors.join(', '),
|
|
599
|
+
)
|
|
600
|
+
}
|
|
601
|
+
const forceWasiError = process.env.NAPI_RS_FORCE_WASI === 'error'
|
|
602
|
+
const forceWasi =
|
|
603
|
+
process.env.NAPI_RS_FORCE_WASI === 'true' ||
|
|
604
|
+
forceWasiError ||
|
|
605
|
+
__napiWasiFlavorRequested
|
|
606
|
+
|
|
607
|
+
if (!forceWasi) {
|
|
608
|
+
nativeBinding = requireNative()
|
|
609
|
+
__napiRequiredNativeBinding = nativeBinding
|
|
610
|
+
}
|
|
512
611
|
|
|
513
|
-
if (!nativeBinding ||
|
|
612
|
+
if (!nativeBinding || forceWasi) {
|
|
514
613
|
let wasiBinding = null
|
|
515
|
-
let
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
614
|
+
let wasiBindingLoaded = false
|
|
615
|
+
const wasiBindingErrors = []
|
|
616
|
+
const __napiWasiResolveCandidate = (specifier, isPackage, localArtifacts) => {
|
|
617
|
+
try {
|
|
618
|
+
require.resolve(specifier)
|
|
619
|
+
} catch (resolveError) {
|
|
620
|
+
if (!resolveError || resolveError.code !== 'MODULE_NOT_FOUND') {
|
|
621
|
+
throw resolveError
|
|
622
|
+
}
|
|
623
|
+
if (isPackage) {
|
|
624
|
+
try {
|
|
625
|
+
require.resolve(specifier + '/package.json')
|
|
626
|
+
} catch (packageError) {
|
|
627
|
+
if (packageError && packageError.code === 'MODULE_NOT_FOUND') {
|
|
628
|
+
return resolveError
|
|
629
|
+
}
|
|
630
|
+
// An exports restriction proves the package exists even when its
|
|
631
|
+
// package.json is not public. Preserve the root resolution failure.
|
|
632
|
+
throw resolveError
|
|
633
|
+
}
|
|
634
|
+
// The package exists but its main/export target is broken.
|
|
635
|
+
throw resolveError
|
|
636
|
+
}
|
|
637
|
+
return resolveError
|
|
638
|
+
}
|
|
639
|
+
if (localArtifacts) {
|
|
640
|
+
let artifactError = null
|
|
641
|
+
for (let i = 0; i < localArtifacts.length; i++) {
|
|
642
|
+
try {
|
|
643
|
+
require.resolve(localArtifacts[i])
|
|
644
|
+
return null
|
|
645
|
+
} catch (resolveError) {
|
|
646
|
+
if (!resolveError || resolveError.code !== 'MODULE_NOT_FOUND') {
|
|
647
|
+
throw resolveError
|
|
648
|
+
}
|
|
649
|
+
artifactError = resolveError
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
return artifactError
|
|
522
653
|
}
|
|
654
|
+
return null
|
|
523
655
|
}
|
|
524
|
-
if (!
|
|
656
|
+
if (!wasiBindingLoaded && (!__napiWasiFlavorRequested || __napiWasiFlavor === 'wasm32-wasi')) {
|
|
657
|
+
let candidateError = null
|
|
658
|
+
let candidateFailed = false
|
|
525
659
|
try {
|
|
526
|
-
|
|
527
|
-
|
|
660
|
+
candidateError = __napiWasiResolveCandidate('./pdfdown_ocr.wasi.cjs', false, ['./pdfdown_ocr.wasm32-wasi.debug.wasm', './pdfdown_ocr.wasm32-wasi.wasm'])
|
|
661
|
+
candidateFailed = candidateError !== null
|
|
662
|
+
if (!candidateFailed) {
|
|
663
|
+
wasiBinding = require('./pdfdown_ocr.wasi.cjs')
|
|
664
|
+
nativeBinding = wasiBinding
|
|
665
|
+
__napiLoadedBindingTarget = 'wasm32-wasi'
|
|
666
|
+
wasiBindingLoaded = true
|
|
667
|
+
}
|
|
528
668
|
} catch (err) {
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
669
|
+
candidateError = err
|
|
670
|
+
candidateFailed = true
|
|
671
|
+
}
|
|
672
|
+
if (candidateFailed) {
|
|
673
|
+
wasiBindingErrors.push(candidateError)
|
|
674
|
+
loadErrors.push(candidateError)
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
if (!wasiBindingLoaded && (!__napiWasiFlavorRequested || __napiWasiFlavor === 'wasm32-wasi')) {
|
|
678
|
+
let candidateError = null
|
|
679
|
+
let candidateFailed = false
|
|
680
|
+
try {
|
|
681
|
+
candidateError = __napiWasiResolveCandidate('@d0paminedriven/pdfdown-ocr-wasm32-wasi', true, undefined)
|
|
682
|
+
candidateFailed = candidateError !== null
|
|
683
|
+
if (!candidateFailed) {
|
|
684
|
+
if (process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
|
685
|
+
const bindingPackageVersion = require('@d0paminedriven/pdfdown-ocr-wasm32-wasi/package.json').version
|
|
686
|
+
if (bindingPackageVersion !== '0.9.9') {
|
|
687
|
+
throw new Error(`WASI binding package version mismatch, expected 0.9.9 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
wasiBinding = require('@d0paminedriven/pdfdown-ocr-wasm32-wasi')
|
|
691
|
+
nativeBinding = wasiBinding
|
|
692
|
+
__napiLoadedBindingTarget = 'wasm32-wasi'
|
|
693
|
+
wasiBindingLoaded = true
|
|
532
694
|
}
|
|
695
|
+
} catch (err) {
|
|
696
|
+
candidateError = err
|
|
697
|
+
candidateFailed = true
|
|
698
|
+
}
|
|
699
|
+
if (candidateFailed) {
|
|
700
|
+
wasiBindingErrors.push(candidateError)
|
|
701
|
+
loadErrors.push(candidateError)
|
|
533
702
|
}
|
|
534
703
|
}
|
|
535
|
-
if (
|
|
536
|
-
|
|
537
|
-
|
|
704
|
+
if (
|
|
705
|
+
!wasiBindingLoaded &&
|
|
706
|
+
forceWasi &&
|
|
707
|
+
!forceWasiError &&
|
|
708
|
+
!__napiWasiFlavorRequested
|
|
709
|
+
) {
|
|
710
|
+
nativeBinding = requireNative()
|
|
711
|
+
__napiRequiredNativeBinding = nativeBinding
|
|
712
|
+
}
|
|
713
|
+
if ((forceWasiError || __napiWasiFlavorRequested) && !wasiBindingLoaded) {
|
|
714
|
+
const error = new Error(
|
|
715
|
+
__napiWasiFlavorRequested
|
|
716
|
+
? 'WASI binding for flavor "' + __napiWasiFlavor + '" not found'
|
|
717
|
+
: 'WASI binding not found and NAPI_RS_FORCE_WASI is set to error',
|
|
718
|
+
)
|
|
719
|
+
error.cause = createLoadErrorChain(wasiBindingErrors)
|
|
538
720
|
throw error
|
|
539
721
|
}
|
|
540
722
|
}
|
|
541
723
|
|
|
542
724
|
if (!nativeBinding) {
|
|
543
725
|
if (loadErrors.length > 0) {
|
|
544
|
-
|
|
726
|
+
const error = new Error(
|
|
545
727
|
`Cannot find native binding. ` +
|
|
546
728
|
`npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). ` +
|
|
547
729
|
'Please try `npm i` again after removing both package-lock.json and node_modules directory.',
|
|
548
|
-
{
|
|
549
|
-
cause: loadErrors.reduce((err, cur) => {
|
|
550
|
-
cur.cause = err
|
|
551
|
-
return cur
|
|
552
|
-
}),
|
|
553
|
-
},
|
|
554
730
|
)
|
|
731
|
+
// assign instead of the `new Error(message, { cause })` options form,
|
|
732
|
+
// which Node < 16.9 silently ignores
|
|
733
|
+
error.cause = createLoadErrorChain(loadErrors)
|
|
734
|
+
throw error
|
|
555
735
|
}
|
|
556
736
|
throw new Error(`Failed to load native binding`)
|
|
557
737
|
}
|
|
558
738
|
|
|
739
|
+
// A package can patch its own fallback in front of the load-failure check
|
|
740
|
+
// above, e.g. a WebContainer download through `napi-postinstall/fallback`.
|
|
741
|
+
// That fallback swaps `nativeBinding` for a generated WASI loader, which has
|
|
742
|
+
// already stamped its own flavor, while the target above still says 'native'.
|
|
743
|
+
// Take that flavor over, or the CommonJS stamp throws a conflict and the ESM
|
|
744
|
+
// export reports the wrong artifact. Three limits keep the conflict check:
|
|
745
|
+
// - only a binding this loader did not load itself, so a native addon whose
|
|
746
|
+
// `#[napi(module_exports)]` hook claims the name is still rejected;
|
|
747
|
+
// - only an own data property, read through its descriptor, so no addon
|
|
748
|
+
// getter runs here, and it must read back the same, as the stamp reads it;
|
|
749
|
+
// - only a flavor in `__napiWasiFlavors`, the ones this loader's own WASI
|
|
750
|
+
// chain can report ('wasm32-wasi' for a package without a WASI target).
|
|
751
|
+
// This is metadata and never fails a load: a throwing Proxy trap keeps 'native'.
|
|
752
|
+
if (
|
|
753
|
+
__napiLoadedBindingTarget === 'native' &&
|
|
754
|
+
nativeBinding !== __napiRequiredNativeBinding
|
|
755
|
+
) {
|
|
756
|
+
try {
|
|
757
|
+
const __napiBindingTargetDescriptor = Object.getOwnPropertyDescriptor(
|
|
758
|
+
nativeBinding,
|
|
759
|
+
'__napiBindingTarget',
|
|
760
|
+
)
|
|
761
|
+
if (
|
|
762
|
+
__napiBindingTargetDescriptor &&
|
|
763
|
+
__napiWasiFlavors.indexOf(__napiBindingTargetDescriptor.value) !== -1 &&
|
|
764
|
+
nativeBinding.__napiBindingTarget === __napiBindingTargetDescriptor.value
|
|
765
|
+
) {
|
|
766
|
+
__napiLoadedBindingTarget = __napiBindingTargetDescriptor.value
|
|
767
|
+
}
|
|
768
|
+
} catch {
|
|
769
|
+
// keep 'native'
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
function __napiStampBindingTarget(exportsObject, target) {
|
|
774
|
+
if (
|
|
775
|
+
Object.prototype.hasOwnProperty.call(exportsObject, '__napiBindingTarget')
|
|
776
|
+
) {
|
|
777
|
+
if (exportsObject.__napiBindingTarget === target) {
|
|
778
|
+
// Already ours: the root entry aliases the object it loaded, so a WASI
|
|
779
|
+
// fallback candidate — or a `NAPI_RS_NATIVE_LIBRARY_PATH` override that
|
|
780
|
+
// is a generated loader — arrives already stamped with this same value.
|
|
781
|
+
return target
|
|
782
|
+
}
|
|
783
|
+
const error = new Error(
|
|
784
|
+
'`__napiBindingTarget` is reserved by the generated binding loader, but the loaded binding already exports it. Rename the export, e.g. #[napi(js_name = "...")].',
|
|
785
|
+
)
|
|
786
|
+
error.code = 'ERR_NAPI_BINDING_TARGET_CONFLICT'
|
|
787
|
+
throw error
|
|
788
|
+
}
|
|
789
|
+
if (!Object.isExtensible(exportsObject)) {
|
|
790
|
+
// A `#[napi(module_exports)]` hook may seal or freeze this object
|
|
791
|
+
// (`Object::seal` / `Object::freeze`). Reporting the artifact is metadata,
|
|
792
|
+
// never a reason to fail an otherwise successful load, so the stamp is
|
|
793
|
+
// skipped. What a consumer still sees then follows the entry point: the
|
|
794
|
+
// browser and deferred loaders declare `__napiBindingTarget` at module
|
|
795
|
+
// level and go on reporting it, while the CommonJS entries hand back this
|
|
796
|
+
// very object as `module.exports`, so there the value is absent.
|
|
797
|
+
return target
|
|
798
|
+
}
|
|
799
|
+
try {
|
|
800
|
+
// [[Define]], not [[Set]]: an ordinary assignment walks the prototype
|
|
801
|
+
// chain, so an inherited accessor could swallow the value or throw and
|
|
802
|
+
// fail an otherwise successful load. The descriptor is what a successful
|
|
803
|
+
// assignment would have produced.
|
|
804
|
+
Object.defineProperty(exportsObject, '__napiBindingTarget', {
|
|
805
|
+
configurable: true,
|
|
806
|
+
enumerable: true,
|
|
807
|
+
value: target,
|
|
808
|
+
writable: true,
|
|
809
|
+
})
|
|
810
|
+
} catch {
|
|
811
|
+
// Same rule as the non-extensible skip above: reporting the artifact is
|
|
812
|
+
// metadata, never a reason to fail an otherwise successful load. An exotic
|
|
813
|
+
// object (a Proxy whose defineProperty trap refuses) is skipped, not
|
|
814
|
+
// thrown over.
|
|
815
|
+
}
|
|
816
|
+
// The CommonJS loaders assign this return value so `cjs-module-lexer` — and
|
|
817
|
+
// therefore Node's CJS -> ESM named export detection — can see
|
|
818
|
+
// `__napiBindingTarget` statically.
|
|
819
|
+
return target
|
|
820
|
+
}
|
|
821
|
+
// Stamp before the alias, not after. The guard only reads `nativeBinding`
|
|
822
|
+
// (`hasOwnProperty` plus a comparison), which is safe against any addon
|
|
823
|
+
// accessor; an assignment is not, because a `#[napi(module_exports)]` hook can
|
|
824
|
+
// expose a getter reporting this very value and a setter that throws. So the
|
|
825
|
+
// assignment lands on the loader's own `module.exports`, still the original
|
|
826
|
+
// object here, and the alias below replaces it.
|
|
827
|
+
//
|
|
828
|
+
// The assignment is what keeps the marker a statically visible CommonJS export:
|
|
829
|
+
// `cjs-module-lexer` is Node's CJS -> ESM named export detection, it cannot see
|
|
830
|
+
// a bare call, and the later `module.exports = nativeBinding` does not undo the
|
|
831
|
+
// detection. The assignment itself always succeeds — its target is this
|
|
832
|
+
// loader's own, still extensible `module.exports` — and the alias below then
|
|
833
|
+
// discards the value it wrote. What a consumer reads is whatever the guard put
|
|
834
|
+
// on `nativeBinding`, so on a frozen binding, where the guard skips, the
|
|
835
|
+
// linked import resolves to `undefined`.
|
|
836
|
+
module.exports.__napiBindingTarget = __napiStampBindingTarget(nativeBinding, __napiLoadedBindingTarget)
|
|
559
837
|
module.exports = nativeBinding
|
|
560
838
|
module.exports.PdfDown = nativeBinding.PdfDown
|
|
561
839
|
module.exports.BoxType = nativeBinding.BoxType
|
package/package.json
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d0paminedriven/pdfdown-ocr",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Rust powered PDF extraction for Node with OCR fallback (requires system tesseract).",
|
|
5
|
-
"
|
|
5
|
+
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "https://github.com/DopamineDriven/pdfdown"
|
|
9
9
|
},
|
|
10
|
-
"license": "MIT",
|
|
11
10
|
"files": [
|
|
12
11
|
"index.d.ts",
|
|
13
12
|
"index.js",
|
|
@@ -17,26 +16,25 @@
|
|
|
17
16
|
"darwin",
|
|
18
17
|
"linux"
|
|
19
18
|
],
|
|
19
|
+
"main": "index.js",
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public",
|
|
22
|
+
"registry": "https://registry.npmjs.org/"
|
|
23
|
+
},
|
|
20
24
|
"napi": {
|
|
21
25
|
"binaryName": "pdfdown_ocr",
|
|
22
26
|
"targets": [
|
|
23
|
-
"x86_64-apple-darwin",
|
|
24
27
|
"aarch64-apple-darwin",
|
|
25
28
|
"x86_64-unknown-linux-gnu",
|
|
26
29
|
"aarch64-unknown-linux-gnu"
|
|
27
30
|
]
|
|
28
31
|
},
|
|
29
32
|
"engines": {
|
|
30
|
-
"node": ">=
|
|
31
|
-
},
|
|
32
|
-
"publishConfig": {
|
|
33
|
-
"registry": "https://registry.npmjs.org/",
|
|
34
|
-
"access": "public"
|
|
33
|
+
"node": ">= 22.14.0 < 23 || >= 23.6.0"
|
|
35
34
|
},
|
|
36
35
|
"optionalDependencies": {
|
|
37
|
-
"@d0paminedriven/pdfdown-ocr-darwin-
|
|
38
|
-
"@d0paminedriven/pdfdown-ocr-
|
|
39
|
-
"@d0paminedriven/pdfdown-ocr-linux-
|
|
40
|
-
"@d0paminedriven/pdfdown-ocr-linux-arm64-gnu": "0.9.9"
|
|
36
|
+
"@d0paminedriven/pdfdown-ocr-darwin-arm64": "0.10.0",
|
|
37
|
+
"@d0paminedriven/pdfdown-ocr-linux-x64-gnu": "0.10.0",
|
|
38
|
+
"@d0paminedriven/pdfdown-ocr-linux-arm64-gnu": "0.10.0"
|
|
41
39
|
}
|
|
42
|
-
}
|
|
40
|
+
}
|