jsir 2.6.13 → 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 +117 -104
- package/deps/evalCode.js +4 -2
- package/deps/room.js +32 -32
- 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 = {
|
|
@@ -198,6 +185,7 @@ function checkWorkspaces() {
|
|
|
198
185
|
}
|
|
199
186
|
|
|
200
187
|
async function start() {
|
|
188
|
+
terminalTitle()
|
|
201
189
|
cleanFileLocks();
|
|
202
190
|
|
|
203
191
|
setting.wrapperInput = wrapperInput;
|
|
@@ -279,7 +267,7 @@ async function watchFile(uniqueName) {
|
|
|
279
267
|
closeFileWatcher(workFilePath);
|
|
280
268
|
});
|
|
281
269
|
}
|
|
282
|
-
await
|
|
270
|
+
await eia(getFileOpenExe(), [`"${workFilePath}"`], true)
|
|
283
271
|
}
|
|
284
272
|
|
|
285
273
|
function getExeStr(oldText, newText) {
|
|
@@ -331,8 +319,8 @@ function getExeStr(oldText, newText) {
|
|
|
331
319
|
}
|
|
332
320
|
|
|
333
321
|
async function nextLine(callback, promptStr, hidden) {
|
|
334
|
-
return new Promise(resolve => {
|
|
335
|
-
_nextLine(callback, promptStr, hidden, resolve)
|
|
322
|
+
return new Promise((resolve, reject) => {
|
|
323
|
+
_nextLine(callback, promptStr, hidden, resolve, reject)
|
|
336
324
|
})
|
|
337
325
|
}
|
|
338
326
|
|
|
@@ -342,14 +330,18 @@ function defaultPromptStr(showKey = false) {
|
|
|
342
330
|
return tips;
|
|
343
331
|
}
|
|
344
332
|
|
|
345
|
-
function initRl(callback, promptStr, hidden) {
|
|
333
|
+
function initRl(callback, promptStr, hidden, reject) {
|
|
346
334
|
let origin_writeToOutput;
|
|
347
335
|
if (!_rl) {
|
|
348
336
|
_rl = readline.createInterface({
|
|
349
337
|
input: process.stdin,
|
|
350
|
-
output: process.stdout
|
|
338
|
+
output: process.stdout,
|
|
339
|
+
historySize: 1000
|
|
351
340
|
})
|
|
352
341
|
_rl.on("SIGINT", async () => {
|
|
342
|
+
if (callback !== wrapperInput) {
|
|
343
|
+
reject('Terminal Interrupt');
|
|
344
|
+
}
|
|
353
345
|
if (_noAppendNextLine) {
|
|
354
346
|
setting.promptId = Date.now();
|
|
355
347
|
await delTips();
|
|
@@ -357,7 +349,9 @@ function initRl(callback, promptStr, hidden) {
|
|
|
357
349
|
_rl._writeToOutput = origin_writeToOutput;
|
|
358
350
|
_haveWrapperInput = true;
|
|
359
351
|
_rl.clearLine(0)
|
|
360
|
-
|
|
352
|
+
if (callback === wrapperInput) {
|
|
353
|
+
nextLine();
|
|
354
|
+
}
|
|
361
355
|
}
|
|
362
356
|
});
|
|
363
357
|
_rl.history = _rlHistory
|
|
@@ -407,22 +401,20 @@ function closeRl() {
|
|
|
407
401
|
_rl = null;
|
|
408
402
|
}
|
|
409
403
|
|
|
410
|
-
function _nextLine(callback, promptStr, hidden, resolve, end, isText) {
|
|
404
|
+
function _nextLine(callback, promptStr, hidden, resolve, reject, end, isText) {
|
|
411
405
|
callback = callback || wrapperInput;
|
|
412
406
|
if (!setting.enableNextLine) {
|
|
413
|
-
console.$
|
|
407
|
+
console.$warn("NextLine Disabled");
|
|
414
408
|
resolve(null);
|
|
415
409
|
return;
|
|
416
410
|
}
|
|
417
411
|
if (!_haveWrapperInput) {
|
|
418
|
-
console.$error(
|
|
412
|
+
console.$error("NextLine Blocked");
|
|
419
413
|
resolve(null);
|
|
420
414
|
return;
|
|
421
415
|
}
|
|
422
|
-
initRl(callback, promptStr, hidden);
|
|
423
|
-
if (callback
|
|
424
|
-
terminalTitle()
|
|
425
|
-
} else {
|
|
416
|
+
initRl(callback, promptStr, hidden, reject);
|
|
417
|
+
if (callback !== wrapperInput) {
|
|
426
418
|
_haveWrapperInput = false
|
|
427
419
|
}
|
|
428
420
|
end = trim(end)
|
|
@@ -440,7 +432,7 @@ function _nextLine(callback, promptStr, hidden, resolve, end, isText) {
|
|
|
440
432
|
} else {
|
|
441
433
|
inputs.push(line)
|
|
442
434
|
}
|
|
443
|
-
if (trim(textLine) && (hidden ||
|
|
435
|
+
if (trim(textLine) && (hidden || isText)) {
|
|
444
436
|
_rl.history = _rl.history.slice(1)
|
|
445
437
|
}
|
|
446
438
|
if (!isText || line === end) {
|
|
@@ -474,18 +466,22 @@ function _nextLine(callback, promptStr, hidden, resolve, end, isText) {
|
|
|
474
466
|
_rl.prompt()
|
|
475
467
|
if (callback === wrapperInput) {
|
|
476
468
|
setting.promptId = Date.now();
|
|
469
|
+
setting.lastOutput = null;
|
|
477
470
|
}
|
|
478
|
-
setting.lastOutput = null;
|
|
479
471
|
}
|
|
480
472
|
|
|
481
473
|
async function nextText(callback, end, hidden) {
|
|
482
|
-
return new Promise(resolve => {
|
|
483
|
-
_nextLine(callback, '', hidden, resolve, end, true)
|
|
474
|
+
return new Promise((resolve, reject) => {
|
|
475
|
+
_nextLine(callback, '', hidden, resolve, reject, end, true)
|
|
484
476
|
})
|
|
485
477
|
}
|
|
486
478
|
|
|
487
479
|
async function save(args) {
|
|
488
|
-
let
|
|
480
|
+
let pair = parseUniqueName(args.join(' '));
|
|
481
|
+
if (pair[0] && !setting.workspaceMap[pair[0]]) {
|
|
482
|
+
throw 'invalid name'
|
|
483
|
+
}
|
|
484
|
+
let uniqueName = toUniqueName(toJsirFileName(pair[1]), pair[0])
|
|
489
485
|
let path = getFullPath(uniqueName)
|
|
490
486
|
if (fs.existsSync(path)) {
|
|
491
487
|
console.warn(`${path} already exist`)
|
|
@@ -752,7 +748,7 @@ async function execLibFn(fn, fnArgs) {
|
|
|
752
748
|
|
|
753
749
|
async function joinServer(uniqueName, isApi, exportLib) {
|
|
754
750
|
if (!setting.serviceReg.test(uniqueName)) {
|
|
755
|
-
|
|
751
|
+
return;
|
|
756
752
|
}
|
|
757
753
|
let asyncFnSize = 0
|
|
758
754
|
for (let key of Object.keys(exportLib)) {
|
|
@@ -782,7 +778,7 @@ async function joinServer(uniqueName, isApi, exportLib) {
|
|
|
782
778
|
|
|
783
779
|
async function offServer(uniqueName) {
|
|
784
780
|
if (!setting.serviceReg.test(uniqueName)) {
|
|
785
|
-
|
|
781
|
+
return;
|
|
786
782
|
}
|
|
787
783
|
let pair = parseUniqueName(uniqueName)
|
|
788
784
|
let service = `${pair[0]}/${trimJsirFileName(pair[1])}`;
|
|
@@ -1041,29 +1037,6 @@ function help(filterFn) {
|
|
|
1041
1037
|
}))
|
|
1042
1038
|
}
|
|
1043
1039
|
|
|
1044
|
-
function delTipsByIndex(idxs) {
|
|
1045
|
-
let keys = Object.keys(setting.tips)
|
|
1046
|
-
let indexKeyMap = {}
|
|
1047
|
-
for(let i = 1;i<=keys.length;i++) {
|
|
1048
|
-
indexKeyMap[i] = keys[i - 1]
|
|
1049
|
-
}
|
|
1050
|
-
let params = []
|
|
1051
|
-
for (const index of idxs) {
|
|
1052
|
-
if (setting.tips.hasOwnProperty(index)) {
|
|
1053
|
-
params.push(index)
|
|
1054
|
-
} else if (indexKeyMap[Number(index)]) {
|
|
1055
|
-
params.push(indexKeyMap[Number(index)])
|
|
1056
|
-
} else if (indexKeyMap[keys.length + 1 + Number(index)]) {
|
|
1057
|
-
params.push(indexKeyMap[keys.length + 1 + Number(index)])
|
|
1058
|
-
}
|
|
1059
|
-
}
|
|
1060
|
-
if (idxs.length > 0 && params.length <= 0) {
|
|
1061
|
-
console.warn("no items")
|
|
1062
|
-
return;
|
|
1063
|
-
}
|
|
1064
|
-
return delTips(...params)
|
|
1065
|
-
}
|
|
1066
|
-
|
|
1067
1040
|
function getPackageVersion(space, name) {
|
|
1068
1041
|
let moduleDir = setting.workspaceMap[space] + '/node_modules'
|
|
1069
1042
|
let dir = moduleDir + "/" + name;
|
|
@@ -1168,13 +1141,14 @@ async function showRooms() {
|
|
|
1168
1141
|
node: room.selfNode,
|
|
1169
1142
|
active: room.active,
|
|
1170
1143
|
pid: jsir.newError ? errorStr(pidStr):pidStr,
|
|
1171
|
-
up: jsir.upTime || '',
|
|
1144
|
+
up: formatSec(jsir.upTime) || '',
|
|
1172
1145
|
version: jsir.version || '',
|
|
1173
1146
|
space: jsir.space,
|
|
1174
1147
|
running: jsir.active,
|
|
1175
1148
|
port: jsir.port,
|
|
1176
1149
|
back: jsir.back,
|
|
1177
1150
|
busy: jsir.busy,
|
|
1151
|
+
mem: formatMb(jsir.mem) || '',
|
|
1178
1152
|
tips: Object.keys(jsir.tips).map(i => infoStr(i) + ': ' + jsir.tips[i]).join('\n'),
|
|
1179
1153
|
services: Object.keys(jsir.services || []).map(msgStr).join('\n')
|
|
1180
1154
|
});
|
|
@@ -1208,7 +1182,24 @@ const keywordDef = {
|
|
|
1208
1182
|
clear: {
|
|
1209
1183
|
comment: 'Clear task',
|
|
1210
1184
|
exeFn: async (args) => {
|
|
1211
|
-
|
|
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
|
+
}
|
|
1212
1203
|
},
|
|
1213
1204
|
args: {
|
|
1214
1205
|
tipIndex: 'left prompt index, can be separated by spaces for multiple'
|
|
@@ -1300,7 +1291,7 @@ const keywordDef = {
|
|
|
1300
1291
|
console.warn("no items")
|
|
1301
1292
|
} else {
|
|
1302
1293
|
let path = getFullPath(uniqueName)
|
|
1303
|
-
await eia(getEditor(), [path])
|
|
1294
|
+
await eia(getEditor(), [`"${path}"`], true)
|
|
1304
1295
|
}
|
|
1305
1296
|
},
|
|
1306
1297
|
args: {
|
|
@@ -1329,12 +1320,12 @@ const keywordDef = {
|
|
|
1329
1320
|
console.msg("config service offline")
|
|
1330
1321
|
} else if (_cmdMap[args[0]]){
|
|
1331
1322
|
let uniqueName = _cmdMap[args[0]];
|
|
1332
|
-
await eia(getEditor(), [
|
|
1323
|
+
await eia(getEditor(), [`"${getConfigDir()}/${md5(uniqueName + (global.$TEST ? '.test':''))}.json"`], true)
|
|
1333
1324
|
} else {
|
|
1334
1325
|
console.warn('invalid args')
|
|
1335
1326
|
}
|
|
1336
1327
|
} else {
|
|
1337
|
-
await eia(getEditor(), [
|
|
1328
|
+
await eia(getEditor(), [`"${getLibDataDir()}/config.json"`], true)
|
|
1338
1329
|
}
|
|
1339
1330
|
},
|
|
1340
1331
|
short: 's'
|
|
@@ -1346,18 +1337,24 @@ const keywordDef = {
|
|
|
1346
1337
|
if (!uniqueName) {
|
|
1347
1338
|
console.warn("no items")
|
|
1348
1339
|
} else {
|
|
1349
|
-
let
|
|
1350
|
-
if (
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
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
|
+
}
|
|
1358
1355
|
}
|
|
1356
|
+
await eia(getFileOpenExe(), [`"${path}"`], true)
|
|
1359
1357
|
}
|
|
1360
|
-
await e(`"${getFileOpenExe(parseUniqueName(uniqueName)[1])}" "${path}"`)
|
|
1361
1358
|
}
|
|
1362
1359
|
},
|
|
1363
1360
|
args: {
|
|
@@ -1374,7 +1371,7 @@ const keywordDef = {
|
|
|
1374
1371
|
let bSpace = args[1]
|
|
1375
1372
|
|
|
1376
1373
|
if (aUniqueName && bUniqueName) {
|
|
1377
|
-
let result = await
|
|
1374
|
+
let result = await eia(getConfig("compareExe", "diff"), [`"${getFullPath(aUniqueName)}"`, `"${getFullPath(bUniqueName)}"`], true)
|
|
1378
1375
|
if (result) {
|
|
1379
1376
|
console.log(result)
|
|
1380
1377
|
}
|
|
@@ -1904,26 +1901,36 @@ function getComments(i, cmdName, text, cols = [], col) {
|
|
|
1904
1901
|
let docLines = [getMd5Key(parseUniqueName(cmdName)[1]) + " - " + i]
|
|
1905
1902
|
text = trim(text)
|
|
1906
1903
|
docLines.push(...getTextComments(text))
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
})
|
|
1918
|
-
if (docLines.length > 2) {
|
|
1919
|
-
for (let line of docLines.slice(2)) {
|
|
1920
|
-
cols.push({
|
|
1921
|
-
doc: ' ' + line
|
|
1922
|
-
})
|
|
1923
|
-
}
|
|
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
|
+
})
|
|
1924
1914
|
}
|
|
1925
1915
|
}
|
|
1926
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
|
+
}
|
|
1927
1934
|
}
|
|
1928
1935
|
|
|
1929
1936
|
function trimJsirFileName(name) {
|
|
@@ -2021,12 +2028,6 @@ function getArgComments(argDef, showShort = false) {
|
|
|
2021
2028
|
let item = warnStr(k) + (shortMap[k] ? ` ${warnStr(shortMap[k])}`:'') + (argDef[k] ? ` ${argDef[k]}`:'')
|
|
2022
2029
|
comments.push(item)
|
|
2023
2030
|
}
|
|
2024
|
-
comments[0] = comments[0]
|
|
2025
|
-
if (comments.length > 1) {
|
|
2026
|
-
for(let i = 1; i< comments.length; i++) {
|
|
2027
|
-
comments[i] = comments[i]
|
|
2028
|
-
}
|
|
2029
|
-
}
|
|
2030
2031
|
}
|
|
2031
2032
|
return comments
|
|
2032
2033
|
}
|
|
@@ -2249,20 +2250,22 @@ function parseArgDef(inputString) {
|
|
|
2249
2250
|
result[s1] = '';
|
|
2250
2251
|
}
|
|
2251
2252
|
}
|
|
2253
|
+
let tempResult = {}
|
|
2252
2254
|
for (let key of Object.keys(result)) {
|
|
2253
2255
|
let comment = result[key];
|
|
2254
2256
|
let defaultValStr = ''
|
|
2255
2257
|
if (key.indexOf("=") !== -1) {
|
|
2256
|
-
delete result[key];
|
|
2257
2258
|
let ss = key.split('=').map(trim);
|
|
2258
2259
|
key = ss[0]
|
|
2259
2260
|
defaultValStr = ss[1]
|
|
2260
2261
|
}
|
|
2261
2262
|
if (defaultValStr) {
|
|
2262
|
-
|
|
2263
|
+
tempResult[key] = ['<' + warnStr(defaultValStr) + '>', comment].join(' ')
|
|
2264
|
+
} else {
|
|
2265
|
+
tempResult[key] = comment
|
|
2263
2266
|
}
|
|
2264
2267
|
}
|
|
2265
|
-
return
|
|
2268
|
+
return tempResult;
|
|
2266
2269
|
}
|
|
2267
2270
|
|
|
2268
2271
|
function getArgDef(text, uniqueName) {
|
|
@@ -2429,17 +2432,27 @@ function _addErrorTag(lines) {
|
|
|
2429
2432
|
} else {
|
|
2430
2433
|
result.push(line);
|
|
2431
2434
|
}
|
|
2432
|
-
let isAsyncFn = line.startsWith('async function');
|
|
2433
|
-
let isSyncFn = line.startsWith('function');
|
|
2435
|
+
let isAsyncFn = line.startsWith('async function ');
|
|
2436
|
+
let isSyncFn = line.startsWith('function ');
|
|
2434
2437
|
if (isAsyncFn || isSyncFn) {
|
|
2435
|
-
fnName = reget(line, /function\s+([
|
|
2438
|
+
fnName = reget(line, /function\s+([a-zA-Z_$][a-zA-Z0-9_$.]*)\s*\(/)
|
|
2436
2439
|
fnIsAsync = isAsyncFn;
|
|
2437
2440
|
}
|
|
2438
2441
|
if (functionEnd && fnName && (/\)\s*\{$/.test(trim(line)) || (line.startsWith("{") && trim(line) === '{'))) {
|
|
2439
2442
|
result[i] += 'try{';
|
|
2440
2443
|
if (wrapperFn.length > 0) {
|
|
2441
2444
|
let defaultArgsList = '("'+ fnName +'")';
|
|
2442
|
-
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(",")}],`
|
|
2443
2456
|
if (fnIsAsync) {
|
|
2444
2457
|
result[i] += `return await $aopAsync${runner}async ()=>{`;
|
|
2445
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
|
}
|
|
@@ -113,7 +107,12 @@ async function initNodes() {
|
|
|
113
107
|
}
|
|
114
108
|
}
|
|
115
109
|
} else {
|
|
116
|
-
let resp =
|
|
110
|
+
let resp = '';
|
|
111
|
+
try {
|
|
112
|
+
resp = await e(`${tailscalePath} status`);
|
|
113
|
+
} catch (e) {
|
|
114
|
+
console.$debug("initNodes", e)
|
|
115
|
+
}
|
|
117
116
|
for (let line of resp.split("\n").map(trim)) {
|
|
118
117
|
let offline = line.indexOf('offline') !== -1
|
|
119
118
|
let ss = line.split(/\s+/);
|
|
@@ -140,7 +139,7 @@ async function updateRoomInfo(room) {
|
|
|
140
139
|
room.name = setting.nodeMap[ip]?.name || os.hostname();
|
|
141
140
|
room.selfNode = ip;
|
|
142
141
|
room.lastUpdateTime = setting.roomTime
|
|
143
|
-
debug("init room", room.name)
|
|
142
|
+
console.$debug("init room", room.name)
|
|
144
143
|
|
|
145
144
|
await cleanRoom(room)
|
|
146
145
|
return room;
|
|
@@ -165,7 +164,7 @@ async function syncRooms() {
|
|
|
165
164
|
try {
|
|
166
165
|
respBody = await reqNode(node, "get", "/");
|
|
167
166
|
} catch (e) {
|
|
168
|
-
debug(`sync ${node} failed:`, e);
|
|
167
|
+
console.$debug(`sync ${node} failed:`, e);
|
|
169
168
|
if (!isDisableConnect(node, setting.defaultPort)) {
|
|
170
169
|
respBody = await reqNode(node, "get", "/", setting.defaultPort);
|
|
171
170
|
}
|
|
@@ -180,10 +179,10 @@ async function syncRooms() {
|
|
|
180
179
|
);
|
|
181
180
|
}
|
|
182
181
|
syncRooms.push(currRoom)
|
|
183
|
-
debug(`sync ${node} success`);
|
|
182
|
+
console.$debug(`sync ${node} success`);
|
|
184
183
|
}
|
|
185
184
|
} catch (e) {
|
|
186
|
-
debug(`sync ${node} failed:`, respBody, e);
|
|
185
|
+
console.$debug(`sync ${node} failed:`, respBody, e);
|
|
187
186
|
}
|
|
188
187
|
})
|
|
189
188
|
}
|
|
@@ -203,11 +202,11 @@ async function syncRooms() {
|
|
|
203
202
|
try {
|
|
204
203
|
await fp.unlink(roomsDir + '/' + file)
|
|
205
204
|
} catch (e) {
|
|
206
|
-
debug(e);
|
|
205
|
+
console.$debug(e);
|
|
207
206
|
}
|
|
208
207
|
}
|
|
209
208
|
}
|
|
210
|
-
debug('syncRooms done')
|
|
209
|
+
console.$debug('syncRooms done')
|
|
211
210
|
}
|
|
212
211
|
|
|
213
212
|
async function syncSetting() {
|
|
@@ -248,8 +247,8 @@ async function _syncSetting(room) {
|
|
|
248
247
|
setting.services = {}
|
|
249
248
|
setting.serviceFns = {}
|
|
250
249
|
for (const room of setting.rooms) {
|
|
251
|
-
if (
|
|
252
|
-
continue
|
|
250
|
+
if (!room.active) {
|
|
251
|
+
continue;
|
|
253
252
|
}
|
|
254
253
|
for (const pid of Object.keys(room.jsirs || {})) {
|
|
255
254
|
let jsir = room.jsirs[pid];
|
|
@@ -328,10 +327,10 @@ async function initRoom() {
|
|
|
328
327
|
pros.push(syncConfigs())
|
|
329
328
|
}
|
|
330
329
|
await Promise.all(pros);
|
|
331
|
-
}, false).catch(debug)
|
|
330
|
+
}, false).catch(console.$debug)
|
|
332
331
|
}
|
|
333
332
|
if (defTasks && Object.keys(defTasks).length > 0) {
|
|
334
|
-
processLock(roomTasksFile, processTasks, false).catch(debug)
|
|
333
|
+
processLock(roomTasksFile, processTasks, false).catch(console.$debug)
|
|
335
334
|
}
|
|
336
335
|
await syncSetting();
|
|
337
336
|
}
|
|
@@ -341,7 +340,7 @@ async function syncConfigs() {
|
|
|
341
340
|
try {
|
|
342
341
|
configs = await reqFn(setting.configMainFnKey);
|
|
343
342
|
} catch (e) {
|
|
344
|
-
debug("syncConfigs failed", e)
|
|
343
|
+
console.$debug("syncConfigs failed", e)
|
|
345
344
|
return;
|
|
346
345
|
}
|
|
347
346
|
let configDir = getConfigDir();
|
|
@@ -354,16 +353,16 @@ async function syncConfigs() {
|
|
|
354
353
|
let currText = String(await fp.readFile(path));
|
|
355
354
|
if (currText !== text) {
|
|
356
355
|
await fp.writeFile(path, text)
|
|
357
|
-
debug(`sync ${key} success`);
|
|
356
|
+
console.$debug(`sync ${key} success`);
|
|
358
357
|
}
|
|
359
358
|
} else {
|
|
360
359
|
await fp.writeFile(path, text)
|
|
361
|
-
debug(`sync ${key} success`);
|
|
360
|
+
console.$debug(`sync ${key} success`);
|
|
362
361
|
}
|
|
363
362
|
});
|
|
364
363
|
}
|
|
365
364
|
await batchAsync(fns, 33)
|
|
366
|
-
debug('syncConfigs done')
|
|
365
|
+
console.$debug('syncConfigs done')
|
|
367
366
|
}
|
|
368
367
|
|
|
369
368
|
async function cleanRoom(room) {
|
|
@@ -373,10 +372,10 @@ async function cleanRoom(room) {
|
|
|
373
372
|
let active = Date.now() - (jsir.lastUpdateTime || 0) <= setting.roomHeartbeatExpire;
|
|
374
373
|
if (jsir.back && !active && isPidAlive(pid)) {
|
|
375
374
|
try {
|
|
376
|
-
debug(`kill inactive jsir ${pid}`)
|
|
375
|
+
console.$debug(`kill inactive jsir ${pid}`)
|
|
377
376
|
process.kill(parseInt(pid), 'SIGKILL'); // 强制终止进程
|
|
378
377
|
} catch (e) {
|
|
379
|
-
debug(`cleanRoom ${pid} failed`, e)
|
|
378
|
+
console.$debug(`cleanRoom ${pid} failed`, e)
|
|
380
379
|
}
|
|
381
380
|
}
|
|
382
381
|
}
|
|
@@ -433,7 +432,8 @@ async function initRoomJsir(room) {
|
|
|
433
432
|
services: services,
|
|
434
433
|
newError: setting.newError,
|
|
435
434
|
version: packageJson.version,
|
|
436
|
-
upTime:
|
|
435
|
+
upTime: process.uptime(),
|
|
436
|
+
mem: Math.floor(process.memoryUsage().rss / 1024 / 1024)
|
|
437
437
|
}
|
|
438
438
|
room.jsirs[process.pid] = setting.selfJsir
|
|
439
439
|
}
|
|
@@ -542,7 +542,7 @@ async function reqNode(node, method, url, port, body) {
|
|
|
542
542
|
req.end();
|
|
543
543
|
});
|
|
544
544
|
} finally {
|
|
545
|
-
debug(`Request cost ${Date.now() - time}ms`, JSON.stringify(opt))
|
|
545
|
+
console.$debug(`Request cost ${Date.now() - time}ms`, JSON.stringify(opt))
|
|
546
546
|
}
|
|
547
547
|
}
|
|
548
548
|
|