@authme/util 2.4.2 → 2.4.6

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/index.cjs CHANGED
@@ -20,10 +20,13 @@ require('core-js/modules/es.string.match.js');
20
20
  require('core-js/modules/es.string.replace.js');
21
21
  require('core-js/modules/web.url-search-params.js');
22
22
  require('core-js/modules/es.string.search.js');
23
- var Lottie = require('lottie-web');
24
- require('core-js/modules/es.array.sort.js');
23
+ require('core-js/modules/es.typed-array.uint32-array.js');
25
24
  require('core-js/modules/es.array.includes.js');
26
25
  require('core-js/modules/es.string.includes.js');
26
+ var fileSaver = require('file-saver');
27
+ var JSZip = require('jszip');
28
+ var Lottie = require('lottie-web');
29
+ require('core-js/modules/es.array.sort.js');
27
30
  require('core-js/modules/es.parse-int.js');
28
31
  require('core-js/modules/es.string.trim.js');
29
32
  require('core-js/modules/es.string.starts-with.js');
@@ -32,6 +35,7 @@ require('core-js/modules/es.symbol.description.js');
32
35
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
33
36
 
34
37
  var jwt_decode__default = /*#__PURE__*/_interopDefaultLegacy(jwt_decode);
38
+ var JSZip__default = /*#__PURE__*/_interopDefaultLegacy(JSZip);
35
39
  var Lottie__default = /*#__PURE__*/_interopDefaultLegacy(Lottie);
36
40
 
37
41
  const GLOBAL_KEY = '_AuthmeConfig';
@@ -185,6 +189,7 @@ exports.ErrorCode = void 0;
185
189
  ErrorCode[ErrorCode["USER_CANCEL"] = 903] = "USER_CANCEL";
186
190
  ErrorCode[ErrorCode["CAMERA_NOT_SUPPORT"] = 904] = "CAMERA_NOT_SUPPORT";
187
191
  ErrorCode[ErrorCode["SERVER_ERROR"] = 905] = "SERVER_ERROR";
192
+ ErrorCode[ErrorCode["EVENT_NAME_WRONG"] = 906] = "EVENT_NAME_WRONG";
188
193
  })(exports.ErrorCode || (exports.ErrorCode = {}));
189
194
 
190
195
  function decodeToken(token) {
@@ -478,11 +483,336 @@ const isIphone14proOrProMax = () => {
478
483
  return isIphone() && (screen.width === 430 && screen.height === 932 || screen.width === 393 && screen.height === 852);
479
484
  };
480
485
 
486
+ function fileSaverService() {
487
+ const filesQueue = [];
488
+ function pushFile(file) {
489
+ filesQueue.push(file);
490
+ }
491
+ function popAllFiles() {
492
+ filesQueue.splice(0, filesQueue.length);
493
+ }
494
+ function saveFiles(fileName, json) {
495
+ return __awaiter(this, void 0, void 0, function* () {
496
+ const zip = new JSZip__default["default"]();
497
+ filesQueue.forEach(fileItem => {
498
+ zip.file(fileItem.name, fileItem.file);
499
+ });
500
+ if (json) zip.file('data.json', JSON.stringify(json));
501
+ const content = yield zip.generateAsync({
502
+ type: 'blob'
503
+ });
504
+ fileSaver.saveAs(content, `${fileName}.zip`);
505
+ popAllFiles();
506
+ });
507
+ }
508
+ return {
509
+ pushFile,
510
+ saveFiles
511
+ };
512
+ }
513
+
514
+ const {
515
+ saveFiles,
516
+ pushFile
517
+ } = fileSaverService();
518
+ exports.RUN_FUNCTION_NAME = void 0;
519
+ (function (RUN_FUNCTION_NAME) {
520
+ RUN_FUNCTION_NAME["RECOGNITION"] = "service.recognition";
521
+ RUN_FUNCTION_NAME["ANTI_FARUD_RECOGNITION"] = "antiFraudInstance.recognition";
522
+ RUN_FUNCTION_NAME["API_REQUEST"] = "api.request";
523
+ })(exports.RUN_FUNCTION_NAME || (exports.RUN_FUNCTION_NAME = {}));
481
524
  function debugLog(message, ...others) {
482
525
  if (new URLSearchParams(location.search).get('authme-debug') === 'true') {
483
526
  console.log(message, ...others);
484
527
  }
485
528
  }
529
+ function generateUniqueId(length = 32) {
530
+ if (length <= 0) {
531
+ throw new Error('Length should be a positive integer.');
532
+ }
533
+ // 定義可能的字元
534
+ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
535
+ const charactersLength = characters.length;
536
+ const randomValues = new Uint32Array(length);
537
+ // 使用window.crypto.getRandomValues取得真正的隨機值
538
+ window.crypto.getRandomValues(randomValues);
539
+ let result = '';
540
+ for (let i = 0; i < length; i++) {
541
+ result += characters[randomValues[i] % charactersLength];
542
+ }
543
+ return result;
544
+ }
545
+ function debugTools() {
546
+ const debugMode = new URLSearchParams(location.search).get('authme-debug') === 'true';
547
+ const debugModeUI = new URLSearchParams(location.search).get('authme-debug-info') === 'true';
548
+ const shouldGetDebugImage = new URLSearchParams(location.search).get('authme-debug-get-image') === 'true';
549
+ const debugLogs = [];
550
+ const logInfo = {
551
+ userAgent: navigator.userAgent,
552
+ token: '',
553
+ message: ''
554
+ };
555
+ let currentRoundId = generateUniqueId(8);
556
+ let currentType = null;
557
+ const interiaTimeObj = {};
558
+ function pushNewDebugLog(logParams) {
559
+ var _a;
560
+ if (!debugMode) return;
561
+ const now = new Date();
562
+ const _logParams = JSON.parse(JSON.stringify(logParams));
563
+ if (_logParams.result) {
564
+ (_a = _logParams.result) === null || _a === void 0 ? true : delete _a.imageData;
565
+ }
566
+ const newDebugLog = Object.assign(Object.assign({
567
+ dateTime: now.toLocaleString(),
568
+ roundId: currentRoundId,
569
+ type: currentType !== null && currentType !== void 0 ? currentType : undefined
570
+ }, _logParams), {
571
+ time: now.getTime(),
572
+ _id: generateUniqueId()
573
+ });
574
+ debugLogs.push(newDebugLog);
575
+ return newDebugLog;
576
+ }
577
+ function pushNewDebugImage(image, log, options) {
578
+ var _a, _b;
579
+ if (!debugMode) return;
580
+ const newLog = pushNewDebugLog(log);
581
+ const recognitionFunctions = [exports.RUN_FUNCTION_NAME.ANTI_FARUD_RECOGNITION, exports.RUN_FUNCTION_NAME.RECOGNITION];
582
+ const currentRoundRecognitionResultItem = debugLogs.find(item => item.roundId === (newLog === null || newLog === void 0 ? void 0 : newLog.roundId) && item.status === 'run-end' && (item.runFunction ? recognitionFunctions.includes(item.runFunction) : false));
583
+ const imageStatus = (_a = currentRoundRecognitionResultItem === null || currentRoundRecognitionResultItem === void 0 ? void 0 : currentRoundRecognitionResultItem.result) === null || _a === void 0 ? void 0 : _a.eStatus;
584
+ pushFile({
585
+ name: `${(_b = options === null || options === void 0 ? void 0 : options.prefix) !== null && _b !== void 0 ? _b : ''}${newLog === null || newLog === void 0 ? void 0 : newLog.time}-${newLog === null || newLog === void 0 ? void 0 : newLog._id}${imageStatus ? `__${imageStatus}` : ''}.jpeg`,
586
+ file: image
587
+ });
588
+ }
589
+ function functionLogging(func, logParams) {
590
+ return new Promise((resolve, reject) => {
591
+ pushNewDebugLog(Object.assign(Object.assign({}, logParams), {
592
+ status: 'run-start'
593
+ }));
594
+ func().then(result => {
595
+ pushNewDebugLog(Object.assign(Object.assign({}, logParams), {
596
+ status: 'run-end',
597
+ roundId: currentRoundId,
598
+ result
599
+ }));
600
+ resolve(result);
601
+ }).catch(error => {
602
+ pushNewDebugLog(Object.assign(Object.assign({}, logParams), {
603
+ status: 'run-error',
604
+ roundId: currentRoundId,
605
+ result: error
606
+ }));
607
+ reject(error);
608
+ });
609
+ });
610
+ }
611
+ function renderDebugTips() {
612
+ if (document.getElementById('debug-tips')) {
613
+ return;
614
+ }
615
+ const debugTips = document.createElement('div');
616
+ debugTips.style.position = 'fixed';
617
+ debugTips.style.top = '50px';
618
+ debugTips.style.right = '0';
619
+ debugTips.style.zIndex = '999';
620
+ debugTips.innerHTML = 'Debug Mode';
621
+ debugTips.style.color = 'red';
622
+ debugTips.style.fontWeight = 'bold';
623
+ debugTips.setAttribute('id', 'debug-tips');
624
+ document.body.appendChild(debugTips);
625
+ }
626
+ function renderDebugUI() {
627
+ if (document.getElementById('debug-ui')) {
628
+ return;
629
+ }
630
+ const debugUI = document.createElement('div');
631
+ debugUI.style.position = 'fixed';
632
+ debugUI.style.top = '0';
633
+ debugUI.style.right = '0';
634
+ debugUI.style.zIndex = '777';
635
+ debugUI.style.backgroundColor = 'black';
636
+ debugUI.style.color = 'white';
637
+ debugUI.style.padding = '10px';
638
+ debugUI.style.paddingRight = '150px';
639
+ debugUI.style.fontSize = '12px';
640
+ debugUI.style.opacity = '0.7';
641
+ debugUI.style.boxSizing = 'border-box';
642
+ debugUI.style.width = '100%';
643
+ debugUI.style.maxHeight = '50vh';
644
+ debugUI.style.overflow = 'auto';
645
+ debugUI.setAttribute('id', 'debug-ui');
646
+ const closeUI = document.createElement('button');
647
+ closeUI.style.position = 'fixed';
648
+ closeUI.style.top = '0';
649
+ closeUI.style.right = '100px';
650
+ closeUI.innerHTML = 'close';
651
+ closeUI.addEventListener('click', () => {
652
+ debugUI.style.display = 'none';
653
+ });
654
+ debugUI.appendChild(closeUI);
655
+ const accordionUI = document.createElement('button');
656
+ accordionUI.style.position = 'fixed';
657
+ accordionUI.style.top = '0';
658
+ accordionUI.style.right = '0';
659
+ accordionUI.innerHTML = '收合/展開';
660
+ accordionUI.addEventListener('click', () => {
661
+ if (debugUI.style.maxHeight === '50vh') {
662
+ debugUI.style.maxHeight = '5vh';
663
+ } else {
664
+ debugUI.style.maxHeight = '50vh';
665
+ }
666
+ });
667
+ debugUI.appendChild(accordionUI);
668
+ document.body.appendChild(debugUI);
669
+ }
670
+ function setTimeStart(behavior) {
671
+ if (!debugModeUI) {
672
+ return;
673
+ }
674
+ const dStart = new Date();
675
+ const nStart = dStart.getTime();
676
+ if (interiaTimeObj && interiaTimeObj[behavior]) {
677
+ console.error('debugmode: setTimeStart duplicate', behavior);
678
+ return false;
679
+ }
680
+ interiaTimeObj[behavior] = {
681
+ behavior,
682
+ start: nStart / 1000,
683
+ duration: 0,
684
+ end: 0
685
+ };
686
+ // console.log(interiaTimeObj[behavior])
687
+ renderTimeInfo(behavior);
688
+ return interiaTimeObj[behavior];
689
+ }
690
+ function setTimeEnd(behavior) {
691
+ if (!debugModeUI) {
692
+ return;
693
+ }
694
+ const dEnd = new Date();
695
+ const nEnd = dEnd.getTime();
696
+ if (!interiaTimeObj[behavior]) {
697
+ console.error('debugmode: setTimeEnd not found', behavior);
698
+ return false;
699
+ }
700
+ interiaTimeObj[behavior].end = nEnd / 1000;
701
+ interiaTimeObj[behavior].duration = interiaTimeObj[behavior].end - interiaTimeObj[behavior].start;
702
+ // console.log(interiaTimeObj[behavior])
703
+ renderTimeInfo(behavior);
704
+ return interiaTimeObj[behavior];
705
+ }
706
+ function setTimeDuration(behavior) {
707
+ const ts = Date.now();
708
+ setTimeStart(`${behavior}_${ts}`);
709
+ return {
710
+ end: () => {
711
+ setTimeEnd(`${behavior}_${ts}`);
712
+ }
713
+ };
714
+ }
715
+ function renderTimeInfo(behavior) {
716
+ const dom = document.querySelector(`#debug-ui-${behavior}`);
717
+ const info = `{"behavior":<span style="color:green;">"${behavior}"</span>, "start": ${interiaTimeObj[behavior].start}, "duration": ${interiaTimeObj[behavior].duration}, "end": ${interiaTimeObj[behavior].end}}`;
718
+ // const info = `<span style="color:green;">${behavior}</span>: Duration: ${interiaTimeObj[behavior].duration}s`;
719
+ if (dom) {
720
+ dom.innerHTML = info;
721
+ return;
722
+ }
723
+ const debugUI = document.getElementById('debug-ui');
724
+ const div = document.createElement('div');
725
+ div.setAttribute('class', 'debug-ui-item');
726
+ div.setAttribute('id', `debug-ui-${behavior}`);
727
+ div.innerHTML = info;
728
+ debugUI.appendChild(div);
729
+ }
730
+ function functionRunTime() {
731
+ const arr = [];
732
+ const items = document.querySelectorAll('.debug-ui-item');
733
+ items.forEach(item => {
734
+ arr.push(JSON.parse(item.innerText));
735
+ });
736
+ return arr;
737
+ }
738
+ if (debugMode) {
739
+ renderDebugTips();
740
+ // setRequestLoggingFunc(functionLogging)
741
+ }
742
+
743
+ if (debugModeUI) {
744
+ console.log('debugModeUI Init');
745
+ renderDebugUI();
746
+ }
747
+ function saveDebugImage({
748
+ getDebugImageData,
749
+ data,
750
+ width,
751
+ height,
752
+ result,
753
+ type
754
+ }) {
755
+ return __awaiter(this, void 0, void 0, function* () {
756
+ if (!debugMode || !shouldGetDebugImage) return;
757
+ const debugImage = yield getDebugImageData(data);
758
+ // 創建一個虛擬的 canvas
759
+ const canvas = document.createElement('canvas');
760
+ const ctx = canvas.getContext('2d');
761
+ // 設置 canvas 的尺寸
762
+ canvas.width = width;
763
+ canvas.height = height;
764
+ // 使用 Uint8ClampedArray 將圖像數據繪製到 canvas 上
765
+ const imgData = ctx === null || ctx === void 0 ? void 0 : ctx.createImageData(width, height);
766
+ if (imgData) {
767
+ imgData.data.set(debugImage);
768
+ ctx === null || ctx === void 0 ? void 0 : ctx.putImageData(imgData, 0, 0);
769
+ canvas.toBlob(blob => {
770
+ if (blob) {
771
+ pushNewDebugImage(blob, {
772
+ result: result,
773
+ status: 'recognition',
774
+ type: type
775
+ }, {
776
+ prefix: 'debugImage_'
777
+ });
778
+ }
779
+ }, 'image/jpeg');
780
+ }
781
+ });
782
+ }
783
+ return {
784
+ debugMode,
785
+ debugModeUI,
786
+ pushNewDebugLog,
787
+ getDebugLogs: () => debugLogs,
788
+ getDebugLogsLength: () => debugLogs.length,
789
+ modifyDeubgLog: (index, logParams) => {
790
+ if (!debugMode) return;
791
+ debugLogs[index] = Object.assign(Object.assign({}, debugLogs[index]), logParams);
792
+ },
793
+ downloadDebugLogs: () => {
794
+ if (!debugMode) return;
795
+ saveFiles(`debugLog${new Date().getTime().toString()}`, {
796
+ info: logInfo,
797
+ logs: debugLogs,
798
+ functionRunTime: functionRunTime()
799
+ });
800
+ },
801
+ functionLogging,
802
+ nextDebugRound: type => {
803
+ if (!debugMode) return;
804
+ currentRoundId = generateUniqueId(8);
805
+ if (type) currentType = type;
806
+ },
807
+ modifyDebugType: type => {
808
+ if (!debugMode) return;
809
+ currentType = type;
810
+ },
811
+ pushNewDebugImage,
812
+ setTimeDuration: setTimeDuration,
813
+ saveDebugImage
814
+ };
815
+ }
486
816
 
487
817
  exports.TIME_UNIT = void 0;
488
818
  (function (TIME_UNIT) {
@@ -587,7 +917,7 @@ function cropByRatio(width, height, ratio) {
587
917
  };
588
918
  }
589
919
 
590
- function startSpinner(text) {
920
+ function startSpinner(text, backgroundOpaque) {
591
921
  const loadingLottie = Storage.getItem(exports.STORAGE_KEY.LOADING_LOTTIE);
592
922
  const body = document.querySelector('.authme-container');
593
923
  if (loadingLottie) {
@@ -611,6 +941,9 @@ function startSpinner(text) {
611
941
  loadingSDKText.textContent = text;
612
942
  loadingSDKContent.appendChild(loadingSDKText);
613
943
  }
944
+ if (backgroundOpaque) {
945
+ loadingSDKOuter.classList.add('authme-loading-sdk-outer--opaque');
946
+ }
614
947
  return;
615
948
  }
616
949
  const spinnerOuter = document.createElement('div');
@@ -624,6 +957,9 @@ function startSpinner(text) {
624
957
  spinnerText.textContent = text;
625
958
  spinnerOuter.appendChild(spinnerText);
626
959
  }
960
+ if (backgroundOpaque) {
961
+ spinnerOuter.classList.add('loading-outer--opaque');
962
+ }
627
963
  body === null || body === void 0 ? void 0 : body.appendChild(spinnerOuter);
628
964
  }
629
965
  function stopSpinner() {
@@ -644,7 +980,7 @@ exports.Icon = void 0;
644
980
  Icon["PictureIcon"] = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAt9SURBVHgB7VxdTFRJFq7q23/Q0NC6IKDGhrgiG4mSGAejmyDoToxri5F1ZV9Gk0lWn8CnyY4Pgw9rsk+LT2M2k+juw+KOGBU3cUYjsoljNDFRA1llZwNNEJVGpIFu+v/W1mmmtar63u6G/pHu7e9Fq/rem3s/vlPnVNU5hVAeeeSRx/8vMPqImGntLEWFfisKBZs0RNpAELIiRLaFfyTwfwYY2RHBTvqDE2P8VMahMYQ1Ty2XLwygj4iMExgmzbBwHMuaQ4tk4VKUPAYo+X9FEhqghNpRBpERAnnSUBNKL8JkWq5cuIQygLQSCMRp9N4OQkhnipS2FNgxRpdkDSUzjapMC4FLIS4gYcecVjM0p5NG3DrJMaXXOFxajftHc8Eke93P5zxrioKyqcwvl6/yBasLg6EaU4BU6wgpR7ERJrLk2wtnURqQcgJnjp1swjK6GOUEGFCChqaM2odDJ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0fGPbSGr5cVgmgTI+kGXXpNSPvDNpRSKt1aZHK8XfIBOkWpkCo3OwL1ZiD8pb4x9+Fx7rzKGjsTpXqWKT3AEaouQui44kQmXqkl7gIMnaGKpTP/lRC1oTSi4Hwfg7dkkgncRFkjMAIwqoMoabUkUnCiQCZJI1FxgkUAXNrJMnboMBnsYyAxpNQKaB0DHIY5Ck1zbFwEpRMBlDAZM80aXnkkUceeSzif3+l5taU5LriAAAAAElFTkSuQmCC";
645
981
  })(exports.Icon || (exports.Icon = {}));
646
982
 
647
- function showErrorMessage(text, showRetryBtn, callback) {
983
+ function showErrorMessage(text, showRetryBtn, callback, buttonText, _titleText) {
648
984
  return __awaiter(this, void 0, void 0, function* () {
649
985
  const target = document.querySelector('.authme-container');
650
986
  if (!target) {
@@ -663,7 +999,40 @@ function showErrorMessage(text, showRetryBtn, callback) {
663
999
  if (showRetryBtn) {
664
1000
  const retryText = document.createElement('div');
665
1001
  retryText.className = 'retry-text';
666
- retryText.textContent = '重試';
1002
+ retryText.textContent = buttonText !== null && buttonText !== void 0 ? buttonText : '重試';
1003
+ errorMessagePanel.appendChild(retryText);
1004
+ if (callback) {
1005
+ retryText.addEventListener('click', e => {
1006
+ callback(e, {
1007
+ hideErrorMessage
1008
+ });
1009
+ });
1010
+ }
1011
+ }
1012
+ target === null || target === void 0 ? void 0 : target.appendChild(errorMessagePanel);
1013
+ });
1014
+ }
1015
+ function showErrorMessageEventName(text, showRetryBtn, callback, buttonText, titleText) {
1016
+ return __awaiter(this, void 0, void 0, function* () {
1017
+ const target = document.querySelector('.authme-container');
1018
+ if (!target) {
1019
+ return;
1020
+ }
1021
+ const errorMessagePanel = document.createElement('div');
1022
+ errorMessagePanel.classList.add('error-message-panel');
1023
+ errorMessagePanel.classList.add('event-name');
1024
+ const errorText = document.createElement('div');
1025
+ errorText.className = 'error-text';
1026
+ errorText.textContent = text;
1027
+ const title = document.createElement('div');
1028
+ title.className = 'error-title';
1029
+ title.textContent = titleText !== null && titleText !== void 0 ? titleText : 'error';
1030
+ errorMessagePanel.appendChild(title);
1031
+ errorMessagePanel.appendChild(errorText);
1032
+ if (showRetryBtn) {
1033
+ const retryText = document.createElement('div');
1034
+ retryText.className = 'retry-text';
1035
+ retryText.textContent = buttonText !== null && buttonText !== void 0 ? buttonText : '重試';
667
1036
  errorMessagePanel.appendChild(retryText);
668
1037
  if (callback) {
669
1038
  retryText.addEventListener('click', e => {
@@ -682,27 +1051,29 @@ function hideErrorMessage() {
682
1051
  body === null || body === void 0 ? void 0 : body.removeChild(errorMessagePanel);
683
1052
  }
684
1053
  }
685
- function asyncShowErrorMessage(text, showRetryBtn) {
1054
+ function asyncShowErrorMessage(text, showRetryBtn, options) {
1055
+ var _a;
686
1056
  return __awaiter(this, void 0, void 0, function* () {
1057
+ const _showErrorMessage = (_a = options === null || options === void 0 ? void 0 : options.showErrorMessageHandler) !== null && _a !== void 0 ? _a : showErrorMessage;
687
1058
  return new Promise((res, rej) => {
1059
+ var _a;
688
1060
  const callback = () => {
689
1061
  res(true);
690
1062
  hideErrorMessage();
691
1063
  };
692
- showErrorMessage(text, showRetryBtn, callback);
1064
+ _showErrorMessage(text, showRetryBtn, (_a = options === null || options === void 0 ? void 0 : options.callback) !== null && _a !== void 0 ? _a : callback, options === null || options === void 0 ? void 0 : options.buttonText, options === null || options === void 0 ? void 0 : options.titleText);
693
1065
  });
694
1066
  });
695
1067
  }
696
1068
  function asyncOnLineShowErrorMessage(text, showRetryBtn) {
697
1069
  return __awaiter(this, void 0, void 0, function* () {
698
1070
  return new Promise((res, rej) => {
699
- const callback = () => {
1071
+ showErrorMessage(text, showRetryBtn, (_, tools) => {
700
1072
  if (window.navigator.onLine) {
701
1073
  res(true);
702
- hideErrorMessage();
1074
+ tools === null || tools === void 0 ? void 0 : tools.hideErrorMessage();
703
1075
  }
704
- };
705
- showErrorMessage(text, showRetryBtn, callback);
1076
+ });
706
1077
  });
707
1078
  });
708
1079
  }
@@ -770,7 +1141,10 @@ var BROWSER_CAMERA_ERRORS;
770
1141
  BROWSER_CAMERA_ERRORS["NO_CAMERA"] = "NO_CAMERA";
771
1142
  BROWSER_CAMERA_ERRORS["NOT_ALLOWED_ERROR"] = "NotAllowedError";
772
1143
  BROWSER_CAMERA_ERRORS["NOT_FOUND_ERROR"] = "NotFoundError";
1144
+ BROWSER_CAMERA_ERRORS["NOT_READABLE_ERROR"] = "NotReadableError";
1145
+ BROWSER_CAMERA_ERRORS["ABORT_ERROR"] = "AbortError";
773
1146
  })(BROWSER_CAMERA_ERRORS || (BROWSER_CAMERA_ERRORS = {}));
1147
+ let stream;
774
1148
  const videoConstraintsFactory = (isPC, facingMode) => {
775
1149
  return isPC ? {
776
1150
  video: {
@@ -851,6 +1225,43 @@ function arrayFromAsync(asyncIterable) {
851
1225
  return result;
852
1226
  });
853
1227
  }
1228
+ function switchCamera(deviceId, video) {
1229
+ return __awaiter(this, void 0, void 0, function* () {
1230
+ try {
1231
+ if (stream) {
1232
+ stream.getTracks().forEach(track => track.stop());
1233
+ }
1234
+ const constraints = {
1235
+ video: {
1236
+ // 推測依然需要使用 width & height 的限制條件,
1237
+ // 否則即使是高解析度相機,也有可能拿到低解析度的圖片。(待驗證)
1238
+ width: {
1239
+ min: 1280,
1240
+ ideal: 1920,
1241
+ max: 1920
1242
+ },
1243
+ height: {
1244
+ min: 720,
1245
+ ideal: 1080,
1246
+ max: 1080
1247
+ },
1248
+ focusMode: 'auto',
1249
+ deviceId: deviceId
1250
+ }
1251
+ };
1252
+ stream = yield navigator.mediaDevices.getUserMedia(constraints);
1253
+ video.srcObject = stream;
1254
+ yield video.play();
1255
+ // Note: Fix Safari 15 video not showing bug
1256
+ video.srcObject = null;
1257
+ setTimeout(() => {
1258
+ video.srcObject = stream;
1259
+ }, 10);
1260
+ } catch (e) {
1261
+ throw new AuthmeError(exports.ErrorCode.CAMERA_NOT_SUPPORT, e);
1262
+ }
1263
+ });
1264
+ }
854
1265
  function _requestCamera(video, facingMode) {
855
1266
  var _a;
856
1267
  return __awaiter(this, void 0, void 0, function* () {
@@ -925,39 +1336,14 @@ function _requestCamera(video, facingMode) {
925
1336
  if (!deviceId) {
926
1337
  throw BROWSER_CAMERA_ERRORS.NO_CAMERA;
927
1338
  }
928
- const constraints = {
929
- video: {
930
- // 推測依然需要使用 width & height 的限制條件,
931
- // 否則即使是高解析度相機,也有可能拿到低解析度的圖片。(待驗證)
932
- width: {
933
- min: 1280,
934
- ideal: 1920,
935
- max: 1920
936
- },
937
- height: {
938
- min: 720,
939
- ideal: 1080,
940
- max: 1080
941
- },
942
- focusMode: 'auto',
943
- deviceId: {
944
- exact: deviceId
945
- }
946
- }
1339
+ if (stream) {
1340
+ stream.getTracks().forEach(track => track.stop());
1341
+ }
1342
+ yield switchCamera(deviceId, video);
1343
+ return {
1344
+ facingMode: firstDevice.meta.facingMode,
1345
+ deviceMetas: deviceMetas
947
1346
  };
948
- debugLog('camera info', {
949
- firstDevice,
950
- deviceMetas
951
- });
952
- const stream = yield navigator.mediaDevices.getUserMedia(constraints);
953
- video.srcObject = stream;
954
- yield video.play();
955
- // Note: Fix Safari 15 video not showing bug
956
- video.srcObject = null;
957
- setTimeout(() => {
958
- video.srcObject = stream;
959
- }, 10);
960
- return firstDevice.meta.facingMode;
961
1347
  });
962
1348
  }
963
1349
  function isOverconstrainedError(error) {
@@ -991,6 +1377,15 @@ function requestCamera({
991
1377
  showMessage(translate('camera.error.notFound'));
992
1378
  throw new AuthmeError(exports.ErrorCode.CAMERA_NOT_SUPPORT, error);
993
1379
  }
1380
+ if ((error === null || error === void 0 ? void 0 : error.name) === BROWSER_CAMERA_ERRORS.NOT_READABLE_ERROR || (error === null || error === void 0 ? void 0 : error.name) === BROWSER_CAMERA_ERRORS.ABORT_ERROR) {
1381
+ showMessage(translate('camera.error.notFound'));
1382
+ /* NOT_READABLE_ERROR,ABORT_ERROR 這兩個錯誤是用戶授權了,但在呼叫裝置時出現錯誤,
1383
+ * 常發生在APP webview ,例如: 手機開啟 web 使用 sdk,儘管在 webview 層級上授權了,但手機在設定層級沒有授權 app 使用相機,會導致此問題。
1384
+ * 但每個 android 手機的在這方面的實作不一致,不是每個手機在這種情況下都會拋出錯誤。
1385
+ * 參考 https://developer.mozilla.org/zh-CN/docs/Web/API/MediaDevices/getUserMedia#notreadableerror
1386
+ */
1387
+ throw new AuthmeError(exports.ErrorCode.CAMERA_NOT_SUPPORT, error);
1388
+ }
994
1389
  if (isOverconstrainedError(error)) {
995
1390
  showMessage(translate('camera.error.lowResolution'));
996
1391
  throw new AuthmeError(exports.ErrorCode.CAMERA_NOT_SUPPORT, error);
@@ -1986,15 +2381,46 @@ function RGBToLottieColor(color) {
1986
2381
  return color.map(c => c / 255);
1987
2382
  }
1988
2383
 
2384
+ const QUEUE_LENGTH = 10;
2385
+ const requstQueue = [];
2386
+ function pushRequest(request) {
2387
+ if (requstQueue.length > QUEUE_LENGTH - 1) {
2388
+ requstQueue.shift();
2389
+ }
2390
+ requstQueue.push(request);
2391
+ }
2392
+ function clearRequest() {
2393
+ requstQueue.splice(0, requstQueue.length);
2394
+ }
2395
+ function backgroundRequest(request) {
2396
+ const requestPromise = request();
2397
+ requestPromise.catch(err => {
2398
+ if (err._code === exports.ErrorCode.NETWORK_ERROR) {
2399
+ pushRequest(request);
2400
+ }
2401
+ });
2402
+ }
2403
+ window.ononline = () => {
2404
+ const tasks = [...requstQueue];
2405
+ clearRequest();
2406
+ tasks.forEach(request => {
2407
+ backgroundRequest(request);
2408
+ });
2409
+ };
2410
+
1989
2411
  var name = "@authme/util";
1990
- var version$1 = "2.4.2";
2412
+ var version$1 = "2.4.6";
1991
2413
  var peerDependencies = {
1992
2414
  "core-js": "^3.6.0"
1993
2415
  };
2416
+ var devDependencies = {
2417
+ "@types/file-saver": "^2.0.5"
2418
+ };
1994
2419
  var packageInfo = {
1995
2420
  name: name,
1996
2421
  version: version$1,
1997
- peerDependencies: peerDependencies
2422
+ peerDependencies: peerDependencies,
2423
+ devDependencies: devDependencies
1998
2424
  };
1999
2425
 
2000
2426
  var _a;
@@ -2010,6 +2436,7 @@ exports.UintArrayToBlob = UintArrayToBlob;
2010
2436
  exports.asyncOnLineShowErrorMessage = asyncOnLineShowErrorMessage;
2011
2437
  exports.asyncShowErrorMessage = asyncShowErrorMessage;
2012
2438
  exports.asyncShowPopup = asyncShowPopup;
2439
+ exports.backgroundRequest = backgroundRequest;
2013
2440
  exports.checkOnlineStatus = checkOnlineStatus;
2014
2441
  exports.clearCanvas = clearCanvas;
2015
2442
  exports.colorStringToRGB = colorStringToRGB;
@@ -2018,6 +2445,7 @@ exports.combineResult = combineResult;
2018
2445
  exports.cropByRatio = cropByRatio;
2019
2446
  exports.dataURItoBlob = dataURItoBlob;
2020
2447
  exports.debugLog = debugLog;
2448
+ exports.debugTools = debugTools;
2021
2449
  exports.decodeToken = decodeToken;
2022
2450
  exports.getCanvasSize = getCanvasSize;
2023
2451
  exports.getCssVariable = getCssVariable;
@@ -2037,12 +2465,14 @@ exports.resize = resize;
2037
2465
  exports.retryPromiseWithCondition = retryPromiseWithCondition;
2038
2466
  exports.showElement = showElement;
2039
2467
  exports.showErrorMessage = showErrorMessage;
2468
+ exports.showErrorMessageEventName = showErrorMessageEventName;
2040
2469
  exports.showPopup = showPopup;
2041
2470
  exports.splitResult = splitResult;
2042
2471
  exports.startLoadingSDK = startLoadingSDK;
2043
2472
  exports.startSpinner = startSpinner;
2044
2473
  exports.stopLoadingSDK = stopLoadingSDK;
2045
2474
  exports.stopSpinner = stopSpinner;
2475
+ exports.switchCamera = switchCamera;
2046
2476
  exports.useState = useState;
2047
2477
  exports.version = version;
2048
2478
  exports.videoConstraintsFactory = videoConstraintsFactory;