jsir 2.3.9 → 2.4.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/deps/evalCode.js +6 -2
- package/deps/room.js +18 -12
- package/deps/server.js +12 -6
- package/deps/util.js +13 -3
- package/package.json +1 -1
package/deps/evalCode.js
CHANGED
|
@@ -37,6 +37,10 @@ module.exports = async ($text = '', $cmdName = '', $args = [],
|
|
|
37
37
|
$homeDir, $lib, $cmdMap
|
|
38
38
|
}
|
|
39
39
|
let console = $lib.createConsole($cmdName);
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
try {
|
|
41
|
+
return await eval(`(async ()=>{${$text};
|
|
42
|
+
})()`)
|
|
43
|
+
} catch (e) {
|
|
44
|
+
console.error(e)
|
|
45
|
+
}
|
|
42
46
|
}
|
package/deps/room.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const os = require('os');
|
|
2
2
|
const {fileJson, fileLock, vl, createConsole, getKeyTips, getValTips,
|
|
3
3
|
getRoomsDir, e, regEach, isRunningInBackground,
|
|
4
|
-
batchAsync
|
|
4
|
+
batchAsync, debugStr
|
|
5
5
|
} = require('./util');
|
|
6
6
|
const {setRoute, createSign} = require('../deps/server')
|
|
7
7
|
const roomDataFile = "jsirRoom.json"
|
|
@@ -16,6 +16,12 @@ const http = require('http');
|
|
|
16
16
|
let tailscalePath = os.platform() === 'darwin' ?
|
|
17
17
|
'/Applications/Tailscale.app/Contents/MacOS/Tailscale':'tailscale';
|
|
18
18
|
|
|
19
|
+
function debug(...args) {
|
|
20
|
+
if (global.$DEBUG) {
|
|
21
|
+
console.$log(debugStr('[debug]'), ...args);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
19
25
|
function isPidAlive(pid) {
|
|
20
26
|
try {
|
|
21
27
|
process.kill(Number(pid), 0); // 信号 0 不会实际发送,但会检查进程是否存在
|
|
@@ -24,7 +30,7 @@ function isPidAlive(pid) {
|
|
|
24
30
|
if (err.code === 'ESRCH') {
|
|
25
31
|
return false; // 进程不存在
|
|
26
32
|
}
|
|
27
|
-
|
|
33
|
+
debug(`check isPidAlive ${pid} failed`, err)
|
|
28
34
|
return false;
|
|
29
35
|
}
|
|
30
36
|
}
|
|
@@ -35,7 +41,7 @@ function onRoom() {
|
|
|
35
41
|
setRoute("post", "/", (req, res) => setting.selfRoom)
|
|
36
42
|
setRoute("get", "/", (req, res) => setting.selfRoom)
|
|
37
43
|
} catch (e) {
|
|
38
|
-
|
|
44
|
+
debug("initRoute failed", e)
|
|
39
45
|
}
|
|
40
46
|
_onRoom();
|
|
41
47
|
}
|
|
@@ -46,7 +52,7 @@ function _onRoom() {
|
|
|
46
52
|
try {
|
|
47
53
|
await initRoom();
|
|
48
54
|
} catch (e) {
|
|
49
|
-
|
|
55
|
+
debug('initRoom', e)
|
|
50
56
|
}
|
|
51
57
|
if (setting.roomTid[0]) {
|
|
52
58
|
_onRoom();
|
|
@@ -60,7 +66,7 @@ function offRoom() {
|
|
|
60
66
|
setting.roomTid[0] = null
|
|
61
67
|
if (setting.server) {
|
|
62
68
|
setting.server.close(() => {
|
|
63
|
-
|
|
69
|
+
debug('Existing server shut down.');
|
|
64
70
|
});
|
|
65
71
|
}
|
|
66
72
|
}
|
|
@@ -89,12 +95,12 @@ async function enrichRoomInfo() {
|
|
|
89
95
|
let name = os.hostname();
|
|
90
96
|
if (!vl(room.name) || room.name !== name) {
|
|
91
97
|
room.name = name;
|
|
92
|
-
|
|
98
|
+
debug("set roomName", name)
|
|
93
99
|
}
|
|
94
100
|
room.selfNode = ip;
|
|
95
101
|
room.nodes = nodes;
|
|
96
102
|
room.lastUpdateTime = Date.now()
|
|
97
|
-
|
|
103
|
+
debug("init room", room.name)
|
|
98
104
|
|
|
99
105
|
// 提前设置一下
|
|
100
106
|
setting.selfRoom = Object.assign(setting.selfRoom || {}, room);
|
|
@@ -116,9 +122,9 @@ async function syncRooms() {
|
|
|
116
122
|
try {
|
|
117
123
|
respBody = await reqNode(node, "get", "/");
|
|
118
124
|
syncRooms.push(JSON.parse(respBody))
|
|
119
|
-
|
|
125
|
+
debug(`sync ${node} success`);
|
|
120
126
|
} catch (e) {
|
|
121
|
-
|
|
127
|
+
debug(`sync ${node} failed:`, respBody, e);
|
|
122
128
|
}
|
|
123
129
|
})
|
|
124
130
|
}
|
|
@@ -139,11 +145,11 @@ async function syncRooms() {
|
|
|
139
145
|
try {
|
|
140
146
|
await fp.unlink(roomsDir + '/' + file)
|
|
141
147
|
} catch (e) {
|
|
142
|
-
|
|
148
|
+
debug(e);
|
|
143
149
|
}
|
|
144
150
|
}
|
|
145
151
|
}
|
|
146
|
-
|
|
152
|
+
debug('syncRooms done')
|
|
147
153
|
}
|
|
148
154
|
|
|
149
155
|
async function syncSetting() {
|
|
@@ -324,7 +330,7 @@ async function reqNode(node, method, url) {
|
|
|
324
330
|
'sign': createSign()
|
|
325
331
|
}
|
|
326
332
|
};
|
|
327
|
-
|
|
333
|
+
debug('reqRoom', JSON.stringify(opt))
|
|
328
334
|
return await new Promise((resolve, reject) => {
|
|
329
335
|
const req = http.request(opt, (res) => {
|
|
330
336
|
let data = '';
|
package/deps/server.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const http = require('http');
|
|
2
|
-
const {createConsole, vl, aesDecipher, md5, aesCipher} = require('./util');
|
|
2
|
+
const {createConsole, vl, aesDecipher, md5, aesCipher, debugStr} = require('./util');
|
|
3
3
|
const console = createConsole();
|
|
4
4
|
const setting = require('../deps/setting')
|
|
5
5
|
// 尝试监听的端口
|
|
@@ -8,6 +8,12 @@ const preferredPort = 52108;
|
|
|
8
8
|
const routes = {};
|
|
9
9
|
let invokeStart = false;
|
|
10
10
|
|
|
11
|
+
function debug(...args) {
|
|
12
|
+
if (global.$DEBUG) {
|
|
13
|
+
console.$log(debugStr('[debug]'), ...args);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
11
17
|
function verifySign(sign) {
|
|
12
18
|
try {
|
|
13
19
|
let key = md5([...setting.selfRoom.nodes].sort().join(":")).substring(0, 16);
|
|
@@ -65,11 +71,11 @@ async function createServer(port) {
|
|
|
65
71
|
return await new Promise((resolve, reject) => {
|
|
66
72
|
server.listen(port, () => {
|
|
67
73
|
const address = server.address();
|
|
68
|
-
|
|
74
|
+
debug(`Server listening on port ${address.port}`);
|
|
69
75
|
resolve(server)
|
|
70
76
|
});
|
|
71
77
|
server.on('error', (err) => {
|
|
72
|
-
|
|
78
|
+
debug(`Error occurred: ${err.message}`);
|
|
73
79
|
reject(err)
|
|
74
80
|
});
|
|
75
81
|
})
|
|
@@ -82,7 +88,7 @@ async function startServer() {
|
|
|
82
88
|
try {
|
|
83
89
|
setting.server = await createServer(preferredPort);
|
|
84
90
|
} catch (e) {
|
|
85
|
-
|
|
91
|
+
debug("startServer failed, try other port")
|
|
86
92
|
setting.server = await createServer(0);
|
|
87
93
|
}
|
|
88
94
|
}
|
|
@@ -100,14 +106,14 @@ function setRoute(method, url, fn, handler = jsonHandle) {
|
|
|
100
106
|
} else {
|
|
101
107
|
routes[routeKey] = fn;
|
|
102
108
|
}
|
|
103
|
-
|
|
109
|
+
debug(`AddRoute: ${method.toUpperCase()} ${url} ${handler ? handler.constructor.name:''}`);
|
|
104
110
|
return setting.server;
|
|
105
111
|
}
|
|
106
112
|
|
|
107
113
|
function delRoute(method, url) {
|
|
108
114
|
const routeKey = `${method.toLowerCase()} ${url}`;
|
|
109
115
|
delete routes[routeKey];
|
|
110
|
-
|
|
116
|
+
debug(`DelRoute: ${method.toUpperCase()} ${url}`);
|
|
111
117
|
}
|
|
112
118
|
|
|
113
119
|
function jsonHandle(req, res, fn) {
|
package/deps/util.js
CHANGED
|
@@ -610,7 +610,9 @@ function randomInt(min,max){
|
|
|
610
610
|
}
|
|
611
611
|
|
|
612
612
|
async function timeLimit(proms, mills) {
|
|
613
|
-
let
|
|
613
|
+
let tIds = []
|
|
614
|
+
let result = await Promise.race([Promise.all(proms), sleep(mills, tIds)])
|
|
615
|
+
clearTimeout(tIds[0])
|
|
614
616
|
if (!result) {
|
|
615
617
|
throw new Error(`timeLimit: exceed ${mills} ms`)
|
|
616
618
|
}
|
|
@@ -1246,6 +1248,9 @@ function setTips(key, value, onRm) {
|
|
|
1246
1248
|
function delTips(...keys) {
|
|
1247
1249
|
for (let key of Object.keys(setting.tips)) {
|
|
1248
1250
|
if (keys.length === 0) {
|
|
1251
|
+
if (key === 'DEBUG') {
|
|
1252
|
+
continue
|
|
1253
|
+
}
|
|
1249
1254
|
delete setting.tips[key]
|
|
1250
1255
|
tipsOnRmCallback(key)
|
|
1251
1256
|
} else if (keys.indexOf(key) !== -1) {
|
|
@@ -1255,6 +1260,9 @@ function delTips(...keys) {
|
|
|
1255
1260
|
}
|
|
1256
1261
|
if (keys.length === 0) {
|
|
1257
1262
|
for (let key of Object.keys(_tipsOnRm)) {
|
|
1263
|
+
if (key === 'DEBUG') {
|
|
1264
|
+
continue
|
|
1265
|
+
}
|
|
1258
1266
|
tipsOnRmCallback(key)
|
|
1259
1267
|
}
|
|
1260
1268
|
}
|
|
@@ -1550,8 +1558,10 @@ async function setCbText(str) {
|
|
|
1550
1558
|
fs.unlinkSync(copyFile)
|
|
1551
1559
|
}
|
|
1552
1560
|
|
|
1553
|
-
async function sleep(milliseconds) {
|
|
1554
|
-
return new Promise(resolve =>
|
|
1561
|
+
async function sleep(milliseconds, tIds = []) {
|
|
1562
|
+
return new Promise(resolve => {
|
|
1563
|
+
tIds.push(setTimeout(resolve, milliseconds))
|
|
1564
|
+
});
|
|
1555
1565
|
}
|
|
1556
1566
|
|
|
1557
1567
|
function splitArray(items, size) {
|