@gravito/spectrum 3.0.1 → 3.0.2

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.cts CHANGED
@@ -36,6 +36,8 @@ interface CapturedRequest {
36
36
  requestBody?: any;
37
37
  /** Parsed response body (if captured). */
38
38
  responseBody?: any;
39
+ /** Request ID for correlation with logs and queries. */
40
+ requestId?: string;
39
41
  }
40
42
  /**
41
43
  * Represents a log entry captured during application execution.
@@ -54,6 +56,8 @@ interface CapturedLog {
54
56
  args: any[];
55
57
  /** Epoch timestamp when the log was generated. */
56
58
  timestamp: number;
59
+ /** Request ID for correlation with the originating request. */
60
+ requestId?: string;
57
61
  }
58
62
  /**
59
63
  * Represents a database query captured during application execution.
@@ -74,6 +78,8 @@ interface CapturedQuery {
74
78
  duration: number;
75
79
  /** Epoch timestamp when the query was executed. */
76
80
  timestamp: number;
81
+ /** Request ID for correlation with the originating request. */
82
+ requestId?: string;
77
83
  }
78
84
 
79
85
  /**
@@ -196,20 +202,91 @@ interface SpectrumOrbitDeps {
196
202
  * @since 3.0.0
197
203
  */
198
204
  declare class SpectrumOrbit implements GravitoOrbit {
205
+ /**
206
+ * Global instance of SpectrumOrbit.
207
+ *
208
+ * Used for internal access and singleton patterns within the framework.
209
+ */
199
210
  static instance: SpectrumOrbit | undefined;
200
211
  readonly name = "spectrum";
201
212
  private config;
202
213
  private listeners;
214
+ private currentRequestId;
203
215
  private warnedSecurity;
204
216
  private deps;
217
+ /**
218
+ * Initializes a new instance of SpectrumOrbit.
219
+ *
220
+ * @param config - Configuration options for behavior and storage
221
+ * @param deps - Internal dependencies for integration with other orbits
222
+ *
223
+ * @example
224
+ * ```typescript
225
+ * const spectrum = new SpectrumOrbit({ path: '/debug' });
226
+ * ```
227
+ */
205
228
  constructor(config?: SpectrumConfig, deps?: SpectrumOrbitDeps);
229
+ /**
230
+ * Checks if the current operation should be captured based on sample rate.
231
+ *
232
+ * @returns True if capture is allowed
233
+ */
206
234
  private shouldCapture;
235
+ /**
236
+ * Broadcasts telemetry data to all connected SSE clients.
237
+ *
238
+ * @param type - The category of the data
239
+ * @param data - The telemetry payload
240
+ */
207
241
  private broadcast;
242
+ /**
243
+ * Installs the Spectrum orbit into the Gravito core.
244
+ *
245
+ * Sets up collection listeners, initializes storage, and registers API/UI routes.
246
+ *
247
+ * @param core - The planet core instance
248
+ * @returns Resolves when installation is complete
249
+ * @throws {Error} If storage initialization fails
250
+ *
251
+ * @example
252
+ * ```typescript
253
+ * await spectrum.install(core);
254
+ * ```
255
+ */
208
256
  install(core: PlanetCore): Promise<void>;
257
+ /**
258
+ * Configures collection of database queries from Atlas orbit.
259
+ *
260
+ * @param core - The planet core instance
261
+ */
209
262
  private setupDatabaseCollection;
263
+ /**
264
+ * Configures interception of HTTP requests and responses.
265
+ *
266
+ * @param core - The planet core instance
267
+ */
210
268
  private setupHttpCollection;
269
+ /**
270
+ * Configures interception of application logs.
271
+ *
272
+ * Wraps the core logger to capture all log calls.
273
+ *
274
+ * @param core - The planet core instance
275
+ */
211
276
  private setupLogCollection;
277
+ /**
278
+ * Processes and stores a captured log entry.
279
+ *
280
+ * @param level - Log severity level
281
+ * @param message - Primary message string
282
+ * @param args - Additional log arguments
283
+ */
212
284
  private captureLog;
285
+ /**
286
+ * Registers all API and UI routes for the Spectrum dashboard.
287
+ *
288
+ * @param core - The planet core instance
289
+ */
213
290
  private registerRoutes;
214
291
  }
215
292
 
@@ -244,19 +321,104 @@ declare class FileStorage implements SpectrumStorage {
244
321
  private logsPath;
245
322
  private queriesPath;
246
323
  private cache;
324
+ /**
325
+ * Initializes a new instance of FileStorage.
326
+ *
327
+ * @param config - Configuration including the target directory for JSONL files
328
+ *
329
+ * @example
330
+ * ```typescript
331
+ * const storage = new FileStorage({ directory: './storage' });
332
+ * ```
333
+ */
247
334
  constructor(config: FileStorageConfig);
335
+ /**
336
+ * Initializes the storage by creating the directory and loading existing files into memory.
337
+ *
338
+ * @returns Resolves when initialization is complete
339
+ * @throws {Error} If directory creation fails
340
+ */
248
341
  init(): Promise<void>;
342
+ /**
343
+ * Loads newline-delimited JSON data from a file into a target array.
344
+ *
345
+ * @param path - The absolute path to the JSONL file
346
+ * @param target - The array to populate with parsed objects
347
+ */
249
348
  private loadCache;
349
+ /**
350
+ * Internal helper to append data to both the in-memory cache and the JSONL file.
351
+ *
352
+ * @param path - File path to append to
353
+ * @param data - The telemetry object
354
+ * @param list - The cache array
355
+ */
250
356
  private append;
357
+ /**
358
+ * Persists a captured HTTP request.
359
+ *
360
+ * @param req - The request snapshot
361
+ */
251
362
  storeRequest(req: CapturedRequest): Promise<void>;
363
+ /**
364
+ * Persists a captured log entry.
365
+ *
366
+ * @param log - The log snapshot
367
+ */
252
368
  storeLog(log: CapturedLog): Promise<void>;
369
+ /**
370
+ * Persists a captured database query.
371
+ *
372
+ * @param query - The query snapshot
373
+ */
253
374
  storeQuery(query: CapturedQuery): Promise<void>;
375
+ /**
376
+ * Retrieves recent HTTP requests from storage.
377
+ *
378
+ * @param limit - Maximum number of items to return
379
+ * @param offset - Pagination offset
380
+ * @returns Array of requests
381
+ */
254
382
  getRequests(limit?: number, offset?: number): Promise<CapturedRequest[]>;
383
+ /**
384
+ * Retrieves a specific HTTP request by its unique ID.
385
+ *
386
+ * @param id - The snapshot ID
387
+ * @returns The request or null if not found
388
+ */
255
389
  getRequest(id: string): Promise<CapturedRequest | null>;
390
+ /**
391
+ * Retrieves recent logs from storage.
392
+ *
393
+ * @param limit - Maximum number of items to return
394
+ * @param offset - Pagination offset
395
+ * @returns Array of logs
396
+ */
256
397
  getLogs(limit?: number, offset?: number): Promise<CapturedLog[]>;
398
+ /**
399
+ * Retrieves recent database queries from storage.
400
+ *
401
+ * @param limit - Maximum number of items to return
402
+ * @param offset - Pagination offset
403
+ * @returns Array of queries
404
+ */
257
405
  getQueries(limit?: number, offset?: number): Promise<CapturedQuery[]>;
406
+ /**
407
+ * Wipes all data from both cache and files.
408
+ */
258
409
  clear(): Promise<void>;
410
+ /**
411
+ * Truncates the storage to stay within the specified limit.
412
+ *
413
+ * @param maxItems - The maximum allowed records per category
414
+ */
259
415
  prune(maxItems: number): Promise<void>;
416
+ /**
417
+ * Overwrites the file content with the current in-memory data.
418
+ *
419
+ * @param path - File path to overwrite
420
+ * @param data - The array of data objects
421
+ */
260
422
  private rewrite;
261
423
  }
262
424
 
@@ -266,21 +428,100 @@ declare class FileStorage implements SpectrumStorage {
266
428
  * Non-persistent storage for development.
267
429
  */
268
430
 
431
+ /**
432
+ * MemoryStorage provides a fast, non-persistent storage backend for Spectrum telemetry.
433
+ *
434
+ * This implementation is ideal for local development environments where data persistence
435
+ * across server restarts is not required. It maintains all captured data in memory
436
+ * and automatically prunes old records based on the configured limits.
437
+ *
438
+ * @public
439
+ * @since 3.0.0
440
+ *
441
+ * @example
442
+ * ```typescript
443
+ * const storage = new MemoryStorage();
444
+ * ```
445
+ */
269
446
  declare class MemoryStorage implements SpectrumStorage {
270
447
  private requests;
271
448
  private logs;
272
449
  private queries;
273
450
  private maxItems;
451
+ /**
452
+ * Initializes the memory storage.
453
+ *
454
+ * As memory storage does not require external resources, this is a no-op.
455
+ *
456
+ * @returns Resolves immediately
457
+ */
274
458
  init(): Promise<void>;
459
+ /**
460
+ * Temporarily stores a captured HTTP request in memory.
461
+ *
462
+ * @param req - The request snapshot to store
463
+ */
275
464
  storeRequest(req: CapturedRequest): Promise<void>;
465
+ /**
466
+ * Temporarily stores a captured log entry in memory.
467
+ *
468
+ * @param log - The log entry to store
469
+ */
276
470
  storeLog(log: CapturedLog): Promise<void>;
471
+ /**
472
+ * Temporarily stores a captured database query in memory.
473
+ *
474
+ * @param query - The query information to store
475
+ */
277
476
  storeQuery(query: CapturedQuery): Promise<void>;
477
+ /**
478
+ * Retrieves a list of recent HTTP requests from memory.
479
+ *
480
+ * @param limit - Maximum number of requests to return
481
+ * @param offset - Number of requests to skip for pagination
482
+ * @returns A promise resolving to an array of captured requests
483
+ */
278
484
  getRequests(limit?: number, offset?: number): Promise<CapturedRequest[]>;
485
+ /**
486
+ * Finds a specific HTTP request by its unique identifier.
487
+ *
488
+ * @param id - The unique ID of the request to find
489
+ * @returns The found request or null if not present
490
+ */
279
491
  getRequest(id: string): Promise<CapturedRequest | null>;
492
+ /**
493
+ * Retrieves a list of recent application logs from memory.
494
+ *
495
+ * @param limit - Maximum number of logs to return
496
+ * @param offset - Number of logs to skip for pagination
497
+ * @returns A promise resolving to an array of captured logs
498
+ */
280
499
  getLogs(limit?: number, offset?: number): Promise<CapturedLog[]>;
500
+ /**
501
+ * Retrieves a list of recent database queries from memory.
502
+ *
503
+ * @param limit - Maximum number of queries to return
504
+ * @param offset - Number of queries to skip for pagination
505
+ * @returns A promise resolving to an array of captured queries
506
+ */
281
507
  getQueries(limit?: number, offset?: number): Promise<CapturedQuery[]>;
508
+ /**
509
+ * Wipes all captured telemetry data from memory.
510
+ *
511
+ * @returns Resolves when all collections are emptied
512
+ */
282
513
  clear(): Promise<void>;
514
+ /**
515
+ * Sets a new capacity limit and prunes existing data to fit.
516
+ *
517
+ * @param maxItems - The new maximum number of items to retain per category
518
+ */
283
519
  prune(maxItems: number): Promise<void>;
520
+ /**
521
+ * Truncates an array to ensure it does not exceed the maximum allowed capacity.
522
+ *
523
+ * @param arr - The target array to truncate
524
+ */
284
525
  private trim;
285
526
  }
286
527
 
package/dist/index.d.ts CHANGED
@@ -36,6 +36,8 @@ interface CapturedRequest {
36
36
  requestBody?: any;
37
37
  /** Parsed response body (if captured). */
38
38
  responseBody?: any;
39
+ /** Request ID for correlation with logs and queries. */
40
+ requestId?: string;
39
41
  }
40
42
  /**
41
43
  * Represents a log entry captured during application execution.
@@ -54,6 +56,8 @@ interface CapturedLog {
54
56
  args: any[];
55
57
  /** Epoch timestamp when the log was generated. */
56
58
  timestamp: number;
59
+ /** Request ID for correlation with the originating request. */
60
+ requestId?: string;
57
61
  }
58
62
  /**
59
63
  * Represents a database query captured during application execution.
@@ -74,6 +78,8 @@ interface CapturedQuery {
74
78
  duration: number;
75
79
  /** Epoch timestamp when the query was executed. */
76
80
  timestamp: number;
81
+ /** Request ID for correlation with the originating request. */
82
+ requestId?: string;
77
83
  }
78
84
 
79
85
  /**
@@ -196,20 +202,91 @@ interface SpectrumOrbitDeps {
196
202
  * @since 3.0.0
197
203
  */
198
204
  declare class SpectrumOrbit implements GravitoOrbit {
205
+ /**
206
+ * Global instance of SpectrumOrbit.
207
+ *
208
+ * Used for internal access and singleton patterns within the framework.
209
+ */
199
210
  static instance: SpectrumOrbit | undefined;
200
211
  readonly name = "spectrum";
201
212
  private config;
202
213
  private listeners;
214
+ private currentRequestId;
203
215
  private warnedSecurity;
204
216
  private deps;
217
+ /**
218
+ * Initializes a new instance of SpectrumOrbit.
219
+ *
220
+ * @param config - Configuration options for behavior and storage
221
+ * @param deps - Internal dependencies for integration with other orbits
222
+ *
223
+ * @example
224
+ * ```typescript
225
+ * const spectrum = new SpectrumOrbit({ path: '/debug' });
226
+ * ```
227
+ */
205
228
  constructor(config?: SpectrumConfig, deps?: SpectrumOrbitDeps);
229
+ /**
230
+ * Checks if the current operation should be captured based on sample rate.
231
+ *
232
+ * @returns True if capture is allowed
233
+ */
206
234
  private shouldCapture;
235
+ /**
236
+ * Broadcasts telemetry data to all connected SSE clients.
237
+ *
238
+ * @param type - The category of the data
239
+ * @param data - The telemetry payload
240
+ */
207
241
  private broadcast;
242
+ /**
243
+ * Installs the Spectrum orbit into the Gravito core.
244
+ *
245
+ * Sets up collection listeners, initializes storage, and registers API/UI routes.
246
+ *
247
+ * @param core - The planet core instance
248
+ * @returns Resolves when installation is complete
249
+ * @throws {Error} If storage initialization fails
250
+ *
251
+ * @example
252
+ * ```typescript
253
+ * await spectrum.install(core);
254
+ * ```
255
+ */
208
256
  install(core: PlanetCore): Promise<void>;
257
+ /**
258
+ * Configures collection of database queries from Atlas orbit.
259
+ *
260
+ * @param core - The planet core instance
261
+ */
209
262
  private setupDatabaseCollection;
263
+ /**
264
+ * Configures interception of HTTP requests and responses.
265
+ *
266
+ * @param core - The planet core instance
267
+ */
210
268
  private setupHttpCollection;
269
+ /**
270
+ * Configures interception of application logs.
271
+ *
272
+ * Wraps the core logger to capture all log calls.
273
+ *
274
+ * @param core - The planet core instance
275
+ */
211
276
  private setupLogCollection;
277
+ /**
278
+ * Processes and stores a captured log entry.
279
+ *
280
+ * @param level - Log severity level
281
+ * @param message - Primary message string
282
+ * @param args - Additional log arguments
283
+ */
212
284
  private captureLog;
285
+ /**
286
+ * Registers all API and UI routes for the Spectrum dashboard.
287
+ *
288
+ * @param core - The planet core instance
289
+ */
213
290
  private registerRoutes;
214
291
  }
215
292
 
@@ -244,19 +321,104 @@ declare class FileStorage implements SpectrumStorage {
244
321
  private logsPath;
245
322
  private queriesPath;
246
323
  private cache;
324
+ /**
325
+ * Initializes a new instance of FileStorage.
326
+ *
327
+ * @param config - Configuration including the target directory for JSONL files
328
+ *
329
+ * @example
330
+ * ```typescript
331
+ * const storage = new FileStorage({ directory: './storage' });
332
+ * ```
333
+ */
247
334
  constructor(config: FileStorageConfig);
335
+ /**
336
+ * Initializes the storage by creating the directory and loading existing files into memory.
337
+ *
338
+ * @returns Resolves when initialization is complete
339
+ * @throws {Error} If directory creation fails
340
+ */
248
341
  init(): Promise<void>;
342
+ /**
343
+ * Loads newline-delimited JSON data from a file into a target array.
344
+ *
345
+ * @param path - The absolute path to the JSONL file
346
+ * @param target - The array to populate with parsed objects
347
+ */
249
348
  private loadCache;
349
+ /**
350
+ * Internal helper to append data to both the in-memory cache and the JSONL file.
351
+ *
352
+ * @param path - File path to append to
353
+ * @param data - The telemetry object
354
+ * @param list - The cache array
355
+ */
250
356
  private append;
357
+ /**
358
+ * Persists a captured HTTP request.
359
+ *
360
+ * @param req - The request snapshot
361
+ */
251
362
  storeRequest(req: CapturedRequest): Promise<void>;
363
+ /**
364
+ * Persists a captured log entry.
365
+ *
366
+ * @param log - The log snapshot
367
+ */
252
368
  storeLog(log: CapturedLog): Promise<void>;
369
+ /**
370
+ * Persists a captured database query.
371
+ *
372
+ * @param query - The query snapshot
373
+ */
253
374
  storeQuery(query: CapturedQuery): Promise<void>;
375
+ /**
376
+ * Retrieves recent HTTP requests from storage.
377
+ *
378
+ * @param limit - Maximum number of items to return
379
+ * @param offset - Pagination offset
380
+ * @returns Array of requests
381
+ */
254
382
  getRequests(limit?: number, offset?: number): Promise<CapturedRequest[]>;
383
+ /**
384
+ * Retrieves a specific HTTP request by its unique ID.
385
+ *
386
+ * @param id - The snapshot ID
387
+ * @returns The request or null if not found
388
+ */
255
389
  getRequest(id: string): Promise<CapturedRequest | null>;
390
+ /**
391
+ * Retrieves recent logs from storage.
392
+ *
393
+ * @param limit - Maximum number of items to return
394
+ * @param offset - Pagination offset
395
+ * @returns Array of logs
396
+ */
256
397
  getLogs(limit?: number, offset?: number): Promise<CapturedLog[]>;
398
+ /**
399
+ * Retrieves recent database queries from storage.
400
+ *
401
+ * @param limit - Maximum number of items to return
402
+ * @param offset - Pagination offset
403
+ * @returns Array of queries
404
+ */
257
405
  getQueries(limit?: number, offset?: number): Promise<CapturedQuery[]>;
406
+ /**
407
+ * Wipes all data from both cache and files.
408
+ */
258
409
  clear(): Promise<void>;
410
+ /**
411
+ * Truncates the storage to stay within the specified limit.
412
+ *
413
+ * @param maxItems - The maximum allowed records per category
414
+ */
259
415
  prune(maxItems: number): Promise<void>;
416
+ /**
417
+ * Overwrites the file content with the current in-memory data.
418
+ *
419
+ * @param path - File path to overwrite
420
+ * @param data - The array of data objects
421
+ */
260
422
  private rewrite;
261
423
  }
262
424
 
@@ -266,21 +428,100 @@ declare class FileStorage implements SpectrumStorage {
266
428
  * Non-persistent storage for development.
267
429
  */
268
430
 
431
+ /**
432
+ * MemoryStorage provides a fast, non-persistent storage backend for Spectrum telemetry.
433
+ *
434
+ * This implementation is ideal for local development environments where data persistence
435
+ * across server restarts is not required. It maintains all captured data in memory
436
+ * and automatically prunes old records based on the configured limits.
437
+ *
438
+ * @public
439
+ * @since 3.0.0
440
+ *
441
+ * @example
442
+ * ```typescript
443
+ * const storage = new MemoryStorage();
444
+ * ```
445
+ */
269
446
  declare class MemoryStorage implements SpectrumStorage {
270
447
  private requests;
271
448
  private logs;
272
449
  private queries;
273
450
  private maxItems;
451
+ /**
452
+ * Initializes the memory storage.
453
+ *
454
+ * As memory storage does not require external resources, this is a no-op.
455
+ *
456
+ * @returns Resolves immediately
457
+ */
274
458
  init(): Promise<void>;
459
+ /**
460
+ * Temporarily stores a captured HTTP request in memory.
461
+ *
462
+ * @param req - The request snapshot to store
463
+ */
275
464
  storeRequest(req: CapturedRequest): Promise<void>;
465
+ /**
466
+ * Temporarily stores a captured log entry in memory.
467
+ *
468
+ * @param log - The log entry to store
469
+ */
276
470
  storeLog(log: CapturedLog): Promise<void>;
471
+ /**
472
+ * Temporarily stores a captured database query in memory.
473
+ *
474
+ * @param query - The query information to store
475
+ */
277
476
  storeQuery(query: CapturedQuery): Promise<void>;
477
+ /**
478
+ * Retrieves a list of recent HTTP requests from memory.
479
+ *
480
+ * @param limit - Maximum number of requests to return
481
+ * @param offset - Number of requests to skip for pagination
482
+ * @returns A promise resolving to an array of captured requests
483
+ */
278
484
  getRequests(limit?: number, offset?: number): Promise<CapturedRequest[]>;
485
+ /**
486
+ * Finds a specific HTTP request by its unique identifier.
487
+ *
488
+ * @param id - The unique ID of the request to find
489
+ * @returns The found request or null if not present
490
+ */
279
491
  getRequest(id: string): Promise<CapturedRequest | null>;
492
+ /**
493
+ * Retrieves a list of recent application logs from memory.
494
+ *
495
+ * @param limit - Maximum number of logs to return
496
+ * @param offset - Number of logs to skip for pagination
497
+ * @returns A promise resolving to an array of captured logs
498
+ */
280
499
  getLogs(limit?: number, offset?: number): Promise<CapturedLog[]>;
500
+ /**
501
+ * Retrieves a list of recent database queries from memory.
502
+ *
503
+ * @param limit - Maximum number of queries to return
504
+ * @param offset - Number of queries to skip for pagination
505
+ * @returns A promise resolving to an array of captured queries
506
+ */
281
507
  getQueries(limit?: number, offset?: number): Promise<CapturedQuery[]>;
508
+ /**
509
+ * Wipes all captured telemetry data from memory.
510
+ *
511
+ * @returns Resolves when all collections are emptied
512
+ */
282
513
  clear(): Promise<void>;
514
+ /**
515
+ * Sets a new capacity limit and prunes existing data to fit.
516
+ *
517
+ * @param maxItems - The new maximum number of items to retain per category
518
+ */
283
519
  prune(maxItems: number): Promise<void>;
520
+ /**
521
+ * Truncates an array to ensure it does not exceed the maximum allowed capacity.
522
+ *
523
+ * @param arr - The target array to truncate
524
+ */
284
525
  private trim;
285
526
  }
286
527