biometry-react-components 3.0.0 → 3.0.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/dist/index.esm.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,useEffect,useMemo}from'react';const DocScan = ({ rectWidth = 640, rectHeight = 400, onConfirmCapture, onCapture, className, style, noShadow = false, }) => {
2
- var _a;
3
2
  const [isConfirming, setIsConfirming] = useState(false);
4
3
  const [capturedUrl, setCapturedUrl] = useState(null);
5
4
  const [capturedFile, setCapturedFile] = useState(null);
@@ -10,7 +9,6 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
10
9
  const [isFrontCamera, setIsFrontCamera] = useState(false);
11
10
  const [capabilities, setCapabilities] = useState({});
12
11
  const [torchOn, setTorchOn] = useState(false);
13
- const QUALITY_MULTIPLIER = 3;
14
12
  // Crop area (dashed area) as percentages
15
13
  const cropTop = 0.075, cropLeft = 0.075, cropWidth = 0.85, cropHeight = 0.85;
16
14
  const getDevices = async () => {
@@ -40,14 +38,14 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
40
38
  (async () => {
41
39
  try {
42
40
  const videoConstraints = {
43
- width: rectWidth,
44
- height: rectHeight,
41
+ width: { ideal: 1920 },
42
+ height: { ideal: 1080 },
45
43
  };
46
44
  if (selectedDeviceId) {
47
45
  videoConstraints.deviceId = { exact: selectedDeviceId };
48
46
  }
49
47
  else {
50
- videoConstraints.facingMode = "environment";
48
+ videoConstraints.facingMode = { ideal: "environment" };
51
49
  }
52
50
  const stream = await navigator.mediaDevices.getUserMedia({ video: videoConstraints });
53
51
  if (!isMounted) {
@@ -64,8 +62,13 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
64
62
  streamRef.current = stream;
65
63
  const track = stream.getVideoTracks()[0];
66
64
  if (track) {
67
- if (track.getCapabilities) {
68
- setCapabilities(track.getCapabilities());
65
+ if (typeof track.getCapabilities === "function") {
66
+ try {
67
+ setCapabilities(track.getCapabilities());
68
+ }
69
+ catch (_a) {
70
+ setCapabilities({});
71
+ }
69
72
  }
70
73
  const settings = track.getSettings();
71
74
  let isFront = false;
@@ -109,28 +112,30 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
109
112
  streamRef.current = null;
110
113
  }
111
114
  };
112
- }, [isConfirming, rectWidth, rectHeight, selectedDeviceId]);
115
+ }, [isConfirming, selectedDeviceId]);
113
116
  const handleCapture = () => {
114
117
  const video = videoRef.current;
115
118
  if (!video)
116
119
  return;
120
+ // Use native video stream dimensions for crisp, full-resolution capture
121
+ const vWidth = video.videoWidth || rectWidth;
122
+ const vHeight = video.videoHeight || rectHeight;
123
+ const targetAspect = rectWidth / rectHeight;
124
+ const videoAspect = vWidth / vHeight;
125
+ let sx = 0, sy = 0, sw = vWidth, sh = vHeight;
126
+ if (videoAspect > targetAspect) {
127
+ sw = vHeight * targetAspect;
128
+ sx = (vWidth - sw) / 2;
129
+ }
130
+ else {
131
+ sh = vWidth / targetAspect;
132
+ sy = (vHeight - sh) / 2;
133
+ }
117
134
  const canvas = document.createElement("canvas");
118
- canvas.width = rectWidth * QUALITY_MULTIPLIER;
119
- canvas.height = rectHeight * QUALITY_MULTIPLIER;
135
+ canvas.width = Math.round(sw);
136
+ canvas.height = Math.round(sh);
120
137
  const ctx = canvas.getContext("2d");
121
138
  if (ctx) {
122
- // Aspect-ratio crop to fill the output area
123
- const videoAspect = video.videoWidth / video.videoHeight;
124
- const targetAspect = rectWidth / rectHeight;
125
- let sx = 0, sy = 0, sw = video.videoWidth, sh = video.videoHeight;
126
- if (videoAspect > targetAspect) {
127
- sw = video.videoHeight * targetAspect;
128
- sx = (video.videoWidth - sw) / 2;
129
- }
130
- else {
131
- sh = video.videoWidth / targetAspect;
132
- sy = (video.videoHeight - sh) / 2;
133
- }
134
139
  ctx.drawImage(video, sx, sy, sw, sh, 0, 0, canvas.width, canvas.height);
135
140
  canvas.toBlob((blob) => {
136
141
  if (blob) {
@@ -188,7 +193,7 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
188
193
  setTorchOn(nextTorch);
189
194
  }
190
195
  catch (err) {
191
- console.error("Torch toggle failed:", err);
196
+ console.warn("Torch toggle failed:", err);
192
197
  }
193
198
  }
194
199
  };
@@ -196,8 +201,12 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
196
201
  if (!streamRef.current)
197
202
  return;
198
203
  const track = streamRef.current.getVideoTracks()[0];
199
- if (track && capabilities.focusMode) {
200
- const focusType = capabilities.focusMode.includes("single-shot") ? "single-shot" : capabilities.focusMode.includes("manual") ? "manual" : null;
204
+ if (track && capabilities.focusMode && Array.isArray(capabilities.focusMode)) {
205
+ const focusType = capabilities.focusMode.includes("single-shot")
206
+ ? "single-shot"
207
+ : capabilities.focusMode.includes("manual")
208
+ ? "manual"
209
+ : null;
201
210
  if (!focusType)
202
211
  return;
203
212
  const rect = e.currentTarget.getBoundingClientRect();
@@ -210,7 +219,7 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
210
219
  await track.applyConstraints({ advanced: [{ focusMode: focusType, pointsOfInterest: [{ x, y }] }] });
211
220
  }
212
221
  catch (err) {
213
- console.error("Focus failed:", err);
222
+ console.warn("Focus failed:", err);
214
223
  }
215
224
  }
216
225
  };
@@ -236,7 +245,7 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
236
245
  display: 'flex',
237
246
  alignItems: 'center',
238
247
  justifyContent: 'center',
239
- cursor: ((_a = capabilities.focusMode) === null || _a === void 0 ? void 0 : _a.includes("manual")) ? "crosshair" : "default"
248
+ cursor: Array.isArray(capabilities.focusMode) && capabilities.focusMode.includes("manual") ? "crosshair" : "default"
240
249
  };
241
250
  const defaultDashedArea = {
242
251
  position: 'absolute',
@@ -343,62 +352,192 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
343
352
  borderRadius: noShadow ? 0 : 8,
344
353
  pointerEvents: 'none'
345
354
  } }), jsx("div", { className: "docscan-dashed-area", style: defaultDashedArea, children: jsx("span", { className: "docscan-guidance-text", style: defaultGuidanceText, children: "Place your document here" }) })] }), jsx("button", { type: "button", className: "docscan-capture-btn", style: defaultCaptureButton, onClick: handleCapture, children: "Capture" })] })) : (jsxs(Fragment, { children: [jsx("div", { className: "docscan-preview", style: defaultPreview, children: capturedUrl && (jsx("img", { src: capturedUrl, alt: "Captured document", className: "docscan-preview-img", style: { maxWidth: '100%', maxHeight: '100%', borderRadius: 8 } })) }), jsx("div", { className: "docscan-confirm-text", style: defaultConfirmText, children: "Do you want to use this photo?" }), jsxs("div", { className: "docscan-confirm-btns", style: { display: 'flex', justifyContent: 'center', gap: 32, marginTop: 16, width: '100%' }, children: [jsx("button", { type: "button", "aria-label": "Retake", className: "docscan-icon-btn", style: defaultIconButton, onClick: handleDecline, onMouseOver: e => (e.currentTarget.style.background = 'rgba(211,47,47,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsx("path", { d: "M12 12L24 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" }), jsx("path", { d: "M24 12L12 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" })] }) }), jsx("button", { type: "button", "aria-label": "Confirm", className: "docscan-icon-btn", style: defaultIconButton, onClick: handleConfirm, onMouseOver: e => (e.currentTarget.style.background = 'rgba(56,142,60,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsx("path", { d: "M10 19L16 25L26 13", stroke: "#388e3c", strokeWidth: "3", strokeLinecap: "round", strokeLinejoin: "round" })] }) })] })] })) }));
355
+ };// Video constraints are IDEAL-ONLY - never `min` (BM-530).
356
+ //
357
+ // Without a deviceId, getUserMedia considers every camera and returns one that
358
+ // satisfies all required (min/max/exact) constraints. A hard `min` therefore
359
+ // filters cameras: with `width.min: 1280` a 640x480 laptop webcam is silently
360
+ // excluded and the browser falls back to whatever passes - typically a virtual
361
+ // camera (OBS) that accepts any resolution - or, with no such camera, throws
362
+ // OverconstrainedError, which used to surface as "Camera Permission Required".
363
+ // Chrome's camera picker for the site is narrowed the same way.
364
+ //
365
+ // The old minimums survive as floors on the ideal (`idealSize`): an HD camera
366
+ // is still steered to at least the same resolution as before, while a weaker
367
+ // camera is accepted at its best mode instead of being rejected.
368
+ const idealSize = (requested, floor, cap) => Math.max(floor, Math.min(requested, cap));
369
+ const usePermissions = ({ rectWidth = 1920, rectHeight = 1080, audio = true } = {}) => {
370
+ const [permission, setPermission] = useState(false);
371
+ const [stream, setStream] = useState(null);
372
+ const [isLoading, setIsLoading] = useState(true);
373
+ // The stream is mirrored in a ref so stopStreamTracks and the unmount
374
+ // cleanup act on the live stream rather than a stale closure, and so a
375
+ // getUserMedia that resolves after unmount can be stopped immediately
376
+ // (restored from 1.3.5, which 1.4.0 reverted).
377
+ const streamRef = useRef(null);
378
+ const isMountedRef = useRef(false);
379
+ const stopStreamTracks = () => {
380
+ const currentStream = streamRef.current;
381
+ if (currentStream) {
382
+ currentStream.getTracks().forEach(track => track.stop());
383
+ streamRef.current = null;
384
+ setStream(null);
385
+ }
386
+ };
387
+ const getAdaptiveConstraints = () => {
388
+ const isMobile = /Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
389
+ const isLowEndDevice = navigator.hardwareConcurrency && navigator.hardwareConcurrency <= 4;
390
+ const baseAudio = audio ? {
391
+ echoCancellation: true,
392
+ noiseSuppression: true,
393
+ autoGainControl: true,
394
+ channelCount: 1 // Mono: the gateway downmixes with -ac 1 and the engine expects mono (BM-446)
395
+ } : false;
396
+ // High-end device settings
397
+ if (!isMobile && !isLowEndDevice) {
398
+ return {
399
+ audio: baseAudio ? {
400
+ ...baseAudio,
401
+ sampleRate: 48000,
402
+ } : false,
403
+ video: {
404
+ width: { ideal: idealSize(rectWidth, 1280, 1920) },
405
+ height: { ideal: idealSize(rectHeight, 720, 1080) },
406
+ frameRate: { ideal: 30 },
407
+ facingMode: 'user',
408
+ aspectRatio: { ideal: rectWidth / rectHeight }
409
+ }
410
+ };
411
+ }
412
+ // Mobile device settings
413
+ if (isMobile) {
414
+ return {
415
+ audio: baseAudio,
416
+ video: {
417
+ width: { ideal: idealSize(rectWidth, 640, 1280) },
418
+ height: { ideal: idealSize(rectHeight, 480, 720) },
419
+ frameRate: { ideal: 24 },
420
+ facingMode: 'user',
421
+ aspectRatio: { ideal: rectWidth / rectHeight }
422
+ }
423
+ };
424
+ }
425
+ // Low-end device settings
426
+ return {
427
+ audio: baseAudio,
428
+ video: {
429
+ width: { ideal: idealSize(rectWidth, 640, 1280) },
430
+ height: { ideal: idealSize(rectHeight, 480, 720) },
431
+ frameRate: { ideal: 20 },
432
+ facingMode: 'user',
433
+ aspectRatio: { ideal: rectWidth / rectHeight }
434
+ }
435
+ };
436
+ };
437
+ const requestPermissions = async () => {
438
+ var _a, _b;
439
+ try {
440
+ setIsLoading(true);
441
+ const constraints = getAdaptiveConstraints();
442
+ const streamData = await navigator.mediaDevices.getUserMedia(constraints);
443
+ console.log('Media stream obtained, active:', streamData.active);
444
+ // Unmounted while getUserMedia was resolving: release the devices now,
445
+ // otherwise the camera/mic indicator stays on with nothing holding it.
446
+ if (!isMountedRef.current) {
447
+ streamData.getTracks().forEach(track => track.stop());
448
+ return;
449
+ }
450
+ streamRef.current = streamData;
451
+ setPermission(true);
452
+ setStream(streamData);
453
+ }
454
+ catch (err) {
455
+ // Not every failure is a denied permission: OverconstrainedError names
456
+ // the failing rule, NotReadableError means the camera is busy.
457
+ const e = err;
458
+ console.error('biometry-react-components: getUserMedia failed:', e === null || e === void 0 ? void 0 : e.name, (_a = e === null || e === void 0 ? void 0 : e.constraint) !== null && _a !== void 0 ? _a : '', (_b = e === null || e === void 0 ? void 0 : e.message) !== null && _b !== void 0 ? _b : '');
459
+ if (isMountedRef.current)
460
+ setPermission(false);
461
+ }
462
+ finally {
463
+ if (isMountedRef.current)
464
+ setIsLoading(false);
465
+ }
466
+ };
467
+ useEffect(() => {
468
+ isMountedRef.current = true;
469
+ void requestPermissions();
470
+ return () => {
471
+ isMountedRef.current = false;
472
+ if (streamRef.current) {
473
+ streamRef.current.getTracks().forEach(track => track.stop());
474
+ streamRef.current = null;
475
+ }
476
+ };
477
+ }, [rectWidth, rectHeight, audio]);
478
+ return { permission, stream, stopStreamTracks, requestPermissions, isLoading };
346
479
  };const FaceCapture = ({ rectWidth = 360, rectHeight = 576, onConfirmCapture, onCapture, className, style, noShadow = false, }) => {
347
480
  const [isConfirming, setIsConfirming] = useState(false);
348
481
  const [capturedUrl, setCapturedUrl] = useState(null);
349
482
  const [capturedFile, setCapturedFile] = useState(null);
350
483
  const videoRef = useRef(null);
351
- const streamRef = useRef(null);
352
- const QUALITY_MULTIPLIER = 3;
484
+ const { stream, stopStreamTracks, requestPermissions} = usePermissions({
485
+ rectWidth,
486
+ rectHeight,
487
+ audio: false,
488
+ });
353
489
  useEffect(() => {
354
490
  let isMounted = true;
355
- if (!isConfirming) {
356
- (async () => {
357
- try {
358
- const stream = await navigator.mediaDevices.getUserMedia({ video: { width: rectWidth, height: rectHeight } });
359
- if (!isMounted) {
360
- stream.getTracks().forEach((track) => track.stop());
361
- return;
362
- }
363
- if (videoRef.current) {
364
- videoRef.current.srcObject = stream;
365
- videoRef.current.onloadedmetadata = () => {
366
- var _a;
367
- (_a = videoRef.current) === null || _a === void 0 ? void 0 : _a.play();
368
- };
369
- }
370
- streamRef.current = stream;
371
- }
372
- catch (err) {
373
- console.error(err);
374
- }
375
- })();
491
+ if (!isConfirming && stream && videoRef.current && isMounted) {
492
+ videoRef.current.srcObject = stream;
493
+ videoRef.current.onloadedmetadata = () => {
494
+ var _a;
495
+ (_a = videoRef.current) === null || _a === void 0 ? void 0 : _a.play();
496
+ };
376
497
  }
377
498
  return () => {
378
499
  isMounted = false;
379
- if (streamRef.current) {
380
- streamRef.current.getTracks().forEach((track) => track.stop());
381
- streamRef.current = null;
500
+ };
501
+ }, [isConfirming, stream]);
502
+ useEffect(() => {
503
+ return () => {
504
+ if (capturedUrl) {
505
+ URL.revokeObjectURL(capturedUrl);
382
506
  }
383
507
  };
384
- }, [isConfirming, rectWidth, rectHeight]);
508
+ }, [capturedUrl]);
385
509
  const handleCapture = () => {
386
510
  const video = videoRef.current;
387
511
  if (!video)
388
512
  return;
389
513
  const canvas = document.createElement("canvas");
390
- canvas.width = rectWidth * QUALITY_MULTIPLIER;
391
- canvas.height = rectHeight * QUALITY_MULTIPLIER;
514
+ const targetAspect = (rectWidth - 40) / (rectHeight - 40);
515
+ const vw = video.videoWidth || (rectWidth - 40);
516
+ const vh = video.videoHeight || (rectHeight - 40);
517
+ const videoAspect = vw / vh;
518
+ let sWidth, sHeight, sx, sy;
519
+ if (videoAspect > targetAspect) {
520
+ // Source is wider than target: crop the sides.
521
+ sHeight = vh;
522
+ sWidth = vh * targetAspect;
523
+ sx = (vw - sWidth) / 2;
524
+ sy = 0;
525
+ }
526
+ else {
527
+ // Source is taller than target: crop top/bottom.
528
+ sWidth = vw;
529
+ sHeight = vw / targetAspect;
530
+ sx = 0;
531
+ sy = (vh - sHeight) / 2;
532
+ }
533
+ canvas.width = Math.round(sWidth);
534
+ canvas.height = Math.round(sHeight);
392
535
  const ctx = canvas.getContext("2d");
393
536
  if (ctx) {
394
- ctx.drawImage(video, 0, 0, rectWidth, rectHeight, 0, 0, rectWidth * QUALITY_MULTIPLIER, rectHeight * QUALITY_MULTIPLIER);
537
+ ctx.drawImage(video, sx, sy, sWidth, sHeight, 0, 0, canvas.width, canvas.height);
395
538
  canvas.toBlob((blob) => {
396
539
  if (blob) {
397
- // Stop the stream immediately — don't wait for useEffect cleanup
398
- if (streamRef.current) {
399
- streamRef.current.getTracks().forEach((track) => track.stop());
400
- streamRef.current = null;
401
- }
540
+ stopStreamTracks();
402
541
  const file = new File([blob], "face.jpg", { type: "image/jpeg" });
403
542
  setCapturedFile(file);
404
543
  setCapturedUrl(URL.createObjectURL(blob));
@@ -427,6 +566,7 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
427
566
  setCapturedUrl(null);
428
567
  setCapturedFile(null);
429
568
  setIsConfirming(false);
569
+ requestPermissions();
430
570
  };
431
571
  const wrapperStyle = {
432
572
  background: '#fff',
@@ -450,6 +590,9 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
450
590
  display: 'flex',
451
591
  alignItems: 'center',
452
592
  justifyContent: 'center',
593
+ background: '#111',
594
+ borderRadius: noShadow ? 0 : 8,
595
+ overflow: 'hidden',
453
596
  };
454
597
  const ovalArea = {
455
598
  position: 'absolute',
@@ -499,14 +642,16 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
499
642
  display: 'block',
500
643
  };
501
644
  const preview = {
502
- width: '100%',
503
- height: '100%',
645
+ position: 'relative',
646
+ width: rectWidth - 40,
647
+ height: rectHeight - 40,
504
648
  display: 'flex',
505
649
  alignItems: 'center',
506
650
  justifyContent: 'center',
507
651
  background: '#111',
508
- borderRadius: 8,
652
+ borderRadius: noShadow ? 0 : 8,
509
653
  margin: '0 auto',
654
+ overflow: 'hidden',
510
655
  };
511
656
  const confirmText = {
512
657
  textAlign: 'center',
@@ -541,7 +686,12 @@ import {jsx,jsxs,Fragment}from'react/jsx-runtime';import React,{useState,useRef,
541
686
  background: 'rgba(0,0,0,0.4)',
542
687
  zIndex: 1,
543
688
  borderRadius: noShadow ? 0 : 8,
544
- } }), jsxs("div", { className: "facecapture-oval-area", style: ovalArea, children: [jsx("span", { className: "facecapture-guidance-text", style: guidanceText, children: "Place your face in the oval" }), jsx("div", { className: "facecapture-oval-dashed", style: ovalDashed })] })] }), jsx("button", { type: "button", className: "facecapture-capture-btn", style: captureButton, onClick: handleCapture, children: "Capture" })] })) : (jsxs(Fragment, { children: [jsx("div", { className: "facecapture-preview", style: preview, children: capturedUrl && (jsx("img", { src: capturedUrl, alt: "Captured face", className: "facecapture-preview-img", style: { maxWidth: '100%', maxHeight: '100%', borderRadius: 8 } })) }), jsx("div", { className: "facecapture-confirm-text", style: confirmText, children: "Do you want to use this photo?" }), jsxs("div", { className: "facecapture-confirm-btns", style: { display: 'flex', justifyContent: 'center', gap: 32, marginTop: 16, width: '100%' }, children: [jsx("button", { type: "button", "aria-label": "Retake", className: "facecapture-icon-btn", style: iconButton, onClick: handleDecline, onMouseOver: e => (e.currentTarget.style.background = 'rgba(211,47,47,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsx("path", { d: "M12 12L24 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" }), jsx("path", { d: "M24 12L12 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" })] }) }), jsx("button", { type: "button", "aria-label": "Confirm", className: "facecapture-icon-btn", style: iconButton, onClick: handleConfirm, onMouseOver: e => (e.currentTarget.style.background = 'rgba(56,142,60,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsx("path", { d: "M10 19L16 25L26 13", stroke: "#388e3c", strokeWidth: "3", strokeLinecap: "round", strokeLinejoin: "round" })] }) })] })] })) }));
689
+ } }), jsxs("div", { className: "facecapture-oval-area", style: ovalArea, children: [jsx("span", { className: "facecapture-guidance-text", style: guidanceText, children: "Place your face in the oval" }), jsx("div", { className: "facecapture-oval-dashed", style: ovalDashed })] })] }), jsx("button", { type: "button", className: "facecapture-capture-btn", style: captureButton, onClick: handleCapture, children: "Capture" })] })) : (jsxs(Fragment, { children: [jsx("div", { className: "facecapture-preview", style: preview, children: capturedUrl && (jsx("img", { src: capturedUrl, alt: "Captured face", className: "facecapture-preview-img", style: {
690
+ width: '100%',
691
+ height: '100%',
692
+ objectFit: 'cover',
693
+ borderRadius: noShadow ? 0 : 8,
694
+ } })) }), jsx("div", { className: "facecapture-confirm-text", style: confirmText, children: "Do you want to use this photo?" }), jsxs("div", { className: "facecapture-confirm-btns", style: { display: 'flex', justifyContent: 'center', gap: 32, marginTop: 16, width: '100%' }, children: [jsx("button", { type: "button", "aria-label": "Retake", className: "facecapture-icon-btn", style: iconButton, onClick: handleDecline, onMouseOver: e => (e.currentTarget.style.background = 'rgba(211,47,47,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsx("path", { d: "M12 12L24 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" }), jsx("path", { d: "M24 12L12 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" })] }) }), jsx("button", { type: "button", "aria-label": "Confirm", className: "facecapture-icon-btn", style: iconButton, onClick: handleConfirm, onMouseOver: e => (e.currentTarget.style.background = 'rgba(56,142,60,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsx("path", { d: "M10 19L16 25L26 13", stroke: "#388e3c", strokeWidth: "3", strokeLinecap: "round", strokeLinejoin: "round" })] }) })] })] })) }));
545
695
  };var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
546
696
 
547
697
  function getDefaultExportFromCjs (x) {
@@ -7345,8 +7495,14 @@ function parseChallenge(challenge) {
7345
7495
  }
7346
7496
  return digits;
7347
7497
  }function getSupportedMimeType() {
7348
- if (MediaRecorder.isTypeSupported('video/webm;codecs=vp9')) {
7349
- return 'video/webm;codecs=vp9';
7498
+ if (typeof MediaRecorder === 'undefined') {
7499
+ return 'video/webm';
7500
+ }
7501
+ if (MediaRecorder.isTypeSupported('video/webm;codecs=vp8,opus')) {
7502
+ return 'video/webm;codecs=vp8,opus';
7503
+ }
7504
+ else if (MediaRecorder.isTypeSupported('video/webm;codecs=vp9,opus')) {
7505
+ return 'video/webm;codecs=vp9,opus';
7350
7506
  }
7351
7507
  else if (MediaRecorder.isTypeSupported('video/webm')) {
7352
7508
  return 'video/webm';
@@ -7355,7 +7511,8 @@ function parseChallenge(challenge) {
7355
7511
  return 'video/mp4';
7356
7512
  }
7357
7513
  else {
7358
- console.error("no suitable mimetype found for this device");
7514
+ console.warn("no suitable mimetype found for this device, defaulting to video/webm");
7515
+ return 'video/webm';
7359
7516
  }
7360
7517
  }const VIDEO_LENGTH_MS = 7000;
7361
7518
  /**
@@ -7437,12 +7594,21 @@ const useRecorder = (stream, type, onStopRecording, lengthMs = VIDEO_LENGTH_MS)
7437
7594
  }
7438
7595
  recorderRef.current.stopRecording(() => {
7439
7596
  const blob = recorderRef.current.getBlob();
7440
- fixWebmDuration(blob, lengthMs, function (fixedBlob) {
7597
+ const isWebm = !blob.type || blob.type.includes('webm');
7598
+ if (isWebm) {
7599
+ fixWebmDuration(blob, lengthMs, function (fixedBlob) {
7600
+ setIsRecording(false);
7601
+ recorderRef.current.destroy();
7602
+ recorderRef.current = null;
7603
+ onStopRecording(fixedBlob);
7604
+ }, { logger: false });
7605
+ }
7606
+ else {
7441
7607
  setIsRecording(false);
7442
7608
  recorderRef.current.destroy();
7443
7609
  recorderRef.current = null;
7444
- onStopRecording(fixedBlob);
7445
- });
7610
+ onStopRecording(blob);
7611
+ }
7446
7612
  });
7447
7613
  };
7448
7614
  const cancelRecording = () => {
@@ -7459,140 +7625,6 @@ const useRecorder = (stream, type, onStopRecording, lengthMs = VIDEO_LENGTH_MS)
7459
7625
  }
7460
7626
  };
7461
7627
  return { isRecording, startRecording, stopRecording, cancelRecording };
7462
- };// Video constraints are IDEAL-ONLY - never `min` (BM-530).
7463
- //
7464
- // Without a deviceId, getUserMedia considers every camera and returns one that
7465
- // satisfies all required (min/max/exact) constraints. A hard `min` therefore
7466
- // filters cameras: with `width.min: 1280` a 640x480 laptop webcam is silently
7467
- // excluded and the browser falls back to whatever passes - typically a virtual
7468
- // camera (OBS) that accepts any resolution - or, with no such camera, throws
7469
- // OverconstrainedError, which used to surface as "Camera Permission Required".
7470
- // Chrome's camera picker for the site is narrowed the same way.
7471
- //
7472
- // The old minimums survive as floors on the ideal (`idealSize`): an HD camera
7473
- // is still steered to at least the same resolution as before, while a weaker
7474
- // camera is accepted at its best mode instead of being rejected.
7475
- const idealSize = (requested, floor, cap) => Math.max(floor, Math.min(requested, cap));
7476
- const usePermissions = ({ rectWidth = 1920, rectHeight = 1080 } = {}) => {
7477
- const [permission, setPermission] = useState(false);
7478
- const [stream, setStream] = useState(null);
7479
- const [isLoading, setIsLoading] = useState(true);
7480
- // The stream is mirrored in a ref so stopStreamTracks and the unmount
7481
- // cleanup act on the live stream rather than a stale closure, and so a
7482
- // getUserMedia that resolves after unmount can be stopped immediately
7483
- // (restored from 1.3.5, which 1.4.0 reverted).
7484
- const streamRef = useRef(null);
7485
- const isMountedRef = useRef(false);
7486
- const stopStreamTracks = () => {
7487
- const currentStream = streamRef.current;
7488
- if (currentStream) {
7489
- currentStream.getTracks().forEach(track => track.stop());
7490
- streamRef.current = null;
7491
- setStream(null);
7492
- }
7493
- };
7494
- const getAdaptiveConstraints = () => {
7495
- // Detect device capabilities and choose appropriate quality
7496
- const isMobile = /Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
7497
- const isLowEndDevice = navigator.hardwareConcurrency && navigator.hardwareConcurrency <= 4;
7498
- // High-end device settings
7499
- if (!isMobile && !isLowEndDevice) {
7500
- return {
7501
- audio: {
7502
- echoCancellation: true,
7503
- noiseSuppression: true,
7504
- autoGainControl: true,
7505
- sampleRate: 48000,
7506
- channelCount: 1 // Mono: the gateway downmixes with -ac 1 and the engine expects mono (BM-446)
7507
- },
7508
- video: {
7509
- width: { ideal: idealSize(rectWidth, 1280, 1920) },
7510
- height: { ideal: idealSize(rectHeight, 720, 1080) },
7511
- frameRate: { ideal: 30 },
7512
- facingMode: 'user',
7513
- aspectRatio: { ideal: rectWidth / rectHeight }
7514
- }
7515
- };
7516
- }
7517
- // Mobile device settings
7518
- if (isMobile) {
7519
- return {
7520
- audio: {
7521
- echoCancellation: true,
7522
- noiseSuppression: true,
7523
- autoGainControl: true,
7524
- sampleRate: 22050, // Lower for mobile
7525
- channelCount: 1
7526
- },
7527
- video: {
7528
- width: { ideal: idealSize(rectWidth, 640, 1280) },
7529
- height: { ideal: idealSize(rectHeight, 480, 720) },
7530
- frameRate: { ideal: 24 },
7531
- facingMode: 'user',
7532
- aspectRatio: { ideal: rectWidth / rectHeight }
7533
- }
7534
- };
7535
- }
7536
- // Low-end device settings
7537
- return {
7538
- audio: {
7539
- echoCancellation: true,
7540
- noiseSuppression: true,
7541
- autoGainControl: true,
7542
- sampleRate: 22050,
7543
- channelCount: 1
7544
- },
7545
- video: {
7546
- width: { ideal: idealSize(rectWidth, 640, 1280) },
7547
- height: { ideal: idealSize(rectHeight, 480, 720) },
7548
- frameRate: { ideal: 20 },
7549
- facingMode: 'user',
7550
- aspectRatio: { ideal: rectWidth / rectHeight }
7551
- }
7552
- };
7553
- };
7554
- const requestPermissions = async () => {
7555
- var _a, _b;
7556
- try {
7557
- setIsLoading(true);
7558
- const constraints = getAdaptiveConstraints();
7559
- const streamData = await navigator.mediaDevices.getUserMedia(constraints);
7560
- console.log('Media stream obtained, active:', streamData.active);
7561
- // Unmounted while getUserMedia was resolving: release the devices now,
7562
- // otherwise the camera/mic indicator stays on with nothing holding it.
7563
- if (!isMountedRef.current) {
7564
- streamData.getTracks().forEach(track => track.stop());
7565
- return;
7566
- }
7567
- streamRef.current = streamData;
7568
- setPermission(true);
7569
- setStream(streamData);
7570
- }
7571
- catch (err) {
7572
- // Not every failure is a denied permission: OverconstrainedError names
7573
- // the failing rule, NotReadableError means the camera is busy.
7574
- const e = err;
7575
- console.error('biometry-react-components: getUserMedia failed:', e === null || e === void 0 ? void 0 : e.name, (_a = e === null || e === void 0 ? void 0 : e.constraint) !== null && _a !== void 0 ? _a : '', (_b = e === null || e === void 0 ? void 0 : e.message) !== null && _b !== void 0 ? _b : '');
7576
- if (isMountedRef.current)
7577
- setPermission(false);
7578
- }
7579
- finally {
7580
- if (isMountedRef.current)
7581
- setIsLoading(false);
7582
- }
7583
- };
7584
- useEffect(() => {
7585
- isMountedRef.current = true;
7586
- void requestPermissions();
7587
- return () => {
7588
- isMountedRef.current = false;
7589
- if (streamRef.current) {
7590
- streamRef.current.getTracks().forEach(track => track.stop());
7591
- streamRef.current = null;
7592
- }
7593
- };
7594
- }, [rectWidth, rectHeight]);
7595
- return { permission, stream, stopStreamTracks, requestPermissions, isLoading };
7596
7628
  };// spinner animation
7597
7629
  const spinnerStyle$1 = `
7598
7630
  @keyframes spin {
@@ -7742,10 +7774,10 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, challengeDigits, reco
7742
7774
  startAudioVisualization(stream);
7743
7775
  }
7744
7776
  else {
7745
- stopAudioVisualization();
7777
+ pauseAudioVisualization();
7746
7778
  }
7747
7779
  return () => {
7748
- stopAudioVisualization();
7780
+ pauseAudioVisualization();
7749
7781
  };
7750
7782
  }, [stream, state]);
7751
7783
  // Recording timer
@@ -7855,15 +7887,34 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, challengeDigits, reco
7855
7887
  requestPermissions();
7856
7888
  };
7857
7889
  function startAudioVisualization(stream) {
7858
- // Create audio context + analyser
7859
- audioCtxRef.current = new AudioContext();
7860
- const source = audioCtxRef.current.createMediaStreamSource(stream);
7861
- const analyser = audioCtxRef.current.createAnalyser();
7862
- analyser.fftSize = 256;
7863
- source.connect(analyser);
7864
- analyserRef.current = analyser;
7890
+ if (!audioCtxRef.current) {
7891
+ const AudioCtx = window.AudioContext || window.webkitAudioContext;
7892
+ if (!AudioCtx)
7893
+ return;
7894
+ audioCtxRef.current = new AudioCtx();
7895
+ }
7896
+ const audioCtx = audioCtxRef.current;
7897
+ if (audioCtx.state === 'suspended') {
7898
+ audioCtx.resume().catch(() => { });
7899
+ }
7900
+ if (!analyserRef.current) {
7901
+ try {
7902
+ const source = audioCtx.createMediaStreamSource(stream);
7903
+ const analyser = audioCtx.createAnalyser();
7904
+ analyser.fftSize = 256;
7905
+ source.connect(analyser);
7906
+ analyserRef.current = analyser;
7907
+ }
7908
+ catch (err) {
7909
+ console.warn('Audio visualization setup failed:', err);
7910
+ return;
7911
+ }
7912
+ }
7913
+ const analyser = analyserRef.current;
7865
7914
  const bufferLength = analyser.frequencyBinCount;
7866
7915
  const dataArray = new Uint8Array(bufferLength);
7916
+ if (rafRef.current)
7917
+ cancelAnimationFrame(rafRef.current);
7867
7918
  function update() {
7868
7919
  analyser.getByteFrequencyData(dataArray);
7869
7920
  // Calculate average volume (0–1)
@@ -7873,16 +7924,24 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, challengeDigits, reco
7873
7924
  }
7874
7925
  update();
7875
7926
  }
7876
- function stopAudioVisualization() {
7877
- var _a, _b;
7927
+ function pauseAudioVisualization() {
7878
7928
  if (rafRef.current)
7879
7929
  cancelAnimationFrame(rafRef.current);
7880
7930
  rafRef.current = null;
7881
- (_a = analyserRef.current) === null || _a === void 0 ? void 0 : _a.disconnect();
7882
- (_b = audioCtxRef.current) === null || _b === void 0 ? void 0 : _b.close();
7931
+ setMicLevel(0);
7932
+ }
7933
+ function stopAudioVisualization() {
7934
+ var _a, _b;
7935
+ pauseAudioVisualization();
7936
+ try {
7937
+ (_a = analyserRef.current) === null || _a === void 0 ? void 0 : _a.disconnect();
7938
+ (_b = audioCtxRef.current) === null || _b === void 0 ? void 0 : _b.close();
7939
+ }
7940
+ catch (_c) {
7941
+ // ignore
7942
+ }
7883
7943
  audioCtxRef.current = null;
7884
7944
  analyserRef.current = null;
7885
- setMicLevel(0);
7886
7945
  }
7887
7946
  const wrapperStyle = {
7888
7947
  background: '#fff',
@@ -8099,11 +8158,21 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, challengeDigits, reco
8099
8158
  borderRadius: '50%',
8100
8159
  animation: 'spin 1s linear infinite',
8101
8160
  marginBottom: 16,
8102
- } }), jsx("div", { style: { fontSize: 18, fontWeight: 500, marginBottom: 8 }, children: "Processing video..." }), jsx("div", { style: { fontSize: 14, opacity: 0.8 }, children: "Please wait while we prepare your recording" })] }) }) })), state === 'result' && isConfirming && (jsxs(Fragment, { children: [jsx("div", { className: "facerecorder-preview", style: preview, children: videoUrl && (jsx("video", { ref: previewRef, src: videoUrl, controls: true, style: {
8161
+ } }), jsx("div", { style: { fontSize: 18, fontWeight: 500, marginBottom: 8 }, children: "Processing video..." }), jsx("div", { style: { fontSize: 14, opacity: 0.8 }, children: "Please wait while we prepare your recording" })] }) }) })), state === 'result' && isConfirming && (jsxs(Fragment, { children: [jsx("div", { className: "facerecorder-preview", style: preview, children: videoUrl && (jsx("video", { ref: previewRef, src: videoUrl, controls: true, playsInline: true, style: {
8103
8162
  width: '100%',
8104
8163
  height: '100%',
8105
8164
  borderRadius: 8,
8106
8165
  objectFit: 'cover'
8166
+ }, onLoadedMetadata: (e) => {
8167
+ const video = e.currentTarget;
8168
+ if (video && (!isFinite(video.duration) || video.duration > 86400)) {
8169
+ video.currentTime = 1e101;
8170
+ const onTimeUpdate = () => {
8171
+ video.removeEventListener('timeupdate', onTimeUpdate);
8172
+ video.currentTime = 0;
8173
+ };
8174
+ video.addEventListener('timeupdate', onTimeUpdate);
8175
+ }
8107
8176
  }, onLoadedData: () => console.log('Video loaded successfully'), onError: (e) => console.error('Video error:', e), onLoadStart: () => console.log('Video load started'), onCanPlay: () => console.log('Video can play') })) }), jsx("div", { className: "facerecorder-confirm-text", style: confirmText, children: "Do you want to use this video?" }), jsxs("div", { className: "facerecorder-confirm-btns", style: { display: 'flex', justifyContent: 'center', gap: 32, marginTop: 16, width: '100%' }, children: [jsx("button", { type: "button", "aria-label": "Retake", className: "facerecorder-icon-btn", style: iconButton, onClick: handleDecline, onMouseOver: e => (e.currentTarget.style.background = 'rgba(211,47,47,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsx("path", { d: "M12 12L24 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" }), jsx("path", { d: "M24 12L12 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" })] }) }), jsx("button", { type: "button", "aria-label": "Confirm", className: "facerecorder-icon-btn", style: iconButton, onClick: handleConfirm, onMouseOver: e => (e.currentTarget.style.background = 'rgba(56,142,60,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsx("path", { d: "M10 19L16 25L26 13", stroke: "#388e3c", strokeWidth: "3", strokeLinecap: "round", strokeLinejoin: "round" })] }) })] })] }))] }));
8108
8177
  };const useAudioPermissions = () => {
8109
8178
  const [permission, setPermission] = useState(false);