@seedcord/services 0.2.2 → 0.3.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/dist/index.cjs CHANGED
@@ -113,124 +113,124 @@ ${parts.join(" ")}`;
113
113
  });
114
114
  }
115
115
  /**
116
- * Logs an error message with optional additional data.
117
- *
118
- * @param msg - The error message to log
119
- * @param args - Additional data to include in the log entry
120
- */
116
+ * Logs an error message with optional additional data.
117
+ *
118
+ * @param msg - The error message to log
119
+ * @param args - Additional data to include in the log entry
120
+ */
121
121
  error(msg, ...args) {
122
122
  this.logger.error(msg, ...args);
123
123
  }
124
124
  /**
125
- * Logs a warning message with optional additional data.
126
- *
127
- * @param msg - The warning message to log
128
- * @param args - Additional data to include in the log entry
129
- */
125
+ * Logs a warning message with optional additional data.
126
+ *
127
+ * @param msg - The warning message to log
128
+ * @param args - Additional data to include in the log entry
129
+ */
130
130
  warn(msg, ...args) {
131
131
  this.logger.warn(msg, ...args);
132
132
  }
133
133
  /**
134
- * Logs an informational message with optional additional data.
135
- *
136
- * @param msg - The informational message to log
137
- * @param args - Additional data to include in the log entry
138
- */
134
+ * Logs an informational message with optional additional data.
135
+ *
136
+ * @param msg - The informational message to log
137
+ * @param args - Additional data to include in the log entry
138
+ */
139
139
  info(msg, ...args) {
140
140
  this.logger.info(msg, ...args);
141
141
  }
142
142
  /**
143
- * Logs an HTTP-related message with optional additional data.
144
- *
145
- * @param msg - The HTTP message to log
146
- * @param args - Additional data to include in the log entry
147
- */
143
+ * Logs an HTTP-related message with optional additional data.
144
+ *
145
+ * @param msg - The HTTP message to log
146
+ * @param args - Additional data to include in the log entry
147
+ */
148
148
  http(msg, ...args) {
149
149
  this.logger.http(msg, ...args);
150
150
  }
151
151
  /**
152
- * Logs a verbose message with optional additional data.
153
- *
154
- * @param msg - The verbose message to log
155
- * @param args - Additional data to include in the log entry
156
- */
152
+ * Logs a verbose message with optional additional data.
153
+ *
154
+ * @param msg - The verbose message to log
155
+ * @param args - Additional data to include in the log entry
156
+ */
157
157
  verbose(msg, ...args) {
158
158
  this.logger.verbose(msg, ...args);
159
159
  }
160
160
  /**
161
- * Logs a debug message with optional additional data.
162
- *
163
- * @param msg - The debug message to log
164
- * @param args - Additional data to include in the log entry
165
- */
161
+ * Logs a debug message with optional additional data.
162
+ *
163
+ * @param msg - The debug message to log
164
+ * @param args - Additional data to include in the log entry
165
+ */
166
166
  debug(msg, ...args) {
167
167
  this.logger.debug(msg, ...args);
168
168
  }
169
169
  /**
170
- * Logs a silly/trace level message with optional additional data.
171
- *
172
- * @param msg - The silly message to log
173
- * @param args - Additional data to include in the log entry
174
- */
170
+ * Logs a silly/trace level message with optional additional data.
171
+ *
172
+ * @param msg - The silly message to log
173
+ * @param args - Additional data to include in the log entry
174
+ */
175
175
  silly(msg, ...args) {
176
176
  this.logger.silly(msg, ...args);
177
177
  }
178
178
  /**
179
- * Static method to log an error message with a specific prefix.
180
- * Creates or retrieves a logger instance for the given prefix.
181
- *
182
- * @param prefix - The logger prefix/label to use
183
- * @param msg - The error message to log
184
- * @param args - Additional data to include in the log entry
185
- */
179
+ * Static method to log an error message with a specific prefix.
180
+ * Creates or retrieves a logger instance for the given prefix.
181
+ *
182
+ * @param prefix - The logger prefix/label to use
183
+ * @param msg - The error message to log
184
+ * @param args - Additional data to include in the log entry
185
+ */
186
186
  static Error(prefix, msg, ...args) {
187
187
  const logger = this.instance(prefix);
188
188
  logger.error(msg, ...args);
189
189
  }
190
190
  /**
191
- * Static method to log an informational message with a specific prefix.
192
- * Creates or retrieves a logger instance for the given prefix.
193
- *
194
- * @param prefix - The logger prefix/label to use
195
- * @param msg - The informational message to log
196
- * @param args - Additional data to include in the log entry
197
- */
191
+ * Static method to log an informational message with a specific prefix.
192
+ * Creates or retrieves a logger instance for the given prefix.
193
+ *
194
+ * @param prefix - The logger prefix/label to use
195
+ * @param msg - The informational message to log
196
+ * @param args - Additional data to include in the log entry
197
+ */
198
198
  static Info(prefix, msg, ...args) {
199
199
  const logger = this.instance(prefix);
200
200
  logger.info(msg, ...args);
201
201
  }
202
202
  /**
203
- * Static method to log a warning message with a specific prefix.
204
- * Creates or retrieves a logger instance for the given prefix.
205
- *
206
- * @param prefix - The logger prefix/label to use
207
- * @param msg - The warning message to log
208
- * @param args - Additional data to include in the log entry
209
- */
203
+ * Static method to log a warning message with a specific prefix.
204
+ * Creates or retrieves a logger instance for the given prefix.
205
+ *
206
+ * @param prefix - The logger prefix/label to use
207
+ * @param msg - The warning message to log
208
+ * @param args - Additional data to include in the log entry
209
+ */
210
210
  static Warn(prefix, msg, ...args) {
211
211
  const logger = this.instance(prefix);
212
212
  logger.warn(msg, ...args);
213
213
  }
214
214
  /**
215
- * Static method to log a debug message with a specific prefix.
216
- * Creates or retrieves a logger instance for the given prefix.
217
- *
218
- * @param prefix - The logger prefix/label to use
219
- * @param msg - The debug message to log
220
- * @param args - Additional data to include in the log entry
221
- */
215
+ * Static method to log a debug message with a specific prefix.
216
+ * Creates or retrieves a logger instance for the given prefix.
217
+ *
218
+ * @param prefix - The logger prefix/label to use
219
+ * @param msg - The debug message to log
220
+ * @param args - Additional data to include in the log entry
221
+ */
222
222
  static Debug(prefix, msg, ...args) {
223
223
  const logger = this.instance(prefix);
224
224
  logger.debug(msg, ...args);
225
225
  }
226
226
  /**
227
- * Static method to log a silly/trace level message with a specific prefix.
228
- * Creates or retrieves a logger instance for the given prefix.
229
- *
230
- * @param prefix - The logger prefix/label to use
231
- * @param msg - The silly message to log
232
- * @param args - Additional data to include in the log entry
233
- */
227
+ * Static method to log a silly/trace level message with a specific prefix.
228
+ * Creates or retrieves a logger instance for the given prefix.
229
+ *
230
+ * @param prefix - The logger prefix/label to use
231
+ * @param msg - The silly message to log
232
+ * @param args - Additional data to include in the log entry
233
+ */
234
234
  static Silly(prefix, msg, ...args) {
235
235
  const logger = this.instance(prefix);
236
236
  logger.silly(msg, ...args);
@@ -252,22 +252,22 @@ var CoordinatedLifecycle = class {
252
252
  this.phaseOrder.forEach((phase) => this.tasksMap.set(phase, []));
253
253
  }
254
254
  /**
255
- * Adds a lifecycle task to a specific phase.
256
- *
257
- * Tasks are executed in phase order during lifecycle operations.
258
- * Each task has a timeout to prevent hanging operations.
259
- *
260
- * @param phase - The lifecycle phase to add the task to
261
- * @param taskName - Unique name for the task (used for logging and removal)
262
- * @param task - Async function to execute during the phase
263
- * @param timeoutMs - Maximum time allowed for task execution in milliseconds
264
- * @example
265
- * ```typescript
266
- * lifecycle.addTask(StartupPhase.Services, 'start-database', async () => {
267
- * await database.connect();
268
- * }, 10000);
269
- * ```
270
- */
255
+ * Adds a lifecycle task to a specific phase.
256
+ *
257
+ * Tasks are executed in phase order during lifecycle operations.
258
+ * Each task has a timeout to prevent hanging operations.
259
+ *
260
+ * @param phase - The lifecycle phase to add the task to
261
+ * @param taskName - Unique name for the task (used for logging and removal)
262
+ * @param task - Async function to execute during the phase
263
+ * @param timeoutMs - Maximum time allowed for task execution in milliseconds
264
+ * @example
265
+ * ```typescript
266
+ * lifecycle.addTask(StartupPhase.Services, 'start-database', async () => {
267
+ * await database.connect();
268
+ * }, 10000);
269
+ * ```
270
+ */
271
271
  addTask(phase, taskName, task, timeoutMs) {
272
272
  if (!this.canAddTask()) return;
273
273
  const tasks = this.tasksMap.get(phase);
@@ -280,12 +280,12 @@ var CoordinatedLifecycle = class {
280
280
  this.logger.debug(`${chalk__default.default.italic("Added")} ${this.getTaskType()} task ${chalk__default.default.bold.cyan(taskName)} to phase ${chalk__default.default.bold.magenta(this.phaseEnum[phase])}`);
281
281
  }
282
282
  /**
283
- * Removes a lifecycle task from a specific phase.
284
- *
285
- * @param phase - The lifecycle phase to remove the task from
286
- * @param taskName - Name of the task to remove
287
- * @returns True if the task was found and removed, false otherwise
288
- */
283
+ * Removes a lifecycle task from a specific phase.
284
+ *
285
+ * @param phase - The lifecycle phase to remove the task from
286
+ * @param taskName - Name of the task to remove
287
+ * @returns True if the task was found and removed, false otherwise
288
+ */
289
289
  removeTask(phase, taskName) {
290
290
  if (!this.canRemoveTask()) return false;
291
291
  const tasks = this.tasksMap.get(phase);
@@ -300,8 +300,8 @@ var CoordinatedLifecycle = class {
300
300
  return removed;
301
301
  }
302
302
  /**
303
- * Run all tasks in a specific phase
304
- */
303
+ * Run all tasks in a specific phase
304
+ */
305
305
  async runPhase(phase) {
306
306
  const tasks = this.tasksMap.get(phase) ?? [];
307
307
  if (tasks.length === 0) {
@@ -321,8 +321,8 @@ var CoordinatedLifecycle = class {
321
321
  this.emit(`phase:${phase}:complete`);
322
322
  }
323
323
  /**
324
- * Run a single task with timeout
325
- */
324
+ * Run a single task with timeout
325
+ */
326
326
  async runTaskWithTimeout(phase, task) {
327
327
  this.logger.info(`${chalk__default.default.italic("Starting")} task ${chalk__default.default.bold.cyan(task.name)} in phase ${chalk__default.default.bold.magenta(this.phaseEnum[phase])}`);
328
328
  try {
@@ -341,14 +341,14 @@ var CoordinatedLifecycle = class {
341
341
  }
342
342
  }
343
343
  /**
344
- * Subscribe to lifecycle events
345
- */
344
+ * Subscribe to lifecycle events
345
+ */
346
346
  on(event, listener) {
347
347
  this.events.on(event, listener);
348
348
  }
349
349
  /**
350
- * Unsubscribe from lifecycle events
351
- */
350
+ * Unsubscribe from lifecycle events
351
+ */
352
352
  off(event, listener) {
353
353
  this.events.off(event, listener);
354
354
  }
@@ -433,41 +433,41 @@ var CoordinatedShutdown = class extends CoordinatedLifecycle {
433
433
  });
434
434
  }
435
435
  /**
436
- * Adds a task to a specific shutdown phase with timeout.
437
- *
438
- * @param phase - The shutdown phase from {@link ShutdownPhase}
439
- * @param taskName - Unique identifier for the task
440
- * @param task - Async function to execute
441
- * @param timeoutMs - Task timeout in milliseconds (default: 5000)
442
- */
436
+ * Adds a task to a specific shutdown phase with timeout.
437
+ *
438
+ * @param phase - The shutdown phase from {@link ShutdownPhase}
439
+ * @param taskName - Unique identifier for the task
440
+ * @param task - Async function to execute
441
+ * @param timeoutMs - Task timeout in milliseconds (default: 5000)
442
+ */
443
443
  addTask(phase, taskName, task, timeoutMs = 5e3) {
444
444
  super.addTask(phase, taskName, task, timeoutMs);
445
445
  }
446
446
  /**
447
- * Removes a task from a specific shutdown phase.
448
- *
449
- * @param phase - The shutdown phase to remove from
450
- * @param taskName - Name of the task to remove
451
- * @returns True if task was found and removed
452
- */
447
+ * Removes a task from a specific shutdown phase.
448
+ *
449
+ * @param phase - The shutdown phase to remove from
450
+ * @param taskName - Name of the task to remove
451
+ * @returns True if task was found and removed
452
+ */
453
453
  removeTask(phase, taskName) {
454
454
  return super.removeTask(phase, taskName);
455
455
  }
456
456
  /**
457
- * Executes the coordinated shutdown sequence.
458
- *
459
- * Runs all registered tasks across shutdown phases in reverse order.
460
- * Tasks within each phase are executed in parallel for faster shutdown.
461
- * Process exits with the specified code when complete.
462
- *
463
- * @param exitCode - Process exit code (default: 0)
464
- * @returns Promise that resolves when shutdown is complete
465
- * @example
466
- * ```typescript
467
- * shutdown.addTask(ShutdownPhase.Services, 'database', () => db.disconnect(), 5000);
468
- * await shutdown.run(0); // Graceful shutdown
469
- * ```
470
- */
457
+ * Executes the coordinated shutdown sequence.
458
+ *
459
+ * Runs all registered tasks across shutdown phases in reverse order.
460
+ * Tasks within each phase are executed in parallel for faster shutdown.
461
+ * Process exits with the specified code when complete.
462
+ *
463
+ * @param exitCode - Process exit code (default: `0`)
464
+ * @returns Promise that resolves when shutdown is complete
465
+ * @example
466
+ * ```typescript
467
+ * shutdown.addTask(ShutdownPhase.Services, 'database', () => db.disconnect(), 5000);
468
+ * await shutdown.run(0); // Graceful shutdown
469
+ * ```
470
+ */
471
471
  async run(exitCode = 0) {
472
472
  if (this.isShuttingDown) {
473
473
  this.logger.warn("Shutdown sequence already in progress");
@@ -494,14 +494,14 @@ var CoordinatedShutdown = class extends CoordinatedLifecycle {
494
494
  }
495
495
  }
496
496
  /**
497
- * Subscribe to shutdown events
498
- */
497
+ * Subscribe to shutdown events
498
+ */
499
499
  on(event, listener) {
500
500
  super.on(event, listener);
501
501
  }
502
502
  /**
503
- * Unsubscribe from shutdown events
504
- */
503
+ * Unsubscribe from shutdown events
504
+ */
505
505
  off(event, listener) {
506
506
  super.off(event, listener);
507
507
  }
@@ -537,9 +537,9 @@ var HealthCheck = class {
537
537
  shutdown.addTask(ShutdownPhase.StopServices, "stop-healthcheck-server", async () => await this.stop());
538
538
  }
539
539
  /**
540
- * Starts the health check server.
541
- * @returns Promise that resolves when the server is listening
542
- */
540
+ * Starts the health check server.
541
+ * @returns Promise that resolves when the server is listening
542
+ */
543
543
  async init() {
544
544
  return new Promise((resolve, reject) => {
545
545
  this.server = http.createServer((req, res) => {
@@ -576,10 +576,10 @@ var HealthCheck = class {
576
576
  });
577
577
  }
578
578
  /**
579
- * Stops the health check server.
580
- *
581
- * @returns Promise that resolves when the server is closed
582
- */
579
+ * Stops the health check server.
580
+ *
581
+ * @returns Promise that resolves when the server is closed
582
+ */
583
583
  stop() {
584
584
  if (this.server !== void 0) {
585
585
  const server = this.server;
@@ -618,32 +618,32 @@ var CooldownManager = class {
618
618
  msg;
619
619
  map = /* @__PURE__ */ new Map();
620
620
  /**
621
- * Creates a new CooldownManager instance.
622
- *
623
- * @param opts - Configuration options for the cooldown behavior
624
- */
621
+ * Creates a new CooldownManager instance.
622
+ *
623
+ * @param opts - Configuration options for the cooldown behavior
624
+ */
625
625
  constructor(opts = {}) {
626
626
  this.window = opts.cooldown ?? 1e3;
627
627
  this.Err = opts.err ?? Error;
628
628
  this.msg = opts.message ?? "Cooldown active";
629
629
  }
630
630
  /**
631
- * Records usage timestamp for a key without any cooldown checks.
632
- *
633
- * @param key - The unique identifier for the cooldown entry
634
- */
631
+ * Records usage timestamp for a key without any cooldown checks.
632
+ *
633
+ * @param key - The unique identifier for the cooldown entry
634
+ */
635
635
  set(key) {
636
636
  this.map.set(key, Date.now());
637
637
  }
638
638
  /**
639
- * Verifies cooldown status for a key and updates timestamp if not active.
640
- *
641
- * If the cooldown is still active, throws the configured error.
642
- * If not active, updates the timestamp and returns successfully.
643
- *
644
- * @param key - The unique identifier to check cooldown for
645
- * @throws An {@link Err} When the cooldown is still active for the given key
646
- */
639
+ * Verifies cooldown status for a key and updates timestamp if not active.
640
+ *
641
+ * If the cooldown is still active, throws the configured error.
642
+ * If not active, updates the timestamp and returns successfully.
643
+ *
644
+ * @param key - The unique identifier to check cooldown for
645
+ * @throws An {@link Err} When the cooldown is still active for the given key
646
+ */
647
647
  check(key) {
648
648
  const now = Date.now();
649
649
  const last = this.map.get(key);
@@ -657,20 +657,20 @@ var CooldownManager = class {
657
657
  this.map.set(key, now);
658
658
  }
659
659
  /**
660
- * Checks if a key is currently cooling down without updating timestamp.
661
- *
662
- * @param key - The unique identifier to check
663
- * @returns True if the key is still cooling down, false otherwise
664
- */
660
+ * Checks if a key is currently cooling down without updating timestamp.
661
+ *
662
+ * @param key - The unique identifier to check
663
+ * @returns True if the key is still cooling down, false otherwise
664
+ */
665
665
  isActive(key) {
666
666
  const last = this.map.get(key);
667
667
  return last !== void 0 && Date.now() - last < this.window;
668
668
  }
669
669
  /**
670
- * Removes a key from the cooldown map.
671
- *
672
- * @param key - The unique identifier to remove (useful for manual resets)
673
- */
670
+ * Removes a key from the cooldown map.
671
+ *
672
+ * @param key - The unique identifier to remove (useful for manual resets)
673
+ */
674
674
  clear(key) {
675
675
  this.map.delete(key);
676
676
  }
@@ -704,13 +704,13 @@ var CoordinatedStartup = class extends CoordinatedLifecycle {
704
704
  super("CoordinatedStartup", PHASE_ORDER2, StartupPhase);
705
705
  }
706
706
  /**
707
- * Adds a task to a specific startup phase with timeout.
708
- *
709
- * @param phase - The startup phase from {@link StartupPhase}
710
- * @param taskName - Unique identifier for the task
711
- * @param task - Async function to execute
712
- * @param timeoutMs - Task timeout in milliseconds (default: 10000)
713
- */
707
+ * Adds a task to a specific startup phase with timeout.
708
+ *
709
+ * @param phase - The startup phase from {@link StartupPhase}
710
+ * @param taskName - Unique identifier for the task
711
+ * @param task - Async function to execute
712
+ * @param timeoutMs - Task timeout in milliseconds (default: 10000)
713
+ */
714
714
  addTask(phase, taskName, task, timeoutMs = 1e4) {
715
715
  super.addTask(phase, taskName, task, timeoutMs);
716
716
  }
@@ -750,21 +750,21 @@ var CoordinatedStartup = class extends CoordinatedLifecycle {
750
750
  return results;
751
751
  }
752
752
  /**
753
- * Executes the coordinated startup sequence.
754
- *
755
- * Runs all registered tasks across startup phases in the correct order.
756
- * Each phase completes before the next phase begins. Tasks within a phase
757
- * are executed sequentially to maintain predictable initialization.
758
- *
759
- * @returns Promise that resolves when startup is complete
760
- * @throws An {@link Error} If startup fails or is called multiple times
761
- * @example
762
- * ```typescript
763
- * const startup = new CoordinatedStartup();
764
- * startup.addTask(StartupPhase.Services, 'database', () => db.connect(), 10000);
765
- * await startup.run();
766
- * ```
767
- */
753
+ * Executes the coordinated startup sequence.
754
+ *
755
+ * Runs all registered tasks across startup phases in the correct order.
756
+ * Each phase completes before the next phase begins. Tasks within a phase
757
+ * are executed sequentially to maintain predictable initialization.
758
+ *
759
+ * @returns Promise that resolves when startup is complete
760
+ * @throws An {@link Error} If startup fails or is called multiple times
761
+ * @example
762
+ * ```typescript
763
+ * const startup = new CoordinatedStartup();
764
+ * startup.addTask(StartupPhase.Services, 'database', () => db.connect(), 10000);
765
+ * await startup.run();
766
+ * ```
767
+ */
768
768
  async run() {
769
769
  if (this.hasStarted) {
770
770
  this.logger.warn("Startup sequence has already completed");
@@ -791,26 +791,26 @@ var CoordinatedStartup = class extends CoordinatedLifecycle {
791
791
  }
792
792
  }
793
793
  /**
794
- * Subscribe to startup events
795
- */
794
+ * Subscribe to startup events
795
+ */
796
796
  on(event, listener) {
797
797
  super.on(event, listener);
798
798
  }
799
799
  /**
800
- * Unsubscribe from startup events
801
- */
800
+ * Unsubscribe from startup events
801
+ */
802
802
  off(event, listener) {
803
803
  super.off(event, listener);
804
804
  }
805
805
  /**
806
- * Check if startup has completed
807
- */
806
+ * Check if startup has completed
807
+ */
808
808
  get isReady() {
809
809
  return this.hasStarted;
810
810
  }
811
811
  /**
812
- * Check if startup is currently running
813
- */
812
+ * Check if startup is currently running
813
+ */
814
814
  get isRunning() {
815
815
  return this.isStartingUp;
816
816
  }