@faasjs/node-utils 8.0.0-beta.8 → 8.0.0-beta.9

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/index.d.ts CHANGED
@@ -1,6 +1,3 @@
1
- import { Config, ExportedHandler } from "@faasjs/func";
2
- import { Logger } from "@faasjs/logger";
3
-
4
1
  //#region src/deep_merge.d.ts
5
2
  /**
6
3
  * Deep merge two objects or arrays.
@@ -17,13 +14,142 @@ import { Logger } from "@faasjs/logger";
17
14
  */
18
15
  declare function deepMerge(...sources: any[]): any;
19
16
  //#endregion
17
+ //#region src/logger.d.ts
18
+ /** Logger Level */
19
+ type Level = 'debug' | 'info' | 'warn' | 'error';
20
+ /**
21
+ * Formats the provided arguments into a string, filtering out any objects
22
+ * with a `__hidden__` property set to `true`. If formatting fails, it attempts
23
+ * to stringify each argument individually.
24
+ *
25
+ * @param {...any[]} args - The arguments to format.
26
+ * @returns {string} The formatted string.
27
+ */
28
+ declare function formatLogger(fmt: any, ...args: any[]): string;
29
+ /**
30
+ * Logger Class
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * const logger = new Logger()
35
+ *
36
+ * logger.debug('debug message')
37
+ * logger.info('info message')
38
+ * logger.warn('warn message')
39
+ * logger.error('error message')
40
+ *
41
+ * logger.time('timer name')
42
+ * logger.timeEnd('timer name', 'message') // => 'message +1ms'
43
+ * ```
44
+ */
45
+ declare class Logger {
46
+ silent: boolean;
47
+ level: Level;
48
+ colorfyOutput: boolean;
49
+ label?: string;
50
+ size: number;
51
+ disableTransport: boolean;
52
+ stdout: (text: string) => void;
53
+ stderr: (text: string) => void;
54
+ private cachedTimers;
55
+ /**
56
+ * @param label {string} Prefix label
57
+ */
58
+ constructor(label?: string);
59
+ /**
60
+ * @param message {string} message
61
+ * @param args {...any=} arguments
62
+ */
63
+ debug(message: string, ...args: any[]): Logger;
64
+ /**
65
+ * @param message {string} message
66
+ * @param args {...any=} arguments
67
+ */
68
+ info(message: string, ...args: any[]): Logger;
69
+ /**
70
+ * @param message {string} message
71
+ * @param args {...any=} arguments
72
+ */
73
+ warn(message: string, ...args: any[]): Logger;
74
+ /**
75
+ * @param message {any} message or Error object
76
+ * @param args {...any=} arguments
77
+ */
78
+ error(message: string | Error | unknown, ...args: any[]): Logger;
79
+ /**
80
+ * Start a timer with a specific key and log level.
81
+ *
82
+ * @param key - The unique identifier for the timer.
83
+ * @param level - The log level for the timer. Defaults to 'debug'.
84
+ * @returns The Logger instance for chaining.
85
+ */
86
+ time(key: string, level?: Level): Logger;
87
+ /**
88
+ * End a timer with a specific key and log the elapsed time.
89
+ *
90
+ * @param key - The unique identifier for the timer.
91
+ * @param message - The message to log with the elapsed time.
92
+ * @param args - Additional arguments to log with the message.
93
+ * @returns The Logger instance for chaining.
94
+ */
95
+ timeEnd(key: string, message: string, ...args: any[]): Logger;
96
+ /**
97
+ * @param message {string} message
98
+ * @param args {...any=} arguments
99
+ */
100
+ raw(message: string, ...args: any[]): Logger;
101
+ private log;
102
+ }
103
+ //#endregion
104
+ //#region src/color.d.ts
105
+ declare const Color: {
106
+ DEFAULT: number;
107
+ BLACK: number;
108
+ RED: number;
109
+ GREEN: number;
110
+ ORANGE: number;
111
+ BLUE: number;
112
+ MAGENTA: number;
113
+ CYAN: number;
114
+ GRAY: number;
115
+ };
116
+ declare const LevelColor: {
117
+ debug: number;
118
+ info: number;
119
+ warn: number;
120
+ error: number;
121
+ };
122
+ /**
123
+ * Apply ANSI color codes to a message based on the log level.
124
+ *
125
+ * @param level - The log level to determine the color.
126
+ * @param message - The message to be colorized.
127
+ * @returns The colorized message string.
128
+ */
129
+ declare function colorfy(level: Level, message: string): string;
130
+ //#endregion
20
131
  //#region src/load_config.d.ts
132
+ type FuncPluginConfig = {
133
+ [key: string]: any;
134
+ type?: string;
135
+ config?: {
136
+ [key: string]: any;
137
+ };
138
+ name?: string;
139
+ };
140
+ type FuncConfig = {
141
+ [key: string]: any;
142
+ plugins?: {
143
+ [key: string]: FuncPluginConfig;
144
+ };
145
+ };
21
146
  /**
22
147
  * Load configuration from faas.yaml
23
148
  */
24
- declare function loadConfig(root: string, filename: string, staging: string, logger?: Logger): Config;
149
+ declare function loadConfig(root: string, filename: string, staging: string, logger?: Logger): FuncConfig;
25
150
  //#endregion
26
151
  //#region src/load_func.d.ts
152
+ type ExportedHandler<TEvent = any, TContext = any, TResult = any> = (event?: TEvent, context?: TContext, callback?: (...args: any) => any) => Promise<TResult>;
27
153
  /**
28
154
  * Load a FaasJS function and its configuration, returning the handler.
29
155
  *
@@ -86,4 +212,123 @@ declare function streamToText(stream: ReadableStream<Uint8Array>): Promise<strin
86
212
  declare function streamToObject<T = any>(stream: ReadableStream<Uint8Array>): Promise<T>;
87
213
  declare const streamToString: typeof streamToText;
88
214
  //#endregion
89
- export { type NodeRuntime, deepMerge, detectNodeRuntime, loadConfig, loadFunc, loadPackage, resetRuntime, streamToObject, streamToString, streamToText };
215
+ //#region src/transport.d.ts
216
+ type LoggerMessage = {
217
+ level: Level;
218
+ labels: string[];
219
+ message: string;
220
+ timestamp: number;
221
+ extra?: any[];
222
+ };
223
+ type TransportHandler = (messages: LoggerMessage[]) => Promise<void>;
224
+ type TransportOptions = {
225
+ /** @default 'LoggerTransport' */label?: string; /** @default 5000 */
226
+ interval?: number; /** @default false */
227
+ debug?: boolean;
228
+ };
229
+ /**
230
+ * The transport class that manages the transport handlers and log messages.
231
+ *
232
+ * **Note: This class is not meant to be used directly. Use the {@link getTransport} instead.**
233
+ *
234
+ * @example
235
+ * ```typescript
236
+ * import { getTransport } from '@faasjs/node-utils'
237
+ *
238
+ * const transport = getTransport()
239
+ *
240
+ * transport.register('test', async (messages) => {
241
+ * for (const { level, message } of messages)
242
+ * console.log(level, message)
243
+ * })
244
+ *
245
+ * transport.config({ label: 'test', debug: true })
246
+ *
247
+ * // If you using Logger, it will automatically insert messages to the transport.
248
+ * // Otherwise, you can insert messages manually.
249
+ * transport.insert({
250
+ * level: 'info',
251
+ * labels: ['server'],
252
+ * message: 'test message',
253
+ * timestamp: Date.now()
254
+ * })
255
+ *
256
+ * process.on('SIGINT', async () => {
257
+ * await transport.stop()
258
+ * process.exit(0)
259
+ * })
260
+ * ```
261
+ */
262
+ declare class Transport {
263
+ private enabled;
264
+ handlers: Map<string, TransportHandler>;
265
+ private logger;
266
+ messages: LoggerMessage[];
267
+ private flushing;
268
+ private interval;
269
+ private intervalTime;
270
+ constructor();
271
+ /**
272
+ * Registers a new transport handler.
273
+ *
274
+ * @param name - The name of the transport handler.
275
+ * @param handler - The transport handler function to be registered.
276
+ */
277
+ register(name: string, handler: TransportHandler): void;
278
+ /**
279
+ * Unregister a handler by its name.
280
+ *
281
+ * This method logs the unregistration process, removes the handler from the internal collection,
282
+ * and disables the logger if no handlers remain.
283
+ *
284
+ * @param name - The name of the handler to unregister.
285
+ */
286
+ unregister(name: string): void;
287
+ /**
288
+ * Inserts a log message into the transport if it is enabled.
289
+ *
290
+ * @param message - The log message to be inserted.
291
+ */
292
+ insert(message: LoggerMessage): void;
293
+ /**
294
+ * Flushes the current messages by processing them with the registered handlers.
295
+ *
296
+ * If the transport is already flushing, it will wait until the current flush is complete.
297
+ * If the transport is disabled or there are no messages to flush, it will return immediately.
298
+ * If there are no handlers registered, it will log a warning, clear the messages, disable the transport, and stop the interval.
299
+ *
300
+ * The method processes all messages with each handler and logs any errors encountered during the process.
301
+ *
302
+ * @returns {Promise<void>} A promise that resolves when the flush operation is complete.
303
+ */
304
+ flush(): Promise<void>;
305
+ /**
306
+ * Stops the logger transport.
307
+ *
308
+ * This method performs the following actions:
309
+ * 1. Logs a 'stopping' message.
310
+ * 2. Clears the interval if it is set.
311
+ * 3. Flushes any remaining logs.
312
+ * 4. Disables the transport.
313
+ *
314
+ * @returns {Promise<void>} A promise that resolves when the transport has been stopped.
315
+ */
316
+ stop(): Promise<void>;
317
+ /**
318
+ * Resets the transport by clearing handlers, emptying messages, and re-enabling the transport.
319
+ * If an interval is set, it will be cleared.
320
+ */
321
+ reset(): void;
322
+ /**
323
+ * Configure the transport options for the logger.
324
+ *
325
+ * @param {TransportOptions} options - The configuration options for the transport.
326
+ * @param {string} [options.label] - The label to be used by the logger.
327
+ * @param {boolean} [options.debug] - If true, sets the logger level to 'debug', otherwise sets it to 'info'.
328
+ * @param {number} [options.interval] - The interval time in milliseconds for flushing the logs. If different from the current interval, it updates the interval and resets the timer.
329
+ */
330
+ config(options: TransportOptions): void;
331
+ }
332
+ declare function getTransport(): Transport;
333
+ //#endregion
334
+ export { Color, type ExportedHandler, type FuncConfig, type FuncPluginConfig, type Level, LevelColor, Logger, type LoggerMessage, type NodeRuntime, Transport, type TransportHandler, type TransportOptions, colorfy, deepMerge, detectNodeRuntime, formatLogger, getTransport, loadConfig, loadFunc, loadPackage, resetRuntime, streamToObject, streamToString, streamToText };