@minto-ai/tools 1.0.60 → 1.0.62

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.
@@ -10,7 +10,8 @@ declare enum AudioPermissionErrorType {
10
10
  NOT_READABLE_ERROR = "NotReadableError",
11
11
  OVERCONSTRAINED_ERROR = "OverconstrainedError",
12
12
  SECURITY_ERROR = "SecurityError",
13
- MICROPHONE_UNKNOWN_ERROR = "MicrophoneUnknownError"
13
+ MICROPHONE_UNKNOWN_ERROR = "MicrophoneUnknownError",
14
+ MICROPHONE_TIMEOUT_ERROR = "MicrophoneTimeoutError"
14
15
  }
15
16
  declare class AudioPermissionError extends Error {
16
17
  name: AudioPermissionErrorType;
@@ -62,6 +63,7 @@ declare class AudioPermissionError extends Error {
62
63
  */
63
64
  declare class AudioPlaybackPermission {
64
65
  private userInteracted;
66
+ private executeContext;
65
67
  constructor();
66
68
  /**
67
69
  * 初始化播放权限检测
@@ -80,6 +82,11 @@ declare class AudioPlaybackPermission {
80
82
  * 根据Web Audio API最佳实践实现
81
83
  */
82
84
  requestPlaybackPermission(): Promise<void>;
85
+ /**
86
+ * 执行上下文设置
87
+ * @param context 执行上下文
88
+ */
89
+ setExecuteContext(context: AudioPermission): void;
83
90
  }
84
91
  /**
85
92
  * 麦克风权限管理类
@@ -142,6 +149,13 @@ declare class MicrophonePermission {
142
149
  * 检查是否在安全上下文中
143
150
  */
144
151
  private isSecureContext;
152
+ /**
153
+ * 带超时的getUserMedia调用
154
+ * @param constraints - 媒体约束
155
+ * @param timeoutMs - 超时时间(毫秒)
156
+ * @returns Promise<MediaStream>
157
+ */
158
+ private getUserMediaWithTimeout;
145
159
  /**
146
160
  * 请求麦克风权限
147
161
  * 根据MediaDevices.getUserMedia规范实现
@@ -156,8 +170,8 @@ declare class MicrophonePermission {
156
170
  * 统一的音频权限管理类
157
171
  */
158
172
  declare class AudioPermission {
159
- private playbackPermission;
160
- private microphonePermission;
173
+ playbackPermission: AudioPlaybackPermission;
174
+ microphonePermission: MicrophonePermission;
161
175
  constructor();
162
176
  /**
163
177
  * 请求音频播放权限
package/dist/index.js CHANGED
@@ -13288,7 +13288,7 @@ var AudioPermissionError = /* @__PURE__ */ function(_Error) {
13288
13288
  key: "create",
13289
13289
  value: function create4(name, message) {
13290
13290
  var _codeMessageMap;
13291
- var codeMessageMap = (_codeMessageMap = {}, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_codeMessageMap, "NoUserInteractionError", "需要用户交互才能播放音频"), "AutoplayBlocked", "自动播放被浏览器阻止"), "AutoplayUnknownError", "未知自动播放错误"), "SecureContextError", "麦克风权限需要在安全上下文(HTTPS)中使用"), "PermissionDeniedPermanently", "音频权限已被永久拒绝,需在浏览器设置中开启麦克风权限"), "MediaDevicesApiUnavailable", "MediaDevices API不可用"), "NotAllowedError", "麦克风权限被拒绝"), "NotReadableError", "音频设备被其他应用程序占用"), "NotFoundError", "未找到音频输入设备"), "SecurityError", "安全策略阻止访问麦克风"), _defineProperty(_defineProperty(_codeMessageMap, "OverconstrainedError", "音频设备不满足指定的约束条件"), "MicrophoneUnknownError", "未知麦克风权限错误"));
13291
+ var codeMessageMap = (_codeMessageMap = {}, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_codeMessageMap, "NoUserInteractionError", "需要用户交互才能播放音频"), "AutoplayBlocked", "自动播放被浏览器阻止"), "AutoplayUnknownError", "未知自动播放错误"), "SecureContextError", "麦克风权限需要在安全上下文(HTTPS)中使用"), "PermissionDeniedPermanently", "音频权限已被永久拒绝,需在浏览器设置中开启麦克风权限"), "MediaDevicesApiUnavailable", "MediaDevices API不可用"), "NotAllowedError", "麦克风权限被拒绝"), "NotReadableError", "音频设备被其他应用程序占用"), "NotFoundError", "未找到音频输入设备"), "SecurityError", "安全策略阻止访问麦克风"), _defineProperty(_defineProperty(_defineProperty(_codeMessageMap, "OverconstrainedError", "音频设备不满足指定的约束条件"), "MicrophoneUnknownError", "未知麦克风权限错误"), "MicrophoneTimeoutError", "获取麦克风权限超时,请检查设备权限设置或重试"));
13292
13292
  return new AudioPermissionError2(name, message || codeMessageMap[name]);
13293
13293
  }
13294
13294
  }]);
@@ -13297,6 +13297,7 @@ var AudioPlaybackPermission = /* @__PURE__ */ function() {
13297
13297
  function AudioPlaybackPermission2() {
13298
13298
  _classCallCheck(this, AudioPlaybackPermission2);
13299
13299
  _defineProperty(this, "userInteracted", false);
13300
+ _defineProperty(this, "executeContext", void 0);
13300
13301
  this.initPlaybackPermission();
13301
13302
  }
13302
13303
  return _createClass(AudioPlaybackPermission2, [{
@@ -13388,68 +13389,87 @@ var AudioPlaybackPermission = /* @__PURE__ */ function() {
13388
13389
  /* NO_USER_INTERACTION_ERROR */
13389
13390
  );
13390
13391
  case 4:
13392
+ if (!(isIos() && this.executeContext)) {
13393
+ _context2.next = 7;
13394
+ break;
13395
+ }
13396
+ _context2.next = 7;
13397
+ return this.executeContext.requestMicrophonePermission();
13398
+ case 7:
13391
13399
  audioContext = new (window.AudioContext || window.webkitAudioContext)();
13392
13400
  if (!(isIos() && audioContext.state === "suspended")) {
13393
- _context2.next = 10;
13401
+ _context2.next = 13;
13394
13402
  break;
13395
13403
  }
13396
- _context2.next = 8;
13397
- return audioContext.resume();
13398
- case 8:
13399
- _context2.next = 10;
13404
+ _context2.next = 11;
13405
+ return Promise.race([audioContext.resume(), new Promise(function(_, reject2) {
13406
+ return setTimeout(function() {
13407
+ return reject2(AudioPermissionError.create(
13408
+ "AutoplayBlocked"
13409
+ /* AUTOPLAY_BLOCKED */
13410
+ ));
13411
+ }, 3e3);
13412
+ })]);
13413
+ case 11:
13414
+ _context2.next = 13;
13400
13415
  return new Promise(function(resolve2) {
13401
13416
  return setTimeout(resolve2, 50);
13402
13417
  });
13403
- case 10:
13418
+ case 13:
13404
13419
  if (!(audioContext.state !== "running")) {
13405
- _context2.next = 12;
13420
+ _context2.next = 15;
13406
13421
  break;
13407
13422
  }
13408
13423
  throw AudioPermissionError.create(
13409
13424
  "AutoplayBlocked"
13410
13425
  /* AUTOPLAY_BLOCKED */
13411
13426
  );
13412
- case 12:
13413
- return _context2.abrupt("return", Promise.resolve());
13414
13427
  case 15:
13415
- _context2.prev = 15;
13428
+ return _context2.abrupt("return", Promise.resolve());
13429
+ case 18:
13430
+ _context2.prev = 18;
13416
13431
  _context2.t0 = _context2["catch"](1);
13417
13432
  if (!(_context2.t0 instanceof AudioPermissionError)) {
13418
- _context2.next = 19;
13433
+ _context2.next = 22;
13419
13434
  break;
13420
13435
  }
13421
13436
  return _context2.abrupt("return", Promise.reject(_context2.t0));
13422
- case 19:
13437
+ case 22:
13423
13438
  unknownError = AudioPermissionError.create("AutoplayUnknownError", _context2.t0 instanceof Error ? _context2.t0.message : void 0);
13424
13439
  return _context2.abrupt("return", Promise.reject(unknownError));
13425
- case 21:
13426
- _context2.prev = 21;
13440
+ case 24:
13441
+ _context2.prev = 24;
13427
13442
  if (!(audioContext && audioContext.state !== "closed")) {
13428
- _context2.next = 30;
13443
+ _context2.next = 33;
13429
13444
  break;
13430
13445
  }
13431
- _context2.prev = 23;
13432
- _context2.next = 26;
13446
+ _context2.prev = 26;
13447
+ _context2.next = 29;
13433
13448
  return audioContext.close();
13434
- case 26:
13435
- _context2.next = 30;
13449
+ case 29:
13450
+ _context2.next = 33;
13436
13451
  break;
13437
- case 28:
13438
- _context2.prev = 28;
13439
- _context2.t1 = _context2["catch"](23);
13440
- case 30:
13441
- return _context2.finish(21);
13442
13452
  case 31:
13453
+ _context2.prev = 31;
13454
+ _context2.t1 = _context2["catch"](26);
13455
+ case 33:
13456
+ return _context2.finish(24);
13457
+ case 34:
13443
13458
  case "end":
13444
13459
  return _context2.stop();
13445
13460
  }
13446
- }, _callee2, this, [[1, 15, 21, 31], [23, 28]]);
13461
+ }, _callee2, this, [[1, 18, 24, 34], [26, 31]]);
13447
13462
  }));
13448
13463
  function requestPlaybackPermission() {
13449
13464
  return _requestPlaybackPermission.apply(this, arguments);
13450
13465
  }
13451
13466
  return requestPlaybackPermission;
13452
13467
  }()
13468
+ }, {
13469
+ key: "setExecuteContext",
13470
+ value: function setExecuteContext(context) {
13471
+ this.executeContext = context;
13472
+ }
13453
13473
  }]);
13454
13474
  }();
13455
13475
  var MicrophonePermission = /* @__PURE__ */ function() {
@@ -13469,20 +13489,54 @@ var MicrophonePermission = /* @__PURE__ */ function() {
13469
13489
  }
13470
13490
  )
13471
13491
  /**
13472
- * 请求麦克风权限
13473
- * 根据MediaDevices.getUserMedia规范实现
13492
+ * 带超时的getUserMedia调用
13493
+ * @param constraints - 媒体约束
13494
+ * @param timeoutMs - 超时时间(毫秒)
13495
+ * @returns Promise<MediaStream>
13474
13496
  */
13475
13497
  }, {
13476
- key: "requestMicrophonePermission",
13498
+ key: "getUserMediaWithTimeout",
13477
13499
  value: function() {
13478
- var _requestMicrophonePermission = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee3() {
13479
- var stream, errorType, audioError, unknownError;
13500
+ var _getUserMediaWithTimeout = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee3(constraints) {
13501
+ var timeoutMs, _args3 = arguments;
13480
13502
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
13481
13503
  while (1) switch (_context3.prev = _context3.next) {
13482
13504
  case 0:
13483
- _context3.prev = 0;
13505
+ timeoutMs = _args3.length > 1 && _args3[1] !== void 0 ? _args3[1] : 1e4;
13506
+ return _context3.abrupt("return", new Promise(function(resolve2, reject2) {
13507
+ var timeoutId = setTimeout(function() {
13508
+ reject2(AudioPermissionError.create("MicrophoneTimeoutError", "获取麦克风权限超时(".concat(timeoutMs, "ms)")));
13509
+ }, timeoutMs);
13510
+ navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
13511
+ clearTimeout(timeoutId);
13512
+ resolve2(stream);
13513
+ })["catch"](function(error2) {
13514
+ clearTimeout(timeoutId);
13515
+ reject2(error2);
13516
+ });
13517
+ }));
13518
+ case 2:
13519
+ case "end":
13520
+ return _context3.stop();
13521
+ }
13522
+ }, _callee3);
13523
+ }));
13524
+ function getUserMediaWithTimeout(_x) {
13525
+ return _getUserMediaWithTimeout.apply(this, arguments);
13526
+ }
13527
+ return getUserMediaWithTimeout;
13528
+ }()
13529
+ }, {
13530
+ key: "requestMicrophonePermission",
13531
+ value: function() {
13532
+ var _requestMicrophonePermission = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee4() {
13533
+ var stream, errorType, audioError, unknownError;
13534
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
13535
+ while (1) switch (_context4.prev = _context4.next) {
13536
+ case 0:
13537
+ _context4.prev = 0;
13484
13538
  if (this.isSecureContext()) {
13485
- _context3.next = 3;
13539
+ _context4.next = 3;
13486
13540
  break;
13487
13541
  }
13488
13542
  throw AudioPermissionError.create(
@@ -13491,7 +13545,7 @@ var MicrophonePermission = /* @__PURE__ */ function() {
13491
13545
  );
13492
13546
  case 3:
13493
13547
  if (!this.permissionDeniedPermanently) {
13494
- _context3.next = 5;
13548
+ _context4.next = 5;
13495
13549
  break;
13496
13550
  }
13497
13551
  throw AudioPermissionError.create(
@@ -13500,13 +13554,13 @@ var MicrophonePermission = /* @__PURE__ */ function() {
13500
13554
  );
13501
13555
  case 5:
13502
13556
  if (!this.microphonePermissionGranted) {
13503
- _context3.next = 7;
13557
+ _context4.next = 7;
13504
13558
  break;
13505
13559
  }
13506
- return _context3.abrupt("return", Promise.resolve());
13560
+ return _context4.abrupt("return", Promise.resolve());
13507
13561
  case 7:
13508
13562
  if (!(!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia)) {
13509
- _context3.next = 9;
13563
+ _context4.next = 9;
13510
13564
  break;
13511
13565
  }
13512
13566
  throw AudioPermissionError.create(
@@ -13514,8 +13568,8 @@ var MicrophonePermission = /* @__PURE__ */ function() {
13514
13568
  /* MEDIA_DEVICES_API_UNAVAILABLE */
13515
13569
  );
13516
13570
  case 9:
13517
- _context3.next = 11;
13518
- return navigator.mediaDevices.getUserMedia({
13571
+ _context4.next = 11;
13572
+ return this.getUserMediaWithTimeout({
13519
13573
  audio: {
13520
13574
  echoCancellation: true,
13521
13575
  noiseSuppression: true,
@@ -13523,58 +13577,58 @@ var MicrophonePermission = /* @__PURE__ */ function() {
13523
13577
  }
13524
13578
  });
13525
13579
  case 11:
13526
- stream = _context3.sent;
13580
+ stream = _context4.sent;
13527
13581
  this.microphonePermissionGranted = true;
13528
13582
  stream.getTracks().forEach(function(track2) {
13529
13583
  track2.stop();
13530
13584
  });
13531
- return _context3.abrupt("return", Promise.resolve());
13585
+ return _context4.abrupt("return", Promise.resolve());
13532
13586
  case 17:
13533
- _context3.prev = 17;
13534
- _context3.t0 = _context3["catch"](0);
13587
+ _context4.prev = 17;
13588
+ _context4.t0 = _context4["catch"](0);
13535
13589
  this.microphonePermissionGranted = false;
13536
- if (!(_context3.t0 instanceof AudioPermissionError)) {
13537
- _context3.next = 22;
13590
+ if (!(_context4.t0 instanceof AudioPermissionError)) {
13591
+ _context4.next = 22;
13538
13592
  break;
13539
13593
  }
13540
- return _context3.abrupt("return", Promise.reject(_context3.t0));
13594
+ return _context4.abrupt("return", Promise.reject(_context4.t0));
13541
13595
  case 22:
13542
- if (!(_context3.t0 instanceof DOMException)) {
13543
- _context3.next = 40;
13596
+ if (!(_context4.t0 instanceof DOMException)) {
13597
+ _context4.next = 40;
13544
13598
  break;
13545
13599
  }
13546
- _context3.t1 = _context3.t0.name;
13547
- _context3.next = _context3.t1 === "NotFoundError" ? 26 : _context3.t1 === "NotAllowedError" ? 28 : _context3.t1 === "NotReadableError" ? 31 : _context3.t1 === "OverconstrainedError" ? 33 : _context3.t1 === "SecurityError" ? 35 : 37;
13600
+ _context4.t1 = _context4.t0.name;
13601
+ _context4.next = _context4.t1 === "NotFoundError" ? 26 : _context4.t1 === "NotAllowedError" ? 28 : _context4.t1 === "NotReadableError" ? 31 : _context4.t1 === "OverconstrainedError" ? 33 : _context4.t1 === "SecurityError" ? 35 : 37;
13548
13602
  break;
13549
13603
  case 26:
13550
13604
  errorType = "NotFoundError";
13551
- return _context3.abrupt("break", 38);
13605
+ return _context4.abrupt("break", 38);
13552
13606
  case 28:
13553
13607
  this.permissionDeniedPermanently = true;
13554
13608
  errorType = "NotAllowedError";
13555
- return _context3.abrupt("break", 38);
13609
+ return _context4.abrupt("break", 38);
13556
13610
  case 31:
13557
13611
  errorType = "NotReadableError";
13558
- return _context3.abrupt("break", 38);
13612
+ return _context4.abrupt("break", 38);
13559
13613
  case 33:
13560
13614
  errorType = "OverconstrainedError";
13561
- return _context3.abrupt("break", 38);
13615
+ return _context4.abrupt("break", 38);
13562
13616
  case 35:
13563
13617
  errorType = "SecurityError";
13564
- return _context3.abrupt("break", 38);
13618
+ return _context4.abrupt("break", 38);
13565
13619
  case 37:
13566
13620
  errorType = "MicrophoneUnknownError";
13567
13621
  case 38:
13568
13622
  audioError = AudioPermissionError.create(errorType);
13569
- return _context3.abrupt("return", Promise.reject(audioError));
13623
+ return _context4.abrupt("return", Promise.reject(audioError));
13570
13624
  case 40:
13571
- unknownError = AudioPermissionError.create("MicrophoneUnknownError", _context3.t0 instanceof Error ? _context3.t0.message : void 0);
13572
- return _context3.abrupt("return", Promise.reject(unknownError));
13625
+ unknownError = AudioPermissionError.create("MicrophoneUnknownError", _context4.t0 instanceof Error ? _context4.t0.message : void 0);
13626
+ return _context4.abrupt("return", Promise.reject(unknownError));
13573
13627
  case 42:
13574
13628
  case "end":
13575
- return _context3.stop();
13629
+ return _context4.stop();
13576
13630
  }
13577
- }, _callee3, this, [[0, 17]]);
13631
+ }, _callee4, this, [[0, 17]]);
13578
13632
  }));
13579
13633
  function requestMicrophonePermission() {
13580
13634
  return _requestMicrophonePermission.apply(this, arguments);
@@ -13595,20 +13649,21 @@ var AudioPermission = /* @__PURE__ */ function() {
13595
13649
  _defineProperty(this, "microphonePermission", void 0);
13596
13650
  this.playbackPermission = new AudioPlaybackPermission();
13597
13651
  this.microphonePermission = new MicrophonePermission();
13652
+ this.playbackPermission.setExecuteContext(this);
13598
13653
  }
13599
13654
  return _createClass(AudioPermission2, [{
13600
13655
  key: "requestPlaybackPermission",
13601
13656
  value: function() {
13602
- var _requestPlaybackPermission2 = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee4() {
13603
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
13604
- while (1) switch (_context4.prev = _context4.next) {
13657
+ var _requestPlaybackPermission2 = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee5() {
13658
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
13659
+ while (1) switch (_context5.prev = _context5.next) {
13605
13660
  case 0:
13606
- return _context4.abrupt("return", this.playbackPermission.requestPlaybackPermission());
13661
+ return _context5.abrupt("return", this.playbackPermission.requestPlaybackPermission());
13607
13662
  case 1:
13608
13663
  case "end":
13609
- return _context4.stop();
13664
+ return _context5.stop();
13610
13665
  }
13611
- }, _callee4, this);
13666
+ }, _callee5, this);
13612
13667
  }));
13613
13668
  function requestPlaybackPermission() {
13614
13669
  return _requestPlaybackPermission2.apply(this, arguments);
@@ -13618,16 +13673,16 @@ var AudioPermission = /* @__PURE__ */ function() {
13618
13673
  }, {
13619
13674
  key: "requestMicrophonePermission",
13620
13675
  value: function() {
13621
- var _requestMicrophonePermission2 = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee5() {
13622
- return _regeneratorRuntime().wrap(function _callee5$(_context5) {
13623
- while (1) switch (_context5.prev = _context5.next) {
13676
+ var _requestMicrophonePermission2 = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee6() {
13677
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
13678
+ while (1) switch (_context6.prev = _context6.next) {
13624
13679
  case 0:
13625
- return _context5.abrupt("return", this.microphonePermission.requestMicrophonePermission());
13680
+ return _context6.abrupt("return", this.microphonePermission.requestMicrophonePermission());
13626
13681
  case 1:
13627
13682
  case "end":
13628
- return _context5.stop();
13683
+ return _context6.stop();
13629
13684
  }
13630
- }, _callee5, this);
13685
+ }, _callee6, this);
13631
13686
  }));
13632
13687
  function requestMicrophonePermission() {
13633
13688
  return _requestMicrophonePermission2.apply(this, arguments);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@minto-ai/tools",
3
3
  "type": "module",
4
- "version": "1.0.60",
4
+ "version": "1.0.62",
5
5
  "description": "明途公共工具库",
6
6
  "author": "hcc",
7
7
  "license": "ISC",