@isdk/util 0.3.2 → 0.3.4
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 +464 -1
- package/dist/index.d.ts +464 -1
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/docs/classes/BinarySemaphore.md +418 -0
- package/docs/classes/ConfigFile.md +9 -9
- package/docs/classes/Deque.md +1912 -0
- package/docs/classes/IntSet.md +216 -0
- package/docs/classes/Semaphore.md +538 -0
- package/docs/classes/SignalGate.md +184 -0
- package/docs/functions/RateLimit.md +37 -0
- package/docs/functions/arrayHasAll.md +1 -1
- package/docs/functions/extNameLevel.md +1 -1
- package/docs/functions/filenameReservedRegex.md +1 -1
- package/docs/functions/findPort.md +25 -0
- package/docs/functions/getMultiLevelExtname.md +1 -1
- package/docs/functions/glob.md +3 -3
- package/docs/functions/isStringIn.md +1 -1
- package/docs/functions/isValidFilename.md +1 -1
- package/docs/functions/isValidFilepath.md +1 -1
- package/docs/functions/normalizeIncludeFiles.md +2 -2
- package/docs/functions/parseFrontMatter.md +1 -1
- package/docs/functions/parseYaml.md +2 -2
- package/docs/functions/reControlCharsRegex.md +1 -1
- package/docs/functions/registerYamlTag.md +2 -2
- 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 +36 -0
- package/docs/functions/stringifyYaml.md +2 -2
- 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 +27 -0
- package/docs/globals.md +17 -0
- package/docs/interfaces/BinarySemaphoreAcquireOptions.md +25 -0
- package/docs/interfaces/BinarySemaphoreOptions.md +57 -0
- package/docs/interfaces/BinarySemaphoreReleaseOptions.md +25 -0
- package/docs/interfaces/BinarySemaphoreReleaserFunc.md +37 -0
- package/docs/interfaces/IncludeFiles.md +3 -3
- package/docs/interfaces/LoadConfigFileOptions.md +3 -3
- package/docs/interfaces/SanitizeFilenameOptions.md +3 -3
- package/docs/interfaces/SemaphoreOptions.md +89 -0
- package/docs/interfaces/SemaphoreTaskItem.md +73 -0
- package/docs/type-aliases/SemaphoreIsReadyFuncType.md +15 -0
- package/docs/type-aliases/StringifyFunc.md +1 -1
- package/docs/type-aliases/TraverseFolderHandler.md +1 -1
- package/docs/type-aliases/TraverseFolderSyncHandler.md +1 -1
- package/docs/variables/DefaultAllTextFiles.md +1 -1
- package/docs/variables/DefaultAsyncSemaphoreCapacity.md +11 -0
- package/docs/variables/FilenameReservedRegex.md +1 -1
- package/docs/variables/WindowsReservedNameRegex.md +1 -1
- package/package.json +19 -17
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Dirent } from 'fs';
|
|
2
2
|
import { ScalarTag, CollectionTag, TagId, Tags, ParseOptions, DocumentOptions, SchemaOptions, ToJSOptions, CreateNodeOptions, ToStringOptions } from 'yaml';
|
|
3
|
+
import { EventEmitter } from 'events-ex';
|
|
3
4
|
|
|
4
5
|
type StringifyFunc = (content: any) => string;
|
|
5
6
|
interface LoadConfigFileOptions {
|
|
@@ -464,4 +465,466 @@ declare function extNameLevel(extName: string): number;
|
|
|
464
465
|
*/
|
|
465
466
|
declare function arrayHasAll<T = any>(array: T[], elements: T[]): boolean;
|
|
466
467
|
|
|
467
|
-
|
|
468
|
+
declare class Deque<T = any> extends Array<T> {
|
|
469
|
+
private _capacity;
|
|
470
|
+
private _length;
|
|
471
|
+
private _front;
|
|
472
|
+
private _disableAutoResize?;
|
|
473
|
+
constructor(capacity?: number | T[], disableAutoResize?: boolean);
|
|
474
|
+
push(item: T): number;
|
|
475
|
+
unshift(item: T): number;
|
|
476
|
+
/**
|
|
477
|
+
* Removes and returns the element at the back of the deque.
|
|
478
|
+
*
|
|
479
|
+
* @param skipNull When `true`, skips trailing `null`/`undefined` values until a valid element is found or all elements are processed.
|
|
480
|
+
* @returns The removed element, or `undefined` if the deque is empty or all elements are skipped.
|
|
481
|
+
*
|
|
482
|
+
* @example
|
|
483
|
+
* // Normal pop without skipping
|
|
484
|
+
* const deque = new Deque([1, 2, 3]);
|
|
485
|
+
* deque.pop(); // 3
|
|
486
|
+
*
|
|
487
|
+
* @example
|
|
488
|
+
* // Skipping null values
|
|
489
|
+
* const nullDeque = new Deque([null, 4, null]);
|
|
490
|
+
* nullDeque.pop(true); // 4
|
|
491
|
+
* nullDeque.pop(true); // undefined (skipped remaining nulls)
|
|
492
|
+
*
|
|
493
|
+
* @example
|
|
494
|
+
* // Mixed elements with skip
|
|
495
|
+
* const mixedDeque = new Deque([5, null, 6, null]);
|
|
496
|
+
* mixedDeque.pop(true); // 6
|
|
497
|
+
* mixedDeque.pop(false); // null (explicitly not skipping)
|
|
498
|
+
*/
|
|
499
|
+
pop(skipNull?: boolean): T | undefined;
|
|
500
|
+
/**
|
|
501
|
+
* Removes and returns the element at the front of the deque.
|
|
502
|
+
*
|
|
503
|
+
* @param skipNull When `true`, skips leading `null`/`undefined` values until a valid element is found or all elements are processed.
|
|
504
|
+
* @returns The removed element, or `undefined` if the deque is empty or all elements are skipped.
|
|
505
|
+
*
|
|
506
|
+
* @example
|
|
507
|
+
* // Normal shift without skipping
|
|
508
|
+
* const deque = new Deque([1, 2, 3]);
|
|
509
|
+
* deque.shift(); // 1
|
|
510
|
+
*
|
|
511
|
+
* @example
|
|
512
|
+
* // Skipping null values
|
|
513
|
+
* const nullDeque = new Deque([null, 4, null]);
|
|
514
|
+
* nullDeque.shift(true); // 4
|
|
515
|
+
* nullDeque.shift(true); // undefined (skipped remaining nulls)
|
|
516
|
+
*
|
|
517
|
+
* @example
|
|
518
|
+
* // Mixed elements with skip
|
|
519
|
+
* const mixedDeque = new Deque([null, 5, null, 6]);
|
|
520
|
+
* mixedDeque.shift(true); // 5
|
|
521
|
+
* mixedDeque.shift(false); // null (explicitly not skipping)
|
|
522
|
+
*/
|
|
523
|
+
shift(skipNull?: boolean): T | undefined;
|
|
524
|
+
/**
|
|
525
|
+
* Gets the number of elements in the deque.
|
|
526
|
+
*
|
|
527
|
+
* @returns The current count of elements in the deque.
|
|
528
|
+
*
|
|
529
|
+
* @example
|
|
530
|
+
* const deque = new Deque([1, 2, 3]);
|
|
531
|
+
* console.log(deque.size); // 3
|
|
532
|
+
*
|
|
533
|
+
* @important
|
|
534
|
+
* Do NOT use the native `Array.length` property. The Deque implementation uses a circular buffer with capacity management, so the native `length` reflects the underlying array capacity, not the actual element count. Always use `size` to get the correct element count.
|
|
535
|
+
*/
|
|
536
|
+
get size(): number;
|
|
537
|
+
get(index: number): T | undefined;
|
|
538
|
+
peekBack(): T | undefined;
|
|
539
|
+
peekFront(): T | undefined;
|
|
540
|
+
clear(): void;
|
|
541
|
+
isEmpty(): boolean;
|
|
542
|
+
/**
|
|
543
|
+
* Removes the element at the specified index.
|
|
544
|
+
* @param index Logical index position (0 represents the front, length-1 represents the back)
|
|
545
|
+
* @returns The removed element
|
|
546
|
+
*/
|
|
547
|
+
removeAt(index: number): T | undefined;
|
|
548
|
+
private checkCapacity;
|
|
549
|
+
private resizeTo;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* Represents a set of integers using a bit field.
|
|
554
|
+
* Each bit in the bit field represents an integer starting from 0,
|
|
555
|
+
* where the flag value 0 represents the 0th bit, 1 represents the 1st bit, and so on.
|
|
556
|
+
*/
|
|
557
|
+
declare class IntSet {
|
|
558
|
+
private bitField;
|
|
559
|
+
static has(bitField: number, flag: number): boolean;
|
|
560
|
+
static add(bitField: number, flag: number): number;
|
|
561
|
+
static delete(bitField: number, flag: number): number;
|
|
562
|
+
constructor(bitField?: number);
|
|
563
|
+
/**
|
|
564
|
+
* Adds an element to the set.
|
|
565
|
+
*
|
|
566
|
+
* @param flag - The flag value representing the bit position to set.
|
|
567
|
+
* Note: the flag value 0 represents the 0th bit, and so on.
|
|
568
|
+
*/
|
|
569
|
+
add(flag: number): this;
|
|
570
|
+
/**
|
|
571
|
+
* Removes an element from the set.
|
|
572
|
+
*
|
|
573
|
+
* @param flag - The flag value representing the bit position to set. 0 represents the 0th bit
|
|
574
|
+
*/
|
|
575
|
+
delete(flag: number): this;
|
|
576
|
+
/**
|
|
577
|
+
* Determines whether an element is in the set.
|
|
578
|
+
*
|
|
579
|
+
* @param flag - The flag value representing the bit position to set. 0 represents the 0th bit
|
|
580
|
+
* @returns true if the element is in the set; otherwise, false.
|
|
581
|
+
*/
|
|
582
|
+
has(flag: number): boolean;
|
|
583
|
+
/**
|
|
584
|
+
* Clears all elements from the set.
|
|
585
|
+
*/
|
|
586
|
+
clear(): this;
|
|
587
|
+
valueOf(): number;
|
|
588
|
+
toString(): string;
|
|
589
|
+
toJSON(): number;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* Suspends execution for a specified number of milliseconds
|
|
594
|
+
* @param ms - The number of milliseconds to pause execution
|
|
595
|
+
* @example
|
|
596
|
+
* ```ts
|
|
597
|
+
* await sleep(500); // Pause for half a second
|
|
598
|
+
* ```
|
|
599
|
+
* @remarks
|
|
600
|
+
* This implementation uses `setTimeout` under the hood and is more precise
|
|
601
|
+
* for longer durations than `setImmediate`-based approaches
|
|
602
|
+
*/
|
|
603
|
+
declare function sleep(ms: number): Promise<void>;
|
|
604
|
+
/**
|
|
605
|
+
* Yields execution control to the event loop, allowing pending I/O operations
|
|
606
|
+
* and scheduled tasks to run before continuing.
|
|
607
|
+
*
|
|
608
|
+
* @remarks
|
|
609
|
+
* This method creates a microtask checkpoint using `setImmediate`, which helps:
|
|
610
|
+
* - Interleave CPU-intensive work with I/O events
|
|
611
|
+
* - Prevent event loop blocking
|
|
612
|
+
* - Maintain application responsiveness
|
|
613
|
+
*
|
|
614
|
+
* Particularly useful for breaking up long synchronous operations in Node.js.
|
|
615
|
+
*/
|
|
616
|
+
declare function yieldExec(): Promise<void>;
|
|
617
|
+
|
|
618
|
+
declare const DefaultAsyncSemaphoreCapacity = 32;
|
|
619
|
+
type SemaphoreIsReadyFuncType = () => Promise<boolean> | boolean;
|
|
620
|
+
interface BinarySemaphoreOptions {
|
|
621
|
+
initFn?: () => any;
|
|
622
|
+
pauseFn?: () => void;
|
|
623
|
+
resumeFn?: () => void;
|
|
624
|
+
capacity?: number;
|
|
625
|
+
}
|
|
626
|
+
interface BinarySemaphoreAcquireOptions {
|
|
627
|
+
signal?: AbortSignal;
|
|
628
|
+
[n: string]: any;
|
|
629
|
+
}
|
|
630
|
+
interface BinarySemaphoreReleaseOptions {
|
|
631
|
+
token?: any;
|
|
632
|
+
[n: string]: any;
|
|
633
|
+
}
|
|
634
|
+
interface BinarySemaphoreReleaserFunc extends BinarySemaphoreReleaseOptions {
|
|
635
|
+
(): void;
|
|
636
|
+
}
|
|
637
|
+
interface SemaphoreOptions extends BinarySemaphoreOptions {
|
|
638
|
+
maxConcurrency?: number;
|
|
639
|
+
isReadyFn?: SemaphoreIsReadyFuncType;
|
|
640
|
+
}
|
|
641
|
+
interface SemaphoreTaskItem extends BinarySemaphoreAcquireOptions {
|
|
642
|
+
resolve: (value: any) => void;
|
|
643
|
+
reject: (reason?: any) => void;
|
|
644
|
+
token?: any;
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* A binary semaphore implementation for managing concurrency in asynchronous operations.
|
|
648
|
+
* Unlike a general semaphore, a binary semaphore allows only one caller to acquire the semaphore at a time.
|
|
649
|
+
* It provides methods to acquire, release, and manage waiting tasks efficiently.
|
|
650
|
+
*
|
|
651
|
+
* Example usage:
|
|
652
|
+
*
|
|
653
|
+
* ```typescript
|
|
654
|
+
* const semaphore = new Semaphore(5); // Allows 5 concurrent operations.
|
|
655
|
+
*
|
|
656
|
+
* const semaphore = new Semaphore(
|
|
657
|
+
* 4, // Allow 4 concurrent async calls
|
|
658
|
+
* {
|
|
659
|
+
* capacity: 100 // Prealloc space for 100 tokens
|
|
660
|
+
* }
|
|
661
|
+
* );
|
|
662
|
+
*
|
|
663
|
+
* async function fetchData(x) {
|
|
664
|
+
* await semaphore.acquire()
|
|
665
|
+
* try {
|
|
666
|
+
* console.log(semaphore.pendingCount + ' calls to fetch are waiting')
|
|
667
|
+
* // ... do some async stuff with x
|
|
668
|
+
* } finally {
|
|
669
|
+
* semaphore.release();
|
|
670
|
+
* }
|
|
671
|
+
* }
|
|
672
|
+
*
|
|
673
|
+
* const data = await Promise.all(array.map(fetchData));
|
|
674
|
+
* ```
|
|
675
|
+
*/
|
|
676
|
+
declare class BinarySemaphore {
|
|
677
|
+
readonly waiting: Deque<SemaphoreTaskItem | undefined>;
|
|
678
|
+
protected free: any;
|
|
679
|
+
protected emitter: EventEmitter;
|
|
680
|
+
protected useDefaultTokens: boolean;
|
|
681
|
+
protected pauseFn?: () => void;
|
|
682
|
+
protected resumeFn?: () => void;
|
|
683
|
+
protected initTokenFn: (token?: any) => void;
|
|
684
|
+
protected paused: boolean;
|
|
685
|
+
protected _activeCount: number;
|
|
686
|
+
/**
|
|
687
|
+
* Creates a binary semaphore object for managing concurrency in asynchronous operations.
|
|
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
|
+
* ```
|
|
730
|
+
*
|
|
731
|
+
*/
|
|
732
|
+
constructor(options?: BinarySemaphoreOptions);
|
|
733
|
+
initFree(options?: BinarySemaphoreOptions): void;
|
|
734
|
+
onReleased(options?: BinarySemaphoreReleaseOptions): void;
|
|
735
|
+
init(options: BinarySemaphoreOptions): void;
|
|
736
|
+
_newReleaser(options?: BinarySemaphoreReleaseOptions): BinarySemaphoreReleaserFunc;
|
|
737
|
+
_dispatchTask(task: SemaphoreTaskItem, options?: BinarySemaphoreReleaseOptions): void;
|
|
738
|
+
lock(options?: BinarySemaphoreAcquireOptions): any;
|
|
739
|
+
unlock(token?: any): void;
|
|
740
|
+
/**
|
|
741
|
+
* Attempt to acquire a token from the semaphore, if one is available immediately. Otherwise, return undefined.
|
|
742
|
+
*
|
|
743
|
+
* @return Returns a token if the semaphore is not full; otherwise, returns `undefined`.
|
|
744
|
+
*/
|
|
745
|
+
tryAcquire(options?: BinarySemaphoreAcquireOptions): any | undefined;
|
|
746
|
+
/**
|
|
747
|
+
* Acquire a token from the semaphore, thus decrement the number of available execution slots. If initFn is not used then the return value of the function can be discarded.
|
|
748
|
+
* @param options.signal An optional AbortSignal to abort the acquisition process. If aborted, the promise will reject with an AbortError.
|
|
749
|
+
* @return A promise that resolves to a release function when a token is acquired. If the semaphore is full, the caller will be added to a waiting queue.
|
|
750
|
+
*/
|
|
751
|
+
acquire(options?: BinarySemaphoreAcquireOptions): Promise<BinarySemaphoreReleaserFunc>;
|
|
752
|
+
/**
|
|
753
|
+
* Releases the semaphore, incrementing the number of free execution slots. If there are tasks in the waiting queue, the next task will be dispatched.
|
|
754
|
+
* @param options.token Optional token returned by `acquire()` when using a custom `initFn`. If provided, it will be used to unlock the semaphore.
|
|
755
|
+
*/
|
|
756
|
+
release(options?: BinarySemaphoreReleaseOptions): void;
|
|
757
|
+
/**
|
|
758
|
+
* Drains the semaphore and returns all the initialized tokens in an array. Draining is an ideal way to ensure there are no pending async tasks, for example before a process will terminate.
|
|
759
|
+
*/
|
|
760
|
+
drain(): Promise<any[]>;
|
|
761
|
+
abort(reason?: any): void;
|
|
762
|
+
/**
|
|
763
|
+
* Get the total count of all active operations.
|
|
764
|
+
*
|
|
765
|
+
* This method returns the number of operations that are either:
|
|
766
|
+
* - Waiting in the queue to acquire the semaphore (`pendingCount`).
|
|
767
|
+
* - Already acquired the semaphore but have not yet released it.
|
|
768
|
+
*
|
|
769
|
+
* @returns {number} The total count of active operations, including both waiting and ongoing tasks.
|
|
770
|
+
*/
|
|
771
|
+
get activeCount(): number;
|
|
772
|
+
/**
|
|
773
|
+
* Get the number of callers waiting on the semaphore, i.e. the number of pending promises.
|
|
774
|
+
*
|
|
775
|
+
* @returns The number of waiters in the waiting list.
|
|
776
|
+
*/
|
|
777
|
+
get pendingCount(): number;
|
|
778
|
+
}
|
|
779
|
+
/**
|
|
780
|
+
* A Semaphore implementation for managing concurrency in asynchronous operations.
|
|
781
|
+
* Semaphores allow a fixed number of resources to be accessed concurrently.
|
|
782
|
+
* This class extends BinarySemaphore and adds support for a maximum concurrency limit and an optional readiness check.
|
|
783
|
+
*
|
|
784
|
+
* Example usage:
|
|
785
|
+
*
|
|
786
|
+
* ```typescript
|
|
787
|
+
* const semaphore = new Semaphore(5); // Allows 5 concurrent operations.
|
|
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
|
+
* );
|
|
805
|
+
*
|
|
806
|
+
* async function fetchData(x) {
|
|
807
|
+
* await semaphore.acquire()
|
|
808
|
+
* try {
|
|
809
|
+
* console.log(semaphore.pendingCount() + ' calls to fetch are waiting')
|
|
810
|
+
* // ... do some async stuff with x
|
|
811
|
+
* } finally {
|
|
812
|
+
* semaphore.release();
|
|
813
|
+
* }
|
|
814
|
+
* }
|
|
815
|
+
*
|
|
816
|
+
* const data = await Promise.all(array.map(fetchData));
|
|
817
|
+
* ```
|
|
818
|
+
*/
|
|
819
|
+
declare class Semaphore extends BinarySemaphore {
|
|
820
|
+
readonly maxConcurrency: number;
|
|
821
|
+
protected free: Deque<any>;
|
|
822
|
+
private isReady?;
|
|
823
|
+
/**
|
|
824
|
+
* Creates a semaphore object. The first argument is the maximum concurrently number and the second argument is optional.
|
|
825
|
+
*
|
|
826
|
+
* @param maxConcurrency The maximum number of callers allowed to acquire the semaphore concurrently.
|
|
827
|
+
* @param options.initFn Function that is used to initialize the tokens used to manage the semaphore. The default is () => '1'.
|
|
828
|
+
* @param options.pauseFn An optional function that is called to opportunistically request pausing the incoming stream of data,
|
|
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.
|
|
835
|
+
*/
|
|
836
|
+
constructor(maxConcurrency: number | SemaphoreOptions, options?: SemaphoreOptions);
|
|
837
|
+
initFree(options: SemaphoreOptions): void;
|
|
838
|
+
tryAcquire(options?: BinarySemaphoreAcquireOptions): Promise<any | undefined> | any | undefined;
|
|
839
|
+
unlock(token?: any): void;
|
|
840
|
+
lock(options?: BinarySemaphoreAcquireOptions): any;
|
|
841
|
+
drain(): Promise<any[]>;
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
844
|
+
* Creates a rate limiter function that blocks with a promise whenever the rate limit is hit and resolves the promise once the call rate is within the limit set by rps. The second argument is optional.
|
|
845
|
+
*
|
|
846
|
+
* @param rps
|
|
847
|
+
* @param options.timeUnit The `timeUnit` is an optional argument setting the width of the rate limiting window in milliseconds.
|
|
848
|
+
* The default `timeUnit` is 1000 ms, therefore making the rps argument act as requests per second limit.
|
|
849
|
+
* @param options.uniformDistribution The `uniformDistribution` argument enforces a discrete uniform distribution over time,
|
|
850
|
+
* instead of the default that allows hitting the function rps time and then pausing for timeWindow milliseconds. Setting
|
|
851
|
+
* the `uniformDistribution` option is mainly useful in a situation where the flow of rate limit function calls is continuous
|
|
852
|
+
* and and occuring faster than timeUnit (e.g. reading a file) and not enabling it would cause the maximum number of calls to
|
|
853
|
+
* resolve immediately (thus exhaust the limit immediately) and therefore the next bunch calls would need to wait for timeWindow
|
|
854
|
+
* milliseconds. However if the flow is sparse then this option may make the code run slower with no advantages.
|
|
855
|
+
*
|
|
856
|
+
* Examples:
|
|
857
|
+
*
|
|
858
|
+
* ```ts
|
|
859
|
+
* async function f() {
|
|
860
|
+
* const lim = RateLimit(5); // rps
|
|
861
|
+
*
|
|
862
|
+
* for (let i = 0; i < n; i++) {
|
|
863
|
+
* await lim();
|
|
864
|
+
* // ... do something async
|
|
865
|
+
* }
|
|
866
|
+
* }
|
|
867
|
+
* ```
|
|
868
|
+
*
|
|
869
|
+
*
|
|
870
|
+
*/
|
|
871
|
+
declare function RateLimit(rps: number, { timeUnit, uniformDistribution, }?: {
|
|
872
|
+
timeUnit?: number;
|
|
873
|
+
uniformDistribution?: boolean;
|
|
874
|
+
}): () => Promise<void>;
|
|
875
|
+
|
|
876
|
+
/**
|
|
877
|
+
* An asynchronous signal gate that blocks operations until a signal is emitted.
|
|
878
|
+
* This class allows multiple awaiters to wait for a signal and resolves all pending promises with the emitted value.
|
|
879
|
+
* The gate can be reset to reuse for subsequent signals.
|
|
880
|
+
*
|
|
881
|
+
* @example
|
|
882
|
+
*
|
|
883
|
+
* ```typescript
|
|
884
|
+
* // Default type is void, can call signal() without parameters
|
|
885
|
+
* const gate = new SignalGate();
|
|
886
|
+
* gate.signal(); // No parameters required
|
|
887
|
+
*
|
|
888
|
+
* // Example with explicit type
|
|
889
|
+
* const valueGate = new SignalGate<number>();
|
|
890
|
+
* valueGate.signal(42); // Must provide a number value
|
|
891
|
+
* ```
|
|
892
|
+
*/
|
|
893
|
+
declare class SignalGate<T = void> {
|
|
894
|
+
protected _isSignaled: boolean;
|
|
895
|
+
protected _signalValue: T | undefined;
|
|
896
|
+
protected waitQueue: Array<{
|
|
897
|
+
resolve: (value: T) => void;
|
|
898
|
+
reject: (error: any) => void;
|
|
899
|
+
}>;
|
|
900
|
+
get signaled(): boolean;
|
|
901
|
+
/**
|
|
902
|
+
* Emits the signal with an optional value, resolving all pending {@link wait} promises.
|
|
903
|
+
* Subsequent calls have no effect until {@link reset} is called.
|
|
904
|
+
*
|
|
905
|
+
* @param value The value to emit with the signal (only required if T is not void).
|
|
906
|
+
*/
|
|
907
|
+
signal(value?: T): void;
|
|
908
|
+
/**
|
|
909
|
+
* Resets the gate to its initial state, allowing a new signal to be emitted.
|
|
910
|
+
*/
|
|
911
|
+
reset(): void;
|
|
912
|
+
/**
|
|
913
|
+
* Aborts all pending waits, rejecting their promises with an error.
|
|
914
|
+
* This does **not** reset the signal state (the gate remains signaled or unsignaled).
|
|
915
|
+
*
|
|
916
|
+
* @param reason The reason for aborting the waits.
|
|
917
|
+
*/
|
|
918
|
+
abort(reason?: any): void;
|
|
919
|
+
/**
|
|
920
|
+
* Returns a promise that resolves with the emitted signal value.
|
|
921
|
+
* If called after the signal has been emitted, resolves immediately with the stored value.
|
|
922
|
+
*
|
|
923
|
+
* @returns A promise resolving to the signal value (type T).
|
|
924
|
+
*/
|
|
925
|
+
wait(): Promise<T>;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
declare function findPort(port: string | number, portRetryCount?: number): Promise<number>;
|
|
929
|
+
|
|
930
|
+
export { BinarySemaphore, type BinarySemaphoreAcquireOptions, type BinarySemaphoreOptions, type BinarySemaphoreReleaseOptions, type BinarySemaphoreReleaserFunc, ConfigFile, DefaultAllTextFiles, DefaultAsyncSemaphoreCapacity, Deque, FilenameReservedRegex, 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, filenameReservedRegex, findPort, getMultiLevelExtname, glob, isStringIn, isValidFilename, isValidFilepath, normalizeIncludeFiles, parseFrontMatter, parseYaml, reControlCharsRegex, registerYamlTag, removeLeadingEmptyLines, sanitizeFilename, sanitizeFilepath, sleep, stringifyYaml, toCamelCase, toCapitalCase, toPascalCase, traverseFolder, traverseFolderSync, yieldExec };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e,t=Object.create,n=Object.defineProperty,r=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,i=Object.getPrototypeOf,s=Object.prototype.hasOwnProperty,a=(e,t,i,a)=>{if(t&&"object"==typeof t||"function"==typeof t)for(let c of o(t))s.call(e,c)||c===i||n(e,c,{get:()=>t[c],enumerable:!(a=r(t,c))||a.enumerable});return e},c=(e,r,o)=>(o=null!=e?t(i(e)):{},a(!r&&e&&e.__esModule?o:n(o,"default",{value:e,enumerable:!0}),e)),u={};((e,t)=>{for(var r in t)n(e,r,{get:t[r],enumerable:!0})})(u,{ConfigFile:()=>j,DefaultAllTextFiles:()=>O,FilenameReservedRegex:()=>D,WindowsReservedNameRegex:()=>I,arrayHasAll:()=>ee,extNameLevel:()=>X,filenameReservedRegex:()=>W,getMultiLevelExtname:()=>g,glob:()=>S,isStringIn:()=>E,isValidFilename:()=>G,isValidFilepath:()=>K,normalizeIncludeFiles:()=>A,parseFrontMatter:()=>w,parseYaml:()=>h,reControlCharsRegex:()=>Z,registerYamlTag:()=>v,removeLeadingEmptyLines:()=>F,sanitizeFilename:()=>Q,sanitizeFilepath:()=>U,stringifyYaml:()=>x,toCamelCase:()=>M,toCapitalCase:()=>_,toPascalCase:()=>N,traverseFolder:()=>L,traverseFolderSync:()=>T}),module.exports=(e=u,a(n({},"__esModule",{value:!0}),e));var f=require("fs"),l=c(require("path")),m=require("load-config-file"),p=c(require("path"));function g(e,t=1){let n="";for(;t--;){const t=p.default.extname(e);if(!t)break;n=t+n,e=p.default.basename(e,t)}return n}var y=require("yaml"),d=[];function v(e){Array.isArray(e)||(e=[e]);for(const t of e){-1===d.indexOf(t)&&d.push(t)}}function h(e,t){if(t)if(t.customTags){if(Array.isArray(t.customTags))t.customTags=d.concat(t.customTags);else if("function"==typeof t.customTags){const e=t.customTags;t.customTags=t=>e(d.concat(t))}}else t.customTags=d;else t={customTags:d};return(0,y.parse)(e,t)}function x(e,t){if(t)if(t.customTags){if(Array.isArray(t.customTags))t.customTags=d.concat(t.customTags);else if("function"==typeof t.customTags){const e=t.customTags;t.customTags=t=>e(d.concat(t))}}else t.customTags=d;else t={customTags:d};return(0,y.stringify)(e,t)}function F(e){const t=/^\s*(#[^\r\n]*)?[\r\n]+/;let n;for(;null!==(n=t.exec(e))&&((e=e.substring(n[0].length)).startsWith("\n")||e.startsWith("\r")||e.trimStart().startsWith("#")););return e}var b="---";function w(e,t=b){const n=t.length,r=F(e);if(r.startsWith(t)&&("\n"===r[t.length]||"\r"===r[t.length])){let e=r.indexOf("\n"+t,n);if(-1!==e){const o=r.slice(n,e);for(e+=t.length+1;"\n"===r[e]||"\r"===r[e];)e++;const i=r.slice(e);return{data:h(o)||{},content:i}}}return{data:{},content:e}}var j=class{static register(e,t,n){m.Config.register(e,t),"string"==typeof e&&(e=[e]);for(const t of e)this.stringifys[t]=n}static loadSync(e,t){return function(e,{extLevel:t=1,externalFile:n}={}){e=q(e,t);let r=m.Config.loadSync(e);if(!r&&n){if(!l.default.isAbsolute(n)){const t=l.default.dirname(e);n=l.default.join(t,n)}if((0,f.existsSync)(n)){const e=w((0,f.readFileSync)(n,"utf8")).data;Object.keys(e).length&&(r=e)}}return r}(e,t)}static saveSync(e,t,n){return function(e,t,{extLevel:n=1}={}){const r=function(e,t=1){"."===e[0]&&t++;let n=g(e,t);(!n||n.split(".").length<=1)&&(e+=".yaml",n=".yaml");const r=new String(e);return r.extname=n,r}(e,n),o=r.extname;e=r.toString();const i=j.stringifys[o];if(!i)throw new Error(`${e} unsupported mime type: ${o}`);t=i(t);const s=l.default.dirname(e);(0,f.existsSync)(s)||(0,f.mkdirSync)(s,{recursive:!0});return(0,f.writeFileSync)(e,t,{encoding:"utf8"}),e}(e,t,n)}static existsSync(e,t){return e=q(e,t?.extLevel),m.Config.existsSync(e,t)}};function q(e,t=1){"."===e[0]&&t++;const n=g(e,t);return n&&n.split(".").length>1&&(e=e.slice(0,-n.length)),e}j.stringifys={},j.register([".yml",".yaml"],h,x),j.register([".json"],(function(e){return JSON.parse(e)}),(e=>JSON.stringify(e,null,2)));var $=require("@isdk/glob"),R=c(require("path"));function S(e,t,n){return n&&(e=R.default.relative(n,e)),(0,$.globMatch)(e,t)}function E(e,t){return"string"==typeof t&&(t=[t]),-1!==t.indexOf(e)}var O=["**/*.((j|t)s?(x)|m(j|t)s)?(x)","**/*.(md|markdown|txt|?(x)htm?(l)|yaml|yml|xml|json|bat|sh|bash|zsh|ini|css|scss|less|sass|py|rb|php|go|java|c|cpp|h|hpp|hxx|rust|zig)"];function A(e,t=[]){if(e)if(Array.isArray(e))e=[...e];else{const n=e.include||[],r=e.exclude||[];0===n.length&&n.push(...t),e=[...n];for(const t of r)e.push(`!${t}`)}else e=[...t];return 0===e.length&&e.push(...t),e}var C=require("fs/promises"),z=require("fs"),k=c(require("path"));async function L(e,t){const n=await(0,C.readdir)(e,{withFileTypes:!0});for(const r of n){const n=k.default.join(e,r.name);try{if(r.isDirectory()){await t(n,r)||await L(n,t)}else{if(!0===await t(n,r))break}}catch(e){console.error(`Error processing file: ${n}`),console.error(e)}}}function T(e,t){const n=(0,z.readdirSync)(e,{withFileTypes:!0});for(const r of n){const n=k.default.join(e,r.name);try{if(r.isDirectory()){t(n,r)||L(n,t)}else{if(!0===t(n,r))break}}catch(e){console.error(`Error processing file: ${n}`),console.error(e)}}}function N(e){if(!e)return"";return e.replace(/[-_ ]+/g," ").split(" ").filter(Boolean).map((e=>e.charAt(0).toUpperCase()+e.slice(1))).join("")}function M(e){return(e=N(e)).charAt(0).toLowerCase()+e.slice(1)}function _(e){if(!e)return"";return e.replace(/[-_ ]+/g," ").replace(/([a-z])([A-Z])/g,"$1 $2").split(" ").filter(Boolean).map((e=>e.charAt(0).toUpperCase()+e.slice(1).toLowerCase())).join(" ")}var Y=c(require("path")),B=require("@isdk/common-error"),D=/[<>:"/\\|?*\u0000-\u001F]/,I=/^(con|prn|aux|nul|com\d|lpt\d)$/i,J=100,V=/^\.+(\\|\/)|^\.+$/,H=/\.+$/,P=/[\u0000-\u001F\u0080-\u009F\u200E\u200F\u202A-\u202E\u2066-\u2069]/;function W(){return new RegExp(D.source,"g")}function Z(){return new RegExp(P.source,"g")}function G(e){return e&&!(D.test(e)||Z().test(e)||V.test(e)||H.test(e))}function K(e){const t=e.split(Y.default.sep);return("/"===e[0]||t[0]&&Y.default.dirname(t[0])===t[0])&&t.shift(),t.every(G)}function Q(e,t={}){const n=t.replacement||"!";if((D.test(n)||P.test(n))&&(0,B.throwError)("Replacement string cannot contain reserved filename characters","sanitizeFilename",B.ErrorCode.InvalidArgument),n.length>0){const t=/([<>:"/\\|?*\u0000-\u001F]){2,}/;e=e.replace(t,"$1")}if(e=(e=(e=(e=(e=e.normalize("NFD")).replace(V,n)).replace(W(),n)).replace(Z(),n)).replace(H,""),n.length>0){"."===e[0]||"."!==e[0]||(e=n+e),"."===e[e.length-1]&&(e+=n)}e=I.test(e)?e+n:e;const r="number"==typeof t.maxLength?t.maxLength:J;if(e.length>r){const t=e.lastIndexOf(".");if(-1===t)e=e.slice(0,r);else{const n=e.slice(0,t),o=e.slice(t);e=n.slice(0,Math.max(1,r-o.length))+o}}return e}function U(e,t={}){const n=e.split(Y.default.sep);let r;("/"===e[0]||n[0]&&Y.default.dirname(n[0])===n[0])&&(r=n.shift());const o=n.map((e=>Q(e,t)));return void 0!==r&&o.unshift(r),o.join(Y.default.sep)}function X(e){return e.split(".").length-1}function ee(e,t){const n=new Set(t),r=new Set;for(const t of e)if(n.has(t)&&(r.add(t),r.size===n.size))return!0;return r.size===n.size}
|
|
1
|
+
"use strict";var t,e=Object.create,i=Object.defineProperty,s=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,n=Object.getPrototypeOf,o=Object.prototype.hasOwnProperty,u=(t,e,n,u)=>{if(e&&"object"==typeof e||"function"==typeof e)for(let c of r(e))o.call(t,c)||c===n||i(t,c,{get:()=>e[c],enumerable:!(u=s(e,c))||u.enumerable});return t},c=(t,s,r)=>(r=null!=t?e(n(t)):{},u(!s&&t&&t.__esModule?r:i(r,"default",{value:t,enumerable:!0}),t)),h={};((t,e)=>{for(var s in e)i(t,s,{get:e[s],enumerable:!0})})(h,{BinarySemaphore:()=>ft,ConfigFile:()=>q,DefaultAllTextFiles:()=>R,DefaultAsyncSemaphoreCapacity:()=>ht,Deque:()=>it,FilenameReservedRegex:()=>_,IntSet:()=>st,RateLimit:()=>pt,Semaphore:()=>mt,SignalGate:()=>yt,WindowsReservedNameRegex:()=>B,arrayHasAll:()=>tt,extNameLevel:()=>X,filenameReservedRegex:()=>Z,findPort:()=>gt,getMultiLevelExtname:()=>p,glob:()=>A,isStringIn:()=>E,isValidFilename:()=>H,isValidFilepath:()=>W,normalizeIncludeFiles:()=>C,parseFrontMatter:()=>x,parseYaml:()=>g,reControlCharsRegex:()=>G,registerYamlTag:()=>v,removeLeadingEmptyLines:()=>b,sanitizeFilename:()=>K,sanitizeFilepath:()=>Q,sleep:()=>rt,stringifyYaml:()=>w,toCamelCase:()=>L,toCapitalCase:()=>M,toPascalCase:()=>D,traverseFolder:()=>T,traverseFolderSync:()=>z,yieldExec:()=>nt}),module.exports=(t=h,u(i({},"__esModule",{value:!0}),t));var a=require("fs"),l=c(require("path")),f=require("load-config-file"),m=c(require("path"));function p(t,e=1){let i="";for(;e--;){const e=m.default.extname(t);if(!e)break;i=e+i,t=m.default.basename(t,e)}return i}var d=require("yaml"),y=[];function v(t){Array.isArray(t)||(t=[t]);for(const e of t){-1===y.indexOf(e)&&y.push(e)}}function g(t,e){if(e)if(e.customTags){if(Array.isArray(e.customTags))e.customTags=y.concat(e.customTags);else if("function"==typeof e.customTags){const t=e.customTags;e.customTags=e=>t(y.concat(e))}}else e.customTags=y;else e={customTags:y};return(0,d.parse)(t,e)}function w(t,e){if(e)if(e.customTags){if(Array.isArray(e.customTags))e.customTags=y.concat(e.customTags);else if("function"==typeof e.customTags){const t=e.customTags;e.customTags=e=>t(y.concat(e))}}else e.customTags=y;else e={customTags:y};return(0,d.stringify)(t,e)}function b(t){const e=/^\s*(#[^\r\n]*)?[\r\n]+/;let i;for(;null!==(i=e.exec(t))&&((t=t.substring(i[0].length)).startsWith("\n")||t.startsWith("\r")||t.trimStart().startsWith("#")););return t}var F="---";function x(t,e=F){const i=e.length,s=b(t);if(s.startsWith(e)&&("\n"===s[e.length]||"\r"===s[e.length])){let t=s.indexOf("\n"+e,i);if(-1!==t){const r=s.slice(i,t);for(t+=e.length+1;"\n"===s[t]||"\r"===s[t];)t++;const n=s.slice(t);return{data:g(r)||{},content:n}}}return{data:{},content:t}}var q=class{static register(t,e,i){f.Config.register(t,e),"string"==typeof t&&(t=[t]);for(const e of t)this.stringifys[e]=i}static loadSync(t,e){return function(t,{extLevel:e=1,externalFile:i}={}){t=k(t,e);let s=f.Config.loadSync(t);if(!s&&i){if(!l.default.isAbsolute(i)){const e=l.default.dirname(t);i=l.default.join(e,i)}if((0,a.existsSync)(i)){const t=x((0,a.readFileSync)(i,"utf8")).data;Object.keys(t).length&&(s=t)}}return s}(t,e)}static saveSync(t,e,i){return function(t,e,{extLevel:i=1}={}){const s=function(t,e=1){"."===t[0]&&e++;let i=p(t,e);(!i||i.split(".").length<=1)&&(t+=".yaml",i=".yaml");const s=new String(t);return s.extname=i,s}(t,i),r=s.extname;t=s.toString();const n=q.stringifys[r];if(!n)throw new Error(`${t} unsupported mime type: ${r}`);e=n(e);const o=l.default.dirname(t);(0,a.existsSync)(o)||(0,a.mkdirSync)(o,{recursive:!0});return(0,a.writeFileSync)(t,e,{encoding:"utf8"}),t}(t,e,i)}static existsSync(t,e){return t=k(t,e?.extLevel),f.Config.existsSync(t,e)}};function k(t,e=1){"."===t[0]&&e++;const i=p(t,e);return i&&i.split(".").length>1&&(t=t.slice(0,-i.length)),t}q.stringifys={},q.register([".yml",".yaml"],g,w),q.register([".json"],function(t){return JSON.parse(t)},t=>JSON.stringify(t,null,2));var S=require("@isdk/glob"),j=c(require("path"));function A(t,e,i){return i&&(t=j.default.relative(i,t)),(0,S.globMatch)(t,e)}function E(t,e){return"string"==typeof e&&(e=[e]),-1!==e.indexOf(t)}var R=["**/*.((j|t)s?(x)|m(j|t)s)?(x)","**/*.(md|markdown|txt|?(x)htm?(l)|yaml|yml|xml|json|bat|sh|bash|zsh|ini|css|scss|less|sass|py|rb|php|go|java|c|cpp|h|hpp|hxx|rust|zig)"];function C(t,e=[]){if(t)if(Array.isArray(t))t=[...t];else{const i=t.include||[],s=t.exclude||[];0===i.length&&i.push(...e),t=[...i];for(const e of s)t.push(`!${e}`)}else t=[...e];return 0===t.length&&t.push(...e),t}var O=require("fs/promises"),$=require("fs"),P=c(require("path"));async function T(t,e){const i=await(0,O.readdir)(t,{withFileTypes:!0});for(const s of i){const i=P.default.join(t,s.name);try{if(s.isDirectory()){await e(i,s)||await T(i,e)}else{if(!0===await e(i,s))break}}catch(t){console.error(`Error processing file: ${i}`),console.error(t)}}}function z(t,e){const i=(0,$.readdirSync)(t,{withFileTypes:!0});for(const s of i){const i=P.default.join(t,s.name);try{if(s.isDirectory()){e(i,s)||T(i,e)}else{if(!0===e(i,s))break}}catch(t){console.error(`Error processing file: ${i}`),console.error(t)}}}function D(t){if(!t)return"";return t.replace(/[-_ ]+/g," ").split(" ").filter(Boolean).map(t=>{let e=t.charAt(0).toUpperCase();t=t.slice(1);let i=0;for(;/[A-Z]/.test(t[i]);)i++;return e+=i?t.slice(0,i).toLowerCase()+t.slice(i):t,e}).join("")}function L(t){return(t=D(t)).charAt(0).toLowerCase()+t.slice(1)}function M(t){if(!t)return"";return t.replace(/[-_ ]+/g," ").replace(/([a-z])([A-Z])/g,"$1 $2").split(" ").filter(Boolean).map(t=>t.charAt(0).toUpperCase()+t.slice(1).toLowerCase()).join(" ")}var N=c(require("path")),I=require("@isdk/common-error"),_=/[<>:"/\\|?*\u0000-\u001F]/,B=/^(con|prn|aux|nul|com\d|lpt\d)$/i,J=100,Y=/^\.+(\\|\/)|^\.+$/,U=/\.+$/,V=/[\u0000-\u001F\u0080-\u009F\u200E\u200F\u202A-\u202E\u2066-\u2069]/;function Z(){return new RegExp(_.source,"g")}function G(){return new RegExp(V.source,"g")}function H(t){return t&&!(_.test(t)||G().test(t)||Y.test(t)||U.test(t))}function W(t){const e=t.split(N.default.sep);return("/"===t[0]||e[0]&&N.default.dirname(e[0])===e[0])&&e.shift(),e.every(H)}function K(t,e={}){const i=e.replacement||"!";if((_.test(i)||V.test(i))&&(0,I.throwError)("Replacement string cannot contain reserved filename characters","sanitizeFilename",I.ErrorCode.InvalidArgument),i.length>0){const e=/([<>:"/\\|?*\u0000-\u001F]){2,}/;t=t.replace(e,"$1")}if(t=(t=(t=(t=(t=t.normalize("NFD")).replace(Y,i)).replace(Z(),i)).replace(G(),i)).replace(U,""),i.length>0){"."===t[0]||"."!==t[0]||(t=i+t),"."===t[t.length-1]&&(t+=i)}t=B.test(t)?t+i:t;const s="number"==typeof e.maxLength?e.maxLength:J;if(t.length>s){const e=t.lastIndexOf(".");if(-1===e)t=t.slice(0,s);else{const i=t.slice(0,e),r=t.slice(e);t=i.slice(0,Math.max(1,s-r.length))+r}}return t}function Q(t,e={}){const i=t.split(N.default.sep);let s;("/"===t[0]||i[0]&&N.default.dirname(i[0])===i[0])&&(s=i.shift());const r=i.map(t=>K(t,e));return void 0!==s&&r.unshift(s),r.join(N.default.sep)}function X(t){return t.split(".").length-1}function tt(t,e){const i=new Set(e),s=new Set;for(const e of t)if(i.has(e)&&(s.add(e),s.size===i.size))return!0;return s.size===i.size}function et(t=0){return e=Math.min(Math.max(16,t),1073741824),e>>>=0,e-=1,e|=e>>1,e|=e>>2,e|=e>>4,e|=e>>8,1+(e|=e>>16);var e}var it=class extends Array{constructor(t,e){Array.isArray(t)?(super(...t),this._length=t.length,t=void 0):(super(),this._length=0),this._capacity=et(t),this._front=0,this._disableAutoResize=e}push(t){let e=this._length;this.checkCapacity(e+1);const i=this._front+e&this._capacity-1;return this[i]=t,++e<=this._capacity&&(this._length=e),i}unshift(t){let e=this._length;this.checkCapacity(++e);const i=this._capacity,s=(this._front-1&i-1^i)-i;return this[s]=t,this._front=s,e<=this._capacity&&(this._length=e),s}pop(t){let e=this._length;if(0===e)return;let i=this._front+e-1&this._capacity-1,s=this[i];for(;--e>0&&t&&null==s;)i--,s=this[i];return this[i]=void 0,this._length=e,s}shift(t){let e=this._length;if(0===e)return;let i=this._front,s=this[i];for(;--e>0&&t&&null==s;)i=i+1&this._capacity-1,s=this[i];return this[i]=void 0,this._front=i+1&this._capacity-1,this._length=e,s}get size(){return this._length}get(t){let e;if(t===(0|t)){const i=this._length;t<0&&(t+=i),t>=0&&t<i&&(e=this[this._front+t&this._capacity-1])}return e}peekBack(){const t=this._length;if(0===t)return;return this[this._front+t-1&this._capacity-1]}peekFront(){if(0!==this._length)return this[this._front]}clear(){const t=this._length,e=this._front,i=this._capacity;for(let s=0;s<t;++s)this[e+s&i-1]=void 0;this._length=0,this._front=0}isEmpty(){return 0===this._length}removeAt(t){const e=this._length;if(t<0||t>=e)return;const i=this._front,s=this._capacity-1,r=i+t&s,n=this[r];if(t<e/2)this.copyWithin(i+1&s,i,i+t&s),this[i]=void 0,this._front=i+1&s;else{this.copyWithin(r,r+1&s,i+e&s);this[i+e-1&s]=void 0}return this._length=e-1,n}checkCapacity(t){this._capacity<t&&!this._disableAutoResize&&this.resizeTo(et(1.5*this._capacity+16))}resizeTo(t){const e=this._capacity;this._capacity=t;const i=this._front,s=this._length;if(i+s>e){!function(t,e,i,s,r){for(let n=0;n<r;++n)i[n+s]=t[n+e],t[n+e]=void 0}(this,0,this,e,i+s&e-1)}}},st=class t{constructor(t=0){this.bitField=t}static has(t,e){return!!(t&1<<e)}static add(t,e){return t|1<<e}static delete(t,e){return t&~(1<<e)}add(t){return this.bitField|=1<<t,this}delete(t){return this.bitField&=~(1<<t),this}has(e){return t.has(this.bitField,e)}clear(){return this.bitField=0,this}valueOf(){return this.bitField}toString(){return this.bitField.toString()}toJSON(){return this.bitField}};async function rt(t){return new Promise(e=>setTimeout(e,t))}async function nt(){return new Promise(t=>{setImmediate(t)})}var ot=require("util-ex"),ut=require("events-ex"),ct=require("@isdk/common-error"),ht=32;function at(t){return"function"==typeof t}function lt(){return"1"}var ft=class{constructor(t={}){const{initFn:e=lt,pauseFn:i,resumeFn:s,capacity:r=ht}=t;if(at(i)!==at(s))throw new Error("pauseFn and resumeFn must be both set for pausing");this.waiting=new it(r),this.emitter=new ut.EventEmitter,this.useDefaultTokens=e===lt,this.pauseFn=i,this.resumeFn=s,this.initTokenFn=e,this.paused=!1,this._activeCount=0,this.initFree(t),this.init(t)}initFree(t){this.free=this.initTokenFn()}onReleased(t){const e=t?.token,i=this.waiting.shift(!0);i?this._dispatchTask(i,t):(this.resumeFn&&this.paused&&(this.paused=!1,this.resumeFn()),this.unlock(e))}init(t){this.emitter.on("release",t=>{this.onReleased(t)})}_newReleaser(t){let e=!1;const i=()=>{e||(e=!0,this.release(t))};return t&&Object.assign(i,t),i}_dispatchTask(t,e){const{resolve:i}=t;i(this._newReleaser(e))}lock(t){let e=this.free;if(e)return this.free=void 0,e}unlock(t){this.free=this.useDefaultTokens?"1":t??this.initTokenFn()}tryAcquire(t){return this.lock(t)}async acquire(t){this._activeCount++;const e=t?.signal,i=t=>{this.pauseFn&&!this.paused&&(this.paused=!0,this.pauseFn());const i=this.waiting.push(t),s=t.reject;return e&&e.addEventListener("abort",()=>{this.waiting[i]=void 0;const t=e.reason instanceof Error?e.reason:new ct.AbortError(e.reason||"aborted");e.alreadyRejected=!0,s(t)}),i};let s=this.tryAcquire(t);const r=s&&(0,ot.isAsync)(s),n=e=>new Promise((s,r)=>{const n={...t,resolve:s,reject:r,token:e};void 0===e?i(n):this._dispatchTask(n,{...t,token:e})});return r?s.then(t=>n(t)):n(s)}release(t){this._activeCount--,this.emitter.emit("release",t)}drain(){const t=[this.acquire()];return Promise.all(t)}abort(t){let e;for(;e=this.waiting.shift(!0);)e.reject(new ct.AbortError(t))}get activeCount(){return this._activeCount}get pendingCount(){return this.waiting.size}},mt=class extends ft{constructor(t,e){if("number"==typeof t)(e=e||{}).maxConcurrency=t;else{if("number"!=typeof(e=t).maxConcurrency)throw new Error("maxConcurrency must be set");t=e.maxConcurrency}super(e),this.maxConcurrency=e.maxConcurrency,e.isReadyFn&&(this.isReady=e.isReadyFn)}initFree(t){const e=t.maxConcurrency=Math.max(1,t.maxConcurrency);this.free=new it(e);for(let t=0;t<e;t++)this.free.push(this.initTokenFn())}tryAcquire(t){let e=this.isReady;if(e&&(0,ot.isAsync)(e)){return e instanceof Promise||(e=e()),e.then(e=>{if(e)return this.lock(t)})}if(!e||e())return this.lock(t)}unlock(t){this.free.push(this.useDefaultTokens?"1":t??this.initTokenFn())}lock(t){return this.free.pop()}drain(){const t=new Array(this.maxConcurrency);for(let e=0;e<this.maxConcurrency;e++)t[e]=this.acquire();return Promise.all(t)}};function pt(t,{timeUnit:e=1e3,uniformDistribution:i=!1}={}){const s=new mt(i?1:t),r=i?e/t:e;return async function(){await s.acquire(),setTimeout(()=>s.release(),r)}}var dt=require("@isdk/common-error"),yt=class{constructor(){this._isSignaled=!1,this.waitQueue=[]}get signaled(){return this._isSignaled}signal(t){if(this._isSignaled)return;this._isSignaled=!0,this._signalValue=t;const e=this.waitQueue.slice();for(this.waitQueue.length=0;e.length>0;){const t=e.shift();t?.resolve(this._signalValue)}}reset(){this._isSignaled=!1,this._signalValue=void 0,this.waitQueue.length=0}abort(t){if(this.waitQueue.length){const e=this.waitQueue.slice();this.waitQueue.length=0;const i=new dt.AbortError(t);for(;e.length>0;){const{reject:t}=e.shift();t(i)}}}async wait(){return new Promise((t,e)=>{this._isSignaled?t(this._signalValue):this.waitQueue.push({resolve:t,reject:e})})}},vt=c(require("net"));async function gt(t,e=10){return new Promise((i,s)=>{void 0===t?t=0:("string"==typeof t&&(t=parseInt(t)),t>=0||(t=0));const r=vt.default.createServer();r.on("error",i=>{"EADDRINUSE"===i.code&&(t++,--e>0)?r.listen(t):s(i)}),r.on("listening",async()=>{const t=r.address().port;r.close(e=>{e?s(e):i(t)})}),r.listen(t)})}
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{existsSync as t,mkdirSync as n,readFileSync as r,writeFileSync as o}from"fs";import e from"path";import{Config as i}from"load-config-file";import s from"path";function c(t,n=1){let r="";for(;n--;){const n=s.extname(t);if(!n)break;r=n+r,t=s.basename(t,n)}return r}import{parse as f,stringify as u}from"yaml";var a=[];function l(t){Array.isArray(t)||(t=[t]);for(const n of t){-1===a.indexOf(n)&&a.push(n)}}function m(t,n){if(n)if(n.customTags){if(Array.isArray(n.customTags))n.customTags=a.concat(n.customTags);else if("function"==typeof n.customTags){const t=n.customTags;n.customTags=n=>t(a.concat(n))}}else n.customTags=a;else n={customTags:a};return f(t,n)}function p(t,n){if(n)if(n.customTags){if(Array.isArray(n.customTags))n.customTags=a.concat(n.customTags);else if("function"==typeof n.customTags){const t=n.customTags;n.customTags=n=>t(a.concat(n))}}else n.customTags=a;else n={customTags:a};return u(t,n)}function y(t){const n=/^\s*(#[^\r\n]*)?[\r\n]+/;let r;for(;null!==(r=n.exec(t))&&((t=t.substring(r[0].length)).startsWith("\n")||t.startsWith("\r")||t.trimStart().startsWith("#")););return t}function g(t,n="---"){const r=n.length,o=y(t);if(o.startsWith(n)&&("\n"===o[n.length]||"\r"===o[n.length])){let t=o.indexOf("\n"+n,r);if(-1!==t){const e=o.slice(r,t);for(t+=n.length+1;"\n"===o[t]||"\r"===o[t];)t++;const i=o.slice(t);return{data:m(e)||{},content:i}}}return{data:{},content:t}}var h=class{static register(t,n,r){i.register(t,n),"string"==typeof t&&(t=[t]);for(const n of t)this.stringifys[n]=r}static loadSync(n,o){return function(n,{extLevel:o=1,externalFile:s}={}){n=x(n,o);let c=i.loadSync(n);if(!c&&s){if(!e.isAbsolute(s)){const t=e.dirname(n);s=e.join(t,s)}if(t(s)){const t=g(r(s,"utf8")).data;Object.keys(t).length&&(c=t)}}return c}(n,o)}static saveSync(r,i,s){return function(r,i,{extLevel:s=1}={}){const f=function(t,n=1){"."===t[0]&&n++;let r=c(t,n);(!r||r.split(".").length<=1)&&(t+=".yaml",r=".yaml");const o=new String(t);return o.extname=r,o}(r,s),u=f.extname;r=f.toString();const a=h.stringifys[u];if(!a)throw new Error(`${r} unsupported mime type: ${u}`);i=a(i);const l=e.dirname(r);t(l)||n(l,{recursive:!0});return o(r,i,{encoding:"utf8"}),r}(r,i,s)}static existsSync(t,n){return t=x(t,n?.extLevel),i.existsSync(t,n)}};function x(t,n=1){"."===t[0]&&n++;const r=c(t,n);return r&&r.split(".").length>1&&(t=t.slice(0,-r.length)),t}h.stringifys={},h.register([".yml",".yaml"],m,p),h.register([".json"],(function(t){return JSON.parse(t)}),(t=>JSON.stringify(t,null,2)));import{globMatch as d}from"@isdk/glob";import w from"path";function v(t,n,r){return r&&(t=w.relative(r,t)),d(t,n)}function $(t,n){return"string"==typeof n&&(n=[n]),-1!==n.indexOf(t)}var F=["**/*.((j|t)s?(x)|m(j|t)s)?(x)","**/*.(md|markdown|txt|?(x)htm?(l)|yaml|yml|xml|json|bat|sh|bash|zsh|ini|css|scss|less|sass|py|rb|php|go|java|c|cpp|h|hpp|hxx|rust|zig)"];function b(t,n=[]){if(t)if(Array.isArray(t))t=[...t];else{const r=t.include||[],o=t.exclude||[];0===r.length&&r.push(...n),t=[...r];for(const n of o)t.push(`!${n}`)}else t=[...n];return 0===t.length&&t.push(...n),t}import{readdir as S}from"fs/promises";import{readdirSync as E}from"fs";import j from"path";async function k(t,n){const r=await S(t,{withFileTypes:!0});for(const o of r){const r=j.join(t,o.name);try{if(o.isDirectory()){await n(r,o)||await k(r,n)}else{if(!0===await n(r,o))break}}catch(t){console.error(`Error processing file: ${r}`),console.error(t)}}}function A(t,n){const r=E(t,{withFileTypes:!0});for(const o of r){const r=j.join(t,o.name);try{if(o.isDirectory()){n(r,o)||k(r,n)}else{if(!0===n(r,o))break}}catch(t){console.error(`Error processing file: ${r}`),console.error(t)}}}function z(t){if(!t)return"";return t.replace(/[-_ ]+/g," ").split(" ").filter(Boolean).map((t=>t.charAt(0).toUpperCase()+t.slice(1))).join("")}function T(t){return(t=z(t)).charAt(0).toLowerCase()+t.slice(1)}function N(t){if(!t)return"";return t.replace(/[-_ ]+/g," ").replace(/([a-z])([A-Z])/g,"$1 $2").split(" ").filter(Boolean).map((t=>t.charAt(0).toUpperCase()+t.slice(1).toLowerCase())).join(" ")}import O from"path";import{ErrorCode as R,throwError as B}from"@isdk/common-error";var J=/[<>:"/\\|?*\u0000-\u001F]/,L=/^(con|prn|aux|nul|com\d|lpt\d)$/i,_=/^\.+(\\|\/)|^\.+$/,C=/\.+$/,D=/[\u0000-\u001F\u0080-\u009F\u200E\u200F\u202A-\u202E\u2066-\u2069]/;function M(){return new RegExp(J.source,"g")}function Z(){return new RegExp(D.source,"g")}function q(t){return t&&!(J.test(t)||Z().test(t)||_.test(t)||C.test(t))}function G(t){const n=t.split(O.sep);return("/"===t[0]||n[0]&&O.dirname(n[0])===n[0])&&n.shift(),n.every(q)}function H(t,n={}){const r=n.replacement||"!";if((J.test(r)||D.test(r))&&B("Replacement string cannot contain reserved filename characters","sanitizeFilename",R.InvalidArgument),r.length>0){const n=/([<>:"/\\|?*\u0000-\u001F]){2,}/;t=t.replace(n,"$1")}if(t=(t=(t=(t=(t=t.normalize("NFD")).replace(_,r)).replace(M(),r)).replace(Z(),r)).replace(C,""),r.length>0){"."===t[0]||"."!==t[0]||(t=r+t),"."===t[t.length-1]&&(t+=r)}t=L.test(t)?t+r:t;const o="number"==typeof n.maxLength?n.maxLength:100;if(t.length>o){const n=t.lastIndexOf(".");if(-1===n)t=t.slice(0,o);else{const r=t.slice(0,n),e=t.slice(n);t=r.slice(0,Math.max(1,o-e.length))+e}}return t}function I(t,n={}){const r=t.split(O.sep);let o;("/"===t[0]||r[0]&&O.dirname(r[0])===r[0])&&(o=r.shift());const e=r.map((t=>H(t,n)));return void 0!==o&&e.unshift(o),e.join(O.sep)}function K(t){return t.split(".").length-1}function P(t,n){const r=new Set(n),o=new Set;for(const n of t)if(r.has(n)&&(o.add(n),o.size===r.size))return!0;return o.size===r.size}export{h as ConfigFile,F as DefaultAllTextFiles,J as FilenameReservedRegex,L as WindowsReservedNameRegex,P as arrayHasAll,K as extNameLevel,M as filenameReservedRegex,c as getMultiLevelExtname,v as glob,$ as isStringIn,q as isValidFilename,G as isValidFilepath,b as normalizeIncludeFiles,g as parseFrontMatter,m as parseYaml,Z as reControlCharsRegex,l as registerYamlTag,y as removeLeadingEmptyLines,H as sanitizeFilename,I as sanitizeFilepath,p as stringifyYaml,T as toCamelCase,N as toCapitalCase,z as toPascalCase,k as traverseFolder,A as traverseFolderSync};
|
|
1
|
+
import{existsSync as t,mkdirSync as s,readFileSync as i,writeFileSync as r}from"fs";import n from"path";import{Config as e}from"load-config-file";import o from"path";function h(t,s=1){let i="";for(;s--;){const s=o.extname(t);if(!s)break;i=s+i,t=o.basename(t,s)}return i}import{parse as c,stringify as u}from"yaml";var f=[];function a(t){Array.isArray(t)||(t=[t]);for(const s of t){-1===f.indexOf(s)&&f.push(s)}}function l(t,s){if(s)if(s.customTags){if(Array.isArray(s.customTags))s.customTags=f.concat(s.customTags);else if("function"==typeof s.customTags){const t=s.customTags;s.customTags=s=>t(f.concat(s))}}else s.customTags=f;else s={customTags:f};return c(t,s)}function m(t,s){if(s)if(s.customTags){if(Array.isArray(s.customTags))s.customTags=f.concat(s.customTags);else if("function"==typeof s.customTags){const t=s.customTags;s.customTags=s=>t(f.concat(s))}}else s.customTags=f;else s={customTags:f};return u(t,s)}function p(t){const s=/^\s*(#[^\r\n]*)?[\r\n]+/;let i;for(;null!==(i=s.exec(t))&&((t=t.substring(i[0].length)).startsWith("\n")||t.startsWith("\r")||t.trimStart().startsWith("#")););return t}function y(t,s="---"){const i=s.length,r=p(t);if(r.startsWith(s)&&("\n"===r[s.length]||"\r"===r[s.length])){let t=r.indexOf("\n"+s,i);if(-1!==t){const n=r.slice(i,t);for(t+=s.length+1;"\n"===r[t]||"\r"===r[t];)t++;const e=r.slice(t);return{data:l(n)||{},content:e}}}return{data:{},content:t}}var d=class{static register(t,s,i){e.register(t,s),"string"==typeof t&&(t=[t]);for(const s of t)this.stringifys[s]=i}static loadSync(s,r){return function(s,{extLevel:r=1,externalFile:o}={}){s=v(s,r);let h=e.loadSync(s);if(!h&&o){if(!n.isAbsolute(o)){const t=n.dirname(s);o=n.join(t,o)}if(t(o)){const t=y(i(o,"utf8")).data;Object.keys(t).length&&(h=t)}}return h}(s,r)}static saveSync(i,e,o){return function(i,e,{extLevel:o=1}={}){const c=function(t,s=1){"."===t[0]&&s++;let i=h(t,s);(!i||i.split(".").length<=1)&&(t+=".yaml",i=".yaml");const r=new String(t);return r.extname=i,r}(i,o),u=c.extname;i=c.toString();const f=d.stringifys[u];if(!f)throw new Error(`${i} unsupported mime type: ${u}`);e=f(e);const a=n.dirname(i);t(a)||s(a,{recursive:!0});return r(i,e,{encoding:"utf8"}),i}(i,e,o)}static existsSync(t,s){return t=v(t,s?.extLevel),e.existsSync(t,s)}};function v(t,s=1){"."===t[0]&&s++;const i=h(t,s);return i&&i.split(".").length>1&&(t=t.slice(0,-i.length)),t}d.stringifys={},d.register([".yml",".yaml"],l,m),d.register([".json"],function(t){return JSON.parse(t)},t=>JSON.stringify(t,null,2));import{globMatch as g}from"@isdk/glob";import w from"path";function b(t,s,i){return i&&(t=w.relative(i,t)),g(t,s)}function x(t,s){return"string"==typeof s&&(s=[s]),-1!==s.indexOf(t)}var k=["**/*.((j|t)s?(x)|m(j|t)s)?(x)","**/*.(md|markdown|txt|?(x)htm?(l)|yaml|yml|xml|json|bat|sh|bash|zsh|ini|css|scss|less|sass|py|rb|php|go|java|c|cpp|h|hpp|hxx|rust|zig)"];function F(t,s=[]){if(t)if(Array.isArray(t))t=[...t];else{const i=t.include||[],r=t.exclude||[];0===i.length&&i.push(...s),t=[...i];for(const s of r)t.push(`!${s}`)}else t=[...s];return 0===t.length&&t.push(...s),t}import{readdir as A}from"fs/promises";import{readdirSync as E}from"fs";import S from"path";async function $(t,s){const i=await A(t,{withFileTypes:!0});for(const r of i){const i=S.join(t,r.name);try{if(r.isDirectory()){await s(i,r)||await $(i,s)}else{if(!0===await s(i,r))break}}catch(t){console.error(`Error processing file: ${i}`),console.error(t)}}}function j(t,s){const i=E(t,{withFileTypes:!0});for(const r of i){const i=S.join(t,r.name);try{if(r.isDirectory()){s(i,r)||$(i,s)}else{if(!0===s(i,r))break}}catch(t){console.error(`Error processing file: ${i}`),console.error(t)}}}function P(t){if(!t)return"";return t.replace(/[-_ ]+/g," ").split(" ").filter(Boolean).map(t=>{let s=t.charAt(0).toUpperCase();t=t.slice(1);let i=0;for(;/[A-Z]/.test(t[i]);)i++;return s+=i?t.slice(0,i).toLowerCase()+t.slice(i):t,s}).join("")}function T(t){return(t=P(t)).charAt(0).toLowerCase()+t.slice(1)}function z(t){if(!t)return"";return t.replace(/[-_ ]+/g," ").replace(/([a-z])([A-Z])/g,"$1 $2").split(" ").filter(Boolean).map(t=>t.charAt(0).toUpperCase()+t.slice(1).toLowerCase()).join(" ")}import O from"path";import{ErrorCode as R,throwError as C}from"@isdk/common-error";var N=/[<>:"/\\|?*\u0000-\u001F]/,D=/^(con|prn|aux|nul|com\d|lpt\d)$/i,M=/^\.+(\\|\/)|^\.+$/,_=/\.+$/,q=/[\u0000-\u001F\u0080-\u009F\u200E\u200F\u202A-\u202E\u2066-\u2069]/;function B(){return new RegExp(N.source,"g")}function I(){return new RegExp(q.source,"g")}function J(t){return t&&!(N.test(t)||I().test(t)||M.test(t)||_.test(t))}function L(t){const s=t.split(O.sep);return("/"===t[0]||s[0]&&O.dirname(s[0])===s[0])&&s.shift(),s.every(J)}function U(t,s={}){const i=s.replacement||"!";if((N.test(i)||q.test(i))&&C("Replacement string cannot contain reserved filename characters","sanitizeFilename",R.InvalidArgument),i.length>0){const s=/([<>:"/\\|?*\u0000-\u001F]){2,}/;t=t.replace(s,"$1")}if(t=(t=(t=(t=(t=t.normalize("NFD")).replace(M,i)).replace(B(),i)).replace(I(),i)).replace(_,""),i.length>0){"."===t[0]||"."!==t[0]||(t=i+t),"."===t[t.length-1]&&(t+=i)}t=D.test(t)?t+i:t;const r="number"==typeof s.maxLength?s.maxLength:100;if(t.length>r){const s=t.lastIndexOf(".");if(-1===s)t=t.slice(0,r);else{const i=t.slice(0,s),n=t.slice(s);t=i.slice(0,Math.max(1,r-n.length))+n}}return t}function Z(t,s={}){const i=t.split(O.sep);let r;("/"===t[0]||i[0]&&O.dirname(i[0])===i[0])&&(r=i.shift());const n=i.map(t=>U(t,s));return void 0!==r&&n.unshift(r),n.join(O.sep)}function G(t){return t.split(".").length-1}function H(t,s){const i=new Set(s),r=new Set;for(const s of t)if(i.has(s)&&(r.add(s),r.size===i.size))return!0;return r.size===i.size}function K(t=0){return s=Math.min(Math.max(16,t),1073741824),s>>>=0,s-=1,s|=s>>1,s|=s>>2,s|=s>>4,s|=s>>8,1+(s|=s>>16);var s}var Q=class extends Array{constructor(t,s){Array.isArray(t)?(super(...t),this._length=t.length,t=void 0):(super(),this._length=0),this._capacity=K(t),this._front=0,this._disableAutoResize=s}push(t){let s=this._length;this.checkCapacity(s+1);const i=this._front+s&this._capacity-1;return this[i]=t,++s<=this._capacity&&(this._length=s),i}unshift(t){let s=this._length;this.checkCapacity(++s);const i=this._capacity,r=(this._front-1&i-1^i)-i;return this[r]=t,this._front=r,s<=this._capacity&&(this._length=s),r}pop(t){let s=this._length;if(0===s)return;let i=this._front+s-1&this._capacity-1,r=this[i];for(;--s>0&&t&&null==r;)i--,r=this[i];return this[i]=void 0,this._length=s,r}shift(t){let s=this._length;if(0===s)return;let i=this._front,r=this[i];for(;--s>0&&t&&null==r;)i=i+1&this._capacity-1,r=this[i];return this[i]=void 0,this._front=i+1&this._capacity-1,this._length=s,r}get size(){return this._length}get(t){let s;if(t===(0|t)){const i=this._length;t<0&&(t+=i),t>=0&&t<i&&(s=this[this._front+t&this._capacity-1])}return s}peekBack(){const t=this._length;if(0===t)return;return this[this._front+t-1&this._capacity-1]}peekFront(){if(0!==this._length)return this[this._front]}clear(){const t=this._length,s=this._front,i=this._capacity;for(let r=0;r<t;++r)this[s+r&i-1]=void 0;this._length=0,this._front=0}isEmpty(){return 0===this._length}removeAt(t){const s=this._length;if(t<0||t>=s)return;const i=this._front,r=this._capacity-1,n=i+t&r,e=this[n];if(t<s/2)this.copyWithin(i+1&r,i,i+t&r),this[i]=void 0,this._front=i+1&r;else{this.copyWithin(n,n+1&r,i+s&r);this[i+s-1&r]=void 0}return this._length=s-1,e}checkCapacity(t){this._capacity<t&&!this._disableAutoResize&&this.resizeTo(K(1.5*this._capacity+16))}resizeTo(t){const s=this._capacity;this._capacity=t;const i=this._front,r=this._length;if(i+r>s){!function(t,s,i,r,n){for(let e=0;e<n;++e)i[e+r]=t[e+s],t[e+s]=void 0}(this,0,this,s,i+r&s-1)}}},V=class t{constructor(t=0){this.bitField=t}static has(t,s){return!!(t&1<<s)}static add(t,s){return t|1<<s}static delete(t,s){return t&~(1<<s)}add(t){return this.bitField|=1<<t,this}delete(t){return this.bitField&=~(1<<t),this}has(s){return t.has(this.bitField,s)}clear(){return this.bitField=0,this}valueOf(){return this.bitField}toString(){return this.bitField.toString()}toJSON(){return this.bitField}};async function W(t){return new Promise(s=>setTimeout(s,t))}async function X(){return new Promise(t=>{setImmediate(t)})}import{isAsync as Y}from"util-ex";import{EventEmitter as tt}from"events-ex";import{AbortError as st}from"@isdk/common-error";var it=32;function rt(t){return"function"==typeof t}function nt(){return"1"}var et=class{constructor(t={}){const{initFn:s=nt,pauseFn:i,resumeFn:r,capacity:n=it}=t;if(rt(i)!==rt(r))throw new Error("pauseFn and resumeFn must be both set for pausing");this.waiting=new Q(n),this.emitter=new tt,this.useDefaultTokens=s===nt,this.pauseFn=i,this.resumeFn=r,this.initTokenFn=s,this.paused=!1,this._activeCount=0,this.initFree(t),this.init(t)}initFree(t){this.free=this.initTokenFn()}onReleased(t){const s=t?.token,i=this.waiting.shift(!0);i?this._dispatchTask(i,t):(this.resumeFn&&this.paused&&(this.paused=!1,this.resumeFn()),this.unlock(s))}init(t){this.emitter.on("release",t=>{this.onReleased(t)})}_newReleaser(t){let s=!1;const i=()=>{s||(s=!0,this.release(t))};return t&&Object.assign(i,t),i}_dispatchTask(t,s){const{resolve:i}=t;i(this._newReleaser(s))}lock(t){let s=this.free;if(s)return this.free=void 0,s}unlock(t){this.free=this.useDefaultTokens?"1":t??this.initTokenFn()}tryAcquire(t){return this.lock(t)}async acquire(t){this._activeCount++;const s=t?.signal,i=t=>{this.pauseFn&&!this.paused&&(this.paused=!0,this.pauseFn());const i=this.waiting.push(t),r=t.reject;return s&&s.addEventListener("abort",()=>{this.waiting[i]=void 0;const t=s.reason instanceof Error?s.reason:new st(s.reason||"aborted");s.alreadyRejected=!0,r(t)}),i};let r=this.tryAcquire(t);const n=r&&Y(r),e=s=>new Promise((r,n)=>{const e={...t,resolve:r,reject:n,token:s};void 0===s?i(e):this._dispatchTask(e,{...t,token:s})});return n?r.then(t=>e(t)):e(r)}release(t){this._activeCount--,this.emitter.emit("release",t)}drain(){const t=[this.acquire()];return Promise.all(t)}abort(t){let s;for(;s=this.waiting.shift(!0);)s.reject(new st(t))}get activeCount(){return this._activeCount}get pendingCount(){return this.waiting.size}},ot=class extends et{constructor(t,s){if("number"==typeof t)(s=s||{}).maxConcurrency=t;else{if("number"!=typeof(s=t).maxConcurrency)throw new Error("maxConcurrency must be set");t=s.maxConcurrency}super(s),this.maxConcurrency=s.maxConcurrency,s.isReadyFn&&(this.isReady=s.isReadyFn)}initFree(t){const s=t.maxConcurrency=Math.max(1,t.maxConcurrency);this.free=new Q(s);for(let t=0;t<s;t++)this.free.push(this.initTokenFn())}tryAcquire(t){let s=this.isReady;if(s&&Y(s)){return s instanceof Promise||(s=s()),s.then(s=>{if(s)return this.lock(t)})}if(!s||s())return this.lock(t)}unlock(t){this.free.push(this.useDefaultTokens?"1":t??this.initTokenFn())}lock(t){return this.free.pop()}drain(){const t=new Array(this.maxConcurrency);for(let s=0;s<this.maxConcurrency;s++)t[s]=this.acquire();return Promise.all(t)}};function ht(t,{timeUnit:s=1e3,uniformDistribution:i=!1}={}){const r=new ot(i?1:t),n=i?s/t:s;return async function(){await r.acquire(),setTimeout(()=>r.release(),n)}}import{AbortError as ct}from"@isdk/common-error";var ut=class{constructor(){this._isSignaled=!1,this.waitQueue=[]}get signaled(){return this._isSignaled}signal(t){if(this._isSignaled)return;this._isSignaled=!0,this._signalValue=t;const s=this.waitQueue.slice();for(this.waitQueue.length=0;s.length>0;){const t=s.shift();t?.resolve(this._signalValue)}}reset(){this._isSignaled=!1,this._signalValue=void 0,this.waitQueue.length=0}abort(t){if(this.waitQueue.length){const s=this.waitQueue.slice();this.waitQueue.length=0;const i=new ct(t);for(;s.length>0;){const{reject:t}=s.shift();t(i)}}}async wait(){return new Promise((t,s)=>{this._isSignaled?t(this._signalValue):this.waitQueue.push({resolve:t,reject:s})})}};import ft from"net";async function at(t,s=10){return new Promise((i,r)=>{void 0===t?t=0:("string"==typeof t&&(t=parseInt(t)),t>=0||(t=0));const n=ft.createServer();n.on("error",i=>{"EADDRINUSE"===i.code&&(t++,--s>0)?n.listen(t):r(i)}),n.on("listening",async()=>{const t=n.address().port;n.close(s=>{s?r(s):i(t)})}),n.listen(t)})}export{et as BinarySemaphore,d as ConfigFile,k as DefaultAllTextFiles,it as DefaultAsyncSemaphoreCapacity,Q as Deque,N as FilenameReservedRegex,V as IntSet,ht as RateLimit,ot as Semaphore,ut as SignalGate,D as WindowsReservedNameRegex,H as arrayHasAll,G as extNameLevel,B as filenameReservedRegex,at as findPort,h as getMultiLevelExtname,b as glob,x as isStringIn,J as isValidFilename,L as isValidFilepath,F as normalizeIncludeFiles,y as parseFrontMatter,l as parseYaml,I as reControlCharsRegex,a as registerYamlTag,p as removeLeadingEmptyLines,U as sanitizeFilename,Z as sanitizeFilepath,W as sleep,m as stringifyYaml,T as toCamelCase,z as toCapitalCase,P as toPascalCase,$ as traverseFolder,j as traverseFolderSync,X as yieldExec};
|