@azure-net/kit 3.1.0 → 3.9.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/README.md +2 -2
- package/dist/core/delivery/injectableDependencies/AsyncHelpers.d.ts +8 -2
- package/dist/core/delivery/injectableDependencies/AsyncHelpers.js +32 -10
- package/dist/core/delivery/schema/Schema.js +1 -1
- package/dist/core/delivery/serverAction/CreateServerAction.js +1 -1
- package/dist/core/infra/datasource/BaseDatasource.d.ts +11 -11
- package/dist/core/infra/datasource/BaseDatasource.js +4 -4
- package/dist/core/infra/httpService/HttpServiceInstance.d.ts +82 -0
- package/dist/core/infra/httpService/HttpServiceInstance.js +342 -0
- package/dist/core/infra/httpService/index.d.ts +1 -1
- package/dist/core/infra/httpService/index.js +1 -1
- package/dist/core/infra/query/QueryBuilder.d.ts +9 -21
- package/dist/core/infra/query/QueryBuilder.js +62 -50
- package/dist/core/shared/appEventBus/EventBus.d.ts +1 -1
- package/dist/core/shared/appEventBus/EventBus.js +2 -2
- package/dist/core/shared/boundaryProvider/Provider.d.ts +2 -2
- package/dist/core/shared/boundaryProvider/Provider.js +14 -8
- package/dist/core/shared/cookie/UniversalCookie.js +2 -2
- package/dist/core/shared/middleware/Middleware.js +12 -8
- package/dist/core/svelte/ActiveForm/ActiveForm.svelte.d.ts +4 -1
- package/dist/core/svelte/ActiveForm/ActiveForm.svelte.js +19 -18
- package/dist/core/svelte/AsyncSignal/AsyncSignal.svelte.js +1 -1
- package/dist/core/svelte/Query/Query.svelte.js +1 -1
- package/dist/core/ui/onClickOutside/OnClickOutside.js +1 -1
- package/dist/core/ui/scrollTo/index.js +1 -1
- package/dist/edges/context/index.d.ts +1 -1
- package/dist/edges/context/index.js +1 -1
- package/dist/edges/dev/index.d.ts +1 -1
- package/dist/edges/dev/index.js +1 -1
- package/dist/edges/index.d.ts +1 -1
- package/dist/edges/index.js +1 -1
- package/dist/edges/plugin/index.d.ts +1 -1
- package/dist/edges/plugin/index.js +1 -1
- package/dist/edges/server/index.d.ts +1 -1
- package/dist/edges/server/index.js +1 -1
- package/dist/edges/state/index.d.ts +1 -1
- package/dist/edges/state/index.js +1 -1
- package/dist/i18n/index.d.ts +1 -1
- package/dist/i18n/index.js +1 -1
- package/dist/tools/index.d.ts +2 -1
- package/dist/tools/index.js +2 -1
- package/package.json +36 -42
- package/dist/core/infra/httpService/HttpService.d.ts +0 -74
- package/dist/core/infra/httpService/HttpService.js +0 -164
- package/dist/core/shared/eventBus/EventBus.d.ts +0 -87
- package/dist/core/shared/eventBus/EventBus.js +0 -227
- package/dist/core/shared/eventBus/index.d.ts +0 -1
- package/dist/core/shared/eventBus/index.js +0 -1
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { type Options } from 'ky';
|
|
2
|
-
export interface IHttpServiceResponse<T = unknown> {
|
|
3
|
-
headers: Record<string, string>;
|
|
4
|
-
status: number;
|
|
5
|
-
success: boolean;
|
|
6
|
-
data: T;
|
|
7
|
-
message: string;
|
|
8
|
-
}
|
|
9
|
-
export interface IHttpServiceError<T = unknown> extends IHttpServiceResponse<T> {
|
|
10
|
-
original?: Error;
|
|
11
|
-
}
|
|
12
|
-
export declare class HttpServiceResponse<T> implements IHttpServiceResponse<T> {
|
|
13
|
-
headers: Record<string, string>;
|
|
14
|
-
status: number;
|
|
15
|
-
success: boolean;
|
|
16
|
-
data: T;
|
|
17
|
-
message: string;
|
|
18
|
-
constructor({ headers, status, success, data, message }: IHttpServiceResponse<T>);
|
|
19
|
-
}
|
|
20
|
-
export declare class HttpServiceError<T> implements IHttpServiceError<T> {
|
|
21
|
-
headers: Record<string, string>;
|
|
22
|
-
status: number;
|
|
23
|
-
success: boolean;
|
|
24
|
-
data: T;
|
|
25
|
-
message: string;
|
|
26
|
-
original?: Error;
|
|
27
|
-
constructor({ headers, status, success, data, message, original }: IHttpServiceError<T>);
|
|
28
|
-
}
|
|
29
|
-
export interface IHttpServiceOptions extends Options {
|
|
30
|
-
responseFormat?: 'json' | 'blob' | 'text' | 'arrayBuffer' | 'body';
|
|
31
|
-
}
|
|
32
|
-
export interface IHttpServiceInstanceResponse<T = unknown> extends Response {
|
|
33
|
-
arrayBuffer: () => Promise<ArrayBuffer>;
|
|
34
|
-
blob: () => Promise<Blob>;
|
|
35
|
-
formData: () => Promise<FormData>;
|
|
36
|
-
json: <J = T>() => Promise<J>;
|
|
37
|
-
text: () => Promise<string>;
|
|
38
|
-
}
|
|
39
|
-
export interface IHttpServiceInstance {
|
|
40
|
-
get: <T>(url: string | URL | Request, options?: IHttpServiceOptions) => Promise<IHttpServiceInstanceResponse<T>>;
|
|
41
|
-
post: <T>(url: string | URL | Request, options?: IHttpServiceOptions) => Promise<IHttpServiceInstanceResponse<T>>;
|
|
42
|
-
put: <T>(url: string | URL | Request, options?: IHttpServiceOptions) => Promise<IHttpServiceInstanceResponse<T>>;
|
|
43
|
-
delete: <T>(url: string | URL | Request, options?: IHttpServiceOptions) => Promise<IHttpServiceInstanceResponse<T>>;
|
|
44
|
-
patch: <T>(url: string | URL | Request, options?: IHttpServiceOptions) => Promise<IHttpServiceInstanceResponse<T>>;
|
|
45
|
-
head?: (url: string | URL | Request, options?: IHttpServiceOptions) => Promise<IHttpServiceInstanceResponse>;
|
|
46
|
-
create: (defaultOptions?: IHttpServiceOptions) => IHttpServiceInstance;
|
|
47
|
-
extend?: (defaultOptions: IHttpServiceOptions | ((parentOptions: IHttpServiceOptions) => IHttpServiceOptions)) => IHttpServiceInstance;
|
|
48
|
-
readonly stop: typeof stop;
|
|
49
|
-
}
|
|
50
|
-
export declare class HttpService {
|
|
51
|
-
private baseUrl;
|
|
52
|
-
private readonly instance;
|
|
53
|
-
private readonly requestHandler?;
|
|
54
|
-
private readonly errorHandler?;
|
|
55
|
-
private readonly globalErrorHandler?;
|
|
56
|
-
constructor(opts: {
|
|
57
|
-
baseUrl: string;
|
|
58
|
-
instance?: IHttpServiceInstance;
|
|
59
|
-
baseOptions?: Options;
|
|
60
|
-
onRequest?: (options: Options) => void | Promise<void>;
|
|
61
|
-
onError?: (err: IHttpServiceError) => HttpServiceError<unknown>;
|
|
62
|
-
handleError?: (err: unknown) => Promise<HttpServiceError<unknown>>;
|
|
63
|
-
});
|
|
64
|
-
setBaseUrl(newBaseUrl: string): void;
|
|
65
|
-
private prepareOptions;
|
|
66
|
-
private prepareResponse;
|
|
67
|
-
post<T>(url: string, options?: IHttpServiceOptions): Promise<IHttpServiceResponse<T>>;
|
|
68
|
-
get<T>(url: string, options?: IHttpServiceOptions): Promise<IHttpServiceResponse<T>>;
|
|
69
|
-
patch<T>(url: string, options?: IHttpServiceOptions): Promise<IHttpServiceResponse<T>>;
|
|
70
|
-
delete<T>(url: string, options?: IHttpServiceOptions): Promise<IHttpServiceResponse<T>>;
|
|
71
|
-
put<T>(url: string, options?: IHttpServiceOptions): Promise<IHttpServiceResponse<T>>;
|
|
72
|
-
private handleError;
|
|
73
|
-
private baseErrorHandler;
|
|
74
|
-
}
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import ky, {} from 'ky';
|
|
2
|
-
export class HttpServiceResponse {
|
|
3
|
-
headers;
|
|
4
|
-
status;
|
|
5
|
-
success;
|
|
6
|
-
data;
|
|
7
|
-
message;
|
|
8
|
-
constructor({ headers, status, success, data, message }) {
|
|
9
|
-
this.headers = headers;
|
|
10
|
-
this.status = status;
|
|
11
|
-
this.success = success;
|
|
12
|
-
this.data = data;
|
|
13
|
-
this.message = message;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
export class HttpServiceError {
|
|
17
|
-
headers;
|
|
18
|
-
status;
|
|
19
|
-
success;
|
|
20
|
-
data;
|
|
21
|
-
message;
|
|
22
|
-
original;
|
|
23
|
-
constructor({ headers, status, success, data, message, original }) {
|
|
24
|
-
this.headers = headers;
|
|
25
|
-
this.status = status;
|
|
26
|
-
this.success = success;
|
|
27
|
-
this.data = data;
|
|
28
|
-
this.message = message;
|
|
29
|
-
this.original = original;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
export class HttpService {
|
|
33
|
-
baseUrl;
|
|
34
|
-
instance;
|
|
35
|
-
requestHandler;
|
|
36
|
-
errorHandler;
|
|
37
|
-
globalErrorHandler;
|
|
38
|
-
constructor(opts) {
|
|
39
|
-
this.baseUrl = opts.baseUrl;
|
|
40
|
-
this.instance = opts.instance ? opts.instance.create(opts?.baseOptions) : ky.create(opts?.baseOptions);
|
|
41
|
-
this.errorHandler = opts?.onError ?? undefined;
|
|
42
|
-
this.requestHandler = opts?.onRequest ?? undefined;
|
|
43
|
-
this.globalErrorHandler = opts?.handleError ?? undefined;
|
|
44
|
-
}
|
|
45
|
-
setBaseUrl(newBaseUrl) {
|
|
46
|
-
this.baseUrl = newBaseUrl;
|
|
47
|
-
}
|
|
48
|
-
async prepareOptions(requestOptions) {
|
|
49
|
-
const options = typeof requestOptions === 'object' ? { ...requestOptions } : {};
|
|
50
|
-
if (this.requestHandler) {
|
|
51
|
-
await this.requestHandler(options);
|
|
52
|
-
}
|
|
53
|
-
return options;
|
|
54
|
-
}
|
|
55
|
-
async prepareResponse(request, format = 'json') {
|
|
56
|
-
let data;
|
|
57
|
-
try {
|
|
58
|
-
switch (format) {
|
|
59
|
-
case 'blob':
|
|
60
|
-
data = (await request.blob());
|
|
61
|
-
break;
|
|
62
|
-
case 'text':
|
|
63
|
-
data = (await request.text());
|
|
64
|
-
break;
|
|
65
|
-
case 'arrayBuffer':
|
|
66
|
-
data = (await request.arrayBuffer());
|
|
67
|
-
break;
|
|
68
|
-
case 'body':
|
|
69
|
-
data = request.body;
|
|
70
|
-
break;
|
|
71
|
-
case 'json':
|
|
72
|
-
data = (await request.json());
|
|
73
|
-
break;
|
|
74
|
-
default:
|
|
75
|
-
data = (await request.json());
|
|
76
|
-
break;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
catch {
|
|
80
|
-
data = null;
|
|
81
|
-
}
|
|
82
|
-
return new HttpServiceResponse({
|
|
83
|
-
headers: request.headers instanceof Headers ? Object.fromEntries(request.headers.entries()) : {},
|
|
84
|
-
status: request.status,
|
|
85
|
-
success: request.status < 400,
|
|
86
|
-
data,
|
|
87
|
-
message: 'Request completed'
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
async post(url, options) {
|
|
91
|
-
return await this.instance
|
|
92
|
-
.post(this.baseUrl + url, await this.prepareOptions(options))
|
|
93
|
-
.then((response) => {
|
|
94
|
-
return this.prepareResponse(response, options?.responseFormat);
|
|
95
|
-
})
|
|
96
|
-
.catch(async (error) => {
|
|
97
|
-
throw await this.handleError(error);
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
async get(url, options) {
|
|
101
|
-
return await this.instance
|
|
102
|
-
.get(this.baseUrl + url, await this.prepareOptions(options))
|
|
103
|
-
.then((response) => {
|
|
104
|
-
return this.prepareResponse(response, options?.responseFormat);
|
|
105
|
-
})
|
|
106
|
-
.catch(async (error) => {
|
|
107
|
-
throw await this.handleError(error);
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
async patch(url, options) {
|
|
111
|
-
return await this.instance
|
|
112
|
-
.patch(this.baseUrl + url, await this.prepareOptions(options))
|
|
113
|
-
.then((response) => {
|
|
114
|
-
return this.prepareResponse(response, options?.responseFormat);
|
|
115
|
-
})
|
|
116
|
-
.catch(async (error) => {
|
|
117
|
-
throw await this.handleError(error);
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
async delete(url, options) {
|
|
121
|
-
return await this.instance
|
|
122
|
-
.delete(this.baseUrl + url, await this.prepareOptions(options))
|
|
123
|
-
.then((response) => {
|
|
124
|
-
return this.prepareResponse(response, options?.responseFormat);
|
|
125
|
-
})
|
|
126
|
-
.catch(async (error) => {
|
|
127
|
-
throw await this.handleError(error);
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
async put(url, options) {
|
|
131
|
-
return await this.instance
|
|
132
|
-
.put(this.baseUrl + url, await this.prepareOptions(options))
|
|
133
|
-
.then((response) => {
|
|
134
|
-
return this.prepareResponse(response, options?.responseFormat);
|
|
135
|
-
})
|
|
136
|
-
.catch(async (error) => {
|
|
137
|
-
throw await this.handleError(error);
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
async handleError(error) {
|
|
141
|
-
return this.globalErrorHandler ? await this.globalErrorHandler(error) : await this.baseErrorHandler(error);
|
|
142
|
-
}
|
|
143
|
-
async baseErrorHandler(err) {
|
|
144
|
-
let headers;
|
|
145
|
-
let response;
|
|
146
|
-
try {
|
|
147
|
-
headers = err?.response?.headers && err?.response?.headers instanceof Headers ? Object.fromEntries(err?.response?.headers.entries()) : {};
|
|
148
|
-
response = await err?.response?.json();
|
|
149
|
-
}
|
|
150
|
-
catch {
|
|
151
|
-
headers = {};
|
|
152
|
-
response = {};
|
|
153
|
-
}
|
|
154
|
-
const error = new HttpServiceError({
|
|
155
|
-
data: response,
|
|
156
|
-
status: err?.response?.status ?? 500,
|
|
157
|
-
headers,
|
|
158
|
-
success: false,
|
|
159
|
-
original: err,
|
|
160
|
-
message: 'Request failed'
|
|
161
|
-
});
|
|
162
|
-
return this.errorHandler ? this.errorHandler(error) : error;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
export interface IEvent<T = unknown> {
|
|
2
|
-
aggregateId: string;
|
|
3
|
-
eventType: string;
|
|
4
|
-
eventVersion: number;
|
|
5
|
-
occurredAt: Date;
|
|
6
|
-
metadata: IEventMetadata;
|
|
7
|
-
busName?: string;
|
|
8
|
-
payload: T;
|
|
9
|
-
}
|
|
10
|
-
export interface IEventMetadata {
|
|
11
|
-
userId?: string;
|
|
12
|
-
correlationId: string;
|
|
13
|
-
causationId?: string;
|
|
14
|
-
sessionId?: string;
|
|
15
|
-
[key: string]: unknown;
|
|
16
|
-
}
|
|
17
|
-
export type DomainEventHandler<T = unknown> = (event: IEvent<T>) => void | Promise<void>;
|
|
18
|
-
export interface IEventSubscription {
|
|
19
|
-
unsubscribe(): void;
|
|
20
|
-
}
|
|
21
|
-
export declare class EventBus<TEventMap extends object> {
|
|
22
|
-
private options;
|
|
23
|
-
private handlers;
|
|
24
|
-
private wildcardHandlers;
|
|
25
|
-
private middleware;
|
|
26
|
-
private eventQueue;
|
|
27
|
-
private isProcessing;
|
|
28
|
-
private eventHistory;
|
|
29
|
-
private priorities;
|
|
30
|
-
private readonly debug;
|
|
31
|
-
private readonly maxHistorySize;
|
|
32
|
-
private readonly busName?;
|
|
33
|
-
constructor(options?: {
|
|
34
|
-
busName?: string;
|
|
35
|
-
enableHistory?: boolean;
|
|
36
|
-
maxHistorySize?: number;
|
|
37
|
-
enableAsyncProcessing?: boolean;
|
|
38
|
-
debug?: boolean;
|
|
39
|
-
});
|
|
40
|
-
publish<K extends keyof TEventMap>(eventType: K, payload: TEventMap[K], metadata?: Partial<IEventMetadata>): void;
|
|
41
|
-
publishBatch<K extends keyof TEventMap>(events: Array<{
|
|
42
|
-
type: K;
|
|
43
|
-
payload: TEventMap[K];
|
|
44
|
-
metadata?: Partial<IEventMetadata>;
|
|
45
|
-
}>): void;
|
|
46
|
-
subscribe<K extends keyof TEventMap>(eventType: K, handler: DomainEventHandler<TEventMap[K]>, options?: {
|
|
47
|
-
priority?: number;
|
|
48
|
-
}): IEventSubscription;
|
|
49
|
-
subscribeMany<K extends keyof TEventMap>(eventTypes: K[], handler: DomainEventHandler<TEventMap[K]>): IEventSubscription;
|
|
50
|
-
subscribeAll(handler: DomainEventHandler): IEventSubscription;
|
|
51
|
-
use(middleware: <T = unknown>(event: IEvent<T>, next: () => Promise<void>) => Promise<void>): void;
|
|
52
|
-
private processEvent;
|
|
53
|
-
private executeHandlers;
|
|
54
|
-
private safeExecuteHandler;
|
|
55
|
-
private getHandlersForEvent;
|
|
56
|
-
private processQueue;
|
|
57
|
-
private addToHistory;
|
|
58
|
-
getHistory(filter?: {
|
|
59
|
-
eventType?: keyof TEventMap;
|
|
60
|
-
aggregateId?: string;
|
|
61
|
-
}): IEvent[];
|
|
62
|
-
waitForCompletion(): Promise<void>;
|
|
63
|
-
clear(): void;
|
|
64
|
-
private generateAggregateId;
|
|
65
|
-
private generateCorrelationId;
|
|
66
|
-
}
|
|
67
|
-
export declare const loggingMiddleware: (event: IEvent, next: () => Promise<void>) => Promise<void>;
|
|
68
|
-
export declare const createEventBus: <TEventMap extends object>(opts: {
|
|
69
|
-
history?: boolean;
|
|
70
|
-
asyncProcessing?: boolean;
|
|
71
|
-
historySize?: number;
|
|
72
|
-
middlewares?: (<T = unknown>(event: IEvent<T>, next: () => Promise<void>) => Promise<void>)[];
|
|
73
|
-
debug?: boolean;
|
|
74
|
-
busName?: string;
|
|
75
|
-
}) => {
|
|
76
|
-
eventBus: EventBus<TEventMap>;
|
|
77
|
-
CreateEvent: <K extends keyof TEventMap>(eventName: K, data: TEventMap[K]) => void;
|
|
78
|
-
CreateBatch: <K extends keyof TEventMap>(events: Array<{
|
|
79
|
-
type: K;
|
|
80
|
-
payload: TEventMap[K];
|
|
81
|
-
metadata?: Partial<IEventMetadata>;
|
|
82
|
-
}>) => void;
|
|
83
|
-
CreateSubscriber: <K extends keyof TEventMap>(eventType: K, handler: DomainEventHandler<TEventMap[K]>, options?: {
|
|
84
|
-
priority?: number;
|
|
85
|
-
}) => IEventSubscription;
|
|
86
|
-
CreateSubscribers: <K extends keyof TEventMap>(eventTypes: K[], handler: DomainEventHandler<TEventMap[K]>) => IEventSubscription;
|
|
87
|
-
};
|
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
export class EventBus {
|
|
2
|
-
options;
|
|
3
|
-
handlers = new Map();
|
|
4
|
-
wildcardHandlers = new Set();
|
|
5
|
-
middleware = [];
|
|
6
|
-
eventQueue = [];
|
|
7
|
-
isProcessing = false;
|
|
8
|
-
eventHistory = [];
|
|
9
|
-
priorities = new Map();
|
|
10
|
-
debug = false;
|
|
11
|
-
maxHistorySize = 1000;
|
|
12
|
-
busName;
|
|
13
|
-
constructor(options = { debug: false }) {
|
|
14
|
-
this.options = options;
|
|
15
|
-
this.busName = options.busName ?? undefined;
|
|
16
|
-
this.debug = options.debug ?? false;
|
|
17
|
-
if (options.maxHistorySize) {
|
|
18
|
-
this.maxHistorySize = options.maxHistorySize;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
publish(eventType, payload, metadata) {
|
|
22
|
-
const event = {
|
|
23
|
-
aggregateId: this.generateAggregateId(String(eventType), payload),
|
|
24
|
-
eventType: String(eventType),
|
|
25
|
-
eventVersion: 1,
|
|
26
|
-
occurredAt: new Date(),
|
|
27
|
-
metadata: {
|
|
28
|
-
correlationId: this.generateCorrelationId(),
|
|
29
|
-
...metadata
|
|
30
|
-
},
|
|
31
|
-
busName: this.busName,
|
|
32
|
-
payload
|
|
33
|
-
};
|
|
34
|
-
void this.processEvent(event);
|
|
35
|
-
}
|
|
36
|
-
publishBatch(events) {
|
|
37
|
-
const domainEvents = events.map(({ type, payload, metadata }) => ({
|
|
38
|
-
aggregateId: this.generateAggregateId(type, payload),
|
|
39
|
-
eventType: type,
|
|
40
|
-
eventVersion: 1,
|
|
41
|
-
occurredAt: new Date(),
|
|
42
|
-
busName: this.busName,
|
|
43
|
-
metadata: {
|
|
44
|
-
correlationId: this.generateCorrelationId(),
|
|
45
|
-
...metadata
|
|
46
|
-
},
|
|
47
|
-
payload
|
|
48
|
-
}));
|
|
49
|
-
domainEvents.forEach((event) => this.processEvent(event));
|
|
50
|
-
}
|
|
51
|
-
subscribe(eventType, handler, options) {
|
|
52
|
-
if (!this.handlers.has(eventType)) {
|
|
53
|
-
this.handlers.set(eventType, new Set());
|
|
54
|
-
}
|
|
55
|
-
const handlers = this.handlers.get(eventType);
|
|
56
|
-
handlers.add(handler);
|
|
57
|
-
if (options?.priority !== undefined) {
|
|
58
|
-
this.priorities.set(eventType, options.priority);
|
|
59
|
-
}
|
|
60
|
-
return {
|
|
61
|
-
unsubscribe: () => {
|
|
62
|
-
handlers.delete(handler);
|
|
63
|
-
if (handlers.size === 0) {
|
|
64
|
-
this.handlers.delete(eventType);
|
|
65
|
-
this.priorities.delete(eventType);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
subscribeMany(eventTypes, handler) {
|
|
71
|
-
const subscriptions = eventTypes.map((type) => this.subscribe(type, handler));
|
|
72
|
-
return {
|
|
73
|
-
unsubscribe: () => {
|
|
74
|
-
subscriptions.forEach((sub) => sub.unsubscribe());
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
subscribeAll(handler) {
|
|
79
|
-
this.wildcardHandlers.add(handler);
|
|
80
|
-
return {
|
|
81
|
-
unsubscribe: () => {
|
|
82
|
-
this.wildcardHandlers.delete(handler);
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
use(middleware) {
|
|
87
|
-
this.middleware.push(middleware);
|
|
88
|
-
}
|
|
89
|
-
async processEvent(event) {
|
|
90
|
-
if (this.options.enableHistory) {
|
|
91
|
-
this.addToHistory(event);
|
|
92
|
-
}
|
|
93
|
-
const handlers = this.getHandlersForEvent(event);
|
|
94
|
-
if (!this.debug && handlers.size === 0 && this.wildcardHandlers.size === 0) {
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
if (this.options.enableAsyncProcessing) {
|
|
98
|
-
this.eventQueue.push({ event, handlers });
|
|
99
|
-
if (!this.isProcessing) {
|
|
100
|
-
void this.processQueue();
|
|
101
|
-
}
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
await this.executeHandlers(event, handlers);
|
|
105
|
-
}
|
|
106
|
-
async executeHandlers(event, handlers) {
|
|
107
|
-
const allHandlers = [...handlers, ...this.wildcardHandlers];
|
|
108
|
-
let index = -1;
|
|
109
|
-
const dispatch = async (i) => {
|
|
110
|
-
if (i <= index) {
|
|
111
|
-
throw new Error('next() called multiple times');
|
|
112
|
-
}
|
|
113
|
-
index = i;
|
|
114
|
-
if (i === this.middleware.length) {
|
|
115
|
-
await Promise.all(allHandlers.map((handler) => this.safeExecuteHandler(handler, event)));
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
const middleware = this.middleware[i];
|
|
119
|
-
await middleware(event, () => dispatch(i + 1));
|
|
120
|
-
};
|
|
121
|
-
await dispatch(0);
|
|
122
|
-
}
|
|
123
|
-
async safeExecuteHandler(handler, event) {
|
|
124
|
-
try {
|
|
125
|
-
await handler(event);
|
|
126
|
-
}
|
|
127
|
-
catch (error) {
|
|
128
|
-
console.error(`Error handling event ${event.eventType}:`, error);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
getHandlersForEvent(event) {
|
|
132
|
-
const handlers = this.handlers.get(event.eventType);
|
|
133
|
-
return handlers || new Set();
|
|
134
|
-
}
|
|
135
|
-
async processQueue() {
|
|
136
|
-
if (this.isProcessing || this.eventQueue.length === 0) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
this.isProcessing = true;
|
|
140
|
-
this.eventQueue.sort((a, b) => {
|
|
141
|
-
const priorityA = this.priorities.get(a.event.eventType) || 0;
|
|
142
|
-
const priorityB = this.priorities.get(b.event.eventType) || 0;
|
|
143
|
-
return priorityB - priorityA;
|
|
144
|
-
});
|
|
145
|
-
while (this.eventQueue.length > 0) {
|
|
146
|
-
const batch = this.eventQueue.splice(0, 10);
|
|
147
|
-
await Promise.all(batch.map(({ event, handlers }) => this.executeHandlers(event, handlers)));
|
|
148
|
-
}
|
|
149
|
-
this.isProcessing = false;
|
|
150
|
-
}
|
|
151
|
-
addToHistory(event) {
|
|
152
|
-
this.eventHistory.push(event);
|
|
153
|
-
if (this.eventHistory.length > this.maxHistorySize) {
|
|
154
|
-
this.eventHistory.shift();
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
getHistory(filter) {
|
|
158
|
-
if (!filter)
|
|
159
|
-
return [...this.eventHistory];
|
|
160
|
-
return this.eventHistory.filter((event) => {
|
|
161
|
-
switch (true) {
|
|
162
|
-
case filter.eventType && event.eventType !== filter.eventType:
|
|
163
|
-
return false;
|
|
164
|
-
case filter.aggregateId && event.aggregateId !== filter.aggregateId:
|
|
165
|
-
return false;
|
|
166
|
-
default:
|
|
167
|
-
return true;
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
async waitForCompletion() {
|
|
172
|
-
while (this.isProcessing || this.eventQueue.length > 0) {
|
|
173
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
clear() {
|
|
177
|
-
this.handlers.clear();
|
|
178
|
-
this.wildcardHandlers.clear();
|
|
179
|
-
this.eventQueue = [];
|
|
180
|
-
this.eventHistory = [];
|
|
181
|
-
}
|
|
182
|
-
generateAggregateId(eventType, payload) {
|
|
183
|
-
const maybeEventPayload = payload;
|
|
184
|
-
if (typeof maybeEventPayload === 'object' && maybeEventPayload?.id)
|
|
185
|
-
return String(maybeEventPayload.id);
|
|
186
|
-
if (typeof maybeEventPayload === 'object' && maybeEventPayload?.aggregateId)
|
|
187
|
-
return String(maybeEventPayload.aggregateId);
|
|
188
|
-
return `${eventType}-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
189
|
-
}
|
|
190
|
-
generateCorrelationId() {
|
|
191
|
-
return `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
export const loggingMiddleware = async (event, next) => {
|
|
195
|
-
console.log(`[${event.busName ?? ''}Event] ${event.eventType} at ${event.occurredAt.toISOString()}`);
|
|
196
|
-
console.log(`[${event.busName ?? ''}Event] data:`, event);
|
|
197
|
-
const start = performance.now();
|
|
198
|
-
await next();
|
|
199
|
-
const duration = performance.now() - start;
|
|
200
|
-
console.log(`[${event.busName ?? ''}Event] ${event.eventType} processed in ${duration.toFixed(2)}ms`);
|
|
201
|
-
};
|
|
202
|
-
export const createEventBus = (opts) => {
|
|
203
|
-
const { history = true, asyncProcessing = true, historySize = 1000, middlewares = [], debug = false } = opts;
|
|
204
|
-
const eventBus = new EventBus({
|
|
205
|
-
busName: opts.busName,
|
|
206
|
-
enableHistory: history,
|
|
207
|
-
enableAsyncProcessing: asyncProcessing,
|
|
208
|
-
maxHistorySize: historySize,
|
|
209
|
-
debug
|
|
210
|
-
});
|
|
211
|
-
middlewares.forEach((value) => {
|
|
212
|
-
eventBus.use(value);
|
|
213
|
-
});
|
|
214
|
-
const CreateEvent = (eventName, data) => {
|
|
215
|
-
eventBus.publish(eventName, data);
|
|
216
|
-
};
|
|
217
|
-
const CreateBatch = (events) => {
|
|
218
|
-
eventBus.publishBatch(events);
|
|
219
|
-
};
|
|
220
|
-
const CreateSubscriber = (eventType, handler, options) => {
|
|
221
|
-
return eventBus.subscribe(eventType, handler, options);
|
|
222
|
-
};
|
|
223
|
-
const CreateSubscribers = (eventTypes, handler) => {
|
|
224
|
-
return eventBus.subscribeMany(eventTypes, handler);
|
|
225
|
-
};
|
|
226
|
-
return { eventBus, CreateEvent, CreateBatch, CreateSubscriber, CreateSubscribers };
|
|
227
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './EventBus.js';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './EventBus.js';
|