@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.mjs CHANGED
@@ -107,124 +107,124 @@ ${parts.join(" ")}`;
107
107
  });
108
108
  }
109
109
  /**
110
- * Logs an error message with optional additional data.
111
- *
112
- * @param msg - The error message to log
113
- * @param args - Additional data to include in the log entry
114
- */
110
+ * Logs an error message with optional additional data.
111
+ *
112
+ * @param msg - The error message to log
113
+ * @param args - Additional data to include in the log entry
114
+ */
115
115
  error(msg, ...args) {
116
116
  this.logger.error(msg, ...args);
117
117
  }
118
118
  /**
119
- * Logs a warning message with optional additional data.
120
- *
121
- * @param msg - The warning message to log
122
- * @param args - Additional data to include in the log entry
123
- */
119
+ * Logs a warning message with optional additional data.
120
+ *
121
+ * @param msg - The warning message to log
122
+ * @param args - Additional data to include in the log entry
123
+ */
124
124
  warn(msg, ...args) {
125
125
  this.logger.warn(msg, ...args);
126
126
  }
127
127
  /**
128
- * Logs an informational message with optional additional data.
129
- *
130
- * @param msg - The informational message to log
131
- * @param args - Additional data to include in the log entry
132
- */
128
+ * Logs an informational message with optional additional data.
129
+ *
130
+ * @param msg - The informational message to log
131
+ * @param args - Additional data to include in the log entry
132
+ */
133
133
  info(msg, ...args) {
134
134
  this.logger.info(msg, ...args);
135
135
  }
136
136
  /**
137
- * Logs an HTTP-related message with optional additional data.
138
- *
139
- * @param msg - The HTTP message to log
140
- * @param args - Additional data to include in the log entry
141
- */
137
+ * Logs an HTTP-related message with optional additional data.
138
+ *
139
+ * @param msg - The HTTP message to log
140
+ * @param args - Additional data to include in the log entry
141
+ */
142
142
  http(msg, ...args) {
143
143
  this.logger.http(msg, ...args);
144
144
  }
145
145
  /**
146
- * Logs a verbose message with optional additional data.
147
- *
148
- * @param msg - The verbose message to log
149
- * @param args - Additional data to include in the log entry
150
- */
146
+ * Logs a verbose message with optional additional data.
147
+ *
148
+ * @param msg - The verbose message to log
149
+ * @param args - Additional data to include in the log entry
150
+ */
151
151
  verbose(msg, ...args) {
152
152
  this.logger.verbose(msg, ...args);
153
153
  }
154
154
  /**
155
- * Logs a debug message with optional additional data.
156
- *
157
- * @param msg - The debug message to log
158
- * @param args - Additional data to include in the log entry
159
- */
155
+ * Logs a debug message with optional additional data.
156
+ *
157
+ * @param msg - The debug message to log
158
+ * @param args - Additional data to include in the log entry
159
+ */
160
160
  debug(msg, ...args) {
161
161
  this.logger.debug(msg, ...args);
162
162
  }
163
163
  /**
164
- * Logs a silly/trace level message with optional additional data.
165
- *
166
- * @param msg - The silly message to log
167
- * @param args - Additional data to include in the log entry
168
- */
164
+ * Logs a silly/trace level message with optional additional data.
165
+ *
166
+ * @param msg - The silly message to log
167
+ * @param args - Additional data to include in the log entry
168
+ */
169
169
  silly(msg, ...args) {
170
170
  this.logger.silly(msg, ...args);
171
171
  }
172
172
  /**
173
- * Static method to log an error message with a specific prefix.
174
- * Creates or retrieves a logger instance for the given prefix.
175
- *
176
- * @param prefix - The logger prefix/label to use
177
- * @param msg - The error message to log
178
- * @param args - Additional data to include in the log entry
179
- */
173
+ * Static method to log an error message with a specific prefix.
174
+ * Creates or retrieves a logger instance for the given prefix.
175
+ *
176
+ * @param prefix - The logger prefix/label to use
177
+ * @param msg - The error message to log
178
+ * @param args - Additional data to include in the log entry
179
+ */
180
180
  static Error(prefix, msg, ...args) {
181
181
  const logger = this.instance(prefix);
182
182
  logger.error(msg, ...args);
183
183
  }
184
184
  /**
185
- * Static method to log an informational message with a specific prefix.
186
- * Creates or retrieves a logger instance for the given prefix.
187
- *
188
- * @param prefix - The logger prefix/label to use
189
- * @param msg - The informational message to log
190
- * @param args - Additional data to include in the log entry
191
- */
185
+ * Static method to log an informational message with a specific prefix.
186
+ * Creates or retrieves a logger instance for the given prefix.
187
+ *
188
+ * @param prefix - The logger prefix/label to use
189
+ * @param msg - The informational message to log
190
+ * @param args - Additional data to include in the log entry
191
+ */
192
192
  static Info(prefix, msg, ...args) {
193
193
  const logger = this.instance(prefix);
194
194
  logger.info(msg, ...args);
195
195
  }
196
196
  /**
197
- * Static method to log a warning message with a specific prefix.
198
- * Creates or retrieves a logger instance for the given prefix.
199
- *
200
- * @param prefix - The logger prefix/label to use
201
- * @param msg - The warning message to log
202
- * @param args - Additional data to include in the log entry
203
- */
197
+ * Static method to log a warning message with a specific prefix.
198
+ * Creates or retrieves a logger instance for the given prefix.
199
+ *
200
+ * @param prefix - The logger prefix/label to use
201
+ * @param msg - The warning message to log
202
+ * @param args - Additional data to include in the log entry
203
+ */
204
204
  static Warn(prefix, msg, ...args) {
205
205
  const logger = this.instance(prefix);
206
206
  logger.warn(msg, ...args);
207
207
  }
208
208
  /**
209
- * Static method to log a debug message with a specific prefix.
210
- * Creates or retrieves a logger instance for the given prefix.
211
- *
212
- * @param prefix - The logger prefix/label to use
213
- * @param msg - The debug message to log
214
- * @param args - Additional data to include in the log entry
215
- */
209
+ * Static method to log a debug message with a specific prefix.
210
+ * Creates or retrieves a logger instance for the given prefix.
211
+ *
212
+ * @param prefix - The logger prefix/label to use
213
+ * @param msg - The debug message to log
214
+ * @param args - Additional data to include in the log entry
215
+ */
216
216
  static Debug(prefix, msg, ...args) {
217
217
  const logger = this.instance(prefix);
218
218
  logger.debug(msg, ...args);
219
219
  }
220
220
  /**
221
- * Static method to log a silly/trace level message with a specific prefix.
222
- * Creates or retrieves a logger instance for the given prefix.
223
- *
224
- * @param prefix - The logger prefix/label to use
225
- * @param msg - The silly message to log
226
- * @param args - Additional data to include in the log entry
227
- */
221
+ * Static method to log a silly/trace level message with a specific prefix.
222
+ * Creates or retrieves a logger instance for the given prefix.
223
+ *
224
+ * @param prefix - The logger prefix/label to use
225
+ * @param msg - The silly message to log
226
+ * @param args - Additional data to include in the log entry
227
+ */
228
228
  static Silly(prefix, msg, ...args) {
229
229
  const logger = this.instance(prefix);
230
230
  logger.silly(msg, ...args);
@@ -246,22 +246,22 @@ var CoordinatedLifecycle = class {
246
246
  this.phaseOrder.forEach((phase) => this.tasksMap.set(phase, []));
247
247
  }
248
248
  /**
249
- * Adds a lifecycle task to a specific phase.
250
- *
251
- * Tasks are executed in phase order during lifecycle operations.
252
- * Each task has a timeout to prevent hanging operations.
253
- *
254
- * @param phase - The lifecycle phase to add the task to
255
- * @param taskName - Unique name for the task (used for logging and removal)
256
- * @param task - Async function to execute during the phase
257
- * @param timeoutMs - Maximum time allowed for task execution in milliseconds
258
- * @example
259
- * ```typescript
260
- * lifecycle.addTask(StartupPhase.Services, 'start-database', async () => {
261
- * await database.connect();
262
- * }, 10000);
263
- * ```
264
- */
249
+ * Adds a lifecycle task to a specific phase.
250
+ *
251
+ * Tasks are executed in phase order during lifecycle operations.
252
+ * Each task has a timeout to prevent hanging operations.
253
+ *
254
+ * @param phase - The lifecycle phase to add the task to
255
+ * @param taskName - Unique name for the task (used for logging and removal)
256
+ * @param task - Async function to execute during the phase
257
+ * @param timeoutMs - Maximum time allowed for task execution in milliseconds
258
+ * @example
259
+ * ```typescript
260
+ * lifecycle.addTask(StartupPhase.Services, 'start-database', async () => {
261
+ * await database.connect();
262
+ * }, 10000);
263
+ * ```
264
+ */
265
265
  addTask(phase, taskName, task, timeoutMs) {
266
266
  if (!this.canAddTask()) return;
267
267
  const tasks = this.tasksMap.get(phase);
@@ -274,12 +274,12 @@ var CoordinatedLifecycle = class {
274
274
  this.logger.debug(`${chalk.italic("Added")} ${this.getTaskType()} task ${chalk.bold.cyan(taskName)} to phase ${chalk.bold.magenta(this.phaseEnum[phase])}`);
275
275
  }
276
276
  /**
277
- * Removes a lifecycle task from a specific phase.
278
- *
279
- * @param phase - The lifecycle phase to remove the task from
280
- * @param taskName - Name of the task to remove
281
- * @returns True if the task was found and removed, false otherwise
282
- */
277
+ * Removes a lifecycle task from a specific phase.
278
+ *
279
+ * @param phase - The lifecycle phase to remove the task from
280
+ * @param taskName - Name of the task to remove
281
+ * @returns True if the task was found and removed, false otherwise
282
+ */
283
283
  removeTask(phase, taskName) {
284
284
  if (!this.canRemoveTask()) return false;
285
285
  const tasks = this.tasksMap.get(phase);
@@ -294,8 +294,8 @@ var CoordinatedLifecycle = class {
294
294
  return removed;
295
295
  }
296
296
  /**
297
- * Run all tasks in a specific phase
298
- */
297
+ * Run all tasks in a specific phase
298
+ */
299
299
  async runPhase(phase) {
300
300
  const tasks = this.tasksMap.get(phase) ?? [];
301
301
  if (tasks.length === 0) {
@@ -315,8 +315,8 @@ var CoordinatedLifecycle = class {
315
315
  this.emit(`phase:${phase}:complete`);
316
316
  }
317
317
  /**
318
- * Run a single task with timeout
319
- */
318
+ * Run a single task with timeout
319
+ */
320
320
  async runTaskWithTimeout(phase, task) {
321
321
  this.logger.info(`${chalk.italic("Starting")} task ${chalk.bold.cyan(task.name)} in phase ${chalk.bold.magenta(this.phaseEnum[phase])}`);
322
322
  try {
@@ -335,14 +335,14 @@ var CoordinatedLifecycle = class {
335
335
  }
336
336
  }
337
337
  /**
338
- * Subscribe to lifecycle events
339
- */
338
+ * Subscribe to lifecycle events
339
+ */
340
340
  on(event, listener) {
341
341
  this.events.on(event, listener);
342
342
  }
343
343
  /**
344
- * Unsubscribe from lifecycle events
345
- */
344
+ * Unsubscribe from lifecycle events
345
+ */
346
346
  off(event, listener) {
347
347
  this.events.off(event, listener);
348
348
  }
@@ -427,41 +427,41 @@ var CoordinatedShutdown = class extends CoordinatedLifecycle {
427
427
  });
428
428
  }
429
429
  /**
430
- * Adds a task to a specific shutdown phase with timeout.
431
- *
432
- * @param phase - The shutdown phase from {@link ShutdownPhase}
433
- * @param taskName - Unique identifier for the task
434
- * @param task - Async function to execute
435
- * @param timeoutMs - Task timeout in milliseconds (default: 5000)
436
- */
430
+ * Adds a task to a specific shutdown phase with timeout.
431
+ *
432
+ * @param phase - The shutdown phase from {@link ShutdownPhase}
433
+ * @param taskName - Unique identifier for the task
434
+ * @param task - Async function to execute
435
+ * @param timeoutMs - Task timeout in milliseconds (default: 5000)
436
+ */
437
437
  addTask(phase, taskName, task, timeoutMs = 5e3) {
438
438
  super.addTask(phase, taskName, task, timeoutMs);
439
439
  }
440
440
  /**
441
- * Removes a task from a specific shutdown phase.
442
- *
443
- * @param phase - The shutdown phase to remove from
444
- * @param taskName - Name of the task to remove
445
- * @returns True if task was found and removed
446
- */
441
+ * Removes a task from a specific shutdown phase.
442
+ *
443
+ * @param phase - The shutdown phase to remove from
444
+ * @param taskName - Name of the task to remove
445
+ * @returns True if task was found and removed
446
+ */
447
447
  removeTask(phase, taskName) {
448
448
  return super.removeTask(phase, taskName);
449
449
  }
450
450
  /**
451
- * Executes the coordinated shutdown sequence.
452
- *
453
- * Runs all registered tasks across shutdown phases in reverse order.
454
- * Tasks within each phase are executed in parallel for faster shutdown.
455
- * Process exits with the specified code when complete.
456
- *
457
- * @param exitCode - Process exit code (default: 0)
458
- * @returns Promise that resolves when shutdown is complete
459
- * @example
460
- * ```typescript
461
- * shutdown.addTask(ShutdownPhase.Services, 'database', () => db.disconnect(), 5000);
462
- * await shutdown.run(0); // Graceful shutdown
463
- * ```
464
- */
451
+ * Executes the coordinated shutdown sequence.
452
+ *
453
+ * Runs all registered tasks across shutdown phases in reverse order.
454
+ * Tasks within each phase are executed in parallel for faster shutdown.
455
+ * Process exits with the specified code when complete.
456
+ *
457
+ * @param exitCode - Process exit code (default: `0`)
458
+ * @returns Promise that resolves when shutdown is complete
459
+ * @example
460
+ * ```typescript
461
+ * shutdown.addTask(ShutdownPhase.Services, 'database', () => db.disconnect(), 5000);
462
+ * await shutdown.run(0); // Graceful shutdown
463
+ * ```
464
+ */
465
465
  async run(exitCode = 0) {
466
466
  if (this.isShuttingDown) {
467
467
  this.logger.warn("Shutdown sequence already in progress");
@@ -488,14 +488,14 @@ var CoordinatedShutdown = class extends CoordinatedLifecycle {
488
488
  }
489
489
  }
490
490
  /**
491
- * Subscribe to shutdown events
492
- */
491
+ * Subscribe to shutdown events
492
+ */
493
493
  on(event, listener) {
494
494
  super.on(event, listener);
495
495
  }
496
496
  /**
497
- * Unsubscribe from shutdown events
498
- */
497
+ * Unsubscribe from shutdown events
498
+ */
499
499
  off(event, listener) {
500
500
  super.off(event, listener);
501
501
  }
@@ -531,9 +531,9 @@ var HealthCheck = class {
531
531
  shutdown.addTask(ShutdownPhase.StopServices, "stop-healthcheck-server", async () => await this.stop());
532
532
  }
533
533
  /**
534
- * Starts the health check server.
535
- * @returns Promise that resolves when the server is listening
536
- */
534
+ * Starts the health check server.
535
+ * @returns Promise that resolves when the server is listening
536
+ */
537
537
  async init() {
538
538
  return new Promise((resolve, reject) => {
539
539
  this.server = createServer((req, res) => {
@@ -570,10 +570,10 @@ var HealthCheck = class {
570
570
  });
571
571
  }
572
572
  /**
573
- * Stops the health check server.
574
- *
575
- * @returns Promise that resolves when the server is closed
576
- */
573
+ * Stops the health check server.
574
+ *
575
+ * @returns Promise that resolves when the server is closed
576
+ */
577
577
  stop() {
578
578
  if (this.server !== void 0) {
579
579
  const server = this.server;
@@ -612,32 +612,32 @@ var CooldownManager = class {
612
612
  msg;
613
613
  map = /* @__PURE__ */ new Map();
614
614
  /**
615
- * Creates a new CooldownManager instance.
616
- *
617
- * @param opts - Configuration options for the cooldown behavior
618
- */
615
+ * Creates a new CooldownManager instance.
616
+ *
617
+ * @param opts - Configuration options for the cooldown behavior
618
+ */
619
619
  constructor(opts = {}) {
620
620
  this.window = opts.cooldown ?? 1e3;
621
621
  this.Err = opts.err ?? Error;
622
622
  this.msg = opts.message ?? "Cooldown active";
623
623
  }
624
624
  /**
625
- * Records usage timestamp for a key without any cooldown checks.
626
- *
627
- * @param key - The unique identifier for the cooldown entry
628
- */
625
+ * Records usage timestamp for a key without any cooldown checks.
626
+ *
627
+ * @param key - The unique identifier for the cooldown entry
628
+ */
629
629
  set(key) {
630
630
  this.map.set(key, Date.now());
631
631
  }
632
632
  /**
633
- * Verifies cooldown status for a key and updates timestamp if not active.
634
- *
635
- * If the cooldown is still active, throws the configured error.
636
- * If not active, updates the timestamp and returns successfully.
637
- *
638
- * @param key - The unique identifier to check cooldown for
639
- * @throws An {@link Err} When the cooldown is still active for the given key
640
- */
633
+ * Verifies cooldown status for a key and updates timestamp if not active.
634
+ *
635
+ * If the cooldown is still active, throws the configured error.
636
+ * If not active, updates the timestamp and returns successfully.
637
+ *
638
+ * @param key - The unique identifier to check cooldown for
639
+ * @throws An {@link Err} When the cooldown is still active for the given key
640
+ */
641
641
  check(key) {
642
642
  const now = Date.now();
643
643
  const last = this.map.get(key);
@@ -651,20 +651,20 @@ var CooldownManager = class {
651
651
  this.map.set(key, now);
652
652
  }
653
653
  /**
654
- * Checks if a key is currently cooling down without updating timestamp.
655
- *
656
- * @param key - The unique identifier to check
657
- * @returns True if the key is still cooling down, false otherwise
658
- */
654
+ * Checks if a key is currently cooling down without updating timestamp.
655
+ *
656
+ * @param key - The unique identifier to check
657
+ * @returns True if the key is still cooling down, false otherwise
658
+ */
659
659
  isActive(key) {
660
660
  const last = this.map.get(key);
661
661
  return last !== void 0 && Date.now() - last < this.window;
662
662
  }
663
663
  /**
664
- * Removes a key from the cooldown map.
665
- *
666
- * @param key - The unique identifier to remove (useful for manual resets)
667
- */
664
+ * Removes a key from the cooldown map.
665
+ *
666
+ * @param key - The unique identifier to remove (useful for manual resets)
667
+ */
668
668
  clear(key) {
669
669
  this.map.delete(key);
670
670
  }
@@ -698,13 +698,13 @@ var CoordinatedStartup = class extends CoordinatedLifecycle {
698
698
  super("CoordinatedStartup", PHASE_ORDER2, StartupPhase);
699
699
  }
700
700
  /**
701
- * Adds a task to a specific startup phase with timeout.
702
- *
703
- * @param phase - The startup phase from {@link StartupPhase}
704
- * @param taskName - Unique identifier for the task
705
- * @param task - Async function to execute
706
- * @param timeoutMs - Task timeout in milliseconds (default: 10000)
707
- */
701
+ * Adds a task to a specific startup phase with timeout.
702
+ *
703
+ * @param phase - The startup phase from {@link StartupPhase}
704
+ * @param taskName - Unique identifier for the task
705
+ * @param task - Async function to execute
706
+ * @param timeoutMs - Task timeout in milliseconds (default: 10000)
707
+ */
708
708
  addTask(phase, taskName, task, timeoutMs = 1e4) {
709
709
  super.addTask(phase, taskName, task, timeoutMs);
710
710
  }
@@ -744,21 +744,21 @@ var CoordinatedStartup = class extends CoordinatedLifecycle {
744
744
  return results;
745
745
  }
746
746
  /**
747
- * Executes the coordinated startup sequence.
748
- *
749
- * Runs all registered tasks across startup phases in the correct order.
750
- * Each phase completes before the next phase begins. Tasks within a phase
751
- * are executed sequentially to maintain predictable initialization.
752
- *
753
- * @returns Promise that resolves when startup is complete
754
- * @throws An {@link Error} If startup fails or is called multiple times
755
- * @example
756
- * ```typescript
757
- * const startup = new CoordinatedStartup();
758
- * startup.addTask(StartupPhase.Services, 'database', () => db.connect(), 10000);
759
- * await startup.run();
760
- * ```
761
- */
747
+ * Executes the coordinated startup sequence.
748
+ *
749
+ * Runs all registered tasks across startup phases in the correct order.
750
+ * Each phase completes before the next phase begins. Tasks within a phase
751
+ * are executed sequentially to maintain predictable initialization.
752
+ *
753
+ * @returns Promise that resolves when startup is complete
754
+ * @throws An {@link Error} If startup fails or is called multiple times
755
+ * @example
756
+ * ```typescript
757
+ * const startup = new CoordinatedStartup();
758
+ * startup.addTask(StartupPhase.Services, 'database', () => db.connect(), 10000);
759
+ * await startup.run();
760
+ * ```
761
+ */
762
762
  async run() {
763
763
  if (this.hasStarted) {
764
764
  this.logger.warn("Startup sequence has already completed");
@@ -785,26 +785,26 @@ var CoordinatedStartup = class extends CoordinatedLifecycle {
785
785
  }
786
786
  }
787
787
  /**
788
- * Subscribe to startup events
789
- */
788
+ * Subscribe to startup events
789
+ */
790
790
  on(event, listener) {
791
791
  super.on(event, listener);
792
792
  }
793
793
  /**
794
- * Unsubscribe from startup events
795
- */
794
+ * Unsubscribe from startup events
795
+ */
796
796
  off(event, listener) {
797
797
  super.off(event, listener);
798
798
  }
799
799
  /**
800
- * Check if startup has completed
801
- */
800
+ * Check if startup has completed
801
+ */
802
802
  get isReady() {
803
803
  return this.hasStarted;
804
804
  }
805
805
  /**
806
- * Check if startup is currently running
807
- */
806
+ * Check if startup is currently running
807
+ */
808
808
  get isRunning() {
809
809
  return this.isStartingUp;
810
810
  }