@velion-la/onboarding-sdk-react-native 1.0.0-beta.3 → 1.0.0-beta.5

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 (50) hide show
  1. package/app.plugin.js +101 -0
  2. package/lib/commonjs/camera/LivenessCamera.js +85 -6
  3. package/lib/commonjs/camera/LivenessCamera.js.map +1 -1
  4. package/lib/commonjs/core/VelionOnboarding.js +17 -3
  5. package/lib/commonjs/core/VelionOnboarding.js.map +1 -1
  6. package/lib/commonjs/steps/DocumentsOcrStep.js +8 -4
  7. package/lib/commonjs/steps/DocumentsOcrStep.js.map +1 -1
  8. package/lib/commonjs/steps/LivenessMatchStep.js +435 -11
  9. package/lib/commonjs/steps/LivenessMatchStep.js.map +1 -1
  10. package/lib/commonjs/types/index.js +16 -0
  11. package/lib/commonjs/types/index.js.map +1 -1
  12. package/lib/commonjs/utils/googleMapsPreflight.js +20 -9
  13. package/lib/commonjs/utils/googleMapsPreflight.js.map +1 -1
  14. package/lib/commonjs/utils/velionExtra.js +48 -0
  15. package/lib/commonjs/utils/velionExtra.js.map +1 -0
  16. package/lib/module/camera/LivenessCamera.js +86 -7
  17. package/lib/module/camera/LivenessCamera.js.map +1 -1
  18. package/lib/module/core/VelionOnboarding.js +17 -3
  19. package/lib/module/core/VelionOnboarding.js.map +1 -1
  20. package/lib/module/steps/DocumentsOcrStep.js +9 -5
  21. package/lib/module/steps/DocumentsOcrStep.js.map +1 -1
  22. package/lib/module/steps/LivenessMatchStep.js +434 -11
  23. package/lib/module/steps/LivenessMatchStep.js.map +1 -1
  24. package/lib/module/types/index.js +19 -0
  25. package/lib/module/types/index.js.map +1 -1
  26. package/lib/module/utils/googleMapsPreflight.js +20 -9
  27. package/lib/module/utils/googleMapsPreflight.js.map +1 -1
  28. package/lib/module/utils/velionExtra.js +44 -0
  29. package/lib/module/utils/velionExtra.js.map +1 -0
  30. package/lib/typescript/camera/LivenessCamera.d.ts +18 -1
  31. package/lib/typescript/camera/LivenessCamera.d.ts.map +1 -1
  32. package/lib/typescript/core/VelionOnboarding.d.ts.map +1 -1
  33. package/lib/typescript/steps/LivenessMatchStep.d.ts.map +1 -1
  34. package/lib/typescript/steps/types.d.ts +7 -1
  35. package/lib/typescript/steps/types.d.ts.map +1 -1
  36. package/lib/typescript/types/index.d.ts +57 -0
  37. package/lib/typescript/types/index.d.ts.map +1 -1
  38. package/lib/typescript/utils/googleMapsPreflight.d.ts +15 -7
  39. package/lib/typescript/utils/googleMapsPreflight.d.ts.map +1 -1
  40. package/lib/typescript/utils/velionExtra.d.ts +24 -0
  41. package/lib/typescript/utils/velionExtra.d.ts.map +1 -0
  42. package/package.json +9 -2
  43. package/src/camera/LivenessCamera.tsx +100 -3
  44. package/src/core/VelionOnboarding.ts +18 -3
  45. package/src/steps/DocumentsOcrStep.tsx +7 -3
  46. package/src/steps/LivenessMatchStep.tsx +449 -11
  47. package/src/steps/types.ts +7 -1
  48. package/src/types/index.ts +61 -0
  49. package/src/utils/googleMapsPreflight.ts +27 -10
  50. package/src/utils/velionExtra.ts +46 -0
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.LivenessMatchStep = void 0;
7
7
  var _react = require("react");
8
8
  var _reactNative = require("react-native");
9
+ var _expoFileSystem = require("expo-file-system");
9
10
  var ImageManipulator = _interopRequireWildcard(require("expo-image-manipulator"));
10
11
  var _types = require("../types");
11
12
  var _Banner = require("../ui/primitives/Banner");
@@ -39,6 +40,13 @@ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r
39
40
  * 7. SESSION_COMPLETE → POST /liveness-verify → resolve the step.
40
41
  */
41
42
 
43
+ // `expo-file-system` v19's `File` class wraps a `file://` path with a Blob-
44
+ // compatible API (`arrayBuffer`, `size`, `delete`). We read the recorded mp4
45
+ // to bytes in JS and PUT them with the global `fetch`. For the typical
46
+ // 1–5 MB session video this is well within the bridge budget; if the
47
+ // recorded files grow significantly we can switch to a streaming uploader
48
+ // without changing the call sites.
49
+
42
50
  // Capture loop defaults tuned for the Iris orchestrator's frame cadence.
43
51
  const FRAME_MS_DEFAULT = 350;
44
52
  const FRAME_MS_TURN = 220;
@@ -52,6 +60,30 @@ const PROGRESS_WATCHDOG_MS = 25_000;
52
60
  const LIVENESS_VERIFY_TIMEOUT_MS = 20_000;
53
61
  const FRAME_MAX_LONG_EDGE = 720;
54
62
  const FRAME_QUALITY = 0.55;
63
+ /**
64
+ * Hard ceiling for the video upload PUT to GCS that runs between SESSION_COMPLETE
65
+ * and POST /liveness-verify. Long enough for a 5 MB upload on a slow mobile link
66
+ * but short enough that a stalled connection can't keep the user on the
67
+ * analyzing overlay indefinitely. On timeout we fall back to verify-without-body.
68
+ */
69
+ /**
70
+ * Hard ceiling for the video upload PUT to GCS that runs between
71
+ * SESSION_COMPLETE and POST /liveness-verify. The signed URL TTL is 10 min,
72
+ * so 60 s leaves comfortable headroom for a typical 1–5 MB session video
73
+ * over a slow mobile uplink. On timeout we fall back to verify-without-body.
74
+ */
75
+ const VIDEO_UPLOAD_TIMEOUT_MS = 60_000;
76
+ // Upper bound for vision-camera to deliver `onRecordingFinished` after
77
+ // `stopRecording()` resolves. Observed ~50–200 ms in practice; we set the
78
+ // cap generously so a slow flush on older Androids doesn't strand the
79
+ // failure path waiting for a signal that's just running late.
80
+ const RECORDING_FINISH_TIMEOUT_MS = 3_000;
81
+ const VIDEO_CONTENT_TYPE = 'video/mp4';
82
+
83
+ /** Returns the first 80 chars of a URL — enough to spot the bucket without dumping the V4 signature. */
84
+ function truncateUrl(url) {
85
+ return url.length <= 80 ? url : `${url.slice(0, 80)}…`;
86
+ }
55
87
  class LivenessMatchStep {
56
88
  destroyed = false;
57
89
  constructor(deps) {
@@ -68,6 +100,7 @@ class LivenessMatchStep {
68
100
  calibrationLabel: this.deps.livenessCalibrationLabel,
69
101
  showIntro: true,
70
102
  showBackButton: true,
103
+ onVideoUploaded: this.deps.onLivenessVideoUploaded,
71
104
  onComplete: () => {
72
105
  if (this.destroyed) return;
73
106
  this.deps.ui.setNode(null);
@@ -94,6 +127,7 @@ function LivenessMatchView({
94
127
  calibrationLabel,
95
128
  showIntro,
96
129
  showBackButton,
130
+ onVideoUploaded,
97
131
  onComplete,
98
132
  onCancel
99
133
  }) {
@@ -134,6 +168,38 @@ function LivenessMatchView({
134
168
  // warnings flash in <2s and the user couldn't read them).
135
169
  const activeToastRef = (0, _react.useRef)(null);
136
170
 
171
+ // ── Video recording state (OSDKR-4) ─────────────────────────────────────
172
+ // `videoUploadRef` holds the signed URL returned by /liveness-init when the
173
+ // backend enabled video capture for this session. `null` → skip recording.
174
+ const videoUploadRef = (0, _react.useRef)(null);
175
+ // Current state of the video pipeline. Best-effort: any value other than
176
+ // `'uploaded'` ends up calling /liveness-verify WITHOUT a body — liveness
177
+ // still completes normally.
178
+
179
+ const videoOutcomeRef = (0, _react.useRef)('none');
180
+ // Filled in when the GCS PUT returns 200; consumed by the verify call.
181
+ const videoMetaRef = (0, _react.useRef)(null);
182
+ // Resolves when the upload (success or failure path) is fully settled. The
183
+ // SESSION_COMPLETE handler awaits this with a bounded timeout before calling
184
+ // /liveness-verify so the metadata, if any, makes it into the body.
185
+ const videoUploadPromiseRef = (0, _react.useRef)(null);
186
+ // Resolves as soon as vision-camera fires `onRecordingFinished` (i.e. when
187
+ // the mp4 file is fully written and `uploadRecordedVideo` has been kicked
188
+ // off — fetch is in flight but not yet complete). `failWith` awaits this with
189
+ // a short timeout so the upload's underlying fetch is *started* before the
190
+ // step is torn down. After the unmount the fetch closure keeps its own refs
191
+ // alive and the PUT to GCS completes in the background.
192
+ const videoRecordingFinishedPromiseRef = (0, _react.useRef)(null);
193
+ // Local file system path of the temp mp4. Tracked so the unmount cleanup
194
+ // can delete it if the user backs out before the upload finished.
195
+ const videoTempPathRef = (0, _react.useRef)(null);
196
+ // Cached so `failWith` / unmount can invoke the callback with the prop
197
+ // captured at start, even if the host re-renders.
198
+ const onVideoUploadedRef = (0, _react.useRef)(onVideoUploaded);
199
+ (0, _react.useEffect)(() => {
200
+ onVideoUploadedRef.current = onVideoUploaded;
201
+ }, [onVideoUploaded]);
202
+
137
203
  // Each toast stays on screen at least this long. The orchestrator can spam
138
204
  // WARNINGs (POSITIONING_REQUIRED, SPOOF_RISK_HIGH, …) several times per
139
205
  // second; without a floor the user literally cannot read them.
@@ -180,17 +246,275 @@ function LivenessMatchView({
180
246
  }
181
247
  inFlightRef.current = false;
182
248
  }, []);
249
+
250
+ // ── Video upload pipeline (OSDKR-4) ─────────────────────────────────────
251
+ // Everything below is best-effort: never throws into the caller; never
252
+ // blocks liveness completion. The worst case is `videoOutcomeRef = 'failed'`
253
+ // and verify being called without a body.
254
+ const deleteVideoTemp = (0, _react.useCallback)(path => {
255
+ if (!path) return;
256
+ try {
257
+ // `File.delete()` is synchronous in expo-file-system v19.
258
+ new _expoFileSystem.File(path).delete();
259
+ } catch {
260
+ // Best-effort cleanup; we can't do much if rm fails.
261
+ }
262
+ }, []);
263
+ const uploadRecordedVideo = (0, _react.useCallback)(async video => {
264
+ const target = videoUploadRef.current;
265
+ videoTempPathRef.current = video.path;
266
+ if (!target) {
267
+ _log.log.warn('[Liveness] video upload skipped: no signed URL in init response');
268
+ videoOutcomeRef.current = 'failed';
269
+ deleteVideoTemp(video.path);
270
+ videoTempPathRef.current = null;
271
+ return;
272
+ }
273
+ const durationMs = typeof video.duration === 'number' && isFinite(video.duration) ? Math.round(video.duration * 1000) : null;
274
+ let sizeBytes = null;
275
+ let file = null;
276
+ try {
277
+ file = new _expoFileSystem.File(video.path);
278
+ sizeBytes = typeof file.size === 'number' ? file.size : null;
279
+ } catch (err) {
280
+ _log.log.warn('[Liveness] could not stat video file before upload', err);
281
+ }
282
+ _log.log.info('[Liveness] video upload starting', {
283
+ url: truncateUrl(target.upload_url),
284
+ method: target.method,
285
+ contentType: VIDEO_CONTENT_TYPE,
286
+ path: video.path,
287
+ sizeBytes,
288
+ durationMs
289
+ });
290
+ const t0 = Date.now();
291
+ try {
292
+ // Build a NATIVE React Native Blob from the `file://` URI: fetching
293
+ // the local path returns a Response whose `.blob()` is the BlobModule
294
+ // implementation that the bridge actually knows how to upload. Using
295
+ // the expo-file-system `File` directly silently sends an empty body
296
+ // (GCS responds 200 with a 0-byte object — exactly what we hit
297
+ // before). Reading the bytes through the native Blob keeps the data
298
+ // on the native side and uploads streamed from disk.
299
+ const fileUri = video.path.startsWith('file://') ? video.path : `file://${video.path}`;
300
+ const nativeBlob = await (await fetch(fileUri)).blob();
301
+ _log.log.info('[Liveness] video upload body prepared', {
302
+ blobSize: nativeBlob.size,
303
+ blobType: nativeBlob.type
304
+ });
305
+ const result = await fetch(target.upload_url, {
306
+ method: 'PUT',
307
+ headers: {
308
+ 'Content-Type': VIDEO_CONTENT_TYPE
309
+ },
310
+ body: nativeBlob
311
+ });
312
+ const elapsedMs = Date.now() - t0;
313
+ if (result.ok) {
314
+ _log.log.info('[Liveness] video upload ok', {
315
+ status: result.status,
316
+ elapsedMs,
317
+ sizeBytes
318
+ });
319
+ videoOutcomeRef.current = 'uploaded';
320
+ videoMetaRef.current = {
321
+ content_type: VIDEO_CONTENT_TYPE,
322
+ duration_ms: durationMs
323
+ };
324
+ // Invoke the host callback BEFORE cleanup so the path stays valid
325
+ // for any synchronous host work (rare, but cheap to guarantee).
326
+ try {
327
+ onVideoUploadedRef.current?.({
328
+ upload_url: target.upload_url,
329
+ content_type: VIDEO_CONTENT_TYPE,
330
+ duration_ms: durationMs,
331
+ size_bytes: sizeBytes
332
+ });
333
+ } catch (err) {
334
+ _log.log.warn('[Liveness] onLivenessVideoUploaded threw', err);
335
+ }
336
+ } else {
337
+ // Try to extract the GCS error XML — its body tells us exactly which
338
+ // permission / signature param failed (otherwise we are blind).
339
+ let responseSnippet = '';
340
+ try {
341
+ responseSnippet = (await result.text()).slice(0, 400);
342
+ } catch {
343
+ /* body not readable; status alone is the signal */
344
+ }
345
+ _log.log.warn('[Liveness] video upload non-200', {
346
+ status: result.status,
347
+ elapsedMs,
348
+ sizeBytes,
349
+ body: responseSnippet
350
+ });
351
+ videoOutcomeRef.current = 'failed';
352
+ }
353
+ } catch (err) {
354
+ _log.log.warn('[Liveness] video upload error', {
355
+ elapsedMs: Date.now() - t0,
356
+ sizeBytes,
357
+ err
358
+ });
359
+ videoOutcomeRef.current = 'failed';
360
+ } finally {
361
+ deleteVideoTemp(video.path);
362
+ videoTempPathRef.current = null;
363
+ }
364
+ }, [deleteVideoTemp]);
365
+ const startVideoRecordingIfEnabled = (0, _react.useCallback)(() => {
366
+ if (!videoUploadRef.current) {
367
+ _log.log.info('[Liveness] video skipped: no video_upload in liveness-init');
368
+ return;
369
+ }
370
+ const camera = cameraRef.current;
371
+ if (!camera) {
372
+ _log.log.warn('[Liveness] video skipped: LivenessCamera ref not ready at SESSION_READY');
373
+ return;
374
+ }
375
+ _log.log.info('[Liveness] video recording starting', {
376
+ uploadUrl: truncateUrl(videoUploadRef.current.upload_url),
377
+ expiresAt: videoUploadRef.current.expires_at
378
+ });
379
+ // Pre-assign the upload promise BEFORE calling startRecording. vision-camera
380
+ // dispatches `onRecordingFinished` on a separate microtask from the
381
+ // `stopRecording()` resolution, so a "set the promise inside the callback"
382
+ // pattern races SESSION_COMPLETE — the verify POST fires before
383
+ // `uploadRecordedVideo` is even invoked, and the body ends up empty.
384
+ // The Deferred resolves once the recording's success/error path is fully
385
+ // settled (including the upload PUT and the cleanup).
386
+ let settle = () => {};
387
+ let settled = false;
388
+ videoUploadPromiseRef.current = new Promise(resolve => {
389
+ settle = () => {
390
+ if (settled) return;
391
+ settled = true;
392
+ resolve();
393
+ };
394
+ });
395
+ let signalRecordingFinished = () => {};
396
+ let recordingFinishedSettled = false;
397
+ videoRecordingFinishedPromiseRef.current = new Promise(resolve => {
398
+ signalRecordingFinished = () => {
399
+ if (recordingFinishedSettled) return;
400
+ recordingFinishedSettled = true;
401
+ resolve();
402
+ };
403
+ });
404
+ try {
405
+ camera.startVideoRecording({
406
+ onRecordingFinished: video => {
407
+ _log.log.info('[Liveness] video recording finished', {
408
+ path: video.path,
409
+ durationSec: video.duration
410
+ });
411
+ // Signal `failWith` (and anyone else waiting for the file to be
412
+ // ready) BEFORE awaiting the upload. The fetch closure keeps the
413
+ // upload alive past any unmount, so it's safe to let the component
414
+ // tear down once the recording-finished signal has fired.
415
+ uploadRecordedVideo(video).finally(() => {
416
+ settle();
417
+ });
418
+ signalRecordingFinished();
419
+ },
420
+ onRecordingError: err => {
421
+ _log.log.warn('[Liveness] video recording error', err);
422
+ videoOutcomeRef.current = 'failed';
423
+ signalRecordingFinished();
424
+ settle();
425
+ }
426
+ });
427
+ _log.log.info('[Liveness] video recording started');
428
+ videoOutcomeRef.current = 'recording';
429
+ } catch (err) {
430
+ _log.log.warn('[Liveness] failed to start video recording', err);
431
+ videoOutcomeRef.current = 'failed';
432
+ // No callback will ever fire → unblock anyone awaiting either Deferred.
433
+ signalRecordingFinished();
434
+ settle();
435
+ }
436
+ }, [uploadRecordedVideo]);
437
+ const stopVideoRecordingIfActive = (0, _react.useCallback)(async () => {
438
+ if (videoOutcomeRef.current !== 'recording') {
439
+ _log.log.info('[Liveness] video stop skipped', {
440
+ outcome: videoOutcomeRef.current
441
+ });
442
+ return;
443
+ }
444
+ const camera = cameraRef.current;
445
+ if (!camera) {
446
+ _log.log.warn('[Liveness] video stop skipped: LivenessCamera ref gone');
447
+ return;
448
+ }
449
+ _log.log.info('[Liveness] video recording stopping');
450
+ try {
451
+ // Returns when vision-camera flushed the file; the upload Deferred
452
+ // (set in startVideoRecordingIfEnabled) is what actually tells us when
453
+ // the PUT + cleanup are done — the SESSION_COMPLETE handler awaits it.
454
+ await camera.stopVideoRecording();
455
+ _log.log.info('[Liveness] video recording stop resolved (onRecordingFinished will follow)');
456
+ } catch (err) {
457
+ _log.log.warn('[Liveness] failed to stop video recording', err);
458
+ videoOutcomeRef.current = 'failed';
459
+ }
460
+ }, []);
183
461
  const failWith = (0, _react.useCallback)(err => {
184
462
  stopWatchdog();
185
463
  stopCapture();
186
- try {
187
- wsRef.current?.close(1000, 'fail');
188
- } catch {
189
- /* ignore */
464
+ // OSDKR-4 requirement: the video must upload on EVERY outcome,
465
+ // including LIVENESS_FAILED, so back-office reviewers can audit
466
+ // what happened in declined sessions. To make that work we need
467
+ // `onRecordingFinished` to fire *before* the component unmounts —
468
+ // once the file is flushed and `uploadRecordedVideo` has kicked
469
+ // off, the fetch closure carries the upload through to completion
470
+ // on its own, no matter how quickly the UI is torn down.
471
+ //
472
+ // We don't wait for the GCS PUT to complete (that can take
473
+ // seconds and the user shouldn't sit on the error screen for it),
474
+ // just for the recording-finished signal.
475
+ const recordingWasActive = videoOutcomeRef.current === 'recording';
476
+ const finish = () => {
477
+ if (videoTempPathRef.current && !recordingWasActive) {
478
+ // Only delete the temp file when there was no recording in
479
+ // flight; otherwise `uploadRecordedVideo` is responsible for
480
+ // deleting it after the PUT settles.
481
+ const path = videoTempPathRef.current;
482
+ videoTempPathRef.current = null;
483
+ void deleteVideoTemp(path);
484
+ }
485
+ try {
486
+ wsRef.current?.close(1000, 'fail');
487
+ } catch {
488
+ /* ignore */
489
+ }
490
+ wsRef.current = null;
491
+ onCancel(err);
492
+ };
493
+ if (!recordingWasActive) {
494
+ finish();
495
+ return;
190
496
  }
191
- wsRef.current = null;
192
- onCancel(err);
193
- }, [onCancel, stopCapture, stopWatchdog]);
497
+ const finishedPromise = videoRecordingFinishedPromiseRef.current;
498
+ void (async () => {
499
+ await stopVideoRecordingIfActive();
500
+ if (finishedPromise) {
501
+ // Bound the wait so a misbehaving recorder can't strand the
502
+ // user on the camera screen forever. `RECORDING_FINISH_TIMEOUT_MS`
503
+ // is conservative — vision-camera typically lands the callback
504
+ // tens of milliseconds after stopRecording resolves.
505
+ let finishTimer = null;
506
+ const timeoutPromise = new Promise(resolve => {
507
+ finishTimer = setTimeout(() => {
508
+ _log.log.warn('[Liveness] recording-finished signal timed out — proceeding to fail without ensuring the upload kicked off');
509
+ resolve();
510
+ }, RECORDING_FINISH_TIMEOUT_MS);
511
+ });
512
+ await Promise.race([finishedPromise, timeoutPromise]);
513
+ if (finishTimer) clearTimeout(finishTimer);
514
+ }
515
+ finish();
516
+ })();
517
+ }, [deleteVideoTemp, onCancel, stopCapture, stopVideoRecordingIfActive, stopWatchdog]);
194
518
  const captureAndSend = (0, _react.useCallback)(async () => {
195
519
  if (inFlightRef.current) return;
196
520
  const ws = wsRef.current;
@@ -199,10 +523,18 @@ function LivenessMatchView({
199
523
  try {
200
524
  const photo = await cameraRef.current?.takeFrame();
201
525
  if (!photo || !ws.isOpen()) return;
526
+ // Front-camera frames come back mirrored (vision-camera's default for
527
+ // front cam, which we keep so the recorded mp4 has a correct rotation
528
+ // matrix — see the `isMirrored` comment in LivenessCamera). For Iris,
529
+ // we want the un-mirrored yaw so TURN_LEFT/RIGHT challenges map to
530
+ // real-world directions, so we flip the JPEG horizontally here before
531
+ // signing and sending.
202
532
  const compressed = await ImageManipulator.manipulateAsync(photo.path.startsWith('file://') ? photo.path : `file://${photo.path}`, [{
203
533
  resize: {
204
534
  width: FRAME_MAX_LONG_EDGE
205
535
  }
536
+ }, {
537
+ flip: ImageManipulator.FlipType.Horizontal
206
538
  }], {
207
539
  compress: FRAME_QUALITY,
208
540
  format: ImageManipulator.SaveFormat.JPEG
@@ -301,6 +633,10 @@ function LivenessMatchView({
301
633
  setChallengeText(t.liveness.centerFace);
302
634
  showToast('success', t.liveness.getReady);
303
635
  startCaptureLoop(FRAME_MS_DEFAULT);
636
+ // Start the local mp4 recording in parallel with the frame loop, if
637
+ // the backend opted in via `video_upload` in /liveness-init. Pure
638
+ // best-effort: any failure here is logged and never blocks liveness.
639
+ startVideoRecordingIfEnabled();
304
640
  resetWatchdog();
305
641
  return;
306
642
  }
@@ -349,10 +685,26 @@ function LivenessMatchView({
349
685
  // can take several seconds to land while the worker drains
350
686
  // its queue; without this the user would stare at the camera
351
687
  // and the stale instruction the whole time.
688
+ //
689
+ // CRITICAL: the video recording MUST be stopped before the phase
690
+ // change. Switching phase unmounts <LivenessCamera>, which makes
691
+ // vision-camera tear down the recording mid-flush — the
692
+ // `onRecordingFinished` callback never fires and the upload
693
+ // promise stays pending until the 60 s timeout. We stop first,
694
+ // then unmount.
352
695
  const meta = lastChallengeMetaRef.current;
353
696
  if (meta && meta.index >= meta.total - 1) {
697
+ _log.log.info('[Liveness] last challenge complete — preparing to finalize', {
698
+ index: meta.index,
699
+ total: meta.total,
700
+ videoOutcome: videoOutcomeRef.current
701
+ });
354
702
  stopCapture();
355
- setPhase('finalizing');
703
+ (async () => {
704
+ await stopVideoRecordingIfActive();
705
+ _log.log.info('[Liveness] last challenge: switching phase to finalizing');
706
+ setPhase('finalizing');
707
+ })();
356
708
  }
357
709
  resetWatchdog();
358
710
  return;
@@ -452,8 +804,46 @@ function LivenessMatchView({
452
804
  /* ignore */
453
805
  }
454
806
  wsRef.current = null;
455
- setPhase('finalizing');
807
+ // IMPORTANT: do NOT setPhase('finalizing') yet. Switching phase
808
+ // unmounts LivenessCamera, which cancels the in-flight video
809
+ // recording mid-flush and prevents `onRecordingFinished` from
810
+ // firing — the upload would never start. We stop the recording
811
+ // first (camera still mounted, ref valid), then transition phase
812
+ // and run the verify pipeline.
456
813
  (async () => {
814
+ // ── Settle the video pipeline before /liveness-verify ─────────
815
+ // Best-effort: stop the recording (if still running), then await
816
+ // the upload promise with a hard timeout. Whatever the outcome
817
+ // (uploaded / failed / timeout), we proceed to verify — the body
818
+ // is only attached when the upload actually succeeded.
819
+ await stopVideoRecordingIfActive();
820
+ // Safe to unmount LivenessCamera now: the recording has been
821
+ // flushed and `onRecordingFinished` has been scheduled.
822
+ setPhase('finalizing');
823
+ if (videoUploadPromiseRef.current) {
824
+ _log.log.info('[Liveness] awaiting video upload settle', {
825
+ timeoutMs: VIDEO_UPLOAD_TIMEOUT_MS
826
+ });
827
+ const uploadPromise = videoUploadPromiseRef.current;
828
+ let uploadTimer = null;
829
+ const timeoutPromise = new Promise(resolve => {
830
+ uploadTimer = setTimeout(() => {
831
+ _log.log.warn('[Liveness] video upload timed out — verify will go without body');
832
+ videoOutcomeRef.current = 'failed';
833
+ resolve();
834
+ }, VIDEO_UPLOAD_TIMEOUT_MS);
835
+ });
836
+ await Promise.race([uploadPromise, timeoutPromise]);
837
+ if (uploadTimer) clearTimeout(uploadTimer);
838
+ }
839
+ const verifyBody = videoOutcomeRef.current === 'uploaded' && videoMetaRef.current ? {
840
+ video: videoMetaRef.current
841
+ } : {};
842
+ _log.log.info('[Liveness] POST /liveness-verify', {
843
+ outcome: videoOutcomeRef.current,
844
+ hasBody: 'video' in verifyBody
845
+ });
846
+
457
847
  // Bound the verify call with an explicit timeout. SESSION_COMPLETE
458
848
  // already turned the progress watchdog off, so if this POST hangs
459
849
  // (the dev network shows recurring HTTP/2 INTERNAL_ERROR resets)
@@ -463,7 +853,7 @@ function LivenessMatchView({
463
853
  const verifyController = new AbortController();
464
854
  const verifyTimeout = setTimeout(() => verifyController.abort(), LIVENESS_VERIFY_TIMEOUT_MS);
465
855
  try {
466
- await api.post(`/sessions/${sessionId}/liveness-verify`, {}, {
856
+ await api.post(`/sessions/${sessionId}/liveness-verify`, verifyBody, {
467
857
  signal: verifyController.signal
468
858
  });
469
859
  clearTimeout(verifyTimeout);
@@ -484,7 +874,7 @@ function LivenessMatchView({
484
874
  default:
485
875
  return;
486
876
  }
487
- }, [adjustFps, api, challengeLabel, failWith, onComplete, presentTerminalRetry, resetWatchdog, sessionId, showToast, startCaptureLoop, stopCapture, stopWatchdog, t.liveness.getReady, t.liveness.positioningReady, t.liveness.repositioning, t.liveness.warningConsecutiveMisses, t.liveness.warningGeneric]);
877
+ }, [adjustFps, api, challengeLabel, failWith, onComplete, presentTerminalRetry, resetWatchdog, sessionId, showToast, startCaptureLoop, startVideoRecordingIfEnabled, stopCapture, stopVideoRecordingIfActive, stopWatchdog, t.liveness.getReady, t.liveness.positioningReady, t.liveness.repositioning, t.liveness.warningConsecutiveMisses, t.liveness.warningGeneric]);
488
878
  const startVerification = (0, _react.useCallback)(async () => {
489
879
  setBannerError(null);
490
880
  setRetry(null);
@@ -529,6 +919,27 @@ function LivenessMatchView({
529
919
  hmacRef.current = null;
530
920
  }
531
921
  }
922
+ // Reset the video pipeline state for this attempt (the host may retry).
923
+ // We honour `video_upload` even when the host did not register the
924
+ // `onLivenessVideoUploaded` callback — the upload still happens, only
925
+ // the host notification is skipped. `outcome` stays `'none'` until
926
+ // `startVideoRecordingIfEnabled` confirms the recording actually started;
927
+ // see that callback for why we cannot trust `init.video_upload` alone.
928
+ videoUploadRef.current = init.video_upload ?? null;
929
+ videoOutcomeRef.current = 'none';
930
+ videoMetaRef.current = null;
931
+ videoUploadPromiseRef.current = null;
932
+ videoRecordingFinishedPromiseRef.current = null;
933
+ videoTempPathRef.current = null;
934
+ if (init.video_upload) {
935
+ _log.log.info('[Liveness] init.video_upload received', {
936
+ url: truncateUrl(init.video_upload.upload_url),
937
+ expiresAt: init.video_upload.expires_at,
938
+ method: init.video_upload.method
939
+ });
940
+ } else {
941
+ _log.log.info('[Liveness] init.video_upload absent — recording disabled for this session');
942
+ }
532
943
  setStatusText(t.liveness.preparingCamera);
533
944
  let connection;
534
945
  try {
@@ -606,6 +1017,19 @@ function LivenessMatchView({
606
1017
  stopCapture();
607
1018
  stopWatchdog();
608
1019
  if (toastTimerRef.current) clearTimeout(toastTimerRef.current);
1020
+ // Best-effort video pipeline shutdown on unmount. Stop the recording
1021
+ // if it's still running (vision-camera deletes its temp file when the
1022
+ // <Camera> tears down anyway, but we explicitly stop to flush). Then
1023
+ // delete any temp file we own, so a backed-out user doesn't leak
1024
+ // storage.
1025
+ if (videoOutcomeRef.current === 'recording') {
1026
+ void stopVideoRecordingIfActive();
1027
+ }
1028
+ const leftoverPath = videoTempPathRef.current;
1029
+ videoTempPathRef.current = null;
1030
+ if (leftoverPath) {
1031
+ void deleteVideoTemp(leftoverPath);
1032
+ }
609
1033
  try {
610
1034
  wsRef.current?.close(1000, 'unmount');
611
1035
  } catch {