@darthcav/ts-utils 0.10.6 → 0.10.7
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 +1 -1
- package/dist/millisecondsToString.d.ts.map +1 -1
- package/dist/millisecondsToString.js +13 -0
- package/dist/millisecondsToString.js.map +1 -1
- package/dist/monitorMemory.d.ts +3 -1
- package/dist/monitorMemory.d.ts.map +1 -1
- package/dist/monitorMemory.js +6 -1
- package/dist/monitorMemory.js.map +1 -1
- package/package.json +2 -2
- package/src/millisecondsToString.ts +14 -0
- package/src/monitorMemory.ts +8 -1
package/README.md
CHANGED
|
@@ -216,7 +216,7 @@ public/ # Documentation output (generated)
|
|
|
216
216
|
|
|
217
217
|
[node-version]: https://img.shields.io/badge/node-%3E%3D26-orange.svg?style=flat-square
|
|
218
218
|
[node-url]: https://nodejs.org
|
|
219
|
-
[version-image]: https://img.shields.io/badge/version-0.10.
|
|
219
|
+
[version-image]: https://img.shields.io/badge/version-0.10.7-blue.svg?style=flat-square
|
|
220
220
|
[ci-badge]: https://github.com/darthcav/ts-utils/actions/workflows/tests.yml/badge.svg
|
|
221
221
|
[coverage-badge]:
|
|
222
222
|
https://codecov.io/github/darthcav/ts-utils/branch/dev/graph/badge.svg?token=RNEPER4KEI
|
|
@@ -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;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,CAAC,OAAO,UAAU,oBAAoB,CACxC,EAAE,EAAE,MAAM,EACV,MAAM,SAAO,GACd,MAAM,CAkBR"}
|
|
@@ -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,6 +17,12 @@ 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;
|
|
@@ -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,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ACnC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,SAAS,GAAG,KAAK,CAAC,GAAG,IAAI,CAAC;QAC7C,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAC5C,OAAO,EAAE,SAAS,GAAG,EAAE;KAC1B,CAAC,CAAA;IAEF,OAAO,YAAY,GAAG,CAAC,IAAI,SAAS,CAAC,CAAC,CAAC,IAAI,SAAS,EAAE,CAAC,CAAC,CAAC,SAAS,CAAA;AACtE,CAAC"}
|
package/dist/monitorMemory.d.ts
CHANGED
|
@@ -6,7 +6,9 @@ import type { Logger } from "@logtape/logtape";
|
|
|
6
6
|
* {@link https://nodejs.org/api/process.html#processmemoryusage | process.memoryUsage()}.
|
|
7
7
|
*
|
|
8
8
|
* @param logger - Logger instance used to emit the memory reports.
|
|
9
|
-
* @param hours - Interval between reports in hours.
|
|
9
|
+
* @param hours - Interval between reports in hours. Must be a finite number
|
|
10
|
+
* greater than `0`. Defaults to `24`.
|
|
11
|
+
* @throws {RangeError} If `hours` is not a finite number greater than `0`.
|
|
10
12
|
*
|
|
11
13
|
* @example
|
|
12
14
|
* ```ts
|
|
@@ -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;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,CACjC,MAAM,EAAE,MAAM,EACd,KAAK,GAAE,MAAW,GACnB,IAAI,CAcN"}
|
package/dist/monitorMemory.js
CHANGED
|
@@ -7,7 +7,9 @@ import millisecondsToString from "./millisecondsToString.js";
|
|
|
7
7
|
* {@link https://nodejs.org/api/process.html#processmemoryusage | process.memoryUsage()}.
|
|
8
8
|
*
|
|
9
9
|
* @param logger - Logger instance used to emit the memory reports.
|
|
10
|
-
* @param hours - Interval between reports in hours.
|
|
10
|
+
* @param hours - Interval between reports in hours. Must be a finite number
|
|
11
|
+
* greater than `0`. Defaults to `24`.
|
|
12
|
+
* @throws {RangeError} If `hours` is not a finite number greater than `0`.
|
|
11
13
|
*
|
|
12
14
|
* @example
|
|
13
15
|
* ```ts
|
|
@@ -18,6 +20,9 @@ import millisecondsToString from "./millisecondsToString.js";
|
|
|
18
20
|
* ```
|
|
19
21
|
*/
|
|
20
22
|
export default function monitorMemory(logger, hours = 24) {
|
|
23
|
+
if (!Number.isFinite(hours) || hours <= 0) {
|
|
24
|
+
throw new RangeError(`monitorMemory: "hours" must be a finite number greater than 0, received ${hours}`);
|
|
25
|
+
}
|
|
21
26
|
const delay = 60 * 60 * 1_000 * hours;
|
|
22
27
|
setInterval(() => {
|
|
23
28
|
const { rss, heapTotal, heapUsed, external } = memoryUsage();
|
|
@@ -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;;;;;;;;;;;;;;;;;;GAkBG;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,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,MAAM,CAAC,IAAI,CAAC,mBAAmB,oBAAoB,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,EAAE,CAAC,CAAA;QACxE,MAAM,CAAC,IAAI,CACP,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.7",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "darthcav",
|
|
7
7
|
"url": "https://github.com/darthcav"
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"!dist/__tests__"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@logtape/logtape": "2.1.
|
|
27
|
+
"@logtape/logtape": "2.1.5"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@biomejs/biome": "2.5.0",
|
|
@@ -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
|
|
package/src/monitorMemory.ts
CHANGED
|
@@ -9,7 +9,9 @@ import millisecondsToString from "./millisecondsToString.ts"
|
|
|
9
9
|
* {@link https://nodejs.org/api/process.html#processmemoryusage | process.memoryUsage()}.
|
|
10
10
|
*
|
|
11
11
|
* @param logger - Logger instance used to emit the memory reports.
|
|
12
|
-
* @param hours - Interval between reports in hours.
|
|
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`.
|
|
13
15
|
*
|
|
14
16
|
* @example
|
|
15
17
|
* ```ts
|
|
@@ -23,6 +25,11 @@ export default function monitorMemory(
|
|
|
23
25
|
logger: Logger,
|
|
24
26
|
hours: number = 24,
|
|
25
27
|
): void {
|
|
28
|
+
if (!Number.isFinite(hours) || hours <= 0) {
|
|
29
|
+
throw new RangeError(
|
|
30
|
+
`monitorMemory: "hours" must be a finite number greater than 0, received ${hours}`,
|
|
31
|
+
)
|
|
32
|
+
}
|
|
26
33
|
const delay = 60 * 60 * 1_000 * hours
|
|
27
34
|
setInterval(() => {
|
|
28
35
|
const { rss, heapTotal, heapUsed, external } = memoryUsage()
|