mihomo-cli 1.2.2 → 1.2.4

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/src/utils.js ADDED
@@ -0,0 +1,101 @@
1
+ const { execSync } = require('child_process');
2
+
3
+ const _sleepBuf = new Int32Array(1);
4
+
5
+ function sleepSync(ms) {
6
+ Atomics.wait(_sleepBuf, 0, 0, ms);
7
+ }
8
+
9
+ function formatBytes(bytes) {
10
+ if (bytes === undefined || bytes === null) return '未知';
11
+ const num = Number(bytes);
12
+ if (isNaN(num) || num < 0) return '未知';
13
+ if (num === 0) return '0 B';
14
+ const k = 1024;
15
+ const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
16
+ const i = Math.floor(Math.log(num) / Math.log(k));
17
+ return parseFloat((num / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
18
+ }
19
+
20
+ function formatTimestamp(ts) {
21
+ if (ts === undefined || ts === null) return '未知';
22
+ try {
23
+ return new Date(ts * 1000).toLocaleString('zh-CN');
24
+ } catch {
25
+ return '未知';
26
+ }
27
+ }
28
+
29
+ function formatDate(dateOrIso) {
30
+ if (dateOrIso === undefined || dateOrIso === null) return '未知';
31
+ try {
32
+ const d = dateOrIso instanceof Date ? dateOrIso : new Date(dateOrIso);
33
+ if (isNaN(d.getTime())) return '未知';
34
+ return d.toLocaleString('zh-CN');
35
+ } catch {
36
+ return '未知';
37
+ }
38
+ }
39
+
40
+ function hasFlag(args, short, long) {
41
+ return args && (args.includes(short) || args.includes(long));
42
+ }
43
+
44
+ function parseIntArg(args, short, long, defaultValue) {
45
+ if (!args) return defaultValue;
46
+ for (let i = 0; i < args.length; i++) {
47
+ if (args[i] === short || args[i] === long) {
48
+ if (i + 1 < args.length) {
49
+ const val = parseInt(args[i + 1]);
50
+ return isNaN(val) ? defaultValue : val;
51
+ }
52
+ }
53
+ }
54
+ return defaultValue;
55
+ }
56
+
57
+ function getNonFlagArg(args, startIdx) {
58
+ if (!args) return null;
59
+ for (let i = startIdx; i < args.length; i++) {
60
+ if (!args[i].startsWith('-')) {
61
+ return args[i];
62
+ }
63
+ }
64
+ return null;
65
+ }
66
+
67
+ function isProcessRunning(pid) {
68
+ if (!pid) return false;
69
+ try {
70
+ const output = execSync('ps -p ' + pid + ' -o pid= 2>/dev/null || true', {
71
+ encoding: 'utf8',
72
+ }).trim();
73
+ return output.length > 0;
74
+ } catch (e) {
75
+ return false;
76
+ }
77
+ }
78
+
79
+ function isProcessRoot(pid) {
80
+ if (!pid) return false;
81
+ try {
82
+ const uidOutput = execSync('ps -p ' + pid + ' -o uid= 2>/dev/null || true', {
83
+ encoding: 'utf8',
84
+ }).trim();
85
+ return uidOutput === '0';
86
+ } catch (e) {
87
+ return false;
88
+ }
89
+ }
90
+
91
+ module.exports = {
92
+ sleepSync,
93
+ formatBytes,
94
+ formatTimestamp,
95
+ formatDate,
96
+ hasFlag,
97
+ parseIntArg,
98
+ getNonFlagArg,
99
+ isProcessRunning,
100
+ isProcessRoot,
101
+ };