veryfi-lens-wasm 3.5.12 → 3.5.13

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/core/constants.js CHANGED
@@ -11,6 +11,8 @@ export const WASM = {
11
11
  LOCAL_BASE_URL: '/wasm/',
12
12
  };
13
13
 
14
+ export const TESSERACT_URL = 'https://cdn.jsdelivr.net/npm/tesseract.js@7.0.0/dist/tesseract.esm.min.js';
15
+
14
16
  const buildApiUrls = (base) => ({
15
17
  VALIDATE_PARTNER: `${base}/rest/validate_partner`,
16
18
  SEND_CROPPED: `${base}/rest/send-cropped`,
@@ -40,7 +42,7 @@ export const setApiBaseUrl = (base) => {
40
42
  };
41
43
 
42
44
  export const DEFAULTS = {
43
- SDK_VERSION: '3.5.12',
45
+ SDK_VERSION: '3.5.13',
44
46
  BLUR_THRESHOLD: 0.7,
45
47
  LCD_THRESHOLD: 0.5,
46
48
  BOX_COLOR: 'rgba(84, 192, 139, 0.6)',
@@ -12,6 +12,20 @@ const base64Bytes = (base64) => Math.floor((base64.length * 3) / 4);
12
12
  const encode = (canvas, quality) =>
13
13
  canvas.toDataURL('image/jpeg', quality).split(',')[1];
14
14
 
15
+ // JPEG has no alpha channel. Composite over white without changing the source
16
+ // canvas used by capture, review, or subsequent encodes.
17
+ const flattenForJpeg = (source) => {
18
+ if (source.width === 0 || source.height === 0) return source;
19
+ const opaque = document.createElement('canvas');
20
+ opaque.width = source.width;
21
+ opaque.height = source.height;
22
+ const ctx = opaque.getContext('2d');
23
+ ctx.fillStyle = '#fff';
24
+ ctx.fillRect(0, 0, opaque.width, opaque.height);
25
+ ctx.drawImage(source, 0, 0);
26
+ return opaque;
27
+ };
28
+
15
29
  const scaleCanvas = (source, scale) => {
16
30
  const width = Math.max(1, Math.round(source.width * scale));
17
31
  const height = Math.max(1, Math.round(source.height * scale));
@@ -30,7 +44,8 @@ const scaleCanvas = (source, scale) => {
30
44
  * Applies to still images only — recorded video is never re-encoded.
31
45
  */
32
46
  export const canvasToJpegBase64 = (canvas, quality = DEFAULTS.IMAGE_QUALITY) => {
33
- let base64 = encode(canvas, quality);
47
+ const opaque = flattenForJpeg(canvas);
48
+ let base64 = encode(opaque, quality);
34
49
 
35
50
  const maxSizeInMB = state.originalImageMaxSizeInMB;
36
51
  if (!maxSizeInMB) return base64;
@@ -40,11 +55,11 @@ export const canvasToJpegBase64 = (canvas, quality = DEFAULTS.IMAGE_QUALITY) =>
40
55
 
41
56
  for (const step of QUALITY_STEPS) {
42
57
  if (step >= quality) continue;
43
- base64 = encode(canvas, step);
58
+ base64 = encode(opaque, step);
44
59
  if (base64Bytes(base64) <= maxBytes) return base64;
45
60
  }
46
61
 
47
- let working = canvas;
62
+ let working = opaque;
48
63
  for (let attempt = 0; attempt < MAX_SCALE_ATTEMPTS; attempt += 1) {
49
64
  working = scaleCanvas(working, SCALE_STEP);
50
65
  base64 = encode(working, QUALITY_STEPS[QUALITY_STEPS.length - 1]);
package/core/state.js CHANGED
@@ -136,6 +136,7 @@ export const state = {
136
136
  frontBackDetectionForChecks: false,
137
137
  documentFieldsToDetect: [],
138
138
  disableSubmitUntilFieldDetectionDone: false,
139
+ tesseractBaseUrl: null,
139
140
  fieldDetectionResult: null,
140
141
  cropPreviewRevealed: true,
141
142
  submitInProgress: false,
@@ -1,7 +1,9 @@
1
- import { createWorker } from 'tesseract.js';
2
1
  import { state } from './state.js';
3
2
  import { shouldRunFieldDetection } from './field-detection-policy.js';
3
+ import { TESSERACT_URL } from './constants.js';
4
4
 
5
+ let tesseractModulePromise = null;
6
+ let tesseractModuleUrl = null;
5
7
  let workerPromise = null;
6
8
  let workerLang = null;
7
9
  let tesseractBootstrapStarted = false;
@@ -53,6 +55,37 @@ const RECOGNIZE_OUTPUT = {
53
55
  blocks: true,
54
56
  };
55
57
 
58
+ /**
59
+ * Loader URL + worker paths. With `tesseractBaseUrl` set, every Tesseract file is read from it:
60
+ * `tesseract.esm.min.js`, `worker.min.js`, `core/tesseract-core-*.wasm.js`, `lang/<lang>.traineddata.gz`.
61
+ */
62
+ const getTesseractPaths = () => {
63
+ const base = state.tesseractBaseUrl ? String(state.tesseractBaseUrl).replace(/\/?$/, '/') : null;
64
+ if (!base) {
65
+ return { loaderUrl: TESSERACT_URL, workerPaths: {} };
66
+ }
67
+ return {
68
+ loaderUrl: `${base}tesseract.esm.min.js`,
69
+ workerPaths: {
70
+ workerPath: `${base}worker.min.js`,
71
+ corePath: `${base}core`,
72
+ langPath: `${base}lang`,
73
+ },
74
+ };
75
+ };
76
+
77
+ const loadCreateWorker = async (loaderUrl) => {
78
+ if (!tesseractModulePromise || tesseractModuleUrl !== loaderUrl) {
79
+ tesseractModuleUrl = loaderUrl;
80
+ tesseractModulePromise = import(/* webpackIgnore: true */ /* @vite-ignore */ loaderUrl).catch((error) => {
81
+ tesseractModulePromise = null;
82
+ throw error;
83
+ });
84
+ }
85
+ const { default: Tesseract } = await tesseractModulePromise;
86
+ return Tesseract.createWorker;
87
+ };
88
+
56
89
  /**
57
90
  * Shared Tesseract worker (lazy). Default eng+spa for receipt-style documents.
58
91
  */
@@ -67,19 +100,23 @@ export const getTesseractWorker = async (lang = 'eng+spa') => {
67
100
  }
68
101
 
69
102
  workerLang = lang;
70
- workerPromise = createWorker(lang, 1, {
71
- logger: (message) => {
72
- if (!state.debug_mode || !message?.status) return;
73
- if (message.status === 'recognizing text' && typeof message.progress === 'number') {
74
- const pct = Math.round(message.progress * 100);
75
- if (pct % 25 === 0) {
76
- console.log(`[Tesseract] ${message.status} ${pct}%`);
103
+ const { loaderUrl, workerPaths } = getTesseractPaths();
104
+ workerPromise = loadCreateWorker(loaderUrl).then((createWorker) =>
105
+ createWorker(lang, 1, {
106
+ ...workerPaths,
107
+ logger: (message) => {
108
+ if (!state.debug_mode || !message?.status) return;
109
+ if (message.status === 'recognizing text' && typeof message.progress === 'number') {
110
+ const pct = Math.round(message.progress * 100);
111
+ if (pct % 25 === 0) {
112
+ console.log(`[Tesseract] ${message.status} ${pct}%`);
113
+ }
114
+ } else {
115
+ console.log('[Tesseract]', message.status);
77
116
  }
78
- } else {
79
- console.log('[Tesseract]', message.status);
80
- }
81
- },
82
- });
117
+ },
118
+ })
119
+ );
83
120
  return workerPromise;
84
121
  };
85
122
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "veryfi-lens-wasm",
3
- "version": "3.5.12",
3
+ "version": "3.5.13",
4
4
  "description": "Lens For Web SDK with WASM",
5
5
  "main": "veryfi-lens.js",
6
6
  "type": "module",
@@ -14,8 +14,7 @@
14
14
  "dependencies": {
15
15
  "@fingerprintjs/fingerprintjs": "4.6.2",
16
16
  "@fingerprintjs/fingerprintjs-pro": "3.12.8",
17
- "jszip": "3.10.1",
18
- "tesseract.js": "7.0.0"
17
+ "jszip": "3.10.1"
19
18
  },
20
19
  "files": [
21
20
  "dist",