pdf-oxide 0.3.24

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 (62) hide show
  1. package/README.md +218 -0
  2. package/binding.gyp +35 -0
  3. package/package.json +78 -0
  4. package/src/builders/annotation-builder.ts +367 -0
  5. package/src/builders/conversion-options-builder.ts +257 -0
  6. package/src/builders/index.ts +12 -0
  7. package/src/builders/metadata-builder.ts +317 -0
  8. package/src/builders/pdf-builder.ts +386 -0
  9. package/src/builders/search-options-builder.ts +151 -0
  10. package/src/document-editor-manager.ts +318 -0
  11. package/src/errors.ts +1629 -0
  12. package/src/form-field-manager.ts +666 -0
  13. package/src/hybrid-ml-manager.ts +283 -0
  14. package/src/index.ts +453 -0
  15. package/src/managers/accessibility-manager.ts +338 -0
  16. package/src/managers/annotation-manager.ts +439 -0
  17. package/src/managers/barcode-manager.ts +235 -0
  18. package/src/managers/batch-manager.ts +533 -0
  19. package/src/managers/cache-manager.ts +486 -0
  20. package/src/managers/compliance-manager.ts +375 -0
  21. package/src/managers/content-manager.ts +339 -0
  22. package/src/managers/document-utility-manager.ts +922 -0
  23. package/src/managers/dom-pdf-creator.ts +365 -0
  24. package/src/managers/editing-manager.ts +514 -0
  25. package/src/managers/enterprise-manager.ts +478 -0
  26. package/src/managers/extended-managers.ts +437 -0
  27. package/src/managers/extraction-manager.ts +583 -0
  28. package/src/managers/final-utilities.ts +429 -0
  29. package/src/managers/hybrid-ml-advanced.ts +479 -0
  30. package/src/managers/index.ts +239 -0
  31. package/src/managers/layer-manager.ts +500 -0
  32. package/src/managers/metadata-manager.ts +303 -0
  33. package/src/managers/ocr-manager.ts +756 -0
  34. package/src/managers/optimization-manager.ts +262 -0
  35. package/src/managers/outline-manager.ts +196 -0
  36. package/src/managers/page-manager.ts +289 -0
  37. package/src/managers/pattern-detection.ts +440 -0
  38. package/src/managers/rendering-manager.ts +863 -0
  39. package/src/managers/search-manager.ts +385 -0
  40. package/src/managers/security-manager.ts +345 -0
  41. package/src/managers/signature-manager.ts +1664 -0
  42. package/src/managers/streams.ts +618 -0
  43. package/src/managers/xfa-manager.ts +500 -0
  44. package/src/pdf-creator-manager.ts +494 -0
  45. package/src/properties.ts +522 -0
  46. package/src/result-accessors-manager.ts +867 -0
  47. package/src/tests/advanced-features.test.ts +414 -0
  48. package/src/tests/advanced.test.ts +266 -0
  49. package/src/tests/extended-managers.test.ts +316 -0
  50. package/src/tests/final-utilities.test.ts +455 -0
  51. package/src/tests/foundation.test.ts +315 -0
  52. package/src/tests/high-demand.test.ts +257 -0
  53. package/src/tests/specialized.test.ts +97 -0
  54. package/src/thumbnail-manager.ts +272 -0
  55. package/src/types/common.ts +142 -0
  56. package/src/types/document-types.ts +457 -0
  57. package/src/types/index.ts +6 -0
  58. package/src/types/manager-types.ts +284 -0
  59. package/src/types/native-bindings.ts +517 -0
  60. package/src/workers/index.ts +7 -0
  61. package/src/workers/pool.ts +274 -0
  62. package/src/workers/worker.ts +131 -0
package/src/index.ts ADDED
@@ -0,0 +1,453 @@
1
+ // PDF Oxide Node.js bindings - Native module loader
2
+
3
+ import { platform, arch } from 'node:process';
4
+ import { createRequire } from 'node:module';
5
+ import { fileURLToPath } from 'node:url';
6
+ import { dirname } from 'node:path';
7
+ import {
8
+ PdfException,
9
+ ParseException,
10
+ IoException,
11
+ EncryptionException,
12
+ UnsupportedFeatureException,
13
+ InvalidStateException,
14
+ ValidationException,
15
+ RenderingException,
16
+ SearchException,
17
+ ComplianceException,
18
+ OcrException,
19
+ SignatureException,
20
+ CertificateLoadFailed,
21
+ SigningFailed,
22
+ RedactionException,
23
+ AccessibilityException,
24
+ OptimizationException,
25
+ UnknownError,
26
+ ErrorCategory,
27
+ ErrorSeverity,
28
+ wrapError,
29
+ wrapMethod,
30
+ wrapAsyncMethod,
31
+ mapFfiErrorCode,
32
+ } from './errors';
33
+ import {
34
+ addPdfDocumentProperties,
35
+ addPdfProperties,
36
+ addPdfPageProperties,
37
+ } from './properties';
38
+ import {
39
+ PdfBuilder,
40
+ ConversionOptionsBuilder,
41
+ MetadataBuilder,
42
+ AnnotationBuilder,
43
+ SearchOptionsBuilder,
44
+ } from './builders/index';
45
+ import {
46
+ OutlineManager,
47
+ MetadataManager,
48
+ ExtractionManager,
49
+ SearchManager,
50
+ SecurityManager,
51
+ AnnotationManager,
52
+ LayerManager,
53
+ RenderingManager,
54
+ SearchStream,
55
+ ExtractionStream,
56
+ MetadataStream,
57
+ createSearchStream,
58
+ createExtractionStream,
59
+ createMetadataStream,
60
+ BatchManager,
61
+ type BatchDocument,
62
+ type BatchProgress,
63
+ type BatchResult,
64
+ type BatchOptions,
65
+ type BatchStatistics,
66
+ } from './managers/index';
67
+ import { WorkerPool, workerPool } from './workers/index';
68
+ import type { WorkerTask, WorkerResult } from './workers/index';
69
+
70
+ // Create require function for CommonJS modules
71
+ const __filename = fileURLToPath(import.meta.url);
72
+ const __dirname = dirname(__filename);
73
+ const require = createRequire(import.meta.url);
74
+
75
+ // Phase 4+ managers (compiled JavaScript - use require for dynamic import)
76
+ // Phase 9: Now imports from canonical consolidated managers in managers/
77
+ const {
78
+ OcrManager,
79
+ OcrManager: OCRManager,
80
+ OcrDetectionMode: OCRDetectionMode,
81
+ ComplianceManager,
82
+ PdfALevel,
83
+ PdfXLevel,
84
+ PdfUALevel,
85
+ ComplianceIssueType,
86
+ IssueSeverity,
87
+ SignatureManager,
88
+ SignatureAlgorithm,
89
+ DigestAlgorithm,
90
+ BarcodeManager,
91
+ BarcodeFormat,
92
+ BarcodeErrorCorrection,
93
+ FormFieldManager,
94
+ FormFieldType,
95
+ FieldVisibility,
96
+ ResultAccessorsManager,
97
+ SearchResultProperties,
98
+ FontProperties,
99
+ ImageProperties,
100
+ AnnotationProperties,
101
+ ThumbnailManager,
102
+ ThumbnailSize,
103
+ ImageFormat,
104
+ HybridMLManager,
105
+ PageComplexity,
106
+ ContentType,
107
+ XfaManager,
108
+ XfaFormType,
109
+ XfaFieldType,
110
+ CacheManager,
111
+ EditingManager,
112
+ AccessibilityManager,
113
+ OptimizationManager,
114
+ EnterpriseManager,
115
+ } = require('../lib/managers/index.js') as any;
116
+ // OcrLanguage re-exported from canonical OcrManager
117
+ const { OcrLanguage: OCRLanguage } = require('../lib/managers/ocr-manager.js') as any;
118
+
119
+ /**
120
+ * Platform-specific native module packages
121
+ */
122
+ const PLATFORMS: Record<string, Record<string, string>> = {
123
+ 'darwin': {
124
+ 'x64': 'pdf_oxide-darwin-x64',
125
+ 'arm64': 'pdf_oxide-darwin-arm64',
126
+ },
127
+ 'linux': {
128
+ 'x64': 'pdf_oxide-linux-x64-gnu',
129
+ 'arm64': 'pdf_oxide-linux-arm64-gnu',
130
+ },
131
+ 'win32': {
132
+ 'x64': 'pdf_oxide-win32-x64-msvc',
133
+ 'arm64': 'pdf_oxide-win32-arm64-msvc',
134
+ },
135
+ };
136
+
137
+ /**
138
+ * Gets the native package name for the current platform and architecture
139
+ * @returns Native package name
140
+ * @throws Error if platform or architecture is not supported
141
+ */
142
+ function getNativePackageName(): string {
143
+ const osPackages = PLATFORMS[platform];
144
+ if (!osPackages) {
145
+ throw new Error(`Unsupported platform: ${platform}. Supported platforms: ${Object.keys(PLATFORMS).join(', ')}`);
146
+ }
147
+
148
+ const pkg = osPackages[arch];
149
+ if (!pkg) {
150
+ throw new Error(`Unsupported architecture: ${arch} for ${platform}. Supported architectures: ${Object.keys(osPackages).join(', ')}`);
151
+ }
152
+
153
+ return pkg;
154
+ }
155
+
156
+ let nativeModule: any;
157
+
158
+ /**
159
+ * Loads the native module dynamically based on platform and architecture
160
+ * @returns Native module
161
+ * @throws Error if native module cannot be loaded
162
+ */
163
+ function loadNativeModule(): any {
164
+ if (nativeModule) {
165
+ return nativeModule;
166
+ }
167
+
168
+ try {
169
+ // Try loading from platform-specific package first (CommonJS)
170
+ const packageName = getNativePackageName();
171
+ try {
172
+ // Use require to load the native module
173
+ nativeModule = require(packageName);
174
+ } catch (e) {
175
+ // Fallback to local binary if in development
176
+ if (process.env.NODE_ENV === 'development' || process.env.NAPI_DEV) {
177
+ try {
178
+ nativeModule = require('./pdf-oxide');
179
+ } catch {
180
+ throw e;
181
+ }
182
+ } else {
183
+ throw e;
184
+ }
185
+ }
186
+ return nativeModule;
187
+ } catch (error) {
188
+ throw new Error(`Failed to load native module: ${(error as Error).message}`);
189
+ }
190
+ }
191
+
192
+ // Load native module
193
+ const native = loadNativeModule();
194
+
195
+ /**
196
+ * Wraps native class methods to convert errors to proper JavaScript Error subclasses.
197
+ * This ensures that errors thrown from native code are instanceof the appropriate Error class.
198
+ * @param nativeClass - The native class to wrap
199
+ * @param asyncMethods - Names of async methods to wrap specially
200
+ * @returns Wrapped class with error-handling methods
201
+ */
202
+ function wrapNativeClass(nativeClass: any, asyncMethods: string[] = []): any {
203
+ if (!nativeClass) return nativeClass;
204
+
205
+ // For static methods like PdfDocument.open()
206
+ for (const key of Object.getOwnPropertyNames(nativeClass)) {
207
+ if (key !== 'prototype' && key !== 'length' && key !== 'name' && typeof nativeClass[key] === 'function') {
208
+ const isAsync = asyncMethods.includes(key);
209
+ if (isAsync) {
210
+ nativeClass[key] = wrapAsyncMethod(nativeClass[key], nativeClass);
211
+ } else {
212
+ nativeClass[key] = wrapMethod(nativeClass[key], nativeClass);
213
+ }
214
+ }
215
+ }
216
+
217
+ // For instance methods, wrap the prototype
218
+ if (nativeClass.prototype) {
219
+ for (const key of Object.getOwnPropertyNames(nativeClass.prototype)) {
220
+ if (key !== 'constructor' && typeof nativeClass.prototype[key] === 'function') {
221
+ const isAsync = asyncMethods.includes(key);
222
+ const descriptor = Object.getOwnPropertyDescriptor(nativeClass.prototype, key);
223
+ if (descriptor && descriptor.writable) {
224
+ if (isAsync) {
225
+ nativeClass.prototype[key] = wrapAsyncMethod(nativeClass.prototype[key]);
226
+ } else {
227
+ nativeClass.prototype[key] = wrapMethod(nativeClass.prototype[key]);
228
+ }
229
+ }
230
+ }
231
+ }
232
+ }
233
+
234
+ return nativeClass;
235
+ }
236
+
237
+ // List of async methods for each class (Phase 2.4 implementation)
238
+ const asyncMethodsByClass: Record<string, string[]> = {
239
+ PdfDocument: [
240
+ 'extract_text_async',
241
+ 'to_markdown_async',
242
+ ],
243
+ Pdf: [
244
+ 'save_async',
245
+ ],
246
+ PdfBuilder: [],
247
+ PdfPage: [],
248
+ PdfElement: [],
249
+ PdfText: [],
250
+ PdfImage: [],
251
+ PdfPath: [],
252
+ PdfTable: [],
253
+ PdfStructure: [],
254
+ Annotation: [],
255
+ TextAnnotation: [],
256
+ HighlightAnnotation: [],
257
+ LinkAnnotation: [],
258
+ TextSearcher: [],
259
+ };
260
+
261
+ // Wrap native classes with error handling and property getters
262
+ const wrappedClasses: Record<string, any> = {};
263
+ for (const className of Object.keys(asyncMethodsByClass)) {
264
+ if (native[className]) {
265
+ wrappedClasses[className] = wrapNativeClass(native[className], asyncMethodsByClass[className]);
266
+ }
267
+ }
268
+
269
+ // Add property getters to enhance idiomatic JavaScript API
270
+ if (wrappedClasses.PdfDocument) {
271
+ addPdfDocumentProperties(wrappedClasses.PdfDocument);
272
+ }
273
+ if (wrappedClasses.Pdf) {
274
+ addPdfProperties(wrappedClasses.Pdf);
275
+ }
276
+ if (wrappedClasses.PdfPage) {
277
+ addPdfPageProperties(wrappedClasses.PdfPage);
278
+ }
279
+
280
+ // Export as ES module
281
+ const getVersion = native.getVersion;
282
+ const getPdfOxideVersion = native.getPdfOxideVersion;
283
+ const PdfDocument = wrappedClasses.PdfDocument || native.PdfDocument;
284
+ const Pdf = wrappedClasses.Pdf || native.Pdf;
285
+ // PdfBuilder is imported from ./builders/index - don't redeclare
286
+ const PdfPage = wrappedClasses.PdfPage || native.PdfPage;
287
+ const PdfElement = wrappedClasses.PdfElement || native.PdfElement;
288
+ const PdfText = wrappedClasses.PdfText || native.PdfText;
289
+ const PdfImage = wrappedClasses.PdfImage || native.PdfImage;
290
+ const PdfPath = wrappedClasses.PdfPath || native.PdfPath;
291
+ const PdfTable = wrappedClasses.PdfTable || native.PdfTable;
292
+ const PdfStructure = wrappedClasses.PdfStructure || native.PdfStructure;
293
+ const Annotation = wrappedClasses.Annotation || native.Annotation;
294
+ const TextAnnotation = wrappedClasses.TextAnnotation || native.TextAnnotation;
295
+ const HighlightAnnotation = wrappedClasses.HighlightAnnotation || native.HighlightAnnotation;
296
+ const LinkAnnotation = wrappedClasses.LinkAnnotation || native.LinkAnnotation;
297
+ const PdfError = PdfException;
298
+ const PageSize = native.PageSize;
299
+ const Rect = native.Rect;
300
+ const Point = native.Point;
301
+ const Color = native.Color;
302
+ const ConversionOptions = native.ConversionOptions;
303
+ const SearchOptions = native.SearchOptions;
304
+ const SearchResult = native.SearchResult;
305
+ const TextSearcher = wrappedClasses.TextSearcher || native.TextSearcher;
306
+
307
+ export {
308
+ // Version info
309
+ getVersion,
310
+ getPdfOxideVersion,
311
+
312
+ // Main classes
313
+ PdfDocument,
314
+ Pdf,
315
+ PdfPage,
316
+
317
+ // Element types
318
+ PdfElement,
319
+ PdfText,
320
+ PdfImage,
321
+ PdfPath,
322
+ PdfTable,
323
+ PdfStructure,
324
+
325
+ // Annotation types
326
+ Annotation,
327
+ TextAnnotation,
328
+ HighlightAnnotation,
329
+ LinkAnnotation,
330
+
331
+ // Error types
332
+ PdfError,
333
+ PdfException,
334
+ ParseException,
335
+ IoException,
336
+ EncryptionException,
337
+ UnsupportedFeatureException,
338
+ InvalidStateException,
339
+ ValidationException,
340
+ RenderingException,
341
+ SearchException,
342
+ ComplianceException,
343
+ OcrException,
344
+ SignatureException,
345
+ CertificateLoadFailed,
346
+ SigningFailed,
347
+ RedactionException,
348
+ AccessibilityException,
349
+ OptimizationException,
350
+ UnknownError,
351
+
352
+ // Types
353
+ PageSize,
354
+ Rect,
355
+ Point,
356
+ Color,
357
+ ConversionOptions,
358
+ SearchOptions,
359
+ SearchResult,
360
+
361
+ // Utilities
362
+ TextSearcher,
363
+
364
+ // Error utilities
365
+ ErrorCategory,
366
+ ErrorSeverity,
367
+ wrapError,
368
+ wrapMethod,
369
+ wrapAsyncMethod,
370
+ mapFfiErrorCode,
371
+
372
+ // Builders
373
+ PdfBuilder,
374
+ ConversionOptionsBuilder,
375
+ MetadataBuilder,
376
+ AnnotationBuilder,
377
+ SearchOptionsBuilder,
378
+
379
+ // Managers (Phase 1-3: Core)
380
+ OutlineManager,
381
+ MetadataManager,
382
+ ExtractionManager,
383
+ SearchManager,
384
+ SecurityManager,
385
+ AnnotationManager,
386
+ LayerManager,
387
+ RenderingManager,
388
+
389
+ // Managers (Phase 4+, consolidated in Phase 9)
390
+ OcrManager,
391
+ OCRManager,
392
+ OCRLanguage,
393
+ OCRDetectionMode,
394
+ ComplianceManager,
395
+ PdfALevel,
396
+ PdfXLevel,
397
+ PdfUALevel,
398
+ ComplianceIssueType,
399
+ IssueSeverity,
400
+ SignatureManager,
401
+ SignatureAlgorithm,
402
+ DigestAlgorithm,
403
+ BarcodeManager,
404
+ BarcodeFormat,
405
+ BarcodeErrorCorrection,
406
+ FormFieldManager,
407
+ FormFieldType,
408
+ FieldVisibility,
409
+ ResultAccessorsManager,
410
+ SearchResultProperties,
411
+ FontProperties,
412
+ ImageProperties,
413
+ AnnotationProperties,
414
+ ThumbnailManager,
415
+ ThumbnailSize,
416
+ ImageFormat,
417
+ HybridMLManager,
418
+ PageComplexity,
419
+ ContentType,
420
+ XfaManager,
421
+ XfaFormType,
422
+ XfaFieldType,
423
+ CacheManager,
424
+ EditingManager,
425
+ AccessibilityManager,
426
+ OptimizationManager,
427
+ EnterpriseManager,
428
+
429
+ // Phase 2.4: Stream API
430
+ SearchStream,
431
+ ExtractionStream,
432
+ MetadataStream,
433
+ createSearchStream,
434
+ createExtractionStream,
435
+ createMetadataStream,
436
+
437
+ // Phase 2.5: Batch Processing API
438
+ BatchManager,
439
+
440
+ // Worker Threads API
441
+ WorkerPool,
442
+ workerPool,
443
+ };
444
+
445
+ export type {
446
+ WorkerTask,
447
+ WorkerResult,
448
+ BatchDocument,
449
+ BatchProgress,
450
+ BatchResult,
451
+ BatchOptions,
452
+ BatchStatistics,
453
+ };