@nhtio/swarm 1.20250425.0 → 1.20260111.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/index.d.ts CHANGED
@@ -1,14 +1,342 @@
1
- /**
2
- * The module which contains types and utilities for using the library.
3
- * @module @nhtio/swarm
4
- */
5
- /**
6
- * The current version of the package.
7
- */
8
- export declare const version: string;
9
- export * as types from './types';
10
- export * as errors from './errors';
11
- export { setLogLevel } from './lib/logger';
12
- export { Swarm } from './lib/class_swarm';
13
- export { Secret } from '@nhtio/web-encryption';
14
- export { setPSK } from './lib/encryption';
1
+ /**
2
+ * Thrown by the {@link @nhtio/swarm!Swarm | Swarm} class when trying to be initialize more than one instance within the same context.
3
+ */
4
+ declare class AlreadyInitializedInContextError extends SwarmError {
5
+ constructor();
6
+ }
7
+
8
+ /**
9
+ * The module which contains types for the library.
10
+ * @module @nhtio/swarm/types
11
+ */
12
+ /**
13
+ * An inteface which describes the information about the browser and operating system
14
+ */
15
+ declare interface BrowserInfo {
16
+ /**
17
+ * The name of the browser
18
+ */
19
+ browserName: string;
20
+ /**
21
+ * The version of the browser
22
+ */
23
+ browserVersion: string;
24
+ /**
25
+ * The name of the operating system
26
+ */
27
+ osName: string;
28
+ /**
29
+ * The version of the operating system
30
+ */
31
+ osVersion: string;
32
+ /**
33
+ * The type of platform i.e. desktop / mobile
34
+ */
35
+ platformType: string;
36
+ /**
37
+ * The name of the browser engine
38
+ */
39
+ engineName: string;
40
+ /**
41
+ * The version of the browser engine
42
+ */
43
+ engineVersion: string;
44
+ }
45
+
46
+ export declare namespace errors {
47
+ export {
48
+ AlreadyInitializedInContextError,
49
+ MissingEncryptionKey,
50
+ RequestTimeoutError,
51
+ SwarmError,
52
+ SwarmErrorOptions,
53
+ UnsupportedEnvironmentError
54
+ }
55
+ }
56
+
57
+ declare type LogLevel = keyof typeof LogLevelStyles;
58
+
59
+ declare const LogLevelStyles: {
60
+ EMERG: (text: string) => string[];
61
+ ALERT: (text: string) => string[];
62
+ CRIT: (text: string) => string[];
63
+ ERROR: (text: string) => string[];
64
+ WARNING: (text: string) => string[];
65
+ NOTICE: (text: string) => string[];
66
+ INFO: (text: string) => string[];
67
+ DEBUG: (text: string) => string[];
68
+ };
69
+
70
+ /**
71
+ * Thrown by the {@link @nhtio/swarm!Swarm | Swarm} class trying to retrieve an instance without having previously or currently provided an encryption key.
72
+ */
73
+ declare class MissingEncryptionKey extends SwarmError {
74
+ constructor();
75
+ }
76
+
77
+ declare type RequestHandlerMap<Events extends TypedEventMap> = {
78
+ [K in keyof Events]: (...args: Events[K]) => any;
79
+ };
80
+
81
+ /**
82
+ * Thrown by the {@link @nhtio/swarm!Swarm | Swarm} class when a request to the leader is not answered.
83
+ */
84
+ declare class RequestTimeoutError extends SwarmError {
85
+ constructor(event: string | number | symbol);
86
+ }
87
+
88
+ /**
89
+ * Define a Secret value that hides itself from the logs or the console
90
+ * statements.
91
+ *
92
+ * The idea is to prevent accidental leaking of sensitive information.
93
+ * Idea borrowed from.
94
+ * https://transcend.io/blog/keep-sensitive-values-out-of-your-logs-with-types
95
+ */
96
+ export declare class Secret<T> {
97
+ #private;
98
+ constructor(value: T, redactedKeyword?: string);
99
+ toJSON(): string;
100
+ valueOf(): string;
101
+ toLocaleString(): string;
102
+ toString(): string;
103
+ /**
104
+ * Returns the original value
105
+ */
106
+ release(): T;
107
+ /**
108
+ * Transform the original value and create a new
109
+ * secret from it.
110
+ */
111
+ map<R>(transformFunc: (value: T) => R): Secret<R>;
112
+ }
113
+
114
+ /**
115
+ * Sets the log level for the logger.
116
+ * @param level - The log level to set. Can be one of the following:
117
+ * - `EMERG` or `emerg`
118
+ * - `ALERT` or `alert`
119
+ * - `CRIT` or `crit`
120
+ * - `ERROR` or `error`
121
+ * - `WARNING` or `warning`
122
+ * - `NOTICE` or `notice`
123
+ * - `INFO` or `info`
124
+ * - `DEBUG` or `debug`
125
+ *
126
+ * @remarks You can also set `window.SWARM_LOG_LEVEL` or `globalThis.SWARM_LOG_LEVEL` to the same values to set the log level globally before initializing any of the Swarm modules.
127
+ */
128
+ export declare const setLogLevel: (level: LogLevel) => void;
129
+
130
+ /**
131
+ * Set the pre-shared key used to ensure that any data shared between nodes in the swarm is encrypted and namespaced.
132
+ * @param secret The pre-shared key to use
133
+ * @returns
134
+ */
135
+ export declare const setPSK: (secret: string | Secret<string>) => void;
136
+
137
+ /**
138
+ * The class that provides peer communication between browser tabs and service workers
139
+ * @typeParam Events - The map of events and their arguments that can be emitted and listened to.
140
+ */
141
+ export declare class Swarm<Events extends TypedEventMap = TypedEventMap> extends TypedEventEmitter<Events> {
142
+ #private;
143
+ private static readonly INTERNAL_HEARTBEAT;
144
+ private static readonly INTERNAL_OBITUARY;
145
+ /** @private */
146
+ constructor(secret?: string | Secret<string>);
147
+ /**
148
+ * The ID of the current instance.
149
+ */
150
+ get id(): string;
151
+ /**
152
+ * Whether this instance is the leader or not.
153
+ */
154
+ get leader(): boolean;
155
+ /**
156
+ * Emit an event to all connected peers.
157
+ * @param event - The name of the event to emit.
158
+ * @param args - The arguments to pass to the event listeners.
159
+ * @typeParam E - The name of the event to emit.
160
+ */
161
+ emit<E extends keyof Events>(event: E, ...args: Events[E]): this;
162
+ /**
163
+ * Subscribe to the leadership change event.
164
+ * @param cb - The callback function to be called when the leadership status changes.
165
+ */
166
+ onLeadershipChange(cb: (is: boolean) => void): this;
167
+ /**
168
+ * Remove the leadership change event listener.
169
+ * @param cb - The callback function to be removed from the leadership change event.
170
+ */
171
+ offLeadershipChange(cb: (is: boolean) => void): this;
172
+ /**
173
+ * Subscribe to the next occurance of a leadership change event.
174
+ * @param cb - The callback function to be called when the leadership status changes.
175
+ */
176
+ onceLeadershipChange(cb: (is: boolean) => void): this;
177
+ /**
178
+ * Collect responses from all peers for a specific event.
179
+ * @param event The name of the event to collect responses for.
180
+ * @param args The arguments which will be passed to the event handler.
181
+ * @returns A promise that resolves with an array of responses from all peers.
182
+ * @typeParam R - The type of the response.
183
+ * @typeParam E - The name of the event to collect responses for.
184
+ */
185
+ collect<R, E extends keyof Events = keyof Events>(event: E, ...args: Events[E]): Promise<R[]>;
186
+ /**
187
+ * Register a handler for a specific event which will respond to incoming messages which require a general response
188
+ * @param event The name of the event to listen for.
189
+ * @param handler The function to call when the event is emitted.
190
+ * @typeParam E - The name of the event to listen for.
191
+ */
192
+ onCollect<E extends keyof Events>(event: E, handler: (...args: Events[E]) => ReturnType<RequestHandlerMap<Events>[E]>): this;
193
+ /**
194
+ * De-register a handler for a specific event which will respond to incoming messages which require a general response
195
+ * @param event The name of the event to listen for.
196
+ * @param handler The function to call when the event is emitted.
197
+ * @typeParam E - The name of the event to listen for.
198
+ */
199
+ offCollect<E extends keyof Events>(event: E): this;
200
+ /**
201
+ * Request feedback from the leader for a specific event.
202
+ * @param event The name of the event to request feedback for.
203
+ * @param args The arguments to pass to the event handler.
204
+ * @returns A promise that resolves with the response from the event handler.
205
+ * @typeParam R - The type of the response.
206
+ * @typeParam E - The name of the event to request feedback for.
207
+ */
208
+ request<R = any, E extends keyof Events = keyof Events>(event: E, ...args: Events[E]): Promise<R>;
209
+ /**
210
+ * Register a handler for a specific event which will respond to incoming messages which require a response from the leader
211
+ * @param event The name of the event to listen for.
212
+ * @param handler The function to call when the event is emitted.
213
+ * @typeParam E - The name of the event to listen for.
214
+ */
215
+ onRequest<E extends keyof Events>(event: E, handler: (...args: Events[E]) => ReturnType<RequestHandlerMap<Events>[E]>): this;
216
+ /**
217
+ * De-register a handler for a specific event which will respond to incoming messages which require a response from the leader
218
+ * @param event The name of the event to listen for.
219
+ * @param handler The function to call when the event is emitted.
220
+ * @typeParam E - The name of the event to listen for.
221
+ */
222
+ offRequest<E extends keyof Events>(event: E): this;
223
+ /**
224
+ * Set the timeout for waiting for a response from `request()` or `collect()`.
225
+ * @param timeout The timeout in milliseconds to wait for a response.
226
+ * @returns The current instance of the Swarm class for the current context.
227
+ */
228
+ setAwaitResponseTimeout(timeout: number): this;
229
+ /**
230
+ * Retreive the current instance of the Swarm class for the current context.
231
+ * @typeParam Events - The map of events and their arguments that can be emitted and listened to.
232
+ * @returns The current instance of the Swarm class for the current context.
233
+ */
234
+ static instance<Events extends TypedEventMap = TypedEventMap>(secret?: string | Secret<string>): Swarm<Events>;
235
+ }
236
+
237
+ /**
238
+ * Base class for all Swarm errors.
239
+ * @extends Error
240
+ */
241
+ declare class SwarmError extends Error {
242
+ /** @private */
243
+ readonly $__name: string;
244
+ /** @private */
245
+ readonly $__message: string;
246
+ /**
247
+ * Creates a new SwarmError instance.
248
+ * @param message The error message.
249
+ * @param options The error options.
250
+ */
251
+ constructor(message: string, options?: SwarmErrorOptions);
252
+ get name(): string;
253
+ get message(): string;
254
+ get [Symbol.toStringTag](): string;
255
+ toString(): string;
256
+ [Symbol.toPrimitive](hint: 'number' | 'string' | 'default'): string | true;
257
+ static [Symbol.hasInstance](instance: unknown): boolean;
258
+ }
259
+
260
+ /**
261
+ * Easily accessible error classes for Swarm
262
+ * @module @nhtio/swarm/errors
263
+ */
264
+ /**
265
+ * Describes the options for the SwarmError class.
266
+ */
267
+ declare interface SwarmErrorOptions {
268
+ /**
269
+ * The cause data property of an Error instance indicates the specific original cause of the error.
270
+ */
271
+ cause?: Error;
272
+ /**
273
+ * How many rows to trim from the stack trace.
274
+ * This is useful for removing the stack trace of the current function from the error.
275
+ */
276
+ trim?: number;
277
+ }
278
+
279
+ declare class TinyEmitter {
280
+ on(event: string, callback: Function, ctx?: any): this;
281
+ once(event: string, callback: Function, ctx?: any): this;
282
+ emit(event: string, ...args: any[]): this;
283
+ off(event: string, callback?: Function): this;
284
+ }
285
+
286
+ /**
287
+ * A strongly-typed Tiny Event Emitter.
288
+ *
289
+ * @template Events
290
+ * key = event name,
291
+ * value = tuple type of that event’s arguments
292
+ */
293
+ declare class TypedEventEmitter<Events extends TypedEventMap = TypedEventMap> extends TinyEmitter {
294
+ /**
295
+ * Subscribe to an event with a typed listener.
296
+ */
297
+ on<E extends keyof Events>(event: E, listener: (...args: Events[E]) => void, ctx?: any): this;
298
+ /**
299
+ * Subscribe once to an event.
300
+ */
301
+ once<E extends keyof Events>(event: E, listener: (...args: Events[E]) => void, ctx?: any): this;
302
+ /**
303
+ * Emit an event, enforcing that the args match Events[E].
304
+ */
305
+ emit<E extends keyof Events>(event: E, ...args: Events[E]): this;
306
+ /**
307
+ * Remove a listener.
308
+ */
309
+ off<E extends keyof Events>(event: E, listener?: (...args: Events[E]) => void): this;
310
+ }
311
+
312
+ /**
313
+ * A map from event-names to the tuple of arguments
314
+ * that listeners for that event will receive.
315
+ */
316
+ declare interface TypedEventMap {
317
+ [event: string]: any[];
318
+ }
319
+
320
+ export declare namespace types {
321
+ export {
322
+ BrowserInfo,
323
+ TypedEventMap,
324
+ RequestHandlerMap,
325
+ LogLevel,
326
+ LogLevelStyles
327
+ }
328
+ }
329
+
330
+ /**
331
+ * Thrown by the {@link @nhtio/swarm!Swarm | Swarm} class when trying to be initialized in an environment that does not support Service Workers.
332
+ */
333
+ declare class UnsupportedEnvironmentError extends SwarmError {
334
+ constructor();
335
+ }
336
+
337
+ /**
338
+ * The current version of the package.
339
+ */
340
+ export declare const version: string;
341
+
342
+ export { }