@darthcav/ts-utils 0.7.0 → 0.8.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/README.md +58 -13
- package/dist/__tests__/main.test.js +43 -26
- package/dist/__tests__/main.test.js.map +1 -1
- package/dist/__tests__/millisecondsToString.test.d.ts +2 -0
- package/dist/__tests__/millisecondsToString.test.d.ts.map +1 -0
- package/dist/__tests__/millisecondsToString.test.js +31 -0
- package/dist/__tests__/millisecondsToString.test.js.map +1 -0
- package/dist/__tests__/monitorMemory.test.d.ts +2 -0
- package/dist/__tests__/monitorMemory.test.d.ts.map +1 -0
- package/dist/__tests__/monitorMemory.test.js +36 -0
- package/dist/__tests__/monitorMemory.test.js.map +1 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/loggers/getDummyLogger.d.ts +12 -0
- package/dist/loggers/getDummyLogger.d.ts.map +1 -0
- package/dist/loggers/getDummyLogger.js +29 -0
- package/dist/loggers/getDummyLogger.js.map +1 -0
- package/dist/main.d.ts +44 -25
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +27 -12
- package/dist/main.js.map +1 -1
- package/dist/millisecondsToString.d.ts +19 -0
- package/dist/millisecondsToString.d.ts.map +1 -0
- package/dist/millisecondsToString.js +37 -0
- package/dist/millisecondsToString.js.map +1 -0
- package/dist/monitorMemory.d.ts +20 -0
- package/dist/monitorMemory.d.ts.map +1 -0
- package/dist/monitorMemory.js +28 -0
- package/dist/monitorMemory.js.map +1 -0
- package/package.json +3 -3
- package/src/__tests__/main.test.ts +76 -48
- package/src/__tests__/millisecondsToString.test.ts +37 -0
- package/src/__tests__/monitorMemory.test.ts +47 -0
- package/src/index.ts +11 -1
- package/src/loggers/getDummyLogger.ts +31 -0
- package/src/main.ts +96 -39
- package/src/millisecondsToString.ts +37 -0
- package/src/monitorMemory.ts +34 -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
|
+
}
|