@darthcav/ts-utils 0.10.6 → 0.10.8
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 +11 -9
- package/dist/millisecondsToString.d.ts +8 -6
- package/dist/millisecondsToString.d.ts.map +1 -1
- package/dist/millisecondsToString.js +29 -13
- package/dist/millisecondsToString.js.map +1 -1
- package/dist/monitorMemory.d.ts +11 -4
- package/dist/monitorMemory.d.ts.map +1 -1
- package/dist/monitorMemory.js +17 -6
- package/dist/monitorMemory.js.map +1 -1
- package/package.json +6 -6
- package/src/millisecondsToString.ts +30 -13
- package/src/monitorMemory.ts +22 -6
package/README.md
CHANGED
|
@@ -52,8 +52,8 @@ const logger = await getConsoleLogger("my-app", "debug")
|
|
|
52
52
|
|
|
53
53
|
### `monitorMemory`
|
|
54
54
|
|
|
55
|
-
Starts a periodic interval that logs process uptime and memory usage (in bytes)
|
|
56
|
-
defaults to every 24 hours.
|
|
55
|
+
Starts a periodic interval that logs process uptime and memory usage (in bytes) through a
|
|
56
|
+
`"monitorMemory"` child category of the given logger. The interval defaults to every 24 hours.
|
|
57
57
|
|
|
58
58
|
```ts
|
|
59
59
|
import { getConsoleLogger, main, monitorMemory } from "@darthcav/ts-utils"
|
|
@@ -68,18 +68,20 @@ main("my-app", logger, () => {
|
|
|
68
68
|
|
|
69
69
|
### `millisecondsToString`
|
|
70
70
|
|
|
71
|
-
Converts a duration in milliseconds to a human-readable string via `Intl.DurationFormat`.
|
|
72
|
-
|
|
73
|
-
zero-
|
|
74
|
-
|
|
75
|
-
|
|
71
|
+
Converts a duration in milliseconds to a human-readable string via `Intl.DurationFormat`.
|
|
72
|
+
Milliseconds are shown as their own integer component (fractional inputs are rounded to the nearest
|
|
73
|
+
millisecond), and any zero-valued components are omitted — so a zero-millisecond input returns an
|
|
74
|
+
empty string. Negative durations are formatted from their magnitude and prefixed with `"-"`. An
|
|
75
|
+
optional BCP 47 locale tag (default `"en"`) controls the unit labels. Throws a `RangeError` if `ms`
|
|
76
|
+
is not finite or `locale` is not a valid BCP 47 tag.
|
|
76
77
|
|
|
77
78
|
```ts
|
|
78
79
|
import { millisecondsToString } from "@darthcav/ts-utils"
|
|
79
80
|
|
|
80
81
|
millisecondsToString(3_661_000) // "1h 1m 1s"
|
|
81
82
|
millisecondsToString(90_000) // "1m 30s"
|
|
82
|
-
millisecondsToString(
|
|
83
|
+
millisecondsToString(5_250) // "5s 250ms"
|
|
84
|
+
millisecondsToString(499) // "499ms"
|
|
83
85
|
millisecondsToString(-90_000) // "-1m 30s"
|
|
84
86
|
millisecondsToString(90_061_000, "es") // "1d 1h 1min 1s"
|
|
85
87
|
```
|
|
@@ -216,7 +218,7 @@ public/ # Documentation output (generated)
|
|
|
216
218
|
|
|
217
219
|
[node-version]: https://img.shields.io/badge/node-%3E%3D26-orange.svg?style=flat-square
|
|
218
220
|
[node-url]: https://nodejs.org
|
|
219
|
-
[version-image]: https://img.shields.io/badge/version-0.10.
|
|
221
|
+
[version-image]: https://img.shields.io/badge/version-0.10.8-blue.svg?style=flat-square
|
|
220
222
|
[ci-badge]: https://github.com/darthcav/ts-utils/actions/workflows/tests.yml/badge.svg
|
|
221
223
|
[coverage-badge]:
|
|
222
224
|
https://codecov.io/github/darthcav/ts-utils/branch/dev/graph/badge.svg?token=RNEPER4KEI
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Converts a duration in milliseconds to a human-readable string such as
|
|
3
|
-
* `"1d 4h 32m 10s"`.
|
|
3
|
+
* `"1d 4h 32m 10s 250ms"`.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
5
|
+
* Milliseconds are shown as their own integer component; fractional inputs
|
|
6
|
+
* are rounded to the nearest millisecond. Any zero-valued components are
|
|
7
|
+
* omitted from the formatted output, so a zero-millisecond input produces an
|
|
8
|
+
* empty string. Negative durations are formatted from their magnitude and
|
|
9
|
+
* prefixed with `"-"` (e.g. `-90_000` → `"-1m 30s"`).
|
|
9
10
|
*
|
|
10
11
|
* @param ms - Duration in milliseconds. Must be a finite number.
|
|
11
12
|
* @param locale - BCP 47 locale tag passed to `Intl.DurationFormat`. Defaults
|
|
@@ -18,7 +19,8 @@
|
|
|
18
19
|
* ```ts
|
|
19
20
|
* millisecondsToString(3_661_000) // "1h 1m 1s"
|
|
20
21
|
* millisecondsToString(90_000) // "1m 30s"
|
|
21
|
-
* millisecondsToString(
|
|
22
|
+
* millisecondsToString(5_250) // "5s 250ms"
|
|
23
|
+
* millisecondsToString(499) // "499ms"
|
|
22
24
|
* millisecondsToString(-90_000) // "-1m 30s"
|
|
23
25
|
* millisecondsToString(90_061_000, "es") // "1d 1h 1min 1s"
|
|
24
26
|
* ```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"millisecondsToString.d.ts","sourceRoot":"","sources":["../src/millisecondsToString.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"millisecondsToString.d.ts","sourceRoot":"","sources":["../src/millisecondsToString.ts"],"names":[],"mappings":"AAmCA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,CAAC,OAAO,UAAU,oBAAoB,CACxC,EAAE,EAAE,MAAM,EACV,MAAM,SAAO,GACd,MAAM,CAmBR"}
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maximum number of {@link Intl.DurationFormat} instances retained in the
|
|
3
|
+
* cache. Bounds memory growth when many distinct locales are requested (e.g.
|
|
4
|
+
* a locale derived from untrusted input). When the limit is reached, the
|
|
5
|
+
* least-recently-inserted entry is evicted.
|
|
6
|
+
*/
|
|
7
|
+
const MAX_CACHED_FORMATTERS = 64;
|
|
1
8
|
const durationFormatters = new Map();
|
|
2
9
|
function getDurationFormatter(locale) {
|
|
3
10
|
let formatter = durationFormatters.get(locale);
|
|
@@ -10,18 +17,25 @@ function getDurationFormatter(locale) {
|
|
|
10
17
|
catch (cause) {
|
|
11
18
|
throw new RangeError(`millisecondsToString: invalid locale "${locale}"`, { cause });
|
|
12
19
|
}
|
|
20
|
+
if (durationFormatters.size >= MAX_CACHED_FORMATTERS) {
|
|
21
|
+
const oldest = durationFormatters.keys().next().value;
|
|
22
|
+
if (oldest !== undefined) {
|
|
23
|
+
durationFormatters.delete(oldest);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
13
26
|
durationFormatters.set(locale, formatter);
|
|
14
27
|
}
|
|
15
28
|
return formatter;
|
|
16
29
|
}
|
|
17
30
|
/**
|
|
18
31
|
* Converts a duration in milliseconds to a human-readable string such as
|
|
19
|
-
* `"1d 4h 32m 10s"`.
|
|
32
|
+
* `"1d 4h 32m 10s 250ms"`.
|
|
20
33
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
34
|
+
* Milliseconds are shown as their own integer component; fractional inputs
|
|
35
|
+
* are rounded to the nearest millisecond. Any zero-valued components are
|
|
36
|
+
* omitted from the formatted output, so a zero-millisecond input produces an
|
|
37
|
+
* empty string. Negative durations are formatted from their magnitude and
|
|
38
|
+
* prefixed with `"-"` (e.g. `-90_000` → `"-1m 30s"`).
|
|
25
39
|
*
|
|
26
40
|
* @param ms - Duration in milliseconds. Must be a finite number.
|
|
27
41
|
* @param locale - BCP 47 locale tag passed to `Intl.DurationFormat`. Defaults
|
|
@@ -34,7 +48,8 @@ function getDurationFormatter(locale) {
|
|
|
34
48
|
* ```ts
|
|
35
49
|
* millisecondsToString(3_661_000) // "1h 1m 1s"
|
|
36
50
|
* millisecondsToString(90_000) // "1m 30s"
|
|
37
|
-
* millisecondsToString(
|
|
51
|
+
* millisecondsToString(5_250) // "5s 250ms"
|
|
52
|
+
* millisecondsToString(499) // "499ms"
|
|
38
53
|
* millisecondsToString(-90_000) // "-1m 30s"
|
|
39
54
|
* millisecondsToString(90_061_000, "es") // "1d 1h 1min 1s"
|
|
40
55
|
* ```
|
|
@@ -43,14 +58,15 @@ export default function millisecondsToString(ms, locale = "en") {
|
|
|
43
58
|
if (!Number.isFinite(ms)) {
|
|
44
59
|
throw new RangeError(`millisecondsToString: "ms" must be a finite number, received ${ms}`);
|
|
45
60
|
}
|
|
46
|
-
const
|
|
47
|
-
const magnitude = Math.abs(
|
|
61
|
+
const totalMilliseconds = Math.round(ms);
|
|
62
|
+
const magnitude = Math.abs(totalMilliseconds);
|
|
48
63
|
const formatted = getDurationFormatter(locale).format({
|
|
49
|
-
days: Math.floor(magnitude /
|
|
50
|
-
hours: Math.floor((magnitude %
|
|
51
|
-
minutes: Math.floor((magnitude %
|
|
52
|
-
seconds: magnitude %
|
|
64
|
+
days: Math.floor(magnitude / 86_400_000),
|
|
65
|
+
hours: Math.floor((magnitude % 86_400_000) / 3_600_000),
|
|
66
|
+
minutes: Math.floor((magnitude % 3_600_000) / 60_000),
|
|
67
|
+
seconds: Math.floor((magnitude % 60_000) / 1000),
|
|
68
|
+
milliseconds: magnitude % 1000,
|
|
53
69
|
});
|
|
54
|
-
return
|
|
70
|
+
return totalMilliseconds < 0 && formatted ? `-${formatted}` : formatted;
|
|
55
71
|
}
|
|
56
72
|
//# sourceMappingURL=millisecondsToString.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"millisecondsToString.js","sourceRoot":"","sources":["../src/millisecondsToString.ts"],"names":[],"mappings":"AAAA,MAAM,kBAAkB,GAAG,IAAI,GAAG,EAA+B,CAAA;AAEjE,SAAS,oBAAoB,CAAC,MAAc;IACxC,IAAI,SAAS,GAAG,kBAAkB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;IAC9C,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;QAC1B,IAAI,CAAC;YACD,SAAS,GAAG,IAAI,IAAI,CAAC,cAAc,CAAC,MAAM,EAAE;gBACxC,KAAK,EAAE,QAAQ;aAClB,CAAC,CAAA;QACN,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,MAAM,IAAI,UAAU,CAChB,yCAAyC,MAAM,GAAG,EAClD,EAAE,KAAK,EAAE,CACZ,CAAA;QACL,CAAC;QACD,kBAAkB,CAAC,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;IAC7C,CAAC;IAED,OAAO,SAAS,CAAA;AACpB,CAAC;AAED
|
|
1
|
+
{"version":3,"file":"millisecondsToString.js","sourceRoot":"","sources":["../src/millisecondsToString.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,qBAAqB,GAAG,EAAE,CAAA;AAEhC,MAAM,kBAAkB,GAAG,IAAI,GAAG,EAA+B,CAAA;AAEjE,SAAS,oBAAoB,CAAC,MAAc;IACxC,IAAI,SAAS,GAAG,kBAAkB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;IAC9C,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;QAC1B,IAAI,CAAC;YACD,SAAS,GAAG,IAAI,IAAI,CAAC,cAAc,CAAC,MAAM,EAAE;gBACxC,KAAK,EAAE,QAAQ;aAClB,CAAC,CAAA;QACN,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,MAAM,IAAI,UAAU,CAChB,yCAAyC,MAAM,GAAG,EAClD,EAAE,KAAK,EAAE,CACZ,CAAA;QACL,CAAC;QACD,IAAI,kBAAkB,CAAC,IAAI,IAAI,qBAAqB,EAAE,CAAC;YACnD,MAAM,MAAM,GAAG,kBAAkB,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAA;YACrD,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;gBACvB,kBAAkB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;YACrC,CAAC;QACL,CAAC;QACD,kBAAkB,CAAC,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;IAC7C,CAAC;IAED,OAAO,SAAS,CAAA;AACpB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,CAAC,OAAO,UAAU,oBAAoB,CACxC,EAAU,EACV,MAAM,GAAG,IAAI;IAEb,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,UAAU,CAChB,gEAAgE,EAAE,EAAE,CACvE,CAAA;IACL,CAAC;IAED,MAAM,iBAAiB,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;IACxC,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAA;IAE7C,MAAM,SAAS,GAAG,oBAAoB,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC;QAClD,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,SAAS,GAAG,UAAU,CAAC;QACxC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,SAAS,GAAG,UAAU,CAAC,GAAG,SAAS,CAAC;QACvD,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,SAAS,GAAG,SAAS,CAAC,GAAG,MAAM,CAAC;QACrD,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,SAAS,GAAG,MAAM,CAAC,GAAG,IAAI,CAAC;QAChD,YAAY,EAAE,SAAS,GAAG,IAAI;KACjC,CAAC,CAAA;IAEF,OAAO,iBAAiB,GAAG,CAAC,IAAI,SAAS,CAAC,CAAC,CAAC,IAAI,SAAS,EAAE,CAAC,CAAC,CAAC,SAAS,CAAA;AAC3E,CAAC"}
|
package/dist/monitorMemory.d.ts
CHANGED
|
@@ -4,16 +4,23 @@ import type { Logger } from "@logtape/logtape";
|
|
|
4
4
|
*
|
|
5
5
|
* Memory figures are reported in bytes as returned by Node.js
|
|
6
6
|
* {@link https://nodejs.org/api/process.html#processmemoryusage | process.memoryUsage()}.
|
|
7
|
+
* Reports are emitted through a `"monitorMemory"` child category of the given
|
|
8
|
+
* logger.
|
|
7
9
|
*
|
|
8
|
-
* @param logger - Logger
|
|
9
|
-
*
|
|
10
|
+
* @param logger - Logger whose `"monitorMemory"` child emits the memory
|
|
11
|
+
* reports.
|
|
12
|
+
* @param hours - Interval between reports in hours. Must be a finite number
|
|
13
|
+
* greater than `0`. Defaults to `24`.
|
|
14
|
+
* @throws {RangeError} If `hours` is not a finite number greater than `0`.
|
|
10
15
|
*
|
|
11
16
|
* @example
|
|
12
17
|
* ```ts
|
|
13
18
|
* import { getConsoleLogger, monitorMemory } from "@darthcav/ts-utils"
|
|
14
19
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
20
|
+
* const logger = await getConsoleLogger("my-app")
|
|
21
|
+
*
|
|
22
|
+
* monitorMemory(logger) // every 24 hours
|
|
23
|
+
* monitorMemory(logger, 1) // every hour
|
|
17
24
|
* ```
|
|
18
25
|
*/
|
|
19
26
|
export default function monitorMemory(logger: Logger, hours?: number): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"monitorMemory.d.ts","sourceRoot":"","sources":["../src/monitorMemory.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAG9C
|
|
1
|
+
{"version":3,"file":"monitorMemory.d.ts","sourceRoot":"","sources":["../src/monitorMemory.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAG9C;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,CACjC,MAAM,EAAE,MAAM,EACd,KAAK,GAAE,MAAW,GACnB,IAAI,CAkBN"}
|
package/dist/monitorMemory.js
CHANGED
|
@@ -5,24 +5,35 @@ import millisecondsToString from "./millisecondsToString.js";
|
|
|
5
5
|
*
|
|
6
6
|
* Memory figures are reported in bytes as returned by Node.js
|
|
7
7
|
* {@link https://nodejs.org/api/process.html#processmemoryusage | process.memoryUsage()}.
|
|
8
|
+
* Reports are emitted through a `"monitorMemory"` child category of the given
|
|
9
|
+
* logger.
|
|
8
10
|
*
|
|
9
|
-
* @param logger - Logger
|
|
10
|
-
*
|
|
11
|
+
* @param logger - Logger whose `"monitorMemory"` child emits the memory
|
|
12
|
+
* reports.
|
|
13
|
+
* @param hours - Interval between reports in hours. Must be a finite number
|
|
14
|
+
* greater than `0`. Defaults to `24`.
|
|
15
|
+
* @throws {RangeError} If `hours` is not a finite number greater than `0`.
|
|
11
16
|
*
|
|
12
17
|
* @example
|
|
13
18
|
* ```ts
|
|
14
19
|
* import { getConsoleLogger, monitorMemory } from "@darthcav/ts-utils"
|
|
15
20
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
21
|
+
* const logger = await getConsoleLogger("my-app")
|
|
22
|
+
*
|
|
23
|
+
* monitorMemory(logger) // every 24 hours
|
|
24
|
+
* monitorMemory(logger, 1) // every hour
|
|
18
25
|
* ```
|
|
19
26
|
*/
|
|
20
27
|
export default function monitorMemory(logger, hours = 24) {
|
|
28
|
+
if (!Number.isFinite(hours) || hours <= 0) {
|
|
29
|
+
throw new RangeError(`monitorMemory: "hours" must be a finite number greater than 0, received ${hours}`);
|
|
30
|
+
}
|
|
31
|
+
const __logger = logger.getChild(["monitorMemory"]);
|
|
21
32
|
const delay = 60 * 60 * 1_000 * hours;
|
|
22
33
|
setInterval(() => {
|
|
23
34
|
const { rss, heapTotal, heapUsed, external } = memoryUsage();
|
|
24
|
-
|
|
25
|
-
|
|
35
|
+
__logger.info(`Process uptime: ${millisecondsToString(uptime() * 1_000)}`);
|
|
36
|
+
__logger.info(`Process memory - Resident set size: ${rss} | Heap total: ${heapTotal} | Heap used: ${heapUsed} | External: ${external}`);
|
|
26
37
|
}, delay);
|
|
27
38
|
}
|
|
28
39
|
//# sourceMappingURL=monitorMemory.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"monitorMemory.js","sourceRoot":"","sources":["../src/monitorMemory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAElD,OAAO,oBAAoB,MAAM,2BAA2B,CAAA;AAE5D
|
|
1
|
+
{"version":3,"file":"monitorMemory.js","sourceRoot":"","sources":["../src/monitorMemory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAElD,OAAO,oBAAoB,MAAM,2BAA2B,CAAA;AAE5D;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,CACjC,MAAc,EACd,QAAgB,EAAE;IAElB,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,EAAE,CAAC;QACxC,MAAM,IAAI,UAAU,CAChB,2EAA2E,KAAK,EAAE,CACrF,CAAA;IACL,CAAC;IACD,MAAM,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,eAAe,CAAC,CAAC,CAAA;IAEnD,MAAM,KAAK,GAAG,EAAE,GAAG,EAAE,GAAG,KAAK,GAAG,KAAK,CAAA;IACrC,WAAW,CAAC,GAAG,EAAE;QACb,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,WAAW,EAAE,CAAA;QAC5D,QAAQ,CAAC,IAAI,CACT,mBAAmB,oBAAoB,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,EAAE,CAC9D,CAAA;QACD,QAAQ,CAAC,IAAI,CACT,uCAAuC,GAAG,kBAAkB,SAAS,iBAAiB,QAAQ,gBAAgB,QAAQ,EAAE,CAC3H,CAAA;IACL,CAAC,EAAE,KAAK,CAAC,CAAA;AACb,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darthcav/ts-utils",
|
|
3
3
|
"description": "A collection of utility functions for TypeScript applications and modules",
|
|
4
|
-
"version": "0.10.
|
|
4
|
+
"version": "0.10.8",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "darthcav",
|
|
7
7
|
"url": "https://github.com/darthcav"
|
|
@@ -24,15 +24,15 @@
|
|
|
24
24
|
"!dist/__tests__"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@logtape/logtape": "2.
|
|
27
|
+
"@logtape/logtape": "2.2.4"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@biomejs/biome": "2.5.
|
|
31
|
-
"@types/node": "
|
|
30
|
+
"@biomejs/biome": "2.5.4",
|
|
31
|
+
"@types/node": "26.1.1",
|
|
32
32
|
"asserttt": "1.0.1",
|
|
33
|
-
"prettier": "3.
|
|
33
|
+
"prettier": "3.9.5",
|
|
34
34
|
"shx": "0.4.0",
|
|
35
|
-
"typedoc": "0.28.
|
|
35
|
+
"typedoc": "0.28.20"
|
|
36
36
|
},
|
|
37
37
|
"repository": {
|
|
38
38
|
"type": "git",
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maximum number of {@link Intl.DurationFormat} instances retained in the
|
|
3
|
+
* cache. Bounds memory growth when many distinct locales are requested (e.g.
|
|
4
|
+
* a locale derived from untrusted input). When the limit is reached, the
|
|
5
|
+
* least-recently-inserted entry is evicted.
|
|
6
|
+
*/
|
|
7
|
+
const MAX_CACHED_FORMATTERS = 64
|
|
8
|
+
|
|
1
9
|
const durationFormatters = new Map<string, Intl.DurationFormat>()
|
|
2
10
|
|
|
3
11
|
function getDurationFormatter(locale: string): Intl.DurationFormat {
|
|
@@ -13,6 +21,12 @@ function getDurationFormatter(locale: string): Intl.DurationFormat {
|
|
|
13
21
|
{ cause },
|
|
14
22
|
)
|
|
15
23
|
}
|
|
24
|
+
if (durationFormatters.size >= MAX_CACHED_FORMATTERS) {
|
|
25
|
+
const oldest = durationFormatters.keys().next().value
|
|
26
|
+
if (oldest !== undefined) {
|
|
27
|
+
durationFormatters.delete(oldest)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
16
30
|
durationFormatters.set(locale, formatter)
|
|
17
31
|
}
|
|
18
32
|
|
|
@@ -21,12 +35,13 @@ function getDurationFormatter(locale: string): Intl.DurationFormat {
|
|
|
21
35
|
|
|
22
36
|
/**
|
|
23
37
|
* Converts a duration in milliseconds to a human-readable string such as
|
|
24
|
-
* `"1d 4h 32m 10s"`.
|
|
38
|
+
* `"1d 4h 32m 10s 250ms"`.
|
|
25
39
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
40
|
+
* Milliseconds are shown as their own integer component; fractional inputs
|
|
41
|
+
* are rounded to the nearest millisecond. Any zero-valued components are
|
|
42
|
+
* omitted from the formatted output, so a zero-millisecond input produces an
|
|
43
|
+
* empty string. Negative durations are formatted from their magnitude and
|
|
44
|
+
* prefixed with `"-"` (e.g. `-90_000` → `"-1m 30s"`).
|
|
30
45
|
*
|
|
31
46
|
* @param ms - Duration in milliseconds. Must be a finite number.
|
|
32
47
|
* @param locale - BCP 47 locale tag passed to `Intl.DurationFormat`. Defaults
|
|
@@ -39,7 +54,8 @@ function getDurationFormatter(locale: string): Intl.DurationFormat {
|
|
|
39
54
|
* ```ts
|
|
40
55
|
* millisecondsToString(3_661_000) // "1h 1m 1s"
|
|
41
56
|
* millisecondsToString(90_000) // "1m 30s"
|
|
42
|
-
* millisecondsToString(
|
|
57
|
+
* millisecondsToString(5_250) // "5s 250ms"
|
|
58
|
+
* millisecondsToString(499) // "499ms"
|
|
43
59
|
* millisecondsToString(-90_000) // "-1m 30s"
|
|
44
60
|
* millisecondsToString(90_061_000, "es") // "1d 1h 1min 1s"
|
|
45
61
|
* ```
|
|
@@ -54,15 +70,16 @@ export default function millisecondsToString(
|
|
|
54
70
|
)
|
|
55
71
|
}
|
|
56
72
|
|
|
57
|
-
const
|
|
58
|
-
const magnitude = Math.abs(
|
|
73
|
+
const totalMilliseconds = Math.round(ms)
|
|
74
|
+
const magnitude = Math.abs(totalMilliseconds)
|
|
59
75
|
|
|
60
76
|
const formatted = getDurationFormatter(locale).format({
|
|
61
|
-
days: Math.floor(magnitude /
|
|
62
|
-
hours: Math.floor((magnitude %
|
|
63
|
-
minutes: Math.floor((magnitude %
|
|
64
|
-
seconds: magnitude %
|
|
77
|
+
days: Math.floor(magnitude / 86_400_000),
|
|
78
|
+
hours: Math.floor((magnitude % 86_400_000) / 3_600_000),
|
|
79
|
+
minutes: Math.floor((magnitude % 3_600_000) / 60_000),
|
|
80
|
+
seconds: Math.floor((magnitude % 60_000) / 1000),
|
|
81
|
+
milliseconds: magnitude % 1000,
|
|
65
82
|
})
|
|
66
83
|
|
|
67
|
-
return
|
|
84
|
+
return totalMilliseconds < 0 && formatted ? `-${formatted}` : formatted
|
|
68
85
|
}
|
package/src/monitorMemory.ts
CHANGED
|
@@ -7,27 +7,43 @@ import millisecondsToString from "./millisecondsToString.ts"
|
|
|
7
7
|
*
|
|
8
8
|
* Memory figures are reported in bytes as returned by Node.js
|
|
9
9
|
* {@link https://nodejs.org/api/process.html#processmemoryusage | process.memoryUsage()}.
|
|
10
|
+
* Reports are emitted through a `"monitorMemory"` child category of the given
|
|
11
|
+
* logger.
|
|
10
12
|
*
|
|
11
|
-
* @param logger - Logger
|
|
12
|
-
*
|
|
13
|
+
* @param logger - Logger whose `"monitorMemory"` child emits the memory
|
|
14
|
+
* reports.
|
|
15
|
+
* @param hours - Interval between reports in hours. Must be a finite number
|
|
16
|
+
* greater than `0`. Defaults to `24`.
|
|
17
|
+
* @throws {RangeError} If `hours` is not a finite number greater than `0`.
|
|
13
18
|
*
|
|
14
19
|
* @example
|
|
15
20
|
* ```ts
|
|
16
21
|
* import { getConsoleLogger, monitorMemory } from "@darthcav/ts-utils"
|
|
17
22
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
23
|
+
* const logger = await getConsoleLogger("my-app")
|
|
24
|
+
*
|
|
25
|
+
* monitorMemory(logger) // every 24 hours
|
|
26
|
+
* monitorMemory(logger, 1) // every hour
|
|
20
27
|
* ```
|
|
21
28
|
*/
|
|
22
29
|
export default function monitorMemory(
|
|
23
30
|
logger: Logger,
|
|
24
31
|
hours: number = 24,
|
|
25
32
|
): void {
|
|
33
|
+
if (!Number.isFinite(hours) || hours <= 0) {
|
|
34
|
+
throw new RangeError(
|
|
35
|
+
`monitorMemory: "hours" must be a finite number greater than 0, received ${hours}`,
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
const __logger = logger.getChild(["monitorMemory"])
|
|
39
|
+
|
|
26
40
|
const delay = 60 * 60 * 1_000 * hours
|
|
27
41
|
setInterval(() => {
|
|
28
42
|
const { rss, heapTotal, heapUsed, external } = memoryUsage()
|
|
29
|
-
|
|
30
|
-
|
|
43
|
+
__logger.info(
|
|
44
|
+
`Process uptime: ${millisecondsToString(uptime() * 1_000)}`,
|
|
45
|
+
)
|
|
46
|
+
__logger.info(
|
|
31
47
|
`Process memory - Resident set size: ${rss} | Heap total: ${heapTotal} | Heap used: ${heapUsed} | External: ${external}`,
|
|
32
48
|
)
|
|
33
49
|
}, delay)
|