@gjsify/os 0.3.12 → 0.3.14
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/lib/esm/constants.js +136 -135
- package/lib/esm/createSubnet.js +13 -13
- package/lib/esm/darwin.js +132 -133
- package/lib/esm/index.js +170 -196
- package/lib/esm/linux.js +158 -152
- package/lib/esm/win32.js +3 -0
- package/package.json +6 -6
package/lib/esm/index.js
CHANGED
|
@@ -1,29 +1,35 @@
|
|
|
1
|
+
import constants_default from "./constants.js";
|
|
2
|
+
import { cpus as cpus$1, endianness as endianness$1, freemem as freemem$1, loadavg as loadavg$1, networkInterfaces as networkInterfaces$1, totalmem as totalmem$1, uptime as uptime$1 } from "./darwin.js";
|
|
3
|
+
import { cpus as cpus$2, endianness as endianness$2, freemem as freemem$2, loadavg as loadavg$2, networkInterfaces as networkInterfaces$2, totalmem as totalmem$2, uptime as uptime$2 } from "./linux.js";
|
|
1
4
|
import { cli, getPathSeparator } from "@gjsify/utils";
|
|
2
5
|
import Gio from "@girs/gio-2.0";
|
|
6
|
+
import GLib from "@girs/glib-2.0";
|
|
7
|
+
|
|
8
|
+
//#region src/index.ts
|
|
9
|
+
/** Cached OS detection result */
|
|
3
10
|
let _os = "";
|
|
11
|
+
/** Get the OS name (darwin, linux, win32) via uname */
|
|
4
12
|
const getOs = () => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
};
|
|
13
|
+
if (_os) return _os;
|
|
14
|
+
const os = cli("uname -o").trim();
|
|
15
|
+
if (/\bDarwin\b/i.test(os)) {
|
|
16
|
+
_os = "darwin";
|
|
17
|
+
return _os;
|
|
18
|
+
}
|
|
19
|
+
if (/\bLinux\b/i.test(os)) {
|
|
20
|
+
_os = "linux";
|
|
21
|
+
return _os;
|
|
22
|
+
}
|
|
23
|
+
_os = "win32";
|
|
24
|
+
return _os;
|
|
25
|
+
};
|
|
26
|
+
/** Cached PID */
|
|
18
27
|
let _pid = 0;
|
|
28
|
+
/** Get the current process ID via Gio.Credentials */
|
|
19
29
|
const getPid = () => {
|
|
20
|
-
|
|
21
|
-
|
|
30
|
+
if (!_pid) _pid = new Gio.Credentials().get_unix_pid();
|
|
31
|
+
return _pid;
|
|
22
32
|
};
|
|
23
|
-
import * as linux from "./linux.js";
|
|
24
|
-
import * as darwin from "./darwin.js";
|
|
25
|
-
import GLib from "@girs/glib-2.0";
|
|
26
|
-
import constants from "./constants.js";
|
|
27
33
|
const EOL = getPathSeparator() === "/" ? "\n" : "\r\n";
|
|
28
34
|
const devNull = getPathSeparator() === "/" ? "/dev/null" : "\\\\.\\nul";
|
|
29
35
|
const homedir = () => GLib.get_home_dir();
|
|
@@ -33,202 +39,170 @@ const tmpdir = () => GLib.get_tmp_dir();
|
|
|
33
39
|
const type = () => cli("uname").trim();
|
|
34
40
|
const platform = () => cli("uname -s").trim().toLowerCase();
|
|
35
41
|
const arch = () => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
+
const machine = cli("uname -m").trim();
|
|
43
|
+
if (machine === "x86_64" || machine === "amd64") return "x64";
|
|
44
|
+
if (machine === "aarch64" || machine === "arm64") return "arm64";
|
|
45
|
+
if (machine === "i686" || machine === "i386") return "ia32";
|
|
46
|
+
if (machine.startsWith("arm")) return "arm";
|
|
47
|
+
return machine;
|
|
42
48
|
};
|
|
43
49
|
const machine = () => cli("uname -m").trim();
|
|
44
50
|
const version = () => cli("uname -v").trim();
|
|
45
51
|
const uptime = () => {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
default:
|
|
53
|
-
return 0;
|
|
54
|
-
}
|
|
52
|
+
const _os = getOs();
|
|
53
|
+
switch (_os) {
|
|
54
|
+
case "darwin": return uptime$1();
|
|
55
|
+
case "linux": return uptime$2();
|
|
56
|
+
default: return 0;
|
|
57
|
+
}
|
|
55
58
|
};
|
|
56
59
|
const totalmem = () => {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
default:
|
|
64
|
-
return 0;
|
|
65
|
-
}
|
|
60
|
+
const _os = getOs();
|
|
61
|
+
switch (_os) {
|
|
62
|
+
case "darwin": return totalmem$1();
|
|
63
|
+
case "linux": return totalmem$2();
|
|
64
|
+
default: return 0;
|
|
65
|
+
}
|
|
66
66
|
};
|
|
67
67
|
const availableParallelism = () => {
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
const c = cpus();
|
|
69
|
+
return c ? c.length : 1;
|
|
70
70
|
};
|
|
71
71
|
const userInfo = () => {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
};
|
|
72
|
+
let uid = 1e3;
|
|
73
|
+
let gid = 100;
|
|
74
|
+
let shell = "";
|
|
75
|
+
try {
|
|
76
|
+
uid = parseInt(cli("id -u"), 10);
|
|
77
|
+
gid = parseInt(cli("id -g"), 10);
|
|
78
|
+
shell = GLib.getenv("SHELL") || "";
|
|
79
|
+
} catch {}
|
|
80
|
+
return {
|
|
81
|
+
uid,
|
|
82
|
+
gid,
|
|
83
|
+
username: GLib.get_user_name(),
|
|
84
|
+
homedir: GLib.get_home_dir(),
|
|
85
|
+
shell
|
|
86
|
+
};
|
|
88
87
|
};
|
|
89
88
|
const cpus = () => {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
break;
|
|
99
|
-
}
|
|
89
|
+
const _os = getOs();
|
|
90
|
+
switch (_os) {
|
|
91
|
+
case "darwin": return cpus$1();
|
|
92
|
+
case "linux": return cpus$2();
|
|
93
|
+
default:
|
|
94
|
+
console.warn(`${_os} is not supported!`);
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
100
97
|
};
|
|
101
98
|
const endianness = () => {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
break;
|
|
111
|
-
}
|
|
99
|
+
const _os = getOs();
|
|
100
|
+
switch (_os) {
|
|
101
|
+
case "darwin": return endianness$1();
|
|
102
|
+
case "linux": return endianness$2();
|
|
103
|
+
default:
|
|
104
|
+
console.warn(`${_os} is not supported!`);
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
112
107
|
};
|
|
113
108
|
const freemem = () => {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
break;
|
|
123
|
-
}
|
|
109
|
+
const _os = getOs();
|
|
110
|
+
switch (_os) {
|
|
111
|
+
case "darwin": return freemem$1();
|
|
112
|
+
case "linux": return freemem$2();
|
|
113
|
+
default:
|
|
114
|
+
console.warn(`${_os} is not supported!`);
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
124
117
|
};
|
|
125
118
|
const loadavg = () => {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
break;
|
|
135
|
-
}
|
|
119
|
+
const _os = getOs();
|
|
120
|
+
switch (_os) {
|
|
121
|
+
case "darwin": return loadavg$1();
|
|
122
|
+
case "linux": return loadavg$2();
|
|
123
|
+
default:
|
|
124
|
+
console.warn(`${_os} is not supported!`);
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
136
127
|
};
|
|
137
128
|
const networkInterfaces = () => {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
129
|
+
const _os = getOs();
|
|
130
|
+
switch (_os) {
|
|
131
|
+
case "darwin": return networkInterfaces$1();
|
|
132
|
+
case "linux": return networkInterfaces$2();
|
|
133
|
+
default:
|
|
134
|
+
console.warn(`${_os} is not supported!`);
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* Get process scheduling priority.
|
|
140
|
+
* Uses `ps -o ni=` to read the nice value for a given process.
|
|
141
|
+
* pid 0 (or omitted) means the current process.
|
|
142
|
+
*/
|
|
149
143
|
const getPriority = (pid) => {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
144
|
+
const targetPid = pid === undefined || pid === 0 ? getPid() : pid;
|
|
145
|
+
try {
|
|
146
|
+
const nice = cli(`ps -o ni= -p ${targetPid}`).trim();
|
|
147
|
+
const val = parseInt(nice, 10);
|
|
148
|
+
if (!isNaN(val)) return val;
|
|
149
|
+
} catch {}
|
|
150
|
+
return 0;
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* Set process scheduling priority.
|
|
154
|
+
* Uses `renice` command. Requires appropriate permissions for other processes.
|
|
155
|
+
*/
|
|
159
156
|
const setPriority = (pidOrPriority, priority) => {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
};
|
|
184
|
-
var
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
availableParallelism,
|
|
213
|
-
constants,
|
|
214
|
-
cpus,
|
|
215
|
-
index_default as default,
|
|
216
|
-
devNull,
|
|
217
|
-
endianness,
|
|
218
|
-
freemem,
|
|
219
|
-
getPriority,
|
|
220
|
-
homedir,
|
|
221
|
-
hostname,
|
|
222
|
-
loadavg,
|
|
223
|
-
machine,
|
|
224
|
-
networkInterfaces,
|
|
225
|
-
platform,
|
|
226
|
-
release,
|
|
227
|
-
setPriority,
|
|
228
|
-
tmpdir,
|
|
229
|
-
totalmem,
|
|
230
|
-
type,
|
|
231
|
-
uptime,
|
|
232
|
-
userInfo,
|
|
233
|
-
version
|
|
234
|
-
};
|
|
157
|
+
let pid;
|
|
158
|
+
let prio;
|
|
159
|
+
if (priority === undefined) {
|
|
160
|
+
prio = pidOrPriority;
|
|
161
|
+
pid = 0;
|
|
162
|
+
} else {
|
|
163
|
+
pid = pidOrPriority;
|
|
164
|
+
prio = priority;
|
|
165
|
+
}
|
|
166
|
+
if (typeof pid !== "number" || !Number.isInteger(pid)) {
|
|
167
|
+
throw new TypeError("The \"pid\" argument must be an integer");
|
|
168
|
+
}
|
|
169
|
+
if (typeof prio !== "number" || !Number.isInteger(prio) || prio < -20 || prio > 19) {
|
|
170
|
+
throw new RangeError("The \"priority\" argument must be an integer between -20 and 19");
|
|
171
|
+
}
|
|
172
|
+
try {
|
|
173
|
+
const actualPid = pid === 0 ? getPid() : pid;
|
|
174
|
+
cli(`renice -n ${prio} -p ${actualPid}`);
|
|
175
|
+
} catch (err) {
|
|
176
|
+
const error = new Error(`A system error occurred: priority could not be set`);
|
|
177
|
+
error.code = "ERR_SYSTEM_ERROR";
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
var src_default = {
|
|
182
|
+
EOL,
|
|
183
|
+
arch,
|
|
184
|
+
availableParallelism,
|
|
185
|
+
constants: constants_default,
|
|
186
|
+
cpus,
|
|
187
|
+
devNull,
|
|
188
|
+
endianness,
|
|
189
|
+
freemem,
|
|
190
|
+
getPriority,
|
|
191
|
+
homedir,
|
|
192
|
+
hostname,
|
|
193
|
+
loadavg,
|
|
194
|
+
machine,
|
|
195
|
+
networkInterfaces,
|
|
196
|
+
platform,
|
|
197
|
+
release,
|
|
198
|
+
setPriority,
|
|
199
|
+
tmpdir,
|
|
200
|
+
totalmem,
|
|
201
|
+
type,
|
|
202
|
+
uptime,
|
|
203
|
+
userInfo,
|
|
204
|
+
version
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
//#endregion
|
|
208
|
+
export { EOL, arch, availableParallelism, constants_default as constants, cpus, src_default as default, devNull, endianness, freemem, getPriority, homedir, hostname, loadavg, machine, networkInterfaces, platform, release, setPriority, tmpdir, totalmem, type, uptime, userInfo, version };
|