@arcships/light-ocr 0.4.0 → 0.5.1

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/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  },
8
8
  "dependencies": {
9
9
  "@arcships/light-ocr-model-ppocrv6-small": "0.3.4",
10
- "@arcships/light-ocr-runtime": "0.1.0"
10
+ "@arcships/light-ocr-runtime": "0.1.1"
11
11
  },
12
12
  "description": "Offline PP-OCRv6 Small OCR for Node.js — the stable default tier",
13
13
  "engines": {
@@ -31,6 +31,9 @@
31
31
  "main": "./src/index.cjs",
32
32
  "module": "./src/index.mjs",
33
33
  "name": "@arcships/light-ocr",
34
+ "optionalDependencies": {
35
+ "pdfium-native": "0.6.1"
36
+ },
34
37
  "publishConfig": {
35
38
  "access": "public",
36
39
  "provenance": true
@@ -41,5 +44,5 @@
41
44
  },
42
45
  "type": "commonjs",
43
46
  "types": "./src/index.d.ts",
44
- "version": "0.4.0"
47
+ "version": "0.5.1"
45
48
  }
package/src/cli.cjs CHANGED
@@ -1,9 +1,30 @@
1
1
  #!/usr/bin/env node
2
2
  'use strict';
3
3
 
4
+ const path = require('node:path');
5
+
4
6
  const facade = require('./index.cjs');
5
- const { createCli } = require('@arcships/light-ocr-runtime/cli');
6
- const { coreVersion } = require('@arcships/light-ocr-runtime/metadata');
7
+
8
+ // Try to use workspace dependencies, fallback to local paths
9
+ let createCli, coreVersion, loadNative;
10
+ try {
11
+ ({ createCli } = require('@arcships/light-ocr-runtime/cli'));
12
+ ({ coreVersion } = require('@arcships/light-ocr-runtime/metadata'));
13
+ } catch {
14
+ // Fallback to local runtime
15
+ ({ createCli } = require(path.join(__dirname, '..', '..', 'runtime', 'src', 'cli.cjs')));
16
+ ({ coreVersion } = require(path.join(__dirname, '..', '..', 'runtime', 'src', 'metadata.cjs')));
17
+ }
18
+ try {
19
+ ({ loadNative } = require('@arcships/light-ocr-runtime'));
20
+ } catch {
21
+ try {
22
+ ({ loadNative } = require(path.join(__dirname, '..', '..', 'runtime', 'src', 'load-native.cjs')));
23
+ } catch {
24
+ // loadNative unavailable — doctor will report native as unavailable
25
+ }
26
+ }
27
+
7
28
  const packageMetadata = require('../package.json');
8
29
 
9
30
  const cli = createCli({
@@ -11,6 +32,7 @@ const cli = createCli({
11
32
  commandName: 'light-ocr',
12
33
  packageVersion: packageMetadata.version,
13
34
  coreVersion,
35
+ loadNative,
14
36
  });
15
37
 
16
38
  if (require.main === module) {
package/src/index.cjs CHANGED
@@ -1,8 +1,20 @@
1
1
  'use strict';
2
2
 
3
- const { createModelFacade } = require('@arcships/light-ocr-runtime/facade');
3
+ const path = require('node:path');
4
4
 
5
- module.exports = createModelFacade({
5
+ // Try to use workspace dependencies, fallback to local paths
6
+ let createModelFacade;
7
+ try {
8
+ ({ createModelFacade } = require('@arcships/light-ocr-runtime/facade'));
9
+ } catch {
10
+ // Fallback to local runtime
11
+ const facadePath = path.join(__dirname, '..', '..', 'runtime', 'src', 'facade.cjs');
12
+ ({ createModelFacade } = require(facadePath));
13
+ }
14
+
15
+ const fs = require('node:fs');
16
+
17
+ const facade = createModelFacade({
6
18
  model: 'ppocrv6-small',
7
19
  modelPackage: '@arcships/light-ocr-model-ppocrv6-small',
8
20
  compatibleBundleIds: [
@@ -25,3 +37,250 @@ module.exports = createModelFacade({
25
37
  maturity: 'stable',
26
38
  },
27
39
  });
40
+
41
+ // Lazy load pdfium-native
42
+ let pdfium = null;
43
+ let pdfiumLoaded = false;
44
+
45
+ function loadPdfium() {
46
+ if (pdfiumLoaded) return pdfium;
47
+ pdfiumLoaded = true;
48
+ try {
49
+ pdfium = require('pdfium-native');
50
+ } catch {
51
+ pdfium = null;
52
+ }
53
+ return pdfium;
54
+ }
55
+
56
+ function hasPdfSupport() {
57
+ return loadPdfium() !== null;
58
+ }
59
+
60
+ async function* processPdf(engine, pdfBuffer, options = {}) {
61
+ const pdfiumNative = loadPdfium();
62
+ if (!pdfiumNative) {
63
+ throw new facade.OcrError('unsupported_capability', 'PDF support not available. Install pdfium-native.');
64
+ }
65
+
66
+ const {
67
+ pageRange,
68
+ dpi = 150,
69
+ maxPages = 100,
70
+ maxPagePixels = 4096 * 4096,
71
+ maxTotalPixels = 100 * 1024 * 1024,
72
+ maxFileBytes = 100 * 1024 * 1024,
73
+ signal,
74
+ ocrOptions = {}
75
+ } = options;
76
+
77
+ // Check file size
78
+ if (pdfBuffer.byteLength > maxFileBytes) {
79
+ throw new facade.OcrError('resource_limit_exceeded',
80
+ `File size ${pdfBuffer.byteLength} exceeds maxFileBytes ${maxFileBytes}`);
81
+ }
82
+
83
+ let totalPixels = 0;
84
+
85
+ // Open PDF document
86
+ const doc = await pdfiumNative.loadDocument(pdfBuffer);
87
+
88
+ try {
89
+ const pageCount = doc.pageCount;
90
+
91
+ // Apply page range
92
+ const start = pageRange?.start ? Math.max(1, pageRange.start) : 1;
93
+ const end = pageRange?.end ? Math.min(pageCount, pageRange.end) : pageCount;
94
+
95
+ // Check page limits
96
+ if (end - start + 1 > maxPages) {
97
+ throw new facade.OcrError('resource_limit_exceeded',
98
+ `Page count ${end - start + 1} exceeds maxPages ${maxPages}`);
99
+ }
100
+
101
+ for (let i = start; i <= end; i++) {
102
+ // Check abort signal
103
+ if (signal?.aborted) {
104
+ throw new facade.OcrError('internal_error', 'Operation aborted');
105
+ }
106
+
107
+ const page = await doc.getPage(i - 1); // 0-indexed
108
+
109
+ // Get page dimensions
110
+ const { width, height } = page;
111
+ const pagePixels = width * height;
112
+
113
+ // Check pixel limits
114
+ if (pagePixels > maxPagePixels) {
115
+ throw new facade.OcrError('resource_limit_exceeded',
116
+ `Page ${i} pixels ${pagePixels} exceeds maxPagePixels ${maxPagePixels}`);
117
+ }
118
+
119
+ totalPixels += pagePixels;
120
+ if (totalPixels > maxTotalPixels) {
121
+ throw new facade.OcrError('resource_limit_exceeded',
122
+ `Total pixels ${totalPixels} exceeds maxTotalPixels ${maxTotalPixels}`);
123
+ }
124
+
125
+ // Render page to PNG
126
+ const renderStart = Date.now();
127
+ const scale = dpi / 72; // PDF default is 72 DPI
128
+ const pngBuffer = await page.render({ scale });
129
+ const renderTime = (Date.now() - renderStart) * 1000;
130
+
131
+ // OCR the rendered image
132
+ const ocrStart = Date.now();
133
+ const ocrResult = await engine.recognizeEncoded(pngBuffer, ocrOptions);
134
+ const ocrTime = (Date.now() - ocrStart) * 1000;
135
+
136
+ // Close page to free native memory
137
+ await page.close();
138
+
139
+ // Build page result
140
+ const pageResult = {
141
+ index: i - 1,
142
+ width: ocrResult.imageWidth,
143
+ height: ocrResult.imageHeight,
144
+ coordinateSpace: 'pageSpace',
145
+ structure: 'ocr-order',
146
+ lines: ocrResult.lines.map((line, idx) => ({
147
+ id: `L${idx}`,
148
+ text: line.text,
149
+ confidence: line.confidence,
150
+ box: line.box
151
+ })),
152
+ source: {
153
+ kind: 'pdf',
154
+ mediaType: 'application/pdf',
155
+ identity: { pageIndex: i - 1 },
156
+ appliedTransforms: {
157
+ pdf: {
158
+ rotation: 0,
159
+ mediaBox: { x: 0, y: 0, width, height },
160
+ cropBox: { x: 0, y: 0, width, height },
161
+ dpi,
162
+ scale: dpi / 72
163
+ }
164
+ }
165
+ },
166
+ timingUs: {
167
+ total: renderTime + ocrTime,
168
+ decode: renderTime,
169
+ ocr: ocrTime
170
+ },
171
+ modelBundleId: ocrResult.modelBundleId
172
+ };
173
+
174
+ yield pageResult;
175
+ }
176
+ } finally {
177
+ doc.destroy();
178
+ }
179
+ }
180
+
181
+ async function* processImages(engine, imageBuffers, options = {}) {
182
+ const { signal, ocrOptions = {} } = options;
183
+
184
+ for (let i = 0; i < imageBuffers.length; i++) {
185
+ if (signal?.aborted) {
186
+ throw new facade.OcrError('internal_error', 'Operation aborted');
187
+ }
188
+
189
+ const buffer = imageBuffers[i];
190
+
191
+ const ocrStart = Date.now();
192
+ const ocrResult = await engine.recognizeEncoded(buffer, {
193
+ ...ocrOptions,
194
+ applyExif: true
195
+ });
196
+ const ocrTime = (Date.now() - ocrStart) * 1000;
197
+
198
+ const pageResult = {
199
+ index: i,
200
+ width: ocrResult.imageWidth,
201
+ height: ocrResult.imageHeight,
202
+ coordinateSpace: 'pageSpace',
203
+ structure: 'ocr-order',
204
+ lines: ocrResult.lines.map((line, idx) => ({
205
+ id: `L${idx}`,
206
+ text: line.text,
207
+ confidence: line.confidence,
208
+ box: line.box
209
+ })),
210
+ source: {
211
+ kind: 'image',
212
+ mediaType: 'image/png',
213
+ identity: { index: i },
214
+ appliedTransforms: {
215
+ exif: { orientation: 1, applied: false }
216
+ }
217
+ },
218
+ timingUs: {
219
+ total: ocrTime,
220
+ decode: 0,
221
+ ocr: ocrTime
222
+ },
223
+ modelBundleId: ocrResult.modelBundleId
224
+ };
225
+
226
+ yield pageResult;
227
+ }
228
+ }
229
+
230
+ async function* recognizeDocument(source, options = {}) {
231
+ // Create engine if not provided
232
+ let engine = options.engine;
233
+ let engineCreated = false;
234
+
235
+ if (!engine) {
236
+ engine = await facade.createEngine();
237
+ engineCreated = true;
238
+ }
239
+
240
+ try {
241
+ let buffers;
242
+ let isPdf = false;
243
+
244
+ if (Array.isArray(source)) {
245
+ // Multiple images
246
+ buffers = [];
247
+ for (const s of source) {
248
+ if (typeof s === 'string') {
249
+ buffers.push(fs.readFileSync(s));
250
+ } else {
251
+ buffers.push(s);
252
+ }
253
+ }
254
+ } else if (typeof source === 'string') {
255
+ // File path
256
+ const ext = path.extname(source).toLowerCase();
257
+ if (ext === '.pdf') {
258
+ isPdf = true;
259
+ buffers = [fs.readFileSync(source)];
260
+ } else {
261
+ buffers = [fs.readFileSync(source)];
262
+ }
263
+ } else {
264
+ // Buffer
265
+ isPdf = source[0] === 0x25 && source[1] === 0x50 && source[2] === 0x44 && source[3] === 0x46;
266
+ buffers = [source];
267
+ }
268
+
269
+ if (isPdf) {
270
+ yield* processPdf(engine, buffers[0], options);
271
+ } else {
272
+ yield* processImages(engine, buffers, options);
273
+ }
274
+ } finally {
275
+ if (engineCreated) {
276
+ await engine.close();
277
+ }
278
+ }
279
+ }
280
+
281
+ // Export facade + document capabilities
282
+ module.exports = {
283
+ ...facade,
284
+ hasPdfSupport,
285
+ recognizeDocument
286
+ };