@minto-ai/tools 1.0.59 → 1.0.61

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,18 +149,29 @@ 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规范实现
148
162
  */
149
163
  requestMicrophonePermission(): Promise<void>;
164
+ /**
165
+ * 检查麦克风权限状态
166
+ */
167
+ isMicrophonePermissionGranted(): boolean;
150
168
  }
151
169
  /**
152
170
  * 统一的音频权限管理类
153
171
  */
154
172
  declare class AudioPermission {
155
- private playbackPermission;
156
- private microphonePermission;
173
+ playbackPermission: AudioPlaybackPermission;
174
+ microphonePermission: MicrophonePermission;
157
175
  constructor();
158
176
  /**
159
177
  * 请求音频播放权限
@@ -163,6 +181,10 @@ declare class AudioPermission {
163
181
  * 请求麦克风权限
164
182
  */
165
183
  requestMicrophonePermission(): Promise<void>;
184
+ /**
185
+ * 检查麦克风权限状态
186
+ */
187
+ isMicrophonePermissionGranted(): boolean;
166
188
  }
167
189
  /**
168
190
  * 创建或获取音频权限管理实例
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, [{
@@ -13354,7 +13355,6 @@ var AudioPlaybackPermission = /* @__PURE__ */ function() {
13354
13355
  var interactionEvents = ["click", "touchstart", "touchend", "keydown", "mousedown", "pointerdown"];
13355
13356
  var _handleInteraction = function handleInteraction() {
13356
13357
  _this3.userInteracted = true;
13357
- console.log(_this3.userInteracted);
13358
13358
  interactionEvents.forEach(function(eventType) {
13359
13359
  document.removeEventListener(eventType, _handleInteraction);
13360
13360
  });
@@ -13389,68 +13389,80 @@ var AudioPlaybackPermission = /* @__PURE__ */ function() {
13389
13389
  /* NO_USER_INTERACTION_ERROR */
13390
13390
  );
13391
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:
13392
13399
  audioContext = new (window.AudioContext || window.webkitAudioContext)();
13393
13400
  if (!(isIos() && audioContext.state === "suspended")) {
13394
- _context2.next = 10;
13401
+ _context2.next = 13;
13395
13402
  break;
13396
13403
  }
13397
- _context2.next = 8;
13404
+ _context2.next = 11;
13398
13405
  return audioContext.resume();
13399
- case 8:
13400
- _context2.next = 10;
13406
+ case 11:
13407
+ _context2.next = 13;
13401
13408
  return new Promise(function(resolve2) {
13402
13409
  return setTimeout(resolve2, 50);
13403
13410
  });
13404
- case 10:
13411
+ case 13:
13405
13412
  if (!(audioContext.state !== "running")) {
13406
- _context2.next = 12;
13413
+ _context2.next = 15;
13407
13414
  break;
13408
13415
  }
13409
13416
  throw AudioPermissionError.create(
13410
13417
  "AutoplayBlocked"
13411
13418
  /* AUTOPLAY_BLOCKED */
13412
13419
  );
13413
- case 12:
13414
- return _context2.abrupt("return", Promise.resolve());
13415
13420
  case 15:
13416
- _context2.prev = 15;
13421
+ return _context2.abrupt("return", Promise.resolve());
13422
+ case 18:
13423
+ _context2.prev = 18;
13417
13424
  _context2.t0 = _context2["catch"](1);
13418
13425
  if (!(_context2.t0 instanceof AudioPermissionError)) {
13419
- _context2.next = 19;
13426
+ _context2.next = 22;
13420
13427
  break;
13421
13428
  }
13422
13429
  return _context2.abrupt("return", Promise.reject(_context2.t0));
13423
- case 19:
13430
+ case 22:
13424
13431
  unknownError = AudioPermissionError.create("AutoplayUnknownError", _context2.t0 instanceof Error ? _context2.t0.message : void 0);
13425
13432
  return _context2.abrupt("return", Promise.reject(unknownError));
13426
- case 21:
13427
- _context2.prev = 21;
13433
+ case 24:
13434
+ _context2.prev = 24;
13428
13435
  if (!(audioContext && audioContext.state !== "closed")) {
13429
- _context2.next = 30;
13436
+ _context2.next = 33;
13430
13437
  break;
13431
13438
  }
13432
- _context2.prev = 23;
13433
- _context2.next = 26;
13439
+ _context2.prev = 26;
13440
+ _context2.next = 29;
13434
13441
  return audioContext.close();
13435
- case 26:
13436
- _context2.next = 30;
13442
+ case 29:
13443
+ _context2.next = 33;
13437
13444
  break;
13438
- case 28:
13439
- _context2.prev = 28;
13440
- _context2.t1 = _context2["catch"](23);
13441
- case 30:
13442
- return _context2.finish(21);
13443
13445
  case 31:
13446
+ _context2.prev = 31;
13447
+ _context2.t1 = _context2["catch"](26);
13448
+ case 33:
13449
+ return _context2.finish(24);
13450
+ case 34:
13444
13451
  case "end":
13445
13452
  return _context2.stop();
13446
13453
  }
13447
- }, _callee2, this, [[1, 15, 21, 31], [23, 28]]);
13454
+ }, _callee2, this, [[1, 18, 24, 34], [26, 31]]);
13448
13455
  }));
13449
13456
  function requestPlaybackPermission() {
13450
13457
  return _requestPlaybackPermission.apply(this, arguments);
13451
13458
  }
13452
13459
  return requestPlaybackPermission;
13453
13460
  }()
13461
+ }, {
13462
+ key: "setExecuteContext",
13463
+ value: function setExecuteContext(context) {
13464
+ this.executeContext = context;
13465
+ }
13454
13466
  }]);
13455
13467
  }();
13456
13468
  var MicrophonePermission = /* @__PURE__ */ function() {
@@ -13470,20 +13482,54 @@ var MicrophonePermission = /* @__PURE__ */ function() {
13470
13482
  }
13471
13483
  )
13472
13484
  /**
13473
- * 请求麦克风权限
13474
- * 根据MediaDevices.getUserMedia规范实现
13485
+ * 带超时的getUserMedia调用
13486
+ * @param constraints - 媒体约束
13487
+ * @param timeoutMs - 超时时间(毫秒)
13488
+ * @returns Promise<MediaStream>
13475
13489
  */
13476
13490
  }, {
13477
- key: "requestMicrophonePermission",
13491
+ key: "getUserMediaWithTimeout",
13478
13492
  value: function() {
13479
- var _requestMicrophonePermission = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee3() {
13480
- var stream, errorType, audioError, unknownError;
13493
+ var _getUserMediaWithTimeout = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee3(constraints) {
13494
+ var timeoutMs, _args3 = arguments;
13481
13495
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
13482
13496
  while (1) switch (_context3.prev = _context3.next) {
13483
13497
  case 0:
13484
- _context3.prev = 0;
13498
+ timeoutMs = _args3.length > 1 && _args3[1] !== void 0 ? _args3[1] : 1e4;
13499
+ return _context3.abrupt("return", new Promise(function(resolve2, reject2) {
13500
+ var timeoutId = setTimeout(function() {
13501
+ reject2(AudioPermissionError.create("MicrophoneTimeoutError", "获取麦克风权限超时(".concat(timeoutMs, "ms)")));
13502
+ }, timeoutMs);
13503
+ navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
13504
+ clearTimeout(timeoutId);
13505
+ resolve2(stream);
13506
+ })["catch"](function(error2) {
13507
+ clearTimeout(timeoutId);
13508
+ reject2(error2);
13509
+ });
13510
+ }));
13511
+ case 2:
13512
+ case "end":
13513
+ return _context3.stop();
13514
+ }
13515
+ }, _callee3);
13516
+ }));
13517
+ function getUserMediaWithTimeout(_x) {
13518
+ return _getUserMediaWithTimeout.apply(this, arguments);
13519
+ }
13520
+ return getUserMediaWithTimeout;
13521
+ }()
13522
+ }, {
13523
+ key: "requestMicrophonePermission",
13524
+ value: function() {
13525
+ var _requestMicrophonePermission = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee4() {
13526
+ var stream, errorType, audioError, unknownError;
13527
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
13528
+ while (1) switch (_context4.prev = _context4.next) {
13529
+ case 0:
13530
+ _context4.prev = 0;
13485
13531
  if (this.isSecureContext()) {
13486
- _context3.next = 3;
13532
+ _context4.next = 3;
13487
13533
  break;
13488
13534
  }
13489
13535
  throw AudioPermissionError.create(
@@ -13492,7 +13538,7 @@ var MicrophonePermission = /* @__PURE__ */ function() {
13492
13538
  );
13493
13539
  case 3:
13494
13540
  if (!this.permissionDeniedPermanently) {
13495
- _context3.next = 5;
13541
+ _context4.next = 5;
13496
13542
  break;
13497
13543
  }
13498
13544
  throw AudioPermissionError.create(
@@ -13501,13 +13547,13 @@ var MicrophonePermission = /* @__PURE__ */ function() {
13501
13547
  );
13502
13548
  case 5:
13503
13549
  if (!this.microphonePermissionGranted) {
13504
- _context3.next = 7;
13550
+ _context4.next = 7;
13505
13551
  break;
13506
13552
  }
13507
- return _context3.abrupt("return", Promise.resolve());
13553
+ return _context4.abrupt("return", Promise.resolve());
13508
13554
  case 7:
13509
13555
  if (!(!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia)) {
13510
- _context3.next = 9;
13556
+ _context4.next = 9;
13511
13557
  break;
13512
13558
  }
13513
13559
  throw AudioPermissionError.create(
@@ -13515,8 +13561,8 @@ var MicrophonePermission = /* @__PURE__ */ function() {
13515
13561
  /* MEDIA_DEVICES_API_UNAVAILABLE */
13516
13562
  );
13517
13563
  case 9:
13518
- _context3.next = 11;
13519
- return navigator.mediaDevices.getUserMedia({
13564
+ _context4.next = 11;
13565
+ return this.getUserMediaWithTimeout({
13520
13566
  audio: {
13521
13567
  echoCancellation: true,
13522
13568
  noiseSuppression: true,
@@ -13524,64 +13570,69 @@ var MicrophonePermission = /* @__PURE__ */ function() {
13524
13570
  }
13525
13571
  });
13526
13572
  case 11:
13527
- stream = _context3.sent;
13573
+ stream = _context4.sent;
13528
13574
  this.microphonePermissionGranted = true;
13529
13575
  stream.getTracks().forEach(function(track2) {
13530
13576
  track2.stop();
13531
13577
  });
13532
- return _context3.abrupt("return", Promise.resolve());
13578
+ return _context4.abrupt("return", Promise.resolve());
13533
13579
  case 17:
13534
- _context3.prev = 17;
13535
- _context3.t0 = _context3["catch"](0);
13580
+ _context4.prev = 17;
13581
+ _context4.t0 = _context4["catch"](0);
13536
13582
  this.microphonePermissionGranted = false;
13537
- if (!(_context3.t0 instanceof AudioPermissionError)) {
13538
- _context3.next = 22;
13583
+ if (!(_context4.t0 instanceof AudioPermissionError)) {
13584
+ _context4.next = 22;
13539
13585
  break;
13540
13586
  }
13541
- return _context3.abrupt("return", Promise.reject(_context3.t0));
13587
+ return _context4.abrupt("return", Promise.reject(_context4.t0));
13542
13588
  case 22:
13543
- if (!(_context3.t0 instanceof DOMException)) {
13544
- _context3.next = 40;
13589
+ if (!(_context4.t0 instanceof DOMException)) {
13590
+ _context4.next = 40;
13545
13591
  break;
13546
13592
  }
13547
- _context3.t1 = _context3.t0.name;
13548
- _context3.next = _context3.t1 === "NotFoundError" ? 26 : _context3.t1 === "NotAllowedError" ? 28 : _context3.t1 === "NotReadableError" ? 31 : _context3.t1 === "OverconstrainedError" ? 33 : _context3.t1 === "SecurityError" ? 35 : 37;
13593
+ _context4.t1 = _context4.t0.name;
13594
+ _context4.next = _context4.t1 === "NotFoundError" ? 26 : _context4.t1 === "NotAllowedError" ? 28 : _context4.t1 === "NotReadableError" ? 31 : _context4.t1 === "OverconstrainedError" ? 33 : _context4.t1 === "SecurityError" ? 35 : 37;
13549
13595
  break;
13550
13596
  case 26:
13551
13597
  errorType = "NotFoundError";
13552
- return _context3.abrupt("break", 38);
13598
+ return _context4.abrupt("break", 38);
13553
13599
  case 28:
13554
13600
  this.permissionDeniedPermanently = true;
13555
13601
  errorType = "NotAllowedError";
13556
- return _context3.abrupt("break", 38);
13602
+ return _context4.abrupt("break", 38);
13557
13603
  case 31:
13558
13604
  errorType = "NotReadableError";
13559
- return _context3.abrupt("break", 38);
13605
+ return _context4.abrupt("break", 38);
13560
13606
  case 33:
13561
13607
  errorType = "OverconstrainedError";
13562
- return _context3.abrupt("break", 38);
13608
+ return _context4.abrupt("break", 38);
13563
13609
  case 35:
13564
13610
  errorType = "SecurityError";
13565
- return _context3.abrupt("break", 38);
13611
+ return _context4.abrupt("break", 38);
13566
13612
  case 37:
13567
13613
  errorType = "MicrophoneUnknownError";
13568
13614
  case 38:
13569
13615
  audioError = AudioPermissionError.create(errorType);
13570
- return _context3.abrupt("return", Promise.reject(audioError));
13616
+ return _context4.abrupt("return", Promise.reject(audioError));
13571
13617
  case 40:
13572
- unknownError = AudioPermissionError.create("MicrophoneUnknownError", _context3.t0 instanceof Error ? _context3.t0.message : void 0);
13573
- return _context3.abrupt("return", Promise.reject(unknownError));
13618
+ unknownError = AudioPermissionError.create("MicrophoneUnknownError", _context4.t0 instanceof Error ? _context4.t0.message : void 0);
13619
+ return _context4.abrupt("return", Promise.reject(unknownError));
13574
13620
  case 42:
13575
13621
  case "end":
13576
- return _context3.stop();
13622
+ return _context4.stop();
13577
13623
  }
13578
- }, _callee3, this, [[0, 17]]);
13624
+ }, _callee4, this, [[0, 17]]);
13579
13625
  }));
13580
13626
  function requestMicrophonePermission() {
13581
13627
  return _requestMicrophonePermission.apply(this, arguments);
13582
13628
  }
13583
13629
  return requestMicrophonePermission;
13584
13630
  }()
13631
+ }, {
13632
+ key: "isMicrophonePermissionGranted",
13633
+ value: function isMicrophonePermissionGranted() {
13634
+ return this.microphonePermissionGranted;
13635
+ }
13585
13636
  }]);
13586
13637
  }();
13587
13638
  var AudioPermission = /* @__PURE__ */ function() {
@@ -13591,20 +13642,21 @@ var AudioPermission = /* @__PURE__ */ function() {
13591
13642
  _defineProperty(this, "microphonePermission", void 0);
13592
13643
  this.playbackPermission = new AudioPlaybackPermission();
13593
13644
  this.microphonePermission = new MicrophonePermission();
13645
+ this.playbackPermission.setExecuteContext(this);
13594
13646
  }
13595
13647
  return _createClass(AudioPermission2, [{
13596
13648
  key: "requestPlaybackPermission",
13597
13649
  value: function() {
13598
- var _requestPlaybackPermission2 = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee4() {
13599
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
13600
- while (1) switch (_context4.prev = _context4.next) {
13650
+ var _requestPlaybackPermission2 = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee5() {
13651
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
13652
+ while (1) switch (_context5.prev = _context5.next) {
13601
13653
  case 0:
13602
- return _context4.abrupt("return", this.playbackPermission.requestPlaybackPermission());
13654
+ return _context5.abrupt("return", this.playbackPermission.requestPlaybackPermission());
13603
13655
  case 1:
13604
13656
  case "end":
13605
- return _context4.stop();
13657
+ return _context5.stop();
13606
13658
  }
13607
- }, _callee4, this);
13659
+ }, _callee5, this);
13608
13660
  }));
13609
13661
  function requestPlaybackPermission() {
13610
13662
  return _requestPlaybackPermission2.apply(this, arguments);
@@ -13614,22 +13666,27 @@ var AudioPermission = /* @__PURE__ */ function() {
13614
13666
  }, {
13615
13667
  key: "requestMicrophonePermission",
13616
13668
  value: function() {
13617
- var _requestMicrophonePermission2 = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee5() {
13618
- return _regeneratorRuntime().wrap(function _callee5$(_context5) {
13619
- while (1) switch (_context5.prev = _context5.next) {
13669
+ var _requestMicrophonePermission2 = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee6() {
13670
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
13671
+ while (1) switch (_context6.prev = _context6.next) {
13620
13672
  case 0:
13621
- return _context5.abrupt("return", this.microphonePermission.requestMicrophonePermission());
13673
+ return _context6.abrupt("return", this.microphonePermission.requestMicrophonePermission());
13622
13674
  case 1:
13623
13675
  case "end":
13624
- return _context5.stop();
13676
+ return _context6.stop();
13625
13677
  }
13626
- }, _callee5, this);
13678
+ }, _callee6, this);
13627
13679
  }));
13628
13680
  function requestMicrophonePermission() {
13629
13681
  return _requestMicrophonePermission2.apply(this, arguments);
13630
13682
  }
13631
13683
  return requestMicrophonePermission;
13632
13684
  }()
13685
+ }, {
13686
+ key: "isMicrophonePermissionGranted",
13687
+ value: function isMicrophonePermissionGranted() {
13688
+ return this.microphonePermission.isMicrophonePermissionGranted();
13689
+ }
13633
13690
  }]);
13634
13691
  }();
13635
13692
  var audioPermissionInstance = null;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@minto-ai/tools",
3
3
  "type": "module",
4
- "version": "1.0.59",
4
+ "version": "1.0.61",
5
5
  "description": "明途公共工具库",
6
6
  "author": "hcc",
7
7
  "license": "ISC",