@darthcav/ts-utils 0.10.10 → 0.12.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 +170 -39
- package/dist/index.d.ts +16 -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/type-testing/Assert.d.ts +23 -0
- package/dist/type-testing/Assert.d.ts.map +1 -0
- package/dist/type-testing/Assert.js +2 -0
- package/dist/type-testing/Assert.js.map +1 -0
- package/dist/type-testing/Equal.d.ts +25 -0
- package/dist/type-testing/Equal.d.ts.map +1 -0
- package/dist/type-testing/Equal.js +2 -0
- package/dist/type-testing/Equal.js.map +1 -0
- package/dist/type-testing/Extends.d.ts +22 -0
- package/dist/type-testing/Extends.d.ts.map +1 -0
- package/dist/type-testing/Extends.js +2 -0
- package/dist/type-testing/Extends.js.map +1 -0
- package/dist/type-testing/IsAny.d.ts +17 -0
- package/dist/type-testing/IsAny.d.ts.map +1 -0
- package/dist/type-testing/IsAny.js +2 -0
- package/dist/type-testing/IsAny.js.map +1 -0
- package/dist/type-testing/IsNever.d.ts +17 -0
- package/dist/type-testing/IsNever.d.ts.map +1 -0
- package/dist/type-testing/IsNever.js +2 -0
- package/dist/type-testing/IsNever.js.map +1 -0
- package/dist/type-testing/IsUnknown.d.ts +20 -0
- package/dist/type-testing/IsUnknown.d.ts.map +1 -0
- package/dist/type-testing/IsUnknown.js +2 -0
- package/dist/type-testing/IsUnknown.js.map +1 -0
- package/dist/type-testing/Not.d.ts +17 -0
- package/dist/type-testing/Not.d.ts.map +1 -0
- package/dist/type-testing/Not.js +2 -0
- package/dist/type-testing/Not.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 +10 -12
- package/src/index.ts +26 -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/type-testing/Assert.ts +22 -0
- package/src/type-testing/Equal.ts +27 -0
- package/src/type-testing/Extends.ts +21 -0
- package/src/type-testing/IsAny.ts +16 -0
- package/src/type-testing/IsNever.ts +16 -0
- package/src/type-testing/IsUnknown.ts +21 -0
- package/src/type-testing/Not.ts +16 -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
package/src/osRelease.ts
ADDED
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compile-time assertion: type-checks only when `T` is exactly `true`.
|
|
3
|
+
*
|
|
4
|
+
* Wrap one of the type-level predicates of this module ({@link Equal},
|
|
5
|
+
* {@link Extends}, {@link Not}, {@link IsAny}, {@link IsNever},
|
|
6
|
+
* {@link IsUnknown}) in it and declare the result as an (unused) type alias.
|
|
7
|
+
* A predicate that evaluates to `false` or `boolean` becomes a type error that
|
|
8
|
+
* `tsc` reports. The alias resolves to `true` and, like every type, is erased
|
|
9
|
+
* at runtime.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam T - The predicate result that must be `true`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import type { Assert, Equal } from "@darthcav/ts-utils"
|
|
16
|
+
*
|
|
17
|
+
* type _Ok = Assert<Equal<ReturnType<typeof parseInt>, number>>
|
|
18
|
+
* // @ts-expect-error -- `string` is not `number`
|
|
19
|
+
* type _Fails = Assert<Equal<string, number>>
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export type Assert<T extends true> = T
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: `true` when `X` and `Y` are identical types, `false`
|
|
3
|
+
* otherwise.
|
|
4
|
+
*
|
|
5
|
+
* The comparison is the one the compiler uses for type identity, so it is
|
|
6
|
+
* stricter than mutual assignability: `any` only equals `any`, and modifiers
|
|
7
|
+
* such as `readonly` and `?` must match. An intersection is not identical to
|
|
8
|
+
* the equivalent flattened object type (`{ a: 1 } & { b: 2 }` is not
|
|
9
|
+
* `{ a: 1; b: 2 }`); flatten it with a mapped type before comparing when
|
|
10
|
+
* needed.
|
|
11
|
+
*
|
|
12
|
+
* @typeParam X - The first type to compare.
|
|
13
|
+
* @typeParam Y - The second type to compare.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import type { Assert, Equal, Not } from "@darthcav/ts-utils"
|
|
18
|
+
*
|
|
19
|
+
* type _Same = Assert<Equal<{ a: string }, { a: string }>>
|
|
20
|
+
* type _Readonly = Assert<Not<Equal<{ readonly a: string }, { a: string }>>>
|
|
21
|
+
* type _Any = Assert<Not<Equal<any, string>>>
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export type Equal<X, Y> =
|
|
25
|
+
(<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2
|
|
26
|
+
? true
|
|
27
|
+
: false
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: `true` when `X` is assignable to `Y`, `false`
|
|
3
|
+
* otherwise.
|
|
4
|
+
*
|
|
5
|
+
* Unions are compared as a whole rather than member by member, so
|
|
6
|
+
* `Extends<string | number, string>` is `false`. Since `any` and `never` are
|
|
7
|
+
* assignable to every type, `Extends<any, Y>` and `Extends<never, Y>` are
|
|
8
|
+
* always `true`; use {@link Equal} to check for an exact type.
|
|
9
|
+
*
|
|
10
|
+
* @typeParam X - The type to check.
|
|
11
|
+
* @typeParam Y - The type `X` must be assignable to.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import type { Assert, Extends, Not } from "@darthcav/ts-utils"
|
|
16
|
+
*
|
|
17
|
+
* type _Literal = Assert<Extends<"a", string>>
|
|
18
|
+
* type _Union = Assert<Not<Extends<string | number, string>>>
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export type Extends<X, Y> = [X] extends [Y] ? true : false
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: `true` when `T` is `any`, `false` otherwise.
|
|
3
|
+
*
|
|
4
|
+
* Useful to catch a value or return type that silently widened to `any`.
|
|
5
|
+
*
|
|
6
|
+
* @typeParam T - The type to check.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import type { Assert, IsAny, Not } from "@darthcav/ts-utils"
|
|
11
|
+
*
|
|
12
|
+
* type _Parsed = Assert<IsAny<ReturnType<typeof JSON.parse>>>
|
|
13
|
+
* type _Typed = Assert<Not<IsAny<unknown>>>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export type IsAny<T> = 0 extends 1 & T ? true : false
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: `true` when `T` is `never`, `false` otherwise.
|
|
3
|
+
*
|
|
4
|
+
* Useful to check that a union has been exhausted or that a conditional type
|
|
5
|
+
* filters out every member.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam T - The type to check.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type { Assert, IsNever } from "@darthcav/ts-utils"
|
|
12
|
+
*
|
|
13
|
+
* type _Empty = Assert<IsNever<Exclude<"a" | "b", string>>>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export type IsNever<T> = [T] extends [never] ? true : false
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { IsAny } from "./IsAny.ts"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Type-level predicate: `true` when `T` is `unknown`, `false` otherwise
|
|
5
|
+
* (including for `any`).
|
|
6
|
+
*
|
|
7
|
+
* Useful to check that an untrusted value (e.g. parsed input) is typed as
|
|
8
|
+
* `unknown` and has to be narrowed before use.
|
|
9
|
+
*
|
|
10
|
+
* @typeParam T - The type to check.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import type { Assert, IsUnknown, Not } from "@darthcav/ts-utils"
|
|
15
|
+
*
|
|
16
|
+
* type _Unknown = Assert<IsUnknown<unknown>>
|
|
17
|
+
* type _Any = Assert<Not<IsUnknown<any>>>
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export type IsUnknown<T> =
|
|
21
|
+
IsAny<T> extends true ? false : unknown extends T ? true : false
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: negates a boolean predicate result.
|
|
3
|
+
*
|
|
4
|
+
* `Not<true>` is `false` and `Not<false>` is `true`. An undecided `boolean`
|
|
5
|
+
* stays `boolean`, so `Assert<Not<boolean>>` still fails.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam T - The predicate result to negate.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type { Assert, Equal, Not } from "@darthcav/ts-utils"
|
|
12
|
+
*
|
|
13
|
+
* type _Different = Assert<Not<Equal<string, number>>>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export type Not<T extends boolean> = T extends true ? false : true
|
package/src/types.ts
CHANGED
|
@@ -8,15 +8,70 @@
|
|
|
8
8
|
export type RuntimeObject = Record<string, unknown>
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
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
|
|
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
|
|
18
|
-
|
|
19
|
-
|
|
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
|
|
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
|
-
|
|
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
|
}
|
package/dist/os-utils.d.ts.map
DELETED
|
@@ -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
|
package/dist/os-utils.js.map
DELETED
|
@@ -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
|
-
}
|