@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.
Files changed (51) hide show
  1. package/README.md +137 -39
  2. package/dist/index.d.ts +9 -9
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +8 -8
  5. package/dist/index.js.map +1 -1
  6. package/dist/loggers/getConsoleLogger.d.ts +10 -4
  7. package/dist/loggers/getConsoleLogger.d.ts.map +1 -1
  8. package/dist/loggers/getConsoleLogger.js +36 -9
  9. package/dist/loggers/getConsoleLogger.js.map +1 -1
  10. package/dist/loggers/getDummyLogger.d.ts +14 -3
  11. package/dist/loggers/getDummyLogger.d.ts.map +1 -1
  12. package/dist/loggers/getDummyLogger.js +29 -11
  13. package/dist/loggers/getDummyLogger.js.map +1 -1
  14. package/dist/main.d.ts +83 -51
  15. package/dist/main.d.ts.map +1 -1
  16. package/dist/main.js +200 -40
  17. package/dist/main.js.map +1 -1
  18. package/dist/millisecondsToString.d.ts +1 -1
  19. package/dist/millisecondsToString.d.ts.map +1 -1
  20. package/dist/millisecondsToString.js +1 -1
  21. package/dist/millisecondsToString.js.map +1 -1
  22. package/dist/monitorMemory.d.ts +25 -7
  23. package/dist/monitorMemory.d.ts.map +1 -1
  24. package/dist/monitorMemory.js +52 -14
  25. package/dist/monitorMemory.js.map +1 -1
  26. package/dist/noop.d.ts +1 -2
  27. package/dist/noop.d.ts.map +1 -1
  28. package/dist/noop.js +1 -2
  29. package/dist/noop.js.map +1 -1
  30. package/dist/{os-utils.d.ts → osRelease.d.ts} +11 -7
  31. package/dist/osRelease.d.ts.map +1 -0
  32. package/dist/osRelease.js +153 -0
  33. package/dist/osRelease.js.map +1 -0
  34. package/dist/types.d.ts +42 -2
  35. package/dist/types.d.ts.map +1 -1
  36. package/dist/types.js +61 -16
  37. package/dist/types.js.map +1 -1
  38. package/package.json +8 -8
  39. package/src/index.ts +12 -7
  40. package/src/loggers/getConsoleLogger.ts +41 -8
  41. package/src/loggers/getDummyLogger.ts +31 -11
  42. package/src/main.ts +249 -126
  43. package/src/millisecondsToString.ts +1 -4
  44. package/src/monitorMemory.ts +58 -19
  45. package/src/noop.ts +1 -3
  46. package/src/osRelease.ts +172 -0
  47. package/src/types.ts +65 -17
  48. package/dist/os-utils.d.ts.map +0 -1
  49. package/dist/os-utils.js +0 -94
  50. package/dist/os-utils.js.map +0 -1
  51. package/src/os-utils.ts +0 -110
@@ -0,0 +1,172 @@
1
+ import { readFileSync } from "node:fs"
2
+ import { arch, release as kernelRelease, platform } from "node:os"
3
+ import type { RuntimeObject } from "./types.ts"
4
+
5
+ /**
6
+ * OS release information with normalized fields and optional platform-specific extras.
7
+ *
8
+ * `name`, `version`, and `arch` are always present. On Linux, all keys from
9
+ * `/etc/os-release` (e.g. `ID`, `PRETTY_NAME`, `ID_LIKE`) are also accessible by string index.
10
+ */
11
+ export type OsRelease = {
12
+ /** Human-readable OS name, e.g. `"Ubuntu"` or `"Windows 11"`. */
13
+ name: string
14
+ /** OS version string, e.g. `"24.04"` or `"10.0.22000"`. */
15
+ version: string
16
+ /** Processor architecture as returned by `os.arch()`, e.g. `"x64"`. */
17
+ arch: string
18
+ } & RuntimeObject
19
+
20
+ /**
21
+ * Candidate os-release files, in lookup order. Per the freedesktop.org spec,
22
+ * `/etc/os-release` takes precedence and `/usr/lib/os-release` is the fallback.
23
+ */
24
+ const OS_RELEASE_FILES = ["/etc/os-release", "/usr/lib/os-release"] as const
25
+
26
+ /**
27
+ * Valid os-release keys: shell-compatible, upper-case variable names. This
28
+ * also rules out keys such as `__proto__` or `constructor`.
29
+ */
30
+ const OS_RELEASE_KEY = /^[A-Z][A-Z0-9_]*$/
31
+
32
+ /**
33
+ * Reads the first readable os-release file, or returns `null` when none can be
34
+ * read (missing, unreadable, or not a regular file).
35
+ */
36
+ function readOsReleaseFile(): string | null {
37
+ for (const file of OS_RELEASE_FILES) {
38
+ try {
39
+ return readFileSync(file, "utf-8")
40
+ } catch {
41
+ // Try the next candidate.
42
+ }
43
+ }
44
+ return null
45
+ }
46
+
47
+ /**
48
+ * Removes one level of shell-style quoting from an os-release value. Double
49
+ * quotes allow the `\\`, `\"`, `` \` `` and `\$` escapes; single quotes are
50
+ * literal.
51
+ */
52
+ function unquote(value: string): string {
53
+ if (value.length >= 2) {
54
+ const quote = value[0]
55
+ if (quote === '"' && value.endsWith('"')) {
56
+ return value.slice(1, -1).replace(/\\([\\"`$])/g, "$1")
57
+ }
58
+ if (quote === "'" && value.endsWith("'")) {
59
+ return value.slice(1, -1)
60
+ }
61
+ }
62
+ return value
63
+ }
64
+
65
+ /**
66
+ * Parses os-release content into a null-prototype record. Blank lines,
67
+ * comments, lines without `=`, and keys that are not valid variable names are
68
+ * skipped.
69
+ */
70
+ function parseOsRelease(content: string): Record<string, string> {
71
+ const raw: Record<string, string> = Object.create(null)
72
+ for (const line of content.split("\n")) {
73
+ const trimmed = line.trim()
74
+ if (trimmed === "" || trimmed.startsWith("#")) {
75
+ continue
76
+ }
77
+ const eq = trimmed.indexOf("=")
78
+ if (eq === -1) {
79
+ continue
80
+ }
81
+ const key = trimmed.substring(0, eq).trim()
82
+ if (!OS_RELEASE_KEY.test(key)) {
83
+ continue
84
+ }
85
+ raw[key] = unquote(trimmed.substring(eq + 1).trim())
86
+ }
87
+ return raw
88
+ }
89
+
90
+ /**
91
+ * Returns OS release information for the current platform, or `null` on
92
+ * unsupported platforms or when no os-release file can be read on Linux.
93
+ *
94
+ * On Linux, the information is read from `/etc/os-release`, falling back to
95
+ * `/usr/lib/os-release`. `name`, `version`, and `arch` are normalized from
96
+ * `NAME`/`PRETTY_NAME`, `VERSION_ID`/`VERSION`, and `os.arch()` respectively.
97
+ * All raw keys from the file are also present on the returned object. Comment
98
+ * lines are ignored, single- and double-quoted values are unquoted (with
99
+ * backslash escapes inside double quotes), and keys that are not upper-case
100
+ * shell variable names are skipped.
101
+ *
102
+ * On Windows, distinguishes Windows 11 from Windows 10 by NT build number
103
+ * (>= 22000 → Windows 11).
104
+ *
105
+ * @returns An {@link OsRelease} object on supported platforms, or `null` when
106
+ * the platform is unsupported or no os-release file can be read on Linux.
107
+ *
108
+ * @example
109
+ * ```ts
110
+ * import { osRelease } from "@darthcav/ts-utils"
111
+ *
112
+ * const info = osRelease()
113
+ * if (info) console.log(info.name) // e.g. "Ubuntu" or "Windows 11"
114
+ * if (info) console.log(info.PRETTY_NAME) // e.g. "Ubuntu 24.04 LTS" (Linux only)
115
+ * ```
116
+ */
117
+ export function osRelease(): OsRelease | null {
118
+ const p = platform()
119
+ if (p === "linux") {
120
+ const content = readOsReleaseFile()
121
+ if (content === null) {
122
+ return null
123
+ }
124
+ const raw = parseOsRelease(content)
125
+ return {
126
+ ...raw,
127
+ name: raw["NAME"] ?? raw["PRETTY_NAME"] ?? "",
128
+ version: raw["VERSION_ID"] ?? raw["VERSION"] ?? "",
129
+ arch: arch(),
130
+ }
131
+ }
132
+ if (p === "win32") {
133
+ const version = kernelRelease()
134
+ const [major, , build] = version.split(".").map(Number)
135
+ let name = "Windows "
136
+ if (major === 10 && (build ?? 0) >= 22000) {
137
+ name += "11"
138
+ } else {
139
+ switch (version.substring(0, 3)) {
140
+ case "10.":
141
+ name += "10"
142
+ break
143
+ case "6.3":
144
+ name += "8.1"
145
+ break
146
+ case "6.2":
147
+ name += "8"
148
+ break
149
+ case "6.1":
150
+ name += "7"
151
+ break
152
+ case "6.0":
153
+ name += "Vista"
154
+ break
155
+ case "5.2":
156
+ name += "XP 64-Bit Edition"
157
+ break
158
+ case "5.1":
159
+ name += "XP"
160
+ break
161
+ case "5.0":
162
+ name += "2000"
163
+ break
164
+ default:
165
+ name += "unknown version"
166
+ break
167
+ }
168
+ }
169
+ return { name, version, arch: arch() }
170
+ }
171
+ return null
172
+ }
package/src/types.ts CHANGED
@@ -8,15 +8,70 @@
8
8
  export type RuntimeObject = Record<string, unknown>
9
9
 
10
10
  /**
11
- * Returns the input when it is a non-array object that can be treated as a runtime JSON object.
11
+ * Type guard that checks whether a value is a plain object that can be treated
12
+ * as a runtime JSON object.
13
+ *
14
+ * Plain objects are object literals, `JSON.parse` results, and
15
+ * `Object.create(null)` objects, including those from another realm (e.g. a
16
+ * `node:vm` context). Arrays, class instances, and built-ins such as `Date`,
17
+ * `Map`, `Set`, `RegExp`, or module namespace objects are rejected.
18
+ *
19
+ * The check is shallow and keys are not sanitized: an own `__proto__` or
20
+ * `constructor` key (as `JSON.parse` can produce from untrusted input) is kept.
21
+ * Do not merge the value into other objects key by key without guarding
22
+ * against those keys, or prototype pollution becomes possible.
23
+ *
24
+ * @param value - Value to check.
25
+ * @returns `true` when the value is a plain object, narrowing it to
26
+ * {@link RuntimeObject}.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * const body: unknown = JSON.parse(input)
31
+ * if (isRuntimeObject(body)) {
32
+ * console.log(Object.keys(body)) // body: RuntimeObject
33
+ * }
34
+ * ```
35
+ */
36
+ export function isRuntimeObject(value: unknown): value is RuntimeObject {
37
+ if (typeof value !== "object" || value === null) {
38
+ return false
39
+ }
40
+ const prototype: unknown = Object.getPrototypeOf(value)
41
+ // A null prototype, or a realm's root `Object.prototype` (whose own
42
+ // prototype is null), marks a plain object.
43
+ if (
44
+ prototype !== null &&
45
+ prototype !== Object.prototype &&
46
+ Object.getPrototypeOf(prototype) !== null
47
+ ) {
48
+ return false
49
+ }
50
+ // Exclude null-prototype exotics such as module namespace objects.
51
+ return !(Symbol.toStringTag in value || Symbol.iterator in value)
52
+ }
53
+
54
+ /**
55
+ * Returns the input when it is a plain object that can be treated as a runtime
56
+ * JSON object, as decided by {@link isRuntimeObject} (see there for what counts
57
+ * as a plain object and the caveats about unsanitized keys).
12
58
  *
13
59
  * @param value - Value to narrow.
14
- * @returns The same value as a {@link RuntimeObject}, or `undefined` when the value is not an object.
60
+ * @returns The same value as a {@link RuntimeObject}, or `undefined` when the
61
+ * value is not a plain object.
15
62
  */
16
63
  export function asRuntimeObject(value: unknown): RuntimeObject | undefined {
17
- return typeof value === "object" && value !== null && !Array.isArray(value)
18
- ? (value as RuntimeObject)
19
- : undefined
64
+ return isRuntimeObject(value) ? value : undefined
65
+ }
66
+
67
+ /**
68
+ * Type guard that checks whether a value is a string.
69
+ *
70
+ * @param value - Value to check.
71
+ * @returns `true` when the value is a string, narrowing it to `string`.
72
+ */
73
+ export function isString(value: unknown): value is string {
74
+ return typeof value === "string"
20
75
  }
21
76
 
22
77
  /**
@@ -26,25 +81,18 @@ export function asRuntimeObject(value: unknown): RuntimeObject | undefined {
26
81
  * @returns The same value as a string, or `undefined` when the value is not a string.
27
82
  */
28
83
  export function asString(value: unknown): string | undefined {
29
- return typeof value === "string" ? value : undefined
84
+ return isString(value) ? value : undefined
30
85
  }
31
86
 
32
87
  /**
33
88
  * Filters an unknown value down to an array of runtime JSON objects.
34
89
  *
90
+ * Elements are kept when {@link isRuntimeObject} accepts them, i.e. only plain
91
+ * objects; the same caveats about unsanitized keys apply.
92
+ *
35
93
  * @param value - Value that may contain runtime objects.
36
94
  * @returns Only the elements that can be treated as {@link RuntimeObject} values.
37
95
  */
38
96
  export function toRuntimeObjectArray(value: unknown): RuntimeObject[] {
39
- if (!Array.isArray(value)) {
40
- return []
41
- }
42
-
43
- return value.reduce<RuntimeObject[]>((objects, item) => {
44
- const object = asRuntimeObject(item)
45
- if (object) {
46
- objects.push(object)
47
- }
48
- return objects
49
- }, [])
97
+ return Array.isArray(value) ? value.filter(isRuntimeObject) : []
50
98
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"os-utils.d.ts","sourceRoot":"","sources":["../src/os-utils.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;AAIjB;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,SAAS,IAAI,SAAS,GAAG,IAAI,CAiE5C"}
package/dist/os-utils.js DELETED
@@ -1,94 +0,0 @@
1
- import { existsSync, readFileSync } from "node:fs";
2
- import { arch, release as kernelRelease, platform } from "node:os";
3
- const OS_RELEASE = "/etc/os-release";
4
- /**
5
- * Returns OS release information for the current platform, or `null` on unsupported
6
- * platforms or when `/etc/os-release` is absent on Linux.
7
- *
8
- * On Linux, `name`, `version`, and `arch` are normalized from `NAME`/`PRETTY_NAME`,
9
- * `VERSION_ID`/`VERSION`, and `os.arch()` respectively. All raw keys from
10
- * `/etc/os-release` are also present on the returned object.
11
- *
12
- * On Windows, distinguishes Windows 11 from Windows 10 by NT build number
13
- * (>= 22000 → Windows 11).
14
- *
15
- * @returns An {@link OsRelease} object on supported platforms, or `null` when
16
- * the platform is unsupported or `/etc/os-release` is absent on Linux.
17
- *
18
- * @example
19
- * ```ts
20
- * import { osRelease } from "@darthcav/ts-utils"
21
- *
22
- * const info = osRelease()
23
- * if (info) console.log(info.name) // e.g. "Ubuntu" or "Windows 11"
24
- * if (info) console.log(info.PRETTY_NAME) // e.g. "Ubuntu 24.04 LTS" (Linux only)
25
- * ```
26
- */
27
- export function osRelease() {
28
- const p = platform();
29
- if (p === "linux") {
30
- if (!existsSync(OS_RELEASE)) {
31
- return null;
32
- }
33
- const raw = {};
34
- for (const line of readFileSync(OS_RELEASE, "utf-8").split("\n")) {
35
- const eq = line.indexOf("=");
36
- if (eq === -1) {
37
- continue;
38
- }
39
- const key = line.substring(0, eq).trim();
40
- raw[key] = line
41
- .substring(eq + 1)
42
- .trim()
43
- .replace(/^"|"$/g, "");
44
- }
45
- return {
46
- ...raw,
47
- name: raw["NAME"] ?? raw["PRETTY_NAME"] ?? "",
48
- version: raw["VERSION_ID"] ?? raw["VERSION"] ?? "",
49
- arch: arch(),
50
- };
51
- }
52
- if (p === "win32") {
53
- const version = kernelRelease();
54
- const [major, , build] = version.split(".").map(Number);
55
- let name = "Windows ";
56
- if (major === 10 && (build ?? 0) >= 22000) {
57
- name += "11";
58
- }
59
- else {
60
- switch (version.substring(0, 3)) {
61
- case "10.":
62
- name += "10";
63
- break;
64
- case "6.3":
65
- name += "8.1";
66
- break;
67
- case "6.2":
68
- name += "8";
69
- break;
70
- case "6.1":
71
- name += "7";
72
- break;
73
- case "6.0":
74
- name += "Vista";
75
- break;
76
- case "5.2":
77
- name += "XP 64-Bit Edition";
78
- break;
79
- case "5.1":
80
- name += "XP";
81
- break;
82
- case "5.0":
83
- name += "2000";
84
- break;
85
- default:
86
- name += "unknown version";
87
- break;
88
- }
89
- }
90
- return { name, version, arch: arch() };
91
- }
92
- return null;
93
- }
94
- //# sourceMappingURL=os-utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"os-utils.js","sourceRoot":"","sources":["../src/os-utils.ts"],"names":[],"mappings":"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"}
package/src/os-utils.ts DELETED
@@ -1,110 +0,0 @@
1
- import { existsSync, readFileSync } from "node:fs"
2
- import { arch, release as kernelRelease, platform } from "node:os"
3
- import type { RuntimeObject } from "./types.ts"
4
-
5
- /**
6
- * OS release information with normalized fields and optional platform-specific extras.
7
- *
8
- * `name`, `version`, and `arch` are always present. On Linux, all keys from
9
- * `/etc/os-release` (e.g. `ID`, `PRETTY_NAME`, `ID_LIKE`) are also accessible by string index.
10
- */
11
- export type OsRelease = {
12
- /** Human-readable OS name, e.g. `"Ubuntu"` or `"Windows 11"`. */
13
- name: string
14
- /** OS version string, e.g. `"24.04"` or `"10.0.22000"`. */
15
- version: string
16
- /** Processor architecture as returned by `os.arch()`, e.g. `"x64"`. */
17
- arch: string
18
- } & RuntimeObject
19
-
20
- const OS_RELEASE = "/etc/os-release"
21
-
22
- /**
23
- * Returns OS release information for the current platform, or `null` on unsupported
24
- * platforms or when `/etc/os-release` is absent on Linux.
25
- *
26
- * On Linux, `name`, `version`, and `arch` are normalized from `NAME`/`PRETTY_NAME`,
27
- * `VERSION_ID`/`VERSION`, and `os.arch()` respectively. All raw keys from
28
- * `/etc/os-release` are also present on the returned object.
29
- *
30
- * On Windows, distinguishes Windows 11 from Windows 10 by NT build number
31
- * (>= 22000 → Windows 11).
32
- *
33
- * @returns An {@link OsRelease} object on supported platforms, or `null` when
34
- * the platform is unsupported or `/etc/os-release` is absent on Linux.
35
- *
36
- * @example
37
- * ```ts
38
- * import { osRelease } from "@darthcav/ts-utils"
39
- *
40
- * const info = osRelease()
41
- * if (info) console.log(info.name) // e.g. "Ubuntu" or "Windows 11"
42
- * if (info) console.log(info.PRETTY_NAME) // e.g. "Ubuntu 24.04 LTS" (Linux only)
43
- * ```
44
- */
45
- export function osRelease(): OsRelease | null {
46
- const p = platform()
47
- if (p === "linux") {
48
- if (!existsSync(OS_RELEASE)) {
49
- return null
50
- }
51
- const raw: Record<string, string> = {}
52
- for (const line of readFileSync(OS_RELEASE, "utf-8").split("\n")) {
53
- const eq = line.indexOf("=")
54
- if (eq === -1) {
55
- continue
56
- }
57
- const key = line.substring(0, eq).trim()
58
- raw[key] = line
59
- .substring(eq + 1)
60
- .trim()
61
- .replace(/^"|"$/g, "")
62
- }
63
- return {
64
- ...raw,
65
- name: raw["NAME"] ?? raw["PRETTY_NAME"] ?? "",
66
- version: raw["VERSION_ID"] ?? raw["VERSION"] ?? "",
67
- arch: arch(),
68
- }
69
- }
70
- if (p === "win32") {
71
- const version = kernelRelease()
72
- const [major, , build] = version.split(".").map(Number)
73
- let name = "Windows "
74
- if (major === 10 && (build ?? 0) >= 22000) {
75
- name += "11"
76
- } else {
77
- switch (version.substring(0, 3)) {
78
- case "10.":
79
- name += "10"
80
- break
81
- case "6.3":
82
- name += "8.1"
83
- break
84
- case "6.2":
85
- name += "8"
86
- break
87
- case "6.1":
88
- name += "7"
89
- break
90
- case "6.0":
91
- name += "Vista"
92
- break
93
- case "5.2":
94
- name += "XP 64-Bit Edition"
95
- break
96
- case "5.1":
97
- name += "XP"
98
- break
99
- case "5.0":
100
- name += "2000"
101
- break
102
- default:
103
- name += "unknown version"
104
- break
105
- }
106
- }
107
- return { name, version, arch: arch() }
108
- }
109
- return null
110
- }