veryfi-lens-wasm 3.0.5 → 3.1.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.
@@ -69,6 +69,13 @@ export const createCroppedImage = (container, options = {}) => {
69
69
  updatePreviewImg();
70
70
  };
71
71
 
72
+ const originalDrawImage = cropImgCanvas.getContext('2d').drawImage;
73
+ cropImgCanvas.getContext('2d').drawImage = function(...args) {
74
+ const result = originalDrawImage.apply(this, args);
75
+ updatePreviewImg();
76
+ return result;
77
+ };
78
+
72
79
  const layouts = createLayoutManager(previewImg, cropImgCanvas, container, defaultStyles);
73
80
 
74
81
  container.appendChild(cropImgCanvas);
package/core/api.js CHANGED
@@ -37,11 +37,16 @@ export const validatePartner = async (client_id) => {
37
37
  };
38
38
 
39
39
  export const getMetaData = () => {
40
+ const flavor = state.lensFlavor;
41
+ const documentType =
42
+ flavor === 'checks' ? 'checks'
43
+ : (flavor === 'anydocs' || flavor === 'prescription_label') ? 'anydocs'
44
+ : 'documents';
40
45
  return {
41
- device_id: state.uuidv4,
42
- sdk: "lfb-wasm",
43
- document_type: state.lensFlavor === 'checks' ? 'checks' : 'documents',
44
- app_version: state.sdkVersion,
46
+ uuid: state.uuidv4,
47
+ sdk_version: "lfb-wasm",
48
+ document_type: documentType,
49
+ app_ver: state.sdkVersion,
45
50
  blur_detected: state.blurStatus ? 1 : 0,
46
51
  torch_mode: state.torchTrack?.getConstraints()?.advanced?.[0]?.torch ? 1 : 0,
47
52
  corners: state.coordinates,
@@ -50,32 +55,165 @@ export const getMetaData = () => {
50
55
  };
51
56
  };
52
57
 
53
- export const docReady = async () => {
58
+ const buildJsonData = async () => {
59
+ const browserData = await getBrowserData();
60
+ const UAData = flattenObject(parseUA());
61
+ if ('os_name' in UAData) { UAData.device_platform = UAData.os_name; delete UAData.os_name; }
62
+ if ('os_version' in UAData) { UAData.system_ver = UAData.os_version; delete UAData.os_version; }
63
+ const meta = getMetaData();
64
+ // Legacy aliases for FastAPI deployments that still read the pre-rename
65
+ // field names (device_id, app_version, os_version, sdk, device_vendor).
66
+ // The current FastAPI uses uuid/app_ver/system_ver/sdk_version/device_platform;
67
+ // emitting both keeps the new SDK compatible with both server versions.
68
+ const legacyAliases = {
69
+ device_id: meta.uuid,
70
+ app_version: meta.app_ver,
71
+ os_version: UAData.system_ver,
72
+ sdk: meta.sdk_version,
73
+ device_vendor: UAData.device_platform,
74
+ };
75
+ return { ...browserData, ...UAData, ...meta, ...legacyAliases };
76
+ };
77
+
78
+ // Legacy per-crop flow: posts each captured scan to /rest/send-cropped, which
79
+ // uploads a single-scan zip to S3 and fires document_ready server-side with a
80
+ // generated UUID. Used when packageMode is disabled to preserve the
81
+ // pre-refactor behavior.
82
+ //
83
+ // - isBase64 = true => JSON body { image: <base64>, json_data, client_id }
84
+ // - isBase64 = false => multipart body { file: <Blob>, json_data, client_id }
85
+ const sendCroppedLegacy = async ({ data, fileName, isBase64 }) => {
86
+ const jsonData = await buildJsonData();
87
+ const clientId = state.clientId;
88
+ const sessionId = state.clientData?.session;
89
+
54
90
  const spinner = createSpinner(document.body, 'Preparing document...');
55
91
  spinner.show();
56
92
 
57
93
  try {
58
- const base64Image = state.finalImage;
59
- const browserData = await getBrowserData();
60
- const UAData = flattenObject(parseUA());
61
- const jsonData = { ...browserData, ...UAData, ...getMetaData() };
62
-
63
- const payload = {
64
- image: base64Image,
65
- json_data: jsonData,
66
- client_id: state.clientId
94
+ const headers = {
95
+ 'CLIENT-ID': clientId,
96
+ 'SESSION-ID': sessionId,
67
97
  };
98
+ let body;
99
+ if (isBase64) {
100
+ headers['Content-Type'] = 'application/json';
101
+ body = JSON.stringify({
102
+ image: data,
103
+ json_data: jsonData,
104
+ client_id: clientId,
105
+ });
106
+ } else {
107
+ const formData = new FormData();
108
+ formData.append('file', data, fileName);
109
+ formData.append('json_data', JSON.stringify(jsonData));
110
+ formData.append('client_id', clientId);
111
+ body = formData;
112
+ }
113
+
114
+ const response = await fetch(API_URLS.SEND_CROPPED, { method: 'POST', headers, body });
115
+ if (!response.ok) {
116
+ if (response.status === 401) {
117
+ throw new Error('Authorization failed, please contact customer support at support@veryfi.com');
118
+ }
119
+ throw new Error(`HTTP error! status: ${response.status}`);
120
+ }
121
+ return await response.json();
122
+ } catch (error) {
123
+ console.error('Error processing document:', error);
124
+ state.failureHandler?.(error);
125
+ throw error;
126
+ } finally {
127
+ spinner.remove();
128
+ }
129
+ };
130
+
131
+ // Per-capture entrypoint. Two modes:
132
+ // packageMode = true => collect into state.packageScans for one cumulative
133
+ // zip uploaded at submit time via uploadPackage().
134
+ // Matches mobile's serverN.json + imageN.jpg layout.
135
+ // packageMode = false => fire the legacy per-crop /rest/send-cropped call,
136
+ // which uploads a single-scan zip to S3 and fires
137
+ // document_ready immediately with a generated UUID.
138
+ //
139
+ // Scan record shape:
140
+ // { data, fileName, isBase64, jsonData }
141
+ // - data: base64 string (image flavors) | Blob (prescription_label video)
142
+ // - fileName: entry name, e.g. 'image0.jpg' or 'prescription_label.mp4'
143
+ // - isBase64: true => JSZip will decode the string when zipping
144
+ // - jsonData: per-scan server{N}.json contents
145
+ export const addPackageScan = async ({ data, fileName, isBase64 = false }) => {
146
+ if (!state.packageMode) {
147
+ return await sendCroppedLegacy({ data, fileName, isBase64 });
148
+ }
149
+
150
+ const jsonData = await buildJsonData();
151
+
152
+ if (!state.packageUploadId) {
153
+ state.packageUploadId = (typeof crypto !== 'undefined' && crypto.randomUUID)
154
+ ? crypto.randomUUID()
155
+ : `${Date.now()}-${Math.random().toString(36).slice(2)}`;
156
+ }
157
+ if (!state.packageFilename) {
158
+ const sanitizedClient = String(state.clientId || 'lfb').replace(/[^\w\-]+/g, '');
159
+ state.packageFilename = `${sanitizedClient}_veryfi_pckupl_${state.packageUploadId}.zip`;
160
+ }
161
+
162
+ state.packageScans.push({ data, fileName, isBase64, jsonData });
163
+
164
+ return { upload_id: state.packageUploadId };
165
+ };
166
+
167
+ export const docReady = async () => {
168
+ const index = state.packageScans.length;
169
+ return addPackageScan({
170
+ data: state.finalImage,
171
+ fileName: `image${index}.jpg`,
172
+ isBase64: true,
173
+ });
174
+ };
175
+
176
+ export const uploadPackage = async () => {
177
+ // Snapshot state into locals before any await: the submit button's click
178
+ // handler fires onSubmit() then synchronously calls removeOverlay(), which
179
+ // triggers resetState() and wipes state.packageScans/Filename/UploadId mid-flight.
180
+ const scans = state.packageScans?.slice();
181
+ const filename = state.packageFilename;
182
+ const uploadId = state.packageUploadId;
183
+ const clientId = state.clientId;
184
+ const sessionId = state.clientData?.session;
185
+
186
+ if (!scans?.length) {
187
+ throw new Error('No scans to upload');
188
+ }
189
+
190
+ const spinner = createSpinner(document.body, 'Processing document...');
191
+ spinner.show();
192
+
193
+ try {
194
+ const { default: JSZip } = await import('jszip');
195
+ const zip = new JSZip();
196
+ scans.forEach((scan, index) => {
197
+ zip.file(scan.fileName, scan.data, scan.isBase64 ? { base64: true } : {});
198
+ zip.file(`server${index}.json`, JSON.stringify(scan.jsonData, null, 2));
199
+ });
200
+ const zipBlob = await zip.generateAsync({ type: 'blob' });
201
+
202
+ const lastJsonData = scans[scans.length - 1].jsonData;
203
+ const formData = new FormData();
204
+ formData.append('file', zipBlob, filename);
205
+ formData.append('json_data', JSON.stringify(lastJsonData));
206
+ formData.append('upload_id', uploadId);
68
207
 
69
208
  const response = await fetch(
70
- API_URLS.SEND_CROPPED,
209
+ API_URLS.SEND_PACKAGE,
71
210
  {
72
211
  method: "POST",
73
212
  headers: {
74
- "Content-Type": "application/json",
75
- "CLIENT-ID": state.clientId,
76
- "SESSION-ID": state.clientData.session
213
+ "CLIENT-ID": clientId,
214
+ "SESSION-ID": sessionId
77
215
  },
78
- body: JSON.stringify(payload)
216
+ body: formData
79
217
  }
80
218
  );
81
219
 
@@ -86,73 +224,138 @@ export const docReady = async () => {
86
224
  throw new Error(`HTTP error! status: ${response.status}`);
87
225
  }
88
226
  const result = await response.json();
89
- spinner.remove();
227
+ state.docReadyResult = result;
90
228
  return result;
91
229
  } catch (error) {
92
- spinner.remove();
93
- console.error("Error processing document:", error);
230
+ console.error("Error uploading package:", error);
94
231
  state.failureHandler?.(error);
95
232
  throw error;
233
+ } finally {
234
+ spinner.remove();
235
+ }
236
+ };
237
+
238
+ export const ensurePackageUploaded = async () => {
239
+ if (!state.packageMode) return null;
240
+ if (state.docReadyResult?.package_path) {
241
+ return state.docReadyResult;
242
+ }
243
+ if (state.packageScans?.length) {
244
+ return await uploadPackage();
245
+ }
246
+ return state.docReadyResult;
247
+ };
248
+
249
+ // When packageMode is on and the SDK's submit button is disabled, the partner
250
+ // has no submit signal to trigger uploadPackage(). To still surface
251
+ // { bucket, package_path, file_name, package_id } to the partner via the
252
+ // showCamera setter, eagerly upload here and merge the result into the
253
+ // payload. Callers that know more captures are still expected (checks front
254
+ // when back is required, anydocs page captures before "Done") pass
255
+ // autoUpload: false so the upload is deferred to the actual last capture.
256
+ //
257
+ // Returns true when the call was the finalizing fire — the package was
258
+ // uploaded and the setter received { ...payload, ...packageInfo }. Callers
259
+ // use this to know when to tear down the overlay (since the SDK's submit
260
+ // button is hidden and there's no other teardown trigger). Returns false in
261
+ // all other cases (packageMode: false, enableSubmit: true, or autoUpload: false).
262
+ export const fireResultSetter = async (payload, { autoUpload = true } = {}) => {
263
+ if (typeof state.resultSetter !== 'function') return false;
264
+ if (autoUpload && state.packageMode && !state.enableSubmit) {
265
+ const packageInfo = await ensurePackageUploaded();
266
+ if (packageInfo) {
267
+ state.resultSetter({ ...payload, ...packageInfo });
268
+ return true;
269
+ }
96
270
  }
271
+ state.resultSetter(payload);
272
+ return false;
97
273
  };
98
274
 
99
275
  export const handleSubmit = async (image) => {
276
+ // Snapshot state into locals before any await — the submit button click handler
277
+ // fires removeOverlay()/resetState() concurrently with this flow.
100
278
  const lensFlavor = state.lensFlavor;
101
279
  const selectedBlueprint = state.selectedBlueprint;
102
280
  const clientData = state.clientData;
103
281
  const customData = state.customData;
282
+ const clientId = state.clientId;
283
+ const sessionId = state.clientData?.session;
284
+ const uploadId = state.packageUploadId;
285
+ const lastJsonData = state.packageScans?.[state.packageScans.length - 1]?.jsonData;
104
286
 
105
- const spinner = createSpinner(document.body, 'Submitting document...');
106
- spinner.show();
107
287
  state.updateHandler?.({ status: 'submission_started' });
108
288
 
109
- const deviceData = await getDeviceData();
289
+ try {
290
+ const packageInfo = await ensurePackageUploaded();
291
+ const hasPackagePath = !!packageInfo?.package_path;
110
292
 
111
- const getDocType = () => {
112
- if (lensFlavor === 'checks') return 'checks';
113
- if (lensFlavor === 'anydocs' || lensFlavor === 'prescription_label') return 'anydocs';
114
- return 'documents';
115
- };
293
+ const spinner = createSpinner(document.body, 'Submitting document...');
294
+ spinner.show();
116
295
 
117
- const requestBody = {
118
- image: image,
119
- doc_type: getDocType(),
120
- resource: "api",
121
- client_name: clientData.client_name,
122
- device_data: deviceData,
123
- ...((lensFlavor === 'anydocs' || lensFlavor === 'prescription_label') && selectedBlueprint ? { blueprint_name: selectedBlueprint } : {}),
124
- ...customData
125
- };
296
+ try {
297
+ const deviceData = await getDeviceData();
126
298
 
127
- const requestOptions = {
128
- method: 'POST',
129
- headers: {
130
- 'Content-Type': 'application/json',
131
- 'CLIENT-ID': state.clientId,
132
- 'SESSION-ID': state.clientData.session
133
- },
134
- body: JSON.stringify(requestBody)
135
- };
299
+ const getDocType = () => {
300
+ if (lensFlavor === 'checks') return 'checks';
301
+ if (lensFlavor === 'anydocs' || lensFlavor === 'prescription_label') return 'anydocs';
302
+ return 'documents';
303
+ };
136
304
 
137
- try {
138
- const response = await fetch(API_URLS.PROCESS_DOCUMENT, requestOptions);
139
- if (!response.ok) {
140
- const errorData = await response.json();
141
- console.error('Submit error:', errorData);
142
- state.updateHandler?.({ status: 'submission_failed', error: errorData });
143
- state.failureHandler?.(errorData);
144
- throw new Error(`HTTP error! status: ${response.status}`);
145
- }
146
- state.debug_mode && console.log("Document submitted successfully");
147
- state.updateHandler?.({ status: 'submission_completed' });
148
- if (state.successHandler && typeof state.successHandler === 'function') {
149
- state.debug_mode && console.log("Success handler called");
150
- const result = await response.json();
151
- state.successHandler(result);
305
+ const requestBody = {
306
+ doc_type: getDocType(),
307
+ resource: "api",
308
+ client_name: clientData.client_name,
309
+ device_data: deviceData,
310
+ ...(hasPackagePath
311
+ ? {
312
+ package_path: packageInfo.package_path,
313
+ bucket: packageInfo.bucket,
314
+ file_name: packageInfo.file_name,
315
+ upload_id: uploadId,
316
+ ...(lastJsonData ? { package_metadata: lastJsonData } : {}),
317
+ }
318
+ : { image: image }),
319
+ ...((lensFlavor === 'anydocs' || lensFlavor === 'prescription_label') && selectedBlueprint ? { blueprint_name: selectedBlueprint } : {}),
320
+ ...customData
321
+ };
322
+
323
+ const qrSessionId = new URLSearchParams(window.location.search).get('session');
324
+
325
+ const requestOptions = {
326
+ method: 'POST',
327
+ headers: {
328
+ 'Content-Type': 'application/json',
329
+ 'CLIENT-ID': clientId,
330
+ 'SESSION-ID': sessionId,
331
+ ...(qrSessionId ? { 'QR-SESSION-ID': qrSessionId } : {})
332
+ },
333
+ body: JSON.stringify(requestBody)
334
+ };
335
+
336
+ const response = await fetch(API_URLS.PROCESS_DOCUMENT, requestOptions);
337
+ if (!response.ok) {
338
+ const errorData = await response.json().catch(() => null);
339
+ const message = errorData?.detail || `HTTP error! status: ${response.status}`;
340
+ console.error('Submit error:', errorData ?? response.status);
341
+ state.updateHandler?.({ status: 'submission_failed', error: errorData });
342
+ if (typeof state.failureHandler === 'function') {
343
+ state.failureHandler(errorData ?? new Error(message));
344
+ return;
345
+ }
346
+ throw new Error(message);
347
+ }
348
+ state.debug_mode && console.log("Document submitted successfully");
349
+ state.updateHandler?.({ status: 'submission_completed' });
350
+ if (state.successHandler && typeof state.successHandler === 'function') {
351
+ state.debug_mode && console.log("Success handler called");
352
+ const result = await response.json();
353
+ state.successHandler(result);
354
+ }
355
+ } finally {
356
+ spinner.remove();
152
357
  }
153
- spinner.remove();
154
358
  } catch (error) {
155
- spinner.remove();
156
359
  console.error('Error submitting document:', error);
157
360
  state.failureHandler?.(error);
158
361
  throw error;
package/core/camera.js CHANGED
@@ -19,7 +19,7 @@ export const getVideo = async (lensFlavor) => {
19
19
  const isAndroid = /Android/i.test(navigator.userAgent);
20
20
 
21
21
  let videoConfig = {
22
- ...(lensFlavor === 'long_document' || state.fullScreenEnabled ? {
22
+ ...(lensFlavor === 'long_document' || lensFlavor === 'prescription_label' || state.fullScreenEnabled ? {
23
23
  width: { ideal: 1920 },
24
24
  height: { ideal: 1080 }
25
25
  } : {
@@ -69,6 +69,7 @@ export const getVideo = async (lensFlavor) => {
69
69
 
70
70
  state.videoRef.addEventListener('loadedmetadata', () => {
71
71
  state.videoRef.style.opacity = '1';
72
+ console.log(`[camera] negotiated resolution ${state.videoRef.videoWidth}x${state.videoRef.videoHeight}`);
72
73
 
73
74
  if (state.mirrorOnDesktop) {
74
75
  const isMobile = window.innerWidth <= 768 || window.innerHeight <= 768;
@@ -1,5 +1,5 @@
1
1
  import { state } from '../state.js';
2
- import { handleSubmit } from '../api.js';
2
+ import { handleSubmit, ensurePackageUploaded, fireResultSetter } from '../api.js';
3
3
  import { showVideoOverlay } from '../overlay-controls.js';
4
4
  import { newStartLens, removeOverlay } from '../lifecycle.js';
5
5
  import { createVideo } from '../../components/video.js';
@@ -21,6 +21,11 @@ export const handleAnydocPageCapture = () => {
21
21
  state.anydocPageControls = null;
22
22
  }
23
23
 
24
+ // In packageMode + !enableSubmit + no customSubmitHandler the action button
25
+ // doesn't submit to /api/document — it signals "no more captures, upload now
26
+ // and hand the result to the showCamera setter". Label it "Done" to match.
27
+ const partnerDriven = state.packageMode && !state.enableSubmit && typeof state.customSubmitHandler !== 'function';
28
+
24
29
  state.anydocPageControls = createAnydocPageControls(
25
30
  state.videoOverlay.overlay,
26
31
  async () => {
@@ -56,28 +61,23 @@ export const handleAnydocPageCapture = () => {
56
61
  totalPages: state.anydocPages.length
57
62
  });
58
63
 
59
- let imageToSubmit;
60
- if (state.anydocPages.length === 1) {
61
- imageToSubmit = state.anydocPages[0];
62
- } else {
63
- const { default: JSZip } = await import('jszip');
64
- const zip = new JSZip();
65
- state.anydocPages.forEach((page, index) => {
66
- zip.file(`page_${index + 1}.jpg`, page, { base64: true });
67
- });
68
- imageToSubmit = await zip.generateAsync({ type: "base64" });
69
- }
70
-
71
64
  if (typeof state.customSubmitHandler === 'function') {
72
- state.customSubmitHandler(imageToSubmit);
65
+ const packageInfo = await ensurePackageUploaded();
66
+ state.customSubmitHandler(state.finalImage, packageInfo);
67
+ } else if (state.packageMode && !state.enableSubmit) {
68
+ // packageMode + !enableSubmit: no SDK submit, no custom handler.
69
+ // "Done" is the only signal that all pages have been captured, so
70
+ // upload the cumulative zip now and surface bucket/package_path
71
+ // through the showCamera setter.
72
+ await fireResultSetter({ image: state.finalImage });
73
73
  } else {
74
- handleSubmit(imageToSubmit);
74
+ handleSubmit(state.finalImage);
75
75
  }
76
76
  },
77
77
  removeOverlay,
78
78
  {
79
79
  addPageText: state.anydocAddPageText,
80
- submitText: state.anydocSubmitText,
80
+ submitText: partnerDriven ? 'Done' : state.anydocSubmitText,
81
81
  onClose: state.onClose,
82
82
  onCloseRedirectUrl: state.onCloseRedirectUrl
83
83
  }
@@ -97,6 +97,11 @@ export const handleAnydocPageCapture = () => {
97
97
  state.cropImgRef
98
98
  );
99
99
  } else {
100
- state.enableSubmit && state.anydocPageControls.show(pageNum, canAddMore, state.anydocPages, state.cropImgRef);
100
+ // Page controls are required to reach "Done" in both submit-enabled and
101
+ // packageMode-with-disabled-submit flows; only suppress them in the legacy
102
+ // packageMode: false + !enableSubmit case where the partner has no way to
103
+ // consume cumulative uploads anyway.
104
+ const showControls = state.enableSubmit || state.packageMode;
105
+ showControls && state.anydocPageControls.show(pageNum, canAddMore, state.anydocPages, state.cropImgRef);
101
106
  }
102
107
  };
@@ -1,7 +1,7 @@
1
1
  import { state, refs } from '../state.js';
2
2
  import { cropWasm } from '../wasm-processing.js';
3
3
  import { checkIfDocumentOnCapture, checkIfBlurOnCapture, checkIfLcdOnCapture } from '../validation.js';
4
- import { docReady, handleSubmit } from '../api.js';
4
+ import { docReady, handleSubmit, ensurePackageUploaded, fireResultSetter } from '../api.js';
5
5
  import { hideVideoOverlay, showVideoOverlay } from '../overlay-controls.js';
6
6
  import { newStartLens, removeOverlay } from '../lifecycle.js';
7
7
  import { createSubmitButton } from '../../components/submitButton.js';
@@ -19,14 +19,23 @@ export const handleChecksFrontAccepted = async () => {
19
19
  state.updateHandler?.({ status: 'processing_completed' });
20
20
  state.debug_mode && console.log("Front check image processed successfully");
21
21
 
22
- state.resultSetter?.({
23
- ...state.docReadyResult,
24
- frontImage: state.frontImage,
25
- frontLcdResult: state.frontLcdResult,
26
- frontFrontBackResult: state.frontFrontBackResult
27
- });
22
+ const isBackExpected = state.captureBackOfCheck || state.enforceBothSides || state.enforceAndSkipBothSides;
23
+
24
+ const finalized = await fireResultSetter(
25
+ {
26
+ ...state.docReadyResult,
27
+ frontImage: state.frontImage,
28
+ frontLcdResult: state.frontLcdResult,
29
+ frontFrontBackResult: state.frontFrontBackResult
30
+ },
31
+ { autoUpload: !isBackExpected }
32
+ );
33
+ if (finalized) {
34
+ removeOverlay();
35
+ return;
36
+ }
28
37
 
29
- if (state.captureBackOfCheck || state.enforceBothSides || state.enforceAndSkipBothSides) {
38
+ if (isBackExpected) {
30
39
  if (state.enforceAndSkipBothSides) {
31
40
  const proceedToBackCapture = () => {
32
41
  if (state.videoRef) state.videoRef.style.display = 'block';
@@ -73,9 +82,10 @@ export const handleChecksFrontAccepted = async () => {
73
82
  state.enforceBothSides ? null : () => {
74
83
  hideVideoOverlay();
75
84
  if (state.isDocument) {
76
- state.submitButton = createSubmitButton(state.videoOverlay.overlay, () => {
85
+ state.submitButton = createSubmitButton(state.videoOverlay.overlay, async () => {
77
86
  if (typeof state.customSubmitHandler === 'function') {
78
- state.customSubmitHandler(state.frontImage);
87
+ const packageInfo = await ensurePackageUploaded();
88
+ state.customSubmitHandler(state.frontImage, packageInfo);
79
89
  } else {
80
90
  handleSubmit(state.frontImage);
81
91
  }
@@ -101,16 +111,21 @@ export const handleChecksFrontAccepted = async () => {
101
111
  } else {
102
112
  hideVideoOverlay();
103
113
  if (state.isDocument) {
104
- state.resultSetter?.({
114
+ const finalized = await fireResultSetter({
105
115
  ...state.docReadyResult,
106
116
  frontImage: state.frontImage,
107
117
  frontLcdResult: state.frontLcdResult,
108
118
  frontFrontBackResult: state.frontFrontBackResult
109
119
  });
120
+ if (finalized) {
121
+ removeOverlay();
122
+ return;
123
+ }
110
124
 
111
- state.submitButton = createSubmitButton(state.videoOverlay.overlay, () => {
125
+ state.submitButton = createSubmitButton(state.videoOverlay.overlay, async () => {
112
126
  if (typeof state.customSubmitHandler === 'function') {
113
- state.customSubmitHandler(state.frontImage);
127
+ const packageInfo = await ensurePackageUploaded();
128
+ state.customSubmitHandler(state.frontImage, packageInfo);
114
129
  } else {
115
130
  handleSubmit(state.frontImage);
116
131
  }
@@ -140,32 +155,29 @@ export const handleChecksBackAccepted = async () => {
140
155
  try {
141
156
  const elements = await refs.checkImagesComponent.renderCheckImages(state.frontImage, state.backImage, state.cropImgRef);
142
157
  if (state.isDocument) {
143
- const { default: JSZip } = await import('jszip');
144
- const zip = new JSZip();
145
- zip.file("front.jpg", state.frontImage, { base64: true });
146
- zip.file("back.jpg", state.backImage, { base64: true });
147
- const zipContent = await zip.generateAsync({ type: "base64" });
148
- state.submitButton = createSubmitButton(state.videoOverlay.overlay, () => {
158
+ state.submitButton = createSubmitButton(state.videoOverlay.overlay, async () => {
149
159
  if (typeof state.customSubmitHandler === 'function') {
150
- state.customSubmitHandler((state.captureBackOfCheck || state.enforceBothSides) ? zipContent : state.frontImage);
160
+ const packageInfo = await ensurePackageUploaded();
161
+ state.customSubmitHandler(state.frontImage, packageInfo);
151
162
  } else {
152
- (state.captureBackOfCheck || state.enforceBothSides) ? handleSubmit(zipContent) : handleSubmit(state.frontImage);
163
+ handleSubmit(state.frontImage);
153
164
  }
154
165
  }, removeOverlay);
155
166
  state.enableSubmit && state.submitButton.show();
156
167
  }
157
168
 
158
- if (state.resultSetter && typeof state.resultSetter === 'function') {
159
- state.debug_mode && console.log("Setting result setter");
160
- state.resultSetter({
161
- ...state.docReadyResult,
162
- frontImage: state.frontImage,
163
- backImage: state.backImage,
164
- frontLcdResult: state.frontLcdResult,
165
- backLcdResult: state.backLcdResult,
166
- frontFrontBackResult: state.frontFrontBackResult,
167
- backFrontBackResult: state.backFrontBackResult
168
- });
169
+ state.debug_mode && console.log("Setting result setter");
170
+ const finalized = await fireResultSetter({
171
+ ...state.docReadyResult,
172
+ frontImage: state.frontImage,
173
+ backImage: state.backImage,
174
+ frontLcdResult: state.frontLcdResult,
175
+ backLcdResult: state.backLcdResult,
176
+ frontFrontBackResult: state.frontFrontBackResult,
177
+ backFrontBackResult: state.backFrontBackResult
178
+ });
179
+ if (finalized) {
180
+ removeOverlay();
169
181
  }
170
182
  } catch (error) {
171
183
  console.error('Error rendering check images:', error);
@@ -1,7 +1,7 @@
1
1
  import { state, refs } from '../state.js';
2
2
  import { cropWasm } from '../wasm-processing.js';
3
3
  import { checkIfDocumentOnCapture, checkIfBlurOnCapture, checkIfLcdOnCapture } from '../validation.js';
4
- import { docReady, handleSubmit } from '../api.js';
4
+ import { docReady, handleSubmit, ensurePackageUploaded, fireResultSetter } from '../api.js';
5
5
  import { hideVideoOverlay } from '../overlay-controls.js';
6
6
  import { stopLens, startStitching, stopStitching, removeOverlay } from '../lifecycle.js';
7
7
  import { generateDeviceIDifNeeded } from '../device.js';
@@ -117,7 +117,15 @@ export const handleDocumentAutoCaptureDone = async () => {
117
117
  state.lcdStatus = documentLcdResult.is_lcd || (documentLcdResult.lcdProb !== undefined && documentLcdResult.lcdProb > state.lcdThreshold);
118
118
  }
119
119
 
120
- state.resultSetter?.({ ...state.docReadyResult, image: state.finalImage });
120
+ // anydocs is multi-page; defer the eager upload until "Done" is clicked.
121
+ const finalized = await fireResultSetter(
122
+ { ...state.docReadyResult, image: state.finalImage },
123
+ { autoUpload: state.lensFlavor !== 'anydocs' }
124
+ );
125
+ if (finalized) {
126
+ removeOverlay();
127
+ return;
128
+ }
121
129
 
122
130
  const isDocumentDetected = checkIfDocumentOnCapture();
123
131
  const documentModalShown = !isDocumentDetected && state.isDocumentModal && !!state.documentModal;
@@ -134,9 +142,10 @@ export const handleDocumentAutoCaptureDone = async () => {
134
142
  const { handleAnydocPageCapture } = await import('./anydoc.js');
135
143
  handleAnydocPageCapture();
136
144
  } else {
137
- state.submitButton = createSubmitButton(state.videoOverlay.overlay, () => {
145
+ state.submitButton = createSubmitButton(state.videoOverlay.overlay, async () => {
138
146
  if (typeof state.customSubmitHandler === 'function') {
139
- state.customSubmitHandler(state.finalImage);
147
+ const packageInfo = await ensurePackageUploaded();
148
+ state.customSubmitHandler(state.finalImage, packageInfo);
140
149
  } else {
141
150
  handleSubmit(state.finalImage);
142
151
  }
@@ -226,7 +235,14 @@ export const newCapture = async () => {
226
235
  state.debug_mode && console.log(`LCD detection - prob: ${documentLcdResult.lcdProb}, threshold: ${state.lcdThreshold}, detected: ${state.lcdStatus}`);
227
236
  }
228
237
 
229
- state.resultSetter?.({ ...state.docReadyResult, image: state.finalImage });
238
+ const finalized = await fireResultSetter(
239
+ { ...state.docReadyResult, image: state.finalImage },
240
+ { autoUpload: state.lensFlavor !== 'anydocs' }
241
+ );
242
+ if (finalized) {
243
+ removeOverlay();
244
+ return;
245
+ }
230
246
  }
231
247
 
232
248
  const documentModalShown = !isDocumentDetected && state.isDocumentModal && !!state.documentModal;
@@ -244,9 +260,10 @@ export const newCapture = async () => {
244
260
  const { handleAnydocPageCapture } = await import('./anydoc.js');
245
261
  handleAnydocPageCapture();
246
262
  } else {
247
- state.submitButton = createSubmitButton(state.videoOverlay.overlay, () => {
263
+ state.submitButton = createSubmitButton(state.videoOverlay.overlay, async () => {
248
264
  if (typeof state.customSubmitHandler === 'function') {
249
- state.customSubmitHandler(state.finalImage);
265
+ const packageInfo = await ensurePackageUploaded();
266
+ state.customSubmitHandler(state.finalImage, packageInfo);
250
267
  } else {
251
268
  handleSubmit(state.finalImage);
252
269
  }
@@ -1,11 +1,13 @@
1
1
  import { state } from '../state.js';
2
- import { handleSubmit } from '../api.js';
2
+ import { handleSubmit, addPackageScan, ensurePackageUploaded } from '../api.js';
3
3
  import { hideVideoOverlay, showVideoOverlay } from '../overlay-controls.js';
4
4
  import { removeOverlay, newStartLens } from '../lifecycle.js';
5
5
  import { createSubmitButton } from '../../components/submitButton.js';
6
6
 
7
- let mediaRecorder = null;
8
- let recordedChunks = [];
7
+ let previewRecorder = null;
8
+ let previewChunks = [];
9
+ let submissionRecorder = null;
10
+ let submissionChunks = [];
9
11
  let isRecording = false;
10
12
  let recordingIndicator = null;
11
13
  let pulseStyleEl = null;
@@ -15,6 +17,7 @@ let playPauseButtonEl = null;
15
17
  let lastVideoBlob = null;
16
18
  let cropCanvas = null;
17
19
  let cropAnimationFrameId = null;
20
+ let recordingStartTime = 0;
18
21
 
19
22
  const ensurePulseKeyframes = () => {
20
23
  if (pulseStyleEl) return;
@@ -182,7 +185,6 @@ const computeSourceCropRegion = (videoElement) => {
182
185
  };
183
186
 
184
187
  const startCroppedDrawLoop = (videoElement, region) => {
185
- // Even dimensions are required by some video codecs (e.g. H.264).
186
188
  const outW = Math.max(2, Math.floor(region.srcW / 2) * 2);
187
189
  const outH = Math.max(2, Math.floor(region.srcH / 2) * 2);
188
190
 
@@ -207,8 +209,6 @@ const startCroppedDrawLoop = (videoElement, region) => {
207
209
  cropAnimationFrameId = requestAnimationFrame(draw);
208
210
  };
209
211
  draw();
210
-
211
- return cropCanvas.captureStream(30);
212
212
  };
213
213
 
214
214
  const stopCroppedDrawLoop = () => {
@@ -227,39 +227,48 @@ const startRecording = () => {
227
227
  return;
228
228
  }
229
229
 
230
- recordedChunks = [];
230
+ previewChunks = [];
231
+ submissionChunks = [];
231
232
  const mimeType = getRecorderMimeType();
232
233
  const recorderOptions = mimeType ? { mimeType } : {};
233
234
  console.log(`[prescription_label] MediaRecorder options:`, recorderOptions);
234
235
 
235
- let recordingStream = stream;
236
+ let previewStream = stream;
237
+ let submissionStream = stream;
236
238
  const region = computeSourceCropRegion(state.videoRef);
237
239
  if (region) {
238
240
  try {
239
- recordingStream = startCroppedDrawLoop(state.videoRef, region);
241
+ startCroppedDrawLoop(state.videoRef, region);
240
242
  state.debug_mode && console.log(`[prescription_label] Cropping video to ${cropCanvas.width}x${cropCanvas.height} (source region ${Math.round(region.srcW)}x${Math.round(region.srcH)} at ${Math.round(region.srcX)},${Math.round(region.srcY)})`);
243
+ previewStream = cropCanvas.captureStream(30);
244
+ submissionStream = cropCanvas.captureStream(30);
241
245
  } catch (e) {
242
246
  console.warn('[prescription_label] Failed to set up cropped recording stream, falling back to full video:', e);
243
247
  stopCroppedDrawLoop();
244
- recordingStream = stream;
248
+ previewStream = stream;
249
+ submissionStream = stream;
245
250
  }
246
251
  }
247
252
 
248
253
  try {
249
- mediaRecorder = new MediaRecorder(recordingStream, recorderOptions);
254
+ previewRecorder = new MediaRecorder(previewStream, recorderOptions);
255
+ submissionRecorder = new MediaRecorder(submissionStream, recorderOptions);
250
256
  } catch (e) {
251
257
  console.error('Failed to create MediaRecorder:', e);
252
258
  state.failureHandler?.(e);
253
259
  return;
254
260
  }
255
261
 
256
- mediaRecorder.ondataavailable = (event) => {
257
- if (event.data.size > 0) {
258
- recordedChunks.push(event.data);
259
- }
262
+ previewRecorder.ondataavailable = (event) => {
263
+ if (event.data.size > 0) previewChunks.push(event.data);
264
+ };
265
+ submissionRecorder.ondataavailable = (event) => {
266
+ if (event.data.size > 0) submissionChunks.push(event.data);
260
267
  };
261
268
 
262
- mediaRecorder.start(200);
269
+ previewRecorder.start(200);
270
+ submissionRecorder.start(200);
271
+ recordingStartTime = performance.now();
263
272
  isRecording = true;
264
273
  updateRecordingUI(true);
265
274
  state.prescriptionLabelGuide?.hide?.();
@@ -269,24 +278,40 @@ const startRecording = () => {
269
278
 
270
279
  const stopRecording = () => {
271
280
  return new Promise((resolve) => {
272
- if (!mediaRecorder || !isRecording) {
281
+ if (!previewRecorder || !submissionRecorder || !isRecording) {
273
282
  resolve(null);
274
283
  return;
275
284
  }
276
285
 
277
- mediaRecorder.onstop = () => {
286
+ let previewBlob = null;
287
+ let submissionBlob = null;
288
+ const finalize = () => {
289
+ if (!previewBlob || !submissionBlob) return;
290
+ const durationSec = (performance.now() - recordingStartTime) / 1000;
291
+ console.log(`[prescription_label] preview blob — size: ${previewBlob.size} bytes (${(previewBlob.size / 1024).toFixed(1)} KB), duration: ${durationSec.toFixed(2)}s, bitrate: ${((previewBlob.size * 8) / durationSec / 1000).toFixed(0)} kbps`);
292
+ console.log(`[prescription_label] submission blob — size: ${submissionBlob.size} bytes (${(submissionBlob.size / 1024).toFixed(1)} KB), duration: ${durationSec.toFixed(2)}s, bitrate: ${((submissionBlob.size * 8) / durationSec / 1000).toFixed(0)} kbps`);
293
+ previewChunks = [];
294
+ submissionChunks = [];
295
+ previewRecorder = null;
296
+ submissionRecorder = null;
278
297
  isRecording = false;
279
298
  updateRecordingUI(false);
280
- const blob = new Blob(recordedChunks, { type: mediaRecorder.mimeType });
281
- recordedChunks = [];
282
- mediaRecorder = null;
283
299
  stopCroppedDrawLoop();
284
300
  state.updateHandler?.({ status: 'recording_stopped' });
285
- state.debug_mode && console.log('[EVENT] prescription label recording stopped, blob size:', blob.size);
286
- resolve(blob);
301
+ resolve({ previewBlob, submissionBlob });
302
+ };
303
+
304
+ previewRecorder.onstop = () => {
305
+ previewBlob = new Blob(previewChunks, { type: previewRecorder.mimeType });
306
+ finalize();
307
+ };
308
+ submissionRecorder.onstop = () => {
309
+ submissionBlob = new Blob(submissionChunks, { type: submissionRecorder.mimeType });
310
+ finalize();
287
311
  };
288
312
 
289
- mediaRecorder.stop();
313
+ previewRecorder.stop();
314
+ submissionRecorder.stop();
290
315
  });
291
316
  };
292
317
 
@@ -424,6 +449,11 @@ const showVideoPreview = (videoBlob) => {
424
449
  state.submitButton.remove();
425
450
  state.submitButton = null;
426
451
  }
452
+ // The recording-end addPackageScan accumulated a scan in package mode;
453
+ // drop it so the next recording replaces (not appends to) it.
454
+ if (state.packageMode && state.packageScans?.length) {
455
+ state.packageScans.pop();
456
+ }
427
457
  showVideoOverlay();
428
458
  state.prescriptionLabelGuide?.show?.();
429
459
  await newStartLens();
@@ -432,27 +462,36 @@ const showVideoPreview = (videoBlob) => {
432
462
  };
433
463
 
434
464
  const submitVideoBlob = async (videoBlob) => {
465
+ const submissionStartTime = performance.now();
435
466
  state.updateHandler?.({ status: 'processing_started' });
436
467
 
437
- const { default: JSZip } = await import('jszip');
438
- const zip = new JSZip();
439
-
440
- const ext = videoBlob.type.includes('mp4') ? 'mp4' : videoBlob.type.includes('quicktime') ? 'mov' : 'webm';
441
- zip.file(`prescription_label.${ext}`, videoBlob);
442
-
443
- const zipBase64 = await zip.generateAsync({ type: 'base64' });
468
+ // /api/document accepts either { package_path, ... } (packageMode true) or
469
+ // { image: <base64> } (legacy). Blobs cannot be JSON-serialized, so for the
470
+ // legacy path we zip+base64 the video here, mirroring the pre-refactor flow.
471
+ let submitData = videoBlob;
472
+ if (!state.packageMode) {
473
+ const ext = videoBlob.type.includes('mp4') ? 'mp4' : videoBlob.type.includes('quicktime') ? 'mov' : 'webm';
474
+ const { default: JSZip } = await import('jszip');
475
+ const zip = new JSZip();
476
+ zip.file(`prescription_label.${ext}`, videoBlob);
477
+ submitData = await zip.generateAsync({ type: 'base64' });
478
+ }
444
479
 
445
- state.finalImage = zipBase64;
446
480
  state.updateHandler?.({ status: 'processing_completed' });
447
481
 
482
+ const uploadStartTime = performance.now();
448
483
  if (typeof state.customSubmitHandler === 'function') {
449
- state.customSubmitHandler(zipBase64);
484
+ const packageInfo = await ensurePackageUploaded();
485
+ state.customSubmitHandler(videoBlob, packageInfo);
450
486
  } else {
451
- await handleSubmit(zipBase64);
487
+ await handleSubmit(submitData);
452
488
  }
489
+ const uploadDurationSec = (performance.now() - uploadStartTime) / 1000;
490
+ const totalDurationSec = (performance.now() - submissionStartTime) / 1000;
491
+ console.log(`[prescription_label] submission complete — total: ${totalDurationSec.toFixed(2)}s, upload: ${uploadDurationSec.toFixed(2)}s, video: ${(videoBlob.size / 1024).toFixed(1)} KB`);
453
492
 
454
493
  state.updateHandler?.({ status: 'capture_completed' });
455
- return zipBase64;
494
+ return videoBlob;
456
495
  };
457
496
 
458
497
  export const capturePrescriptionLabel = async () => {
@@ -466,10 +505,11 @@ export const capturePrescriptionLabel = async () => {
466
505
  state.debug_mode && console.log('[EVENT] stopping prescription label recording');
467
506
  state.updateHandler?.({ status: 'capture_started' });
468
507
 
469
- const videoBlob = await stopRecording();
470
- if (!videoBlob || videoBlob.size === 0) {
508
+ const result = await stopRecording();
509
+ if (!result || !result.previewBlob || result.previewBlob.size === 0 || !result.submissionBlob || result.submissionBlob.size === 0) {
471
510
  throw new Error('Failed to record video');
472
511
  }
512
+ const { previewBlob, submissionBlob } = result;
473
513
 
474
514
  hideVideoOverlay();
475
515
  state.exitButtonRef?.hide?.();
@@ -478,12 +518,24 @@ export const capturePrescriptionLabel = async () => {
478
518
  state.videoRef.srcObject.getTracks().forEach((track) => track.stop());
479
519
  }
480
520
 
481
- showVideoPreview(videoBlob);
521
+ showVideoPreview(previewBlob);
522
+
523
+ // Per-capture upload, parity with image flavors' docReady() on crop accept:
524
+ // - packageMode: true => accumulate scan into state.packageScans
525
+ // - packageMode: false => POST multipart Blob to /rest/send-cropped now
526
+ const ext = submissionBlob.type.includes('mp4') ? 'mp4'
527
+ : submissionBlob.type.includes('quicktime') ? 'mov'
528
+ : 'webm';
529
+ state.docReadyResult = await addPackageScan({
530
+ data: submissionBlob,
531
+ fileName: `prescription_label.${ext}`,
532
+ isBase64: false,
533
+ });
482
534
 
483
535
  state.submitButton = createSubmitButton(
484
536
  state.container,
485
537
  async () => {
486
- await submitVideoBlob(videoBlob);
538
+ await submitVideoBlob(submissionBlob);
487
539
  removeOverlay();
488
540
  },
489
541
  () => {}
@@ -500,7 +552,14 @@ export const capturePrescriptionLabel = async () => {
500
552
  }
501
553
  state.submitButton.show();
502
554
 
503
- state.resultSetter?.(videoBlob);
555
+ if (state.packageMode && !state.enableSubmit) {
556
+ // packageMode + !enableSubmit: upload now and surface bucket/package_path
557
+ // through the setter alongside the raw videoBlob.
558
+ const packageInfo = await ensurePackageUploaded();
559
+ state.resultSetter?.({ ...(packageInfo || {}), image: submissionBlob });
560
+ } else {
561
+ state.resultSetter?.(submissionBlob);
562
+ }
504
563
 
505
564
  return null;
506
565
  } catch (error) {
@@ -512,15 +571,14 @@ export const capturePrescriptionLabel = async () => {
512
571
  };
513
572
 
514
573
  export const cleanupPrescriptionLabelRecording = () => {
515
- if (mediaRecorder && isRecording) {
516
- try {
517
- mediaRecorder.stop();
518
- } catch (e) {
519
- // ignore
520
- }
574
+ if (isRecording) {
575
+ try { previewRecorder?.stop(); } catch (e) { /* ignore */ }
576
+ try { submissionRecorder?.stop(); } catch (e) { /* ignore */ }
521
577
  }
522
- mediaRecorder = null;
523
- recordedChunks = [];
578
+ previewRecorder = null;
579
+ submissionRecorder = null;
580
+ previewChunks = [];
581
+ submissionChunks = [];
524
582
  isRecording = false;
525
583
  updateRecordingUI(false);
526
584
  stopCroppedDrawLoop();
@@ -1,5 +1,5 @@
1
1
  import { state } from '../state.js';
2
- import { docReady, handleSubmit } from '../api.js';
2
+ import { docReady, handleSubmit, ensurePackageUploaded, fireResultSetter } from '../api.js';
3
3
  import { hideVideoOverlay } from '../overlay-controls.js';
4
4
  import { removeOverlay } from '../lifecycle.js';
5
5
  import { createSubmitButton } from '../../components/submitButton.js';
@@ -22,6 +22,14 @@ export const captureBottleCap = async () => {
22
22
  state.isDocument = true;
23
23
  hideVideoOverlay();
24
24
 
25
+ // hideVideoOverlay() doesn't cover the bottle-cap dark mask (it lives in
26
+ // its own overlay), and capturing doesn't stop the camera stream — so
27
+ // without this the cropped preview shows through a grey backdrop and the
28
+ // device camera LED stays on. Hide the specialty overlay and stop the
29
+ // tracks now that the still frame has been captured.
30
+ state.bottleCapOverlay?.hide?.();
31
+ state.videoRef?.srcObject?.getTracks?.().forEach((track) => track.stop());
32
+
25
33
  state.cropImgRef?.setSquareDisplay?.();
26
34
  state.cropImgRef?.showSuccessBorder?.();
27
35
 
@@ -31,14 +39,20 @@ export const captureBottleCap = async () => {
31
39
  state.updateHandler?.({ status: 'processing_started' });
32
40
  state.docReadyResult = await docReady();
33
41
  state.updateHandler?.({ status: 'processing_completed' });
34
- state.resultSetter?.({ ...state.docReadyResult, image: state.finalImage });
42
+ const finalized = await fireResultSetter({ ...state.docReadyResult, image: state.finalImage });
43
+ if (finalized) {
44
+ state.updateHandler?.({ status: 'capture_completed' });
45
+ removeOverlay();
46
+ return state.finalImage;
47
+ }
35
48
  } catch (error) {
36
49
  console.error("Error processing document:", error);
37
50
  }
38
51
 
39
- state.submitButton = createSubmitButton(state.videoOverlay.overlay, () => {
52
+ state.submitButton = createSubmitButton(state.videoOverlay.overlay, async () => {
40
53
  if (typeof state.customSubmitHandler === 'function') {
41
- state.customSubmitHandler(state.finalImage);
54
+ const packageInfo = await ensurePackageUploaded();
55
+ state.customSubmitHandler(state.finalImage, packageInfo);
42
56
  } else {
43
57
  handleSubmit(state.finalImage);
44
58
  }
@@ -74,6 +88,14 @@ export const captureCodeStrip = async () => {
74
88
  state.isDocument = true;
75
89
  hideVideoOverlay();
76
90
 
91
+ // hideVideoOverlay() doesn't cover the code-strips dark mask (it lives in
92
+ // its own overlay), and capturing doesn't stop the camera stream — so
93
+ // without this the cropped preview shows through a grey backdrop and the
94
+ // device camera LED stays on. Hide the specialty overlay and stop the
95
+ // tracks now that the still frame has been captured.
96
+ state.codeStripsOverlay?.hide?.();
97
+ state.videoRef?.srcObject?.getTracks?.().forEach((track) => track.stop());
98
+
77
99
  state.cropImgRef?.setRectangleDisplay?.();
78
100
  state.cropImgRef?.showSuccessBorder?.();
79
101
 
@@ -83,14 +105,20 @@ export const captureCodeStrip = async () => {
83
105
  state.updateHandler?.({ status: 'processing_started' });
84
106
  state.docReadyResult = await docReady();
85
107
  state.updateHandler?.({ status: 'processing_completed' });
86
- state.resultSetter?.({ ...state.docReadyResult, image: state.finalImage });
108
+ const finalized = await fireResultSetter({ ...state.docReadyResult, image: state.finalImage });
109
+ if (finalized) {
110
+ state.updateHandler?.({ status: 'capture_completed' });
111
+ removeOverlay();
112
+ return state.finalImage;
113
+ }
87
114
  } catch (error) {
88
115
  console.error("Error processing document:", error);
89
116
  }
90
117
 
91
- state.submitButton = createSubmitButton(state.videoOverlay.overlay, () => {
118
+ state.submitButton = createSubmitButton(state.videoOverlay.overlay, async () => {
92
119
  if (typeof state.customSubmitHandler === 'function') {
93
- state.customSubmitHandler(state.finalImage);
120
+ const packageInfo = await ensurePackageUploaded();
121
+ state.customSubmitHandler(state.finalImage, packageInfo);
94
122
  } else {
95
123
  handleSubmit(state.finalImage);
96
124
  }
package/core/constants.js CHANGED
@@ -5,6 +5,7 @@ export const WASM = {
5
5
  export const API_URLS = {
6
6
  VALIDATE_PARTNER: 'https://lens.veryfi.com/rest/validate_partner',
7
7
  SEND_CROPPED: 'https://lens.veryfi.com/rest/send-cropped',
8
+ SEND_PACKAGE: 'https://lens.veryfi.com/rest/send-package',
8
9
  PROCESS_DOCUMENT: 'https://lens.veryfi.com/api/document',
9
10
  BLUEPRINTS: 'https://lens.veryfi.com/api/blueprints',
10
11
  FINGERPRINT: 'https://fp.veryfi.com',
@@ -12,7 +13,7 @@ export const API_URLS = {
12
13
  };
13
14
 
14
15
  export const DEFAULTS = {
15
- SDK_VERSION: '3.0.5',
16
+ SDK_VERSION: '3.1.1',
16
17
  BLUR_THRESHOLD: 0.7,
17
18
  LCD_THRESHOLD: 0.5,
18
19
  BOX_COLOR: 'rgba(84, 192, 139, 0.6)',
package/core/lifecycle.js CHANGED
@@ -2,7 +2,7 @@ import { state, refs, resetState, actions } from './state.js';
2
2
  import { getVideo, displayVideo } from './camera.js';
3
3
  import { initializeWasmWrapper, sendWasm, sendCardWasm, getLongImage, logDocument, stopWasm, resetAutoCaptureCounters } from './wasm-processing.js';
4
4
  import { hideVideoOverlay } from './overlay-controls.js';
5
- import { docReady, handleSubmit } from './api.js';
5
+ import { docReady, handleSubmit, fireResultSetter } from './api.js';
6
6
  import { createSubmitButton } from '../components/submitButton.js';
7
7
  import { createLongPreview } from '../components/longPreview.js';
8
8
  import { createCheckOverlay } from '../components/checkOverlay.js';
@@ -171,7 +171,12 @@ export const stopStitching = async () => {
171
171
  hideVideoOverlay();
172
172
  state.resultSetter?.(state.finalImage);
173
173
  state.docReadyResult = await docReady();
174
- state.resultSetter?.({ ...state.docReadyResult, image: state.finalImage });
174
+ const finalized = await fireResultSetter({ ...state.docReadyResult, image: state.finalImage });
175
+ if (finalized) {
176
+ state.updateHandler?.({ status: 'stitching_completed' });
177
+ removeOverlay();
178
+ return;
179
+ }
175
180
  state.submitButton = createSubmitButton(state.videoOverlay.overlay, () => {
176
181
  handleSubmit(state.finalImage);
177
182
  }, () => actions.removeOverlay());
package/core/state.js CHANGED
@@ -124,6 +124,9 @@ export const state = {
124
124
  anydocPages: [],
125
125
  anydocMaxPages: null,
126
126
  anydocPageControls: null,
127
+ packageScans: [],
128
+ packageFilename: null,
129
+ packageUploadId: null,
127
130
  anydocAddPageText: MESSAGES.ANYDOC_ADD_PAGE,
128
131
  anydocSubmitText: MESSAGES.ANYDOC_SUBMIT,
129
132
  anydocShowFlipMessage: false,
@@ -250,6 +253,8 @@ export const state = {
250
253
 
251
254
  customData: {},
252
255
 
256
+ packageMode: false,
257
+
253
258
  enforceFingerprint: false,
254
259
  disableFingerprint: false,
255
260
 
@@ -326,6 +331,9 @@ export const resetState = () => {
326
331
  state.blueprintsData = null;
327
332
  state.anydocPages = [];
328
333
  state.anydocPageControls = null;
334
+ state.packageScans = [];
335
+ state.packageFilename = null;
336
+ state.packageUploadId = null;
329
337
 
330
338
  if (state.usingOverlayContainer) {
331
339
  state.container = null;
@@ -1,6 +1,7 @@
1
1
  import { state, refs, actions } from './state.js';
2
2
  import { hideVideoOverlay, showVideoOverlay } from './overlay-controls.js';
3
3
  import { toggleTorchLight, mirrorVideo, switchCamera } from './camera.js';
4
+ import { ensurePackageUploaded } from './api.js';
4
5
  import { createVideo } from '../components/video.js';
5
6
  import { createBlurModal } from '../components/blurModal.js';
6
7
  import { createDocumentModal } from '../components/documentModal.js';
@@ -88,7 +89,7 @@ const createSpecialtyOverlayOnReady = (overlayKey, createFn, options) => {
88
89
  };
89
90
 
90
91
  const getModalSubmitHandler = () => {
91
- return () => {
92
+ return async () => {
92
93
  hideVideoOverlay();
93
94
  if (state.lensFlavor === 'anydocs') {
94
95
  actions.handleAnydocPageCapture();
@@ -99,7 +100,8 @@ const getModalSubmitHandler = () => {
99
100
  actions.handleChecksBackAccepted?.();
100
101
  }
101
102
  } else if (typeof state.customSubmitHandler === 'function') {
102
- state.customSubmitHandler(state.finalImage);
103
+ const packageInfo = await ensurePackageUploaded();
104
+ state.customSubmitHandler(state.finalImage, packageInfo);
103
105
  } else {
104
106
  state.submitHandler?.(state.finalImage);
105
107
  }
package/flavors/upload.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { createFileUpload } from '../components/createFileUpload.js';
2
2
  import { createExitButton } from '../components/exitButton.js';
3
3
  import { captureUploaded } from '../core/capture/document.js';
4
- import { handleSubmit } from '../core/api.js';
4
+ import { docReady, handleSubmit, ensurePackageUploaded, fireResultSetter } from '../core/api.js';
5
5
  import { removeOverlay } from '../core/lifecycle.js';
6
6
 
7
7
  export const setup = async (state, refs, actions) => {
@@ -9,17 +9,37 @@ export const setup = async (state, refs, actions) => {
9
9
 
10
10
  export const render = async (state, refs, actions) => {
11
11
  if (!state.fileUpload) {
12
- state.fileUpload = createFileUpload(state.container, captureUploaded, (result) => {
12
+ state.fileUpload = createFileUpload(state.container, captureUploaded, async (result) => {
13
13
  const formattedResult = typeof result === 'string'
14
14
  ? { image: result, doc_type: 'receipt' }
15
15
  : result;
16
16
 
17
17
  if (!state.enableSubmit) {
18
18
  state.debug_mode && console.log("Submit disabled, handling image directly");
19
- state.resultSetter?.(formattedResult);
19
+ if (state.packageMode) {
20
+ // packageMode + !enableSubmit: upload the cumulative zip on capture so
21
+ // the showCamera setter receives bucket/package_path alongside image.
22
+ state.finalImage = formattedResult.image;
23
+ try {
24
+ state.docReadyResult = await docReady();
25
+ } catch (error) {
26
+ state.debug_mode && console.error('docReady failed for gallery upload:', error);
27
+ }
28
+ }
29
+ const finalized = await fireResultSetter(formattedResult);
30
+ if (finalized) {
31
+ removeOverlay();
32
+ }
20
33
  } else {
34
+ state.finalImage = formattedResult.image;
35
+ try {
36
+ state.docReadyResult = await docReady();
37
+ } catch (error) {
38
+ state.debug_mode && console.error('docReady failed for gallery upload:', error);
39
+ }
21
40
  if (typeof state.customSubmitHandler === 'function') {
22
- state.customSubmitHandler(formattedResult.image);
41
+ const packageInfo = await ensurePackageUploaded();
42
+ state.customSubmitHandler(formattedResult.image, packageInfo);
23
43
  } else {
24
44
  handleSubmit(formattedResult.image);
25
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "veryfi-lens-wasm",
3
- "version": "3.0.5",
3
+ "version": "3.1.1",
4
4
  "description": "Lens For Web SDK with WASM",
5
5
  "main": "veryfi-lens.js",
6
6
  "type": "module",
@@ -33,8 +33,8 @@ async function gatherBrowserData () {
33
33
  };
34
34
 
35
35
  return {
36
- timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
37
- tzOffset: new Date().getTimezoneOffset(),
36
+ timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
37
+ timezone_offset: new Date().getTimezoneOffset(),
38
38
  screenSize: `${window.screen.width}x${window.screen.height}x${window.screen.colorDepth}`,
39
39
  isCookiesEnabled: navigator.cookieEnabled,
40
40
  domLocalStorage: typeof window.localStorage !== 'undefined',
package/veryfi-lens.js CHANGED
@@ -74,6 +74,7 @@ const VeryfiLens = {
74
74
 
75
75
  state.enforceFingerprint = config.enforceFingerprint ?? false;
76
76
  state.disableFingerprint = config.disableFingerprint ?? false;
77
+ state.packageMode = config.packageMode ?? true;
77
78
 
78
79
  } catch (error) {
79
80
  console.error("Error initializing Lens:", error);