@darthcav/ts-utils 0.12.3 → 1.0.1
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 +162 -84
- package/dist/core/guards.d.ts +176 -0
- package/dist/core/guards.d.ts.map +1 -0
- package/dist/core/guards.js +201 -0
- package/dist/core/guards.js.map +1 -0
- package/dist/core/index.d.ts +17 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +17 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/millisecondsToString.d.ts.map +1 -0
- package/dist/{millisecondsToString.js → core/millisecondsToString.js} +9 -14
- package/dist/core/millisecondsToString.js.map +1 -0
- package/dist/core/noop.d.ts +3 -0
- package/dist/core/noop.d.ts.map +1 -0
- package/dist/core/noop.js +3 -0
- package/dist/core/noop.js.map +1 -0
- package/dist/index.d.ts +13 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -10
- package/dist/index.js.map +1 -1
- package/dist/loggers/getConsoleLogger.d.ts +11 -12
- package/dist/loggers/getConsoleLogger.d.ts.map +1 -1
- package/dist/loggers/getConsoleLogger.js +14 -19
- package/dist/loggers/getConsoleLogger.js.map +1 -1
- package/dist/loggers/index.d.ts +14 -0
- package/dist/loggers/index.d.ts.map +1 -0
- package/dist/loggers/index.js +14 -0
- package/dist/loggers/index.js.map +1 -0
- package/dist/node/delays.d.ts +11 -0
- package/dist/node/delays.d.ts.map +1 -0
- package/dist/node/delays.js +14 -0
- package/dist/node/delays.js.map +1 -0
- package/dist/node/index.d.ts +15 -0
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.js +15 -0
- package/dist/node/index.js.map +1 -0
- package/dist/node/main.d.ts +158 -0
- package/dist/node/main.d.ts.map +1 -0
- package/dist/node/main.js +255 -0
- package/dist/node/main.js.map +1 -0
- package/dist/{monitorMemory.d.ts → node/monitorMemory.d.ts} +14 -5
- package/dist/node/monitorMemory.d.ts.map +1 -0
- package/dist/{monitorMemory.js → node/monitorMemory.js} +22 -16
- package/dist/node/monitorMemory.js.map +1 -0
- package/dist/node/osRelease.d.ts +58 -0
- package/dist/node/osRelease.d.ts.map +1 -0
- package/dist/node/osRelease.js +111 -0
- package/dist/node/osRelease.js.map +1 -0
- package/dist/node/releaseParsers.d.ts +11 -0
- package/dist/node/releaseParsers.d.ts.map +1 -0
- package/dist/node/releaseParsers.js +92 -0
- package/dist/node/releaseParsers.js.map +1 -0
- package/dist/type-testing/index.d.ts +18 -0
- package/dist/type-testing/index.d.ts.map +1 -0
- package/dist/type-testing/index.js +12 -0
- package/dist/type-testing/index.js.map +1 -0
- package/package.json +22 -7
- package/src/core/guards.ts +239 -0
- package/src/core/index.ts +32 -0
- package/src/{millisecondsToString.ts → core/millisecondsToString.ts} +9 -16
- package/src/core/noop.ts +2 -0
- package/src/index.ts +13 -49
- package/src/loggers/getConsoleLogger.ts +14 -19
- package/src/loggers/index.ts +14 -0
- package/src/node/delays.ts +15 -0
- package/src/node/index.ts +21 -0
- package/src/node/main.ts +419 -0
- package/src/{monitorMemory.ts → node/monitorMemory.ts} +24 -18
- package/src/node/osRelease.ts +152 -0
- package/src/node/releaseParsers.ts +109 -0
- package/src/type-testing/index.ts +18 -0
- package/dist/main.d.ts +0 -112
- package/dist/main.d.ts.map +0 -1
- package/dist/main.js +0 -219
- package/dist/main.js.map +0 -1
- package/dist/millisecondsToString.d.ts.map +0 -1
- package/dist/millisecondsToString.js.map +0 -1
- package/dist/monitorMemory.d.ts.map +0 -1
- package/dist/monitorMemory.js.map +0 -1
- package/dist/noop.d.ts +0 -7
- package/dist/noop.d.ts.map +0 -1
- package/dist/noop.js +0 -7
- package/dist/noop.js.map +0 -1
- package/dist/osRelease.d.ts +0 -46
- package/dist/osRelease.d.ts.map +0 -1
- package/dist/osRelease.js +0 -153
- package/dist/osRelease.js.map +0 -1
- package/dist/types.d.ts +0 -70
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -84
- package/dist/types.js.map +0 -1
- package/src/main.ts +0 -302
- package/src/noop.ts +0 -6
- package/src/osRelease.ts +0 -174
- package/src/types.ts +0 -98
- /package/dist/{millisecondsToString.d.ts → core/millisecondsToString.d.ts} +0 -0
package/src/osRelease.ts
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
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, the keys read from
|
|
9
|
-
* the os-release file (`/etc/os-release`, or `/usr/lib/os-release` as a
|
|
10
|
-
* fallback; e.g. `ID`, `PRETTY_NAME`, `ID_LIKE`) are also accessible by string
|
|
11
|
-
* index.
|
|
12
|
-
*/
|
|
13
|
-
export type OsRelease = {
|
|
14
|
-
/** Human-readable OS name, e.g. `"Ubuntu"` or `"Windows 11"`. */
|
|
15
|
-
name: string
|
|
16
|
-
/** OS version string, e.g. `"24.04"` or `"10.0.22000"`. */
|
|
17
|
-
version: string
|
|
18
|
-
/** Processor architecture as returned by `os.arch()`, e.g. `"x64"`. */
|
|
19
|
-
arch: string
|
|
20
|
-
} & RuntimeObject
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Candidate os-release files, in lookup order. Per the freedesktop.org spec,
|
|
24
|
-
* `/etc/os-release` takes precedence and `/usr/lib/os-release` is the fallback.
|
|
25
|
-
*/
|
|
26
|
-
const OS_RELEASE_FILES = ["/etc/os-release", "/usr/lib/os-release"] as const
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Valid os-release keys: shell-compatible, upper-case variable names. This
|
|
30
|
-
* also rules out keys such as `__proto__` or `constructor`.
|
|
31
|
-
*/
|
|
32
|
-
const OS_RELEASE_KEY = /^[A-Z][A-Z0-9_]*$/
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Reads the first readable os-release file, or returns `null` when none can be
|
|
36
|
-
* read (missing, unreadable, or not a regular file).
|
|
37
|
-
*/
|
|
38
|
-
function readOsReleaseFile(): string | null {
|
|
39
|
-
for (const file of OS_RELEASE_FILES) {
|
|
40
|
-
try {
|
|
41
|
-
return readFileSync(file, "utf-8")
|
|
42
|
-
} catch {
|
|
43
|
-
// Try the next candidate.
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return null
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Removes one level of shell-style quoting from an os-release value. Double
|
|
51
|
-
* quotes allow the `\\`, `\"`, `` \` `` and `\$` escapes; single quotes are
|
|
52
|
-
* literal.
|
|
53
|
-
*/
|
|
54
|
-
function unquote(value: string): string {
|
|
55
|
-
if (value.length >= 2) {
|
|
56
|
-
const quote = value[0]
|
|
57
|
-
if (quote === '"' && value.endsWith('"')) {
|
|
58
|
-
return value.slice(1, -1).replace(/\\([\\"`$])/g, "$1")
|
|
59
|
-
}
|
|
60
|
-
if (quote === "'" && value.endsWith("'")) {
|
|
61
|
-
return value.slice(1, -1)
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return value
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Parses os-release content into a null-prototype record. Blank lines,
|
|
69
|
-
* comments, lines without `=`, and keys that are not valid variable names are
|
|
70
|
-
* skipped.
|
|
71
|
-
*/
|
|
72
|
-
function parseOsRelease(content: string): Record<string, string> {
|
|
73
|
-
const raw: Record<string, string> = Object.create(null)
|
|
74
|
-
for (const line of content.split("\n")) {
|
|
75
|
-
const trimmed = line.trim()
|
|
76
|
-
if (trimmed === "" || trimmed.startsWith("#")) {
|
|
77
|
-
continue
|
|
78
|
-
}
|
|
79
|
-
const eq = trimmed.indexOf("=")
|
|
80
|
-
if (eq === -1) {
|
|
81
|
-
continue
|
|
82
|
-
}
|
|
83
|
-
const key = trimmed.substring(0, eq).trim()
|
|
84
|
-
if (!OS_RELEASE_KEY.test(key)) {
|
|
85
|
-
continue
|
|
86
|
-
}
|
|
87
|
-
raw[key] = unquote(trimmed.substring(eq + 1).trim())
|
|
88
|
-
}
|
|
89
|
-
return raw
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Returns OS release information for the current platform, or `null` on
|
|
94
|
-
* unsupported platforms or when no os-release file can be read on Linux.
|
|
95
|
-
*
|
|
96
|
-
* On Linux, the information is read from `/etc/os-release`, falling back to
|
|
97
|
-
* `/usr/lib/os-release`. `name`, `version`, and `arch` are normalized from
|
|
98
|
-
* `NAME`/`PRETTY_NAME`, `VERSION_ID`/`VERSION`, and `os.arch()` respectively.
|
|
99
|
-
* All raw keys from the file are also present on the returned object. Comment
|
|
100
|
-
* lines are ignored, single- and double-quoted values are unquoted (with
|
|
101
|
-
* backslash escapes inside double quotes), and keys that are not upper-case
|
|
102
|
-
* shell variable names are skipped.
|
|
103
|
-
*
|
|
104
|
-
* On Windows, distinguishes Windows 11 from Windows 10 by NT build number
|
|
105
|
-
* (>= 22000 → Windows 11).
|
|
106
|
-
*
|
|
107
|
-
* @returns An {@link OsRelease} object on supported platforms, or `null` when
|
|
108
|
-
* the platform is unsupported or no os-release file can be read on Linux.
|
|
109
|
-
*
|
|
110
|
-
* @example
|
|
111
|
-
* ```ts
|
|
112
|
-
* import { osRelease } from "@darthcav/ts-utils"
|
|
113
|
-
*
|
|
114
|
-
* const info = osRelease()
|
|
115
|
-
* if (info) console.log(info.name) // e.g. "Ubuntu" or "Windows 11"
|
|
116
|
-
* if (info) console.log(info.PRETTY_NAME) // e.g. "Ubuntu 24.04 LTS" (Linux only)
|
|
117
|
-
* ```
|
|
118
|
-
*/
|
|
119
|
-
export function osRelease(): OsRelease | null {
|
|
120
|
-
const p = platform()
|
|
121
|
-
if (p === "linux") {
|
|
122
|
-
const content = readOsReleaseFile()
|
|
123
|
-
if (content === null) {
|
|
124
|
-
return null
|
|
125
|
-
}
|
|
126
|
-
const raw = parseOsRelease(content)
|
|
127
|
-
return {
|
|
128
|
-
...raw,
|
|
129
|
-
name: raw["NAME"] ?? raw["PRETTY_NAME"] ?? "",
|
|
130
|
-
version: raw["VERSION_ID"] ?? raw["VERSION"] ?? "",
|
|
131
|
-
arch: arch(),
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
if (p === "win32") {
|
|
135
|
-
const version = kernelRelease()
|
|
136
|
-
const [major, , build] = version.split(".").map(Number)
|
|
137
|
-
let name = "Windows "
|
|
138
|
-
if (major === 10 && (build ?? 0) >= 22000) {
|
|
139
|
-
name += "11"
|
|
140
|
-
} else {
|
|
141
|
-
switch (version.substring(0, 3)) {
|
|
142
|
-
case "10.":
|
|
143
|
-
name += "10"
|
|
144
|
-
break
|
|
145
|
-
case "6.3":
|
|
146
|
-
name += "8.1"
|
|
147
|
-
break
|
|
148
|
-
case "6.2":
|
|
149
|
-
name += "8"
|
|
150
|
-
break
|
|
151
|
-
case "6.1":
|
|
152
|
-
name += "7"
|
|
153
|
-
break
|
|
154
|
-
case "6.0":
|
|
155
|
-
name += "Vista"
|
|
156
|
-
break
|
|
157
|
-
case "5.2":
|
|
158
|
-
name += "XP 64-Bit Edition"
|
|
159
|
-
break
|
|
160
|
-
case "5.1":
|
|
161
|
-
name += "XP"
|
|
162
|
-
break
|
|
163
|
-
case "5.0":
|
|
164
|
-
name += "2000"
|
|
165
|
-
break
|
|
166
|
-
default:
|
|
167
|
-
name += "unknown version"
|
|
168
|
-
break
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
return { name, version, arch: arch() }
|
|
172
|
-
}
|
|
173
|
-
return null
|
|
174
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A generic runtime object with property names and values that are not known at
|
|
3
|
-
* compile time.
|
|
4
|
-
*
|
|
5
|
-
* Uses `unknown` instead of `any` so callers must narrow values before using
|
|
6
|
-
* them in a type-safe way.
|
|
7
|
-
*/
|
|
8
|
-
export type RuntimeObject = Record<string, unknown>
|
|
9
|
-
|
|
10
|
-
/**
|
|
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).
|
|
58
|
-
*
|
|
59
|
-
* @param value - Value to narrow.
|
|
60
|
-
* @returns The same value as a {@link RuntimeObject}, or `undefined` when the
|
|
61
|
-
* value is not a plain object.
|
|
62
|
-
*/
|
|
63
|
-
export function asRuntimeObject(value: unknown): RuntimeObject | 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"
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Returns the input when it is a string.
|
|
79
|
-
*
|
|
80
|
-
* @param value - Value to narrow.
|
|
81
|
-
* @returns The same value as a string, or `undefined` when the value is not a string.
|
|
82
|
-
*/
|
|
83
|
-
export function asString(value: unknown): string | undefined {
|
|
84
|
-
return isString(value) ? value : undefined
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Filters an unknown value down to an array of runtime JSON objects.
|
|
89
|
-
*
|
|
90
|
-
* Elements are kept when {@link isRuntimeObject} accepts them, i.e. only plain
|
|
91
|
-
* objects; the same caveats about unsanitized keys apply.
|
|
92
|
-
*
|
|
93
|
-
* @param value - Value that may contain runtime objects.
|
|
94
|
-
* @returns Only the elements that can be treated as {@link RuntimeObject} values.
|
|
95
|
-
*/
|
|
96
|
-
export function toRuntimeObjectArray(value: unknown): RuntimeObject[] {
|
|
97
|
-
return Array.isArray(value) ? value.filter(isRuntimeObject) : []
|
|
98
|
-
}
|
|
File without changes
|