@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.js
CHANGED
|
@@ -364,6 +364,7 @@ function template(str, data) {
|
|
|
364
364
|
// src/utils/eventBus.ts
|
|
365
365
|
var EventBus = class {
|
|
366
366
|
listeners = /* @__PURE__ */ new Map();
|
|
367
|
+
scopedHandlers = /* @__PURE__ */ new Map();
|
|
367
368
|
/**
|
|
368
369
|
* Register an event listener.
|
|
369
370
|
*
|
|
@@ -430,6 +431,80 @@ var EventBus = class {
|
|
|
430
431
|
listenerCount(event) {
|
|
431
432
|
return this.listeners.get(event)?.size ?? 0;
|
|
432
433
|
}
|
|
434
|
+
/**
|
|
435
|
+
* Register an event listener with a scope (e.g., plugin name).
|
|
436
|
+
* Scoped listeners can be removed all at once using removeScope().
|
|
437
|
+
*
|
|
438
|
+
* @param scope - Scope identifier (e.g., plugin name)
|
|
439
|
+
* @param event - Event name to listen for
|
|
440
|
+
* @param handler - Function to call when event is emitted
|
|
441
|
+
*/
|
|
442
|
+
onScoped(scope, event, handler) {
|
|
443
|
+
this.on(event, handler);
|
|
444
|
+
if (!this.scopedHandlers.has(scope)) {
|
|
445
|
+
this.scopedHandlers.set(scope, /* @__PURE__ */ new Map());
|
|
446
|
+
}
|
|
447
|
+
const scopeMap = this.scopedHandlers.get(scope);
|
|
448
|
+
if (!scopeMap.has(event)) {
|
|
449
|
+
scopeMap.set(event, /* @__PURE__ */ new Set());
|
|
450
|
+
}
|
|
451
|
+
scopeMap.get(event).add(handler);
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Remove all listeners registered under a specific scope.
|
|
455
|
+
* Useful for cleanup when a plugin is unregistered.
|
|
456
|
+
*
|
|
457
|
+
* @param scope - Scope identifier to remove
|
|
458
|
+
*/
|
|
459
|
+
removeScope(scope) {
|
|
460
|
+
const scopeMap = this.scopedHandlers.get(scope);
|
|
461
|
+
if (!scopeMap) return;
|
|
462
|
+
for (const [event, handlers] of scopeMap) {
|
|
463
|
+
for (const handler of handlers) {
|
|
464
|
+
this.off(event, handler);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
this.scopedHandlers.delete(scope);
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Check if a scope has any registered listeners.
|
|
471
|
+
*
|
|
472
|
+
* @param scope - Scope identifier
|
|
473
|
+
* @returns True if scope has listeners
|
|
474
|
+
*/
|
|
475
|
+
hasScope(scope) {
|
|
476
|
+
return this.scopedHandlers.has(scope);
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
var ScopedEventBus = class {
|
|
480
|
+
constructor(eventBus, scope) {
|
|
481
|
+
this.eventBus = eventBus;
|
|
482
|
+
this.scope = scope;
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Register an event listener (automatically scoped).
|
|
486
|
+
*/
|
|
487
|
+
on(event, handler) {
|
|
488
|
+
this.eventBus.onScoped(this.scope, event, handler);
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Unregister an event listener.
|
|
492
|
+
*/
|
|
493
|
+
off(event, handler) {
|
|
494
|
+
this.eventBus.off(event, handler);
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Emit an event.
|
|
498
|
+
*/
|
|
499
|
+
emit(event, ...args) {
|
|
500
|
+
this.eventBus.emit(event, ...args);
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Remove all listeners registered by this scoped bus.
|
|
504
|
+
*/
|
|
505
|
+
destroy() {
|
|
506
|
+
this.eventBus.removeScope(this.scope);
|
|
507
|
+
}
|
|
433
508
|
};
|
|
434
509
|
|
|
435
510
|
// src/errors.ts
|
|
@@ -442,6 +517,24 @@ var UtilsError = class extends Error {
|
|
|
442
517
|
Error.captureStackTrace(this, this.constructor);
|
|
443
518
|
}
|
|
444
519
|
};
|
|
520
|
+
var InvalidPathError = class extends UtilsError {
|
|
521
|
+
constructor(path) {
|
|
522
|
+
super(`Invalid path: ${path}`, "INVALID_PATH");
|
|
523
|
+
this.name = "InvalidPathError";
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
var CircularReferenceError = class extends UtilsError {
|
|
527
|
+
constructor() {
|
|
528
|
+
super("Circular reference detected", "CIRCULAR_REF");
|
|
529
|
+
this.name = "CircularReferenceError";
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
var TimeoutError = class extends UtilsError {
|
|
533
|
+
constructor(ms) {
|
|
534
|
+
super(`Operation timed out after ${ms}ms`, "TIMEOUT");
|
|
535
|
+
this.name = "TimeoutError";
|
|
536
|
+
}
|
|
537
|
+
};
|
|
445
538
|
var PluginAlreadyRegisteredError = class extends UtilsError {
|
|
446
539
|
constructor(name) {
|
|
447
540
|
super(`Plugin "${name}" already registered`, "PLUGIN_ALREADY_REGISTERED");
|
|
@@ -460,15 +553,57 @@ var PluginDependencyMissingError = class extends UtilsError {
|
|
|
460
553
|
this.name = "PluginDependencyMissingError";
|
|
461
554
|
}
|
|
462
555
|
};
|
|
556
|
+
var MaxRetriesExceededError = class extends UtilsError {
|
|
557
|
+
constructor(attempts) {
|
|
558
|
+
super(`Maximum retry attempts (${attempts}) exceeded`, "MAX_RETRIES_EXCEEDED");
|
|
559
|
+
this.name = "MaxRetriesExceededError";
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
var CircularDependencyError = class extends UtilsError {
|
|
563
|
+
constructor(cycle) {
|
|
564
|
+
super(`Circular dependency detected: ${cycle.join(" -> ")}`, "CIRCULAR_DEPENDENCY");
|
|
565
|
+
this.name = "CircularDependencyError";
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
var PluginInitializationError = class extends UtilsError {
|
|
569
|
+
pluginName;
|
|
570
|
+
cause;
|
|
571
|
+
constructor(pluginName, cause) {
|
|
572
|
+
super(`Plugin "${pluginName}" failed to initialize: ${cause.message}`, "PLUGIN_INIT_FAILED");
|
|
573
|
+
this.name = "PluginInitializationError";
|
|
574
|
+
this.pluginName = pluginName;
|
|
575
|
+
this.cause = cause;
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
var KernelAlreadyInitializedError = class extends UtilsError {
|
|
579
|
+
constructor() {
|
|
580
|
+
super("Kernel is already initialized", "KERNEL_ALREADY_INITIALIZED");
|
|
581
|
+
this.name = "KernelAlreadyInitializedError";
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
var KernelInitializingError = class extends UtilsError {
|
|
585
|
+
constructor() {
|
|
586
|
+
super("Kernel is currently initializing", "KERNEL_INITIALIZING");
|
|
587
|
+
this.name = "KernelInitializingError";
|
|
588
|
+
}
|
|
589
|
+
};
|
|
463
590
|
|
|
464
591
|
// src/kernel.ts
|
|
465
592
|
var Kernel = class {
|
|
466
593
|
plugins = /* @__PURE__ */ new Map();
|
|
467
|
-
context
|
|
594
|
+
context;
|
|
468
595
|
eventBus = new EventBus();
|
|
596
|
+
state = "idle";
|
|
597
|
+
initPromise = null;
|
|
598
|
+
constructor(options) {
|
|
599
|
+
this.context = options?.context ?? {};
|
|
600
|
+
}
|
|
469
601
|
/**
|
|
470
602
|
* Register a plugin with the kernel.
|
|
471
603
|
*
|
|
604
|
+
* If the kernel is already initialized, the plugin will be
|
|
605
|
+
* automatically initialized after registration.
|
|
606
|
+
*
|
|
472
607
|
* @param plugin - Plugin to register
|
|
473
608
|
* @throws {PluginAlreadyRegisteredError} If plugin name already exists
|
|
474
609
|
* @throws {PluginDependencyMissingError} If plugin dependencies are not met
|
|
@@ -484,51 +619,177 @@ var Kernel = class {
|
|
|
484
619
|
}
|
|
485
620
|
}
|
|
486
621
|
}
|
|
487
|
-
this.
|
|
622
|
+
const scopedBus = new ScopedEventBus(this.eventBus, plugin.name);
|
|
623
|
+
const entry = {
|
|
624
|
+
plugin,
|
|
625
|
+
state: "registered",
|
|
626
|
+
scopedBus
|
|
627
|
+
};
|
|
628
|
+
this.plugins.set(plugin.name, entry);
|
|
488
629
|
try {
|
|
489
630
|
plugin.install(this);
|
|
490
631
|
} catch (error) {
|
|
491
632
|
this.plugins.delete(plugin.name);
|
|
633
|
+
scopedBus.destroy();
|
|
492
634
|
throw error;
|
|
493
635
|
}
|
|
494
636
|
this.eventBus.emit("plugin:registered", { name: plugin.name, version: plugin.version });
|
|
637
|
+
if (this.state === "initialized") {
|
|
638
|
+
this.initPlugin(entry).catch((error) => {
|
|
639
|
+
entry.state = "error";
|
|
640
|
+
this.eventBus.emit("plugin:error", { name: plugin.name, error });
|
|
641
|
+
});
|
|
642
|
+
}
|
|
495
643
|
}
|
|
496
644
|
/**
|
|
497
645
|
* Initialize all registered plugins.
|
|
498
646
|
*
|
|
499
|
-
*
|
|
500
|
-
*
|
|
647
|
+
* Plugins are initialized in topological order based on dependencies.
|
|
648
|
+
* If initialization fails, already initialized plugins are rolled back.
|
|
649
|
+
*
|
|
650
|
+
* @throws {CircularDependencyError} If circular dependency detected
|
|
651
|
+
* @throws {PluginInitializationError} If a plugin fails to initialize
|
|
501
652
|
*/
|
|
502
653
|
async init() {
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
654
|
+
if (this.state === "initialized") {
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
if (this.state === "initializing") {
|
|
658
|
+
return this.initPromise;
|
|
659
|
+
}
|
|
660
|
+
if (this.state === "destroyed") {
|
|
661
|
+
throw new Error("Cannot initialize destroyed kernel");
|
|
662
|
+
}
|
|
663
|
+
this.state = "initializing";
|
|
664
|
+
this.initPromise = this.doInit();
|
|
665
|
+
try {
|
|
666
|
+
await this.initPromise;
|
|
667
|
+
} finally {
|
|
668
|
+
this.initPromise = null;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
async doInit() {
|
|
672
|
+
const sortedEntries = this.topologicalSort();
|
|
673
|
+
const initializedStack = [];
|
|
674
|
+
for (const entry of sortedEntries) {
|
|
675
|
+
try {
|
|
676
|
+
await this.initPlugin(entry);
|
|
677
|
+
initializedStack.push(entry);
|
|
678
|
+
} catch (error) {
|
|
679
|
+
entry.state = "error";
|
|
680
|
+
for (const initialized of initializedStack.reverse()) {
|
|
681
|
+
try {
|
|
682
|
+
await this.destroyPlugin(initialized);
|
|
683
|
+
} catch (destroyError) {
|
|
684
|
+
console.error(
|
|
685
|
+
`Error during rollback of plugin "${initialized.plugin.name}":`,
|
|
686
|
+
destroyError
|
|
687
|
+
);
|
|
510
688
|
}
|
|
511
|
-
throw error;
|
|
512
689
|
}
|
|
690
|
+
this.state = "idle";
|
|
691
|
+
throw new PluginInitializationError(
|
|
692
|
+
entry.plugin.name,
|
|
693
|
+
error instanceof Error ? error : new Error(String(error))
|
|
694
|
+
);
|
|
513
695
|
}
|
|
514
696
|
}
|
|
697
|
+
this.state = "initialized";
|
|
515
698
|
this.eventBus.emit("kernel:initialized");
|
|
516
699
|
}
|
|
700
|
+
/**
|
|
701
|
+
* Initialize a single plugin.
|
|
702
|
+
*/
|
|
703
|
+
async initPlugin(entry) {
|
|
704
|
+
if (entry.state !== "registered") {
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
entry.state = "initializing";
|
|
708
|
+
this.eventBus.emit("plugin:initializing", { name: entry.plugin.name });
|
|
709
|
+
if (entry.plugin.onInit) {
|
|
710
|
+
try {
|
|
711
|
+
await entry.plugin.onInit(this.context);
|
|
712
|
+
} catch (error) {
|
|
713
|
+
if (entry.plugin.onError) {
|
|
714
|
+
entry.plugin.onError(error);
|
|
715
|
+
}
|
|
716
|
+
throw error;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
entry.state = "active";
|
|
720
|
+
this.eventBus.emit("plugin:initialized", { name: entry.plugin.name });
|
|
721
|
+
}
|
|
722
|
+
/**
|
|
723
|
+
* Destroy a single plugin.
|
|
724
|
+
*/
|
|
725
|
+
async destroyPlugin(entry) {
|
|
726
|
+
if (entry.state === "destroyed") {
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
entry.scopedBus.destroy();
|
|
730
|
+
if (entry.plugin.onDestroy) {
|
|
731
|
+
await entry.plugin.onDestroy();
|
|
732
|
+
}
|
|
733
|
+
entry.state = "destroyed";
|
|
734
|
+
}
|
|
735
|
+
/**
|
|
736
|
+
* Topological sort of plugins based on dependencies.
|
|
737
|
+
* Detects circular dependencies.
|
|
738
|
+
*
|
|
739
|
+
* @returns Plugins sorted in dependency order
|
|
740
|
+
* @throws {CircularDependencyError} If circular dependency detected
|
|
741
|
+
*/
|
|
742
|
+
topologicalSort() {
|
|
743
|
+
const result = [];
|
|
744
|
+
const visited = /* @__PURE__ */ new Set();
|
|
745
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
746
|
+
const visit = (name, path) => {
|
|
747
|
+
if (visited.has(name)) return;
|
|
748
|
+
if (visiting.has(name)) {
|
|
749
|
+
const cycleStart = path.indexOf(name);
|
|
750
|
+
const cycle = [...path.slice(cycleStart), name];
|
|
751
|
+
throw new CircularDependencyError(cycle);
|
|
752
|
+
}
|
|
753
|
+
const entry = this.plugins.get(name);
|
|
754
|
+
if (!entry) return;
|
|
755
|
+
visiting.add(name);
|
|
756
|
+
const currentPath = [...path, name];
|
|
757
|
+
if (entry.plugin.dependencies) {
|
|
758
|
+
for (const dep of entry.plugin.dependencies) {
|
|
759
|
+
visit(dep, currentPath);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
visiting.delete(name);
|
|
763
|
+
visited.add(name);
|
|
764
|
+
result.push(entry);
|
|
765
|
+
};
|
|
766
|
+
for (const name of this.plugins.keys()) {
|
|
767
|
+
visit(name, []);
|
|
768
|
+
}
|
|
769
|
+
return result;
|
|
770
|
+
}
|
|
517
771
|
/**
|
|
518
772
|
* Unregister a plugin.
|
|
519
773
|
*
|
|
774
|
+
* Automatically cleans up event listeners registered by this plugin.
|
|
775
|
+
*
|
|
520
776
|
* @param name - Plugin name to unregister
|
|
521
777
|
* @throws {PluginNotFoundError} If plugin doesn't exist
|
|
522
778
|
*/
|
|
523
|
-
unregister(name) {
|
|
524
|
-
const
|
|
525
|
-
if (!
|
|
779
|
+
async unregister(name) {
|
|
780
|
+
const entry = this.plugins.get(name);
|
|
781
|
+
if (!entry) {
|
|
526
782
|
throw new PluginNotFoundError(name);
|
|
527
783
|
}
|
|
528
|
-
this.plugins
|
|
529
|
-
|
|
530
|
-
|
|
784
|
+
for (const [otherName, otherEntry] of this.plugins) {
|
|
785
|
+
if (otherName !== name && otherEntry.plugin.dependencies?.includes(name)) {
|
|
786
|
+
throw new Error(
|
|
787
|
+
`Cannot unregister plugin "${name}": plugin "${otherName}" depends on it`
|
|
788
|
+
);
|
|
789
|
+
}
|
|
531
790
|
}
|
|
791
|
+
await this.destroyPlugin(entry);
|
|
792
|
+
this.plugins.delete(name);
|
|
532
793
|
this.eventBus.emit("plugin:unregistered", { name });
|
|
533
794
|
}
|
|
534
795
|
/**
|
|
@@ -546,7 +807,16 @@ var Kernel = class {
|
|
|
546
807
|
* @returns Plugin instance or undefined
|
|
547
808
|
*/
|
|
548
809
|
getPlugin(name) {
|
|
549
|
-
return this.plugins.get(name);
|
|
810
|
+
return this.plugins.get(name)?.plugin;
|
|
811
|
+
}
|
|
812
|
+
/**
|
|
813
|
+
* Get the state of a registered plugin.
|
|
814
|
+
*
|
|
815
|
+
* @param name - Plugin name
|
|
816
|
+
* @returns Plugin state or undefined if not found
|
|
817
|
+
*/
|
|
818
|
+
getPluginState(name) {
|
|
819
|
+
return this.plugins.get(name)?.state;
|
|
550
820
|
}
|
|
551
821
|
/**
|
|
552
822
|
* Check if a plugin is registered.
|
|
@@ -565,6 +835,33 @@ var Kernel = class {
|
|
|
565
835
|
listPlugins() {
|
|
566
836
|
return Array.from(this.plugins.keys());
|
|
567
837
|
}
|
|
838
|
+
/**
|
|
839
|
+
* Get the kernel's current state.
|
|
840
|
+
*
|
|
841
|
+
* @returns Current kernel state
|
|
842
|
+
*/
|
|
843
|
+
getState() {
|
|
844
|
+
return this.state;
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* Check if kernel is initialized.
|
|
848
|
+
*
|
|
849
|
+
* @returns True if kernel is initialized
|
|
850
|
+
*/
|
|
851
|
+
isInitialized() {
|
|
852
|
+
return this.state === "initialized";
|
|
853
|
+
}
|
|
854
|
+
/**
|
|
855
|
+
* Get the scoped event bus for a plugin.
|
|
856
|
+
* Listeners registered through this bus are automatically
|
|
857
|
+
* cleaned up when the plugin is unregistered.
|
|
858
|
+
*
|
|
859
|
+
* @param pluginName - Plugin name
|
|
860
|
+
* @returns Scoped event bus or undefined
|
|
861
|
+
*/
|
|
862
|
+
getScopedEventBus(pluginName) {
|
|
863
|
+
return this.plugins.get(pluginName)?.scopedBus;
|
|
864
|
+
}
|
|
568
865
|
/**
|
|
569
866
|
* Register an event listener.
|
|
570
867
|
*
|
|
@@ -595,17 +892,27 @@ var Kernel = class {
|
|
|
595
892
|
/**
|
|
596
893
|
* Destroy the kernel and all plugins.
|
|
597
894
|
*
|
|
598
|
-
* Unregisters all plugins and cleans up resources.
|
|
895
|
+
* Unregisters all plugins in reverse dependency order and cleans up resources.
|
|
599
896
|
*/
|
|
600
897
|
async destroy() {
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
898
|
+
if (this.state === "destroyed") {
|
|
899
|
+
return;
|
|
900
|
+
}
|
|
901
|
+
const sortedEntries = this.topologicalSort().reverse();
|
|
902
|
+
for (const entry of sortedEntries) {
|
|
903
|
+
try {
|
|
904
|
+
await this.destroyPlugin(entry);
|
|
905
|
+
} catch (error) {
|
|
906
|
+
console.error(`Error destroying plugin "${entry.plugin.name}":`, error);
|
|
907
|
+
}
|
|
604
908
|
}
|
|
909
|
+
this.plugins.clear();
|
|
605
910
|
this.eventBus.removeAllListeners();
|
|
911
|
+
this.state = "destroyed";
|
|
912
|
+
this.eventBus.emit("kernel:destroyed");
|
|
606
913
|
}
|
|
607
914
|
};
|
|
608
915
|
|
|
609
|
-
export { Kernel, array_exports as array, camelCase, capitalize, chunk, compact, entries, first, flatten, fromEntries, get, groupBy, has, kebabCase, keyBy, keys, last, merge, object_exports as object, omit, pascalCase, pick, sample, set, setMut, slugify, snakeCase, string_exports as string, template, truncate, uniq, uniqBy, values };
|
|
916
|
+
export { CircularDependencyError, CircularReferenceError, EventBus, InvalidPathError, Kernel, KernelAlreadyInitializedError, KernelInitializingError, MaxRetriesExceededError, PluginAlreadyRegisteredError, PluginDependencyMissingError, PluginInitializationError, PluginNotFoundError, ScopedEventBus, TimeoutError, UtilsError, array_exports as array, camelCase, capitalize, chunk, compact, entries, first, flatten, fromEntries, get, groupBy, has, kebabCase, keyBy, keys, last, merge, object_exports as object, omit, pascalCase, pick, sample, set, setMut, slugify, snakeCase, string_exports as string, template, truncate, uniq, uniqBy, values };
|
|
610
917
|
//# sourceMappingURL=index.js.map
|
|
611
918
|
//# sourceMappingURL=index.js.map
|