@minto-ai/tools 1.0.60 → 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,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,80 @@ 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;
13404
+ _context2.next = 11;
13397
13405
  return audioContext.resume();
13398
- case 8:
13399
- _context2.next = 10;
13406
+ case 11:
13407
+ _context2.next = 13;
13400
13408
  return new Promise(function(resolve2) {
13401
13409
  return setTimeout(resolve2, 50);
13402
13410
  });
13403
- case 10:
13411
+ case 13:
13404
13412
  if (!(audioContext.state !== "running")) {
13405
- _context2.next = 12;
13413
+ _context2.next = 15;
13406
13414
  break;
13407
13415
  }
13408
13416
  throw AudioPermissionError.create(
13409
13417
  "AutoplayBlocked"
13410
13418
  /* AUTOPLAY_BLOCKED */
13411
13419
  );
13412
- case 12:
13413
- return _context2.abrupt("return", Promise.resolve());
13414
13420
  case 15:
13415
- _context2.prev = 15;
13421
+ return _context2.abrupt("return", Promise.resolve());
13422
+ case 18:
13423
+ _context2.prev = 18;
13416
13424
  _context2.t0 = _context2["catch"](1);
13417
13425
  if (!(_context2.t0 instanceof AudioPermissionError)) {
13418
- _context2.next = 19;
13426
+ _context2.next = 22;
13419
13427
  break;
13420
13428
  }
13421
13429
  return _context2.abrupt("return", Promise.reject(_context2.t0));
13422
- case 19:
13430
+ case 22:
13423
13431
  unknownError = AudioPermissionError.create("AutoplayUnknownError", _context2.t0 instanceof Error ? _context2.t0.message : void 0);
13424
13432
  return _context2.abrupt("return", Promise.reject(unknownError));
13425
- case 21:
13426
- _context2.prev = 21;
13433
+ case 24:
13434
+ _context2.prev = 24;
13427
13435
  if (!(audioContext && audioContext.state !== "closed")) {
13428
- _context2.next = 30;
13436
+ _context2.next = 33;
13429
13437
  break;
13430
13438
  }
13431
- _context2.prev = 23;
13432
- _context2.next = 26;
13439
+ _context2.prev = 26;
13440
+ _context2.next = 29;
13433
13441
  return audioContext.close();
13434
- case 26:
13435
- _context2.next = 30;
13442
+ case 29:
13443
+ _context2.next = 33;
13436
13444
  break;
13437
- case 28:
13438
- _context2.prev = 28;
13439
- _context2.t1 = _context2["catch"](23);
13440
- case 30:
13441
- return _context2.finish(21);
13442
13445
  case 31:
13446
+ _context2.prev = 31;
13447
+ _context2.t1 = _context2["catch"](26);
13448
+ case 33:
13449
+ return _context2.finish(24);
13450
+ case 34:
13443
13451
  case "end":
13444
13452
  return _context2.stop();
13445
13453
  }
13446
- }, _callee2, this, [[1, 15, 21, 31], [23, 28]]);
13454
+ }, _callee2, this, [[1, 18, 24, 34], [26, 31]]);
13447
13455
  }));
13448
13456
  function requestPlaybackPermission() {
13449
13457
  return _requestPlaybackPermission.apply(this, arguments);
13450
13458
  }
13451
13459
  return requestPlaybackPermission;
13452
13460
  }()
13461
+ }, {
13462
+ key: "setExecuteContext",
13463
+ value: function setExecuteContext(context) {
13464
+ this.executeContext = context;
13465
+ }
13453
13466
  }]);
13454
13467
  }();
13455
13468
  var MicrophonePermission = /* @__PURE__ */ function() {
@@ -13469,20 +13482,54 @@ var MicrophonePermission = /* @__PURE__ */ function() {
13469
13482
  }
13470
13483
  )
13471
13484
  /**
13472
- * 请求麦克风权限
13473
- * 根据MediaDevices.getUserMedia规范实现
13485
+ * 带超时的getUserMedia调用
13486
+ * @param constraints - 媒体约束
13487
+ * @param timeoutMs - 超时时间(毫秒)
13488
+ * @returns Promise<MediaStream>
13474
13489
  */
13475
13490
  }, {
13476
- key: "requestMicrophonePermission",
13491
+ key: "getUserMediaWithTimeout",
13477
13492
  value: function() {
13478
- var _requestMicrophonePermission = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee3() {
13479
- var stream, errorType, audioError, unknownError;
13493
+ var _getUserMediaWithTimeout = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee3(constraints) {
13494
+ var timeoutMs, _args3 = arguments;
13480
13495
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
13481
13496
  while (1) switch (_context3.prev = _context3.next) {
13482
13497
  case 0:
13483
- _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;
13484
13531
  if (this.isSecureContext()) {
13485
- _context3.next = 3;
13532
+ _context4.next = 3;
13486
13533
  break;
13487
13534
  }
13488
13535
  throw AudioPermissionError.create(
@@ -13491,7 +13538,7 @@ var MicrophonePermission = /* @__PURE__ */ function() {
13491
13538
  );
13492
13539
  case 3:
13493
13540
  if (!this.permissionDeniedPermanently) {
13494
- _context3.next = 5;
13541
+ _context4.next = 5;
13495
13542
  break;
13496
13543
  }
13497
13544
  throw AudioPermissionError.create(
@@ -13500,13 +13547,13 @@ var MicrophonePermission = /* @__PURE__ */ function() {
13500
13547
  );
13501
13548
  case 5:
13502
13549
  if (!this.microphonePermissionGranted) {
13503
- _context3.next = 7;
13550
+ _context4.next = 7;
13504
13551
  break;
13505
13552
  }
13506
- return _context3.abrupt("return", Promise.resolve());
13553
+ return _context4.abrupt("return", Promise.resolve());
13507
13554
  case 7:
13508
13555
  if (!(!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia)) {
13509
- _context3.next = 9;
13556
+ _context4.next = 9;
13510
13557
  break;
13511
13558
  }
13512
13559
  throw AudioPermissionError.create(
@@ -13514,8 +13561,8 @@ var MicrophonePermission = /* @__PURE__ */ function() {
13514
13561
  /* MEDIA_DEVICES_API_UNAVAILABLE */
13515
13562
  );
13516
13563
  case 9:
13517
- _context3.next = 11;
13518
- return navigator.mediaDevices.getUserMedia({
13564
+ _context4.next = 11;
13565
+ return this.getUserMediaWithTimeout({
13519
13566
  audio: {
13520
13567
  echoCancellation: true,
13521
13568
  noiseSuppression: true,
@@ -13523,58 +13570,58 @@ var MicrophonePermission = /* @__PURE__ */ function() {
13523
13570
  }
13524
13571
  });
13525
13572
  case 11:
13526
- stream = _context3.sent;
13573
+ stream = _context4.sent;
13527
13574
  this.microphonePermissionGranted = true;
13528
13575
  stream.getTracks().forEach(function(track2) {
13529
13576
  track2.stop();
13530
13577
  });
13531
- return _context3.abrupt("return", Promise.resolve());
13578
+ return _context4.abrupt("return", Promise.resolve());
13532
13579
  case 17:
13533
- _context3.prev = 17;
13534
- _context3.t0 = _context3["catch"](0);
13580
+ _context4.prev = 17;
13581
+ _context4.t0 = _context4["catch"](0);
13535
13582
  this.microphonePermissionGranted = false;
13536
- if (!(_context3.t0 instanceof AudioPermissionError)) {
13537
- _context3.next = 22;
13583
+ if (!(_context4.t0 instanceof AudioPermissionError)) {
13584
+ _context4.next = 22;
13538
13585
  break;
13539
13586
  }
13540
- return _context3.abrupt("return", Promise.reject(_context3.t0));
13587
+ return _context4.abrupt("return", Promise.reject(_context4.t0));
13541
13588
  case 22:
13542
- if (!(_context3.t0 instanceof DOMException)) {
13543
- _context3.next = 40;
13589
+ if (!(_context4.t0 instanceof DOMException)) {
13590
+ _context4.next = 40;
13544
13591
  break;
13545
13592
  }
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;
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;
13548
13595
  break;
13549
13596
  case 26:
13550
13597
  errorType = "NotFoundError";
13551
- return _context3.abrupt("break", 38);
13598
+ return _context4.abrupt("break", 38);
13552
13599
  case 28:
13553
13600
  this.permissionDeniedPermanently = true;
13554
13601
  errorType = "NotAllowedError";
13555
- return _context3.abrupt("break", 38);
13602
+ return _context4.abrupt("break", 38);
13556
13603
  case 31:
13557
13604
  errorType = "NotReadableError";
13558
- return _context3.abrupt("break", 38);
13605
+ return _context4.abrupt("break", 38);
13559
13606
  case 33:
13560
13607
  errorType = "OverconstrainedError";
13561
- return _context3.abrupt("break", 38);
13608
+ return _context4.abrupt("break", 38);
13562
13609
  case 35:
13563
13610
  errorType = "SecurityError";
13564
- return _context3.abrupt("break", 38);
13611
+ return _context4.abrupt("break", 38);
13565
13612
  case 37:
13566
13613
  errorType = "MicrophoneUnknownError";
13567
13614
  case 38:
13568
13615
  audioError = AudioPermissionError.create(errorType);
13569
- return _context3.abrupt("return", Promise.reject(audioError));
13616
+ return _context4.abrupt("return", Promise.reject(audioError));
13570
13617
  case 40:
13571
- unknownError = AudioPermissionError.create("MicrophoneUnknownError", _context3.t0 instanceof Error ? _context3.t0.message : void 0);
13572
- 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));
13573
13620
  case 42:
13574
13621
  case "end":
13575
- return _context3.stop();
13622
+ return _context4.stop();
13576
13623
  }
13577
- }, _callee3, this, [[0, 17]]);
13624
+ }, _callee4, this, [[0, 17]]);
13578
13625
  }));
13579
13626
  function requestMicrophonePermission() {
13580
13627
  return _requestMicrophonePermission.apply(this, arguments);
@@ -13595,20 +13642,21 @@ var AudioPermission = /* @__PURE__ */ function() {
13595
13642
  _defineProperty(this, "microphonePermission", void 0);
13596
13643
  this.playbackPermission = new AudioPlaybackPermission();
13597
13644
  this.microphonePermission = new MicrophonePermission();
13645
+ this.playbackPermission.setExecuteContext(this);
13598
13646
  }
13599
13647
  return _createClass(AudioPermission2, [{
13600
13648
  key: "requestPlaybackPermission",
13601
13649
  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) {
13650
+ var _requestPlaybackPermission2 = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee5() {
13651
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
13652
+ while (1) switch (_context5.prev = _context5.next) {
13605
13653
  case 0:
13606
- return _context4.abrupt("return", this.playbackPermission.requestPlaybackPermission());
13654
+ return _context5.abrupt("return", this.playbackPermission.requestPlaybackPermission());
13607
13655
  case 1:
13608
13656
  case "end":
13609
- return _context4.stop();
13657
+ return _context5.stop();
13610
13658
  }
13611
- }, _callee4, this);
13659
+ }, _callee5, this);
13612
13660
  }));
13613
13661
  function requestPlaybackPermission() {
13614
13662
  return _requestPlaybackPermission2.apply(this, arguments);
@@ -13618,16 +13666,16 @@ var AudioPermission = /* @__PURE__ */ function() {
13618
13666
  }, {
13619
13667
  key: "requestMicrophonePermission",
13620
13668
  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) {
13669
+ var _requestMicrophonePermission2 = _asyncToGenerator(/* @__PURE__ */ _regeneratorRuntime().mark(function _callee6() {
13670
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
13671
+ while (1) switch (_context6.prev = _context6.next) {
13624
13672
  case 0:
13625
- return _context5.abrupt("return", this.microphonePermission.requestMicrophonePermission());
13673
+ return _context6.abrupt("return", this.microphonePermission.requestMicrophonePermission());
13626
13674
  case 1:
13627
13675
  case "end":
13628
- return _context5.stop();
13676
+ return _context6.stop();
13629
13677
  }
13630
- }, _callee5, this);
13678
+ }, _callee6, this);
13631
13679
  }));
13632
13680
  function requestMicrophonePermission() {
13633
13681
  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.61",
5
5
  "description": "明途公共工具库",
6
6
  "author": "hcc",
7
7
  "license": "ISC",