@oxog/utils 1.0.0 → 1.0.1
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/CHANGELOG.md +38 -0
- package/README.md +50 -5
- package/dist/index.cjs +344 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +257 -5
- package/dist/index.d.ts +257 -5
- package/dist/index.js +331 -24
- package/dist/index.js.map +1 -1
- package/dist/plugins/async.cjs.map +1 -1
- package/dist/plugins/async.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -690,19 +690,145 @@ declare namespace string {
|
|
|
690
690
|
export { string_camelCase as camelCase, string_capitalize as capitalize, string_kebabCase as kebabCase, string_pascalCase as pascalCase, string_slugify as slugify, string_snakeCase as snakeCase, string_template as template, string_truncate as truncate };
|
|
691
691
|
}
|
|
692
692
|
|
|
693
|
+
type EventHandler = (...args: any[]) => void;
|
|
694
|
+
/**
|
|
695
|
+
* Simple event bus for inter-plugin communication.
|
|
696
|
+
*
|
|
697
|
+
* Allows plugins to emit and listen to events, enabling
|
|
698
|
+
* communication between different parts of the system.
|
|
699
|
+
*/
|
|
700
|
+
declare class EventBus {
|
|
701
|
+
private listeners;
|
|
702
|
+
private scopedHandlers;
|
|
703
|
+
/**
|
|
704
|
+
* Register an event listener.
|
|
705
|
+
*
|
|
706
|
+
* @param event - Event name to listen for
|
|
707
|
+
* @param handler - Function to call when event is emitted
|
|
708
|
+
*/
|
|
709
|
+
on(event: string, handler: (...args: any[]) => void): void;
|
|
710
|
+
/**
|
|
711
|
+
* Unregister an event listener.
|
|
712
|
+
*
|
|
713
|
+
* @param event - Event name
|
|
714
|
+
* @param handler - Function to remove
|
|
715
|
+
*/
|
|
716
|
+
off(event: string, handler: (...args: any[]) => void): void;
|
|
717
|
+
/**
|
|
718
|
+
* Emit an event to all registered listeners.
|
|
719
|
+
*
|
|
720
|
+
* @param event - Event name to emit
|
|
721
|
+
* @param args - Arguments to pass to handlers
|
|
722
|
+
*/
|
|
723
|
+
emit(event: string, ...args: any[]): void;
|
|
724
|
+
/**
|
|
725
|
+
* Remove all listeners for an event or all events.
|
|
726
|
+
*
|
|
727
|
+
* @param event - Event name to clear (if not provided, clears all)
|
|
728
|
+
*/
|
|
729
|
+
removeAllListeners(event?: string): void;
|
|
730
|
+
/**
|
|
731
|
+
* Get the number of listeners for an event.
|
|
732
|
+
*
|
|
733
|
+
* @param event - Event name
|
|
734
|
+
* @returns Number of listeners
|
|
735
|
+
*/
|
|
736
|
+
listenerCount(event: string): number;
|
|
737
|
+
/**
|
|
738
|
+
* Register an event listener with a scope (e.g., plugin name).
|
|
739
|
+
* Scoped listeners can be removed all at once using removeScope().
|
|
740
|
+
*
|
|
741
|
+
* @param scope - Scope identifier (e.g., plugin name)
|
|
742
|
+
* @param event - Event name to listen for
|
|
743
|
+
* @param handler - Function to call when event is emitted
|
|
744
|
+
*/
|
|
745
|
+
onScoped(scope: string, event: string, handler: EventHandler): void;
|
|
746
|
+
/**
|
|
747
|
+
* Remove all listeners registered under a specific scope.
|
|
748
|
+
* Useful for cleanup when a plugin is unregistered.
|
|
749
|
+
*
|
|
750
|
+
* @param scope - Scope identifier to remove
|
|
751
|
+
*/
|
|
752
|
+
removeScope(scope: string): void;
|
|
753
|
+
/**
|
|
754
|
+
* Check if a scope has any registered listeners.
|
|
755
|
+
*
|
|
756
|
+
* @param scope - Scope identifier
|
|
757
|
+
* @returns True if scope has listeners
|
|
758
|
+
*/
|
|
759
|
+
hasScope(scope: string): boolean;
|
|
760
|
+
}
|
|
761
|
+
/**
|
|
762
|
+
* Scoped event bus wrapper for plugins.
|
|
763
|
+
* Automatically tracks all listeners under a scope for easy cleanup.
|
|
764
|
+
*/
|
|
765
|
+
declare class ScopedEventBus {
|
|
766
|
+
private readonly eventBus;
|
|
767
|
+
private readonly scope;
|
|
768
|
+
constructor(eventBus: EventBus, scope: string);
|
|
769
|
+
/**
|
|
770
|
+
* Register an event listener (automatically scoped).
|
|
771
|
+
*/
|
|
772
|
+
on(event: string, handler: EventHandler): void;
|
|
773
|
+
/**
|
|
774
|
+
* Unregister an event listener.
|
|
775
|
+
*/
|
|
776
|
+
off(event: string, handler: EventHandler): void;
|
|
777
|
+
/**
|
|
778
|
+
* Emit an event.
|
|
779
|
+
*/
|
|
780
|
+
emit(event: string, ...args: any[]): void;
|
|
781
|
+
/**
|
|
782
|
+
* Remove all listeners registered by this scoped bus.
|
|
783
|
+
*/
|
|
784
|
+
destroy(): void;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
/**
|
|
788
|
+
* Plugin lifecycle states.
|
|
789
|
+
*/
|
|
790
|
+
type PluginState = 'registered' | 'initializing' | 'active' | 'error' | 'destroyed';
|
|
791
|
+
/**
|
|
792
|
+
* Kernel initialization state.
|
|
793
|
+
*/
|
|
794
|
+
type KernelState = 'idle' | 'initializing' | 'initialized' | 'destroyed';
|
|
795
|
+
/**
|
|
796
|
+
* Options for kernel initialization.
|
|
797
|
+
*/
|
|
798
|
+
interface KernelOptions<TContext> {
|
|
799
|
+
/**
|
|
800
|
+
* Initial context value. If not provided, an empty object is used.
|
|
801
|
+
*/
|
|
802
|
+
context?: TContext;
|
|
803
|
+
}
|
|
693
804
|
/**
|
|
694
805
|
* Micro-kernel for plugin system.
|
|
695
806
|
*
|
|
696
807
|
* Manages plugin registration, initialization, and lifecycle.
|
|
697
808
|
* Provides event bus for inter-plugin communication.
|
|
809
|
+
*
|
|
810
|
+
* Features:
|
|
811
|
+
* - Plugin state tracking (registered, initializing, active, error, destroyed)
|
|
812
|
+
* - Automatic scoped event listeners (cleaned up on unregister)
|
|
813
|
+
* - Topological sorting for dependency resolution
|
|
814
|
+
* - Circular dependency detection
|
|
815
|
+
* - Partial initialization rollback
|
|
816
|
+
* - Race condition prevention
|
|
817
|
+
* - Late plugin registration support
|
|
698
818
|
*/
|
|
699
819
|
declare class Kernel<TContext = unknown> {
|
|
700
820
|
private plugins;
|
|
701
821
|
private context;
|
|
702
822
|
private eventBus;
|
|
823
|
+
private state;
|
|
824
|
+
private initPromise;
|
|
825
|
+
constructor(options?: KernelOptions<TContext>);
|
|
703
826
|
/**
|
|
704
827
|
* Register a plugin with the kernel.
|
|
705
828
|
*
|
|
829
|
+
* If the kernel is already initialized, the plugin will be
|
|
830
|
+
* automatically initialized after registration.
|
|
831
|
+
*
|
|
706
832
|
* @param plugin - Plugin to register
|
|
707
833
|
* @throws {PluginAlreadyRegisteredError} If plugin name already exists
|
|
708
834
|
* @throws {PluginDependencyMissingError} If plugin dependencies are not met
|
|
@@ -711,17 +837,39 @@ declare class Kernel<TContext = unknown> {
|
|
|
711
837
|
/**
|
|
712
838
|
* Initialize all registered plugins.
|
|
713
839
|
*
|
|
714
|
-
*
|
|
715
|
-
*
|
|
840
|
+
* Plugins are initialized in topological order based on dependencies.
|
|
841
|
+
* If initialization fails, already initialized plugins are rolled back.
|
|
842
|
+
*
|
|
843
|
+
* @throws {CircularDependencyError} If circular dependency detected
|
|
844
|
+
* @throws {PluginInitializationError} If a plugin fails to initialize
|
|
716
845
|
*/
|
|
717
846
|
init(): Promise<void>;
|
|
847
|
+
private doInit;
|
|
848
|
+
/**
|
|
849
|
+
* Initialize a single plugin.
|
|
850
|
+
*/
|
|
851
|
+
private initPlugin;
|
|
852
|
+
/**
|
|
853
|
+
* Destroy a single plugin.
|
|
854
|
+
*/
|
|
855
|
+
private destroyPlugin;
|
|
856
|
+
/**
|
|
857
|
+
* Topological sort of plugins based on dependencies.
|
|
858
|
+
* Detects circular dependencies.
|
|
859
|
+
*
|
|
860
|
+
* @returns Plugins sorted in dependency order
|
|
861
|
+
* @throws {CircularDependencyError} If circular dependency detected
|
|
862
|
+
*/
|
|
863
|
+
private topologicalSort;
|
|
718
864
|
/**
|
|
719
865
|
* Unregister a plugin.
|
|
720
866
|
*
|
|
867
|
+
* Automatically cleans up event listeners registered by this plugin.
|
|
868
|
+
*
|
|
721
869
|
* @param name - Plugin name to unregister
|
|
722
870
|
* @throws {PluginNotFoundError} If plugin doesn't exist
|
|
723
871
|
*/
|
|
724
|
-
unregister(name: string): void
|
|
872
|
+
unregister(name: string): Promise<void>;
|
|
725
873
|
/**
|
|
726
874
|
* Get the shared context object.
|
|
727
875
|
*
|
|
@@ -735,6 +883,13 @@ declare class Kernel<TContext = unknown> {
|
|
|
735
883
|
* @returns Plugin instance or undefined
|
|
736
884
|
*/
|
|
737
885
|
getPlugin(name: string): Plugin<TContext> | undefined;
|
|
886
|
+
/**
|
|
887
|
+
* Get the state of a registered plugin.
|
|
888
|
+
*
|
|
889
|
+
* @param name - Plugin name
|
|
890
|
+
* @returns Plugin state or undefined if not found
|
|
891
|
+
*/
|
|
892
|
+
getPluginState(name: string): PluginState | undefined;
|
|
738
893
|
/**
|
|
739
894
|
* Check if a plugin is registered.
|
|
740
895
|
*
|
|
@@ -748,6 +903,27 @@ declare class Kernel<TContext = unknown> {
|
|
|
748
903
|
* @returns Array of plugin names
|
|
749
904
|
*/
|
|
750
905
|
listPlugins(): string[];
|
|
906
|
+
/**
|
|
907
|
+
* Get the kernel's current state.
|
|
908
|
+
*
|
|
909
|
+
* @returns Current kernel state
|
|
910
|
+
*/
|
|
911
|
+
getState(): KernelState;
|
|
912
|
+
/**
|
|
913
|
+
* Check if kernel is initialized.
|
|
914
|
+
*
|
|
915
|
+
* @returns True if kernel is initialized
|
|
916
|
+
*/
|
|
917
|
+
isInitialized(): boolean;
|
|
918
|
+
/**
|
|
919
|
+
* Get the scoped event bus for a plugin.
|
|
920
|
+
* Listeners registered through this bus are automatically
|
|
921
|
+
* cleaned up when the plugin is unregistered.
|
|
922
|
+
*
|
|
923
|
+
* @param pluginName - Plugin name
|
|
924
|
+
* @returns Scoped event bus or undefined
|
|
925
|
+
*/
|
|
926
|
+
getScopedEventBus(pluginName: string): ScopedEventBus | undefined;
|
|
751
927
|
/**
|
|
752
928
|
* Register an event listener.
|
|
753
929
|
*
|
|
@@ -772,9 +948,85 @@ declare class Kernel<TContext = unknown> {
|
|
|
772
948
|
/**
|
|
773
949
|
* Destroy the kernel and all plugins.
|
|
774
950
|
*
|
|
775
|
-
* Unregisters all plugins and cleans up resources.
|
|
951
|
+
* Unregisters all plugins in reverse dependency order and cleans up resources.
|
|
776
952
|
*/
|
|
777
953
|
destroy(): Promise<void>;
|
|
778
954
|
}
|
|
779
955
|
|
|
780
|
-
|
|
956
|
+
/**
|
|
957
|
+
* Base error class for all @oxog/utils errors.
|
|
958
|
+
*/
|
|
959
|
+
declare class UtilsError extends Error {
|
|
960
|
+
code: string;
|
|
961
|
+
constructor(message: string, code: string);
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* Error thrown when an invalid path is provided to get/set/has functions.
|
|
965
|
+
*/
|
|
966
|
+
declare class InvalidPathError extends UtilsError {
|
|
967
|
+
constructor(path: string);
|
|
968
|
+
}
|
|
969
|
+
/**
|
|
970
|
+
* Error thrown when a circular reference is detected during deep operations.
|
|
971
|
+
*/
|
|
972
|
+
declare class CircularReferenceError extends UtilsError {
|
|
973
|
+
constructor();
|
|
974
|
+
}
|
|
975
|
+
/**
|
|
976
|
+
* Error thrown when an operation times out.
|
|
977
|
+
*/
|
|
978
|
+
declare class TimeoutError extends UtilsError {
|
|
979
|
+
constructor(ms: number);
|
|
980
|
+
}
|
|
981
|
+
/**
|
|
982
|
+
* Error thrown when a plugin is already registered.
|
|
983
|
+
*/
|
|
984
|
+
declare class PluginAlreadyRegisteredError extends UtilsError {
|
|
985
|
+
constructor(name: string);
|
|
986
|
+
}
|
|
987
|
+
/**
|
|
988
|
+
* Error thrown when a requested plugin is not found.
|
|
989
|
+
*/
|
|
990
|
+
declare class PluginNotFoundError extends UtilsError {
|
|
991
|
+
constructor(name: string);
|
|
992
|
+
}
|
|
993
|
+
/**
|
|
994
|
+
* Error thrown when a plugin dependency is missing.
|
|
995
|
+
*/
|
|
996
|
+
declare class PluginDependencyMissingError extends UtilsError {
|
|
997
|
+
constructor(plugin: string, dependency: string);
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* Error thrown when retry attempts are exhausted.
|
|
1001
|
+
*/
|
|
1002
|
+
declare class MaxRetriesExceededError extends UtilsError {
|
|
1003
|
+
constructor(attempts: number);
|
|
1004
|
+
}
|
|
1005
|
+
/**
|
|
1006
|
+
* Error thrown when circular dependency is detected between plugins.
|
|
1007
|
+
*/
|
|
1008
|
+
declare class CircularDependencyError extends UtilsError {
|
|
1009
|
+
constructor(cycle: string[]);
|
|
1010
|
+
}
|
|
1011
|
+
/**
|
|
1012
|
+
* Error thrown when plugin initialization fails.
|
|
1013
|
+
*/
|
|
1014
|
+
declare class PluginInitializationError extends UtilsError {
|
|
1015
|
+
readonly pluginName: string;
|
|
1016
|
+
readonly cause: Error;
|
|
1017
|
+
constructor(pluginName: string, cause: Error);
|
|
1018
|
+
}
|
|
1019
|
+
/**
|
|
1020
|
+
* Error thrown when kernel is already initialized.
|
|
1021
|
+
*/
|
|
1022
|
+
declare class KernelAlreadyInitializedError extends UtilsError {
|
|
1023
|
+
constructor();
|
|
1024
|
+
}
|
|
1025
|
+
/**
|
|
1026
|
+
* Error thrown when kernel is currently initializing (race condition prevention).
|
|
1027
|
+
*/
|
|
1028
|
+
declare class KernelInitializingError extends UtilsError {
|
|
1029
|
+
constructor();
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
export { CircularDependencyError, CircularReferenceError, EventBus, InvalidPathError, Kernel, KernelAlreadyInitializedError, KernelInitializingError, type KernelOptions, MaxRetriesExceededError, Plugin, PluginAlreadyRegisteredError, PluginDependencyMissingError, PluginInitializationError, PluginNotFoundError, type PluginState, ScopedEventBus, TimeoutError, UtilsError, array, camelCase, capitalize, chunk, compact, entries, first, flatten, fromEntries, get, groupBy, has, kebabCase, keyBy, keys, last, merge, object, omit, pascalCase, pick, sample, set, setMut, slugify, snakeCase, string, template, truncate, uniq, uniqBy, values };
|