@darthcav/ts-utils 0.10.9 → 0.11.0
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 +137 -39
- package/dist/index.d.ts +9 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -8
- package/dist/index.js.map +1 -1
- package/dist/loggers/getConsoleLogger.d.ts +10 -4
- package/dist/loggers/getConsoleLogger.d.ts.map +1 -1
- package/dist/loggers/getConsoleLogger.js +36 -9
- package/dist/loggers/getConsoleLogger.js.map +1 -1
- package/dist/loggers/getDummyLogger.d.ts +14 -3
- package/dist/loggers/getDummyLogger.d.ts.map +1 -1
- package/dist/loggers/getDummyLogger.js +29 -11
- package/dist/loggers/getDummyLogger.js.map +1 -1
- package/dist/main.d.ts +83 -51
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +200 -40
- package/dist/main.js.map +1 -1
- package/dist/millisecondsToString.d.ts +1 -1
- package/dist/millisecondsToString.d.ts.map +1 -1
- package/dist/millisecondsToString.js +1 -1
- package/dist/millisecondsToString.js.map +1 -1
- package/dist/monitorMemory.d.ts +25 -7
- package/dist/monitorMemory.d.ts.map +1 -1
- package/dist/monitorMemory.js +52 -14
- package/dist/monitorMemory.js.map +1 -1
- package/dist/noop.d.ts +1 -2
- package/dist/noop.d.ts.map +1 -1
- package/dist/noop.js +1 -2
- package/dist/noop.js.map +1 -1
- package/dist/{os-utils.d.ts → osRelease.d.ts} +11 -7
- package/dist/osRelease.d.ts.map +1 -0
- package/dist/osRelease.js +153 -0
- package/dist/osRelease.js.map +1 -0
- package/dist/types.d.ts +42 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +61 -16
- package/dist/types.js.map +1 -1
- package/package.json +8 -8
- package/src/index.ts +12 -7
- package/src/loggers/getConsoleLogger.ts +41 -8
- package/src/loggers/getDummyLogger.ts +31 -11
- package/src/main.ts +249 -126
- package/src/millisecondsToString.ts +1 -4
- package/src/monitorMemory.ts +58 -19
- package/src/noop.ts +1 -3
- package/src/osRelease.ts +172 -0
- package/src/types.ts +65 -17
- package/dist/os-utils.d.ts.map +0 -1
- package/dist/os-utils.js +0 -94
- package/dist/os-utils.js.map +0 -1
- package/src/os-utils.ts +0 -110
|
@@ -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;
|
|
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,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAEhE,uDAAuD;AACvD,MAAM,YAAY,GAAG,EAAE,GAAG,KAAK,CAAA;AAE/B;;;GAGG;AACH,MAAM,YAAY,GAAG,aAAa,CAAA;AAElC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,MAAM,UAAU,aAAa,CAAC,MAAc,EAAE,QAAgB,EAAE;IAC5D,MAAM,KAAK,GAAG,EAAE,GAAG,EAAE,GAAG,KAAK,GAAG,KAAK,CAAA;IACrC,IACI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC;QACvB,KAAK,GAAG,YAAY;QACpB,KAAK,GAAG,YAAY,EACtB,CAAC;QACC,MAAM,IAAI,UAAU,CAChB,0DAA0D,YAAY,GAAG,SAAS,QAAQ,YAAY,GAAG,SAAS,wCAAwC,KAAK,EAAE,CACpK,CAAA;IACL,CAAC;IACD,MAAM,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,eAAe,CAAC,CAAC,CAAA;IAEnD,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE;QAC3B,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAA;QAC7C,MAAM,eAAe,GAAG,oBAAoB,CAAC,QAAQ,CAAC,CAAA;QACtD,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,WAAW,EAAE,CAAA;QAC5D,kEAAkE;QAClE,qEAAqE;QACrE,iEAAiE;QACjE,QAAQ,CAAC,IAAI,CAAC,mBAAmB,eAAe,EAAE,EAAE;YAChD,MAAM,EAAE,eAAe;YACvB,QAAQ;SACX,CAAC,CAAA;QACF,QAAQ,CAAC,IAAI,CACT,oHAAoH,EACpH,EAAE,GAAG,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,CACzC,CAAA;IACL,CAAC,EAAE,KAAK,CAAC,CAAA;IACT,oDAAoD;IACpD,KAAK,CAAC,KAAK,EAAE,CAAA;IAEb,OAAO,GAAG,EAAE,CAAC,aAAa,CAAC,KAAK,CAAC,CAAA;AACrC,CAAC"}
|
package/dist/noop.d.ts
CHANGED
package/dist/noop.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"noop.d.ts","sourceRoot":"","sources":["../src/noop.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,
|
|
1
|
+
{"version":3,"file":"noop.d.ts","sourceRoot":"","sources":["../src/noop.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,eAAO,MAAM,IAAI,QAAO,IAAU,CAAA"}
|
package/dist/noop.js
CHANGED
package/dist/noop.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"noop.js","sourceRoot":"","sources":["../src/noop.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,IAAI,GAAG,GAAS,EAAE,GAAE,CAAC,CAAA
|
|
1
|
+
{"version":3,"file":"noop.js","sourceRoot":"","sources":["../src/noop.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,GAAS,EAAE,GAAE,CAAC,CAAA"}
|
|
@@ -14,18 +14,22 @@ export type OsRelease = {
|
|
|
14
14
|
arch: string;
|
|
15
15
|
} & RuntimeObject;
|
|
16
16
|
/**
|
|
17
|
-
* Returns OS release information for the current platform, or `null` on
|
|
18
|
-
* platforms or when
|
|
17
|
+
* Returns OS release information for the current platform, or `null` on
|
|
18
|
+
* unsupported platforms or when no os-release file can be read on Linux.
|
|
19
19
|
*
|
|
20
|
-
* On Linux,
|
|
21
|
-
* `
|
|
22
|
-
*
|
|
20
|
+
* On Linux, the information is read from `/etc/os-release`, falling back to
|
|
21
|
+
* `/usr/lib/os-release`. `name`, `version`, and `arch` are normalized from
|
|
22
|
+
* `NAME`/`PRETTY_NAME`, `VERSION_ID`/`VERSION`, and `os.arch()` respectively.
|
|
23
|
+
* All raw keys from the file are also present on the returned object. Comment
|
|
24
|
+
* lines are ignored, single- and double-quoted values are unquoted (with
|
|
25
|
+
* backslash escapes inside double quotes), and keys that are not upper-case
|
|
26
|
+
* shell variable names are skipped.
|
|
23
27
|
*
|
|
24
28
|
* On Windows, distinguishes Windows 11 from Windows 10 by NT build number
|
|
25
29
|
* (>= 22000 → Windows 11).
|
|
26
30
|
*
|
|
27
31
|
* @returns An {@link OsRelease} object on supported platforms, or `null` when
|
|
28
|
-
* the platform is unsupported or
|
|
32
|
+
* the platform is unsupported or no os-release file can be read on Linux.
|
|
29
33
|
*
|
|
30
34
|
* @example
|
|
31
35
|
* ```ts
|
|
@@ -37,4 +41,4 @@ export type OsRelease = {
|
|
|
37
41
|
* ```
|
|
38
42
|
*/
|
|
39
43
|
export declare function osRelease(): OsRelease | null;
|
|
40
|
-
//# sourceMappingURL=
|
|
44
|
+
//# sourceMappingURL=osRelease.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"osRelease.d.ts","sourceRoot":"","sources":["../src/osRelease.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAE/C;;;;;GAKG;AACH,MAAM,MAAM,SAAS,GAAG;IACpB,iEAAiE;IACjE,IAAI,EAAE,MAAM,CAAA;IACZ,2DAA2D;IAC3D,OAAO,EAAE,MAAM,CAAA;IACf,uEAAuE;IACvE,IAAI,EAAE,MAAM,CAAA;CACf,GAAG,aAAa,CAAA;AAwEjB;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,SAAS,IAAI,SAAS,GAAG,IAAI,CAuD5C"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { arch, release as kernelRelease, platform } from "node:os";
|
|
3
|
+
/**
|
|
4
|
+
* Candidate os-release files, in lookup order. Per the freedesktop.org spec,
|
|
5
|
+
* `/etc/os-release` takes precedence and `/usr/lib/os-release` is the fallback.
|
|
6
|
+
*/
|
|
7
|
+
const OS_RELEASE_FILES = ["/etc/os-release", "/usr/lib/os-release"];
|
|
8
|
+
/**
|
|
9
|
+
* Valid os-release keys: shell-compatible, upper-case variable names. This
|
|
10
|
+
* also rules out keys such as `__proto__` or `constructor`.
|
|
11
|
+
*/
|
|
12
|
+
const OS_RELEASE_KEY = /^[A-Z][A-Z0-9_]*$/;
|
|
13
|
+
/**
|
|
14
|
+
* Reads the first readable os-release file, or returns `null` when none can be
|
|
15
|
+
* read (missing, unreadable, or not a regular file).
|
|
16
|
+
*/
|
|
17
|
+
function readOsReleaseFile() {
|
|
18
|
+
for (const file of OS_RELEASE_FILES) {
|
|
19
|
+
try {
|
|
20
|
+
return readFileSync(file, "utf-8");
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
// Try the next candidate.
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Removes one level of shell-style quoting from an os-release value. Double
|
|
30
|
+
* quotes allow the `\\`, `\"`, `` \` `` and `\$` escapes; single quotes are
|
|
31
|
+
* literal.
|
|
32
|
+
*/
|
|
33
|
+
function unquote(value) {
|
|
34
|
+
if (value.length >= 2) {
|
|
35
|
+
const quote = value[0];
|
|
36
|
+
if (quote === '"' && value.endsWith('"')) {
|
|
37
|
+
return value.slice(1, -1).replace(/\\([\\"`$])/g, "$1");
|
|
38
|
+
}
|
|
39
|
+
if (quote === "'" && value.endsWith("'")) {
|
|
40
|
+
return value.slice(1, -1);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Parses os-release content into a null-prototype record. Blank lines,
|
|
47
|
+
* comments, lines without `=`, and keys that are not valid variable names are
|
|
48
|
+
* skipped.
|
|
49
|
+
*/
|
|
50
|
+
function parseOsRelease(content) {
|
|
51
|
+
const raw = Object.create(null);
|
|
52
|
+
for (const line of content.split("\n")) {
|
|
53
|
+
const trimmed = line.trim();
|
|
54
|
+
if (trimmed === "" || trimmed.startsWith("#")) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const eq = trimmed.indexOf("=");
|
|
58
|
+
if (eq === -1) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const key = trimmed.substring(0, eq).trim();
|
|
62
|
+
if (!OS_RELEASE_KEY.test(key)) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
raw[key] = unquote(trimmed.substring(eq + 1).trim());
|
|
66
|
+
}
|
|
67
|
+
return raw;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Returns OS release information for the current platform, or `null` on
|
|
71
|
+
* unsupported platforms or when no os-release file can be read on Linux.
|
|
72
|
+
*
|
|
73
|
+
* On Linux, the information is read from `/etc/os-release`, falling back to
|
|
74
|
+
* `/usr/lib/os-release`. `name`, `version`, and `arch` are normalized from
|
|
75
|
+
* `NAME`/`PRETTY_NAME`, `VERSION_ID`/`VERSION`, and `os.arch()` respectively.
|
|
76
|
+
* All raw keys from the file are also present on the returned object. Comment
|
|
77
|
+
* lines are ignored, single- and double-quoted values are unquoted (with
|
|
78
|
+
* backslash escapes inside double quotes), and keys that are not upper-case
|
|
79
|
+
* shell variable names are skipped.
|
|
80
|
+
*
|
|
81
|
+
* On Windows, distinguishes Windows 11 from Windows 10 by NT build number
|
|
82
|
+
* (>= 22000 → Windows 11).
|
|
83
|
+
*
|
|
84
|
+
* @returns An {@link OsRelease} object on supported platforms, or `null` when
|
|
85
|
+
* the platform is unsupported or no os-release file can be read on Linux.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* import { osRelease } from "@darthcav/ts-utils"
|
|
90
|
+
*
|
|
91
|
+
* const info = osRelease()
|
|
92
|
+
* if (info) console.log(info.name) // e.g. "Ubuntu" or "Windows 11"
|
|
93
|
+
* if (info) console.log(info.PRETTY_NAME) // e.g. "Ubuntu 24.04 LTS" (Linux only)
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export function osRelease() {
|
|
97
|
+
const p = platform();
|
|
98
|
+
if (p === "linux") {
|
|
99
|
+
const content = readOsReleaseFile();
|
|
100
|
+
if (content === null) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
const raw = parseOsRelease(content);
|
|
104
|
+
return {
|
|
105
|
+
...raw,
|
|
106
|
+
name: raw["NAME"] ?? raw["PRETTY_NAME"] ?? "",
|
|
107
|
+
version: raw["VERSION_ID"] ?? raw["VERSION"] ?? "",
|
|
108
|
+
arch: arch(),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
if (p === "win32") {
|
|
112
|
+
const version = kernelRelease();
|
|
113
|
+
const [major, , build] = version.split(".").map(Number);
|
|
114
|
+
let name = "Windows ";
|
|
115
|
+
if (major === 10 && (build ?? 0) >= 22000) {
|
|
116
|
+
name += "11";
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
switch (version.substring(0, 3)) {
|
|
120
|
+
case "10.":
|
|
121
|
+
name += "10";
|
|
122
|
+
break;
|
|
123
|
+
case "6.3":
|
|
124
|
+
name += "8.1";
|
|
125
|
+
break;
|
|
126
|
+
case "6.2":
|
|
127
|
+
name += "8";
|
|
128
|
+
break;
|
|
129
|
+
case "6.1":
|
|
130
|
+
name += "7";
|
|
131
|
+
break;
|
|
132
|
+
case "6.0":
|
|
133
|
+
name += "Vista";
|
|
134
|
+
break;
|
|
135
|
+
case "5.2":
|
|
136
|
+
name += "XP 64-Bit Edition";
|
|
137
|
+
break;
|
|
138
|
+
case "5.1":
|
|
139
|
+
name += "XP";
|
|
140
|
+
break;
|
|
141
|
+
case "5.0":
|
|
142
|
+
name += "2000";
|
|
143
|
+
break;
|
|
144
|
+
default:
|
|
145
|
+
name += "unknown version";
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return { name, version, arch: arch() };
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=osRelease.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"osRelease.js","sourceRoot":"","sources":["../src/osRelease.ts"],"names":[],"mappings":"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"}
|
package/dist/types.d.ts
CHANGED
|
@@ -7,12 +7,49 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export type RuntimeObject = Record<string, unknown>;
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
10
|
+
* Type guard that checks whether a value is a plain object that can be treated
|
|
11
|
+
* as a runtime JSON object.
|
|
12
|
+
*
|
|
13
|
+
* Plain objects are object literals, `JSON.parse` results, and
|
|
14
|
+
* `Object.create(null)` objects, including those from another realm (e.g. a
|
|
15
|
+
* `node:vm` context). Arrays, class instances, and built-ins such as `Date`,
|
|
16
|
+
* `Map`, `Set`, `RegExp`, or module namespace objects are rejected.
|
|
17
|
+
*
|
|
18
|
+
* The check is shallow and keys are not sanitized: an own `__proto__` or
|
|
19
|
+
* `constructor` key (as `JSON.parse` can produce from untrusted input) is kept.
|
|
20
|
+
* Do not merge the value into other objects key by key without guarding
|
|
21
|
+
* against those keys, or prototype pollution becomes possible.
|
|
22
|
+
*
|
|
23
|
+
* @param value - Value to check.
|
|
24
|
+
* @returns `true` when the value is a plain object, narrowing it to
|
|
25
|
+
* {@link RuntimeObject}.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* const body: unknown = JSON.parse(input)
|
|
30
|
+
* if (isRuntimeObject(body)) {
|
|
31
|
+
* console.log(Object.keys(body)) // body: RuntimeObject
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare function isRuntimeObject(value: unknown): value is RuntimeObject;
|
|
36
|
+
/**
|
|
37
|
+
* Returns the input when it is a plain object that can be treated as a runtime
|
|
38
|
+
* JSON object, as decided by {@link isRuntimeObject} (see there for what counts
|
|
39
|
+
* as a plain object and the caveats about unsanitized keys).
|
|
11
40
|
*
|
|
12
41
|
* @param value - Value to narrow.
|
|
13
|
-
* @returns The same value as a {@link RuntimeObject}, or `undefined` when the
|
|
42
|
+
* @returns The same value as a {@link RuntimeObject}, or `undefined` when the
|
|
43
|
+
* value is not a plain object.
|
|
14
44
|
*/
|
|
15
45
|
export declare function asRuntimeObject(value: unknown): RuntimeObject | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Type guard that checks whether a value is a string.
|
|
48
|
+
*
|
|
49
|
+
* @param value - Value to check.
|
|
50
|
+
* @returns `true` when the value is a string, narrowing it to `string`.
|
|
51
|
+
*/
|
|
52
|
+
export declare function isString(value: unknown): value is string;
|
|
16
53
|
/**
|
|
17
54
|
* Returns the input when it is a string.
|
|
18
55
|
*
|
|
@@ -23,6 +60,9 @@ export declare function asString(value: unknown): string | undefined;
|
|
|
23
60
|
/**
|
|
24
61
|
* Filters an unknown value down to an array of runtime JSON objects.
|
|
25
62
|
*
|
|
63
|
+
* Elements are kept when {@link isRuntimeObject} accepts them, i.e. only plain
|
|
64
|
+
* objects; the same caveats about unsanitized keys apply.
|
|
65
|
+
*
|
|
26
66
|
* @param value - Value that may contain runtime objects.
|
|
27
67
|
* @returns Only the elements that can be treated as {@link RuntimeObject} values.
|
|
28
68
|
*/
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AAEnD
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AAEnD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAgBtE;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,GAAG,SAAS,CAEzE;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAExD;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAE3D;AAED;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,EAAE,CAEpE"}
|
package/dist/types.js
CHANGED
|
@@ -1,13 +1,64 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Type guard that checks whether a value is a plain object that can be treated
|
|
3
|
+
* as a runtime JSON object.
|
|
4
|
+
*
|
|
5
|
+
* Plain objects are object literals, `JSON.parse` results, and
|
|
6
|
+
* `Object.create(null)` objects, including those from another realm (e.g. a
|
|
7
|
+
* `node:vm` context). Arrays, class instances, and built-ins such as `Date`,
|
|
8
|
+
* `Map`, `Set`, `RegExp`, or module namespace objects are rejected.
|
|
9
|
+
*
|
|
10
|
+
* The check is shallow and keys are not sanitized: an own `__proto__` or
|
|
11
|
+
* `constructor` key (as `JSON.parse` can produce from untrusted input) is kept.
|
|
12
|
+
* Do not merge the value into other objects key by key without guarding
|
|
13
|
+
* against those keys, or prototype pollution becomes possible.
|
|
14
|
+
*
|
|
15
|
+
* @param value - Value to check.
|
|
16
|
+
* @returns `true` when the value is a plain object, narrowing it to
|
|
17
|
+
* {@link RuntimeObject}.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const body: unknown = JSON.parse(input)
|
|
22
|
+
* if (isRuntimeObject(body)) {
|
|
23
|
+
* console.log(Object.keys(body)) // body: RuntimeObject
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function isRuntimeObject(value) {
|
|
28
|
+
if (typeof value !== "object" || value === null) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
const prototype = Object.getPrototypeOf(value);
|
|
32
|
+
// A null prototype, or a realm's root `Object.prototype` (whose own
|
|
33
|
+
// prototype is null), marks a plain object.
|
|
34
|
+
if (prototype !== null &&
|
|
35
|
+
prototype !== Object.prototype &&
|
|
36
|
+
Object.getPrototypeOf(prototype) !== null) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
// Exclude null-prototype exotics such as module namespace objects.
|
|
40
|
+
return !(Symbol.toStringTag in value || Symbol.iterator in value);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Returns the input when it is a plain object that can be treated as a runtime
|
|
44
|
+
* JSON object, as decided by {@link isRuntimeObject} (see there for what counts
|
|
45
|
+
* as a plain object and the caveats about unsanitized keys).
|
|
3
46
|
*
|
|
4
47
|
* @param value - Value to narrow.
|
|
5
|
-
* @returns The same value as a {@link RuntimeObject}, or `undefined` when the
|
|
48
|
+
* @returns The same value as a {@link RuntimeObject}, or `undefined` when the
|
|
49
|
+
* value is not a plain object.
|
|
6
50
|
*/
|
|
7
51
|
export function asRuntimeObject(value) {
|
|
8
|
-
return
|
|
9
|
-
|
|
10
|
-
|
|
52
|
+
return isRuntimeObject(value) ? value : undefined;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Type guard that checks whether a value is a string.
|
|
56
|
+
*
|
|
57
|
+
* @param value - Value to check.
|
|
58
|
+
* @returns `true` when the value is a string, narrowing it to `string`.
|
|
59
|
+
*/
|
|
60
|
+
export function isString(value) {
|
|
61
|
+
return typeof value === "string";
|
|
11
62
|
}
|
|
12
63
|
/**
|
|
13
64
|
* Returns the input when it is a string.
|
|
@@ -16,24 +67,18 @@ export function asRuntimeObject(value) {
|
|
|
16
67
|
* @returns The same value as a string, or `undefined` when the value is not a string.
|
|
17
68
|
*/
|
|
18
69
|
export function asString(value) {
|
|
19
|
-
return
|
|
70
|
+
return isString(value) ? value : undefined;
|
|
20
71
|
}
|
|
21
72
|
/**
|
|
22
73
|
* Filters an unknown value down to an array of runtime JSON objects.
|
|
23
74
|
*
|
|
75
|
+
* Elements are kept when {@link isRuntimeObject} accepts them, i.e. only plain
|
|
76
|
+
* objects; the same caveats about unsanitized keys apply.
|
|
77
|
+
*
|
|
24
78
|
* @param value - Value that may contain runtime objects.
|
|
25
79
|
* @returns Only the elements that can be treated as {@link RuntimeObject} values.
|
|
26
80
|
*/
|
|
27
81
|
export function toRuntimeObjectArray(value) {
|
|
28
|
-
|
|
29
|
-
return [];
|
|
30
|
-
}
|
|
31
|
-
return value.reduce((objects, item) => {
|
|
32
|
-
const object = asRuntimeObject(item);
|
|
33
|
-
if (object) {
|
|
34
|
-
objects.push(object);
|
|
35
|
-
}
|
|
36
|
-
return objects;
|
|
37
|
-
}, []);
|
|
82
|
+
return Array.isArray(value) ? value.filter(isRuntimeObject) : [];
|
|
38
83
|
}
|
|
39
84
|
//# sourceMappingURL=types.js.map
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AASA
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AASA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc;IAC1C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAC9C,OAAO,KAAK,CAAA;IAChB,CAAC;IACD,MAAM,SAAS,GAAY,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAA;IACvD,oEAAoE;IACpE,4CAA4C;IAC5C,IACI,SAAS,KAAK,IAAI;QAClB,SAAS,KAAK,MAAM,CAAC,SAAS;QAC9B,MAAM,CAAC,cAAc,CAAC,SAAS,CAAC,KAAK,IAAI,EAC3C,CAAC;QACC,OAAO,KAAK,CAAA;IAChB,CAAC;IACD,mEAAmE;IACnE,OAAO,CAAC,CAAC,MAAM,CAAC,WAAW,IAAI,KAAK,IAAI,MAAM,CAAC,QAAQ,IAAI,KAAK,CAAC,CAAA;AACrE,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc;IAC1C,OAAO,eAAe,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;AACrD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc;IACnC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAA;AACpC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc;IACnC,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;AAC9C,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,oBAAoB,CAAC,KAAc;IAC/C,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;AACpE,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.
|
|
4
|
+
"version": "0.11.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "darthcav",
|
|
7
7
|
"url": "https://github.com/darthcav"
|
|
@@ -24,13 +24,13 @@
|
|
|
24
24
|
"!dist/__tests__"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@logtape/logtape": "2.3.
|
|
27
|
+
"@logtape/logtape": "2.3.10"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@biomejs/biome": "2.5.
|
|
31
|
-
"@types/node": "26.
|
|
30
|
+
"@biomejs/biome": "2.5.15",
|
|
31
|
+
"@types/node": "26.6.4",
|
|
32
32
|
"asserttt": "1.0.1",
|
|
33
|
-
"prettier": "3.9.
|
|
33
|
+
"prettier": "3.9.9",
|
|
34
34
|
"shx": "0.4.0",
|
|
35
35
|
"typedoc": "0.28.20"
|
|
36
36
|
},
|
|
@@ -49,9 +49,9 @@
|
|
|
49
49
|
"\n===== Typecheck =====": "",
|
|
50
50
|
"typecheck": "tsc --noEmit",
|
|
51
51
|
"\n===== Test =====": "",
|
|
52
|
-
"test": "node --env-file=.env.local --experimental-test-module-mocks --test --test-reporter=spec
|
|
53
|
-
"test:coverage": "node --env-file=.env.local --experimental-test-module-mocks --experimental-test-coverage --test
|
|
54
|
-
"test:coverage:lcov": "shx mkdir -p coverage && node --env-file=.env.local --experimental-test-module-mocks --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/lcov.info --test-reporter=spec --test-reporter-destination=stdout --test
|
|
52
|
+
"test": "node --env-file-if-exists=.env.local --experimental-test-module-mocks --test --test-reporter=spec \"src/**/*.test.ts\"",
|
|
53
|
+
"test:coverage": "node --env-file-if-exists=.env.local --experimental-test-module-mocks --experimental-test-coverage --test \"src/**/*.test.ts\"",
|
|
54
|
+
"test:coverage:lcov": "shx mkdir -p coverage && node --env-file-if-exists=.env.local --experimental-test-module-mocks --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/lcov.info --test-reporter=spec --test-reporter-destination=stdout --test \"src/**/*.test.ts\"",
|
|
55
55
|
"\n===== Linter =====": "",
|
|
56
56
|
"lint": "biome check . && prettier --check \"**/*.md\" --ignore-path .gitignore",
|
|
57
57
|
"lint:fix": "biome check --write . && prettier --write \"**/*.md\" --ignore-path .gitignore",
|
package/src/index.ts
CHANGED
|
@@ -4,16 +4,18 @@
|
|
|
4
4
|
* @packageDocumentation
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import getConsoleLogger from "./loggers/getConsoleLogger.ts"
|
|
8
|
-
import getDummyLogger from "./loggers/getDummyLogger.ts"
|
|
9
|
-
import { type LauncherFunction, main } from "./main.ts"
|
|
10
|
-
import millisecondsToString from "./millisecondsToString.ts"
|
|
11
|
-
import monitorMemory from "./monitorMemory.ts"
|
|
12
|
-
import noop from "./noop.ts"
|
|
13
|
-
import { type OsRelease, osRelease } from "./
|
|
7
|
+
import { getConsoleLogger } from "./loggers/getConsoleLogger.ts"
|
|
8
|
+
import { getDummyLogger } from "./loggers/getDummyLogger.ts"
|
|
9
|
+
import { type LauncherFunction, type MainOptions, main } from "./main.ts"
|
|
10
|
+
import { millisecondsToString } from "./millisecondsToString.ts"
|
|
11
|
+
import { monitorMemory } from "./monitorMemory.ts"
|
|
12
|
+
import { noop } from "./noop.ts"
|
|
13
|
+
import { type OsRelease, osRelease } from "./osRelease.ts"
|
|
14
14
|
import {
|
|
15
15
|
asRuntimeObject,
|
|
16
16
|
asString,
|
|
17
|
+
isRuntimeObject,
|
|
18
|
+
isString,
|
|
17
19
|
type RuntimeObject,
|
|
18
20
|
toRuntimeObjectArray,
|
|
19
21
|
} from "./types.ts"
|
|
@@ -23,7 +25,10 @@ export {
|
|
|
23
25
|
asString,
|
|
24
26
|
getConsoleLogger,
|
|
25
27
|
getDummyLogger,
|
|
28
|
+
isRuntimeObject,
|
|
29
|
+
isString,
|
|
26
30
|
type LauncherFunction,
|
|
31
|
+
type MainOptions,
|
|
27
32
|
main,
|
|
28
33
|
millisecondsToString,
|
|
29
34
|
monitorMemory,
|
|
@@ -1,18 +1,52 @@
|
|
|
1
|
+
import process from "node:process"
|
|
1
2
|
import {
|
|
2
3
|
configure,
|
|
3
4
|
getAnsiColorFormatter,
|
|
4
5
|
getConsoleSink,
|
|
5
6
|
getLogger,
|
|
7
|
+
getTextFormatter,
|
|
6
8
|
type Logger,
|
|
7
9
|
type LogLevel,
|
|
10
|
+
type TextFormatterOptions,
|
|
8
11
|
} from "@logtape/logtape"
|
|
9
12
|
|
|
13
|
+
/** Formatter options shared by the colored and the plain formatter. */
|
|
14
|
+
const FORMATTER_OPTIONS: TextFormatterOptions = {
|
|
15
|
+
timestamp: "rfc3339",
|
|
16
|
+
level: "FULL",
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Decides whether console output should use ANSI colors.
|
|
21
|
+
*
|
|
22
|
+
* `FORCE_COLOR` wins when set (`"0"` or `"false"` disable colors, any other
|
|
23
|
+
* value enables them). Otherwise colors are used only when both stdout and
|
|
24
|
+
* stderr are terminals that support them, since the console sink writes
|
|
25
|
+
* warnings and errors to stderr. `hasColors()` also honors `NO_COLOR`,
|
|
26
|
+
* `NODE_DISABLE_COLORS`, and `TERM=dumb`.
|
|
27
|
+
*/
|
|
28
|
+
function useColors(): boolean {
|
|
29
|
+
const force = process.env["FORCE_COLOR"]
|
|
30
|
+
if (force !== undefined) {
|
|
31
|
+
return force !== "0" && force !== "false"
|
|
32
|
+
}
|
|
33
|
+
return [process.stdout, process.stderr].every(
|
|
34
|
+
(stream) => stream.isTTY === true && stream.hasColors(),
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
10
38
|
/**
|
|
11
39
|
* Configures logging and returns a {@link Logger} for the given category name.
|
|
12
40
|
*
|
|
13
|
-
* Records at or above `lowestLevel` are written to the console
|
|
14
|
-
*
|
|
15
|
-
*
|
|
41
|
+
* Records at or above `lowestLevel` are written to the console with RFC 3339
|
|
42
|
+
* timestamps and full level names. The internal `logtape/meta` logger is
|
|
43
|
+
* silenced.
|
|
44
|
+
*
|
|
45
|
+
* ANSI colors are used only when both stdout and stderr are terminals that
|
|
46
|
+
* support them (respecting `NO_COLOR`, `NODE_DISABLE_COLORS`, and `TERM=dumb`),
|
|
47
|
+
* so output redirected to files or pipes stays free of escape codes. Set
|
|
48
|
+
* `FORCE_COLOR` to override: `"0"` or `"false"` disables colors, any other
|
|
49
|
+
* value enables them. The decision is made when this function is called.
|
|
16
50
|
*
|
|
17
51
|
* Logging is configured with `reset: true`, so this function is safe to call
|
|
18
52
|
* more than once: each call reconfigures logtape from scratch and the most
|
|
@@ -23,17 +57,16 @@ import {
|
|
|
23
57
|
* @param lowestLevel - The minimum log level to output. Defaults to `"info"`.
|
|
24
58
|
* @returns A promise resolving to a logger scoped to the given category.
|
|
25
59
|
*/
|
|
26
|
-
export
|
|
60
|
+
export async function getConsoleLogger(
|
|
27
61
|
name: string,
|
|
28
62
|
lowestLevel: LogLevel = "info",
|
|
29
63
|
): Promise<Logger> {
|
|
30
64
|
await configure({
|
|
31
65
|
sinks: {
|
|
32
66
|
console: getConsoleSink({
|
|
33
|
-
formatter:
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}),
|
|
67
|
+
formatter: useColors()
|
|
68
|
+
? getAnsiColorFormatter(FORMATTER_OPTIONS)
|
|
69
|
+
: getTextFormatter(FORMATTER_OPTIONS),
|
|
37
70
|
}),
|
|
38
71
|
},
|
|
39
72
|
loggers: [
|
|
@@ -1,26 +1,46 @@
|
|
|
1
1
|
import type { Logger, LogLevel, LogRecord } from "@logtape/logtape"
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Already-settled promise returned by every logging method. It satisfies the
|
|
5
|
+
* overloads that return `Promise<void>` (asynchronous lazy properties), just as
|
|
6
|
+
* a real logtape logger does for a disabled level.
|
|
7
|
+
*/
|
|
8
|
+
const settled: Promise<void> = Promise.resolve()
|
|
9
|
+
|
|
10
|
+
/** A logging method that accepts every logtape overload and does nothing. */
|
|
11
|
+
const log = (..._args: readonly unknown[]): Promise<void> => settled
|
|
3
12
|
|
|
4
13
|
/**
|
|
5
14
|
* Returns a no-op {@link Logger} useful as a placeholder in tests.
|
|
6
15
|
*
|
|
7
16
|
* All logging methods (`trace`, `debug`, `info`, `warn`, `warning`, `error`,
|
|
8
|
-
* `fatal`) are no-ops
|
|
9
|
-
*
|
|
17
|
+
* `fatal`) are no-ops; the ones logtape types as returning a promise return an
|
|
18
|
+
* already-settled one. `isEnabledFor` always returns `false`. `getChild` and
|
|
19
|
+
* `with` return the same dummy logger instance.
|
|
20
|
+
*
|
|
21
|
+
* Each call returns a new instance, so mocking a method on one (e.g. with
|
|
22
|
+
* `mock.method(logger, "info")`) does not affect others.
|
|
10
23
|
*
|
|
11
24
|
* @returns A logger whose methods are all no-ops.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { getDummyLogger, main } from "@darthcav/ts-utils"
|
|
29
|
+
*
|
|
30
|
+
* main("test-app", getDummyLogger())
|
|
31
|
+
* ```
|
|
12
32
|
*/
|
|
13
|
-
export
|
|
33
|
+
export function getDummyLogger(): Logger {
|
|
14
34
|
const logger: Logger = {
|
|
15
35
|
category: [],
|
|
16
36
|
parent: null,
|
|
17
|
-
trace:
|
|
18
|
-
debug:
|
|
19
|
-
info:
|
|
20
|
-
warn:
|
|
21
|
-
warning:
|
|
22
|
-
error:
|
|
23
|
-
fatal:
|
|
37
|
+
trace: log,
|
|
38
|
+
debug: log,
|
|
39
|
+
info: log,
|
|
40
|
+
warn: log,
|
|
41
|
+
warning: log,
|
|
42
|
+
error: log,
|
|
43
|
+
fatal: log,
|
|
24
44
|
emit: (_record: Omit<LogRecord, "category">): void => {},
|
|
25
45
|
getChild: () => logger,
|
|
26
46
|
isEnabledFor: (_level: LogLevel): boolean => false,
|