conduithub 0.0.1 → 0.0.2
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/core/event-bus/index.cjs +126 -0
- package/dist/core/event-bus/index.d.cts +42 -0
- package/dist/core/event-bus/index.d.mts +42 -0
- package/dist/core/event-bus/index.d.ts +42 -0
- package/dist/core/event-bus/index.mjs +124 -0
- package/dist/core/hook/index.cjs +210 -0
- package/dist/core/hook/index.d.cts +40 -0
- package/dist/core/hook/index.d.mts +40 -0
- package/dist/core/hook/index.d.ts +40 -0
- package/dist/core/hook/index.mjs +208 -0
- package/dist/core/index.cjs +11 -0
- package/dist/core/index.d.cts +3 -0
- package/dist/core/index.d.mts +3 -0
- package/dist/core/index.d.ts +3 -0
- package/dist/core/index.mjs +4 -0
- package/dist/index.cjs +11 -5
- package/dist/index.d.cts +4 -1
- package/dist/index.d.mts +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.mjs +4 -1
- package/dist/shared/conduithub.74V0wiLi.mjs +62 -0
- package/dist/shared/conduithub.B7aryjPG.d.cts +18 -0
- package/dist/shared/conduithub.B7aryjPG.d.mts +18 -0
- package/dist/shared/conduithub.B7aryjPG.d.ts +18 -0
- package/dist/shared/conduithub.CvMLTa-R.cjs +68 -0
- package/dist/utils/index.cjs +9 -55
- package/dist/utils/index.d.cts +4 -14
- package/dist/utils/index.d.mts +4 -14
- package/dist/utils/index.d.ts +4 -14
- package/dist/utils/index.mjs +2 -53
- package/package.json +41 -2
@@ -0,0 +1,126 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
const uuid = require('../../shared/conduithub.CvMLTa-R.cjs');
|
4
|
+
require('uuid');
|
5
|
+
|
6
|
+
class EventBus {
|
7
|
+
handlers = {};
|
8
|
+
initialized = false;
|
9
|
+
logger;
|
10
|
+
constructor(options) {
|
11
|
+
this.logger = uuid.createLogger(options?.logger);
|
12
|
+
}
|
13
|
+
ensureInitialized() {
|
14
|
+
if (!this.initialized) throw new Error("EventBus is not initialized");
|
15
|
+
}
|
16
|
+
async initialize() {
|
17
|
+
if (this.initialized) return;
|
18
|
+
this.initialized = true;
|
19
|
+
this.logger.success("EventBus system initialized successfully");
|
20
|
+
}
|
21
|
+
async shutdown() {
|
22
|
+
this.ensureInitialized();
|
23
|
+
this.handlers = {};
|
24
|
+
this.initialized = false;
|
25
|
+
this.logger.success("EventBus system shutdown completed");
|
26
|
+
}
|
27
|
+
on(type, handler) {
|
28
|
+
this.ensureInitialized();
|
29
|
+
const id = uuid.generateUuid();
|
30
|
+
const record = {
|
31
|
+
id,
|
32
|
+
handler,
|
33
|
+
once: false
|
34
|
+
};
|
35
|
+
(this.handlers[type] ??= []).push(record);
|
36
|
+
this.logger.debug(`Event handler registered: '${String(type)}' \u2192 ${id}`);
|
37
|
+
return id;
|
38
|
+
}
|
39
|
+
once(type, handler) {
|
40
|
+
this.ensureInitialized();
|
41
|
+
const id = uuid.generateUuid();
|
42
|
+
const record = {
|
43
|
+
id,
|
44
|
+
handler,
|
45
|
+
once: true
|
46
|
+
};
|
47
|
+
(this.handlers[type] ??= []).push(record);
|
48
|
+
this.logger.debug(
|
49
|
+
`One-time event handler registered: '${String(type)}' \u2192 ${id}`
|
50
|
+
);
|
51
|
+
return id;
|
52
|
+
}
|
53
|
+
off(type, id) {
|
54
|
+
this.ensureInitialized();
|
55
|
+
const list = this.handlers[type];
|
56
|
+
if (!list) return;
|
57
|
+
if (!id) {
|
58
|
+
delete this.handlers[type];
|
59
|
+
this.logger.debug(`All event handlers removed for: '${String(type)}'`);
|
60
|
+
return;
|
61
|
+
}
|
62
|
+
const index = list.findIndex((h) => h.id === id);
|
63
|
+
if (index !== -1) {
|
64
|
+
list.splice(index, 1);
|
65
|
+
this.logger.debug(`Event handler removed: '${String(type)}' \u2192 ${id}`);
|
66
|
+
if (list.length === 0) delete this.handlers[type];
|
67
|
+
}
|
68
|
+
}
|
69
|
+
async emit(type, payload, source = "event-bus") {
|
70
|
+
this.ensureInitialized();
|
71
|
+
const list = this.handlers[type];
|
72
|
+
if (!list?.length) return;
|
73
|
+
const event = {
|
74
|
+
type,
|
75
|
+
payload,
|
76
|
+
timestamp: Date.now(),
|
77
|
+
source
|
78
|
+
};
|
79
|
+
for (const { id, handler, once } of [...list]) {
|
80
|
+
try {
|
81
|
+
await handler(event);
|
82
|
+
if (once) this.off(type, id);
|
83
|
+
} catch (err) {
|
84
|
+
this.logger.error(
|
85
|
+
`Event handler execution failed: '${String(type)}' \u2192 ${id}`,
|
86
|
+
err
|
87
|
+
);
|
88
|
+
}
|
89
|
+
}
|
90
|
+
}
|
91
|
+
removeAllListeners(type) {
|
92
|
+
this.ensureInitialized();
|
93
|
+
if (type) {
|
94
|
+
delete this.handlers[type];
|
95
|
+
this.logger.debug(
|
96
|
+
`All listeners removed for event type: '${String(type)}'`
|
97
|
+
);
|
98
|
+
} else {
|
99
|
+
this.handlers = {};
|
100
|
+
this.logger.debug("All event listeners cleared from EventBus");
|
101
|
+
}
|
102
|
+
}
|
103
|
+
listenerCount(type) {
|
104
|
+
return this.handlers[type]?.length ?? 0;
|
105
|
+
}
|
106
|
+
eventNames() {
|
107
|
+
return Object.keys(this.handlers);
|
108
|
+
}
|
109
|
+
getStats() {
|
110
|
+
const names = this.eventNames();
|
111
|
+
const countMap = Object.fromEntries(
|
112
|
+
names.map((name) => [name, this.listenerCount(name)])
|
113
|
+
);
|
114
|
+
return {
|
115
|
+
totalEvents: names.length,
|
116
|
+
totalListeners: Object.values(countMap).reduce((a, b) => a + b, 0),
|
117
|
+
eventNames: names,
|
118
|
+
listenerCount: countMap
|
119
|
+
};
|
120
|
+
}
|
121
|
+
get isInitialized() {
|
122
|
+
return this.initialized;
|
123
|
+
}
|
124
|
+
}
|
125
|
+
|
126
|
+
exports.EventBus = EventBus;
|
@@ -0,0 +1,42 @@
|
|
1
|
+
import { a as Logger } from '../../shared/conduithub.B7aryjPG.cjs';
|
2
|
+
|
3
|
+
interface EventData<K extends string, P> {
|
4
|
+
type: K;
|
5
|
+
payload: P;
|
6
|
+
timestamp: number;
|
7
|
+
source: string;
|
8
|
+
}
|
9
|
+
interface EventHandlerRecord<K extends string, P> {
|
10
|
+
id: string;
|
11
|
+
once: boolean;
|
12
|
+
handler: (event: EventData<K, P>) => Promise<void> | void;
|
13
|
+
}
|
14
|
+
|
15
|
+
declare class EventBus<EM extends Record<string, unknown>> {
|
16
|
+
private handlers;
|
17
|
+
private initialized;
|
18
|
+
private logger;
|
19
|
+
constructor(options?: {
|
20
|
+
logger?: Logger;
|
21
|
+
});
|
22
|
+
private ensureInitialized;
|
23
|
+
initialize(): Promise<void>;
|
24
|
+
shutdown(): Promise<void>;
|
25
|
+
on<K extends keyof EM>(type: K, handler: (event: EventData<K & string, EM[K]>) => Promise<void> | void): string;
|
26
|
+
once<K extends keyof EM>(type: K, handler: (event: EventData<K & string, EM[K]>) => Promise<void> | void): string;
|
27
|
+
off<K extends keyof EM>(type: K, id?: string): void;
|
28
|
+
emit<K extends keyof EM>(type: K, payload: EM[K], source?: string): Promise<void>;
|
29
|
+
removeAllListeners<K extends keyof EM>(type?: K): void;
|
30
|
+
listenerCount<K extends keyof EM>(type: K): number;
|
31
|
+
eventNames(): Array<keyof EM>;
|
32
|
+
getStats(): {
|
33
|
+
totalEvents: number;
|
34
|
+
totalListeners: number;
|
35
|
+
eventNames: (keyof EM)[];
|
36
|
+
listenerCount: Record<keyof EM, number>;
|
37
|
+
};
|
38
|
+
get isInitialized(): boolean;
|
39
|
+
}
|
40
|
+
|
41
|
+
export { EventBus };
|
42
|
+
export type { EventData, EventHandlerRecord };
|
@@ -0,0 +1,42 @@
|
|
1
|
+
import { a as Logger } from '../../shared/conduithub.B7aryjPG.mjs';
|
2
|
+
|
3
|
+
interface EventData<K extends string, P> {
|
4
|
+
type: K;
|
5
|
+
payload: P;
|
6
|
+
timestamp: number;
|
7
|
+
source: string;
|
8
|
+
}
|
9
|
+
interface EventHandlerRecord<K extends string, P> {
|
10
|
+
id: string;
|
11
|
+
once: boolean;
|
12
|
+
handler: (event: EventData<K, P>) => Promise<void> | void;
|
13
|
+
}
|
14
|
+
|
15
|
+
declare class EventBus<EM extends Record<string, unknown>> {
|
16
|
+
private handlers;
|
17
|
+
private initialized;
|
18
|
+
private logger;
|
19
|
+
constructor(options?: {
|
20
|
+
logger?: Logger;
|
21
|
+
});
|
22
|
+
private ensureInitialized;
|
23
|
+
initialize(): Promise<void>;
|
24
|
+
shutdown(): Promise<void>;
|
25
|
+
on<K extends keyof EM>(type: K, handler: (event: EventData<K & string, EM[K]>) => Promise<void> | void): string;
|
26
|
+
once<K extends keyof EM>(type: K, handler: (event: EventData<K & string, EM[K]>) => Promise<void> | void): string;
|
27
|
+
off<K extends keyof EM>(type: K, id?: string): void;
|
28
|
+
emit<K extends keyof EM>(type: K, payload: EM[K], source?: string): Promise<void>;
|
29
|
+
removeAllListeners<K extends keyof EM>(type?: K): void;
|
30
|
+
listenerCount<K extends keyof EM>(type: K): number;
|
31
|
+
eventNames(): Array<keyof EM>;
|
32
|
+
getStats(): {
|
33
|
+
totalEvents: number;
|
34
|
+
totalListeners: number;
|
35
|
+
eventNames: (keyof EM)[];
|
36
|
+
listenerCount: Record<keyof EM, number>;
|
37
|
+
};
|
38
|
+
get isInitialized(): boolean;
|
39
|
+
}
|
40
|
+
|
41
|
+
export { EventBus };
|
42
|
+
export type { EventData, EventHandlerRecord };
|
@@ -0,0 +1,42 @@
|
|
1
|
+
import { a as Logger } from '../../shared/conduithub.B7aryjPG.js';
|
2
|
+
|
3
|
+
interface EventData<K extends string, P> {
|
4
|
+
type: K;
|
5
|
+
payload: P;
|
6
|
+
timestamp: number;
|
7
|
+
source: string;
|
8
|
+
}
|
9
|
+
interface EventHandlerRecord<K extends string, P> {
|
10
|
+
id: string;
|
11
|
+
once: boolean;
|
12
|
+
handler: (event: EventData<K, P>) => Promise<void> | void;
|
13
|
+
}
|
14
|
+
|
15
|
+
declare class EventBus<EM extends Record<string, unknown>> {
|
16
|
+
private handlers;
|
17
|
+
private initialized;
|
18
|
+
private logger;
|
19
|
+
constructor(options?: {
|
20
|
+
logger?: Logger;
|
21
|
+
});
|
22
|
+
private ensureInitialized;
|
23
|
+
initialize(): Promise<void>;
|
24
|
+
shutdown(): Promise<void>;
|
25
|
+
on<K extends keyof EM>(type: K, handler: (event: EventData<K & string, EM[K]>) => Promise<void> | void): string;
|
26
|
+
once<K extends keyof EM>(type: K, handler: (event: EventData<K & string, EM[K]>) => Promise<void> | void): string;
|
27
|
+
off<K extends keyof EM>(type: K, id?: string): void;
|
28
|
+
emit<K extends keyof EM>(type: K, payload: EM[K], source?: string): Promise<void>;
|
29
|
+
removeAllListeners<K extends keyof EM>(type?: K): void;
|
30
|
+
listenerCount<K extends keyof EM>(type: K): number;
|
31
|
+
eventNames(): Array<keyof EM>;
|
32
|
+
getStats(): {
|
33
|
+
totalEvents: number;
|
34
|
+
totalListeners: number;
|
35
|
+
eventNames: (keyof EM)[];
|
36
|
+
listenerCount: Record<keyof EM, number>;
|
37
|
+
};
|
38
|
+
get isInitialized(): boolean;
|
39
|
+
}
|
40
|
+
|
41
|
+
export { EventBus };
|
42
|
+
export type { EventData, EventHandlerRecord };
|
@@ -0,0 +1,124 @@
|
|
1
|
+
import { c as createLogger, g as generateUuid } from '../../shared/conduithub.74V0wiLi.mjs';
|
2
|
+
import 'uuid';
|
3
|
+
|
4
|
+
class EventBus {
|
5
|
+
handlers = {};
|
6
|
+
initialized = false;
|
7
|
+
logger;
|
8
|
+
constructor(options) {
|
9
|
+
this.logger = createLogger(options?.logger);
|
10
|
+
}
|
11
|
+
ensureInitialized() {
|
12
|
+
if (!this.initialized) throw new Error("EventBus is not initialized");
|
13
|
+
}
|
14
|
+
async initialize() {
|
15
|
+
if (this.initialized) return;
|
16
|
+
this.initialized = true;
|
17
|
+
this.logger.success("EventBus system initialized successfully");
|
18
|
+
}
|
19
|
+
async shutdown() {
|
20
|
+
this.ensureInitialized();
|
21
|
+
this.handlers = {};
|
22
|
+
this.initialized = false;
|
23
|
+
this.logger.success("EventBus system shutdown completed");
|
24
|
+
}
|
25
|
+
on(type, handler) {
|
26
|
+
this.ensureInitialized();
|
27
|
+
const id = generateUuid();
|
28
|
+
const record = {
|
29
|
+
id,
|
30
|
+
handler,
|
31
|
+
once: false
|
32
|
+
};
|
33
|
+
(this.handlers[type] ??= []).push(record);
|
34
|
+
this.logger.debug(`Event handler registered: '${String(type)}' \u2192 ${id}`);
|
35
|
+
return id;
|
36
|
+
}
|
37
|
+
once(type, handler) {
|
38
|
+
this.ensureInitialized();
|
39
|
+
const id = generateUuid();
|
40
|
+
const record = {
|
41
|
+
id,
|
42
|
+
handler,
|
43
|
+
once: true
|
44
|
+
};
|
45
|
+
(this.handlers[type] ??= []).push(record);
|
46
|
+
this.logger.debug(
|
47
|
+
`One-time event handler registered: '${String(type)}' \u2192 ${id}`
|
48
|
+
);
|
49
|
+
return id;
|
50
|
+
}
|
51
|
+
off(type, id) {
|
52
|
+
this.ensureInitialized();
|
53
|
+
const list = this.handlers[type];
|
54
|
+
if (!list) return;
|
55
|
+
if (!id) {
|
56
|
+
delete this.handlers[type];
|
57
|
+
this.logger.debug(`All event handlers removed for: '${String(type)}'`);
|
58
|
+
return;
|
59
|
+
}
|
60
|
+
const index = list.findIndex((h) => h.id === id);
|
61
|
+
if (index !== -1) {
|
62
|
+
list.splice(index, 1);
|
63
|
+
this.logger.debug(`Event handler removed: '${String(type)}' \u2192 ${id}`);
|
64
|
+
if (list.length === 0) delete this.handlers[type];
|
65
|
+
}
|
66
|
+
}
|
67
|
+
async emit(type, payload, source = "event-bus") {
|
68
|
+
this.ensureInitialized();
|
69
|
+
const list = this.handlers[type];
|
70
|
+
if (!list?.length) return;
|
71
|
+
const event = {
|
72
|
+
type,
|
73
|
+
payload,
|
74
|
+
timestamp: Date.now(),
|
75
|
+
source
|
76
|
+
};
|
77
|
+
for (const { id, handler, once } of [...list]) {
|
78
|
+
try {
|
79
|
+
await handler(event);
|
80
|
+
if (once) this.off(type, id);
|
81
|
+
} catch (err) {
|
82
|
+
this.logger.error(
|
83
|
+
`Event handler execution failed: '${String(type)}' \u2192 ${id}`,
|
84
|
+
err
|
85
|
+
);
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|
89
|
+
removeAllListeners(type) {
|
90
|
+
this.ensureInitialized();
|
91
|
+
if (type) {
|
92
|
+
delete this.handlers[type];
|
93
|
+
this.logger.debug(
|
94
|
+
`All listeners removed for event type: '${String(type)}'`
|
95
|
+
);
|
96
|
+
} else {
|
97
|
+
this.handlers = {};
|
98
|
+
this.logger.debug("All event listeners cleared from EventBus");
|
99
|
+
}
|
100
|
+
}
|
101
|
+
listenerCount(type) {
|
102
|
+
return this.handlers[type]?.length ?? 0;
|
103
|
+
}
|
104
|
+
eventNames() {
|
105
|
+
return Object.keys(this.handlers);
|
106
|
+
}
|
107
|
+
getStats() {
|
108
|
+
const names = this.eventNames();
|
109
|
+
const countMap = Object.fromEntries(
|
110
|
+
names.map((name) => [name, this.listenerCount(name)])
|
111
|
+
);
|
112
|
+
return {
|
113
|
+
totalEvents: names.length,
|
114
|
+
totalListeners: Object.values(countMap).reduce((a, b) => a + b, 0),
|
115
|
+
eventNames: names,
|
116
|
+
listenerCount: countMap
|
117
|
+
};
|
118
|
+
}
|
119
|
+
get isInitialized() {
|
120
|
+
return this.initialized;
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
export { EventBus };
|
@@ -0,0 +1,210 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
const uuid = require('../../shared/conduithub.CvMLTa-R.cjs');
|
4
|
+
require('uuid');
|
5
|
+
|
6
|
+
class Hook {
|
7
|
+
hooks = /* @__PURE__ */ new Map();
|
8
|
+
initialized = false;
|
9
|
+
logger;
|
10
|
+
failFast;
|
11
|
+
onError;
|
12
|
+
constructor(options, logger) {
|
13
|
+
this.failFast = options?.failFast ?? false;
|
14
|
+
this.onError = options?.onError;
|
15
|
+
this.logger = logger ?? uuid.createLogger({ name: "Hook" });
|
16
|
+
}
|
17
|
+
ensure() {
|
18
|
+
if (!this.initialized) throw new Error("Hook system is not initialized");
|
19
|
+
}
|
20
|
+
async initialize() {
|
21
|
+
if (!this.initialized) {
|
22
|
+
this.initialized = true;
|
23
|
+
this.logger.success("Hook system initialized successfully");
|
24
|
+
}
|
25
|
+
}
|
26
|
+
async shutdown() {
|
27
|
+
this.ensure();
|
28
|
+
const hookCount = Array.from(this.hooks.values()).reduce(
|
29
|
+
(total, handlers) => total + handlers.length,
|
30
|
+
0
|
31
|
+
);
|
32
|
+
this.hooks.clear();
|
33
|
+
this.initialized = false;
|
34
|
+
this.logger.success(
|
35
|
+
`Hook system shutdown completed - cleared ${hookCount} handlers`
|
36
|
+
);
|
37
|
+
}
|
38
|
+
registerHook(hook, handler, priority = 0, customId) {
|
39
|
+
this.ensure();
|
40
|
+
if (!this.hooks.has(hook)) this.hooks.set(hook, []);
|
41
|
+
const id = customId ?? uuid.generateUuid();
|
42
|
+
const entry = {
|
43
|
+
id,
|
44
|
+
hook: String(hook),
|
45
|
+
handler,
|
46
|
+
priority
|
47
|
+
};
|
48
|
+
this.hooks.get(hook).push(entry);
|
49
|
+
this.hooks.get(hook).sort((a, b) => b.priority - a.priority);
|
50
|
+
const handlerCount = this.hooks.get(hook).length;
|
51
|
+
this.logger.debug(
|
52
|
+
`Hook registered: '${String(hook)}' \u2192 ${id} (priority: ${priority}, total handlers: ${handlerCount})`
|
53
|
+
);
|
54
|
+
return id;
|
55
|
+
}
|
56
|
+
unregisterHook(hook, id) {
|
57
|
+
this.ensure();
|
58
|
+
const list = this.hooks.get(hook);
|
59
|
+
if (!list) {
|
60
|
+
this.logger.warn(
|
61
|
+
`Failed to unregister hook: '${String(hook)}' \u2192 ${id} (handler not found)`
|
62
|
+
);
|
63
|
+
return false;
|
64
|
+
}
|
65
|
+
const index = list.findIndex((h) => h.id === id);
|
66
|
+
if (index >= 0) {
|
67
|
+
list.splice(index, 1);
|
68
|
+
const remainingCount = list.length;
|
69
|
+
if (remainingCount === 0) this.hooks.delete(hook);
|
70
|
+
this.logger.debug(
|
71
|
+
`Hook unregistered: '${String(hook)}' \u2192 ${id} (remaining handlers: ${remainingCount})`
|
72
|
+
);
|
73
|
+
return true;
|
74
|
+
}
|
75
|
+
this.logger.warn(
|
76
|
+
`Failed to unregister hook: '${String(hook)}' \u2192 ${id} (handler not found)`
|
77
|
+
);
|
78
|
+
return false;
|
79
|
+
}
|
80
|
+
async handleHookResult(raw, failFast, hook, handlerId) {
|
81
|
+
try {
|
82
|
+
const result = await raw;
|
83
|
+
if (typeof result === "object" && result && "success" in result) {
|
84
|
+
const res = result;
|
85
|
+
if (!res.success && failFast) throw new Error(res.error);
|
86
|
+
this.logger.debug(
|
87
|
+
`Hook handler completed: '${String(hook)}' \u2192 ${handlerId} (success: ${res.success})`
|
88
|
+
);
|
89
|
+
return res;
|
90
|
+
}
|
91
|
+
this.logger.debug(
|
92
|
+
`Hook handler completed: '${String(hook)}' \u2192 ${handlerId} (success: true)`
|
93
|
+
);
|
94
|
+
return { success: true, data: result };
|
95
|
+
} catch (err) {
|
96
|
+
if (failFast) throw err;
|
97
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
98
|
+
this.logger.error(
|
99
|
+
`Hook handler failed: '${String(hook)}' \u2192 ${handlerId} - ${errorMessage}`
|
100
|
+
);
|
101
|
+
this.onError?.(err, hook);
|
102
|
+
return {
|
103
|
+
success: false,
|
104
|
+
error: errorMessage
|
105
|
+
};
|
106
|
+
}
|
107
|
+
}
|
108
|
+
async executeHook(hook, data) {
|
109
|
+
this.ensure();
|
110
|
+
const list = this.hooks.get(hook);
|
111
|
+
if (!list?.length) {
|
112
|
+
this.logger.debug(
|
113
|
+
`Hook execution skipped: '${String(hook)}' (no handlers registered)`
|
114
|
+
);
|
115
|
+
return data;
|
116
|
+
}
|
117
|
+
this.logger.debug(
|
118
|
+
`Hook execution started: '${String(hook)}' (${list.length} handlers)`
|
119
|
+
);
|
120
|
+
let result = data;
|
121
|
+
let successCount = 0;
|
122
|
+
let errorCount = 0;
|
123
|
+
for (const { handler, id } of list) {
|
124
|
+
const {
|
125
|
+
success,
|
126
|
+
data: output,
|
127
|
+
error
|
128
|
+
} = await this.handleHookResult(
|
129
|
+
Promise.resolve().then(() => handler(result)),
|
130
|
+
this.failFast,
|
131
|
+
hook,
|
132
|
+
id
|
133
|
+
);
|
134
|
+
if (success) {
|
135
|
+
successCount++;
|
136
|
+
if (output !== void 0) result = output;
|
137
|
+
} else {
|
138
|
+
errorCount++;
|
139
|
+
if (this.failFast) throw new Error(error);
|
140
|
+
}
|
141
|
+
}
|
142
|
+
this.logger.debug(
|
143
|
+
`Hook execution completed: '${String(hook)}' (${successCount} succeeded, ${errorCount} failed)`
|
144
|
+
);
|
145
|
+
return result;
|
146
|
+
}
|
147
|
+
async executeHookSeries(hook, data) {
|
148
|
+
this.ensure();
|
149
|
+
const list = this.hooks.get(hook);
|
150
|
+
if (!list?.length) {
|
151
|
+
this.logger.debug(
|
152
|
+
`Hook series execution skipped: '${String(hook)}' (no handlers registered)`
|
153
|
+
);
|
154
|
+
return [];
|
155
|
+
}
|
156
|
+
this.logger.debug(
|
157
|
+
`Hook series execution started: '${String(hook)}' (${list.length} handlers)`
|
158
|
+
);
|
159
|
+
const results = [];
|
160
|
+
for (const { handler, id } of list) {
|
161
|
+
const result = await this.handleHookResult(
|
162
|
+
Promise.resolve().then(() => handler(data)),
|
163
|
+
false,
|
164
|
+
// Never fail fast in series execution, just collect results
|
165
|
+
hook,
|
166
|
+
id
|
167
|
+
);
|
168
|
+
results.push(result);
|
169
|
+
if (!result.success && this.failFast) break;
|
170
|
+
}
|
171
|
+
const successCount = results.filter((r) => r.success).length;
|
172
|
+
const errorCount = results.filter((r) => !r.success).length;
|
173
|
+
this.logger.debug(
|
174
|
+
`Hook series execution completed: '${String(hook)}' (${successCount} succeeded, ${errorCount} failed)`
|
175
|
+
);
|
176
|
+
return results;
|
177
|
+
}
|
178
|
+
async executeHookSeriesWithContext(hook, data, context) {
|
179
|
+
this.ensure();
|
180
|
+
const handlers = this.hooks.get(hook);
|
181
|
+
if (!handlers || handlers.length === 0) {
|
182
|
+
this.logger.debug(
|
183
|
+
`Hook series execution skipped: '${String(hook)}' (no handlers registered)`
|
184
|
+
);
|
185
|
+
return [];
|
186
|
+
}
|
187
|
+
this.logger.debug(
|
188
|
+
`Hook series execution started: '${String(hook)}' (${handlers.length} handlers)`
|
189
|
+
);
|
190
|
+
const results = [];
|
191
|
+
for (const { handler, id } of handlers) {
|
192
|
+
const result = await this.handleHookResult(
|
193
|
+
Promise.resolve().then(() => handler(data)),
|
194
|
+
this.failFast,
|
195
|
+
hook,
|
196
|
+
id
|
197
|
+
);
|
198
|
+
results.push(result);
|
199
|
+
if (!result.success && this.failFast) break;
|
200
|
+
}
|
201
|
+
const successCount = results.filter((r) => r.success).length;
|
202
|
+
const errorCount = results.filter((r) => !r.success).length;
|
203
|
+
this.logger.debug(
|
204
|
+
`Hook series execution completed: '${String(hook)}' (${successCount} succeeded, ${errorCount} failed)`
|
205
|
+
);
|
206
|
+
return results;
|
207
|
+
}
|
208
|
+
}
|
209
|
+
|
210
|
+
exports.Hook = Hook;
|
@@ -0,0 +1,40 @@
|
|
1
|
+
import { c as createLogger } from '../../shared/conduithub.B7aryjPG.cjs';
|
2
|
+
|
3
|
+
interface HookResult<T = unknown> {
|
4
|
+
success: boolean;
|
5
|
+
data?: T;
|
6
|
+
error?: string;
|
7
|
+
}
|
8
|
+
interface HookHandler<I = unknown, O = unknown> {
|
9
|
+
id: string;
|
10
|
+
hook: string;
|
11
|
+
handler: (data: I) => Promise<HookResult<O> | O> | HookResult<O> | O;
|
12
|
+
priority: number;
|
13
|
+
}
|
14
|
+
|
15
|
+
declare class Hook<HM extends Record<string, {
|
16
|
+
input: any;
|
17
|
+
output: any;
|
18
|
+
}>> {
|
19
|
+
private hooks;
|
20
|
+
private initialized;
|
21
|
+
private logger;
|
22
|
+
private failFast;
|
23
|
+
private onError?;
|
24
|
+
constructor(options?: {
|
25
|
+
failFast?: boolean;
|
26
|
+
onError?: (err: unknown, hook: keyof HM) => void;
|
27
|
+
}, logger?: ReturnType<typeof createLogger>);
|
28
|
+
private ensure;
|
29
|
+
initialize(): Promise<void>;
|
30
|
+
shutdown(): Promise<void>;
|
31
|
+
registerHook<K extends keyof HM>(hook: K, handler: (data: HM[K]["input"]) => HookResult<HM[K]["output"]> | Promise<HookResult<HM[K]["output"]>> | HM[K]["output"] | Promise<HM[K]["output"]>, priority?: number, customId?: string): string;
|
32
|
+
unregisterHook<K extends keyof HM>(hook: K, id: string): boolean;
|
33
|
+
private handleHookResult;
|
34
|
+
executeHook<K extends keyof HM>(hook: K, data: HM[K]["input"]): Promise<HM[K]["output"]>;
|
35
|
+
executeHookSeries<K extends keyof HM>(hook: K, data: HM[K]["input"]): Promise<HookResult<HM[K]["output"]>[]>;
|
36
|
+
executeHookSeriesWithContext<K extends keyof HM>(hook: K, data: HM[K]["input"], context: Record<string, unknown>): Promise<HookResult<HM[K]["output"]>[]>;
|
37
|
+
}
|
38
|
+
|
39
|
+
export { Hook };
|
40
|
+
export type { HookHandler, HookResult };
|
@@ -0,0 +1,40 @@
|
|
1
|
+
import { c as createLogger } from '../../shared/conduithub.B7aryjPG.mjs';
|
2
|
+
|
3
|
+
interface HookResult<T = unknown> {
|
4
|
+
success: boolean;
|
5
|
+
data?: T;
|
6
|
+
error?: string;
|
7
|
+
}
|
8
|
+
interface HookHandler<I = unknown, O = unknown> {
|
9
|
+
id: string;
|
10
|
+
hook: string;
|
11
|
+
handler: (data: I) => Promise<HookResult<O> | O> | HookResult<O> | O;
|
12
|
+
priority: number;
|
13
|
+
}
|
14
|
+
|
15
|
+
declare class Hook<HM extends Record<string, {
|
16
|
+
input: any;
|
17
|
+
output: any;
|
18
|
+
}>> {
|
19
|
+
private hooks;
|
20
|
+
private initialized;
|
21
|
+
private logger;
|
22
|
+
private failFast;
|
23
|
+
private onError?;
|
24
|
+
constructor(options?: {
|
25
|
+
failFast?: boolean;
|
26
|
+
onError?: (err: unknown, hook: keyof HM) => void;
|
27
|
+
}, logger?: ReturnType<typeof createLogger>);
|
28
|
+
private ensure;
|
29
|
+
initialize(): Promise<void>;
|
30
|
+
shutdown(): Promise<void>;
|
31
|
+
registerHook<K extends keyof HM>(hook: K, handler: (data: HM[K]["input"]) => HookResult<HM[K]["output"]> | Promise<HookResult<HM[K]["output"]>> | HM[K]["output"] | Promise<HM[K]["output"]>, priority?: number, customId?: string): string;
|
32
|
+
unregisterHook<K extends keyof HM>(hook: K, id: string): boolean;
|
33
|
+
private handleHookResult;
|
34
|
+
executeHook<K extends keyof HM>(hook: K, data: HM[K]["input"]): Promise<HM[K]["output"]>;
|
35
|
+
executeHookSeries<K extends keyof HM>(hook: K, data: HM[K]["input"]): Promise<HookResult<HM[K]["output"]>[]>;
|
36
|
+
executeHookSeriesWithContext<K extends keyof HM>(hook: K, data: HM[K]["input"], context: Record<string, unknown>): Promise<HookResult<HM[K]["output"]>[]>;
|
37
|
+
}
|
38
|
+
|
39
|
+
export { Hook };
|
40
|
+
export type { HookHandler, HookResult };
|