jsir 2.6.3 → 2.6.5
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 +106 -43
- package/deps/room.js +31 -17
- package/deps/setting.js +1 -0
- package/deps/util.js +15 -11
- package/package.json +1 -1
package/cmd/oaa.js
CHANGED
|
@@ -173,6 +173,7 @@ async function start() {
|
|
|
173
173
|
checkWorkspaces()
|
|
174
174
|
initWorkspace()
|
|
175
175
|
|
|
176
|
+
await Room.initNodes();
|
|
176
177
|
await Room.syncSetting();
|
|
177
178
|
|
|
178
179
|
lastFilterArg = getConfig("lastFilterArg");
|
|
@@ -377,17 +378,23 @@ function closeRl() {
|
|
|
377
378
|
function _nextLine(callback, promptStr, hidden, resolve, end, isText) {
|
|
378
379
|
if (!setting.enableNextLine) {
|
|
379
380
|
console.$log(warnStr("[warn]"), "NextLine Disabled");
|
|
380
|
-
|
|
381
|
+
resolve(null);
|
|
382
|
+
return;
|
|
381
383
|
}
|
|
382
|
-
end = trim(end)
|
|
383
384
|
if (!_haveWrapperInput) {
|
|
384
|
-
|
|
385
|
+
console.$error(errorStr("[error]"), "NextLine Blocked");
|
|
386
|
+
resolve(null);
|
|
387
|
+
return;
|
|
385
388
|
}
|
|
386
389
|
initRl(callback, promptStr, hidden);
|
|
387
|
-
|
|
388
|
-
if (!
|
|
390
|
+
let isJsirNext = !callback || callback === wrapperInput
|
|
391
|
+
if (!isJsirNext) {
|
|
392
|
+
_haveWrapperInput = false
|
|
393
|
+
}
|
|
394
|
+
if (isJsirNext) {
|
|
389
395
|
global.$newInput = true;
|
|
390
396
|
}
|
|
397
|
+
end = trim(end)
|
|
391
398
|
let inputStr = ''
|
|
392
399
|
let lineHandler = async line => {
|
|
393
400
|
let textLine = line
|
|
@@ -413,7 +420,7 @@ function _nextLine(callback, promptStr, hidden, resolve, end, isText) {
|
|
|
413
420
|
inputStr = '.e ' + inputStr;
|
|
414
421
|
}
|
|
415
422
|
let pro = (callback || wrapperInput)(inputStr);
|
|
416
|
-
resolve
|
|
423
|
+
resolve(inputStr);
|
|
417
424
|
try {
|
|
418
425
|
await pro;
|
|
419
426
|
} finally {
|
|
@@ -479,6 +486,24 @@ function resetCmdMap(cmdMap = {}) {
|
|
|
479
486
|
_cmdMap = cmdMap;
|
|
480
487
|
}
|
|
481
488
|
|
|
489
|
+
function moveConfig(old, _new) {
|
|
490
|
+
let configDir = getConfigDir();
|
|
491
|
+
let uniqueOld = toUniqueName(old);
|
|
492
|
+
let uniqueNew = toUniqueName(_new);
|
|
493
|
+
|
|
494
|
+
let configFileOld = configDir + '/' + md5(uniqueOld) + '.json';
|
|
495
|
+
let configFileNew = configDir + '/' + md5(uniqueNew) + '.json';
|
|
496
|
+
if (fs.existsSync(configFileOld) && !fs.existsSync(configFileNew)) {
|
|
497
|
+
fs.renameSync(configFileOld, configFileNew)
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
let configFileOldTest = configDir + '/' + md5(uniqueOld + '.test') + '.json';
|
|
501
|
+
let configFileNewTest = configDir + '/' + md5(uniqueNew + '.test') + '.json';
|
|
502
|
+
if (fs.existsSync(configFileOldTest) && !fs.existsSync(configFileNewTest)) {
|
|
503
|
+
fs.renameSync(configFileOldTest, configFileNewTest)
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
482
507
|
function rename(dir, oldName, newName) {
|
|
483
508
|
let old = dir + '/' + oldName;
|
|
484
509
|
let _new = dir + '/' + newName;
|
|
@@ -487,15 +512,7 @@ function rename(dir, oldName, newName) {
|
|
|
487
512
|
} else {
|
|
488
513
|
fs.renameSync(old, _new)
|
|
489
514
|
console.info(`${_new} renamed`)
|
|
490
|
-
|
|
491
|
-
let configDir = getConfigDir();
|
|
492
|
-
let uniqueOld = toUniqueName(old);
|
|
493
|
-
let uniqueNew = toUniqueName(_new);
|
|
494
|
-
let configFileOld = configDir + '/' + md5(uniqueOld) + '.json';
|
|
495
|
-
let configFileNew = configDir + '/' + md5(uniqueNew) + '.json';
|
|
496
|
-
if (fs.existsSync(configFileOld) && !fs.existsSync(configFileNew)) {
|
|
497
|
-
fs.renameSync(configFileOld, configFileNew)
|
|
498
|
-
}
|
|
515
|
+
moveConfig(old, _new);
|
|
499
516
|
}
|
|
500
517
|
resetCmdMap({
|
|
501
518
|
1: toUniqueName(_new)
|
|
@@ -581,12 +598,7 @@ async function wrapperInput(str) {
|
|
|
581
598
|
}
|
|
582
599
|
|
|
583
600
|
function wrapperAlias(str) {
|
|
584
|
-
let aliasMap =
|
|
585
|
-
try {
|
|
586
|
-
aliasMap = getConfig("alias");
|
|
587
|
-
} catch (e) {
|
|
588
|
-
console.$error("getAlias failed ", e);
|
|
589
|
-
}
|
|
601
|
+
let aliasMap = getConfig("alias");
|
|
590
602
|
if (!aliasMap || Object.keys(aliasMap).length <= 0) {
|
|
591
603
|
return str;
|
|
592
604
|
}
|
|
@@ -647,7 +659,7 @@ async function _wrapperInput(str) {
|
|
|
647
659
|
} else if (firstName === setting.exeKey) {
|
|
648
660
|
await runCmd(str)
|
|
649
661
|
} else if (firstName === setting.initKey) {
|
|
650
|
-
await runInit(uniqueName,
|
|
662
|
+
await runInit(uniqueName, enrichArgs(str).slice(1))
|
|
651
663
|
} else {
|
|
652
664
|
console.log(String(fs.readFileSync(path)));
|
|
653
665
|
}
|
|
@@ -657,20 +669,44 @@ async function _wrapperInput(str) {
|
|
|
657
669
|
}
|
|
658
670
|
}
|
|
659
671
|
|
|
660
|
-
async function runInit(uniqueName,
|
|
672
|
+
async function runInit(uniqueName, args) {
|
|
673
|
+
let arg = args[0]
|
|
661
674
|
if (arg === '-') {
|
|
662
675
|
await offServer(uniqueName)
|
|
663
676
|
} else {
|
|
664
|
-
await
|
|
677
|
+
let exportLib = await _requireSource(setting.defaultSpace, uniqueName, true);
|
|
678
|
+
if (typeof exportLib === "function") {
|
|
679
|
+
await execLibFn(exportLib, args)
|
|
680
|
+
} else if (exportLib && exportLib[arg]) {
|
|
681
|
+
await execLibFn(exportLib[arg], args.slice(1))
|
|
682
|
+
} else if (!arg || arg === '+') {
|
|
683
|
+
await joinServer(uniqueName, arg === '+', exportLib)
|
|
684
|
+
} else {
|
|
685
|
+
console.warn("no items")
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
async function execLibFn(fn, fnArgs) {
|
|
691
|
+
for (let i = 0; i < fnArgs.length; i++) {
|
|
692
|
+
let item = fnArgs[i]
|
|
693
|
+
if (item.endsWith(' ')) {
|
|
694
|
+
fnArgs[i] = await evalText('return ' + item.trim())
|
|
695
|
+
} else {
|
|
696
|
+
fnArgs[i] = item.trim()
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
let result = await fn(...fnArgs);
|
|
700
|
+
if (vl(result)) {
|
|
701
|
+
console.log(result)
|
|
665
702
|
}
|
|
666
703
|
}
|
|
667
704
|
|
|
668
|
-
async function joinServer(uniqueName, isApi) {
|
|
705
|
+
async function joinServer(uniqueName, isApi, exportLib) {
|
|
669
706
|
Room.onRoom();
|
|
670
707
|
if (!setting.serviceReg.test(uniqueName)) {
|
|
671
708
|
throw 'invalid service name';
|
|
672
709
|
}
|
|
673
|
-
let exportLib = await _requireSource(setting.defaultSpace, uniqueName, true);
|
|
674
710
|
let asyncFnSize = 0
|
|
675
711
|
for (let key of Object.keys(exportLib)) {
|
|
676
712
|
let val = exportLib[key];
|
|
@@ -1224,7 +1260,7 @@ const keywordDef = {
|
|
|
1224
1260
|
console.msg("config service offline")
|
|
1225
1261
|
} else if (_cmdMap[args[0]]){
|
|
1226
1262
|
let uniqueName = _cmdMap[args[0]];
|
|
1227
|
-
await eia(getEditor(), [`${getConfigDir()}/${md5(uniqueName)}.json`])
|
|
1263
|
+
await eia(getEditor(), [`${getConfigDir()}/${md5(uniqueName + (global.$TEST ? '.test':''))}.json`])
|
|
1228
1264
|
} else {
|
|
1229
1265
|
console.warn('invalid args')
|
|
1230
1266
|
}
|
|
@@ -1316,7 +1352,7 @@ const keywordDef = {
|
|
|
1316
1352
|
for (let un of [uniqueName, ...cmds]) {
|
|
1317
1353
|
let path = getFullPath(un);
|
|
1318
1354
|
let text = removeComment(String(fs.readFileSync(path)))
|
|
1319
|
-
let [space] = parseUniqueName(
|
|
1355
|
+
let [space] = parseUniqueName(un)
|
|
1320
1356
|
|
|
1321
1357
|
text = wrapperJsirText(text)
|
|
1322
1358
|
|
|
@@ -1450,7 +1486,7 @@ const keywordDef = {
|
|
|
1450
1486
|
} else {
|
|
1451
1487
|
let typeKey = getJsirTypeKey(parseUniqueName(cmds[0])[1]);
|
|
1452
1488
|
if (setting.initKey === typeKey) {
|
|
1453
|
-
await runInit(cmds[0], args
|
|
1489
|
+
await runInit(cmds[0], args.slice(1))
|
|
1454
1490
|
} else if (setting.exeKey === typeKey) {
|
|
1455
1491
|
await runScript(cmds[0], args.slice(1))
|
|
1456
1492
|
}
|
|
@@ -1542,15 +1578,49 @@ const keywordDef = {
|
|
|
1542
1578
|
debug: {
|
|
1543
1579
|
comment: 'debug mode',
|
|
1544
1580
|
exeFn: (args) => {
|
|
1545
|
-
if (
|
|
1546
|
-
|
|
1581
|
+
if ('+' === args[0]) {
|
|
1582
|
+
if (!global.$DEBUG) {
|
|
1583
|
+
global.$DEBUG = true;
|
|
1584
|
+
setTips("DEBUG", "DEBUG", () => delete global.$DEBUG);
|
|
1585
|
+
}
|
|
1586
|
+
} else if ('-' === args[0]) {
|
|
1587
|
+
if (global.$DEBUG) {
|
|
1588
|
+
delTips("DEBUG")
|
|
1589
|
+
}
|
|
1547
1590
|
} else {
|
|
1548
|
-
global.$DEBUG
|
|
1549
|
-
|
|
1591
|
+
if (global.$DEBUG) {
|
|
1592
|
+
delTips("DEBUG")
|
|
1593
|
+
} else {
|
|
1594
|
+
global.$DEBUG = true;
|
|
1595
|
+
setTips("DEBUG", "DEBUG", () => delete global.$DEBUG);
|
|
1596
|
+
}
|
|
1550
1597
|
}
|
|
1551
1598
|
},
|
|
1552
1599
|
short: 'D'
|
|
1553
1600
|
},
|
|
1601
|
+
test: {
|
|
1602
|
+
comment: 'test mode',
|
|
1603
|
+
exeFn: (args) => {
|
|
1604
|
+
if ('+' === args[0]) {
|
|
1605
|
+
if (!global.$TEST) {
|
|
1606
|
+
global.$TEST = true;
|
|
1607
|
+
setTips("TEST", "TEST", () => delete global.$TEST);
|
|
1608
|
+
}
|
|
1609
|
+
} else if ('-' === args[0]) {
|
|
1610
|
+
if (global.$TEST) {
|
|
1611
|
+
delTips("TEST")
|
|
1612
|
+
}
|
|
1613
|
+
} else {
|
|
1614
|
+
if (global.$TEST) {
|
|
1615
|
+
delTips("TEST")
|
|
1616
|
+
} else {
|
|
1617
|
+
global.$TEST = true;
|
|
1618
|
+
setTips("TEST", "TEST", () => delete global.$TEST);
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
},
|
|
1622
|
+
short: 'T'
|
|
1623
|
+
},
|
|
1554
1624
|
room: {
|
|
1555
1625
|
comment: 'manage room ([node])',
|
|
1556
1626
|
exeFn: async (args) => {
|
|
@@ -1607,15 +1677,8 @@ function getQuickRunCmds(input, matchStr) {
|
|
|
1607
1677
|
} else if (input.startsWith("/")) {
|
|
1608
1678
|
matchStr = '/' + matchStr;
|
|
1609
1679
|
}
|
|
1610
|
-
return filterCmd(matchStr).filter(i =>
|
|
1611
|
-
|
|
1612
|
-
if (setting.serviceReg.test(i)) {
|
|
1613
|
-
types = [setting.exeKey, setting.initKey];
|
|
1614
|
-
} else {
|
|
1615
|
-
types = [setting.exeKey]
|
|
1616
|
-
}
|
|
1617
|
-
return types.includes(getJsirTypeKey(parseUniqueName(i)[1]))
|
|
1618
|
-
})
|
|
1680
|
+
return filterCmd(matchStr).filter(i =>
|
|
1681
|
+
[setting.exeKey, setting.initKey].includes(getJsirTypeKey(parseUniqueName(i)[1])))
|
|
1619
1682
|
}
|
|
1620
1683
|
|
|
1621
1684
|
async function preLoad(text, fnNameMatch, fn, packages = [], space) {
|
|
@@ -2209,7 +2272,7 @@ async function _requireSource(currSpace, cmdMatchStr, force = false) {
|
|
|
2209
2272
|
let typeKey = getJsirTypeKey(pair[1]);
|
|
2210
2273
|
if (typeKey === setting.initKey) {
|
|
2211
2274
|
let pair = parseUniqueName(uniqueName)
|
|
2212
|
-
if (
|
|
2275
|
+
if (global.$TEST || force) {
|
|
2213
2276
|
result = await evalText(text, uniqueName)
|
|
2214
2277
|
} else {
|
|
2215
2278
|
if (setting.serviceReg.test(uniqueName)) {
|
package/deps/room.js
CHANGED
|
@@ -115,26 +115,39 @@ function getSelfIP(nodes) {
|
|
|
115
115
|
return ips[0] || '127.0.0.1'
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
async function
|
|
119
|
-
let resp = await e(`${tailscalePath} status`);
|
|
118
|
+
async function initNodes() {
|
|
120
119
|
let nodes = {}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
120
|
+
let configNodes = getConfig("nodes", []).map(trim).filter(i => i)
|
|
121
|
+
if (configNodes.length > 0) {
|
|
122
|
+
for (const node of configNodes) {
|
|
123
|
+
let ss = node.split("@").map(trim).filter(i => i);
|
|
124
|
+
if (ss.length > 1) {
|
|
125
|
+
nodes[ss[1]] = {
|
|
126
|
+
account: ss[0]
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
nodes[ss[0]] = {}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
let resp = await e(`${tailscalePath} status`);
|
|
134
|
+
for (let line of resp.split("\n").map(trim)) {
|
|
135
|
+
let offline = line.indexOf('offline') !== -1
|
|
136
|
+
let ss = line.split(/\s+/);
|
|
137
|
+
if (/^\d+\.\d+\.\d+\.\d+$/.test(ss[0])) {
|
|
138
|
+
nodes[ss[0]] = {
|
|
139
|
+
name: ss[1],
|
|
140
|
+
offline,
|
|
141
|
+
account: ss[2]
|
|
142
|
+
}
|
|
129
143
|
}
|
|
130
144
|
}
|
|
131
145
|
}
|
|
132
|
-
|
|
146
|
+
setting.nodeMap = await fileJson("jsirNodes.json", obj => nodes);
|
|
133
147
|
}
|
|
134
148
|
|
|
135
149
|
async function updateRoomInfo() {
|
|
136
|
-
|
|
137
|
-
await fileJson("jsirNodes.json", obj => setting.nodeMap);
|
|
150
|
+
await initNodes();
|
|
138
151
|
let nodes = Object.keys(setting.nodeMap)
|
|
139
152
|
let ip = getSelfIP(nodes)
|
|
140
153
|
await fileJson(roomDataFile, async room => {
|
|
@@ -144,7 +157,7 @@ async function updateRoomInfo() {
|
|
|
144
157
|
// 设置roomName
|
|
145
158
|
room.name = setting.nodeMap[ip]?.name || os.hostname();
|
|
146
159
|
room.selfNode = ip;
|
|
147
|
-
room.lastUpdateTime =
|
|
160
|
+
room.lastUpdateTime = setting.roomTime
|
|
148
161
|
debug("init room", room.name)
|
|
149
162
|
|
|
150
163
|
await cleanRoom(room)
|
|
@@ -217,7 +230,6 @@ async function syncRooms() {
|
|
|
217
230
|
}
|
|
218
231
|
|
|
219
232
|
async function syncSetting() {
|
|
220
|
-
setting.nodeMap = await getTailscaleNodes();
|
|
221
233
|
let room = await fileJson(roomDataFile)
|
|
222
234
|
await fileLock(roomsDirLockKey, async () => await _syncSetting(room));
|
|
223
235
|
}
|
|
@@ -314,6 +326,7 @@ async function processTasks() {
|
|
|
314
326
|
}
|
|
315
327
|
|
|
316
328
|
async function initRoom() {
|
|
329
|
+
setting.roomTime = Date.now();
|
|
317
330
|
setting.nodeMap = await fileJson(jsirNodesFile);
|
|
318
331
|
let roomUpdateTouchTime = false;
|
|
319
332
|
await fileJson(roomDataFile, async room => {
|
|
@@ -431,7 +444,7 @@ async function initRoomJsir(room) {
|
|
|
431
444
|
}, {}),
|
|
432
445
|
busy: await getEventLoopDelay() || (lastJsir ? lastJsir.busy:0),
|
|
433
446
|
back: isRunningInBackground(),
|
|
434
|
-
lastUpdateTime:
|
|
447
|
+
lastUpdateTime: setting.roomTime,
|
|
435
448
|
port: setting.server ? setting.server.address()?.port:null,
|
|
436
449
|
services: services,
|
|
437
450
|
newError: global.$newError,
|
|
@@ -590,5 +603,6 @@ module.exports = {
|
|
|
590
603
|
syncSetting,
|
|
591
604
|
reqNode,
|
|
592
605
|
reqFn,
|
|
593
|
-
localConfigs
|
|
606
|
+
localConfigs,
|
|
607
|
+
initNodes
|
|
594
608
|
}
|
package/deps/setting.js
CHANGED
package/deps/util.js
CHANGED
|
@@ -233,7 +233,12 @@ function createConfig(uniqueName) {
|
|
|
233
233
|
}
|
|
234
234
|
if (uniqueName) {
|
|
235
235
|
result.local = (key, defaultVal) => {
|
|
236
|
-
|
|
236
|
+
try {
|
|
237
|
+
return _getConfig(key, defaultVal, uniqueName);
|
|
238
|
+
} catch (e) {
|
|
239
|
+
console.$error(`getConfig [${uniqueName}] "${key}" failed`, e)
|
|
240
|
+
}
|
|
241
|
+
return defaultVal;
|
|
237
242
|
}
|
|
238
243
|
}
|
|
239
244
|
return result;
|
|
@@ -580,14 +585,8 @@ function trimEmptyLine(text) {
|
|
|
580
585
|
return lines.slice(start, end + 1).join('\n');
|
|
581
586
|
}
|
|
582
587
|
|
|
583
|
-
let defaultEditor = "vi";
|
|
584
588
|
function getEditor() {
|
|
585
|
-
|
|
586
|
-
defaultEditor = getConfig("defaultEditor", "vi");
|
|
587
|
-
} catch (e) {
|
|
588
|
-
console.$error('getEditor failed', e);
|
|
589
|
-
}
|
|
590
|
-
return defaultEditor
|
|
589
|
+
return getConfig("defaultEditor", "vi")
|
|
591
590
|
}
|
|
592
591
|
|
|
593
592
|
function timeStr(fmt, date) {
|
|
@@ -722,7 +721,7 @@ function createDirs(filePath) {
|
|
|
722
721
|
}
|
|
723
722
|
|
|
724
723
|
function createLimitLogger(fileName, {
|
|
725
|
-
maxChars =
|
|
724
|
+
maxChars = 128 * 1024 * 1024,
|
|
726
725
|
logInfo = true,
|
|
727
726
|
time = true,
|
|
728
727
|
error = false,
|
|
@@ -985,7 +984,12 @@ function aesDecipher(str, key){
|
|
|
985
984
|
}
|
|
986
985
|
|
|
987
986
|
function getConfig(key, defaultVal) {
|
|
988
|
-
|
|
987
|
+
try {
|
|
988
|
+
return _getConfig(key, defaultVal)
|
|
989
|
+
} catch (e) {
|
|
990
|
+
console.$error(`getConfig "${key}" failed`, e)
|
|
991
|
+
}
|
|
992
|
+
return defaultVal;
|
|
989
993
|
}
|
|
990
994
|
|
|
991
995
|
function getConfigDir() {
|
|
@@ -1030,7 +1034,7 @@ function _getConfig(key, defaultVal, uniqueName) {
|
|
|
1030
1034
|
let configInit = {}
|
|
1031
1035
|
let configFile = getLibDataDir() + '/config.json';
|
|
1032
1036
|
if (uniqueName) {
|
|
1033
|
-
configFile = getConfigDir() + "/" + md5(uniqueName) + ".json"
|
|
1037
|
+
configFile = getConfigDir() + "/" + md5(uniqueName + (global.$TEST ? ".test":"")) + ".json"
|
|
1034
1038
|
}
|
|
1035
1039
|
if (!fs.existsSync(configFile)) {
|
|
1036
1040
|
fs.writeFileSync(configFile, JSON.stringify(configInit, null, 2));
|