jsir 2.4.9 → 2.5.1
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 +31 -30
- package/deps/room.js +8 -3
- package/deps/server.js +53 -46
- package/deps/util.js +17 -6
- package/package.json +1 -1
package/cmd/oaa.js
CHANGED
|
@@ -585,8 +585,7 @@ function wrapperAlias(str) {
|
|
|
585
585
|
return str;
|
|
586
586
|
}
|
|
587
587
|
let fstr = str.split(/\s+/)[0];
|
|
588
|
-
|
|
589
|
-
return [fstr, str.substring(fstr.length)].map(trim).filter(i => i).join(' ')
|
|
588
|
+
return [getAlias(aliasMap, fstr), str.substring(fstr.length)].map(trim).filter(i => i).join(' ')
|
|
590
589
|
}
|
|
591
590
|
|
|
592
591
|
async function _wrapperInput(str) {
|
|
@@ -1405,13 +1404,11 @@ const keywordDef = {
|
|
|
1405
1404
|
}
|
|
1406
1405
|
}
|
|
1407
1406
|
let pair = parseUniqueName(keyword)
|
|
1408
|
-
let cmds
|
|
1409
|
-
if (
|
|
1410
|
-
cmds = getQuickRunCmds(keyword,
|
|
1411
|
-
} else {
|
|
1412
|
-
cmds = getQuickRunCmds(keyword, '^e ' + toJsirFileName(pair[1]) + '$')
|
|
1407
|
+
let cmds = getQuickRunCmds(keyword, pair[1])
|
|
1408
|
+
if (cmds.length > 1) {
|
|
1409
|
+
cmds = getQuickRunCmds(keyword, '^' + toJsirFileName(pair[1]) + '$')
|
|
1413
1410
|
if (cmds.length === 0) {
|
|
1414
|
-
cmds = getQuickRunCmds(keyword,
|
|
1411
|
+
cmds = getQuickRunCmds(keyword, '^e[^a-zA-Z]' + toJsirFileName(pair[1]) + '$')
|
|
1415
1412
|
}
|
|
1416
1413
|
}
|
|
1417
1414
|
if (cmds.length === 1) {
|
|
@@ -1421,7 +1418,7 @@ const keywordDef = {
|
|
|
1421
1418
|
await runScript(cmds[0], args.slice(1))
|
|
1422
1419
|
}
|
|
1423
1420
|
} else {
|
|
1424
|
-
console.warn(
|
|
1421
|
+
console.warn(`require "${keyword}" not unique`)
|
|
1425
1422
|
}
|
|
1426
1423
|
},
|
|
1427
1424
|
args: {
|
|
@@ -1470,13 +1467,15 @@ const keywordDef = {
|
|
|
1470
1467
|
log: {
|
|
1471
1468
|
comment: 'View log (type[[lt][le]], index)',
|
|
1472
1469
|
exeFn: async (args) => {
|
|
1473
|
-
let path = `${getLibDataDir()}/log/
|
|
1470
|
+
let path = `${getLibDataDir()}/log/room`
|
|
1474
1471
|
let type = '';
|
|
1475
1472
|
for (let arg of args) {
|
|
1476
1473
|
if (_cmdMap[arg]) {
|
|
1477
1474
|
let pair = parseUniqueName(_cmdMap[arg])
|
|
1478
1475
|
let fileName = pair[0] + '/' + pair[1].split(".")[0]
|
|
1479
1476
|
path = `${getLibDataDir()}/log/${fileName}`
|
|
1477
|
+
} else if (arg === '0') {
|
|
1478
|
+
path = `${getLibDataDir()}/log/jsir`
|
|
1480
1479
|
} else {
|
|
1481
1480
|
type = arg;
|
|
1482
1481
|
}
|
|
@@ -1633,17 +1632,17 @@ async function _dealKeyword(keyword, args) {
|
|
|
1633
1632
|
if (item.short === shortKey) {
|
|
1634
1633
|
unMatched = false;
|
|
1635
1634
|
if (item === keywordDef.run) {
|
|
1636
|
-
console.$log('
|
|
1635
|
+
console.$log('Execute', key)
|
|
1637
1636
|
await item.exeFn(args);
|
|
1638
1637
|
} else {
|
|
1639
|
-
console.$log('
|
|
1638
|
+
console.$log('Execute', key)
|
|
1640
1639
|
await item.exeFn(args.map(trim));
|
|
1641
1640
|
}
|
|
1642
1641
|
break;
|
|
1643
1642
|
}
|
|
1644
1643
|
}
|
|
1645
1644
|
if (unMatched) {
|
|
1646
|
-
console.$log('
|
|
1645
|
+
console.$log('Execute run', keyword)
|
|
1647
1646
|
await keywordDef.run.exeFn([keyword, ...args])
|
|
1648
1647
|
}
|
|
1649
1648
|
}
|
|
@@ -2133,25 +2132,24 @@ function filterRequire(currSpace, cmdMatchStr) {
|
|
|
2133
2132
|
}
|
|
2134
2133
|
cmdMatchStr = trim(cmdMatchStr);
|
|
2135
2134
|
|
|
2136
|
-
let
|
|
2137
|
-
let
|
|
2138
|
-
let
|
|
2139
|
-
if (
|
|
2140
|
-
cmds = [
|
|
2141
|
-
} else {
|
|
2142
|
-
let appointSpace = parseUniqueName(cmdMatchStr)[0]
|
|
2143
|
-
cmds = filterCmd(cmdMatchStr);
|
|
2144
|
-
if (cmds.length > 0) {
|
|
2145
|
-
cmds = cmds.filter(cmd => parseUniqueName(cmd)[0] === (appointSpace || currSpace))
|
|
2146
|
-
}
|
|
2147
|
-
if (cmds.length > 0) {
|
|
2148
|
-
cmds = cmds.filter(cmd => getJsirTypeKey(parseUniqueName(cmd)[1]) === setting.initKey)
|
|
2149
|
-
}
|
|
2135
|
+
let pair = parseUniqueName(cmdMatchStr);
|
|
2136
|
+
let appointSpace = pair[0] || currSpace;
|
|
2137
|
+
let cmds = filterCmd(appointSpace + '/' + pair[1]);
|
|
2138
|
+
if (cmds.length > 1) {
|
|
2139
|
+
cmds = cmds.filter(cmd => [setting.initKey, setting.exeKey].indexOf(getJsirTypeKey(parseUniqueName(cmd)[1])) !== -1)
|
|
2150
2140
|
}
|
|
2151
2141
|
if (cmds.length > 1) {
|
|
2152
|
-
|
|
2153
|
-
}
|
|
2154
|
-
|
|
2142
|
+
cmds = cmds.filter(cmd => setting.initKey === getJsirTypeKey(parseUniqueName(cmd)[1]))
|
|
2143
|
+
}
|
|
2144
|
+
if (cmds.length > 1) {
|
|
2145
|
+
let jsirFileName = toJsirFileName(pair[1])
|
|
2146
|
+
cmds = filterCmd(appointSpace + '/^' + jsirFileName + "$");
|
|
2147
|
+
if (cmds.length === 0) {
|
|
2148
|
+
cmds = filterCmd(appointSpace + '/^[ei][^a-zA-Z]' + jsirFileName + "$");
|
|
2149
|
+
}
|
|
2150
|
+
}
|
|
2151
|
+
if (cmds.length !== 1) {
|
|
2152
|
+
throw `require "${cmdMatchStr}" not unique`
|
|
2155
2153
|
}
|
|
2156
2154
|
return cmds[0];
|
|
2157
2155
|
}
|
|
@@ -2215,6 +2213,9 @@ function addErrorTag(text) {
|
|
|
2215
2213
|
}
|
|
2216
2214
|
|
|
2217
2215
|
async function evalText($text = '', $cmdName = '', $args = []) {
|
|
2216
|
+
if ($cmdName) {
|
|
2217
|
+
console.$log(`Execute ${$cmdName}`);
|
|
2218
|
+
}
|
|
2218
2219
|
let currSpace;
|
|
2219
2220
|
if ($cmdName) {
|
|
2220
2221
|
let pair = parseUniqueName($cmdName)
|
package/deps/room.js
CHANGED
|
@@ -71,6 +71,7 @@ function onRoom(wrapperInput) {
|
|
|
71
71
|
let outputs = setting.enterOutputs || [];
|
|
72
72
|
setting.enterOutputs = null;
|
|
73
73
|
res.output = outputs.join('');
|
|
74
|
+
global.$newInput = true;
|
|
74
75
|
}
|
|
75
76
|
})
|
|
76
77
|
if (configMain) {
|
|
@@ -246,9 +247,9 @@ async function _syncSetting(room) {
|
|
|
246
247
|
setting.services = {}
|
|
247
248
|
setting.serviceFns = {}
|
|
248
249
|
for (const room of setting.rooms) {
|
|
249
|
-
for (const pid of Object.keys(room.jsirs ||
|
|
250
|
+
for (const pid of Object.keys(room.jsirs || {})) {
|
|
250
251
|
let jsir = room.jsirs[pid];
|
|
251
|
-
for (const service of Object.keys(jsir.services ||
|
|
252
|
+
for (const service of Object.keys(jsir.services || {})) {
|
|
252
253
|
let serviceFns = jsir.services[service] || [];
|
|
253
254
|
|
|
254
255
|
let existFns = setting.services[service] || [];
|
|
@@ -365,6 +366,10 @@ async function initRoomJsir(room) {
|
|
|
365
366
|
getOr(services, ss[1], []).push(ss[2])
|
|
366
367
|
}
|
|
367
368
|
}
|
|
369
|
+
// 去重
|
|
370
|
+
for (let key of Object.keys(services)) {
|
|
371
|
+
services[key] = [...new Set(services[key])]
|
|
372
|
+
}
|
|
368
373
|
// 初始化jsir
|
|
369
374
|
let lastJsir = room.jsirs[process.pid]
|
|
370
375
|
room.jsirs[process.pid] = {
|
|
@@ -490,7 +495,7 @@ async function reqNode(node, method, url, port, body) {
|
|
|
490
495
|
}
|
|
491
496
|
});
|
|
492
497
|
} finally {
|
|
493
|
-
debug(`
|
|
498
|
+
debug(`Request cost ${Date.now() - time}ms`, JSON.stringify(opt))
|
|
494
499
|
}
|
|
495
500
|
}
|
|
496
501
|
|
package/deps/server.js
CHANGED
|
@@ -33,6 +33,54 @@ function createSign() {
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
async function process(routeKey, req, res, params) {
|
|
37
|
+
if (routes[routeKey]) {
|
|
38
|
+
if (!apiReg.test(routeKey)) {
|
|
39
|
+
// 如果不是api接口,则校验签名
|
|
40
|
+
// 取header里的sign字段
|
|
41
|
+
const sign = req.headers['sign'];
|
|
42
|
+
if (sign) {
|
|
43
|
+
const decodedTime = verifySign(sign);
|
|
44
|
+
const currentTime = Date.now();
|
|
45
|
+
|
|
46
|
+
if (decodedTime === null || Math.abs(currentTime - decodedTime) > setting.serverSignExpire) {
|
|
47
|
+
res.writeHead(403, {'Content-Type': 'text/plain'});
|
|
48
|
+
res.end('Forbidden: Invalid Sign');
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
} else {
|
|
52
|
+
// 如果没有sign字段,返回403
|
|
53
|
+
res.writeHead(403, {'Content-Type': 'text/plain'});
|
|
54
|
+
res.end('Forbidden: Missing Sign');
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// 如果签名通过,继续处理请求
|
|
59
|
+
try {
|
|
60
|
+
let body = '';
|
|
61
|
+
await new Promise((resolve, reject) => {
|
|
62
|
+
// 监听请求数据
|
|
63
|
+
req.on('data', chunk => {
|
|
64
|
+
body += chunk;
|
|
65
|
+
});
|
|
66
|
+
req.on('error', (err) => {
|
|
67
|
+
reject(err)
|
|
68
|
+
});
|
|
69
|
+
req.on('end', async () => {
|
|
70
|
+
resolve()
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
await routes[routeKey](body || params, res);
|
|
74
|
+
} catch (e) {
|
|
75
|
+
res.writeHead(500, {'Content-Type': 'text/plain'});
|
|
76
|
+
res.end(String(isError(e) ? e.message : e));
|
|
77
|
+
}
|
|
78
|
+
} else {
|
|
79
|
+
res.writeHead(404, {'Content-Type': 'text/plain'});
|
|
80
|
+
res.end('Not Found');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
36
84
|
// 创建一个 HTTP 服务
|
|
37
85
|
async function createServer(port) {
|
|
38
86
|
const server = http.createServer(async (req, res) => {
|
|
@@ -51,52 +99,11 @@ async function createServer(port) {
|
|
|
51
99
|
const clientIp = req.socket.remoteAddress || req.connection.remoteAddress;
|
|
52
100
|
// 查找对应的路由处理函数
|
|
53
101
|
const routeKey = `${method} ${url}`;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
// 取header里的sign字段
|
|
60
|
-
const sign = req.headers['sign'];
|
|
61
|
-
if (sign) {
|
|
62
|
-
const decodedTime = verifySign(sign);
|
|
63
|
-
const currentTime = Date.now();
|
|
64
|
-
|
|
65
|
-
if (decodedTime === null || Math.abs(currentTime - decodedTime) > setting.serverSignExpire) {
|
|
66
|
-
res.writeHead(403, { 'Content-Type': 'text/plain' });
|
|
67
|
-
res.end('Forbidden: Invalid Sign');
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
} else {
|
|
71
|
-
// 如果没有sign字段,返回403
|
|
72
|
-
res.writeHead(403, { 'Content-Type': 'text/plain' });
|
|
73
|
-
res.end('Forbidden: Missing Sign');
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
// 如果签名通过,继续处理请求
|
|
78
|
-
try {
|
|
79
|
-
let body = '';
|
|
80
|
-
await new Promise((resolve, reject) => {
|
|
81
|
-
// 监听请求数据
|
|
82
|
-
req.on('data', chunk => {
|
|
83
|
-
body += chunk;
|
|
84
|
-
});
|
|
85
|
-
req.on('error', (err) => {
|
|
86
|
-
reject(err)
|
|
87
|
-
});
|
|
88
|
-
req.on('end', async () => {
|
|
89
|
-
resolve()
|
|
90
|
-
})
|
|
91
|
-
})
|
|
92
|
-
await routes[routeKey](body || params, res);
|
|
93
|
-
} catch (e) {
|
|
94
|
-
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
|
95
|
-
res.end(String(isError(e) ? e.message:e));
|
|
96
|
-
}
|
|
97
|
-
} else {
|
|
98
|
-
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
99
|
-
res.end('Not Found');
|
|
102
|
+
let time = Date.now();
|
|
103
|
+
try {
|
|
104
|
+
await process(routeKey, req, res, params);
|
|
105
|
+
} finally {
|
|
106
|
+
debug(`Process cost ${Date.now() - time}ms for ${routeKey} from ${clientIp}`);
|
|
100
107
|
}
|
|
101
108
|
});
|
|
102
109
|
|
package/deps/util.js
CHANGED
|
@@ -96,13 +96,23 @@ function syncQueue(task, key) {
|
|
|
96
96
|
return queue.enqueue(task)
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
const
|
|
99
|
+
const roomLog = createLimitLogger(`room.log`, {
|
|
100
100
|
logInfo: false
|
|
101
101
|
});
|
|
102
|
+
const roomError = createLimitLogger(`room.error`, {
|
|
103
|
+
logInfo: false,
|
|
104
|
+
error: true,
|
|
105
|
+
syncLogs: [roomLog]
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const $log = createLimitLogger(`${setting.name}.log`, {
|
|
109
|
+
logInfo: false,
|
|
110
|
+
syncLogs: [roomLog]
|
|
111
|
+
});
|
|
102
112
|
const $error = createLimitLogger(`${setting.name}.error`, {
|
|
103
113
|
logInfo: false,
|
|
104
114
|
error: true,
|
|
105
|
-
syncLogs: [$log]
|
|
115
|
+
syncLogs: [$log, roomError]
|
|
106
116
|
});
|
|
107
117
|
let _globalDraft= createLimitLogger(`draft.log`, {
|
|
108
118
|
logInfo: false,
|
|
@@ -236,12 +246,13 @@ function createConsole(uniqueName) {
|
|
|
236
246
|
let pair = parseUniqueName(uniqueName)
|
|
237
247
|
let fileName = pair[0] + '/' + pair[1].split(".")[0]
|
|
238
248
|
result.$log = createLimitLogger(fileName + '.log', {
|
|
239
|
-
logInfo: false
|
|
249
|
+
logInfo: false,
|
|
250
|
+
syncLogs: [roomLog]
|
|
240
251
|
});
|
|
241
252
|
result.$error = createLimitLogger(fileName + '.error', {
|
|
242
253
|
logInfo: false,
|
|
243
254
|
error: true,
|
|
244
|
-
syncLogs: [result.$log]
|
|
255
|
+
syncLogs: [result.$log, roomError]
|
|
245
256
|
});
|
|
246
257
|
}
|
|
247
258
|
let quite = false
|
|
@@ -2078,7 +2089,7 @@ function interceptStdStreams() {
|
|
|
2078
2089
|
if(setting.enterOutputs) {
|
|
2079
2090
|
setting.enterOutputs.push(chunk.toString());
|
|
2080
2091
|
} else if (isRunningInBackground()) {
|
|
2081
|
-
console.$log(
|
|
2092
|
+
console.$log(chunk.toString().trimEnd());
|
|
2082
2093
|
} else {
|
|
2083
2094
|
originalStdoutWrite(chunk, ...args); // 保留原始行为
|
|
2084
2095
|
}
|
|
@@ -2089,7 +2100,7 @@ function interceptStdStreams() {
|
|
|
2089
2100
|
if(setting.enterOutputs) {
|
|
2090
2101
|
setting.enterOutputs.push(chunk.toString());
|
|
2091
2102
|
} else if (isRunningInBackground()) {
|
|
2092
|
-
console.$error(
|
|
2103
|
+
console.$error(chunk.toString().trimEnd());
|
|
2093
2104
|
} else {
|
|
2094
2105
|
originalStderrWrite(chunk, ...args); // 保留原始行为
|
|
2095
2106
|
}
|