abxbus 2.4.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.
Files changed (137) hide show
  1. package/README.md +647 -0
  2. package/dist/cjs/async_context.d.ts +12 -0
  3. package/dist/cjs/async_context.js +70 -0
  4. package/dist/cjs/async_context.js.map +7 -0
  5. package/dist/cjs/base_event.d.ts +207 -0
  6. package/dist/cjs/base_event.js +871 -0
  7. package/dist/cjs/base_event.js.map +7 -0
  8. package/dist/cjs/bridge_jsonl.d.ts +26 -0
  9. package/dist/cjs/bridge_jsonl.js +170 -0
  10. package/dist/cjs/bridge_jsonl.js.map +7 -0
  11. package/dist/cjs/bridge_nats.d.ts +20 -0
  12. package/dist/cjs/bridge_nats.js +108 -0
  13. package/dist/cjs/bridge_nats.js.map +7 -0
  14. package/dist/cjs/bridge_postgres.d.ts +31 -0
  15. package/dist/cjs/bridge_postgres.js +251 -0
  16. package/dist/cjs/bridge_postgres.js.map +7 -0
  17. package/dist/cjs/bridge_redis.d.ts +34 -0
  18. package/dist/cjs/bridge_redis.js +175 -0
  19. package/dist/cjs/bridge_redis.js.map +7 -0
  20. package/dist/cjs/bridge_sqlite.d.ts +30 -0
  21. package/dist/cjs/bridge_sqlite.js +255 -0
  22. package/dist/cjs/bridge_sqlite.js.map +7 -0
  23. package/dist/cjs/bridges.d.ts +49 -0
  24. package/dist/cjs/bridges.js +326 -0
  25. package/dist/cjs/bridges.js.map +7 -0
  26. package/dist/cjs/event_bus.d.ts +127 -0
  27. package/dist/cjs/event_bus.js +1058 -0
  28. package/dist/cjs/event_bus.js.map +7 -0
  29. package/dist/cjs/event_handler.d.ts +139 -0
  30. package/dist/cjs/event_handler.js +299 -0
  31. package/dist/cjs/event_handler.js.map +7 -0
  32. package/dist/cjs/event_history.d.ts +45 -0
  33. package/dist/cjs/event_history.js +192 -0
  34. package/dist/cjs/event_history.js.map +7 -0
  35. package/dist/cjs/event_result.d.ts +86 -0
  36. package/dist/cjs/event_result.js +446 -0
  37. package/dist/cjs/event_result.js.map +7 -0
  38. package/dist/cjs/events_suck.d.ts +40 -0
  39. package/dist/cjs/events_suck.js +59 -0
  40. package/dist/cjs/events_suck.js.map +7 -0
  41. package/dist/cjs/helpers.d.ts +1 -0
  42. package/dist/cjs/helpers.js +84 -0
  43. package/dist/cjs/helpers.js.map +7 -0
  44. package/dist/cjs/index.d.ts +17 -0
  45. package/dist/cjs/index.js +54 -0
  46. package/dist/cjs/index.js.map +7 -0
  47. package/dist/cjs/lock_manager.d.ts +70 -0
  48. package/dist/cjs/lock_manager.js +343 -0
  49. package/dist/cjs/lock_manager.js.map +7 -0
  50. package/dist/cjs/logging.d.ts +16 -0
  51. package/dist/cjs/logging.js +216 -0
  52. package/dist/cjs/logging.js.map +7 -0
  53. package/dist/cjs/middlewares.d.ts +13 -0
  54. package/dist/cjs/middlewares.js +17 -0
  55. package/dist/cjs/middlewares.js.map +7 -0
  56. package/dist/cjs/optional_deps.d.ts +3 -0
  57. package/dist/cjs/optional_deps.js +64 -0
  58. package/dist/cjs/optional_deps.js.map +7 -0
  59. package/dist/cjs/package.json +5 -0
  60. package/dist/cjs/retry.d.ts +52 -0
  61. package/dist/cjs/retry.js +257 -0
  62. package/dist/cjs/retry.js.map +7 -0
  63. package/dist/cjs/timing.d.ts +3 -0
  64. package/dist/cjs/timing.js +76 -0
  65. package/dist/cjs/timing.js.map +7 -0
  66. package/dist/cjs/type_inference.test.d.ts +1 -0
  67. package/dist/cjs/types.d.ts +36 -0
  68. package/dist/cjs/types.js +104 -0
  69. package/dist/cjs/types.js.map +7 -0
  70. package/dist/esm/async_context.js +50 -0
  71. package/dist/esm/async_context.js.map +7 -0
  72. package/dist/esm/base_event.js +857 -0
  73. package/dist/esm/base_event.js.map +7 -0
  74. package/dist/esm/bridge_jsonl.js +150 -0
  75. package/dist/esm/bridge_jsonl.js.map +7 -0
  76. package/dist/esm/bridge_nats.js +88 -0
  77. package/dist/esm/bridge_nats.js.map +7 -0
  78. package/dist/esm/bridge_postgres.js +231 -0
  79. package/dist/esm/bridge_postgres.js.map +7 -0
  80. package/dist/esm/bridge_redis.js +155 -0
  81. package/dist/esm/bridge_redis.js.map +7 -0
  82. package/dist/esm/bridge_sqlite.js +235 -0
  83. package/dist/esm/bridge_sqlite.js.map +7 -0
  84. package/dist/esm/bridges.js +306 -0
  85. package/dist/esm/bridges.js.map +7 -0
  86. package/dist/esm/event_bus.js +1046 -0
  87. package/dist/esm/event_bus.js.map +7 -0
  88. package/dist/esm/event_handler.js +279 -0
  89. package/dist/esm/event_handler.js.map +7 -0
  90. package/dist/esm/event_history.js +172 -0
  91. package/dist/esm/event_history.js.map +7 -0
  92. package/dist/esm/event_result.js +426 -0
  93. package/dist/esm/event_result.js.map +7 -0
  94. package/dist/esm/events_suck.js +39 -0
  95. package/dist/esm/events_suck.js.map +7 -0
  96. package/dist/esm/helpers.js +64 -0
  97. package/dist/esm/helpers.js.map +7 -0
  98. package/dist/esm/index.js +47 -0
  99. package/dist/esm/index.js.map +7 -0
  100. package/dist/esm/lock_manager.js +323 -0
  101. package/dist/esm/lock_manager.js.map +7 -0
  102. package/dist/esm/logging.js +196 -0
  103. package/dist/esm/logging.js.map +7 -0
  104. package/dist/esm/middlewares.js +1 -0
  105. package/dist/esm/middlewares.js.map +7 -0
  106. package/dist/esm/optional_deps.js +44 -0
  107. package/dist/esm/optional_deps.js.map +7 -0
  108. package/dist/esm/retry.js +237 -0
  109. package/dist/esm/retry.js.map +7 -0
  110. package/dist/esm/timing.js +56 -0
  111. package/dist/esm/timing.js.map +7 -0
  112. package/dist/esm/types.js +84 -0
  113. package/dist/esm/types.js.map +7 -0
  114. package/dist/types/async_context.d.ts +12 -0
  115. package/dist/types/base_event.d.ts +207 -0
  116. package/dist/types/bridge_jsonl.d.ts +26 -0
  117. package/dist/types/bridge_nats.d.ts +20 -0
  118. package/dist/types/bridge_postgres.d.ts +31 -0
  119. package/dist/types/bridge_redis.d.ts +34 -0
  120. package/dist/types/bridge_sqlite.d.ts +30 -0
  121. package/dist/types/bridges.d.ts +49 -0
  122. package/dist/types/event_bus.d.ts +127 -0
  123. package/dist/types/event_handler.d.ts +139 -0
  124. package/dist/types/event_history.d.ts +45 -0
  125. package/dist/types/event_result.d.ts +86 -0
  126. package/dist/types/events_suck.d.ts +40 -0
  127. package/dist/types/helpers.d.ts +1 -0
  128. package/dist/types/index.d.ts +17 -0
  129. package/dist/types/lock_manager.d.ts +70 -0
  130. package/dist/types/logging.d.ts +16 -0
  131. package/dist/types/middlewares.d.ts +13 -0
  132. package/dist/types/optional_deps.d.ts +3 -0
  133. package/dist/types/retry.d.ts +52 -0
  134. package/dist/types/timing.d.ts +3 -0
  135. package/dist/types/type_inference.test.d.ts +1 -0
  136. package/dist/types/types.d.ts +36 -0
  137. package/package.json +87 -0
@@ -0,0 +1,139 @@
1
+ import { z } from 'zod';
2
+ import { type EventHandlerCallable, type EventPattern } from './types.js';
3
+ import { BaseEvent } from './base_event.js';
4
+ import type { EventResult } from './event_result.js';
5
+ export type EphemeralFindEventHandler = {
6
+ event_pattern: string | '*';
7
+ matches: (event: BaseEvent) => boolean;
8
+ resolve: (event: BaseEvent) => void;
9
+ timeout_id?: ReturnType<typeof setTimeout>;
10
+ };
11
+ export declare const FindWaiterJSONSchema: z.ZodObject<{
12
+ event_pattern: z.ZodUnion<readonly [z.ZodString, z.ZodLiteral<"*">]>;
13
+ has_timeout: z.ZodBoolean;
14
+ }, z.core.$strict>;
15
+ export type FindWaiterJSON = z.infer<typeof FindWaiterJSONSchema>;
16
+ export declare class FindWaiter {
17
+ static toJSON(waiter: EphemeralFindEventHandler): FindWaiterJSON;
18
+ static fromJSON(data: unknown, overrides?: {
19
+ matches?: (event: BaseEvent) => boolean;
20
+ resolve?: (event: BaseEvent) => void;
21
+ }): EphemeralFindEventHandler;
22
+ static toJSONArray(waiters: Iterable<EphemeralFindEventHandler>): FindWaiterJSON[];
23
+ static fromJSONArray(data: unknown, overrides?: {
24
+ matches?: (event: BaseEvent) => boolean;
25
+ resolve?: (event: BaseEvent) => void;
26
+ }): EphemeralFindEventHandler[];
27
+ }
28
+ export declare const EventHandlerJSONSchema: z.ZodObject<{
29
+ id: z.ZodString;
30
+ eventbus_name: z.ZodString;
31
+ eventbus_id: z.ZodString;
32
+ event_pattern: z.ZodUnion<readonly [z.ZodString, z.ZodLiteral<"*">]>;
33
+ handler_name: z.ZodString;
34
+ handler_file_path: z.ZodOptional<z.ZodNullable<z.ZodString>>;
35
+ handler_timeout: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
36
+ handler_slow_timeout: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
37
+ handler_registered_at: z.ZodString;
38
+ }, z.core.$strict>;
39
+ export type EventHandlerJSON = z.infer<typeof EventHandlerJSONSchema>;
40
+ export declare class EventHandler {
41
+ id: string;
42
+ handler: EventHandlerCallable;
43
+ handler_name: string;
44
+ handler_file_path: string | null;
45
+ handler_timeout?: number | null;
46
+ handler_slow_timeout?: number | null;
47
+ handler_registered_at: string;
48
+ event_pattern: string | '*';
49
+ eventbus_name: string;
50
+ eventbus_id: string;
51
+ constructor(params: {
52
+ id?: string;
53
+ handler: EventHandlerCallable;
54
+ handler_name: string;
55
+ handler_file_path?: string | null;
56
+ handler_timeout?: number | null;
57
+ handler_slow_timeout?: number | null;
58
+ handler_registered_at: string;
59
+ event_pattern: string | '*';
60
+ eventbus_name: string;
61
+ eventbus_id: string;
62
+ });
63
+ get _handler_async(): EventHandlerCallable;
64
+ static computeHandlerId(params: {
65
+ eventbus_id: string;
66
+ handler_name: string;
67
+ handler_file_path?: string | null;
68
+ handler_registered_at: string;
69
+ event_pattern: string | '*';
70
+ }): string;
71
+ static fromCallable<TEvent extends BaseEvent = BaseEvent>(params: {
72
+ handler: EventHandlerCallable<TEvent>;
73
+ event_pattern: EventPattern | '*';
74
+ eventbus_name: string;
75
+ eventbus_id: string;
76
+ detect_handler_file_path?: boolean;
77
+ id?: string;
78
+ handler_file_path?: string | null;
79
+ handler_timeout?: number | null;
80
+ handler_slow_timeout?: number | null;
81
+ handler_registered_at?: string;
82
+ }): EventHandler;
83
+ toString(): string;
84
+ _detectHandlerFilePath(): void;
85
+ toJSON(): EventHandlerJSON;
86
+ static fromJSON(data: unknown, handler?: EventHandlerCallable): EventHandler;
87
+ static toJSONArray(handlers: Iterable<EventHandler>): EventHandlerJSON[];
88
+ static fromJSONArray(data: unknown, handler?: EventHandlerCallable): EventHandler[];
89
+ get eventbus_label(): string;
90
+ }
91
+ export declare class TimeoutError extends Error {
92
+ constructor(message: string);
93
+ }
94
+ export declare class EventHandlerError extends Error {
95
+ event_result: EventResult;
96
+ timeout_seconds: number | null;
97
+ cause: Error;
98
+ constructor(message: string, params: {
99
+ event_result: EventResult;
100
+ timeout_seconds?: number | null;
101
+ cause: Error;
102
+ });
103
+ get event(): BaseEvent;
104
+ get event_type(): string;
105
+ get handler_name(): string;
106
+ get handler_id(): string;
107
+ get event_timeout(): number | null;
108
+ }
109
+ export declare class EventHandlerTimeoutError extends EventHandlerError {
110
+ constructor(message: string, params: {
111
+ event_result: EventResult;
112
+ timeout_seconds?: number | null;
113
+ cause?: Error;
114
+ });
115
+ }
116
+ export declare class EventHandlerCancelledError extends EventHandlerError {
117
+ constructor(message: string, params: {
118
+ event_result: EventResult;
119
+ timeout_seconds?: number | null;
120
+ cause: Error;
121
+ });
122
+ }
123
+ export declare class EventHandlerAbortedError extends EventHandlerError {
124
+ constructor(message: string, params: {
125
+ event_result: EventResult;
126
+ timeout_seconds?: number | null;
127
+ cause: Error;
128
+ });
129
+ }
130
+ export declare class EventHandlerResultSchemaError extends EventHandlerError {
131
+ raw_value: unknown;
132
+ constructor(message: string, params: {
133
+ event_result: EventResult;
134
+ timeout_seconds?: number | null;
135
+ cause: Error;
136
+ raw_value: unknown;
137
+ });
138
+ get expected_schema(): any;
139
+ }
@@ -0,0 +1,45 @@
1
+ import { BaseEvent } from './base_event.js';
2
+ import type { EventPattern, FindWindow } from './types.js';
3
+ export type EventHistoryFindOptions = {
4
+ past?: FindWindow;
5
+ future?: FindWindow;
6
+ child_of?: BaseEvent | null;
7
+ event_is_child_of?: (event: BaseEvent, ancestor: BaseEvent) => boolean;
8
+ wait_for_future_match?: (event_pattern: string | '*', matches: (event: BaseEvent) => boolean, future: FindWindow) => Promise<BaseEvent | null>;
9
+ } & Record<string, unknown>;
10
+ export type EventHistoryTrimOptions<TEvent extends BaseEvent = BaseEvent> = {
11
+ is_event_complete?: (event: TEvent) => boolean;
12
+ on_remove?: (event: TEvent) => void;
13
+ owner_label?: string;
14
+ max_history_size?: number | null;
15
+ max_history_drop?: boolean;
16
+ };
17
+ export declare class EventHistory<TEvent extends BaseEvent = BaseEvent> implements Iterable<[string, TEvent]> {
18
+ max_history_size: number | null;
19
+ max_history_drop: boolean;
20
+ private _events;
21
+ private _warned_about_dropping_uncompleted_events;
22
+ constructor(options?: {
23
+ max_history_size?: number | null;
24
+ max_history_drop?: boolean;
25
+ });
26
+ get size(): number;
27
+ [Symbol.iterator](): Iterator<[string, TEvent]>;
28
+ entries(): IterableIterator<[string, TEvent]>;
29
+ keys(): IterableIterator<string>;
30
+ values(): IterableIterator<TEvent>;
31
+ clear(): void;
32
+ get(event_id: string): TEvent | undefined;
33
+ set(event_id: string, event: TEvent): this;
34
+ has(event_id: string): boolean;
35
+ delete(event_id: string): boolean;
36
+ addEvent(event: TEvent): void;
37
+ getEvent(event_id: string): TEvent | undefined;
38
+ removeEvent(event_id: string): boolean;
39
+ hasEvent(event_id: string): boolean;
40
+ static normalizeEventPattern(event_pattern: EventPattern | '*'): string | '*';
41
+ find(event_pattern: '*', where?: (event: TEvent) => boolean, options?: EventHistoryFindOptions): Promise<TEvent | null>;
42
+ find<TMatch extends TEvent>(event_pattern: EventPattern<TMatch>, where?: (event: TMatch) => boolean, options?: EventHistoryFindOptions): Promise<TMatch | null>;
43
+ trimEventHistory(options?: EventHistoryTrimOptions<TEvent>): number;
44
+ private eventIsChildOf;
45
+ }
@@ -0,0 +1,86 @@
1
+ import { z } from 'zod';
2
+ import { BaseEvent } from './base_event.js';
3
+ import type { EventBus } from './event_bus.js';
4
+ import { EventHandler } from './event_handler.js';
5
+ import { type HandlerLock } from './lock_manager.js';
6
+ import type { Deferred } from './lock_manager.js';
7
+ import type { EventResultType } from './types.js';
8
+ export type EventResultStatus = 'pending' | 'started' | 'completed' | 'error';
9
+ export declare const EventResultJSONSchema: z.ZodObject<{
10
+ id: z.ZodString;
11
+ status: z.ZodEnum<{
12
+ pending: "pending";
13
+ started: "started";
14
+ completed: "completed";
15
+ error: "error";
16
+ }>;
17
+ event_id: z.ZodString;
18
+ handler_id: z.ZodString;
19
+ handler_name: z.ZodString;
20
+ handler_file_path: z.ZodOptional<z.ZodNullable<z.ZodString>>;
21
+ handler_timeout: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
22
+ handler_slow_timeout: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
23
+ handler_registered_at: z.ZodOptional<z.ZodString>;
24
+ handler_event_pattern: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodLiteral<"*">]>>;
25
+ eventbus_name: z.ZodString;
26
+ eventbus_id: z.ZodString;
27
+ started_at: z.ZodOptional<z.ZodNullable<z.ZodString>>;
28
+ completed_at: z.ZodOptional<z.ZodNullable<z.ZodString>>;
29
+ result: z.ZodOptional<z.ZodUnknown>;
30
+ error: z.ZodOptional<z.ZodUnknown>;
31
+ event_children: z.ZodArray<z.ZodString>;
32
+ }, z.core.$strict>;
33
+ export type EventResultJSON = z.infer<typeof EventResultJSONSchema>;
34
+ export declare class EventResult<TEvent extends BaseEvent = BaseEvent> {
35
+ id: string;
36
+ status: EventResultStatus;
37
+ event: TEvent;
38
+ handler: EventHandler;
39
+ started_at: string | null;
40
+ completed_at: string | null;
41
+ result?: EventResultType<TEvent>;
42
+ error?: unknown;
43
+ event_children: BaseEvent[];
44
+ _abort: Deferred<never> | null;
45
+ _lock: HandlerLock | null;
46
+ _queue_jump_pause_releases: Map<EventBus, () => void> | null;
47
+ constructor(params: {
48
+ event: TEvent;
49
+ handler: EventHandler;
50
+ });
51
+ toString(): string;
52
+ get event_id(): string;
53
+ get bus(): EventBus;
54
+ get handler_id(): string;
55
+ get handler_name(): string;
56
+ get handler_file_path(): string | null;
57
+ get eventbus_name(): string;
58
+ get eventbus_id(): string;
59
+ get eventbus_label(): string;
60
+ private getHookBus;
61
+ private _notifyStatusHook;
62
+ get value(): EventResultType<TEvent> | undefined;
63
+ get result_type(): TEvent['event_result_type'];
64
+ _linkEmittedChildEvent(child_event: BaseEvent): void;
65
+ get raw_value(): EventResultType<TEvent> | undefined;
66
+ get handler_timeout(): number | null;
67
+ get handler_slow_timeout(): number | null;
68
+ _createSlowHandlerWarningTimer(effective_timeout: number | null): ReturnType<typeof setTimeout> | null;
69
+ _ensureQueueJumpPause(bus: EventBus): void;
70
+ _releaseQueueJumpPauses(): void;
71
+ update(params: {
72
+ status?: EventResultStatus;
73
+ result?: EventResultType<TEvent> | BaseEvent | undefined;
74
+ error?: unknown;
75
+ }): this;
76
+ private _createHandlerTimeoutError;
77
+ private _handleHandlerError;
78
+ private _onHandlerExit;
79
+ runHandler(handler_lock: HandlerLock | null): Promise<void>;
80
+ _signalAbort(error: Error): void;
81
+ _markStarted(notify_hook?: boolean): Promise<never>;
82
+ _markCompleted(result: EventResultType<TEvent> | BaseEvent | undefined, notify_hook?: boolean): void;
83
+ _markError(error: unknown, notify_hook?: boolean): void;
84
+ toJSON(): EventResultJSON;
85
+ static fromJSON<TEvent extends BaseEvent>(event: TEvent, data: unknown): EventResult<TEvent>;
86
+ }
@@ -0,0 +1,40 @@
1
+ import { EventBus } from './event_bus.js';
2
+ import { BaseEvent } from './base_event.js';
3
+ import type { EventClass, EventResultType } from './types.js';
4
+ type EventMap = Record<string, EventClass<BaseEvent>>;
5
+ type AnyFn = (...args: any[]) => any;
6
+ type FunctionMap = Record<string, AnyFn>;
7
+ type ExtraDict = Record<string, unknown>;
8
+ type EventFieldsFromFn<TFunc extends AnyFn> = Parameters<TFunc> extends [infer TArg] ? (TArg extends Record<string, unknown> ? TArg : ExtraDict) : ExtraDict;
9
+ type GeneratedEvent<TFunc extends AnyFn> = {
10
+ (data: EventFieldsFromFn<TFunc> & ExtraDict): BaseEvent & EventFieldsFromFn<TFunc> & {
11
+ __event_result_type__?: Awaited<ReturnType<TFunc>>;
12
+ };
13
+ new (data: EventFieldsFromFn<TFunc> & ExtraDict): BaseEvent & EventFieldsFromFn<TFunc> & {
14
+ __event_result_type__?: Awaited<ReturnType<TFunc>>;
15
+ };
16
+ event_type?: string;
17
+ };
18
+ export type GeneratedEvents<TEvents extends FunctionMap> = {
19
+ by_name: {
20
+ [K in keyof TEvents]: GeneratedEvent<TEvents[K]>;
21
+ };
22
+ } & {
23
+ [K in keyof TEvents]: GeneratedEvent<TEvents[K]>;
24
+ };
25
+ type EventInit<TEventClass extends EventClass<BaseEvent>> = ConstructorParameters<TEventClass> extends [infer TInit, ...unknown[]] ? TInit : never;
26
+ type EventMethodArgs<TEventClass extends EventClass<BaseEvent>> = {} extends EventInit<TEventClass> ? [init?: EventInit<TEventClass>, extra?: Record<string, unknown>] : [init: EventInit<TEventClass>, extra?: Record<string, unknown>];
27
+ type EventMethodResult<TEventClass extends EventClass<BaseEvent>> = EventResultType<InstanceType<TEventClass>> | undefined;
28
+ export type EventsSuckClient<TEvents extends EventMap> = {
29
+ bus: EventBus;
30
+ } & {
31
+ [K in keyof TEvents]: (...args: EventMethodArgs<TEvents[K]>) => Promise<EventMethodResult<TEvents[K]>>;
32
+ };
33
+ export type EventsSuckClientClass<TEvents extends EventMap> = new (bus?: EventBus) => EventsSuckClient<TEvents>;
34
+ export declare const make_events: <TEvents extends FunctionMap>(events: TEvents) => GeneratedEvents<TEvents>;
35
+ export declare const wrap: <TEvents extends EventMap>(class_name: string, methods: TEvents) => EventsSuckClientClass<TEvents>;
36
+ export declare const events_suck: {
37
+ readonly make_events: <TEvents extends FunctionMap>(events: TEvents) => GeneratedEvents<TEvents>;
38
+ readonly wrap: <TEvents extends EventMap>(class_name: string, methods: TEvents) => EventsSuckClientClass<TEvents>;
39
+ };
40
+ export {};
@@ -0,0 +1 @@
1
+ export declare function monotonicDatetime(isostring?: string): string;
@@ -0,0 +1,17 @@
1
+ export { BaseEvent, BaseEventSchema } from './base_event.js';
2
+ export { EventHistory } from './event_history.js';
3
+ export type { EventHistoryFindOptions, EventHistoryTrimOptions } from './event_history.js';
4
+ export { EventResult } from './event_result.js';
5
+ export { EventBus } from './event_bus.js';
6
+ export type { EventBusJSON, EventBusOptions } from './event_bus.js';
7
+ export { monotonicDatetime } from './helpers.js';
8
+ export type { EventBusMiddleware, EventBusMiddlewareCtor, EventBusMiddlewareInput } from './middlewares.js';
9
+ export { EventHandlerTimeoutError, EventHandlerCancelledError, EventHandlerAbortedError, EventHandlerResultSchemaError, } from './event_handler.js';
10
+ export type { EventConcurrencyMode, EventHandlerConcurrencyMode, EventHandlerCompletionMode, EventBusInterfaceForLockManager, } from './lock_manager.js';
11
+ export type { EventClass, EventHandlerCallable as EventHandler, EventPattern, EventStatus, FindOptions, FindWindow } from './types.js';
12
+ export { retry, clearSemaphoreRegistry, RetryTimeoutError, SemaphoreTimeoutError } from './retry.js';
13
+ export type { RetryOptions } from './retry.js';
14
+ export { HTTPEventBridge, SocketEventBridge, NATSEventBridge, RedisEventBridge, PostgresEventBridge, JSONLEventBridge, SQLiteEventBridge, } from './bridges.js';
15
+ export type { HTTPEventBridgeOptions } from './bridges.js';
16
+ export { events_suck } from './events_suck.js';
17
+ export type { EventsSuckClient, EventsSuckClientClass, GeneratedEvents } from './events_suck.js';
@@ -0,0 +1,70 @@
1
+ import type { BaseEvent } from './base_event.js';
2
+ import type { EventResult } from './event_result.js';
3
+ export type Deferred<T> = {
4
+ promise: Promise<T>;
5
+ resolve: (value: T | PromiseLike<T>) => void;
6
+ reject: (reason?: unknown) => void;
7
+ };
8
+ export declare const withResolvers: <T>() => Deferred<T>;
9
+ export declare const EVENT_CONCURRENCY_MODES: readonly ["global-serial", "bus-serial", "parallel"];
10
+ export type EventConcurrencyMode = (typeof EVENT_CONCURRENCY_MODES)[number];
11
+ export declare const EVENT_HANDLER_CONCURRENCY_MODES: readonly ["serial", "parallel"];
12
+ export type EventHandlerConcurrencyMode = (typeof EVENT_HANDLER_CONCURRENCY_MODES)[number];
13
+ export declare const EVENT_HANDLER_COMPLETION_MODES: readonly ["all", "first"];
14
+ export type EventHandlerCompletionMode = (typeof EVENT_HANDLER_COMPLETION_MODES)[number];
15
+ export declare class AsyncLock {
16
+ size: number;
17
+ in_use: number;
18
+ waiters: Array<() => void>;
19
+ constructor(size: number);
20
+ acquire(): Promise<void>;
21
+ release(): void;
22
+ }
23
+ export declare const runWithLock: <T>(lock: AsyncLock | null, fn: () => Promise<T>) => Promise<T>;
24
+ export type HandlerExecutionState = 'held' | 'yielded' | 'closed';
25
+ export declare class HandlerLock {
26
+ private lock;
27
+ private state;
28
+ constructor(lock: AsyncLock | null);
29
+ yieldHandlerLockForChildRun(): boolean;
30
+ reclaimHandlerLockIfRunning(): Promise<boolean>;
31
+ exitHandlerRun(): void;
32
+ runQueueJump<T>(fn: () => Promise<T>): Promise<T>;
33
+ }
34
+ export type EventBusInterfaceForLockManager = {
35
+ isIdleAndQueueEmpty: () => boolean;
36
+ event_concurrency: EventConcurrencyMode;
37
+ _lock_for_event_global_serial: AsyncLock;
38
+ };
39
+ export type LockManagerOptions = {
40
+ auto_schedule_idle_checks?: boolean;
41
+ };
42
+ export declare class LockManager {
43
+ private bus;
44
+ private auto_schedule_idle_checks;
45
+ readonly bus_event_lock: AsyncLock;
46
+ private pause_depth;
47
+ private pause_waiters;
48
+ private active_handler_results;
49
+ private idle_waiters;
50
+ private idle_check_pending;
51
+ private idle_check_streak;
52
+ constructor(bus: EventBusInterfaceForLockManager, options?: LockManagerOptions);
53
+ _requestRunloopPause(): () => void;
54
+ _waitUntilRunloopResumed(): Promise<void>;
55
+ _isPaused(): boolean;
56
+ _runWithHandlerDispatchContext<T>(result: EventResult, fn: () => Promise<T>): Promise<T>;
57
+ _getActiveHandlerResult(): EventResult | undefined;
58
+ _getActiveHandlerResults(): EventResult[];
59
+ _isAnyHandlerActive(): boolean;
60
+ waitForIdle(timeout_seconds?: number | null): Promise<boolean>;
61
+ _notifyIdleListeners(): void;
62
+ getLockForEvent(event: BaseEvent): AsyncLock | null;
63
+ _runWithEventLock<T>(event: BaseEvent, fn: () => Promise<T>, options?: {
64
+ bypass_event_locks?: boolean;
65
+ pre_acquired_lock?: AsyncLock | null;
66
+ }): Promise<T>;
67
+ _runWithHandlerLock<T>(event: BaseEvent, default_handler_concurrency: EventHandlerConcurrencyMode | undefined, fn: (lock: HandlerLock | null) => Promise<T>): Promise<T>;
68
+ private scheduleIdleCheck;
69
+ clear(): void;
70
+ }
@@ -0,0 +1,16 @@
1
+ import { BaseEvent } from './base_event.js';
2
+ import { EventResult } from './event_result.js';
3
+ type LogTreeBus = {
4
+ name: string;
5
+ event_history: {
6
+ values(): IterableIterator<BaseEvent>;
7
+ has(event_id: string): boolean;
8
+ };
9
+ toString?: () => string;
10
+ };
11
+ export declare const logTree: (bus: LogTreeBus) => string;
12
+ export declare const buildTreeLine: (event: BaseEvent, indent: string, is_last: boolean, parent_to_children: Map<string, BaseEvent[]>, visited: Set<string>) => string;
13
+ export declare const buildResultLine: (result: EventResult, indent: string, is_last: boolean, parent_to_children: Map<string, BaseEvent[]>, visited: Set<string>) => string;
14
+ export declare const formatTimestamp: (value?: string) => string;
15
+ export declare const formatResultValue: (value: unknown) => string;
16
+ export {};
@@ -0,0 +1,13 @@
1
+ import type { BaseEvent } from './base_event.js';
2
+ import type { EventBus } from './event_bus.js';
3
+ import type { EventHandler } from './event_handler.js';
4
+ import type { EventResult } from './event_result.js';
5
+ import type { EventStatus } from './types.js';
6
+ export type { EventStatus } from './types.js';
7
+ export interface EventBusMiddleware {
8
+ onEventChange?(eventbus: EventBus, event: BaseEvent, status: EventStatus): void | Promise<void>;
9
+ onEventResultChange?(eventbus: EventBus, event: BaseEvent, event_result: EventResult, status: EventStatus): void | Promise<void>;
10
+ onBusHandlersChange?(eventbus: EventBus, handler: EventHandler, registered: boolean): void | Promise<void>;
11
+ }
12
+ export type EventBusMiddlewareCtor = new () => EventBusMiddleware;
13
+ export type EventBusMiddlewareInput = EventBusMiddleware | EventBusMiddlewareCtor;
@@ -0,0 +1,3 @@
1
+ export declare const isNodeRuntime: () => boolean;
2
+ export declare const assertOptionalDependencyAvailable: (bridge_name: string, package_name: string) => void;
3
+ export declare const importOptionalDependency: (bridge_name: string, package_name: string) => Promise<any>;
@@ -0,0 +1,52 @@
1
+ export interface RetryOptions {
2
+ /** Total number of attempts including the initial call (1 = no retry, 3 = up to 2 retries). Default: 1 */
3
+ max_attempts?: number;
4
+ /** Seconds to wait between retries. Default: 0 */
5
+ retry_after?: number;
6
+ /** Multiplier applied to retry_after after each attempt for exponential backoff. Default: 1.0 (constant delay) */
7
+ retry_backoff_factor?: number;
8
+ /** Only retry when the thrown error matches one of these matchers. Accepts error class constructors,
9
+ * string error names (matched against error.name), or RegExp patterns (tested against String(error)).
10
+ * Default: undefined (retry on any error) */
11
+ retry_on_errors?: Array<(new (...args: any[]) => Error) | string | RegExp>;
12
+ /** Per-attempt timeout in seconds. Default: undefined (no per-attempt timeout) */
13
+ timeout?: number | null;
14
+ /** Maximum concurrent executions sharing this semaphore. Default: undefined (no concurrency limit) */
15
+ semaphore_limit?: number | null;
16
+ /** Semaphore identifier. Functions with the same name share the same concurrency slot pool. Default: function name.
17
+ * If a function is provided, it receives the same arguments as the wrapped function. */
18
+ semaphore_name?: string | ((...args: any[]) => string) | null;
19
+ /** If true, proceed without concurrency limit when semaphore acquisition times out. Default: true */
20
+ semaphore_lax?: boolean;
21
+ /** Semaphore scoping strategy. Default: 'global'
22
+ * - 'global': all calls share one semaphore (keyed by semaphore_name)
23
+ * - 'class': all instances of the same class share one semaphore (keyed by className.semaphore_name)
24
+ * - 'instance': each object instance gets its own semaphore (keyed by instanceId.semaphore_name)
25
+ * 'class' and 'instance' require `this` to be an object; they fall back to 'global' for standalone calls. */
26
+ semaphore_scope?: 'global' | 'class' | 'instance';
27
+ /** Maximum seconds to wait for semaphore acquisition. Default: undefined → timeout * max(1, limit - 1) */
28
+ semaphore_timeout?: number | null;
29
+ }
30
+ /** Thrown when a single attempt exceeds the per-attempt timeout. */
31
+ export declare class RetryTimeoutError extends Error {
32
+ timeout_seconds: number;
33
+ attempt: number;
34
+ constructor(message: string, params: {
35
+ timeout_seconds: number;
36
+ attempt: number;
37
+ });
38
+ }
39
+ /** Thrown (when semaphore_lax=false) if the semaphore cannot be acquired within the timeout. */
40
+ export declare class SemaphoreTimeoutError extends Error {
41
+ semaphore_name: string;
42
+ semaphore_limit: number;
43
+ timeout_seconds: number;
44
+ constructor(message: string, params: {
45
+ semaphore_name: string;
46
+ semaphore_limit: number;
47
+ timeout_seconds: number;
48
+ });
49
+ }
50
+ /** Reset the global semaphore registry. Useful in tests. */
51
+ export declare function clearSemaphoreRegistry(): void;
52
+ export declare function retry(options?: RetryOptions): <T extends (...args: any[]) => any>(target: T, _context?: ClassMethodDecoratorContext) => T;
@@ -0,0 +1,3 @@
1
+ export declare function _runWithTimeout<T>(timeout_seconds: number | null, on_timeout: () => Error, fn: () => Promise<T>): Promise<T>;
2
+ export declare function _runWithSlowMonitor<T>(slow_timer: ReturnType<typeof setTimeout> | null, fn: () => Promise<T>): Promise<T>;
3
+ export declare function _runWithAbortMonitor<T>(fn: () => T | Promise<T>, abort_signal: Promise<never>): Promise<T>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,36 @@
1
+ import { z } from 'zod';
2
+ import type { BaseEvent } from './base_event.js';
3
+ export type EventStatus = 'pending' | 'started' | 'completed';
4
+ export type EventClass<T extends BaseEvent = BaseEvent> = {
5
+ event_type?: string;
6
+ } & (new (...args: any[]) => T);
7
+ export type EventPattern<T extends BaseEvent = BaseEvent> = string | EventClass<T>;
8
+ export type EventWithResultSchema<TResult> = BaseEvent & {
9
+ __event_result_type__?: TResult;
10
+ };
11
+ export type EventResultType<TEvent extends BaseEvent> = TEvent extends {
12
+ __event_result_type__?: infer TResult;
13
+ } ? TResult : unknown;
14
+ export type EventResultTypeConstructor = StringConstructor | NumberConstructor | BooleanConstructor | ArrayConstructor | ObjectConstructor;
15
+ export type EventResultTypeInput = z.ZodTypeAny | EventResultTypeConstructor | unknown;
16
+ export type EventHandlerReturn<T extends BaseEvent = BaseEvent> = EventResultType<T> | BaseEvent | null | void;
17
+ export type EventHandlerCallable<T extends BaseEvent = BaseEvent> = (event: T) => EventHandlerReturn<T> | Promise<EventHandlerReturn<T>>;
18
+ export type UntypedEventHandlerFunction<T extends BaseEvent = BaseEvent> = (event: T) => EventHandlerReturn<T> | unknown | Promise<EventHandlerReturn<T> | unknown>;
19
+ export type FindWindow = boolean | number;
20
+ type FindReservedOptionKeys = 'past' | 'future' | 'child_of';
21
+ type EventFilterFields<T extends BaseEvent> = {
22
+ [K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K extends FindReservedOptionKeys ? never : T[K] extends (...args: any[]) => any ? never : K]?: T[K];
23
+ };
24
+ export type FindOptions<T extends BaseEvent = BaseEvent> = {
25
+ past?: FindWindow;
26
+ future?: FindWindow;
27
+ child_of?: BaseEvent | null;
28
+ } & EventFilterFields<T> & Record<string, unknown>;
29
+ export declare const normalizeEventPattern: (event_pattern: EventPattern | "*") => string | "*";
30
+ export declare const isZodSchema: (value: unknown) => value is z.ZodTypeAny;
31
+ export declare const eventResultTypeFromConstructor: (value: unknown) => z.ZodTypeAny | undefined;
32
+ export declare const extractZodShape: (raw: Record<string, unknown>) => z.ZodRawShape;
33
+ export declare const toJsonSchema: (schema: unknown) => unknown;
34
+ export declare const fromJsonSchema: (schema: unknown) => z.ZodTypeAny;
35
+ export declare const normalizeEventResultType: (value: EventResultTypeInput) => z.ZodTypeAny | undefined;
36
+ export {};
package/package.json ADDED
@@ -0,0 +1,87 @@
1
+ {
2
+ "name": "abxbus",
3
+ "version": "2.4.1",
4
+ "description": "Event bus library for browsers and ESM Node.js",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "./dist/cjs/index.js",
8
+ "module": "./dist/esm/index.js",
9
+ "types": "./dist/types/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/types/index.d.ts",
13
+ "import": "./dist/esm/index.js",
14
+ "require": "./dist/cjs/index.js",
15
+ "default": "./dist/esm/index.js"
16
+ },
17
+ "./*": {
18
+ "types": "./dist/types/*.d.ts",
19
+ "import": "./dist/esm/*.js",
20
+ "require": "./dist/cjs/*.js",
21
+ "default": "./dist/esm/*.js"
22
+ }
23
+ },
24
+ "files": [
25
+ "dist/cjs",
26
+ "dist/esm",
27
+ "dist/types"
28
+ ],
29
+ "keywords": [],
30
+ "author": "",
31
+ "license": "MIT",
32
+ "dependencies": {
33
+ "uuid": "^13.0.0",
34
+ "zod": "^4.3.6"
35
+ },
36
+ "devDependencies": {
37
+ "@types/node": "^25.2.3",
38
+ "@typescript-eslint/eslint-plugin": "^8.55.0",
39
+ "@typescript-eslint/parser": "^8.55.0",
40
+ "esbuild": "^0.27.3",
41
+ "eslint": "^9.39.2",
42
+ "prettier": "^3.8.1",
43
+ "tsc-files": "^1.1.4",
44
+ "tsx": "^4.21.0",
45
+ "typescript": "^5.9.3"
46
+ },
47
+ "repository": {
48
+ "type": "git",
49
+ "url": "git+https://github.com/ArchiveBox/abx-bus.git",
50
+ "directory": "abxbus-ts"
51
+ },
52
+ "bugs": {
53
+ "url": "https://github.com/ArchiveBox/abx-bus/issues"
54
+ },
55
+ "homepage": "https://abxbus.archivebox.io",
56
+ "publishConfig": {
57
+ "access": "public",
58
+ "registry": "https://registry.npmjs.org/"
59
+ },
60
+ "optionalDependencies": {
61
+ "ioredis": "^5.9.3",
62
+ "nats": "^2.29.3",
63
+ "pg": "^8.18.0"
64
+ },
65
+ "scripts": {
66
+ "build": "pnpm run build:esm && pnpm run build:cjs && pnpm run build:types && pnpm run build:cjs:types",
67
+ "build:esm": "node ./scripts/build_esm.mjs",
68
+ "build:cjs": "node ./scripts/build_cjs.mjs",
69
+ "build:cjs:types": "node ./scripts/copy_types_to_cjs.mjs",
70
+ "build:types": "tsc -p tsconfig.json --emitDeclarationOnly",
71
+ "typecheck": "tsc -p tsconfig.typecheck.json",
72
+ "prettier": "prettier --write .",
73
+ "eslint": "eslint .",
74
+ "lint": "pnpm run prettier && pnpm run eslint && pnpm run typecheck",
75
+ "test": "NODE_OPTIONS='--expose-gc' node --expose-gc --test --import tsx \"tests/**/*.test.ts\"",
76
+ "perf": "pnpm run perf:node && pnpm run perf:bun && pnpm run perf:deno && pnpm run perf:browser",
77
+ "debug:node": "NODE_OPTIONS='--expose-gc' node --expose-gc --import tsx",
78
+ "debug:bun": "bun --expose-gc run",
79
+ "debug:deno": "deno run --sloppy-imports --v8-flags=--expose-gc",
80
+ "perf:node": "pnpm run build && pnpm run debug:node -- tests/performance.runtime.ts --scenario 50k-events && pnpm run debug:node -- tests/performance.runtime.ts --scenario 500-buses-x-100-events && pnpm run debug:node -- tests/performance.runtime.ts --scenario 1-event-x-50k-parallel-handlers && pnpm run debug:node -- tests/performance.runtime.ts --scenario 50k-one-off-handlers && pnpm run debug:node -- tests/performance.runtime.ts --scenario worst-case-forwarding-timeouts && pnpm run debug:node -- tests/performance.runtime.ts --scenario cleanup-equivalence",
81
+ "perf:bun": "pnpm run build && pnpm run debug:bun -- tests/performance.runtime.ts --scenario 50k-events && pnpm run debug:bun -- tests/performance.runtime.ts --scenario 500-buses-x-100-events && pnpm run debug:bun -- tests/performance.runtime.ts --scenario 1-event-x-50k-parallel-handlers && pnpm run debug:bun -- tests/performance.runtime.ts --scenario 50k-one-off-handlers && pnpm run debug:bun -- tests/performance.runtime.ts --scenario worst-case-forwarding-timeouts && pnpm run debug:bun -- tests/performance.runtime.ts --scenario cleanup-equivalence",
82
+ "perf:deno": "pnpm run build && pnpm run debug:deno -- tests/performance.runtime.ts --scenario 50k-events && pnpm run debug:deno -- tests/performance.runtime.ts --scenario 500-buses-x-100-events && pnpm run debug:deno -- tests/performance.runtime.ts --scenario 1-event-x-50k-parallel-handlers && pnpm run debug:deno -- tests/performance.runtime.ts --scenario 50k-one-off-handlers && pnpm run debug:deno -- tests/performance.runtime.ts --scenario worst-case-forwarding-timeouts && pnpm run debug:deno -- tests/performance.runtime.ts --scenario cleanup-equivalence",
83
+ "perf:browser": "pnpm run build && bash ./scripts/run_browser_perf.sh",
84
+ "release:dry-run": "pnpm publish --access public --dry-run --no-git-checks",
85
+ "release:check": "pnpm run typecheck && pnpm test && pnpm run build"
86
+ }
87
+ }