@darthcav/ts-utils 0.8.0 → 0.8.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.
Files changed (50) hide show
  1. package/README.md +70 -12
  2. package/dist/__tests__/main.test.js +43 -6
  3. package/dist/__tests__/main.test.js.map +1 -1
  4. package/dist/__tests__/millisecondsToString.test.d.ts +2 -0
  5. package/dist/__tests__/millisecondsToString.test.d.ts.map +1 -0
  6. package/dist/__tests__/millisecondsToString.test.js +31 -0
  7. package/dist/__tests__/millisecondsToString.test.js.map +1 -0
  8. package/dist/__tests__/monitorMemory.test.d.ts +2 -0
  9. package/dist/__tests__/monitorMemory.test.d.ts.map +1 -0
  10. package/dist/__tests__/monitorMemory.test.js +36 -0
  11. package/dist/__tests__/monitorMemory.test.js.map +1 -0
  12. package/dist/__tests__/noop.test.d.ts +2 -0
  13. package/dist/__tests__/noop.test.d.ts.map +1 -0
  14. package/dist/__tests__/noop.test.js +12 -0
  15. package/dist/__tests__/noop.test.js.map +1 -0
  16. package/dist/index.d.ts +5 -1
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +5 -1
  19. package/dist/index.js.map +1 -1
  20. package/dist/loggers/getDummyLogger.d.ts +12 -0
  21. package/dist/loggers/getDummyLogger.d.ts.map +1 -0
  22. package/dist/loggers/getDummyLogger.js +29 -0
  23. package/dist/loggers/getDummyLogger.js.map +1 -0
  24. package/dist/main.d.ts +43 -24
  25. package/dist/main.d.ts.map +1 -1
  26. package/dist/main.js +27 -8
  27. package/dist/main.js.map +1 -1
  28. package/dist/millisecondsToString.d.ts +19 -0
  29. package/dist/millisecondsToString.d.ts.map +1 -0
  30. package/dist/millisecondsToString.js +37 -0
  31. package/dist/millisecondsToString.js.map +1 -0
  32. package/dist/monitorMemory.d.ts +20 -0
  33. package/dist/monitorMemory.d.ts.map +1 -0
  34. package/dist/monitorMemory.js +28 -0
  35. package/dist/monitorMemory.js.map +1 -0
  36. package/dist/noop.d.ts +8 -0
  37. package/dist/noop.d.ts.map +1 -0
  38. package/dist/noop.js +8 -0
  39. package/dist/noop.js.map +1 -0
  40. package/package.json +3 -3
  41. package/src/__tests__/main.test.ts +76 -7
  42. package/src/__tests__/millisecondsToString.test.ts +37 -0
  43. package/src/__tests__/monitorMemory.test.ts +47 -0
  44. package/src/__tests__/noop.test.ts +13 -0
  45. package/src/index.ts +13 -1
  46. package/src/loggers/getDummyLogger.ts +30 -0
  47. package/src/main.ts +95 -34
  48. package/src/millisecondsToString.ts +37 -0
  49. package/src/monitorMemory.ts +34 -0
  50. package/src/noop.ts +8 -0
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Converts a duration in milliseconds to a human-readable string such as
3
+ * `"1d 4h 32m 10s"`.
4
+ *
5
+ * Sub-second values are rounded to the nearest second. Leading zero components
6
+ * are omitted except for seconds, which are always included.
7
+ *
8
+ * @param ms - Duration in milliseconds.
9
+ * @returns A formatted duration string.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * millisecondsToString(3_661_000) // "1h 1m 1s"
14
+ * millisecondsToString(90_000) // "1m 30s"
15
+ * millisecondsToString(5_000) // "5s"
16
+ * ```
17
+ */
18
+ export default function millisecondsToString(ms: number): string {
19
+ const totalSeconds = Math.round(ms / 1000)
20
+ const days = Math.floor(totalSeconds / 86400)
21
+ const hours = Math.floor((totalSeconds % 86400) / 3600)
22
+ const minutes = Math.floor((totalSeconds % 3600) / 60)
23
+ const seconds = totalSeconds % 60
24
+
25
+ const parts: string[] = []
26
+ if (days > 0) {
27
+ parts.push(`${days}d`)
28
+ }
29
+ if (hours > 0) {
30
+ parts.push(`${hours}h`)
31
+ }
32
+ if (minutes > 0) {
33
+ parts.push(`${minutes}m`)
34
+ }
35
+ parts.push(`${seconds}s`)
36
+ return parts.join(" ")
37
+ }
@@ -0,0 +1,34 @@
1
+ import { memoryUsage, uptime } from "node:process"
2
+ import type { Logger } from "@logtape/logtape"
3
+ import millisecondsToString from "./millisecondsToString.ts"
4
+
5
+ /**
6
+ * Starts a periodic interval that logs process uptime and memory usage.
7
+ *
8
+ * Memory figures are reported in bytes as returned by Node.js
9
+ * {@link https://nodejs.org/api/process.html#processmemoryusage | process.memoryUsage()}.
10
+ *
11
+ * @param logger - Logger instance used to emit the memory reports.
12
+ * @param hours - Interval between reports in hours. Defaults to `24`.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import { getConsoleLogger, monitorMemory } from "@darthcav/ts-utils"
17
+ *
18
+ * monitorMemory(getConsoleLogger()) // every 24 hours
19
+ * monitorMemory(getConsoleLogger(), 1) // every hour
20
+ * ```
21
+ */
22
+ export default function monitorMemory(
23
+ logger: Logger,
24
+ hours: number = 24,
25
+ ): void {
26
+ const delay = 60 * 60 * 1_000 * hours
27
+ setInterval(() => {
28
+ const { rss, heapTotal, heapUsed, external } = memoryUsage()
29
+ logger.info(`Process uptime: ${millisecondsToString(uptime() * 1_000)}`)
30
+ logger.info(
31
+ `Process memory - Resident set size: ${rss} | Heap total: ${heapTotal} | Heap used: ${heapUsed} | External: ${external}`,
32
+ )
33
+ }, delay)
34
+ }
package/src/noop.ts ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * A no-op function that does nothing and returns `void`.
3
+ *
4
+ * Useful as a placeholder callback, default handler, or in tests.
5
+ */
6
+ const noop = (): void => {}
7
+
8
+ export default noop