@prosody-events/prosody 0.1.0
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/LICENSE +21 -0
- package/README.md +978 -0
- package/bindings.d.ts +522 -0
- package/bindings.js +587 -0
- package/index.d.ts +307 -0
- package/index.js +588 -0
- package/package.json +82 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module prosody-js
|
|
3
|
+
* @description A high-performance messaging client for Kafka with built-in OpenTelemetry support.
|
|
4
|
+
* Provides functionality for sending messages, subscribing to topics, and managing consumer state.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
Configuration,
|
|
9
|
+
ConsumerState,
|
|
10
|
+
Message,
|
|
11
|
+
Mode,
|
|
12
|
+
Timer,
|
|
13
|
+
} from "./bindings";
|
|
14
|
+
|
|
15
|
+
export { Configuration, ConsumerState, Message, Timer, Mode };
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Wrapper around `MessageContext` for use in Node.js bindings.
|
|
19
|
+
* Automatically injects OpenTelemetry context for all operations.
|
|
20
|
+
*/
|
|
21
|
+
export declare class Context {
|
|
22
|
+
/**
|
|
23
|
+
* Checks whether cancellation has been signaled.
|
|
24
|
+
* Cancellation includes message-level cancellation (e.g., timeout) and partition shutdown. During shutdown, cancellation is delayed until near the end of the shutdown timeout to allow in-flight work to complete.
|
|
25
|
+
*
|
|
26
|
+
* @returns True if cancellation was requested, otherwise false.
|
|
27
|
+
*/
|
|
28
|
+
get shouldCancel(): boolean;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Waits for a cancellation signal.
|
|
32
|
+
* Cancellation includes message-level cancellation (e.g., timeout) and partition shutdown. During shutdown, cancellation is delayed until near the end of the shutdown timeout to allow in-flight work to complete.
|
|
33
|
+
*
|
|
34
|
+
* @returns A promise that resolves when cancellation is signaled.
|
|
35
|
+
*/
|
|
36
|
+
onCancel(): Promise<void>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Schedule a timer at the given time.
|
|
40
|
+
*
|
|
41
|
+
* @param time - The UTC timestamp to schedule.
|
|
42
|
+
* @returns A promise that resolves when the timer has been scheduled.
|
|
43
|
+
* @throws Error if time conversion or scheduling fails.
|
|
44
|
+
*/
|
|
45
|
+
schedule(time: Date): Promise<void>;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Clear existing timers and schedule a new one at the given time.
|
|
49
|
+
*
|
|
50
|
+
* @param time - The UTC timestamp to schedule.
|
|
51
|
+
* @returns A promise that resolves when the timer has been scheduled.
|
|
52
|
+
* @throws Error if time conversion or scheduling fails.
|
|
53
|
+
*/
|
|
54
|
+
clearAndSchedule(time: Date): Promise<void>;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Unschedules the timer for the specified time.
|
|
58
|
+
* @param time - The time to unschedule.
|
|
59
|
+
* @returns A promise that resolves when the timer has been unscheduled.
|
|
60
|
+
* @throws Error if unscheduling fails.
|
|
61
|
+
*/
|
|
62
|
+
unschedule(time: Date): Promise<void>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Clears all scheduled timers.
|
|
66
|
+
* @returns A promise that resolves when all timers have been cleared.
|
|
67
|
+
* @throws Error if clearing schedules fails.
|
|
68
|
+
*/
|
|
69
|
+
clearScheduled(): Promise<void>;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Retrieves all scheduled times.
|
|
73
|
+
* @returns An array of scheduled times as Date objects.
|
|
74
|
+
* @throws Error if retrieval fails.
|
|
75
|
+
*/
|
|
76
|
+
scheduled(): Promise<Array<Date>>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* JavaScript logger interface for use with Prosody client.
|
|
81
|
+
*
|
|
82
|
+
* Each logging method receives a message string and optional metadata object.
|
|
83
|
+
*/
|
|
84
|
+
export interface Logger {
|
|
85
|
+
/** Logs error-level messages. */
|
|
86
|
+
error: (message: string | undefined | null, metadata?: any) => void;
|
|
87
|
+
/** Logs warning-level messages. */
|
|
88
|
+
warn: (message: string | undefined | null, metadata?: any) => void;
|
|
89
|
+
/** Logs info-level messages. */
|
|
90
|
+
info: (message: string | undefined | null, metadata?: any) => void;
|
|
91
|
+
/** Logs debug-level messages. */
|
|
92
|
+
debug: (message: string | undefined | null, metadata?: any) => void;
|
|
93
|
+
/** Logs trace-level messages. */
|
|
94
|
+
trace: (message: string | undefined | null, metadata?: any) => void;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface EventHandler {
|
|
98
|
+
/**
|
|
99
|
+
* Callback function to handle incoming messages.
|
|
100
|
+
*
|
|
101
|
+
* @param context - The context of the message processing.
|
|
102
|
+
* @param message - The received Kafka message.
|
|
103
|
+
* @param signal - An AbortSignal that can be used to cancel the message processing.
|
|
104
|
+
* @returns A promise that resolves when the message has been processed.
|
|
105
|
+
*/
|
|
106
|
+
onMessage?: (
|
|
107
|
+
context: Context,
|
|
108
|
+
message: Message,
|
|
109
|
+
signal: AbortSignal,
|
|
110
|
+
) => Promise<void>;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Callback function to handle timers.
|
|
114
|
+
*
|
|
115
|
+
* @param context - The context of the message processing.
|
|
116
|
+
* @param timer - The triggered timer.
|
|
117
|
+
* @param signal - An AbortSignal that can be used to cancel the message processing.
|
|
118
|
+
* @returns A promise that resolves when the timer has been processed.
|
|
119
|
+
*/
|
|
120
|
+
onTimer?: (
|
|
121
|
+
context: Context,
|
|
122
|
+
timer: Timer,
|
|
123
|
+
signal: AbortSignal,
|
|
124
|
+
) => Promise<void>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export declare class ProsodyClient {
|
|
128
|
+
/**
|
|
129
|
+
* Creates a new ProsodyClient instance.
|
|
130
|
+
*
|
|
131
|
+
* @param config - The configuration options for the client.
|
|
132
|
+
*/
|
|
133
|
+
constructor(config: Configuration);
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Gets the current state of the consumer.
|
|
137
|
+
*
|
|
138
|
+
* @returns The current state of the consumer.
|
|
139
|
+
* @throws Error if the operation fails.
|
|
140
|
+
*/
|
|
141
|
+
consumerState(): Promise<ConsumerState>;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Gets the number of partitions assigned to the consumer.
|
|
145
|
+
*
|
|
146
|
+
* @returns The number of assigned partitions, or 0 if the consumer is not in the Running state.
|
|
147
|
+
* @throws Error if the operation fails.
|
|
148
|
+
*/
|
|
149
|
+
assignedPartitionCount(): Promise<number>;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Checks if the consumer is stalled.
|
|
153
|
+
*
|
|
154
|
+
* @returns Whether the consumer is stalled, or false if the consumer is not in the Running state.
|
|
155
|
+
* @throws Error if the operation fails.
|
|
156
|
+
*/
|
|
157
|
+
isStalled(): Promise<boolean>;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Gets the source system identifier configured for the client.
|
|
161
|
+
*
|
|
162
|
+
* @returns The source system identifier.
|
|
163
|
+
*/
|
|
164
|
+
get sourceSystem(): string;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Sends a message to a specified topic.
|
|
168
|
+
*
|
|
169
|
+
* @param topic - The name of the topic to send the message to.
|
|
170
|
+
* @param key - The key of the message.
|
|
171
|
+
* @param payload - The message payload (must be JSON-serializable).
|
|
172
|
+
* @param signal - An optional AbortSignal that can be used to cancel the send operation. When aborted, the promise will reject with the abort reason.
|
|
173
|
+
* @returns A promise that resolves when the message has been successfully sent.
|
|
174
|
+
* @throws Error if the send operation fails or is aborted.
|
|
175
|
+
*/
|
|
176
|
+
send(
|
|
177
|
+
topic: string,
|
|
178
|
+
key: string,
|
|
179
|
+
payload: unknown,
|
|
180
|
+
signal?: AbortSignal,
|
|
181
|
+
): Promise<void>;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Subscribes to receive messages using the provided event handler.
|
|
185
|
+
*
|
|
186
|
+
* @param eventHandler - The event handler to process received messages and timers.
|
|
187
|
+
* @returns A promise that resolves when the subscription is successfully established and the consumer is ready to receive messages.
|
|
188
|
+
* @throws Error if the subscription fails to establish.
|
|
189
|
+
*/
|
|
190
|
+
subscribe(eventHandler: EventHandler): Promise<void>;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Unsubscribes from receiving messages and shuts down the consumer.
|
|
194
|
+
*
|
|
195
|
+
* @returns A promise that resolves when the unsubscribe operation is complete.
|
|
196
|
+
* @throws Error if the unsubscribe operation fails.
|
|
197
|
+
*/
|
|
198
|
+
unsubscribe(): Promise<void>;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Initializes the logging system for the Prosody client.
|
|
203
|
+
*
|
|
204
|
+
* This function sets up the tracing infrastructure and prepares the logging system
|
|
205
|
+
* to accept JavaScript loggers. It should be called once during application startup
|
|
206
|
+
* before any other logging operations.
|
|
207
|
+
*/
|
|
208
|
+
export function initialize(): void;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Checks if a logger has been set in the logging system.
|
|
212
|
+
*
|
|
213
|
+
* @returns True if a logger is currently configured, false otherwise.
|
|
214
|
+
*/
|
|
215
|
+
export function loggerIsSet(): boolean;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Sets a new JavaScript logger for the Prosody client.
|
|
219
|
+
*
|
|
220
|
+
* This function configures the logging system to use the provided JavaScript logger
|
|
221
|
+
* for all log output. The logger must implement all required log levels.
|
|
222
|
+
*
|
|
223
|
+
* @param logger - The JavaScript logger object with error, warn, info, debug, and trace methods.
|
|
224
|
+
* @throws Error if creating the new JavaScript logger fails.
|
|
225
|
+
*/
|
|
226
|
+
export function setLogger(logger: Logger): void;
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Sets a JavaScript logger only if no logger is currently configured.
|
|
230
|
+
*
|
|
231
|
+
* This function is useful for providing a default logger without overriding
|
|
232
|
+
* an existing one that may have been set earlier.
|
|
233
|
+
*
|
|
234
|
+
* @param logger - The JavaScript logger object with error, warn, info, debug, and trace methods.
|
|
235
|
+
* @returns True if the logger was set (no previous logger existed), false if a logger was already configured.
|
|
236
|
+
* @throws Error if creating the new JavaScript logger fails.
|
|
237
|
+
*/
|
|
238
|
+
export function setLoggerIfUnset(logger: Logger): boolean;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Gets the current configured logger.
|
|
242
|
+
*
|
|
243
|
+
* @returns The current logger instance, or null/undefined if no logger is configured.
|
|
244
|
+
*/
|
|
245
|
+
export function getCurrentLogger(): Logger | null | undefined;
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Base class for event handler errors.
|
|
249
|
+
* Provides a common interface for determining if an error is permanent.
|
|
250
|
+
*/
|
|
251
|
+
export abstract class EventHandlerError extends Error {
|
|
252
|
+
/**
|
|
253
|
+
* Indicates whether the error is permanent and should not be retried.
|
|
254
|
+
*/
|
|
255
|
+
abstract get isPermanent(): boolean;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Represents a transient error that may be resolved by retrying.
|
|
260
|
+
* These errors are temporary and the operation should be retried.
|
|
261
|
+
*/
|
|
262
|
+
export class TransientError extends EventHandlerError {
|
|
263
|
+
/**
|
|
264
|
+
* @returns Always false, indicating the error is not permanent.
|
|
265
|
+
*/
|
|
266
|
+
get isPermanent(): false;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Represents a permanent error that should not be retried.
|
|
271
|
+
* These errors indicate unrecoverable failures.
|
|
272
|
+
*/
|
|
273
|
+
export class PermanentError extends EventHandlerError {
|
|
274
|
+
/**
|
|
275
|
+
* @returns Always true, indicating the error is permanent.
|
|
276
|
+
*/
|
|
277
|
+
get isPermanent(): true;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Type alias for a constructor of an Error subclass. */
|
|
281
|
+
type ErrorClass<T extends Error> = new (...args: any[]) => T;
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Type for a decorator function that can be applied to both methods and standalone functions.
|
|
285
|
+
*/
|
|
286
|
+
type DecoratorFunction = (
|
|
287
|
+
target: Function,
|
|
288
|
+
context: ClassMethodDecoratorContext | ClassFieldDecoratorContext,
|
|
289
|
+
) => Function | void;
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Decorator factory for marking errors as transient.
|
|
293
|
+
* Can be applied to both methods and standalone functions.
|
|
294
|
+
* @param exceptionTypes The error types to be treated as transient.
|
|
295
|
+
*/
|
|
296
|
+
export declare function transient<E extends Error>(
|
|
297
|
+
...exceptionTypes: ErrorClass<E>[]
|
|
298
|
+
): DecoratorFunction;
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Decorator factory for marking errors as permanent.
|
|
302
|
+
* Can be applied to both methods and standalone functions.
|
|
303
|
+
* @param exceptionTypes The error types to be treated as permanent.
|
|
304
|
+
*/
|
|
305
|
+
export declare function permanent<E extends Error>(
|
|
306
|
+
...exceptionTypes: ErrorClass<E>[]
|
|
307
|
+
): DecoratorFunction;
|