From 70d1c8d44ee15a914f1367ed3ae6afbf400d0319 Mon Sep 17 00:00:00 2001 From: dds_feng Date: Tue, 22 May 2018 22:04:44 +0800 Subject: [PATCH 001/163] =?UTF-8?q?=E6=80=A7=E8=83=BD=E4=BC=98=E5=8C=96=20?= =?UTF-8?q?websocket=20upgrade=20=E7=9A=84=E8=AF=B7=E6=B1=82=E4=B8=8D?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E5=AF=B9=20req=20=E8=BF=9B=E8=A1=8C=E5=8C=85?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/proxy/http.proxy.js | 91 +++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 45 deletions(-) diff --git a/bin/proxy/http.proxy.js b/bin/proxy/http.proxy.js index b922b04c..b1bb68fc 100644 --- a/bin/proxy/http.proxy.js +++ b/bin/proxy/http.proxy.js @@ -47,7 +47,7 @@ var heartBeatCount = 0; function doRoute(req,res){ - + if(routeCache === null){ routeCache = require('./http.route.js'); config = require('./config.js'); @@ -123,25 +123,25 @@ process.on('top100',function(e){ process.on('heapdump',function(e){ logger.info('heapdump'); - + if(!isWindows){ - + require('heapdump').writeSnapshot(__dirname + '/cpu' + serverInfo.cpu + '.' + Date.now() + '.heapsnapshot',function(err, filename) { logger.info('dump written to ${filename}', { filename: filename }); }); - + } - + }); process.on('profiler',function(data = {}){ logger.info('profiler time: ${time}', data); - + if(!isWindows){ - + require('util/v8-profiler.js').writeProfilerOpt(__dirname + '/cpu' + serverInfo.cpu + '.' + Date.now() + '.cpuprofile', { recordTime: data.time || 5000 }, function(filename) { @@ -149,36 +149,36 @@ process.on('profiler',function(data = {}){ filename: filename }); }); - + } - + }); //process.emit('globaldump',m.GET); process.on('globaldump',function(GET){ - + var cpu = GET.cpu || 0; var depth = GET.depth || 6; - + if(cpu != serverInfo.cpu){ return; } - + var filename = __dirname + '/cpu' + serverInfo.cpu + '.' + Date.now() + '.globaldump'; - + logger.info('globaldump'); logger.info(GET); logger.info(filename); - - + + var str = util.inspect(global,{ depth: depth }); - + fs.writeFile(filename,str,'UTF-8',function(){ logger.info('globaldump finish'); }); - + }); function requestHandler(req, res){ @@ -198,17 +198,18 @@ function requestHandler(req, res){ cleanCache(); } + if('upgrade' === req.headers.connection && 'websocket' === req.headers.upgrade) { + //websocket + return; + } + res.flush = res.flush || function(){return true;}; //解析get参数 parseGet(req); - if('upgrade' === req.headers.connection && 'websocket' === req.headers.upgrade) { - //websocket - }else{ - //HTTP路由 - doRoute(req,res); - } + //HTTP路由 + doRoute(req,res); } @@ -225,7 +226,7 @@ global.TSW_HTTP_SERVER = server; if(config.httpsOptions){ serverHttps = https.createServer(config.httpsOptions,function(req,res){ req.headers['x-client-proto'] = 'https'; - + requestHandler(req,res); }); @@ -253,11 +254,11 @@ process.on('message',function(m){ }); function startHeartBeat(){ - + if(isStartHeartBeat){ return; } - + isStartHeartBeat = true; global.cpuUsed = 0; @@ -282,13 +283,13 @@ function startHeartBeat(){ global.cpuUsed = cpuUtil.getCpuUsed(serverInfo.cpu); tnm2.Attr_API_Set('AVG_TSW_CPU_USED', global.cpuUsed); - + if(global.cpuUsed >= 80) { global.cpuUsed80 = ~~global.cpuUsed80 + 1; }else{ global.cpuUsed80 = 0; } - + var cpuUsed = global.cpuUsed; //高负载告警 @@ -330,7 +331,7 @@ function startHeartBeat(){ if(data.ownerMain){ owner = [owner,data.ownerMain].join(';'); } - + if(data.ownerBack){ owner = [owner,data.ownerBack].join(';'); } @@ -364,66 +365,66 @@ function startHeartBeat(){ tnm2.Attr_API_Set('AVG_TSW_MEMORY_EXTERNAL', currMemory.external); },5000); - + } //restart methodMap.restart = function(){ - + logger.info('cpu: ${cpu} restart',serverInfo); - + process.emit('restart'); }; //reload methodMap.reload = function(){ - + logger.info('cpu: ${cpu} reload',serverInfo); - + process.emit('reload'); }; //heapdump methodMap.heapdump = function(m){ - + logger.info('cpu: ${cpu} heapdump',serverInfo); - + process.emit('heapdump',m.GET); }; //profiler methodMap.profiler = function(m){ - + logger.info('cpu: ${cpu} profiler',serverInfo); - + process.emit('profiler',m.GET); }; //globaldump methodMap.globaldump = function(m){ - + logger.info('cpu: ${cpu} globaldump',serverInfo); - + process.emit('globaldump',m.GET); }; //top100 methodMap.top100 = function(m){ - + logger.info('cpu: ${cpu} top100',serverInfo); - + process.emit('top100',m.GET); }; -//监听端口 +//监听端口 methodMap.listen = function(message){ - + var user_00 = config.workerUid || 'user_00'; serverInfo.cpu = message.cpu || 0; global.cpuUsed = cpuUtil.getCpuUsed(serverInfo.cpu); - + process.title = 'TSW/worker/' + serverInfo.cpu; global.TSW_HTTP_WORKER_PORT = config.workerPortBase + serverInfo.cpu; From dadecbcdf0ae3bd2630cda5c3860101c522341b9 Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 11:38:15 +0800 Subject: [PATCH 002/163] ignore nyc_output --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 267ef05d..b35fb141 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules .vscode .idea .DS_Store +.nyc_output From e4ed7367eaf5f710ff98e4aeb8bfabeeb748c853 Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 11:51:20 +0800 Subject: [PATCH 003/163] ignore conf & log --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index b35fb141..40d6387f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ node_modules .idea .DS_Store .nyc_output +conf +log From 082cef052c6deaf2fc6194282660cc23a3f206f6 Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 17:46:19 +0800 Subject: [PATCH 004/163] =?UTF-8?q?=E7=94=9F=E6=88=90lcov=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E8=A6=86=E7=9B=96=E7=8E=87=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 +++-- package.json | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 40d6387f..99df75d6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ node_modules .idea .DS_Store .nyc_output -conf -log +/conf +/coverage +/log diff --git a/package.json b/package.json index 517006f1..dbb60218 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "lint": "eslint examples bin/wwwroot bin/proxy bin/tsw bin/lib test --fix", "precommit": "lint-staged", "test": "mocha --recursive test/bin", - "coverage": "nyc mocha --recursive test/bin" + "coverage": "nyc --reporter=lcov --reporter=text-lcov npm test" }, "repository": { "type": "git", From 469e6349239215a3381dbba8df17201265f5657f Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 17:51:49 +0800 Subject: [PATCH 005/163] =?UTF-8?q?coveralls=20=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .travis.yml | 4 +++- package.json | 14 ++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7c525c7f..7370355b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,4 +6,6 @@ node_js: script: - npm run lint - - npm test && npm run coverage \ No newline at end of file + - npm run coverage +after_success: + - npm run coveralls \ No newline at end of file diff --git a/package.json b/package.json index dbb60218..296a0e9c 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "lint": "eslint examples bin/wwwroot bin/proxy bin/tsw bin/lib test --fix", "precommit": "lint-staged", "test": "mocha --recursive test/bin", - "coverage": "nyc --reporter=lcov --reporter=text-lcov npm test" + "coverage": "nyc --reporter=lcov --reporter=text-lcov npm test", + "coveralls": "cat ./coverage/lcov.info | coveralls" }, "repository": { "type": "git", @@ -19,14 +20,15 @@ "memcached": "~0.2.8" }, "devDependencies": { - "plug": "file:./bin/plug", "chai": "^4.1.2", - "mocha": "^5.0.5", - "nyc": "^11.6.0", - "sinon": "^4.1.2", + "coveralls": "^3.0.1", "eslint": "^4.10.0", "husky": "^0.14.3", - "lint-staged": "^7.0.0" + "lint-staged": "^7.0.0", + "mocha": "^5.0.5", + "nyc": "^11.6.0", + "plug": "file:./bin/plug", + "sinon": "^4.1.2" }, "optionalDependencies": { "heapdump": "^0.3.9", From 740f28664783351b7d6d0e7ef051755953d8d06a Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 18:00:46 +0800 Subject: [PATCH 006/163] only test js --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 296a0e9c..7ab36ec6 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "scripts": { "lint": "eslint examples bin/wwwroot bin/proxy bin/tsw bin/lib test --fix", "precommit": "lint-staged", - "test": "mocha --recursive test/bin", + "test": "mocha test/**/*.js", "coverage": "nyc --reporter=lcov --reporter=text-lcov npm test", "coveralls": "cat ./coverage/lcov.info | coveralls" }, From 32229c35f3cb649a54fe5766bf138f01369527c1 Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 18:08:01 +0800 Subject: [PATCH 007/163] --recursive --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 7ab36ec6..ab9a361e 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,9 @@ "scripts": { "lint": "eslint examples bin/wwwroot bin/proxy bin/tsw bin/lib test --fix", "precommit": "lint-staged", - "test": "mocha test/**/*.js", + "test": "mocha --recursive test/bin", "coverage": "nyc --reporter=lcov --reporter=text-lcov npm test", - "coveralls": "cat ./coverage/lcov.info | coveralls" + "coveralls": "coveralls" }, "repository": { "type": "git", From 6ed2867580e506586d64110e2f4d52c3e4a66262 Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 18:09:48 +0800 Subject: [PATCH 008/163] =?UTF-8?q?CI=20=E5=8A=A0=E9=80=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .travis.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7370355b..5f50d21a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,10 @@ sudo: false language: node_js node_js: - - "8" - "9" - +cache: + directories: + - node_modules script: - npm run lint - npm run coverage From 8bf5dcb43be1be5e89266182923fb48df45d84e5 Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 18:14:09 +0800 Subject: [PATCH 009/163] bugfix --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ab9a361e..296a0e9c 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "precommit": "lint-staged", "test": "mocha --recursive test/bin", "coverage": "nyc --reporter=lcov --reporter=text-lcov npm test", - "coveralls": "coveralls" + "coveralls": "cat ./coverage/lcov.info | coveralls" }, "repository": { "type": "git", From 1ad131584d9e1e5c69393f997b141fdb5a93a7b6 Mon Sep 17 00:00:00 2001 From: youkunhuang Date: Wed, 23 May 2018 18:18:49 +0800 Subject: [PATCH 010/163] =?UTF-8?q?<=3D0=20=E6=9B=B4=E6=96=B0=E4=B8=BA=20?= =?UTF-8?q?=3D=3D=3D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/tsw/util/Queue/index.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/bin/tsw/util/Queue/index.js b/bin/tsw/util/Queue/index.js index 3305745c..7324fef3 100644 --- a/bin/tsw/util/Queue/index.js +++ b/bin/tsw/util/Queue/index.js @@ -40,11 +40,11 @@ Queue.prototype.dequeue = function(){ var domain,that,fn; - if(this._queue.length <= 0){ + if(this._queue.length === 0){ return this; } - if(this._queue[0] === 'ing'){ + if(this._queue[0] === 'pending'){ this._queue.shift(); this.dequeue(); @@ -53,15 +53,14 @@ Queue.prototype.dequeue = function(){ } fn = this._queue[0]; - this._queue[0] = 'ing'; + this._queue[0] = 'pending'; domain = fn._domain; - that = this; fn._domain = undefined; if(domain && domain !== process.domain){ - domain.run(function(){ - fn.call(that); + domain.run(() => { + fn.call(this); }); }else{ fn.call(this); From f630c14a975b4685abf17bb5b98143c8b36176d8 Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 19:38:27 +0800 Subject: [PATCH 011/163] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/tsw/util/Queue/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/tsw/util/Queue/index.js b/bin/tsw/util/Queue/index.js index 7324fef3..b2594bb5 100644 --- a/bin/tsw/util/Queue/index.js +++ b/bin/tsw/util/Queue/index.js @@ -38,7 +38,7 @@ Queue.prototype.queue = function(fn){ Queue.prototype.dequeue = function(){ - var domain,that,fn; + var domain,fn; if(this._queue.length === 0){ return this; From e4ccecac304048fd3a0d786355a96562b66fb0fb Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 17:46:19 +0800 Subject: [PATCH 012/163] =?UTF-8?q?=E7=94=9F=E6=88=90lcov=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E8=A6=86=E7=9B=96=E7=8E=87=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 +++-- package.json | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 40d6387f..99df75d6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ node_modules .idea .DS_Store .nyc_output -conf -log +/conf +/coverage +/log diff --git a/package.json b/package.json index 517006f1..dbb60218 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "lint": "eslint examples bin/wwwroot bin/proxy bin/tsw bin/lib test --fix", "precommit": "lint-staged", "test": "mocha --recursive test/bin", - "coverage": "nyc mocha --recursive test/bin" + "coverage": "nyc --reporter=lcov --reporter=text-lcov npm test" }, "repository": { "type": "git", From c979d085705b410d5752c328839baaf4ddc76965 Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 17:51:49 +0800 Subject: [PATCH 013/163] =?UTF-8?q?coveralls=20=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .travis.yml | 4 +++- package.json | 14 ++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7c525c7f..7370355b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,4 +6,6 @@ node_js: script: - npm run lint - - npm test && npm run coverage \ No newline at end of file + - npm run coverage +after_success: + - npm run coveralls \ No newline at end of file diff --git a/package.json b/package.json index dbb60218..296a0e9c 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "lint": "eslint examples bin/wwwroot bin/proxy bin/tsw bin/lib test --fix", "precommit": "lint-staged", "test": "mocha --recursive test/bin", - "coverage": "nyc --reporter=lcov --reporter=text-lcov npm test" + "coverage": "nyc --reporter=lcov --reporter=text-lcov npm test", + "coveralls": "cat ./coverage/lcov.info | coveralls" }, "repository": { "type": "git", @@ -19,14 +20,15 @@ "memcached": "~0.2.8" }, "devDependencies": { - "plug": "file:./bin/plug", "chai": "^4.1.2", - "mocha": "^5.0.5", - "nyc": "^11.6.0", - "sinon": "^4.1.2", + "coveralls": "^3.0.1", "eslint": "^4.10.0", "husky": "^0.14.3", - "lint-staged": "^7.0.0" + "lint-staged": "^7.0.0", + "mocha": "^5.0.5", + "nyc": "^11.6.0", + "plug": "file:./bin/plug", + "sinon": "^4.1.2" }, "optionalDependencies": { "heapdump": "^0.3.9", From 5ccc7094e8a6e6da73d06bd80537bd687db1f455 Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 18:00:46 +0800 Subject: [PATCH 014/163] only test js --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 296a0e9c..7ab36ec6 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "scripts": { "lint": "eslint examples bin/wwwroot bin/proxy bin/tsw bin/lib test --fix", "precommit": "lint-staged", - "test": "mocha --recursive test/bin", + "test": "mocha test/**/*.js", "coverage": "nyc --reporter=lcov --reporter=text-lcov npm test", "coveralls": "cat ./coverage/lcov.info | coveralls" }, From add17e46a362c39c0beacd7578db2c00e05784e0 Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 18:08:01 +0800 Subject: [PATCH 015/163] --recursive --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 7ab36ec6..ab9a361e 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,9 @@ "scripts": { "lint": "eslint examples bin/wwwroot bin/proxy bin/tsw bin/lib test --fix", "precommit": "lint-staged", - "test": "mocha test/**/*.js", + "test": "mocha --recursive test/bin", "coverage": "nyc --reporter=lcov --reporter=text-lcov npm test", - "coveralls": "cat ./coverage/lcov.info | coveralls" + "coveralls": "coveralls" }, "repository": { "type": "git", From f3ba3427184cbaf19a7aed4cfde03711758a1b47 Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 18:09:48 +0800 Subject: [PATCH 016/163] =?UTF-8?q?CI=20=E5=8A=A0=E9=80=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .travis.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7370355b..5f50d21a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,10 @@ sudo: false language: node_js node_js: - - "8" - "9" - +cache: + directories: + - node_modules script: - npm run lint - npm run coverage From f13e3f86cb2f816eb7eb7818ce24cc77f4be81f7 Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 18:14:09 +0800 Subject: [PATCH 017/163] bugfix --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ab9a361e..296a0e9c 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "precommit": "lint-staged", "test": "mocha --recursive test/bin", "coverage": "nyc --reporter=lcov --reporter=text-lcov npm test", - "coveralls": "coveralls" + "coveralls": "cat ./coverage/lcov.info | coveralls" }, "repository": { "type": "git", From 32f65174aa71feaa2cbce9b3118033babe1eb45f Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 19:38:27 +0800 Subject: [PATCH 018/163] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/tsw/util/Queue/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/tsw/util/Queue/index.js b/bin/tsw/util/Queue/index.js index 7324fef3..b2594bb5 100644 --- a/bin/tsw/util/Queue/index.js +++ b/bin/tsw/util/Queue/index.js @@ -38,7 +38,7 @@ Queue.prototype.queue = function(fn){ Queue.prototype.dequeue = function(){ - var domain,that,fn; + var domain,fn; if(this._queue.length === 0){ return this; From 9bf18a2bb9a23e8abe43c80b0886029a879abcb7 Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 19:53:59 +0800 Subject: [PATCH 019/163] =?UTF-8?q?=E8=A6=86=E7=9B=96=E7=8E=87=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=8E=BB=E6=8E=89=E6=89=93=E5=8D=B0text-lcov?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 296a0e9c..7d692981 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "lint": "eslint examples bin/wwwroot bin/proxy bin/tsw bin/lib test --fix", "precommit": "lint-staged", "test": "mocha --recursive test/bin", - "coverage": "nyc --reporter=lcov --reporter=text-lcov npm test", + "coverage": "nyc --reporter=lcov npm test", "coveralls": "cat ./coverage/lcov.info | coveralls" }, "repository": { From 409b53af25c42deb362ffc43cd2d15d537f760f8 Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 20:05:16 +0800 Subject: [PATCH 020/163] update .gitignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 只忽略顶层node_modules 2. 覆盖率测试打印下结果 --- .gitignore | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 99df75d6..b343e80c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -node_modules +/node_modules .vscode .idea .DS_Store diff --git a/package.json b/package.json index 7d692981..2b5cd80f 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "lint": "eslint examples bin/wwwroot bin/proxy bin/tsw bin/lib test --fix", "precommit": "lint-staged", "test": "mocha --recursive test/bin", - "coverage": "nyc --reporter=lcov npm test", + "coverage": "nyc --reporter=lcov --reporter=text npm test", "coveralls": "cat ./coverage/lcov.info | coveralls" }, "repository": { From fe60585cf4c072673444a6c5c4b94ad917f0bf88 Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 23 May 2018 20:19:46 +0800 Subject: [PATCH 021/163] change Codecov (#2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 生成lcov格式覆盖率结果 * coveralls 支持 * only test js * --recursive * CI 加速 * bugfix * 删除没有使用的变量 * 覆盖率测试去掉打印text-lcov * update .gitignore 1. 只忽略顶层node_modules 2. 覆盖率测试打印下结果 --- .gitignore | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 99df75d6..b343e80c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -node_modules +/node_modules .vscode .idea .DS_Store diff --git a/package.json b/package.json index 296a0e9c..2b5cd80f 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "lint": "eslint examples bin/wwwroot bin/proxy bin/tsw bin/lib test --fix", "precommit": "lint-staged", "test": "mocha --recursive test/bin", - "coverage": "nyc --reporter=lcov --reporter=text-lcov npm test", + "coverage": "nyc --reporter=lcov --reporter=text npm test", "coveralls": "cat ./coverage/lcov.info | coveralls" }, "repository": { From ad8dae8cdf4a4ddbd188c2d0574a20160c7cc0db Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 20:30:57 +0800 Subject: [PATCH 022/163] =?UTF-8?q?coveralls=20=E6=9B=B4=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?=20codecov?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .travis.yml | 4 ++-- package.json | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5f50d21a..51d19219 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,6 @@ cache: - node_modules script: - npm run lint - - npm run coverage + - npm test after_success: - - npm run coveralls \ No newline at end of file + - npm run coverage \ No newline at end of file diff --git a/package.json b/package.json index 2b5cd80f..5198d820 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,7 @@ "lint": "eslint examples bin/wwwroot bin/proxy bin/tsw bin/lib test --fix", "precommit": "lint-staged", "test": "mocha --recursive test/bin", - "coverage": "nyc --reporter=lcov --reporter=text npm test", - "coveralls": "cat ./coverage/lcov.info | coveralls" + "coverage": "nyc --reporter=lcov --reporter=text npm test && codecov" }, "repository": { "type": "git", @@ -21,7 +20,7 @@ }, "devDependencies": { "chai": "^4.1.2", - "coveralls": "^3.0.1", + "codecov": "^3.0.2", "eslint": "^4.10.0", "husky": "^0.14.3", "lint-staged": "^7.0.0", From 4681c9774b7ee015fc24a7d7ed0aee3529271a20 Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 23 May 2018 20:34:24 +0800 Subject: [PATCH 023/163] =?UTF-8?q?=E5=86=B2=E7=AA=81=E8=A7=A3=E5=86=B3=20?= =?UTF-8?q?(#3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 生成lcov格式覆盖率结果 * coveralls 支持 * only test js * --recursive * CI 加速 * bugfix * 删除没有使用的变量 * 覆盖率测试去掉打印text-lcov * update .gitignore 1. 只忽略顶层node_modules 2. 覆盖率测试打印下结果 From 6e354b82f20a3795bb0301de371a44dd461cce5f Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 20:50:21 +0800 Subject: [PATCH 024/163] =?UTF-8?q?=E7=A8=B3=E5=AE=9A=E7=89=88=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 51d19219..93e4882d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ sudo: false language: node_js node_js: + - "8" - "9" cache: directories: From 4b48549c5f06ea8648cd565d590e0c38ed4965ca Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 21:01:44 +0800 Subject: [PATCH 025/163] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 覆盖率检查包含单元测试 --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 51d19219..fb92f03f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,11 @@ sudo: false language: node_js node_js: + - "8" - "9" cache: directories: - node_modules script: - npm run lint - - npm test -after_success: - npm run coverage \ No newline at end of file From b27a108ff77c6c67cf1a8e108be5fce3cf25f18b Mon Sep 17 00:00:00 2001 From: youkunhuang Date: Wed, 23 May 2018 21:22:47 +0800 Subject: [PATCH 026/163] =?UTF-8?q?listening=E6=8B=BC=E5=86=99=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/proxy/http.proxy.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/proxy/http.proxy.js b/bin/proxy/http.proxy.js index b1bb68fc..02044a98 100644 --- a/bin/proxy/http.proxy.js +++ b/bin/proxy/http.proxy.js @@ -543,11 +543,11 @@ methodMap.listen = function(message){ if(cluster.isMaster){ if(isWindows){ - logger.info('isWindows, start litening'); + logger.info('isWindows, start listening'); methodMap.listen({cpu : 0}); }else if(debugOptions && debugOptions.inspectorEnabled){ logger.setLogLevel('debug'); - logger.info('inspectorEnabled, start litening'); + logger.info('inspectorEnabled, start listening'); methodMap.listen({cpu : 0}); } } From b5ee12cd1aa0c8edd096a3b954ba90da215e820e Mon Sep 17 00:00:00 2001 From: neilcui Date: Wed, 23 May 2018 21:43:56 +0800 Subject: [PATCH 027/163] eslint: end_of_line --- .eslintignore | 2 +- .travis.yml | 18 +++--- LICENSE | 8 +-- README.md | 166 +++++++++++++++++++++++++------------------------- README_en.md | 156 +++++++++++++++++++++++------------------------ index.js | 8 +-- package.json | 2 +- 7 files changed, 180 insertions(+), 180 deletions(-) diff --git a/.eslintignore b/.eslintignore index 1e6079d2..4e4d2883 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,3 @@ bin/wwwroot/**/highlight-tsw.js bin/wwwroot/**/zepto.min.js -bin/lib/api/L5/L5.api.js \ No newline at end of file +bin/lib/api/L5/L5.api.js diff --git a/.travis.yml b/.travis.yml index 7c525c7f..81230975 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ -sudo: false -language: node_js -node_js: - - "8" - - "9" - -script: - - npm run lint - - npm test && npm run coverage \ No newline at end of file +sudo: false +language: node_js +node_js: + - "8" + - "9" + +script: + - npm run lint + - npm test && npm run coverage diff --git a/LICENSE b/LICENSE index 39f47434..79c16d9b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Tencent is pleased to support the open source community by making Tencent Server Web available. +Tencent is pleased to support the open source community by making Tencent Server Web available. Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved. If you have downloaded a copy of the Tencent Server Web binary from Tencent, please note that the Tencent Server Web binary is licensed under the MIT License. If you have downloaded a copy of the Tencent Server Web source code from Tencent, please note that Tencent Server Web source code is licensed under the MIT License, except for the third-party components listed below which are subject to different license terms. Your integration of Tencent Server Web into your own projects may require compliance with the MIT License, as well as the other licenses applicable to the third-party components included within Tencent Server Web. @@ -6,7 +6,7 @@ A copy of the MIT License is included in this file. Other dependencies and licenses: -Open Source Software Licensed Under the BSD 3-Clause License: +Open Source Software Licensed Under the BSD 3-Clause License: ---------------------------------------------------------------------------------------- 1. qs 6.5.0 Copyright (c) 2014 Nathan LaFreniere and other contributors. @@ -29,7 +29,7 @@ Redistribution and use in source and binary forms, with or without modification, THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -Open Source Software Licensed Under the MIT License: +Open Source Software Licensed Under the MIT License: ---------------------------------------------------------------------------------------- 1. node-archiver 0.16.0 Copyright (c) 2012-2014 Chris Talkington, contributors. @@ -48,7 +48,7 @@ Copyright (c) 2010 Arnout Kazemier,3rd-Eden 5. seajs 1.3.0 Copyright (c) 2012 Frank Wang, http://seajs.org/ -6. Semantic-UI 2.1.5 +6. Semantic-UI 2.1.5 Copyright 2014 Contributors 7. ws 2.2.1 diff --git a/README.md b/README.md index 72f3feec..91f54ab7 100644 --- a/README.md +++ b/README.md @@ -1,84 +1,84 @@ -# [Tencent Server Web](https://tswjs.org) - - -[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](./LICENSE) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/TSW/pulls) [![wiki](https://img.shields.io/badge/Wiki-open-brightgreen.svg)](https://tswjs.org/guide/index) ![node](https://img.shields.io/badge/node-%3E%3D8.0.0-green.svg) [![Build Status](https://travis-ci.org/Tencent/TSW.svg?branch=master)](https://travis-ci.org/Tencent/TSW) - ---- - - -[English Version](./README_en.md) - - -## 简介 - -Tencent Server Web(TSW),是一套面向WEB前端开发者,以提升问题定位效率为初衷,提供染色抓包、全息日志和异常发现的Node.js基础设施。TSW关注业务的运维监控能力,适用于http、websocket协议的业务场景,可无缝与即有项目进行整合。支持公有云、私有云和本地部署。 - -![tsw](./static/resource/structure.png) - -#### 染色抓包 - -TSW 支持用户维度的抓包 - -- 对于指定的用户(白名单内) -- 请求生命周期内,对请求本身及衍生请求进行抓包 -- 提供抓包查看、下载等功能 -- 抓包格式支持Fiddler和Charles,以及HAR - -#### 全息日志 - -TSW 提供用户维度的全息日志帮助开发者快速定位问题 - -- 对请求生命周期内的日志,采用全息的方式记录,形成流水 -- 流水按用户维度聚合 -- 提供查看功能,快速定位问题原因 - -#### 异常发现 - -- 内置指标实时监控 -- 代码异常推送告警 - -## 环境要求 - -- 操作系统: Windows/Mac/Linux -- Node.js: 8.0.0+ - -## 开始 - -1. 需先安装[Node.js](https://nodejs.org/en/download/),并且Node.js的版本需不低于8.0.0。 -1. 安装 -- `git clone https://github.com/Tencent/TSW.git` -1. 切换工作目录 -- `cd TSW` -1. 补全依赖 -- `npm install --no-optional` -1. 配置 -- 修改 `TSW/conf/config.js`,更多配置项参考 [手册](https://tswjs.org/doc/api/config) - - ```js - this.httpAddress = '0.0.0.0'; - - this.httpPort = 80; - ``` - -1. 启动 -- `node index.js` -1. 预览 -- 打开浏览器,访问 `http://127.0.0.1/` 即可 - -## 官方网站 - -- 更多教程 [https://tswjs.org/guide/index](https://tswjs.org/guide/index) - -- 使用手册 [https://tswjs.org/doc/api/index](https://tswjs.org/doc/api/index) - -## 配套设施 - -- TSW开放平台 [https://tswjs.org](https://tswjs.org) - -## 贡献代码 - -如果您使用过程中发现Bug,请通过 [https://github.com/Tencent/TSW/issues](https://github.com/Tencent/TSW/issues) 来提交并描述相关的问题,您也可以在这里查看其它的issue,通过解决这些issue来贡献代码。 - -如果您是第一次贡献代码,请阅读 [CONTRIBUTING](./CONTRIBUTING.md) 了解我们的贡献流程,并提交 pull request 给我们。 - -## 许可证 - -Tencent Server Web 的开源协议为 MIT, 详情参见 [LICENSE](./LICENSE) 。 - -## 联系方式 - +# [Tencent Server Web](https://tswjs.org) + + +[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](./LICENSE) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/TSW/pulls) [![wiki](https://img.shields.io/badge/Wiki-open-brightgreen.svg)](https://tswjs.org/guide/index) ![node](https://img.shields.io/badge/node-%3E%3D8.0.0-green.svg) [![Build Status](https://travis-ci.org/Tencent/TSW.svg?branch=master)](https://travis-ci.org/Tencent/TSW) + +--- + + +[English Version](./README_en.md) + + +## 简介 + +Tencent Server Web(TSW),是一套面向WEB前端开发者,以提升问题定位效率为初衷,提供染色抓包、全息日志和异常发现的Node.js基础设施。TSW关注业务的运维监控能力,适用于http、websocket协议的业务场景,可无缝与即有项目进行整合。支持公有云、私有云和本地部署。 + +![tsw](./static/resource/structure.png) + +#### 染色抓包 + +TSW 支持用户维度的抓包 + +- 对于指定的用户(白名单内) +- 请求生命周期内,对请求本身及衍生请求进行抓包 +- 提供抓包查看、下载等功能 +- 抓包格式支持Fiddler和Charles,以及HAR + +#### 全息日志 + +TSW 提供用户维度的全息日志帮助开发者快速定位问题 + +- 对请求生命周期内的日志,采用全息的方式记录,形成流水 +- 流水按用户维度聚合 +- 提供查看功能,快速定位问题原因 + +#### 异常发现 + +- 内置指标实时监控 +- 代码异常推送告警 + +## 环境要求 + +- 操作系统: Windows/Mac/Linux +- Node.js: 8.0.0+ + +## 开始 + +1. 需先安装[Node.js](https://nodejs.org/en/download/),并且Node.js的版本需不低于8.0.0。 +1. 安装 -- `git clone https://github.com/Tencent/TSW.git` +1. 切换工作目录 -- `cd TSW` +1. 补全依赖 -- `npm install --no-optional` +1. 配置 -- 修改 `TSW/conf/config.js`,更多配置项参考 [手册](https://tswjs.org/doc/api/config) + + ```js + this.httpAddress = '0.0.0.0'; + + this.httpPort = 80; + ``` + +1. 启动 -- `node index.js` +1. 预览 -- 打开浏览器,访问 `http://127.0.0.1/` 即可 + +## 官方网站 + +- 更多教程 [https://tswjs.org/guide/index](https://tswjs.org/guide/index) + +- 使用手册 [https://tswjs.org/doc/api/index](https://tswjs.org/doc/api/index) + +## 配套设施 + +- TSW开放平台 [https://tswjs.org](https://tswjs.org) + +## 贡献代码 + +如果您使用过程中发现Bug,请通过 [https://github.com/Tencent/TSW/issues](https://github.com/Tencent/TSW/issues) 来提交并描述相关的问题,您也可以在这里查看其它的issue,通过解决这些issue来贡献代码。 + +如果您是第一次贡献代码,请阅读 [CONTRIBUTING](./CONTRIBUTING.md) 了解我们的贡献流程,并提交 pull request 给我们。 + +## 许可证 + +Tencent Server Web 的开源协议为 MIT, 详情参见 [LICENSE](./LICENSE) 。 + +## 联系方式 + tsw@tencent.com \ No newline at end of file diff --git a/README_en.md b/README_en.md index 26186c4d..42071040 100644 --- a/README_en.md +++ b/README_en.md @@ -1,78 +1,78 @@ - -## Introduction - -Tencent Server side Web(TSW) is a Node.js infrastructure which is designed for improving the efficiency of locating problems, providing multiple functions for front-end developers: online dyeing/packet capture, holographic logging and abnormity discovery. TSW, focusing on the operation and maintenance of businesses, is suitable for web and mobile applications based on http or websocket protocols. It can be readily integrated with existing systems. - -![tsw](./static/resource/structure_en.png) - -#### Online dyeing/packet capture - -TSW supports packet capture based on user granularity. - -- Dye labelled user by users' feature IDs -- Collect packets within corresponding requests' lifecycles for dyed users. -- Provid functions to view and download captured packets. -- Support Fiddler(windows), Charles(mac) and HAR format. - -#### Holographic logging - -TSW provides powerful logging abilities for developers to locate problems quickly. - -- Record packets within requests' lifecycles holographically in the form of log flow. -- Aggregate log flows based on user granularity. -- View log flows by user features. Help analyzing and solving problems efficiently. - -#### Abnormity discovery - -- Monitor preset indicators in real time -- Push alarms for code exceptions - -## Requirement -- OS: Windows/Mac/Linux -- Node.js: 8.0.0+ - -## Getting started - -- Before installing, [download and install Node.js](https://nodejs.org/en/download/). Node.js 8.0.0 or higher is required. - -- Clone the repo: `git clone https://github.com/Tencent/TSW.git` - -- Enter the TSW: `cd TSW` - -- NPM : `npm install --no-optional` - -- Config ``TSW/conf/config.js`` - ```js - this.httpAddress = '0.0.0.0' - - this.httpPort = 80; - ``` - -- Run `node index.js` to start Node.js server - -- Open `http://127.0.0.1/` in your browser. - - -## Documentation - -- More Tutorials [https://tswjs.org/guide/index](https://tswjs.org/guide/index) - -- Manual [https://tswjs.org/doc/api/index](https://tswjs.org/doc/api/index) - -## Supporting facilities - -- TSW Open Platform [https://tswjs.org](https://tswjs.org) - -## Contributing - -Find a bug or have a feature request? Please read the [issues](https://github.com/Tencent/TSW/issues) guidelines and search for existing and closed issues. - -If your problem or idea is not addressed yet, please read through our [contributing guidelines](./CONTRIBUTING.md) and open a new [issues](https://github.com/Tencent/TSW/issues). - -## License - -Tencent Server Web is released under [MIT license](./LICENSE). - -## Contacts - -tsw@tencent.com + +## Introduction + +Tencent Server side Web(TSW) is a Node.js infrastructure which is designed for improving the efficiency of locating problems, providing multiple functions for front-end developers: online dyeing/packet capture, holographic logging and abnormity discovery. TSW, focusing on the operation and maintenance of businesses, is suitable for web and mobile applications based on http or websocket protocols. It can be readily integrated with existing systems. + +![tsw](./static/resource/structure_en.png) + +#### Online dyeing/packet capture + +TSW supports packet capture based on user granularity. + +- Dye labelled user by users' feature IDs +- Collect packets within corresponding requests' lifecycles for dyed users. +- Provid functions to view and download captured packets. +- Support Fiddler(windows), Charles(mac) and HAR format. + +#### Holographic logging + +TSW provides powerful logging abilities for developers to locate problems quickly. + +- Record packets within requests' lifecycles holographically in the form of log flow. +- Aggregate log flows based on user granularity. +- View log flows by user features. Help analyzing and solving problems efficiently. + +#### Abnormity discovery + +- Monitor preset indicators in real time +- Push alarms for code exceptions + +## Requirement +- OS: Windows/Mac/Linux +- Node.js: 8.0.0+ + +## Getting started + +- Before installing, [download and install Node.js](https://nodejs.org/en/download/). Node.js 8.0.0 or higher is required. + +- Clone the repo: `git clone https://github.com/Tencent/TSW.git` + +- Enter the TSW: `cd TSW` + +- NPM : `npm install --no-optional` + +- Config ``TSW/conf/config.js`` + ```js + this.httpAddress = '0.0.0.0' + + this.httpPort = 80; + ``` + +- Run `node index.js` to start Node.js server + +- Open `http://127.0.0.1/` in your browser. + + +## Documentation + +- More Tutorials [https://tswjs.org/guide/index](https://tswjs.org/guide/index) + +- Manual [https://tswjs.org/doc/api/index](https://tswjs.org/doc/api/index) + +## Supporting facilities + +- TSW Open Platform [https://tswjs.org](https://tswjs.org) + +## Contributing + +Find a bug or have a feature request? Please read the [issues](https://github.com/Tencent/TSW/issues) guidelines and search for existing and closed issues. + +If your problem or idea is not addressed yet, please read through our [contributing guidelines](./CONTRIBUTING.md) and open a new [issues](https://github.com/Tencent/TSW/issues). + +## License + +Tencent Server Web is released under [MIT license](./LICENSE). + +## Contacts + +tsw@tencent.com diff --git a/index.js b/index.js index bc494f5b..6961c4b4 100644 --- a/index.js +++ b/index.js @@ -5,10 +5,10 @@ * http://opensource.org/licenses/MIT * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -"use strict"; +'use strict'; if(process.mainModule === module){ - require('./bin/proxy/index.js'); + require('./bin/proxy/index.js'); }else{ - module.exports = require('./plug.js'); -} \ No newline at end of file + module.exports = require('./plug.js'); +} diff --git a/package.json b/package.json index 517006f1..86216014 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "TSW", "version": "1.0.1449", "scripts": { - "lint": "eslint examples bin/wwwroot bin/proxy bin/tsw bin/lib test --fix", + "lint": "eslint examples bin test --fix", "precommit": "lint-staged", "test": "mocha --recursive test/bin", "coverage": "nyc mocha --recursive test/bin" From 42c5c47b313f7edb275390265e78ce57b167c5a3 Mon Sep 17 00:00:00 2001 From: youkunhuang Date: Wed, 23 May 2018 21:51:20 +0800 Subject: [PATCH 028/163] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/tsw/util/logger/index.js | 2 +- bin/tsw/util/logger/logger.config.js | 2 +- bin/tsw/util/logger/logger.js | 253 ++++++++++++++------------- 3 files changed, 129 insertions(+), 128 deletions(-) diff --git a/bin/tsw/util/logger/index.js b/bin/tsw/util/logger/index.js index e194ced6..f35a7210 100644 --- a/bin/tsw/util/logger/index.js +++ b/bin/tsw/util/logger/index.js @@ -7,4 +7,4 @@ */ 'use strict'; -module.exports = require('./logger.js'); \ No newline at end of file +module.exports = require('./logger.js'); diff --git a/bin/tsw/util/logger/logger.config.js b/bin/tsw/util/logger/logger.config.js index 5d78c328..882949fe 100644 --- a/bin/tsw/util/logger/logger.config.js +++ b/bin/tsw/util/logger/logger.config.js @@ -48,4 +48,4 @@ this.getLogLevel = function(){ } return this.logLevel; -}; \ No newline at end of file +}; diff --git a/bin/tsw/util/logger/logger.js b/bin/tsw/util/logger/logger.js index 8b5fed3e..74e41156 100644 --- a/bin/tsw/util/logger/logger.js +++ b/bin/tsw/util/logger/logger.js @@ -15,8 +15,8 @@ const {isWindows} = require('util/isWindows.js'); const tnm2 = require('api/tnm2'); const canIuse = /^[0-9a-zA-Z_-]{0,64}$/; const cache = global[__filename] || { - filterWatcher: null -}; + filterWatcher: null + }; const freqCache = { clearTime: 0, count: 0, @@ -43,29 +43,29 @@ module.exports.Logger = Logger; function Logger(){ - + return this; } Logger.prototype = { - + occurError: function(){ - + var curr = contextMod.currentContext(); - + if(curr.window && curr.window.request){ curr.log = curr.log || {}; } - + if(curr.log){ curr.log.showLineNumber = true; } - + }, - + getLog: function(){ var log = contextMod.currentContext().log || null; - + return log; }, @@ -78,18 +78,18 @@ Logger.prototype = { contextMod.currentContext().log = null; }, - + getJson: function(){ var log = this.getLog(); var json = { curr: {}, ajax: [] }; - + if(log === null){ return null; } - + if(log.json){ json = log.json; }else{ @@ -98,23 +98,23 @@ Logger.prototype = { return json; }, - + getText: function(){ var log = this.getLog(); var arr = []; - + if(log && log.arr){ - + log.arr.forEach(function(fn){ arr.push(fn()); }); - + return arr.join('\n'); } return ''; }, - + setKey: function(key){ if(!canIuse.test(key)){ @@ -123,7 +123,7 @@ Logger.prototype = { } this.debug('setKey: ${key}',{key:key}); - + var log = this.getLog(); var alpha = require('util/alpha.js'); @@ -140,11 +140,11 @@ Logger.prototype = { getKey: function(){ var log = this.getLog(); - + if(log){ return log.key; } - + return null; }, @@ -173,53 +173,53 @@ Logger.prototype = { return null; }, - + isReport: function(){ var log = this.getLog(); - + if(!log){ return false; } - + if(log.force){ return true; } - + if(log.ERRO){ return true; } - + return false; }, - + report: function(key){ - + this.debug('report ${key}',{key:key}); - + var log = this.getLog(); - + if(log){ log.force = 1; } - + if(key){ this.setKey(key); } }, - + fillBuffer: function(type,fn){ var log = this.getLog(); - + if(log){ - + if(!log.arr){ log.arr = []; } - + if(fn){ log.arr.push(fn); } - + if(type){ if(log[type]){ log[type]++; @@ -229,95 +229,95 @@ Logger.prototype = { } } }, - + getSN: function(){ return contextMod.currentContext().SN || 0; }, - + getCpu: function(){ var cpu = process.serverInfo && process.serverInfo.cpu; - + if(cpu === undefined){ cpu = ''; } - + return cpu; }, - + debug : function(str,obj){ this.writeLog('DBUG',str,obj); }, - + info : function(str,obj){ this.writeLog('INFO',str,obj); }, - + warn : function(str,obj){ this.writeLog('WARN',str,obj); }, - + error : function(str,obj){ //this.occurError(); this.writeLog('ERRO',str,obj); }, - + writeLog : function(type,str,obj){ - + var level = this.type2level(type); var log = this.getLog(); var allow = filter(level,str,obj); var logStr = null; - + if(log || allow === true || level >= config.getLogLevel()){ logStr = this._getLog(type,level,str,obj); } - + if(logStr === null){ return this; } - + this.fillBuffer(type,logStr); - + if(allow === false){ return this; } - + if(allow === true){ return this.asyncLog(logStr,level); } - + if(level >= config.getLogLevel()){ return this.asyncLog(logStr,level); } }, - + type2level: function(type){ - + if(type === 'DBUG'){ return 10; } - + if(type === 'INFO'){ return 20; } - + if(type === 'WARN'){ return 30; } - + if(type === 'ERRO'){ return 40; } - + return 0; }, setLogLevel: function(level){ config.logLevel = level; }, - + _getLog: function(type,level,str,obj){ - + var log = this.getLog(); var that = this; var filename = ''; @@ -325,7 +325,7 @@ Logger.prototype = { var line = ''; var enable = false; var info = {}; - + if(level >= config.getLogLevel()){ enable = true; } @@ -334,48 +334,49 @@ Logger.prototype = { //if(log && log['ERRO']){ // enable = true; //} - + if(log && log['showLineNumber']){ enable = true; } if((enable && global.cpuUsed < 70) || isWindows){ - + info = callInfo.getCallInfo(3); - + line = info.line; column = info.column; filename = info.filename || ''; } - - + + var now = new Date(); var text = null; - + var fn = function(){ - + if(text!== null){ return text; } - + filename = filename || ''; - + if(isWindows){ filename = filename.replace(/\\/g,'/'); } - + + var type = typeof str; var index = filename.lastIndexOf('/node_modules/'); - + if(index >= 0){ - index += 14; + index += 14; }else{ index = filename.lastIndexOf('/') + 1; } - + if(index >= 0){ filename = filename.slice(index); } - + text = that.format({ SN : that.getSN(), yyyy : now.getFullYear(), @@ -388,34 +389,34 @@ Logger.prototype = { type : type, mod_act : contextMod.currentContext().mod_act || null, file : filename, - txt : typeof str === 'string' ? merge(str,obj) : '\n' + util.inspect(str), + txt : type === 'string' ? merge(str,obj) : (type === 'object' ? '\n' : '') + util.inspect(str), line : line, column : column, cpu : that.getCpu(), pid : process.pid }); - + return text; }; - + return fn; }, - + asyncLog: function(fn,level){ - + var str; - + if(typeof fn === 'function'){ str = fn(); }else{ str = fn; } - + this.print(str,level); - + return this; }, - + print: function(str,level){ /* eslint-disable no-console */ @@ -434,42 +435,42 @@ Logger.prototype = { //process.stdout.write(str); //process.stdout.write('\n'); }, - + format: function(data){ - + var str = data.yyyy - + '-' - + data.MM - + '-' - + data.dd - + ' ' - + data.HH - + ':' - + data.mm - + ':' - + data.ss - + '.' - + data.msec - + ' [' - + data.type - + '] [' - + data.pid - + ' cpu' - + data.cpu - + ' ' - + data.SN - + '] [' - + data.mod_act - + '] [' - + data.file - + ':' - + data.line - + '] ' - + data.txt; - + + '-' + + data.MM + + '-' + + data.dd + + ' ' + + data.HH + + ':' + + data.mm + + ':' + + data.ss + + '.' + + data.msec + + ' [' + + data.type + + '] [' + + data.pid + + ' cpu' + + data.cpu + + ' ' + + data.SN + + '] [' + + data.mod_act + + '] [' + + data.file + + ':' + + data.line + + '] ' + + data.txt; + return str; }, - + merge: merge, zeroize: zeroize }; @@ -481,13 +482,13 @@ function merge(str,obj){ } return str && str.replace(/\$\{(.+?)\}/g,function($0,$1){ - - var rs = obj && obj[$1]; - var undefined; - - return rs === undefined ? '' : - typeof rs === 'string' ? rs : util.inspect(rs); - }); + + var rs = obj && obj[$1]; + var undefined; + + return rs === undefined ? '' : + typeof rs === 'object' ? util.inspect(rs) : String(rs); + }); } @@ -496,7 +497,7 @@ function merge(str,obj){ * @param {Number} num 一个整数 * @param {Number} width 总宽度 * @return {String} 加完前导0的字符串 - * + * */ function zeroize(num,width){ var s = String(num), @@ -534,7 +535,7 @@ function isExceedFreq(level,str,obj){ tnm2.Attr_API_Set('AVG_TSW_ERROE_LOG_5S', freqCache.count); freqCache.count = 0; - + freqCache.detail = {}; } @@ -579,7 +580,7 @@ function filter(level,str,obj){ } -logger = new Logger(__filename); +logger = new Logger(); From 1baa41ec5bdb244a98391184aaad351c1295c1ae Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Wed, 23 May 2018 22:27:47 +0800 Subject: [PATCH 029/163] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 测试用例 http.isInnerIP isTST --- test/bin/lib/util/isTST.test.js | 51 ++++++++++++++++++++ test/bin/tsw/util/http.isInnerIP.test.js | 59 ++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 test/bin/lib/util/isTST.test.js create mode 100644 test/bin/tsw/util/http.isInnerIP.test.js diff --git a/test/bin/lib/util/isTST.test.js b/test/bin/lib/util/isTST.test.js new file mode 100644 index 00000000..4a7a1327 --- /dev/null +++ b/test/bin/lib/util/isTST.test.js @@ -0,0 +1,51 @@ +const chai = require('chai'); +const expect = chai.expect; +const plug = require('plug'); + +let isTST = plug('util/isTST.js'); +let logger = plug('logger'); +let config = plug('config'); + +logger.setLogLevel('error'); + +describe('test isTST', () => { + + let req = {}; + + it('no extendMod', async () => { + + + config.extendMod = false; + + expect(isTST.isTST(req)).to.equal(false); + + }); + + it('extendMod no isTST', async () => { + + config.extendMod = {}; + + expect(isTST.isTST(req)).to.equal(false); + + }); + + it('extendMod isTST return false', async () => { + + config.extendMod = { + isTST: () => false + }; + + expect(isTST.isTST(req)).to.equal(false); + + }); + + it('extendMod isTST return true', async () => { + + config.extendMod = { + isTST: () => true + }; + + expect(isTST.isTST(req)).to.equal(true); + + }); +}); \ No newline at end of file diff --git a/test/bin/tsw/util/http.isInnerIP.test.js b/test/bin/tsw/util/http.isInnerIP.test.js new file mode 100644 index 00000000..e32f3e9e --- /dev/null +++ b/test/bin/tsw/util/http.isInnerIP.test.js @@ -0,0 +1,59 @@ +const chai = require('chai'); +const expect = chai.expect; +const plug = require('plug'); +const isInnerIP = plug('util/http.isInnerIP.js'); +const logger = plug('logger'); + +logger.setLogLevel('error'); + +describe('test http.isInnerIP', () => { + + it('#ipAddress not string', () => { + expect(isInnerIP.isInnerIP(null)).to.equal(false); + expect(isInnerIP.isInnerIP(2)).to.equal(false); + }); + + it('#not IPv4', () => { + expect(isInnerIP.isInnerIP('skdgedakfksa')).to.equal(false); + }); + + it('#127.x.x.x', () => { + expect(isInnerIP.isInnerIP('127.0.0.1')).to.equal('127.0.0.1'); + expect(isInnerIP.isInnerIP('127.0.1.1')).to.equal('127.0.0.1'); + expect(isInnerIP.isInnerIP('127.1.1.1')).to.equal('127.0.0.1'); + }); + + it('#A 100.64.0.0/10', () => { + expect(isInnerIP.isInnerIP('100.64.0.0')).to.equal('A'); + expect(isInnerIP.isInnerIP('100.125.100.1')).to.equal('A'); + expect(isInnerIP.isInnerIP('100.127.255.255')).to.equal('A'); + }); + + it('#A类 10.0.0.0-10.255.255.255', () => { + expect(isInnerIP.isInnerIP('10.0.0.0')).to.equal('a'); + expect(isInnerIP.isInnerIP('10.2.100.1')).to.equal('a'); + expect(isInnerIP.isInnerIP('10.255.255.255')).to.equal('a'); + }); + + it('#a2 9.0.0.0-9.255.255.255', () => { + expect(isInnerIP.isInnerIP('9.0.0.0')).to.equal('a2'); + expect(isInnerIP.isInnerIP('9.125.255.255')).to.equal('a2'); + expect(isInnerIP.isInnerIP('9.255.255.255')).to.equal('a2'); + }); + + it('#b 172.16.0.0-172.31.255.255', () => { + expect(isInnerIP.isInnerIP('172.16.0.0')).to.equal('b'); + expect(isInnerIP.isInnerIP('172.17.255.1')).to.equal('b'); + expect(isInnerIP.isInnerIP('172.31.255.255')).to.equal('b'); + }); + + it('#c 192.168.0.0-192.168.255.255', () => { + expect(isInnerIP.isInnerIP('192.168.0.0')).to.equal('c'); + expect(isInnerIP.isInnerIP('192.168.1.0')).to.equal('c'); + expect(isInnerIP.isInnerIP('192.168.255.255')).to.equal('c'); + }); + + it('#not innerIp Test', () => { + expect(isInnerIP.isInnerIP('14.44.22.55')).to.equal(false); + }); +}); \ No newline at end of file From ccddc1ec7fe5a7c1a2c4c5be50c643466e6679c4 Mon Sep 17 00:00:00 2001 From: youkunhuang Date: Thu, 24 May 2018 09:54:25 +0800 Subject: [PATCH 030/163] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BB=91=E6=A0=B8?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=EF=BC=8C=E7=94=9F=E4=BA=A7=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E6=94=AF=E6=8C=81windows?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/proxy/master.js | 139 +++++++++++++++++++------------------------- bin/tsw/util/cpu.js | 117 +++++++++++++++++++++++++++++-------- 2 files changed, 153 insertions(+), 103 deletions(-) diff --git a/bin/proxy/master.js b/bin/proxy/master.js index 3470f18f..32eb4778 100644 --- a/bin/proxy/master.js +++ b/bin/proxy/master.js @@ -117,21 +117,16 @@ process.noProcessWarnings = true; startServer(); -/** - * 通过cluster启动master && worker - */ +// 通过cluster启动master && worker function startServer(){ var useWorker = true; - if(!serverOS.isLinux){ - useWorker = false; - } - if(debugOptions && debugOptions.inspectorEnabled){ useWorker = false; } + //windows下需要使用RR cluster.schedulingPolicy = cluster.SCHED_RR; if(cluster.isMaster && useWorker){ @@ -143,23 +138,13 @@ function startServer(){ //启动管理进程 require('./admin.js'); - + logger.info('start master....'); logger.info('version node: ${node}, modules: ${modules}',process.versions); if(serverOS.isLinux){ //当前目录777,为heapsnapshot文件创建提供权限 fs.chmodSync(__dirname, 0x1ff); //0777 - - //清除heapsnapshot文件 - cp.exec('rm -rf ./*.heapsnapshot',{ - timeout: 5000, - cwd: __dirname - },function(err){ - if(err){ - logger.error(err.stack); - } - }); } //根据cpu数来初始化并启动子进程 @@ -177,21 +162,24 @@ function startServer(){ //监听子进程是否fork成功 cluster.on('fork',function(currWorker){ - + var cpu = getToBindCpu(currWorker); - + logger.info('worker fork success! pid:${pid} cpu: ${cpu}',{ pid: currWorker.process.pid, cpu: cpu }); - + + //绑定cpu + cpuUtil.taskset(cpu,currWorker.process.pid); + if(workerMap[cpu]){ closeWorker(workerMap[cpu]); } - + workerMap[cpu] = currWorker; cpuMap[cpu] = 1; - + //监听子进程发来的消息并处理 currWorker.on('message',function(...args){ var m = args[0]; @@ -199,65 +187,65 @@ function startServer(){ methodMap[m.cmd].apply(this,args); } }); - + //给子进程发送消息,启动http服务 currWorker.send({ from:'master', cmd:'listen', cpu: cpu }); - + }); - - //退出时 + + //子进程退出时做下处理 cluster.on('disconnect', function(worker) { var cpu = getToBindCpu(worker); - + if(worker.hasRestart){ return; } - + logger.info('worker${cpu} pid=${pid} has disconnected. restart new worker again.',{ pid: worker.process.pid, cpu: cpu }); - + restartWorker(worker); }); - + //子进程被杀死的时候做下处理,原地复活 cluster.on('exit',function(worker){ - + var cpu = getToBindCpu(worker); - + if(worker.hasRestart){ return; } - + logger.info('worker${cpu} pid=${pid} has been killed. restart new worker again.',{ pid: worker.process.pid, cpu: cpu }); - + restartWorker(worker); }); - + process.on('reload',function(GET){ - + var timeout = 1000, cpu = 0, key,worker; - + if(isDeaded){ process.exit(0); } - + logger.info('reload'); - + for(key in workerMap){ worker = workerMap[key]; try{ - + cpu = getToBindCpu(worker); if(config.isTest || config.devMode){ @@ -323,12 +311,12 @@ function startServer(){ checkWorkerAlive(); startLogMan(); - + //process.title = 'TSW/master/node'; //保留node命令,不然运维监控不到 - + }else{ - + //子进程直接引入proxy文件,当然也可以直接在这里写逻辑运行,注意此处else作用域属于子进程作用域,非本程序作用域 process.title = 'TSW/worker/node'; logger.info('start worker....'); @@ -346,10 +334,10 @@ function startServer(){ //处理子进程的心跳消息 methodMap.heartBeat = function(m){ - - var worker = this; + + var worker = this; var now = new Date().getTime(); - + worker.lastMessage = m; worker.lastLiveTime = now; }; @@ -369,15 +357,15 @@ function closeWorker(worker){ if(worker.hasClose){ return; } - + if(workerMap[cpu] === worker){ delete workerMap[cpu]; } - + var closeFn = function(worker){ var closed = false; var pid = worker.process.pid; - + return function(){ if(closed){ return; @@ -389,11 +377,11 @@ function closeWorker(worker){ } worker.destroy(); - + closed = true; }; }(worker); - + setTimeout(closeFn,closeTimeWait); if(worker.exitedAfterDisconnect){ @@ -406,15 +394,13 @@ function closeWorker(worker){ }catch(e){ logger.info(e.stack); } - + } -/** - * 重启worker - */ +//重启worker function restartWorker(worker){ var cpu = getToBindCpu(worker); - + if(worker.hasRestart){ return; } @@ -429,40 +415,40 @@ function restartWorker(worker){ },10000); cpuMap[cpu] = 0; - + worker.hasRestart = true; cluster.fork(process.env).cpuid = cpu; } -//定时检测子进程死活,发现15秒没响应的就干掉 +//定时检测子进程存活,发现15秒没响应的就干掉 function checkWorkerAlive(){ - + setInterval(function(){ - - var + + var nowDate = new Date(), now = nowDate.getTime(), key, worker, cpuid; - + for(key in workerMap){ worker = workerMap[key]; cpuid = worker.cpuid; - + worker.lastLiveTime = worker.lastLiveTime || now; if(!worker.startTime){ worker.startTime = now; } - + //无响应进程处理 if(now - worker.lastLiveTime > 15000 && cpuMap[cpuid] === 1){ - + logger.error('worker${cpu} pid=${pid} miss heartBeat, kill it',{ pid : worker.process.pid, cpu: cpuid }); - + restartWorker(worker); } @@ -501,13 +487,11 @@ function checkWorkerAlive(){ },5000); } -/** - * 获取需要绑定的CPU编号 - */ +//获取需要绑定的CPU编号 function getToBindCpu(worker){ - + var cpu = 0;//如果只有一个cpu或者都占用了 - + if(worker.cpuid !== undefined){ cpu = worker.cpuid; return cpu; @@ -521,18 +505,15 @@ function getToBindCpu(worker){ } } } - + cpuMap[i] = 1; - + return cpu; } -/** - * log管理 - */ +//log管理 function startLogMan(){ require('api/logman').start({ - //按小时归类log - delay: 'H' + delay: 'H' //按小时归类log }); } diff --git a/bin/tsw/util/cpu.js b/bin/tsw/util/cpu.js index 43e36612..87700bd9 100644 --- a/bin/tsw/util/cpu.js +++ b/bin/tsw/util/cpu.js @@ -33,7 +33,7 @@ if(!global[__filename]){ this.getCpuUsed = function(cpu){ var now = Date.now(); - + cpu = cpu || ''; if(isWindows){ @@ -100,49 +100,118 @@ this.getCpuUsed = function(cpu){ this.cpus = function(){ - + var res = []; - - + + os.cpus().forEach(function(v){ - + if(v.times && v.times.idle !== 0){ res.push(v); } - + }); - - + + return res; }; +this.taskset = function(oriCpu,pid){ + if(isWindows){ + return; + } + + //绑定CPU + logger.info('taskset -cp ${pid}',{ + pid: pid + }); + + //打印shell执行信息 + cp.exec(`taskset -cp ${pid}`,{ + timeout: 5000 + },function(err,data,errData){ + + var str = data.toString('UTF-8'); + var tmp = str.split(':'); + var cpus; + + if(tmp.length >= 2){ + cpus = exports.parseTaskset(tmp[1]); + } + + var cpu = oriCpu; + if(cpus.length > 1){ + //cpu编号修正 + cpu = parseInt(cpus[cpu % cpus.length],10); + }else{ + //超过cpu编号时,修正 + cpu = cpu % exports.cpus().length; + } + + if(err){ + logger.error(err.stack); + } + + if(data.length){ + logger.info('\n' + data.toString('UTF-8')); + } + + if(errData.length){ + logger.error('\n' + errData.toString('UTF-8')); + } + + logger.info('taskset -cp ${cpu} ${pid}',{ + cpu: cpu, + pid: pid + }); + + cp.exec(`taskset -cp ${cpu} ${pid}`,{ + timeout: 5000 + },function(err,data,errData){ + if(err){ + logger.error(err.stack); + } + + if(data.length){ + logger.info('\n' + data.toString('UTF-8')); + } + + if(errData.length){ + logger.error('\n' + errData.toString('UTF-8')); + } + }); + + }); + +} + + this.parseTaskset = function(str){ - - var res = []; + + var res = []; var arr = str.split(','); - + arr.forEach(function(v){ - + v = v.trim(); - - var tmp = v.split('-'); - var start = ~~tmp[0]; - var end = ~~tmp[1]; - var i; - + + var tmp = v.split('-'); + var start = ~~tmp[0]; + var end = ~~tmp[1]; + var i,len; + if(end < start){ end = start; } - + for(i = start; i<=end ;i++){ - res.push(i); + res.push(i); } }); - - return res; -}; + return res; +} if(process.mainModule === module){ setInterval(function(){ @@ -150,4 +219,4 @@ if(process.mainModule === module){ console.log('cpu: ' + module.exports.getCpuUsed()); /* eslint-enable no-console */ },1000); -} \ No newline at end of file +} From 167a59a134bdcba6fbc6f710d23e3bd1aaeb4e3a Mon Sep 17 00:00:00 2001 From: youkunhuang Date: Thu, 24 May 2018 10:00:51 +0800 Subject: [PATCH 031/163] =?UTF-8?q?fix=20cpu=E7=BB=91=E5=AE=9A=E4=BE=9D?= =?UTF-8?q?=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/tsw/util/cpu.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/tsw/util/cpu.js b/bin/tsw/util/cpu.js index 87700bd9..fa7275fa 100644 --- a/bin/tsw/util/cpu.js +++ b/bin/tsw/util/cpu.js @@ -9,7 +9,9 @@ const os = require('os'); const fs = require('fs'); -const {isWindows} = require('./isWindows.js'); +const cp = require('child_process'); +const {isWindows} = require('./isWindows.js'); +const logger = require('logger'); var cache; if(!global[__filename]){ From 987cd59cdcbab8f5248929e65836f2b51805d3c6 Mon Sep 17 00:00:00 2001 From: neilcui Date: Thu, 24 May 2018 10:01:26 +0800 Subject: [PATCH 032/163] eslint: eol new line --- .eslintrc.json | 21 ++++- bin/lib/api/habo/habo.js | 2 +- bin/lib/api/keyman/alphaMap.js | 1 - bin/lib/api/keyman/blackIpMap.js | 3 - bin/lib/api/keyman/index.js | 2 +- bin/lib/api/keyman/runtimeAdd.js | 2 - bin/lib/api/notify/index.js | 12 +-- bin/lib/api/tnm2/index.js | 28 +++--- bin/lib/loader/extentions.js | 2 +- bin/lib/runtime/md5.check.js | 5 - bin/lib/util/gzipHttp.js | 2 +- bin/lib/util/mail/mail.js | 9 +- bin/lib/util/mail/src/_config.js | 2 +- bin/lib/util/oa-login/index.js | 4 +- bin/lib/webapp/Server.js | 2 +- bin/lib/webapp/utils/format.js | 2 +- bin/proxy/check.js | 4 +- bin/proxy/config.js | 4 +- bin/proxy/http.mod.act.js | 4 +- bin/proxy/http.mod.map.js | 4 +- bin/proxy/http.proxy.js | 11 +-- bin/proxy/http.route.js | 3 +- bin/proxy/index.js | 2 - bin/proxy/master.js | 1 - bin/proxy/websocket.js | 2 +- bin/proxy/ws.mod.act.js | 2 +- bin/proxy/ws.mod.map.js | 2 +- bin/proxy/ws.route.js | 8 +- bin/tsw/ajax/ajax.js | 6 +- bin/tsw/ajax/form.js | 6 +- bin/tsw/ajax/http-https.options.js | 8 +- bin/tsw/ajax/index.js | 2 +- bin/tsw/ajax/token.js | 3 +- bin/tsw/api/date.js | 2 +- bin/tsw/api/fileCache/index.js | 3 +- bin/tsw/api/logman/index.js | 2 +- bin/tsw/context.js | 5 +- bin/tsw/default/config.default.sky.js | 1 - bin/tsw/plug.js | 5 +- bin/tsw/router.js | 7 +- bin/tsw/runtime/CCFinder.js | 8 +- bin/tsw/runtime/Context.js | 1 - bin/tsw/runtime/ContextWrap.js | 28 +++--- bin/tsw/runtime/Dns.hack.js | 22 ++--- bin/tsw/runtime/JankWatcher.js | 2 +- bin/tsw/runtime/fs.hack.js | 1 - bin/tsw/runtime/overloadProtection.js | 2 +- bin/tsw/util/Deferred/index.js | 1 - bin/tsw/util/Queue/index.js | 2 - bin/tsw/util/auto-report/TEReport.js | 2 +- bin/tsw/util/auto-report/alpha.js | 2 +- bin/tsw/util/auto-report/download.js | 106 +++++++++++----------- bin/tsw/util/auto-report/highlight-tsw.js | 2 +- bin/tsw/util/auto-report/logReport.js | 12 +-- bin/tsw/util/auto-report/post.js | 6 +- bin/tsw/util/auto-report/post.openapi.js | 1 - bin/tsw/util/auto-report/src/_config.js | 2 +- bin/tsw/util/auto-report/tmpl.js | 16 ++-- bin/tsw/util/auto-report/view.js | 2 - bin/tsw/util/cpu.js | 40 ++++---- bin/tsw/util/h5-test/add.js | 2 +- bin/tsw/util/h5-test/del.js | 4 +- bin/tsw/util/h5-test/group/src/_config.js | 2 +- bin/tsw/util/h5-test/group/view.js | 2 +- bin/tsw/util/h5-test/is-test.js | 6 +- bin/tsw/util/h5-test/page/src/_config.js | 2 +- bin/tsw/util/h5-test/page/tmpl.js | 2 +- bin/tsw/util/h5-test/page/view.js | 2 +- bin/tsw/util/home/navMenus.js | 2 +- bin/tsw/util/home/src/_config.js | 2 +- bin/tsw/util/home/view.js | 2 +- bin/tsw/util/html302/index.js | 2 +- bin/tsw/util/html302/src/_config.js | 2 +- bin/tsw/util/http.isInnerIP.js | 34 +++---- bin/tsw/util/http.js | 65 +++++++------ bin/tsw/util/http/parseBody.js | 1 - bin/tsw/util/isWindows.js | 2 +- bin/tsw/util/logger/callInfo.js | 2 +- bin/tsw/util/logger/index.js | 2 +- bin/tsw/util/logger/logger.config.js | 10 +- bin/tsw/util/logger/logger.js | 61 ++++++------- bin/tsw/util/openapi.js | 2 +- bin/tsw/util/static/mime.js | 2 +- bin/tsw/util/static/static.js | 1 - bin/tsw/util/v8-profiler.js | 4 +- bin/tsw/util/xss.js | 2 +- examples/framework/config.js | 2 +- examples/framework/express.js | 4 +- examples/framework/hapi.js | 3 - examples/framework/router.js | 2 +- examples/framework/wsRouter.js | 2 +- examples/skyMode/config.js | 2 +- test/bin/config.js | 2 +- test/bin/tsw/runtime/context.js | 2 +- test/bin/tsw/util/CD.test.js | 4 +- test/bin/tsw/util/cpu.test.js | 2 +- test/bin/tsw/util/xss.test.js | 2 +- 97 files changed, 326 insertions(+), 375 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 1bcea667..eb8c0f3b 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -24,6 +24,23 @@ "error", "unix" ], + "eol-last": [ + "error", + "always" + ], + "no-multiple-empty-lines": [ + "error", + { + "max": 2, + "maxEOF": 1 + } + ], + "no-multi-spaces": [ + "error", + { + "ignoreEOLComments": true + } + ], "quotes": [ "error", "single" @@ -33,10 +50,10 @@ "always" ], "no-unused-vars": [ - "error", + "error", { "args": "none" } ] } -} \ No newline at end of file +} diff --git a/bin/lib/api/habo/habo.js b/bin/lib/api/habo/habo.js index c54c17f2..5afc1cca 100644 --- a/bin/lib/api/habo/habo.js +++ b/bin/lib/api/habo/habo.js @@ -9,4 +9,4 @@ module.exports.report = function(obj, rate) { -}; \ No newline at end of file +}; diff --git a/bin/lib/api/keyman/alphaMap.js b/bin/lib/api/keyman/alphaMap.js index 6908e270..63c6fad3 100644 --- a/bin/lib/api/keyman/alphaMap.js +++ b/bin/lib/api/keyman/alphaMap.js @@ -192,4 +192,3 @@ function init(){ init(); - diff --git a/bin/lib/api/keyman/blackIpMap.js b/bin/lib/api/keyman/blackIpMap.js index 1ac6f9a1..21cb31c9 100644 --- a/bin/lib/api/keyman/blackIpMap.js +++ b/bin/lib/api/keyman/blackIpMap.js @@ -191,6 +191,3 @@ this.get = function(){ return defer; }; - - - diff --git a/bin/lib/api/keyman/index.js b/bin/lib/api/keyman/index.js index 9477d390..d1dae300 100644 --- a/bin/lib/api/keyman/index.js +++ b/bin/lib/api/keyman/index.js @@ -37,4 +37,4 @@ this.ipCheck = function(opt){ this.runtimeAdd = function(opt){ return require('./runtimeAdd.js').add(opt); -}; \ No newline at end of file +}; diff --git a/bin/lib/api/keyman/runtimeAdd.js b/bin/lib/api/keyman/runtimeAdd.js index 367501a6..5de3bf6b 100644 --- a/bin/lib/api/keyman/runtimeAdd.js +++ b/bin/lib/api/keyman/runtimeAdd.js @@ -25,5 +25,3 @@ this.hello = function(){ return; }; - - diff --git a/bin/lib/api/notify/index.js b/bin/lib/api/notify/index.js index 6183ed33..f9fc0370 100644 --- a/bin/lib/api/notify/index.js +++ b/bin/lib/api/notify/index.js @@ -7,17 +7,17 @@ */ 'use strict'; -const Deferred = require('util/Deferred'); +const Deferred = require('util/Deferred'); this.DecryptTicketWithClientIP = function(opt){ - var defer = Deferred.create(); + var defer = Deferred.create(); return defer.resolve(); }; this.SendRTX = function(opt){ - var defer = Deferred.create(); + var defer = Deferred.create(); return defer.resolve(); }; @@ -45,7 +45,7 @@ this.SendMail = function(opt){ 'attachment' : '' //邮件附件的文件名以及文件的内容(在发送请求时,文件内容是二进制数据流的形式发送) },opt); - var defer = Deferred.create(); + var defer = Deferred.create(); return defer.resolve(); }; @@ -62,7 +62,7 @@ this.SendWeiXin = function(opt){ 'Sender' : '' //发送人 },opt); - var defer = Deferred.create(); + var defer = Deferred.create(); return defer.resolve(); }; @@ -79,7 +79,7 @@ this.SendSMS = function(opt){ 'Sender' : '' //发送人 },opt); - var defer = Deferred.create(); + var defer = Deferred.create(); return defer.resolve(); }; diff --git a/bin/lib/api/tnm2/index.js b/bin/lib/api/tnm2/index.js index 6708c4d6..8484b289 100644 --- a/bin/lib/api/tnm2/index.js +++ b/bin/lib/api/tnm2/index.js @@ -7,12 +7,12 @@ */ 'use strict'; -const serverInfo = require('serverInfo.js'); -const mapping = require('./mapping.json'); -const {isWindows} = require('util/isWindows.js'); -const url = require('url'); -const Deferred = require('util/Deferred'); -const cluster = require('cluster'); +const serverInfo = require('serverInfo.js'); +const mapping = require('./mapping.json'); +const {isWindows} = require('util/isWindows.js'); +const url = require('url'); +const Deferred = require('util/Deferred'); +const cluster = require('cluster'); var cache = { curr: {}, @@ -29,8 +29,8 @@ if(global[__filename]){ if(isFirstLoad){ cluster.worker && cluster.worker.once('disconnect', function(worker){ - var logger = require('logger'); - var last = cache.curr; + var logger = require('logger'); + var last = cache.curr; logger.info('report on disconnect event...'); @@ -70,8 +70,8 @@ var cacheOrRepoet = function(attr, iValue){ } curr = cache.curr[attr]; - curr.sum += iValue; - curr.count += 1; + curr.sum += iValue; + curr.count += 1; var now = Date.now(); @@ -89,11 +89,11 @@ var cacheOrRepoet = function(attr, iValue){ var reportOpenapi = function(last){ - var defer = Deferred.create(); + var defer = Deferred.create(); var openapi = require('util/openapi'); - var logger = require('logger'); - var config = require('config'); + var logger = require('logger'); + var config = require('config'); var retCall; if(typeof config.beforeReportApp === 'function'){ @@ -173,4 +173,4 @@ var reportOpenapi = function(last){ }); return defer; -}; \ No newline at end of file +}; diff --git a/bin/lib/loader/extentions.js b/bin/lib/loader/extentions.js index bb6991d1..ee6ac07b 100644 --- a/bin/lib/loader/extentions.js +++ b/bin/lib/loader/extentions.js @@ -5,4 +5,4 @@ * http://opensource.org/licenses/MIT * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -'use strict'; \ No newline at end of file +'use strict'; diff --git a/bin/lib/runtime/md5.check.js b/bin/lib/runtime/md5.check.js index 071a604a..0b63be70 100644 --- a/bin/lib/runtime/md5.check.js +++ b/bin/lib/runtime/md5.check.js @@ -12,8 +12,3 @@ this.check = function(root){}; this.findNodeCpp = function(curr){}; - - - - - diff --git a/bin/lib/util/gzipHttp.js b/bin/lib/util/gzipHttp.js index 4ab22c7e..175ef039 100644 --- a/bin/lib/util/gzipHttp.js +++ b/bin/lib/util/gzipHttp.js @@ -16,7 +16,7 @@ const defaultChunkSize = 8 * 1024; this.httpUtil = httpUtil; this.create = this.getGzipResponse = function(opt){ - var window = context.window || {}; + var window = context.window || {}; opt = opt || {}; diff --git a/bin/lib/util/mail/mail.js b/bin/lib/util/mail/mail.js index f5076835..7bcab8dd 100644 --- a/bin/lib/util/mail/mail.js +++ b/bin/lib/util/mail/mail.js @@ -73,12 +73,11 @@ this.SendMail = function(key,group,second,oriOpt){ }; - var reportOpenapi = function(data){ - var defer = Deferred.create(); - var config = require('config'); + var defer = Deferred.create(); + var config = require('config'); var openapi = require('util/openapi'); - var logger = require('logger'); + var logger = require('logger'); var retCall; @@ -162,5 +161,3 @@ this.findMailARS = function(file){ return ''; }; - - diff --git a/bin/lib/util/mail/src/_config.js b/bin/lib/util/mail/src/_config.js index 09597f20..723cde21 100644 --- a/bin/lib/util/mail/src/_config.js +++ b/bin/lib/util/mail/src/_config.js @@ -20,4 +20,4 @@ define(function(require, exports, module){ }; -}); \ No newline at end of file +}); diff --git a/bin/lib/util/oa-login/index.js b/bin/lib/util/oa-login/index.js index dc651e93..9c4a0f2b 100644 --- a/bin/lib/util/oa-login/index.js +++ b/bin/lib/util/oa-login/index.js @@ -7,10 +7,10 @@ */ 'use strict'; -module.exports = function(request, response, callback, tofCfg){ +module.exports = function(request, response, callback, tofCfg){ callback(); }; module.exports.checkLoginForTSW = function(request, response, callback){ callback(); -}; \ No newline at end of file +}; diff --git a/bin/lib/webapp/Server.js b/bin/lib/webapp/Server.js index 305af58a..7fd3c605 100644 --- a/bin/lib/webapp/Server.js +++ b/bin/lib/webapp/Server.js @@ -13,4 +13,4 @@ this.startServer = function(){ this.stopServer = function(){ -}; \ No newline at end of file +}; diff --git a/bin/lib/webapp/utils/format.js b/bin/lib/webapp/utils/format.js index 9bbd34b3..e794bf2a 100644 --- a/bin/lib/webapp/utils/format.js +++ b/bin/lib/webapp/utils/format.js @@ -27,4 +27,4 @@ function formatBuffer(buffer) { module.exports = { formatBuffer -}; \ No newline at end of file +}; diff --git a/bin/proxy/check.js b/bin/proxy/check.js index 5a5f2e31..3135d204 100644 --- a/bin/proxy/check.js +++ b/bin/proxy/check.js @@ -11,8 +11,8 @@ try{ - var plug = require('plug'); - var config = plug('config'); + var plug = require('plug'); + var config = plug('config'); process.stdout.write('typeof config: ' + typeof config + '\r\n'); diff --git a/bin/proxy/config.js b/bin/proxy/config.js index 49877d6e..17d9cfb9 100644 --- a/bin/proxy/config.js +++ b/bin/proxy/config.js @@ -28,7 +28,7 @@ if(global[__filename]){ if(isFirstLoad){ process.dlopen = function(fn){ - var parent = path.join(__dirname , '..'); + var parent = path.join(__dirname , '..'); return function(module,curr){ //检查node私有文件 @@ -44,7 +44,6 @@ if(isFirstLoad){ } - if(fs.existsSync('/etc/tsw.config.js')){ cache.config = require('/usr/local/node_modules/config.js'); }else if(fs.existsSync('/usr/local/node_modules/config.js')){ @@ -56,7 +55,6 @@ if(fs.existsSync('/etc/tsw.config.js')){ } - Deferred.extend(true,exports,defaultValue,cache.config); diff --git a/bin/proxy/http.mod.act.js b/bin/proxy/http.mod.act.js index 9cd832d1..f5717a03 100644 --- a/bin/proxy/http.mod.act.js +++ b/bin/proxy/http.mod.act.js @@ -7,8 +7,8 @@ */ 'use strict'; -const config = require('./config.js'); -var base = null; +const config = require('./config.js'); +var base = null; if(config.skyMode){ base = require('default/config.default.sky.js'); diff --git a/bin/proxy/http.mod.map.js b/bin/proxy/http.mod.map.js index 8c889d91..ebc987fc 100644 --- a/bin/proxy/http.mod.map.js +++ b/bin/proxy/http.mod.map.js @@ -7,8 +7,8 @@ */ 'use strict'; -const config = require('./config.js'); -var base = null; +const config = require('./config.js'); +var base = null; if(config.skyMode){ base = require('default/config.default.sky.js'); diff --git a/bin/proxy/http.proxy.js b/bin/proxy/http.proxy.js index b1bb68fc..6a516a20 100644 --- a/bin/proxy/http.proxy.js +++ b/bin/proxy/http.proxy.js @@ -45,7 +45,6 @@ var isStartHeartBeat = false; var heartBeatCount = 0; - function doRoute(req,res){ if(routeCache === null){ @@ -324,9 +323,9 @@ function startHeartBeat(){ //获取本机信息,用来分组 require('api/cmdb').GetDeviceThisServer().done(function(data){ - data = data || {}; - var business = data.business && data.business[0] || {}; - var owner = ''; + data = data || {}; + var business = data.business && data.business[0] || {}; + var owner = ''; if(data.ownerMain){ owner = [owner,data.ownerMain].join(';'); @@ -369,7 +368,6 @@ function startHeartBeat(){ } - //restart methodMap.restart = function(){ @@ -555,6 +553,3 @@ if(cluster.isMaster){ require('webapp/Server.js').startServer(); - - - diff --git a/bin/proxy/http.route.js b/bin/proxy/http.route.js index 0a67c8b9..05923527 100644 --- a/bin/proxy/http.route.js +++ b/bin/proxy/http.route.js @@ -763,7 +763,6 @@ function doRoute(req,res){ } - function onerror(req,res,err){ var listener = req.listeners('fail'); var window = context.window || {}; @@ -790,4 +789,4 @@ function onerror(req,res,err){ window.onerror = null; } -} \ No newline at end of file +} diff --git a/bin/proxy/index.js b/bin/proxy/index.js index 9727e5bc..939a9027 100644 --- a/bin/proxy/index.js +++ b/bin/proxy/index.js @@ -17,5 +17,3 @@ plug('runtime/capturer.js'); require('./master.js'); - - diff --git a/bin/proxy/master.js b/bin/proxy/master.js index 3470f18f..949d7e4b 100644 --- a/bin/proxy/master.js +++ b/bin/proxy/master.js @@ -113,7 +113,6 @@ process.on('unhandledRejection', (reason = {}, p = {}) => { process.noProcessWarnings = true; - startServer(); diff --git a/bin/proxy/websocket.js b/bin/proxy/websocket.js index 9d9865d3..6ecad0a1 100644 --- a/bin/proxy/websocket.js +++ b/bin/proxy/websocket.js @@ -225,4 +225,4 @@ exports.start_listen = function() { }); bind_listen(wss); } -}; \ No newline at end of file +}; diff --git a/bin/proxy/ws.mod.act.js b/bin/proxy/ws.mod.act.js index 0ee8dd82..7aa75209 100644 --- a/bin/proxy/ws.mod.act.js +++ b/bin/proxy/ws.mod.act.js @@ -7,4 +7,4 @@ */ 'use strict'; -module.exports = require('./config.js').wsModAct; \ No newline at end of file +module.exports = require('./config.js').wsModAct; diff --git a/bin/proxy/ws.mod.map.js b/bin/proxy/ws.mod.map.js index a0a20b47..a5824f69 100644 --- a/bin/proxy/ws.mod.map.js +++ b/bin/proxy/ws.mod.map.js @@ -7,4 +7,4 @@ */ 'use strict'; -module.exports = require('./config.js').wsModMap; \ No newline at end of file +module.exports = require('./config.js').wsModMap; diff --git a/bin/proxy/ws.route.js b/bin/proxy/ws.route.js index 1f05802c..2662954c 100644 --- a/bin/proxy/ws.route.js +++ b/bin/proxy/ws.route.js @@ -8,10 +8,10 @@ 'use strict'; exports.doRoute = function(ws, type, d1, d2) { - var wsModAct = require('./ws.mod.act'); + var wsModAct = require('./ws.mod.act'); var wsModMap = require('./ws.mod.map'); - var logger = require('logger'); - var contextMod = require('context.js'); + var logger = require('logger'); + var contextMod = require('context.js'); var mod_act = wsModAct.getModAct(ws), moduleObj = wsModMap.find(mod_act, ws); @@ -53,4 +53,4 @@ exports.doRoute = function(ws, type, d1, d2) { } else if ('error' == type) { moduleObj.onError(ws, d1); } -}; \ No newline at end of file +}; diff --git a/bin/tsw/ajax/ajax.js b/bin/tsw/ajax/ajax.js index c5afb30d..fec401b8 100644 --- a/bin/tsw/ajax/ajax.js +++ b/bin/tsw/ajax/ajax.js @@ -177,7 +177,7 @@ Ajax.prototype.l5Request = function(opt){ //普通请求 Ajax.prototype.doRequest = function(opt){ - var window = context.window || {}; + var window = context.window || {}; var defer = Deferred.create(), tid = null, that = this, @@ -360,8 +360,6 @@ Ajax.prototype.doRequest = function(opt){ } } } - - if(config.isTest){ @@ -1142,7 +1140,6 @@ Ajax.prototype.doRequest = function(opt){ }); }); - if(opt.send){ @@ -1165,4 +1162,3 @@ Ajax.prototype.doRequest = function(opt){ return defer; }; - diff --git a/bin/tsw/ajax/form.js b/bin/tsw/ajax/form.js index 78c07cf7..f9dae38d 100644 --- a/bin/tsw/ajax/form.js +++ b/bin/tsw/ajax/form.js @@ -41,9 +41,9 @@ function textField(boundary, key, value) { function fileField(boundary, key, value = {}) { let tmp = []; - let contentType = value.contentType || 'application/octet-stream'; + let contentType = value.contentType || 'application/octet-stream'; let dispositionType = value.dispositionType || 'form-data'; - let fileName = value.fileName || ''; + let fileName = value.fileName || ''; tmp.push('--' + boundary); @@ -97,4 +97,4 @@ function getFormBuffer(opt = {}) { module.exports = { getFormBuffer -}; \ No newline at end of file +}; diff --git a/bin/tsw/ajax/http-https.options.js b/bin/tsw/ajax/http-https.options.js index f1180c3e..5a0e3ec0 100644 --- a/bin/tsw/ajax/http-https.options.js +++ b/bin/tsw/ajax/http-https.options.js @@ -7,9 +7,9 @@ */ 'use strict'; -const https = require('https'); -const http = require('http'); -const cache = {}; +const https = require('https'); +const http = require('http'); +const cache = {}; ///etc/pki/tls/certs/ca-bundle.crt @@ -45,4 +45,4 @@ this.getHttpAgent = function(host){ } return cache[key]; -}; \ No newline at end of file +}; diff --git a/bin/tsw/ajax/index.js b/bin/tsw/ajax/index.js index 71874808..dab6c5b1 100644 --- a/bin/tsw/ajax/index.js +++ b/bin/tsw/ajax/index.js @@ -7,4 +7,4 @@ */ 'use strict'; -module.exports = require('./ajax.js'); \ No newline at end of file +module.exports = require('./ajax.js'); diff --git a/bin/tsw/ajax/token.js b/bin/tsw/ajax/token.js index c5397dcf..6b8536e6 100644 --- a/bin/tsw/ajax/token.js +++ b/bin/tsw/ajax/token.js @@ -14,7 +14,7 @@ this.token = function(skey){ var hash = 5381; if(typeof context !== 'undefined'){ - let window = context.window || {}; + let window = context.window || {}; if(window.request){ str = str || window.request.cookies.p_skey @@ -31,4 +31,3 @@ this.token = function(skey){ return hash & 0x7fffffff; }; - diff --git a/bin/tsw/api/date.js b/bin/tsw/api/date.js index 425dafa3..893bbb49 100644 --- a/bin/tsw/api/date.js +++ b/bin/tsw/api/date.js @@ -52,4 +52,4 @@ var Date = { } }; -module.exports = Date; \ No newline at end of file +module.exports = Date; diff --git a/bin/tsw/api/fileCache/index.js b/bin/tsw/api/fileCache/index.js index ad71bb1b..604d84e6 100644 --- a/bin/tsw/api/fileCache/index.js +++ b/bin/tsw/api/fileCache/index.js @@ -212,7 +212,6 @@ this.get = this.getAsync = function(filepath){ }; - this.updateMtime = function(filepath, atime, mtime){ var filename = this.getDir(filepath); @@ -230,4 +229,4 @@ this.updateMtime = function(filepath, atime, mtime){ logger.debug('[updateMtime] ${filename} succ'); }); -}; \ No newline at end of file +}; diff --git a/bin/tsw/api/logman/index.js b/bin/tsw/api/logman/index.js index cf2314f0..ef736f1b 100644 --- a/bin/tsw/api/logman/index.js +++ b/bin/tsw/api/logman/index.js @@ -99,4 +99,4 @@ var LogMan = { }; -module.exports = LogMan; \ No newline at end of file +module.exports = LogMan; diff --git a/bin/tsw/context.js b/bin/tsw/context.js index 1225d4cb..427634a7 100644 --- a/bin/tsw/context.js +++ b/bin/tsw/context.js @@ -7,8 +7,8 @@ */ 'use strict'; -const Context = require('runtime/Context'); -const Window = require('runtime/Window'); +const Context = require('runtime/Context'); +const Window = require('runtime/Window'); this.currentContext = function(){ return (process.domain && process.domain.currentContext) || new Context(); @@ -40,4 +40,3 @@ if(!global.context){ }); } - diff --git a/bin/tsw/default/config.default.sky.js b/bin/tsw/default/config.default.sky.js index 09b18ff2..68f7282a 100644 --- a/bin/tsw/default/config.default.sky.js +++ b/bin/tsw/default/config.default.sky.js @@ -65,4 +65,3 @@ this.modAct = this.modMap = { } }; - diff --git a/bin/tsw/plug.js b/bin/tsw/plug.js index 4342d983..89da6e07 100644 --- a/bin/tsw/plug.js +++ b/bin/tsw/plug.js @@ -22,8 +22,8 @@ function plug(id){ if(!global.plug){ - plug.__dirname = __dirname; - plug.parent = path.join(__dirname , '..'); + plug.__dirname = __dirname; + plug.parent = path.join(__dirname , '..'); plug.paths = [ path.join(__dirname , '../deps'), path.join(__dirname , '../tsw'), @@ -54,4 +54,3 @@ if(!global.plug){ module.exports = plug; - diff --git a/bin/tsw/router.js b/bin/tsw/router.js index 36d979e8..3a6d27a5 100644 --- a/bin/tsw/router.js +++ b/bin/tsw/router.js @@ -14,7 +14,7 @@ const parseGet = require('util/http/parseGet.js'); this.route = function(url,mod_act){ var req,res; - var window = context.window || {}; + var window = context.window || {}; req = window.request; res = window.response; @@ -41,12 +41,11 @@ this.route = function(url,mod_act){ mod_act: mod_act }); - context.mod_act = mod_act; + context.mod_act = mod_act; }else{ - context.mod_act = null; + context.mod_act = null; } httpRoute.doRoute(req,res); }; - diff --git a/bin/tsw/runtime/CCFinder.js b/bin/tsw/runtime/CCFinder.js index ba8b01b3..113298e0 100644 --- a/bin/tsw/runtime/CCFinder.js +++ b/bin/tsw/runtime/CCFinder.js @@ -36,7 +36,7 @@ this.addWhiteList = function(userIp){ this.checkHost = function(req, res){ var hostAllow = config.allowHost || []; - var host = req.headers['host']; + var host = req.headers['host']; var i,len,v; if(hostAllow.length === 0){ @@ -79,7 +79,7 @@ this.StdX10 = function(ipCache){ var arr = Object.keys(ipCache).filter(function(item){ var tmp = ipCache[item]; if(typeof tmp === 'object' && tmp.list){ - sum += tmp.list.length; + sum += tmp.list.length; return true; } return false; @@ -141,7 +141,7 @@ this.check = function(req, res){ cache.ipCache.StdX10= this.StdX10(cache.ipCache); cache.ipCacheLast = cache.ipCache; cache.ipCache = {}; - cache.whiteList = {}; + cache.whiteList = {}; //上报标准方差 tnm2.Attr_API_Set('AVG_TSW_IP_STD_X10', cache.ipCacheLast.StdX10); @@ -221,7 +221,7 @@ this.check = function(req, res){ 'CC' : config.mailCC, 'Title' : `[IP聚集告警][${cache.ipCacheLast.StdX10}%]${serverInfo.intranetIp}`, 'Content' : '

服务器IP:' + serverInfo.intranetIp + '

' - + '

IP聚集度:' + cache.ipCacheLast.StdX10 + '%

' + + '

IP聚集度:' + cache.ipCacheLast.StdX10 + '%

' + '

告警阀值:' + CCIPLimit + '

' + '

正常值:5-50

' + '

检测耗时:' + parseInt((cache.ipCacheLast.end - cache.ipCacheLast.start)/1000) + 's

' diff --git a/bin/tsw/runtime/Context.js b/bin/tsw/runtime/Context.js index ac541568..2d2b904b 100644 --- a/bin/tsw/runtime/Context.js +++ b/bin/tsw/runtime/Context.js @@ -26,4 +26,3 @@ Context.prototype.getModAct = function(){ return context.mod_act; }; - diff --git a/bin/tsw/runtime/ContextWrap.js b/bin/tsw/runtime/ContextWrap.js index 905fd0c6..5810d48b 100644 --- a/bin/tsw/runtime/ContextWrap.js +++ b/bin/tsw/runtime/ContextWrap.js @@ -8,12 +8,12 @@ 'use strict'; const EventEmitter = require('events'); -const net = require('net'); -const http = require('http'); -const domain = require('domain'); -const Context = require('runtime/Context'); -const Window = require('runtime/Window'); -const parseGet = require('util/http/parseGet'); +const net = require('net'); +const http = require('http'); +const domain = require('domain'); +const Context = require('runtime/Context'); +const Window = require('runtime/Window'); +const parseGet = require('util/http/parseGet'); class ContextWrap extends EventEmitter { @@ -50,11 +50,11 @@ class ContextWrap extends EventEmitter { this._domain.add(this._req); this._domain.add(this._rsp); - this._domain.currentContext = new Context(); - this._domain.currentContext.log = {}; - this._domain.currentContext.SN = ++process.SN; - this._domain.currentContext.window = new Window(); - this._domain.currentContext.window.request = this._req; + this._domain.currentContext = new Context(); + this._domain.currentContext.log = {}; + this._domain.currentContext.SN = ++process.SN; + this._domain.currentContext.window = new Window(); + this._domain.currentContext.window.request = this._req; this._domain.currentContext.window.response = this._rsp; this._domain.on('error', e => { @@ -69,9 +69,9 @@ class ContextWrap extends EventEmitter { this._domain.remove(this._rsp); this._domain = null; - this._req = null; - this._rsp = null; + this._req = null; + this._rsp = null; } } -module.exports = ContextWrap; \ No newline at end of file +module.exports = ContextWrap; diff --git a/bin/tsw/runtime/Dns.hack.js b/bin/tsw/runtime/Dns.hack.js index 0f48ae4b..7758e1be 100644 --- a/bin/tsw/runtime/Dns.hack.js +++ b/bin/tsw/runtime/Dns.hack.js @@ -12,16 +12,16 @@ if(!global[__filename]){ global[__filename] = true; process.nextTick(function(){ - var config = require('config'); - var dns = require('dns'); - var dcapi = require('api/libdcapi/dcapi'); - var logger = require('logger'); - var net = require('net'); + var config = require('config'); + var dns = require('dns'); + var dcapi = require('api/libdcapi/dcapi'); + var logger = require('logger'); + var net = require('net'); dns.lookup = (function(fn) { return function(hostname, options, callback) { - var args = [hostname]; + var args = [hostname]; var start = Date.now(); if(net.isIP(hostname)){ @@ -39,16 +39,16 @@ if(!global[__filename]){ var isCalled = false; var callbackWrap = function(err, address, family) { - if (isCalled) return; + if (isCalled) return; if(!err) { - code = 0; + code = 0; isFail = 0; } else if(err === timeoutError) { - code = 513; + code = 513; isFail = 1; } else { - code = 500; + code = 500; isFail = 1; } @@ -85,4 +85,4 @@ if(!global[__filename]){ })(dns.lookup); }); -} \ No newline at end of file +} diff --git a/bin/tsw/runtime/JankWatcher.js b/bin/tsw/runtime/JankWatcher.js index 4ac1fc8a..e85dd98e 100644 --- a/bin/tsw/runtime/JankWatcher.js +++ b/bin/tsw/runtime/JankWatcher.js @@ -29,4 +29,4 @@ if(!global[__filename]) { }; watch(); -} \ No newline at end of file +} diff --git a/bin/tsw/runtime/fs.hack.js b/bin/tsw/runtime/fs.hack.js index cf857bcd..10ebc5f7 100644 --- a/bin/tsw/runtime/fs.hack.js +++ b/bin/tsw/runtime/fs.hack.js @@ -168,4 +168,3 @@ if(global[__filename]){ } - diff --git a/bin/tsw/runtime/overloadProtection.js b/bin/tsw/runtime/overloadProtection.js index dadaeecf..89d8c2cb 100644 --- a/bin/tsw/runtime/overloadProtection.js +++ b/bin/tsw/runtime/overloadProtection.js @@ -40,4 +40,4 @@ process.nextTick(function() { } } -}); \ No newline at end of file +}); diff --git a/bin/tsw/util/Deferred/index.js b/bin/tsw/util/Deferred/index.js index e1ab6f80..690a4404 100644 --- a/bin/tsw/util/Deferred/index.js +++ b/bin/tsw/util/Deferred/index.js @@ -441,7 +441,6 @@ jQuery.Callbacks = function( flags ) { }; - var // Static reference to slice sliceDeferred = [].slice; diff --git a/bin/tsw/util/Queue/index.js b/bin/tsw/util/Queue/index.js index b2594bb5..7e78b06f 100644 --- a/bin/tsw/util/Queue/index.js +++ b/bin/tsw/util/Queue/index.js @@ -69,5 +69,3 @@ Queue.prototype.dequeue = function(){ return this; }; - - diff --git a/bin/tsw/util/auto-report/TEReport.js b/bin/tsw/util/auto-report/TEReport.js index bc59a943..3c4dde09 100644 --- a/bin/tsw/util/auto-report/TEReport.js +++ b/bin/tsw/util/auto-report/TEReport.js @@ -54,7 +54,7 @@ this.report = function(){ }; } - logJson.moduleId = business.moduleId; + logJson.moduleId = business.moduleId; logJson.moduleName = [business.L1Business,business.L2Business,business.L3Business,business.module].join('->'); logJson = Deferred.extend(true,{ diff --git a/bin/tsw/util/auto-report/alpha.js b/bin/tsw/util/auto-report/alpha.js index 34dcce74..4ac16e2b 100644 --- a/bin/tsw/util/auto-report/alpha.js +++ b/bin/tsw/util/auto-report/alpha.js @@ -64,7 +64,7 @@ this.isAlpha = function(req){ this.getUin = function(req){ var uin; - var window = context.window || {}; + var window = context.window || {}; req = req || window.request; diff --git a/bin/tsw/util/auto-report/download.js b/bin/tsw/util/auto-report/download.js index 6023365f..5112c791 100644 --- a/bin/tsw/util/auto-report/download.js +++ b/bin/tsw/util/auto-report/download.js @@ -7,15 +7,15 @@ */ 'use strict'; -const logger = require('logger'); -const gzipHttp = require('util/gzipHttp.js'); -const Archiver = require('archiver'); -const OALogin = require('util/oa-login/index.js'); -const tmpl = require('./tmpl'); -const post = require('./post'); -const postOpenapi = require('./post.openapi.js'); -const canIuse = /^[0-9a-zA-Z_-]{0,64}$/; -const zlib = require('zlib'); +const logger = require('logger'); +const gzipHttp = require('util/gzipHttp.js'); +const Archiver = require('archiver'); +const OALogin = require('util/oa-login/index.js'); +const tmpl = require('./tmpl'); +const post = require('./post'); +const postOpenapi = require('./post.openapi.js'); +const canIuse = /^[0-9a-zA-Z_-]{0,64}$/; +const zlib = require('zlib'); module.exports = function(request, response){ @@ -26,25 +26,25 @@ module.exports = function(request, response){ module.exports.go = function(request, response){ - var arr = request.REQUEST.pathname.split('/'); - var appid = context.appid || ''; - var group = arr[3]; - var key = arr[4]; - var groupKey = 'v2.group.alpha'; - var limit = ~~context.limit || 64; - var currPost = post; + var arr = request.REQUEST.pathname.split('/'); + var appid = context.appid || ''; + var group = arr[3]; + var key = arr[4]; + var groupKey = 'v2.group.alpha'; + var limit = ~~context.limit || 64; + var currPost = post; var flagOfDownloadFileFormat = request.query.fileFormat; if(appid){ - currPost = postOpenapi; - groupKey = `${appid}/v2.group.alpha`; + currPost = postOpenapi; + groupKey = `${appid}/v2.group.alpha`; } if(!key){ - key = group; - group = ''; + key = group; + group = ''; } if(!canIuse.test(appid)){ @@ -60,28 +60,28 @@ module.exports.go = function(request, response){ } var createLogKey = function(appid,group,key){ - var logKey = key; + var logKey = key; if(group){ - logKey = `${group}/${logKey}`; + logKey = `${group}/${logKey}`; } return logKey; }; - var logKey = createLogKey(appid,group,key); + var logKey = createLogKey(appid,group,key); if(appid){ - logKey = `${appid}/${logKey}`; + logKey = `${appid}/${logKey}`; } //上下文设置 - context.group = group; - context.limit = limit; - context.key = key; - context.groupKey = groupKey; - context.logKey = logKey; - context.createLogKey = createLogKey; + context.group = group; + context.limit = limit; + context.key = key; + context.groupKey = groupKey; + context.logKey = logKey; + context.createLogKey = createLogKey; logger.debug('logKey :${logKey}',{ logKey: logKey @@ -145,7 +145,7 @@ function returnError(message){ //curr -var initRequestHar = function (request) { +var initRequestHar = function (request) { var unpackRaw = function (requestRaw) { var requestArr = []; // 补全没有的数据 @@ -228,7 +228,7 @@ var initRequestHar = function (request) { return request; }; - var getUrl = function (curr) { + var getUrl = function (curr) { var url; var host; if(curr){ @@ -247,15 +247,14 @@ var initRequestHar = function (request) { requestHeader, responseHeader; - requestHeader = unpackRaw((Buffer.from(request.requestRaw ||'')).toString('utf8')); + requestHeader = unpackRaw((Buffer.from(request.requestRaw ||'')).toString('utf8')); responseHeader = unpackRaw((Buffer.from(request.responseHeader || '')).toString('utf8')); - requestHaz.startedDateTime = request.timestamps.ClientConnected; requestHaz.time = 3000; - requestHaz.request = { + requestHaz.request = { 'method':requestHeader.method, 'url':getUrl(request), 'httpVersion': requestHeader.protocol, @@ -316,7 +315,7 @@ var initRequestHar = function (request) { if(typeof request.responseBody !== 'undefined' && request.responseBody.length !== 0 ){ requestHaz.response.content.size = request.responseBody.length; - var requestResponseBodyBaseBuffer = (Buffer.from(request.responseBody || '', 'base64')); + var requestResponseBodyBaseBuffer = (Buffer.from(request.responseBody || '', 'base64')); // chunked decode if(typeof responseHeader['Transfer-Encoding'] !== 'undefined' && responseHeader['Transfer-Encoding'] === 'chunked'){ @@ -351,11 +350,11 @@ var initRequestHar = function (request) { var downloadHaz = function (request, response, opt) { opt = opt || {}; - var data = opt.data || [], - viewData = [], - filename = opt.id || 'log', - index = parseInt(request.GET.index,10), - SNKey = request.GET.SNKey; + var data = opt.data || [], + viewData = [], + filename = opt.id || 'log', + index = parseInt(request.GET.index,10), + SNKey = request.GET.SNKey; var hazJson = { 'log':{ @@ -380,7 +379,7 @@ var downloadHaz = function (request, response, opt) { return; } - if(typeof data == 'string'){ + if(typeof data == 'string'){ failRet(request, response, 'key类型不对'); return; @@ -433,11 +432,11 @@ var downloadHaz = function (request, response, opt) { var download = function(request, response, opt){ opt = opt || {}; - var data = opt.data || [], - viewData = [], - filename = opt.id || 'log', - index = parseInt(request.GET.index,10), - SNKey = request.GET.SNKey; + var data = opt.data || [], + viewData = [], + filename = opt.id || 'log', + index = parseInt(request.GET.index,10), + SNKey = request.GET.SNKey; if(data.length <= 0){ failRet(request, response, 'not find log'); @@ -453,7 +452,7 @@ var download = function(request, response, opt){ //data = data[0]; - if(typeof data == 'string'){ + if(typeof data == 'string'){ failRet(request, response, 'key类型不对'); return; @@ -488,13 +487,13 @@ var download = function(request, response, opt){ archiver.append(tmpl.download_content_types(), {name: '[Content_Types].xml'}); data.forEach(function(tmp,i){ - var sid = ('0000' + (i + 1)).slice(-3); - tmp.curr = tmp.curr || {}; - tmp.curr.sid = sid + '.0000'; + var sid = ('0000' + (i + 1)).slice(-3); + tmp.curr = tmp.curr || {}; + tmp.curr.sid = sid + '.0000'; - tmp.curr.logText = tmp.curr.logText || ''; - tmp.curr.logText = tmp.curr.logText.replace(/\r\n|\r|\n/gm,'\r\n'); + tmp.curr.logText = tmp.curr.logText || ''; + tmp.curr.logText = tmp.curr.logText.replace(/\r\n|\r|\n/gm,'\r\n'); var logSNKey = post.getLogSN(tmp.curr.logText); var log = { @@ -594,7 +593,6 @@ var failRet = function(request, response, msg){ }; - // chunked decode buffer , 格式 :size +\r\n + rawText +\r\n + 0|r\n , \r\n ====》 13,10, 中间的即为rawText var decodeChunkedUint8Array = function (Uint8ArrayBuffer) { diff --git a/bin/tsw/util/auto-report/highlight-tsw.js b/bin/tsw/util/auto-report/highlight-tsw.js index 0b1979a4..bf313b77 100644 --- a/bin/tsw/util/auto-report/highlight-tsw.js +++ b/bin/tsw/util/auto-report/highlight-tsw.js @@ -7,4 +7,4 @@ */ 'use strict'; -module.exports = plug('../wwwroot/static/tsw/highlightjs/highlight-tsw.js'); \ No newline at end of file +module.exports = plug('../wwwroot/static/tsw/highlightjs/highlight-tsw.js'); diff --git a/bin/tsw/util/auto-report/logReport.js b/bin/tsw/util/auto-report/logReport.js index b5f3f667..b82410f9 100644 --- a/bin/tsw/util/auto-report/logReport.js +++ b/bin/tsw/util/auto-report/logReport.js @@ -44,7 +44,6 @@ var limit = { }; - module.exports = function(req, res){ var window = context.window || {}; @@ -133,7 +132,7 @@ module.exports = function(req, res){ } if(isTST.isTST(req)){ - logger.debug('log type origin: ' + type); + logger.debug('log type origin: ' + type); type = 'TST'; typeKey = 'EVENT_TSW_LOG_TST'; arrtKey = 'SUM_TSW_LOG_TST'; @@ -171,7 +170,7 @@ module.exports = function(req, res){ limit.time = Date.now(); } - logger.debug('logType: ' + type); + logger.debug('logType: ' + type); dcapi.report({ key : typeKey, @@ -497,7 +496,7 @@ module.exports.receiveCloud = function(req,res){ var appid = context.appid; var appkey = context.appkey; - var reportKey = [appid,data.key].join('/'); + var reportKey = [appid,data.key].join('/'); var currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24); var logNumMax = context.MAX_ALPHA_LOG || MAX_ALPHA_LOG; @@ -558,9 +557,6 @@ module.exports.receiveCloud = function(req,res){ }; - - - module.exports.top100 = function(req, res){ var item,str,filename,result; @@ -681,8 +677,6 @@ module.exports.top100 = function(req, res){ buffer.push('\r\n\r\n'); - - result.forEach(function(v,i){ buffer.push(v.ip, ', socket ip: ' + v.socketIp); diff --git a/bin/tsw/util/auto-report/post.js b/bin/tsw/util/auto-report/post.js index 4c2bcefa..0ba1243f 100644 --- a/bin/tsw/util/auto-report/post.js +++ b/bin/tsw/util/auto-report/post.js @@ -57,7 +57,7 @@ module.exports.getLogArr = function(uin,type,key,limit){ var start = data || 0; var keyTextArr = type === 'text' ? module.exports.keyTextArr(uin,start) : module.exports.keyJsonArr(uin,start); - var keyJsonArr = module.exports.keyJsonArr(uin,start); + var keyJsonArr = module.exports.keyJsonArr(uin,start); //如果传递进来key,则只需要关注传进来的key if(key){ @@ -118,7 +118,6 @@ module.exports.getLogArr = function(uin,type,key,limit){ }); }); - return defer; }; @@ -292,6 +291,3 @@ module.exports.decode = function(appid,appkey,body){ return data; }; - - - diff --git a/bin/tsw/util/auto-report/post.openapi.js b/bin/tsw/util/auto-report/post.openapi.js index d8e4b6a9..fa2a0d58 100644 --- a/bin/tsw/util/auto-report/post.openapi.js +++ b/bin/tsw/util/auto-report/post.openapi.js @@ -17,4 +17,3 @@ module.exports.cmem = function(){ return cmemTSW.openapi(); }; - diff --git a/bin/tsw/util/auto-report/src/_config.js b/bin/tsw/util/auto-report/src/_config.js index 09597f20..723cde21 100644 --- a/bin/tsw/util/auto-report/src/_config.js +++ b/bin/tsw/util/auto-report/src/_config.js @@ -20,4 +20,4 @@ define(function(require, exports, module){ }; -}); \ No newline at end of file +}); diff --git a/bin/tsw/util/auto-report/tmpl.js b/bin/tsw/util/auto-report/tmpl.js index fa5bea80..5ff4da38 100644 --- a/bin/tsw/util/auto-report/tmpl.js +++ b/bin/tsw/util/auto-report/tmpl.js @@ -74,7 +74,7 @@ define(function(require, exports, module){ } var pad = function(n){return n < 10 ? '0' + n : n;}, - tz = d.getTimezoneOffset(), + tz = d.getTimezoneOffset(), tzs = (tz > 0 ? '-' : '+') + pad(parseInt(Math.abs(tz / 60))); if (tz === 0){ @@ -94,7 +94,7 @@ define(function(require, exports, module){ + pad(d.getMilliseconds()) + tzs; }; - var timeStamp = data.timestamps || {}, + var timeStamp = data.timestamps || {}, defaultTime = new Date(); __p.push('\n\n = 0) { - defer.resolve(res); - } else { - defer.reject(res); - } - - return defer; -} - -//上报结果 -this.ApiRouteResultUpdate = function(options){ - - var defer = Deferred.create(); - var res = this.ApiRouteResultUpdateSync(options); - - if(res.ret >= 0){ - defer.resolve(res); - }else{ - defer.reject(res); - } - - return defer; -} - -//获取路由 -this.ApiGetRouteSync = function(options){ - - var res = { - ret: -1 - }; - - return res; -}; - -//上报结果 -this.ApiRouteResultUpdateSync = function(options){ - - var res = { - ret: -1 - }; - - return res; -} - -//多一组方法 -this.updateRoute = this.ApiRouteResultUpdateSync; -this.getRouteAsync = this.ApiGetRoute; +/*! + * Tencent is pleased to support the open source community by making Tencent Server Web available. + * Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + * http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + */ +"use strict"; + +const logger = require('logger'); +const Deferred = require('util/Deferred'); +const config = require('config'); + +//获取路由 +this.ApiGetRoute = function(options,callback){ + + var defer = Deferred.create(); + var res = this.ApiGetRouteSync(options) || {}; + + if (typeof callback === 'function') { + callback(res); + } + + if (res.ret >= 0) { + defer.resolve(res); + } else { + defer.reject(res); + } + + return defer; +} + +//上报结果 +this.ApiRouteResultUpdate = function(options){ + + var defer = Deferred.create(); + var res = this.ApiRouteResultUpdateSync(options); + + if(res.ret >= 0){ + defer.resolve(res); + }else{ + defer.reject(res); + } + + return defer; +} + +//获取路由 +this.ApiGetRouteSync = function(options){ + + var res = { + ret: -1 + }; + + return res; +}; + +//上报结果 +this.ApiRouteResultUpdateSync = function(options){ + + var res = { + ret: -1 + }; + + return res; +} + +//多一组方法 +this.updateRoute = this.ApiRouteResultUpdateSync; +this.getRouteAsync = this.ApiGetRoute; diff --git a/bin/lib/api/cmdb/index.js b/bin/lib/api/cmdb/index.js index bf0966e6..6f8e1154 100644 --- a/bin/lib/api/cmdb/index.js +++ b/bin/lib/api/cmdb/index.js @@ -7,7 +7,7 @@ */ 'use strict'; -const Deferred = require('util/Deferred'); +const Deferred = require('util/Deferred'); this.GetDeviceThisServer = function(){ @@ -20,13 +20,13 @@ this.GetDeviceThisServer = function(){ * 查询父业务树信息 */ this.GetAllParentBusiness = function(opt){ - var defer = Deferred.create(); + var defer = Deferred.create(); return defer.resolve(null); }; this.GetDeviceByIp = function(opt){ - var defer = Deferred.create(); + var defer = Deferred.create(); return defer.resolve(null); }; diff --git a/bin/lib/api/code/watcher.js b/bin/lib/api/code/watcher.js index 407cd12e..c86ea750 100644 --- a/bin/lib/api/code/watcher.js +++ b/bin/lib/api/code/watcher.js @@ -8,6 +8,6 @@ 'use strict'; this.watch = function(){ - + }; diff --git a/bin/lib/api/keyman/alphaMap.js b/bin/lib/api/keyman/alphaMap.js index 63c6fad3..845ce000 100644 --- a/bin/lib/api/keyman/alphaMap.js +++ b/bin/lib/api/keyman/alphaMap.js @@ -7,15 +7,15 @@ */ 'use strict'; -const Deferred = require('util/Deferred'); -const logger = require('logger'); -const config = require('config'); -const fs = require('fs'); -const fileCache = require('api/fileCache'); -const fileUrl = config.alphaFileUrl; - -var isFirstLoad = false; -var cache = { +const Deferred = require('util/Deferred'); +const logger = require('logger'); +const config = require('config'); +const fs = require('fs'); +const fileCache = require('api/fileCache'); +const fileUrl = config.alphaFileUrl; + +var isFirstLoad = false; +var cache = { timeUpdate: 0, data: {}, dataFile: {} @@ -51,7 +51,7 @@ if(isFirstLoad){ return; } - cache.dataFile = getMap(text); + cache.dataFile = getMap(text); updateMap(); })(); @@ -63,7 +63,7 @@ if(isFirstLoad){ function getMap(text){ - var map = {}; + var map = {}; text = text || ''; @@ -76,7 +76,7 @@ function getMap(text){ function updateMap(text){ - var map = getMap(text); + var map = getMap(text); //copy Object.assign(map,cache.dataFile); @@ -89,48 +89,48 @@ function updateMap(text){ this.getSync = function(){ this.get(); - + return cache.data; }; this.get = function(){ - - var defer = Deferred.create(); - var delay = (process.serverInfo && process.serverInfo.cpu * 1000) || 0; - var l5api = config.tswL5api['alphaFileUrl']; + + var defer = Deferred.create(); + var delay = (process.serverInfo && process.serverInfo.cpu * 1000) || 0; + var l5api = config.tswL5api['alphaFileUrl']; if(Date.now() - cache.timeUpdate < 60000 + delay){ return defer.resolve(cache.data); } - + cache.timeUpdate = Date.now(); if(!fileUrl){ return defer.resolve(cache.data); } - + fileCache.getAsync(fileUrl).done(function(d){ - + var lastModifyTime = 0; var text = ''; - + if(d && d.stats){ lastModifyTime = d.stats.mtime.getTime(); } - + if(d && d.data){ text = d.data.toString('utf-8'); } - + if(Date.now() - lastModifyTime < 60000){ logger.debug('使用本地文件'); - + updateMap(text); - + defer.resolve(cache.data); return; } - + require('ajax').request({ url: fileUrl, type: 'get', @@ -146,11 +146,11 @@ this.get = function(){ }).fail(function(d){ defer.resolve(cache.data); }).done(function(d){ - - var text = ''; - + + var text = ''; + if(d && d.result && typeof d.result === 'string'){ - + text = d.result; } @@ -158,17 +158,17 @@ this.get = function(){ logger.error('alpha file limit <=2M'); return defer.resolve(cache.data); } - + updateMap(text); - + //保存在本地 fileCache.set(fileUrl,Buffer.from(text,'UTF-8')); - + defer.resolve(cache.data); }); }); - - + + return defer; }; diff --git a/bin/lib/api/keyman/blackIpMap.js b/bin/lib/api/keyman/blackIpMap.js index 21cb31c9..40095074 100644 --- a/bin/lib/api/keyman/blackIpMap.js +++ b/bin/lib/api/keyman/blackIpMap.js @@ -7,15 +7,15 @@ */ 'use strict'; -const Deferred = require('util/Deferred'); -const logger = require('logger'); -const config = require('config'); -const fs = require('fs'); -const fileCache = require('api/fileCache'); -const fileUrl = config.blackIpFileUrl; +const Deferred = require('util/Deferred'); +const logger = require('logger'); +const config = require('config'); +const fs = require('fs'); +const fileCache = require('api/fileCache'); +const fileUrl = config.blackIpFileUrl; var isFirstLoad = false; -var cache = { +var cache = { timeUpdate: 0, data: {}, dataFile: {} @@ -89,7 +89,7 @@ this.getSync = function(){ function getMap(text){ - var map = {}; + var map = {}; text = text || ''; @@ -102,7 +102,7 @@ function getMap(text){ function updateMap(text){ - var map = getMap(text); + var map = getMap(text); //copy Object.assign(map,cache.dataFile); @@ -113,43 +113,43 @@ function updateMap(text){ } this.get = function(){ - - var defer = Deferred.create(); - var delay = ((process.serverInfo && process.serverInfo.cpu) * 1000) || 0; - var l5api = config.tswL5api['blackIpFileUrl']; - + + var defer = Deferred.create(); + var delay = ((process.serverInfo && process.serverInfo.cpu) * 1000) || 0; + var l5api = config.tswL5api['blackIpFileUrl']; + if(Date.now() - cache.timeUpdate < 300000 + delay){ return defer.resolve(cache.data); } - + cache.timeUpdate = Date.now(); if(!fileUrl){ return defer.resolve(cache.data); } - + fileCache.getAsync(fileUrl).done(function(d){ - + var lastModifyTime = 0; var text = ''; - + if(d && d.stats){ lastModifyTime = d.stats.mtime.getTime(); } - + if(d && d.data){ text = d.data.toString('utf-8'); } - + if(Date.now() - lastModifyTime < 300000){ logger.debug('使用本地文件'); - + updateMap(text); - + defer.resolve(cache.data); return; } - + require('ajax').request({ url: fileUrl, type: 'get', @@ -165,11 +165,11 @@ this.get = function(){ }).fail(function(d){ defer.resolve(cache.data); }).done(function(d){ - - var text = ''; - + + var text = ''; + if(d && d.result && typeof d.result === 'string'){ - + text = d.result; } @@ -177,17 +177,17 @@ this.get = function(){ logger.error('blackIp file limit <=2M'); return defer.resolve(cache.data); } - + updateMap(text); - + //保存在本地 fileCache.set(fileUrl,Buffer.from(text,'UTF-8')); - + defer.resolve(cache.data); }); }); - - + + return defer; }; diff --git a/bin/lib/api/keyman/index.js b/bin/lib/api/keyman/index.js index d1dae300..27d40dd9 100644 --- a/bin/lib/api/keyman/index.js +++ b/bin/lib/api/keyman/index.js @@ -31,10 +31,10 @@ this.getAllGroup = function(){ return require('util/auto-report/TEReport.js').getAllGroup(); }; -this.ipCheck = function(opt){ +this.ipCheck = function(opt){ return require('./ipCheck.js').info(opt); }; -this.runtimeAdd = function(opt){ +this.runtimeAdd = function(opt){ return require('./runtimeAdd.js').add(opt); }; diff --git a/bin/lib/api/keyman/ipCheck.js b/bin/lib/api/keyman/ipCheck.js index 39602cfe..80224d01 100644 --- a/bin/lib/api/keyman/ipCheck.js +++ b/bin/lib/api/keyman/ipCheck.js @@ -7,10 +7,10 @@ */ 'use strict'; -const Deferred = require('util/Deferred'); +const Deferred = require('util/Deferred'); this.info = function(opt){ - var defer = Deferred.create(); + var defer = Deferred.create(); return defer.reject(); }; diff --git a/bin/lib/api/keyman/runtimeAdd.js b/bin/lib/api/keyman/runtimeAdd.js index 5de3bf6b..b995f898 100644 --- a/bin/lib/api/keyman/runtimeAdd.js +++ b/bin/lib/api/keyman/runtimeAdd.js @@ -7,11 +7,11 @@ */ 'use strict'; -const Deferred = require('util/Deferred'); +const Deferred = require('util/Deferred'); this.add = function(opt){ - - var defer = Deferred.create(); + + var defer = Deferred.create(); return defer.resolve(); }; diff --git a/bin/lib/api/libdcapi/dcapi.js b/bin/lib/api/libdcapi/dcapi.js index ef0d4fad..3fd5337d 100644 --- a/bin/lib/api/libdcapi/dcapi.js +++ b/bin/lib/api/libdcapi/dcapi.js @@ -9,8 +9,8 @@ this.baselog = function(table, param) { return this.reportLog({ - table : table, - data : param + table : table, + data : param }); }; @@ -19,6 +19,6 @@ this.reportLog = function(opt){ }; this.report = function(opt) { - + }; diff --git a/bin/lib/api/tnm2/index.js b/bin/lib/api/tnm2/index.js index 8484b289..aabecae7 100644 --- a/bin/lib/api/tnm2/index.js +++ b/bin/lib/api/tnm2/index.js @@ -134,26 +134,26 @@ var reportOpenapi = function(last){ now : Date.now() }; - var sig = openapi.signature({ + var sig = openapi.signature({ pathname: url.parse(config.appReportUrl).pathname, method: 'POST', data: postData, appkey: config.appkey }); - postData.sig = sig; + postData.sig = sig; require('ajax').request({ - url : config.appReportUrl, - type : 'POST', - l5api : config.tswL5api['openapi.tswjs.org'], - dcapi : { + url : config.appReportUrl, + type : 'POST', + l5api : config.tswL5api['openapi.tswjs.org'], + dcapi : { key: 'EVENT_TSW_OPENAPI_APP_REPORT' }, - data : postData, - keepAlive : true, - autoToken : false, - dataType : 'json' + data : postData, + keepAlive : true, + autoToken : false, + dataType : 'json' }).fail(function(){ logger.error('app report fail.'); defer.reject(); diff --git a/bin/lib/api/xssFilter/index.js b/bin/lib/api/xssFilter/index.js index 4791a489..cf1b8de1 100644 --- a/bin/lib/api/xssFilter/index.js +++ b/bin/lib/api/xssFilter/index.js @@ -7,10 +7,10 @@ */ 'use strict'; -const Deferred = require('util/Deferred'); +const Deferred = require('util/Deferred'); this.check = function(){ - var defer = Deferred.create(); + var defer = Deferred.create(); return defer.resolve(); }; diff --git a/bin/lib/data/cmem.tsw.js b/bin/lib/data/cmem.tsw.js index 735ad166..f0680aa0 100644 --- a/bin/lib/data/cmem.tsw.js +++ b/bin/lib/data/cmem.tsw.js @@ -7,8 +7,8 @@ */ 'use strict'; -const config = require('config'); -const cmem = require('pool/cmem.l5.js'); +const config = require('config'); +const cmem = require('pool/cmem.l5.js'); this.cmem = function(){ diff --git a/bin/lib/default/config.default.js b/bin/lib/default/config.default.js index c428347d..50d90be3 100644 --- a/bin/lib/default/config.default.js +++ b/bin/lib/default/config.default.js @@ -46,16 +46,16 @@ this.websoDstPort = this.httpPort; this.websoDstIp = '127.0.0.1'; //alpha号码文件 -this.alphaFile = null; +this.alphaFile = null; //alpha号码文件 -this.alphaFileUrl = null; +this.alphaFileUrl = null; //ip黑名单文件 -this.blackIpFile = null; +this.blackIpFile = null; //ip黑名单文件 -this.blackIpFileUrl = null; +this.blackIpFileUrl = null; //邮件接收者 this.mailTo = ''; @@ -112,11 +112,11 @@ this.idc = 'sz'; this.l5api = {}; this.timeout = { - socket : 120000, - post : 30000, - get : 10000, - keepAlive : 10000, - dns : 3000 + socket : 120000, + post : 30000, + get : 10000, + keepAlive : 10000, + dns : 3000 }; @@ -125,11 +125,11 @@ this.extendMod = { }; //openapi -this.logReportUrl = 'https://openapi.tswjs.org/v1/log/report'; -this.h5testSyncUrl = 'https://openapi.tswjs.org/v1/h5test/sync'; -this.utilCDUrl = 'https://openapi.tswjs.org/v1/util/cd'; -this.appReportUrl = 'https://openapi.tswjs.org/v1/app/report'; -this.runtimeReportUrl = 'https://openapi.tswjs.org/v1/runtime/report'; +this.logReportUrl = 'https://openapi.tswjs.org/v1/log/report'; +this.h5testSyncUrl = 'https://openapi.tswjs.org/v1/h5test/sync'; +this.utilCDUrl = 'https://openapi.tswjs.org/v1/util/cd'; +this.appReportUrl = 'https://openapi.tswjs.org/v1/app/report'; +this.runtimeReportUrl = 'https://openapi.tswjs.org/v1/runtime/report'; this.ignoreTST = false; @@ -137,9 +137,9 @@ this.ajaxDefaultOptions = { useJsonParseOnly : true }; -this.tswL5api = {}; -this.tswL5api['cmem.tsw.sz'] = null; -this.tswL5api['cmem.tsw.sh'] = this.tswL5api['cmem.tsw.sz']; -this.tswL5api['cmem.tsw.tj'] = this.tswL5api['cmem.tsw.sh']; -this.tswL5api['cmem.tsw.h5test'] = this.tswL5api['cmem.tsw.sz']; +this.tswL5api = {}; +this.tswL5api['cmem.tsw.sz'] = null; +this.tswL5api['cmem.tsw.sh'] = this.tswL5api['cmem.tsw.sz']; +this.tswL5api['cmem.tsw.tj'] = this.tswL5api['cmem.tsw.sh']; +this.tswL5api['cmem.tsw.h5test'] = this.tswL5api['cmem.tsw.sz']; diff --git a/bin/lib/default/logReport.js b/bin/lib/default/logReport.js index 746af273..4302ffb3 100644 --- a/bin/lib/default/logReport.js +++ b/bin/lib/default/logReport.js @@ -7,21 +7,21 @@ */ 'use strict'; -const config = require('config'); +const config = require('config'); module.exports.report = function(data){ - var retValue; + var retValue; var reportData = { table:'reportData', data:{ - type : data.type, - log : data.logText, - uin : data.key, - mod_act : data.mod_act, - ua : data.ua, - userip : data.userip, - domain : data.host, - path : data.pathname, + type : data.type, + log : data.logText, + uin : data.key, + mod_act : data.mod_act, + ua : data.ua, + userip : data.userip, + domain : data.host, + path : data.pathname, httpCode: data.statusCode } }; diff --git a/bin/lib/util/gzipHttp.js b/bin/lib/util/gzipHttp.js index 175ef039..3da56175 100644 --- a/bin/lib/util/gzipHttp.js +++ b/bin/lib/util/gzipHttp.js @@ -7,25 +7,25 @@ */ 'use strict'; -const zlib = require('zlib'); -const logger = require('logger'); -const crypto = require('crypto'); -const httpUtil = require('util/http.js'); -const defaultChunkSize = 8 * 1024; +const zlib = require('zlib'); +const logger = require('logger'); +const crypto = require('crypto'); +const httpUtil = require('util/http.js'); +const defaultChunkSize = 8 * 1024; -this.httpUtil = httpUtil; +this.httpUtil = httpUtil; this.create = this.getGzipResponse = function(opt){ var window = context.window || {}; opt = opt || {}; - var request = opt.request || window.request, - response = opt.response ||window.response, - code = opt.code || 200, - headers = opt.headers || null, - chunkSize = opt.chunkSize || defaultChunkSize, - contentType = opt.contentType || (headers && headers['content-type']) || 'text/html; charset=UTF-8', + var request = opt.request || window.request, + response = opt.response ||window.response, + code = opt.code || 200, + headers = opt.headers || null, + chunkSize = opt.chunkSize || defaultChunkSize, + contentType = opt.contentType || (headers && headers['content-type']) || 'text/html; charset=UTF-8', gzipOutputStream; if(headers && headers['content-length'] !== undefined){ @@ -36,17 +36,17 @@ this.create = this.getGzipResponse = function(opt){ } response.setHeader('Content-Type', contentType); - + if(/\bgzip\b/.test(request.headers['accept-encoding'])){ - + response.setHeader('Content-Encoding', 'gzip'); response.writeHead(code,headers); - + response.socket && response.socket.setNoDelay(true); gzipOutputStream = zlib.createGzip({ chunkSize: chunkSize }); - + gzipOutputStream.on('data',function(buffer){ logger.debug('gzip chunked send ${len}',{ len: buffer.length @@ -56,23 +56,23 @@ this.create = this.getGzipResponse = function(opt){ response.write(buffer); } }); - + gzipOutputStream.once('end',function(){ response.end(); }); - + return gzipOutputStream; - + }else{ - + response.writeHead(code,headers); - + response.on('data',function(buffer){ logger.debug('chunked send ${len}',{ len: buffer.length }); }); - + if(!response.flush){ response.flush = function(){return true;}; } @@ -80,10 +80,10 @@ this.create = this.getGzipResponse = function(opt){ if(response.flush && response.flushHeaders){ response.flush = function(){return true;}; } - + return response; } - + }; @@ -91,6 +91,6 @@ this.create = this.getGzipResponse = function(opt){ 从buffer获取SHA1的方法,独立出来其实是为了方便测试 */ exports.getSHA1 = function(buffer){ - return crypto.createHash('sha1').update(buffer).digest('hex'); + return crypto.createHash('sha1').update(buffer).digest('hex'); }; diff --git a/bin/lib/util/isTST.js b/bin/lib/util/isTST.js index d443929f..37936c98 100644 --- a/bin/lib/util/isTST.js +++ b/bin/lib/util/isTST.js @@ -7,7 +7,7 @@ */ 'use strict'; -const config = require('config'); +const config = require('config'); //是否安全扫描器请求 this.isTST = function(request){ diff --git a/bin/lib/util/mail/mail.js b/bin/lib/util/mail/mail.js index 7bcab8dd..a0175643 100644 --- a/bin/lib/util/mail/mail.js +++ b/bin/lib/util/mail/mail.js @@ -7,20 +7,20 @@ */ 'use strict'; -const serverInfo = require('serverInfo.js'); -const config = require('config.js'); -const isWindows = require('util/isWindows.js'); -const httpUtil = require('util/http'); -const logger = require('logger'); -const Deferred = require('util/Deferred'); -const url = require('url'); +const serverInfo = require('serverInfo.js'); +const config = require('config.js'); +const isWindows = require('util/isWindows.js'); +const httpUtil = require('util/http'); +const logger = require('logger'); +const Deferred = require('util/Deferred'); +const url = require('url'); this.SendMail = function(key,group,second,oriOpt){ - var opt = Deferred.extend({},oriOpt); - var data = {}; - var now = new Date(); - var prefix = '[runtime]'; + var opt = Deferred.extend({},oriOpt); + var data = {}; + var now = new Date(); + var prefix = '[runtime]'; if(isWindows.isWindows){ return; @@ -37,29 +37,29 @@ this.SendMail = function(key,group,second,oriOpt){ } if(config.isTest){ - prefix += '[测试环境]'; + prefix += '[测试环境]'; }else{ if(opt.runtimeType){ prefix += `[${opt.runtimeType}][考核]`; } } - opt.Title = prefix + opt.Title; + opt.Title = prefix + opt.Title; - data.Title = opt.Title || ''; - data.isTest = ~~config.isTest; - data.Content = opt.Content || ''; - data.MsgInfo = opt.MsgInfo || ''; - data.intranetIp = serverInfo.intranetIp || ''; - data.second = second || ''; - data.idc = config.idc || ''; - data.logText = logger.getText() || ''; - data.headerText = httpUtil.getRequestHeaderStr() || ''; - data.runtimeType = opt.runtimeType || ''; - data.processTitle = process.title || ''; - data.processPid = process.pid || ''; + data.Title = opt.Title || ''; + data.isTest = ~~config.isTest; + data.Content = opt.Content || ''; + data.MsgInfo = opt.MsgInfo || ''; + data.intranetIp = serverInfo.intranetIp || ''; + data.second = second || ''; + data.idc = config.idc || ''; + data.logText = logger.getText() || ''; + data.headerText = httpUtil.getRequestHeaderStr() || ''; + data.runtimeType = opt.runtimeType || ''; + data.processTitle = process.title || ''; + data.processPid = process.pid || ''; - opt.data = data; + opt.data = data; if(isWindows.isWindows){ key = key + Date.now(); @@ -100,29 +100,29 @@ var reportOpenapi = function(data){ var postData = data; - postData.appid = config.appid; - postData.now = Date.now(); + postData.appid = config.appid; + postData.now = Date.now(); - var sig = openapi.signature({ - pathname : url.parse(config.runtimeReportUrl).pathname, - method : 'POST', - data : postData, - appkey : config.appkey + var sig = openapi.signature({ + pathname : url.parse(config.runtimeReportUrl).pathname, + method : 'POST', + data : postData, + appkey : config.appkey }); - postData.sig = sig; + postData.sig = sig; require('ajax').request({ - url : config.runtimeReportUrl, - type : 'POST', - l5api : config.tswL5api['openapi.tswjs.org'], - dcapi : { + url : config.runtimeReportUrl, + type : 'POST', + l5api : config.tswL5api['openapi.tswjs.org'], + dcapi : { key: 'EVENT_TSW_OPENAPI_RUNTIME_REPORT' }, - data : postData, - keepAlive : true, - autoToken : false, - dataType : 'json' + data : postData, + keepAlive : true, + autoToken : false, + dataType : 'json' }).fail(function(){ logger.error('runtime report fail.'); defer.reject(); @@ -152,7 +152,7 @@ this.SendTSWMail = function(opt){ //发送周知邮件 this.SendArsMail = function(opt){ - + }; diff --git a/bin/lib/util/mail/src/_config.js b/bin/lib/util/mail/src/_config.js index 723cde21..0a1aae4c 100644 --- a/bin/lib/util/mail/src/_config.js +++ b/bin/lib/util/mail/src/_config.js @@ -10,14 +10,14 @@ define(function(require, exports, module){ return { - + tmpl: { create: true }, all: { create: false } - + }; - + }); diff --git a/bin/lib/util/mail/tmpl.js b/bin/lib/util/mail/tmpl.js index 6ec2b32d..fdf0d409 100644 --- a/bin/lib/util/mail/tmpl.js +++ b/bin/lib/util/mail/tmpl.js @@ -11,32 +11,32 @@ define(function(require, exports, module){ var window = context.window || {}; - __p.push('\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n

'); + __p.push('\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n

'); _p(data.Content); - __p.push('

\n

服务器IP: '); + __p.push('

\n

服务器IP: '); _p(data.intranetIp); - __p.push('

\n\n

进程名字: '); + __p.push('

\n\n

进程名字: '); _p(tmpl.encodeHtml(data.processTitle)); - __p.push('

\n

进程ID: '); + __p.push('

\n

进程ID: '); _p(data.processPid.pid); - __p.push('

\n

mod_act: '); + __p.push('

\n

mod_act: '); _p(context.mod_act || 'null'); __p.push('

'); - if(window.request){__p.push('

域名: '); + if(window.request){__p.push('

域名: '); _p(window.request && window.request.headers.host); - __p.push('

\n

path: '); + __p.push('

\n

path: '); _p(window.request && window.request.REQUEST.pathname); __p.push('

'); - }__p.push('

idc: '); + }__p.push('

idc: '); _p(data.idc); - __p.push('

\n

发送间隔: '); + __p.push('

\n

发送间隔: '); _p(data.second); __p.push('s

'); - if(data.headerText){__p.push('

请求头:

\n
'); + if(data.headerText){__p.push('

请求头:

\n
'); _p(tmpl.encodeHtml(data.headerText || '').replace(/\r\n|\r|\n/g,'
')); __p.push('
'); - }__p.push(' '); - if(data.logText){__p.push('

全息日志:

\n
');
+            }__p.push('    ');
+            if(data.logText){__p.push('    

全息日志:

\n
');
                 _p(tmpl.encodeHtml(data.logText || '').replace(/\r\n|\r|\n/g,'
')); __p.push('
'); }__p.push('\n'); diff --git a/bin/proxy/admin.js b/bin/proxy/admin.js index ab7f065e..0bfec417 100644 --- a/bin/proxy/admin.js +++ b/bin/proxy/admin.js @@ -8,12 +8,12 @@ 'use strict'; -const logger = require('logger'); -const config = require('./config.js'); -const http = require('http'); -const codeWatch = require('api/code/watcher.js'); -const parseGet = require('util/http/parseGet.js'); -const cp = require('child_process'); +const logger = require('logger'); +const config = require('./config.js'); +const http = require('http'); +const codeWatch = require('api/code/watcher.js'); +const parseGet = require('util/http/parseGet.js'); +const cp = require('child_process'); const server = http.createServer(function(req, res){ diff --git a/bin/proxy/config.js b/bin/proxy/config.js index 17d9cfb9..49b60246 100644 --- a/bin/proxy/config.js +++ b/bin/proxy/config.js @@ -7,14 +7,14 @@ */ 'use strict'; -const fs = require('fs'); -const path = require('path'); -const plug = require('plug'); -const Deferred = plug('util/Deferred'); -const defaultValue = plug('default/config.default.js'); - -var isFirstLoad = false; -var cache = { +const fs = require('fs'); +const path = require('path'); +const plug = require('plug'); +const Deferred = plug('util/Deferred'); +const defaultValue = plug('default/config.default.js'); + +var isFirstLoad = false; +var cache = { config: null }; diff --git a/bin/proxy/http.proxy.js b/bin/proxy/http.proxy.js index 6a516a20..ccc11791 100644 --- a/bin/proxy/http.proxy.js +++ b/bin/proxy/http.proxy.js @@ -12,37 +12,37 @@ process.on('uncaughtException',function(e){ }); -const logger = require('logger'); -const http = require('http'); -const https = require('https'); -const cluster = require('cluster'); -const util = require('util'); -const fs = require('fs'); -const cp = require('child_process'); -const parseGet = require('util/http/parseGet.js'); -const tnm2 = require('api/tnm2'); -const cpuUtil = require('util/cpu.js'); -const httpUtil = require('util/http.js'); -const TEReport = require('util/auto-report/TEReport.js'); -const mail = require('util/mail/mail.js'); -const websocket = require('./websocket.js'); -const packageJSON = require('../../package.json'); -const headerServer = `TSW/${packageJSON.version}`; -const methodMap = {}; -const {isWindows} = require('util/isWindows.js'); -const {debugOptions} = process.binding('config'); -const serverInfo = { +const logger = require('logger'); +const http = require('http'); +const https = require('https'); +const cluster = require('cluster'); +const util = require('util'); +const fs = require('fs'); +const cp = require('child_process'); +const parseGet = require('util/http/parseGet.js'); +const tnm2 = require('api/tnm2'); +const cpuUtil = require('util/cpu.js'); +const httpUtil = require('util/http.js'); +const TEReport = require('util/auto-report/TEReport.js'); +const mail = require('util/mail/mail.js'); +const websocket = require('./websocket.js'); +const packageJSON = require('../../package.json'); +const headerServer = `TSW/${packageJSON.version}`; +const methodMap = {}; +const {isWindows} = require('util/isWindows.js'); +const {debugOptions} = process.binding('config'); +const serverInfo = { intranetIp: require('serverInfo.js').intranetIp, cpu: 'X' }; var server; var serverThis; var serverHttps; -var config = require('./config.js'); -var routeCache = null; -var cleanCacheTid = null; -var isStartHeartBeat = false; -var heartBeatCount = 0; +var config = require('./config.js'); +var routeCache = null; +var cleanCacheTid = null; +var isStartHeartBeat = false; +var heartBeatCount = 0; function doRoute(req,res){ @@ -98,7 +98,7 @@ function doRoute(req,res){ routeCache(req,res); } -process.serverInfo = serverInfo; +process.serverInfo = serverInfo; /** * 清除缓存 @@ -156,8 +156,8 @@ process.on('profiler',function(data = {}){ //process.emit('globaldump',m.GET); process.on('globaldump',function(GET){ - var cpu = GET.cpu || 0; - var depth = GET.depth || 6; + var cpu = GET.cpu || 0; + var depth = GET.depth || 6; if(cpu != serverInfo.cpu){ return; @@ -212,12 +212,12 @@ function requestHandler(req, res){ } -server = http.createServer(requestHandler); -serverThis = http.createServer(requestHandler); +server = http.createServer(requestHandler); +serverThis = http.createServer(requestHandler); -server.timeout = Math.max(config.timeout.upload || config.timeout.socket,0); -serverThis.timeout = Math.max(config.timeout.upload || config.timeout.socket,0); -server.keepAliveTimeout = Math.max(config.timeout.keepAlive,0); +server.timeout = Math.max(config.timeout.upload || config.timeout.socket,0); +serverThis.timeout = Math.max(config.timeout.upload || config.timeout.socket,0); +server.keepAliveTimeout = Math.max(config.timeout.keepAlive,0); serverThis.keepAliveTimeout = Math.max(config.timeout.keepAlive,0); global.TSW_HTTP_SERVER = server; @@ -233,8 +233,8 @@ if(config.httpsOptions){ socket.end('HTTP/1.1 400 Bad Request\r\n\r\n'); }); - serverHttps.timeout = Math.max(config.timeout.upload || config.timeout.socket,0); - serverHttps.keepAliveTimeout = Math.max(config.timeout.keepAlive,0); + serverHttps.timeout = Math.max(config.timeout.upload || config.timeout.socket,0); + serverHttps.keepAliveTimeout = Math.max(config.timeout.keepAlive,0); global.TSW_HTTPS_SERVER = serverHttps; } @@ -260,7 +260,7 @@ function startHeartBeat(){ isStartHeartBeat = true; - global.cpuUsed = 0; + global.cpuUsed = 0; //定时给父进程发送心跳包 setInterval(function(){ @@ -294,8 +294,8 @@ function startHeartBeat(){ //高负载告警 if ( global.cpuUsed80 === 4 && - !config.isTest && - !isWindows + !config.isTest && + !isWindows ) { //取进程快照 //ps aux --sort=-pcpu @@ -305,7 +305,7 @@ function startHeartBeat(){ }, timeout: 5000 },function(err,data,errData) { - var key = ['cpu80.v4',serverInfo.intranetIp].join(':'); + var key = ['cpu80.v4',serverInfo.intranetIp].join(':'); var Content = [ '单核CPU' + serverInfo.cpu + '使用率为:' + cpuUsed + ',超过80%, 最近5秒钟CPU Profiler见附件' ].join('
'); @@ -340,11 +340,11 @@ function startHeartBeat(){ recordTime: 5000 }, result => { mail.SendMail(key, 'js', 600, { - 'To' : config.mailTo, - 'CC' : owner, - 'MsgInfo' : business.module + '[CPU]' + serverInfo.intranetIp + '单核CPU' + serverInfo.cpu + '使用率为:' + cpuUsed + ',超过80%', - 'Title' : business.module + '[CPU]' + serverInfo.intranetIp + '单核CPU' + serverInfo.cpu + '使用率为:' + cpuUsed + ',超过80%', - 'Content' : Content, + 'To' : config.mailTo, + 'CC' : owner, + 'MsgInfo' : business.module + '[CPU]' + serverInfo.intranetIp + '单核CPU' + serverInfo.cpu + '使用率为:' + cpuUsed + ',超过80%', + 'Title' : business.module + '[CPU]' + serverInfo.intranetIp + '单核CPU' + serverInfo.cpu + '使用率为:' + cpuUsed + ',超过80%', + 'Content' : Content, 'attachment': result ? { fileType : true, dispositionType: 'attachment', @@ -357,11 +357,11 @@ function startHeartBeat(){ }); } - let currMemory = process.memoryUsage(); + let currMemory = process.memoryUsage(); - tnm2.Attr_API_Set('AVG_TSW_MEMORY_RSS', currMemory.rss); - tnm2.Attr_API_Set('AVG_TSW_MEMORY_HEAP', currMemory.heapTotal); - tnm2.Attr_API_Set('AVG_TSW_MEMORY_EXTERNAL', currMemory.external); + tnm2.Attr_API_Set('AVG_TSW_MEMORY_RSS', currMemory.rss); + tnm2.Attr_API_Set('AVG_TSW_MEMORY_HEAP', currMemory.heapTotal); + tnm2.Attr_API_Set('AVG_TSW_MEMORY_EXTERNAL', currMemory.external); },5000); @@ -419,9 +419,9 @@ methodMap.top100 = function(m){ //监听端口 methodMap.listen = function(message){ - var user_00 = config.workerUid || 'user_00'; - serverInfo.cpu = message.cpu || 0; - global.cpuUsed = cpuUtil.getCpuUsed(serverInfo.cpu); + var user_00 = config.workerUid || 'user_00'; + serverInfo.cpu = message.cpu || 0; + global.cpuUsed = cpuUtil.getCpuUsed(serverInfo.cpu); process.title = 'TSW/worker/' + serverInfo.cpu; global.TSW_HTTP_WORKER_PORT = config.workerPortBase + serverInfo.cpu; diff --git a/bin/proxy/http.route.js b/bin/proxy/http.route.js index 05923527..9e97767b 100644 --- a/bin/proxy/http.route.js +++ b/bin/proxy/http.route.js @@ -7,28 +7,28 @@ */ 'use strict'; -const logger = require('logger'); -const domain = require('domain'); -const serverInfo = require('serverInfo.js'); -const config = require('./config.js'); -const dcapi = require('api/libdcapi/dcapi.js'); -const {isWindows} = require('util/isWindows.js'); -const contextMod = require('context.js'); -const Context = require('runtime/Context'); -const Window = require('runtime/Window'); -const xssFilter = require('api/xssFilter'); -const alpha = require('util/auto-report/alpha.js'); -const httpUtil = require('util/http.js'); -const isTST = require('util/isTST.js'); -const h5test = require('util/h5-test/is-test'); -const logReport = require('util/auto-report/logReport.js'); -const httpModAct = require('./http.mod.act.js'); -const httpModMap = require('./http.mod.map.js'); -const mail = require('util/mail/mail.js'); -const CCFinder = require('runtime/CCFinder.js'); -const parseBody = require('util/http/parseBody.js'); -const TSW = require('api/keyman'); -const tnm2 = require('api/tnm2'); +const logger = require('logger'); +const domain = require('domain'); +const serverInfo = require('serverInfo.js'); +const config = require('./config.js'); +const dcapi = require('api/libdcapi/dcapi.js'); +const {isWindows} = require('util/isWindows.js'); +const contextMod = require('context.js'); +const Context = require('runtime/Context'); +const Window = require('runtime/Window'); +const xssFilter = require('api/xssFilter'); +const alpha = require('util/auto-report/alpha.js'); +const httpUtil = require('util/http.js'); +const isTST = require('util/isTST.js'); +const h5test = require('util/h5-test/is-test'); +const logReport = require('util/auto-report/logReport.js'); +const httpModAct = require('./http.mod.act.js'); +const httpModMap = require('./http.mod.map.js'); +const mail = require('util/mail/mail.js'); +const CCFinder = require('runtime/CCFinder.js'); +const parseBody = require('util/http/parseBody.js'); +const TSW = require('api/keyman'); +const tnm2 = require('api/tnm2'); module.exports = function(req,res){ @@ -36,10 +36,10 @@ module.exports = function(req,res){ process.SN = process.SN || 0; var timeLimit = httpUtil.isPostLike(req) ? config.timeout.post : config.timeout.get; - var start = new Date(); - var d = domain.create(); - var tid = null; - var clear = function(){ + var start = new Date(); + var d = domain.create(); + var tid = null; + var clear = function(){ if(tid === null){ return; @@ -109,12 +109,12 @@ module.exports = function(req,res){ d.add(req); d.add(res); - d.currentContext = new Context(); - d.currentContext.log = {}; - d.currentContext.SN = ++process.SN; - d.currentContext.window = new Window(); - d.currentContext.window.request = req; - d.currentContext.window.response = res; + d.currentContext = new Context(); + d.currentContext.log = {}; + d.currentContext.SN = ++process.SN; + d.currentContext.window = new Window(); + d.currentContext.window.request = req; + d.currentContext.window.response = res; if(config.enableWindow){ d.currentContext.window.enable(); @@ -205,11 +205,11 @@ module.exports = function(req,res){ }); dcapi.report({ - key : 'EVENT_TSW_HTTP_PAGE', - toIp : '127.0.0.1', - code : -100503, - isFail : 1, - delay : Date.now() - start.getTime() + key : 'EVENT_TSW_HTTP_PAGE', + toIp : '127.0.0.1', + code : -100503, + isFail : 1, + delay : Date.now() - start.getTime() }); }else{ @@ -220,11 +220,11 @@ module.exports = function(req,res){ }); dcapi.report({ - key : 'EVENT_TSW_HTTP_PAGE', - toIp : '127.0.0.1', - code : 100503, - isFail : 1, - delay : Date.now() - start.getTime() + key : 'EVENT_TSW_HTTP_PAGE', + toIp : '127.0.0.1', + code : 100503, + isFail : 1, + delay : Date.now() - start.getTime() }); try{ @@ -275,10 +275,10 @@ module.exports = function(req,res){ ].join(''); mail.SendMail(key,'js',600,{ - 'Title' : key, - 'runtimeType' : 'Error', - 'MsgInfo' : err.stack || err.message, - 'Content' : Content + 'Title' : key, + 'runtimeType' : 'Error', + 'MsgInfo' : err.stack || err.message, + 'Content' : Content }); } @@ -360,11 +360,11 @@ module.exports = function(req,res){ tnm2.Attr_API('SUM_TSW_HTTP_OTHER', 1); } - req.timestamps.ServerBeginResponse = req.timestamps.ServerBeginResponse || now; - req.timestamps.GotResponseHeaders = req.timestamps.ServerBeginResponse; - req.timestamps.ServerDoneResponse = res.ServerDoneResponse || now; - req.timestamps.ClientBeginResponse = req.timestamps.ServerBeginResponse; - req.timestamps.ClientDoneResponse = req.timestamps.ServerDoneResponse; + req.timestamps.ServerBeginResponse = req.timestamps.ServerBeginResponse || now; + req.timestamps.GotResponseHeaders = req.timestamps.ServerBeginResponse; + req.timestamps.ServerDoneResponse = res.ServerDoneResponse || now; + req.timestamps.ClientBeginResponse = req.timestamps.ServerBeginResponse; + req.timestamps.ClientDoneResponse = req.timestamps.ServerDoneResponse; if(isFail === 1){ @@ -404,38 +404,38 @@ module.exports = function(req,res){ if(res.__hasTimeout && isFail !== 1){ dcapi.report({ - key : 'EVENT_TSW_HTTP_TIMEOUT', - toIp : '127.0.0.1', - code : res.statusCode || 0, - isFail : isFail, - delay : Date.now() - start.getTime() + key : 'EVENT_TSW_HTTP_TIMEOUT', + toIp : '127.0.0.1', + code : res.statusCode || 0, + isFail : isFail, + delay : Date.now() - start.getTime() }); }else if(res.__hasClosed){ dcapi.report({ - key : 'EVENT_TSW_HTTP_CLOSE', - toIp : '127.0.0.1', - code : res.statusCode || 0, - isFail : isFail, - delay : Date.now() - start.getTime() + key : 'EVENT_TSW_HTTP_CLOSE', + toIp : '127.0.0.1', + code : res.statusCode || 0, + isFail : isFail, + delay : Date.now() - start.getTime() }); }else{ dcapi.report({ - key : 'EVENT_TSW_HTTP_PAGE', - toIp : '127.0.0.1', - code : res.statusCode || 0, - isFail : isFail, - delay : Date.now() - start.getTime() + key : 'EVENT_TSW_HTTP_PAGE', + toIp : '127.0.0.1', + code : res.statusCode || 0, + isFail : isFail, + delay : Date.now() - start.getTime() }); dcapi.report({ - key : 'EVENT_TSW_HTTP_CODE', - toIp : '127.0.0.1', - code : res.statusCode || 0, - isFail : isFail, - delay : Date.now() - start.getTime() + key : 'EVENT_TSW_HTTP_CODE', + toIp : '127.0.0.1', + code : res.statusCode || 0, + isFail : isFail, + delay : Date.now() - start.getTime() }); } @@ -455,7 +455,7 @@ module.exports = function(req,res){ if(res.__hasClosed){ - try{res.writeHead(202); }catch(e){logger.info(`response 202 fail ${e.message}`);} + try{res.writeHead(202); }catch(e){logger.info(`response 202 fail ${e.message}`);} try{res.end();}catch(e){logger.info(`response end fail ${e.message}`);} try{res.emit('done');}catch(e){logger.info(`emit done event fail ${e.message}`);} }else if(res.finished){ @@ -471,7 +471,7 @@ module.exports = function(req,res){ host: req.headers.host }); - try{res.writeHead(513); }catch(e){logger.info(`response 513 fail ${e.message}`);} + try{res.writeHead(513); }catch(e){logger.info(`response 513 fail ${e.message}`);} try{res.end();}catch(e){logger.info(`response end fail ${e.message}`);} try{res.emit('done');}catch(e){logger.info(`emit done event fail ${e.message}`);} @@ -494,12 +494,12 @@ module.exports.doRoute = doRoute; function doRoute(req,res){ - var clientIp = httpUtil.getUserIp(req); - var userIp24 = httpUtil.getUserIp24(req); + var clientIp = httpUtil.getUserIp(req); + var userIp24 = httpUtil.getUserIp24(req); //增加测试环境header if(config.isTest) { - res.setHeader('Test-Head', serverInfo.intranetIp || ''); + res.setHeader('Test-Head', serverInfo.intranetIp || ''); } logger.debug('${method} ${protocol}://${host}${path}',{ @@ -572,8 +572,8 @@ function doRoute(req,res){ }; }(res.writeHead)); - var mod_act = contextMod.currentContext().mod_act || httpModAct.getModAct(req); - contextMod.currentContext().mod_act = mod_act; + var mod_act = contextMod.currentContext().mod_act || httpModAct.getModAct(req); + contextMod.currentContext().mod_act = mod_act; if(alpha.isAlpha(req)){ if(logger.getLog()){ @@ -613,7 +613,7 @@ function doRoute(req,res){ req.headers['tsw-trace-steps'] = steps + 1; - var modulePath = httpModMap.find(mod_act,req,res); + var modulePath = httpModMap.find(mod_act,req,res); if(res.headersSent || res._headerSent || res.finished){ return; @@ -661,8 +661,8 @@ function doRoute(req,res){ var maybePromise = modulePath(req, res, plug); if( typeof maybePromise === 'object' - && - typeof maybePromise.catch === 'function' + && + typeof maybePromise.catch === 'function' ){ maybePromise.catch(function(err){ logger.error(err); @@ -686,11 +686,11 @@ function doRoute(req,res){ logger.debug('命中黑名单IP'); dcapi.report({ - key : 'EVENT_TSW_HTTP_IP_BLOCK', - toIp : clientIp || '127.0.0.1', - code : 0, - isFail : 0, - delay : 100 + key : 'EVENT_TSW_HTTP_IP_BLOCK', + toIp : clientIp || '127.0.0.1', + code : 0, + isFail : 0, + delay : 100 }); tnm2.Attr_API('SUM_TSW_IP_BLOCK', 1); res.writeHead(403, {'Content-Type': 'text/plain; charset=UTF-8'}); @@ -712,7 +712,7 @@ function doRoute(req,res){ if(httpUtil.isFromWns(req) && req.headers['if-none-match']){ logger.debug('webso limit 304, cpuUsed: ${cpuUsed}',{ - cpuUsed : global.cpuUsed + cpuUsed : global.cpuUsed }); tnm2.Attr_API('SUM_TSW_WEBSO_LIMIT', 1); @@ -750,8 +750,8 @@ function doRoute(req,res){ return modulePathHandler(); }else if( contentType.indexOf('application/x-www-form-urlencoded') > -1 - || contentType.indexOf('text/plain') > -1 - || contentType.indexOf('application/json') > -1 + || contentType.indexOf('text/plain') > -1 + || contentType.indexOf('application/json') > -1 ){ parseBody(req,res,function(){ return modulePathHandler(); @@ -765,7 +765,7 @@ function doRoute(req,res){ function onerror(req,res,err){ var listener = req.listeners('fail'); - var window = context.window || {}; + var window = context.window || {}; if(res.headersSent || res._headerSent || res.finished){ return; diff --git a/bin/proxy/master.js b/bin/proxy/master.js index 949d7e4b..5559c9e3 100644 --- a/bin/proxy/master.js +++ b/bin/proxy/master.js @@ -7,18 +7,18 @@ */ 'use strict'; -const logger = require('logger'); -const cp = require('child_process'); -const config = require('./config.js'); -const cluster = require('cluster'); -const cpuUtil = require('util/cpu.js'); -const fs = require('fs'); -const serverOS = require('util/isWindows.js'); -const {debugOptions} = process.binding('config'); -const methodMap = {}; -const workerMap = {}; -const cpuMap = []; -var isDeaded = false; +const logger = require('logger'); +const cp = require('child_process'); +const config = require('./config.js'); +const cluster = require('cluster'); +const cpuUtil = require('util/cpu.js'); +const fs = require('fs'); +const serverOS = require('util/isWindows.js'); +const {debugOptions} = process.binding('config'); +const methodMap = {}; +const workerMap = {}; +const cpuMap = []; +var isDeaded = false; //阻止进程因异常而退出 process.on('uncaughtException',function(e){ @@ -31,8 +31,8 @@ process.on('uncaughtException',function(e){ process.on('warning',function(warning){ - var key = String(warning); - var errStr = warning && warning.stack || String(warning); + var key = String(warning); + var errStr = warning && warning.stack || String(warning); logger.error(errStr); @@ -46,9 +46,9 @@ process.on('warning',function(warning){ setImmediate(function(){ require('util/mail/mail.js').SendMail(key,'js',600,{ - 'Title' : key, - 'runtimeType' : 'warning', - 'Content' : Content + 'Title' : key, + 'runtimeType' : 'warning', + 'Content' : Content }); }); @@ -57,14 +57,14 @@ process.on('warning',function(warning){ process.on('unhandledRejection', (reason = {}, p = {}) => { let - key = String(reason.message), - errStr = String(reason.stack), + key = String(reason.message), + errStr = String(reason.stack), mod_act, module, REQUEST; if(p && p.domain && p.domain.currentContext){ - mod_act = p.domain.currentContext.mod_act; - module = p.domain.currentContext.module; - REQUEST = p.domain.currentContext.window.request.REQUEST; + mod_act = p.domain.currentContext.mod_act; + module = p.domain.currentContext.module; + REQUEST = p.domain.currentContext.window.request.REQUEST; } if(errStr === 'undefined'){ @@ -104,9 +104,9 @@ process.on('unhandledRejection', (reason = {}, p = {}) => { } require('util/mail/mail.js').SendMail(key,'js',600,{ - 'Title' : key, - 'runtimeType' : 'unhandledRejection', - 'Content' : Content + 'Title' : key, + 'runtimeType' : 'unhandledRejection', + 'Content' : Content }); }); @@ -142,7 +142,7 @@ function startServer(){ //启动管理进程 require('./admin.js'); - + logger.info('start master....'); logger.info('version node: ${node}, modules: ${modules}',process.versions); @@ -176,21 +176,21 @@ function startServer(){ //监听子进程是否fork成功 cluster.on('fork',function(currWorker){ - + var cpu = getToBindCpu(currWorker); - + logger.info('worker fork success! pid:${pid} cpu: ${cpu}',{ pid: currWorker.process.pid, cpu: cpu }); - + if(workerMap[cpu]){ closeWorker(workerMap[cpu]); } - + workerMap[cpu] = currWorker; cpuMap[cpu] = 1; - + //监听子进程发来的消息并处理 currWorker.on('message',function(...args){ var m = args[0]; @@ -198,71 +198,71 @@ function startServer(){ methodMap[m.cmd].apply(this,args); } }); - + //给子进程发送消息,启动http服务 currWorker.send({ from:'master', cmd:'listen', cpu: cpu }); - + }); - + //退出时 cluster.on('disconnect', function(worker) { var cpu = getToBindCpu(worker); - + if(worker.hasRestart){ return; } - + logger.info('worker${cpu} pid=${pid} has disconnected. restart new worker again.',{ pid: worker.process.pid, cpu: cpu }); - + restartWorker(worker); }); - + //子进程被杀死的时候做下处理,原地复活 cluster.on('exit',function(worker){ - + var cpu = getToBindCpu(worker); - + if(worker.hasRestart){ return; } - + logger.info('worker${cpu} pid=${pid} has been killed. restart new worker again.',{ pid: worker.process.pid, cpu: cpu }); - + restartWorker(worker); }); - + process.on('reload',function(GET){ - + var timeout = 1000, cpu = 0, key,worker; - + if(isDeaded){ process.exit(0); } - + logger.info('reload'); - + for(key in workerMap){ worker = workerMap[key]; try{ - + cpu = getToBindCpu(worker); if(config.isTest || config.devMode){ - timeout = (cpu % 8) * 1000; + timeout = (cpu % 8) * 1000; }else{ - timeout = (cpu % 8) * 3000; + timeout = (cpu % 8) * 3000; } setTimeout(function(worker,cpu){ @@ -292,8 +292,8 @@ function startServer(){ process.on('sendCmd2workerOnce',function(data){ var key,worker; - var CMD = data.CMD; - var GET = data.GET; + var CMD = data.CMD; + var GET = data.GET; if(isDeaded){ process.exit(0); @@ -322,12 +322,12 @@ function startServer(){ checkWorkerAlive(); startLogMan(); - + //process.title = 'TSW/master/node'; //保留node命令,不然运维监控不到 - + }else{ - + //子进程直接引入proxy文件,当然也可以直接在这里写逻辑运行,注意此处else作用域属于子进程作用域,非本程序作用域 process.title = 'TSW/worker/node'; logger.info('start worker....'); @@ -345,38 +345,38 @@ function startServer(){ //处理子进程的心跳消息 methodMap.heartBeat = function(m){ - - var worker = this; - var now = new Date().getTime(); - - worker.lastMessage = m; - worker.lastLiveTime = now; + + var worker = this; + var now = new Date().getTime(); + + worker.lastMessage = m; + worker.lastLiveTime = now; }; //关闭一个worker function closeWorker(worker){ - var cpu = worker.cpuid; - var closeTimeWait = 10000; + var cpu = worker.cpuid; + var closeTimeWait = 10000; - closeTimeWait = Math.max(closeTimeWait,config.timeout.socket); - closeTimeWait = Math.max(closeTimeWait,config.timeout.post); - closeTimeWait = Math.max(closeTimeWait,config.timeout.get); - closeTimeWait = Math.max(closeTimeWait,config.timeout.keepAlive); - closeTimeWait = Math.min(60000,closeTimeWait) || 10000; + closeTimeWait = Math.max(closeTimeWait,config.timeout.socket); + closeTimeWait = Math.max(closeTimeWait,config.timeout.post); + closeTimeWait = Math.max(closeTimeWait,config.timeout.get); + closeTimeWait = Math.max(closeTimeWait,config.timeout.keepAlive); + closeTimeWait = Math.min(60000,closeTimeWait) || 10000; if(worker.hasClose){ return; } - + if(workerMap[cpu] === worker){ delete workerMap[cpu]; } - + var closeFn = function(worker){ var closed = false; var pid = worker.process.pid; - + return function(){ if(closed){ return; @@ -388,11 +388,11 @@ function closeWorker(worker){ } worker.destroy(); - + closed = true; }; }(worker); - + setTimeout(closeFn,closeTimeWait); if(worker.exitedAfterDisconnect){ @@ -405,7 +405,7 @@ function closeWorker(worker){ }catch(e){ logger.info(e.stack); } - + } /** @@ -413,7 +413,7 @@ function closeWorker(worker){ */ function restartWorker(worker){ var cpu = getToBindCpu(worker); - + if(worker.hasRestart){ return; } @@ -428,46 +428,46 @@ function restartWorker(worker){ },10000); cpuMap[cpu] = 0; - + worker.hasRestart = true; cluster.fork(process.env).cpuid = cpu; } //定时检测子进程死活,发现15秒没响应的就干掉 function checkWorkerAlive(){ - + setInterval(function(){ - + var nowDate = new Date(), - now = nowDate.getTime(), + now = nowDate.getTime(), key, worker, cpuid; - + for(key in workerMap){ worker = workerMap[key]; cpuid = worker.cpuid; - + worker.lastLiveTime = worker.lastLiveTime || now; if(!worker.startTime){ worker.startTime = now; } - + //无响应进程处理 if(now - worker.lastLiveTime > 15000 && cpuMap[cpuid] === 1){ - + logger.error('worker${cpu} pid=${pid} miss heartBeat, kill it',{ pid : worker.process.pid, cpu: cpuid }); - + restartWorker(worker); } //内存超限进程处理 if(worker.lastMessage){ - let currMemory = worker.lastMessage.memoryUsage; + let currMemory = worker.lastMessage.memoryUsage; //logger.debug(currMemory); @@ -489,9 +489,9 @@ function checkWorkerAlive(){ if( true - && (nowDate.getHours() % 8 === 0) - && nowDate.getMinutes() === 1 - && nowDate.getSeconds() <= 10 + && (nowDate.getHours() % 8 === 0) + && nowDate.getMinutes() === 1 + && nowDate.getSeconds() <= 10 ){ //8小时一次 require('api/keyman/runtimeAdd.js').hello(); @@ -504,25 +504,25 @@ function checkWorkerAlive(){ * 获取需要绑定的CPU编号 */ function getToBindCpu(worker){ - + var cpu = 0;//如果只有一个cpu或者都占用了 - + if(worker.cpuid !== undefined){ cpu = worker.cpuid; return cpu; }else{ for(var i=0;i 0 && times.end - lastRetry > 1000){ lastRetry = times.end; - + opt.retry = opt.retry - 1; - + logger.debug('retry: ' + opt.retry); opt.isRetriedRequest = true; that.request(opt).done(function(d){ d.hasRetry = true; - + defer.resolve(d); }).fail(function(d){ d.hasRetry = true; - + defer.reject(d); }); - - return; + + return; }else{ report(opt,1,513 + opt.retry); } @@ -678,18 +678,18 @@ Ajax.prototype.doRequest = function(opt){ msg: 'request error', times: times }); - + }); request.once('response',function(response){ - var result = []; - var pipe = response; - var isProxy = false; - + var result = []; + var pipe = response; + var isProxy = false; + if(opt.dataType === 'proxy' && that._proxyResponse){ isProxy = true; } - + if(currRetry != opt.retry){ //防止第一个请求又回包捣乱 logger.debug('currRetry: ${currRetry}, opt.retry: ${retry}',{ @@ -698,21 +698,21 @@ Ajax.prototype.doRequest = function(opt){ }); return; } - + //不能再重试了 opt.retry = 0; - + //不能省掉哦 process.domain && process.domain.add(response); - - opt.statusCode = response.statusCode; - opt.remoteAddress = request.socket && request.socket.remoteAddress; - opt.remotePort = request.socket && request.socket.remotePort; - - response._bodySize = 0; - + + opt.statusCode = response.statusCode; + opt.remoteAddress = request.socket && request.socket.remoteAddress; + opt.remotePort = request.socket && request.socket.remotePort; + + response._bodySize = 0; + times.response = new Date().getTime(); - + if(opt.dataType === 'proxy'){ if(response.statusCode >= 500 && response.statusCode <= 599 && response.statusCode !== 501){ logger.debug(logPre + '${ip}:${port} response ${statusCode} cost:${cost}ms ${encoding}\nrequest: ${headers}\nresponse: ${resHeaders}',{ @@ -737,9 +737,9 @@ Ajax.prototype.doRequest = function(opt){ }else{ if( opt.statusCode === 200 - || opt.statusCode === 206 - || opt.statusCode === 666 - || opt.dataType === response.statusCode + || opt.statusCode === 206 + || opt.statusCode === 666 + || opt.dataType === response.statusCode ){ logger.debug(logPre + '${ip}:${port} response ${statusCode} cost:${cost}ms ${encoding}',{ ip: opt.remoteAddress, @@ -760,7 +760,7 @@ Ajax.prototype.doRequest = function(opt){ }); } } - + if(defer.isRejected() || defer.isResolved()){ logger.debug(logPre + 'isRejected: ${isRejected}, isResolved: ${isResolved}', { isRejected: defer.isRejected(), @@ -768,7 +768,7 @@ Ajax.prototype.doRequest = function(opt){ }); return; } - + if(typeof opt.response === 'function'){ if(opt.response(response) === false){ //中断处理流程 @@ -782,12 +782,12 @@ Ajax.prototype.doRequest = function(opt){ return; } } - + if(opt.dataType === 'buffer' || (isProxy && response.headers['content-encoding']) || response.headers['content-length'] == 0){ logger.debug(logPre + 'response type: buffer'); }else{ if(response.headers['content-encoding'] === 'gzip'){ - + pipe = zlib.createGunzip(); response.on('data',function(buffer){ pipe.write(buffer); @@ -796,9 +796,9 @@ Ajax.prototype.doRequest = function(opt){ pipe.end(); }); }else if(response.headers['content-encoding'] === 'deflate'){ - + pipe = zlib.createInflateRaw(); - + response.on('data',function(buffer){ pipe.write(buffer); }); @@ -807,7 +807,7 @@ Ajax.prototype.doRequest = function(opt){ }); } } - + if(isProxy){ if(response.headers['transfer-encoding'] !== 'chunked'){ @@ -823,10 +823,10 @@ Ajax.prototype.doRequest = function(opt){ if( !response.headers['content-encoding'] && - response.headers['content-type'] && - (response.headers['content-type'].indexOf('text/') === 0 || - response.headers['content-type'] === 'application/x-javascript' || - response.headers['content-type'] === 'x-json') + response.headers['content-type'] && + (response.headers['content-type'].indexOf('text/') === 0 || + response.headers['content-type'] === 'application/x-javascript' || + response.headers['content-type'] === 'x-json') ){ //自带压缩功能 @@ -836,14 +836,14 @@ Ajax.prototype.doRequest = function(opt){ //解决循环依赖 that._proxyResponse = require('util/gzipHttp.js').create({ - request : that._proxyRequest, - response : that._proxyResponse, - code : response.statusCode, - headers : httpUtil.formatHeader(response.headers) + request : that._proxyRequest, + response : that._proxyResponse, + code : response.statusCode, + headers : httpUtil.formatHeader(response.headers) }); }else{ delete response.headers['connection']; - + that._proxyResponse.writeHead(response.statusCode,httpUtil.formatHeader(response.headers)); } } @@ -857,9 +857,9 @@ Ajax.prototype.doRequest = function(opt){ pipe.timeCurr = Date.now(); //logger.debug('${logPre}receive data: ${size},\tcost: ${cost}ms',{ - // logPre: logPre, - // cost: cost, - // size: chunk.length + // logPre: logPre, + // cost: cost, + // size: chunk.length //}); response._bodySize += chunk.length; @@ -873,7 +873,7 @@ Ajax.prototype.doRequest = function(opt){ request.emit('fail'); return; } - + if(isProxy){ if(!that._proxyResponse.finished){ that._proxyResponse.write(chunk); @@ -883,12 +883,12 @@ Ajax.prototype.doRequest = function(opt){ result.push(chunk); } }); - + pipe.once('close',function(){ logger.debug(logPre + 'close'); this.emit('done'); }); - + pipe.once('end',function(){ var cost = Date.now() - pipe.timeStart; @@ -900,17 +900,17 @@ Ajax.prototype.doRequest = function(opt){ this.emit('done'); }); - + pipe.once('done',function(){ - + var obj,responseText,buffer,code; var key,Content; - + this.removeAllListeners('close'); this.removeAllListeners('end'); this.removeAllListeners('data'); this.removeAllListeners('done'); - + if(defer.isRejected() || defer.isResolved()){ return; } @@ -918,20 +918,20 @@ Ajax.prototype.doRequest = function(opt){ times.end = new Date().getTime(); buffer = Buffer.concat(result); result = []; - + logger.debug(logPre + 'done size:${len}', { len: response._bodySize }); - + if(isProxy){ if (response.statusCode >= 500 && response.statusCode <= 599 && response.statusCode !== 501) { report(opt, 1, response.statusCode); }else{ report(opt,0,response.statusCode); } - + that._proxyResponse.end(); - + defer.resolve({ opt: opt, buffer: buffer, @@ -942,11 +942,11 @@ Ajax.prototype.doRequest = function(opt){ response: response, times: times }); - - + + return; } - + if(opt.dataType === response.statusCode){ report(opt,0,response.statusCode); defer.resolve({ @@ -962,11 +962,11 @@ Ajax.prototype.doRequest = function(opt){ return; } - + if(opt.dataType === 'json' || opt.dataType === 'jsonp' || opt.dataType === 'text' || opt.dataType === 'html'){ responseText = buffer.toString('UTF-8'); } - + if(buffer.length <= 1024){ if(responseText){ if(opt.dataType === 'json' || opt.dataType === 'jsonp' || opt.dataType === 'text'){ @@ -976,11 +976,11 @@ Ajax.prototype.doRequest = function(opt){ logger.debug(logPre + 'responseText:\n' + buffer.toString('UTF-8')); } } - + if(responseText){ buffer = null; } - + if(response.statusCode !== 200 && response.statusCode !== 666 && response.statusCode !== 206){ //dataType为proxy但是不走代理模式的时候,30x类型的返回码当作成功上报 if(response.statusCode >= 300 && response.statusCode < 400){ @@ -992,7 +992,7 @@ Ajax.prototype.doRequest = function(opt){ }else{ report(opt,1,response.statusCode); } - + defer.reject({ opt: opt, buffer: buffer, @@ -1008,7 +1008,7 @@ Ajax.prototype.doRequest = function(opt){ } if(opt.dataType === 'json' || opt.dataType === 'jsonp'){ - + try{ if(opt.jsonpCallback){ @@ -1032,7 +1032,7 @@ Ajax.prototype.doRequest = function(opt){ }catch(e){ let parseErr = e; - + if(e && code){ try{ code = code.replace(/[\u2028\u2029]/g,''); @@ -1062,32 +1062,32 @@ Ajax.prototype.doRequest = function(opt){ response: response, times: times }); - + key = [window.request.headers.host,context.mod_act,parseErr.message].join(':'); - + Content = [ '

错误堆栈

', '

',
                             parseErr.stack,
                             '

', ].join(''); - + require('util/mail/mail.js').SendMail(key,'js data',1800,{ - 'Title' : key, - 'runtimeType' : 'ParseError', - 'Content' : Content, - 'MsgInfo' : '错误堆栈:\n' + parseErr.stack + 'Title' : key, + 'runtimeType' : 'ParseError', + 'Content' : Content, + 'MsgInfo' : '错误堆栈:\n' + parseErr.stack }); - + return; } - + } - + if(obj){ code = obj.code || 0; } - + }else{ code = { isFail: 0, @@ -1096,37 +1096,37 @@ Ajax.prototype.doRequest = function(opt){ obj = responseText; } - + if(typeof opt.formatCode === 'function'){ code = opt.formatCode(obj,opt,response); } - + //支持{isFail:0,code:1,message:''} if(typeof code === 'object'){ - + if(typeof code.isFail !== 'number'){ code.isFail = ~~code.isFail; } - + if(typeof code.code !== 'number'){ code.code = ~~code.code; } - + report(opt,code.isFail,code.code); }else{ - + if(typeof code !== 'number'){ code = ~~code; } - + if(code === 0){ report(opt,0,code); }else{ report(opt,2,code); } } - - + + defer.resolve({ opt: opt, buffer: buffer, @@ -1141,13 +1141,13 @@ Ajax.prototype.doRequest = function(opt){ }); - + if(opt.send){ opt.send(request); }else{ - + if(opt.body){ - + request.useChunkedEncodingByDefault = false; try{ request.write(opt.body); @@ -1155,10 +1155,10 @@ Ajax.prototype.doRequest = function(opt){ logger.info(e.stack); } } - + request.end(); } - + return defer; }; diff --git a/bin/tsw/ajax/form.js b/bin/tsw/ajax/form.js index f9dae38d..7ef3859f 100644 --- a/bin/tsw/ajax/form.js +++ b/bin/tsw/ajax/form.js @@ -68,7 +68,7 @@ function fileField(boundary, key, value = {}) { /** * 构建Form表单发送时的buffer * @param {[type]} opt = {} [description] - * @return {[type]} [description] + * @return {[type]} [description] */ function getFormBuffer(opt = {}) { @@ -77,11 +77,11 @@ function getFormBuffer(opt = {}) { let v; for(let key in opt.data){ - + v = opt.data[key]; - + if(v !== undefined && v !== null){ - + if(v.fileType){ buffer = Buffer.concat([buffer, fileField(opt.boundary, key, v)]); }else{ @@ -89,7 +89,7 @@ function getFormBuffer(opt = {}) { } } } - + buffer = Buffer.concat([buffer, Buffer.from('--' + opt.boundary + '--')]); return buffer; diff --git a/bin/tsw/ajax/http-https.options.js b/bin/tsw/ajax/http-https.options.js index 5a0e3ec0..bc6b207e 100644 --- a/bin/tsw/ajax/http-https.options.js +++ b/bin/tsw/ajax/http-https.options.js @@ -23,7 +23,7 @@ this.getHttpsAgent = function(host){ maxSockets : 65535, maxFreeSockets : 32, maxCachedSessions : 65535, - keepAlive : true, + keepAlive : true, keepAliveMsecs : 5000 }); } diff --git a/bin/tsw/ajax/token.js b/bin/tsw/ajax/token.js index 6b8536e6..a25cfad0 100644 --- a/bin/tsw/ajax/token.js +++ b/bin/tsw/ajax/token.js @@ -8,20 +8,20 @@ 'use strict'; this.token = function(skey){ - - - var str = skey || ''; - var hash = 5381; + + + var str = skey || ''; + var hash = 5381; if(typeof context !== 'undefined'){ let window = context.window || {}; if(window.request){ str = str - || window.request.cookies.p_skey - || window.request.cookies.skey - || window.request.cookies.rv2 - || window.request.cookies.access_token - || ''; + || window.request.cookies.p_skey + || window.request.cookies.skey + || window.request.cookies.rv2 + || window.request.cookies.access_token + || ''; } } diff --git a/bin/tsw/api/date.js b/bin/tsw/api/date.js index 893bbb49..8dd4af80 100644 --- a/bin/tsw/api/date.js +++ b/bin/tsw/api/date.js @@ -8,13 +8,13 @@ 'use strict'; var Date = { - + /** - * 格式化时间 + * 格式化时间 * @method formatData - * @param {Date} mDate 时间对象 - * @param {String} fmt 格式化形式,如 MM月dd日 HH:mm - */ + * @param {Date} mDate 时间对象 + * @param {String} fmt 格式化形式,如 MM月dd日 HH:mm + */ format: function(mDate, fmt){ var o = { 'M+': mDate.getMonth() + 1, //月份 @@ -34,20 +34,20 @@ var Date = { '5': '\u4e94', '6': '\u516d' }; - + if (/(Y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (mDate.getFullYear() + '').substr(4 - RegExp.$1.length)); } if (/(E+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[mDate.getDay() + '']); } - + for (var k in o) { if (new RegExp('(' + k + ')').test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))); } } - + return fmt; } }; diff --git a/bin/tsw/api/fileCache/index.js b/bin/tsw/api/fileCache/index.js index 604d84e6..e1f78218 100644 --- a/bin/tsw/api/fileCache/index.js +++ b/bin/tsw/api/fileCache/index.js @@ -7,19 +7,19 @@ */ 'use strict'; -const Deferred = require('util/Deferred'); -const logger = require('logger'); -const path = require('path'); -const fs = require('fs'); -const root = path.join(__dirname,'../../../../log/cache'); -const tnm2 = require('api/tnm2'); -const existsCache = {}; +const Deferred = require('util/Deferred'); +const logger = require('logger'); +const path = require('path'); +const fs = require('fs'); +const root = path.join(__dirname,'../../../../log/cache'); +const tnm2 = require('api/tnm2'); +const existsCache = {}; this.getDir = function(filepath){ - + var res,tmp; - + filepath = filepath || ''; if(!/^[a-z0-9_/\-.\\:]+$/i.test(filepath)){ @@ -29,12 +29,12 @@ this.getDir = function(filepath){ if(filepath.indexOf('http://') > -1){ filepath = filepath.replace('http://',''); } - + tmp = path.join('/',filepath); res = path.join(root,tmp); - + res = res.replace(/\\/g,'/'); - + return res; }; @@ -49,10 +49,10 @@ this.mkdir = function(dirname){ return; } - var arr = dirname.split('/'); - var i = 0; + var arr = dirname.split('/'); + var i = 0; var curr; - + for(i = 1; i < arr.length; i++){ curr = arr.slice(0,i + 1).join('/'); if(!fs.existsSync(curr)){ @@ -70,39 +70,39 @@ this.mkdir = function(dirname){ }; this.set = function(filepath,data){ - var start = Date.now(); - var buffer = null; - var filename = this.getDir(filepath); - var dirname = path.dirname(filename); - var basename = path.basename(filename); - var randomname = basename + '.tmp.' + String(Math.random()).slice(2); - + var start = Date.now(); + var buffer = null; + var filename = this.getDir(filepath); + var dirname = path.dirname(filename); + var basename = path.basename(filename); + var randomname = basename + '.tmp.' + String(Math.random()).slice(2); + logger.debug('[set] filename : ${filename}',{ filename: filename }); - + logger.debug('[set] dirname : ${dirname}',{ dirname: dirname }); - + logger.debug('[set] basename : ${basename}',{ basename: basename }); - + logger.debug('[set] randomname : ${randomname}',{ randomname: randomname }); - + this.mkdir(dirname); - + if(typeof data === 'string'){ buffer = Buffer.from(data,'UTF-8'); }else{ buffer = data; } - + fs.writeFile([dirname,randomname].join('/'),buffer,{mode:0o666},function(err){ - + logger.debug('[write] done: ${randomname}, size: ${size}',{ randomname: randomname, size: buffer.length @@ -112,35 +112,35 @@ this.set = function(filepath,data){ logger.info(err.stack); return; } - + fs.rename([dirname,randomname].join('/'),[dirname,basename].join('/'),function(err){ - var end = Date.now(); - + var end = Date.now(); + if(err){ logger.debug(err); } - + logger.debug('[rename] done: ${basename}, cost: ${cost}ms',{ basename: basename, cost: end - start }); }); - + }); tnm2.Attr_API('SUM_TSW_FILECACHE_WRITE', 1); }; this.getSync = function(filepath){ - - var start = Date.now(); - var buffer = null; - var filename = this.getDir(filepath); - var res = { + + var start = Date.now(); + var buffer = null; + var filename = this.getDir(filepath); + var res = { stats: null, data: null }; - + try{ buffer = fs.readFileSync(filename); if(buffer.length === 0){ @@ -154,73 +154,73 @@ this.getSync = function(filepath){ }catch(err){ buffer = null; } - - var end = Date.now(); - + + var end = Date.now(); + logger.debug('[get] done: ${filename}, size: ${size}, cost: ${cost}ms',{ filename: filename, size: buffer && buffer.length, cost: end - start }); - + tnm2.Attr_API('SUM_TSW_FILECACHE_READ', 1); return res; }; this.get = this.getAsync = function(filepath){ - - var defer = Deferred.create(); - var filename = this.getDir(filepath); - - + + var defer = Deferred.create(); + var filename = this.getDir(filepath); + + var res = { stats: null, data: null }; - + logger.debug('[getAsync] ${filename}',{ filename: filename }); - + fs.readFile(filename,function(err, buffer){ - + if(err){ logger.debug(err.stack); defer.resolve(res); return; } - + res.data = buffer; - + fs.stat(filename,function(err,stats){ - + if(err){ logger.debug(err.stack); defer.resolve(res); return; } - + logger.debug('[getAsync] mtime: ${mtime}, size: ${size}',stats); - + res.stats = stats; defer.resolve(res); }); - + }); - + return defer; }; this.updateMtime = function(filepath, atime, mtime){ - var filename = this.getDir(filepath); - - + var filename = this.getDir(filepath); + + logger.debug('[updateMtime] ${filename}',{ filename: filename }); - + fs.utimes(filename, atime || new Date(), mtime || new Date(), function(err){ if(err){ logger.debug(err.stack); diff --git a/bin/tsw/api/logman/index.js b/bin/tsw/api/logman/index.js index ef736f1b..47dc5c1f 100644 --- a/bin/tsw/api/logman/index.js +++ b/bin/tsw/api/logman/index.js @@ -7,15 +7,15 @@ */ 'use strict'; -const cp = require('child_process'); -const fs = require('fs'); -const path = require('path'); -const logger = require('logger'); -const dateApi = require('api/date.js'); -const {isWindows} = require('util/isWindows.js'); -const logDir = path.resolve(__dirname, '../../../../log/').replace(/\\/g, '/'); -const backupDir = path.resolve(logDir, './backup/').replace(/\\/g, '/'); -const runlogPath = path.resolve(logDir, './run.log.0').replace(/\\/g, '/'); +const cp = require('child_process'); +const fs = require('fs'); +const path = require('path'); +const logger = require('logger'); +const dateApi = require('api/date.js'); +const {isWindows} = require('util/isWindows.js'); +const logDir = path.resolve(__dirname, '../../../../log/').replace(/\\/g, '/'); +const backupDir = path.resolve(logDir, './backup/').replace(/\\/g, '/'); +const runlogPath = path.resolve(logDir, './run.log.0').replace(/\\/g, '/'); //判断logDir目录是否存在 fs.exists(logDir, function(exists){ @@ -32,19 +32,19 @@ fs.exists(logDir, function(exists){ }); var LogMan = { - + /** - * 按分钟\小时\天去备份log - */ + * 按分钟\小时\天去备份log + */ delayMap: { m: 60000, H: 3600000, D: 86400000 }, - + /** - * 启动log管理 - */ + * 启动log管理 + */ start: function(config){ logger.info('start log manager'); var self = this; @@ -54,10 +54,10 @@ var LogMan = { self.backLog(); }, this.delay); }, - + /** - * 备份log - */ + * 备份log + */ backLog: function(){ logger.info('start backup log'); var self = this; @@ -69,14 +69,14 @@ var LogMan = { var logFilePath = path.resolve(curBackupDir, './' + dateApi.format(new Date, self.delayType + self.delayType) + '.log'); var cmdCat = 'cat ' + runlogPath + ' >> ' + logFilePath; var cmdClear = 'cat /dev/null > ' + runlogPath; - + //兼容windows if(isWindows){ logFilePath = logFilePath.replace(/\\/g, '\\\\'); cmdCat = 'type ' + runlogPath + ' > ' + logFilePath; cmdClear = 'type NUL > ' + runlogPath; } - + //backup logger.info('backup: '+ cmdCat); @@ -84,7 +84,7 @@ var LogMan = { if (error !== null) { logger.error('cat error, ' + error); } - + //clear logger.info('clear: ' + cmdClear); @@ -96,7 +96,7 @@ var LogMan = { }); }); } - + }; module.exports = LogMan; diff --git a/bin/tsw/default/config.default.sky.js b/bin/tsw/default/config.default.sky.js index 68f7282a..a17b895c 100644 --- a/bin/tsw/default/config.default.sky.js +++ b/bin/tsw/default/config.default.sky.js @@ -11,28 +11,28 @@ this.modAct = this.modMap = { map: { - log_view : 'util/auto-report/view.js', - log_download : 'util/auto-report/download.js', - group_page : 'util/h5-test/group/view.js', - h5test_page : 'util/h5-test/page/view.js', - h5test_managerget : 'util/h5-test/get.js', - h5test_manageradd : 'util/h5-test/add.js', - h5test_managerdel : 'util/h5-test/del.js', - '/api/h5test/get' : 'util/h5-test/get.js', - '/api/h5test/add' : 'util/h5-test/add.js', - '/api/h5test/del' : 'util/h5-test/del.js', - '/' : 'util/home/view.js', - '/index' : 'util/home/view.js', - static_tsw : 'util/static/static.js', - default_page : 'util/static/static.js' + log_view : 'util/auto-report/view.js', + log_download : 'util/auto-report/download.js', + group_page : 'util/h5-test/group/view.js', + h5test_page : 'util/h5-test/page/view.js', + h5test_managerget : 'util/h5-test/get.js', + h5test_manageradd : 'util/h5-test/add.js', + h5test_managerdel : 'util/h5-test/del.js', + '/api/h5test/get' : 'util/h5-test/get.js', + '/api/h5test/add' : 'util/h5-test/add.js', + '/api/h5test/del' : 'util/h5-test/del.js', + '/' : 'util/home/view.js', + '/index' : 'util/home/view.js', + static_tsw : 'util/static/static.js', + default_page : 'util/static/static.js' }, getModAct: function(req){ var pathname= req.REQUEST.pathname || ''; - var arr = pathname.split('/',3); - var mod = arr[1] || 'default'; - var act = arr[2] || 'page'; - var mod_act = mod + '_' + act; + var arr = pathname.split('/',3); + var mod = arr[1] || 'default'; + var act = arr[2] || 'page'; + var mod_act = mod + '_' + act; if(this.map[pathname]){ diff --git a/bin/tsw/plug.js b/bin/tsw/plug.js index 89da6e07..fa9bca92 100644 --- a/bin/tsw/plug.js +++ b/bin/tsw/plug.js @@ -20,11 +20,11 @@ function plug(id){ } if(!global.plug){ - - + + plug.__dirname = __dirname; plug.parent = path.join(__dirname , '..'); - plug.paths = [ + plug.paths = [ path.join(__dirname , '../deps'), path.join(__dirname , '../tsw'), path.join(__dirname , '../tencent'), @@ -34,7 +34,7 @@ if(!global.plug){ module.paths = plug.paths.concat(module.paths); global.plug = plug; - + //支持seajs模块 require('loader/seajs'); require('loader/extentions.js'); @@ -42,14 +42,14 @@ if(!global.plug){ JSON.stringify = function(stringify){ return function(){ var str = stringify.apply(this,arguments); - + if(str && str.indexOf('<') > -1){ str = str.replace(/ 0){ command = command.slice(0,Math.min(128,index)); } @@ -101,11 +101,11 @@ function queueWrap(memcached){ query.callback = function(callback){ return function(...args){ - var err = args[0]; - var code = 0; - var isFail = 0; - var delay = Date.now() - start; - var toIp = servers.split(':')[0]; + var err = args[0]; + var code = 0; + var isFail = 0; + var delay = Date.now() - start; + var toIp = servers.split(':')[0]; if(err && err.message !== 'Item is not stored'){ if(err.stack){ @@ -120,11 +120,11 @@ function queueWrap(memcached){ } dcapi.report({ - key : 'EVENT_TSW_MEMCACHED', - toIp : toIp, - code : code, - isFail : isFail, - delay : delay + key : 'EVENT_TSW_MEMCACHED', + toIp : toIp, + code : code, + isFail : isFail, + delay : delay }); queue.dequeue(); @@ -134,12 +134,12 @@ function queueWrap(memcached){ return query; }; })(queryCompiler); - + command.call(memcached,fn, server); }); }; }(memcached.command); - - + + return memcached; } diff --git a/bin/tsw/router.js b/bin/tsw/router.js index 3a6d27a5..28e81707 100644 --- a/bin/tsw/router.js +++ b/bin/tsw/router.js @@ -7,45 +7,45 @@ */ 'use strict'; -const logger = require('logger'); -const httpRoute = require('../proxy/http.route.js'); -const parseGet = require('util/http/parseGet.js'); +const logger = require('logger'); +const httpRoute = require('../proxy/http.route.js'); +const parseGet = require('util/http/parseGet.js'); this.route = function(url,mod_act){ - + var req,res; var window = context.window || {}; - + req = window.request; res = window.response; - + if(!req){ return; } - + res.removeAllListeners('afterFinish'); - + if(url){ - + logger.debug('route to : ${url}',{ url: url }); - + req.url = url; //解析get参数 parseGet(req); } - + if(mod_act){ logger.debug('route to mod_act: ${mod_act}',{ mod_act: mod_act }); - + context.mod_act = mod_act; }else{ context.mod_act = null; } - + httpRoute.doRoute(req,res); }; diff --git a/bin/tsw/runtime/CCFinder.js b/bin/tsw/runtime/CCFinder.js index 113298e0..f532373e 100644 --- a/bin/tsw/runtime/CCFinder.js +++ b/bin/tsw/runtime/CCFinder.js @@ -7,17 +7,17 @@ */ 'use strict'; -const config = require('config'); -const logger = require('logger'); -const httpUtil = require('util/http'); -const serverInfo = require('serverInfo.js'); -const isTST = require('util/isTST.js'); -const mail = require('util/mail/mail.js'); -const tnm2 = require('api/tnm2'); -const CCIPSize = 1000; //统计周期 -const CCIPLimit = config.CCIPLimit; //限制 -var ipConut = CCIPSize; -var cache = { +const config = require('config'); +const logger = require('logger'); +const httpUtil = require('util/http'); +const serverInfo = require('serverInfo.js'); +const isTST = require('util/isTST.js'); +const mail = require('util/mail/mail.js'); +const tnm2 = require('api/tnm2'); +const CCIPSize = 1000; //统计周期 +const CCIPLimit = config.CCIPLimit; //限制 +var ipConut = CCIPSize; +var cache = { ipCache: {}, whiteList: {}, ipCacheLast: {} @@ -35,7 +35,7 @@ this.addWhiteList = function(userIp){ this.checkHost = function(req, res){ - var hostAllow = config.allowHost || []; + var hostAllow = config.allowHost || []; var host = req.headers['host']; var i,len,v; @@ -73,10 +73,10 @@ this.checkHost = function(req, res){ //计算标准方差 this.StdX10 = function(ipCache){ - var res = 0; - var sum = 0; - var avg = 0; - var arr = Object.keys(ipCache).filter(function(item){ + var res = 0; + var sum = 0; + var avg = 0; + var arr = Object.keys(ipCache).filter(function(item){ var tmp = ipCache[item]; if(typeof tmp === 'object' && tmp.list){ sum += tmp.list.length; @@ -92,9 +92,9 @@ this.StdX10 = function(ipCache){ avg = sum / arr.length; var sumXsum = arr.reduce(function(pre,key){ - var item = ipCache[key]; - var value = item.list.length; - item.avg = avg; + var item = ipCache[key]; + var value = item.list.length; + item.avg = avg; return pre + (value - avg)*(value - avg); },0); @@ -106,14 +106,14 @@ this.StdX10 = function(ipCache){ this.check = function(req, res){ - var userIp = httpUtil.getUserIp(req); - var userIp24 = httpUtil.getUserIp24(req); + var userIp = httpUtil.getUserIp(req); + var userIp24 = httpUtil.getUserIp24(req); var key,Content,curr; - var info = { - userIp : userIp, - hostname : req.headers.host, - pathname : req.REQUEST.pathname + var info = { + userIp : userIp, + hostname : req.headers.host, + pathname : req.REQUEST.pathname }; if(cache.whiteList[userIp]){ @@ -132,15 +132,15 @@ this.check = function(req, res){ if(!cache.ipCache.start){ cache.ipCache.start = Date.now(); } - - ipConut = ipConut - 1; - + + ipConut = ipConut - 1; + if(ipConut <= 0){ - ipConut = CCIPSize; - cache.ipCache.end = Date.now(); + ipConut = CCIPSize; + cache.ipCache.end = Date.now(); cache.ipCache.StdX10= this.StdX10(cache.ipCache); - cache.ipCacheLast = cache.ipCache; - cache.ipCache = {}; + cache.ipCacheLast = cache.ipCache; + cache.ipCache = {}; cache.whiteList = {}; //上报标准方差 @@ -152,7 +152,7 @@ this.check = function(req, res){ ipConut = 0; return true; } - + if(!cache.ipCache[userIp]){ cache.ipCache[userIp] = { ip007Info: null, @@ -160,9 +160,9 @@ this.check = function(req, res){ list: [] }; } - + curr = cache.ipCache[userIp]; - + curr.list.push(info); if(CCIPLimit <= -1){ @@ -173,7 +173,7 @@ this.check = function(req, res){ //测试环境 return true; } - + if(config.devMode){ //开发环境 return true; @@ -182,7 +182,7 @@ this.check = function(req, res){ if(!cache.ipCacheLast.StdX10){ return true; } - + if(cache.ipCacheLast.StdX10 <= CCIPLimit){ return true; } @@ -207,26 +207,26 @@ this.check = function(req, res){ if( cache.ipCacheLast[ip] - && cache.ipCacheLast[ip].list - && cache.ipCacheLast[ip].list.length > 1 + && cache.ipCacheLast[ip].list + && cache.ipCacheLast[ip].list.length > 1 ){ num = '' + cache.ipCacheLast[ip].list.length; num = (num + 'XXXXXX').slice(0,8).replace(/X/g,' '); - Content += `
${num}${ip}
`; + Content += `
${num}${ip}
`; } }); mail.SendMail(key,'TSW',3600,{ - 'To' : config.mailTo, - 'CC' : config.mailCC, - 'Title' : `[IP聚集告警][${cache.ipCacheLast.StdX10}%]${serverInfo.intranetIp}`, - 'Content' : '

服务器IP:' + serverInfo.intranetIp + '

' - + '

IP聚集度:' + cache.ipCacheLast.StdX10 + '%

' - + '

告警阀值:' + CCIPLimit + '

' - + '

正常值:5-50

' - + '

检测耗时:' + parseInt((cache.ipCacheLast.end - cache.ipCacheLast.start)/1000) + 's

' - + '

证据列表:

' - + Content + 'To' : config.mailTo, + 'CC' : config.mailCC, + 'Title' : `[IP聚集告警][${cache.ipCacheLast.StdX10}%]${serverInfo.intranetIp}`, + 'Content' : '

服务器IP:' + serverInfo.intranetIp + '

' + + '

IP聚集度:' + cache.ipCacheLast.StdX10 + '%

' + + '

告警阀值:' + CCIPLimit + '

' + + '

正常值:5-50

' + + '

检测耗时:' + parseInt((cache.ipCacheLast.end - cache.ipCacheLast.start)/1000) + 's

' + + '

证据列表:

' + + Content }); diff --git a/bin/tsw/runtime/Console.hack.js b/bin/tsw/runtime/Console.hack.js index a02761e3..9e2ad3fc 100644 --- a/bin/tsw/runtime/Console.hack.js +++ b/bin/tsw/runtime/Console.hack.js @@ -12,8 +12,8 @@ if(!global[__filename]){ global[__filename] = true; process.nextTick(function(){ - var util = require('util'); - var logger = require('logger'); + var util = require('util'); + var logger = require('logger'); /* eslint-disable no-console */ diff --git a/bin/tsw/runtime/Context.js b/bin/tsw/runtime/Context.js index 2d2b904b..a05cd3cb 100644 --- a/bin/tsw/runtime/Context.js +++ b/bin/tsw/runtime/Context.js @@ -15,14 +15,14 @@ function Context(){ } Context.prototype.setModAct = function(mod_act){ - + context.mod_act = mod_act; - + return true; }; Context.prototype.getModAct = function(){ - + return context.mod_act; }; diff --git a/bin/tsw/runtime/JankWatcher.js b/bin/tsw/runtime/JankWatcher.js index e85dd98e..51d60f18 100644 --- a/bin/tsw/runtime/JankWatcher.js +++ b/bin/tsw/runtime/JankWatcher.js @@ -7,7 +7,7 @@ */ 'use strict'; -const tnm2 = require('api/tnm2'); +const tnm2 = require('api/tnm2'); if(!global[__filename]) { diff --git a/bin/tsw/runtime/capturer.js b/bin/tsw/runtime/capturer.js index ee17aead..5c1d215d 100644 --- a/bin/tsw/runtime/capturer.js +++ b/bin/tsw/runtime/capturer.js @@ -11,9 +11,9 @@ * 抓包 * */ -const http = require('http'); -const https = require('https'); -var isFirstLoad = true; +const http = require('http'); +const https = require('https'); +var isFirstLoad = true; if(global[__filename]){ isFirstLoad = false; @@ -23,29 +23,29 @@ if(global[__filename]){ isFirstLoad && process.nextTick(function() { - const logger = require('logger'); - const httpUtil = require('util/http'); - const alpha = require('util/auto-report/alpha.js'); - const serverInfo = require('serverInfo.js'); + const logger = require('logger'); + const httpUtil = require('util/http'); + const alpha = require('util/auto-report/alpha.js'); + const serverInfo = require('serverInfo.js'); - const create = function(oriRequest,protocol){ + const create = function(oriRequest,protocol){ return function(...args){ - var opt = args[0]; - var request = oriRequest.apply(this,args); - var captureBody = false; - var result = []; - var buffer = Buffer.alloc(0); - var bodySize = 0; - var maxBodySize = 1024 * 1024; - var timeStart = Date.now(); - var timeEnd = 0; - var timeResponse = 0; - // var timeCurr = timeStart; - var remoteAddress = ''; - var remotePort = ''; - var localAddress = ''; - var localPort = ''; - var host = (opt.headers && opt.headers.host) || opt.host; + var opt = args[0]; + var request = oriRequest.apply(this,args); + var captureBody = false; + var result = []; + var buffer = Buffer.alloc(0); + var bodySize = 0; + var maxBodySize = 1024 * 1024; + var timeStart = Date.now(); + var timeEnd = 0; + var timeResponse = 0; + // var timeCurr = timeStart; + var remoteAddress = ''; + var remotePort = ''; + var localAddress = ''; + var localPort = ''; + var host = (opt.headers && opt.headers.host) || opt.host; if(context.requestCaptureSN){ context.requestCaptureSN++; @@ -53,16 +53,16 @@ process.nextTick(function() { context.requestCaptureSN = 1; } - var SN = context.requestCaptureSN || 0; - var logPre = `[${SN}] `; + var SN = context.requestCaptureSN || 0; + var logPre = `[${SN}] `; logger.debug(logPre + '${method} ${ip}:${port} ~ ${protocol}//${host}${path}',{ - protocol : protocol, - method : opt.method, - host : host, - ip : opt.host, - port : opt.port || (protocol === 'https:' ? 443 : 80), - path : opt.path + protocol : protocol, + method : opt.method, + host : host, + ip : opt.host, + port : opt.port || (protocol === 'https:' ? 443 : 80), + path : opt.path }); //抓包 @@ -80,15 +80,15 @@ process.nextTick(function() { } var report = function(oriResponse){ - var logJson = logger.getJson(); - var response = oriResponse || { + var logJson = logger.getJson(); + var response = oriResponse || { headers : { - 'content-length' : 0, - 'content-type' : 'text/html' + 'content-length' : 0, + 'content-type' : 'text/html' }, - httpVersion : '1.1', - statusCode : 513, - statusMessage : 'server buisy' + httpVersion : '1.1', + statusCode : 513, + statusMessage : 'server buisy' }; if(!logJson){ @@ -96,25 +96,25 @@ process.nextTick(function() { return; } - var curr = { - SN : SN, - - protocol : protocol === 'https:' ? 'HTTPS' : 'HTTP', - host : host, - url : `${protocol}//${host}${opt.path}`, - cache : '', - process : 'TSW:' + process.pid, - resultCode : response.statusCode, - contentLength : bodySize || response.headers['content-length'], - contentType : response.headers['content-type'], - clientIp : localAddress || serverInfo.intranetIp, - clientPort : localPort || '', - serverIp : remoteAddress || opt.host, - serverPort : remotePort || opt.port, - requestRaw : httpUtil.getClientRequestHeaderStr(request) + (request._body.toString('UTF-8') || ''), - responseHeader : httpUtil.getClientResponseHeaderStr(response,bodySize), - responseBody : (buffer.toString('base64')) || '', - timestamps : { + var curr = { + SN : SN, + + protocol : protocol === 'https:' ? 'HTTPS' : 'HTTP', + host : host, + url : `${protocol}//${host}${opt.path}`, + cache : '', + process : 'TSW:' + process.pid, + resultCode : response.statusCode, + contentLength : bodySize || response.headers['content-length'], + contentType : response.headers['content-type'], + clientIp : localAddress || serverInfo.intranetIp, + clientPort : localPort || '', + serverIp : remoteAddress || opt.host, + serverPort : remotePort || opt.port, + requestRaw : httpUtil.getClientRequestHeaderStr(request) + (request._body.toString('UTF-8') || ''), + responseHeader : httpUtil.getClientResponseHeaderStr(response,bodySize), + responseBody : (buffer.toString('base64')) || '', + timestamps : { ClientConnected : new Date(timeStart), ClientBeginRequest : new Date(timeStart), GotRequestHeaders : new Date(timeStart), @@ -138,22 +138,22 @@ process.nextTick(function() { }; request.once('response',(response)=>{ - timeResponse = Date.now(); + timeResponse = Date.now(); - var socket = response.socket; + var socket = response.socket; process.domain && process.domain.add(response); - remoteAddress = socket.remoteAddress; - remotePort = socket.remotePort; - localAddress = socket.localAddress; - localPort = socket.localPort; + remoteAddress = socket.remoteAddress; + remotePort = socket.remotePort; + localAddress = socket.localAddress; + localPort = socket.localPort; logger.debug(logPre + '${localAddress}:${localPort} > ${remoteAddress}:${remotePort} response ${statusCode} cost:${cost}ms ${encoding}',{ - remoteAddress : remoteAddress, - remotePort : remotePort, - localAddress : localAddress, - localPort : localPort, + remoteAddress : remoteAddress, + remotePort : remotePort, + localAddress : localAddress, + localPort : localPort, statusCode: response.statusCode, encoding: response.headers['content-encoding'], cost: timeResponse - timeStart @@ -185,9 +185,9 @@ process.nextTick(function() { // timeCurr = Date.now(); //logger.debug('${logPre}receive data: ${size},\tcost: ${cost}ms',{ - // logPre: logPre, - // cost: cost, - // size: chunk.length + // logPre: logPre, + // cost: cost, + // size: chunk.length //}); bodySize += chunk.length; @@ -223,7 +223,7 @@ process.nextTick(function() { }; }; - http.request = create(http.request,'http:'); - https.request = create(https.request,'https:'); + http.request = create(http.request,'http:'); + https.request = create(https.request,'https:'); }); diff --git a/bin/tsw/runtime/fs.hack.js b/bin/tsw/runtime/fs.hack.js index 10ebc5f7..6a88b3f4 100644 --- a/bin/tsw/runtime/fs.hack.js +++ b/bin/tsw/runtime/fs.hack.js @@ -19,20 +19,20 @@ if(global[__filename]){ //追踪重复读写 process.nextTick(function(){ - var cache = global[__filename]; - var isWindows = require('util/isWindows'); - var fs = require('fs'); - var logger = require('logger'); - var config = require('config'); - var tnm2 = require('api/tnm2'); + var cache = global[__filename]; + var isWindows = require('util/isWindows'); + var fs = require('fs'); + var logger = require('logger'); + var config = require('config'); + var tnm2 = require('api/tnm2'); fs.existsSync = hack(fs.existsSync,function(file){ - var sum = 0; - var name = 'fs.existsSync'; - var key = name + file; + var sum = 0; + var name = 'fs.existsSync'; + var key = name + file; - cache.map[key] = ~~cache.map[key] + 1; - sum = cache.map[key]; + cache.map[key] = ~~cache.map[key] + 1; + sum = cache.map[key]; if(sum % TIMES_LIMIT === 0 && !config.devMode){ logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}',{ @@ -47,12 +47,12 @@ if(global[__filename]){ }); fs.writeFileSync = hack(fs.writeFileSync,function(file){ - var sum = 0; - var name = 'fs.writeFileSync'; - var key = name + file; + var sum = 0; + var name = 'fs.writeFileSync'; + var key = name + file; - cache.map[key] = ~~cache.map[key] + 1; - sum = cache.map[key]; + cache.map[key] = ~~cache.map[key] + 1; + sum = cache.map[key]; if(sum % TIMES_LIMIT === 0 && !config.devMode){ logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}',{ @@ -67,12 +67,12 @@ if(global[__filename]){ }); fs.statSync = hack(fs.statSync,function(file){ - var sum = 0; - var name = 'fs.statSync'; - var key = name + file; + var sum = 0; + var name = 'fs.statSync'; + var key = name + file; - cache.map[key] = ~~cache.map[key] + 1; - sum = cache.map[key]; + cache.map[key] = ~~cache.map[key] + 1; + sum = cache.map[key]; if(sum > TIMES_LIMIT && !config.devMode){ logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}',{ @@ -87,12 +87,12 @@ if(global[__filename]){ }); fs.accessSync = hack(fs.accessSync,function(file){ - var sum = 0; - var name = 'fs.accessSync'; - var key = name + file; + var sum = 0; + var name = 'fs.accessSync'; + var key = name + file; - cache.map[key] = ~~cache.map[key] + 1; - sum = cache.map[key]; + cache.map[key] = ~~cache.map[key] + 1; + sum = cache.map[key]; if(sum % TIMES_LIMIT === 0 && !config.devMode){ logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}',{ @@ -107,12 +107,12 @@ if(global[__filename]){ }); fs.readFileSync = hack(fs.readFileSync,function(file){ - var sum = 0; - var name = 'fs.readFileSync'; - var key = name + file; + var sum = 0; + var name = 'fs.readFileSync'; + var key = name + file; - cache.map[key] = ~~cache.map[key] + 1; - sum = cache.map[key]; + cache.map[key] = ~~cache.map[key] + 1; + sum = cache.map[key]; if(sum > TIMES_LIMIT && !config.devMode){ logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}',{ @@ -127,12 +127,12 @@ if(global[__filename]){ }); fs.readdirSync = hack(fs.readdirSync,function(file){ - var sum = 0; - var name = 'fs.readdirSync'; - var key = name + file; + var sum = 0; + var name = 'fs.readdirSync'; + var key = name + file; - cache.map[key] = ~~cache.map[key] + 1; - sum = cache.map[key]; + cache.map[key] = ~~cache.map[key] + 1; + sum = cache.map[key]; if(sum % TIMES_LIMIT === 0 && !config.devMode){ logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}',{ @@ -147,10 +147,10 @@ if(global[__filename]){ }); //fs.realpathSync = hack(fs.realpathSync,function(file){ - // logger.debug('[sync] ${name} ${file}',{ - // name: 'fs.realpathSync', - // file: file - // }); + // logger.debug('[sync] ${name} ${file}',{ + // name: 'fs.realpathSync', + // file: file + // }); //}); function hack(fn,callback){ diff --git a/bin/tsw/serverInfo.js b/bin/tsw/serverInfo.js index a23172bb..ccee75e4 100644 --- a/bin/tsw/serverInfo.js +++ b/bin/tsw/serverInfo.js @@ -7,9 +7,9 @@ */ 'use strict'; -const os = require('os'); -const {isWindows} = require('util/isWindows'); -const isInnerIP = require('util/http.isInnerIP.js'); +const os = require('os'); +const {isWindows} = require('util/isWindows'); +const isInnerIP = require('util/http.isInnerIP.js'); this.intranetIp = '127.0.0.1'; @@ -24,8 +24,8 @@ function getLinuxLocalIpv4(){ var networkInterfaces = os.networkInterfaces(); Object.keys(networkInterfaces).forEach(function(key){ - var eth = networkInterfaces[key]; - var address = eth && eth[0] && eth[0].address; + var eth = networkInterfaces[key]; + var address = eth && eth[0] && eth[0].address; if(!address){ return; diff --git a/bin/tsw/util/CD.js b/bin/tsw/util/CD.js index e20d1fe0..3b3398a1 100644 --- a/bin/tsw/util/CD.js +++ b/bin/tsw/util/CD.js @@ -7,19 +7,19 @@ */ 'use strict'; -const url = require('url'); -const Deferred = require('util/Deferred'); -const config = require('config'); -const logger = require('logger'); -const cmemTSW = require('data/cmem.tsw.js'); -const tnm2 = require('api/tnm2'); -const gzipHttp = require('util/gzipHttp.js'); -const openapi = require('util/openapi'); -const crypto = require('crypto'); -const cacheTime = 10 * 60 * 1000; //最大cache时间 - -var cacheStart = Date.now(); -var cache = {}; +const url = require('url'); +const Deferred = require('util/Deferred'); +const config = require('config'); +const logger = require('logger'); +const cmemTSW = require('data/cmem.tsw.js'); +const tnm2 = require('api/tnm2'); +const gzipHttp = require('util/gzipHttp.js'); +const openapi = require('util/openapi'); +const crypto = require('crypto'); +const cacheTime = 10 * 60 * 1000; //最大cache时间 + +var cacheStart = Date.now(); +var cache = {}; if(global[__filename]){ cache = global[__filename]; @@ -39,10 +39,10 @@ this.check = function(key,count,second){ var checkByOpenapi = function(keyOri,count,second){ - var defer = Deferred.create(); - var appid = context.appid || null; + var defer = Deferred.create(); + var appid = context.appid || null; - var key = 'CD3.' + crypto.createHash('sha1').update(`CD3.${appid}.${keyOri}.${count}.${second}`).digest('hex'); + var key = 'CD3.' + crypto.createHash('sha1').update(`CD3.${appid}.${keyOri}.${count}.${second}`).digest('hex'); var start = Date.now(); if(cache[key]){ @@ -57,8 +57,8 @@ var checkByOpenapi = function(keyOri,count,second){ //清缓存逻辑 if(start - cacheStart > cacheTime){ - cache = {}; - cacheStart = Date.now(); + cache = {}; + cacheStart = Date.now(); } @@ -79,26 +79,26 @@ var checkByOpenapi = function(keyOri,count,second){ return defer.reject(); } - var sig = openapi.signature({ + var sig = openapi.signature({ pathname: url.parse(config.utilCDUrl).pathname, method: 'POST', data: postData, appkey: config.appkey }); - postData.sig = sig; + postData.sig = sig; require('ajax').request({ - url : config.utilCDUrl, - type : 'POST', - l5api : config.tswL5api['openapi.tswjs.org'], - dcapi : { + url : config.utilCDUrl, + type : 'POST', + l5api : config.tswL5api['openapi.tswjs.org'], + dcapi : { key: 'EVENT_TSW_OPENAPI_UTIL_CD' }, - data : postData, - keepAlive : true, - autoToken : false, - dataType : 'json' + data : postData, + keepAlive : true, + autoToken : false, + dataType : 'json' }).fail(function(){ logger.error('checkByOpenapi fail.'); defer.reject(); @@ -122,10 +122,10 @@ var checkByOpenapi = function(keyOri,count,second){ this.curr = function(keyOri,count,second){ - var defer = Deferred.create(); - var appid = context.appid || null; + var defer = Deferred.create(); + var appid = context.appid || null; - var key = 'CD3.' + crypto.createHash('sha1').update(`CD3.${appid}.${keyOri}.${count}.${second}`).digest('hex'); + var key = 'CD3.' + crypto.createHash('sha1').update(`CD3.${appid}.${keyOri}.${count}.${second}`).digest('hex'); var memcached = module.exports.cmem(); @@ -147,10 +147,10 @@ this.curr = function(keyOri,count,second){ var checkByCmem = function(keyOri,count,second){ - var defer = Deferred.create(); - var appid = context.appid || null; + var defer = Deferred.create(); + var appid = context.appid || null; - var key = 'CD3.' + crypto.createHash('sha1').update(`CD3.${appid}.${keyOri}.${count}.${second}`).digest('hex'); + var key = 'CD3.' + crypto.createHash('sha1').update(`CD3.${appid}.${keyOri}.${count}.${second}`).digest('hex'); var start = Date.now(); if(cache[key]){ @@ -165,8 +165,8 @@ var checkByCmem = function(keyOri,count,second){ //清缓存逻辑 if(start - cacheStart > cacheTime){ - cache = {}; - cacheStart = Date.now(); + cache = {}; + cacheStart = Date.now(); } var memcached = module.exports.cmem(); @@ -233,8 +233,8 @@ this.cmem = function(){ //开放接口 this.openapi = async function(req,res){ - var appid = context.appid; - var appkey = context.appkey; + var appid = context.appid; + var appkey = context.appkey; if(req.param('appid') !== appid){ returnJson({ code: -2 , message: 'appid错误'}); @@ -256,11 +256,11 @@ this.openapi = async function(req,res){ return; } - logger.setKey(`CD_${appid}`); //上报key + logger.setKey(`CD_${appid}`); //上报key - var key = req.param('key'); - var second = ~~req.param('second'); //单位秒 - var count = ~~req.param('count'); //默认是1 + var key = req.param('key'); + var second = ~~req.param('second'); //单位秒 + var count = ~~req.param('count'); //默认是1 if(!key){ returnJson({ code: -2 , message: 'key is required'}); diff --git a/bin/tsw/util/Deferred/index.js b/bin/tsw/util/Deferred/index.js index 690a4404..d74daf3e 100644 --- a/bin/tsw/util/Deferred/index.js +++ b/bin/tsw/util/Deferred/index.js @@ -27,7 +27,7 @@ function createFlags( flags ) { } var jQuery = { - + // See test/unit/core.js for details concerning isFunction. // Since version 1.3, DOM methods and functions like alert // aren't supported. They return false on IE (#2968). @@ -65,8 +65,8 @@ var jQuery = { try { // Not own constructor property must be Object if ( obj.constructor && - !hasOwn.call(obj, 'constructor') && - !hasOwn.call(obj.constructor.prototype, 'isPrototypeOf') ) { + !hasOwn.call(obj, 'constructor') && + !hasOwn.call(obj.constructor.prototype, 'isPrototypeOf') ) { return false; } } catch ( e ) { @@ -137,7 +137,7 @@ var jQuery = { return object; } - + }; // Populate the class2type map @@ -213,23 +213,23 @@ jQuery.extend = function() { /* * Create a callback list using the following parameters: * - * flags: an optional list of space-separated flags that will change how - * the callback list behaves + * flags: an optional list of space-separated flags that will change how + * the callback list behaves * * By default a callback list will act like an event callback list and can be * "fired" multiple times. * * Possible flags: * - * once: will ensure the callback list can only be fired once (like a Deferred) + * once: will ensure the callback list can only be fired once (like a Deferred) * - * memory: will keep track of previous values and will call any callback added - * after the list has been fired right away with the latest "memorized" - * values (like a Deferred) + * memory: will keep track of previous values and will call any callback added + * after the list has been fired right away with the latest "memorized" + * values (like a Deferred) * - * unique: will ensure a callback can only be added once (no duplicate in the list) + * unique: will ensure a callback can only be added once (no duplicate in the list) * - * stopOnFalse: interrupt callings when a callback returns false + * stopOnFalse: interrupt callings when a callback returns false * */ jQuery.Callbacks = function( flags ) { @@ -276,9 +276,9 @@ jQuery.Callbacks = function( flags ) { }, // Fire callbacks fire = function( context, args ) { - + var fn,domain,ret; - + args = args || []; memory = !flags.memory || [context, args]; firing = true; @@ -286,11 +286,11 @@ jQuery.Callbacks = function( flags ) { firingStart = 0; firingLength = list.length; for (; list && firingIndex < firingLength; firingIndex++) { - + fn = list[firingIndex]; domain = fn.__domain; fn.__domain = undefined; - + // restore domain if needed if (domain && domain !== process.domain) { domain.run(function(){ @@ -299,7 +299,7 @@ jQuery.Callbacks = function( flags ) { }else { ret = fn.apply(context, args); } - + if (ret === false && flags.stopOnFalse) { memory = true; // Mark as halted break; diff --git a/bin/tsw/util/Queue/index.js b/bin/tsw/util/Queue/index.js index 7e78b06f..f5ff3cd1 100644 --- a/bin/tsw/util/Queue/index.js +++ b/bin/tsw/util/Queue/index.js @@ -8,7 +8,7 @@ 'use strict'; function Queue(){ - + this._queue = []; } @@ -19,45 +19,45 @@ this.create = function(){ Queue.prototype.queue = function(fn){ - + if(typeof fn !== 'function'){ - + return this; } - + fn._domain = process.domain; - + this._queue.push(fn); - + if(this._queue.length === 1){ this.dequeue(); } - + return this; }; Queue.prototype.dequeue = function(){ - + var domain,fn; - + if(this._queue.length === 0){ return this; } - + if(this._queue[0] === 'pending'){ - + this._queue.shift(); this.dequeue(); - + return this; } - + fn = this._queue[0]; this._queue[0] = 'pending'; - - domain = fn._domain; - fn._domain = undefined; - + + domain = fn._domain; + fn._domain = undefined; + if(domain && domain !== process.domain){ domain.run(() => { fn.call(this); @@ -65,7 +65,7 @@ Queue.prototype.dequeue = function(){ }else{ fn.call(this); } - + return this; }; diff --git a/bin/tsw/util/auto-report/TEReport.js b/bin/tsw/util/auto-report/TEReport.js index 3c4dde09..a7a93f70 100644 --- a/bin/tsw/util/auto-report/TEReport.js +++ b/bin/tsw/util/auto-report/TEReport.js @@ -11,17 +11,17 @@ * 测试环境自动发现 */ -const serverInfo = require('serverInfo'); -const config = require('config'); -const logReport = require('./logReport.js'); -const post = require('util/auto-report/post.js'); -const postOpenapi = require('util/auto-report/post.openapi.js'); -const Deferred = require('util/Deferred'); -const isWindows = require('util/isWindows.js'); +const serverInfo = require('serverInfo'); +const config = require('config'); +const logReport = require('./logReport.js'); +const post = require('util/auto-report/post.js'); +const postOpenapi = require('util/auto-report/post.openapi.js'); +const Deferred = require('util/Deferred'); +const isWindows = require('util/isWindows.js'); this.report = function(){ - + if(isWindows.isWindows){ return; } @@ -32,8 +32,8 @@ this.report = function(){ var logText = `${serverInfo.intranetIp}:${config.httpPort}`; var logJson = { - ip : serverInfo.intranetIp, - port : config.httpPort + ip : serverInfo.intranetIp, + port : config.httpPort }; require('api/cmdb').GetDeviceThisServer().done(function(d){ @@ -54,19 +54,19 @@ this.report = function(){ }; } - logJson.moduleId = business.moduleId; - logJson.moduleName = [business.L1Business,business.L2Business,business.L3Business,business.module].join('->'); + logJson.moduleId = business.moduleId; + logJson.moduleName = [business.L1Business,business.L2Business,business.L3Business,business.module].join('->'); - logJson = Deferred.extend(true,{ - time : new Date().toGMTString(), - name : '', - group : 'unknown', - desc : '', - order : 0, - owner : '' + logJson = Deferred.extend(true,{ + time : new Date().toGMTString(), + name : '', + group : 'unknown', + desc : '', + order : 0, + owner : '' },config.testInfo,logJson); - var logKey = 'h5test' + logJson.group; + var logKey = 'h5test' + logJson.group; //上报自己 post.report(logKey,logText,logJson); @@ -74,17 +74,17 @@ this.report = function(){ //开放平台上报,不用再分组了 if(config.appid && config.appkey){ logReport.reportCloud({ - type : 'alpha', - logText : logText, - logJson : logJson, - key : 'h5test', - group : 'tsw', - mod_act : 'h5test', - ua : '', - userip : '', - host : '', - pathname : '', - statusCode : '' + type : 'alpha', + logText : logText, + logJson : logJson, + key : 'h5test', + group : 'tsw', + mod_act : 'h5test', + ua : '', + userip : '', + host : '', + pathname : '', + statusCode : '' }); } @@ -94,16 +94,16 @@ this.report = function(){ }); }); - + }; this.list = function(group){ - - var defer = Deferred.create(); + + var defer = Deferred.create(); var getLogJsonDefer; - group = group || ''; + group = group || ''; //开平对应的存储 if(context.appid && context.appkey){ @@ -113,10 +113,10 @@ this.list = function(group){ } getLogJsonDefer.done(function(arr){ - + var res = []; var map = {}; - + arr.forEach(function(v){ if(!map[v.ip]){ map[v.ip] = true; @@ -144,14 +144,14 @@ this.list = function(group){ defer.resolve(res); }); - + return defer; }; this.getAllGroup = function(){ - var defer = Deferred.create(); + var defer = Deferred.create(); post.getLogJson('group.h5test').done(function(arr){ diff --git a/bin/tsw/util/auto-report/alpha.js b/bin/tsw/util/auto-report/alpha.js index 4ac16e2b..93f84089 100644 --- a/bin/tsw/util/auto-report/alpha.js +++ b/bin/tsw/util/auto-report/alpha.js @@ -7,10 +7,10 @@ */ 'use strict'; -const config = require('config'); -const logger = require('logger'); -const {isWindows} = require('util/isWindows'); -const TSW = require('api/keyman'); +const config = require('config'); +const logger = require('logger'); +const {isWindows} = require('util/isWindows'); +const TSW = require('api/keyman'); if(!global[__filename]){ global[__filename] = {}; diff --git a/bin/tsw/util/auto-report/logReport.js b/bin/tsw/util/auto-report/logReport.js index b82410f9..75332c93 100644 --- a/bin/tsw/util/auto-report/logReport.js +++ b/bin/tsw/util/auto-report/logReport.js @@ -7,38 +7,38 @@ */ 'use strict'; -const fs = require('fs'); -const url = require('url'); -const logger = require('logger'); -const dcapi = require('api/libdcapi/dcapi.js'); -const serverInfo = require('serverInfo'); -const config = require('config'); -const alpha = require('./alpha.js'); -const httpUtil = require('util/http'); -const openapi = require('util/openapi'); -const isTST = require('util/isTST.js'); -const post = require('util/auto-report/post.js'); -const postOpenapi = require('util/auto-report/post.openapi.js'); -const tnm2 = require('api/tnm2'); -const format = require('webapp/utils/format'); -const CD = require('util/CD.js'); -const canIuse = /^[0-9a-zA-Z_-]{0,64}$/; -const MAX_ALPHA_LOG = post.MAX_ALPHA_LOG; +const fs = require('fs'); +const url = require('url'); +const logger = require('logger'); +const dcapi = require('api/libdcapi/dcapi.js'); +const serverInfo = require('serverInfo'); +const config = require('config'); +const alpha = require('./alpha.js'); +const httpUtil = require('util/http'); +const openapi = require('util/openapi'); +const isTST = require('util/isTST.js'); +const post = require('util/auto-report/post.js'); +const postOpenapi = require('util/auto-report/post.openapi.js'); +const tnm2 = require('api/tnm2'); +const format = require('webapp/utils/format'); +const CD = require('util/CD.js'); +const canIuse = /^[0-9a-zA-Z_-]{0,64}$/; +const MAX_ALPHA_LOG = post.MAX_ALPHA_LOG; var limit = { count: { - error : 0, - alpha : 0, - force : 0, - fail : 0, - tst : 0 + error : 0, + alpha : 0, + force : 0, + fail : 0, + tst : 0 }, max: { - error : 20, - alpha : 20, - force : 9999, - fail : 9999, - tst : 9999 + error : 20, + alpha : 20, + force : 9999, + fail : 9999, + tst : 9999 }, time: 0 }; @@ -46,7 +46,7 @@ var limit = { module.exports = function(req, res){ - var window = context.window || {}; + var window = context.window || {}; var isWebSocket = !!window.websocket; if(isWebSocket){ @@ -67,12 +67,12 @@ module.exports = function(req, res){ req.removeAllListeners('reportLog'); req.once('reportLog',function(){ - + var log = logger.getLog(), - type = '', - typeKey = '', - arrtKey = '', - code = 0, + type = '', + typeKey = '', + arrtKey = '', + code = 0, logJson,logText, key,group,mod_act; @@ -80,18 +80,18 @@ module.exports = function(req, res){ if(!isWebSocket && global.top100){ module.exports.top100(req,res); } - + if(!log){ logger.debug('log is null'); return; } - - key = logger.getKey(); - group = logger.getGroup(); - + + key = logger.getKey(); + group = logger.getGroup(); + if(!key){ key = alpha.getUin(req); - + if(key){ if(canIuse.test(key)){ logger.setKey(key); @@ -101,30 +101,30 @@ module.exports = function(req, res){ } } - mod_act = context.mod_act || 'null'; - log = logger.getLog(); - + mod_act = context.mod_act || 'null'; + log = logger.getLog(); + if(alpha.isAlpha(key) && !isTST.isTST(req)){ - type = 'alpha'; - typeKey = 'EVENT_TSW_LOG_ALPHA'; - arrtKey = 'SUM_TSW_LOG_ALPHA'; - code = 0; + type = 'alpha'; + typeKey = 'EVENT_TSW_LOG_ALPHA'; + arrtKey = 'SUM_TSW_LOG_ALPHA'; + code = 0; }else if(log.ERRO || log.WARN){ key && logger.error('report key: ${key}',{key: key}); - type = 'error'; - typeKey = 'EVENT_TSW_LOG_ERROR'; - arrtKey = 'SUM_TSW_LOG_ERROR'; - code = 1; + type = 'error'; + typeKey = 'EVENT_TSW_LOG_ERROR'; + arrtKey = 'SUM_TSW_LOG_ERROR'; + code = 1; }else if(log.force){ - type = 'force'; - typeKey = 'EVENT_TSW_LOG_FORCE'; - arrtKey = 'SUM_TSW_LOG_FORCE'; - code = 3; + type = 'force'; + typeKey = 'EVENT_TSW_LOG_FORCE'; + arrtKey = 'SUM_TSW_LOG_FORCE'; + code = 3; }else if(context.dcapiIsFail && key){ - type = 'fail'; - typeKey = 'EVENT_TSW_LOG_FAIL'; - arrtKey = 'SUM_TSW_LOG_FAIL'; - code = 2; + type = 'fail'; + typeKey = 'EVENT_TSW_LOG_FAIL'; + arrtKey = 'SUM_TSW_LOG_FAIL'; + code = 2; }else{ logger.debug('report nothing: ' + key); //不用上报 @@ -133,10 +133,10 @@ module.exports = function(req, res){ if(isTST.isTST(req)){ logger.debug('log type origin: ' + type); - type = 'TST'; - typeKey = 'EVENT_TSW_LOG_TST'; - arrtKey = 'SUM_TSW_LOG_TST'; - code = -1; + type = 'TST'; + typeKey = 'EVENT_TSW_LOG_TST'; + arrtKey = 'SUM_TSW_LOG_TST'; + code = -1; } if(limit.max[type] > 0 === false){ @@ -149,35 +149,35 @@ module.exports = function(req, res){ }else{ limit.count[type] = 1; } - + if(Date.now() - limit.time < 5000){ - + if(limit.count[type] > limit.max[type]){ - + dcapi.report({ - key : typeKey, - toIp : '127.0.0.1', - code : -1, - isFail : 1, - delay : 100 + key : typeKey, + toIp : '127.0.0.1', + code : -1, + isFail : 1, + delay : 100 }); - + return; } - + }else{ limit.count = {}; - limit.time = Date.now(); + limit.time = Date.now(); } logger.debug('logType: ' + type); dcapi.report({ - key : typeKey, - toIp : '127.0.0.1', - code : code, - isFail : 0, - delay : 100 + key : typeKey, + toIp : '127.0.0.1', + code : code, + isFail : 0, + delay : 100 }); tnm2.Attr_API(arrtKey, 1); @@ -187,14 +187,14 @@ module.exports = function(req, res){ } logger.debug('\n${headers}${body}\r\nresponse ${statusCode} ${resHeaders}',{ - headers : httpUtil.getRequestHeaderStr(req), - body : req.REQUEST.body || '', - statusCode : res.statusCode, - resHeaders : JSON.stringify(res._headers,null,2) + headers : httpUtil.getRequestHeaderStr(req), + body : req.REQUEST.body || '', + statusCode : res.statusCode, + resHeaders : JSON.stringify(res._headers,null,2) }); logText = logText || logger.getText(); - + if(type === 'alpha'){ try{ @@ -203,25 +203,25 @@ module.exports = function(req, res){ res._body = Buffer.from(format.formatBuffer(res._body)); } - logJson = logJson || logger.getJson(); - logJson.curr = { - protocol : 'HTTP', - host : req.headers.host, - url : req.REQUEST.path, - cache : '', - process : 'TSW:' + process.pid, - resultCode : res.statusCode, - contentLength : isWebSocket ? (res._body ? res._body.length : 0) : (res._headers['content-length'] || res._bodySize), - contentType : isWebSocket ? 'websocket' : res._headers['content-type'], - clientIp : httpUtil.getUserIp(req), - clientPort : req.socket && req.socket.remotePort, - serverIp : serverInfo.intranetIp, - serverPort : config.httpPort, - requestRaw : httpUtil.getRequestHeaderStr(req) + (req.REQUEST.body || ''), - responseHeader : httpUtil.getResponseHeaderStr(res), - responseBody : res._body ? res._body.toString('base64') : '', - logText : logText, - timestamps : req.timestamps + logJson = logJson || logger.getJson(); + logJson.curr = { + protocol : 'HTTP', + host : req.headers.host, + url : req.REQUEST.path, + cache : '', + process : 'TSW:' + process.pid, + resultCode : res.statusCode, + contentLength : isWebSocket ? (res._body ? res._body.length : 0) : (res._headers['content-length'] || res._bodySize), + contentType : isWebSocket ? 'websocket' : res._headers['content-type'], + clientIp : httpUtil.getUserIp(req), + clientPort : req.socket && req.socket.remotePort, + serverIp : serverInfo.intranetIp, + serverPort : config.httpPort, + requestRaw : httpUtil.getRequestHeaderStr(req) + (req.REQUEST.body || ''), + responseHeader : httpUtil.getResponseHeaderStr(res), + responseBody : res._body ? res._body.toString('base64') : '', + logText : logText, + timestamps : req.timestamps }; }catch(e){ logger.error(e.stack); @@ -230,18 +230,18 @@ module.exports = function(req, res){ } var reportData = { - type : type || '', - logText : logText || '', - logJson : logJson, - key : String(key), - group : String(group || ''), - mod_act : String(mod_act || ''), - ua : req.headers['user-agent'] || '', - userip : req.userIp || '', - host : req.headers.host || '', - pathname : req.REQUEST.pathname || '', - ext_info : '', - statusCode : res.statusCode + type : type || '', + logText : logText || '', + logJson : logJson, + key : String(key), + group : String(group || ''), + mod_act : String(mod_act || ''), + ua : req.headers['user-agent'] || '', + userip : req.userIp || '', + host : req.headers.host || '', + pathname : req.REQUEST.pathname || '', + ext_info : '', + statusCode : res.statusCode }; if(type === 'alpha'){ @@ -269,7 +269,7 @@ module.exports = function(req, res){ module.exports.report(reportData); }); - + }; module.exports.fingureCroup = function(opts){ @@ -289,7 +289,7 @@ module.exports.fingureCroup = function(opts){ //没声明算html return 'html'; } - + switch(true){ case /^text\/html/.test(contentType): group = 'html'; @@ -339,8 +339,8 @@ module.exports.reportAlpha = function(data){ return; } - var reportKey = [data.key].join('/'); - var logNumMax = context.MAX_ALPHA_LOG || MAX_ALPHA_LOG; + var reportKey = [data.key].join('/'); + var logNumMax = context.MAX_ALPHA_LOG || MAX_ALPHA_LOG; post.report(reportKey,data.logText,data.logJson).done(function(isFirst){ if(isFirst){ @@ -411,31 +411,31 @@ module.exports.reportCloud = function(data){ //加密 postData.logText = post.encode(config.appid,config.appkey,data.logText); postData.logJson = post.encode(config.appid,config.appkey,data.logJson); - postData.appid = config.appid; - postData.userip = ''; - postData.now = Date.now(); + postData.appid = config.appid; + postData.userip = ''; + postData.now = Date.now(); - var sig = openapi.signature({ + var sig = openapi.signature({ pathname: url.parse(config.logReportUrl).pathname, method: 'POST', data: postData, appkey: config.appkey }); - postData.sig = sig; + postData.sig = sig; require('ajax').request({ - url : config.logReportUrl, - type : 'POST', - l5api : config.tswL5api['openapi.tswjs.org'], - dcapi : { + url : config.logReportUrl, + type : 'POST', + l5api : config.tswL5api['openapi.tswjs.org'], + dcapi : { key: 'EVENT_TSW_OPENAPI_LOG_REPORT' }, - data : postData, - keepAlive : true, - autoToken : false, - dataType : 'json' + data : postData, + keepAlive : true, + autoToken : false, + dataType : 'json' }).done(function(){ logger.debug('reportCloud success.'); }).fail(function(){ @@ -494,13 +494,13 @@ module.exports.receiveCloud = function(req,res){ return returnJson('get appkey error'); } - var appid = context.appid; - var appkey = context.appkey; + var appid = context.appid; + var appkey = context.appkey; var reportKey = [appid,data.key].join('/'); - var currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24); - var logNumMax = context.MAX_ALPHA_LOG || MAX_ALPHA_LOG; + var currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24); + var logNumMax = context.MAX_ALPHA_LOG || MAX_ALPHA_LOG; - logger.setKey(`report_${appid}_${data.key}`); //上报key + logger.setKey(`report_${appid}_${data.key}`); //上报key logger.debug('report log type: ${type}, key ${key}',data); CD.curr(`SUM_TSW_ALPHA_LOG_KEY.${currDays}`,logNumMax,24 * 60 * 60).fail(function(err){ @@ -544,8 +544,8 @@ module.exports.receiveCloud = function(req,res){ returnJson(); }).done(function(){ - var logText = postOpenapi.encode(appid,appkey,data.group); - var logJson = postOpenapi.encode(appid,appkey,{group: data.group}); + var logText = postOpenapi.encode(appid,appkey,data.group); + var logJson = postOpenapi.encode(appid,appkey,{group: data.group}); postOpenapi.report(`${appid}/v2.group.alpha`,logText,logJson).always(function(){ returnJson(); @@ -558,13 +558,13 @@ module.exports.receiveCloud = function(req,res){ module.exports.top100 = function(req, res){ - + var item,str,filename,result; - + if(!global.top100){ return; } - + global.top100.push({ pathname: req.REQUEST.pathname, hostname: req.headers.host, @@ -575,110 +575,110 @@ module.exports.top100 = function(req, res){ statusCode : res.statusCode, resHeader: JSON.stringify(res._headers,null,2) }); - + if(global.top100.length % 100 === 0){ logger.info('top: ' + global.top100.length); } - + if(global.top100.length < 1000){ return; } - + result = global.top100; global.top100 = null; - - + + var map = {}; var arr = []; var key; var buffer = []; - + //分析pathname聚集 map = {}; arr = []; result.forEach(function(v,i){ - + var key = v.hostname + v.pathname; - + if(map[key]){ map[key] ++; }else{ map[key] = 1; } - + }); - + for(key in map){ - + item = map[key]; - + arr.push({ count: item, key: key }); - + } - + arr.sort(function(a,b){ return b.count - a.count; }); - + arr.forEach(function(v,i){ - + //top 100 if(i < 100){ buffer.push(v.count + '\t' + v.key); buffer.push('\r\n'); } - + }); - + buffer.push('\r\n\r\n'); - - + + //分析ip聚集 map = {}; arr = []; result.forEach(function(v,i){ - + if(map[v.ip]){ map[v.ip] ++; }else{ map[v.ip] = 1; } - + }); - + for(key in map){ - + item = map[key]; - + arr.push({ count: item, key: key }); - + } - + arr.sort(function(a,b){ return b.count - a.count; }); - + arr.forEach(function(v,i){ - + //top 100 if(i < 100){ buffer.push(v.count + '\t' + v.key); buffer.push('\r\n'); } - + }); - + buffer.push('\r\n\r\n'); - - + + result.forEach(function(v,i){ - + buffer.push(v.ip, ', socket ip: ' + v.socketIp); buffer.push('\r\n'); buffer.push(v.header); @@ -686,17 +686,17 @@ module.exports.top100 = function(req, res){ buffer.push('\r\n\r\n'); buffer.push(v.statusCode + ':' + v.resHeader); buffer.push('\r\n\r\n\r\n'); - + }); - - + + str = buffer.join(''); - + filename = __dirname + '/../../../proxy/cpu' + process.serverInfo.cpu + '.' + Date.now() + '.top100'; - - + + fs.writeFile(filename,Buffer.from(str,'utf-8'),function(err){ logger.info('top100: ' + filename); }); - + }; diff --git a/bin/tsw/util/auto-report/post.js b/bin/tsw/util/auto-report/post.js index 0ba1243f..071ef05b 100644 --- a/bin/tsw/util/auto-report/post.js +++ b/bin/tsw/util/auto-report/post.js @@ -7,13 +7,13 @@ */ 'use strict'; -const logger = require('logger'); -const Deferred = require('util/Deferred'); -const cmemTSW = require('data/cmem.tsw.js'); -const crypto = require('crypto'); -const zlib = require('zlib'); -const MAX_NUM = 64; -const MAX_ALPHA_LOG = 1000; +const logger = require('logger'); +const Deferred = require('util/Deferred'); +const cmemTSW = require('data/cmem.tsw.js'); +const crypto = require('crypto'); +const zlib = require('zlib'); +const MAX_NUM = 64; +const MAX_ALPHA_LOG = 1000; module.exports.MAX_ALPHA_LOG = MAX_ALPHA_LOG; @@ -44,19 +44,19 @@ module.exports.getLogJsonByKey = function (uin,key) { }; module.exports.getLogArr = function(uin,type,key,limit){ - - var defer = Deferred.create(); - var memcached = this.cmem(); - var keyBitmap = this.keyBitmap(uin); + + var defer = Deferred.create(); + var memcached = this.cmem(); + var keyBitmap = this.keyBitmap(uin); if(!memcached){ return defer.resolve([]); } memcached.get(keyBitmap,function(err,data){ - var start = data || 0; + var start = data || 0; - var keyTextArr = type === 'text' ? module.exports.keyTextArr(uin,start) : module.exports.keyJsonArr(uin,start); + var keyTextArr = type === 'text' ? module.exports.keyTextArr(uin,start) : module.exports.keyJsonArr(uin,start); var keyJsonArr = module.exports.keyJsonArr(uin,start); //如果传递进来key,则只需要关注传进来的key @@ -118,7 +118,7 @@ module.exports.getLogArr = function(uin,type,key,limit){ }); }); - + return defer; }; @@ -147,22 +147,22 @@ module.exports.getLogResultCode = function (logText) { }; module.exports.report = function(key,logText,logJson){ - var defer = Deferred.create(); + var defer = Deferred.create(); if(!key){ return defer.reject(); } - + if(!logText){ return defer.reject(); } - + logJson = logJson || {}; - var memcached = this.cmem(); //要用this - var keyText = module.exports.keyText(key); - var keyJson = module.exports.keyJson(key); - var keyBitmap = module.exports.keyBitmap(key); + var memcached = this.cmem(); //要用this + var keyText = module.exports.keyText(key); + var keyJson = module.exports.keyJson(key); + var keyBitmap = module.exports.keyBitmap(key); if(!memcached){ return defer.resolve(); @@ -223,7 +223,7 @@ module.exports.keyText = function(key){ }; module.exports.keyJsonArr = function(key,index){ - + var i = 0; var arr = []; var keyJson = this.keyJson(key); @@ -232,12 +232,12 @@ module.exports.keyJsonArr = function(key,index){ for(i = MAX_NUM; i > 0; i--){ arr.push([keyJson,(i + index)%MAX_NUM].join('.')); } - + return arr; }; module.exports.keyTextArr = function(key,index){ - + var i = 0; var arr = []; var keyText = this.keyText(key); @@ -246,40 +246,40 @@ module.exports.keyTextArr = function(key,index){ for(i = MAX_NUM; i > 0; i--){ arr.push([keyText,(i + index)%MAX_NUM].join('.')); } - + return arr; }; //加密 module.exports.encode = function(appid,appkey,data){ - var input = Buffer.from(JSON.stringify(data),'UTF-8'); - var buff = zlib.deflateSync(input); - var des = crypto.createCipher('des',(appid + appkey)); - var buf1 = des.update(buff, null , 'hex'); - var buf2 = des.final('hex'); - var body = Buffer.from(buf1 + buf2,'hex').toString('base64'); + var input = Buffer.from(JSON.stringify(data),'UTF-8'); + var buff = zlib.deflateSync(input); + var des = crypto.createCipher('des',(appid + appkey)); + var buf1 = des.update(buff, null , 'hex'); + var buf2 = des.final('hex'); + var body = Buffer.from(buf1 + buf2,'hex').toString('base64'); return body; }; //解密 module.exports.decode = function(appid,appkey,body){ - var des = crypto.createDecipher('des',(appid + appkey)); - var buf1 = ''; - var buf2 = ''; + var des = crypto.createDecipher('des',(appid + appkey)); + var buf1 = ''; + var buf2 = ''; try{ - buf1 = des.update(body,'base64', 'hex'); - buf2 = des.final('hex'); + buf1 = des.update(body,'base64', 'hex'); + buf2 = des.final('hex'); }catch(e){ logger.error(e.stack); return null; } - var buff = Buffer.from(buf1 + buf2 ,'hex'); - var input = zlib.inflateSync(buff); + var buff = Buffer.from(buf1 + buf2 ,'hex'); + var input = zlib.inflateSync(buff); - var data = null; + var data = null; try{ data = JSON.parse(input.toString('UTF-8')); diff --git a/bin/tsw/util/auto-report/post.openapi.js b/bin/tsw/util/auto-report/post.openapi.js index fa2a0d58..60adfcdb 100644 --- a/bin/tsw/util/auto-report/post.openapi.js +++ b/bin/tsw/util/auto-report/post.openapi.js @@ -7,8 +7,8 @@ */ 'use strict'; -const cmemTSW = require('data/cmem.tsw.js'); -const post = require('./post.js'); +const cmemTSW = require('data/cmem.tsw.js'); +const post = require('./post.js'); module.exports = Object.assign({},post); diff --git a/bin/tsw/util/auto-report/src/_config.js b/bin/tsw/util/auto-report/src/_config.js index 723cde21..0a1aae4c 100644 --- a/bin/tsw/util/auto-report/src/_config.js +++ b/bin/tsw/util/auto-report/src/_config.js @@ -10,14 +10,14 @@ define(function(require, exports, module){ return { - + tmpl: { create: true }, all: { create: false } - + }; - + }); diff --git a/bin/tsw/util/auto-report/view.js b/bin/tsw/util/auto-report/view.js index 4963baf1..abca08a1 100644 --- a/bin/tsw/util/auto-report/view.js +++ b/bin/tsw/util/auto-report/view.js @@ -7,16 +7,16 @@ */ 'use strict'; -const logger = require('logger'); -const gzipHttp = require('util/gzipHttp.js'); -const post = require('./post'); -const postOpenapi = require('./post.openapi.js'); -const tmpl = require('./tmpl'); -const OALogin = require('util/oa-login/index.js'); -const hls = require('./highlight-tsw.js'); -const canIuse = /^[0-9a-zA-Z_-]{0,64}$/; -const CD = require('util/CD.js'); -const MAX_ALPHA_LOG = post.MAX_ALPHA_LOG; +const logger = require('logger'); +const gzipHttp = require('util/gzipHttp.js'); +const post = require('./post'); +const postOpenapi = require('./post.openapi.js'); +const tmpl = require('./tmpl'); +const OALogin = require('util/oa-login/index.js'); +const hls = require('./highlight-tsw.js'); +const canIuse = /^[0-9a-zA-Z_-]{0,64}$/; +const CD = require('util/CD.js'); +const MAX_ALPHA_LOG = post.MAX_ALPHA_LOG; tmpl.hls = hls; @@ -34,22 +34,22 @@ module.exports.checkLogin = function(request, response, callback){ module.exports.go = function(request, response){ - var arr = request.REQUEST.pathname.split('/'); - var appid = context.appid || ''; - var group = arr[3]; - var key = arr[4]; - var groupKey = 'v2.group.alpha'; - var limit = ~~context.limit || 64; - var currPost = post; + var arr = request.REQUEST.pathname.split('/'); + var appid = context.appid || ''; + var group = arr[3]; + var key = arr[4]; + var groupKey = 'v2.group.alpha'; + var limit = ~~context.limit || 64; + var currPost = post; if(appid){ - currPost = postOpenapi; - groupKey = `${appid}/v2.group.alpha`; + currPost = postOpenapi; + groupKey = `${appid}/v2.group.alpha`; } if(!key){ - key = group; - group = ''; + key = group; + group = ''; } if(!canIuse.test(appid)){ @@ -65,33 +65,33 @@ module.exports.go = function(request, response){ } var createLogKey = function(appid,group,key){ - var logKey = key; + var logKey = key; if(group){ - logKey = `${group}/${logKey}`; + logKey = `${group}/${logKey}`; } return logKey; }; - var logKey = createLogKey(appid,group,key); + var logKey = createLogKey(appid,group,key); if(appid){ - logKey = `${appid}/${logKey}`; + logKey = `${appid}/${logKey}`; } //上下文设置 - context.group = group; - context.limit = limit; - context.key = key; - context.groupKey = groupKey; - context.logKey = logKey; - context.createLogKey = createLogKey; - - var logCount = 0; - var logKeyCount = 0; - var logNumMax = context.MAX_ALPHA_LOG || MAX_ALPHA_LOG; - var currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24); + context.group = group; + context.limit = limit; + context.key = key; + context.groupKey = groupKey; + context.logKey = logKey; + context.createLogKey = createLogKey; + + var logCount = 0; + var logKeyCount = 0; + var logNumMax = context.MAX_ALPHA_LOG || MAX_ALPHA_LOG; + var currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24); logger.debug('logKey :${logKey}',{ logKey: logKey @@ -126,15 +126,15 @@ module.exports.go = function(request, response){ groupArr: groupArr, //也代表顺序 nameMap : { - html : 'html', - XHR : 'XHR', - js : 'js', - image : 'image', - css : 'css', + html : 'html', + XHR : 'XHR', + js : 'js', + image : 'image', + css : 'css', webapp : 'webapp', websocket : 'websocket', - tsw : 'tsw', - font : '字体' + tsw : 'tsw', + font : '字体' } }); diff --git a/bin/tsw/util/cache.cleaner.js b/bin/tsw/util/cache.cleaner.js index 75d75b1b..fd2b5b44 100644 --- a/bin/tsw/util/cache.cleaner.js +++ b/bin/tsw/util/cache.cleaner.js @@ -7,8 +7,8 @@ */ 'use strict'; -const path = require('path'); -const logger = require('logger'); +const path = require('path'); +const logger = require('logger'); /** * @@ -16,15 +16,15 @@ const logger = require('logger'); * @param {String} dir 目录绝对路径 */ this.clear = function(dir,showLog){ - - dir = path.normalize(dir); + + dir = path.normalize(dir); let key; - + logger.info('clear dir: ${dir}',{ dir: dir }); - + for(key in require.cache){ require.cache[key].children = []; require.cache[key].resolveFilenameCache = {}; diff --git a/bin/tsw/util/h5-test/add.js b/bin/tsw/util/h5-test/add.js index 45bece7f..ec8426c9 100644 --- a/bin/tsw/util/h5-test/add.js +++ b/bin/tsw/util/h5-test/add.js @@ -7,13 +7,13 @@ */ 'use strict'; -const logger = require('logger'); -const Deferred = require('util/Deferred'); -const isTest = require('./is-test.js'); -const post = require('util/auto-report/post.js'); -const OALogin = require('util/oa-login/index.js'); -const gzipHttp = require('util/gzipHttp.js'); -const canIuse = /^[0-9a-zA-Z_-]{0,64}$/; +const logger = require('logger'); +const Deferred = require('util/Deferred'); +const isTest = require('./is-test.js'); +const post = require('util/auto-report/post.js'); +const OALogin = require('util/oa-login/index.js'); +const gzipHttp = require('util/gzipHttp.js'); +const canIuse = /^[0-9a-zA-Z_-]{0,64}$/; module.exports = function(request, response) { OALogin.checkLoginForTSW(request, response, function() { @@ -22,9 +22,9 @@ module.exports = function(request, response) { }; module.exports.go = async function(request, response){ - - var uin = request.param('uin'); - var val = request.param('val'); + + var uin = request.param('uin'); + var val = request.param('val'); var data = await module.exports.addTestUser(uin,val).toES6Promise().catch(function(){ return null; @@ -48,14 +48,14 @@ var returnJson = function(json){ module.exports.addTestUser = function(uin, val){ logger.debug('addTestUser:' + uin); val = val || true; - var memcached = isTest.cmem(); - var keyText = isTest.keyBitmap(); + var memcached = isTest.cmem(); + var keyText = isTest.keyBitmap(); var defer = Deferred.create(); - var appid = ''; + var appid = ''; if(context.appid && context.appkey){ //开平过来的 - appid = context.appid; + appid = context.appid; keyText = `${keyText}.${appid}`; } diff --git a/bin/tsw/util/h5-test/del.js b/bin/tsw/util/h5-test/del.js index d8acabc4..dbf157e4 100644 --- a/bin/tsw/util/h5-test/del.js +++ b/bin/tsw/util/h5-test/del.js @@ -7,13 +7,13 @@ */ 'use strict'; -const logger = require('logger'); -const Deferred = require('util/Deferred'); -const isTest = require('./is-test.js'); -const post = require('util/auto-report/post.js'); -const OALogin = require('util/oa-login/index.js'); -const gzipHttp = require('util/gzipHttp.js'); -const canIuse = /^[0-9a-zA-Z_-]{0,64}$/; +const logger = require('logger'); +const Deferred = require('util/Deferred'); +const isTest = require('./is-test.js'); +const post = require('util/auto-report/post.js'); +const OALogin = require('util/oa-login/index.js'); +const gzipHttp = require('util/gzipHttp.js'); +const canIuse = /^[0-9a-zA-Z_-]{0,64}$/; module.exports = function(request, response) { @@ -23,9 +23,9 @@ module.exports = function(request, response) { }; module.exports.go = async function(request, response){ - - var uin = request.param('uin'); - + + var uin = request.param('uin'); + var data = await module.exports.deleteTestUser( uin ).toES6Promise().catch(function(){ @@ -50,10 +50,10 @@ var returnJson = function(json){ module.exports.deleteTestUser = function(uin){ logger.debug('deleteTestUser:' + uin); - var memcached = isTest.cmem(); - var keyText = isTest.keyBitmap(); + var memcached = isTest.cmem(); + var keyText = isTest.keyBitmap(); var defer = Deferred.create(); - var appid = ''; + var appid = ''; if(!uin){ return defer.reject(); @@ -65,7 +65,7 @@ module.exports.deleteTestUser = function(uin){ if(context.appid && context.appkey){ //开平过来的 - appid = context.appid; + appid = context.appid; keyText = `${keyText}.${appid}`; } diff --git a/bin/tsw/util/h5-test/get.js b/bin/tsw/util/h5-test/get.js index 4d8a387c..a083ad21 100644 --- a/bin/tsw/util/h5-test/get.js +++ b/bin/tsw/util/h5-test/get.js @@ -7,12 +7,12 @@ */ 'use strict'; -const logger = require('logger'); -const Deferred = require('util/Deferred'); -const isTest = require('./is-test.js'); -const post = require('util/auto-report/post.js'); -const gzipHttp = require('util/gzipHttp.js'); -const OALogin = require('util/oa-login/index.js'); +const logger = require('logger'); +const Deferred = require('util/Deferred'); +const isTest = require('./is-test.js'); +const post = require('util/auto-report/post.js'); +const gzipHttp = require('util/gzipHttp.js'); +const OALogin = require('util/oa-login/index.js'); module.exports = function(request, response) { OALogin.checkLoginForTSW(request, response, function() { @@ -21,7 +21,7 @@ module.exports = function(request, response) { }; module.exports.go = async function(request, response){ - + var data = await module.exports.getTestUser().toES6Promise().catch(function(){ return null; }); @@ -35,14 +35,14 @@ module.exports.getTestUser = function(){ logger.debug('getTestUser'); //从内存中读取testTargetMap - var memcached = isTest.cmem(); - var keyText = isTest.keyBitmap(); - var defer = Deferred.create(); - var appid = ''; + var memcached = isTest.cmem(); + var keyText = isTest.keyBitmap(); + var defer = Deferred.create(); + var appid = ''; if(context.appid && context.appkey){ //开平过来的 - appid = context.appid; + appid = context.appid; keyText = `${keyText}.${appid}`; } @@ -68,15 +68,15 @@ module.exports.getTestUser = function(){ defer.resolve({}); } }); - + return defer; }; module.exports.openapi = async function(req,res){ - var appid = context.appid; - var appkey = context.appkey; + var appid = context.appid; + var appkey = context.appkey; if(req.param('appid') !== appid){ returnJson({ code: -2 , message: 'appid错误'}); @@ -98,7 +98,7 @@ module.exports.openapi = async function(req,res){ return; } - logger.setKey(`h5testSync_${appid}`); //上报key + logger.setKey(`h5testSync_${appid}`); //上报key var data = await module.exports.getTestUser().toES6Promise().catch(function(){ return null; diff --git a/bin/tsw/util/h5-test/group/src/_config.js b/bin/tsw/util/h5-test/group/src/_config.js index f67bc1c6..37830df4 100644 --- a/bin/tsw/util/h5-test/group/src/_config.js +++ b/bin/tsw/util/h5-test/group/src/_config.js @@ -4,14 +4,14 @@ define(function(require, exports, module){ return { - + tmpl: { create: true }, all: { create: false } - + }; - + }); diff --git a/bin/tsw/util/h5-test/group/tmpl.js b/bin/tsw/util/h5-test/group/tmpl.js index d2a764bb..f4f2c2e6 100644 --- a/bin/tsw/util/h5-test/group/tmpl.js +++ b/bin/tsw/util/h5-test/group/tmpl.js @@ -33,10 +33,10 @@ define(function(require, exports, module){ var __p=[],_p=function(s){__p.push(s);},out=_p; __p.push(' '); - data = data || {}; + data = data || {}; - var body = data.body || '', - head = data.head || {}; + var body = data.body || '', + head = data.head || {}; __p.push(' \n \n \n \n \n \n '); _p(tmpl.encodeHtml(head.title || '')); __p.push('\n \n\n \n \n \n \n '); _p(tmpl.encodeHtml(head.title || '')); __p.push('\n \n \n window.getUrlParam = function(name, loc) {\n loc = loc || window.location;\n var r = new RegExp(\'(\\\\?|#|&)\' + name + \'=(.*?)(&|#|$)\');\n var m = (loc.href || \'\').match(r);\n return decodeURIComponent(m ? m[2] : \'\');\n };\n window.getCookie = function(key){\n var cookies = document.cookie ? document.cookie.split(\'; \') : [], tmp;\n for(var i=0;i\n window.getUrlParam = function(name, loc) {\n loc = loc || window.location;\n var r = new RegExp(\'(\\\\?|#|&)\' + name + \'=(.*?)(&|#|$)\');\n var m = (loc.href || \'\').match(r);\n return decodeURIComponent(m ? m[2] : \'\');\n };\n window.getCookie = function(key){\n var cookies = document.cookie ? document.cookie.split(\'; \') : [], tmp;\n for(var i=0;i\n \n (function() {\n var handlerNums = function(val){\n return val.split(/(\\b[0-9a-zA-Z_\\-]{1,64}\\b)/g);\n }\n $(\'#join-submit2\').click(function(event){\n var deferArr = [];\n var val = $(\'#join-textarea\').val();\n var arr = handlerNums(val);\n var selectVal = $(\'.selected\')[0].value;\n for(var i = 1; i < arr.length; i++) {\n (function(uin){\n var request = api.addTestUser({\n uin : uin,\n val : selectVal\n });\n deferArr.push(request);\n }(arr[i]));\n i++;\n }\n $.when.apply(this, deferArr).done(function(){\n alert("你的号码,现已加入H5测试环境");\n }).fail(function(errMsg){\n errMsg = errMsg || "出了点小问题,请联系系统管理员处理";\n alert(errMsg);\n }).always(function () {\n });\n return false;\n });\n $(\'#remove-submit2\').click(function(event){\n var deferArr = [];\n var val = $(\'#join-textarea\').val();\n var arr = handlerNums(val);\n\n for(var i = 1; i < arr.length; i++) {\n \n var request = api.delTestUser({\n uin : arr[i]\n });\n deferArr.push(request);\n \n i++;\n }\n $.when.apply(this, deferArr).done(function(){\n alert("H5测试环境删除成功");\n }).fail(function(errMsg){\n errMsg = errMsg || "出了点小问题,请联系系统管理员处理";\n alert(errMsg);\n }).always(function () {\n });\n return false;\n });\n $(\'#number-get\').click(function(event){\n var request = api.getTestUser();\n request.done(function(data) {\n var html = "";\n var num = 0;\n for (var i in data.data) {\n num++;\n var val = data.data[i];\n html += \'

\' + i + \':\' + val + \'

\';\n }\n if (num == 0) {\n html = "还没有测试号码~"\n }\n $(\'#number-list\').html(html);\n }).fail(function(errMsg) {\n errMsg = errMsg || "出了点小问题,请联系系统管理员处理";\n alert(errMsg);\n });\n return false;\n });\n\n $(document).on(\'click\', \'.js-select-btn\', function(event){\n var target = $(this);\n $(\'.js-select-btn\').removeClass(\'btn-primary positive\');\n $(\'.js-select-btn\').removeClass(\'selected positive\');\n target.addClass(\'btn-primary positive\');\n target.addClass(\'selected\');\n if (history.replaceState) {\n var server = target.attr(\'value\');\n var newUrl = location.protocol + \'//\' + location.host + location.pathname + \'?server=\' + encodeURIComponent(server);\n history.replaceState({server: server}, \'\', newUrl);\n }\n });\n var server = getUrlParam(\'server\');\n if (server) {\n $(\'.js-select-btn\').each(function(i, el) {\n var $el = $(el);\n if ($el.attr(\'value\') === server) {\n var $box = $el.closest(\'.column>div\');\n $box.scrollTop(el.offsetTop - 140);\n $el.trigger(\'click\');\n return false;\n }\n });\n }\n })();\n
\n (function() {\n var handlerNums = function(val){\n return val.split(/(\\b[0-9a-zA-Z_\\-]{1,64}\\b)/g);\n }\n $(\'#join-submit2\').click(function(event){\n var deferArr = [];\n var val = $(\'#join-textarea\').val();\n var arr = handlerNums(val);\n var selectVal = $(\'.selected\')[0].value;\n for(var i = 1; i < arr.length; i++) {\n (function(uin){\n var request = api.addTestUser({\n uin : uin,\n val : selectVal\n });\n deferArr.push(request);\n }(arr[i]));\n i++;\n }\n $.when.apply(this, deferArr).done(function(){\n alert("你的号码,现已加入H5测试环境");\n }).fail(function(errMsg){\n errMsg = errMsg || "出了点小问题,请联系系统管理员处理";\n alert(errMsg);\n }).always(function () {\n });\n return false;\n });\n $(\'#remove-submit2\').click(function(event){\n var deferArr = [];\n var val = $(\'#join-textarea\').val();\n var arr = handlerNums(val);\n\n for(var i = 1; i < arr.length; i++) {\n \n var request = api.delTestUser({\n uin : arr[i]\n });\n deferArr.push(request);\n \n i++;\n }\n $.when.apply(this, deferArr).done(function(){\n alert("H5测试环境删除成功");\n }).fail(function(errMsg){\n errMsg = errMsg || "出了点小问题,请联系系统管理员处理";\n alert(errMsg);\n }).always(function () {\n });\n return false;\n });\n $(\'#number-get\').click(function(event){\n var request = api.getTestUser();\n request.done(function(data) {\n var html = "";\n var num = 0;\n for (var i in data.data) {\n num++;\n var val = data.data[i];\n html += \'

\' + i + \':\' + val + \'

\';\n }\n if (num == 0) {\n html = "还没有测试号码~"\n }\n $(\'#number-list\').html(html);\n }).fail(function(errMsg) {\n errMsg = errMsg || "出了点小问题,请联系系统管理员处理";\n alert(errMsg);\n });\n return false;\n });\n\n $(document).on(\'click\', \'.js-select-btn\', function(event){\n var target = $(this);\n $(\'.js-select-btn\').removeClass(\'btn-primary positive\');\n $(\'.js-select-btn\').removeClass(\'selected positive\');\n target.addClass(\'btn-primary positive\');\n target.addClass(\'selected\');\n if (history.replaceState) {\n var server = target.attr(\'value\');\n var newUrl = location.protocol + \'//\' + location.host + location.pathname + \'?server=\' + encodeURIComponent(server);\n history.replaceState({server: server}, \'\', newUrl);\n }\n });\n var server = getUrlParam(\'server\');\n if (server) {\n $(\'.js-select-btn\').each(function(i, el) {\n var $el = $(el);\n if ($el.attr(\'value\') === server) {\n var $box = $el.closest(\'.column>div\');\n $box.scrollTop(el.offsetTop - 140);\n $el.trigger(\'click\');\n return false;\n }\n });\n }\n })();\n
'); return __p.join(''); diff --git a/bin/tsw/util/h5-test/page/view.js b/bin/tsw/util/h5-test/page/view.js index 898bbd74..112d47f6 100644 --- a/bin/tsw/util/h5-test/page/view.js +++ b/bin/tsw/util/h5-test/page/view.js @@ -7,13 +7,13 @@ */ 'use strict'; -const gzipHttp = require('util/gzipHttp.js'); -const pagetmpl = require('./tmpl'); -const TEReport = require('util/auto-report/TEReport.js'); -const OALogin = require('util/oa-login/index.js'); +const gzipHttp = require('util/gzipHttp.js'); +const pagetmpl = require('./tmpl'); +const TEReport = require('util/auto-report/TEReport.js'); +const OALogin = require('util/oa-login/index.js'); module.exports = function(request, response) { - + OALogin.checkLoginForTSW(request, response, function() { module.exports.go(request, response); }); diff --git a/bin/tsw/util/home/src/_config.js b/bin/tsw/util/home/src/_config.js index f67bc1c6..37830df4 100644 --- a/bin/tsw/util/home/src/_config.js +++ b/bin/tsw/util/home/src/_config.js @@ -4,14 +4,14 @@ define(function(require, exports, module){ return { - + tmpl: { create: true }, all: { create: false } - + }; - + }); diff --git a/bin/tsw/util/home/tmpl.js b/bin/tsw/util/home/tmpl.js index 63bbae56..9af12452 100644 --- a/bin/tsw/util/home/tmpl.js +++ b/bin/tsw/util/home/tmpl.js @@ -18,10 +18,10 @@ define(function(require, exports, module){ var __p=[],_p=function(s){__p.push(s);},out=_p; __p.push(' '); - data = data || {}; + data = data || {}; - var body = data.body || '', - head = data.head || {}; + var body = data.body || '', + head = data.head || {}; __p.push(' \n \n \n \n \n \n '); _p(tmpl.encodeHtml(head.title || '')); __p.push('\n ])/g, +var encodeMap = { + reg : /([&"'<>])/g, '&' : '&', - '"' : '"', - '\'' : ''', - '<' : '<', - '>' : '>' + '"' : '"', + '\'' : ''', + '<' : '<', + '>' : '>' }, decodeMap = { reg : /(<)|(")|(')|(')|(')|(&)|(>)/g, @@ -64,7 +64,7 @@ var encodeMap = { ''' : '\'', '&' : '&' }, - encode = function($0,c){return encodeMap[c];}, + encode = function($0,c){return encodeMap[c];}, decode = function(c){return decodeMap[c];}; //encode diff --git a/bin/wwwroot/static/tsw/highlightjs/highlight-tsw.js b/bin/wwwroot/static/tsw/highlightjs/highlight-tsw.js index 2b1f4536..89a4617f 100644 --- a/bin/wwwroot/static/tsw/highlightjs/highlight-tsw.js +++ b/bin/wwwroot/static/tsw/highlightjs/highlight-tsw.js @@ -784,9 +784,9 @@ https://highlightjs.org/ }, // Other numbers //{ - // className: 'number', - // begin: '\\s\\d{1,20}$', - // relevance: 0 + // className: 'number', + // begin: '\\s\\d{1,20}$', + // relevance: 0 //}, //{ // className: 'number', @@ -844,9 +844,9 @@ https://highlightjs.org/ }, //Dates //{ - // className: 'string', - // begin: /\[/, end: /\]/, - // illegal: '\\n' + // className: 'string', + // begin: /\[/, end: /\]/, + // illegal: '\\n' //}, // Strings { diff --git a/examples/framework/router.js b/examples/framework/router.js index 8b6ded78..9e3b2f41 100644 --- a/examples/framework/router.js +++ b/examples/framework/router.js @@ -2,16 +2,16 @@ //定义一个路由表 const map = { - hapi: './hapi.js', //http://127.0.0.1/hapi - express: './express.js', //http://127.0.0.1/express - koa: './koa.js', //http://127.0.0.1/koa + hapi: './hapi.js', //http://127.0.0.1/hapi + express: './express.js', //http://127.0.0.1/express + koa: './koa.js', //http://127.0.0.1/koa default: './helloworld.js' //http://127.0.0.1/other }; //路由:起个名字 this.name = function(req){ - const pathname = req.REQUEST.pathname; - const arr = pathname.split('/',2); + const pathname = req.REQUEST.pathname; + const arr = pathname.split('/',2); return arr[1]; diff --git a/examples/framework/wsRouter.js b/examples/framework/wsRouter.js index d39e7bfb..015584ae 100644 --- a/examples/framework/wsRouter.js +++ b/examples/framework/wsRouter.js @@ -8,7 +8,7 @@ const map = { //路由:起个名字 this.name = function(ws){ // const req = ws.upgradeReq; - + return 'websocket'; }; diff --git a/examples/skyMode/config.js b/examples/skyMode/config.js index 18e25313..db7bdaa6 100644 --- a/examples/skyMode/config.js +++ b/examples/skyMode/config.js @@ -11,4 +11,4 @@ this.logger = { logLevel: 'debug' }; -this.memcached = {host:'127.0.0.1:11211', timeout: 500, poolSize: 20, retries: 1, maxQueueSize: 1000}; +this.memcached = {host:'127.0.0.1:11211', timeout: 500, poolSize: 20, retries: 1, maxQueueSize: 1000}; diff --git a/test/bin/tsw/runtime/context.js b/test/bin/tsw/runtime/context.js index c4d621cf..a3b237b0 100644 --- a/test/bin/tsw/runtime/context.js +++ b/test/bin/tsw/runtime/context.js @@ -10,7 +10,7 @@ describe('context', () => { describe('#await', () => { it('#await in domain', async ()=> { let before = process.domain; - await new Promise(resolve =>{resolve(); }); + await new Promise(resolve =>{resolve(); }); let after = process.domain; expect(true).to.equal(before === after); diff --git a/test/bin/tsw/util/CD.test.js b/test/bin/tsw/util/CD.test.js index 2f433ec7..cc1391a9 100644 --- a/test/bin/tsw/util/CD.test.js +++ b/test/bin/tsw/util/CD.test.js @@ -6,12 +6,12 @@ const plug = require('plug'); /** 这一部分是CD.js依赖的模块,我们需要弄一些stub,以避免干扰 */ -let CD = plug('util/CD.js'); -let logger = plug('logger'); -let config = plug('config'); -let Deferred = plug('util/Deferred'); +let CD = plug('util/CD.js'); +let logger = plug('logger'); +let config = plug('config'); +let Deferred = plug('util/Deferred'); let ajax = plug('ajax'); -let gzipHttp = plug('util/gzipHttp.js'); +let gzipHttp = plug('util/gzipHttp.js'); logger.setLogLevel('error'); diff --git a/test/bin/tsw/util/cpu.test.js b/test/bin/tsw/util/cpu.test.js index ce6d5814..7bbcbd62 100644 --- a/test/bin/tsw/util/cpu.test.js +++ b/test/bin/tsw/util/cpu.test.js @@ -3,7 +3,7 @@ const expect = chai.expect; const sinon = require('sinon'); const plug = require('plug'); const cpuUtil = plug('util/cpu.js'); -const logger = plug('logger'); +const logger = plug('logger'); logger.setLogLevel('error'); From c72205b04caa109aa5ce5a5a34774f65945234a5 Mon Sep 17 00:00:00 2001 From: RobinzZH Date: Thu, 24 May 2018 10:31:49 +0800 Subject: [PATCH 035/163] v1.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5198d820..69c08623 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "TSW", - "version": "1.0.1449", + "version": "1.1.0", "scripts": { "lint": "eslint examples bin/wwwroot bin/proxy bin/tsw bin/lib test --fix", "precommit": "lint-staged", From b7c001f62cdcff8e14bdd8966b807f105bbb5efa Mon Sep 17 00:00:00 2001 From: youkunhuang Date: Thu, 24 May 2018 11:09:21 +0800 Subject: [PATCH 036/163] =?UTF-8?q?=E5=AE=9E=E6=97=B6=E7=9B=91=E6=8E=A7?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81windows?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/lib/api/tnm2/index.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/bin/lib/api/tnm2/index.js b/bin/lib/api/tnm2/index.js index aabecae7..b575be41 100644 --- a/bin/lib/api/tnm2/index.js +++ b/bin/lib/api/tnm2/index.js @@ -9,7 +9,6 @@ const serverInfo = require('serverInfo.js'); const mapping = require('./mapping.json'); -const {isWindows} = require('util/isWindows.js'); const url = require('url'); const Deferred = require('util/Deferred'); const cluster = require('cluster'); @@ -90,7 +89,7 @@ var cacheOrRepoet = function(attr, iValue){ var reportOpenapi = function(last){ var defer = Deferred.create(); - + var openapi = require('util/openapi'); var logger = require('logger'); var config = require('config'); @@ -105,10 +104,6 @@ var reportOpenapi = function(last){ return; } - if(isWindows){ - return; - } - if(config.isTest){ return; } From 7be958d9dea0e302eefbf1ab665ad58bd03682fa Mon Sep 17 00:00:00 2001 From: youkunhuang Date: Thu, 24 May 2018 11:24:14 +0800 Subject: [PATCH 037/163] =?UTF-8?q?=E5=BC=80=E5=8F=91=E8=80=85=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E4=B8=8D=E4=B8=8A=E6=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/lib/api/tnm2/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bin/lib/api/tnm2/index.js b/bin/lib/api/tnm2/index.js index b575be41..9463d797 100644 --- a/bin/lib/api/tnm2/index.js +++ b/bin/lib/api/tnm2/index.js @@ -108,6 +108,10 @@ var reportOpenapi = function(last){ return; } + if(config.devMode){ + return; + } + if(!config.appid || !config.appkey){ return; } From 55978abf151ad1bad85f7835b9019287865e648a Mon Sep 17 00:00:00 2001 From: youkunhuang Date: Thu, 24 May 2018 11:26:44 +0800 Subject: [PATCH 038/163] =?UTF-8?q?=E4=BF=9D=E6=8C=81=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=80=BC=E4=B8=BAdefer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/lib/api/tnm2/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bin/lib/api/tnm2/index.js b/bin/lib/api/tnm2/index.js index 9463d797..0ebf97f3 100644 --- a/bin/lib/api/tnm2/index.js +++ b/bin/lib/api/tnm2/index.js @@ -101,23 +101,23 @@ var reportOpenapi = function(last){ //阻止默认上报 if(retCall === false){ - return; + return defer.resolve(); } if(config.isTest){ - return; + return defer.resolve(); } if(config.devMode){ - return; + return defer.resolve(); } if(!config.appid || !config.appkey){ - return; + return defer.resolve(); } if(!config.appReportUrl){ - return; + return defer.resolve(); } var arr = []; From ad8ebc2ba5bd7a383d0dd60a8f61947f6c45c9d1 Mon Sep 17 00:00:00 2001 From: youkunhuang Date: Thu, 24 May 2018 11:53:35 +0800 Subject: [PATCH 039/163] =?UTF-8?q?=E8=B0=83=E6=95=B4windows=E4=B8=8B?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E9=80=BB=E8=BE=91=EF=BC=8C=E5=8E=BB=E6=8E=89?= =?UTF-8?q?=E6=8E=A2=E6=B5=8B=E8=AF=B7=E6=B1=82=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/proxy/http.proxy.js | 71 +++++++---------------------------------- bin/proxy/master.js | 2 +- 2 files changed, 12 insertions(+), 61 deletions(-) diff --git a/bin/proxy/http.proxy.js b/bin/proxy/http.proxy.js index 0f31d5c4..a3943f06 100644 --- a/bin/proxy/http.proxy.js +++ b/bin/proxy/http.proxy.js @@ -22,7 +22,6 @@ const cp = require('child_process'); const parseGet = require('util/http/parseGet.js'); const tnm2 = require('api/tnm2'); const cpuUtil = require('util/cpu.js'); -const httpUtil = require('util/http.js'); const TEReport = require('util/auto-report/TEReport.js'); const mail = require('util/mail/mail.js'); const websocket = require('./websocket.js'); @@ -35,14 +34,14 @@ const serverInfo = { intranetIp: require('serverInfo.js').intranetIp, cpu: 'X' }; -var server; -var serverThis; -var serverHttps; -var config = require('./config.js'); -var routeCache = null; -var cleanCacheTid = null; -var isStartHeartBeat = false; -var heartBeatCount = 0; +let server; +let serverThis; +let serverHttps; +let config = require('./config.js'); +let routeCache = null; +let cleanCacheTid = null; +let isStartHeartBeat = false; +let heartBeatCount = 0; function doRoute(req,res){ @@ -52,57 +51,12 @@ function doRoute(req,res){ config = require('./config.js'); } - if(req.headers['user-agent'] === 'nws' && req.headers.host === serverInfo.intranetIp){ - //nws探测请求 - res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'}); - res.end('hello nws'); - - return; - } - - if(req.headers['user-agent'] === 'TgwProbe'){ - //stgw探测请求 - res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'}); - res.end('hello TgwProbe'); - - return; - } - - if(req.headers['user-agent'] === 'StgwProbe'){ - //stgw探测请求 - res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'}); - res.end('hello StgwProbe'); - - return; - } - - if(req.headers['user-agent'] === 'TgwProbe'){ - //stgw探测请求 - res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'}); - res.end('hello TgwProbe'); - - return; - } - - if(req.REQUEST.pathname === '/' && !req.headers['user-agent']){ - - if(httpUtil.isInnerIP(httpUtil.getUserIp(req))){ - //l7探测请求 - res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'}); - res.end('hello l7'); - return; - } - - } - routeCache(req,res); } process.serverInfo = serverInfo; -/** - * 清除缓存 - */ +//清除缓存 function cleanCache(){ clearTimeout(cleanCacheTid); @@ -127,7 +81,7 @@ process.on('heapdump',function(e){ require('heapdump').writeSnapshot(__dirname + '/cpu' + serverInfo.cpu + '.' + Date.now() + '.heapsnapshot',function(err, filename) { logger.info('dump written to ${filename}', { - filename: filename + filenamelet: filename }); }); @@ -540,10 +494,7 @@ methodMap.listen = function(message){ }; if(cluster.isMaster){ - if(isWindows){ - logger.info('isWindows, start listening'); - methodMap.listen({cpu : 0}); - }else if(debugOptions && debugOptions.inspectorEnabled){ + if(debugOptions && debugOptions.inspectorEnabled){ logger.setLogLevel('debug'); logger.info('inspectorEnabled, start listening'); methodMap.listen({cpu : 0}); diff --git a/bin/proxy/master.js b/bin/proxy/master.js index ed86946e..f764081b 100644 --- a/bin/proxy/master.js +++ b/bin/proxy/master.js @@ -315,7 +315,7 @@ function startServer(){ }else{ - //子进程直接引入proxy文件,当然也可以直接在这里写逻辑运行,注意此处else作用域属于子进程作用域,非本程序作用域 + //子进程直接引入proxy文件,注意此处else作用域属于子进程作用域,非本程序作用域 process.title = 'TSW/worker/node'; logger.info('start worker....'); require('./http.proxy.js'); From 078bfd6a9d834dd98c29ea7238164bcaabdf7c15 Mon Sep 17 00:00:00 2001 From: neilcui Date: Thu, 24 May 2018 12:03:23 +0800 Subject: [PATCH 040/163] test commitlint --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index 4b8adb5a..96af0a1f 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "scripts": { "lint": "eslint examples bin test --fix", "precommit": "lint-staged", + "commitmsg": "commitlint -e $GIT_PARAMS", "test": "mocha --recursive test/bin", "coverage": "nyc --reporter=lcov --reporter=text npm test && codecov" }, @@ -19,6 +20,8 @@ "memcached": "~0.2.8" }, "devDependencies": { + "@commitlint/cli": "^6.2.0", + "@commitlint/config-conventional": "^6.1.3", "chai": "^4.1.2", "codecov": "^3.0.2", "eslint": "^4.10.0", From 4a3f62ebb58c3093690d94d15ab07e344a1e5c42 Mon Sep 17 00:00:00 2001 From: neilcui Date: Thu, 24 May 2018 12:17:21 +0800 Subject: [PATCH 041/163] chore: add commitline to validate commit message --- .commitlintrc.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .commitlintrc.yml diff --git a/.commitlintrc.yml b/.commitlintrc.yml new file mode 100644 index 00000000..9cb74a70 --- /dev/null +++ b/.commitlintrc.yml @@ -0,0 +1,2 @@ +extends: + - "@commitlint/config-conventional" From 4aeb7959bd884e546179313281cfd20c26e80239 Mon Sep 17 00:00:00 2001 From: youkunhuang Date: Thu, 24 May 2018 12:19:12 +0800 Subject: [PATCH 042/163] fix bug --- bin/proxy/http.proxy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/proxy/http.proxy.js b/bin/proxy/http.proxy.js index a3943f06..d4c3e8d7 100644 --- a/bin/proxy/http.proxy.js +++ b/bin/proxy/http.proxy.js @@ -81,7 +81,7 @@ process.on('heapdump',function(e){ require('heapdump').writeSnapshot(__dirname + '/cpu' + serverInfo.cpu + '.' + Date.now() + '.heapsnapshot',function(err, filename) { logger.info('dump written to ${filename}', { - filenamelet: filename + filename: filename }); }); From 251184a391a779a423ae0a017f66a121fcbfd509 Mon Sep 17 00:00:00 2001 From: neilcui Date: Thu, 24 May 2018 12:23:56 +0800 Subject: [PATCH 043/163] chore: add commit lint to travis ci --- .travis.yml | 1 + package.json | 1 + 2 files changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 81230975..fdbc59bd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,5 +5,6 @@ node_js: - "9" script: + - commitlint-travis - npm run lint - npm test && npm run coverage diff --git a/package.json b/package.json index 96af0a1f..0632d145 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "devDependencies": { "@commitlint/cli": "^6.2.0", "@commitlint/config-conventional": "^6.1.3", + "@commitlint/travis-cli": "^6.2.0", "chai": "^4.1.2", "codecov": "^3.0.2", "eslint": "^4.10.0", From d582821397d8ee05837d7500984ee1a7ed4a6563 Mon Sep 17 00:00:00 2001 From: neilcui Date: Thu, 24 May 2018 14:52:45 +0800 Subject: [PATCH 044/163] chore(all): include more eslint rules --- .eslintrc.json | 32 +- bin/lib/api/cmdb/index.js | 6 +- bin/lib/api/code/watcher.js | 2 +- bin/lib/api/keyman/alphaMap.js | 56 ++-- bin/lib/api/keyman/blackIpMap.js | 58 ++-- bin/lib/api/keyman/index.js | 16 +- bin/lib/api/keyman/ipCheck.js | 2 +- bin/lib/api/keyman/runtimeAdd.js | 4 +- bin/lib/api/libdcapi/dcapi.js | 2 +- bin/lib/api/notify/index.js | 16 +- bin/lib/api/tnm2/index.js | 38 +-- bin/lib/api/xssFilter/index.js | 2 +- bin/lib/data/cmem.tsw.js | 16 +- bin/lib/default/config.default.h5test.js | 4 +- bin/lib/default/config.default.js | 4 +- bin/lib/default/logReport.js | 6 +- bin/lib/runtime/md5.check.js | 4 +- bin/lib/util/gzipHttp.js | 36 +- bin/lib/util/http.more.js | 8 +- bin/lib/util/isTST.js | 4 +- bin/lib/util/mail/mail.js | 44 +-- bin/lib/util/mail/src/_config.js | 2 +- bin/lib/util/mail/tmpl.js | 33 +- bin/lib/util/oa-login/index.js | 4 +- bin/lib/webapp/Server.js | 4 +- bin/lib/webapp/utils/format.js | 4 +- bin/proxy/admin.js | 46 +-- bin/proxy/check.js | 12 +- bin/proxy/config.js | 44 +-- bin/proxy/http.mod.act.js | 10 +- bin/proxy/http.mod.map.js | 14 +- bin/proxy/http.proxy.js | 190 +++++------ bin/proxy/http.route.js | 342 +++++++++++-------- bin/proxy/master.js | 190 +++++------ bin/proxy/websocket.js | 28 +- bin/tsw/ajax/ajax.js | 392 +++++++++++----------- bin/tsw/ajax/form.js | 8 +- bin/tsw/ajax/http-https.options.js | 12 +- bin/tsw/ajax/token.js | 8 +- bin/tsw/api/date.js | 2 +- bin/tsw/api/fileCache/index.js | 92 ++--- bin/tsw/api/logman/index.js | 24 +- bin/tsw/config.js | 6 +- bin/tsw/context.js | 12 +- bin/tsw/default/config.default.sky.js | 14 +- bin/tsw/loader/seajs/lib/sea-node.js | 28 +- bin/tsw/plug.js | 24 +- bin/tsw/pool/cmem.l5.js | 56 ++-- bin/tsw/router.js | 16 +- bin/tsw/runtime/CCFinder.js | 76 ++--- bin/tsw/runtime/Console.hack.js | 4 +- bin/tsw/runtime/Context.js | 6 +- bin/tsw/runtime/Dns.hack.js | 8 +- bin/tsw/runtime/Window.js | 6 +- bin/tsw/runtime/capturer.js | 54 +-- bin/tsw/runtime/fs.hack.js | 46 +-- bin/tsw/serverInfo.js | 26 +- bin/tsw/util/CD.js | 116 +++---- bin/tsw/util/Deferred/index.js | 20 +- bin/tsw/util/Queue/index.js | 20 +- bin/tsw/util/auto-report/TEReport.js | 48 +-- bin/tsw/util/auto-report/alpha.js | 28 +- bin/tsw/util/auto-report/download.js | 158 ++++----- bin/tsw/util/auto-report/logReport.js | 230 ++++++------- bin/tsw/util/auto-report/post.js | 132 ++++---- bin/tsw/util/auto-report/post.openapi.js | 4 +- bin/tsw/util/auto-report/src/_config.js | 2 +- bin/tsw/util/auto-report/tmpl.js | 118 ++++--- bin/tsw/util/auto-report/view.js | 52 +-- bin/tsw/util/cache.cleaner.js | 12 +- bin/tsw/util/cpu.js | 88 ++--- bin/tsw/util/h5-test/add.js | 40 +-- bin/tsw/util/h5-test/del.js | 38 +-- bin/tsw/util/h5-test/get.js | 48 +-- bin/tsw/util/h5-test/group/src/_config.js | 2 +- bin/tsw/util/h5-test/group/tmpl.js | 31 +- bin/tsw/util/h5-test/group/view.js | 6 +- bin/tsw/util/h5-test/is-test.js | 102 +++--- bin/tsw/util/h5-test/page/src/_config.js | 2 +- bin/tsw/util/h5-test/page/tmpl.js | 71 ++-- bin/tsw/util/h5-test/page/view.js | 2 +- bin/tsw/util/home/src/_config.js | 2 +- bin/tsw/util/home/tmpl.js | 34 +- bin/tsw/util/home/view.js | 4 +- bin/tsw/util/html302/index.js | 6 +- bin/tsw/util/html302/src/_config.js | 2 +- bin/tsw/util/html302/tmpl.js | 8 +- bin/tsw/util/http.isInnerIP.js | 30 +- bin/tsw/util/http.js | 154 ++++----- bin/tsw/util/http/parseBody.js | 28 +- bin/tsw/util/http/parseGet.js | 66 ++-- bin/tsw/util/logger/callInfo.js | 10 +- bin/tsw/util/logger/logger.config.js | 12 +- bin/tsw/util/logger/logger.js | 216 ++++++------ bin/tsw/util/nodeVersion.js | 4 +- bin/tsw/util/process.args.js | 4 +- bin/tsw/util/static/static.js | 30 +- bin/tsw/util/v8-profiler.js | 4 +- bin/tsw/util/xss.js | 34 +- bin/wwwroot/.eslintrc.json | 8 - examples/framework/express.js | 2 +- examples/framework/hapi.js | 4 +- examples/framework/helloworld.js | 2 +- examples/framework/router.js | 6 +- examples/framework/websocket.js | 2 +- examples/framework/wsRouter.js | 4 +- test/bin/tsw/runtime/context.js | 4 +- test/bin/tsw/util/CD.test.js | 38 ++- 108 files changed, 2229 insertions(+), 2062 deletions(-) delete mode 100644 bin/wwwroot/.eslintrc.json diff --git a/.eslintrc.json b/.eslintrc.json index eb8c0f3b..53c7f37c 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -54,6 +54,36 @@ { "args": "none" } + ], + "no-mixed-requires": [ + "error", + true + ], + "block-spacing": [ + "error", + "always" + ], + "brace-style": [ + "error", + "1tbs" + ], + "comma-spacing": [ + "error" + ], + "space-before-blocks": [ + "error", + "always" ] - } + }, + "overrides": [ + { + "files": ["bin/tsw/wwwroot/*.js"], + "env": { + "browser": true + }, + "globals": { + "define": true + } + } + ] } diff --git a/bin/lib/api/cmdb/index.js b/bin/lib/api/cmdb/index.js index 6f8e1154..ddca83dc 100644 --- a/bin/lib/api/cmdb/index.js +++ b/bin/lib/api/cmdb/index.js @@ -10,7 +10,7 @@ const Deferred = require('util/Deferred'); -this.GetDeviceThisServer = function(){ +this.GetDeviceThisServer = function() { var defer = Deferred.create(); return defer.resolve(null); @@ -19,13 +19,13 @@ this.GetDeviceThisServer = function(){ /** * 查询父业务树信息 */ -this.GetAllParentBusiness = function(opt){ +this.GetAllParentBusiness = function(opt) { var defer = Deferred.create(); return defer.resolve(null); }; -this.GetDeviceByIp = function(opt){ +this.GetDeviceByIp = function(opt) { var defer = Deferred.create(); return defer.resolve(null); diff --git a/bin/lib/api/code/watcher.js b/bin/lib/api/code/watcher.js index c86ea750..85c43379 100644 --- a/bin/lib/api/code/watcher.js +++ b/bin/lib/api/code/watcher.js @@ -7,7 +7,7 @@ */ 'use strict'; -this.watch = function(){ +this.watch = function() { }; diff --git a/bin/lib/api/keyman/alphaMap.js b/bin/lib/api/keyman/alphaMap.js index 845ce000..060430f5 100644 --- a/bin/lib/api/keyman/alphaMap.js +++ b/bin/lib/api/keyman/alphaMap.js @@ -21,7 +21,7 @@ var cache = { dataFile: {} }; -if(global[__filename]){ +if(global[__filename]) { cache = global[__filename]; }else{ global[__filename] = cache; @@ -29,24 +29,24 @@ if(global[__filename]){ } -if(isFirstLoad){ - if(config.alphaFile){ +if(isFirstLoad) { + if(config.alphaFile) { - (function(){ + (function() { //导入alphaFile var text = ''; try{ - text = fs.readFileSync(config.alphaFile,'UTF-8'); - }catch(e){ + text = fs.readFileSync(config.alphaFile, 'UTF-8'); + }catch(e) { logger.warn(e.stack); } - if(!text){ + if(!text) { return; } - if(text.length >= 2 * 1024 * 1024){ + if(text.length >= 2 * 1024 * 1024) { logger.error('alpha file limit <=2M'); return; } @@ -61,25 +61,25 @@ if(isFirstLoad){ } -function getMap(text){ +function getMap(text) { var map = {}; text = text || ''; - text.replace(/^([0-9a-zA-Z_-]+).*$/gm,function($0,key){ + text.replace(/^([0-9a-zA-Z_-]+).*$/gm, function($0, key) { map[key] = 1; }); return map; } -function updateMap(text){ +function updateMap(text) { var map = getMap(text); //copy - Object.assign(map,cache.dataFile); + Object.assign(map, cache.dataFile); cache.data = map; @@ -87,42 +87,42 @@ function updateMap(text){ } -this.getSync = function(){ +this.getSync = function() { this.get(); return cache.data; }; -this.get = function(){ +this.get = function() { var defer = Deferred.create(); var delay = (process.serverInfo && process.serverInfo.cpu * 1000) || 0; var l5api = config.tswL5api['alphaFileUrl']; - if(Date.now() - cache.timeUpdate < 60000 + delay){ + if(Date.now() - cache.timeUpdate < 60000 + delay) { return defer.resolve(cache.data); } cache.timeUpdate = Date.now(); - if(!fileUrl){ + if(!fileUrl) { return defer.resolve(cache.data); } - fileCache.getAsync(fileUrl).done(function(d){ + fileCache.getAsync(fileUrl).done(function(d) { var lastModifyTime = 0; var text = ''; - if(d && d.stats){ + if(d && d.stats) { lastModifyTime = d.stats.mtime.getTime(); } - if(d && d.data){ + if(d && d.data) { text = d.data.toString('utf-8'); } - if(Date.now() - lastModifyTime < 60000){ + if(Date.now() - lastModifyTime < 60000) { logger.debug('使用本地文件'); updateMap(text); @@ -143,18 +143,18 @@ this.get = function(){ key: 'EVENT_TSW_ALPHA_FILE_URL' }, dataType: 'text' - }).fail(function(d){ + }).fail(function(d) { defer.resolve(cache.data); - }).done(function(d){ + }).done(function(d) { var text = ''; - if(d && d.result && typeof d.result === 'string'){ + if(d && d.result && typeof d.result === 'string') { text = d.result; } - if(text.length >= 2 * 1024 * 1024){ + if(text.length >= 2 * 1024 * 1024) { logger.error('alpha file limit <=2M'); return defer.resolve(cache.data); } @@ -162,7 +162,7 @@ this.get = function(){ updateMap(text); //保存在本地 - fileCache.set(fileUrl,Buffer.from(text,'UTF-8')); + fileCache.set(fileUrl, Buffer.from(text, 'UTF-8')); defer.resolve(cache.data); }); @@ -173,16 +173,16 @@ this.get = function(){ }; -function init(){ +function init() { var buffer = null; var text = ''; - if(fileUrl){ + if(fileUrl) { buffer = fileCache.getSync(fileUrl).data; } - if(buffer){ + if(buffer) { text = buffer.toString('utf-8'); updateMap(text); } diff --git a/bin/lib/api/keyman/blackIpMap.js b/bin/lib/api/keyman/blackIpMap.js index 40095074..6760a67c 100644 --- a/bin/lib/api/keyman/blackIpMap.js +++ b/bin/lib/api/keyman/blackIpMap.js @@ -21,31 +21,31 @@ var cache = { dataFile: {} }; -if(global[__filename]){ +if(global[__filename]) { cache = global[__filename]; }else{ global[__filename] = cache; isFirstLoad = true; } -if(isFirstLoad){ - if(config.blackIpFile){ +if(isFirstLoad) { + if(config.blackIpFile) { - (function(){ + (function() { //导入blackIpFile var text = ''; try{ - text = fs.readFileSync(config.blackIpFile,'UTF-8'); - }catch(e){ + text = fs.readFileSync(config.blackIpFile, 'UTF-8'); + }catch(e) { logger.warn(e.stack); } - if(!text){ + if(!text) { return; } - if(text.length >= 2 * 1024 * 1024){ + if(text.length >= 2 * 1024 * 1024) { logger.error('blackIp file limit <=2M'); return; } @@ -61,87 +61,87 @@ if(isFirstLoad){ init(); -function init(){ +function init() { var buffer = null; var text = ''; - if(fileUrl){ + if(fileUrl) { buffer = fileCache.getSync(fileUrl).data; } - if(buffer){ + if(buffer) { text = buffer.toString('utf-8'); updateMap(text); } } -this.getFileMapSync = function(){ +this.getFileMapSync = function() { return cache.dataFile; }; -this.getSync = function(){ +this.getSync = function() { this.get(); return cache.data; }; -function getMap(text){ +function getMap(text) { var map = {}; text = text || ''; - text.replace(/^([0-9][0-9.*]+).*$/gm,function($0,key){ + text.replace(/^([0-9][0-9.*]+).*$/gm, function($0, key) { map[key] = 1; }); return map; } -function updateMap(text){ +function updateMap(text) { var map = getMap(text); //copy - Object.assign(map,cache.dataFile); + Object.assign(map, cache.dataFile); cache.data = map; logger.debug('update ok'); } -this.get = function(){ +this.get = function() { var defer = Deferred.create(); var delay = ((process.serverInfo && process.serverInfo.cpu) * 1000) || 0; var l5api = config.tswL5api['blackIpFileUrl']; - if(Date.now() - cache.timeUpdate < 300000 + delay){ + if(Date.now() - cache.timeUpdate < 300000 + delay) { return defer.resolve(cache.data); } cache.timeUpdate = Date.now(); - if(!fileUrl){ + if(!fileUrl) { return defer.resolve(cache.data); } - fileCache.getAsync(fileUrl).done(function(d){ + fileCache.getAsync(fileUrl).done(function(d) { var lastModifyTime = 0; var text = ''; - if(d && d.stats){ + if(d && d.stats) { lastModifyTime = d.stats.mtime.getTime(); } - if(d && d.data){ + if(d && d.data) { text = d.data.toString('utf-8'); } - if(Date.now() - lastModifyTime < 300000){ + if(Date.now() - lastModifyTime < 300000) { logger.debug('使用本地文件'); updateMap(text); @@ -162,18 +162,18 @@ this.get = function(){ key: 'EVENT_TSW_BLACKIP_FILE_URL' }, dataType: 'text' - }).fail(function(d){ + }).fail(function(d) { defer.resolve(cache.data); - }).done(function(d){ + }).done(function(d) { var text = ''; - if(d && d.result && typeof d.result === 'string'){ + if(d && d.result && typeof d.result === 'string') { text = d.result; } - if(text.length >= 2 * 1024 * 1024){ + if(text.length >= 2 * 1024 * 1024) { logger.error('blackIp file limit <=2M'); return defer.resolve(cache.data); } @@ -181,7 +181,7 @@ this.get = function(){ updateMap(text); //保存在本地 - fileCache.set(fileUrl,Buffer.from(text,'UTF-8')); + fileCache.set(fileUrl, Buffer.from(text, 'UTF-8')); defer.resolve(cache.data); }); diff --git a/bin/lib/api/keyman/index.js b/bin/lib/api/keyman/index.js index 27d40dd9..59871dd1 100644 --- a/bin/lib/api/keyman/index.js +++ b/bin/lib/api/keyman/index.js @@ -7,34 +7,34 @@ */ 'use strict'; -this.getBlockIpMapSync = function(){ +this.getBlockIpMapSync = function() { return require('./blackIpMap.js').getSync(); }; -this.getBlockIpMap = function(){ +this.getBlockIpMap = function() { return require('./blackIpMap.js').get(); }; -this.getAlphaUinMapSync = function(){ +this.getAlphaUinMapSync = function() { return require('./alphaMap.js').getSync(); }; -this.getAlphaUinMap = function(){ +this.getAlphaUinMap = function() { return require('./alphaMap.js').get(); }; -this.getTestIp = function(group){ +this.getTestIp = function(group) { return require('util/auto-report/TEReport.js').list(group); }; -this.getAllGroup = function(){ +this.getAllGroup = function() { return require('util/auto-report/TEReport.js').getAllGroup(); }; -this.ipCheck = function(opt){ +this.ipCheck = function(opt) { return require('./ipCheck.js').info(opt); }; -this.runtimeAdd = function(opt){ +this.runtimeAdd = function(opt) { return require('./runtimeAdd.js').add(opt); }; diff --git a/bin/lib/api/keyman/ipCheck.js b/bin/lib/api/keyman/ipCheck.js index 80224d01..9f11e08a 100644 --- a/bin/lib/api/keyman/ipCheck.js +++ b/bin/lib/api/keyman/ipCheck.js @@ -9,7 +9,7 @@ const Deferred = require('util/Deferred'); -this.info = function(opt){ +this.info = function(opt) { var defer = Deferred.create(); return defer.reject(); diff --git a/bin/lib/api/keyman/runtimeAdd.js b/bin/lib/api/keyman/runtimeAdd.js index b995f898..3dff2af9 100644 --- a/bin/lib/api/keyman/runtimeAdd.js +++ b/bin/lib/api/keyman/runtimeAdd.js @@ -9,7 +9,7 @@ const Deferred = require('util/Deferred'); -this.add = function(opt){ +this.add = function(opt) { var defer = Deferred.create(); @@ -21,7 +21,7 @@ this.add = function(opt){ * 考核心跳上报,表示当前模块参与考核 * */ -this.hello = function(){ +this.hello = function() { return; }; diff --git a/bin/lib/api/libdcapi/dcapi.js b/bin/lib/api/libdcapi/dcapi.js index 3fd5337d..271332b1 100644 --- a/bin/lib/api/libdcapi/dcapi.js +++ b/bin/lib/api/libdcapi/dcapi.js @@ -14,7 +14,7 @@ this.baselog = function(table, param) { }); }; -this.reportLog = function(opt){ +this.reportLog = function(opt) { }; diff --git a/bin/lib/api/notify/index.js b/bin/lib/api/notify/index.js index f9fc0370..70bfba7b 100644 --- a/bin/lib/api/notify/index.js +++ b/bin/lib/api/notify/index.js @@ -9,21 +9,21 @@ const Deferred = require('util/Deferred'); -this.DecryptTicketWithClientIP = function(opt){ +this.DecryptTicketWithClientIP = function(opt) { var defer = Deferred.create(); return defer.resolve(); }; -this.SendRTX = function(opt){ +this.SendRTX = function(opt) { var defer = Deferred.create(); return defer.resolve(); }; -this.SendMail = function(opt){ +this.SendMail = function(opt) { opt = Deferred.extend({ 'appKey' : '', @@ -43,7 +43,7 @@ this.SendMail = function(opt){ 'StartTime' : '', //当邮件为约会邮件时,约会开始时间 'EndTime' : '', //当邮件为约会邮件时,约会结束时间 'attachment' : '' //邮件附件的文件名以及文件的内容(在发送请求时,文件内容是二进制数据流的形式发送) - },opt); + }, opt); var defer = Deferred.create(); @@ -51,7 +51,7 @@ this.SendMail = function(opt){ }; -this.SendWeiXin = function(opt){ +this.SendWeiXin = function(opt) { opt = Deferred.extend({ 'appKey' : '', @@ -60,7 +60,7 @@ this.SendWeiXin = function(opt){ 'Priority' : 0, //优先级,-1低优先级,0普通,1高优先级 'Receiver' : '', //接收人,英文名,多人用英文分号分隔 'Sender' : '' //发送人 - },opt); + }, opt); var defer = Deferred.create(); @@ -68,7 +68,7 @@ this.SendWeiXin = function(opt){ }; -this.SendSMS = function(opt){ +this.SendSMS = function(opt) { opt = Deferred.extend({ 'appKey' : '', @@ -77,7 +77,7 @@ this.SendSMS = function(opt){ 'Priority' : 0, //优先级,-1低优先级,0普通,1高优先级 'Receiver' : '', //接收人,英文名,多人用英文分号分隔 'Sender' : '' //发送人 - },opt); + }, opt); var defer = Deferred.create(); diff --git a/bin/lib/api/tnm2/index.js b/bin/lib/api/tnm2/index.js index b575be41..295f583c 100644 --- a/bin/lib/api/tnm2/index.js +++ b/bin/lib/api/tnm2/index.js @@ -19,15 +19,15 @@ var cache = { }; var isFirstLoad = false; -if(global[__filename]){ +if(global[__filename]) { cache = global[__filename]; }else{ global[__filename] = cache; isFirstLoad = true; } -if(isFirstLoad){ - cluster.worker && cluster.worker.once('disconnect', function(worker){ +if(isFirstLoad) { + cluster.worker && cluster.worker.once('disconnect', function(worker) { var logger = require('logger'); var last = cache.curr; @@ -54,14 +54,14 @@ this.Attr_API = function (attr, iValue) { cacheOrRepoet(attr, iValue); }; -var cacheOrRepoet = function(attr, iValue){ +var cacheOrRepoet = function(attr, iValue) { var curr; - if(!mapping[attr]){ + if(!mapping[attr]) { return; } - if(!cache.curr[attr]){ + if(!cache.curr[attr]) { cache.curr[attr] = { count: 0, sum: 0 @@ -74,7 +74,7 @@ var cacheOrRepoet = function(attr, iValue){ var now = Date.now(); - if(now - cache.time < 60000){ + if(now - cache.time < 60000) { return; } @@ -87,7 +87,7 @@ var cacheOrRepoet = function(attr, iValue){ }; -var reportOpenapi = function(last){ +var reportOpenapi = function(last) { var defer = Deferred.create(); var openapi = require('util/openapi'); @@ -95,31 +95,31 @@ var reportOpenapi = function(last){ var config = require('config'); var retCall; - if(typeof config.beforeReportApp === 'function'){ + if(typeof config.beforeReportApp === 'function') { retCall = config.beforeReportApp(last); } //阻止默认上报 - if(retCall === false){ + if(retCall === false) { return; } - if(config.isTest){ + if(config.isTest) { return; } - if(!config.appid || !config.appkey){ + if(!config.appid || !config.appkey) { return; } - if(!config.appReportUrl){ + if(!config.appReportUrl) { return; } var arr = []; - Object.keys(last).forEach(function(v,i){ - arr.push([v,last[v].sum,last[v].count].join('.')); + Object.keys(last).forEach(function(v, i) { + arr.push([v, last[v].sum, last[v].count].join('.')); }); var postData = { @@ -149,12 +149,12 @@ var reportOpenapi = function(last){ keepAlive : true, autoToken : false, dataType : 'json' - }).fail(function(){ + }).fail(function() { logger.error('app report fail.'); defer.reject(); - }).done(function(d){ - if(d.result){ - if(d.result.code === 0){ + }).done(function(d) { + if(d.result) { + if(d.result.code === 0) { logger.debug('app report success.'); return defer.resolve(); }else{ diff --git a/bin/lib/api/xssFilter/index.js b/bin/lib/api/xssFilter/index.js index cf1b8de1..d9a6ef41 100644 --- a/bin/lib/api/xssFilter/index.js +++ b/bin/lib/api/xssFilter/index.js @@ -9,7 +9,7 @@ const Deferred = require('util/Deferred'); -this.check = function(){ +this.check = function() { var defer = Deferred.create(); return defer.resolve(); diff --git a/bin/lib/data/cmem.tsw.js b/bin/lib/data/cmem.tsw.js index f0680aa0..a075746e 100644 --- a/bin/lib/data/cmem.tsw.js +++ b/bin/lib/data/cmem.tsw.js @@ -10,35 +10,35 @@ const config = require('config'); const cmem = require('pool/cmem.l5.js'); -this.cmem = function(){ +this.cmem = function() { - if(config.idc === 'sh'){ + if(config.idc === 'sh') { return this.sh(); } - if(config.idc === 'tj'){ + if(config.idc === 'tj') { return this.tj(); } return this.sz(); }; -this.openapi = function(){ +this.openapi = function() { return cmem(config.memcached); }; -this.h5test = function(){ +this.h5test = function() { return cmem(config.memcached); }; -this.sz = function(){ +this.sz = function() { return cmem(config.memcached); }; -this.sh = function(){ +this.sh = function() { return cmem(config.memcached); }; -this.tj = function(){ +this.tj = function() { return cmem(config.memcached); }; diff --git a/bin/lib/default/config.default.h5test.js b/bin/lib/default/config.default.h5test.js index 47fe74e8..fc5af055 100644 --- a/bin/lib/default/config.default.h5test.js +++ b/bin/lib/default/config.default.h5test.js @@ -9,9 +9,9 @@ this.isTest = true; -Object.assign(this,require('./config.default.js')); +Object.assign(this, require('./config.default.js')); -if(process.mainModule === module){ +if(process.mainModule === module) { /* eslint-disable no-console */ console.log(this); /* eslint-enable no-console */ diff --git a/bin/lib/default/config.default.js b/bin/lib/default/config.default.js index 50d90be3..911675ce 100644 --- a/bin/lib/default/config.default.js +++ b/bin/lib/default/config.default.js @@ -71,14 +71,14 @@ this.workerUid = 'nobody'; //mod_act映射 this.modAct = { - getModAct: function(req){ + getModAct: function(req) { return null; } }; //路由 this.modMap = { - find: function(mod_act,req,res){ + find: function(mod_act, req, res) { return null; } }; diff --git a/bin/lib/default/logReport.js b/bin/lib/default/logReport.js index 4302ffb3..f18f0a88 100644 --- a/bin/lib/default/logReport.js +++ b/bin/lib/default/logReport.js @@ -9,7 +9,7 @@ const config = require('config'); -module.exports.report = function(data){ +module.exports.report = function(data) { var retValue; var reportData = { table:'reportData', @@ -26,11 +26,11 @@ module.exports.report = function(data){ } }; - if(config.beforeReportLog && typeof config.beforeReportLog === 'function'){ + if(config.beforeReportLog && typeof config.beforeReportLog === 'function') { retValue = config.beforeReportLog(reportData); } - if(retValue === false){ + if(retValue === false) { return; } }; diff --git a/bin/lib/runtime/md5.check.js b/bin/lib/runtime/md5.check.js index 0b63be70..eb7d0436 100644 --- a/bin/lib/runtime/md5.check.js +++ b/bin/lib/runtime/md5.check.js @@ -8,7 +8,7 @@ 'use strict'; -this.check = function(root){}; +this.check = function(root) {}; -this.findNodeCpp = function(curr){}; +this.findNodeCpp = function(curr) {}; diff --git a/bin/lib/util/gzipHttp.js b/bin/lib/util/gzipHttp.js index 3da56175..f33df2b8 100644 --- a/bin/lib/util/gzipHttp.js +++ b/bin/lib/util/gzipHttp.js @@ -15,7 +15,7 @@ const defaultChunkSize = 8 * 1024; this.httpUtil = httpUtil; -this.create = this.getGzipResponse = function(opt){ +this.create = this.getGzipResponse = function(opt) { var window = context.window || {}; opt = opt || {}; @@ -28,7 +28,7 @@ this.create = this.getGzipResponse = function(opt){ contentType = opt.contentType || (headers && headers['content-type']) || 'text/html; charset=UTF-8', gzipOutputStream; - if(headers && headers['content-length'] !== undefined){ + if(headers && headers['content-length'] !== undefined) { response.useChunkedEncodingByDefault = false; delete headers['transfer-encoding']; delete headers['content-length']; @@ -37,27 +37,27 @@ this.create = this.getGzipResponse = function(opt){ response.setHeader('Content-Type', contentType); - if(/\bgzip\b/.test(request.headers['accept-encoding'])){ + if(/\bgzip\b/.test(request.headers['accept-encoding'])) { response.setHeader('Content-Encoding', 'gzip'); - response.writeHead(code,headers); + response.writeHead(code, headers); response.socket && response.socket.setNoDelay(true); gzipOutputStream = zlib.createGzip({ chunkSize: chunkSize }); - gzipOutputStream.on('data',function(buffer){ - logger.debug('gzip chunked send ${len}',{ + gzipOutputStream.on('data', function(buffer) { + logger.debug('gzip chunked send ${len}', { len: buffer.length }); - if(!response.finished){ + if(!response.finished) { response.write(buffer); } }); - gzipOutputStream.once('end',function(){ + gzipOutputStream.once('end', function() { response.end(); }); @@ -65,20 +65,24 @@ this.create = this.getGzipResponse = function(opt){ }else{ - response.writeHead(code,headers); + response.writeHead(code, headers); - response.on('data',function(buffer){ - logger.debug('chunked send ${len}',{ + response.on('data', function(buffer) { + logger.debug('chunked send ${len}', { len: buffer.length }); }); - if(!response.flush){ - response.flush = function(){return true;}; + if(!response.flush) { + response.flush = function() { + return true; + }; } - if(response.flush && response.flushHeaders){ - response.flush = function(){return true;}; + if(response.flush && response.flushHeaders) { + response.flush = function() { + return true; + }; } return response; @@ -90,7 +94,7 @@ this.create = this.getGzipResponse = function(opt){ /** 从buffer获取SHA1的方法,独立出来其实是为了方便测试 */ -exports.getSHA1 = function(buffer){ +exports.getSHA1 = function(buffer) { return crypto.createHash('sha1').update(buffer).digest('hex'); }; diff --git a/bin/lib/util/http.more.js b/bin/lib/util/http.more.js index 80a44cd2..2745e8e1 100644 --- a/bin/lib/util/http.more.js +++ b/bin/lib/util/http.more.js @@ -7,17 +7,17 @@ */ 'use strict'; -this.isFromWns = function(request){ +this.isFromWns = function(request) { return false; }; -this.getIpCromQuia = function(qvia){ +this.getIpCromQuia = function(qvia) { return null; }; -this.getBase = function(request){ +this.getBase = function(request) { return ''; }; -this.fixPicUrl = function(url){ +this.fixPicUrl = function(url) { return ''; }; diff --git a/bin/lib/util/isTST.js b/bin/lib/util/isTST.js index 37936c98..642e688f 100644 --- a/bin/lib/util/isTST.js +++ b/bin/lib/util/isTST.js @@ -10,9 +10,9 @@ const config = require('config'); //是否安全扫描器请求 -this.isTST = function(request){ +this.isTST = function(request) { - if(config.extendMod && typeof config.extendMod.isTST === 'function'){ + if(config.extendMod && typeof config.extendMod.isTST === 'function') { return config.extendMod.isTST(request); } diff --git a/bin/lib/util/mail/mail.js b/bin/lib/util/mail/mail.js index a0175643..56ac2af8 100644 --- a/bin/lib/util/mail/mail.js +++ b/bin/lib/util/mail/mail.js @@ -15,31 +15,31 @@ const logger = require('logger'); const Deferred = require('util/Deferred'); const url = require('url'); -this.SendMail = function(key,group,second,oriOpt){ +this.SendMail = function(key, group, second, oriOpt) { - var opt = Deferred.extend({},oriOpt); + var opt = Deferred.extend({}, oriOpt); var data = {}; var now = new Date(); var prefix = '[runtime]'; - if(isWindows.isWindows){ + if(isWindows.isWindows) { return; } //晚上不发 //allDaySend强制全天候24小时发送 - if(!opt.allDaySend && now.getHours() < 8){ + if(!opt.allDaySend && now.getHours() < 8) { return; } - if(context.title){ + if(context.title) { opt.Title = `[${context.title}]${opt.Title}`; } - if(config.isTest){ + if(config.isTest) { prefix += '[测试环境]'; }else{ - if(opt.runtimeType){ + if(opt.runtimeType) { prefix += `[${opt.runtimeType}][考核]`; } } @@ -61,19 +61,19 @@ this.SendMail = function(key,group,second,oriOpt){ opt.data = data; - if(isWindows.isWindows){ + if(isWindows.isWindows) { key = key + Date.now(); } - process.nextTick(function(){ - require('util/CD.js').check(key,1,second).done(function(){ + process.nextTick(function() { + require('util/CD.js').check(key, 1, second).done(function() { reportOpenapi(data); }); }); }; -var reportOpenapi = function(data){ +var reportOpenapi = function(data) { var defer = Deferred.create(); var config = require('config'); var openapi = require('util/openapi'); @@ -81,20 +81,20 @@ var reportOpenapi = function(data){ var retCall; - if(typeof config.beforeRuntimeReport === 'function'){ + if(typeof config.beforeRuntimeReport === 'function') { retCall = config.beforeRuntimeReport(data); } //阻止默认上报 - if(retCall === false){ + if(retCall === false) { return defer.resolve(0); } - if(!config.appid || !config.appkey){ + if(!config.appid || !config.appkey) { return; } - if(!config.runtimeReportUrl){ + if(!config.runtimeReportUrl) { return; } @@ -123,12 +123,12 @@ var reportOpenapi = function(data){ keepAlive : true, autoToken : false, dataType : 'json' - }).fail(function(){ + }).fail(function() { logger.error('runtime report fail.'); defer.reject(); - }).done(function(d){ - if(d.result){ - if(d.result.code === 0){ + }).done(function(d) { + if(d.result) { + if(d.result.code === 0) { logger.debug('runtime report success.'); return defer.resolve(); }else{ @@ -146,18 +146,18 @@ var reportOpenapi = function(data){ //升级周知 -this.SendTSWMail = function(opt){ +this.SendTSWMail = function(opt) { }; //发送周知邮件 -this.SendArsMail = function(opt){ +this.SendArsMail = function(opt) { }; -this.findMailARS = function(file){ +this.findMailARS = function(file) { return ''; }; diff --git a/bin/lib/util/mail/src/_config.js b/bin/lib/util/mail/src/_config.js index 0a1aae4c..9cb4d1d3 100644 --- a/bin/lib/util/mail/src/_config.js +++ b/bin/lib/util/mail/src/_config.js @@ -7,7 +7,7 @@ */ 'use strict'; -define(function(require, exports, module){ +define(function(require, exports, module) { return { diff --git a/bin/lib/util/mail/tmpl.js b/bin/lib/util/mail/tmpl.js index fdf0d409..114f0fc8 100644 --- a/bin/lib/util/mail/tmpl.js +++ b/bin/lib/util/mail/tmpl.js @@ -1,13 +1,19 @@ //tmpl file list: //mail/src/mail.tmpl.html -define(function(require, exports, module){ +define(function(require, exports, module) { var tmpl = { - 'encodeHtml': function(s){return (s+'').replace(/[\x26\x3c\x3e\x27\x22\x60]/g,function($0){return '&#'+$0.charCodeAt(0)+';';});}, + 'encodeHtml': function(s) { + return (s+'').replace(/[\x26\x3c\x3e\x27\x22\x60]/g, function($0) { + return '&#'+$0.charCodeAt(0)+';'; + }); + }, - 'email': function(data){ + 'email': function(data) { - var __p=[],_p=function(s){__p.push(s);},out=_p; + var __p=[], _p=function(s) { + __p.push(s); + }, out=_p; var window = context.window || {}; @@ -22,7 +28,8 @@ define(function(require, exports, module){ __p.push('

\n

mod_act: '); _p(context.mod_act || 'null'); __p.push('

'); - if(window.request){__p.push('

域名: '); + if(window.request) { + __p.push('

域名: '); _p(window.request && window.request.headers.host); __p.push('

\n

path: '); _p(window.request && window.request.REQUEST.pathname); @@ -32,21 +39,25 @@ define(function(require, exports, module){ __p.push('

\n

发送间隔: '); _p(data.second); __p.push('s

'); - if(data.headerText){__p.push('

请求头:

\n
'); - _p(tmpl.encodeHtml(data.headerText || '').replace(/\r\n|\r|\n/g,'
')); + if(data.headerText) { + __p.push('

请求头:

\n
'); + _p(tmpl.encodeHtml(data.headerText || '').replace(/\r\n|\r|\n/g, '
')); __p.push('
'); }__p.push(' '); - if(data.logText){__p.push('

全息日志:

\n
');
-                _p(tmpl.encodeHtml(data.logText || '').replace(/\r\n|\r|\n/g,'
')); + if(data.logText) { + __p.push('

全息日志:

\n
');
+                _p(tmpl.encodeHtml(data.logText || '').replace(/\r\n|\r|\n/g, '
')); __p.push('
'); }__p.push('\n'); return __p.join(''); }, - 'rtx': function(data){ + 'rtx': function(data) { - var __p=[],_p=function(s){__p.push(s);},out=_p; + var __p=[], _p=function(s) { + __p.push(s); + }, out=_p; var window = context.window || {}; diff --git a/bin/lib/util/oa-login/index.js b/bin/lib/util/oa-login/index.js index 9c4a0f2b..089cee24 100644 --- a/bin/lib/util/oa-login/index.js +++ b/bin/lib/util/oa-login/index.js @@ -7,10 +7,10 @@ */ 'use strict'; -module.exports = function(request, response, callback, tofCfg){ +module.exports = function(request, response, callback, tofCfg) { callback(); }; -module.exports.checkLoginForTSW = function(request, response, callback){ +module.exports.checkLoginForTSW = function(request, response, callback) { callback(); }; diff --git a/bin/lib/webapp/Server.js b/bin/lib/webapp/Server.js index 7fd3c605..c6064970 100644 --- a/bin/lib/webapp/Server.js +++ b/bin/lib/webapp/Server.js @@ -7,10 +7,10 @@ */ 'use strict'; -this.startServer = function(){ +this.startServer = function() { }; -this.stopServer = function(){ +this.stopServer = function() { }; diff --git a/bin/lib/webapp/utils/format.js b/bin/lib/webapp/utils/format.js index e794bf2a..e6f72e8d 100644 --- a/bin/lib/webapp/utils/format.js +++ b/bin/lib/webapp/utils/format.js @@ -8,14 +8,14 @@ 'use strict'; function formatBuffer(buffer) { - if(!(buffer && buffer.length)){ + if(!(buffer && buffer.length)) { return ''; } var str = ''; for (var i = 0, len = buffer.length; i < len; i++) { - if(i % 16 === 0){ + if(i % 16 === 0) { str += '0x' + ('00000000000' + i.toString(16)).slice(-8) + ': '; } diff --git a/bin/proxy/admin.js b/bin/proxy/admin.js index 0bfec417..b5b9d811 100644 --- a/bin/proxy/admin.js +++ b/bin/proxy/admin.js @@ -15,11 +15,11 @@ const codeWatch = require('api/code/watcher.js'); const parseGet = require('util/http/parseGet.js'); const cp = require('child_process'); -const server = http.createServer(function(req, res){ +const server = http.createServer(function(req, res) { var action; - logger.info('admin request by: ${url}',{ + logger.info('admin request by: ${url}', { url: req.url }); @@ -28,20 +28,20 @@ const server = http.createServer(function(req, res){ action = methodMap[req.REQUEST.pathname] || methodMap['default']; - action.apply(methodMap,arguments); + action.apply(methodMap, arguments); }); logger.info('start admin'); -server.listen(config.httpAdminPort,'127.0.0.1',function(err){ - if(err){ - logger.info('admin listen error ${address}:${port}',{ +server.listen(config.httpAdminPort, '127.0.0.1', function(err) { + if(err) { + logger.info('admin listen error ${address}:${port}', { address: '127.0.0.1', port: config.httpAdminPort }); }else{ - logger.info('admin listen ok ${address}:${port}',{ + logger.info('admin listen ok ${address}:${port}', { address: '127.0.0.1', port: config.httpAdminPort }); @@ -51,13 +51,13 @@ server.listen(config.httpAdminPort,'127.0.0.1',function(err){ const methodMap = { - 'default' : function(req, res){ + 'default' : function(req, res) { res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'}); res.end('no such command!'); }, - '/globaldump' : function(req, res){ - process.emit('sendCmd2workerOnce',{ + '/globaldump' : function(req, res) { + process.emit('sendCmd2workerOnce', { CMD: 'globaldump', GET: req.GET }); @@ -65,8 +65,8 @@ const methodMap = { res.end('done!\r\n'); }, - '/heapdump' : function(req, res){ - process.emit('sendCmd2workerOnce',{ + '/heapdump' : function(req, res) { + process.emit('sendCmd2workerOnce', { CMD: 'heapdump', GET: req.GET }); @@ -74,8 +74,8 @@ const methodMap = { res.end('done!\r\n'); }, - '/profiler' : function(req, res){ - process.emit('sendCmd2workerOnce',{ + '/profiler' : function(req, res) { + process.emit('sendCmd2workerOnce', { CMD: 'profiler', GET: req.GET }); @@ -83,8 +83,8 @@ const methodMap = { res.end('done!\r\n'); }, - '/top100' : function(req, res){ - process.emit('sendCmd2workerOnce',{ + '/top100' : function(req, res) { + process.emit('sendCmd2workerOnce', { CMD: 'top100', GET: req.GET }); @@ -92,13 +92,13 @@ const methodMap = { res.end('done!\r\n'); }, - '/reload' : function(req, res){ + '/reload' : function(req, res) { - cp.exec('./check.js',{ + cp.exec('./check.js', { timeout: 5000, cwd: __dirname - },function(err, stdout, stderr){ - if(err){ + }, function(err, stdout, stderr) { + if(err) { logger.error(err.stack); res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'}); res.write(err.stack); @@ -107,7 +107,7 @@ const methodMap = { return; } - if(stderr && stderr.length > 0){ + if(stderr && stderr.length > 0) { logger.error(stderr.toString('UTF-8')); res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'}); @@ -117,10 +117,10 @@ const methodMap = { return; } - if(stdout && stdout.length > 0){ + if(stdout && stdout.length > 0) { logger.info(stdout.toString('UTF-8')); - process.emit('reload',req.GET); + process.emit('reload', req.GET); res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'}); res.write(stderr.toString('UTF-8')); diff --git a/bin/proxy/check.js b/bin/proxy/check.js index 3135d204..c8e48470 100644 --- a/bin/proxy/check.js +++ b/bin/proxy/check.js @@ -19,17 +19,17 @@ try{ process.stdout.write('check config ok'); process.stdout.write('\r\n'); - setTimeout(function(){ + setTimeout(function() { process.exit(0); - },500); -}catch(err){ + }, 500); +}catch(err) { - if(err){ + if(err) { process.stderr.write(err.stack); process.stderr.write('\r\n'); - setTimeout(function(){ + setTimeout(function() { process.exit(0); - },500); + }, 500); } } diff --git a/bin/proxy/config.js b/bin/proxy/config.js index 49b60246..dc433a7f 100644 --- a/bin/proxy/config.js +++ b/bin/proxy/config.js @@ -19,24 +19,24 @@ var cache = { }; -if(global[__filename]){ +if(global[__filename]) { cache = global[__filename]; }else{ global[__filename] = cache; isFirstLoad = true; } -if(isFirstLoad){ - process.dlopen = function(fn){ - var parent = path.join(__dirname , '..'); +if(isFirstLoad) { + process.dlopen = function(fn) { + var parent = path.join(__dirname, '..'); - return function(module,curr){ + return function(module, curr) { //检查node私有文件 - if(/\.node$/i.test(curr) && curr.indexOf(parent) !== 0){ + if(/\.node$/i.test(curr) && curr.indexOf(parent) !== 0) { //发现私有node扩展 - setTimeout(function(){ + setTimeout(function() { require('runtime/md5.check.js').findNodeCpp(curr); - },3000); + }, 3000); } return fn.apply(this, arguments); }; @@ -44,42 +44,42 @@ if(isFirstLoad){ } -if(fs.existsSync('/etc/tsw.config.js')){ +if(fs.existsSync('/etc/tsw.config.js')) { cache.config = require('/usr/local/node_modules/config.js'); -}else if(fs.existsSync('/usr/local/node_modules/config.js')){ +}else if(fs.existsSync('/usr/local/node_modules/config.js')) { cache.config = require('/usr/local/node_modules/config.js'); -}else if(fs.existsSync('/data/release/node_modules/config.js')){ +}else if(fs.existsSync('/data/release/node_modules/config.js')) { cache.config = require('/data/release/node_modules/config.js'); -}else if(fs.existsSync(__dirname + '/../../conf/config.js')){ +}else if(fs.existsSync(__dirname + '/../../conf/config.js')) { cache.config = require('../../conf/config.js'); } -Deferred.extend(true,exports,defaultValue,cache.config); +Deferred.extend(true, exports, defaultValue, cache.config); -if(exports.router){ +if(exports.router) { exports.modAct = { - getModAct : function(req){ + getModAct : function(req) { return exports.router.name(req); } }; exports.modMap = { - find : function(name,req,res){ - return exports.router.find(name,req,res); + find : function(name, req, res) { + return exports.router.find(name, req, res); } }; } -if(exports.wsRouter){ +if(exports.wsRouter) { exports.wsModAct = { - getModAct : function(ws){ + getModAct : function(ws) { return exports.wsRouter.name(ws); } }; exports.wsModMap = { - find : function(name,ws){ - return exports.wsRouter.find(name,ws); + find : function(name, ws) { + return exports.wsRouter.find(name, ws); } }; } @@ -87,7 +87,7 @@ if(exports.wsRouter){ module.exports = exports; -if(process.mainModule === module){ +if(process.mainModule === module) { /* eslint-disable no-console */ console.log(exports); /* eslint-enable no-console */ diff --git a/bin/proxy/http.mod.act.js b/bin/proxy/http.mod.act.js index f5717a03..7c2459b3 100644 --- a/bin/proxy/http.mod.act.js +++ b/bin/proxy/http.mod.act.js @@ -10,18 +10,18 @@ const config = require('./config.js'); var base = null; -if(config.skyMode){ +if(config.skyMode) { base = require('default/config.default.sky.js'); -}else if(config.isTest){ +}else if(config.isTest) { base = require('default/config.default.h5test.js'); } -if(base){ - module.exports.getModAct = function(req){ +if(base) { + module.exports.getModAct = function(req) { var mod = base.modAct.getModAct(req); - if(mod){ + if(mod) { return mod; } diff --git a/bin/proxy/http.mod.map.js b/bin/proxy/http.mod.map.js index ebc987fc..98128c1d 100644 --- a/bin/proxy/http.mod.map.js +++ b/bin/proxy/http.mod.map.js @@ -10,20 +10,20 @@ const config = require('./config.js'); var base = null; -if(config.skyMode){ +if(config.skyMode) { base = require('default/config.default.sky.js'); -}else if(config.isTest){ +}else if(config.isTest) { base = require('default/config.default.h5test.js'); } -if(base){ - module.exports.find = function(mod_act,req,res){ - var mod = base.modMap.find(mod_act,req,res); +if(base) { + module.exports.find = function(mod_act, req, res) { + var mod = base.modMap.find(mod_act, req, res); - if(mod){ + if(mod) { return mod; } - return config.modMap.find(mod_act,req,res); + return config.modMap.find(mod_act, req, res); }; }else{ diff --git a/bin/proxy/http.proxy.js b/bin/proxy/http.proxy.js index 0f31d5c4..e49ed5e4 100644 --- a/bin/proxy/http.proxy.js +++ b/bin/proxy/http.proxy.js @@ -7,7 +7,7 @@ */ 'use strict'; -process.on('uncaughtException',function(e){ +process.on('uncaughtException', function(e) { logger.error(e && e.stack); }); @@ -45,14 +45,14 @@ var isStartHeartBeat = false; var heartBeatCount = 0; -function doRoute(req,res){ +function doRoute(req, res) { - if(routeCache === null){ + if(routeCache === null) { routeCache = require('./http.route.js'); config = require('./config.js'); } - if(req.headers['user-agent'] === 'nws' && req.headers.host === serverInfo.intranetIp){ + if(req.headers['user-agent'] === 'nws' && req.headers.host === serverInfo.intranetIp) { //nws探测请求 res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'}); res.end('hello nws'); @@ -60,7 +60,7 @@ function doRoute(req,res){ return; } - if(req.headers['user-agent'] === 'TgwProbe'){ + if(req.headers['user-agent'] === 'TgwProbe') { //stgw探测请求 res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'}); res.end('hello TgwProbe'); @@ -68,7 +68,7 @@ function doRoute(req,res){ return; } - if(req.headers['user-agent'] === 'StgwProbe'){ + if(req.headers['user-agent'] === 'StgwProbe') { //stgw探测请求 res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'}); res.end('hello StgwProbe'); @@ -76,7 +76,7 @@ function doRoute(req,res){ return; } - if(req.headers['user-agent'] === 'TgwProbe'){ + if(req.headers['user-agent'] === 'TgwProbe') { //stgw探测请求 res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'}); res.end('hello TgwProbe'); @@ -84,9 +84,9 @@ function doRoute(req,res){ return; } - if(req.REQUEST.pathname === '/' && !req.headers['user-agent']){ + if(req.REQUEST.pathname === '/' && !req.headers['user-agent']) { - if(httpUtil.isInnerIP(httpUtil.getUserIp(req))){ + if(httpUtil.isInnerIP(httpUtil.getUserIp(req))) { //l7探测请求 res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'}); res.end('hello l7'); @@ -95,7 +95,7 @@ function doRoute(req,res){ } - routeCache(req,res); + routeCache(req, res); } process.serverInfo = serverInfo; @@ -103,29 +103,29 @@ process.serverInfo = serverInfo; /** * 清除缓存 */ -function cleanCache(){ +function cleanCache() { clearTimeout(cleanCacheTid); - cleanCacheTid = setTimeout(function(){ + cleanCacheTid = setTimeout(function() { require('util/cache.cleaner.js').clear('/'); routeCache = null; - },5000); + }, 5000); } -process.on('top100',function(e){ +process.on('top100', function(e) { logger.info('top100'); global.top100 = []; }); -process.on('heapdump',function(e){ +process.on('heapdump', function(e) { logger.info('heapdump'); - if(!isWindows){ + if(!isWindows) { - require('heapdump').writeSnapshot(__dirname + '/cpu' + serverInfo.cpu + '.' + Date.now() + '.heapsnapshot',function(err, filename) { + require('heapdump').writeSnapshot(__dirname + '/cpu' + serverInfo.cpu + '.' + Date.now() + '.heapsnapshot', function(err, filename) { logger.info('dump written to ${filename}', { filename: filename }); @@ -136,10 +136,10 @@ process.on('heapdump',function(e){ }); -process.on('profiler',function(data = {}){ +process.on('profiler', function(data = {}) { logger.info('profiler time: ${time}', data); - if(!isWindows){ + if(!isWindows) { require('util/v8-profiler.js').writeProfilerOpt(__dirname + '/cpu' + serverInfo.cpu + '.' + Date.now() + '.cpuprofile', { recordTime: data.time || 5000 @@ -154,12 +154,12 @@ process.on('profiler',function(data = {}){ }); //process.emit('globaldump',m.GET); -process.on('globaldump',function(GET){ +process.on('globaldump', function(GET) { var cpu = GET.cpu || 0; var depth = GET.depth || 6; - if(cpu != serverInfo.cpu){ + if(cpu != serverInfo.cpu) { return; } @@ -170,17 +170,17 @@ process.on('globaldump',function(GET){ logger.info(filename); - var str = util.inspect(global,{ + var str = util.inspect(global, { depth: depth }); - fs.writeFile(filename,str,'UTF-8',function(){ + fs.writeFile(filename, str, 'UTF-8', function() { logger.info('globaldump finish'); }); }); -function requestHandler(req, res){ +function requestHandler(req, res) { if(server.keepAliveTimeout > 0) { res.setHeader('Connection', 'keep-alive'); res.setHeader('Keep-Alive', `timeout=${parseInt(server.keepAliveTimeout / 1000)}`); @@ -192,7 +192,7 @@ function requestHandler(req, res){ res.setHeader('Server', headerServer); res.setHeader('Cache-Control', 'no-cache'); - if(config.devMode){ + if(config.devMode) { //发者模式清除缓存 cleanCache(); } @@ -202,59 +202,61 @@ function requestHandler(req, res){ return; } - res.flush = res.flush || function(){return true;}; + res.flush = res.flush || function() { + return true; + }; //解析get参数 parseGet(req); //HTTP路由 - doRoute(req,res); + doRoute(req, res); } server = http.createServer(requestHandler); serverThis = http.createServer(requestHandler); -server.timeout = Math.max(config.timeout.upload || config.timeout.socket,0); -serverThis.timeout = Math.max(config.timeout.upload || config.timeout.socket,0); -server.keepAliveTimeout = Math.max(config.timeout.keepAlive,0); -serverThis.keepAliveTimeout = Math.max(config.timeout.keepAlive,0); +server.timeout = Math.max(config.timeout.upload || config.timeout.socket, 0); +serverThis.timeout = Math.max(config.timeout.upload || config.timeout.socket, 0); +server.keepAliveTimeout = Math.max(config.timeout.keepAlive, 0); +serverThis.keepAliveTimeout = Math.max(config.timeout.keepAlive, 0); global.TSW_HTTP_SERVER = server; -if(config.httpsOptions){ - serverHttps = https.createServer(config.httpsOptions,function(req,res){ +if(config.httpsOptions) { + serverHttps = https.createServer(config.httpsOptions, function(req, res) { req.headers['x-client-proto'] = 'https'; - requestHandler(req,res); + requestHandler(req, res); }); - serverHttps.on('clientError', function(err, socket){ + serverHttps.on('clientError', function(err, socket) { socket.end('HTTP/1.1 400 Bad Request\r\n\r\n'); }); - serverHttps.timeout = Math.max(config.timeout.upload || config.timeout.socket,0); - serverHttps.keepAliveTimeout = Math.max(config.timeout.keepAlive,0); + serverHttps.timeout = Math.max(config.timeout.upload || config.timeout.socket, 0); + serverHttps.keepAliveTimeout = Math.max(config.timeout.keepAlive, 0); global.TSW_HTTPS_SERVER = serverHttps; } -logger.info('pid:${pid} createServer ok',{ +logger.info('pid:${pid} createServer ok', { pid: process.pid }); //分发父进程发送来的消息 -process.on('message',function(m){ - if(m && methodMap[m.cmd]){ - methodMap[m.cmd].apply(this,arguments); +process.on('message', function(m) { + if(m && methodMap[m.cmd]) { + methodMap[m.cmd].apply(this, arguments); } }); -function startHeartBeat(){ +function startHeartBeat() { - if(isStartHeartBeat){ + if(isStartHeartBeat) { return; } @@ -263,7 +265,7 @@ function startHeartBeat(){ global.cpuUsed = 0; //定时给父进程发送心跳包 - setInterval(function(){ + setInterval(function() { heartBeatCount += 1; @@ -272,9 +274,9 @@ function startHeartBeat(){ memoryUsage: process.memoryUsage() }); - if(serverInfo.cpu === 0){ + if(serverInfo.cpu === 0) { //测试环境1分钟上报一次 - if(heartBeatCount % 12 === 0){ + if(heartBeatCount % 12 === 0) { TEReport.report(); } } @@ -299,13 +301,13 @@ function startHeartBeat(){ ) { //取进程快照 //ps aux --sort=-pcpu - cp.exec('top -bcn1',{ + cp.exec('top -bcn1', { env: { COLUMNS: 200 }, timeout: 5000 - },function(err,data,errData) { - var key = ['cpu80.v4',serverInfo.intranetIp].join(':'); + }, function(err, data, errData) { + var key = ['cpu80.v4', serverInfo.intranetIp].join(':'); var Content = [ '单核CPU' + serverInfo.cpu + '使用率为:' + cpuUsed + ',超过80%, 最近5秒钟CPU Profiler见附件' ].join('
'); @@ -322,17 +324,17 @@ function startHeartBeat(){ //获取本机信息,用来分组 - require('api/cmdb').GetDeviceThisServer().done(function(data){ + require('api/cmdb').GetDeviceThisServer().done(function(data) { data = data || {}; var business = data.business && data.business[0] || {}; var owner = ''; - if(data.ownerMain){ - owner = [owner,data.ownerMain].join(';'); + if(data.ownerMain) { + owner = [owner, data.ownerMain].join(';'); } - if(data.ownerBack){ - owner = [owner,data.ownerBack].join(';'); + if(data.ownerBack) { + owner = [owner, data.ownerBack].join(';'); } //再抓一份CPU Profiler @@ -363,61 +365,61 @@ function startHeartBeat(){ tnm2.Attr_API_Set('AVG_TSW_MEMORY_HEAP', currMemory.heapTotal); tnm2.Attr_API_Set('AVG_TSW_MEMORY_EXTERNAL', currMemory.external); - },5000); + }, 5000); } //restart -methodMap.restart = function(){ +methodMap.restart = function() { - logger.info('cpu: ${cpu} restart',serverInfo); + logger.info('cpu: ${cpu} restart', serverInfo); process.emit('restart'); }; //reload -methodMap.reload = function(){ +methodMap.reload = function() { - logger.info('cpu: ${cpu} reload',serverInfo); + logger.info('cpu: ${cpu} reload', serverInfo); process.emit('reload'); }; //heapdump -methodMap.heapdump = function(m){ +methodMap.heapdump = function(m) { - logger.info('cpu: ${cpu} heapdump',serverInfo); + logger.info('cpu: ${cpu} heapdump', serverInfo); - process.emit('heapdump',m.GET); + process.emit('heapdump', m.GET); }; //profiler -methodMap.profiler = function(m){ +methodMap.profiler = function(m) { - logger.info('cpu: ${cpu} profiler',serverInfo); + logger.info('cpu: ${cpu} profiler', serverInfo); - process.emit('profiler',m.GET); + process.emit('profiler', m.GET); }; //globaldump -methodMap.globaldump = function(m){ +methodMap.globaldump = function(m) { - logger.info('cpu: ${cpu} globaldump',serverInfo); + logger.info('cpu: ${cpu} globaldump', serverInfo); - process.emit('globaldump',m.GET); + process.emit('globaldump', m.GET); }; //top100 -methodMap.top100 = function(m){ +methodMap.top100 = function(m) { - logger.info('cpu: ${cpu} top100',serverInfo); + logger.info('cpu: ${cpu} top100', serverInfo); - process.emit('top100',m.GET); + process.emit('top100', m.GET); }; //监听端口 -methodMap.listen = function(message){ +methodMap.listen = function(message) { var user_00 = config.workerUid || 'user_00'; serverInfo.cpu = message.cpu || 0; @@ -426,22 +428,22 @@ methodMap.listen = function(message){ process.title = 'TSW/worker/' + serverInfo.cpu; global.TSW_HTTP_WORKER_PORT = config.workerPortBase + serverInfo.cpu; - logger.info('cpu: ${cpu}, beforeStartup...',serverInfo); + logger.info('cpu: ${cpu}, beforeStartup...', serverInfo); - if(typeof config.beforeStartup === 'function'){ + if(typeof config.beforeStartup === 'function') { config.beforeStartup(serverInfo.cpu); } - logger.info('cpu: ${cpu}, listen...',serverInfo); + logger.info('cpu: ${cpu}, listen...', serverInfo); //直接根据配置启动,无需拿到_handle server.listen({ host: config.httpAddress, port: config.httpPort, exclusive: false - },function(err){ - if(err){ - logger.info('cpu: ${cpu}, listen http error ${address}:${port}',{ + }, function(err) { + if(err) { + logger.info('cpu: ${cpu}, listen http error ${address}:${port}', { cpu:serverInfo.cpu, address: config.httpAddress, port: config.httpPort @@ -450,36 +452,36 @@ methodMap.listen = function(message){ return; } - logger.info('cpu: ${cpu}, listen http ok ${address}:${port}',{ + logger.info('cpu: ${cpu}, listen http ok ${address}:${port}', { cpu:serverInfo.cpu, address: config.httpAddress, port: config.httpPort }); - var finish = function(){ + var finish = function() { //开始发送心跳 logger.info('start heart beat'); startHeartBeat(); - if(!isWindows){ + if(!isWindows) { try{ process.setuid(user_00); - }catch(err){ + }catch(err) { logger.error(`switch to uid: ${user_00} fail!`); logger.error(err.stack); } - logger.info('switch to uid: ${uid}',{ + logger.info('switch to uid: ${uid}', { uid:user_00 }); } websocket.start_listen(); - logger.info('cpu: ${cpu}, afterStartup...',serverInfo); + logger.info('cpu: ${cpu}, afterStartup...', serverInfo); - if(typeof config.afterStartup === 'function'){ + if(typeof config.afterStartup === 'function') { config.afterStartup(serverInfo.cpu); } }; @@ -489,7 +491,7 @@ methodMap.listen = function(message){ host: config.httpAddress, port: global.TSW_HTTP_WORKER_PORT, exclusive: false - },function(err) { + }, function(err) { if (err) { logger.info('cpu: ${cpu}, listen http error ${address}:${port}', { cpu: serverInfo.cpu, @@ -507,12 +509,12 @@ methodMap.listen = function(message){ }); - if(serverHttps){ + if(serverHttps) { //启动https - serverHttps.listen(config.httpsPort,config.httpsAddress,function(err){ - if(err){ - logger.info('cpu: ${cpu}, listen https error ${address}:${port}',{ + serverHttps.listen(config.httpsPort, config.httpsAddress, function(err) { + if(err) { + logger.info('cpu: ${cpu}, listen https error ${address}:${port}', { cpu:serverInfo.cpu, address: config.httpsPort, port: config.httpsAddress @@ -521,7 +523,7 @@ methodMap.listen = function(message){ return; } - logger.info('cpu: ${cpu}, listen https ok ${address}:${port}',{ + logger.info('cpu: ${cpu}, listen https ok ${address}:${port}', { cpu:serverInfo.cpu, address: config.httpsAddress, port: config.httpsPort @@ -539,11 +541,11 @@ methodMap.listen = function(message){ }; -if(cluster.isMaster){ - if(isWindows){ +if(cluster.isMaster) { + if(isWindows) { logger.info('isWindows, start listening'); methodMap.listen({cpu : 0}); - }else if(debugOptions && debugOptions.inspectorEnabled){ + }else if(debugOptions && debugOptions.inspectorEnabled) { logger.setLogLevel('debug'); logger.info('inspectorEnabled, start listening'); methodMap.listen({cpu : 0}); diff --git a/bin/proxy/http.route.js b/bin/proxy/http.route.js index 9e97767b..5245ea87 100644 --- a/bin/proxy/http.route.js +++ b/bin/proxy/http.route.js @@ -31,7 +31,7 @@ const TSW = require('api/keyman'); const tnm2 = require('api/tnm2'); -module.exports = function(req,res){ +module.exports = function(req, res) { process.SN = process.SN || 0; @@ -39,9 +39,9 @@ module.exports = function(req,res){ var start = new Date(); var d = domain.create(); var tid = null; - var clear = function(){ + var clear = function() { - if(tid === null){ + if(tid === null) { return; } @@ -50,19 +50,19 @@ module.exports = function(req,res){ logger.debug('clear called'); - process.nextTick(function(){ + process.nextTick(function() { var timeout = timeLimit - (Date.now() - start.getTime()); - if(timeout > 2000){ + if(timeout > 2000) { timeout = 2000; } - if(!timeout){ + if(!timeout) { timeout = 0; } - if(timeout < 0){ + if(timeout < 0) { timeout = 0; } @@ -74,20 +74,20 @@ module.exports = function(req,res){ res.removeAllListeners('afterFinish'); res.removeAllListeners('done'); - setTimeout(function(){ + setTimeout(function() { logger.debug('clearing'); d.remove(req); d.remove(res); - if(d.currentContext){ + if(d.currentContext) { d.currentContext.window.request = null; d.currentContext.window.response = null; d.currentContext.window.onerror = null; d.currentContext.window = null; } - if(d.currentContext){ + if(d.currentContext) { d.currentContext.log = null; d.currentContext = null; } @@ -101,7 +101,7 @@ module.exports = function(req,res){ logger.debug('cleared'); - },timeout); + }, timeout); }); }; @@ -116,7 +116,7 @@ module.exports = function(req,res){ d.currentContext.window.request = req; d.currentContext.window.response = res; - if(config.enableWindow){ + if(config.enableWindow) { d.currentContext.window.enable(); } @@ -139,66 +139,66 @@ module.exports = function(req,res){ ClientDoneResponse : 0 }; - if(isWindows || config.devMode){ + if(isWindows || config.devMode) { d.currentContext.log.showLineNumber = true; } - if(global.cpuUsed > config.cpuLimit){ + if(global.cpuUsed > config.cpuLimit) { d.currentContext.log = null; } - d.on('error',function(err){ + d.on('error', function(err) { - if(err && err.message === 'socket hang up'){ + if(err && err.message === 'socket hang up') { logger.warn(err && err.stack); //忽略ajax错误 return; } - if(err && err.message === 'Cannot read property \'asyncReset\' of null'){ + if(err && err.message === 'Cannot read property \'asyncReset\' of null') { logger.warn(err && err.stack); //忽略asyncReset错误 return; } - if(err && err.message === 'Cannot read property \'resume\' of null'){ + if(err && err.message === 'Cannot read property \'resume\' of null') { logger.warn(err && err.stack); //忽略io错误 return; } - if(err && err.message === 'write ECONNRESET'){ + if(err && err.message === 'write ECONNRESET') { logger.warn(err && err.stack); //忽略io错误 return; } - if(err && err.message === 'This socket is closed'){ + if(err && err.message === 'This socket is closed') { logger.warn(err && err.stack); //忽略io错误 return; } - if(err && err.stack && err.stack.indexOf('/') === -1 && err.stack.indexOf('\\') === -1){ + if(err && err.stack && err.stack.indexOf('/') === -1 && err.stack.indexOf('\\') === -1) { logger.warn(err && err.stack); //忽略原生错误 return; } - if(clear === null){ + if(clear === null) { logger.warn(err && err.stack); return; } - onerror(req,res,err); + onerror(req, res, err); - if(httpUtil.isSent(res)){ - logger.warn('${err}\nhttp://${host}${url}',{ + if(httpUtil.isSent(res)) { + logger.warn('${err}\nhttp://${host}${url}', { err: err && err.stack, url: req.url, host: req.headers.host @@ -213,7 +213,7 @@ module.exports = function(req,res){ }); }else{ - logger.error('${err}\nhttp://${host}${url}',{ + logger.error('${err}\nhttp://${host}${url}', { err: err && err.stack, url: req.url, host: req.headers.host @@ -230,38 +230,42 @@ module.exports = function(req,res){ try{ res.writeHead(503, {'Content-Type': 'text/html; charset=UTF-8'}); res.end(); - }catch(e){ + }catch(e) { logger.info(`response 503 fail ${e.message}`); } } - try{res.emit('done');}catch(e){logger.info(`emit done event fail ${e.message}`);} + try{ + res.emit('done'); + }catch(e) { + logger.info(`emit done event fail ${e.message}`); + } - var key,Content; + var key, Content; - if(err && err.stack && err.message){ + if(err && err.stack && err.message) { - if(err.message === 'Cannot read property \'asyncReset\' of null'){ + if(err.message === 'Cannot read property \'asyncReset\' of null') { return; } - if(err.message.indexOf('ETIMEDOUT') > 0){ + if(err.message.indexOf('ETIMEDOUT') > 0) { return; } - if(err.message.indexOf('timeout') > 0){ + if(err.message.indexOf('timeout') > 0) { return; } - if(err.message.indexOf('hang up') > 0){ + if(err.message.indexOf('hang up') > 0) { return; } - if(err.message.indexOf('error:140943FC') > 0){ + if(err.message.indexOf('error:140943FC') > 0) { return; } - if(isWindows){ + if(isWindows) { //return; } @@ -274,7 +278,7 @@ module.exports = function(req,res){ '

', ].join(''); - mail.SendMail(key,'js',600,{ + mail.SendMail(key, 'js', 600, { 'Title' : key, 'runtimeType' : 'Error', 'MsgInfo' : err.stack || err.message, @@ -284,75 +288,75 @@ module.exports = function(req,res){ }); - res.once('finish',function(){ + res.once('finish', function() { this.emit('done'); }); - res.once('close',function(){ + res.once('close', function() { res.__hasClosed = true; logger.debug('response has close'); this.emit('done');// let it going }); - res.once('done',function(){ + res.once('done', function() { - var isFail = 0,now; + var isFail = 0, now; - if(clear === null){ + if(clear === null) { return; } clear(); now = new Date(); - if(!res.statusCode){ + if(!res.statusCode) { isFail = 1; } - if(res.statusCode === 200){ + if(res.statusCode === 200) { isFail = 0; tnm2.Attr_API('SUM_TSW_HTTP_20X', 1); - }else if(res.statusCode === 206 || res.statusCode === 204){ + }else if(res.statusCode === 206 || res.statusCode === 204) { isFail = 0; tnm2.Attr_API('SUM_TSW_HTTP_20X', 1); - }else if(res.statusCode === 301){ + }else if(res.statusCode === 301) { isFail = 0; tnm2.Attr_API('SUM_TSW_HTTP_302', 1); - }else if(res.statusCode === 302){ + }else if(res.statusCode === 302) { isFail = 0; tnm2.Attr_API('SUM_TSW_HTTP_302', 1); - }else if(res.statusCode === 303){ + }else if(res.statusCode === 303) { isFail = 0; tnm2.Attr_API('SUM_TSW_HTTP_302', 1); - }else if(res.statusCode === 307){ + }else if(res.statusCode === 307) { isFail = 0; tnm2.Attr_API('SUM_TSW_HTTP_302', 1); - }else if(res.statusCode === 304){ + }else if(res.statusCode === 304) { isFail = 0; tnm2.Attr_API('SUM_TSW_HTTP_304', 1); - }else if(res.statusCode === 403){ + }else if(res.statusCode === 403) { isFail = 0; tnm2.Attr_API('SUM_TSW_HTTP_403', 1); - }else if(res.statusCode === 404){ + }else if(res.statusCode === 404) { isFail = 2; tnm2.Attr_API('SUM_TSW_HTTP_404', 1); - }else if(res.statusCode === 418){ + }else if(res.statusCode === 418) { isFail = 0; tnm2.Attr_API('SUM_TSW_HTTP_418', 1); - }else if(res.statusCode === 419){ + }else if(res.statusCode === 419) { isFail = 0; tnm2.Attr_API('SUM_TSW_HTTP_419', 1); - }else if(res.statusCode === 666){ + }else if(res.statusCode === 666) { isFail = 0; tnm2.Attr_API('SUM_TSW_HTTP_666', 1); - }else if(res.statusCode === 501){ + }else if(res.statusCode === 501) { isFail = 2; tnm2.Attr_API('SUM_TSW_HTTP_501', 1); - }else if(res.statusCode === 508){ + }else if(res.statusCode === 508) { isFail = 2; tnm2.Attr_API('SUM_TSW_HTTP_508', 1); - }else if(res.statusCode >= 500 && res.statusCode <= 599){ + }else if(res.statusCode >= 500 && res.statusCode <= 599) { isFail = 1; tnm2.Attr_API('SUM_TSW_HTTP_500', 1); }else{ @@ -367,41 +371,41 @@ module.exports = function(req,res){ req.timestamps.ClientDoneResponse = req.timestamps.ServerDoneResponse; - if(isFail === 1){ - logger.debug('finish, statusCode: ${statusCode},cost: ${cost}ms',{ + if(isFail === 1) { + logger.debug('finish, statusCode: ${statusCode},cost: ${cost}ms', { statusCode: res.statusCode, cost: Date.now() - start.getTime() }); - if(typeof req.REQUEST.body === 'string'){ + if(typeof req.REQUEST.body === 'string') { - if(req.REQUEST.body.length < 32 * 1024){ - logger.debug('\n${head}${body}',{ + if(req.REQUEST.body.length < 32 * 1024) { + logger.debug('\n${head}${body}', { head: httpUtil.getRequestHeaderStr(req), body: req.REQUEST.body || '' }); }else{ - logger.debug('\n${head}${body}',{ + logger.debug('\n${head}${body}', { head: httpUtil.getRequestHeaderStr(req), body: 'body size >= 32KB' }); } }else{ - logger.debug('\n${head}${body}',{ + logger.debug('\n${head}${body}', { head: httpUtil.getRequestHeaderStr(req), body: req.REQUEST.body || '' }); } }else{ - logger.debug('finish, statusCode: ${statusCode}, cost: ${cost}ms',{ + logger.debug('finish, statusCode: ${statusCode}, cost: ${cost}ms', { statusCode: res.statusCode, cost: Date.now() - start.getTime() }); } - if(res.__hasTimeout && isFail !== 1){ + if(res.__hasTimeout && isFail !== 1) { dcapi.report({ key : 'EVENT_TSW_HTTP_TIMEOUT', @@ -411,7 +415,7 @@ module.exports = function(req,res){ delay : Date.now() - start.getTime() }); - }else if(res.__hasClosed){ + }else if(res.__hasClosed) { dcapi.report({ key : 'EVENT_TSW_HTTP_CLOSE', @@ -444,55 +448,95 @@ module.exports = function(req,res){ res.emit('afterFinish'); }); - d.run(function(){ - tid = setTimeout(function(){ + d.run(function() { + tid = setTimeout(function() { res.__hasTimeout = true; logger.debug('timeout: ' + timeLimit); - onerror(req,res,new Error('timeout')); + onerror(req, res, new Error('timeout')); - if(res.__hasClosed){ + if(res.__hasClosed) { - try{res.writeHead(202); }catch(e){logger.info(`response 202 fail ${e.message}`);} - try{res.end();}catch(e){logger.info(`response end fail ${e.message}`);} - try{res.emit('done');}catch(e){logger.info(`emit done event fail ${e.message}`);} - }else if(res.finished){ + try{ + res.writeHead(202); + }catch(e) { + logger.info(`response 202 fail ${e.message}`); + } + try{ + res.end(); + }catch(e) { + logger.info(`response end fail ${e.message}`); + } + try{ + res.emit('done'); + }catch(e) { + logger.info(`emit done event fail ${e.message}`); + } + }else if(res.finished) { - try{res.end();}catch(e){logger.info(`response end fail ${e.message}`);} - try{res.emit('done');}catch(e){logger.info(`emit done event fail ${e.message}`);} - }else if(!res._headerSent && !res.headersSent && !res.finished && res.statusCode === 200){ - logger.debug('statusCode: ${statusCode}, _headerSent: ${_headerSent}, headersSent: ${headersSent}, finished: ${finished}',res); + try{ + res.end(); + }catch(e) { + logger.info(`response end fail ${e.message}`); + } + try{ + res.emit('done'); + }catch(e) { + logger.info(`emit done event fail ${e.message}`); + } + }else if(!res._headerSent && !res.headersSent && !res.finished && res.statusCode === 200) { + logger.debug('statusCode: ${statusCode}, _headerSent: ${_headerSent}, headersSent: ${headersSent}, finished: ${finished}', res); //输出一条错误log方便定位问题 - logger.error('response timeout http://${host}${url}',{ + logger.error('response timeout http://${host}${url}', { url: req.url, host: req.headers.host }); - try{res.writeHead(513); }catch(e){logger.info(`response 513 fail ${e.message}`);} - try{res.end();}catch(e){logger.info(`response end fail ${e.message}`);} + try{ + res.writeHead(513); + }catch(e) { + logger.info(`response 513 fail ${e.message}`); + } + try{ + res.end(); + }catch(e) { + logger.info(`response end fail ${e.message}`); + } - try{res.emit('done');}catch(e){logger.info(`emit done event fail ${e.message}`);} + try{ + res.emit('done'); + }catch(e) { + logger.info(`emit done event fail ${e.message}`); + } }else{ - logger.debug('statusCode: ${statusCode}, _headerSent: ${_headerSent}, headersSent: ${headersSent}, finished: ${finished}',res); + logger.debug('statusCode: ${statusCode}, _headerSent: ${_headerSent}, headersSent: ${headersSent}, finished: ${finished}', res); - try{res.end();}catch(e){logger.info(`response end fail ${e.message}`);} - try{res.emit('done');}catch(e){logger.info(`emit done event fail ${e.message}`);} + try{ + res.end(); + }catch(e) { + logger.info(`response end fail ${e.message}`); + } + try{ + res.emit('done'); + }catch(e) { + logger.info(`emit done event fail ${e.message}`); + } } req.emit('close'); - },timeLimit); + }, timeLimit); - doRoute(req,res); + doRoute(req, res); }); }; module.exports.doRoute = doRoute; -function doRoute(req,res){ +function doRoute(req, res) { var clientIp = httpUtil.getUserIp(req); var userIp24 = httpUtil.getUserIp24(req); @@ -502,14 +546,14 @@ function doRoute(req,res){ res.setHeader('Test-Head', serverInfo.intranetIp || ''); } - logger.debug('${method} ${protocol}://${host}${path}',{ + logger.debug('${method} ${protocol}://${host}${path}', { protocol: req.REQUEST.protocol, path: req.REQUEST.path, host: req.headers.host, method: req.method }); - logger.debug('idc: ${idc}, server ip: ${intranetIp}, tcp: ${remoteAddress}:${remotePort} > ${localAddress}:${localPort}, client ip: ${clientIp}, cpuUsed: ${cpuUsed}',{ + logger.debug('idc: ${idc}, server ip: ${intranetIp}, tcp: ${remoteAddress}:${remotePort} > ${localAddress}:${localPort}, client ip: ${clientIp}, cpuUsed: ${cpuUsed}', { cpuUsed: global.cpuUsed, idc: config.idc, intranetIp: serverInfo.intranetIp, @@ -520,9 +564,9 @@ function doRoute(req,res){ localPort: (req.socket && req.socket.localPort) }); - if(config.isTest){ + if(config.isTest) { logger.debug('config.isTest is true'); - if(isTST.isTST(req)){ + if(isTST.isTST(req)) { res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'}); res.end(); return; @@ -532,10 +576,10 @@ function doRoute(req,res){ //安全中心扫描报个指标 //支持从配置中直接屏蔽安全中心扫描请求 - if(isTST.isTST(req)){ + if(isTST.isTST(req)) { tnm2.Attr_API('SUM_TSW_HTTP_TST', 1); - if(config.ignoreTST){ + if(config.ignoreTST) { logger.debug('ignore TST request'); res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'}); @@ -545,17 +589,17 @@ function doRoute(req,res){ } } - if(config.devMode){ + if(config.devMode) { logger.debug('config.devMode is true'); } //log自动上报 - logReport(req,res); + logReport(req, res); - res.writeHead = (function(fn){ - return function(...args){ - if(alpha.isAlpha(req)){ - if(logger.getLog()){ + res.writeHead = (function(fn) { + return function(...args) { + if(alpha.isAlpha(req)) { + if(logger.getLog()) { logger.getLog().showLineNumber = true; logger.debug('showLineNumber on'); } @@ -564,32 +608,32 @@ function doRoute(req,res){ httpUtil.captureBody(this); } - logger.debug('response ${statusCode}',{ + logger.debug('response ${statusCode}', { statusCode: args[0] }); - return fn.apply(this,args); + return fn.apply(this, args); }; }(res.writeHead)); var mod_act = contextMod.currentContext().mod_act || httpModAct.getModAct(req); contextMod.currentContext().mod_act = mod_act; - if(alpha.isAlpha(req)){ - if(logger.getLog()){ + if(alpha.isAlpha(req)) { + if(logger.getLog()) { logger.getLog().showLineNumber = true; logger.debug('showLineNumber on'); } } - logger.debug('node-${version}, name: ${name}, appid: ${appid}',{ + logger.debug('node-${version}, name: ${name}, appid: ${appid}', { version: process.version, name: mod_act || null, appid: config.appid || null }); //测试环境 - if(h5test.isTestUser(req, res)){ + if(h5test.isTestUser(req, res)) { return; } @@ -597,14 +641,14 @@ function doRoute(req,res){ var steps = parseInt(req.headers['tsw-trace-steps'] || '0') || 0; //深度超过5层,直接拒绝 - if(steps >= 5){ + if(steps >= 5) { tnm2.Attr_API('SUM_TSW_ROUTE_EXCEED', 1); try{ res.writeHead(503, {'Content-Type': 'text/html; charset=UTF-8'}); res.end('503'); - }catch(e){ + }catch(e) { logger.info(`response 503 fail ${e.message}`); } @@ -613,67 +657,67 @@ function doRoute(req,res){ req.headers['tsw-trace-steps'] = steps + 1; - var modulePath = httpModMap.find(mod_act,req,res); + var modulePath = httpModMap.find(mod_act, req, res); - if(res.headersSent || res._headerSent || res.finished){ + if(res.headersSent || res._headerSent || res.finished) { return; } - if(modulePath && typeof modulePath.handle === 'function'){ + if(modulePath && typeof modulePath.handle === 'function') { let app = modulePath; - modulePath = function(req,res,plug){ - return app.handle(req,res); + modulePath = function(req, res, plug) { + return app.handle(req, res); }; } - if(modulePath && typeof modulePath.callback === 'function'){ + if(modulePath && typeof modulePath.callback === 'function') { let app = modulePath; - modulePath = function(req,res,plug){ - return app.callback()(req,res); + modulePath = function(req, res, plug) { + return app.callback()(req, res); }; } - if(typeof modulePath !== 'function'){ + if(typeof modulePath !== 'function') { if(req.REQUEST.pathname === '/419') { - if(typeof config.page419 === 'string'){ + if(typeof config.page419 === 'string') { modulePath = require(config.page419); } } else { - if(typeof config.page404 === 'string'){ + if(typeof config.page404 === 'string') { modulePath = require(config.page404); } } } - if(typeof modulePath !== 'function'){ + if(typeof modulePath !== 'function') { try{ res.writeHead(404, {'Content-Type': 'text/html; charset=UTF-8'}); res.end('404'); - }catch(e){ + }catch(e) { logger.info(`response 404 fail ${e.message}`); } return; } - var modulePathHandler = function(){ + var modulePathHandler = function() { var maybePromise = modulePath(req, res, plug); if( typeof maybePromise === 'object' && typeof maybePromise.catch === 'function' - ){ - maybePromise.catch(function(err){ + ) { + maybePromise.catch(function(err) { logger.error(err); - process.domain && process.domain.emit('error',err); + process.domain && process.domain.emit('error', err); }); } }; var blackIpMap = TSW.getBlockIpMapSync() || {}; - if(blackIpMap[clientIp] || blackIpMap[userIp24] || !clientIp){ + if(blackIpMap[clientIp] || blackIpMap[userIp24] || !clientIp) { logger.debug('连接已断开'); tnm2.Attr_API('SUM_TSW_IP_EMPTY', 1); @@ -682,7 +726,7 @@ function doRoute(req,res){ return; } - if(blackIpMap[clientIp] || blackIpMap[userIp24]){ + if(blackIpMap[clientIp] || blackIpMap[userIp24]) { logger.debug('命中黑名单IP'); dcapi.report({ @@ -698,20 +742,20 @@ function doRoute(req,res){ return; } - if(CCFinder.checkHost(req,res) === false){ + if(CCFinder.checkHost(req, res) === false) { return; } - if(CCFinder.check(req,res) === false){ + if(CCFinder.check(req, res) === false) { return; } //webso柔性 - if(global.cpuUsed > 80){ + if(global.cpuUsed > 80) { - if(httpUtil.isFromWns(req) && req.headers['if-none-match']){ + if(httpUtil.isFromWns(req) && req.headers['if-none-match']) { - logger.debug('webso limit 304, cpuUsed: ${cpuUsed}',{ + logger.debug('webso limit 304, cpuUsed: ${cpuUsed}', { cpuUsed : global.cpuUsed }); @@ -722,7 +766,7 @@ function doRoute(req,res){ 'Etag': req.headers['if-none-match'] }); res.end(); - }catch(e){ + }catch(e) { logger.info(`response 304 fail ${e.message}`); } @@ -733,27 +777,27 @@ function doRoute(req,res){ var contentType = req.headers['content-type'] || 'application/x-www-form-urlencoded'; - if(req.method === 'GET' || req.method === 'HEAD'){ + if(req.method === 'GET' || req.method === 'HEAD') { - if(httpUtil.isFromWns(req)){ + if(httpUtil.isFromWns(req)) { //wns请求不过门神检查 return modulePathHandler(); } - xssFilter.check().done(function(){ + xssFilter.check().done(function() { return modulePathHandler(); - }).fail(function(){ + }).fail(function() { res.writeHead(501, {'Content-Type': 'text/plain; charset=UTF-8'}); res.end('501 by TSW'); }); - }else if(context.autoParseBody === false){ + }else if(context.autoParseBody === false) { return modulePathHandler(); }else if( contentType.indexOf('application/x-www-form-urlencoded') > -1 || contentType.indexOf('text/plain') > -1 || contentType.indexOf('application/json') > -1 - ){ - parseBody(req,res,function(){ + ) { + parseBody(req, res, function() { return modulePathHandler(); }); }else{ @@ -763,27 +807,27 @@ function doRoute(req,res){ } -function onerror(req,res,err){ +function onerror(req, res, err) { var listener = req.listeners('fail'); var window = context.window || {}; - if(res.headersSent || res._headerSent || res.finished){ + if(res.headersSent || res._headerSent || res.finished) { return; } - if(listener && listener.length > 0){ + if(listener && listener.length > 0) { try{ - req.emit('fail',err); - }catch(e){ + req.emit('fail', err); + }catch(e) { logger.error(e && e.stack); } req.removeAllListeners('fail'); - }else if(window.onerror){ + }else if(window.onerror) { try{ window.onerror(err); - }catch(e){ + }catch(e) { logger.error(e && e.stack); } diff --git a/bin/proxy/master.js b/bin/proxy/master.js index ed86946e..34df7703 100644 --- a/bin/proxy/master.js +++ b/bin/proxy/master.js @@ -20,7 +20,7 @@ const cpuMap = []; var isDeaded = false; //阻止进程因异常而退出 -process.on('uncaughtException',function(e){ +process.on('uncaughtException', function(e) { if (/\blisten EACCES\b/.test(e.message) && config.httpPort < 1024 && (serverOS.isOSX || serverOS.isLinux)) { logger.error('This is OSX/Linux, you may need to use "sudo" prefix to start server.\n'); } @@ -29,7 +29,7 @@ process.on('uncaughtException',function(e){ }); -process.on('warning',function(warning){ +process.on('warning', function(warning) { var key = String(warning); var errStr = warning && warning.stack || String(warning); @@ -43,8 +43,8 @@ process.on('warning',function(warning){ ].join(''); - setImmediate(function(){ - require('util/mail/mail.js').SendMail(key,'js',600,{ + setImmediate(function() { + require('util/mail/mail.js').SendMail(key, 'js', 600, { 'Title' : key, 'runtimeType' : 'warning', 'Content' : Content @@ -60,19 +60,19 @@ process.on('unhandledRejection', (reason = {}, p = {}) => { errStr = String(reason.stack), mod_act, module, REQUEST; - if(p && p.domain && p.domain.currentContext){ + if(p && p.domain && p.domain.currentContext) { mod_act = p.domain.currentContext.mod_act; module = p.domain.currentContext.module; REQUEST = p.domain.currentContext.window.request.REQUEST; } - if(errStr === 'undefined'){ + if(errStr === 'undefined') { let reasonStr = JSON.stringify(reason); logger.error('unhandledRejection reason: ' + reasonStr); - if(reasonStr === '{}'){ + if(reasonStr === '{}') { return; } @@ -88,21 +88,21 @@ process.on('unhandledRejection', (reason = {}, p = {}) => { '

', ].join(''); - if(mod_act){ + if(mod_act) { Content += `

mod_act: ${mod_act}

`; } - if(module){ + if(module) { Content += `

module: ${module}

`; } - if(REQUEST){ + if(REQUEST) { Content += `

url: ${REQUEST.protocol}://${REQUEST.hostname}${REQUEST.href}

`; } - if(serverOS.isWindows){ + if(serverOS.isWindows) { //return; } - require('util/mail/mail.js').SendMail(key,'js',600,{ + require('util/mail/mail.js').SendMail(key, 'js', 600, { 'Title' : key, 'runtimeType' : 'unhandledRejection', 'Content' : Content @@ -116,62 +116,62 @@ startServer(); // 通过cluster启动master && worker -function startServer(){ +function startServer() { var useWorker = true; - if(debugOptions && debugOptions.inspectorEnabled){ + if(debugOptions && debugOptions.inspectorEnabled) { useWorker = false; } //windows下需要使用RR cluster.schedulingPolicy = cluster.SCHED_RR; - if(cluster.isMaster && useWorker){ + if(cluster.isMaster && useWorker) { global.cpuUsed = cpuUtil.getCpuUsed(); - setInterval(function(){ + setInterval(function() { global.cpuUsed = cpuUtil.getCpuUsed(); - },3000); + }, 3000); //启动管理进程 require('./admin.js'); logger.info('start master....'); - logger.info('version node: ${node}, modules: ${modules}',process.versions); + logger.info('version node: ${node}, modules: ${modules}', process.versions); - if(serverOS.isLinux){ + if(serverOS.isLinux) { //当前目录777,为heapsnapshot文件创建提供权限 fs.chmodSync(__dirname, 0x1ff); //0777 } //根据cpu数来初始化并启动子进程 - if(config.runAtThisCpu === 'auto'){ - cpuUtil.cpus().forEach(function(v,i){ + if(config.runAtThisCpu === 'auto') { + cpuUtil.cpus().forEach(function(v, i) { cpuMap.push(0); cluster.fork(process.env).cpuid = i; }); }else{ - config.runAtThisCpu.forEach(function(v,i){ + config.runAtThisCpu.forEach(function(v, i) { cpuMap.push(0); cluster.fork(process.env).cpuid = v; }); } //监听子进程是否fork成功 - cluster.on('fork',function(currWorker){ + cluster.on('fork', function(currWorker) { var cpu = getToBindCpu(currWorker); - logger.info('worker fork success! pid:${pid} cpu: ${cpu}',{ + logger.info('worker fork success! pid:${pid} cpu: ${cpu}', { pid: currWorker.process.pid, cpu: cpu }); //绑定cpu - cpuUtil.taskset(cpu,currWorker.process.pid); + cpuUtil.taskset(cpu, currWorker.process.pid); - if(workerMap[cpu]){ + if(workerMap[cpu]) { closeWorker(workerMap[cpu]); } @@ -179,10 +179,10 @@ function startServer(){ cpuMap[cpu] = 1; //监听子进程发来的消息并处理 - currWorker.on('message',function(...args){ + currWorker.on('message', function(...args) { var m = args[0]; - if(m && methodMap[m.cmd]){ - methodMap[m.cmd].apply(this,args); + if(m && methodMap[m.cmd]) { + methodMap[m.cmd].apply(this, args); } }); @@ -199,11 +199,11 @@ function startServer(){ cluster.on('disconnect', function(worker) { var cpu = getToBindCpu(worker); - if(worker.hasRestart){ + if(worker.hasRestart) { return; } - logger.info('worker${cpu} pid=${pid} has disconnected. restart new worker again.',{ + logger.info('worker${cpu} pid=${pid} has disconnected. restart new worker again.', { pid: worker.process.pid, cpu: cpu }); @@ -212,15 +212,15 @@ function startServer(){ }); //子进程被杀死的时候做下处理,原地复活 - cluster.on('exit',function(worker){ + cluster.on('exit', function(worker) { var cpu = getToBindCpu(worker); - if(worker.hasRestart){ + if(worker.hasRestart) { return; } - logger.info('worker${cpu} pid=${pid} has been killed. restart new worker again.',{ + logger.info('worker${cpu} pid=${pid} has been killed. restart new worker again.', { pid: worker.process.pid, cpu: cpu }); @@ -228,80 +228,80 @@ function startServer(){ restartWorker(worker); }); - process.on('reload',function(GET){ + process.on('reload', function(GET) { var timeout = 1000, cpu = 0, - key,worker; + key, worker; - if(isDeaded){ + if(isDeaded) { process.exit(0); } logger.info('reload'); - for(key in workerMap){ + for(key in workerMap) { worker = workerMap[key]; try{ cpu = getToBindCpu(worker); - if(config.isTest || config.devMode){ + if(config.isTest || config.devMode) { timeout = (cpu % 8) * 1000; }else{ timeout = (cpu % 8) * 3000; } - setTimeout(function(worker,cpu){ - return function(){ - if(!worker.exitedAfterDisconnect){ - logger.info('cpu${cpu} send restart message',{ + setTimeout(function(worker, cpu) { + return function() { + if(!worker.exitedAfterDisconnect) { + logger.info('cpu${cpu} send restart message', { cpu: cpu }); - worker.send({from:'master',cmd:'restart'}); + worker.send({from:'master', cmd:'restart'}); } restartWorker(worker); }; - }(worker,cpu),timeout); + }(worker, cpu), timeout); - logger.info('cpu${cpu} reload after ${timeout}ms',{ + logger.info('cpu${cpu} reload after ${timeout}ms', { cpu: cpu, timeout: timeout }); - }catch(e){ + }catch(e) { logger.error(e.stack); } } }); - process.on('sendCmd2workerOnce',function(data){ + process.on('sendCmd2workerOnce', function(data) { - var key,worker; + var key, worker; var CMD = data.CMD; var GET = data.GET; - if(isDeaded){ + if(isDeaded) { process.exit(0); } - logger.info('sendCmd2workerOnce CMD: ${CMD}',{ + logger.info('sendCmd2workerOnce CMD: ${CMD}', { CMD }); var targetCpu = GET.cpu || 0; - for(key in workerMap){ + for(key in workerMap) { worker = workerMap[key]; try{ - if(targetCpu == getToBindCpu(worker)){ - if(!worker.exitedAfterDisconnect){ + if(targetCpu == getToBindCpu(worker)) { + if(!worker.exitedAfterDisconnect) { worker.send({from:'master', cmd:CMD, GET: GET}); } break; } - }catch(e){ + }catch(e) { logger.error(e.stack); } } @@ -323,15 +323,15 @@ function startServer(){ //30分钟后开始算 !config.isTest && !config.devMode && - setTimeout(function(){ + setTimeout(function() { require('runtime/md5.check.js').check(); - },30 * 60000); + }, 30 * 60000); } } //处理子进程的心跳消息 -methodMap.heartBeat = function(m){ +methodMap.heartBeat = function(m) { var worker = this; var now = new Date().getTime(); @@ -341,36 +341,36 @@ methodMap.heartBeat = function(m){ }; //关闭一个worker -function closeWorker(worker){ +function closeWorker(worker) { var cpu = worker.cpuid; var closeTimeWait = 10000; - closeTimeWait = Math.max(closeTimeWait,config.timeout.socket); - closeTimeWait = Math.max(closeTimeWait,config.timeout.post); - closeTimeWait = Math.max(closeTimeWait,config.timeout.get); - closeTimeWait = Math.max(closeTimeWait,config.timeout.keepAlive); - closeTimeWait = Math.min(60000,closeTimeWait) || 10000; + closeTimeWait = Math.max(closeTimeWait, config.timeout.socket); + closeTimeWait = Math.max(closeTimeWait, config.timeout.post); + closeTimeWait = Math.max(closeTimeWait, config.timeout.get); + closeTimeWait = Math.max(closeTimeWait, config.timeout.keepAlive); + closeTimeWait = Math.min(60000, closeTimeWait) || 10000; - if(worker.hasClose){ + if(worker.hasClose) { return; } - if(workerMap[cpu] === worker){ + if(workerMap[cpu] === worker) { delete workerMap[cpu]; } - var closeFn = function(worker){ + var closeFn = function(worker) { var closed = false; var pid = worker.process.pid; - return function(){ - if(closed){ + return function() { + if(closed) { return; } try{ - process.kill(pid,9); - }catch(e){ + process.kill(pid, 9); + }catch(e) { logger.info(`kill worker fail ${e.message}`); } @@ -380,37 +380,37 @@ function closeWorker(worker){ }; }(worker); - setTimeout(closeFn,closeTimeWait); + setTimeout(closeFn, closeTimeWait); - if(worker.exitedAfterDisconnect){ + if(worker.exitedAfterDisconnect) { worker.hasClose = true; return; } try{ worker.disconnect(closeFn); - }catch(e){ + }catch(e) { logger.info(e.stack); } } //重启worker -function restartWorker(worker){ +function restartWorker(worker) { var cpu = getToBindCpu(worker); - if(worker.hasRestart){ + if(worker.hasRestart) { return; } - logger.info('worker${cpu} pid=${pid} close. restart new worker again.',{ + logger.info('worker${cpu} pid=${pid} close. restart new worker again.', { pid: worker.process.pid, cpu: cpu }); - setTimeout(function(){ + setTimeout(function() { closeWorker(worker); - },10000); + }, 10000); cpuMap[cpu] = 0; @@ -419,9 +419,9 @@ function restartWorker(worker){ } //定时检测子进程存活,发现15秒没响应的就干掉 -function checkWorkerAlive(){ +function checkWorkerAlive() { - setInterval(function(){ + setInterval(function() { var nowDate = new Date(), @@ -430,19 +430,19 @@ function checkWorkerAlive(){ worker, cpuid; - for(key in workerMap){ + for(key in workerMap) { worker = workerMap[key]; cpuid = worker.cpuid; worker.lastLiveTime = worker.lastLiveTime || now; - if(!worker.startTime){ + if(!worker.startTime) { worker.startTime = now; } //无响应进程处理 - if(now - worker.lastLiveTime > 15000 && cpuMap[cpuid] === 1){ + if(now - worker.lastLiveTime > 15000 && cpuMap[cpuid] === 1) { - logger.error('worker${cpu} pid=${pid} miss heartBeat, kill it',{ + logger.error('worker${cpu} pid=${pid} miss heartBeat, kill it', { pid : worker.process.pid, cpu: cpuid }); @@ -451,14 +451,14 @@ function checkWorkerAlive(){ } //内存超限进程处理 - if(worker.lastMessage){ + if(worker.lastMessage) { let currMemory = worker.lastMessage.memoryUsage; //logger.debug(currMemory); - if(currMemory && currMemory.rss > config.memoryLimit){ + if(currMemory && currMemory.rss > config.memoryLimit) { - logger.error('worker${cpu} pid=${pid} memoryUsage ${memoryUsage}, hit memoryLimit: ${memoryLimit}, kill it',{ + logger.error('worker${cpu} pid=${pid} memoryUsage ${memoryUsage}, hit memoryLimit: ${memoryLimit}, kill it', { memoryUsage: currMemory.rss, memoryLimit: config.memoryLimit, pid : worker.process.pid, @@ -477,26 +477,26 @@ function checkWorkerAlive(){ && (nowDate.getHours() % 8 === 0) && nowDate.getMinutes() === 1 && nowDate.getSeconds() <= 10 - ){ + ) { //8小时一次 require('api/keyman/runtimeAdd.js').hello(); } - },5000); + }, 5000); } //获取需要绑定的CPU编号 -function getToBindCpu(worker){ +function getToBindCpu(worker) { var cpu = 0;//如果只有一个cpu或者都占用了 - if(worker.cpuid !== undefined){ + if(worker.cpuid !== undefined) { cpu = worker.cpuid; return cpu; }else{ - for(var i=0;i 0){ - if(opt.enctype === 'application/json'){ + if(!opt.body && opt.data && Object.keys(opt.data).length > 0) { + if(opt.enctype === 'application/json') { opt.headers['content-type'] = 'application/json'; opt.body = JSON.stringify(opt.data); - }else if(opt.enctype === 'multipart/form-data'){ + }else if(opt.enctype === 'multipart/form-data') { opt.boundary = opt.boundary || Math.random().toString(16); opt.headers['content-type'] = 'multipart/form-data; boundary=' + opt.boundary; @@ -349,10 +349,10 @@ Ajax.prototype.doRequest = function(opt){ } } }else{ - if(!opt.isRetriedRequest){ + if(!opt.isRetriedRequest) { let query = qs.stringify(opt.data, { arrayFormat: 'brackets', skipNulls: true, encoder: opt.encoder }); - if(query){ - if(opt.path.indexOf('?') === -1){ + if(query) { + if(opt.path.indexOf('?') === -1) { opt.path = opt.path + '?' + query; }else{ opt.path = opt.path + '&' + query; @@ -362,18 +362,18 @@ Ajax.prototype.doRequest = function(opt){ } - if(config.isTest){ + if(config.isTest) { - if(opt.testIp){ + if(opt.testIp) { logger.debug('use testIp'); //测试环境模式 opt.ip = opt.testIp || opt.ip; opt.port = opt.testPort || opt.port; } - }else if((config.devMode || isWindows)){ + }else if((config.devMode || isWindows)) { - if(opt.devIp){ + if(opt.devIp) { logger.debug('use devIp'); //开发者模式 opt.ip = opt.devIp || opt.ip; @@ -384,17 +384,17 @@ Ajax.prototype.doRequest = function(opt){ } - if(opt.host){ + if(opt.host) { opt.headers.host = opt.host; }else{ opt.host = opt.headers.host; } - if(opt.useIPAsHost){ + if(opt.useIPAsHost) { opt.headers.host = opt.ip; } - if(currRetry === opt.retry){ + if(currRetry === opt.retry) { //防止重复注册 opt.success && defer.done(opt.success); opt.error && defer.fail(opt.error); @@ -406,20 +406,20 @@ Ajax.prototype.doRequest = function(opt){ function report(opt, isFail, code) { - if(isTST.isTST(opt)){ + if(isTST.isTST(opt)) { //忽略安全中心请求 return; } - if(isFail === 1 && opt.ignoreErrorReport){ + if(isFail === 1 && opt.ignoreErrorReport) { isFail = 2; } - if(opt.dcapi){ + if(opt.dcapi) { logger.debug(logPre + '返回码:' + code + ', isFail:' + isFail); - dcapi.report(Deferred.extend({},opt.dcapi,{ + dcapi.report(Deferred.extend({}, opt.dcapi, { toIp : opt.ip, code : code, isFail : isFail, @@ -429,27 +429,27 @@ Ajax.prototype.doRequest = function(opt){ } //解决undefined报错问题 - for(key in opt.headers){ + for(key in opt.headers) { v = opt.headers[key]; - if(v === undefined){ - logger.debug('delete header: ${key}',{ + if(v === undefined) { + logger.debug('delete header: ${key}', { key: key }); delete opt.headers[key]; continue; } - if(v === null){ - logger.debug('delete header: ${key}',{ + if(v === null) { + logger.debug('delete header: ${key}', { key: key }); delete opt.headers[key]; continue; } - if(key.indexOf(' ') >= 0){ - logger.debug('delete header: ${key}',{ + if(key.indexOf(' ') >= 0) { + logger.debug('delete header: ${key}', { key: key }); delete opt.headers[key]; @@ -458,26 +458,26 @@ Ajax.prototype.doRequest = function(opt){ v = httpUtil.filterInvalidHeaderChar(v); - if(v !== opt.headers[key]){ + if(v !== opt.headers[key]) { opt.headers[key] = v; - logger.debug('find invalid characters in header: ${key}',{ + logger.debug('find invalid characters in header: ${key}', { key: key }); } } - if((httpUtil.isPostLike(opt.type)) && opt.dataType === 'proxy' && this._proxyRequest){ + if((httpUtil.isPostLike(opt.type)) && opt.dataType === 'proxy' && this._proxyRequest) { - if(this._proxyRequest.REQUEST.body !== undefined){ + if(this._proxyRequest.REQUEST.body !== undefined) { opt.body = this._proxyRequest.REQUEST.body; }else{ - opt.send = function(request){ - that._proxyRequest.on('data',function(buffer){ + opt.send = function(request) { + that._proxyRequest.on('data', function(buffer) { request.write(buffer); }); - that._proxyRequest.once('end',function(buffer){ + that._proxyRequest.once('end', function(buffer) { that._proxyRequest.removeAllListeners('data'); request.end(); }); @@ -485,33 +485,33 @@ Ajax.prototype.doRequest = function(opt){ } } - if(httpUtil.isGetLike(opt.type) && opt.headers['content-length']){ + if(httpUtil.isGetLike(opt.type) && opt.headers['content-length']) { logger.debug('reset Content-Length: 0 , origin: ' + opt.headers['content-length']); delete opt.headers['content-length']; delete opt.headers['content-type']; opt.body = null; - }else if(opt.body){ - if(!Buffer.isBuffer(opt.body)){ - opt.body = Buffer.from(opt.body,'UTF-8'); + }else if(opt.body) { + if(!Buffer.isBuffer(opt.body)) { + opt.body = Buffer.from(opt.body, 'UTF-8'); } opt.headers['Content-Length'] = opt.body.length; } - if(opt.protocol === 'https:'){ + if(opt.protocol === 'https:') { //https不走代理 opt.proxyIp = null; opt.proxyPort= null; } //过滤特殊字符 - if(httpUtil.checkInvalidHeaderChar(opt.path)){ + if(httpUtil.checkInvalidHeaderChar(opt.path)) { opt.path = encodeURI(opt.path); } - logger.debug(logPre + '${type} ${dataType} ~ ${ip}:${port} ${protocol}//${host}${path}',{ + logger.debug(logPre + '${type} ${dataType} ~ ${ip}:${port} ${protocol}//${host}${path}', { protocol : opt.protocol, type : opt.type, dataType : opt.dataType, @@ -521,21 +521,21 @@ Ajax.prototype.doRequest = function(opt){ path : opt.path }); - if(opt.proxyPort){ + if(opt.proxyPort) { opt.proxyPath = 'http://' + opt.host + opt.path; } - if(opt.agent){ + if(opt.agent) { currAgent = opt.agent; - }else if(opt.keepAlive === 'https'){ - if(opt.protocol === 'https:'){ + }else if(opt.keepAlive === 'https') { + if(opt.protocol === 'https:') { currAgent = optionsUtil.getHttpsAgent(opt.headers.host); }else{ currAgent = false; } - }else if(opt.keepAlive){ - if(opt.protocol === 'https:'){ + }else if(opt.keepAlive) { + if(opt.protocol === 'https:') { currAgent = optionsUtil.getHttpsAgent(opt.headers.host); }else{ currAgent = optionsUtil.getHttpAgent(opt.headers.host); @@ -556,7 +556,7 @@ Ajax.prototype.doRequest = function(opt){ request.setNoDelay(true); request.setSocketKeepAlive(true); - defer.always(function(){ + defer.always(function() { clearTimeout(tid); request.removeAllListeners(); @@ -566,28 +566,28 @@ Ajax.prototype.doRequest = function(opt){ tid = null; }); - tid = setTimeout(function(){ + tid = setTimeout(function() { logger.debug(logPre + 'timeout: ${timeout}', { timeout : opt.timeout }); request.abort(); request.emit('fail'); - },opt.timeout); + }, opt.timeout); - request.once('error',function(err){ - request.emit('fail',err); + request.once('error', function(err) { + request.emit('fail', err); }); - request.once('fail',function(err){ + request.once('fail', function(err) { - if(defer.isRejected() || defer.isResolved()){ + if(defer.isRejected() || defer.isResolved()) { return; } times.end = new Date().getTime(); - if(err){ - logger.error(logPre + '[${userIp}] ${type} error ~ ${ip}:${port} ${protocol}//${host}${path} ' + err.stack,{ + if(err) { + logger.error(logPre + '[${userIp}] ${type} error ~ ${ip}:${port} ${protocol}//${host}${path} ' + err.stack, { protocol : opt.protocol, type : opt.type, dataType : opt.dataType, @@ -597,7 +597,7 @@ Ajax.prototype.doRequest = function(opt){ path : opt.path, userIp : httpUtil.getUserIp() }); - report(opt,1,502); + report(opt, 1, 502); defer.reject({ opt: opt, hasError: true, @@ -610,8 +610,8 @@ Ajax.prototype.doRequest = function(opt){ return; } - if(window.response && window.response.__hasClosed){ - logger.error(logPre + '[${userIp}] ${type} error ~ ${ip}:${port} ${protocol}//${host}${path} socket closed',{ + if(window.response && window.response.__hasClosed) { + logger.error(logPre + '[${userIp}] ${type} error ~ ${ip}:${port} ${protocol}//${host}${path} socket closed', { protocol : opt.protocol, type : opt.type, dataType : opt.dataType, @@ -621,7 +621,7 @@ Ajax.prototype.doRequest = function(opt){ path : opt.path, userIp : httpUtil.getUserIp() }); - report(opt,2,600 + opt.retry); + report(opt, 2, 600 + opt.retry); defer.reject({ opt: opt, hasError: true, @@ -633,9 +633,9 @@ Ajax.prototype.doRequest = function(opt){ return; } - report(opt,1,513 + opt.retry); + report(opt, 1, 513 + opt.retry); - logger.error(logPre + '[${userIp}] ${type} error ~ ${ip}:${port} ${protocol}//${host}${path}',{ + logger.error(logPre + '[${userIp}] ${type} error ~ ${ip}:${port} ${protocol}//${host}${path}', { protocol : opt.protocol, type : opt.type, dataType : opt.dataType, @@ -647,7 +647,7 @@ Ajax.prototype.doRequest = function(opt){ }); //限制次数,1s只放过一个 - if(opt.retry > 0 && times.end - lastRetry > 1000){ + if(opt.retry > 0 && times.end - lastRetry > 1000) { lastRetry = times.end; opt.retry = opt.retry - 1; @@ -655,11 +655,11 @@ Ajax.prototype.doRequest = function(opt){ logger.debug('retry: ' + opt.retry); opt.isRetriedRequest = true; - that.request(opt).done(function(d){ + that.request(opt).done(function(d) { d.hasRetry = true; defer.resolve(d); - }).fail(function(d){ + }).fail(function(d) { d.hasRetry = true; defer.reject(d); @@ -667,7 +667,7 @@ Ajax.prototype.doRequest = function(opt){ return; }else{ - report(opt,1,513 + opt.retry); + report(opt, 1, 513 + opt.retry); } defer.reject({ @@ -681,18 +681,18 @@ Ajax.prototype.doRequest = function(opt){ }); - request.once('response',function(response){ + request.once('response', function(response) { var result = []; var pipe = response; var isProxy = false; - if(opt.dataType === 'proxy' && that._proxyResponse){ + if(opt.dataType === 'proxy' && that._proxyResponse) { isProxy = true; } - if(currRetry != opt.retry){ + if(currRetry != opt.retry) { //防止第一个请求又回包捣乱 - logger.debug('currRetry: ${currRetry}, opt.retry: ${retry}',{ + logger.debug('currRetry: ${currRetry}, opt.retry: ${retry}', { currRetry: currRetry, retry: opt.retry }); @@ -713,23 +713,23 @@ Ajax.prototype.doRequest = function(opt){ times.response = new Date().getTime(); - if(opt.dataType === 'proxy'){ - if(response.statusCode >= 500 && response.statusCode <= 599 && response.statusCode !== 501){ - logger.debug(logPre + '${ip}:${port} response ${statusCode} cost:${cost}ms ${encoding}\nrequest: ${headers}\nresponse: ${resHeaders}',{ + if(opt.dataType === 'proxy') { + if(response.statusCode >= 500 && response.statusCode <= 599 && response.statusCode !== 501) { + logger.debug(logPre + '${ip}:${port} response ${statusCode} cost:${cost}ms ${encoding}\nrequest: ${headers}\nresponse: ${resHeaders}', { ip: opt.remoteAddress, port: opt.remotePort, statusCode: response.statusCode, - headers: JSON.stringify(opt.headers,null,2), - resHeaders: JSON.stringify(response.headers,null,2), + headers: JSON.stringify(opt.headers, null, 2), + resHeaders: JSON.stringify(response.headers, null, 2), encoding: response.headers['content-encoding'], cost: +new Date() - times.start }); }else{ - logger.debug(logPre + '${ip}:${port} response ${statusCode} cost:${cost}ms ${encoding}\nresponse ${statusCode} ${resHeaders} ',{ + logger.debug(logPre + '${ip}:${port} response ${statusCode} cost:${cost}ms ${encoding}\nresponse ${statusCode} ${resHeaders} ', { ip: opt.remoteAddress, port: opt.remotePort, statusCode: response.statusCode, - resHeaders: JSON.stringify(response.headers,null,2), + resHeaders: JSON.stringify(response.headers, null, 2), encoding: response.headers['content-encoding'], cost: +new Date() - times.start }); @@ -740,8 +740,8 @@ Ajax.prototype.doRequest = function(opt){ || opt.statusCode === 206 || opt.statusCode === 666 || opt.dataType === response.statusCode - ){ - logger.debug(logPre + '${ip}:${port} response ${statusCode} cost:${cost}ms ${encoding}',{ + ) { + logger.debug(logPre + '${ip}:${port} response ${statusCode} cost:${cost}ms ${encoding}', { ip: opt.remoteAddress, port: opt.remotePort, statusCode: response.statusCode, @@ -749,19 +749,19 @@ Ajax.prototype.doRequest = function(opt){ cost: +new Date() - times.start }); }else{ - logger.debug(logPre + '${ip}:${port} response ${statusCode} cost:${cost}ms ${encoding}\nrequest: ${headers}\nresponse: ${resHeaders}',{ + logger.debug(logPre + '${ip}:${port} response ${statusCode} cost:${cost}ms ${encoding}\nrequest: ${headers}\nresponse: ${resHeaders}', { ip: opt.remoteAddress, port: opt.remotePort, statusCode: response.statusCode, - headers: JSON.stringify(opt.headers,null,2), - resHeaders: JSON.stringify(response.headers,null,2), + headers: JSON.stringify(opt.headers, null, 2), + resHeaders: JSON.stringify(response.headers, null, 2), encoding: response.headers['content-encoding'], cost: +new Date() - times.start }); } } - if(defer.isRejected() || defer.isResolved()){ + if(defer.isRejected() || defer.isResolved()) { logger.debug(logPre + 'isRejected: ${isRejected}, isResolved: ${isResolved}', { isRejected: defer.isRejected(), isResolved: defer.isResolved() @@ -769,52 +769,52 @@ Ajax.prototype.doRequest = function(opt){ return; } - if(typeof opt.response === 'function'){ - if(opt.response(response) === false){ + if(typeof opt.response === 'function') { + if(opt.response(response) === false) { //中断处理流程 return; } } - if(isProxy){ - if(httpUtil.isSent(that._proxyResponse)){ + if(isProxy) { + if(httpUtil.isSent(that._proxyResponse)) { logger.debug('proxy end'); return; } } - if(opt.dataType === 'buffer' || (isProxy && response.headers['content-encoding']) || response.headers['content-length'] == 0){ + if(opt.dataType === 'buffer' || (isProxy && response.headers['content-encoding']) || response.headers['content-length'] == 0) { logger.debug(logPre + 'response type: buffer'); }else{ - if(response.headers['content-encoding'] === 'gzip'){ + if(response.headers['content-encoding'] === 'gzip') { pipe = zlib.createGunzip(); - response.on('data',function(buffer){ + response.on('data', function(buffer) { pipe.write(buffer); }); - response.once('end',function(){ + response.once('end', function() { pipe.end(); }); - }else if(response.headers['content-encoding'] === 'deflate'){ + }else if(response.headers['content-encoding'] === 'deflate') { pipe = zlib.createInflateRaw(); - response.on('data',function(buffer){ + response.on('data', function(buffer) { pipe.write(buffer); }); - response.once('end',function(){ + response.once('end', function() { pipe.end(); }); } } - if(isProxy){ + if(isProxy) { - if(response.headers['transfer-encoding'] !== 'chunked'){ + if(response.headers['transfer-encoding'] !== 'chunked') { that._proxyResponse.useChunkedEncodingByDefault = false; } //如果是测试环境,增加一个proxy头以便识别ip - if(opt.ip && config.isTest){ + if(opt.ip && config.isTest) { that._proxyResponse.setHeader('Proxy-Domain-Ip', opt.ip); } if(config.isTest && response.headers['cache-control']) { @@ -827,7 +827,7 @@ Ajax.prototype.doRequest = function(opt){ (response.headers['content-type'].indexOf('text/') === 0 || response.headers['content-type'] === 'application/x-javascript' || response.headers['content-type'] === 'x-json') - ){ + ) { //自带压缩功能 delete response.headers['transfer-encoding']; @@ -844,14 +844,14 @@ Ajax.prototype.doRequest = function(opt){ }else{ delete response.headers['connection']; - that._proxyResponse.writeHead(response.statusCode,httpUtil.formatHeader(response.headers)); + that._proxyResponse.writeHead(response.statusCode, httpUtil.formatHeader(response.headers)); } } pipe.timeStart = Date.now(); pipe.timeCurr = pipe.timeStart; - pipe.on('data',function(chunk){ + pipe.on('data', function(chunk) { // var cost = Date.now() - pipe.timeCurr; pipe.timeCurr = Date.now(); @@ -864,7 +864,7 @@ Ajax.prototype.doRequest = function(opt){ response._bodySize += chunk.length; - if(opt.maxBodySize > 0 && response._bodySize > opt.maxBodySize){ + if(opt.maxBodySize > 0 && response._bodySize > opt.maxBodySize) { logger.debug(logPre + 'request abort(body size too large) size:${len},max:${max}', { len: response._bodySize, max:opt.maxBodySize @@ -874,8 +874,8 @@ Ajax.prototype.doRequest = function(opt){ return; } - if(isProxy){ - if(!that._proxyResponse.finished){ + if(isProxy) { + if(!that._proxyResponse.finished) { that._proxyResponse.write(chunk); } @@ -884,15 +884,15 @@ Ajax.prototype.doRequest = function(opt){ } }); - pipe.once('close',function(){ + pipe.once('close', function() { logger.debug(logPre + 'close'); this.emit('done'); }); - pipe.once('end',function(){ + pipe.once('end', function() { var cost = Date.now() - pipe.timeStart; - logger.debug('${logPre}end:${size},\treceive data cost: ${cost}ms',{ + logger.debug('${logPre}end:${size},\treceive data cost: ${cost}ms', { logPre: logPre, cost: cost, size: response._bodySize @@ -901,17 +901,17 @@ Ajax.prototype.doRequest = function(opt){ this.emit('done'); }); - pipe.once('done',function(){ + pipe.once('done', function() { - var obj,responseText,buffer,code; - var key,Content; + var obj, responseText, buffer, code; + var key, Content; this.removeAllListeners('close'); this.removeAllListeners('end'); this.removeAllListeners('data'); this.removeAllListeners('done'); - if(defer.isRejected() || defer.isResolved()){ + if(defer.isRejected() || defer.isResolved()) { return; } @@ -923,11 +923,11 @@ Ajax.prototype.doRequest = function(opt){ len: response._bodySize }); - if(isProxy){ + if(isProxy) { if (response.statusCode >= 500 && response.statusCode <= 599 && response.statusCode !== 501) { report(opt, 1, response.statusCode); }else{ - report(opt,0,response.statusCode); + report(opt, 0, response.statusCode); } that._proxyResponse.end(); @@ -947,8 +947,8 @@ Ajax.prototype.doRequest = function(opt){ return; } - if(opt.dataType === response.statusCode){ - report(opt,0,response.statusCode); + if(opt.dataType === response.statusCode) { + report(opt, 0, response.statusCode); defer.resolve({ opt: opt, buffer: buffer, @@ -963,34 +963,34 @@ Ajax.prototype.doRequest = function(opt){ return; } - if(opt.dataType === 'json' || opt.dataType === 'jsonp' || opt.dataType === 'text' || opt.dataType === 'html'){ + if(opt.dataType === 'json' || opt.dataType === 'jsonp' || opt.dataType === 'text' || opt.dataType === 'html') { responseText = buffer.toString('UTF-8'); } - if(buffer.length <= 1024){ - if(responseText){ - if(opt.dataType === 'json' || opt.dataType === 'jsonp' || opt.dataType === 'text'){ + if(buffer.length <= 1024) { + if(responseText) { + if(opt.dataType === 'json' || opt.dataType === 'jsonp' || opt.dataType === 'text') { logger.debug(logPre + 'responseText:\n' + responseText); } - }else if(/charset=utf-8/i.test(response.headers['Content-Type'])){ + }else if(/charset=utf-8/i.test(response.headers['Content-Type'])) { logger.debug(logPre + 'responseText:\n' + buffer.toString('UTF-8')); } } - if(responseText){ + if(responseText) { buffer = null; } - if(response.statusCode !== 200 && response.statusCode !== 666 && response.statusCode !== 206){ + if(response.statusCode !== 200 && response.statusCode !== 666 && response.statusCode !== 206) { //dataType为proxy但是不走代理模式的时候,30x类型的返回码当作成功上报 - if(response.statusCode >= 300 && response.statusCode < 400){ - if(opt.dataType === 'proxy'){ - report(opt,0,response.statusCode); + if(response.statusCode >= 300 && response.statusCode < 400) { + if(opt.dataType === 'proxy') { + report(opt, 0, response.statusCode); }else{ - report(opt,2,response.statusCode); + report(opt, 2, response.statusCode); } }else{ - report(opt,1,response.statusCode); + report(opt, 1, response.statusCode); } defer.reject({ @@ -1007,15 +1007,15 @@ Ajax.prototype.doRequest = function(opt){ return; } - if(opt.dataType === 'json' || opt.dataType === 'jsonp'){ + if(opt.dataType === 'json' || opt.dataType === 'jsonp') { try{ - if(opt.jsonpCallback){ + if(opt.jsonpCallback) { //json|jsonp code = `var result=null; var ${opt.jsonpCallback}=function($1){result=$1}; ${responseText}; return result;`; obj = new sbFunction(code)(); - }else if(opt.dataType === 'json' && opt.useJsonParseOnly){ + }else if(opt.dataType === 'json' && opt.useJsonParseOnly) { //json only obj = JSON.parse(responseText); }else{ @@ -1023,34 +1023,34 @@ Ajax.prototype.doRequest = function(opt){ code = `return (${responseText})`; try{ obj = new sbFunction(code)(); - }catch(e){ + }catch(e) { //尝试JSON.parse obj = JSON.parse(responseText); } } - }catch(e){ + }catch(e) { let parseErr = e; - if(e && code){ + if(e && code) { try{ - code = code.replace(/[\u2028\u2029]/g,''); + code = code.replace(/[\u2028\u2029]/g, ''); obj = new sbFunction(code)(); parseErr = null; - }catch(err){ + }catch(err) { logger.error(`parse response body fail ${err.message}`); } } - if(parseErr){ - logger.error(logPre + 'parse error: ${error} \n\nrequest: ${headers}\nresponse: ${resHeaders}\n\n${responseTextU8}',{ + if(parseErr) { + logger.error(logPre + 'parse error: ${error} \n\nrequest: ${headers}\nresponse: ${resHeaders}\n\n${responseTextU8}', { error: parseErr.stack, - headers: JSON.stringify(opt.headers,null,2), - resHeaders: JSON.stringify(response.headers,null,2), - responseTextU8: JSON.stringify(responseText,null,2), + headers: JSON.stringify(opt.headers, null, 2), + resHeaders: JSON.stringify(response.headers, null, 2), + responseTextU8: JSON.stringify(responseText, null, 2), }); - report(opt,1,508); + report(opt, 1, 508); defer.reject({ opt: opt, buffer: buffer, @@ -1063,7 +1063,7 @@ Ajax.prototype.doRequest = function(opt){ times: times }); - key = [window.request.headers.host,context.mod_act,parseErr.message].join(':'); + key = [window.request.headers.host, context.mod_act, parseErr.message].join(':'); Content = [ '

错误堆栈

', @@ -1072,7 +1072,7 @@ Ajax.prototype.doRequest = function(opt){ '

', ].join(''); - require('util/mail/mail.js').SendMail(key,'js data',1800,{ + require('util/mail/mail.js').SendMail(key, 'js data', 1800, { 'Title' : key, 'runtimeType' : 'ParseError', 'Content' : Content, @@ -1084,7 +1084,7 @@ Ajax.prototype.doRequest = function(opt){ } - if(obj){ + if(obj) { code = obj.code || 0; } @@ -1097,32 +1097,32 @@ Ajax.prototype.doRequest = function(opt){ obj = responseText; } - if(typeof opt.formatCode === 'function'){ - code = opt.formatCode(obj,opt,response); + if(typeof opt.formatCode === 'function') { + code = opt.formatCode(obj, opt, response); } //支持{isFail:0,code:1,message:''} - if(typeof code === 'object'){ + if(typeof code === 'object') { - if(typeof code.isFail !== 'number'){ + if(typeof code.isFail !== 'number') { code.isFail = ~~code.isFail; } - if(typeof code.code !== 'number'){ + if(typeof code.code !== 'number') { code.code = ~~code.code; } - report(opt,code.isFail,code.code); + report(opt, code.isFail, code.code); }else{ - if(typeof code !== 'number'){ + if(typeof code !== 'number') { code = ~~code; } - if(code === 0){ - report(opt,0,code); + if(code === 0) { + report(opt, 0, code); }else{ - report(opt,2,code); + report(opt, 2, code); } } @@ -1142,16 +1142,16 @@ Ajax.prototype.doRequest = function(opt){ }); - if(opt.send){ + if(opt.send) { opt.send(request); }else{ - if(opt.body){ + if(opt.body) { request.useChunkedEncodingByDefault = false; try{ request.write(opt.body); - }catch(e){ + }catch(e) { logger.info(e.stack); } } diff --git a/bin/tsw/ajax/form.js b/bin/tsw/ajax/form.js index 7ef3859f..ddae1add 100644 --- a/bin/tsw/ajax/form.js +++ b/bin/tsw/ajax/form.js @@ -55,7 +55,7 @@ function fileField(boundary, key, value = {}) { let buffer = Buffer.from(tmp.join('\r\n')); - if(Buffer.isBuffer(value.content)){ + if(Buffer.isBuffer(value.content)) { buffer = Buffer.concat([buffer, value.content, Buffer.from('\r\n')]); }else{ buffer = Buffer.concat([buffer, Buffer.from(value.content || ''), Buffer.from('\r\n')]); @@ -76,13 +76,13 @@ function getFormBuffer(opt = {}) { let v; - for(let key in opt.data){ + for(let key in opt.data) { v = opt.data[key]; - if(v !== undefined && v !== null){ + if(v !== undefined && v !== null) { - if(v.fileType){ + if(v.fileType) { buffer = Buffer.concat([buffer, fileField(opt.boundary, key, v)]); }else{ buffer = Buffer.concat([buffer, textField(opt.boundary, key, v)]); diff --git a/bin/tsw/ajax/http-https.options.js b/bin/tsw/ajax/http-https.options.js index bc6b207e..232be913 100644 --- a/bin/tsw/ajax/http-https.options.js +++ b/bin/tsw/ajax/http-https.options.js @@ -14,11 +14,11 @@ const cache = {}; ///etc/pki/tls/certs/ca-bundle.crt -this.getHttpsAgent = function(host){ +this.getHttpsAgent = function(host) { - var key = ['https',host].join('.'); + var key = ['https', host].join('.'); - if(!cache[key]){ + if(!cache[key]) { cache[key] = new https.Agent({ maxSockets : 65535, maxFreeSockets : 32, @@ -31,11 +31,11 @@ this.getHttpsAgent = function(host){ return cache[key]; }; -this.getHttpAgent = function(host){ +this.getHttpAgent = function(host) { - var key = ['http',host].join('.'); + var key = ['http', host].join('.'); - if(!cache[key]){ + if(!cache[key]) { cache[key] = new http.Agent({ maxSockets : 65535, maxFreeSockets : 32, diff --git a/bin/tsw/ajax/token.js b/bin/tsw/ajax/token.js index a25cfad0..2a504df1 100644 --- a/bin/tsw/ajax/token.js +++ b/bin/tsw/ajax/token.js @@ -7,15 +7,15 @@ */ 'use strict'; -this.token = function(skey){ +this.token = function(skey) { var str = skey || ''; var hash = 5381; - if(typeof context !== 'undefined'){ + if(typeof context !== 'undefined') { let window = context.window || {}; - if(window.request){ + if(window.request) { str = str || window.request.cookies.p_skey || window.request.cookies.skey @@ -25,7 +25,7 @@ this.token = function(skey){ } } - for(var i = 0, len = str.length; i < len; ++i){ + for(var i = 0, len = str.length; i < len; ++i) { hash += (hash << 5) + str.charAt(i).charCodeAt(); } return hash & 0x7fffffff; diff --git a/bin/tsw/api/date.js b/bin/tsw/api/date.js index 8dd4af80..c91c728f 100644 --- a/bin/tsw/api/date.js +++ b/bin/tsw/api/date.js @@ -15,7 +15,7 @@ var Date = { * @param {Date} mDate 时间对象 * @param {String} fmt 格式化形式,如 MM月dd日 HH:mm */ - format: function(mDate, fmt){ + format: function(mDate, fmt) { var o = { 'M+': mDate.getMonth() + 1, //月份 'D+': mDate.getDate(), //日 diff --git a/bin/tsw/api/fileCache/index.js b/bin/tsw/api/fileCache/index.js index e1f78218..ba9e72ef 100644 --- a/bin/tsw/api/fileCache/index.js +++ b/bin/tsw/api/fileCache/index.js @@ -11,40 +11,40 @@ const Deferred = require('util/Deferred'); const logger = require('logger'); const path = require('path'); const fs = require('fs'); -const root = path.join(__dirname,'../../../../log/cache'); +const root = path.join(__dirname, '../../../../log/cache'); const tnm2 = require('api/tnm2'); const existsCache = {}; -this.getDir = function(filepath){ +this.getDir = function(filepath) { - var res,tmp; + var res, tmp; filepath = filepath || ''; - if(!/^[a-z0-9_/\-.\\:]+$/i.test(filepath)){ + if(!/^[a-z0-9_/\-.\\:]+$/i.test(filepath)) { throw new Error('filepath not safe: ' + filepath); } - if(filepath.indexOf('http://') > -1){ - filepath = filepath.replace('http://',''); + if(filepath.indexOf('http://') > -1) { + filepath = filepath.replace('http://', ''); } - tmp = path.join('/',filepath); - res = path.join(root,tmp); + tmp = path.join('/', filepath); + res = path.join(root, tmp); - res = res.replace(/\\/g,'/'); + res = res.replace(/\\/g, '/'); return res; }; -this.mkdir = function(dirname){ +this.mkdir = function(dirname) { - if(existsCache[dirname]){ + if(existsCache[dirname]) { return; } - if(fs.existsSync(dirname)){ + if(fs.existsSync(dirname)) { existsCache[dirname] = true; return; } @@ -53,15 +53,15 @@ this.mkdir = function(dirname){ var i = 0; var curr; - for(i = 1; i < arr.length; i++){ - curr = arr.slice(0,i + 1).join('/'); - if(!fs.existsSync(curr)){ - logger.debug('mkdir : ${dir}',{ + for(i = 1; i < arr.length; i++) { + curr = arr.slice(0, i + 1).join('/'); + if(!fs.existsSync(curr)) { + logger.debug('mkdir : ${dir}', { dir: curr }); try{ - fs.mkdirSync(curr,0o777); - }catch(e){ + fs.mkdirSync(curr, 0o777); + }catch(e) { logger.info(e.stack); } @@ -69,7 +69,7 @@ this.mkdir = function(dirname){ } }; -this.set = function(filepath,data){ +this.set = function(filepath, data) { var start = Date.now(); var buffer = null; var filename = this.getDir(filepath); @@ -77,50 +77,50 @@ this.set = function(filepath,data){ var basename = path.basename(filename); var randomname = basename + '.tmp.' + String(Math.random()).slice(2); - logger.debug('[set] filename : ${filename}',{ + logger.debug('[set] filename : ${filename}', { filename: filename }); - logger.debug('[set] dirname : ${dirname}',{ + logger.debug('[set] dirname : ${dirname}', { dirname: dirname }); - logger.debug('[set] basename : ${basename}',{ + logger.debug('[set] basename : ${basename}', { basename: basename }); - logger.debug('[set] randomname : ${randomname}',{ + logger.debug('[set] randomname : ${randomname}', { randomname: randomname }); this.mkdir(dirname); - if(typeof data === 'string'){ - buffer = Buffer.from(data,'UTF-8'); + if(typeof data === 'string') { + buffer = Buffer.from(data, 'UTF-8'); }else{ buffer = data; } - fs.writeFile([dirname,randomname].join('/'),buffer,{mode:0o666},function(err){ + fs.writeFile([dirname, randomname].join('/'), buffer, {mode:0o666}, function(err) { - logger.debug('[write] done: ${randomname}, size: ${size}',{ + logger.debug('[write] done: ${randomname}, size: ${size}', { randomname: randomname, size: buffer.length }); - if(err){ + if(err) { logger.info(err.stack); return; } - fs.rename([dirname,randomname].join('/'),[dirname,basename].join('/'),function(err){ + fs.rename([dirname, randomname].join('/'), [dirname, basename].join('/'), function(err) { var end = Date.now(); - if(err){ + if(err) { logger.debug(err); } - logger.debug('[rename] done: ${basename}, cost: ${cost}ms',{ + logger.debug('[rename] done: ${basename}, cost: ${cost}ms', { basename: basename, cost: end - start }); @@ -131,7 +131,7 @@ this.set = function(filepath,data){ tnm2.Attr_API('SUM_TSW_FILECACHE_WRITE', 1); }; -this.getSync = function(filepath){ +this.getSync = function(filepath) { var start = Date.now(); var buffer = null; @@ -143,7 +143,7 @@ this.getSync = function(filepath){ try{ buffer = fs.readFileSync(filename); - if(buffer.length === 0){ + if(buffer.length === 0) { //长度为0返回空,因为垃圾清理逻辑会清空文件 buffer = null; } @@ -151,13 +151,13 @@ this.getSync = function(filepath){ res.stats = fs.statSync(filename); res.data = buffer; - }catch(err){ + }catch(err) { buffer = null; } var end = Date.now(); - logger.debug('[get] done: ${filename}, size: ${size}, cost: ${cost}ms',{ + logger.debug('[get] done: ${filename}, size: ${size}, cost: ${cost}ms', { filename: filename, size: buffer && buffer.length, cost: end - start @@ -167,7 +167,7 @@ this.getSync = function(filepath){ return res; }; -this.get = this.getAsync = function(filepath){ +this.get = this.getAsync = function(filepath) { var defer = Deferred.create(); var filename = this.getDir(filepath); @@ -178,13 +178,13 @@ this.get = this.getAsync = function(filepath){ data: null }; - logger.debug('[getAsync] ${filename}',{ + logger.debug('[getAsync] ${filename}', { filename: filename }); - fs.readFile(filename,function(err, buffer){ + fs.readFile(filename, function(err, buffer) { - if(err){ + if(err) { logger.debug(err.stack); defer.resolve(res); return; @@ -192,15 +192,15 @@ this.get = this.getAsync = function(filepath){ res.data = buffer; - fs.stat(filename,function(err,stats){ + fs.stat(filename, function(err, stats) { - if(err){ + if(err) { logger.debug(err.stack); defer.resolve(res); return; } - logger.debug('[getAsync] mtime: ${mtime}, size: ${size}',stats); + logger.debug('[getAsync] mtime: ${mtime}, size: ${size}', stats); res.stats = stats; defer.resolve(res); @@ -212,17 +212,17 @@ this.get = this.getAsync = function(filepath){ }; -this.updateMtime = function(filepath, atime, mtime){ +this.updateMtime = function(filepath, atime, mtime) { var filename = this.getDir(filepath); - logger.debug('[updateMtime] ${filename}',{ + logger.debug('[updateMtime] ${filename}', { filename: filename }); - fs.utimes(filename, atime || new Date(), mtime || new Date(), function(err){ - if(err){ + fs.utimes(filename, atime || new Date(), mtime || new Date(), function(err) { + if(err) { logger.debug(err.stack); return; } diff --git a/bin/tsw/api/logman/index.js b/bin/tsw/api/logman/index.js index 47dc5c1f..75a0f42a 100644 --- a/bin/tsw/api/logman/index.js +++ b/bin/tsw/api/logman/index.js @@ -18,15 +18,15 @@ const backupDir = path.resolve(logDir, './backup/').replace(/\\/g, '/'); const runlogPath = path.resolve(logDir, './run.log.0').replace(/\\/g, '/'); //判断logDir目录是否存在 -fs.exists(logDir, function(exists){ +fs.exists(logDir, function(exists) { if (!exists) { - fs.mkdirSync(logDir,0o777); + fs.mkdirSync(logDir, 0o777); } //判断backup目录是否存在 - fs.exists(backupDir, function(exists){ + fs.exists(backupDir, function(exists) { if (!exists) { - fs.mkdirSync(backupDir,0o777); + fs.mkdirSync(backupDir, 0o777); } }); }); @@ -45,12 +45,12 @@ var LogMan = { /** * 启动log管理 */ - start: function(config){ + start: function(config) { logger.info('start log manager'); var self = this; this.delayType = config.delay || 'D'; this.delay = this.delayMap[this.delayType]; - this.timer = setInterval(function(){ + this.timer = setInterval(function() { self.backLog(); }, this.delay); }, @@ -58,12 +58,12 @@ var LogMan = { /** * 备份log */ - backLog: function(){ + backLog: function() { logger.info('start backup log'); var self = this; var curBackupDir = path.resolve(backupDir, './' + dateApi.format(new Date, 'YYYY-MM-DD')); - fs.exists(curBackupDir, function(exists){ - if(!exists){ + fs.exists(curBackupDir, function(exists) { + if(!exists) { fs.mkdirSync(curBackupDir); } var logFilePath = path.resolve(curBackupDir, './' + dateApi.format(new Date, self.delayType + self.delayType) + '.log'); @@ -71,7 +71,7 @@ var LogMan = { var cmdClear = 'cat /dev/null > ' + runlogPath; //兼容windows - if(isWindows){ + if(isWindows) { logFilePath = logFilePath.replace(/\\/g, '\\\\'); cmdCat = 'type ' + runlogPath + ' > ' + logFilePath; cmdClear = 'type NUL > ' + runlogPath; @@ -80,7 +80,7 @@ var LogMan = { //backup logger.info('backup: '+ cmdCat); - cp.exec(cmdCat, function(error, stdout, stderr){ + cp.exec(cmdCat, function(error, stdout, stderr) { if (error !== null) { logger.error('cat error, ' + error); } @@ -88,7 +88,7 @@ var LogMan = { //clear logger.info('clear: ' + cmdClear); - cp.exec(cmdClear, function(error, stdout, stderr){ + cp.exec(cmdClear, function(error, stdout, stderr) { if (error !== null) { logger.error('clear error, ' + error); } diff --git a/bin/tsw/config.js b/bin/tsw/config.js index 1c1265ca..0b8d61ee 100644 --- a/bin/tsw/config.js +++ b/bin/tsw/config.js @@ -9,21 +9,21 @@ module.exports = require('../../bin/proxy/config.js'); -if(Object.keys(module.exports).length === 0){ +if(Object.keys(module.exports).length === 0) { let curr = module; /* eslint-disable no-console */ console.error('config加载存在循环引用:'); - while(curr.parent){ + while(curr.parent) { console.error(curr.parent.filename); curr = curr.parent; } /* eslint-enable no-console */ - process.emit('warning','config加载存在循环引用'); + process.emit('warning', 'config加载存在循环引用'); } diff --git a/bin/tsw/context.js b/bin/tsw/context.js index 427634a7..40171ccc 100644 --- a/bin/tsw/context.js +++ b/bin/tsw/context.js @@ -10,28 +10,28 @@ const Context = require('runtime/Context'); const Window = require('runtime/Window'); -this.currentContext = function(){ +this.currentContext = function() { return (process.domain && process.domain.currentContext) || new Context(); }; -if(!global.context){ +if(!global.context) { Object.defineProperty(global, 'context', { - get : function(){ + get : function() { return module.exports.currentContext(); } }); Object.defineProperty(global, 'window', { - get : function(){ + get : function() { - if(Window.windowHasDisabled){ + if(Window.windowHasDisabled) { return undefined; } var curr = module.exports.currentContext(); - if(!curr.window){ + if(!curr.window) { curr.window = new Window(); } diff --git a/bin/tsw/default/config.default.sky.js b/bin/tsw/default/config.default.sky.js index a17b895c..7a72245f 100644 --- a/bin/tsw/default/config.default.sky.js +++ b/bin/tsw/default/config.default.sky.js @@ -27,36 +27,36 @@ this.modAct = this.modMap = { default_page : 'util/static/static.js' }, - getModAct: function(req){ + getModAct: function(req) { var pathname= req.REQUEST.pathname || ''; - var arr = pathname.split('/',3); + var arr = pathname.split('/', 3); var mod = arr[1] || 'default'; var act = arr[2] || 'page'; var mod_act = mod + '_' + act; - if(this.map[pathname]){ + if(this.map[pathname]) { req.__mod_act = pathname; return pathname; } - if(this.map[mod_act]){ + if(this.map[mod_act]) { req.__mod_act = mod_act; return mod_act; } return null; }, - find: function(mod_act,req,res){ + find: function(mod_act, req, res) { mod_act = req.__mod_act; var mod = this.map[mod_act]; - if(mod_act === 'default_page'){ + if(mod_act === 'default_page') { req.REQUEST.pathname = '/static/tsw/index.html'; } - if(mod){ + if(mod) { res.setHeader('Mod-Map', mod_act + ':' + mod); return plug(mod); } diff --git a/bin/tsw/loader/seajs/lib/sea-node.js b/bin/tsw/loader/seajs/lib/sea-node.js index 06d1222c..c6b7458f 100644 --- a/bin/tsw/loader/seajs/lib/sea-node.js +++ b/bin/tsw/loader/seajs/lib/sea-node.js @@ -15,33 +15,33 @@ var moduleStack = []; Module._resolveFilename = function(request, parent) { var res; //request = request.replace(/\?.*$/, '') // remove timestamp etc. - + //性能优化 - if(parent.resolveFilenameCache){ - if(parent.resolveFilenameCache[request]){ + if(parent.resolveFilenameCache) { + if(parent.resolveFilenameCache[request]) { return parent.resolveFilenameCache[request]; } }else{ parent.resolveFilenameCache = {}; } - + res = _resolveFilename(request, parent); - + parent.resolveFilenameCache[request] = res; - + return res; }; Module.prototype._compile = function(content, filename) { moduleStack.push(this); try { - if(filename.indexOf(plug.parent) === 0){ + if(filename.indexOf(plug.parent) === 0) { this.paths = plug.paths.concat(this.paths); } return _compile.call(this, content, filename); - }catch(err){ - process.nextTick(function(){ - process.emit('warning',err); + }catch(err) { + process.nextTick(function() { + process.emit('warning', err); }); throw err; }finally { @@ -83,9 +83,7 @@ global.define = function() { if (ret !== undefined) { module.exports = ret; } - } - // define(object) - else { + } else { module.exports = factory; } }; @@ -114,9 +112,7 @@ function createAsync(module) { done(m.exports, index); }); - } - // local file - else { + } else { done(module.require(id), index); } }); diff --git a/bin/tsw/plug.js b/bin/tsw/plug.js index fa9bca92..819b388c 100644 --- a/bin/tsw/plug.js +++ b/bin/tsw/plug.js @@ -15,20 +15,20 @@ const path = require('path'); * * @param {String} id */ -function plug(id){ +function plug(id) { return require(id); } -if(!global.plug){ +if(!global.plug) { plug.__dirname = __dirname; - plug.parent = path.join(__dirname , '..'); + plug.parent = path.join(__dirname, '..'); plug.paths = [ - path.join(__dirname , '../deps'), - path.join(__dirname , '../tsw'), - path.join(__dirname , '../tencent'), - path.join(__dirname , '../lib') + path.join(__dirname, '../deps'), + path.join(__dirname, '../tsw'), + path.join(__dirname, '../tencent'), + path.join(__dirname, '../lib') ]; module.paths = plug.paths.concat(module.paths); @@ -39,12 +39,12 @@ if(!global.plug){ require('loader/seajs'); require('loader/extentions.js'); - JSON.stringify = function(stringify){ - return function(){ - var str = stringify.apply(this,arguments); + JSON.stringify = function(stringify) { + return function() { + var str = stringify.apply(this, arguments); - if(str && str.indexOf('<') > -1){ - str = str.replace(/ -1) { + str = str.replace(/ 0){ - command = command.slice(0,Math.min(128,index)); + if(index > 0) { + command = command.slice(0, Math.min(128, index)); } - if(command.length >= 128){ - command = command.slice(0,128) + '...' + command.length; + if(command.length >= 128) { + command = command.slice(0, 128) + '...' + command.length; } logger.debug(command); - query.callback = function(callback){ - return function(...args){ + query.callback = function(callback) { + return function(...args) { var err = args[0]; var code = 0; var isFail = 0; var delay = Date.now() - start; var toIp = servers.split(':')[0]; - if(err && err.message !== 'Item is not stored'){ - if(err.stack){ + if(err && err.message !== 'Item is not stored') { + if(err.stack) { logger.error(command); logger.error(servers); logger.error(err.stack); @@ -128,14 +128,14 @@ function queueWrap(memcached){ }); queue.dequeue(); - return callback && callback.apply(this,args); + return callback && callback.apply(this, args); }; }(query.callback); return query; }; })(queryCompiler); - command.call(memcached,fn, server); + command.call(memcached, fn, server); }); }; }(memcached.command); diff --git a/bin/tsw/router.js b/bin/tsw/router.js index 28e81707..6e5981c5 100644 --- a/bin/tsw/router.js +++ b/bin/tsw/router.js @@ -11,23 +11,23 @@ const logger = require('logger'); const httpRoute = require('../proxy/http.route.js'); const parseGet = require('util/http/parseGet.js'); -this.route = function(url,mod_act){ +this.route = function(url, mod_act) { - var req,res; + var req, res; var window = context.window || {}; req = window.request; res = window.response; - if(!req){ + if(!req) { return; } res.removeAllListeners('afterFinish'); - if(url){ + if(url) { - logger.debug('route to : ${url}',{ + logger.debug('route to : ${url}', { url: url }); @@ -36,8 +36,8 @@ this.route = function(url,mod_act){ parseGet(req); } - if(mod_act){ - logger.debug('route to mod_act: ${mod_act}',{ + if(mod_act) { + logger.debug('route to mod_act: ${mod_act}', { mod_act: mod_act }); @@ -46,6 +46,6 @@ this.route = function(url,mod_act){ context.mod_act = null; } - httpRoute.doRoute(req,res); + httpRoute.doRoute(req, res); }; diff --git a/bin/tsw/runtime/CCFinder.js b/bin/tsw/runtime/CCFinder.js index f532373e..e35344c3 100644 --- a/bin/tsw/runtime/CCFinder.js +++ b/bin/tsw/runtime/CCFinder.js @@ -23,39 +23,39 @@ var cache = { ipCacheLast: {} }; -if(global[__filename]){ +if(global[__filename]) { cache = global[__filename]; }else{ global[__filename] = cache; } -this.addWhiteList = function(userIp){ +this.addWhiteList = function(userIp) { cache.whiteList[userIp] = true; }; -this.checkHost = function(req, res){ +this.checkHost = function(req, res) { var hostAllow = config.allowHost || []; var host = req.headers['host']; - var i,len,v; + var i, len, v; - if(hostAllow.length === 0){ + if(hostAllow.length === 0) { return true; } - if(host === serverInfo.intranetIp){ + if(host === serverInfo.intranetIp) { return true; } - for( i = 0, len = hostAllow.length; i < len; i++){ + for( i = 0, len = hostAllow.length; i < len; i++) { v = hostAllow[i]; - if(typeof v === 'string'){ - if(v === host){ + if(typeof v === 'string') { + if(v === host) { return true; } - }else if(typeof v === 'object'){ - if(v.test && v.test(host)){ + }else if(typeof v === 'object') { + if(v.test && v.test(host)) { return true; } } @@ -71,44 +71,44 @@ this.checkHost = function(req, res){ }; //计算标准方差 -this.StdX10 = function(ipCache){ +this.StdX10 = function(ipCache) { var res = 0; var sum = 0; var avg = 0; - var arr = Object.keys(ipCache).filter(function(item){ + var arr = Object.keys(ipCache).filter(function(item) { var tmp = ipCache[item]; - if(typeof tmp === 'object' && tmp.list){ + if(typeof tmp === 'object' && tmp.list) { sum += tmp.list.length; return true; } return false; }); - if(arr.length <= 1){ + if(arr.length <= 1) { return 0; } avg = sum / arr.length; - var sumXsum = arr.reduce(function(pre,key){ + var sumXsum = arr.reduce(function(pre, key) { var item = ipCache[key]; var value = item.list.length; item.avg = avg; return pre + (value - avg)*(value - avg); - },0); + }, 0); res = parseInt(Math.sqrt(sumXsum / (arr.length - 1)) * 10); return res; }; -this.check = function(req, res){ +this.check = function(req, res) { var userIp = httpUtil.getUserIp(req); var userIp24 = httpUtil.getUserIp24(req); - var key,Content,curr; + var key, Content, curr; var info = { userIp : userIp, @@ -116,26 +116,26 @@ this.check = function(req, res){ pathname : req.REQUEST.pathname }; - if(cache.whiteList[userIp]){ + if(cache.whiteList[userIp]) { return true; } - if(cache.whiteList[userIp24]){ + if(cache.whiteList[userIp24]) { return true; } //忽略TST请求 - if(isTST.isTST(req)){ + if(isTST.isTST(req)) { return true; } - if(!cache.ipCache.start){ + if(!cache.ipCache.start) { cache.ipCache.start = Date.now(); } ipConut = ipConut - 1; - if(ipConut <= 0){ + if(ipConut <= 0) { ipConut = CCIPSize; cache.ipCache.end = Date.now(); cache.ipCache.StdX10= this.StdX10(cache.ipCache); @@ -147,13 +147,13 @@ this.check = function(req, res){ tnm2.Attr_API_Set('AVG_TSW_IP_STD_X10', cache.ipCacheLast.StdX10); } - if(Date.now() - cache.ipCache.start > 60000){ + if(Date.now() - cache.ipCache.start > 60000) { //时间太长 ipConut = 0; return true; } - if(!cache.ipCache[userIp]){ + if(!cache.ipCache[userIp]) { cache.ipCache[userIp] = { ip007Info: null, isSendMail: false, @@ -165,29 +165,29 @@ this.check = function(req, res){ curr.list.push(info); - if(CCIPLimit <= -1){ + if(CCIPLimit <= -1) { return true; } - if(config.isTest){ + if(config.isTest) { //测试环境 return true; } - if(config.devMode){ + if(config.devMode) { //开发环境 return true; } - if(!cache.ipCacheLast.StdX10){ + if(!cache.ipCacheLast.StdX10) { return true; } - if(cache.ipCacheLast.StdX10 <= CCIPLimit){ + if(cache.ipCacheLast.StdX10 <= CCIPLimit) { return true; } - if(cache.ipCacheLast.hasSendMail){ + if(cache.ipCacheLast.hasSendMail) { return true; } @@ -201,7 +201,7 @@ this.check = function(req, res){ Content = ''; - Object.keys(cache.ipCacheLast).forEach(function(ip,i){ + Object.keys(cache.ipCacheLast).forEach(function(ip, i) { var num = ''; @@ -209,14 +209,14 @@ this.check = function(req, res){ cache.ipCacheLast[ip] && cache.ipCacheLast[ip].list && cache.ipCacheLast[ip].list.length > 1 - ){ + ) { num = '' + cache.ipCacheLast[ip].list.length; - num = (num + 'XXXXXX').slice(0,8).replace(/X/g,' '); + num = (num + 'XXXXXX').slice(0, 8).replace(/X/g, ' '); Content += `
${num}${ip}
`; } }); - mail.SendMail(key,'TSW',3600,{ + mail.SendMail(key, 'TSW', 3600, { 'To' : config.mailTo, 'CC' : config.mailCC, 'Title' : `[IP聚集告警][${cache.ipCacheLast.StdX10}%]${serverInfo.intranetIp}`, @@ -234,12 +234,12 @@ this.check = function(req, res){ }; -this.getIpCache = function(){ +this.getIpCache = function() { return cache.ipCacheLast; }; -this.getIpSize = function(){ +this.getIpSize = function() { return CCIPSize; }; diff --git a/bin/tsw/runtime/Console.hack.js b/bin/tsw/runtime/Console.hack.js index 9e2ad3fc..ad11092b 100644 --- a/bin/tsw/runtime/Console.hack.js +++ b/bin/tsw/runtime/Console.hack.js @@ -7,11 +7,11 @@ */ 'use strict'; -if(!global[__filename]){ +if(!global[__filename]) { global[__filename] = true; - process.nextTick(function(){ + process.nextTick(function() { var util = require('util'); var logger = require('logger'); diff --git a/bin/tsw/runtime/Context.js b/bin/tsw/runtime/Context.js index a05cd3cb..e031f336 100644 --- a/bin/tsw/runtime/Context.js +++ b/bin/tsw/runtime/Context.js @@ -10,18 +10,18 @@ module.exports = Context; -function Context(){ +function Context() { } -Context.prototype.setModAct = function(mod_act){ +Context.prototype.setModAct = function(mod_act) { context.mod_act = mod_act; return true; }; -Context.prototype.getModAct = function(){ +Context.prototype.getModAct = function() { return context.mod_act; }; diff --git a/bin/tsw/runtime/Dns.hack.js b/bin/tsw/runtime/Dns.hack.js index 7758e1be..21d57a20 100644 --- a/bin/tsw/runtime/Dns.hack.js +++ b/bin/tsw/runtime/Dns.hack.js @@ -7,11 +7,11 @@ */ 'use strict'; -if(!global[__filename]){ +if(!global[__filename]) { global[__filename] = true; - process.nextTick(function(){ + process.nextTick(function() { var config = require('config'); var dns = require('dns'); var dcapi = require('api/libdcapi/dcapi'); @@ -24,7 +24,7 @@ if(!global[__filename]){ var args = [hostname]; var start = Date.now(); - if(net.isIP(hostname)){ + if(net.isIP(hostname)) { return fn.apply(this, arguments); } @@ -52,7 +52,7 @@ if(!global[__filename]){ isFail = 1; } - if(err){ + if(err) { logger.error('dns lookup error: ' + err.stack); }else{ logger.debug(`dns lookup: ${hostname} --> ${address}`); diff --git a/bin/tsw/runtime/Window.js b/bin/tsw/runtime/Window.js index d65f8b93..40aa2077 100644 --- a/bin/tsw/runtime/Window.js +++ b/bin/tsw/runtime/Window.js @@ -9,15 +9,15 @@ module.exports = Window; -function Window(){ +function Window() { } -Window.prototype.disable = function(){ +Window.prototype.disable = function() { Window.windowHasDisabled = true; }; -Window.prototype.enable = function(){ +Window.prototype.enable = function() { Window.windowHasDisabled = false; }; diff --git a/bin/tsw/runtime/capturer.js b/bin/tsw/runtime/capturer.js index 5c1d215d..7c77f00a 100644 --- a/bin/tsw/runtime/capturer.js +++ b/bin/tsw/runtime/capturer.js @@ -15,7 +15,7 @@ const http = require('http'); const https = require('https'); var isFirstLoad = true; -if(global[__filename]){ +if(global[__filename]) { isFirstLoad = false; }else{ global[__filename] = {}; @@ -28,10 +28,10 @@ process.nextTick(function() { const alpha = require('util/auto-report/alpha.js'); const serverInfo = require('serverInfo.js'); - const create = function(oriRequest,protocol){ - return function(...args){ + const create = function(oriRequest, protocol) { + return function(...args) { var opt = args[0]; - var request = oriRequest.apply(this,args); + var request = oriRequest.apply(this, args); var captureBody = false; var result = []; var buffer = Buffer.alloc(0); @@ -47,7 +47,7 @@ process.nextTick(function() { var localPort = ''; var host = (opt.headers && opt.headers.host) || opt.host; - if(context.requestCaptureSN){ + if(context.requestCaptureSN) { context.requestCaptureSN++; }else{ context.requestCaptureSN = 1; @@ -56,7 +56,7 @@ process.nextTick(function() { var SN = context.requestCaptureSN || 0; var logPre = `[${SN}] `; - logger.debug(logPre + '${method} ${ip}:${port} ~ ${protocol}//${host}${path}',{ + logger.debug(logPre + '${method} ${ip}:${port} ~ ${protocol}//${host}${path}', { protocol : protocol, method : opt.method, host : host, @@ -66,20 +66,20 @@ process.nextTick(function() { }); //抓包 - if(alpha.isAlpha()){ + if(alpha.isAlpha()) { logger.debug(logPre + 'capture body on'); captureBody = true; } - if(captureBody){ + if(captureBody) { httpUtil.captureBody(request); - request.once('finish',function(){ + request.once('finish', function() { logger.debug(logPre + 'send finish, total size ' + this._body.length); }); } - var report = function(oriResponse){ + var report = function(oriResponse) { var logJson = logger.getJson(); var response = oriResponse || { headers : { @@ -91,7 +91,7 @@ process.nextTick(function() { statusMessage : 'server buisy' }; - if(!logJson){ + if(!logJson) { logger.debug('logger.getJson() is empty!'); return; } @@ -112,7 +112,7 @@ process.nextTick(function() { serverIp : remoteAddress || opt.host, serverPort : remotePort || opt.port, requestRaw : httpUtil.getClientRequestHeaderStr(request) + (request._body.toString('UTF-8') || ''), - responseHeader : httpUtil.getClientResponseHeaderStr(response,bodySize), + responseHeader : httpUtil.getClientResponseHeaderStr(response, bodySize), responseBody : (buffer.toString('base64')) || '', timestamps : { ClientConnected : new Date(timeStart), @@ -137,7 +137,7 @@ process.nextTick(function() { logJson.ajax.push(curr); }; - request.once('response',(response)=>{ + request.once('response', (response)=>{ timeResponse = Date.now(); var socket = response.socket; @@ -149,7 +149,7 @@ process.nextTick(function() { localAddress = socket.localAddress; localPort = socket.localPort; - logger.debug(logPre + '${localAddress}:${localPort} > ${remoteAddress}:${remotePort} response ${statusCode} cost:${cost}ms ${encoding}',{ + logger.debug(logPre + '${localAddress}:${localPort} > ${remoteAddress}:${remotePort} response ${statusCode} cost:${cost}ms ${encoding}', { remoteAddress : remoteAddress, remotePort : remotePort, localAddress : localAddress, @@ -159,27 +159,27 @@ process.nextTick(function() { cost: timeResponse - timeStart }); - var done = function(){ - this.removeListener('data',data); + var done = function() { + this.removeListener('data', data); - if(timeEnd){ + if(timeEnd) { return; } timeEnd = new Date().getTime(); - if(captureBody){ + if(captureBody) { buffer = Buffer.concat(result); result = []; } //上报 - if(captureBody){ + if(captureBody) { report(response); } }; - var data = function(chunk){ + var data = function(chunk) { // var cost = Date.now() - timeCurr; // timeCurr = Date.now(); @@ -192,23 +192,23 @@ process.nextTick(function() { bodySize += chunk.length; - if(captureBody && bodySize <= maxBodySize){ + if(captureBody && bodySize <= maxBodySize) { result.push(chunk); } }; - response.on('data',data); + response.on('data', data); - response.once('close',function(){ + response.once('close', function() { logger.debug(logPre + 'close'); done.call(this); }); - response.once('end',function(){ + response.once('end', function() { var cost = Date.now() - timeStart; - logger.debug('${logPre}end size:${size}, receive data cost: ${cost}ms',{ + logger.debug('${logPre}end size:${size}, receive data cost: ${cost}ms', { logPre: logPre, cost: cost, size: bodySize @@ -223,7 +223,7 @@ process.nextTick(function() { }; }; - http.request = create(http.request,'http:'); - https.request = create(https.request,'https:'); + http.request = create(http.request, 'http:'); + https.request = create(https.request, 'https:'); }); diff --git a/bin/tsw/runtime/fs.hack.js b/bin/tsw/runtime/fs.hack.js index 6a88b3f4..c79fae39 100644 --- a/bin/tsw/runtime/fs.hack.js +++ b/bin/tsw/runtime/fs.hack.js @@ -9,7 +9,7 @@ const TIMES_LIMIT = 5; -if(global[__filename]){ +if(global[__filename]) { global[__filename].map = {}; }else{ @@ -18,7 +18,7 @@ if(global[__filename]){ }; //追踪重复读写 - process.nextTick(function(){ + process.nextTick(function() { var cache = global[__filename]; var isWindows = require('util/isWindows'); var fs = require('fs'); @@ -26,7 +26,7 @@ if(global[__filename]){ var config = require('config'); var tnm2 = require('api/tnm2'); - fs.existsSync = hack(fs.existsSync,function(file){ + fs.existsSync = hack(fs.existsSync, function(file) { var sum = 0; var name = 'fs.existsSync'; var key = name + file; @@ -34,8 +34,8 @@ if(global[__filename]){ cache.map[key] = ~~cache.map[key] + 1; sum = cache.map[key]; - if(sum % TIMES_LIMIT === 0 && !config.devMode){ - logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}',{ + if(sum % TIMES_LIMIT === 0 && !config.devMode) { + logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}', { name: name, file: file, sum: sum, @@ -46,7 +46,7 @@ if(global[__filename]){ tnm2.Attr_API('SUM_TSW_FILE_SYNC', 1); }); - fs.writeFileSync = hack(fs.writeFileSync,function(file){ + fs.writeFileSync = hack(fs.writeFileSync, function(file) { var sum = 0; var name = 'fs.writeFileSync'; var key = name + file; @@ -54,8 +54,8 @@ if(global[__filename]){ cache.map[key] = ~~cache.map[key] + 1; sum = cache.map[key]; - if(sum % TIMES_LIMIT === 0 && !config.devMode){ - logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}',{ + if(sum % TIMES_LIMIT === 0 && !config.devMode) { + logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}', { name: name, file: file, sum: sum, @@ -66,7 +66,7 @@ if(global[__filename]){ tnm2.Attr_API('SUM_TSW_FILE_SYNC', 1); }); - fs.statSync = hack(fs.statSync,function(file){ + fs.statSync = hack(fs.statSync, function(file) { var sum = 0; var name = 'fs.statSync'; var key = name + file; @@ -74,8 +74,8 @@ if(global[__filename]){ cache.map[key] = ~~cache.map[key] + 1; sum = cache.map[key]; - if(sum > TIMES_LIMIT && !config.devMode){ - logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}',{ + if(sum > TIMES_LIMIT && !config.devMode) { + logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}', { name: name, file: file, sum: sum, @@ -86,7 +86,7 @@ if(global[__filename]){ tnm2.Attr_API('SUM_TSW_FILE_SYNC', 1); }); - fs.accessSync = hack(fs.accessSync,function(file){ + fs.accessSync = hack(fs.accessSync, function(file) { var sum = 0; var name = 'fs.accessSync'; var key = name + file; @@ -94,8 +94,8 @@ if(global[__filename]){ cache.map[key] = ~~cache.map[key] + 1; sum = cache.map[key]; - if(sum % TIMES_LIMIT === 0 && !config.devMode){ - logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}',{ + if(sum % TIMES_LIMIT === 0 && !config.devMode) { + logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}', { name: name, file: file, sum: sum, @@ -106,7 +106,7 @@ if(global[__filename]){ tnm2.Attr_API('SUM_TSW_FILE_SYNC', 1); }); - fs.readFileSync = hack(fs.readFileSync,function(file){ + fs.readFileSync = hack(fs.readFileSync, function(file) { var sum = 0; var name = 'fs.readFileSync'; var key = name + file; @@ -114,8 +114,8 @@ if(global[__filename]){ cache.map[key] = ~~cache.map[key] + 1; sum = cache.map[key]; - if(sum > TIMES_LIMIT && !config.devMode){ - logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}',{ + if(sum > TIMES_LIMIT && !config.devMode) { + logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}', { name: name, file: file, sum: sum, @@ -126,7 +126,7 @@ if(global[__filename]){ tnm2.Attr_API('SUM_TSW_FILE_SYNC', 1); }); - fs.readdirSync = hack(fs.readdirSync,function(file){ + fs.readdirSync = hack(fs.readdirSync, function(file) { var sum = 0; var name = 'fs.readdirSync'; var key = name + file; @@ -134,8 +134,8 @@ if(global[__filename]){ cache.map[key] = ~~cache.map[key] + 1; sum = cache.map[key]; - if(sum % TIMES_LIMIT === 0 && !config.devMode){ - logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}',{ + if(sum % TIMES_LIMIT === 0 && !config.devMode) { + logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}', { name: name, file: file, sum: sum, @@ -153,13 +153,13 @@ if(global[__filename]){ // }); //}); - function hack(fn,callback){ + function hack(fn, callback) { - if(isWindows.isWindows){ + if(isWindows.isWindows) { return fn; } - return function(){ + return function() { callback.apply(this, arguments); return fn.apply(this, arguments); }; diff --git a/bin/tsw/serverInfo.js b/bin/tsw/serverInfo.js index ccee75e4..5e16943a 100644 --- a/bin/tsw/serverInfo.js +++ b/bin/tsw/serverInfo.js @@ -13,30 +13,30 @@ const isInnerIP = require('util/http.isInnerIP.js'); this.intranetIp = '127.0.0.1'; -if(isWindows){ +if(isWindows) { this.intranetIp = getWinLocalIpv4(); }else{ this.intranetIp = getLinuxLocalIpv4(); } -function getLinuxLocalIpv4(){ +function getLinuxLocalIpv4() { var intranetIp = ''; var networkInterfaces = os.networkInterfaces(); - Object.keys(networkInterfaces).forEach(function(key){ + Object.keys(networkInterfaces).forEach(function(key) { var eth = networkInterfaces[key]; var address = eth && eth[0] && eth[0].address; - if(!address){ + if(!address) { return; } var tmp = isInnerIP.isInnerIP(address); - if(!tmp){ + if(!tmp) { return; } - if(tmp === '127.0.0.1'){ + if(tmp === '127.0.0.1') { return; } @@ -46,22 +46,22 @@ function getLinuxLocalIpv4(){ return intranetIp; } -function getWinLocalIpv4(){ +function getWinLocalIpv4() { var localNet = os.networkInterfaces(); - var key,item; - var v,i; + var key, item; + var v, i; var userIp = null; - for(key in localNet){ + for(key in localNet) { item = localNet[key]; - if(String(key).indexOf('本地连接') > -1){ + if(String(key).indexOf('本地连接') > -1) { - for(i =0 ; i < item.length; i++){ + for(i =0 ; i < item.length; i++) { v = item[i]; - if(v.family === 'IPv4'){ + if(v.family === 'IPv4') { userIp = v.address; return userIp; } diff --git a/bin/tsw/util/CD.js b/bin/tsw/util/CD.js index 3b3398a1..4c743ced 100644 --- a/bin/tsw/util/CD.js +++ b/bin/tsw/util/CD.js @@ -21,33 +21,33 @@ const cacheTime = 10 * 60 * 1000; //最大cache时间 var cacheStart = Date.now(); var cache = {}; -if(global[__filename]){ +if(global[__filename]) { cache = global[__filename]; }else{ global[__filename] = cache; } -this.check = function(key,count,second){ +this.check = function(key, count, second) { //请求openapi - if(config.appid && config.appkey && config.utilCDUrl){ - return checkByOpenapi(key,count,second); + if(config.appid && config.appkey && config.utilCDUrl) { + return checkByOpenapi(key, count, second); } - return checkByCmem(key,count,second); + return checkByCmem(key, count, second); }; -var checkByOpenapi = function(keyOri,count,second){ +var checkByOpenapi = function(keyOri, count, second) { var defer = Deferred.create(); var appid = context.appid || null; var key = 'CD3.' + crypto.createHash('sha1').update(`CD3.${appid}.${keyOri}.${count}.${second}`).digest('hex'); var start = Date.now(); - if(cache[key]){ - if(start - cache[key] < second * 1000){ - logger.debug('miss mem ${key}',{ + if(cache[key]) { + if(start - cache[key] < second * 1000) { + logger.debug('miss mem ${key}', { key: key }); @@ -56,13 +56,13 @@ var checkByOpenapi = function(keyOri,count,second){ } //清缓存逻辑 - if(start - cacheStart > cacheTime){ + if(start - cacheStart > cacheTime) { cache = {}; cacheStart = Date.now(); } - defer.fail(function(value){ + defer.fail(function(value) { //设置缓存 cache[key] = start; }); @@ -75,7 +75,7 @@ var checkByOpenapi = function(keyOri,count,second){ now: Date.now() }; - if(!config.utilCDUrl){ + if(!config.utilCDUrl) { return defer.reject(); } @@ -99,16 +99,16 @@ var checkByOpenapi = function(keyOri,count,second){ keepAlive : true, autoToken : false, dataType : 'json' - }).fail(function(){ + }).fail(function() { logger.error('checkByOpenapi fail.'); defer.reject(); - }).done(function(d){ + }).done(function(d) { var data = null; - if(d.result && d.result.code === 0){ + if(d.result && d.result.code === 0) { data = d.result.data; } - if(data > 0){ + if(data > 0) { return defer.resolve(data); } @@ -120,7 +120,7 @@ var checkByOpenapi = function(keyOri,count,second){ }; -this.curr = function(keyOri,count,second){ +this.curr = function(keyOri, count, second) { var defer = Deferred.create(); var appid = context.appid || null; @@ -129,13 +129,13 @@ this.curr = function(keyOri,count,second){ var memcached = module.exports.cmem(); - if(!memcached){ + if(!memcached) { return defer.reject(); } - memcached.get(key,function(err, result){ + memcached.get(key, function(err, result) { - if(err){ + if(err) { return defer.reject(); } @@ -145,7 +145,7 @@ this.curr = function(keyOri,count,second){ return defer; }; -var checkByCmem = function(keyOri,count,second){ +var checkByCmem = function(keyOri, count, second) { var defer = Deferred.create(); var appid = context.appid || null; @@ -153,9 +153,9 @@ var checkByCmem = function(keyOri,count,second){ var key = 'CD3.' + crypto.createHash('sha1').update(`CD3.${appid}.${keyOri}.${count}.${second}`).digest('hex'); var start = Date.now(); - if(cache[key]){ - if(start - cache[key] < second * 1000){ - logger.debug('miss mem ${key}',{ + if(cache[key]) { + if(start - cache[key] < second * 1000) { + logger.debug('miss mem ${key}', { key: key }); @@ -164,51 +164,51 @@ var checkByCmem = function(keyOri,count,second){ } //清缓存逻辑 - if(start - cacheStart > cacheTime){ + if(start - cacheStart > cacheTime) { cache = {}; cacheStart = Date.now(); } var memcached = module.exports.cmem(); - if(!memcached){ + if(!memcached) { return defer.reject(); } - defer.fail(function(value){ + defer.fail(function(value) { //设置缓存 cache[key] = start; }); - defer.always(function(){ + defer.always(function() { tnm2.Attr_API('SUM_TSW_CD_CHECK', 1); }); - memcached.add(key,1,second,function(err, result){ + memcached.add(key, 1, second, function(err, result) { - if(err){ + if(err) { //add失败是正常的 } - if(result === true){ + if(result === true) { logger.debug(`add ${key} true`); return defer.resolve(1); } logger.debug(`add ${key} fail`); - if(count <= 1){ + if(count <= 1) { return defer.reject(); } - memcached.incr(key,1,function(err, result){ + memcached.incr(key, 1, function(err, result) { - if(err){ + if(err) { logger.error(err.satck); return defer.reject(); } - if(result <= count){ + if(result <= count) { logger.debug(`incr ${key} : ${result}`); return defer.resolve(result); }else{ @@ -223,36 +223,36 @@ var checkByCmem = function(keyOri,count,second){ this.checkByCmem = checkByCmem; -this.cmem = function(){ - if(context.appid && context.appkey){ +this.cmem = function() { + if(context.appid && context.appkey) { return cmemTSW.openapi(); } return cmemTSW.sz(); }; //开放接口 -this.openapi = async function(req,res){ +this.openapi = async function(req, res) { var appid = context.appid; var appkey = context.appkey; - if(req.param('appid') !== appid){ - returnJson({ code: -2 , message: 'appid错误'}); + if(req.param('appid') !== appid) { + returnJson({ code: -2, message: 'appid错误'}); return; } - if(!appid){ - returnJson({ code: -2 , message: 'appid is required'}); + if(!appid) { + returnJson({ code: -2, message: 'appid is required'}); return; } - if(!appkey){ - returnJson({ code: -2 , message: 'appkey is required'}); + if(!appkey) { + returnJson({ code: -2, message: 'appkey is required'}); return; } - if(!/^[a-zA-Z0-9_-]{0,50}$/.test(appid)){ - returnJson({ code: -2 , message: 'appid is required'}); + if(!/^[a-zA-Z0-9_-]{0,50}$/.test(appid)) { + returnJson({ code: -2, message: 'appid is required'}); return; } @@ -262,41 +262,41 @@ this.openapi = async function(req,res){ var second = ~~req.param('second'); //单位秒 var count = ~~req.param('count'); //默认是1 - if(!key){ - returnJson({ code: -2 , message: 'key is required'}); + if(!key) { + returnJson({ code: -2, message: 'key is required'}); return; } - if(!second){ - returnJson({ code: -2 , message: 'second is required'}); + if(!second) { + returnJson({ code: -2, message: 'second is required'}); return; } - if(second > 48 * 60 * 60){ - returnJson({ code: -2 , message: '不支持超过48h的second'}); + if(second > 48 * 60 * 60) { + returnJson({ code: -2, message: '不支持超过48h的second'}); return; } - if(key.length > 256){ - returnJson({ code: -2 , message: 'key.length超过了256'}); + if(key.length > 256) { + returnJson({ code: -2, message: 'key.length超过了256'}); return; } - var data = await checkByCmem(key,count,second).toES6Promise().catch(function(){ + var data = await checkByCmem(key, count, second).toES6Promise().catch(function() { return null; }); - var result = {code: 0,data: data}; + var result = {code: 0, data: data}; returnJson(result); }; -var returnJson = function(json){ +var returnJson = function(json) { var gzip = gzipHttp.create({ contentType: 'application/json; charset=UTF-8', code: 200 }); - gzip.write(JSON.stringify(json,null,2)); + gzip.write(JSON.stringify(json, null, 2)); gzip.end(); }; diff --git a/bin/tsw/util/Deferred/index.js b/bin/tsw/util/Deferred/index.js index d74daf3e..ac0855d5 100644 --- a/bin/tsw/util/Deferred/index.js +++ b/bin/tsw/util/Deferred/index.js @@ -277,7 +277,7 @@ jQuery.Callbacks = function( flags ) { // Fire callbacks fire = function( context, args ) { - var fn,domain,ret; + var fn, domain, ret; args = args || []; memory = !flags.memory || [context, args]; @@ -293,7 +293,7 @@ jQuery.Callbacks = function( flags ) { // restore domain if needed if (domain && domain !== process.domain) { - domain.run(function(){ + domain.run(function() { ret = fn.apply(context, args); }); }else { @@ -312,12 +312,10 @@ jQuery.Callbacks = function( flags ) { memory = stack.shift(); self.fireWith(memory[0], memory[1]); } - } - else + } else if (memory === true) { self.disable(); - } - else { + } else { list = []; } } @@ -478,16 +476,16 @@ jQuery.extend({ return this; }, - toES6Promise: function(){ + toES6Promise: function() { var self = this; - return new Promise(function (resolve, reject){ + return new Promise(function (resolve, reject) { - self.done(function(d){ + self.done(function(d) { resolve(d); }); - self.fail(function(d){ + self.fail(function(d) { reject(d); }); }); @@ -599,7 +597,7 @@ jQuery.extend({ module.exports = { - create: function(){ + create: function() { return jQuery.Deferred(); }, when: jQuery.when, diff --git a/bin/tsw/util/Queue/index.js b/bin/tsw/util/Queue/index.js index f5ff3cd1..f45ef35a 100644 --- a/bin/tsw/util/Queue/index.js +++ b/bin/tsw/util/Queue/index.js @@ -7,20 +7,20 @@ */ 'use strict'; -function Queue(){ +function Queue() { this._queue = []; } -this.create = function(){ +this.create = function() { return new Queue(); }; -Queue.prototype.queue = function(fn){ +Queue.prototype.queue = function(fn) { - if(typeof fn !== 'function'){ + if(typeof fn !== 'function') { return this; } @@ -29,22 +29,22 @@ Queue.prototype.queue = function(fn){ this._queue.push(fn); - if(this._queue.length === 1){ + if(this._queue.length === 1) { this.dequeue(); } return this; }; -Queue.prototype.dequeue = function(){ +Queue.prototype.dequeue = function() { - var domain,fn; + var domain, fn; - if(this._queue.length === 0){ + if(this._queue.length === 0) { return this; } - if(this._queue[0] === 'pending'){ + if(this._queue[0] === 'pending') { this._queue.shift(); this.dequeue(); @@ -58,7 +58,7 @@ Queue.prototype.dequeue = function(){ domain = fn._domain; fn._domain = undefined; - if(domain && domain !== process.domain){ + if(domain && domain !== process.domain) { domain.run(() => { fn.call(this); }); diff --git a/bin/tsw/util/auto-report/TEReport.js b/bin/tsw/util/auto-report/TEReport.js index a7a93f70..b44b7ae7 100644 --- a/bin/tsw/util/auto-report/TEReport.js +++ b/bin/tsw/util/auto-report/TEReport.js @@ -20,13 +20,13 @@ const Deferred = require('util/Deferred'); const isWindows = require('util/isWindows.js'); -this.report = function(){ +this.report = function() { - if(isWindows.isWindows){ + if(isWindows.isWindows) { return; } - if(!config.isTest){ + if(!config.isTest) { return; } @@ -36,15 +36,15 @@ this.report = function(){ port : config.httpPort }; - require('api/cmdb').GetDeviceThisServer().done(function(d){ + require('api/cmdb').GetDeviceThisServer().done(function(d) { var business; - if(d && d.business){ + if(d && d.business) { business = d.business[0]; } - if(!business){ + if(!business) { business = { moduleId: 0, L1Business: '未知', @@ -55,24 +55,24 @@ this.report = function(){ } logJson.moduleId = business.moduleId; - logJson.moduleName = [business.L1Business,business.L2Business,business.L3Business,business.module].join('->'); + logJson.moduleName = [business.L1Business, business.L2Business, business.L3Business, business.module].join('->'); - logJson = Deferred.extend(true,{ + logJson = Deferred.extend(true, { time : new Date().toGMTString(), name : '', group : 'unknown', desc : '', order : 0, owner : '' - },config.testInfo,logJson); + }, config.testInfo, logJson); var logKey = 'h5test' + logJson.group; //上报自己 - post.report(logKey,logText,logJson); + post.report(logKey, logText, logJson); //开放平台上报,不用再分组了 - if(config.appid && config.appkey){ + if(config.appid && config.appkey) { logReport.reportCloud({ type : 'alpha', logText : logText, @@ -89,8 +89,8 @@ this.report = function(){ } //上报分组 - require('util/CD.js').check('h5test' + logJson.group,1,60).done(function(){ - post.report('group.h5test',logText,logJson); + require('util/CD.js').check('h5test' + logJson.group, 1, 60).done(function() { + post.report('group.h5test', logText, logJson); }); }); @@ -98,7 +98,7 @@ this.report = function(){ }; -this.list = function(group){ +this.list = function(group) { var defer = Deferred.create(); var getLogJsonDefer; @@ -106,19 +106,19 @@ this.list = function(group){ group = group || ''; //开平对应的存储 - if(context.appid && context.appkey){ + if(context.appid && context.appkey) { getLogJsonDefer = postOpenapi.getLogJson(`${context.appid}/tsw/h5test`); }else{ getLogJsonDefer = post.getLogJson(`h5test${group}`); } - getLogJsonDefer.done(function(arr){ + getLogJsonDefer.done(function(arr) { var res = []; var map = {}; - arr.forEach(function(v){ - if(!map[v.ip]){ + arr.forEach(function(v) { + if(!map[v.ip]) { map[v.ip] = true; res.push(v); } @@ -138,7 +138,7 @@ this.list = function(group){ moduleName: 'null' }); - res.sort(function(a,b){ + res.sort(function(a, b) { return a.order - b.order; }); @@ -149,23 +149,23 @@ this.list = function(group){ }; -this.getAllGroup = function(){ +this.getAllGroup = function() { var defer = Deferred.create(); - post.getLogJson('group.h5test').done(function(arr){ + post.getLogJson('group.h5test').done(function(arr) { var res = []; var map = {}; - arr.forEach(function(v){ - if(!map[v.group]){ + arr.forEach(function(v) { + if(!map[v.group]) { map[v.group] = true; res.push(v); } }); - res.sort(function(a,b){ + res.sort(function(a, b) { return a.order - b.order; }); diff --git a/bin/tsw/util/auto-report/alpha.js b/bin/tsw/util/auto-report/alpha.js index 93f84089..bf5273c6 100644 --- a/bin/tsw/util/auto-report/alpha.js +++ b/bin/tsw/util/auto-report/alpha.js @@ -12,46 +12,46 @@ const logger = require('logger'); const {isWindows} = require('util/isWindows'); const TSW = require('api/keyman'); -if(!global[__filename]){ +if(!global[__filename]) { global[__filename] = {}; } -this.update = function(map){ +this.update = function(map) { global[__filename] = map || {}; }; -this.add = function(uin){ +this.add = function(uin) { // }; -this.isAlpha = function(req){ +this.isAlpha = function(req) { var uin; - if(typeof req === 'object'){ + if(typeof req === 'object') { - if(!uin){ + if(!uin) { uin = logger.getKey(); } - if(!uin){ + if(!uin) { uin = this.getUin(req); } }else{ uin = req; - if(!uin){ + if(!uin) { uin = logger.getKey(); } - if(!uin){ + if(!uin) { uin = this.getUin(); } } - if(uin && isWindows){ + if(uin && isWindows) { //windows 抓包用 - if(config.skyMode){ + if(config.skyMode) { return true; } } @@ -61,19 +61,19 @@ this.isAlpha = function(req){ return uinMap[uin] || TSW.getAlphaUinMapSync()[uin]; }; -this.getUin = function(req){ +this.getUin = function(req) { var uin; var window = context.window || {}; req = req || window.request; - if(!req){ + if(!req) { return uin; } //业务有可能不使用uin登录态,支持业务扩展getUin实现 - if(config.extendMod && typeof config.extendMod.getUin === 'function'){ + if(config.extendMod && typeof config.extendMod.getUin === 'function') { return config.extendMod.getUin(req); } diff --git a/bin/tsw/util/auto-report/download.js b/bin/tsw/util/auto-report/download.js index 5112c791..24ee0f1e 100644 --- a/bin/tsw/util/auto-report/download.js +++ b/bin/tsw/util/auto-report/download.js @@ -18,13 +18,13 @@ const canIuse = /^[0-9a-zA-Z_-]{0,64}$/; const zlib = require('zlib'); -module.exports = function(request, response){ - OALogin.checkLoginForTSW(request,response,function(){ - module.exports.go(request,response); +module.exports = function(request, response) { + OALogin.checkLoginForTSW(request, response, function() { + module.exports.go(request, response); }); }; -module.exports.go = function(request, response){ +module.exports.go = function(request, response) { var arr = request.REQUEST.pathname.split('/'); var appid = context.appid || ''; @@ -37,41 +37,41 @@ module.exports.go = function(request, response){ var flagOfDownloadFileFormat = request.query.fileFormat; - if(appid){ + if(appid) { currPost = postOpenapi; groupKey = `${appid}/v2.group.alpha`; } - if(!key){ + if(!key) { key = group; group = ''; } - if(!canIuse.test(appid)){ + if(!canIuse.test(appid)) { return returnError('appid格式非法'); } - if(!canIuse.test(group)){ + if(!canIuse.test(group)) { return returnError('group格式非法'); } - if(!canIuse.test(key)){ + if(!canIuse.test(key)) { return returnError('key格式非法'); } - var createLogKey = function(appid,group,key){ + var createLogKey = function(appid, group, key) { var logKey = key; - if(group){ + if(group) { logKey = `${group}/${logKey}`; } return logKey; }; - var logKey = createLogKey(appid,group,key); + var logKey = createLogKey(appid, group, key); - if(appid){ + if(appid) { logKey = `${appid}/${logKey}`; } @@ -83,25 +83,25 @@ module.exports.go = function(request, response){ context.logKey = logKey; context.createLogKey = createLogKey; - logger.debug('logKey :${logKey}',{ + logger.debug('logKey :${logKey}', { logKey: logKey }); //下标。 - var index = parseInt(request.GET.index,10); + var index = parseInt(request.GET.index, 10); key = logKey + '.' + index; - if(index >= 0){ - if(flagOfDownloadFileFormat === 'har'){ - currPost.getLogJsonByKey(logKey,key).done(function(data){ + if(index >= 0) { + if(flagOfDownloadFileFormat === 'har') { + currPost.getLogJsonByKey(logKey, key).done(function(data) { downloadHaz(request, response, { id: logKey + data['SNKeys'][0], data: data }); }); }else { - currPost.getLogJsonByKey(logKey,key).done(function(data){ + currPost.getLogJsonByKey(logKey, key).done(function(data) { download(request, response, { id: logKey + data['SNKeys'][0], data: data @@ -111,15 +111,15 @@ module.exports.go = function(request, response){ }else{ //点击下载全部 - if(flagOfDownloadFileFormat === 'har'){ - currPost.getLogJson(logKey).done(function(data){ + if(flagOfDownloadFileFormat === 'har') { + currPost.getLogJson(logKey).done(function(data) { downloadHaz(request, response, { id: logKey, data: data }); }); }else { - currPost.getLogJson(logKey).done(function(data){ + currPost.getLogJson(logKey).done(function(data) { download(request, response, { id: logKey, data: data @@ -131,7 +131,7 @@ module.exports.go = function(request, response){ }; -function returnError(message){ +function returnError(message) { var window = context.window; var gzipResponse = gzipHttp.getGzipResponse({ request: window.request, @@ -157,19 +157,19 @@ var initRequestHar = function (request) { cache:{}, timings:{} }; - if(requestRaw){ + if(requestRaw) { requestArr = requestRaw.split(/\r\n/g); - if(requestArr.length > 0 ){ + if(requestArr.length > 0 ) { - requestArr.forEach(function (item,index) { + requestArr.forEach(function (item, index) { item = item.trim(); - if(item!=='' && item.length !== 0){ + if(item!=='' && item.length !== 0) { // 第一行,请求头:GET url;回包:HTTP/1.1 200 OK - if(index === 0){ + if(index === 0) { var firstItem = item.split(' '); // 请求头:GET url; - if(firstItem.length === 3){ + if(firstItem.length === 3) { request.protocol = firstItem[2]; }else{ request.protocol = firstItem[1]; @@ -178,7 +178,7 @@ var initRequestHar = function (request) { }else{ item = item.split(':') || []; - if(item.length === 2 ){ + if(item.length === 2 ) { request[item[0]] = item[1].trim(); request.headers.push({ name:item[0], @@ -188,7 +188,7 @@ var initRequestHar = function (request) { } } }); - if(request.method && request.method === 'POST'){ + if(request.method && request.method === 'POST') { var postDataArr = requestArr[requestArr.length - 1]; request.postData.mimeType = 'multipart/form-data'; request.postData.text = postDataArr; @@ -198,9 +198,9 @@ var initRequestHar = function (request) { } } // 补齐cookie - if(request.cookie){ + if(request.cookie) { var cookieArray = request.cookie.split(';'); - if(cookieArray.length > 0){ + if(cookieArray.length > 0) { request.cookieArray = []; cookieArray.forEach(function (item) { item = item.split('=') || []; @@ -212,9 +212,9 @@ var initRequestHar = function (request) { } } // 补齐query - if(request.path && request.path.indexOf('?') !== -1){ + if(request.path && request.path.indexOf('?') !== -1) { var query = request.path.slice(request.path.indexOf('?') + 1); - if(query){ + if(query) { query = query.split('&');// re=va query.forEach(function (item) { item = item.split('='); @@ -231,11 +231,11 @@ var initRequestHar = function (request) { var getUrl = function (curr) { var url; var host; - if(curr){ + if(curr) { host = String(curr.host); url = String(curr.url); // 不包括host,特殊处理下 - if(url && host && url.indexOf(host) === -1){ + if(url && host && url.indexOf(host) === -1) { url = 'https://'+ host + url; } } @@ -262,7 +262,7 @@ var initRequestHar = function (request) { 'headers': requestHeader.headers, 'queryString' : requestHeader.queryString, 'postData' : requestHeader.postData, - 'headersSize' : request.requestRaw.replace(/\n/g,'').length, + 'headersSize' : request.requestRaw.replace(/\n/g, '').length, 'bodySize' : requestHeader['Content-Length'] || 0, }; @@ -313,18 +313,18 @@ var initRequestHar = function (request) { 'comment': '' }; - if(typeof request.responseBody !== 'undefined' && request.responseBody.length !== 0 ){ + if(typeof request.responseBody !== 'undefined' && request.responseBody.length !== 0 ) { requestHaz.response.content.size = request.responseBody.length; var requestResponseBodyBaseBuffer = (Buffer.from(request.responseBody || '', 'base64')); // chunked decode - if(typeof responseHeader['Transfer-Encoding'] !== 'undefined' && responseHeader['Transfer-Encoding'] === 'chunked'){ + if(typeof responseHeader['Transfer-Encoding'] !== 'undefined' && responseHeader['Transfer-Encoding'] === 'chunked') { requestResponseBodyBaseBuffer = decodeChunkedUint8Array(requestResponseBodyBaseBuffer); } // console.error(requestResponseBodyBaseBuffer.length,'requestResponseBodyBaseBuffer'); - if(typeof responseHeader['Content-Encoding'] !== 'undefined' && responseHeader['Content-Encoding'] === 'gzip'){ + if(typeof responseHeader['Content-Encoding'] !== 'undefined' && responseHeader['Content-Encoding'] === 'gzip') { try { var ungziprawText = zlib.gunzipSync(requestResponseBodyBaseBuffer); requestHaz.response.content.text = ungziprawText.toString('utf8');// 暂时文件 @@ -353,7 +353,7 @@ var downloadHaz = function (request, response, opt) { var data = opt.data || [], viewData = [], filename = opt.id || 'log', - index = parseInt(request.GET.index,10), + index = parseInt(request.GET.index, 10), SNKey = request.GET.SNKey; var hazJson = { @@ -367,19 +367,19 @@ var downloadHaz = function (request, response, opt) { } }; - if(data.length <= 0){ + if(data.length <= 0) { failRet(request, response, 'not find log'); return; } - if(SNKey && data.SNKeys && data.SNKeys[0] != SNKey){ + if(SNKey && data.SNKeys && data.SNKeys[0] != SNKey) { failRet(request, response, '该log已经过期,请联系用户慢点刷log~'); return; } - if(typeof data == 'string'){ + if(typeof data == 'string') { failRet(request, response, 'key类型不对'); return; @@ -394,57 +394,57 @@ var downloadHaz = function (request, response, opt) { contentType: 'application/octet-stream' }); - if(SNKey){ + if(SNKey) { //override index index = 0; } - if(index >= 0){ + if(index >= 0) { data = [data[index]]; }else{ data = data.reverse(); } - data.forEach(function (tmp,i) { - if(tmp.curr){ + data.forEach(function (tmp, i) { + if(tmp.curr) { viewData.push(tmp.curr); } tmp.ajax && - tmp.ajax.forEach(function(ajax,i){ - if(!ajax.SN){ + tmp.ajax.forEach(function(ajax, i) { + if(!ajax.SN) { return; } viewData.push(ajax); }); }); - viewData.forEach(function(tmp,i){ + viewData.forEach(function(tmp, i) { hazJson.log.entries.push(initRequestHar(tmp)); }); - var buf = Buffer.from(JSON.stringify(hazJson),'UTF-8'); + var buf = Buffer.from(JSON.stringify(hazJson), 'UTF-8'); gzipResponse.write(buf); gzipResponse.end(); }; -var download = function(request, response, opt){ +var download = function(request, response, opt) { opt = opt || {}; var data = opt.data || [], viewData = [], filename = opt.id || 'log', - index = parseInt(request.GET.index,10), + index = parseInt(request.GET.index, 10), SNKey = request.GET.SNKey; - if(data.length <= 0){ + if(data.length <= 0) { failRet(request, response, 'not find log'); return; } - if(SNKey && data.SNKeys && data.SNKeys[0] != SNKey){ + if(SNKey && data.SNKeys && data.SNKeys[0] != SNKey) { failRet(request, response, '该log已经过期,请联系用户慢点刷log~'); return; @@ -452,13 +452,13 @@ var download = function(request, response, opt){ //data = data[0]; - if(typeof data == 'string'){ + if(typeof data == 'string') { failRet(request, response, 'key类型不对'); return; } - filename = filename.replace(/[^a-zA-Z0-9_.-]/g,'_'); + filename = filename.replace(/[^a-zA-Z0-9_.-]/g, '_'); response.setHeader('Content-disposition', 'attachment; filename=' + filename + '.saz'); @@ -470,12 +470,12 @@ var download = function(request, response, opt){ contentType: 'application/octet-stream' }); - if(SNKey){ + if(SNKey) { //override index index = 0; } - if(index >= 0){ + if(index >= 0) { data = [data[index]]; }else{ data = data.reverse(); @@ -486,14 +486,14 @@ var download = function(request, response, opt){ archiver.append(tmpl.download_index(data), {name: '_index.htm'}); archiver.append(tmpl.download_content_types(), {name: '[Content_Types].xml'}); - data.forEach(function(tmp,i){ + data.forEach(function(tmp, i) { var sid = ('0000' + (i + 1)).slice(-3); tmp.curr = tmp.curr || {}; tmp.curr.sid = sid + '.0000'; tmp.curr.logText = tmp.curr.logText || ''; - tmp.curr.logText = tmp.curr.logText.replace(/\r\n|\r|\n/gm,'\r\n'); + tmp.curr.logText = tmp.curr.logText.replace(/\r\n|\r|\n/gm, '\r\n'); var logSNKey = post.getLogSN(tmp.curr.logText); var log = { @@ -506,7 +506,7 @@ var download = function(request, response, opt){ cache : '', process : tmp.curr.process, resultCode : '200', - contentLength : Buffer.byteLength(tmp.curr.responseBody || '','UTF-8'), + contentLength : Buffer.byteLength(tmp.curr.responseBody || '', 'UTF-8'), contentType : 'text/plain', clientIp : '', clientPort : '', @@ -514,7 +514,7 @@ var download = function(request, response, opt){ serverPort : '', requestRaw : 'GET log/'+logSNKey+' HTTP/1.1\r\n\r\n', responseHeader : 'HTTP/1.1 200 OK\r\nContent-Type: text/plain; charset=UTF-8\r\nConnection: close\r\n\r\n', - responseBody : Buffer.from(tmp.curr.logText || '','UTF-8').toString('base64'), + responseBody : Buffer.from(tmp.curr.logText || '', 'UTF-8').toString('base64'), timestamps : tmp.curr.timestamps } }; @@ -523,11 +523,11 @@ var download = function(request, response, opt){ viewData.push(log); tmp.ajax && - tmp.ajax.forEach(function(curr,i){ + tmp.ajax.forEach(function(curr, i) { var ajax = {}; - if(!curr.SN){ + if(!curr.SN) { return; } @@ -539,7 +539,7 @@ var download = function(request, response, opt){ }); }); - viewData.forEach(function(tmp,i){ + viewData.forEach(function(tmp, i) { archiver.append( tmp.curr.requestRaw || '', { @@ -554,7 +554,7 @@ var download = function(request, response, opt){ ); archiver.append( Buffer.concat([ - Buffer.from(tmp.curr.responseHeader || '','utf-8'), + Buffer.from(tmp.curr.responseHeader || '', 'utf-8'), Buffer.from(tmp.curr.responseBody || '', 'base64') ]), { @@ -565,22 +565,22 @@ var download = function(request, response, opt){ // archiver.pipe(gzipResponse); - archiver.on('error', function(e){ + archiver.on('error', function(e) { logger.error('archive log error \n' + e.message); }); - archiver.on('data', function(buffer){ + archiver.on('data', function(buffer) { gzipResponse.write(buffer); }); - archiver.once('end', function(){ + archiver.once('end', function() { gzipResponse.end(); }); archiver.finalize(); }; -var failRet = function(request, response, msg){ +var failRet = function(request, response, msg) { var gzipResponse = gzipHttp.getGzipResponse({ request: request, response: response, @@ -598,10 +598,10 @@ var failRet = function(request, response, msg){ var decodeChunkedUint8Array = function (Uint8ArrayBuffer) { var rawText = []; var startOfTheRawText = Uint8ArrayBuffer.indexOf(13); - while( startOfTheRawText !== -1 && startOfTheRawText !== 0 ){ - var rawTextSizeUint8ArrayBuffer = Uint8ArrayBuffer.slice(0 ,startOfTheRawText); - var rawTextSizeUint8ArrayInt = parseInt(Buffer.from(rawTextSizeUint8ArrayBuffer),16); - if(rawTextSizeUint8ArrayInt === 0 ){ + while( startOfTheRawText !== -1 && startOfTheRawText !== 0 ) { + var rawTextSizeUint8ArrayBuffer = Uint8ArrayBuffer.slice(0, startOfTheRawText); + var rawTextSizeUint8ArrayInt = parseInt(Buffer.from(rawTextSizeUint8ArrayBuffer), 16); + if(rawTextSizeUint8ArrayInt === 0 ) { break; } var chunkedText = Uint8ArrayBuffer.slice(startOfTheRawText+2, startOfTheRawText+2 +rawTextSizeUint8ArrayInt ); @@ -610,8 +610,8 @@ var decodeChunkedUint8Array = function (Uint8ArrayBuffer) { startOfTheRawText = Uint8ArrayBuffer.indexOf(13); } var bufferText = new Uint8Array( 0 ); - for(var ji=0; ji 0 === false){ + if(limit.max[type] > 0 === false) { return; } //频率限制 - if(limit.count[type] > 0){ + if(limit.count[type] > 0) { limit.count[type]++; }else{ limit.count[type] = 1; } - if(Date.now() - limit.time < 5000){ + if(Date.now() - limit.time < 5000) { - if(limit.count[type] > limit.max[type]){ + if(limit.count[type] > limit.max[type]) { dcapi.report({ key : typeKey, @@ -182,24 +182,24 @@ module.exports = function(req, res){ tnm2.Attr_API(arrtKey, 1); - if(Buffer.isBuffer(req.REQUEST.body)){ + if(Buffer.isBuffer(req.REQUEST.body)) { req.REQUEST.body = format.formatBuffer(req.REQUEST.body); } - logger.debug('\n${headers}${body}\r\nresponse ${statusCode} ${resHeaders}',{ + logger.debug('\n${headers}${body}\r\nresponse ${statusCode} ${resHeaders}', { headers : httpUtil.getRequestHeaderStr(req), body : req.REQUEST.body || '', statusCode : res.statusCode, - resHeaders : JSON.stringify(res._headers,null,2) + resHeaders : JSON.stringify(res._headers, null, 2) }); logText = logText || logger.getText(); - if(type === 'alpha'){ + if(type === 'alpha') { try{ //webapp的二进制回包转成可视化的结构 - if(res._body && res._headers['content-type'] === 'webapp'){ + if(res._body && res._headers['content-type'] === 'webapp') { res._body = Buffer.from(format.formatBuffer(res._body)); } @@ -223,7 +223,7 @@ module.exports = function(req, res){ logText : logText, timestamps : req.timestamps }; - }catch(e){ + }catch(e) { logger.error(e.stack); } @@ -244,9 +244,9 @@ module.exports = function(req, res){ statusCode : res.statusCode }; - if(type === 'alpha'){ + if(type === 'alpha') { //根据content-type设置group便于分类查询抓包 - if(!reportData.group){ + if(!reportData.group) { var pathName = req.REQUEST.pathname; var fileName = pathName.substr( pathName.lastIndexOf('/') + 1); reportData.group = module.exports.fingureCroup({ @@ -259,7 +259,7 @@ module.exports = function(req, res){ }); } - if(config.appid && config.appkey){ + if(config.appid && config.appkey) { module.exports.reportCloud(reportData); }else{ module.exports.reportAlpha(reportData); @@ -272,7 +272,7 @@ module.exports = function(req, res){ }; -module.exports.fingureCroup = function(opts){ +module.exports.fingureCroup = function(opts) { var group = 'other', contentType = opts.resHeaders['content-type'] || '', suffix = opts.suffix.toLowerCase(), @@ -285,12 +285,12 @@ module.exports.fingureCroup = function(opts){ js : 'js' }; - if(!contentType){ + if(!contentType) { //没声明算html return 'html'; } - switch(true){ + switch(true) { case /^text\/html/.test(contentType): group = 'html'; break; @@ -309,7 +309,7 @@ module.exports.fingureCroup = function(opts){ case /^image\/.*/.test(contentType): group = 'image'; break; - case !!~['json','cgi', 'fcg', 'php'].indexOf(suffix): + case !!~['json', 'cgi', 'fcg', 'php'].indexOf(suffix): group = 'XHR'; break; case !!~['eot', 'svg', 'ttf', 'woff'].indexOf(suffix): @@ -317,7 +317,7 @@ module.exports.fingureCroup = function(opts){ break; //其余的通过后缀区分吧 default : - if(!suffix){ + if(!suffix) { group = 'other'; }else { group = groupMap[suffix] || 'other'; @@ -328,89 +328,89 @@ module.exports.fingureCroup = function(opts){ return group; }; -module.exports.reportAlpha = function(data){ +module.exports.reportAlpha = function(data) { var currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24); - if(!data.key){ + if(!data.key) { return; } - if(!canIuse.test(data.key)){ + if(!canIuse.test(data.key)) { return; } var reportKey = [data.key].join('/'); var logNumMax = context.MAX_ALPHA_LOG || MAX_ALPHA_LOG; - post.report(reportKey,data.logText,data.logJson).done(function(isFirst){ - if(isFirst){ + post.report(reportKey, data.logText, data.logJson).done(function(isFirst) { + if(isFirst) { //增加计数 - CD.checkByCmem(`SUM_TSW_ALPHA_LOG_KEY.${currDays}`, logNumMax, 24 * 60 * 60).done(function(curr){ + CD.checkByCmem(`SUM_TSW_ALPHA_LOG_KEY.${currDays}`, logNumMax, 24 * 60 * 60).done(function(curr) { logger.debug('SUM_TSW_ALPHA_LOG_KEY curr count: ' + curr); }); }else{ - CD.checkByCmem(`SUM_TSW_ALPHA_LOG.${currDays}`, logNumMax * logNumMax * logNumMax, 24 * 60 * 60).done(function(curr){ + CD.checkByCmem(`SUM_TSW_ALPHA_LOG.${currDays}`, logNumMax * logNumMax * logNumMax, 24 * 60 * 60).done(function(curr) { logger.debug('SUM_TSW_ALPHA_LOG curr count: ' + curr); }); } }); - if(!data.group){ + if(!data.group) { return; } - if(!canIuse.test(data.group)){ + if(!canIuse.test(data.group)) { return; } - var reportGroupKey = [data.group,data.key].join('/'); + var reportGroupKey = [data.group, data.key].join('/'); - post.report(reportGroupKey,data.logText,data.logJson).done(function(isFirst){ + post.report(reportGroupKey, data.logText, data.logJson).done(function(isFirst) { //这里不用计数了 - }).always(function(){ + }).always(function() { //上报分组 - CD.checkByCmem('v2.group.send.${data.group}',1,60).done(function(){ - post.report('v2.group.alpha',data.group,{group: data.group}); + CD.checkByCmem('v2.group.send.${data.group}', 1, 60).done(function() { + post.report('v2.group.alpha', data.group, {group: data.group}); }); }); }; -module.exports.report = function(data){ +module.exports.report = function(data) { return require('default/logReport.js').report(data); }; -module.exports.reportCloud = function(data){ +module.exports.reportCloud = function(data) { - if(!config.appid){ + if(!config.appid) { return; } - if(!config.appkey){ + if(!config.appkey) { return; } - if(!config.logReportUrl){ + if(!config.logReportUrl) { return; } - if(!data.key){ + if(!data.key) { return; } //only alpha - if(data.type !== 'alpha'){ + if(data.type !== 'alpha') { return; } logger.debug('reportCloud'); - logger.debug('report log type: ${type}, key ${key}',data); + logger.debug('report log type: ${type}, key ${key}', data); - var postData = Object.assign({},data); + var postData = Object.assign({}, data); //加密 - postData.logText = post.encode(config.appid,config.appkey,data.logText); - postData.logJson = post.encode(config.appid,config.appkey,data.logJson); + postData.logText = post.encode(config.appid, config.appkey, data.logText); + postData.logJson = post.encode(config.appid, config.appkey, data.logJson); postData.appid = config.appid; postData.userip = ''; postData.now = Date.now(); @@ -436,17 +436,17 @@ module.exports.reportCloud = function(data){ keepAlive : true, autoToken : false, dataType : 'json' - }).done(function(){ + }).done(function() { logger.debug('reportCloud success.'); - }).fail(function(){ + }).fail(function() { logger.error('reportCloud fail.'); }); }; -module.exports.receiveCloud = function(req,res){ +module.exports.receiveCloud = function(req, res) { - var returnJson = function(message){ + var returnJson = function(message) { res.setHeader('Content-Type', 'application/json; charset=UTF-8'); res.writeHead(200); res.end(JSON.stringify({ @@ -457,97 +457,97 @@ module.exports.receiveCloud = function(req,res){ var data = req.POST || {}; - if(!data.appid){ + if(!data.appid) { return returnJson('appid is required'); } - if(!canIuse.test(data.appid)){ + if(!canIuse.test(data.appid)) { return returnJson('appid is invalid'); } - if(!data.key){ + if(!data.key) { return returnJson('key is required'); } - if(!canIuse.test(data.key)){ + if(!canIuse.test(data.key)) { return returnJson('key is invalid'); } - if(!data.group){ + if(!data.group) { //group is allow empty } - if(!canIuse.test(data.group)){ + if(!canIuse.test(data.group)) { return returnJson('group is invalid'); } //only alpha - if(data.type !== 'alpha'){ + if(data.type !== 'alpha') { return returnJson('type=alpha is required'); } - if(context.appid !== data.appid){ + if(context.appid !== data.appid) { return returnJson('appid is not match'); } - if(!context.appkey){ + if(!context.appkey) { return returnJson('get appkey error'); } var appid = context.appid; var appkey = context.appkey; - var reportKey = [appid,data.key].join('/'); + var reportKey = [appid, data.key].join('/'); var currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24); var logNumMax = context.MAX_ALPHA_LOG || MAX_ALPHA_LOG; logger.setKey(`report_${appid}_${data.key}`); //上报key - logger.debug('report log type: ${type}, key ${key}',data); + logger.debug('report log type: ${type}, key ${key}', data); - CD.curr(`SUM_TSW_ALPHA_LOG_KEY.${currDays}`,logNumMax,24 * 60 * 60).fail(function(err){ + CD.curr(`SUM_TSW_ALPHA_LOG_KEY.${currDays}`, logNumMax, 24 * 60 * 60).fail(function(err) { logger.debug(err && err.stack); returnJson('服务器错误'); - }).done(function(count){ + }).done(function(count) { count = ~~count; - if(count > logNumMax){ + if(count > logNumMax) { return returnJson('alpha log超限'); } - postOpenapi.report(reportKey,data.logText,data.logJson).done(function(isFirst){ - if(isFirst){ + postOpenapi.report(reportKey, data.logText, data.logJson).done(function(isFirst) { + if(isFirst) { //增加计数 - CD.checkByCmem(`SUM_TSW_ALPHA_LOG_KEY.${currDays}`, logNumMax, 24 * 60 * 60).done(function(curr){ + CD.checkByCmem(`SUM_TSW_ALPHA_LOG_KEY.${currDays}`, logNumMax, 24 * 60 * 60).done(function(curr) { logger.debug('curr count: ' + curr); }); }else{ - CD.checkByCmem(`SUM_TSW_ALPHA_LOG.${currDays}`, logNumMax * logNumMax * logNumMax, 24 * 60 * 60).done(function(curr){ + CD.checkByCmem(`SUM_TSW_ALPHA_LOG.${currDays}`, logNumMax * logNumMax * logNumMax, 24 * 60 * 60).done(function(curr) { logger.debug('curr count: ' + curr); }); } }); - if(!data.group){ + if(!data.group) { return returnJson(); } - if(!canIuse.test(data.group)){ + if(!canIuse.test(data.group)) { return returnJson(); } - var reportGroupKey = [appid,data.group,data.key].join('/'); + var reportGroupKey = [appid, data.group, data.key].join('/'); - postOpenapi.report(reportGroupKey,data.logText,data.logJson).done(function(isFirst){ + postOpenapi.report(reportGroupKey, data.logText, data.logJson).done(function(isFirst) { //这里不用计数了 - }).always(function(){ + }).always(function() { //上报分组 - CD.checkByCmem(`v2.group.${appid}.${data.group}`,1,60).fail(function(){ + CD.checkByCmem(`v2.group.${appid}.${data.group}`, 1, 60).fail(function() { returnJson(); - }).done(function(){ + }).done(function() { - var logText = postOpenapi.encode(appid,appkey,data.group); - var logJson = postOpenapi.encode(appid,appkey,{group: data.group}); + var logText = postOpenapi.encode(appid, appkey, data.group); + var logJson = postOpenapi.encode(appid, appkey, {group: data.group}); - postOpenapi.report(`${appid}/v2.group.alpha`,logText,logJson).always(function(){ + postOpenapi.report(`${appid}/v2.group.alpha`, logText, logJson).always(function() { returnJson(); }); }); @@ -557,11 +557,11 @@ module.exports.receiveCloud = function(req,res){ }; -module.exports.top100 = function(req, res){ +module.exports.top100 = function(req, res) { - var item,str,filename,result; + var item, str, filename, result; - if(!global.top100){ + if(!global.top100) { return; } @@ -573,14 +573,14 @@ module.exports.top100 = function(req, res){ header: httpUtil.getRequestHeaderStr(req), body: req.REQUEST.body, statusCode : res.statusCode, - resHeader: JSON.stringify(res._headers,null,2) + resHeader: JSON.stringify(res._headers, null, 2) }); - if(global.top100.length % 100 === 0){ + if(global.top100.length % 100 === 0) { logger.info('top: ' + global.top100.length); } - if(global.top100.length < 1000){ + if(global.top100.length < 1000) { return; } @@ -596,11 +596,11 @@ module.exports.top100 = function(req, res){ //分析pathname聚集 map = {}; arr = []; - result.forEach(function(v,i){ + result.forEach(function(v, i) { var key = v.hostname + v.pathname; - if(map[key]){ + if(map[key]) { map[key] ++; }else{ map[key] = 1; @@ -608,7 +608,7 @@ module.exports.top100 = function(req, res){ }); - for(key in map){ + for(key in map) { item = map[key]; @@ -619,14 +619,14 @@ module.exports.top100 = function(req, res){ } - arr.sort(function(a,b){ + arr.sort(function(a, b) { return b.count - a.count; }); - arr.forEach(function(v,i){ + arr.forEach(function(v, i) { //top 100 - if(i < 100){ + if(i < 100) { buffer.push(v.count + '\t' + v.key); buffer.push('\r\n'); } @@ -639,9 +639,9 @@ module.exports.top100 = function(req, res){ //分析ip聚集 map = {}; arr = []; - result.forEach(function(v,i){ + result.forEach(function(v, i) { - if(map[v.ip]){ + if(map[v.ip]) { map[v.ip] ++; }else{ map[v.ip] = 1; @@ -649,7 +649,7 @@ module.exports.top100 = function(req, res){ }); - for(key in map){ + for(key in map) { item = map[key]; @@ -660,14 +660,14 @@ module.exports.top100 = function(req, res){ } - arr.sort(function(a,b){ + arr.sort(function(a, b) { return b.count - a.count; }); - arr.forEach(function(v,i){ + arr.forEach(function(v, i) { //top 100 - if(i < 100){ + if(i < 100) { buffer.push(v.count + '\t' + v.key); buffer.push('\r\n'); } @@ -677,7 +677,7 @@ module.exports.top100 = function(req, res){ buffer.push('\r\n\r\n'); - result.forEach(function(v,i){ + result.forEach(function(v, i) { buffer.push(v.ip, ', socket ip: ' + v.socketIp); buffer.push('\r\n'); @@ -695,7 +695,7 @@ module.exports.top100 = function(req, res){ filename = __dirname + '/../../../proxy/cpu' + process.serverInfo.cpu + '.' + Date.now() + '.top100'; - fs.writeFile(filename,Buffer.from(str,'utf-8'),function(err){ + fs.writeFile(filename, Buffer.from(str, 'utf-8'), function(err) { logger.info('top100: ' + filename); }); diff --git a/bin/tsw/util/auto-report/post.js b/bin/tsw/util/auto-report/post.js index 071ef05b..c4e81fd2 100644 --- a/bin/tsw/util/auto-report/post.js +++ b/bin/tsw/util/auto-report/post.js @@ -17,61 +17,61 @@ const MAX_ALPHA_LOG = 1000; module.exports.MAX_ALPHA_LOG = MAX_ALPHA_LOG; -module.exports.cmem = function(){ +module.exports.cmem = function() { return cmemTSW.sz(); }; -module.exports.getLog = function(uin,limit){ - return this.getLogArr(uin,'text',null,limit); +module.exports.getLog = function(uin, limit) { + return this.getLogArr(uin, 'text', null, limit); }; -module.exports.getLogJson = function(uin,limit){ - return this.getLogArr(uin,'json',null,limit); +module.exports.getLogJson = function(uin, limit) { + return this.getLogArr(uin, 'json', null, limit); }; -module.exports.getLogJsonByKey = function (uin,key) { +module.exports.getLogJsonByKey = function (uin, key) { var prefix = this.keyJson(''); var keyJson; - if(String(key).startsWith(prefix)){ + if(String(key).startsWith(prefix)) { keyJson = key; }else{ keyJson = this.keyJson(key); } - return this.getLogArr(uin,'json',keyJson); + return this.getLogArr(uin, 'json', keyJson); }; -module.exports.getLogArr = function(uin,type,key,limit){ +module.exports.getLogArr = function(uin, type, key, limit) { var defer = Deferred.create(); var memcached = this.cmem(); var keyBitmap = this.keyBitmap(uin); - if(!memcached){ + if(!memcached) { return defer.resolve([]); } - memcached.get(keyBitmap,function(err,data){ + memcached.get(keyBitmap, function(err, data) { var start = data || 0; - var keyTextArr = type === 'text' ? module.exports.keyTextArr(uin,start) : module.exports.keyJsonArr(uin,start); - var keyJsonArr = module.exports.keyJsonArr(uin,start); + var keyTextArr = type === 'text' ? module.exports.keyTextArr(uin, start) : module.exports.keyJsonArr(uin, start); + var keyJsonArr = module.exports.keyJsonArr(uin, start); //如果传递进来key,则只需要关注传进来的key - if(key){ + if(key) { keyTextArr = [key]; } - if(limit && keyTextArr.length > limit){ + if(limit && keyTextArr.length > limit) { keyTextArr.length = limit; keyJsonArr.length = limit; } - memcached.get(keyTextArr,function(err,data){ + memcached.get(keyTextArr, function(err, data) { - if(err){ + if(err) { logger.error('bad key:'); logger.error(keyTextArr); logger.error((err && err.stack) || err); @@ -79,24 +79,24 @@ module.exports.getLogArr = function(uin,type,key,limit){ return; } - var i,len,index; - var arr = [],keys = [],SNKeys = [],extInfos = []; //keys和SNKeys挂在arr下面,保持跟arr同序。 - var currKey,tmpInfo = {}; + var i, len, index; + var arr = [], keys = [], SNKeys = [], extInfos = []; //keys和SNKeys挂在arr下面,保持跟arr同序。 + var currKey, tmpInfo = {}; - for(len = keyTextArr.length,i = 0; i < len ;i++){ + for(len = keyTextArr.length, i = 0; i < len ;i++) { index = i; currKey = keyTextArr[index]; tmpInfo = {}; - if(data[currKey]){ - if(context.appid && context.appkey){ + if(data[currKey]) { + if(context.appid && context.appkey) { //解密 - data[currKey] = module.exports.decode(context.appid,context.appkey,data[currKey]); + data[currKey] = module.exports.decode(context.appid, context.appkey, data[currKey]); } arr.push(data[currKey]); keys.push(keyJsonArr[index].split('.').slice(-1)); - if(type == 'json'){ + if(type == 'json') { tmpInfo = data[currKey].curr;//内含resultCode SNKeys.push(module.exports.getLogSN(data[currKey].curr && data[currKey].curr.logText)); }else{ @@ -124,18 +124,18 @@ module.exports.getLogArr = function(uin,type,key,limit){ module.exports.getLogSN = function (logText) { var SNReg = /\[(\d+\scpu\d+\s\d+)\]/; - if(logText){ + if(logText) { logText = logText.match && logText.match(SNReg) && logText.match(SNReg)[1] || 'unknown'; - return logText.replace(/\s/igm,''); + return logText.replace(/\s/igm, ''); } return 'unknown'; }; -module.exports.getLogFromReg = function (logText,reg) { - if(logText && reg){ +module.exports.getLogFromReg = function (logText, reg) { + if(logText && reg) { logText = logText.match && logText.match(reg) && logText.match(reg)[1] || 'unknown'; - return logText.replace(/\s/igm,''); + return logText.replace(/\s/igm, ''); } return ''; @@ -143,17 +143,17 @@ module.exports.getLogFromReg = function (logText,reg) { module.exports.getLogResultCode = function (logText) { var reg = /response\s(\d+)\s\{/; - return this.getLogFromReg(logText,reg); + return this.getLogFromReg(logText, reg); }; -module.exports.report = function(key,logText,logJson){ +module.exports.report = function(key, logText, logJson) { var defer = Deferred.create(); - if(!key){ + if(!key) { return defer.reject(); } - if(!logText){ + if(!logText) { return defer.reject(); } @@ -164,25 +164,25 @@ module.exports.report = function(key,logText,logJson){ var keyJson = module.exports.keyJson(key); var keyBitmap = module.exports.keyBitmap(key); - if(!memcached){ + if(!memcached) { return defer.resolve(); } - memcached.add(keyBitmap,0,24 * 60 * 60,function(err,ret){ + memcached.add(keyBitmap, 0, 24 * 60 * 60, function(err, ret) { var isFirst = false; - if(err){ + if(err) { //add err是正常的 //return; } - if(ret === true){ + if(ret === true) { isFirst = true; } - memcached.incr(keyBitmap, 1, function(err, result){ - if(err){ + memcached.incr(keyBitmap, 1, function(err, result) { + if(err) { logger.error(err.stack); defer.reject(); return; @@ -190,12 +190,12 @@ module.exports.report = function(key,logText,logJson){ var index = 0; - if(typeof result === 'number'){ + if(typeof result === 'number') { index = result % MAX_NUM; } - memcached.set([keyText,index].join('.'),logText,24 * 60 * 60,function(err,ret){ - memcached.set([keyJson,index].join('.'),logJson,24 * 60 * 60,function(err,ret){ + memcached.set([keyText, index].join('.'), logText, 24 * 60 * 60, function(err, ret) { + memcached.set([keyJson, index].join('.'), logJson, 24 * 60 * 60, function(err, ret) { defer.resolve(isFirst); }); }); @@ -207,44 +207,44 @@ module.exports.report = function(key,logText,logJson){ }; -module.exports.keyBitmap = function(key){ +module.exports.keyBitmap = function(key) { var currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24); - return ['bitmap.v4.log',currDays,key].join('.'); + return ['bitmap.v4.log', currDays, key].join('.'); }; -module.exports.keyJson = function(key){ +module.exports.keyJson = function(key) { var currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24); - return ['json.v4.log',currDays,key].join('.'); + return ['json.v4.log', currDays, key].join('.'); }; -module.exports.keyText = function(key){ +module.exports.keyText = function(key) { var currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24); - return ['text.v4.log',currDays,key].join('.'); + return ['text.v4.log', currDays, key].join('.'); }; -module.exports.keyJsonArr = function(key,index){ +module.exports.keyJsonArr = function(key, index) { var i = 0; var arr = []; var keyJson = this.keyJson(key); index = index || 0; - for(i = MAX_NUM; i > 0; i--){ - arr.push([keyJson,(i + index)%MAX_NUM].join('.')); + for(i = MAX_NUM; i > 0; i--) { + arr.push([keyJson, (i + index)%MAX_NUM].join('.')); } return arr; }; -module.exports.keyTextArr = function(key,index){ +module.exports.keyTextArr = function(key, index) { var i = 0; var arr = []; var keyText = this.keyText(key); index = index || 0; - for(i = MAX_NUM; i > 0; i--){ - arr.push([keyText,(i + index)%MAX_NUM].join('.')); + for(i = MAX_NUM; i > 0; i--) { + arr.push([keyText, (i + index)%MAX_NUM].join('.')); } return arr; @@ -252,38 +252,38 @@ module.exports.keyTextArr = function(key,index){ //加密 -module.exports.encode = function(appid,appkey,data){ - var input = Buffer.from(JSON.stringify(data),'UTF-8'); +module.exports.encode = function(appid, appkey, data) { + var input = Buffer.from(JSON.stringify(data), 'UTF-8'); var buff = zlib.deflateSync(input); - var des = crypto.createCipher('des',(appid + appkey)); - var buf1 = des.update(buff, null , 'hex'); + var des = crypto.createCipher('des', (appid + appkey)); + var buf1 = des.update(buff, null, 'hex'); var buf2 = des.final('hex'); - var body = Buffer.from(buf1 + buf2,'hex').toString('base64'); + var body = Buffer.from(buf1 + buf2, 'hex').toString('base64'); return body; }; //解密 -module.exports.decode = function(appid,appkey,body){ - var des = crypto.createDecipher('des',(appid + appkey)); +module.exports.decode = function(appid, appkey, body) { + var des = crypto.createDecipher('des', (appid + appkey)); var buf1 = ''; var buf2 = ''; try{ - buf1 = des.update(body,'base64', 'hex'); + buf1 = des.update(body, 'base64', 'hex'); buf2 = des.final('hex'); - }catch(e){ + }catch(e) { logger.error(e.stack); return null; } - var buff = Buffer.from(buf1 + buf2 ,'hex'); + var buff = Buffer.from(buf1 + buf2, 'hex'); var input = zlib.inflateSync(buff); var data = null; try{ data = JSON.parse(input.toString('UTF-8')); - }catch(e){ + }catch(e) { logger.error(e.stack); return null; } diff --git a/bin/tsw/util/auto-report/post.openapi.js b/bin/tsw/util/auto-report/post.openapi.js index 60adfcdb..54e040e2 100644 --- a/bin/tsw/util/auto-report/post.openapi.js +++ b/bin/tsw/util/auto-report/post.openapi.js @@ -11,9 +11,9 @@ const cmemTSW = require('data/cmem.tsw.js'); const post = require('./post.js'); -module.exports = Object.assign({},post); +module.exports = Object.assign({}, post); -module.exports.cmem = function(){ +module.exports.cmem = function() { return cmemTSW.openapi(); }; diff --git a/bin/tsw/util/auto-report/src/_config.js b/bin/tsw/util/auto-report/src/_config.js index 0a1aae4c..9cb4d1d3 100644 --- a/bin/tsw/util/auto-report/src/_config.js +++ b/bin/tsw/util/auto-report/src/_config.js @@ -7,7 +7,7 @@ */ 'use strict'; -define(function(require, exports, module){ +define(function(require, exports, module) { return { diff --git a/bin/tsw/util/auto-report/tmpl.js b/bin/tsw/util/auto-report/tmpl.js index 5ff4da38..77ce0fb3 100644 --- a/bin/tsw/util/auto-report/tmpl.js +++ b/bin/tsw/util/auto-report/tmpl.js @@ -4,28 +4,36 @@ //auto-report/src/mail.tmpl.html //auto-report/src/view.tmpl.html //auto-report/src/zenburn.tmpl.html -define(function(require, exports, module){ +define(function(require, exports, module) { var tmpl = { - 'encodeHtml': function(s){return (s+'').replace(/[\x26\x3c\x3e\x27\x22\x60]/g,function($0){return '&#'+$0.charCodeAt(0)+';';});}, + 'encodeHtml': function(s) { + return (s+'').replace(/[\x26\x3c\x3e\x27\x22\x60]/g, function($0) { + return '&#'+$0.charCodeAt(0)+';'; + }); + }, - 'download_content_types': function(data){ + 'download_content_types': function(data) { - var __p=[],_p=function(s){__p.push(s);},out=_p; + var __p=[], _p=function(s) { + __p.push(s); + }, out=_p; __p.push('\n\n\n\n\n'); return __p.join(''); }, - 'download_index': function(data){ + 'download_index': function(data) { - var __p=[],_p=function(s){__p.push(s);},out=_p; + var __p=[], _p=function(s) { + __p.push(s); + }, out=_p; data = data || []; var i, len, entry; __p.push('\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n '); - for(i = 0, len = data.length; i < len; i++){ + for(i = 0, len = data.length; i < len; i++) { entry = (data[i] || {}).curr || {}; __p.push(' \n
 #ResultProtocolHostURLBodyCachingContent-TypeProcessCommentsCustomServerIP
\n 0 ? '-' : '+') + pad(parseInt(Math.abs(tz / 60))); - if (tz === 0){ + if (tz === 0) { tzs = 'Z'; - }else if (tz % 60 != 0){ + }else if (tz % 60 != 0) { tzs += pad(Math.abs(tz % 60)); }else{ tzs += ':00'; @@ -148,12 +160,16 @@ define(function(require, exports, module){ return __p.join(''); }, - 'errorSummary': function(data){ + 'errorSummary': function(data) { - var __p=[],_p=function(s){__p.push(s);},out=_p; + var __p=[], _p=function(s) { + __p.push(s); + }, out=_p; __p.push('\n\n \n \n \n \n \n \n \n \n \n\n \n\n\n'); - if(data.total_arr.length){__p.push('\n \n \n \n \n \n \n \n \n '); - for(var i = 0,item; i < data.total_arr.length; i++){__p.push(' item = data.total_arr[i++];\n \n \n '); }__p.push(' \n
项目名称mod_act报错次数负责人
'); _p(item.config.title); @@ -165,15 +181,18 @@ define(function(require, exports, module){ _p(item.config.charge); __p.push('
'); - }else{__p.push('

没有报错项目~

'); + }else{ + __p.push('

没有报错项目~

'); }__p.push('\n'); return __p.join(''); }, - 'error': function(data){ + 'error': function(data) { - var __p=[],_p=function(s){__p.push(s);},out=_p; + var __p=[], _p=function(s) { + __p.push(s); + }, out=_p; __p.push('\n\n \n \n \n \n \n \n \n \n \n\n \n\n\n\n

'); _p(data.mod_act); __p.push(' '); @@ -185,18 +204,20 @@ define(function(require, exports, module){ return __p.join(''); }, - 'log_view': function(data){ + 'log_view': function(data) { - var __p=[],_p=function(s){__p.push(s);},out=_p; + var __p=[], _p=function(s) { + __p.push(s); + }, out=_p; var logArr = data.logArr || []; var groupArr= data.groupArr || []; var window = context.window || {}; var hls = require('./highlight-tsw.js'); var getResultCodeStyle = function (code) { - code = parseInt(code,10); + code = parseInt(code, 10); var style = ''; - if(code > 0){ + if(code > 0) { code = Math.floor(code / 100); switch (code) { case 2 : @@ -221,25 +242,25 @@ define(function(require, exports, module){ var group = context.group; var key = context.key; var createLogKey = context.createLogKey; - var currPath = '/log/view/' + createLogKey(appid,group,key); + var currPath = '/log/view/' + createLogKey(appid, group, key); //去重 - var tmp = [{name: '全部',href: '/log/view/' + createLogKey(appid,'',key)}]; + var tmp = [{name: '全部', href: '/log/view/' + createLogKey(appid, '', key)}]; var groupMap= {}; var nameMap = data.nameMap; - for(var i in nameMap){ + for(var i in nameMap) { tmp.push({ name : nameMap[i], - href : '/log/view/' + createLogKey(appid,i,key) + href : '/log/view/' + createLogKey(appid, i, key) }); } - groupArr.sort().forEach(function(v){ - if(!v){ + groupArr.sort().forEach(function(v) { + if(!v) { return; } - if(groupMap[v]){ + if(groupMap[v]) { return; } //默认展示的就去掉 @@ -247,17 +268,19 @@ define(function(require, exports, module){ return; groupMap[v] = 1; - var href = '/log/view/' + createLogKey(appid,v,key); + var href = '/log/view/' + createLogKey(appid, v, key); tmp.push({name: v, href: href}); }); groupArr = tmp; var XSS = plug('util/xss.js'); __p.push('\n\n \n TSW云抓包™\n \n \n\n\n

\n 下载全部抓包\n Fiddler下载\n Whistle下载(mac推荐)\n \n '); - if(appid){__p.push(' 实时监控\n 测试环境'); - }else{__p.push(' 临时染色'); + if(appid) { + __p.push(' 实时监控\n 测试环境'); + }else{ + __p.push(' 临时染色'); }__p.push(' \n
'); - if(groupArr.length){__p.push(' '); + }if(logArr.length === 0) { + __p.push('
\n

还没有实时log

\n
'); } - logArr.forEach(function(logText,i){ + logArr.forEach(function(logText, i) { logText = logText || ''; var logLineArr = logText.split('\n'); var reqUrl = logLineArr && logLineArr[0]; @@ -285,7 +311,7 @@ define(function(require, exports, module){ resultCode = logArr.extInfos[i].resultCode; var hasError = false; - for(var j = 0; j < logLineArr.length; j++){ + for(var j = 0; j < logLineArr.length; j++) { var logTextLine = logLineArr[j]; if (!logTextLine) { continue; @@ -302,14 +328,14 @@ define(function(require, exports, module){ } //匹配ERRO log - if(!hasError && logTextLine.indexOf(' [ERRO] ') > 0){ + if(!hasError && logTextLine.indexOf(' [ERRO] ') > 0) { hasError = true; break; } } - if(i < 2){ - logText = hls.highlight('tswlog',(logText),true).value; + if(i < 2) { + logText = hls.highlight('tswlog', (logText), true).value; }else{ logText = XSS.htmlEncode(logText); } @@ -325,13 +351,13 @@ define(function(require, exports, module){ __p.push('">'); _p(resultCode); __p.push('\n\n
点击下载 云抓包™.saz\n\n '); @@ -28,9 +36,11 @@ define(function(require, exports, module){ return __p.join(''); }, - 'html': function(data){ + 'html': function(data) { - var __p=[],_p=function(s){__p.push(s);},out=_p; + var __p=[], _p=function(s) { + __p.push(s); + }, out=_p; __p.push(' '); data = data || {}; @@ -44,7 +54,8 @@ define(function(require, exports, module){ __p.push('" />\n '); - if(head.share_image){__p.push(' '); }__p.push(' \n \n \n \n \n \n \n \n \n \n '); diff --git a/bin/tsw/util/h5-test/group/view.js b/bin/tsw/util/h5-test/group/view.js index 08a7b406..dcf57b11 100644 --- a/bin/tsw/util/h5-test/group/view.js +++ b/bin/tsw/util/h5-test/group/view.js @@ -12,7 +12,7 @@ const gzipHttp = require('util/gzipHttp.js'); const tmpl = require('./tmpl.js'); const pagetmpl = require('../page/tmpl.js'); -module.exports = function(request,response){ +module.exports = function(request, response) { var gzipResponse = gzipHttp.getGzipResponse({ request: request, @@ -23,7 +23,7 @@ module.exports = function(request,response){ var navMenus = context.navMenus; - if(!navMenus){ + if(!navMenus) { navMenus = [ { href: '/group/page', @@ -44,7 +44,7 @@ module.exports = function(request,response){ ]; } - TEReport.getAllGroup().done(function(allGroup){ + TEReport.getAllGroup().done(function(allGroup) { var data = {}; diff --git a/bin/tsw/util/h5-test/is-test.js b/bin/tsw/util/h5-test/is-test.js index 9a14a92b..a488f84d 100644 --- a/bin/tsw/util/h5-test/is-test.js +++ b/bin/tsw/util/h5-test/is-test.js @@ -22,8 +22,8 @@ const openapi = require('util/openapi'); var isFirstLoad = false; //以下是提高稳定性用的 -function getFileCacheKey(project,key){ - var cacheFilename = [project,key.replace(/\?.+$/,'')].join('/'); +function getFileCacheKey(project, key) { + var cacheFilename = [project, key.replace(/\?.+$/, '')].join('/'); cacheFilename += '.cache'; return cacheFilename; @@ -34,22 +34,22 @@ function getFileCacheKey(project,key){ * 更新缓存 * */ -function updateFileCache(project,key,text){ +function updateFileCache(project, key, text) { text = text || JSON.stringify({}); - var cacheFilename = getFileCacheKey(project,key); + var cacheFilename = getFileCacheKey(project, key); //保存到文件 - fileCache.set(cacheFilename,Buffer.from(text,'UTF-8')); + fileCache.set(cacheFilename, Buffer.from(text, 'UTF-8')); } //监听restart事件 -if(!global[ __filename + '.restart']){ +if(!global[ __filename + '.restart']) { global[ __filename + '.restart'] = true; isFirstLoad = true; } -if(isFirstLoad){ - cluster.worker && cluster.worker.once('disconnect', function(worker){ +if(isFirstLoad) { + cluster.worker && cluster.worker.once('disconnect', function(worker) { logger.info('save h5test on disconnect event...'); onDisconnect('restart'); }); @@ -57,11 +57,11 @@ if(isFirstLoad){ function onDisconnect (type) { type = type || 'restart'; - if(global[__filename]){ + if(global[__filename]) { //有数据才fileCache try{ - updateFileCache('h5test','test.user.list',JSON.stringify(global[__filename])); - }catch(e){ + updateFileCache('h5test', 'test.user.list', JSON.stringify(global[__filename])); + }catch(e) { //依然谨慎 } } @@ -75,15 +75,15 @@ var getTimeout = 60000; var lastUpdateTime = 0; //获取测试用户 -var getTestUserMap = function(){ +var getTestUserMap = function() { //看下fileCache里面有没有 - if(!global[__filename]){ + if(!global[__filename]) { //进入这个逻辑,是worker restart后的一分钟内的[第一次],从硬盘同步数据过来;一旦取到数据之后,这里将不再执行 global[__filename] = module.exports.getTestUserMapFromFileCache() || {}; } - if(Date.now() - lastUpdateTime > getTimeout || isWindows.isWindows){ + if(Date.now() - lastUpdateTime > getTimeout || isWindows.isWindows) { lastUpdateTime = Date.now(); syncFromMemcachedOrCloud(); } @@ -91,30 +91,30 @@ var getTestUserMap = function(){ return global[__filename] || {}; }; -var syncFromMemcachedOrCloud = function(){ - if(config.appid && config.appkey){ +var syncFromMemcachedOrCloud = function() { + if(config.appid && config.appkey) { return syncFromCloud(); } return syncFromMemcached(); }; -var syncFromMemcached = function(){ +var syncFromMemcached = function() { //从内存中读取testTargetMap var memcached = module.exports.cmem(); var keyText = module.exports.keyBitmap(); - if(!memcached){ + if(!memcached) { return; } - memcached.get(keyText,function(err,data){ - if(err){ + memcached.get(keyText, function(err, data) { + if(err) { logger.error('memcache get error:' + err); data = {}; } - if(data === true){ + if(data === true) { logger.error('memcache get data true'); data = {}; } @@ -125,24 +125,24 @@ var syncFromMemcached = function(){ alpha.update(global[__filename]); //服务器上超时逻辑里面不做fileCache,较少不必要的磁盘IO;windows环境下一般不会走到这里,just for jest - if(isWindows.isWindows){ + if(isWindows.isWindows) { onDisconnect('test.in.windows'); } }); }; //从云端同步 -var syncFromCloud = function(merge){ +var syncFromCloud = function(merge) { - if(!config.appid){ + if(!config.appid) { return; } - if(!config.appkey){ + if(!config.appkey) { return; } - if(!config.h5testSyncUrl){ + if(!config.h5testSyncUrl) { return; } @@ -171,22 +171,22 @@ var syncFromCloud = function(merge){ keepAlive : true, autoToken : false, dataType : 'json' - }).fail(function(){ + }).fail(function() { logger.error('syncFromCloud fail.'); - if(merge === 'merge'){ + if(merge === 'merge') { return; } global[__filename] = {}; - }).done(function(d){ + }).done(function(d) { var data = null; - if(d.result && d.result.code === 0){ + if(d.result && d.result.code === 0) { data = d.result.data || {}; } - if(merge === 'merge'){ + if(merge === 'merge') { //追加 global[__filename] = global[__filename] || {}; - Object.assign(global[__filename],data); + Object.assign(global[__filename], data); }else{ //覆盖 global[__filename] = data || {}; @@ -196,7 +196,7 @@ var syncFromCloud = function(merge){ alpha.update(global[__filename]); //服务器上超时逻辑里面不做fileCache,较少不必要的磁盘IO;windows环境下一般不会走到这里,just for jest - if(isWindows.isWindows){ + if(isWindows.isWindows) { onDisconnect('test.in.windows'); } @@ -205,14 +205,14 @@ var syncFromCloud = function(merge){ }; //是否命中测试环境 -module.exports.getTestSpaceInfo = function(req){ +module.exports.getTestSpaceInfo = function(req) { //windows版本,更不用转 - if(isWindows.isWindows){ + if(isWindows.isWindows) { return; } //安全中心请求,也不用转了~ - if(isTST.isTST(req)){ + if(isTST.isTST(req)) { return; } @@ -225,7 +225,7 @@ module.exports.getTestSpaceInfo = function(req){ var testTargetMap = getTestUserMap(); //如果已经是测试环境,就不用转发了 - if(config.isTest){ + if(config.isTest) { return; } @@ -239,12 +239,12 @@ module.exports.getTestSpaceInfo = function(req){ && testTargetMap[uin] && typeof testTargetMap[uin] === 'string' && testTargetMap[uin].split('.').length == 4 - ){ + ) { testIp = testTargetMap[uin]; let arr = testIp.split(':'); - if(arr.length === 2){ + if(arr.length === 2) { testIp = arr[0]; testPort = ~~arr[1]; } @@ -265,10 +265,10 @@ module.exports.getTestSpaceInfo = function(req){ //是否命中测试环境 //命中则直接转发请求,return true;不命中则return false -module.exports.isTestUser = function(req, res){ +module.exports.isTestUser = function(req, res) { var testSpaceInfo = module.exports.getTestSpaceInfo(req); - if(!testSpaceInfo){ + if(!testSpaceInfo) { return false; } @@ -283,7 +283,7 @@ module.exports.isTestUser = function(req, res){ context.mod_act = 'h5_test'; - ajax.proxy(req,res).request({ + ajax.proxy(req, res).request({ url : reqUrl, type : req.method, dataType : 'proxy', @@ -303,8 +303,8 @@ module.exports.isTestUser = function(req, res){ dcapi :{ key : 'EVENT_TSW_HTTP_H5_TEST' } - }).done(function(d){ - }).fail(function(d){ + }).done(function(d) { + }).fail(function(d) { logger.error('h5test proxy fail...'); res.setHeader('Content-Type', 'text/html; charset=UTF-8'); res.writeHead(500); @@ -315,12 +315,12 @@ module.exports.isTestUser = function(req, res){ }; //cmem对象 -module.exports.cmem = function(){ +module.exports.cmem = function() { return cmemTSW.h5test(); }; //uin对应的存储key,每天一变 -module.exports.keyBitmap = function(uin){ +module.exports.keyBitmap = function(uin) { var currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24); return 'bitmap.h5.test.' + currDays; }; @@ -329,24 +329,24 @@ module.exports.keyBitmap = function(uin){ module.exports.getTestUserMap = getTestUserMap; module.exports.getTestUserMapFromFileCache = function () { - var fileCacheKey = getFileCacheKey('h5test','test.user.list'); + var fileCacheKey = getFileCacheKey('h5test', 'test.user.list'); var localData = fileCache.getSync(fileCacheKey).data; var localJSON = {}; - if(localData){ + if(localData) { //发现有数据 try{ localJSON = JSON.parse(localData.toString('utf-8')); - }catch(e){ + }catch(e) { //加个try catch是谨慎一点,防止出现一些奇奇怪怪的问题 } } - if(typeof localJSON !== 'object'){ + if(typeof localJSON !== 'object') { localJSON = {}; } - if(!localJSON){ + if(!localJSON) { localJSON = {}; } diff --git a/bin/tsw/util/h5-test/page/src/_config.js b/bin/tsw/util/h5-test/page/src/_config.js index 37830df4..7895835e 100644 --- a/bin/tsw/util/h5-test/page/src/_config.js +++ b/bin/tsw/util/h5-test/page/src/_config.js @@ -1,7 +1,7 @@ /** * 打包配置 */ -define(function(require, exports, module){ +define(function(require, exports, module) { return { diff --git a/bin/tsw/util/h5-test/page/tmpl.js b/bin/tsw/util/h5-test/page/tmpl.js index 853b8b63..3e75f1f2 100644 --- a/bin/tsw/util/h5-test/page/tmpl.js +++ b/bin/tsw/util/h5-test/page/tmpl.js @@ -3,13 +3,19 @@ //page/src/main.tmpl.html //page/src/new.tmpl.html //page/src/new_body.tmpl.html -define(function(require, exports, module){ +define(function(require, exports, module) { var tmpl = { - 'encodeHtml': function(s){return (s+'').replace(/[\x26\x3c\x3e\x27\x22\x60]/g,function($0){return '&#'+$0.charCodeAt(0)+';';});}, + 'encodeHtml': function(s) { + return (s+'').replace(/[\x26\x3c\x3e\x27\x22\x60]/g, function($0) { + return '&#'+$0.charCodeAt(0)+';'; + }); + }, - 'new_main': function(data){ + 'new_main': function(data) { - var __p=[],_p=function(s){__p.push(s);},out=_p; + var __p=[], _p=function(s) { + __p.push(s); + }, out=_p; data = data || {}; @@ -22,26 +28,32 @@ define(function(require, exports, module){ __p.push('" />\n '); - if(head.share_image){__p.push(' '); }__p.push(' \n \n \n \n \n \n \n \n \n \n'); - if(body === undefined || body === null || body ==''){__p.push('

加载中,请稍候...

'); - }else{_p(body); + if(body === undefined || body === null || body =='') { + __p.push('

加载中,请稍候...

'); + }else{ + _p(body); }__p.push('\n'); return __p.join(''); }, - 'new_header': function(data){ + 'new_header': function(data) { - var __p=[],_p=function(s){__p.push(s);},out=_p; + var __p=[], _p=function(s) { + __p.push(s); + }, out=_p; __p.push('\n
'); - if(context.noticeMessage) { __p.push('
'); + if(context.noticeMessage) { + __p.push('
'); _p(context.noticeMessage); __p.push('
'); } __p.push(''); @@ -58,9 +71,11 @@ define(function(require, exports, module){ return __p.join(''); }, - 'new_body_sync': function(data){ + 'new_body_sync': function(data) { - var __p=[],_p=function(s){__p.push(s);},out=_p; + var __p=[], _p=function(s) { + __p.push(s); + }, out=_p; __p.push(' '); _p(tmpl.new_body(data)); __p.push(' \n
\n

'); _p(data.project && data.project.name || 'TSW 测试环境配置'); __p.push('

\n

注意:每天0点将清空号码

\n
\n
\n
\n
\n

'); @@ -86,7 +103,7 @@ define(function(require, exports, module){ data = data.group; - for(var i = 0; i < data.length; i++){ + for(var i = 0; i < data.length; i++) { var item = data[i]; var val = item['ip'] + (item['port'] && (':' + item['port']) || ''); var name= item['name']; @@ -100,7 +117,7 @@ define(function(require, exports, module){ } if(!name) { - if(module && module != ''){ + if(module && module != '') { name = module; }else { name = val; @@ -123,28 +140,34 @@ define(function(require, exports, module){ } __p.push(' '); - if(isShow){ __p.push('
\n '); - if(/^[0-9.:]+$/.test(val)){ __p.push(' '); _p(val); __p.push(''); - if(isShowDesc){ __p.push(''); + if(isShowDesc) { + __p.push(''); _p(desc); __p.push(''); } __p.push(' '); - }else{ __p.push(' '); + }else{ + __p.push(' '); _p(val); __p.push(''); - if(isShowDesc){ __p.push(''); + if(isShowDesc) { + __p.push(''); _p(desc); __p.push(''); } __p.push(' '); diff --git a/bin/tsw/util/h5-test/page/view.js b/bin/tsw/util/h5-test/page/view.js index 112d47f6..555b94db 100644 --- a/bin/tsw/util/h5-test/page/view.js +++ b/bin/tsw/util/h5-test/page/view.js @@ -46,7 +46,7 @@ module.exports.go = async function(request, response, plug) { var navMenus = context.navMenus; - if(!navMenus){ + if(!navMenus) { navMenus = [ { href: '/group/page', diff --git a/bin/tsw/util/home/src/_config.js b/bin/tsw/util/home/src/_config.js index 37830df4..7895835e 100644 --- a/bin/tsw/util/home/src/_config.js +++ b/bin/tsw/util/home/src/_config.js @@ -1,7 +1,7 @@ /** * 打包配置 */ -define(function(require, exports, module){ +define(function(require, exports, module) { return { diff --git a/bin/tsw/util/home/tmpl.js b/bin/tsw/util/home/tmpl.js index 9af12452..859c70be 100644 --- a/bin/tsw/util/home/tmpl.js +++ b/bin/tsw/util/home/tmpl.js @@ -1,21 +1,29 @@ //tmpl file list: //home/src/main.tmpl.html -define(function(require, exports, module){ +define(function(require, exports, module) { var tmpl = { - 'encodeHtml': function(s){return (s+'').replace(/[\x26\x3c\x3e\x27\x22\x60]/g,function($0){return '&#'+$0.charCodeAt(0)+';';});}, + 'encodeHtml': function(s) { + return (s+'').replace(/[\x26\x3c\x3e\x27\x22\x60]/g, function($0) { + return '&#'+$0.charCodeAt(0)+';'; + }); + }, - 'body': function(data){ + 'body': function(data) { - var __p=[],_p=function(s){__p.push(s);},out=_p; + var __p=[], _p=function(s) { + __p.push(s); + }, out=_p; __p.push('

TSW

\n

Tencent Server Web

\n

针对web前端开发同学的server端web开发解决方案。

'); return __p.join(''); }, - 'html': function(data){ + 'html': function(data) { - var __p=[],_p=function(s){__p.push(s);},out=_p; + var __p=[], _p=function(s) { + __p.push(s); + }, out=_p; __p.push(' '); data = data || {}; @@ -29,7 +37,8 @@ define(function(require, exports, module){ __p.push('" />\n '); - if(head.share_image){__p.push(' '); }__p.push(' \n \n \n \n \n \n \n \n \n \n '); @@ -41,15 +50,18 @@ define(function(require, exports, module){ return __p.join(''); }, - 'new_header': function(data){ + 'new_header': function(data) { - var __p=[],_p=function(s){__p.push(s);},out=_p; + var __p=[], _p=function(s) { + __p.push(s); + }, out=_p; __p.push('\n