@isdk/util 0.3.9 → 0.3.10
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/dist/index.d.mts +247 -142
- package/dist/index.d.ts +247 -142
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/docs/classes/BinarySemaphore.md +145 -64
- package/docs/classes/ConfigFile.md +8 -8
- package/docs/classes/Deque.md +109 -105
- package/docs/classes/IntSet.md +12 -12
- package/docs/classes/Semaphore.md +149 -77
- package/docs/classes/SignalGate.md +11 -11
- package/docs/functions/RateLimit.md +30 -7
- package/docs/functions/arrayHasAll.md +1 -1
- package/docs/functions/extNameLevel.md +1 -1
- package/docs/functions/extractCodeBlock.md +3 -3
- package/docs/functions/extractTopLevelCodeBlocks.md +1 -1
- package/docs/functions/filenameReservedRegex.md +1 -1
- package/docs/functions/findPort.md +5 -5
- package/docs/functions/getMultiLevelExtname.md +1 -1
- package/docs/functions/glob.md +1 -1
- package/docs/functions/isStringIn.md +3 -3
- package/docs/functions/isValidFilename.md +1 -1
- package/docs/functions/isValidFilepath.md +1 -1
- package/docs/functions/normalizeIncludeFiles.md +3 -3
- package/docs/functions/omitDeepBy.md +1 -1
- package/docs/functions/omitEmptyDeep.md +1 -1
- package/docs/functions/parseCodeBlockSelector.md +2 -2
- package/docs/functions/parseFrontMatter.md +1 -1
- package/docs/functions/parseYaml.md +1 -1
- package/docs/functions/reControlCharsRegex.md +1 -1
- package/docs/functions/registerYamlTag.md +3 -3
- package/docs/functions/removeLeadingEmptyLines.md +1 -1
- package/docs/functions/sanitizeFilename.md +1 -1
- package/docs/functions/sanitizeFilepath.md +1 -1
- package/docs/functions/sleep.md +1 -1
- package/docs/functions/stringifyYaml.md +1 -1
- package/docs/functions/toCamelCase.md +1 -1
- package/docs/functions/toCapitalCase.md +1 -1
- package/docs/functions/toPascalCase.md +1 -1
- package/docs/functions/traverseFolder.md +1 -1
- package/docs/functions/traverseFolderSync.md +1 -1
- package/docs/functions/yieldExec.md +1 -1
- package/docs/interfaces/BinarySemaphoreAcquireOptions.md +11 -4
- package/docs/interfaces/BinarySemaphoreOptions.md +29 -12
- package/docs/interfaces/BinarySemaphoreReleaseOptions.md +10 -4
- package/docs/interfaces/BinarySemaphoreReleaserFunc.md +14 -5
- package/docs/interfaces/CodeBlockSelectorPart.md +4 -4
- package/docs/interfaces/CodeString.md +89 -89
- package/docs/interfaces/ExtractCodeBlockOptions.md +9 -9
- package/docs/interfaces/FindPortOptions.md +5 -5
- package/docs/interfaces/IncludeFiles.md +5 -5
- package/docs/interfaces/LoadConfigFileOptions.md +5 -5
- package/docs/interfaces/SanitizeFilenameOptions.md +5 -5
- package/docs/interfaces/SemaphoreOptions.md +39 -16
- package/docs/interfaces/SemaphoreTaskItem.md +23 -10
- package/docs/type-aliases/CodeBlockCombinator.md +1 -1
- package/docs/type-aliases/SemaphoreIsReadyFuncType.md +5 -2
- package/docs/type-aliases/StringifyFunc.md +2 -2
- package/docs/type-aliases/TraverseFolderHandler.md +2 -2
- package/docs/type-aliases/TraverseFolderSyncHandler.md +2 -2
- package/docs/variables/DefaultAllTextFiles.md +1 -1
- package/docs/variables/DefaultAsyncSemaphoreCapacity.md +4 -1
- package/docs/variables/FilenameReservedRegex.md +1 -1
- package/docs/variables/WindowsReservedNameRegex.md +1 -1
- package/package.json +20 -17
package/dist/index.d.mts
CHANGED
|
@@ -615,258 +615,363 @@ declare function sleep(ms: number): Promise<void>;
|
|
|
615
615
|
*/
|
|
616
616
|
declare function yieldExec(): Promise<void>;
|
|
617
617
|
|
|
618
|
+
/**
|
|
619
|
+
* 信号量内部等待队列的默认预分配容量。
|
|
620
|
+
* 当选项中未指定容量时使用此值。
|
|
621
|
+
*/
|
|
618
622
|
declare const DefaultAsyncSemaphoreCapacity = 32;
|
|
623
|
+
/**
|
|
624
|
+
* 就绪检查函数的类型定义。
|
|
625
|
+
* 可以是同步或异步函数。返回 `true` 表示信号量已准备好接受新请求。
|
|
626
|
+
*/
|
|
619
627
|
type SemaphoreIsReadyFuncType = () => Promise<boolean> | boolean;
|
|
628
|
+
/**
|
|
629
|
+
* 二进制信号量的配置选项。
|
|
630
|
+
*/
|
|
620
631
|
interface BinarySemaphoreOptions {
|
|
632
|
+
/**
|
|
633
|
+
* 用于初始化令牌的函数。
|
|
634
|
+
* 当信号量释放且没有等待任务时,如果未提供令牌,将调用此函数生成新令牌。
|
|
635
|
+
* 默认为 `() => '1'`。
|
|
636
|
+
*/
|
|
621
637
|
initFn?: () => any;
|
|
638
|
+
/**
|
|
639
|
+
* 可选的暂停函数。
|
|
640
|
+
* 当等待队列开始积压(第一个任务进入队列)时调用,用于通知上游停止发送数据,
|
|
641
|
+
* 以避免产生过多的等待 Promise 导致内存溢出。
|
|
642
|
+
*/
|
|
622
643
|
pauseFn?: () => void;
|
|
644
|
+
/**
|
|
645
|
+
* 可选的恢复函数。
|
|
646
|
+
* 当信号量有空闲槽位且之前已调用过 `pauseFn` 时调用。
|
|
647
|
+
* 如果定义了 `pauseFn`,则必须同时定义此函数。
|
|
648
|
+
*/
|
|
623
649
|
resumeFn?: () => void;
|
|
650
|
+
/**
|
|
651
|
+
* 内部等待队列的初始预分配容量。
|
|
652
|
+
* 适用于高性能场景,开发者可以根据并发规模预估此值以减少动态扩容。
|
|
653
|
+
*/
|
|
624
654
|
capacity?: number;
|
|
625
655
|
}
|
|
656
|
+
/**
|
|
657
|
+
* 获取信号量时的选项。
|
|
658
|
+
*/
|
|
626
659
|
interface BinarySemaphoreAcquireOptions {
|
|
660
|
+
/**
|
|
661
|
+
* 可选的 AbortSignal,用于取消获取操作。
|
|
662
|
+
* 如果在获取到令牌前信号被中止,`acquire` 返回的 Promise 将被拒绝并抛出 `AbortError`。
|
|
663
|
+
*/
|
|
627
664
|
signal?: AbortSignal;
|
|
665
|
+
/**
|
|
666
|
+
* 允许扩展其他自定义选项。
|
|
667
|
+
*/
|
|
628
668
|
[n: string]: any;
|
|
629
669
|
}
|
|
670
|
+
/**
|
|
671
|
+
* 释放信号量时的选项。
|
|
672
|
+
*/
|
|
630
673
|
interface BinarySemaphoreReleaseOptions {
|
|
674
|
+
/**
|
|
675
|
+
* 可选的令牌。如果使用了自定义的 `initFn`,释放时应归还对应的令牌。
|
|
676
|
+
*/
|
|
631
677
|
token?: any;
|
|
678
|
+
/**
|
|
679
|
+
* 允许扩展其他自定义选项。
|
|
680
|
+
*/
|
|
632
681
|
[n: string]: any;
|
|
633
682
|
}
|
|
683
|
+
/**
|
|
684
|
+
* 信号量释放函数的接口定义。
|
|
685
|
+
* 它本身是一个函数,同时也包含释放选项作为属性。
|
|
686
|
+
*/
|
|
634
687
|
interface BinarySemaphoreReleaserFunc extends BinarySemaphoreReleaseOptions {
|
|
688
|
+
/**
|
|
689
|
+
* 调用此函数以释放信号量。
|
|
690
|
+
*/
|
|
635
691
|
(): void;
|
|
636
692
|
}
|
|
693
|
+
/**
|
|
694
|
+
* 通用信号量的配置选项,继承自二进制信号量选项。
|
|
695
|
+
*/
|
|
637
696
|
interface SemaphoreOptions extends BinarySemaphoreOptions {
|
|
697
|
+
/**
|
|
698
|
+
* 最大并发数。
|
|
699
|
+
* 指定允许同时获取信号量的最大调用方数量。
|
|
700
|
+
*/
|
|
638
701
|
maxConcurrency?: number;
|
|
702
|
+
/**
|
|
703
|
+
* 可选的就绪检查函数。
|
|
704
|
+
* 在尝试获取令牌前调用,如果返回 false(或解析为 false 的 Promise),则无法立即获取。
|
|
705
|
+
*/
|
|
639
706
|
isReadyFn?: SemaphoreIsReadyFuncType;
|
|
640
707
|
}
|
|
708
|
+
/**
|
|
709
|
+
* 内部等待任务项的结构定义。
|
|
710
|
+
*/
|
|
641
711
|
interface SemaphoreTaskItem extends BinarySemaphoreAcquireOptions {
|
|
712
|
+
/**
|
|
713
|
+
* 当获取成功时调用的解析函数,接收释放函数作为参数。
|
|
714
|
+
*/
|
|
642
715
|
resolve: (value: any) => void;
|
|
716
|
+
/**
|
|
717
|
+
* 当获取失败(如被中止)时调用的拒绝函数。
|
|
718
|
+
*/
|
|
643
719
|
reject: (reason?: any) => void;
|
|
720
|
+
/**
|
|
721
|
+
* 与此任务关联的令牌。
|
|
722
|
+
*/
|
|
644
723
|
token?: any;
|
|
645
724
|
}
|
|
646
725
|
/**
|
|
647
|
-
*
|
|
648
|
-
*
|
|
649
|
-
*
|
|
726
|
+
* 二进制信号量(Binary Semaphore)实现。
|
|
727
|
+
* 二进制信号量在任何时刻只允许一个调用方获取成功(类似于互斥锁)。
|
|
728
|
+
* 它提供了获取(acquire)、释放(release)以及管理等待队列的机制,并支持背压控制(通过 pauseFn/resumeFn)。
|
|
650
729
|
*
|
|
651
|
-
*
|
|
730
|
+
* 示例用法:
|
|
652
731
|
*
|
|
653
732
|
* ```typescript
|
|
654
|
-
* const semaphore = new
|
|
733
|
+
* const semaphore = new BinarySemaphore();
|
|
655
734
|
*
|
|
656
|
-
*
|
|
657
|
-
*
|
|
658
|
-
* {
|
|
659
|
-
* capacity: 100 // Prealloc space for 100 tokens
|
|
660
|
-
* }
|
|
661
|
-
* );
|
|
662
|
-
*
|
|
663
|
-
* async function fetchData(x) {
|
|
664
|
-
* await semaphore.acquire()
|
|
735
|
+
* async function performTask(data) {
|
|
736
|
+
* const release = await semaphore.acquire();
|
|
665
737
|
* try {
|
|
666
|
-
* console.log(
|
|
667
|
-
* //
|
|
738
|
+
* console.log('正在处理:', data);
|
|
739
|
+
* // 执行异步操作...
|
|
668
740
|
* } finally {
|
|
669
|
-
* semaphore.release();
|
|
741
|
+
* release(); // 或者使用 semaphore.release();
|
|
670
742
|
* }
|
|
671
743
|
* }
|
|
672
|
-
*
|
|
673
|
-
* const data = await Promise.all(array.map(fetchData));
|
|
674
744
|
* ```
|
|
675
745
|
*/
|
|
676
746
|
declare class BinarySemaphore {
|
|
747
|
+
/** 存储等待获取令牌的任务队列。 */
|
|
677
748
|
readonly waiting: Deque<SemaphoreTaskItem | undefined>;
|
|
749
|
+
/** 当前空闲的令牌。对于二进制信号量,只能持有一个令牌。 */
|
|
678
750
|
protected free: any;
|
|
751
|
+
/** 内部事件触发器,用于协调释放和分发逻辑。 */
|
|
679
752
|
protected emitter: EventEmitter;
|
|
753
|
+
/** 标记是否使用默认的令牌初始化函数。 */
|
|
680
754
|
protected useDefaultTokens: boolean;
|
|
755
|
+
/** 获取积压时的暂停回调。 */
|
|
681
756
|
protected pauseFn?: () => void;
|
|
757
|
+
/** 恢复处理的回调。 */
|
|
682
758
|
protected resumeFn?: () => void;
|
|
759
|
+
/** 令牌初始化函数。 */
|
|
683
760
|
protected initTokenFn: (token?: any) => void;
|
|
761
|
+
/** 记录当前是否处于暂停状态。 */
|
|
684
762
|
protected paused: boolean;
|
|
763
|
+
/** 记录当前活跃的(已获取但未释放)操作总数。 */
|
|
685
764
|
protected _activeCount: number;
|
|
686
765
|
/**
|
|
687
|
-
*
|
|
688
|
-
*
|
|
689
|
-
* @param options.initFn Function that is used to initialize the tokens used to manage the semaphore. The default is () => '1'.
|
|
690
|
-
* @param options.pauseFn An optional fuction that is called to opportunistically request pausing the the incoming stream of data,
|
|
691
|
-
* instead of piling up waiting promises and possibly running out of memory. See examples/pausing.js.
|
|
692
|
-
* @param options.resumeFn An optional function that is called when there is room again to accept new waiters on the semaphore.
|
|
693
|
-
* This function must be declared if a pauseFn is declared.
|
|
694
|
-
* @param options.capacity Sets the size of the preallocated waiting list inside the semaphore.
|
|
695
|
-
* This is typically used by high performance where the developer can make a rough estimate of the number of concurrent users of a semaphore.
|
|
696
|
-
*
|
|
697
|
-
* ```ts
|
|
698
|
-
* const readline = require('readline');
|
|
699
|
-
*
|
|
700
|
-
* const rl = readline.createInterface({
|
|
701
|
-
* input: process.stdin,
|
|
702
|
-
* output: process.stdout,
|
|
703
|
-
* terminal: false
|
|
704
|
-
* });
|
|
705
|
-
*
|
|
706
|
-
* function pause() {
|
|
707
|
-
* console.log('Pausing the stream');
|
|
708
|
-
* rl.pause();
|
|
709
|
-
* }
|
|
710
|
-
*
|
|
711
|
-
* function resume() {
|
|
712
|
-
* console.log('Resuming the stream');
|
|
713
|
-
* rl.resume();
|
|
714
|
-
* }
|
|
715
|
-
*
|
|
716
|
-
* const s = new BinarySemaphore({ pauseFn: pause, resumeFn: resume });
|
|
717
|
-
*
|
|
718
|
-
* async function parse(line) {
|
|
719
|
-
* await s.acquire();
|
|
720
|
-
*
|
|
721
|
-
* console.log(line);
|
|
722
|
-
*
|
|
723
|
-
* s.release();
|
|
724
|
-
* }
|
|
725
|
-
*
|
|
726
|
-
* rl.on('line', line => {
|
|
727
|
-
* parse(line).catch(console.error);
|
|
728
|
-
* });
|
|
729
|
-
* ```
|
|
766
|
+
* 创建一个二进制信号量实例。
|
|
730
767
|
*
|
|
768
|
+
* @param options 配置选项。
|
|
769
|
+
* @throws {Error} 如果只提供了 pauseFn 而未提供 resumeFn,或者反之,则抛出错误。
|
|
731
770
|
*/
|
|
732
771
|
constructor(options?: BinarySemaphoreOptions);
|
|
772
|
+
/**
|
|
773
|
+
* 初始化空闲令牌。在构造函数中被调用。
|
|
774
|
+
* @param options 配置选项。
|
|
775
|
+
*/
|
|
733
776
|
initFree(options?: BinarySemaphoreOptions): void;
|
|
777
|
+
/**
|
|
778
|
+
* 当信号量被释放时执行的内部处理逻辑。
|
|
779
|
+
* 检查等待队列,如果有任务则分发令牌;否则将令牌归还至空闲池,并视情况调用 `resumeFn`。
|
|
780
|
+
*
|
|
781
|
+
* @param options 释放选项,可能包含令牌。
|
|
782
|
+
*/
|
|
734
783
|
onReleased(options?: BinarySemaphoreReleaseOptions): void;
|
|
784
|
+
/**
|
|
785
|
+
* 初始化事件监听。在构造函数中被调用。
|
|
786
|
+
* @param options 配置选项。
|
|
787
|
+
*/
|
|
735
788
|
init(options: BinarySemaphoreOptions): void;
|
|
789
|
+
/**
|
|
790
|
+
* 创建一个新的释放函数。
|
|
791
|
+
* 确保释放逻辑只被执行一次,并携带相关的释放选项。
|
|
792
|
+
*
|
|
793
|
+
* @param options 释放选项。
|
|
794
|
+
* @returns 返回一个可调用的释放函数。
|
|
795
|
+
* @internal
|
|
796
|
+
*/
|
|
736
797
|
_newReleaser(options?: BinarySemaphoreReleaseOptions): BinarySemaphoreReleaserFunc;
|
|
798
|
+
/**
|
|
799
|
+
* 将令牌分发给等待的任务。
|
|
800
|
+
*
|
|
801
|
+
* @param task 等待中的任务项。
|
|
802
|
+
* @param options 释放时传递的选项。
|
|
803
|
+
* @internal
|
|
804
|
+
*/
|
|
737
805
|
_dispatchTask(task: SemaphoreTaskItem, options?: BinarySemaphoreReleaseOptions): void;
|
|
806
|
+
/**
|
|
807
|
+
* 锁定信号量。尝试从空闲池中提取令牌。
|
|
808
|
+
*
|
|
809
|
+
* @param options 获取选项。
|
|
810
|
+
* @returns 如果有可用令牌则返回该令牌,否则返回 undefined。
|
|
811
|
+
*/
|
|
738
812
|
lock(options?: BinarySemaphoreAcquireOptions): any;
|
|
813
|
+
/**
|
|
814
|
+
* 解锁信号量。将令牌归还至空闲池。
|
|
815
|
+
*
|
|
816
|
+
* @param token 要归还的令牌。如果未提供,则使用初始化函数生成。
|
|
817
|
+
*/
|
|
739
818
|
unlock(token?: any): void;
|
|
740
819
|
/**
|
|
741
|
-
*
|
|
820
|
+
* 尝试立即获取令牌。
|
|
821
|
+
* 如果信号量当前不可用,则立即返回 `undefined` 而不进入等待队列。
|
|
742
822
|
*
|
|
743
|
-
* @
|
|
823
|
+
* @param options 获取选项。
|
|
824
|
+
* @returns 如果获取成功则返回令牌,否则返回 `undefined`。
|
|
744
825
|
*/
|
|
745
826
|
tryAcquire(options?: BinarySemaphoreAcquireOptions): any | undefined;
|
|
746
827
|
/**
|
|
747
|
-
*
|
|
748
|
-
*
|
|
749
|
-
*
|
|
828
|
+
* 获取信号量。
|
|
829
|
+
* 如果信号量当前可用,将立即解析。否则,调用方将被加入等待队列,
|
|
830
|
+
* 直到有令牌被释放。
|
|
831
|
+
*
|
|
832
|
+
* 逻辑流程:
|
|
833
|
+
* 1. 增加活跃计数。
|
|
834
|
+
* 2. 尝试通过 `tryAcquire` 立即获取令牌。
|
|
835
|
+
* 3. 如果 `tryAcquire` 返回的是异步结果(通过 `isAsync` 判断),则等待其解析。
|
|
836
|
+
* 4. 如果最终未获得令牌,则将任务推入 `waiting` 队列,并处理可选的 `AbortSignal`。
|
|
837
|
+
* 5. 如果此时是队列中的第一个任务且定义了 `pauseFn`,则触发暂停回调。
|
|
838
|
+
*
|
|
839
|
+
* @param options 获取选项,可包含 `signal` 用于取消。
|
|
840
|
+
* @returns 解析为释放函数(`BinarySemaphoreReleaserFunc`)的 Promise。
|
|
750
841
|
*/
|
|
751
842
|
acquire(options?: BinarySemaphoreAcquireOptions): Promise<BinarySemaphoreReleaserFunc>;
|
|
752
843
|
/**
|
|
753
|
-
*
|
|
754
|
-
*
|
|
844
|
+
* 释放信号量,增加可用执行槽位。
|
|
845
|
+
* 如果等待队列中有任务,将触发下一个任务的执行。
|
|
846
|
+
* 此方法会减少 `activeCount` 并发出 'release' 事件。
|
|
847
|
+
*
|
|
848
|
+
* @param options 释放选项。
|
|
755
849
|
*/
|
|
756
850
|
release(options?: BinarySemaphoreReleaseOptions): void;
|
|
757
851
|
/**
|
|
758
|
-
*
|
|
852
|
+
* 等待所有当前活跃的操作完成。
|
|
853
|
+
* 它通过尝试获取一次信号量来确保之前的操作已经释放。
|
|
854
|
+
* 在进程终止前使用此方法以确保没有挂起的异步任务。
|
|
855
|
+
*
|
|
856
|
+
* @returns 解析为包含已获取令牌的数组的 Promise。
|
|
759
857
|
*/
|
|
760
858
|
drain(): Promise<any[]>;
|
|
761
|
-
abort(reason?: any): void;
|
|
762
859
|
/**
|
|
763
|
-
*
|
|
860
|
+
* 中止所有正在等待的任务。
|
|
861
|
+
* 所有在等待队列中的 Promise 将被拒绝并抛出 `AbortError`。
|
|
764
862
|
*
|
|
765
|
-
*
|
|
766
|
-
|
|
767
|
-
|
|
863
|
+
* @param reason 中止的原因。
|
|
864
|
+
*/
|
|
865
|
+
abort(reason?: any): void;
|
|
866
|
+
/**
|
|
867
|
+
* 获取所有活跃操作的总数。
|
|
868
|
+
* 包含:
|
|
869
|
+
* - 正在队列中等待获取信号量的操作(`pendingCount`)。
|
|
870
|
+
* - 已经成功获取信号量但尚未释放的操作。
|
|
768
871
|
*
|
|
769
|
-
* @returns
|
|
872
|
+
* @returns 活跃操作的总数。
|
|
770
873
|
*/
|
|
771
874
|
get activeCount(): number;
|
|
772
875
|
/**
|
|
773
|
-
*
|
|
876
|
+
* 获取当前在等待队列中的调用方数量。
|
|
774
877
|
*
|
|
775
|
-
* @returns
|
|
878
|
+
* @returns 等待中的 Promise 数量。
|
|
776
879
|
*/
|
|
777
880
|
get pendingCount(): number;
|
|
778
881
|
}
|
|
779
882
|
/**
|
|
780
|
-
*
|
|
781
|
-
*
|
|
782
|
-
*
|
|
883
|
+
* 计数信号量(Semaphore)实现。
|
|
884
|
+
* 扩展自二进制信号量,允许指定最大并发数(`maxConcurrency`)。
|
|
885
|
+
* 支持可选的就绪检查(`isReadyFn`)。
|
|
783
886
|
*
|
|
784
|
-
*
|
|
887
|
+
* 示例用法:
|
|
785
888
|
*
|
|
786
889
|
* ```typescript
|
|
787
|
-
* const semaphore = new Semaphore(5); //
|
|
788
|
-
*
|
|
789
|
-
* const semaphore = new Semaphore(
|
|
790
|
-
* 4, // Allow 4 concurrent async calls
|
|
791
|
-
* {
|
|
792
|
-
* capacity: 100, // Prealloc space for 100 tokens
|
|
793
|
-
* isReadyFn: async () => {
|
|
794
|
-
* // Check if the system is ready to handle more requests
|
|
795
|
-
* return true;
|
|
796
|
-
* },
|
|
797
|
-
* pauseFn: () => {
|
|
798
|
-
* console.log('Pausing the stream');
|
|
799
|
-
* },
|
|
800
|
-
* resumeFn: () => {
|
|
801
|
-
* console.log('Resuming the stream');
|
|
802
|
-
* }
|
|
803
|
-
* }
|
|
804
|
-
* );
|
|
890
|
+
* const semaphore = new Semaphore(5); // 允许 5 个并发操作
|
|
805
891
|
*
|
|
806
|
-
* async function fetchData(
|
|
807
|
-
* await semaphore.acquire()
|
|
892
|
+
* async function fetchData(id) {
|
|
893
|
+
* const release = await semaphore.acquire();
|
|
808
894
|
* try {
|
|
809
|
-
* console.log(semaphore.pendingCount
|
|
810
|
-
* // ...
|
|
895
|
+
* console.log(`正在获取数据 ${id}, 等待中: ${semaphore.pendingCount}`);
|
|
896
|
+
* // ... 异步操作
|
|
811
897
|
* } finally {
|
|
812
|
-
*
|
|
898
|
+
* release();
|
|
813
899
|
* }
|
|
814
900
|
* }
|
|
815
|
-
*
|
|
816
|
-
* const data = await Promise.all(array.map(fetchData));
|
|
817
901
|
* ```
|
|
818
902
|
*/
|
|
819
903
|
declare class Semaphore extends BinarySemaphore {
|
|
904
|
+
/** 最大并发限制。 */
|
|
820
905
|
readonly maxConcurrency: number;
|
|
906
|
+
/** 存储空闲令牌的队列。 */
|
|
821
907
|
protected free: Deque<any>;
|
|
908
|
+
/** 就绪检查函数。 */
|
|
822
909
|
private isReady?;
|
|
823
910
|
/**
|
|
824
|
-
*
|
|
911
|
+
* 创建一个计数信号量实例。
|
|
825
912
|
*
|
|
826
|
-
* @param maxConcurrency
|
|
827
|
-
* @param options
|
|
828
|
-
* @
|
|
829
|
-
* instead of piling up waiting promises and possibly running out of memory. See examples/pausing.js.
|
|
830
|
-
* @param options.resumeFn An optional function that is called when there is room again to accept new waiters on the semaphore.
|
|
831
|
-
* This function must be declared if a pauseFn is declared.
|
|
832
|
-
* @param options.capacity Sets the size of the preallocated waiting list inside the semaphore.
|
|
833
|
-
* This is typically used by high performance where the developer can make a rough estimate of the number of concurrent users of a semaphore.
|
|
834
|
-
* @param options.isReadyFn An optional function that returns a boolean or a promise that resolves to a boolean indicating whether the semaphore is ready to accept new requests.
|
|
913
|
+
* @param maxConcurrency 最大并发数,或者包含并发设置的配置对象。
|
|
914
|
+
* @param options 配置选项。
|
|
915
|
+
* @throws {Error} 如果未指定有效并发数则抛出错误。
|
|
835
916
|
*/
|
|
836
917
|
constructor(maxConcurrency: number | SemaphoreOptions, options?: SemaphoreOptions);
|
|
918
|
+
/**
|
|
919
|
+
* 初始化令牌池,填充至最大并发数。
|
|
920
|
+
* @param options 配置选项。
|
|
921
|
+
*/
|
|
837
922
|
initFree(options: SemaphoreOptions): void;
|
|
923
|
+
/**
|
|
924
|
+
* 尝试获取令牌,包含就绪状态检查。
|
|
925
|
+
* 如果定义了 `isReadyFn`,将先调用它。
|
|
926
|
+
*
|
|
927
|
+
* @param options 获取选项。
|
|
928
|
+
* @returns 可能返回令牌、Promise(当就绪检查为异步时)或 undefined。
|
|
929
|
+
*/
|
|
838
930
|
tryAcquire(options?: BinarySemaphoreAcquireOptions): Promise<any | undefined> | any | undefined;
|
|
931
|
+
/**
|
|
932
|
+
* 将令牌归还至空闲令牌池。
|
|
933
|
+
* @param token 要归还的令牌。
|
|
934
|
+
*/
|
|
839
935
|
unlock(token?: any): void;
|
|
936
|
+
/**
|
|
937
|
+
* 从空闲令牌池中取出一个令牌。
|
|
938
|
+
* @param options 获取选项。
|
|
939
|
+
* @returns 如果池中不为空则返回一个令牌,否则返回 undefined。
|
|
940
|
+
*/
|
|
840
941
|
lock(options?: BinarySemaphoreAcquireOptions): any;
|
|
942
|
+
/**
|
|
943
|
+
* 消耗掉所有并发槽位,确保当前没有其他操作正在运行。
|
|
944
|
+
* 常用于在关键操作前清空并发环境。
|
|
945
|
+
*
|
|
946
|
+
* @returns 解析为包含所有令牌数组的 Promise。
|
|
947
|
+
*/
|
|
841
948
|
drain(): Promise<any[]>;
|
|
842
949
|
}
|
|
843
950
|
/**
|
|
844
|
-
*
|
|
845
|
-
*
|
|
846
|
-
*
|
|
847
|
-
*
|
|
848
|
-
*
|
|
849
|
-
* @param options
|
|
850
|
-
*
|
|
851
|
-
*
|
|
852
|
-
*
|
|
853
|
-
*
|
|
854
|
-
*
|
|
855
|
-
*
|
|
856
|
-
*
|
|
857
|
-
*
|
|
951
|
+
* 创建一个速率限制器函数(Rate Limiter)。
|
|
952
|
+
* 当调用频率超过设定的 `rps`(每秒请求数)时,该函数会通过 Promise 阻塞调用方,
|
|
953
|
+
* 并在调用率回到限制范围内时解析。
|
|
954
|
+
*
|
|
955
|
+
* @param rps 限制的速率。默认单位时间内允许的请求数。
|
|
956
|
+
* @param options 配置项。
|
|
957
|
+
* @param options.timeUnit 时间窗口大小(毫秒)。默认为 1000ms。
|
|
958
|
+
* @param options.uniformDistribution 是否强制离散均匀分布。
|
|
959
|
+
* - 若为 `false`(默认):允许在时间窗口开始时立即耗尽所有配额,然后暂停直到窗口结束。
|
|
960
|
+
* - 若为 `true`:将配额均匀分配到时间窗口内(例如 5 rps 会每 200ms 允许一次请求)。
|
|
961
|
+
* 在流量持续且快速的场景下(如读取文件)建议开启,以避免瞬时突发流量。
|
|
962
|
+
* @returns 返回一个异步限流函数,调用该函数即可实现限流。
|
|
963
|
+
*
|
|
964
|
+
* 示例:
|
|
858
965
|
* ```ts
|
|
859
|
-
*
|
|
860
|
-
* const lim = RateLimit(5); // rps
|
|
966
|
+
* const lim = RateLimit(5); // 限制为 5 次/秒
|
|
861
967
|
*
|
|
862
|
-
*
|
|
863
|
-
*
|
|
864
|
-
*
|
|
968
|
+
* async function processItems(items) {
|
|
969
|
+
* for (const item of items) {
|
|
970
|
+
* await lim(); // 可能会在这里阻塞
|
|
971
|
+
* await handle(item);
|
|
865
972
|
* }
|
|
866
973
|
* }
|
|
867
974
|
* ```
|
|
868
|
-
*
|
|
869
|
-
*
|
|
870
975
|
*/
|
|
871
976
|
declare function RateLimit(rps: number, { timeUnit, uniformDistribution, }?: {
|
|
872
977
|
timeUnit?: number;
|
|
@@ -1092,7 +1197,7 @@ interface CodeBlockSelectorPart {
|
|
|
1092
1197
|
/**
|
|
1093
1198
|
* The target language identifier or alias to match.
|
|
1094
1199
|
*/
|
|
1095
|
-
lang: string;
|
|
1200
|
+
lang: string | string[];
|
|
1096
1201
|
}
|
|
1097
1202
|
/**
|
|
1098
1203
|
* Options for extracting code blocks.
|
|
@@ -1105,7 +1210,7 @@ interface ExtractCodeBlockOptions {
|
|
|
1105
1210
|
* @example 'ts' - Find the last TypeScript block.
|
|
1106
1211
|
* @example 'md > ts' - Find ts blocks inside md blocks.
|
|
1107
1212
|
*/
|
|
1108
|
-
lang?: string;
|
|
1213
|
+
lang?: string | string[];
|
|
1109
1214
|
/**
|
|
1110
1215
|
* Optional. The 0-based index of the code block to extract from the final result set.
|
|
1111
1216
|
* Supports negative indexing: -1 means the last one, -2 means the second to last, etc.
|
|
@@ -1145,7 +1250,7 @@ declare function extractTopLevelCodeBlocks(text: string, options?: ExtractCodeBl
|
|
|
1145
1250
|
* @param lang - The selector string.
|
|
1146
1251
|
* @returns An array of parsed selector parts.
|
|
1147
1252
|
*/
|
|
1148
|
-
declare function parseCodeBlockSelector(lang?: string): CodeBlockSelectorPart[];
|
|
1253
|
+
declare function parseCodeBlockSelector(lang?: string | string[]): CodeBlockSelectorPart[];
|
|
1149
1254
|
/**
|
|
1150
1255
|
* Extracts fenced code block(s) from the input `text`, with support for nested path selectors.
|
|
1151
1256
|
*
|
|
@@ -1170,6 +1275,6 @@ declare function parseCodeBlockSelector(lang?: string): CodeBlockSelectorPart[];
|
|
|
1170
1275
|
* @returns A single {@link CodeString}, an array of {@link CodeString}s (if `all: true`),
|
|
1171
1276
|
* or the original `text` if no matches are found at the specified index.
|
|
1172
1277
|
*/
|
|
1173
|
-
declare function extractCodeBlock(text: string, langOrOptions?: string | ExtractCodeBlockOptions): CodeString | string | (CodeString | string)[];
|
|
1278
|
+
declare function extractCodeBlock(text: string, langOrOptions?: string | string[] | ExtractCodeBlockOptions): CodeString | string | (CodeString | string)[];
|
|
1174
1279
|
|
|
1175
1280
|
export { BinarySemaphore, type BinarySemaphoreAcquireOptions, type BinarySemaphoreOptions, type BinarySemaphoreReleaseOptions, type BinarySemaphoreReleaserFunc, type CodeBlockCombinator, type CodeBlockSelectorPart, type CodeString, ConfigFile, DefaultAllTextFiles, DefaultAsyncSemaphoreCapacity, Deque, type ExtractCodeBlockOptions, FilenameReservedRegex, type FindPortOptions, type IncludeFiles, IntSet, type LoadConfigFileOptions, RateLimit, type SanitizeFilenameOptions, Semaphore, type SemaphoreIsReadyFuncType, type SemaphoreOptions, type SemaphoreTaskItem, SignalGate, type StringifyFunc, type TraverseFolderHandler, type TraverseFolderSyncHandler, WindowsReservedNameRegex, arrayHasAll, extNameLevel, extractCodeBlock, extractTopLevelCodeBlocks, filenameReservedRegex, findPort, getMultiLevelExtname, glob, isStringIn, isValidFilename, isValidFilepath, normalizeIncludeFiles, omitDeepBy, omitEmptyDeep, parseCodeBlockSelector, parseFrontMatter, parseYaml, reControlCharsRegex, registerYamlTag, removeLeadingEmptyLines, sanitizeFilename, sanitizeFilepath, sleep, stringifyYaml, toCamelCase, toCapitalCase, toPascalCase, traverseFolder, traverseFolderSync, yieldExec };
|