jsir 2.6.14 → 3.0.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/cmd/oaa.js +105 -91
- package/deps/evalCode.js +4 -2
- package/deps/room.js +25 -30
- package/deps/server.js +6 -13
- package/deps/setting.js +4 -1
- package/deps/util.js +207 -297
- package/package.json +1 -1
package/cmd/oaa.js
CHANGED
|
@@ -13,7 +13,8 @@ const {
|
|
|
13
13
|
getFullPath, parseUniqueName, toUniqueName, isJsirFileName, toJsirFileName,
|
|
14
14
|
getAlias, wrapperJsirText, eia, getKeyTips, getValTips, getJsirTypeKey,
|
|
15
15
|
createDetachedProcess, interceptStdStreams,
|
|
16
|
-
draftModify, isRunningInBackground, fileJson, fileLock, processLock, cleanFileLocks, getMd5Key, terminalTitle
|
|
16
|
+
draftModify, isRunningInBackground, fileJson, fileLock, processLock, cleanFileLocks, getMd5Key, terminalTitle,
|
|
17
|
+
getFileOpenExe, formatSec, formatMb
|
|
17
18
|
} = $lib;
|
|
18
19
|
const _args = process.argv.slice(2).map(trim);
|
|
19
20
|
const evalCode = require('../deps/evalCode')
|
|
@@ -164,20 +165,6 @@ const $data = {
|
|
|
164
165
|
}
|
|
165
166
|
const $homeDir = getLibDataDir()
|
|
166
167
|
|
|
167
|
-
function getFileOpenExe(fileName) {
|
|
168
|
-
fileName = trim(fileName);
|
|
169
|
-
let strs = fileName.split('.')
|
|
170
|
-
let suffix = ''
|
|
171
|
-
if (strs.length > 1) {
|
|
172
|
-
suffix = strs[strs.length - 1]
|
|
173
|
-
}
|
|
174
|
-
let defaultExe = getConfig("defaultExe", "open");
|
|
175
|
-
if (!suffix) {
|
|
176
|
-
return defaultExe
|
|
177
|
-
}
|
|
178
|
-
return getConfig(`${suffix}_exe`, defaultExe);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
168
|
function checkWorkspaces() {
|
|
182
169
|
let localWorkspace = _libDataDir + '/local';
|
|
183
170
|
setting.workspaceMap = {
|
|
@@ -280,7 +267,7 @@ async function watchFile(uniqueName) {
|
|
|
280
267
|
closeFileWatcher(workFilePath);
|
|
281
268
|
});
|
|
282
269
|
}
|
|
283
|
-
await
|
|
270
|
+
await eia(getFileOpenExe(), [`"${workFilePath}"`], true)
|
|
284
271
|
}
|
|
285
272
|
|
|
286
273
|
function getExeStr(oldText, newText) {
|
|
@@ -332,8 +319,8 @@ function getExeStr(oldText, newText) {
|
|
|
332
319
|
}
|
|
333
320
|
|
|
334
321
|
async function nextLine(callback, promptStr, hidden) {
|
|
335
|
-
return new Promise(resolve => {
|
|
336
|
-
_nextLine(callback, promptStr, hidden, resolve)
|
|
322
|
+
return new Promise((resolve, reject) => {
|
|
323
|
+
_nextLine(callback, promptStr, hidden, resolve, reject)
|
|
337
324
|
})
|
|
338
325
|
}
|
|
339
326
|
|
|
@@ -343,14 +330,18 @@ function defaultPromptStr(showKey = false) {
|
|
|
343
330
|
return tips;
|
|
344
331
|
}
|
|
345
332
|
|
|
346
|
-
function initRl(callback, promptStr, hidden) {
|
|
333
|
+
function initRl(callback, promptStr, hidden, reject) {
|
|
347
334
|
let origin_writeToOutput;
|
|
348
335
|
if (!_rl) {
|
|
349
336
|
_rl = readline.createInterface({
|
|
350
337
|
input: process.stdin,
|
|
351
|
-
output: process.stdout
|
|
338
|
+
output: process.stdout,
|
|
339
|
+
historySize: 1000
|
|
352
340
|
})
|
|
353
341
|
_rl.on("SIGINT", async () => {
|
|
342
|
+
if (callback !== wrapperInput) {
|
|
343
|
+
reject('Terminal Interrupt');
|
|
344
|
+
}
|
|
354
345
|
if (_noAppendNextLine) {
|
|
355
346
|
setting.promptId = Date.now();
|
|
356
347
|
await delTips();
|
|
@@ -358,7 +349,9 @@ function initRl(callback, promptStr, hidden) {
|
|
|
358
349
|
_rl._writeToOutput = origin_writeToOutput;
|
|
359
350
|
_haveWrapperInput = true;
|
|
360
351
|
_rl.clearLine(0)
|
|
361
|
-
|
|
352
|
+
if (callback === wrapperInput) {
|
|
353
|
+
nextLine();
|
|
354
|
+
}
|
|
362
355
|
}
|
|
363
356
|
});
|
|
364
357
|
_rl.history = _rlHistory
|
|
@@ -408,19 +401,19 @@ function closeRl() {
|
|
|
408
401
|
_rl = null;
|
|
409
402
|
}
|
|
410
403
|
|
|
411
|
-
function _nextLine(callback, promptStr, hidden, resolve, end, isText) {
|
|
404
|
+
function _nextLine(callback, promptStr, hidden, resolve, reject, end, isText) {
|
|
412
405
|
callback = callback || wrapperInput;
|
|
413
406
|
if (!setting.enableNextLine) {
|
|
414
|
-
console.$
|
|
407
|
+
console.$warn("NextLine Disabled");
|
|
415
408
|
resolve(null);
|
|
416
409
|
return;
|
|
417
410
|
}
|
|
418
411
|
if (!_haveWrapperInput) {
|
|
419
|
-
console.$error(
|
|
412
|
+
console.$error("NextLine Blocked");
|
|
420
413
|
resolve(null);
|
|
421
414
|
return;
|
|
422
415
|
}
|
|
423
|
-
initRl(callback, promptStr, hidden);
|
|
416
|
+
initRl(callback, promptStr, hidden, reject);
|
|
424
417
|
if (callback !== wrapperInput) {
|
|
425
418
|
_haveWrapperInput = false
|
|
426
419
|
}
|
|
@@ -439,7 +432,7 @@ function _nextLine(callback, promptStr, hidden, resolve, end, isText) {
|
|
|
439
432
|
} else {
|
|
440
433
|
inputs.push(line)
|
|
441
434
|
}
|
|
442
|
-
if (trim(textLine) && (hidden ||
|
|
435
|
+
if (trim(textLine) && (hidden || isText)) {
|
|
443
436
|
_rl.history = _rl.history.slice(1)
|
|
444
437
|
}
|
|
445
438
|
if (!isText || line === end) {
|
|
@@ -473,13 +466,13 @@ function _nextLine(callback, promptStr, hidden, resolve, end, isText) {
|
|
|
473
466
|
_rl.prompt()
|
|
474
467
|
if (callback === wrapperInput) {
|
|
475
468
|
setting.promptId = Date.now();
|
|
469
|
+
setting.lastOutput = null;
|
|
476
470
|
}
|
|
477
|
-
setting.lastOutput = null;
|
|
478
471
|
}
|
|
479
472
|
|
|
480
473
|
async function nextText(callback, end, hidden) {
|
|
481
|
-
return new Promise(resolve => {
|
|
482
|
-
_nextLine(callback, '', hidden, resolve, end, true)
|
|
474
|
+
return new Promise((resolve, reject) => {
|
|
475
|
+
_nextLine(callback, '', hidden, resolve, reject, end, true)
|
|
483
476
|
})
|
|
484
477
|
}
|
|
485
478
|
|
|
@@ -755,7 +748,7 @@ async function execLibFn(fn, fnArgs) {
|
|
|
755
748
|
|
|
756
749
|
async function joinServer(uniqueName, isApi, exportLib) {
|
|
757
750
|
if (!setting.serviceReg.test(uniqueName)) {
|
|
758
|
-
|
|
751
|
+
return;
|
|
759
752
|
}
|
|
760
753
|
let asyncFnSize = 0
|
|
761
754
|
for (let key of Object.keys(exportLib)) {
|
|
@@ -785,7 +778,7 @@ async function joinServer(uniqueName, isApi, exportLib) {
|
|
|
785
778
|
|
|
786
779
|
async function offServer(uniqueName) {
|
|
787
780
|
if (!setting.serviceReg.test(uniqueName)) {
|
|
788
|
-
|
|
781
|
+
return;
|
|
789
782
|
}
|
|
790
783
|
let pair = parseUniqueName(uniqueName)
|
|
791
784
|
let service = `${pair[0]}/${trimJsirFileName(pair[1])}`;
|
|
@@ -1044,29 +1037,6 @@ function help(filterFn) {
|
|
|
1044
1037
|
}))
|
|
1045
1038
|
}
|
|
1046
1039
|
|
|
1047
|
-
function delTipsByIndex(idxs) {
|
|
1048
|
-
let keys = Object.keys(setting.tips)
|
|
1049
|
-
let indexKeyMap = {}
|
|
1050
|
-
for(let i = 1;i<=keys.length;i++) {
|
|
1051
|
-
indexKeyMap[i] = keys[i - 1]
|
|
1052
|
-
}
|
|
1053
|
-
let params = []
|
|
1054
|
-
for (const index of idxs) {
|
|
1055
|
-
if (setting.tips.hasOwnProperty(index)) {
|
|
1056
|
-
params.push(index)
|
|
1057
|
-
} else if (indexKeyMap[Number(index)]) {
|
|
1058
|
-
params.push(indexKeyMap[Number(index)])
|
|
1059
|
-
} else if (indexKeyMap[keys.length + 1 + Number(index)]) {
|
|
1060
|
-
params.push(indexKeyMap[keys.length + 1 + Number(index)])
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
if (idxs.length > 0 && params.length <= 0) {
|
|
1064
|
-
console.warn("no items")
|
|
1065
|
-
return;
|
|
1066
|
-
}
|
|
1067
|
-
return delTips(...params)
|
|
1068
|
-
}
|
|
1069
|
-
|
|
1070
1040
|
function getPackageVersion(space, name) {
|
|
1071
1041
|
let moduleDir = setting.workspaceMap[space] + '/node_modules'
|
|
1072
1042
|
let dir = moduleDir + "/" + name;
|
|
@@ -1171,13 +1141,14 @@ async function showRooms() {
|
|
|
1171
1141
|
node: room.selfNode,
|
|
1172
1142
|
active: room.active,
|
|
1173
1143
|
pid: jsir.newError ? errorStr(pidStr):pidStr,
|
|
1174
|
-
up: jsir.upTime || '',
|
|
1144
|
+
up: formatSec(jsir.upTime) || '',
|
|
1175
1145
|
version: jsir.version || '',
|
|
1176
1146
|
space: jsir.space,
|
|
1177
1147
|
running: jsir.active,
|
|
1178
1148
|
port: jsir.port,
|
|
1179
1149
|
back: jsir.back,
|
|
1180
1150
|
busy: jsir.busy,
|
|
1151
|
+
mem: formatMb(jsir.mem) || '',
|
|
1181
1152
|
tips: Object.keys(jsir.tips).map(i => infoStr(i) + ': ' + jsir.tips[i]).join('\n'),
|
|
1182
1153
|
services: Object.keys(jsir.services || []).map(msgStr).join('\n')
|
|
1183
1154
|
});
|
|
@@ -1211,7 +1182,24 @@ const keywordDef = {
|
|
|
1211
1182
|
clear: {
|
|
1212
1183
|
comment: 'Clear task',
|
|
1213
1184
|
exeFn: async (args) => {
|
|
1214
|
-
|
|
1185
|
+
if (args.length > 0 && _cmdMap[args[0]]) {
|
|
1186
|
+
let cmdName = _cmdMap[args[0]];
|
|
1187
|
+
if (args.length === 1) {
|
|
1188
|
+
$data.del(cmdName)
|
|
1189
|
+
} else {
|
|
1190
|
+
let metadata = $data.get(cmdName);
|
|
1191
|
+
if (!vl(metadata) || typeof metadata !== 'object') {
|
|
1192
|
+
return;
|
|
1193
|
+
}
|
|
1194
|
+
for (let key of args.slice(1)) {
|
|
1195
|
+
if (metadata.hasOwnProperty(key)) {
|
|
1196
|
+
delete metadata[key]
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
} else {
|
|
1201
|
+
await delTips(...args)
|
|
1202
|
+
}
|
|
1215
1203
|
},
|
|
1216
1204
|
args: {
|
|
1217
1205
|
tipIndex: 'left prompt index, can be separated by spaces for multiple'
|
|
@@ -1303,7 +1291,7 @@ const keywordDef = {
|
|
|
1303
1291
|
console.warn("no items")
|
|
1304
1292
|
} else {
|
|
1305
1293
|
let path = getFullPath(uniqueName)
|
|
1306
|
-
await eia(getEditor(), [path])
|
|
1294
|
+
await eia(getEditor(), [`"${path}"`], true)
|
|
1307
1295
|
}
|
|
1308
1296
|
},
|
|
1309
1297
|
args: {
|
|
@@ -1332,12 +1320,12 @@ const keywordDef = {
|
|
|
1332
1320
|
console.msg("config service offline")
|
|
1333
1321
|
} else if (_cmdMap[args[0]]){
|
|
1334
1322
|
let uniqueName = _cmdMap[args[0]];
|
|
1335
|
-
await eia(getEditor(), [
|
|
1323
|
+
await eia(getEditor(), [`"${getConfigDir()}/${md5(uniqueName + (global.$TEST ? '.test':''))}.json"`], true)
|
|
1336
1324
|
} else {
|
|
1337
1325
|
console.warn('invalid args')
|
|
1338
1326
|
}
|
|
1339
1327
|
} else {
|
|
1340
|
-
await eia(getEditor(), [
|
|
1328
|
+
await eia(getEditor(), [`"${getLibDataDir()}/config.json"`], true)
|
|
1341
1329
|
}
|
|
1342
1330
|
},
|
|
1343
1331
|
short: 's'
|
|
@@ -1349,18 +1337,24 @@ const keywordDef = {
|
|
|
1349
1337
|
if (!uniqueName) {
|
|
1350
1338
|
console.warn("no items")
|
|
1351
1339
|
} else {
|
|
1352
|
-
let
|
|
1353
|
-
if (
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1340
|
+
let names = [uniqueName]
|
|
1341
|
+
if (uniqueName === 'ALL') {
|
|
1342
|
+
names = Object.values(_cmdMap)
|
|
1343
|
+
}
|
|
1344
|
+
for (let uniqueName of names) {
|
|
1345
|
+
let path = getFullPath(uniqueName)
|
|
1346
|
+
if (!fs.existsSync(path)) {
|
|
1347
|
+
let cmds = filterCmd(uniqueName);
|
|
1348
|
+
if (cmds.length === 1) {
|
|
1349
|
+
uniqueName = cmds[0];
|
|
1350
|
+
path = getFullPath(cmds[0])
|
|
1351
|
+
} else {
|
|
1352
|
+
console.warn("no items")
|
|
1353
|
+
return;
|
|
1354
|
+
}
|
|
1361
1355
|
}
|
|
1356
|
+
await eia(getFileOpenExe(), [`"${path}"`], true)
|
|
1362
1357
|
}
|
|
1363
|
-
await e(`"${getFileOpenExe(parseUniqueName(uniqueName)[1])}" "${path}"`)
|
|
1364
1358
|
}
|
|
1365
1359
|
},
|
|
1366
1360
|
args: {
|
|
@@ -1377,7 +1371,7 @@ const keywordDef = {
|
|
|
1377
1371
|
let bSpace = args[1]
|
|
1378
1372
|
|
|
1379
1373
|
if (aUniqueName && bUniqueName) {
|
|
1380
|
-
let result = await
|
|
1374
|
+
let result = await eia(getConfig("compareExe", "diff"), [`"${getFullPath(aUniqueName)}"`, `"${getFullPath(bUniqueName)}"`], true)
|
|
1381
1375
|
if (result) {
|
|
1382
1376
|
console.log(result)
|
|
1383
1377
|
}
|
|
@@ -1907,26 +1901,36 @@ function getComments(i, cmdName, text, cols = [], col) {
|
|
|
1907
1901
|
let docLines = [getMd5Key(parseUniqueName(cmdName)[1]) + " - " + i]
|
|
1908
1902
|
text = trim(text)
|
|
1909
1903
|
docLines.push(...getTextComments(text))
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
})
|
|
1921
|
-
if (docLines.length > 2) {
|
|
1922
|
-
for (let line of docLines.slice(2)) {
|
|
1923
|
-
cols.push({
|
|
1924
|
-
doc: ' ' + line
|
|
1925
|
-
})
|
|
1926
|
-
}
|
|
1904
|
+
col.doc = '- ' + docLines[0]
|
|
1905
|
+
if (docLines.length > 1) {
|
|
1906
|
+
cols.push({
|
|
1907
|
+
doc: ' ' + infoStr(docLines[1])
|
|
1908
|
+
})
|
|
1909
|
+
if (docLines.length > 2) {
|
|
1910
|
+
for (let line of docLines.slice(2)) {
|
|
1911
|
+
cols.push({
|
|
1912
|
+
doc: ' ' + line
|
|
1913
|
+
})
|
|
1927
1914
|
}
|
|
1928
1915
|
}
|
|
1929
1916
|
}
|
|
1917
|
+
|
|
1918
|
+
// 处理参数
|
|
1919
|
+
let argDef = getArgDef(text, cmdName)
|
|
1920
|
+
let comments = getArgComments(argDef, true)
|
|
1921
|
+
for (let line of comments) {
|
|
1922
|
+
cols.push({
|
|
1923
|
+
doc: ' ' + line
|
|
1924
|
+
})
|
|
1925
|
+
}
|
|
1926
|
+
|
|
1927
|
+
// 处理元数据
|
|
1928
|
+
let metadata = $data.get(cmdName);
|
|
1929
|
+
if (vl(metadata) && typeof metadata === 'object' && Object.keys(metadata).length > 0) {
|
|
1930
|
+
cols.push({
|
|
1931
|
+
doc: ' ' + Object.keys(metadata).map(msgStr).join(' / ')
|
|
1932
|
+
})
|
|
1933
|
+
}
|
|
1930
1934
|
}
|
|
1931
1935
|
|
|
1932
1936
|
function trimJsirFileName(name) {
|
|
@@ -2428,17 +2432,27 @@ function _addErrorTag(lines) {
|
|
|
2428
2432
|
} else {
|
|
2429
2433
|
result.push(line);
|
|
2430
2434
|
}
|
|
2431
|
-
let isAsyncFn = line.startsWith('async function');
|
|
2432
|
-
let isSyncFn = line.startsWith('function');
|
|
2435
|
+
let isAsyncFn = line.startsWith('async function ');
|
|
2436
|
+
let isSyncFn = line.startsWith('function ');
|
|
2433
2437
|
if (isAsyncFn || isSyncFn) {
|
|
2434
|
-
fnName = reget(line, /function\s+([
|
|
2438
|
+
fnName = reget(line, /function\s+([a-zA-Z_$][a-zA-Z0-9_$.]*)\s*\(/)
|
|
2435
2439
|
fnIsAsync = isAsyncFn;
|
|
2436
2440
|
}
|
|
2437
2441
|
if (functionEnd && fnName && (/\)\s*\{$/.test(trim(line)) || (line.startsWith("{") && trim(line) === '{'))) {
|
|
2438
2442
|
result[i] += 'try{';
|
|
2439
2443
|
if (wrapperFn.length > 0) {
|
|
2440
2444
|
let defaultArgsList = '("'+ fnName +'")';
|
|
2441
|
-
let runner = `([${wrapperFn.map(i =>
|
|
2445
|
+
let runner = `([${wrapperFn.map(i => {
|
|
2446
|
+
if (i.endsWith(")")) {
|
|
2447
|
+
i = i
|
|
2448
|
+
.replace(/^([^(]+)\(\s*null\s*/, '$1("'+ fnName + '"')
|
|
2449
|
+
.replace(/^([^(]+)\(\s*undefined\s*/, '$1("'+ fnName + '"')
|
|
2450
|
+
.replace(/^([^(]+)\(\s*\)/, '$1' + defaultArgsList)
|
|
2451
|
+
} else {
|
|
2452
|
+
i += defaultArgsList;
|
|
2453
|
+
}
|
|
2454
|
+
return 'new ' + i;
|
|
2455
|
+
}).join(",")}],`
|
|
2442
2456
|
if (fnIsAsync) {
|
|
2443
2457
|
result[i] += `return await $aopAsync${runner}async ()=>{`;
|
|
2444
2458
|
} else {
|
package/deps/evalCode.js
CHANGED
|
@@ -17,10 +17,12 @@ module.exports = async ($text = '', $cmdName = '', $args = [],
|
|
|
17
17
|
// 兼容
|
|
18
18
|
$text = $lib.wrapperJsirText($text)
|
|
19
19
|
|
|
20
|
-
const $setTips = $lib.setTips;
|
|
21
|
-
const $delTips = $lib.delTips;
|
|
22
20
|
const $tips = $lib.tipFns;
|
|
23
21
|
const $errorTag = $lib.errorTag;
|
|
22
|
+
|
|
23
|
+
// 不在context中
|
|
24
|
+
const $setTips = $lib.setTips;
|
|
25
|
+
const $delTips = $lib.delTips;
|
|
24
26
|
const $aop = $lib.aop;
|
|
25
27
|
const $aopAsync = $lib.aopAsync;
|
|
26
28
|
|
package/deps/room.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
const os = require('os');
|
|
2
|
-
const {fileJson, fileLock, vl,
|
|
2
|
+
const {fileJson, fileLock, vl, getKeyTips, getValTips,
|
|
3
3
|
getRoomsDir, e, isRunningInBackground,
|
|
4
|
-
batchAsync,
|
|
5
|
-
fileExist,
|
|
4
|
+
batchAsync, trim, getOr, errorTag, warnStr, getConfig, getConfigDir,
|
|
5
|
+
fileExist, processLock, isPidAlive, cleanFileLocks,
|
|
6
|
+
roomConsole: console
|
|
6
7
|
} = require('./util');
|
|
7
8
|
const {setRoute, createSign} = require('../deps/server')
|
|
8
9
|
const roomDataFile = "jsirRoom.json"
|
|
@@ -10,7 +11,6 @@ const jsirNodesFile = "jsirNodes.json"
|
|
|
10
11
|
const roomTasksFile = "roomTasks.json"
|
|
11
12
|
const roomsDirLockKey = "RW_" + getRoomsDir();
|
|
12
13
|
const updateRoomInfoLockKey = "UPDATE_" + roomDataFile
|
|
13
|
-
const console = createConsole();
|
|
14
14
|
const setting = require('../deps/setting')
|
|
15
15
|
const fp = require('fs').promises
|
|
16
16
|
const http = require('http');
|
|
@@ -19,12 +19,6 @@ const tailscalePath = os.platform() === 'darwin' ?
|
|
|
19
19
|
const packageJson = require("../package.json");
|
|
20
20
|
const defTasks = getConfig("tasks");
|
|
21
21
|
|
|
22
|
-
function debug(...args) {
|
|
23
|
-
if (global.$DEBUG) {
|
|
24
|
-
console.$log(debugStr('[debug]'), ...args);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
22
|
async function localConfigs() {
|
|
29
23
|
let configDir = getConfigDir();
|
|
30
24
|
let configFiles = await fp.readdir(configDir)
|
|
@@ -61,7 +55,7 @@ function onRoom() {
|
|
|
61
55
|
}
|
|
62
56
|
})
|
|
63
57
|
} catch (e) {
|
|
64
|
-
debug("initRoute failed", e)
|
|
58
|
+
console.$debug("initRoute failed", e)
|
|
65
59
|
}
|
|
66
60
|
_onRoom();
|
|
67
61
|
}
|
|
@@ -72,7 +66,7 @@ function _onRoom() {
|
|
|
72
66
|
try {
|
|
73
67
|
await initRoom();
|
|
74
68
|
} catch (e) {
|
|
75
|
-
debug('initRoom', e)
|
|
69
|
+
console.$debug('initRoom', e)
|
|
76
70
|
}
|
|
77
71
|
if (setting.roomTid[0]) {
|
|
78
72
|
_onRoom();
|
|
@@ -86,7 +80,7 @@ function offRoom() {
|
|
|
86
80
|
setting.roomTid[0] = null
|
|
87
81
|
if (setting.server) {
|
|
88
82
|
setting.server.close(() => {
|
|
89
|
-
debug('Existing server shut down.');
|
|
83
|
+
console.$debug('Existing server shut down.');
|
|
90
84
|
});
|
|
91
85
|
}
|
|
92
86
|
}
|
|
@@ -117,7 +111,7 @@ async function initNodes() {
|
|
|
117
111
|
try {
|
|
118
112
|
resp = await e(`${tailscalePath} status`);
|
|
119
113
|
} catch (e) {
|
|
120
|
-
debug("initNodes", e)
|
|
114
|
+
console.$debug("initNodes", e)
|
|
121
115
|
}
|
|
122
116
|
for (let line of resp.split("\n").map(trim)) {
|
|
123
117
|
let offline = line.indexOf('offline') !== -1
|
|
@@ -145,7 +139,7 @@ async function updateRoomInfo(room) {
|
|
|
145
139
|
room.name = setting.nodeMap[ip]?.name || os.hostname();
|
|
146
140
|
room.selfNode = ip;
|
|
147
141
|
room.lastUpdateTime = setting.roomTime
|
|
148
|
-
debug("init room", room.name)
|
|
142
|
+
console.$debug("init room", room.name)
|
|
149
143
|
|
|
150
144
|
await cleanRoom(room)
|
|
151
145
|
return room;
|
|
@@ -170,7 +164,7 @@ async function syncRooms() {
|
|
|
170
164
|
try {
|
|
171
165
|
respBody = await reqNode(node, "get", "/");
|
|
172
166
|
} catch (e) {
|
|
173
|
-
debug(`sync ${node} failed:`, e);
|
|
167
|
+
console.$debug(`sync ${node} failed:`, e);
|
|
174
168
|
if (!isDisableConnect(node, setting.defaultPort)) {
|
|
175
169
|
respBody = await reqNode(node, "get", "/", setting.defaultPort);
|
|
176
170
|
}
|
|
@@ -185,10 +179,10 @@ async function syncRooms() {
|
|
|
185
179
|
);
|
|
186
180
|
}
|
|
187
181
|
syncRooms.push(currRoom)
|
|
188
|
-
debug(`sync ${node} success`);
|
|
182
|
+
console.$debug(`sync ${node} success`);
|
|
189
183
|
}
|
|
190
184
|
} catch (e) {
|
|
191
|
-
debug(`sync ${node} failed:`, respBody, e);
|
|
185
|
+
console.$debug(`sync ${node} failed:`, respBody, e);
|
|
192
186
|
}
|
|
193
187
|
})
|
|
194
188
|
}
|
|
@@ -208,11 +202,11 @@ async function syncRooms() {
|
|
|
208
202
|
try {
|
|
209
203
|
await fp.unlink(roomsDir + '/' + file)
|
|
210
204
|
} catch (e) {
|
|
211
|
-
debug(e);
|
|
205
|
+
console.$debug(e);
|
|
212
206
|
}
|
|
213
207
|
}
|
|
214
208
|
}
|
|
215
|
-
debug('syncRooms done')
|
|
209
|
+
console.$debug('syncRooms done')
|
|
216
210
|
}
|
|
217
211
|
|
|
218
212
|
async function syncSetting() {
|
|
@@ -333,10 +327,10 @@ async function initRoom() {
|
|
|
333
327
|
pros.push(syncConfigs())
|
|
334
328
|
}
|
|
335
329
|
await Promise.all(pros);
|
|
336
|
-
}, false).catch(debug)
|
|
330
|
+
}, false).catch(console.$debug)
|
|
337
331
|
}
|
|
338
332
|
if (defTasks && Object.keys(defTasks).length > 0) {
|
|
339
|
-
processLock(roomTasksFile, processTasks, false).catch(debug)
|
|
333
|
+
processLock(roomTasksFile, processTasks, false).catch(console.$debug)
|
|
340
334
|
}
|
|
341
335
|
await syncSetting();
|
|
342
336
|
}
|
|
@@ -346,7 +340,7 @@ async function syncConfigs() {
|
|
|
346
340
|
try {
|
|
347
341
|
configs = await reqFn(setting.configMainFnKey);
|
|
348
342
|
} catch (e) {
|
|
349
|
-
debug("syncConfigs failed", e)
|
|
343
|
+
console.$debug("syncConfigs failed", e)
|
|
350
344
|
return;
|
|
351
345
|
}
|
|
352
346
|
let configDir = getConfigDir();
|
|
@@ -359,16 +353,16 @@ async function syncConfigs() {
|
|
|
359
353
|
let currText = String(await fp.readFile(path));
|
|
360
354
|
if (currText !== text) {
|
|
361
355
|
await fp.writeFile(path, text)
|
|
362
|
-
debug(`sync ${key} success`);
|
|
356
|
+
console.$debug(`sync ${key} success`);
|
|
363
357
|
}
|
|
364
358
|
} else {
|
|
365
359
|
await fp.writeFile(path, text)
|
|
366
|
-
debug(`sync ${key} success`);
|
|
360
|
+
console.$debug(`sync ${key} success`);
|
|
367
361
|
}
|
|
368
362
|
});
|
|
369
363
|
}
|
|
370
364
|
await batchAsync(fns, 33)
|
|
371
|
-
debug('syncConfigs done')
|
|
365
|
+
console.$debug('syncConfigs done')
|
|
372
366
|
}
|
|
373
367
|
|
|
374
368
|
async function cleanRoom(room) {
|
|
@@ -378,10 +372,10 @@ async function cleanRoom(room) {
|
|
|
378
372
|
let active = Date.now() - (jsir.lastUpdateTime || 0) <= setting.roomHeartbeatExpire;
|
|
379
373
|
if (jsir.back && !active && isPidAlive(pid)) {
|
|
380
374
|
try {
|
|
381
|
-
debug(`kill inactive jsir ${pid}`)
|
|
375
|
+
console.$debug(`kill inactive jsir ${pid}`)
|
|
382
376
|
process.kill(parseInt(pid), 'SIGKILL'); // 强制终止进程
|
|
383
377
|
} catch (e) {
|
|
384
|
-
debug(`cleanRoom ${pid} failed`, e)
|
|
378
|
+
console.$debug(`cleanRoom ${pid} failed`, e)
|
|
385
379
|
}
|
|
386
380
|
}
|
|
387
381
|
}
|
|
@@ -438,7 +432,8 @@ async function initRoomJsir(room) {
|
|
|
438
432
|
services: services,
|
|
439
433
|
newError: setting.newError,
|
|
440
434
|
version: packageJson.version,
|
|
441
|
-
upTime:
|
|
435
|
+
upTime: process.uptime(),
|
|
436
|
+
mem: Math.floor(process.memoryUsage().rss / 1024 / 1024)
|
|
442
437
|
}
|
|
443
438
|
room.jsirs[process.pid] = setting.selfJsir
|
|
444
439
|
}
|
|
@@ -547,7 +542,7 @@ async function reqNode(node, method, url, port, body) {
|
|
|
547
542
|
req.end();
|
|
548
543
|
});
|
|
549
544
|
} finally {
|
|
550
|
-
debug(`Request cost ${Date.now() - time}ms`, JSON.stringify(opt))
|
|
545
|
+
console.$debug(`Request cost ${Date.now() - time}ms`, JSON.stringify(opt))
|
|
551
546
|
}
|
|
552
547
|
}
|
|
553
548
|
|
package/deps/server.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
const http = require('http');
|
|
2
|
-
const {
|
|
3
|
-
const console = createConsole();
|
|
2
|
+
const {vl, aesDecipher, md5, aesCipher, isError, roomConsole: console} = require('./util');
|
|
4
3
|
const setting = require('../deps/setting')
|
|
5
4
|
// 尝试监听的端口
|
|
6
5
|
const preferredPort = setting.defaultPort;
|
|
@@ -8,12 +7,6 @@ const preferredPort = setting.defaultPort;
|
|
|
8
7
|
const routes = setting.routes;
|
|
9
8
|
let invokeStart = false;
|
|
10
9
|
|
|
11
|
-
function debug(...args) {
|
|
12
|
-
if (global.$DEBUG) {
|
|
13
|
-
console.$log(debugStr('[debug]'), ...args);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
10
|
function verifySign(sign) {
|
|
18
11
|
try {
|
|
19
12
|
let key = md5(Object.keys(setting.nodeMap).sort().join(":")).substring(0, 16);
|
|
@@ -103,18 +96,18 @@ async function createServer(port) {
|
|
|
103
96
|
try {
|
|
104
97
|
await process(routeKey, req, res, params);
|
|
105
98
|
} finally {
|
|
106
|
-
debug(`Process cost ${Date.now() - time}ms for ${routeKey} from ${clientIp}`);
|
|
99
|
+
console.$debug(`Process cost ${Date.now() - time}ms for ${routeKey} from ${clientIp}`);
|
|
107
100
|
}
|
|
108
101
|
});
|
|
109
102
|
|
|
110
103
|
return await new Promise((resolve, reject) => {
|
|
111
104
|
server.listen(port, () => {
|
|
112
105
|
const address = server.address();
|
|
113
|
-
debug(`Server listening on port ${address.port}`);
|
|
106
|
+
console.$debug(`Server listening on port ${address.port}`);
|
|
114
107
|
resolve(server)
|
|
115
108
|
});
|
|
116
109
|
server.on('error', (err) => {
|
|
117
|
-
debug(`Error occurred: ${err.message}`);
|
|
110
|
+
console.$debug(`Error occurred: ${err.message}`);
|
|
118
111
|
reject(err)
|
|
119
112
|
});
|
|
120
113
|
})
|
|
@@ -127,7 +120,7 @@ async function startServer() {
|
|
|
127
120
|
try {
|
|
128
121
|
setting.server = await createServer(preferredPort);
|
|
129
122
|
} catch (e) {
|
|
130
|
-
debug("startServer failed, try other port")
|
|
123
|
+
console.$debug("startServer failed, try other port")
|
|
131
124
|
setting.server = await createServer(0);
|
|
132
125
|
}
|
|
133
126
|
}
|
|
@@ -156,7 +149,7 @@ function setRoute(method, url, fn, isApi = false, handler = jsonHandle) {
|
|
|
156
149
|
isApi
|
|
157
150
|
};
|
|
158
151
|
}
|
|
159
|
-
debug(`setRoute: ${method.toUpperCase()} ${url} ${handler ? handler.name:''}`);
|
|
152
|
+
console.$debug(`setRoute: ${method.toUpperCase()} ${url} ${handler ? handler.name:''}`);
|
|
160
153
|
return setting.server;
|
|
161
154
|
}
|
|
162
155
|
|