@authme/util 2.8.30 → 2.8.32

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 (3) hide show
  1. package/index.cjs +59 -108
  2. package/index.js +59 -108
  3. package/package.json +1 -1
package/index.cjs CHANGED
@@ -1448,42 +1448,6 @@ const videoConstraintsFactory = (isPC, facingMode) => {
1448
1448
  }
1449
1449
  };
1450
1450
  };
1451
- /**
1452
- * 等待camera完全就緒
1453
- * 用於解決外接camera初始化延遲問題
1454
- * @param videoTrack - MediaStreamTrack 實例
1455
- * @param maxWaitMs - 最大等待時間(毫秒)
1456
- * @returns Promise<boolean> - true表示camera已就緒,false表示超時
1457
- */
1458
- function waitForCameraReady(videoTrack, maxWaitMs = 100) {
1459
- var _a, _b, _c, _d;
1460
- return __awaiter(this, void 0, void 0, function* () {
1461
- const startTime = Date.now();
1462
- const checkInterval = 50; // 每50ms檢查一次
1463
- while (Date.now() - startTime < maxWaitMs) {
1464
- const _settings = videoTrack.getSettings();
1465
- const capabilities = (_a = videoTrack.getCapabilities) === null || _a === void 0 ? void 0 : _a.call(videoTrack);
1466
- // 檢查camera是否已提供完整資訊
1467
- // 1. 必須有基本的寬高設定
1468
- // 2. 如果支援 getCapabilities,確認有最大解析度資訊
1469
- const hasBasicSettings = _settings.width && _settings.height;
1470
- const hasCapabilities = !capabilities || ((_b = capabilities === null || capabilities === void 0 ? void 0 : capabilities.width) === null || _b === void 0 ? void 0 : _b.max) && ((_c = capabilities === null || capabilities === void 0 ? void 0 : capabilities.height) === null || _c === void 0 ? void 0 : _c.max);
1471
- if (hasBasicSettings && hasCapabilities) {
1472
- const elapsedTime = Date.now() - startTime;
1473
- console.log(`✅ Camera就緒 (耗時: ${elapsedTime}ms, 解析度: ${_settings.width}x${_settings.height})`);
1474
- return true;
1475
- }
1476
- yield new Promise(resolve => setTimeout(resolve, checkInterval));
1477
- }
1478
- const settings = videoTrack.getSettings();
1479
- console.warn(`⚠️ Camera未在時限內完全就緒 (${maxWaitMs}ms), 當前狀態:`, {
1480
- width: settings.width,
1481
- height: settings.height,
1482
- hasCapabilities: !!((_d = videoTrack.getCapabilities) === null || _d === void 0 ? void 0 : _d.call(videoTrack))
1483
- });
1484
- return false;
1485
- });
1486
- }
1487
1451
  function inferFacingModeFromLabel(label) {
1488
1452
  const pattern = /rear|back|rück|arrière|trasera|trás|traseira|posteriore|后|後|背|задней|الخلفية|후|arka|achterzijde|หลัง|baksidan|bagside|sau|bak|tylny|takakamera|belakang|אחורית|πίσω|spate|hátsó|zadní|darrere|zadná|задня|stražnja|belakang|बैक/i;
1489
1453
  if (pattern.test(label !== null && label !== void 0 ? label : '')) {
@@ -1537,6 +1501,51 @@ function arrayFromAsync(asyncIterable) {
1537
1501
  return result;
1538
1502
  });
1539
1503
  }
1504
+ function upgradeResolution(videoTrack) {
1505
+ var _a;
1506
+ return __awaiter(this, void 0, void 0, function* () {
1507
+ const targetConstraints = {
1508
+ width: {
1509
+ ideal: 1920,
1510
+ max: 3840
1511
+ },
1512
+ height: {
1513
+ ideal: 1080,
1514
+ max: 2160
1515
+ }
1516
+ };
1517
+ const minAcceptableWidth = 1280; // 最低可接受的解析度寬度
1518
+ const maxAttempts = 300; // 最多嘗試10次
1519
+ const retryDelay = 100; // 每次重試間隔1秒
1520
+ console.log('🚀 開始提升解析度...');
1521
+ const startTime = Date.now();
1522
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
1523
+ try {
1524
+ yield videoTrack.applyConstraints(targetConstraints);
1525
+ const settings = videoTrack.getSettings();
1526
+ const currentWidth = (_a = settings.width) !== null && _a !== void 0 ? _a : 0;
1527
+ console.log(`嘗試 ${attempt}/${maxAttempts} - 解析度: ${currentWidth}x${settings.height}`);
1528
+ // 檢查是否達到可接受的解析度
1529
+ if (currentWidth >= minAcceptableWidth) {
1530
+ const elapsedTime = Date.now() - startTime;
1531
+ console.log(`✅ 解析度提升成功!最終: ${currentWidth}x${settings.height} (耗時: ${elapsedTime}ms)`);
1532
+ return; // 成功,退出
1533
+ }
1534
+ // 解析度還不夠,繼續重試
1535
+ console.log(`⚠️ 解析度 ${currentWidth} < ${minAcceptableWidth},繼續重試...`);
1536
+ } catch (e) {
1537
+ console.warn(`⚠️ 第 ${attempt} 次嘗試失敗:`, e);
1538
+ }
1539
+ // 如果不是最後一次嘗試,等待後重試
1540
+ if (attempt < maxAttempts) {
1541
+ yield new Promise(resolve => setTimeout(resolve, retryDelay));
1542
+ }
1543
+ }
1544
+ // 所有嘗試都失敗
1545
+ const finalSettings = videoTrack.getSettings();
1546
+ console.warn(`⚠️ 達到最大重試次數,使用當前解析度: ${finalSettings.width}x${finalSettings.height}`);
1547
+ });
1548
+ }
1540
1549
  function switchCamera(deviceId, video) {
1541
1550
  var _a, _b;
1542
1551
  return __awaiter(this, void 0, void 0, function* () {
@@ -1554,78 +1563,20 @@ function switchCamera(deviceId, video) {
1554
1563
  };
1555
1564
  const constraint = localStorage.getItem('camera_constraint');
1556
1565
  stream = yield navigator.mediaDevices.getUserMedia(basicConstraints);
1557
- // 第二階段:等待就緒策略 + 漸進式重試提升解析度
1558
1566
  if (!constraint && stream) {
1559
1567
  const videoTrack = stream.getVideoTracks()[0];
1560
- if (videoTrack && videoTrack.applyConstraints) {
1561
- // 🆕 策略 1: 等待camera完全就緒(特別針對外接camera)
1562
- const isReady = yield waitForCameraReady(videoTrack, 2000);
1563
- if (isReady) {
1564
- // 🆕 策略 2: 漸進式重試邏輯
1565
- const targetConstraints = {
1566
- width: {
1567
- ideal: 1920,
1568
- max: 3840
1569
- },
1570
- height: {
1571
- ideal: 1080,
1572
- max: 2160
1573
- }
1574
- };
1575
- const maxRetries = 3;
1576
- const delays = [0, 100, 300]; // 第一次立即嘗試,後續間隔遞增
1577
- for (let attempt = 0; attempt < maxRetries; attempt++) {
1578
- try {
1579
- if (delays[attempt] > 0) {
1580
- yield new Promise(resolve => setTimeout(resolve, delays[attempt]));
1581
- }
1582
- yield videoTrack.applyConstraints(targetConstraints);
1583
- const settings = videoTrack.getSettings();
1584
- console.log(`✅ 解析度提升成功 (嘗試 ${attempt + 1}/${maxRetries})`, {
1585
- resolution: `${settings.width}x${settings.height}`,
1586
- deviceLabel: videoTrack.label
1587
- });
1588
- break; // 成功後跳出
1589
- } catch (error) {
1590
- if (attempt === maxRetries - 1) {
1591
- // 最後一次嘗試失敗,記錄警告但不影響功能
1592
- const settings = videoTrack.getSettings();
1593
- console.warn(`⚠️ 無法提升解析度,使用基本設定 (${settings.width}x${settings.height})`, error);
1594
- } else {
1595
- console.log(`⏳ 解析度提升嘗試 ${attempt + 1} 失敗,準備重試...`);
1596
- }
1597
- }
1598
- }
1599
- } else {
1600
- // Camera未完全就緒,但仍嘗試套用約束
1601
- console.warn('⚠️ Camera未完全就緒,嘗試套用解析度約束...');
1602
- try {
1603
- yield videoTrack.applyConstraints({
1604
- width: {
1605
- ideal: 1920,
1606
- max: 3840
1607
- },
1608
- height: {
1609
- ideal: 1080,
1610
- max: 2160
1611
- }
1612
- });
1613
- } catch (error) {
1614
- console.warn('⚠️ 解析度提升失敗,使用預設解析度', error);
1615
- }
1616
- }
1617
- // 🆕 記錄最終解析度資訊
1618
- const finalSettings = videoTrack.getSettings();
1619
- const initTime = (performance.now() - startTime).toFixed(2);
1620
- console.log('📹 Camera初始化完成', {
1621
- deviceId,
1622
- deviceLabel: videoTrack.label,
1623
- resolution: `${finalSettings.width}x${finalSettings.height}`,
1624
- frameRate: finalSettings.frameRate,
1625
- facingMode: finalSettings.facingMode,
1626
- initTime: `${initTime}ms`
1627
- });
1628
- }
1568
+ yield upgradeResolution(videoTrack); // 加上 await 等待解析度提升完成
1569
+ // 🆕 記錄最終解析度資訊
1570
+ const finalSettings = videoTrack.getSettings();
1571
+ const initTime = (performance.now() - startTime).toFixed(2);
1572
+ console.log('📹 Camera初始化完成', {
1573
+ deviceId,
1574
+ deviceLabel: videoTrack.label,
1575
+ resolution: `${finalSettings.width}x${finalSettings.height}`,
1576
+ frameRate: finalSettings.frameRate,
1577
+ facingMode: finalSettings.facingMode,
1578
+ initTime: `${initTime}ms`
1579
+ });
1629
1580
  }
1630
1581
  // try {
1631
1582
  // stream = await navigator.mediaDevices.getUserMedia(constraints);
@@ -3363,8 +3314,8 @@ const themeConfigDefault = {
3363
3314
  };
3364
3315
 
3365
3316
  var name = "authme/sdk";
3366
- var version$1 = "2.8.30";
3367
- var date = "2025-10-30T10:27:17+0000";
3317
+ var version$1 = "2.8.32";
3318
+ var date = "2025-11-07T03:04:15+0000";
3368
3319
  var packageInfo = {
3369
3320
  name: name,
3370
3321
  version: version$1,
package/index.js CHANGED
@@ -1438,42 +1438,6 @@ const videoConstraintsFactory = (isPC, facingMode) => {
1438
1438
  }
1439
1439
  };
1440
1440
  };
1441
- /**
1442
- * 等待camera完全就緒
1443
- * 用於解決外接camera初始化延遲問題
1444
- * @param videoTrack - MediaStreamTrack 實例
1445
- * @param maxWaitMs - 最大等待時間(毫秒)
1446
- * @returns Promise<boolean> - true表示camera已就緒,false表示超時
1447
- */
1448
- function waitForCameraReady(videoTrack, maxWaitMs = 100) {
1449
- var _a, _b, _c, _d;
1450
- return __awaiter(this, void 0, void 0, function* () {
1451
- const startTime = Date.now();
1452
- const checkInterval = 50; // 每50ms檢查一次
1453
- while (Date.now() - startTime < maxWaitMs) {
1454
- const _settings = videoTrack.getSettings();
1455
- const capabilities = (_a = videoTrack.getCapabilities) === null || _a === void 0 ? void 0 : _a.call(videoTrack);
1456
- // 檢查camera是否已提供完整資訊
1457
- // 1. 必須有基本的寬高設定
1458
- // 2. 如果支援 getCapabilities,確認有最大解析度資訊
1459
- const hasBasicSettings = _settings.width && _settings.height;
1460
- const hasCapabilities = !capabilities || ((_b = capabilities === null || capabilities === void 0 ? void 0 : capabilities.width) === null || _b === void 0 ? void 0 : _b.max) && ((_c = capabilities === null || capabilities === void 0 ? void 0 : capabilities.height) === null || _c === void 0 ? void 0 : _c.max);
1461
- if (hasBasicSettings && hasCapabilities) {
1462
- const elapsedTime = Date.now() - startTime;
1463
- console.log(`✅ Camera就緒 (耗時: ${elapsedTime}ms, 解析度: ${_settings.width}x${_settings.height})`);
1464
- return true;
1465
- }
1466
- yield new Promise(resolve => setTimeout(resolve, checkInterval));
1467
- }
1468
- const settings = videoTrack.getSettings();
1469
- console.warn(`⚠️ Camera未在時限內完全就緒 (${maxWaitMs}ms), 當前狀態:`, {
1470
- width: settings.width,
1471
- height: settings.height,
1472
- hasCapabilities: !!((_d = videoTrack.getCapabilities) === null || _d === void 0 ? void 0 : _d.call(videoTrack))
1473
- });
1474
- return false;
1475
- });
1476
- }
1477
1441
  function inferFacingModeFromLabel(label) {
1478
1442
  const pattern = /rear|back|rück|arrière|trasera|trás|traseira|posteriore|后|後|背|задней|الخلفية|후|arka|achterzijde|หลัง|baksidan|bagside|sau|bak|tylny|takakamera|belakang|אחורית|πίσω|spate|hátsó|zadní|darrere|zadná|задня|stražnja|belakang|बैक/i;
1479
1443
  if (pattern.test(label !== null && label !== void 0 ? label : '')) {
@@ -1527,6 +1491,51 @@ function arrayFromAsync(asyncIterable) {
1527
1491
  return result;
1528
1492
  });
1529
1493
  }
1494
+ function upgradeResolution(videoTrack) {
1495
+ var _a;
1496
+ return __awaiter(this, void 0, void 0, function* () {
1497
+ const targetConstraints = {
1498
+ width: {
1499
+ ideal: 1920,
1500
+ max: 3840
1501
+ },
1502
+ height: {
1503
+ ideal: 1080,
1504
+ max: 2160
1505
+ }
1506
+ };
1507
+ const minAcceptableWidth = 1280; // 最低可接受的解析度寬度
1508
+ const maxAttempts = 300; // 最多嘗試10次
1509
+ const retryDelay = 100; // 每次重試間隔1秒
1510
+ console.log('🚀 開始提升解析度...');
1511
+ const startTime = Date.now();
1512
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
1513
+ try {
1514
+ yield videoTrack.applyConstraints(targetConstraints);
1515
+ const settings = videoTrack.getSettings();
1516
+ const currentWidth = (_a = settings.width) !== null && _a !== void 0 ? _a : 0;
1517
+ console.log(`嘗試 ${attempt}/${maxAttempts} - 解析度: ${currentWidth}x${settings.height}`);
1518
+ // 檢查是否達到可接受的解析度
1519
+ if (currentWidth >= minAcceptableWidth) {
1520
+ const elapsedTime = Date.now() - startTime;
1521
+ console.log(`✅ 解析度提升成功!最終: ${currentWidth}x${settings.height} (耗時: ${elapsedTime}ms)`);
1522
+ return; // 成功,退出
1523
+ }
1524
+ // 解析度還不夠,繼續重試
1525
+ console.log(`⚠️ 解析度 ${currentWidth} < ${minAcceptableWidth},繼續重試...`);
1526
+ } catch (e) {
1527
+ console.warn(`⚠️ 第 ${attempt} 次嘗試失敗:`, e);
1528
+ }
1529
+ // 如果不是最後一次嘗試,等待後重試
1530
+ if (attempt < maxAttempts) {
1531
+ yield new Promise(resolve => setTimeout(resolve, retryDelay));
1532
+ }
1533
+ }
1534
+ // 所有嘗試都失敗
1535
+ const finalSettings = videoTrack.getSettings();
1536
+ console.warn(`⚠️ 達到最大重試次數,使用當前解析度: ${finalSettings.width}x${finalSettings.height}`);
1537
+ });
1538
+ }
1530
1539
  function switchCamera(deviceId, video) {
1531
1540
  var _a, _b;
1532
1541
  return __awaiter(this, void 0, void 0, function* () {
@@ -1544,78 +1553,20 @@ function switchCamera(deviceId, video) {
1544
1553
  };
1545
1554
  const constraint = localStorage.getItem('camera_constraint');
1546
1555
  stream = yield navigator.mediaDevices.getUserMedia(basicConstraints);
1547
- // 第二階段:等待就緒策略 + 漸進式重試提升解析度
1548
1556
  if (!constraint && stream) {
1549
1557
  const videoTrack = stream.getVideoTracks()[0];
1550
- if (videoTrack && videoTrack.applyConstraints) {
1551
- // 🆕 策略 1: 等待camera完全就緒(特別針對外接camera)
1552
- const isReady = yield waitForCameraReady(videoTrack, 2000);
1553
- if (isReady) {
1554
- // 🆕 策略 2: 漸進式重試邏輯
1555
- const targetConstraints = {
1556
- width: {
1557
- ideal: 1920,
1558
- max: 3840
1559
- },
1560
- height: {
1561
- ideal: 1080,
1562
- max: 2160
1563
- }
1564
- };
1565
- const maxRetries = 3;
1566
- const delays = [0, 100, 300]; // 第一次立即嘗試,後續間隔遞增
1567
- for (let attempt = 0; attempt < maxRetries; attempt++) {
1568
- try {
1569
- if (delays[attempt] > 0) {
1570
- yield new Promise(resolve => setTimeout(resolve, delays[attempt]));
1571
- }
1572
- yield videoTrack.applyConstraints(targetConstraints);
1573
- const settings = videoTrack.getSettings();
1574
- console.log(`✅ 解析度提升成功 (嘗試 ${attempt + 1}/${maxRetries})`, {
1575
- resolution: `${settings.width}x${settings.height}`,
1576
- deviceLabel: videoTrack.label
1577
- });
1578
- break; // 成功後跳出
1579
- } catch (error) {
1580
- if (attempt === maxRetries - 1) {
1581
- // 最後一次嘗試失敗,記錄警告但不影響功能
1582
- const settings = videoTrack.getSettings();
1583
- console.warn(`⚠️ 無法提升解析度,使用基本設定 (${settings.width}x${settings.height})`, error);
1584
- } else {
1585
- console.log(`⏳ 解析度提升嘗試 ${attempt + 1} 失敗,準備重試...`);
1586
- }
1587
- }
1588
- }
1589
- } else {
1590
- // Camera未完全就緒,但仍嘗試套用約束
1591
- console.warn('⚠️ Camera未完全就緒,嘗試套用解析度約束...');
1592
- try {
1593
- yield videoTrack.applyConstraints({
1594
- width: {
1595
- ideal: 1920,
1596
- max: 3840
1597
- },
1598
- height: {
1599
- ideal: 1080,
1600
- max: 2160
1601
- }
1602
- });
1603
- } catch (error) {
1604
- console.warn('⚠️ 解析度提升失敗,使用預設解析度', error);
1605
- }
1606
- }
1607
- // 🆕 記錄最終解析度資訊
1608
- const finalSettings = videoTrack.getSettings();
1609
- const initTime = (performance.now() - startTime).toFixed(2);
1610
- console.log('📹 Camera初始化完成', {
1611
- deviceId,
1612
- deviceLabel: videoTrack.label,
1613
- resolution: `${finalSettings.width}x${finalSettings.height}`,
1614
- frameRate: finalSettings.frameRate,
1615
- facingMode: finalSettings.facingMode,
1616
- initTime: `${initTime}ms`
1617
- });
1618
- }
1558
+ yield upgradeResolution(videoTrack); // 加上 await 等待解析度提升完成
1559
+ // 🆕 記錄最終解析度資訊
1560
+ const finalSettings = videoTrack.getSettings();
1561
+ const initTime = (performance.now() - startTime).toFixed(2);
1562
+ console.log('📹 Camera初始化完成', {
1563
+ deviceId,
1564
+ deviceLabel: videoTrack.label,
1565
+ resolution: `${finalSettings.width}x${finalSettings.height}`,
1566
+ frameRate: finalSettings.frameRate,
1567
+ facingMode: finalSettings.facingMode,
1568
+ initTime: `${initTime}ms`
1569
+ });
1619
1570
  }
1620
1571
  // try {
1621
1572
  // stream = await navigator.mediaDevices.getUserMedia(constraints);
@@ -3353,8 +3304,8 @@ const themeConfigDefault = {
3353
3304
  };
3354
3305
 
3355
3306
  var name = "authme/sdk";
3356
- var version$1 = "2.8.30";
3357
- var date = "2025-10-30T10:27:17+0000";
3307
+ var version$1 = "2.8.32";
3308
+ var date = "2025-11-07T03:04:15+0000";
3358
3309
  var packageInfo = {
3359
3310
  name: name,
3360
3311
  version: version$1,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@authme/util",
3
- "version": "2.8.30",
3
+ "version": "2.8.32",
4
4
  "peerDependencies": {
5
5
  "core-js": "^3.6.0",
6
6
  "file-saver": "2.0.5",