From 59bb79ad70f85b83ee79328090eb5adbbc7dcfc1 Mon Sep 17 00:00:00 2001 From: timcui Date: Fri, 25 May 2018 15:50:44 +0800 Subject: [PATCH 1/5] chore: fix eslint dismatch rules --- bin/proxy/http.proxy.js | 2 +- bin/proxy/ws.route.js | 12 +- bin/tsw/ajax/ajax.js | 226 ++++++++++++++++----------------- bin/tsw/util/Deferred/index.js | 6 +- bin/tsw/util/http/parseGet.js | 48 +++---- bin/tsw/util/logger/logger.js | 1 - 6 files changed, 146 insertions(+), 149 deletions(-) diff --git a/bin/proxy/http.proxy.js b/bin/proxy/http.proxy.js index a0a1329a..ceb36aa6 100644 --- a/bin/proxy/http.proxy.js +++ b/bin/proxy/http.proxy.js @@ -158,7 +158,7 @@ function requestHandler(req, res) { cleanCache(); } - if('upgrade' === req.headers.connection && 'websocket' === req.headers.upgrade) { + if(req.headers.connection === 'upgrade' && req.headers.upgrade === 'websocket') { //websocket return; } diff --git a/bin/proxy/ws.route.js b/bin/proxy/ws.route.js index 0d416a6e..c50d077a 100644 --- a/bin/proxy/ws.route.js +++ b/bin/proxy/ws.route.js @@ -25,12 +25,12 @@ exports.doRoute = function(ws, type, d1, d2) { } if (typeof moduleObj.onConnection != 'function') { moduleObj.onConnection = function(ws) { - 1 == ws.readyState && ws.send('no onConnection funtion,so go default'); + ws.readyState === 1 && ws.send('no onConnection funtion,so go default'); }; } if (typeof moduleObj.onMessage != 'function') { moduleObj.onMessage = function(ws, data) { - 1 == ws.readyState && ws.send('no onMessage function,so go default,ws server get message:' + data); + ws.readyState === 1 && ws.send('no onMessage function,so go default,ws server get message:' + data); }; } if (typeof moduleObj.onClose != 'function') { @@ -43,14 +43,14 @@ exports.doRoute = function(ws, type, d1, d2) { logger.debug('no onError function, so go default'); }; } - if ('connection' == type) { + if (type === 'connection') { moduleObj.onConnection(ws); - } else if ('message' == type) { + } else if (type === 'message') { contextMod.currentContext().mod_act = mod_act; moduleObj.onMessage(ws, d1); - } else if ('close' == type) { + } else if (type === 'close') { moduleObj.onClose(ws, d1, d2); - } else if ('error' == type) { + } else if (type === 'error') { moduleObj.onError(ws, d1); } }; diff --git a/bin/tsw/ajax/ajax.js b/bin/tsw/ajax/ajax.js index 28b95ed0..da88da27 100644 --- a/bin/tsw/ajax/ajax.js +++ b/bin/tsw/ajax/ajax.js @@ -26,8 +26,6 @@ const isTST = require('util/isTST.js'); const optionsUtil = require('./http-https.options.js'); const sbFunction = vm.runInNewContext('(Function)', Object.create(null)); //沙堆 let lastRetry = 0; -let undefined; - module.exports = new Ajax(); @@ -74,7 +72,7 @@ Ajax.prototype.request = function(opt) { }else{ return this.doRequest(opt); } - + }; @@ -85,12 +83,12 @@ Ajax.prototype.dnsRequest = function(opt) { //L5带你去ajax Ajax.prototype.l5Request = function(opt) { - - let + + let defer = Deferred.create(), that = this, l5api; - + l5api = Deferred.extend({ modid: 0, cmd: 0 @@ -104,17 +102,17 @@ Ajax.prototype.l5Request = function(opt) { //windows不走L5,直接请求 return this.doRequest(opt); } - + L5.ApiGetRoute(l5api).done(function(route) { - + const start = new Date(); - + opt.ip = route.ip; opt.port = route.port; - + //开始真正的请求 that.doRequest(opt).always(function(d) { - + if(d.opt && d.opt.headers && isTST.isTST(opt)) { //忽略安全中心请求 return; @@ -138,15 +136,15 @@ Ajax.prototype.l5Request = function(opt) { pre : route.pre, flow : route.flow }); - + }).done(function(d) { defer.resolve(d); }).fail(function(d) { defer.reject(d); }); - + }).fail(function(d) { - + if(opt.dcapi) { dcapi.report(Deferred.extend({}, opt.dcapi, { toIp : '127.0.0.1', @@ -155,7 +153,7 @@ Ajax.prototype.l5Request = function(opt) { delay : 100 })); } - + defer.reject({ opt: opt, buffer: null, @@ -166,10 +164,10 @@ Ajax.prototype.l5Request = function(opt) { code: d.ret, response: null }); - + opt.error && defer.fail(opt.error); }); - + return defer; }; @@ -192,17 +190,17 @@ Ajax.prototype.doRequest = function(opt) { request, key, v, obj; - + if(context.AJAXSN) { context.AJAXSN++; }else{ context.AJAXSN = 1; } - + AJAXSN = context.AJAXSN || 0; - + logPre = '[' + AJAXSN + '] '; - + opt = Deferred.extend({ type : 'GET', url : '', @@ -247,7 +245,7 @@ Ajax.prototype.doRequest = function(opt) { } currRetry = opt.retry; - + if(opt.dataType !== 'proxy') { //默认开启gzip opt.headers = Deferred.extend( @@ -273,12 +271,12 @@ Ajax.prototype.doRequest = function(opt) { opt.headers ); } - + if(opt.headers['origin'] === 'null') { //兼容客户端 opt.headers['origin'] = undefined; } - + opt.type = opt.type.toUpperCase(); if(opt.url) { @@ -298,7 +296,7 @@ Ajax.prototype.doRequest = function(opt) { if(!opt.data) { opt.data = {}; } - + if(opt.dataType === 'proxy') { opt.autoToken = false; opt.autoQzoneToken = false; @@ -311,7 +309,7 @@ Ajax.prototype.doRequest = function(opt) { opt.path = opt.path + '&g_tk=' + token.token(); } } - + if(opt.autoQzoneToken) { opt.headers['x-qzone-token'] = opt.headers['x-qzone-token'] || String(opt.autoQzoneToken); } @@ -360,10 +358,10 @@ Ajax.prototype.doRequest = function(opt) { } } } - + if(config.isTest) { - + if(opt.testIp) { logger.debug('use testIp'); @@ -395,7 +393,7 @@ Ajax.prototype.doRequest = function(opt) { } if(currRetry === opt.retry) { - //防止重复注册 + //防止重复注册 opt.success && defer.done(opt.success); opt.error && defer.fail(opt.error); } @@ -404,8 +402,8 @@ Ajax.prototype.doRequest = function(opt) { times.start = new Date().getTime(); function report(opt, isFail, code) { - - + + if(isTST.isTST(opt)) { //忽略安全中心请求 return; @@ -427,7 +425,7 @@ Ajax.prototype.doRequest = function(opt) { })); } } - + //解决undefined报错问题 for(key in opt.headers) { v = opt.headers[key]; @@ -466,7 +464,7 @@ Ajax.prototype.doRequest = function(opt) { }); } } - + if((httpUtil.isPostLike(opt.type)) && opt.dataType === 'proxy' && this._proxyRequest) { if(this._proxyRequest.REQUEST.body !== undefined) { @@ -476,7 +474,7 @@ Ajax.prototype.doRequest = function(opt) { that._proxyRequest.on('data', function(buffer) { request.write(buffer); }); - + that._proxyRequest.once('end', function(buffer) { that._proxyRequest.removeAllListeners('data'); request.end(); @@ -484,11 +482,11 @@ Ajax.prototype.doRequest = function(opt) { }; } } - + 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; @@ -496,7 +494,7 @@ Ajax.prototype.doRequest = function(opt) { if(!Buffer.isBuffer(opt.body)) { opt.body = Buffer.from(opt.body, 'UTF-8'); } - + opt.headers['Content-Length'] = opt.body.length; } @@ -555,17 +553,17 @@ Ajax.prototype.doRequest = function(opt) { request.setNoDelay(true); request.setSocketKeepAlive(true); - + defer.always(function() { clearTimeout(tid); request.removeAllListeners(); - + //request.abort(); 长连接不能开 //request.destroy(); 长连接不能开 request = null; tid = null; }); - + tid = setTimeout(function() { logger.debug(logPre + 'timeout: ${timeout}', { timeout : opt.timeout @@ -573,17 +571,17 @@ Ajax.prototype.doRequest = function(opt) { request.abort(); request.emit('fail'); }, opt.timeout); - + request.once('error', function(err) { request.emit('fail', err); }); - + request.once('fail', function(err) { - + if(defer.isRejected() || defer.isResolved()) { return; } - + times.end = new Date().getTime(); if(err) { @@ -609,7 +607,7 @@ 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', { protocol : opt.protocol, @@ -632,9 +630,9 @@ Ajax.prototype.doRequest = function(opt) { }); return; } - + report(opt, 1, 513 + opt.retry); - + logger.error(logPre + '[${userIp}] ${type} error ~ ${ip}:${port} ${protocol}//${host}${path}', { protocol : opt.protocol, type : opt.type, @@ -645,27 +643,27 @@ Ajax.prototype.doRequest = function(opt) { path : opt.path, userIp : httpUtil.getUserIp() }); - + //限制次数,1s只放过一个 if(opt.retry > 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 +676,18 @@ Ajax.prototype.doRequest = function(opt) { msg: 'request error', times: times }); - + }); request.once('response', function(response) { let result = []; let pipe = response; let isProxy = false; - + if(opt.dataType === 'proxy' && that._proxyResponse) { isProxy = true; } - + if(currRetry != opt.retry) { //防止第一个请求又回包捣乱 logger.debug('currRetry: ${currRetry}, opt.retry: ${retry}', { @@ -698,21 +696,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; - + 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}', { @@ -760,7 +758,7 @@ Ajax.prototype.doRequest = function(opt) { }); } } - + if(defer.isRejected() || defer.isResolved()) { logger.debug(logPre + 'isRejected: ${isRejected}, isResolved: ${isResolved}', { isRejected: defer.isRejected(), @@ -768,7 +766,7 @@ Ajax.prototype.doRequest = function(opt) { }); return; } - + if(typeof opt.response === 'function') { if(opt.response(response) === false) { //中断处理流程 @@ -782,12 +780,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 +794,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 +805,7 @@ Ajax.prototype.doRequest = function(opt) { }); } } - + if(isProxy) { if(response.headers['transfer-encoding'] !== 'chunked') { @@ -843,7 +841,7 @@ Ajax.prototype.doRequest = function(opt) { }); }else{ delete response.headers['connection']; - + that._proxyResponse.writeHead(response.statusCode, httpUtil.formatHeader(response.headers)); } } @@ -873,7 +871,7 @@ Ajax.prototype.doRequest = function(opt) { request.emit('fail'); return; } - + if(isProxy) { if(!that._proxyResponse.finished) { that._proxyResponse.write(chunk); @@ -883,12 +881,12 @@ Ajax.prototype.doRequest = function(opt) { result.push(chunk); } }); - + pipe.once('close', function() { logger.debug(logPre + 'close'); this.emit('done'); }); - + pipe.once('end', function() { const cost = Date.now() - pipe.timeStart; @@ -900,17 +898,17 @@ Ajax.prototype.doRequest = function(opt) { this.emit('done'); }); - + pipe.once('done', function() { - + let obj, responseText, buffer, code; let key, Content; - + this.removeAllListeners('close'); this.removeAllListeners('end'); this.removeAllListeners('data'); this.removeAllListeners('done'); - + if(defer.isRejected() || defer.isResolved()) { return; } @@ -918,20 +916,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 +940,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 +960,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 +974,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 +990,7 @@ Ajax.prototype.doRequest = function(opt) { }else{ report(opt, 1, response.statusCode); } - + defer.reject({ opt: opt, buffer: buffer, @@ -1008,7 +1006,7 @@ Ajax.prototype.doRequest = function(opt) { } if(opt.dataType === 'json' || opt.dataType === 'jsonp') { - + try{ if(opt.jsonpCallback) { @@ -1032,7 +1030,7 @@ Ajax.prototype.doRequest = function(opt) { }catch(e) { let parseErr = e; - + if(e && code) { try{ code = code.replace(/[\u2028\u2029]/g, ''); @@ -1062,32 +1060,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 }); - + return; } - + } - + if(obj) { code = obj.code || 0; } - + }else{ code = { isFail: 0, @@ -1096,37 +1094,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 +1139,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 +1153,10 @@ Ajax.prototype.doRequest = function(opt) { logger.info(e.stack); } } - + request.end(); } - + return defer; }; diff --git a/bin/tsw/util/Deferred/index.js b/bin/tsw/util/Deferred/index.js index a03fe1c9..f20ebe63 100644 --- a/bin/tsw/util/Deferred/index.js +++ b/bin/tsw/util/Deferred/index.js @@ -282,10 +282,10 @@ jQuery.Callbacks = function( flags ) { args = args || []; memory = !flags.memory || [context, args]; firing = true; - firingIndex = firingStart || 0; - firingStart = 0; + // firingIndex = firingStart || 0; + // firingStart = 0; firingLength = list.length; - for (; list && firingIndex < firingLength; firingIndex++) { + for (firingIndex = firingStart || 0, firingStart = 0; list && firingIndex < firingLength; firingIndex++) { fn = list[firingIndex]; domain = fn.__domain; diff --git a/bin/tsw/util/http/parseGet.js b/bin/tsw/util/http/parseGet.js index 3f77996d..cc2f8251 100644 --- a/bin/tsw/util/http/parseGet.js +++ b/bin/tsw/util/http/parseGet.js @@ -16,7 +16,7 @@ const httpUtil = require('util/http.js'); const tnm2 = require('api/tnm2'); module.exports = function(req) { - + let v, key, index1, index2; if(req.url.indexOf('+') > -1) { @@ -30,7 +30,7 @@ module.exports = function(req) { req.POST = {}; req.body = req.POST; req.params = {}; - if('upgrade' === req.headers.connection && 'websocket' === req.headers.upgrade) { + if(req.headers.connection === 'upgrade' && req.headers.upgrade === 'websocket') { if(req.headers['x-client-proto'] === 'https') { req.REQUEST.protocol = 'wss'; } else { @@ -48,9 +48,9 @@ module.exports = function(req) { } req.REQUEST.port = 80; - + req.REQUEST.query = req.REQUEST.query || ''; - + if(req.REQUEST.query.length > 16384) { logger.debug('query large than 16KB :' + req.REQUEST.query.length); req.REQUEST.query = req.REQUEST.query.slice(0, 16384); @@ -91,7 +91,7 @@ module.exports = function(req) { logger.debug('cookie large than 16KB :' + req.headers.cookie.length); req.headers.cookie = req.headers.cookie.slice(0, 16384); } - + if(req.headers.cookie) { index1 = req.headers.cookie.indexOf('; '); } @@ -103,19 +103,19 @@ module.exports = function(req) { && req.headers.cookie.charAt(0) === ',' && req.headers.cookie.charAt(1) === ' ' ) { - + //logger.info('fix bad cookie(-3002):\n' + httpUtil.getUserIp(req) + '\n' + httpUtil.getRequestHeaderStr(req)); logger.debug('orign cookie: ' + req.headers.cookie); //兼容wap cookie开头错误问题 req.headers.cookie = req.headers.cookie.replace(/, /g, '; '); - + logger.report(); - + tnm2.Attr_API('SUM_TSW_BAD_COOKIE', 1); } - + if( !httpUtil.isFromWns(req) && global.cpuUsed < config.cpuLimit @@ -123,15 +123,15 @@ module.exports = function(req) { ) { index2 = req.headers.cookie.indexOf(';'); - + if(index2 > 0 && index2 !== req.headers.cookie.length - 1) { - + //logger.info('fix bad cookie(-3000):\n' + httpUtil.getUserIp(req) + '\n' + httpUtil.getRequestHeaderStr(req)); logger.debug('orign cookie: ' + req.headers.cookie); - //兼容wap cookie分号没空格的问题 + //兼容wap cookie分号没空格的问题 req.headers.cookie = req.headers.cookie.replace(/;/g, '; '); - + if(req.headers['x-wns-uin']) { //... }else{ @@ -140,36 +140,36 @@ module.exports = function(req) { tnm2.Attr_API('SUM_TSW_BAD_COOKIE', 1); }else if(req.headers.cookie.indexOf(',') > 0) { - + //logger.info('fix bad cookie(-3001):\n' + httpUtil.getUserIp(req) + '\n' + httpUtil.getRequestHeaderStr(req)); logger.debug('orign cookie: ' + req.headers.cookie); //兼容wap cookie里全是逗号的问题 req.headers.cookie = req.headers.cookie.replace(/,/g, '; '); - + logger.report(); - + tnm2.Attr_API('SUM_TSW_BAD_COOKIE', 1); } } - + if( !httpUtil.isFromWns(req) && global.cpuUsed < config.cpuLimit ) { - + //去掉header里的\u0000 for(key in req.headers) { v = req.headers[key]; - + if(key.indexOf(' ') > -1) { logger.debug('delete headers : ${key} : ${v}', { key : key, v : v }); - + delete req.headers[key]; - + continue; } @@ -183,11 +183,11 @@ module.exports = function(req) { }); } } - + } - + req.cookies = cookie.parse(req.headers.cookie || ''); - + req.param = paramFn; }; diff --git a/bin/tsw/util/logger/logger.js b/bin/tsw/util/logger/logger.js index 0ba3efb8..1666d660 100644 --- a/bin/tsw/util/logger/logger.js +++ b/bin/tsw/util/logger/logger.js @@ -485,7 +485,6 @@ function merge(str, obj) { return str && str.replace(/\$\{(.+?)\}/g, function($0, $1) { const rs = obj && obj[$1]; - let undefined; return rs === undefined ? '' : typeof rs === 'object' ? util.inspect(rs) : String(rs); From fe5eec3aa1a3973382e74074b4671e0e3847702f Mon Sep 17 00:00:00 2001 From: timcui Date: Mon, 28 May 2018 15:58:27 +0800 Subject: [PATCH 2/5] chore: use alloy eslint config --- .eslintrc.json | 39 +- bin/lib/api/cmdb/index.js | 4 +- bin/lib/api/code/watcher.js | 6 +- bin/lib/api/habo/habo.js | 4 +- bin/lib/api/keyman/alphaMap.js | 84 +- bin/lib/api/keyman/blackIpMap.js | 84 +- bin/lib/api/keyman/index.js | 4 +- bin/lib/api/keyman/ipCheck.js | 4 +- bin/lib/api/keyman/runtimeAdd.js | 6 +- bin/lib/api/libdcapi/dcapi.js | 10 +- bin/lib/api/notify/index.js | 64 +- bin/lib/api/tcss/tcss.js | 4 +- bin/lib/api/tnm2/index.js | 63 +- bin/lib/api/xssFilter/index.js | 4 +- bin/lib/data/cmem.tsw.js | 8 +- bin/lib/default/config.default.h5test.js | 6 +- bin/lib/default/config.default.js | 74 +- bin/lib/default/logReport.js | 28 +- bin/lib/loader/extentions.js | 4 +- bin/lib/runtime/md5.check.js | 3 +- bin/lib/util/gzipHttp.js | 29 +- bin/lib/util/http.more.js | 4 +- bin/lib/util/isProbe.js | 6 +- bin/lib/util/isTST.js | 8 +- bin/lib/util/mail/mail.js | 66 +- bin/lib/util/mail/src/_config.js | 10 +- bin/lib/util/mail/tmpl.js | 34 +- bin/lib/util/oa-login/index.js | 4 +- bin/lib/webapp/Server.js | 4 +- bin/lib/webapp/utils/format.js | 10 +- bin/proxy/admin.actions.js | 21 +- bin/proxy/admin.js | 17 +- bin/proxy/check.js | 10 +- bin/proxy/config.js | 42 +- bin/proxy/http.mod.act.js | 14 +- bin/proxy/http.mod.map.js | 14 +- bin/proxy/http.proxy.js | 168 ++-- bin/proxy/http.route.js | 425 +++++------ bin/proxy/index.js | 4 +- bin/proxy/master.js | 220 +++--- bin/proxy/websocket.js | 80 +- bin/proxy/ws.mod.act.js | 4 +- bin/proxy/ws.mod.map.js | 4 +- bin/proxy/ws.route.js | 15 +- bin/tsw/ajax/ajax.js | 645 ++++++++-------- bin/tsw/ajax/form.js | 24 +- bin/tsw/ajax/http-https.options.js | 28 +- bin/tsw/ajax/index.js | 4 +- bin/tsw/ajax/token.js | 14 +- bin/tsw/api/date.js | 39 +- bin/tsw/api/fileCache/index.js | 127 ++-- bin/tsw/api/logman/index.js | 42 +- bin/tsw/config.js | 8 +- bin/tsw/context.js | 18 +- bin/tsw/default/config.default.sky.js | 44 +- bin/tsw/loader/seajs/lib/helper.js | 10 +- bin/tsw/loader/seajs/lib/sea-node.js | 19 +- bin/tsw/logger.js | 4 +- bin/tsw/plug.js | 28 +- bin/tsw/pool/cmem.l5.js | 87 ++- bin/tsw/router.js | 39 +- bin/tsw/runtime/CCFinder.js | 125 +-- bin/tsw/runtime/Console.hack.js | 30 +- bin/tsw/runtime/Context.js | 10 +- bin/tsw/runtime/ContextWrap.js | 4 +- bin/tsw/runtime/Dns.hack.js | 34 +- bin/tsw/runtime/JankWatcher.js | 8 +- bin/tsw/runtime/Window.js | 4 +- bin/tsw/runtime/capturer.js | 130 ++-- bin/tsw/runtime/fs.hack.js | 28 +- bin/tsw/runtime/overloadProtection.js | 4 +- bin/tsw/serverInfo.js | 30 +- bin/tsw/util/CD.js | 120 +-- bin/tsw/util/Deferred/index.js | 717 +++++++++--------- bin/tsw/util/Queue/index.js | 53 +- bin/tsw/util/alpha.js | 4 +- bin/tsw/util/auto-report/TEReport.js | 84 +- bin/tsw/util/auto-report/alpha.js | 32 +- bin/tsw/util/auto-report/download.js | 258 ++++--- bin/tsw/util/auto-report/highlight-tsw.js | 4 +- bin/tsw/util/auto-report/logReport.js | 510 +++++++------ bin/tsw/util/auto-report/post.js | 131 ++-- bin/tsw/util/auto-report/post.openapi.js | 4 +- bin/tsw/util/auto-report/src/_config.js | 10 +- .../util/auto-report/src/download.tmpl.html | 40 +- bin/tsw/util/auto-report/src/mail.tmpl.html | 7 +- bin/tsw/util/auto-report/src/view.tmpl.html | 522 +++++++------ bin/tsw/util/auto-report/tmpl.js | 252 +++--- bin/tsw/util/auto-report/view.js | 52 +- bin/tsw/util/cache.cleaner.js | 20 +- bin/tsw/util/cpu.js | 62 +- bin/tsw/util/h5-test/add.js | 38 +- bin/tsw/util/h5-test/del.js | 38 +- bin/tsw/util/h5-test/get.js | 44 +- bin/tsw/util/h5-test/group/src/_config.js | 6 +- bin/tsw/util/h5-test/group/tmpl.js | 34 +- bin/tsw/util/h5-test/group/view.js | 14 +- bin/tsw/util/h5-test/is-test.js | 192 ++--- bin/tsw/util/h5-test/page/src/_config.js | 6 +- bin/tsw/util/h5-test/page/tmpl.js | 96 +-- bin/tsw/util/h5-test/page/view.js | 16 +- bin/tsw/util/home/navMenus.js | 4 +- bin/tsw/util/home/src/_config.js | 6 +- bin/tsw/util/home/tmpl.js | 44 +- bin/tsw/util/home/view.js | 6 +- bin/tsw/util/html302/index.js | 36 +- bin/tsw/util/html302/src/_config.js | 6 +- bin/tsw/util/html302/tmpl.js | 13 +- bin/tsw/util/http.isInnerIP.js | 78 +- bin/tsw/util/http.js | 231 +++--- bin/tsw/util/http/parseBody.js | 48 +- bin/tsw/util/http/parseGet.js | 85 ++- bin/tsw/util/isWindows.js | 6 +- bin/tsw/util/logger/callInfo.js | 27 +- bin/tsw/util/logger/index.js | 4 +- bin/tsw/util/logger/logger.config.js | 28 +- bin/tsw/util/logger/logger.js | 224 +++--- bin/tsw/util/nodeVersion.js | 12 +- bin/tsw/util/openapi.js | 19 +- bin/tsw/util/process.args.js | 8 +- bin/tsw/util/static/mime.js | 10 +- bin/tsw/util/static/static.js | 45 +- bin/tsw/util/v8-profiler.js | 20 +- bin/tsw/util/xss.js | 89 +-- examples/framework/config.js | 16 +- examples/framework/express.js | 8 +- examples/framework/hapi.js | 8 +- examples/framework/koa.js | 12 +- examples/framework/router.js | 16 +- examples/framework/websocket.js | 8 +- examples/framework/wsRouter.js | 10 +- examples/skyMode/config.js | 10 +- package.json | 2 + test/bin/config.js | 8 +- test/bin/tsw/runtime/context.js | 8 +- test/bin/tsw/util/CD.test.js | 84 +- test/bin/tsw/util/Queue.js | 12 +- test/bin/tsw/util/openapi.js | 6 +- test/bin/tsw/util/xss.test.js | 6 +- 139 files changed, 4061 insertions(+), 3871 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index afe4d957..1e395531 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -4,7 +4,7 @@ "commonjs": true, "es6": true }, - "extends": "eslint:recommended", + "extends": "eslint-config-alloy", "parserOptions": { "sourceType": "module", "ecmaVersion": 2017 @@ -79,6 +79,36 @@ ], "prefer-const": [ "warn" + ], + "no-param-reassign": [ + "off" + ], + "no-template-curly-in-string": [ + "off" + ], + "one-var": [ + "off" + ], + "guard-for-in": [ + "off" + ], + "no-undefined": [ + "off" + ], + "complexity": [ + "off" + ], + "max-nested-callbacks": [ + "off" + ], + "no-path-concat": [ + "off" + ], + "eqeqeq": [ + "off" + ], + "max-depth": [ + "off" ] }, "overrides": [ @@ -90,6 +120,13 @@ "globals": { "define": true } + }, { + "files": ["bin/tsw/util/auto-report/tmpl.js"], + "rules": { + "no-inner-declarations": [ + "off" + ] + } }, { "files": ["bin/lib/util/mail/tmpl.js", "bin/tsw/util/**/tmpl.js"], "rules": { diff --git a/bin/lib/api/cmdb/index.js b/bin/lib/api/cmdb/index.js index 9fbf9979..264792db 100644 --- a/bin/lib/api/cmdb/index.js +++ b/bin/lib/api/cmdb/index.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 Deferred = require('util/Deferred'); diff --git a/bin/lib/api/code/watcher.js b/bin/lib/api/code/watcher.js index 85c43379..5d4f40fa 100644 --- a/bin/lib/api/code/watcher.js +++ b/bin/lib/api/code/watcher.js @@ -1,13 +1,13 @@ -/*! +/* ! * 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'; + this.watch = function() { - + }; diff --git a/bin/lib/api/habo/habo.js b/bin/lib/api/habo/habo.js index 5afc1cca..2dd66519 100644 --- a/bin/lib/api/habo/habo.js +++ b/bin/lib/api/habo/habo.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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'; + module.exports.report = function(obj, rate) { diff --git a/bin/lib/api/keyman/alphaMap.js b/bin/lib/api/keyman/alphaMap.js index d8d46318..4ad4d7ad 100644 --- a/bin/lib/api/keyman/alphaMap.js +++ b/bin/lib/api/keyman/alphaMap.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 Deferred = require('util/Deferred'); const logger = require('logger'); @@ -21,32 +21,32 @@ let cache = { dataFile: {} }; -if(global[__filename]) { +if (global[__filename]) { cache = global[__filename]; -}else{ +} else { global[__filename] = cache; isFirstLoad = true; } -if(isFirstLoad) { - if(config.alphaFile) { +if (isFirstLoad) { + if (config.alphaFile) { (function() { - //导入alphaFile + // 导入alphaFile let text = ''; - try{ + try { text = fs.readFileSync(config.alphaFile, 'UTF-8'); - }catch(e) { + } 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; } @@ -55,7 +55,7 @@ if(isFirstLoad) { updateMap(); })(); - }else{ + } else { logger.debug('config.alphaFile is: ' + config.alphaFile); } } @@ -78,7 +78,7 @@ function updateMap(text) { const map = getMap(text); - //copy + // copy Object.assign(map, cache.dataFile); cache.data = map; @@ -89,48 +89,48 @@ function updateMap(text) { this.getSync = function() { this.get(); - + return cache.data; }; this.get = function() { - + const defer = Deferred.create(); const delay = (process.serverInfo && process.serverInfo.cpu * 1000) || 0; const 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) { - + let lastModifyTime = 0; let 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); - + defer.resolve(cache.data); return; } - + require('ajax').request({ url: fileUrl, type: 'get', @@ -146,29 +146,29 @@ this.get = function() { }).fail(function(d) { defer.resolve(cache.data); }).done(function(d) { - + let 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); } - + updateMap(text); - - //保存在本地 + + // 保存在本地 fileCache.set(fileUrl, Buffer.from(text, 'UTF-8')); - + defer.resolve(cache.data); }); }); - - + + return defer; }; @@ -178,11 +178,11 @@ function init() { let buffer = null; let 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 5efaffe0..33f1fedc 100644 --- a/bin/lib/api/keyman/blackIpMap.js +++ b/bin/lib/api/keyman/blackIpMap.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 Deferred = require('util/Deferred'); const logger = require('logger'); @@ -21,31 +21,31 @@ let cache = { dataFile: {} }; -if(global[__filename]) { +if (global[__filename]) { cache = global[__filename]; -}else{ +} else { global[__filename] = cache; isFirstLoad = true; } -if(isFirstLoad) { - if(config.blackIpFile) { +if (isFirstLoad) { + if (config.blackIpFile) { (function() { - //导入blackIpFile + // 导入blackIpFile let text = ''; - try{ + try { text = fs.readFileSync(config.blackIpFile, 'UTF-8'); - }catch(e) { + } 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; } @@ -54,7 +54,7 @@ if(isFirstLoad) { updateMap(); })(); - }else{ + } else { logger.debug('config.blackIpFile is: ' + config.blackIpFile); } } @@ -66,11 +66,11 @@ function init() { let buffer = null; let text = ''; - if(fileUrl) { + if (fileUrl) { buffer = fileCache.getSync(fileUrl).data; } - if(buffer) { + if (buffer) { text = buffer.toString('utf-8'); updateMap(text); } @@ -104,7 +104,7 @@ function updateMap(text) { const map = getMap(text); - //copy + // copy Object.assign(map, cache.dataFile); cache.data = map; @@ -113,43 +113,43 @@ function updateMap(text) { } this.get = function() { - + const defer = Deferred.create(); const delay = ((process.serverInfo && process.serverInfo.cpu) * 1000) || 0; const 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) { - + let lastModifyTime = 0; let 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); - + defer.resolve(cache.data); return; } - + require('ajax').request({ url: fileUrl, type: 'get', @@ -165,29 +165,29 @@ this.get = function() { }).fail(function(d) { defer.resolve(cache.data); }).done(function(d) { - + let 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); } - + 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 59871dd1..a48a13df 100644 --- a/bin/lib/api/keyman/index.js +++ b/bin/lib/api/keyman/index.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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'; + this.getBlockIpMapSync = function() { return require('./blackIpMap.js').getSync(); diff --git a/bin/lib/api/keyman/ipCheck.js b/bin/lib/api/keyman/ipCheck.js index 6fede7a7..6362139c 100644 --- a/bin/lib/api/keyman/ipCheck.js +++ b/bin/lib/api/keyman/ipCheck.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 Deferred = require('util/Deferred'); diff --git a/bin/lib/api/keyman/runtimeAdd.js b/bin/lib/api/keyman/runtimeAdd.js index 51d019ee..4e9ef499 100644 --- a/bin/lib/api/keyman/runtimeAdd.js +++ b/bin/lib/api/keyman/runtimeAdd.js @@ -1,16 +1,16 @@ -/*! +/* ! * 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 Deferred = require('util/Deferred'); this.add = function(opt) { - + const defer = Deferred.create(); return defer.resolve(); diff --git a/bin/lib/api/libdcapi/dcapi.js b/bin/lib/api/libdcapi/dcapi.js index 271332b1..d783994a 100644 --- a/bin/lib/api/libdcapi/dcapi.js +++ b/bin/lib/api/libdcapi/dcapi.js @@ -1,16 +1,16 @@ -/*! +/* ! * 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'; + 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/notify/index.js b/bin/lib/api/notify/index.js index 5eab4c69..df0e129b 100644 --- a/bin/lib/api/notify/index.js +++ b/bin/lib/api/notify/index.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 Deferred = require('util/Deferred'); @@ -24,25 +24,25 @@ this.SendRTX = function(opt) { this.SendMail = function(opt) { - + opt = Deferred.extend({ - 'appKey' : '', - 'sysId' : 0, - 'EmailType' : 1, //邮件类型,可选值0(外部邮件),1(内部邮件),2(约会邮件) - 'From' : '', //邮件发送人 - 'To' : '', //邮件接收人 - 'Title' : '', //邮件标题 - 'Content' : '', //邮件内容 - 'Priority' : 0, //邮件优先级,-1低优先级,0普通,1高优先级 - 'BodyFormat' : 0, //邮件格式,0 文本、1 Html - - 'CC' : '', //邮件抄送人 - 'Bcc' : '', //邮件密送人 - 'Location' : '', //当邮件为约会邮件时,约会地点 - 'Organizer' : '', //当邮件为约会邮件时,约会组织者 - 'StartTime' : '', //当邮件为约会邮件时,约会开始时间 - 'EndTime' : '', //当邮件为约会邮件时,约会结束时间 - 'attachment' : '' //邮件附件的文件名以及文件的内容(在发送请求时,文件内容是二进制数据流的形式发送) + 'appKey': '', + 'sysId': 0, + 'EmailType': 1, // 邮件类型,可选值0(外部邮件),1(内部邮件),2(约会邮件) + 'From': '', // 邮件发送人 + 'To': '', // 邮件接收人 + 'Title': '', // 邮件标题 + 'Content': '', // 邮件内容 + 'Priority': 0, // 邮件优先级,-1低优先级,0普通,1高优先级 + 'BodyFormat': 0, // 邮件格式,0 文本、1 Html + + 'CC': '', // 邮件抄送人 + 'Bcc': '', // 邮件密送人 + 'Location': '', // 当邮件为约会邮件时,约会地点 + 'Organizer': '', // 当邮件为约会邮件时,约会组织者 + 'StartTime': '', // 当邮件为约会邮件时,约会开始时间 + 'EndTime': '', // 当邮件为约会邮件时,约会结束时间 + 'attachment': '' // 邮件附件的文件名以及文件的内容(在发送请求时,文件内容是二进制数据流的形式发送) }, opt); const defer = Deferred.create(); @@ -54,12 +54,12 @@ this.SendMail = function(opt) { this.SendWeiXin = function(opt) { opt = Deferred.extend({ - 'appKey' : '', - 'sysId' : 0, - 'MsgInfo' : '', //内容 - 'Priority' : 0, //优先级,-1低优先级,0普通,1高优先级 - 'Receiver' : '', //接收人,英文名,多人用英文分号分隔 - 'Sender' : '' //发送人 + 'appKey': '', + 'sysId': 0, + 'MsgInfo': '', // 内容 + 'Priority': 0, // 优先级,-1低优先级,0普通,1高优先级 + 'Receiver': '', // 接收人,英文名,多人用英文分号分隔 + 'Sender': '' // 发送人 }, opt); const defer = Deferred.create(); @@ -71,12 +71,12 @@ this.SendWeiXin = function(opt) { this.SendSMS = function(opt) { opt = Deferred.extend({ - 'appKey' : '', - 'sysId' : 0, - 'MsgInfo' : '', //内容 - 'Priority' : 0, //优先级,-1低优先级,0普通,1高优先级 - 'Receiver' : '', //接收人,英文名,多人用英文分号分隔 - 'Sender' : '' //发送人 + 'appKey': '', + 'sysId': 0, + 'MsgInfo': '', // 内容 + 'Priority': 0, // 优先级,-1低优先级,0普通,1高优先级 + 'Receiver': '', // 接收人,英文名,多人用英文分号分隔 + 'Sender': '' // 发送人 }, opt); const defer = Deferred.create(); diff --git a/bin/lib/api/tcss/tcss.js b/bin/lib/api/tcss/tcss.js index 219ee2ca..aa1b6f05 100644 --- a/bin/lib/api/tcss/tcss.js +++ b/bin/lib/api/tcss/tcss.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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'; + this.pv = function(dm, url, rate) { diff --git a/bin/lib/api/tnm2/index.js b/bin/lib/api/tnm2/index.js index bb56150e..9bb379b8 100644 --- a/bin/lib/api/tnm2/index.js +++ b/bin/lib/api/tnm2/index.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 serverInfo = require('serverInfo.js'); const mapping = require('./mapping.json'); @@ -19,14 +19,14 @@ let cache = { }; let isFirstLoad = false; -if(global[__filename]) { +if (global[__filename]) { cache = global[__filename]; -}else{ +} else { global[__filename] = cache; isFirstLoad = true; } -if(isFirstLoad) { +if (isFirstLoad) { cluster.worker && cluster.worker.once('disconnect', function(worker) { const logger = require('logger'); const last = cache.curr; @@ -55,26 +55,25 @@ this.Attr_API = function (attr, iValue) { }; const cacheOrRepoet = function(attr, iValue) { - let curr; - if(!mapping[attr]) { + if (!mapping[attr]) { return; } - if(!cache.curr[attr]) { + if (!cache.curr[attr]) { cache.curr[attr] = { count: 0, sum: 0 }; } - curr = cache.curr[attr]; + const curr = cache.curr[attr]; curr.sum += iValue; curr.count += 1; const now = Date.now(); - if(now - cache.time < 60000) { + if (now - cache.time < 60000) { return; } @@ -95,28 +94,28 @@ const reportOpenapi = function(last) { const config = require('config'); let retCall; - if(typeof config.beforeReportApp === 'function') { + if (typeof config.beforeReportApp === 'function') { retCall = config.beforeReportApp(last); } - //阻止默认上报 - if(retCall === false) { + // 阻止默认上报 + if (retCall === false) { return defer.resolve(); } - if(config.isTest) { + if (config.isTest) { return defer.resolve(); } - if(config.devMode) { + if (config.devMode) { return defer.resolve(); } - if(!config.appid || !config.appkey) { + if (!config.appid || !config.appkey) { return defer.resolve(); } - if(!config.appReportUrl) { + if (!config.appReportUrl) { return defer.resolve(); } @@ -127,10 +126,10 @@ const reportOpenapi = function(last) { }); const postData = { - appid : config.appid, - ip : serverInfo.intranetIp, - arr : arr.join('-'), - now : Date.now() + appid: config.appid, + ip: serverInfo.intranetIp, + arr: arr.join('-'), + now: Date.now() }; const sig = openapi.signature({ @@ -143,25 +142,25 @@ const reportOpenapi = function(last) { 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(); }).done(function(d) { - if(d.result) { - if(d.result.code === 0) { + if (d.result) { + if (d.result.code === 0) { logger.debug('app report success.'); return defer.resolve(); - }else{ + } else { logger.debug('app report fail.'); return defer.reject(d.result.code); } diff --git a/bin/lib/api/xssFilter/index.js b/bin/lib/api/xssFilter/index.js index 8e3109d8..a6bc257d 100644 --- a/bin/lib/api/xssFilter/index.js +++ b/bin/lib/api/xssFilter/index.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 Deferred = require('util/Deferred'); diff --git a/bin/lib/data/cmem.tsw.js b/bin/lib/data/cmem.tsw.js index a075746e..c4c1dcf4 100644 --- a/bin/lib/data/cmem.tsw.js +++ b/bin/lib/data/cmem.tsw.js @@ -1,22 +1,22 @@ -/*! +/* ! * 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 config = require('config'); const cmem = require('pool/cmem.l5.js'); 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(); } diff --git a/bin/lib/default/config.default.h5test.js b/bin/lib/default/config.default.h5test.js index fc5af055..fed6ce60 100644 --- a/bin/lib/default/config.default.h5test.js +++ b/bin/lib/default/config.default.h5test.js @@ -1,17 +1,17 @@ -/*! +/* ! * 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'; + this.isTest = true; 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 911675ce..ce306c26 100644 --- a/bin/lib/default/config.default.js +++ b/bin/lib/default/config.default.js @@ -1,82 +1,82 @@ -/*! +/* ! * 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'; + this.defaultConfigLoadFrom = __filename; -//开发者模式 +// 开发者模式 this.devMode = false; -//是否测试环境 +// 是否测试环境 this.isTest = false; -//是否允许url中出现数组 +// 是否允许url中出现数组 this.allowArrayInUrl = false; -//http监听地址 +// http监听地址 this.httpAddress = '0.0.0.0'; -//http监听端口 +// http监听端口 this.httpPort = 80; -//worker起始端口 +// worker起始端口 this.workerPortBase = 12801; -//webso监听端口 +// webso监听端口 this.websoPort = 20000; -//wnshtml监听端口 +// wnshtml监听端口 this.wnshtmlPort = 10080; -//pdu服务监听端口 +// pdu服务监听端口 this.pduServerPort = 19000; -//http管理端口 +// http管理端口 this.httpAdminPort = 12701; -//webso目标端口 +// webso目标端口 this.websoDstPort = this.httpPort; -//webso目标IP +// webso目标IP this.websoDstIp = '127.0.0.1'; -//alpha号码文件 +// alpha号码文件 this.alphaFile = null; -//alpha号码文件 +// alpha号码文件 this.alphaFileUrl = null; -//ip黑名单文件 +// ip黑名单文件 this.blackIpFile = null; -//ip黑名单文件 +// ip黑名单文件 this.blackIpFileUrl = null; -//邮件接收者 +// 邮件接收者 this.mailTo = ''; -//邮件抄送者 +// 邮件抄送者 this.mailCC = ''; -//cpu分配 +// cpu分配 this.runAtThisCpu = 'auto'; -//worker用户 +// worker用户 this.workerUid = 'nobody'; -//mod_act映射 +// mod_act映射 this.modAct = { getModAct: function(req) { return null; } }; -//路由 +// 路由 this.modMap = { find: function(mod_act, req, res) { return null; @@ -89,34 +89,34 @@ this.page419 = null; this.page404 = null; -//logger +// logger this.logger = { logLevel: 'debug' }; -//cpu限制 +// cpu限制 this.cpuLimit = 85; -//内存限制 +// 内存限制 this.memoryLimit = 768 * 1024 * 1024; -//限制 +// 限制 this.CCIPLimit = 500; -//allowHost +// allowHost this.allowHost = []; this.idc = 'sz'; -//l5 api +// l5 api this.l5api = {}; this.timeout = { - socket : 120000, - post : 30000, - get : 10000, - keepAlive : 10000, - dns : 3000 + socket: 120000, + post: 30000, + get: 10000, + keepAlive: 10000, + dns: 3000 }; @@ -124,7 +124,7 @@ this.extendMod = { getUin: req => {} }; -//openapi +// 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'; @@ -134,7 +134,7 @@ this.runtimeReportUrl = 'https://openapi.tswjs.org/v1/runtime/report'; this.ignoreTST = false; this.ajaxDefaultOptions = { - useJsonParseOnly : true + useJsonParseOnly: true }; this.tswL5api = {}; diff --git a/bin/lib/default/logReport.js b/bin/lib/default/logReport.js index 9781a2b3..c727c2e9 100644 --- a/bin/lib/default/logReport.js +++ b/bin/lib/default/logReport.js @@ -1,36 +1,36 @@ -/*! +/* ! * 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 config = require('config'); module.exports.report = function(data) { let retValue; const 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, + 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, httpCode: data.statusCode } }; - 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/loader/extentions.js b/bin/lib/loader/extentions.js index ee6ac07b..0ff2513d 100644 --- a/bin/lib/loader/extentions.js +++ b/bin/lib/loader/extentions.js @@ -1,8 +1,8 @@ -/*! +/* ! * 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'; + diff --git a/bin/lib/runtime/md5.check.js b/bin/lib/runtime/md5.check.js index eb7d0436..e8ae1f35 100644 --- a/bin/lib/runtime/md5.check.js +++ b/bin/lib/runtime/md5.check.js @@ -1,11 +1,10 @@ -/*! +/* ! * 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'; this.check = function(root) {}; diff --git a/bin/lib/util/gzipHttp.js b/bin/lib/util/gzipHttp.js index 896e043c..b1828abf 100644 --- a/bin/lib/util/gzipHttp.js +++ b/bin/lib/util/gzipHttp.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 zlib = require('zlib'); const logger = require('logger'); @@ -19,19 +19,20 @@ this.create = this.getGzipResponse = function(opt) { opt = opt || {}; const window = context.window || {}; const request = opt.request || window.request; - const response = opt.response ||window.response; + const response = opt.response || window.response; - if(!request || !response || response.headersSent) { - return {write:()=>{}, end:()=>{}, flush:()=>{}}; + if (!request || !response || response.headersSent) { + return { write: () => {}, end: () => {}, flush: () => {} }; } - let code = opt.code || 200, + const 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; + contentType = opt.contentType || (headers && headers['content-type']) || 'text/html; charset=UTF-8'; + + let gzipOutputStream; - if(headers && headers['content-length'] !== undefined) { + if (headers && (typeof headers['content-length'] === 'undefined')) { response.useChunkedEncodingByDefault = false; delete headers['transfer-encoding']; delete headers['content-length']; @@ -40,7 +41,7 @@ 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); @@ -55,7 +56,7 @@ this.create = this.getGzipResponse = function(opt) { len: buffer.length }); - if(!response.finished) { + if (!response.finished) { response.write(buffer); } }); @@ -66,7 +67,7 @@ this.create = this.getGzipResponse = function(opt) { return gzipOutputStream; - }else{ + } else { response.writeHead(code, headers); @@ -76,13 +77,13 @@ this.create = this.getGzipResponse = function(opt) { }); }); - if(!response.flush) { + if (!response.flush) { response.flush = function() { return true; }; } - if(response.flush && response.flushHeaders) { + if (response.flush && response.flushHeaders) { response.flush = function() { return true; }; diff --git a/bin/lib/util/http.more.js b/bin/lib/util/http.more.js index 2745e8e1..e393cb91 100644 --- a/bin/lib/util/http.more.js +++ b/bin/lib/util/http.more.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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'; + this.isFromWns = function(request) { return false; diff --git a/bin/lib/util/isProbe.js b/bin/lib/util/isProbe.js index c1749eda..4ff8973d 100644 --- a/bin/lib/util/isProbe.js +++ b/bin/lib/util/isProbe.js @@ -1,13 +1,13 @@ -/*! +/* ! * 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'; -//是否接入层探测请求 + +// 是否接入层探测请求 this.isProbe = (req) => { return false; }; diff --git a/bin/lib/util/isTST.js b/bin/lib/util/isTST.js index 13273a96..71af4401 100644 --- a/bin/lib/util/isTST.js +++ b/bin/lib/util/isTST.js @@ -1,18 +1,18 @@ -/*! +/* ! * 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 config = require('config'); -//是否安全扫描器请求 +// 是否安全扫描器请求 this.isTST = (req) => { - if(config.extendMod && typeof config.extendMod.isTST === 'function') { + if (config.extendMod && typeof config.extendMod.isTST === 'function') { return config.extendMod.isTST(req); } diff --git a/bin/lib/util/mail/mail.js b/bin/lib/util/mail/mail.js index d88a7157..389a270c 100644 --- a/bin/lib/util/mail/mail.js +++ b/bin/lib/util/mail/mail.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 serverInfo = require('serverInfo.js'); const config = require('config.js'); @@ -22,24 +22,24 @@ this.SendMail = function(key, group, second, oriOpt) { const now = new Date(); let prefix = '[runtime]'; - if(isWindows.isWindows) { + if (isWindows.isWindows) { return; } - //晚上不发 - //allDaySend强制全天候24小时发送 - if(!opt.allDaySend && now.getHours() < 8) { + // 晚上不发 + // allDaySend强制全天候24小时发送 + 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) { + } else { + if (opt.runtimeType) { prefix += `[${opt.runtimeType}][考核]`; } } @@ -61,7 +61,7 @@ this.SendMail = function(key, group, second, oriOpt) { opt.data = data; - if(isWindows.isWindows) { + if (isWindows.isWindows) { key = key + Date.now(); } @@ -81,20 +81,20 @@ const reportOpenapi = function(data) { let 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; } @@ -104,34 +104,34 @@ const reportOpenapi = function(data) { postData.now = Date.now(); const sig = openapi.signature({ - pathname : url.parse(config.runtimeReportUrl).pathname, - method : 'POST', - data : postData, - appkey : config.appkey + pathname: url.parse(config.runtimeReportUrl).pathname, + method: 'POST', + data: postData, + appkey: config.appkey }); 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(); }).done(function(d) { - if(d.result) { - if(d.result.code === 0) { + if (d.result) { + if (d.result.code === 0) { logger.debug('runtime report success.'); return defer.resolve(); - }else{ + } else { logger.debug('runtime report fail.'); return defer.reject(d.result.code); } @@ -145,12 +145,12 @@ const reportOpenapi = function(data) { }; -//升级周知 +// 升级周知 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 9cb4d1d3..ce23bf17 100644 --- a/bin/lib/util/mail/src/_config.js +++ b/bin/lib/util/mail/src/_config.js @@ -1,23 +1,23 @@ -/*! +/* ! * 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'; + 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 97fd750b..8d8a4c03 100644 --- a/bin/lib/util/mail/tmpl.js +++ b/bin/lib/util/mail/tmpl.js @@ -1,19 +1,21 @@ -//tmpl file list: -//mail/src/mail.tmpl.html +// tmpl file list: +// mail/src/mail.tmpl.html define(function(require, exports, module) { - var tmpl = { + var tmpl = { 'encodeHtml': function(s) { - return (s+'').replace(/[\x26\x3c\x3e\x27\x22\x60]/g, function($0) { - return '&#'+$0.charCodeAt(0)+';'; - }); + return (String(s)).replace(/[\x26\x3c\x3e\x27\x22\x60]/g, function($0) { + return '&#' + $0.charCodeAt(0) + ';'; + }); }, 'email': function(data) { - let __p=[], _p=function(s) { - __p.push(s); - }, out=_p; + let __p = [], + _p = function(s) { + __p.push(s); + }, + out = _p; const window = context.window || {}; @@ -28,7 +30,7 @@ define(function(require, exports, module) { __p.push('

\n

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

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

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

\n

path: '); @@ -39,12 +41,12 @@ define(function(require, exports, module) { __p.push('

\n

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

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

请求头:

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

全息日志:

\n
');
                 _p(tmpl.encodeHtml(data.logText || '').replace(/\r\n|\r|\n/g, '
')); __p.push('
'); @@ -55,9 +57,11 @@ define(function(require, exports, module) { 'rtx': function(data) { - let __p=[], _p=function(s) { - __p.push(s); - }, out=_p; + let __p = [], + _p = function(s) { + __p.push(s); + }, + out = _p; const window = context.window || {}; diff --git a/bin/lib/util/oa-login/index.js b/bin/lib/util/oa-login/index.js index 089cee24..66ee6d3e 100644 --- a/bin/lib/util/oa-login/index.js +++ b/bin/lib/util/oa-login/index.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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'; + module.exports = function(request, response, callback, tofCfg) { callback(); diff --git a/bin/lib/webapp/Server.js b/bin/lib/webapp/Server.js index c6064970..7bf3f725 100644 --- a/bin/lib/webapp/Server.js +++ b/bin/lib/webapp/Server.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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'; + this.startServer = function() { diff --git a/bin/lib/webapp/utils/format.js b/bin/lib/webapp/utils/format.js index 6ab7266a..4a7e18b7 100644 --- a/bin/lib/webapp/utils/format.js +++ b/bin/lib/webapp/utils/format.js @@ -1,25 +1,25 @@ -/*! +/* ! * 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'; + function formatBuffer(buffer) { - if(!(buffer && buffer.length)) { + if (!(buffer && buffer.length)) { return ''; } let str = ''; for (let i = 0, len = buffer.length; i < len; i++) { - if(i % 16 === 0) { + if (i % 16 === 0) { str += '0x' + ('00000000000' + i.toString(16)).slice(-8) + ': '; } - str += (buffer[i] > 15 ? '' : '0') + buffer[i].toString(16).toUpperCase() + (((i+1) % 16 == 0) ? '\n' : (i % 2 === 0 ? '' : ' ')); + str += (buffer[i] > 15 ? '' : '0') + buffer[i].toString(16).toUpperCase() + (((i + 1) % 16 === 0) ? '\n' : (i % 2 === 0 ? '' : ' ')); } return str; diff --git a/bin/proxy/admin.actions.js b/bin/proxy/admin.actions.js index cd39badc..1ae60a77 100644 --- a/bin/proxy/admin.actions.js +++ b/bin/proxy/admin.actions.js @@ -1,4 +1,4 @@ -/*! +/* ! * 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 @@ -6,14 +6,13 @@ * 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 cp = require('child_process'); module.exports = { 'default': function (req, res) { - res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'}); + res.writeHead(200, { 'Content-Type': 'text/plain; charset=UTF-8' }); res.end('no such command!'); }, @@ -22,7 +21,7 @@ module.exports = { CMD: 'globaldump', GET: req.GET }); - res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'}); + res.writeHead(200, { 'Content-Type': 'text/plain; charset=UTF-8' }); res.end('done!\r\n'); }, @@ -31,7 +30,7 @@ module.exports = { CMD: 'heapdump', GET: req.GET }); - res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'}); + res.writeHead(200, { 'Content-Type': 'text/plain; charset=UTF-8' }); res.end('done!\r\n'); }, @@ -40,7 +39,7 @@ module.exports = { CMD: 'profiler', GET: req.GET }); - res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'}); + res.writeHead(200, { 'Content-Type': 'text/plain; charset=UTF-8' }); res.end('done!\r\n'); }, @@ -49,7 +48,7 @@ module.exports = { CMD: 'top100', GET: req.GET }); - res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'}); + res.writeHead(200, { 'Content-Type': 'text/plain; charset=UTF-8' }); res.end('done!\r\n'); }, @@ -71,8 +70,8 @@ module.exports = { } if (hasError) { - //异常情况下,不能直接res.end(),需要模拟http异常,让shell感知到异常 - res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'}); + // 异常情况下,不能直接res.end(),需要模拟http异常,让shell感知到异常 + res.writeHead(200, { 'Content-Type': 'text/plain; charset=UTF-8' }); res.write(stderr.toString('UTF-8')); res.socket && res.socket.end(); @@ -82,10 +81,10 @@ module.exports = { if (stdout && stdout.length > 0) { logger.info(stdout.toString('UTF-8')); - //开始reload + // 开始reload process.emit('reload', req.GET); - res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'}); + res.writeHead(200, { 'Content-Type': 'text/plain; charset=UTF-8' }); res.write(stderr.toString('UTF-8')); res.end('\r\ndone!\r\n'); diff --git a/bin/proxy/admin.js b/bin/proxy/admin.js index f1ad8781..afbd3f51 100644 --- a/bin/proxy/admin.js +++ b/bin/proxy/admin.js @@ -1,4 +1,4 @@ -/*! +/* ! * 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 @@ -6,7 +6,6 @@ * 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 config = require('./config.js'); @@ -16,26 +15,24 @@ const parseGet = require('util/http/parseGet.js'); const actions = require('./admin.actions.js'); const server = http.createServer(function(req, res) { - let action; - logger.info('admin request by: ${url}', { url: req.url }); - parseGet(req); //解析get参数 - action = actions[req.REQUEST.pathname] || actions['default']; + parseGet(req); // 解析get参数 + const action = actions[req.REQUEST.pathname] || actions['default']; action.call(actions, req, res); }); logger.info('start admin...'); server.listen(config.httpAdminPort, '127.0.0.1', function(err) { - if(err) { + if (err) { logger.info('admin listen error ${address}:${port}', { address: '127.0.0.1', port: config.httpAdminPort }); - }else{ + } else { logger.info('admin listen ok ${address}:${port}', { address: '127.0.0.1', port: config.httpAdminPort @@ -43,8 +40,8 @@ server.listen(config.httpAdminPort, '127.0.0.1', function(err) { } }); -//管理进程开启debug日志 +// 管理进程开启debug日志 logger.setLogLevel('debug'); -//变更感知 +// 变更感知 codeWatch.watch(); diff --git a/bin/proxy/check.js b/bin/proxy/check.js index eb32b820..b98dacd4 100644 --- a/bin/proxy/check.js +++ b/bin/proxy/check.js @@ -1,15 +1,15 @@ #!/usr/bin/env node -/*! +/* ! * 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'; -try{ + +try { const plug = require('plug'); const config = plug('config'); @@ -22,9 +22,9 @@ try{ setTimeout(function() { process.exit(0); }, 500); -}catch(err) { +} catch (err) { - if(err) { + if (err) { process.stderr.write(err.stack); process.stderr.write('\r\n'); setTimeout(function() { diff --git a/bin/proxy/config.js b/bin/proxy/config.js index aa1b39ba..a2afd588 100644 --- a/bin/proxy/config.js +++ b/bin/proxy/config.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 fs = require('fs'); const path = require('path'); @@ -19,38 +19,38 @@ let cache = { }; -if(global[__filename]) { +if (global[__filename]) { cache = global[__filename]; -}else{ +} else { global[__filename] = cache; isFirstLoad = true; } -if(isFirstLoad) { - process.dlopen = function(fn) { +if (isFirstLoad) { + process.dlopen = (function(fn) { const parent = path.join(__dirname, '..'); return function(module, curr) { - //检查node私有文件 - if(/\.node$/i.test(curr) && curr.indexOf(parent) !== 0) { - //发现私有node扩展 + // 检查node私有文件 + if (/\.node$/i.test(curr) && curr.indexOf(parent) !== 0) { + // 发现私有node扩展 setTimeout(function() { require('runtime/md5.check.js').findNodeCpp(curr); }, 3000); } return fn.apply(this, arguments); }; - }(process.dlopen); + })(process.dlopen); } -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'); } @@ -58,27 +58,27 @@ if(fs.existsSync('/etc/tsw.config.js')) { 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) { + 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) { + 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 f7d0a6f1..947afacf 100644 --- a/bin/proxy/http.mod.act.js +++ b/bin/proxy/http.mod.act.js @@ -1,32 +1,32 @@ -/*! +/* ! * 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 config = require('./config.js'); let 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) { +if (base) { module.exports.getModAct = function(req) { const mod = base.modAct.getModAct(req); - if(mod) { + if (mod) { return mod; } return config.modAct.getModAct(req); }; -}else{ +} else { module.exports = config.modAct; } diff --git a/bin/proxy/http.mod.map.js b/bin/proxy/http.mod.map.js index 767c856f..67fa61a9 100644 --- a/bin/proxy/http.mod.map.js +++ b/bin/proxy/http.mod.map.js @@ -1,32 +1,32 @@ -/*! +/* ! * 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 config = require('./config.js'); let 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) { +if (base) { module.exports.find = function(mod_act, req, res) { const mod = base.modMap.find(mod_act, req, res); - if(mod) { + if (mod) { return mod; } return config.modMap.find(mod_act, req, res); }; -}else{ +} else { module.exports = config.modMap; } diff --git a/bin/proxy/http.proxy.js b/bin/proxy/http.proxy.js index ceb36aa6..5a468a76 100644 --- a/bin/proxy/http.proxy.js +++ b/bin/proxy/http.proxy.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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'; + process.on('uncaughtException', function(e) { logger.error(e && e.stack); @@ -29,14 +29,13 @@ 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 { isWindows } = require('util/isWindows.js'); +const { debugOptions } = process.binding('config'); const serverInfo = { intranetIp: require('serverInfo.js').intranetIp, cpu: 'X' }; -let server; -let serverThis; + let serverHttps; let config = require('./config.js'); let routeCache = null; @@ -47,13 +46,13 @@ let heartBeatCount = 0; function doRoute(req, res) { - if(routeCache === null) { + if (routeCache === null) { routeCache = require('./http.route.js'); config = require('./config.js'); } - if(isProbe.isProbe(req)) { - res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'}); + if (isProbe.isProbe(req)) { + res.writeHead(200, { 'Content-Type': 'text/html; charset=UTF-8' }); res.end(); return; } @@ -63,7 +62,7 @@ function doRoute(req, res) { process.serverInfo = serverInfo; -//清除缓存 +// 清除缓存 function cleanCache() { clearTimeout(cleanCacheTid); @@ -84,9 +83,16 @@ process.on('top100', 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) { + + if (err) { + logger.error(`dump heap error ${err.message}`); + + return; + } + logger.info('dump written to ${filename}', { filename: filename }); @@ -100,7 +106,7 @@ process.on('heapdump', function(e) { 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 @@ -114,13 +120,13 @@ process.on('profiler', function(data = {}) { }); -//process.emit('globaldump',m.GET); +// process.emit('globaldump',m.GET); process.on('globaldump', function(GET) { - const cpu = GET.cpu || 0; + const cpu = parseInt(GET.cpu, 10) || 0; const depth = GET.depth || 6; - if(cpu != serverInfo.cpu) { + if (cpu !== serverInfo.cpu) { return; } @@ -142,10 +148,10 @@ process.on('globaldump', function(GET) { }); function requestHandler(req, res) { - if(server.keepAliveTimeout > 0) { + if (server.keepAliveTimeout > 0) { res.setHeader('Connection', 'keep-alive'); - res.setHeader('Keep-Alive', `timeout=${parseInt(server.keepAliveTimeout / 1000)}`); - }else{ + res.setHeader('Keep-Alive', `timeout=${parseInt(server.keepAliveTimeout / 1000, 10)}`); + } else { res.setHeader('Connection', 'close'); } @@ -153,13 +159,13 @@ function requestHandler(req, res) { res.setHeader('Server', headerServer); res.setHeader('Cache-Control', 'no-cache'); - if(config.devMode) { - //发者模式清除缓存 + if (config.devMode) { + // 发者模式清除缓存 cleanCache(); } - if(req.headers.connection === 'upgrade' && req.headers.upgrade === 'websocket') { - //websocket + if (req.headers.connection === 'upgrade' && req.headers.upgrade === 'websocket') { + // websocket return; } @@ -167,16 +173,16 @@ function requestHandler(req, res) { return true; }; - //解析get参数 + // 解析get参数 parseGet(req); - //HTTP路由 + // HTTP路由 doRoute(req, res); } -server = http.createServer(requestHandler); -serverThis = http.createServer(requestHandler); +const server = http.createServer(requestHandler); +const 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); @@ -185,14 +191,14 @@ serverThis.keepAliveTimeout = Math.max(config.timeout.keepAlive, 0); global.TSW_HTTP_SERVER = server; -if(config.httpsOptions) { +if (config.httpsOptions) { serverHttps = https.createServer(config.httpsOptions, function(req, res) { req.headers['x-client-proto'] = 'https'; requestHandler(req, res); }); - serverHttps.on('clientError', function(err, socket) { + serverHttps.on('clientError', function(err, socket) { // eslint-disable-line handle-callback-err socket.end('HTTP/1.1 400 Bad Request\r\n\r\n'); }); @@ -208,16 +214,16 @@ logger.info('pid:${pid} createServer ok', { }); -//分发父进程发送来的消息 +// 分发父进程发送来的消息 process.on('message', function(m) { - if(m && methodMap[m.cmd]) { + if (m && methodMap[m.cmd]) { methodMap[m.cmd].apply(this, arguments); } }); function startHeartBeat() { - if(isStartHeartBeat) { + if (isStartHeartBeat) { return; } @@ -225,7 +231,7 @@ function startHeartBeat() { global.cpuUsed = 0; - //定时给父进程发送心跳包 + // 定时给父进程发送心跳包 setInterval(function() { heartBeatCount += 1; @@ -235,9 +241,9 @@ function startHeartBeat() { memoryUsage: process.memoryUsage() }); - if(serverInfo.cpu === 0) { - //测试环境1分钟上报一次 - if(heartBeatCount % 12 === 0) { + if (serverInfo.cpu === 0) { + // 测试环境1分钟上报一次 + if (heartBeatCount % 12 === 0) { TEReport.report(); } } @@ -246,28 +252,28 @@ function startHeartBeat() { tnm2.Attr_API_Set('AVG_TSW_CPU_USED', global.cpuUsed); - if(global.cpuUsed >= 80) { + if (global.cpuUsed >= 80) { global.cpuUsed80 = ~~global.cpuUsed80 + 1; - }else{ + } else { global.cpuUsed80 = 0; } const cpuUsed = global.cpuUsed; - //高负载告警 + // 高负载告警 if ( global.cpuUsed80 === 4 && !config.isTest && !isWindows ) { - //取进程快照 - //ps aux --sort=-pcpu + // 取进程快照 + // ps aux --sort=-pcpu cp.exec('top -bcn1', { env: { COLUMNS: 200 }, timeout: 5000 - }, function(err, data, errData) { + }, function(err, data, errData) { // eslint-disable-line handle-callback-err const key = ['cpu80.v4', serverInfo.intranetIp].join(':'); let Content = [ '单核CPU' + serverInfo.cpu + '使用率为:' + cpuUsed + ',超过80%, 最近5秒钟CPU Profiler见附件' @@ -284,35 +290,35 @@ function startHeartBeat() { } - //获取本机信息,用来分组 + // 获取本机信息,用来分组 require('api/cmdb').GetDeviceThisServer().done(function(data) { data = data || {}; const business = data.business && data.business[0] || {}; let owner = ''; - if(data.ownerMain) { + if (data.ownerMain) { owner = [owner, data.ownerMain].join(';'); } - if(data.ownerBack) { + if (data.ownerBack) { owner = [owner, data.ownerBack].join(';'); } - //再抓一份CPU Profiler + // 再抓一份CPU Profiler require('util/v8-profiler.js').getProfiler({ 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, + fileType: true, dispositionType: 'attachment', - fileName : 'cpu-profiler.cpuprofile', - content : result + fileName: 'cpu-profiler.cpuprofile', + content: result } : '' }); }); @@ -331,7 +337,7 @@ function startHeartBeat() { } -//restart +// restart methodMap.restart = function() { logger.info('cpu: ${cpu} restart', serverInfo); @@ -339,7 +345,7 @@ methodMap.restart = function() { process.emit('restart'); }; -//reload +// reload methodMap.reload = function() { logger.info('cpu: ${cpu} reload', serverInfo); @@ -347,7 +353,7 @@ methodMap.reload = function() { process.emit('reload'); }; -//heapdump +// heapdump methodMap.heapdump = function(m) { logger.info('cpu: ${cpu} heapdump', serverInfo); @@ -355,7 +361,7 @@ methodMap.heapdump = function(m) { process.emit('heapdump', m.GET); }; -//profiler +// profiler methodMap.profiler = function(m) { logger.info('cpu: ${cpu} profiler', serverInfo); @@ -363,7 +369,7 @@ methodMap.profiler = function(m) { process.emit('profiler', m.GET); }; -//globaldump +// globaldump methodMap.globaldump = function(m) { logger.info('cpu: ${cpu} globaldump', serverInfo); @@ -371,7 +377,7 @@ methodMap.globaldump = function(m) { process.emit('globaldump', m.GET); }; -//top100 +// top100 methodMap.top100 = function(m) { logger.info('cpu: ${cpu} top100', serverInfo); @@ -379,7 +385,7 @@ methodMap.top100 = function(m) { process.emit('top100', m.GET); }; -//监听端口 +// 监听端口 methodMap.listen = function(message) { const user_00 = config.workerUid || 'user_00'; @@ -391,21 +397,21 @@ methodMap.listen = function(message) { 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); - //直接根据配置启动,无需拿到_handle + // 直接根据配置启动,无需拿到_handle server.listen({ host: config.httpAddress, port: config.httpPort, exclusive: false }, function(err) { - if(err) { + if (err) { logger.info('cpu: ${cpu}, listen http error ${address}:${port}', { - cpu:serverInfo.cpu, + cpu: serverInfo.cpu, address: config.httpAddress, port: config.httpPort }); @@ -414,40 +420,40 @@ methodMap.listen = function(message) { } logger.info('cpu: ${cpu}, listen http ok ${address}:${port}', { - cpu:serverInfo.cpu, + cpu: serverInfo.cpu, address: config.httpAddress, port: config.httpPort }); const finish = function() { - //开始发送心跳 + // 开始发送心跳 logger.info('start heart beat'); startHeartBeat(); - if(!isWindows) { - try{ + 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}', { - uid:user_00 + uid: user_00 }); } websocket.start_listen(); logger.info('cpu: ${cpu}, afterStartup...', serverInfo); - if(typeof config.afterStartup === 'function') { + if (typeof config.afterStartup === 'function') { config.afterStartup(serverInfo.cpu); } }; - //监听私有端口 + // 监听私有端口 serverThis.listen({ host: config.httpAddress, port: global.TSW_HTTP_WORKER_PORT, @@ -470,13 +476,13 @@ methodMap.listen = function(message) { }); - if(serverHttps) { + if (serverHttps) { - //启动https + // 启动https serverHttps.listen(config.httpsPort, config.httpsAddress, function(err) { - if(err) { + if (err) { logger.info('cpu: ${cpu}, listen https error ${address}:${port}', { - cpu:serverInfo.cpu, + cpu: serverInfo.cpu, address: config.httpsPort, port: config.httpsAddress }); @@ -485,14 +491,14 @@ methodMap.listen = function(message) { } logger.info('cpu: ${cpu}, listen https ok ${address}:${port}', { - cpu:serverInfo.cpu, + cpu: serverInfo.cpu, address: config.httpsAddress, port: config.httpsPort }); finish(); }); - }else{ + } else { finish(); } }); @@ -502,11 +508,11 @@ methodMap.listen = function(message) { }; -if(cluster.isMaster) { - if(debugOptions && debugOptions.inspectorEnabled) { +if (cluster.isMaster) { + if (debugOptions && debugOptions.inspectorEnabled) { logger.setLogLevel('debug'); logger.info('inspectorEnabled, start listening'); - methodMap.listen({cpu : 0}); + methodMap.listen({ cpu: 0 }); } } diff --git a/bin/proxy/http.route.js b/bin/proxy/http.route.js index 3eade3e1..efca450f 100644 --- a/bin/proxy/http.route.js +++ b/bin/proxy/http.route.js @@ -1,18 +1,18 @@ -/*! +/* ! * 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 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 { isWindows } = require('util/isWindows.js'); const contextMod = require('context.js'); const Context = require('runtime/Context'); const Window = require('runtime/Window'); @@ -42,7 +42,7 @@ module.exports = function(req, res) { let clear = function() { - if(tid === null) { + if (tid === null) { return; } @@ -55,15 +55,15 @@ module.exports = function(req, res) { let 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; } @@ -81,14 +81,14 @@ module.exports = function(req, res) { 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; } @@ -117,88 +117,88 @@ 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(); } req.timestamps = { - ClientConnected : start, - ClientBeginRequest : start, - GotRequestHeaders : start, - ClientDoneRequest : start, - GatewayTime : 0, - DNSTime : 0, - TCPConnectTime : 0, - HTTPSHandshakeTime : 0, - ServerConnected : start, + ClientConnected: start, + ClientBeginRequest: start, + GotRequestHeaders: start, + ClientDoneRequest: start, + GatewayTime: 0, + DNSTime: 0, + TCPConnectTime: 0, + HTTPSHandshakeTime: 0, + ServerConnected: start, FiddlerBeginRequest: start, - ServerGotRequest : start, + ServerGotRequest: start, ServerBeginResponse: 0, - GotResponseHeaders : 0, - ServerDoneResponse : 0, + GotResponseHeaders: 0, + ServerDoneResponse: 0, ClientBeginResponse: 0, - ClientDoneResponse : 0 + 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) { - if(err && err.message === 'socket hang up') { + if (err && err.message === 'socket hang up') { logger.warn(err && err.stack); - //忽略ajax错误 + // 忽略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错误 + // 忽略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错误 + // 忽略io错误 return; } - if(err && err.message === 'write ECONNRESET') { + if (err && err.message === 'write ECONNRESET') { logger.warn(err && err.stack); - //忽略io错误 + // 忽略io错误 return; } - if(err && err.message === 'This socket is closed') { + if (err && err.message === 'This socket is closed') { logger.warn(err && err.stack); - //忽略io错误 + // 忽略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); - if(httpUtil.isSent(res)) { + if (httpUtil.isSent(res)) { logger.warn('${err}\nhttp://${host}${url}', { err: err && err.stack, url: req.url, @@ -206,14 +206,14 @@ 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{ + } else { logger.error('${err}\nhttp://${host}${url}', { err: err && err.stack, url: req.url, @@ -221,53 +221,54 @@ 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{ - res.writeHead(503, {'Content-Type': 'text/html; charset=UTF-8'}); + 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{ + try { res.emit('done'); - }catch(e) { + } catch (e) { logger.info(`emit done event fail ${e.message}`); } - let key, Content; + let 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) { - //return; + if (isWindows) { + // return; } key = [err.message].join(':'); @@ -280,10 +281,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 }); } @@ -302,65 +303,65 @@ module.exports = function(req, res) { res.once('done', function() { - let isFail = 0, now; + let isFail = 0; - if(clear === null) { + if (clear === null) { return; } clear(); - now = new Date(); + const 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{ + } else { isFail = 2; tnm2.Attr_API('SUM_TSW_HTTP_OTHER', 1); } @@ -372,26 +373,26 @@ module.exports = function(req, res) { req.timestamps.ClientDoneResponse = req.timestamps.ServerDoneResponse; - if(isFail === 1) { + 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) { + if (req.REQUEST.body.length < 32 * 1024) { logger.debug('\n${head}${body}', { head: httpUtil.getRequestHeaderStr(req), body: req.REQUEST.body || '' }); - }else{ + } else { logger.debug('\n${head}${body}', { head: httpUtil.getRequestHeaderStr(req), body: 'body size >= 32KB' }); } - }else{ + } else { logger.debug('\n${head}${body}', { head: httpUtil.getRequestHeaderStr(req), @@ -399,48 +400,48 @@ module.exports = function(req, res) { }); } - }else{ + } else { 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', - 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) { + } 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{ + } 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() }); } @@ -458,71 +459,71 @@ module.exports = function(req, res) { onerror(req, res, new Error('timeout')); - if(res.__hasClosed) { + if (res.__hasClosed) { - try{ + try { res.writeHead(202); - }catch(e) { + } catch (e) { logger.info(`response 202 fail ${e.message}`); } - try{ + try { res.end(); - }catch(e) { + } catch (e) { logger.info(`response end fail ${e.message}`); } - try{ + try { res.emit('done'); - }catch(e) { + } catch (e) { logger.info(`emit done event fail ${e.message}`); } - }else if(res.finished) { + } else if (res.finished) { - try{ + try { res.end(); - }catch(e) { + } catch (e) { logger.info(`response end fail ${e.message}`); } - try{ + try { res.emit('done'); - }catch(e) { + } catch (e) { logger.info(`emit done event fail ${e.message}`); } - }else if(!res._headerSent && !res.headersSent && !res.finished && res.statusCode === 200) { + } else if (!res._headerSent && !res.headersSent && !res.finished && res.statusCode === 200) { logger.debug('statusCode: ${statusCode}, _headerSent: ${_headerSent}, headersSent: ${headersSent}, finished: ${finished}', res); - //输出一条错误log方便定位问题 + // 输出一条错误log方便定位问题 logger.error('response timeout http://${host}${url}', { url: req.url, host: req.headers.host }); - try{ + try { res.writeHead(513); - }catch(e) { + } catch (e) { logger.info(`response 513 fail ${e.message}`); } - try{ + try { res.end(); - }catch(e) { + } catch (e) { logger.info(`response end fail ${e.message}`); } - try{ + try { res.emit('done'); - }catch(e) { + } catch (e) { logger.info(`emit done event fail ${e.message}`); } - }else{ + } else { logger.debug('statusCode: ${statusCode}, _headerSent: ${_headerSent}, headersSent: ${headersSent}, finished: ${finished}', res); - try{ + try { res.end(); - }catch(e) { + } catch (e) { logger.info(`response end fail ${e.message}`); } - try{ + try { res.emit('done'); - }catch(e) { + } catch (e) { logger.info(`emit done event fail ${e.message}`); } } @@ -542,8 +543,8 @@ function doRoute(req, res) { const clientIp = httpUtil.getUserIp(req); const userIp24 = httpUtil.getUserIp24(req); - //增加测试环境header - if(config.isTest) { + // 增加测试环境header + if (config.isTest) { res.setHeader('Test-Head', serverInfo.intranetIp || ''); } @@ -565,47 +566,47 @@ 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)) { - res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'}); + if (isTST.isTST(req)) { + res.writeHead(200, { 'Content-Type': 'text/html; charset=UTF-8' }); res.end(); return; } } - //安全中心扫描报个指标 - //支持从配置中直接屏蔽安全中心扫描请求 - 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'}); + res.writeHead(200, { 'Content-Type': 'text/html; charset=UTF-8' }); res.end('200'); return; } } - if(config.devMode) { + if (config.devMode) { logger.debug('config.devMode is true'); } - //log自动上报 + // log自动上报 logReport(req, res); res.writeHead = (function(fn) { return function(...args) { - if(alpha.isAlpha(req)) { - if(logger.getLog()) { + if (alpha.isAlpha(req)) { + if (logger.getLog()) { logger.getLog().showLineNumber = true; logger.debug('showLineNumber on'); } - //抓回包 + // 抓回包 httpUtil.captureBody(this); } @@ -615,13 +616,13 @@ function doRoute(req, res) { return fn.apply(this, args); }; - }(res.writeHead)); + })(res.writeHead); const 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'); } @@ -633,23 +634,23 @@ function doRoute(req, res) { appid: config.appid || null }); - //测试环境 - if(h5test.isTestUser(req, res)) { + // 测试环境 + if (h5test.isTestUser(req, res)) { return; } - //跟踪url调用深度 - const steps = parseInt(req.headers['tsw-trace-steps'] || '0') || 0; + // 跟踪url调用深度 + const steps = parseInt(req.headers['tsw-trace-steps'] || '0', 10) || 0; - //深度超过5层,直接拒绝 - if(steps >= 5) { + // 深度超过5层,直接拒绝 + if (steps >= 5) { tnm2.Attr_API('SUM_TSW_ROUTE_EXCEED', 1); - try{ - res.writeHead(503, {'Content-Type': 'text/html; charset=UTF-8'}); + 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}`); } @@ -660,11 +661,11 @@ function doRoute(req, res) { let 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') { const app = modulePath; modulePath = function(req, res, plug) { @@ -672,7 +673,7 @@ function doRoute(req, res) { }; } - if(modulePath && typeof modulePath.callback === 'function') { + if (modulePath && typeof modulePath.callback === 'function') { const app = modulePath; modulePath = function(req, res, plug) { @@ -680,23 +681,23 @@ function doRoute(req, res) { }; } - if(typeof modulePath !== 'function') { - if(req.REQUEST.pathname === '/419') { - if(typeof config.page419 === 'string') { + if (typeof modulePath !== 'function') { + if (req.REQUEST.pathname === '/419') { + 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') { - try{ - res.writeHead(404, {'Content-Type': 'text/html; charset=UTF-8'}); + 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; @@ -704,7 +705,7 @@ function doRoute(req, res) { const modulePathHandler = function() { const maybePromise = modulePath(req, res, plug); - if( + if ( typeof maybePromise === 'object' && typeof maybePromise.catch === 'function' @@ -718,56 +719,56 @@ function doRoute(req, res) { const 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); - res.writeHead(403, {'Content-Type': 'text/plain; charset=UTF-8'}); + res.writeHead(403, { 'Content-Type': 'text/plain; charset=UTF-8' }); res.end(); return; } - if(blackIpMap[clientIp] || blackIpMap[userIp24]) { + if (blackIpMap[clientIp] || blackIpMap[userIp24]) { 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'}); + res.writeHead(403, { 'Content-Type': 'text/plain; charset=UTF-8' }); res.end(); 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) { + // webso柔性 + 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}', { - cpuUsed : global.cpuUsed + cpuUsed: global.cpuUsed }); tnm2.Attr_API('SUM_TSW_WEBSO_LIMIT', 1); - try{ + try { res.writeHead(304, { 'Content-Type': 'text/html; charset=UTF-8', 'Etag': req.headers['if-none-match'] }); res.end(); - }catch(e) { + } catch (e) { logger.info(`response 304 fail ${e.message}`); } @@ -778,22 +779,22 @@ function doRoute(req, res) { const 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)) { - //wns请求不过门神检查 + if (httpUtil.isFromWns(req)) { + // wns请求不过门神检查 return modulePathHandler(); } xssFilter.check().done(function() { return modulePathHandler(); }).fail(function() { - res.writeHead(501, {'Content-Type': 'text/plain; charset=UTF-8'}); + 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( + } else if ( contentType.indexOf('application/x-www-form-urlencoded') > -1 || contentType.indexOf('text/plain') > -1 || contentType.indexOf('application/json') > -1 @@ -801,7 +802,7 @@ function doRoute(req, res) { parseBody(req, res, function() { return modulePathHandler(); }); - }else{ + } else { return modulePathHandler(); } @@ -812,23 +813,23 @@ function onerror(req, res, err) { const listener = req.listeners('fail'); const window = context.window || {}; - if(res.headersSent || res._headerSent || res.finished) { + if (res.headersSent || res._headerSent || res.finished) { return; } - if(listener && listener.length > 0) { - try{ + if (listener && listener.length > 0) { + try { req.emit('fail', err); - }catch(e) { + } catch (e) { logger.error(e && e.stack); } req.removeAllListeners('fail'); - }else if(window.onerror) { - try{ + } else if (window.onerror) { + try { window.onerror(err); - }catch(e) { + } catch (e) { logger.error(e && e.stack); } diff --git a/bin/proxy/index.js b/bin/proxy/index.js index 939a9027..11b521f1 100644 --- a/bin/proxy/index.js +++ b/bin/proxy/index.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 plug = require('../tsw/plug.js'); diff --git a/bin/proxy/master.js b/bin/proxy/master.js index 33e7343a..de72e494 100644 --- a/bin/proxy/master.js +++ b/bin/proxy/master.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 config = require('./config.js'); @@ -13,13 +13,13 @@ 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 { debugOptions } = process.binding('config'); const methodMap = {}; const workerMap = {}; const cpuMap = []; const isDeaded = false; -//阻止进程因异常而退出 +// 阻止进程因异常而退出 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'); @@ -45,9 +45,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 }); }); @@ -55,24 +55,26 @@ process.on('warning', function(warning) { process.on('unhandledRejection', (reason = {}, p = {}) => { - let - key = String(reason.message), - errStr = String(reason.stack), - mod_act, module, REQUEST; + let errStr = String(reason.stack), + mod_act, + module, + REQUEST; + + const key = String(reason.message); - 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') { const reasonStr = JSON.stringify(reason); logger.error('unhandledRejection reason: ' + reasonStr); - if(reasonStr === '{}') { + if (reasonStr === '{}') { return; } @@ -88,24 +90,24 @@ 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) { - //return; + if (serverOS.isWindows) { + // return; } require('util/mail/mail.js').SendMail(key, 'js', 600, { - 'Title' : key, - 'runtimeType' : 'unhandledRejection', - 'Content' : Content + 'Title': key, + 'runtimeType': 'unhandledRejection', + 'Content': Content }); }); @@ -120,45 +122,45 @@ function startServer() { let useWorker = true; - if(debugOptions && debugOptions.inspectorEnabled) { + if (debugOptions && debugOptions.inspectorEnabled) { useWorker = false; } - //windows下需要使用RR + // windows下需要使用RR cluster.schedulingPolicy = cluster.SCHED_RR; - if(cluster.isMaster && useWorker) { + if (cluster.isMaster && useWorker) { global.cpuUsed = cpuUtil.getCpuUsed(); setInterval(function() { global.cpuUsed = cpuUtil.getCpuUsed(); }, 3000); - //启动管理进程 + // 启动管理进程 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 + if (serverOS.isLinux) { + // 当前目录777,为heapsnapshot文件创建提供权限 + fs.chmodSync(__dirname, 0x1ff); // 0777 } - //根据cpu数来初始化并启动子进程 - if(config.runAtThisCpu === 'auto') { + // 根据cpu数来初始化并启动子进程 + if (config.runAtThisCpu === 'auto') { cpuUtil.cpus().forEach(function(v, i) { cpuMap.push(0); cluster.fork(process.env).cpuid = i; }); - }else{ + } else { config.runAtThisCpu.forEach(function(v, i) { cpuMap.push(0); cluster.fork(process.env).cpuid = v; }); } - //监听子进程是否fork成功 + // 监听子进程是否fork成功 cluster.on('fork', function(currWorker) { const cpu = getToBindCpu(currWorker); @@ -168,38 +170,38 @@ function startServer() { cpu: cpu }); - //绑定cpu + // 绑定cpu cpuUtil.taskset(cpu, currWorker.process.pid); - if(workerMap[cpu]) { + if (workerMap[cpu]) { closeWorker(workerMap[cpu]); } workerMap[cpu] = currWorker; cpuMap[cpu] = 1; - //监听子进程发来的消息并处理 + // 监听子进程发来的消息并处理 currWorker.on('message', function(...args) { const m = args[0]; - if(m && methodMap[m.cmd]) { + if (m && methodMap[m.cmd]) { methodMap[m.cmd].apply(this, args); } }); - //给子进程发送消息,启动http服务 + // 给子进程发送消息,启动http服务 currWorker.send({ - from:'master', - cmd:'listen', + from: 'master', + cmd: 'listen', cpu: cpu }); }); - //子进程退出时做下处理 + // 子进程退出时做下处理 cluster.on('disconnect', function(worker) { const cpu = getToBindCpu(worker); - if(worker.hasRestart) { + if (worker.hasRestart) { return; } @@ -211,12 +213,12 @@ function startServer() { restartWorker(worker); }); - //子进程被杀死的时候做下处理,原地复活 + // 子进程被杀死的时候做下处理,原地复活 cluster.on('exit', function(worker) { const cpu = getToBindCpu(worker); - if(worker.hasRestart) { + if (worker.hasRestart) { return; } @@ -232,44 +234,45 @@ function startServer() { let 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{ + try { cpu = getToBindCpu(worker); - if(config.isTest || config.devMode) { + if (config.isTest || config.devMode) { timeout = (cpu % 8) * 1000; - }else{ + } else { timeout = (cpu % 8) * 3000; } - setTimeout(function(worker, cpu) { + setTimeout((function(worker, cpu) { return function() { - if(!worker.exitedAfterDisconnect) { + 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', { cpu: cpu, timeout: timeout }); - }catch(e) { + } catch (e) { logger.error(e.stack); } } @@ -278,11 +281,12 @@ function startServer() { process.on('sendCmd2workerOnce', function(data) { - let key, worker; + let key, + worker; const CMD = data.CMD; const GET = data.GET; - if(isDeaded) { + if (isDeaded) { process.exit(0); } @@ -290,18 +294,18 @@ function startServer() { CMD }); - const targetCpu = GET.cpu || 0; + const targetCpu = parseInt(GET.cpu, 10) || 0; - for(key in workerMap) { + for (key in workerMap) { worker = workerMap[key]; - try{ - if(targetCpu == getToBindCpu(worker)) { - if(!worker.exitedAfterDisconnect) { - worker.send({from:'master', cmd:CMD, GET: GET}); + try { + if (targetCpu === getToBindCpu(worker)) { + if (!worker.exitedAfterDisconnect) { + worker.send({ from: 'master', cmd: CMD, GET: GET }); } break; } - }catch(e) { + } catch (e) { logger.error(e.stack); } } @@ -310,18 +314,18 @@ function startServer() { checkWorkerAlive(); startLogMan(); - //process.title = 'TSW/master/node'; - //保留node命令,不然运维监控不到 + // process.title = 'TSW/master/node'; + // 保留node命令,不然运维监控不到 - }else{ + } else { - //子进程直接引入proxy文件,注意此处else作用域属于子进程作用域,非本程序作用域 + // 子进程直接引入proxy文件,注意此处else作用域属于子进程作用域,非本程序作用域 process.title = 'TSW/worker/node'; logger.info('start worker....'); require('./http.proxy.js'); require('runtime/JankWatcher.js'); - //30分钟后开始算 + // 30分钟后开始算 !config.isTest && !config.devMode && setTimeout(function() { require('runtime/md5.check.js').check(); @@ -330,7 +334,7 @@ function startServer() { } -//处理子进程的心跳消息 +// 处理子进程的心跳消息 methodMap.heartBeat = function(m) { const worker = this; @@ -340,7 +344,7 @@ methodMap.heartBeat = function(m) { worker.lastLiveTime = now; }; -//关闭一个worker +// 关闭一个worker function closeWorker(worker) { const cpu = worker.cpuid; let closeTimeWait = 10000; @@ -352,25 +356,25 @@ function closeWorker(worker) { closeTimeWait = Math.min(60000, closeTimeWait) || 10000; - if(worker.hasClose) { + if (worker.hasClose) { return; } - if(workerMap[cpu] === worker) { + if (workerMap[cpu] === worker) { delete workerMap[cpu]; } - const closeFn = function(worker) { + const closeFn = (function(worker) { let closed = false; const pid = worker.process.pid; return function() { - if(closed) { + if (closed) { return; } - try{ + try { process.kill(pid, 9); - }catch(e) { + } catch (e) { logger.info(`kill worker fail ${e.message}`); } @@ -378,28 +382,28 @@ function closeWorker(worker) { closed = true; }; - }(worker); + })(worker); setTimeout(closeFn, closeTimeWait); - if(worker.exitedAfterDisconnect) { + if (worker.exitedAfterDisconnect) { worker.hasClose = true; return; } - try{ + try { worker.disconnect(closeFn); - }catch(e) { + } catch (e) { logger.info(e.stack); } } -//重启worker +// 重启worker function restartWorker(worker) { const cpu = getToBindCpu(worker); - if(worker.hasRestart) { + if (worker.hasRestart) { return; } @@ -418,50 +422,50 @@ function restartWorker(worker) { cluster.fork(process.env).cpuid = cpu; } -//定时检测子进程存活,发现15秒没响应的就干掉 +// 定时检测子进程存活,发现15秒没响应的就干掉 function checkWorkerAlive() { setInterval(function() { - let - nowDate = new Date(), - now = nowDate.getTime(), - key, + let key, worker, cpuid; - for(key in workerMap) { + const nowDate = new Date(); + const now = nowDate.getTime(); + + 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', { - pid : worker.process.pid, + pid: worker.process.pid, cpu: cpuid }); restartWorker(worker); } - //内存超限进程处理 - if(worker.lastMessage) { + // 内存超限进程处理 + if (worker.lastMessage) { const currMemory = worker.lastMessage.memoryUsage; - //logger.debug(currMemory); + // 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', { memoryUsage: currMemory.rss, memoryLimit: config.memoryLimit, - pid : worker.process.pid, + pid: worker.process.pid, cpu: cpuid }); @@ -472,33 +476,33 @@ function checkWorkerAlive() { } } - if( + if ( true && (nowDate.getHours() % 8 === 0) && nowDate.getMinutes() === 1 && nowDate.getSeconds() <= 10 ) { - //8小时一次 + // 8小时一次 require('api/keyman/runtimeAdd.js').hello(); } }, 5000); } -//获取需要绑定的CPU编号 +// 获取需要绑定的CPU编号 function getToBindCpu(worker) { - let cpu = 0;//如果只有一个cpu或者都占用了 + let cpu = 0;// 如果只有一个cpu或者都占用了 let i; - if(worker.cpuid !== undefined) { + if (typeof worker.cpuid === 'undefined') { cpu = worker.cpuid; return cpu; - }else{ + } else { - for(i = 0; i < cpuMap.length; i++) { + for (i = 0; i < cpuMap.length; i++) { const c = cpuMap[i]; - if(c == 0) { + if (c === 0) { cpu = i; worker.cpuid = cpu; break; @@ -511,9 +515,9 @@ function getToBindCpu(worker) { return cpu; } -//log管理 +// log管理 function startLogMan() { require('api/logman').start({ - delay: 'H' //按小时归类log + delay: 'H' // 按小时归类log }); } diff --git a/bin/proxy/websocket.js b/bin/proxy/websocket.js index 568507b2..3b57fa1e 100644 --- a/bin/proxy/websocket.js +++ b/bin/proxy/websocket.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 WebSocket = require('ws'); const WSServer = WebSocket.Server; @@ -30,48 +30,48 @@ function bind_listen(server) { parseGet(req); - if(req.headers['x-client-proto'] === 'https') { + if (req.headers['x-client-proto'] === 'https') { req.REQUEST.protocol = 'wss'; } else { req.REQUEST.protocol = 'ws'; } const testSpaceInfo = h5test.getTestSpaceInfo(req); - if(testSpaceInfo) { + if (testSpaceInfo) { const clientReqHeaders = Object.assign({}, req.headers); delete clientReqHeaders['sec-websocket-key']; wsClient = new WebSocket('ws://' + testSpaceInfo.testIp + req.url, testSpaceInfo.testPort, { - headers : clientReqHeaders + headers: clientReqHeaders }); } - if(wsClient) { - //存在代理,new websocket时相当于触发了connection了 + if (wsClient) { + // 存在代理,new websocket时相当于触发了connection了 wsClient.on('message', function(data) { - //代理收到目标服务器的回报,再发送给客户端 - if(ws.readyState == WebSocket.OPEN) { + // 代理收到目标服务器的回报,再发送给客户端 + if (ws.readyState === WebSocket.OPEN) { ws.send(data); } }); wsClient.on('close', function(data, msg) { - //目标服务器关闭 + // 目标服务器关闭 ws.close(); }); wsClient.on('error', function(error) { - if(ws.readyState == WebSocket.OPEN) { + if (ws.readyState === WebSocket.OPEN) { ws.send('TSW_Websocket_proxy_client_error'); } }); - }else{ + } else { wsRoute.doRoute(ws, 'connection'); } ws.on('message', function(message) { tnm2.Attr_API('SUM_TSW_WEBSOCKET_MESSAGE', 1); - if(wsClient) { - //存在代理 - if(wsClient.readyState == WebSocket.OPEN) { - wsClient.send(message); + if (wsClient) { + // 存在代理 + if (wsClient.readyState === WebSocket.OPEN) { + wsClient.send(message); } return; } @@ -79,18 +79,18 @@ function bind_listen(server) { let requestData = {}; try { requestData = JSON.parse(message); - } catch(e) { + } catch (e) { logger.error(`parse message fail ${e.message}`); } // var mod_act = wsModAct.getModAct(ws); const cwrap = new ContextWrap({ url: req.url - //reqSocket: ws, - //rspSocket: ws + // reqSocket: ws, + // rspSocket: ws }); - //cwrap.add(ws); + // cwrap.add(ws); cwrap.run(function() { const window = context.window || {}; window.websocket = ws; @@ -99,7 +99,7 @@ function bind_listen(server) { let hasEnd = false; const tid = setTimeout(function() { logger.debug('[websocket server] respond timeout'); - if(hasEnd) { + if (hasEnd) { return; } hasEnd = true; @@ -107,7 +107,7 @@ function bind_listen(server) { let respond; const window = context.window || {}; - if(requestData.seq) { + if (requestData.seq) { respond = JSON.stringify({ 'seq': requestData.seq, 'ret': 513, @@ -121,12 +121,12 @@ function bind_listen(server) { }); } - //将message转成fiddler抓包的包体内容 - if(cwrap) { - if(window.websocket) { + // 将message转成fiddler抓包的包体内容 + if (cwrap) { + if (window.websocket) { window.websocket.upgradeReq.REQUEST.body = message; } - if(window.response) { + if (window.response) { window.response._body = Buffer.from(respond); window.response.setHeader('content-length', window.response._body.length); window.response.setHeader('content-type', 'websocket'); @@ -134,7 +134,7 @@ function bind_listen(server) { window.response.emit('afterMessage'); window.response.removeAllListeners('sendMessage'); } - //cwrap.remove(ws); + // cwrap.remove(ws); window.websocket = null; cwrap.destroy(); } @@ -145,18 +145,18 @@ function bind_listen(server) { window.response.once('sendMessage', function(respondData) { let respond; - if(tid) { + if (tid) { clearTimeout(tid); } - //确认是否已经响应请求 - if(hasEnd) { + // 确认是否已经响应请求 + if (hasEnd) { return; } hasEnd = true; tnm2.Attr_API('SUM_TSW_WEBSOCKET_RESPONSE', 1); - if(respondData) { + if (respondData) { respond = respondData; } else { respond = { @@ -164,19 +164,19 @@ function bind_listen(server) { 'msg': '' }; } - if(requestData.seq) { + if (requestData.seq) { respond.seq = requestData.seq; } respond = JSON.stringify(respond); - if(ws.readyState === 1) { + if (ws.readyState === 1) { ws.send(respond); logger.debug('websocket message respond: ' + respond); } else { logger.debug('websocket is not open, message respond abort, readyState: ' + ws.readyState); } - //将message转成fiddler抓包的包体内容 - if(window.websocket) { + // 将message转成fiddler抓包的包体内容 + if (window.websocket) { window.websocket.upgradeReq.REQUEST.body = message; } window.response._body = Buffer.from(respond); @@ -184,7 +184,7 @@ function bind_listen(server) { window.response.setHeader('content-type', 'websocket'); window.response.writeHead(101); window.response.emit('afterMessage'); - //cwrap.remove(ws); + // cwrap.remove(ws); window.websocket = null; cwrap.destroy(); }); @@ -194,7 +194,7 @@ function bind_listen(server) { }); ws.on('close', function(code, reason) { tnm2.Attr_API('SUM_TSW_WEBSOCKET_CLOSE', 1); - if(wsClient) { + if (wsClient) { wsClient.close(); return; } @@ -202,8 +202,8 @@ function bind_listen(server) { }); ws.on('error', function(error) { tnm2.Attr_API('SUM_TSW_WEBSOCKET_ERROR', 1); - if(wsClient) { - if(wsClient.readyState == WebSocket.OPEN) { + if (wsClient) { + if (wsClient.readyState === WebSocket.OPEN) { wsClient.send('TSW_Websocket_proxy_server_error'); } return; @@ -219,7 +219,7 @@ exports.start_listen = function() { }); bind_listen(ws); - if(global.TSW_HTTPS_SERVER) { + if (global.TSW_HTTPS_SERVER) { const wss = new WSServer({ server: global.TSW_HTTPS_SERVER }); diff --git a/bin/proxy/ws.mod.act.js b/bin/proxy/ws.mod.act.js index 7aa75209..b74a6119 100644 --- a/bin/proxy/ws.mod.act.js +++ b/bin/proxy/ws.mod.act.js @@ -1,10 +1,10 @@ -/*! +/* ! * 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'; + module.exports = require('./config.js').wsModAct; diff --git a/bin/proxy/ws.mod.map.js b/bin/proxy/ws.mod.map.js index a5824f69..16f15d23 100644 --- a/bin/proxy/ws.mod.map.js +++ b/bin/proxy/ws.mod.map.js @@ -1,10 +1,10 @@ -/*! +/* ! * 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'; + module.exports = require('./config.js').wsModMap; diff --git a/bin/proxy/ws.route.js b/bin/proxy/ws.route.js index c50d077a..de03d4a8 100644 --- a/bin/proxy/ws.route.js +++ b/bin/proxy/ws.route.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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'; + exports.doRoute = function(ws, type, d1, d2) { const wsModAct = require('./ws.mod.act'); @@ -13,8 +13,9 @@ exports.doRoute = function(ws, type, d1, d2) { const logger = require('logger'); const contextMod = require('context.js'); - let mod_act = wsModAct.getModAct(ws), + const mod_act = wsModAct.getModAct(ws), moduleObj = wsModMap.find(mod_act, ws); + if (typeof moduleObj !== 'object') { try { ws.send('module ' + mod_act + ' is not object'); @@ -23,22 +24,22 @@ exports.doRoute = function(ws, type, d1, d2) { } return; } - if (typeof moduleObj.onConnection != 'function') { + if (typeof moduleObj.onConnection !== 'function') { moduleObj.onConnection = function(ws) { ws.readyState === 1 && ws.send('no onConnection funtion,so go default'); }; } - if (typeof moduleObj.onMessage != 'function') { + if (typeof moduleObj.onMessage !== 'function') { moduleObj.onMessage = function(ws, data) { ws.readyState === 1 && ws.send('no onMessage function,so go default,ws server get message:' + data); }; } - if (typeof moduleObj.onClose != 'function') { + if (typeof moduleObj.onClose !== 'function') { moduleObj.onClose = function() { logger.debug('no onClose function, so go default'); }; } - if (typeof moduleObj.onError != 'function') { + if (typeof moduleObj.onError !== 'function') { moduleObj.onError = function() { logger.debug('no onError function, so go default'); }; diff --git a/bin/tsw/ajax/ajax.js b/bin/tsw/ajax/ajax.js index da88da27..1b322f3f 100644 --- a/bin/tsw/ajax/ajax.js +++ b/bin/tsw/ajax/ajax.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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'); @@ -17,14 +17,14 @@ const qs = require('qs'); const form = require('./form.js'); const token = require('./token.js'); const config = require('config.js'); -const {isWindows} = require('util/isWindows.js'); +const { isWindows } = require('util/isWindows.js'); const zlib = require('zlib'); const L5 = require('api/L5/L5.api.js'); const dcapi = require('api/libdcapi/dcapi.js'); const httpUtil = require('util/http.js'); const isTST = require('util/isTST.js'); const optionsUtil = require('./http-https.options.js'); -const sbFunction = vm.runInNewContext('(Function)', Object.create(null)); //沙堆 +const SbFunction = vm.runInNewContext('(Function)', Object.create(null)); // 沙堆 let lastRetry = 0; module.exports = new Ajax(); @@ -41,19 +41,19 @@ Ajax.prototype.proxy = function(req, res) { const window = context.window || {}; - if(!req) { + if (!req) { req = window.request; } - if(!res) { + if (!res) { req = window.response; } - if(!req) { + if (!req) { logger.warn('!req is true'); } - if(!res) { + if (!res) { logger.warn('!res is true'); } @@ -62,44 +62,42 @@ Ajax.prototype.proxy = function(req, res) { Ajax.prototype.request = function(opt) { - if(config.devMode && isWindows && config.httpProxy && config.httpProxy.enable && !opt.ip && !opt.devIp) { + if (config.devMode && isWindows && config.httpProxy && config.httpProxy.enable && !opt.ip && !opt.devIp) { opt.proxyIp = config.httpProxy.ip; opt.proxyPort = config.httpProxy.port; } - if(opt.l5api) { + if (opt.l5api) { return this.l5Request(opt); - }else{ + } else { return this.doRequest(opt); } }; -//dns带你去ajax +// dns带你去ajax Ajax.prototype.dnsRequest = function(opt) { return this.doRequest(opt); }; -//L5带你去ajax +// L5带你去ajax Ajax.prototype.l5Request = function(opt) { - let - defer = Deferred.create(), - that = this, - l5api; + const defer = Deferred.create(); + const that = this; - l5api = Deferred.extend({ + const l5api = Deferred.extend({ modid: 0, cmd: 0 }, opt.l5api); - if(l5api.modid === 0) { + if (l5api.modid === 0) { return this.doRequest(opt); } - if(isWindows) { - //windows不走L5,直接请求 + if (isWindows) { + // windows不走L5,直接请求 return this.doRequest(opt); } @@ -110,31 +108,31 @@ Ajax.prototype.l5Request = function(opt) { opt.ip = route.ip; opt.port = route.port; - //开始真正的请求 + // 开始真正的请求 that.doRequest(opt).always(function(d) { - if(d.opt && d.opt.headers && isTST.isTST(opt)) { - //忽略安全中心请求 + if (d.opt && d.opt.headers && isTST.isTST(opt)) { + // 忽略安全中心请求 return; } - if(route.ip !== opt.ip) { - //不用上报,可能走了devIp + if (route.ip !== opt.ip) { + // 不用上报,可能走了devIp return; } const end = new Date(); - //上报调用结果 + // 上报调用结果 L5.ApiRouteResultUpdate({ - modid : l5api.modid, - cmd : l5api.cmd, - usetime : end - start, - ret : (d && (d.hasError || d.hasRetry)) ? -1 : 0, - ip : route.ip, - port : route.port, - pre : route.pre, - flow : route.flow + modid: l5api.modid, + cmd: l5api.cmd, + usetime: end - start, + ret: (d && (d.hasError || d.hasRetry)) ? -1 : 0, + ip: route.ip, + port: route.port, + pre: route.pre, + flow: route.flow }); }).done(function(d) { @@ -145,12 +143,12 @@ Ajax.prototype.l5Request = function(opt) { }).fail(function(d) { - if(opt.dcapi) { + if (opt.dcapi) { dcapi.report(Deferred.extend({}, opt.dcapi, { - toIp : '127.0.0.1', - code : d.ret, - isFail : 1, - delay : 100 + toIp: '127.0.0.1', + code: d.ret, + isFail: 1, + delay: 100 })); } @@ -172,82 +170,83 @@ Ajax.prototype.l5Request = function(opt) { }; -//普通请求 +// 普通请求 Ajax.prototype.doRequest = function(opt) { const window = context.window || {}; - let defer = Deferred.create(), - tid = null, - that = this, + + const defer = Deferred.create(); + const that = this; + const times = { + start: 0, + response: 0, + end: 0 + }; + + let tid = null, currAgent = false, - times = { - start: 0, - response: 0, - end: 0 - }, - currRetry, - AJAXSN, logPre, request, - key, v, + key, + v, obj; - if(context.AJAXSN) { + if (context.AJAXSN) { context.AJAXSN++; - }else{ + } else { context.AJAXSN = 1; } - AJAXSN = context.AJAXSN || 0; + const AJAXSN = context.AJAXSN || 0; - logPre = '[' + AJAXSN + '] '; + const logPre = '[' + AJAXSN + '] '; opt = Deferred.extend({ - type : 'GET', - url : '', - devIp : '', - devPort : '', - testIp : '', - testPort : '', - ip : '', - port : '', - host : '', - path : '', - headers : {}, - timeout : 2000, - data : {}, - protocol : '', //protocol协议 - retry : 1, //重试次数 - keepAlive : false, //使用长连接 - body : null, //对应HTTP BODY - success : null, //success事件 - error : null, //error事件 - response : null, //response事件 - dataType : 'html', //数据类型 - send : null, //send事件 - l5api : {}, //l5api - useIPAsHost : false, //使用IP覆盖host - dcapi : null, //api监控上报 - maxBodySize : -1, //指定最大body大小,超过之后整个请求直接断掉 - autoToken : false, //自动带token - autoQzoneToken : false, //自动带Qzonetoken - jsonpCallback : null, //JSONP回调名字 - ignoreErrorReport : false, //上报时忽略错误 - useJsonParseOnly : false, //强制jsonParse - encodeMethod : 'encodeURIComponent', - enctype : 'application/x-www-form-urlencoded', //multipart/form-data application/json - isRetriedRequest : false //标记当前请求是否是重试的请求 + type: 'GET', + url: '', + devIp: '', + devPort: '', + testIp: '', + testPort: '', + ip: '', + port: '', + host: '', + path: '', + headers: {}, + timeout: 2000, + data: {}, + protocol: '', // protocol协议 + retry: 1, // 重试次数 + keepAlive: false, // 使用长连接 + body: null, // 对应HTTP BODY + success: null, // success事件 + error: null, // error事件 + response: null, // response事件 + dataType: 'html', // 数据类型 + send: null, // send事件 + l5api: {}, // l5api + useIPAsHost: false, // 使用IP覆盖host + dcapi: null, // api监控上报 + maxBodySize: -1, // 指定最大body大小,超过之后整个请求直接断掉 + autoToken: false, // 自动带token + autoQzoneToken: false, // 自动带Qzonetoken + jsonpCallback: null, // JSONP回调名字 + ignoreErrorReport: false, // 上报时忽略错误 + useJsonParseOnly: false, // 强制jsonParse + encodeMethod: 'encodeURIComponent', + enctype: 'application/x-www-form-urlencoded', // multipart/form-data application/json + isRetriedRequest: false // 标记当前请求是否是重试的请求 }, config.ajaxDefaultOptions, opt); opt.type = opt.type.toUpperCase(); - if(!httpUtil.isGetLike(opt.type)) { + if (!httpUtil.isGetLike(opt.type)) { opt.retry = 0; } - currRetry = opt.retry; + const currRetry = opt.retry; - if(opt.dataType !== 'proxy') { - //默认开启gzip + if (opt.dataType !== 'proxy') { + // 默认开启gzip opt.headers = Deferred.extend( { 'accept-encoding': 'gzip,deflate' @@ -256,103 +255,103 @@ Ajax.prototype.doRequest = function(opt) { ); } - if(this._proxyRequest) { + if (this._proxyRequest) { - //是代理请求 + // 是代理请求 opt.headers = Deferred.extend( { - //这个不能删除,会被修改 + // 这个不能删除,会被修改 }, this._proxyRequest.headers, { - 'x-forwarded-for' : httpUtil.getUserIp(this._proxyRequest) + 'x-forwarded-for': httpUtil.getUserIp(this._proxyRequest) }, opt.headers ); } - if(opt.headers['origin'] === 'null') { - //兼容客户端 + if (opt.headers['origin'] === 'null') { + // 兼容客户端 opt.headers['origin'] = undefined; } opt.type = opt.type.toUpperCase(); - if(opt.url) { + if (opt.url) { obj = url.parse(opt.url); - opt.protocol=opt.protocol || obj.protocol || 'http:'; + opt.protocol = opt.protocol || obj.protocol || 'http:'; opt.host = opt.host || obj.hostname; opt.port = opt.port || obj.port || (opt.protocol === 'https:' ? 443 : 80); opt.path = opt.path || obj.path; logger.debug(logPre + 'host from url: ' + opt.host); - }else{ + } else { opt.port = opt.port || (opt.protocol === 'https:' ? 443 : 80); } - if(!opt.data) { + if (!opt.data) { opt.data = {}; } - if(opt.dataType === 'proxy') { + if (opt.dataType === 'proxy') { opt.autoToken = false; opt.autoQzoneToken = false; } - if(opt.autoToken && !opt.isRetriedRequest) { - if(opt.path.indexOf('?') === -1) { + if (opt.autoToken && !opt.isRetriedRequest) { + if (opt.path.indexOf('?') === -1) { opt.path = opt.path + '?g_tk=' + token.token(); - }else{ + } else { opt.path = opt.path + '&g_tk=' + token.token(); } } - if(opt.autoQzoneToken) { + if (opt.autoQzoneToken) { opt.headers['x-qzone-token'] = opt.headers['x-qzone-token'] || String(opt.autoQzoneToken); } - if(opt.encodeMethod === 'encodeURIComponent') { + if (opt.encodeMethod === 'encodeURIComponent') { opt.encoder = encodeURIComponent; - }else if(opt.encodeMethod === 'encodeURI') { + } else if (opt.encodeMethod === 'encodeURI') { opt.encoder = encodeURI; - }else if(opt.encodeMethod === 'escape') { + } else if (opt.encodeMethod === 'escape') { opt.encoder = escape; - }else{ + } else { opt.encoder = encodeURIComponent; } - if(httpUtil.isPostLike(opt.type)) { + if (httpUtil.isPostLike(opt.type)) { - //优先使用body - if(!opt.body && opt.data && Object.keys(opt.data).length > 0) { - if(opt.enctype === 'application/json') { + // 优先使用body + 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; opt.body = form.getFormBuffer(opt); - }else{ + } else { opt.headers['content-type'] = opt.enctype; opt.body = qs.stringify(opt.data, { arrayFormat: 'brackets', skipNulls: true, encoder: opt.encoder }); } } - }else{ - if(!opt.isRetriedRequest) { + } else { + if (!opt.isRetriedRequest) { const 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{ + } else { opt.path = opt.path + '&' + query; } } @@ -360,20 +359,20 @@ 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; opt.port = opt.devPort || opt.port; opt.proxyPort = null; @@ -382,55 +381,55 @@ Ajax.prototype.doRequest = function(opt) { } - if(opt.host) { + if (opt.host) { opt.headers.host = opt.host; - }else{ + } 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); } - //开始时间点 + // 开始时间点 times.start = new Date().getTime(); 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, { - toIp : opt.ip, - code : code, - isFail : isFail, - delay : new Date - times.start + toIp: opt.ip, + code: code, + isFail: isFail, + delay: new Date() - times.start })); } } - //解决undefined报错问题 - for(key in opt.headers) { + // 解决undefined报错问题 + for (key in opt.headers) { v = opt.headers[key]; - if(v === undefined) { + if (v === undefined) { logger.debug('delete header: ${key}', { key: key }); @@ -438,7 +437,7 @@ Ajax.prototype.doRequest = function(opt) { continue; } - if(v === null) { + if (v === null) { logger.debug('delete header: ${key}', { key: key }); @@ -446,7 +445,7 @@ Ajax.prototype.doRequest = function(opt) { continue; } - if(key.indexOf(' ') >= 0) { + if (key.indexOf(' ') >= 0) { logger.debug('delete header: ${key}', { key: key }); @@ -456,7 +455,7 @@ 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}', { @@ -465,11 +464,11 @@ Ajax.prototype.doRequest = function(opt) { } } - 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{ + } else { opt.send = function(request) { that._proxyRequest.on('data', function(buffer) { request.write(buffer); @@ -483,72 +482,72 @@ 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)) { + } 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:') { - //https不走代理 + if (opt.protocol === 'https:') { + // https不走代理 opt.proxyIp = null; - opt.proxyPort= 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}', { - protocol : opt.protocol, - type : opt.type, - dataType : opt.dataType, - ip : opt.proxyIp || opt.ip || opt.host, - port : opt.proxyPort || opt.port, - host : opt.host, - path : opt.path + protocol: opt.protocol, + type: opt.type, + dataType: opt.dataType, + ip: opt.proxyIp || opt.ip || opt.host, + port: opt.proxyPort || opt.port, + host: opt.host, + 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{ + } 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{ + } else { currAgent = optionsUtil.getHttpAgent(opt.headers.host); } - }else{ + } else { currAgent = false; } request = (opt.protocol === 'https:' ? https : http).request({ - agent : currAgent, - host : opt.proxyIp || opt.ip || opt.host, - port : opt.proxyPort || opt.port, - path : opt.proxyPath || opt.path, - method : opt.type, - headers : opt.headers + agent: currAgent, + host: opt.proxyIp || opt.ip || opt.host, + port: opt.proxyPort || opt.port, + path: opt.proxyPath || opt.path, + method: opt.type, + headers: opt.headers }); request.setNoDelay(true); @@ -558,15 +557,15 @@ Ajax.prototype.doRequest = function(opt) { clearTimeout(tid); request.removeAllListeners(); - //request.abort(); 长连接不能开 - //request.destroy(); 长连接不能开 + // request.abort(); 长连接不能开 + // request.destroy(); 长连接不能开 request = null; tid = null; }); tid = setTimeout(function() { logger.debug(logPre + 'timeout: ${timeout}', { - timeout : opt.timeout + timeout: opt.timeout }); request.abort(); request.emit('fail'); @@ -578,29 +577,29 @@ Ajax.prototype.doRequest = function(opt) { request.once('fail', function(err) { - if(defer.isRejected() || defer.isResolved()) { + if (defer.isRejected() || defer.isResolved()) { return; } times.end = new Date().getTime(); - if(err) { + if (err) { logger.error(logPre + '[${userIp}] ${type} error ~ ${ip}:${port} ${protocol}//${host}${path} ' + err.stack, { - protocol : opt.protocol, - type : opt.type, - dataType : opt.dataType, - ip : opt.proxyIp || opt.ip, - port : opt.proxyPort || opt.port, - host : opt.host, - path : opt.path, - userIp : httpUtil.getUserIp() + protocol: opt.protocol, + type: opt.type, + dataType: opt.dataType, + ip: opt.proxyIp || opt.ip, + port: opt.proxyPort || opt.port, + host: opt.host, + path: opt.path, + userIp: httpUtil.getUserIp() }); report(opt, 1, 502); defer.reject({ opt: opt, hasError: true, code: 502, - e:err, + e: err, msg: err.message, times: times }); @@ -608,23 +607,23 @@ Ajax.prototype.doRequest = function(opt) { return; } - if(window.response && window.response.__hasClosed) { + 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, - ip : opt.proxyIp || opt.ip, - port : opt.proxyPort || opt.port, - host : opt.host, - path : opt.path, - userIp : httpUtil.getUserIp() + protocol: opt.protocol, + type: opt.type, + dataType: opt.dataType, + ip: opt.proxyIp || opt.ip, + port: opt.proxyPort || opt.port, + host: opt.host, + path: opt.path, + userIp: httpUtil.getUserIp() }); report(opt, 2, 600 + opt.retry); defer.reject({ opt: opt, hasError: true, code: 600, - e:err, + e: err, msg: 'request error', times: times }); @@ -634,18 +633,18 @@ Ajax.prototype.doRequest = function(opt) { report(opt, 1, 513 + opt.retry); logger.error(logPre + '[${userIp}] ${type} error ~ ${ip}:${port} ${protocol}//${host}${path}', { - protocol : opt.protocol, - type : opt.type, - dataType : opt.dataType, - ip : opt.proxyIp || opt.ip, - port : opt.proxyPort || opt.port, - host : opt.host, - path : opt.path, - userIp : httpUtil.getUserIp() + protocol: opt.protocol, + type: opt.type, + dataType: opt.dataType, + ip: opt.proxyIp || opt.ip, + port: opt.proxyPort || opt.port, + host: opt.host, + path: opt.path, + userIp: httpUtil.getUserIp() }); - //限制次数,1s只放过一个 - if(opt.retry > 0 && times.end - lastRetry > 1000) { + // 限制次数,1s只放过一个 + if (opt.retry > 0 && times.end - lastRetry > 1000) { lastRetry = times.end; opt.retry = opt.retry - 1; @@ -664,7 +663,7 @@ Ajax.prototype.doRequest = function(opt) { }); return; - }else{ + } else { report(opt, 1, 513 + opt.retry); } @@ -672,7 +671,7 @@ Ajax.prototype.doRequest = function(opt) { opt: opt, hasError: true, code: 513, - e:err, + e: err, msg: 'request error', times: times }); @@ -684,12 +683,12 @@ Ajax.prototype.doRequest = function(opt) { let pipe = response; let 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}', { currRetry: currRetry, retry: opt.retry @@ -697,10 +696,10 @@ Ajax.prototype.doRequest = function(opt) { return; } - //不能再重试了 + // 不能再重试了 opt.retry = 0; - //不能省掉哦 + // 不能省掉哦 process.domain && process.domain.add(response); opt.statusCode = response.statusCode; @@ -711,8 +710,8 @@ Ajax.prototype.doRequest = function(opt) { times.response = new Date().getTime(); - if(opt.dataType === 'proxy') { - if(response.statusCode >= 500 && response.statusCode <= 599 && response.statusCode !== 501) { + 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, @@ -720,20 +719,20 @@ Ajax.prototype.doRequest = function(opt) { headers: JSON.stringify(opt.headers, null, 2), resHeaders: JSON.stringify(response.headers, null, 2), encoding: response.headers['content-encoding'], - cost: +new Date() - times.start + cost: Number(new Date()) - times.start }); - }else{ + } else { 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), encoding: response.headers['content-encoding'], - cost: +new Date() - times.start + cost: Number(new Date()) - times.start }); } - }else{ - if( + } else { + if ( opt.statusCode === 200 || opt.statusCode === 206 || opt.statusCode === 666 @@ -744,9 +743,9 @@ Ajax.prototype.doRequest = function(opt) { port: opt.remotePort, statusCode: response.statusCode, encoding: response.headers['content-encoding'], - cost: +new Date() - times.start + cost: Number(new Date()) - times.start }); - }else{ + } else { logger.debug(logPre + '${ip}:${port} response ${statusCode} cost:${cost}ms ${encoding}\nrequest: ${headers}\nresponse: ${resHeaders}', { ip: opt.remoteAddress, port: opt.remotePort, @@ -754,12 +753,12 @@ Ajax.prototype.doRequest = function(opt) { headers: JSON.stringify(opt.headers, null, 2), resHeaders: JSON.stringify(response.headers, null, 2), encoding: response.headers['content-encoding'], - cost: +new Date() - times.start + cost: Number(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() @@ -767,24 +766,24 @@ 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') { + } else { + if (response.headers['content-encoding'] === 'gzip') { pipe = zlib.createGunzip(); response.on('data', function(buffer) { @@ -793,7 +792,7 @@ Ajax.prototype.doRequest = function(opt) { response.once('end', function() { pipe.end(); }); - }else if(response.headers['content-encoding'] === 'deflate') { + } else if (response.headers['content-encoding'] === 'deflate') { pipe = zlib.createInflateRaw(); @@ -806,20 +805,20 @@ Ajax.prototype.doRequest = function(opt) { } } - 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) { + // 如果是测试环境,增加一个proxy头以便识别ip + if (opt.ip && config.isTest) { that._proxyResponse.setHeader('Proxy-Domain-Ip', opt.ip); } - if(config.isTest && response.headers['cache-control']) { + if (config.isTest && response.headers['cache-control']) { response.headers['cache-control'] = 'max-age=0, must-revalidate'; } - if( + if ( !response.headers['content-encoding'] && response.headers['content-type'] && (response.headers['content-type'].indexOf('text/') === 0 || @@ -827,19 +826,19 @@ Ajax.prototype.doRequest = function(opt) { response.headers['content-type'] === 'x-json') ) { - //自带压缩功能 + // 自带压缩功能 delete response.headers['transfer-encoding']; delete response.headers['content-length']; delete response.headers['connection']; - //解决循环依赖 + // 解决循环依赖 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{ + } else { delete response.headers['connection']; that._proxyResponse.writeHead(response.statusCode, httpUtil.formatHeader(response.headers)); @@ -854,30 +853,30 @@ Ajax.prototype.doRequest = function(opt) { pipe.timeCurr = Date.now(); - //logger.debug('${logPre}receive data: ${size},\tcost: ${cost}ms',{ + // logger.debug('${logPre}receive data: ${size},\tcost: ${cost}ms',{ // logPre: logPre, // cost: cost, // size: chunk.length - //}); + // }); 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 + max: opt.maxBodySize }); request.abort(); request.emit('fail'); return; } - if(isProxy) { - if(!that._proxyResponse.finished) { + if (isProxy) { + if (!that._proxyResponse.finished) { that._proxyResponse.write(chunk); } - }else{ + } else { result.push(chunk); } }); @@ -901,15 +900,19 @@ Ajax.prototype.doRequest = function(opt) { pipe.once('done', function() { - let obj, responseText, buffer, code; - let key, Content; + let obj, + responseText, + buffer, + code; + let 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; } @@ -921,10 +924,10 @@ 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{ + } else { report(opt, 0, response.statusCode); } @@ -945,7 +948,7 @@ Ajax.prototype.doRequest = function(opt) { return; } - if(opt.dataType === response.statusCode) { + if (opt.dataType === response.statusCode) { report(opt, 0, response.statusCode); defer.resolve({ opt: opt, @@ -961,33 +964,33 @@ 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) { - //dataType为proxy但是不走代理模式的时候,30x类型的返回码当作成功上报 - if(response.statusCode >= 300 && response.statusCode < 400) { - if(opt.dataType === 'proxy') { + 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); - }else{ + } else { report(opt, 2, response.statusCode); } - }else{ + } else { report(opt, 1, response.statusCode); } @@ -1005,43 +1008,43 @@ Ajax.prototype.doRequest = function(opt) { return; } - if(opt.dataType === 'json' || opt.dataType === 'jsonp') { + if (opt.dataType === 'json' || opt.dataType === 'jsonp') { - try{ + try { - if(opt.jsonpCallback) { - //json|jsonp + 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) { - //json only + obj = new SbFunction(code)(); + } else if (opt.dataType === 'json' && opt.useJsonParseOnly) { + // json only obj = JSON.parse(responseText); - }else{ - //other + } else { + // other code = `return (${responseText})`; - try{ - obj = new sbFunction(code)(); - }catch(e) { - //尝试JSON.parse + try { + obj = new SbFunction(code)(); + } catch (e) { + // 尝试JSON.parse obj = JSON.parse(responseText); } } - }catch(e) { + } catch (e) { let parseErr = e; - if(e && code) { - try{ + if (e && code) { + try { code = code.replace(/[\u2028\u2029]/g, ''); - obj = new sbFunction(code)(); + obj = new SbFunction(code)(); parseErr = null; - }catch(err) { + } catch (err) { logger.error(`parse response body fail ${err.message}`); } } - if(parseErr) { + 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), @@ -1071,10 +1074,10 @@ Ajax.prototype.doRequest = function(opt) { ].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; @@ -1082,11 +1085,11 @@ Ajax.prototype.doRequest = function(opt) { } - if(obj) { + if (obj) { code = obj.code || 0; } - }else{ + } else { code = { isFail: 0, code: response.statusCode @@ -1095,31 +1098,31 @@ Ajax.prototype.doRequest = function(opt) { obj = responseText; } - if(typeof opt.formatCode === 'function') { + if (typeof opt.formatCode === 'function') { code = opt.formatCode(obj, opt, response); } - //支持{isFail:0,code:1,message:''} - if(typeof code === 'object') { + // 支持{isFail:0,code:1,message:''} + 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); - }else{ + } else { - if(typeof code !== 'number') { + if (typeof code !== 'number') { code = ~~code; } - if(code === 0) { + if (code === 0) { report(opt, 0, code); - }else{ + } else { report(opt, 2, code); } } @@ -1140,16 +1143,16 @@ Ajax.prototype.doRequest = function(opt) { }); - if(opt.send) { + if (opt.send) { opt.send(request); - }else{ + } else { - if(opt.body) { + if (opt.body) { request.useChunkedEncodingByDefault = false; - try{ + 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 65c9b8b3..8cb414d0 100644 --- a/bin/tsw/ajax/form.js +++ b/bin/tsw/ajax/form.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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'; + /** * ajax发请求时组装form-data @@ -55,9 +55,9 @@ 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{ + } else { buffer = Buffer.concat([buffer, Buffer.from(value.content || ''), Buffer.from('\r\n')]); } @@ -76,20 +76,20 @@ function getFormBuffer(opt = {}) { let v; - for(const key in opt.data) { - + for (const key in opt.data) { + v = opt.data[key]; - - if(v !== undefined && v !== null) { - - if(v.fileType) { + + if (typeof v !== 'undefined' && v !== null) { + + if (v.fileType) { buffer = Buffer.concat([buffer, fileField(opt.boundary, key, v)]); - }else{ + } else { buffer = Buffer.concat([buffer, textField(opt.boundary, key, v)]); } } } - + 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 3b6896d5..6ed589f2 100644 --- a/bin/tsw/ajax/http-https.options.js +++ b/bin/tsw/ajax/http-https.options.js @@ -1,30 +1,30 @@ -/*! +/* ! * 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 https = require('https'); const http = require('http'); const cache = {}; -///etc/pki/tls/certs/ca-bundle.crt +// /etc/pki/tls/certs/ca-bundle.crt this.getHttpsAgent = function(host) { const key = ['https', host].join('.'); - if(!cache[key]) { + if (!cache[key]) { cache[key] = new https.Agent({ - maxSockets : 65535, - maxFreeSockets : 32, - maxCachedSessions : 65535, - keepAlive : true, - keepAliveMsecs : 5000 + maxSockets: 65535, + maxFreeSockets: 32, + maxCachedSessions: 65535, + keepAlive: true, + keepAliveMsecs: 5000 }); } @@ -35,12 +35,12 @@ this.getHttpAgent = function(host) { const key = ['http', host].join('.'); - if(!cache[key]) { + if (!cache[key]) { cache[key] = new http.Agent({ - maxSockets : 65535, - maxFreeSockets : 32, - keepAlive : true, - keepAliveMsecs : 5000 + maxSockets: 65535, + maxFreeSockets: 32, + keepAlive: true, + keepAliveMsecs: 5000 }); } diff --git a/bin/tsw/ajax/index.js b/bin/tsw/ajax/index.js index dab6c5b1..59d0977c 100644 --- a/bin/tsw/ajax/index.js +++ b/bin/tsw/ajax/index.js @@ -1,10 +1,10 @@ -/*! +/* ! * 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'; + module.exports = require('./ajax.js'); diff --git a/bin/tsw/ajax/token.js b/bin/tsw/ajax/token.js index 42379165..4eca777a 100644 --- a/bin/tsw/ajax/token.js +++ b/bin/tsw/ajax/token.js @@ -1,21 +1,21 @@ -/*! +/* ! * 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'; + this.token = function(skey) { - - + + let str = skey || ''; let hash = 5381; - if(typeof context !== 'undefined') { + if (typeof context !== 'undefined') { const 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(let i = 0, len = str.length; i < len; ++i) { + for (let 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 d8476149..38f92a98 100644 --- a/bin/tsw/api/date.js +++ b/bin/tsw/api/date.js @@ -1,14 +1,14 @@ -/*! +/* ! * 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 Date = { - + /** * 格式化时间 * @method formatData @@ -16,16 +16,17 @@ const Date = { * @param {String} fmt 格式化形式,如 MM月dd日 HH:mm */ format: function(mDate, fmt) { - let o = { - 'M+': mDate.getMonth() + 1, //月份 - 'D+': mDate.getDate(), //日 - 'h+': mDate.getHours() % 12 == 0 ? 12 : mDate.getHours() % 12, //小时 - 'H+': mDate.getHours(), //小时 - 'm+': mDate.getMinutes(), //分 - 's+': mDate.getSeconds(), //秒 - 'q+': Math.floor((mDate.getMonth() + 3) / 3), //季度 - 'S': mDate.getMilliseconds() //毫秒 - }, week = { + const o = { + 'M+': mDate.getMonth() + 1, // 月份 + 'D+': mDate.getDate(), // 日 + 'h+': mDate.getHours() % 12 == 0 ? 12 : mDate.getHours() % 12, // 小时 + 'H+': mDate.getHours(), // 小时 + 'm+': mDate.getMinutes(), // 分 + 's+': mDate.getSeconds(), // 秒 + 'q+': Math.floor((mDate.getMonth() + 3) / 3), // 季度 + 'S': mDate.getMilliseconds() // 毫秒 + }, + week = { '0': '\u65e5', '1': '\u4e00', '2': '\u4e8c', @@ -34,20 +35,20 @@ const Date = { '5': '\u4e94', '6': '\u516d' }; - + if (/(Y+)/.test(fmt)) { - fmt = fmt.replace(RegExp.$1, (mDate.getFullYear() + '').substr(4 - RegExp.$1.length)); + fmt = fmt.replace(RegExp.$1, (String(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() + '']); + fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[String(mDate.getDay())]); } - + for (const 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))); + fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr((String(o[k])).length))); } } - + return fmt; } }; diff --git a/bin/tsw/api/fileCache/index.js b/bin/tsw/api/fileCache/index.js index 1322b304..2e0289df 100644 --- a/bin/tsw/api/fileCache/index.js +++ b/bin/tsw/api/fileCache/index.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 Deferred = require('util/Deferred'); const logger = require('logger'); @@ -17,34 +17,33 @@ const existsCache = {}; this.getDir = function(filepath) { - - let 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) { + if (filepath.indexOf('http://') > -1) { filepath = filepath.replace('http://', ''); } - - tmp = path.join('/', filepath); - res = path.join(root, tmp); - + + const tmp = path.join('/', filepath); + + let res = path.join(root, tmp); + res = res.replace(/\\/g, '/'); - + return res; }; this.mkdir = function(dirname) { - if(existsCache[dirname]) { + if (existsCache[dirname]) { return; } - if(fs.existsSync(dirname)) { + if (fs.existsSync(dirname)) { existsCache[dirname] = true; return; } @@ -52,16 +51,16 @@ this.mkdir = function(dirname) { const arr = dirname.split('/'); let i = 0; let curr; - - for(i = 1; i < arr.length; i++) { + + for (i = 1; i < arr.length; i++) { curr = arr.slice(0, i + 1).join('/'); - if(!fs.existsSync(curr)) { + if (!fs.existsSync(curr)) { logger.debug('mkdir : ${dir}', { dir: curr }); - try{ + try { fs.mkdirSync(curr, 0o777); - }catch(e) { + } catch (e) { logger.info(e.stack); } @@ -76,63 +75,63 @@ this.set = function(filepath, data) { const dirname = path.dirname(filename); const basename = path.basename(filename); const 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') { + + if (typeof data === 'string') { buffer = Buffer.from(data, 'UTF-8'); - }else{ + } 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}', { randomname: randomname, size: buffer.length }); - if(err) { + if (err) { logger.info(err.stack); return; } - + fs.rename([dirname, randomname].join('/'), [dirname, basename].join('/'), function(err) { const end = Date.now(); - - if(err) { + + 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) { - + const start = Date.now(); let buffer = null; const filename = this.getDir(filepath); @@ -140,74 +139,74 @@ this.getSync = function(filepath) { stats: null, data: null }; - - try{ + + try { buffer = fs.readFileSync(filename); - if(buffer.length === 0) { - //长度为0返回空,因为垃圾清理逻辑会清空文件 + if (buffer.length === 0) { + // 长度为0返回空,因为垃圾清理逻辑会清空文件 buffer = null; } res.stats = fs.statSync(filename); res.data = buffer; - }catch(err) { + } catch (err) { buffer = null; } - + const 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) { - + const defer = Deferred.create(); const filename = this.getDir(filepath); - - + + const res = { stats: null, data: null }; - + logger.debug('[getAsync] ${filename}', { filename: filename }); - + fs.readFile(filename, function(err, buffer) { - - if(err) { + + if (err) { logger.debug(err.stack); defer.resolve(res); return; } - + res.data = buffer; - + 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); - + res.stats = stats; defer.resolve(res); }); - + }); - + return defer; }; @@ -215,14 +214,14 @@ this.get = this.getAsync = function(filepath) { this.updateMtime = function(filepath, atime, mtime) { const filename = this.getDir(filepath); - - + + logger.debug('[updateMtime] ${filename}', { filename: filename }); - + fs.utimes(filename, atime || new Date(), mtime || new Date(), function(err) { - if(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 c594467c..d728a00a 100644 --- a/bin/tsw/api/logman/index.js +++ b/bin/tsw/api/logman/index.js @@ -1,29 +1,29 @@ -/*! +/* ! * 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 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 { 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目录是否存在 +// 判断logDir目录是否存在 fs.exists(logDir, function(exists) { if (!exists) { fs.mkdirSync(logDir, 0o777); } - //判断backup目录是否存在 + // 判断backup目录是否存在 fs.exists(backupDir, function(exists) { if (!exists) { fs.mkdirSync(backupDir, 0o777); @@ -32,7 +32,7 @@ fs.exists(logDir, function(exists) { }); const LogMan = { - + /** * 按分钟\小时\天去备份log */ @@ -41,7 +41,7 @@ const LogMan = { H: 3600000, D: 86400000 }, - + /** * 启动log管理 */ @@ -54,38 +54,38 @@ const LogMan = { self.backLog(); }, this.delay); }, - + /** * 备份log */ backLog: function() { logger.info('start backup log'); const self = this; - const curBackupDir = path.resolve(backupDir, './' + dateApi.format(new Date, 'YYYY-MM-DD')); + const curBackupDir = path.resolve(backupDir, './' + dateApi.format(new Date(), 'YYYY-MM-DD')); fs.exists(curBackupDir, function(exists) { - if(!exists) { + if (!exists) { fs.mkdirSync(curBackupDir); } - let logFilePath = path.resolve(curBackupDir, './' + dateApi.format(new Date, self.delayType + self.delayType) + '.log'); + let logFilePath = path.resolve(curBackupDir, './' + dateApi.format(new Date(), self.delayType + self.delayType) + '.log'); let cmdCat = 'cat ' + runlogPath + ' >> ' + logFilePath; let cmdClear = 'cat /dev/null > ' + runlogPath; - - //兼容windows - if(isWindows) { + + // 兼容windows + if (isWindows) { logFilePath = logFilePath.replace(/\\/g, '\\\\'); cmdCat = 'type ' + runlogPath + ' > ' + logFilePath; cmdClear = 'type NUL > ' + runlogPath; } - - //backup - logger.info('backup: '+ cmdCat); - + + // backup + logger.info('backup: ' + cmdCat); + cp.exec(cmdCat, function(error, stdout, stderr) { if (error !== null) { logger.error('cat error, ' + error); } - - //clear + + // clear logger.info('clear: ' + cmdClear); cp.exec(cmdClear, function(error, stdout, stderr) { @@ -96,7 +96,7 @@ const LogMan = { }); }); } - + }; module.exports = LogMan; diff --git a/bin/tsw/config.js b/bin/tsw/config.js index 0b8d61ee..7808d56a 100644 --- a/bin/tsw/config.js +++ b/bin/tsw/config.js @@ -1,22 +1,22 @@ -/*! +/* ! * 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'; + 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; diff --git a/bin/tsw/context.js b/bin/tsw/context.js index cb804863..f9a8cf01 100644 --- a/bin/tsw/context.js +++ b/bin/tsw/context.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 Context = require('runtime/Context'); const Window = require('runtime/Window'); @@ -14,24 +14,24 @@ 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; } const 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 e752fcf9..82935f30 100644 --- a/bin/tsw/default/config.default.sky.js +++ b/bin/tsw/default/config.default.sky.js @@ -1,46 +1,46 @@ -/*! +/* ! * 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'; -//路由 + +// 路由 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) { - const pathname= req.REQUEST.pathname || ''; + const pathname = req.REQUEST.pathname || ''; const arr = pathname.split('/', 3); const mod = arr[1] || 'default'; const act = arr[2] || 'page'; const 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; } @@ -52,11 +52,11 @@ this.modAct = this.modMap = { const 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/helper.js b/bin/tsw/loader/seajs/lib/helper.js index d3e78cac..71dbf191 100644 --- a/bin/tsw/loader/seajs/lib/helper.js +++ b/bin/tsw/loader/seajs/lib/helper.js @@ -51,10 +51,12 @@ exports.readFile = function(uri, callback) { connect.get(options, function(res) { if (res.statusCode !== 200) { - throw 'Error: No data received from ' + uri; + throw 'Error: No data received from ' + uri; // eslint-disable-line no-throw-literal } - let ret = [], length = 0; + const ret = []; + + let length = 0; res.on('data', function(chunk) { length += chunk.length; @@ -62,7 +64,9 @@ exports.readFile = function(uri, callback) { }); callback && res.on('end', function() { - let buf = Buffer.alloc(length), index = 0; + const buf = Buffer.alloc(length); + + let index = 0; ret.forEach(function(chunk) { chunk.copy(buf, index, 0, chunk.length); diff --git a/bin/tsw/loader/seajs/lib/sea-node.js b/bin/tsw/loader/seajs/lib/sea-node.js index 3b514afd..c18122a4 100644 --- a/bin/tsw/loader/seajs/lib/sea-node.js +++ b/bin/tsw/loader/seajs/lib/sea-node.js @@ -13,19 +13,18 @@ const _resolveFilename = Module._resolveFilename; const moduleStack = []; Module._resolveFilename = function(request, parent) { - let res; - //request = request.replace(/\?.*$/, '') // remove timestamp etc. + // 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{ + } else { parent.resolveFilenameCache = {}; } - res = _resolveFilename(request, parent); + const res = _resolveFilename(request, parent); parent.resolveFilenameCache[request] = res; @@ -35,16 +34,16 @@ Module._resolveFilename = function(request, parent) { 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) { + } catch (err) { process.nextTick(function() { process.emit('warning', err); }); throw err; - }finally { + } finally { moduleStack.pop(); } }; diff --git a/bin/tsw/logger.js b/bin/tsw/logger.js index 7f15cd5a..1643153d 100644 --- a/bin/tsw/logger.js +++ b/bin/tsw/logger.js @@ -1,10 +1,10 @@ -/*! +/* ! * 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'; + module.exports = require('util/logger')(); diff --git a/bin/tsw/plug.js b/bin/tsw/plug.js index 34f8ac63..d6fb1fdf 100644 --- a/bin/tsw/plug.js +++ b/bin/tsw/plug.js @@ -1,27 +1,27 @@ -/*! +/* ! * 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 path = require('path'); /** - * + * * 获取内置模块 - * + * * @param {String} id */ function plug(id) { return require(id); } -if(!global.plug) { - - +if (!global.plug) { + + plug.__dirname = __dirname; plug.parent = path.join(__dirname, '..'); plug.paths = [ @@ -34,22 +34,22 @@ if(!global.plug) { module.paths = plug.paths.concat(module.paths); global.plug = plug; - - //支持seajs模块 + + // 支持seajs模块 require('loader/seajs'); require('loader/extentions.js'); - JSON.stringify = function(stringify) { + JSON.stringify = (function(stringify) { return function() { let str = stringify.apply(this, arguments); - - if(str && str.indexOf('<') > -1) { + + if (str && str.indexOf('<') > -1) { str = str.replace(/ 0) { + const index = command.indexOf('\r\n'); // 不要数据部分 + if (index > 0) { command = command.slice(0, Math.min(128, index)); } - if(command.length >= 128) { + if (command.length >= 128) { command = command.slice(0, 128) + '...' + command.length; } logger.debug(command); - query.callback = function(callback) { + query.callback = (function(callback) { return function(...args) { const err = args[0]; let code = 0; @@ -107,39 +106,39 @@ function queueWrap(memcached) { const delay = Date.now() - start; const 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); code = 2; isFail = 1; - }else{ + } else { logger.debug(err); } } 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(); return callback && callback.apply(this, args); }; - }(query.callback); + })(query.callback); return query; }; })(queryCompiler); - + command.call(memcached, fn, server); }); }; - }(memcached.command); - - + })(memcached.command); + + return memcached; } diff --git a/bin/tsw/router.js b/bin/tsw/router.js index 39858617..c4d2bbd2 100644 --- a/bin/tsw/router.js +++ b/bin/tsw/router.js @@ -1,51 +1,50 @@ -/*! +/* ! * 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 httpRoute = require('../proxy/http.route.js'); const parseGet = require('util/http/parseGet.js'); this.route = function(url, mod_act) { - - let req, res; + const window = context.window || {}; - - req = window.request; - res = window.response; - - if(!req) { + + const req = window.request; + const res = window.response; + + if (!req) { return; } - + res.removeAllListeners('afterFinish'); - - if(url) { - + + if (url) { + logger.debug('route to : ${url}', { url: url }); - + req.url = url; - //解析get参数 + // 解析get参数 parseGet(req); } - - if(mod_act) { + + if (mod_act) { logger.debug('route to mod_act: ${mod_act}', { mod_act: mod_act }); - + context.mod_act = mod_act; - }else{ + } else { context.mod_act = null; } - + httpRoute.doRoute(req, res); }; diff --git a/bin/tsw/runtime/CCFinder.js b/bin/tsw/runtime/CCFinder.js index 0f8ef3ae..7b488132 100644 --- a/bin/tsw/runtime/CCFinder.js +++ b/bin/tsw/runtime/CCFinder.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 config = require('config'); const logger = require('logger'); @@ -14,8 +14,8 @@ 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; //限制 +const CCIPSize = 1000; // 统计周期 +const CCIPLimit = config.CCIPLimit; // 限制 let ipConut = CCIPSize; let cache = { ipCache: {}, @@ -23,9 +23,9 @@ let cache = { ipCacheLast: {} }; -if(global[__filename]) { +if (global[__filename]) { cache = global[__filename]; -}else{ +} else { global[__filename] = cache; } @@ -37,25 +37,27 @@ this.checkHost = function(req, res) { const hostAllow = config.allowHost || []; const host = req.headers['host']; - let i, len, v; + let 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; } } @@ -70,7 +72,7 @@ this.checkHost = function(req, res) { return false; }; -//计算标准方差 +// 计算标准方差 this.StdX10 = function(ipCache) { let res = 0; @@ -78,14 +80,14 @@ this.StdX10 = function(ipCache) { let avg = 0; const arr = Object.keys(ipCache).filter(function(item) { const 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; } @@ -96,10 +98,10 @@ this.StdX10 = function(ipCache) { const value = item.list.length; item.avg = avg; - return pre + (value - avg)*(value - avg); + return pre + (value - avg) * (value - avg); }, 0); - res = parseInt(Math.sqrt(sumXsum / (arr.length - 1)) * 10); + res = parseInt(Math.sqrt(sumXsum / (arr.length - 1)) * 10, 10); return res; }; @@ -108,96 +110,97 @@ this.check = function(req, res) { const userIp = httpUtil.getUserIp(req); const userIp24 = httpUtil.getUserIp24(req); - let key, Content, curr; + + let Content; const info = { - userIp : userIp, - hostname : req.headers.host, - pathname : req.REQUEST.pathname + userIp: userIp, + hostname: req.headers.host, + 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)) { + // 忽略TST请求 + 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); + cache.ipCache.StdX10 = this.StdX10(cache.ipCache); cache.ipCacheLast = cache.ipCache; cache.ipCache = {}; cache.whiteList = {}; - //上报标准方差 + // 上报标准方差 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, list: [] }; } - - curr = cache.ipCache[userIp]; - + + const curr = cache.ipCache[userIp]; + 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; } - //tnm2.Attr_API('SUM_TSW_CC_LIMIT', 1); + // tnm2.Attr_API('SUM_TSW_CC_LIMIT', 1); - //确认没发送过邮件 + // 确认没发送过邮件 cache.ipCacheLast.hasSendMail = true; - //发现目标,发邮件 - key = `[AVG_TSW_IP_STD_X10]:${serverInfo.intranetIp}`; + // 发现目标,发邮件 + const key = `[AVG_TSW_IP_STD_X10]:${serverInfo.intranetIp}`; Content = ''; @@ -205,26 +208,26 @@ this.check = function(req, res) { let num = ''; - if( + if ( cache.ipCacheLast[ip] && cache.ipCacheLast[ip].list && cache.ipCacheLast[ip].list.length > 1 ) { - num = '' + cache.ipCacheLast[ip].list.length; + num = String(cache.ipCacheLast[ip].list.length); num = (num + 'XXXXXX').slice(0, 8).replace(/X/g, ' '); 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 + '

' + '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

' + + '

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

' + '

证据列表:

' + Content }); diff --git a/bin/tsw/runtime/Console.hack.js b/bin/tsw/runtime/Console.hack.js index 306b3722..a6668c50 100644 --- a/bin/tsw/runtime/Console.hack.js +++ b/bin/tsw/runtime/Console.hack.js @@ -1,13 +1,13 @@ -/*! +/* ! * 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'; -if(!global[__filename]) { + +if (!global[__filename]) { global[__filename] = true; @@ -17,44 +17,44 @@ if(!global[__filename]) { /* eslint-disable no-console */ - console.debug = function(log) { + console.debug = (function(log) { return function(...args) { return logger.writeLog('DBUG', `${util.format.apply(null, args)}`); }; - }(console.originDebug = console.debug); + })(console.originDebug = console.debug); - console.log = function(log) { + console.log = (function(log) { return function(...args) { return logger.writeLog('DBUG', `${util.format.apply(null, args)}`); }; - }(console.originLog = console.log); + })(console.originLog = console.log); - console.info = function(log) { + console.info = (function(log) { return function(...args) { return logger.writeLog('INFO', `${util.format.apply(null, args)}`); }; - }(console.originInfo = console.info); + })(console.originInfo = console.info); - console.dir = function(log) { + console.dir = (function(log) { return function(object, options) { options = Object.assign({ customInspect: false }, options); return logger.writeLog('INFO', `${util.inspect(object, options)}`); }; - }(console.originDir = console.dir); + })(console.originDir = console.dir); - console.warn = function(log) { + console.warn = (function(log) { return function(...args) { return logger.writeLog('WARN', `${util.format.apply(null, args)}`); }; - }(console.originWarn = console.warn); + })(console.originWarn = console.warn); - console.error = function(log) { + console.error = (function(log) { return function(...args) { return logger.writeLog('ERRO', `${util.format.apply(null, args)}`); }; - }(console.originError = console.error); + })(console.originError = console.error); /* eslint-enable no-console */ }); diff --git a/bin/tsw/runtime/Context.js b/bin/tsw/runtime/Context.js index e031f336..5a722810 100644 --- a/bin/tsw/runtime/Context.js +++ b/bin/tsw/runtime/Context.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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'; + module.exports = Context; @@ -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/ContextWrap.js b/bin/tsw/runtime/ContextWrap.js index 5810d48b..8dc994dd 100644 --- a/bin/tsw/runtime/ContextWrap.js +++ b/bin/tsw/runtime/ContextWrap.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 EventEmitter = require('events'); const net = require('net'); diff --git a/bin/tsw/runtime/Dns.hack.js b/bin/tsw/runtime/Dns.hack.js index f5fac2f5..48c06db4 100644 --- a/bin/tsw/runtime/Dns.hack.js +++ b/bin/tsw/runtime/Dns.hack.js @@ -1,14 +1,14 @@ -/*! +/* ! * 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'; -if(!global[__filename]) { - + +if (!global[__filename]) { + global[__filename] = true; process.nextTick(function() { @@ -24,7 +24,7 @@ if(!global[__filename]) { const args = [hostname]; const start = Date.now(); - if(net.isIP(hostname)) { + if (net.isIP(hostname)) { return fn.apply(this, arguments); } @@ -32,7 +32,6 @@ if(!global[__filename]) { callback = options; } - let timer; let code; let isFail; let timeoutError; @@ -41,10 +40,10 @@ if(!global[__filename]) { const callbackWrap = function(err, address, family) { if (isCalled) return; - if(!err) { + if (!err) { code = 0; isFail = 0; - } else if(err === timeoutError) { + } else if (err === timeoutError) { code = 513; isFail = 1; } else { @@ -52,18 +51,18 @@ if(!global[__filename]) { isFail = 1; } - if(err) { + if (err) { logger.error('dns lookup error: ' + err.stack); - }else{ + } else { logger.debug(`dns lookup: ${hostname} --> ${address}`); } dcapi.report({ - key : 'EVENT_TSW_DNS_LOOKUP', - toIp : '127.0.0.1', - code : code, - isFail : isFail, - delay : Date.now() - start + key: 'EVENT_TSW_DNS_LOOKUP', + toIp: '127.0.0.1', + code: code, + isFail: isFail, + delay: Date.now() - start }); isCalled = true; @@ -71,12 +70,13 @@ if(!global[__filename]) { callback(err, address, family); }; - if(callback !== options) { + if (callback !== options) { args.push(options); } + args.push(callbackWrap); - timer = setTimeout(function() { + const timer = setTimeout(function() { callbackWrap((timeoutError = new Error('Dns Lookup Timeout'))); }, config.timeout && config.timeout.dns || 3000); diff --git a/bin/tsw/runtime/JankWatcher.js b/bin/tsw/runtime/JankWatcher.js index befde909..8f8c8895 100644 --- a/bin/tsw/runtime/JankWatcher.js +++ b/bin/tsw/runtime/JankWatcher.js @@ -1,15 +1,15 @@ -/*! +/* ! * 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 tnm2 = require('api/tnm2'); -if(!global[__filename]) { +if (!global[__filename]) { global[__filename] = true; @@ -21,7 +21,7 @@ if(!global[__filename]) { tnm2.Attr_API_Set('AVG_TSW_ST0_X10', t); - //10S检查一次 + // 10S检查一次 setTimeout(() => { watch(); }, 10 * 1000); diff --git a/bin/tsw/runtime/Window.js b/bin/tsw/runtime/Window.js index 40aa2077..50a2688f 100644 --- a/bin/tsw/runtime/Window.js +++ b/bin/tsw/runtime/Window.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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'; + module.exports = Window; diff --git a/bin/tsw/runtime/capturer.js b/bin/tsw/runtime/capturer.js index bd760d29..57cd3977 100644 --- a/bin/tsw/runtime/capturer.js +++ b/bin/tsw/runtime/capturer.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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'; + /** * 抓包 @@ -15,9 +15,9 @@ const http = require('http'); const https = require('https'); let isFirstLoad = true; -if(global[__filename]) { +if (global[__filename]) { isFirstLoad = false; -}else{ +} else { global[__filename] = {}; } @@ -47,9 +47,9 @@ process.nextTick(function() { let localPort = ''; const host = (opt.headers && opt.headers.host) || opt.host; - if(context.requestCaptureSN) { + if (context.requestCaptureSN) { context.requestCaptureSN++; - }else{ + } else { context.requestCaptureSN = 1; } @@ -57,21 +57,21 @@ process.nextTick(function() { const 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 }); - //抓包 - 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() { @@ -82,62 +82,62 @@ process.nextTick(function() { const report = function(oriResponse) { const logJson = logger.getJson(); const response = oriResponse || { - headers : { - 'content-length' : 0, - 'content-type' : 'text/html' + headers: { + '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) { + if (!logJson) { logger.debug('logger.getJson() is empty!'); return; } const 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), - ClientDoneRequest : new Date(timeStart), - GatewayTime : 0, - DNSTime : 0, - TCPConnectTime : 0, - HTTPSHandshakeTime : 0, - ServerConnected : new Date(timeStart), + 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), + ClientDoneRequest: new Date(timeStart), + GatewayTime: 0, + DNSTime: 0, + TCPConnectTime: 0, + HTTPSHandshakeTime: 0, + ServerConnected: new Date(timeStart), FiddlerBeginRequest: new Date(timeStart), - ServerGotRequest : new Date(timeStart), + ServerGotRequest: new Date(timeStart), ServerBeginResponse: new Date(timeResponse), - GotResponseHeaders : new Date(timeResponse), - ServerDoneResponse : new Date(timeEnd), + GotResponseHeaders: new Date(timeResponse), + ServerDoneResponse: new Date(timeEnd), ClientBeginResponse: new Date(timeResponse), - ClientDoneResponse : new Date(timeEnd) + ClientDoneResponse: new Date(timeEnd) } }; logJson.ajax.push(curr); }; - request.once('response', (response)=>{ + request.once('response', (response) => { timeResponse = Date.now(); const socket = response.socket; @@ -150,10 +150,10 @@ process.nextTick(function() { 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 @@ -162,19 +162,19 @@ process.nextTick(function() { const 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); } }; @@ -184,15 +184,15 @@ process.nextTick(function() { // timeCurr = Date.now(); - //logger.debug('${logPre}receive data: ${size},\tcost: ${cost}ms',{ + // logger.debug('${logPre}receive data: ${size},\tcost: ${cost}ms',{ // logPre: logPre, // cost: cost, // size: chunk.length - //}); + // }); bodySize += chunk.length; - if(captureBody && bodySize <= maxBodySize) { + if (captureBody && bodySize <= maxBodySize) { result.push(chunk); } }; diff --git a/bin/tsw/runtime/fs.hack.js b/bin/tsw/runtime/fs.hack.js index e4af264a..a7cff016 100644 --- a/bin/tsw/runtime/fs.hack.js +++ b/bin/tsw/runtime/fs.hack.js @@ -1,23 +1,23 @@ -/*! +/* ! * 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 TIMES_LIMIT = 5; -if(global[__filename]) { +if (global[__filename]) { global[__filename].map = {}; -}else{ +} else { global[__filename] = { map: {} }; - //追踪重复读写 + // 追踪重复读写 process.nextTick(function() { const cache = global[__filename]; const isWindows = require('util/isWindows'); @@ -34,7 +34,7 @@ if(global[__filename]) { cache.map[key] = ~~cache.map[key] + 1; sum = cache.map[key]; - if(sum % TIMES_LIMIT === 0 && !config.devMode) { + if (sum % TIMES_LIMIT === 0 && !config.devMode) { logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}', { name: name, file: file, @@ -54,7 +54,7 @@ if(global[__filename]) { cache.map[key] = ~~cache.map[key] + 1; sum = cache.map[key]; - if(sum % TIMES_LIMIT === 0 && !config.devMode) { + if (sum % TIMES_LIMIT === 0 && !config.devMode) { logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}', { name: name, file: file, @@ -74,7 +74,7 @@ if(global[__filename]) { cache.map[key] = ~~cache.map[key] + 1; sum = cache.map[key]; - if(sum > TIMES_LIMIT && !config.devMode) { + if (sum > TIMES_LIMIT && !config.devMode) { logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}', { name: name, file: file, @@ -94,7 +94,7 @@ if(global[__filename]) { cache.map[key] = ~~cache.map[key] + 1; sum = cache.map[key]; - if(sum % TIMES_LIMIT === 0 && !config.devMode) { + if (sum % TIMES_LIMIT === 0 && !config.devMode) { logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}', { name: name, file: file, @@ -114,7 +114,7 @@ if(global[__filename]) { cache.map[key] = ~~cache.map[key] + 1; sum = cache.map[key]; - if(sum > TIMES_LIMIT && !config.devMode) { + if (sum > TIMES_LIMIT && !config.devMode) { logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}', { name: name, file: file, @@ -134,7 +134,7 @@ if(global[__filename]) { cache.map[key] = ~~cache.map[key] + 1; sum = cache.map[key]; - if(sum % TIMES_LIMIT === 0 && !config.devMode) { + if (sum % TIMES_LIMIT === 0 && !config.devMode) { logger.warn('[sync]${name} callee ${sum} times on file: ${file} \n${stack}', { name: name, file: file, @@ -146,16 +146,16 @@ if(global[__filename]) { tnm2.Attr_API('SUM_TSW_FILE_SYNC', 1); }); - //fs.realpathSync = hack(fs.realpathSync,function(file){ + // fs.realpathSync = hack(fs.realpathSync,function(file){ // logger.debug('[sync] ${name} ${file}',{ // name: 'fs.realpathSync', // file: file // }); - //}); + // }); function hack(fn, callback) { - if(isWindows.isWindows) { + if (isWindows.isWindows) { return fn; } diff --git a/bin/tsw/runtime/overloadProtection.js b/bin/tsw/runtime/overloadProtection.js index 917deac1..6adb247b 100644 --- a/bin/tsw/runtime/overloadProtection.js +++ b/bin/tsw/runtime/overloadProtection.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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'; + // 过载保护 process.nextTick(function() { diff --git a/bin/tsw/serverInfo.js b/bin/tsw/serverInfo.js index 436fd7a3..c7e5ef35 100644 --- a/bin/tsw/serverInfo.js +++ b/bin/tsw/serverInfo.js @@ -1,21 +1,21 @@ -/*! +/* ! * 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 os = require('os'); -const {isWindows} = require('util/isWindows'); +const { isWindows } = require('util/isWindows'); const isInnerIP = require('util/http.isInnerIP.js'); this.intranetIp = '127.0.0.1'; -if(isWindows) { +if (isWindows) { this.intranetIp = getWinLocalIpv4(); -}else{ +} else { this.intranetIp = getLinuxLocalIpv4(); } @@ -27,16 +27,16 @@ function getLinuxLocalIpv4() { const eth = networkInterfaces[key]; const address = eth && eth[0] && eth[0].address; - if(!address) { + if (!address) { return; } const tmp = isInnerIP.isInnerIP(address); - if(!tmp) { + if (!tmp) { return; } - if(tmp === '127.0.0.1') { + if (tmp === '127.0.0.1') { return; } @@ -49,19 +49,21 @@ function getLinuxLocalIpv4() { function getWinLocalIpv4() { const localNet = os.networkInterfaces(); - let key, item; - let v, i; + let key, + item; + let v, + i; let 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 ba39a174..5c994ffc 100644 --- a/bin/tsw/util/CD.js +++ b/bin/tsw/util/CD.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 url = require('url'); const Deferred = require('util/Deferred'); @@ -16,21 +16,21 @@ 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时间 +const cacheTime = 10 * 60 * 1000; // 最大cache时间 let cacheStart = Date.now(); let cache = {}; -if(global[__filename]) { +if (global[__filename]) { cache = global[__filename]; -}else{ +} else { global[__filename] = cache; } this.check = function(key, count, second) { - //请求openapi - if(config.appid && config.appkey && config.utilCDUrl) { + // 请求openapi + if (config.appid && config.appkey && config.utilCDUrl) { return checkByOpenapi(key, count, second); } @@ -45,8 +45,8 @@ const checkByOpenapi = function(keyOri, count, second) { const key = 'CD3.' + crypto.createHash('sha1').update(`CD3.${appid}.${keyOri}.${count}.${second}`).digest('hex'); const start = Date.now(); - if(cache[key]) { - if(start - cache[key] < second * 1000) { + if (cache[key]) { + if (start - cache[key] < second * 1000) { logger.debug('miss mem ${key}', { key: key }); @@ -55,15 +55,15 @@ const checkByOpenapi = function(keyOri, count, second) { } } - //清缓存逻辑 - if(start - cacheStart > cacheTime) { + // 清缓存逻辑 + if (start - cacheStart > cacheTime) { cache = {}; cacheStart = Date.now(); } defer.fail(function(value) { - //设置缓存 + // 设置缓存 cache[key] = start; }); @@ -75,7 +75,7 @@ const checkByOpenapi = function(keyOri, count, second) { now: Date.now() }; - if(!config.utilCDUrl) { + if (!config.utilCDUrl) { return defer.reject(); } @@ -89,26 +89,26 @@ const checkByOpenapi = function(keyOri, count, second) { 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(); }).done(function(d) { let 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); } @@ -129,13 +129,13 @@ this.curr = function(keyOri, count, second) { const memcached = module.exports.cmem(); - if(!memcached) { + if (!memcached) { return defer.reject(); } memcached.get(key, function(err, result) { - if(err) { + if (err) { return defer.reject(); } @@ -153,8 +153,8 @@ const checkByCmem = function(keyOri, count, second) { const key = 'CD3.' + crypto.createHash('sha1').update(`CD3.${appid}.${keyOri}.${count}.${second}`).digest('hex'); const start = Date.now(); - if(cache[key]) { - if(start - cache[key] < second * 1000) { + if (cache[key]) { + if (start - cache[key] < second * 1000) { logger.debug('miss mem ${key}', { key: key }); @@ -163,20 +163,20 @@ const checkByCmem = function(keyOri, count, second) { } } - //清缓存逻辑 - if(start - cacheStart > cacheTime) { + // 清缓存逻辑 + if (start - cacheStart > cacheTime) { cache = {}; cacheStart = Date.now(); } const memcached = module.exports.cmem(); - if(!memcached) { + if (!memcached) { return defer.reject(); } defer.fail(function(value) { - //设置缓存 + // 设置缓存 cache[key] = start; }); @@ -186,32 +186,32 @@ const checkByCmem = function(keyOri, count, second) { memcached.add(key, 1, second, function(err, result) { - if(err) { - //add失败是正常的 + 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) { - 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{ + } else { return defer.reject(result); } }); @@ -224,61 +224,61 @@ const checkByCmem = function(keyOri, count, second) { this.checkByCmem = checkByCmem; this.cmem = function() { - if(context.appid && context.appkey) { + if (context.appid && context.appkey) { return cmemTSW.openapi(); } return cmemTSW.sz(); }; -//开放接口 +// 开放接口 this.openapi = async function(req, res) { const appid = context.appid; const 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; } - logger.setKey(`CD_${appid}`); //上报key + logger.setKey(`CD_${appid}`); // 上报key const key = req.param('key'); - const second = ~~req.param('second'); //单位秒 - const count = ~~req.param('count'); //默认是1 + const second = ~~req.param('second'); // 单位秒 + const 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; } @@ -286,7 +286,7 @@ this.openapi = async function(req, res) { return null; }); - const result = {code: 0, data: data}; + const result = { code: 0, data: data }; returnJson(result); }; diff --git a/bin/tsw/util/Deferred/index.js b/bin/tsw/util/Deferred/index.js index f20ebe63..b6c8213e 100644 --- a/bin/tsw/util/Deferred/index.js +++ b/bin/tsw/util/Deferred/index.js @@ -1,4 +1,4 @@ -'use strict'; + /** * @@ -9,20 +9,25 @@ // String to Object flags format cache -let flagsCache = {}, - class2type = {}, - // Save a reference to some core methods - toString = Object.prototype.toString, - hasOwn = Object.prototype.hasOwnProperty; +const flagsCache = {}; +const class2type = {}; +// Save a reference to some core methods +const toString = Object.prototype.toString; +const hasOwn = Object.prototype.hasOwnProperty; // Convert String-formatted flags into Object-formatted ones and store in cache -function createFlags( flags ) { - let object = flagsCache[ flags ] = {}, - i, length; - flags = flags.split( /\s+/ ); - for ( i = 0, length = flags.length; i < length; i++ ) { - object[ flags[i] ] = true; +function createFlags(flags) { + const object = flagsCache[flags] = {}; + + let i, + length; + + flags = flags.split(/\s+/); + + for (i = 0, length = flags.length; i < length; i++) { + object[flags[i]] = true; } + return object; } @@ -31,45 +36,45 @@ const 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). - isFunction: function( obj ) { + isFunction: function(obj) { return jQuery.type(obj) === 'function'; }, - isArray: Array.isArray || function( obj ) { + isArray: Array.isArray || function(obj) { return jQuery.type(obj) === 'array'; }, // A crude way of determining if an object is a window - isWindow: function( obj ) { + isWindow: function(obj) { return obj && typeof obj === 'object' && 'setInterval' in obj; }, - isNumeric: function( obj ) { - return !isNaN( parseFloat(obj) ) && isFinite( obj ); + isNumeric: function(obj) { + return !isNaN(parseFloat(obj)) && isFinite(obj); }, - type: function( obj ) { + type: function(obj) { return obj == null ? - String( obj ) : - class2type[ toString.call(obj) ] || 'object'; + String(obj) : + class2type[toString.call(obj)] || 'object'; }, - isPlainObject: function( obj ) { + isPlainObject: function(obj) { // Must be an Object. // Because of IE, we also have to check the presence of the constructor property. // Make sure that DOM nodes and window objects don't pass through, as well - if ( !obj || jQuery.type(obj) !== 'object' || obj.nodeType || jQuery.isWindow( obj ) ) { + if (!obj || jQuery.type(obj) !== 'object' || obj.nodeType || jQuery.isWindow(obj)) { return false; } try { // Not own constructor property must be Object - if ( obj.constructor && + if (obj.constructor && !hasOwn.call(obj, 'constructor') && - !hasOwn.call(obj.constructor.prototype, 'isPrototypeOf') ) { + !hasOwn.call(obj.constructor.prototype, 'isPrototypeOf')) { return false; } - } catch ( e ) { + } catch (e) { // IE8,9 Will throw exceptions on certain host objects #9897 return false; } @@ -77,42 +82,45 @@ const jQuery = { // Own properties are enumerated firstly, so to speed up, // if last one is own, then all properties are own. - let key, a; + let key, + a; - for (a in obj ) { + for (a in obj) { key = a; } - return key === undefined || hasOwn.call( obj, key ); + return key === undefined || hasOwn.call(obj, key); }, - isEmptyObject: function( obj ) { - for ( const name in obj ) { + isEmptyObject: function(obj) { + for (const name in obj) { return false; } return true; }, - error: function( msg ) { - throw new Error( msg ); + error: function(msg) { + throw new Error(msg); }, // args is for internal usage only - each: function( object, callback, args ) { - let name, i = 0, - length = object.length, - isObj = length === undefined || jQuery.isFunction( object ); - - if ( args ) { - if ( isObj ) { - for ( name in object ) { - if ( callback.apply( object[ name ], args ) === false ) { + each: function(object, callback, args) { + let name, + i = 0; + + const length = object.length; + const isObj = length === undefined || jQuery.isFunction(object); + + if (args) { + if (isObj) { + for (name in object) { + if (callback.apply(object[name], args) === false) { break; } } } else { - for ( ; i < length; ) { - if ( callback.apply( object[ i++ ], args ) === false ) { + for (; i < length;) { + if (callback.apply(object[i++], args) === false) { break; } } @@ -120,15 +128,15 @@ const jQuery = { // A special, fast, case for the most common use of each } else { - if ( isObj ) { - for ( name in object ) { - if ( callback.call( object[ name ], name, object[ name ] ) === false ) { + if (isObj) { + for (name in object) { + if (callback.call(object[name], name, object[name]) === false) { break; } } } else { - for ( ; i < length; ) { - if ( callback.call( object[ i ], i, object[ i++ ] ) === false ) { + for (; i < length;) { + if (callback.call(object[i], i, object[i++]) === false) { break; } } @@ -142,19 +150,25 @@ const jQuery = { // Populate the class2type map jQuery.each('Boolean Number String Function Array Date RegExp Object'.split(' '), function(i, name) { - class2type[ '[object ' + name + ']' ] = name.toLowerCase(); + class2type['[object ' + name + ']'] = name.toLowerCase(); }); jQuery.extend = function() { - let options, name, src, copy, copyIsArray, clone, + let options, + name, + src, + copy, + copyIsArray, + clone, target = arguments[0] || {}, i = 1, - length = arguments.length, deep = false; + const length = arguments.length; + // Handle a deep copy situation - if ( typeof target === 'boolean' ) { + if (typeof target === 'boolean') { deep = target; target = arguments[1] || {}; // skip the boolean and the target @@ -162,32 +176,32 @@ jQuery.extend = function() { } // Handle case when target is a string or something (possible in deep copy) - if ( typeof target !== 'object' && !jQuery.isFunction(target) ) { + if (typeof target !== 'object' && !jQuery.isFunction(target)) { target = {}; } // extend jQuery itself if only one argument is passed - if ( length === i ) { + if (length === i) { target = this; --i; } - for ( ; i < length; i++ ) { + for (; i < length; i++) { // Only deal with non-null/undefined values - if ( (options = arguments[ i ]) != null ) { + if ((options = arguments[i]) != null) { // Extend the base object - for ( name in options ) { - src = target[ name ]; - copy = options[ name ]; + for (name in options) { + src = target[name]; + copy = options[name]; // Prevent never-ending loop - if ( target === copy ) { + if (target === copy) { continue; } // Recurse if we're merging plain objects or arrays - if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) { - if ( copyIsArray ) { + if (deep && copy && (jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)))) { + if (copyIsArray) { copyIsArray = false; clone = src && jQuery.isArray(src) ? src : []; @@ -196,11 +210,11 @@ jQuery.extend = function() { } // Never move original objects, clone them - target[ name ] = jQuery.extend( deep, clone, copy ); + target[name] = jQuery.extend(deep, clone, copy); // Don't bring in undefined values - } else if ( copy !== undefined ) { - target[ name ] = copy; + } else if (copy !== undefined) { + target[name] = copy; } } } @@ -232,11 +246,11 @@ jQuery.extend = function() { * stopOnFalse: interrupt callings when a callback returns false * */ -jQuery.Callbacks = function( flags ) { +jQuery.Callbacks = function(flags) { // Convert flags from String-formatted to Object-formatted // (we check in cache first) - flags = flags ? ( flagsCache[ flags ] || createFlags( flags ) ) : {}; + flags = flags ? (flagsCache[flags] || createFlags(flags)) : {}; let // Actual callback list list = [], @@ -251,189 +265,198 @@ jQuery.Callbacks = function( flags ) { // End of the loop when firing firingLength, // Index of currently firing callback (modified by remove if needed) - firingIndex, + firingIndex; + // Add one or several callbacks to the list - add = function( args ) { - let i, - length, - elem, - type; - - for ( i = 0, length = args.length; i < length; i++ ) { - elem = args[ i ]; - type = jQuery.type( elem ); - if ( type === 'array' ) { - // Inspect recursively - add( elem ); - } else if ( type === 'function' ) { - // Add if not in unique mode and callback is not in - if ( !flags.unique || !self.has( elem ) ) { - elem.__domain = process.domain; - list.push( elem ); - } + const add = function(args) { + let i, + length, + elem, + type; + + for (i = 0, length = args.length; i < length; i++) { + elem = args[i]; + type = jQuery.type(elem); + if (type === 'array') { + // Inspect recursively + add(elem); + } else if (type === 'function') { + // Add if not in unique mode and callback is not in + if (!flags.unique || !self.has(elem)) { + elem.__domain = process.domain; + list.push(elem); } } - }, + } + }; + // Fire callbacks - fire = function( context, args ) { - - let fn, domain, ret; - - args = args || []; - memory = !flags.memory || [context, args]; - firing = true; - // firingIndex = firingStart || 0; - // firingStart = 0; - firingLength = list.length; - for (firingIndex = firingStart || 0, firingStart = 0; 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() { - ret = fn.apply(context, args); - }); - }else { + const fire = function(context, args) { + + let fn, + domain, + ret; + + args = args || []; + memory = !flags.memory || [context, args]; + firing = true; + // firingIndex = firingStart || 0; + // firingStart = 0; + firingLength = list && list.length || 0; + for (firingIndex = firingStart || 0, firingStart = 0; firingIndex < firingLength; firingIndex++) { + + fn = list[firingIndex]; + domain = fn.__domain; + fn.__domain = undefined; + + // restore domain if needed + if (domain && domain !== process.domain) { + domain.run(function() { // eslint-disable-line no-loop-func ret = fn.apply(context, args); - } + }); + } else { + ret = fn.apply(context, args); + } - if (ret === false && flags.stopOnFalse) { - memory = true; // Mark as halted - break; + if (ret === false && flags.stopOnFalse) { + memory = true; // Mark as halted + break; + } + } + firing = false; + if (list) { + if (!flags.once) { + if (stack && stack.length) { + memory = stack.shift(); + self.fireWith(memory[0], memory[1]); } + } else + if (memory === true) { + self.disable(); + } else { + list = []; } - firing = false; + } + }; + + // Actual Callbacks object + const self = { + // Add a callback or a collection of callbacks to the list + add: function() { if (list) { - if (!flags.once) { - if (stack && stack.length) { - memory = stack.shift(); - self.fireWith(memory[0], memory[1]); - } - } else - if (memory === true) { - self.disable(); - } else { - list = []; + const length = list.length; + add(arguments); + // Do we need to add the callbacks to the + // current firing batch? + if (firing) { + firingLength = list.length; + // With memory, if we're not firing then + // we should call right away, unless previous + // firing was halted (stopOnFalse) + } else if (memory && memory !== true) { + firingStart = length; + fire(memory[0], memory[1]); } } + return this; }, - // Actual Callbacks object - self = { - // Add a callback or a collection of callbacks to the list - add: function() { - if ( list ) { - const length = list.length; - add( arguments ); - // Do we need to add the callbacks to the - // current firing batch? - if ( firing ) { - firingLength = list.length; - // With memory, if we're not firing then - // we should call right away, unless previous - // firing was halted (stopOnFalse) - } else if ( memory && memory !== true ) { - firingStart = length; - fire( memory[ 0 ], memory[ 1 ] ); - } - } - return this; - }, - // Remove a callback from the list - remove: function() { - if ( list ) { - let args = arguments, - argIndex = 0, - argLength = args.length; - for ( ; argIndex < argLength ; argIndex++ ) { - for ( let i = 0; i < list.length; i++ ) { - if ( args[ argIndex ] === list[ i ] ) { - // Handle firingIndex and firingLength - if ( firing ) { - if ( i <= firingLength ) { - firingLength--; - if ( i <= firingIndex ) { - firingIndex--; - } + // Remove a callback from the list + remove: function() { + if (list) { + const args = arguments; + + const argLength = args.length; + + let argIndex = 0; + + for (; argIndex < argLength; argIndex++) { + for (let i = 0; i < list.length; i++) { + if (args[argIndex] === list[i]) { + // Handle firingIndex and firingLength + if (firing) { + if (i <= firingLength) { + firingLength--; + if (i <= firingIndex) { + firingIndex--; } } - // Remove the element - list.splice( i--, 1 ); - // If we have some unicity property then - // we only need to do this once - if ( flags.unique ) { - break; - } + } + // Remove the element + list.splice(i--, 1); + // If we have some unicity property then + // we only need to do this once + if (flags.unique) { + break; } } } } - return this; - }, - // Control if a given callback is in the list - has: function( fn ) { - if ( list ) { - let i = 0, - length = list.length; - for ( ; i < length; i++ ) { - if ( fn === list[ i ] ) { - return true; - } + } + return this; + }, + // Control if a given callback is in the list + has: function(fn) { + if (list) { + const length = list.length; + + let i = 0; + for (; i < length; i++) { + if (fn === list[i]) { + return true; } } - return false; - }, - // Remove all callbacks from the list - empty: function() { - list = []; - return this; - }, - // Have the list do nothing anymore - disable: function() { - list = stack = memory = undefined; - return this; - }, - // Is it disabled? - disabled: function() { - return !list; - }, - // Lock the list in its current state - lock: function() { - stack = undefined; - if ( !memory || memory === true ) { - self.disable(); - } - return this; - }, - // Is it locked? - locked: function() { - return !stack; - }, - // Call all callbacks with the given context and arguments - fireWith: function( context, args ) { - if ( stack ) { - if ( firing ) { - if ( !flags.once ) { - stack.push( [ context, args ] ); - } - } else if ( !( flags.once && memory ) ) { - fire( context, args ); + } + return false; + }, + // Remove all callbacks from the list + empty: function() { + list = []; + return this; + }, + // Have the list do nothing anymore + disable: function() { + list = stack = memory = undefined; + return this; + }, + // Is it disabled? + disabled: function() { + return !list; + }, + // Lock the list in its current state + lock: function() { + stack = undefined; + if (!memory || memory === true) { + self.disable(); + } + return this; + }, + // Is it locked? + locked: function() { + return !stack; + }, + // Call all callbacks with the given context and arguments + fireWith: function(context, args) { + if (stack) { + if (firing) { + if (!flags.once) { + stack.push([context, args]); } + } else if (!(flags.once && memory)) { + fire(context, args); } - return this; - }, - // Call all the callbacks with the given arguments - fire: function() { - self.fireWith( this, arguments ); - return this; - }, - // To know if the callbacks have already been called at least once - fired: function() { - return !!memory; } - }; + return this; + }, + // Call all the callbacks with the given arguments + fire: function() { + self.fireWith(this, arguments); + return this; + }, + // To know if the callbacks have already been called at least once + fired: function() { + return !!memory; + } + }; return self; }; @@ -444,108 +467,114 @@ const // Static reference to slice jQuery.extend({ - Deferred: function( func ) { - let doneList = jQuery.Callbacks( 'once memory' ), - failList = jQuery.Callbacks( 'once memory' ), - progressList = jQuery.Callbacks( 'memory' ), - state = 'pending', - lists = { - resolve: doneList, - reject: failList, - notify: progressList + Deferred: function(func) { + const doneList = jQuery.Callbacks('once memory'); + const failList = jQuery.Callbacks('once memory'); + const progressList = jQuery.Callbacks('memory'); + + let state = 'pending'; + + const lists = { + resolve: doneList, + reject: failList, + notify: progressList + }; + + const promise = { + done: doneList.add, + fail: failList.add, + progress: progressList.add, + + state: function() { + return state; + }, + + // Deprecated + isResolved: doneList.fired, + isRejected: failList.fired, + + then: function(doneCallbacks, failCallbacks, progressCallbacks) { + deferred.done(doneCallbacks).fail(failCallbacks).progress(progressCallbacks); + return this; + }, + always: function() { + deferred.done.apply(deferred, arguments).fail.apply(deferred, arguments); + return this; }, - promise = { - done: doneList.add, - fail: failList.add, - progress: progressList.add, - - state: function() { - return state; - }, - - // Deprecated - isResolved: doneList.fired, - isRejected: failList.fired, - - then: function( doneCallbacks, failCallbacks, progressCallbacks ) { - deferred.done( doneCallbacks ).fail( failCallbacks ).progress( progressCallbacks ); - return this; - }, - always: function() { - deferred.done.apply( deferred, arguments ).fail.apply( deferred, arguments ); - return this; - }, - - toES6Promise: function() { - const self = this; - - return new Promise(function (resolve, reject) { - - self.done(function(d) { - resolve(d); - }); - - self.fail(function(d) { - reject(d); - }); + + toES6Promise: function() { + const self = this; + + return new Promise(function (resolve, reject) { + + self.done(function(d) { + resolve(d); }); - }, - pipe: function( fnDone, fnFail, fnProgress ) { - return jQuery.Deferred(function( newDefer ) { - jQuery.each( { - done: [ fnDone, 'resolve' ], - fail: [ fnFail, 'reject' ], - progress: [ fnProgress, 'notify' ] - }, function( handler, data ) { - let fn = data[ 0 ], - action = data[ 1 ], - returned; - if ( jQuery.isFunction( fn ) ) { - deferred[ handler ](function() { - returned = fn.apply( this, arguments ); - if ( returned && jQuery.isFunction( returned.promise ) ) { - returned.promise().then( newDefer.resolve, newDefer.reject, newDefer.notify ); - } else { - newDefer[ action + 'With' ]( this === deferred ? newDefer : this, [ returned ] ); - } - }); - } else { - deferred[ handler ]( newDefer[ action ] ); - } - }); - }).promise(); - }, - // Get a promise for this deferred - // If obj is provided, the promise aspect is added to the object - promise: function( obj ) { - if ( obj == null ) { - obj = promise; - } else { - for ( const key in promise ) { - obj[ key ] = promise[ key ]; + + self.fail(function(d) { + reject(d); + }); + }); + }, + pipe: function(fnDone, fnFail, fnProgress) { + return jQuery.Deferred(function(newDefer) { + jQuery.each({ + done: [fnDone, 'resolve'], + fail: [fnFail, 'reject'], + progress: [fnProgress, 'notify'] + }, function(handler, data) { + const fn = data[0]; + const action = data[1]; + + let returned; + if (jQuery.isFunction(fn)) { + deferred[handler](function() { + returned = fn.apply(this, arguments); + if (returned && jQuery.isFunction(returned.promise)) { + returned.promise().then(newDefer.resolve, newDefer.reject, newDefer.notify); + } else { + newDefer[action + 'With'](this === deferred ? newDefer : this, [returned]); + } + }); + } else { + deferred[handler](newDefer[action]); } + }); + }).promise(); + }, + // Get a promise for this deferred + // If obj is provided, the promise aspect is added to the object + promise: function(obj) { + if (obj == null) { + obj = promise; + } else { + for (const key in promise) { + obj[key] = promise[key]; } - return obj; } - }, - deferred = promise.promise({}), - key; + return obj; + } + }; + + const deferred = promise.promise({}); + + let key; - for ( key in lists ) { - deferred[ key ] = lists[ key ].fire; - deferred[ key + 'With' ] = lists[ key ].fireWith; + for (key in lists) { + deferred[key] = lists[key].fire; + deferred[key + 'With'] = lists[key].fireWith; } // Handle state - deferred.done( function() { + deferred.done(function() { state = 'resolved'; - }, failList.disable, progressList.lock ).fail( function() { + }, failList.disable, progressList.lock).fail(function() { state = 'rejected'; - }, doneList.disable, progressList.lock ); + }, doneList.disable, progressList.lock); // Call given func if any - if ( func ) { - func.call( deferred, deferred ); + if (func) { + func.call(deferred, deferred); } // All done! @@ -553,43 +582,45 @@ jQuery.extend({ }, // Deferred helper - when: function( firstParam ) { - let args = sliceDeferred.call( arguments, 0 ), - i = 0, - length = args.length, - pValues = new Array( length ), - count = length, - deferred = length <= 1 && firstParam && jQuery.isFunction( firstParam.promise ) ? - firstParam : - jQuery.Deferred(), - promise = deferred.promise(); - function resolveFunc( i ) { - return function( value ) { - args[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments, 0 ) : value; - if ( !( --count ) ) { - deferred.resolveWith( deferred, args ); + when: function(firstParam) { + const args = sliceDeferred.call(arguments, 0); + const length = args.length; + const pValues = new Array(length); + const deferred = length <= 1 && firstParam && jQuery.isFunction(firstParam.promise) ? + firstParam : + jQuery.Deferred(); + const promise = deferred.promise(); + + let i = 0, + count = length; + + function resolveFunc(i) { + return function(value) { + args[i] = arguments.length > 1 ? sliceDeferred.call(arguments, 0) : value; + if (!(--count)) { + deferred.resolveWith(deferred, args); } }; } - function progressFunc( i ) { - return function( value ) { - pValues[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments, 0 ) : value; - deferred.notifyWith( promise, pValues ); + function progressFunc(i) { + return function(value) { + pValues[i] = arguments.length > 1 ? sliceDeferred.call(arguments, 0) : value; + deferred.notifyWith(promise, pValues); }; } - if ( length > 1 ) { - for ( ; i < length; i++ ) { - if ( args[ i ] && args[ i ].promise && jQuery.isFunction( args[ i ].promise ) ) { - args[ i ].promise().then( resolveFunc(i), deferred.reject, progressFunc(i) ); + if (length > 1) { + for (; i < length; i++) { + if (args[i] && args[i].promise && jQuery.isFunction(args[i].promise)) { + args[i].promise().then(resolveFunc(i), deferred.reject, progressFunc(i)); } else { --count; } } - if ( !count ) { - deferred.resolveWith( deferred, args ); + if (!count) { + deferred.resolveWith(deferred, args); } - } else if ( deferred !== firstParam ) { - deferred.resolveWith( deferred, length ? [ firstParam ] : [] ); + } else if (deferred !== firstParam) { + deferred.resolveWith(deferred, length ? [firstParam] : []); } return promise; } diff --git a/bin/tsw/util/Queue/index.js b/bin/tsw/util/Queue/index.js index 8a712d09..d7dadd41 100644 --- a/bin/tsw/util/Queue/index.js +++ b/bin/tsw/util/Queue/index.js @@ -1,14 +1,14 @@ -/*! +/* ! * 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'; + function Queue() { - + this._queue = []; } @@ -19,53 +19,50 @@ this.create = function() { Queue.prototype.queue = function(fn) { - - if(typeof fn !== 'function') { - + + if (typeof fn !== 'function') { + return this; } - + fn._domain = process.domain; - + this._queue.push(fn); - - if(this._queue.length === 1) { + + if (this._queue.length === 1) { this.dequeue(); } - + return this; }; Queue.prototype.dequeue = function() { - - let 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(); - + return this; } - - fn = this._queue[0]; + + const fn = this._queue[0]; this._queue[0] = 'pending'; - - domain = fn._domain; + + const domain = fn._domain; fn._domain = undefined; - - if(domain && domain !== process.domain) { + + if (domain && domain !== process.domain) { domain.run(() => { fn.call(this); }); - }else{ + } else { fn.call(this); } - + return this; }; - diff --git a/bin/tsw/util/alpha.js b/bin/tsw/util/alpha.js index bb41e645..fb417c3b 100644 --- a/bin/tsw/util/alpha.js +++ b/bin/tsw/util/alpha.js @@ -1,10 +1,10 @@ -/*! +/* ! * 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'; + module.exports = require('./auto-report/alpha.js'); diff --git a/bin/tsw/util/auto-report/TEReport.js b/bin/tsw/util/auto-report/TEReport.js index 4278ad06..1b47fd75 100644 --- a/bin/tsw/util/auto-report/TEReport.js +++ b/bin/tsw/util/auto-report/TEReport.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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'; + /** * 测试环境自动发现 @@ -21,30 +21,30 @@ const isWindows = require('util/isWindows.js'); this.report = function() { - - if(isWindows.isWindows) { + + if (isWindows.isWindows) { return; } - if(!config.isTest) { + if (!config.isTest) { return; } const logText = `${serverInfo.intranetIp}:${config.httpPort}`; let logJson = { - ip : serverInfo.intranetIp, - port : config.httpPort + ip: serverInfo.intranetIp, + port: config.httpPort }; require('api/cmdb').GetDeviceThisServer().done(function(d) { let business; - if(d && d.business) { + if (d && d.business) { business = d.business[0]; } - if(!business) { + if (!business) { business = { moduleId: 0, L1Business: '未知', @@ -58,80 +58,80 @@ this.report = function() { 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 : '' + time: new Date().toGMTString(), + name: '', + group: 'unknown', + desc: '', + order: 0, + owner: '' }, config.testInfo, logJson); const logKey = 'h5test' + logJson.group; - //上报自己 + // 上报自己 post.report(logKey, logText, logJson); - //开放平台上报,不用再分组了 - if(config.appid && config.appkey) { + // 开放平台上报,不用再分组了 + 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: '' }); } - //上报分组 + // 上报分组 require('util/CD.js').check('h5test' + logJson.group, 1, 60).done(function() { post.report('group.h5test', logText, logJson); }); }); - + }; this.list = function(group) { - + const defer = Deferred.create(); let getLogJsonDefer; group = group || ''; - //开平对应的存储 - if(context.appid && context.appkey) { + // 开平对应的存储 + if (context.appid && context.appkey) { getLogJsonDefer = postOpenapi.getLogJson(`${context.appid}/tsw/h5test`); - }else{ + } else { getLogJsonDefer = post.getLogJson(`h5test${group}`); } getLogJsonDefer.done(function(arr) { - + const res = []; const map = {}; - + arr.forEach(function(v) { - if(!map[v.ip]) { + if (!map[v.ip]) { map[v.ip] = true; res.push(v); } }); - //增加一个临时染色 + // 增加一个临时染色 res.push({ time: new Date(), name: '临时染色', group: 'TSW', desc: '人在家中坐,包从天上来', order: -65536, - //owner: "TSW", + // owner: "TSW", groupName: group, ip: 'alpha', moduleId: 0, @@ -144,7 +144,7 @@ this.list = function(group) { defer.resolve(res); }); - + return defer; }; @@ -159,7 +159,7 @@ this.getAllGroup = function() { const map = {}; arr.forEach(function(v) { - if(!map[v.group]) { + if (!map[v.group]) { map[v.group] = true; res.push(v); } diff --git a/bin/tsw/util/auto-report/alpha.js b/bin/tsw/util/auto-report/alpha.js index 86dde2e6..85233ebe 100644 --- a/bin/tsw/util/auto-report/alpha.js +++ b/bin/tsw/util/auto-report/alpha.js @@ -1,18 +1,18 @@ -/*! +/* ! * 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 config = require('config'); const logger = require('logger'); -const {isWindows} = require('util/isWindows'); +const { isWindows } = require('util/isWindows'); const TSW = require('api/keyman'); -if(!global[__filename]) { +if (!global[__filename]) { global[__filename] = {}; } @@ -28,30 +28,30 @@ this.isAlpha = function(req) { let 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{ + } else { uin = req; - if(!uin) { + if (!uin) { uin = logger.getKey(); } - if(!uin) { + if (!uin) { uin = this.getUin(); } } - if(uin && isWindows) { - //windows 抓包用 - if(config.skyMode) { + if (uin && isWindows) { + // windows 抓包用 + if (config.skyMode) { return true; } } @@ -68,12 +68,12 @@ this.getUin = function(req) { req = req || window.request; - if(!req) { + if (!req) { return uin; } - //业务有可能不使用uin登录态,支持业务扩展getUin实现 - if(config.extendMod && typeof config.extendMod.getUin === 'function') { + // 业务有可能不使用uin登录态,支持业务扩展getUin实现 + 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 db46253a..c7e48579 100644 --- a/bin/tsw/util/auto-report/download.js +++ b/bin/tsw/util/auto-report/download.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 gzipHttp = require('util/gzipHttp.js'); @@ -37,32 +37,32 @@ module.exports.go = function(request, response) { const 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格式非法'); } const createLogKey = function(appid, group, key) { let logKey = key; - if(group) { + if (group) { logKey = `${group}/${logKey}`; } @@ -71,11 +71,11 @@ module.exports.go = function(request, response) { let logKey = createLogKey(appid, group, key); - if(appid) { + if (appid) { logKey = `${appid}/${logKey}`; } - //上下文设置 + // 上下文设置 context.group = group; context.limit = limit; context.key = key; @@ -87,20 +87,20 @@ module.exports.go = function(request, response) { logKey: logKey }); - //下标。 + // 下标。 const index = parseInt(request.GET.index, 10); key = logKey + '.' + index; - if(index >= 0) { - if(flagOfDownloadFileFormat === 'har') { + if (index >= 0) { + if (flagOfDownloadFileFormat === 'har') { currPost.getLogJsonByKey(logKey, key).done(function(data) { downloadHaz(request, response, { id: logKey + data['SNKeys'][0], data: data }); }); - }else { + } else { currPost.getLogJsonByKey(logKey, key).done(function(data) { download(request, response, { id: logKey + data['SNKeys'][0], @@ -109,16 +109,16 @@ module.exports.go = function(request, response) { }); } - }else{ - //点击下载全部 - if(flagOfDownloadFileFormat === 'har') { + } else { + // 点击下载全部 + if (flagOfDownloadFileFormat === 'har') { currPost.getLogJson(logKey).done(function(data) { downloadHaz(request, response, { id: logKey, data: data }); }); - }else { + } else { currPost.getLogJson(logKey).done(function(data) { download(request, response, { id: logKey, @@ -144,51 +144,51 @@ function returnError(message) { } -//curr +// curr const initRequestHar = function (request) { const unpackRaw = function (requestRaw) { let requestArr = []; // 补全没有的数据 const request = { - cookie:'', - headers:[], - postData:{}, - queryString:[], - cache:{}, - timings:{} + cookie: '', + headers: [], + postData: {}, + queryString: [], + 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) { 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) { const firstItem = item.split(' '); // 请求头:GET url; - if(firstItem.length === 3) { + if (firstItem.length === 3) { request.protocol = firstItem[2]; - }else{ + } else { request.protocol = firstItem[1]; } request.method = firstItem[0]; - }else{ + } else { item = item.split(':') || []; - if(item.length === 2 ) { + if (item.length === 2) { request[item[0]] = item[1].trim(); request.headers.push({ - name:item[0], - value:item[1] + name: item[0], + value: item[1] }); } } } }); - if(request.method && request.method === 'POST') { + if (request.method && request.method === 'POST') { const postDataArr = requestArr[requestArr.length - 1]; request.postData.mimeType = 'multipart/form-data'; request.postData.text = postDataArr; @@ -198,29 +198,29 @@ const initRequestHar = function (request) { } } // 补齐cookie - if(request.cookie) { + if (request.cookie) { const cookieArray = request.cookie.split(';'); - if(cookieArray.length > 0) { + if (cookieArray.length > 0) { request.cookieArray = []; cookieArray.forEach(function (item) { item = item.split('=') || []; request.cookieArray.push({ - name:item[0], - value:item[1] + name: item[0], + value: item[1] }); }); } } // 补齐query - if(request.path && request.path.indexOf('?') !== -1) { + if (request.path && request.path.indexOf('?') !== -1) { let query = request.path.slice(request.path.indexOf('?') + 1); - if(query) { + if (query) { query = query.split('&');// re=va query.forEach(function (item) { item = item.split('='); request.queryString.push({ - name:item[0], - value:item[1], + name: item[0], + value: item[1], }); }); } @@ -231,39 +231,35 @@ const initRequestHar = function (request) { const getUrl = function (curr) { let url; let host; - if(curr) { + if (curr) { host = String(curr.host); url = String(curr.url); // 不包括host,特殊处理下 - if(url && host && url.indexOf(host) === -1) { - url = 'https://'+ host + url; + if (url && host && url.indexOf(host) === -1) { + url = 'https://' + host + url; } } return url; }; - let - requestHaz = {}, - requestHeader, - responseHeader; - - requestHeader = unpackRaw((Buffer.from(request.requestRaw ||'')).toString('utf8')); - responseHeader = unpackRaw((Buffer.from(request.responseHeader || '')).toString('utf8')); + const requestHaz = {}; + const requestHeader = unpackRaw((Buffer.from(request.requestRaw || '')).toString('utf8')); + const responseHeader = unpackRaw((Buffer.from(request.responseHeader || '')).toString('utf8')); requestHaz.startedDateTime = request.timestamps.ClientConnected; requestHaz.time = 3000; requestHaz.request = { - 'method':requestHeader.method, - 'url':getUrl(request), + 'method': requestHeader.method, + 'url': getUrl(request), 'httpVersion': requestHeader.protocol, 'cookies': requestHeader.cookieArray, 'headers': requestHeader.headers, - 'queryString' : requestHeader.queryString, - 'postData' : requestHeader.postData, - 'headersSize' : request.requestRaw.replace(/\n/g, '').length, - 'bodySize' : requestHeader['Content-Length'] || 0, + 'queryString': requestHeader.queryString, + 'postData': requestHeader.postData, + 'headersSize': request.requestRaw.replace(/\n/g, '').length, + 'bodySize': requestHeader['Content-Length'] || 0, }; // console.error(request.responseBody,'request.responseBody'); @@ -276,23 +272,23 @@ const initRequestHar = function (request) { // console.log(buffer.toString()) // }); - const mimeTypeTemp = responseHeader['Content-Type'] ; - let mimeType= (typeof mimeTypeTemp === 'undefined')? responseHeader['content-type']: mimeTypeTemp ; - mimeType = (typeof mimeType=== 'undefined') ? mimeType : mimeType.trim(); + const mimeTypeTemp = responseHeader['Content-Type']; + let mimeType = (typeof mimeTypeTemp === 'undefined') ? responseHeader['content-type'] : mimeTypeTemp; + mimeType = (typeof mimeType === 'undefined') ? mimeType : mimeType.trim(); requestHaz.response = { - 'Content-Type':'text/html; charset=UTF-8', - 'status': parseInt(request.resultCode)||0, + 'Content-Type': 'text/html; charset=UTF-8', + 'status': parseInt(request.resultCode, 10) || 0, 'statusText': 'OK', 'httpVersion': responseHeader.protocol, 'cookies': responseHeader.cookieArray, 'headers': responseHeader.headers, 'redirectURL': '', - 'headersSize' : requestHeader['Content-Length'] || 0, - 'bodySize' : request.contentLength || 0, - 'comment' : '', - 'content':{ - 'mimeType':mimeType, + 'headersSize': requestHeader['Content-Length'] || 0, + 'bodySize': request.contentLength || 0, + 'comment': '', + 'content': { + 'mimeType': mimeType, // "compression":request.responseBody.length - (request.contentLength || 0), // "text":request.responseBody, // "text": (Buffer.from(request.responseBody || '', 'base64')).toString('utf8'), @@ -313,18 +309,18 @@ const 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; let 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 { const ungziprawText = zlib.gunzipSync(requestResponseBodyBaseBuffer); requestHaz.response.content.text = ungziprawText.toString('utf8');// 暂时文件 @@ -333,14 +329,14 @@ const initRequestHar = function (request) { } return requestHaz; - }else{ + } else { requestHaz.response.content.text = requestResponseBodyBaseBuffer.toString('utf8'); return requestHaz; } - }else{ - requestHaz.response.content.size =0; + } else { + requestHaz.response.content.size = 0; requestHaz.response.content.text = ''; return requestHaz; } @@ -351,35 +347,36 @@ const downloadHaz = function (request, response, opt) { opt = opt || {}; let data = opt.data || [], - viewData = [], - filename = opt.id || 'log', - index = parseInt(request.GET.index, 10), - SNKey = request.GET.SNKey; + index = parseInt(request.GET.index, 10); + + const viewData = []; + const filename = opt.id || 'log'; + const SNKey = request.GET.SNKey; const hazJson = { - 'log':{ + 'log': { 'version': '1.2', 'creator': { 'name': 'TSW', 'version': '1.0' }, - 'entries':[] + 'entries': [] } }; - 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,24 +391,24 @@ const downloadHaz = function (request, response, opt) { contentType: 'application/octet-stream' }); - if(SNKey) { - //override index + if (SNKey) { + // override index index = 0; } - if(index >= 0) { + if (index >= 0) { data = [data[index]]; - }else{ + } else { data = data.reverse(); } data.forEach(function (tmp, i) { - if(tmp.curr) { + if (tmp.curr) { viewData.push(tmp.curr); } tmp.ajax && tmp.ajax.forEach(function(ajax, i) { - if(!ajax.SN) { + if (!ajax.SN) { return; } viewData.push(ajax); @@ -433,26 +430,27 @@ const download = function(request, response, opt) { opt = opt || {}; let data = opt.data || [], - viewData = [], filename = opt.id || 'log', - index = parseInt(request.GET.index, 10), - SNKey = request.GET.SNKey; + index = parseInt(request.GET.index, 10); + + const viewData = []; + const 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; } - //data = data[0]; + // data = data[0]; - if(typeof data == 'string') { + if (typeof data === 'string') { failRet(request, response, 'key类型不对'); return; @@ -470,21 +468,21 @@ const download = function(request, response, opt) { contentType: 'application/octet-stream' }); - if(SNKey) { - //override index + if (SNKey) { + // override index index = 0; } - if(index >= 0) { + if (index >= 0) { data = [data[index]]; - }else{ + } else { data = data.reverse(); } const archiver = new Archiver('zip'); - archiver.append(tmpl.download_index(data), {name: '_index.htm'}); - archiver.append(tmpl.download_content_types(), {name: '[Content_Types].xml'}); + archiver.append(tmpl.download_index(data), { name: '_index.htm' }); + archiver.append(tmpl.download_content_types(), { name: '[Content_Types].xml' }); data.forEach(function(tmp, i) { const sid = ('0000' + (i + 1)).slice(-3); @@ -498,24 +496,24 @@ const download = function(request, response, opt) { const log = { curr: { - sid : sid, - - protocol : 'HTTP', - host : '', - url : '', - cache : '', - process : tmp.curr.process, - resultCode : '200', - contentLength : Buffer.byteLength(tmp.curr.responseBody || '', 'UTF-8'), - contentType : 'text/plain', - clientIp : '', - clientPort : '', - serverIp : '', - 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'), - timestamps : tmp.curr.timestamps + sid: sid, + + protocol: 'HTTP', + host: '', + url: '', + cache: '', + process: tmp.curr.process, + resultCode: '200', + contentLength: Buffer.byteLength(tmp.curr.responseBody || '', 'UTF-8'), + contentType: 'text/plain', + clientIp: '', + clientPort: '', + serverIp: '', + 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'), + timestamps: tmp.curr.timestamps } }; @@ -527,7 +525,7 @@ const download = function(request, response, opt) { const ajax = {}; - if(!curr.SN) { + if (!curr.SN) { return; } @@ -598,20 +596,20 @@ const failRet = function(request, response, msg) { const decodeChunkedUint8Array = function (Uint8ArrayBuffer) { const rawText = []; let startOfTheRawText = Uint8ArrayBuffer.indexOf(13); - while( startOfTheRawText !== -1 && startOfTheRawText !== 0 ) { + while (startOfTheRawText !== -1 && startOfTheRawText !== 0) { const rawTextSizeUint8ArrayBuffer = Uint8ArrayBuffer.slice(0, startOfTheRawText); const rawTextSizeUint8ArrayInt = parseInt(Buffer.from(rawTextSizeUint8ArrayBuffer), 16); - if(rawTextSizeUint8ArrayInt === 0 ) { + if (rawTextSizeUint8ArrayInt === 0) { break; } - const chunkedText = Uint8ArrayBuffer.slice(startOfTheRawText+2, startOfTheRawText+2 +rawTextSizeUint8ArrayInt ); + const chunkedText = Uint8ArrayBuffer.slice(startOfTheRawText + 2, startOfTheRawText + 2 + rawTextSizeUint8ArrayInt); rawText.push(Buffer.from(chunkedText)); - Uint8ArrayBuffer = Uint8ArrayBuffer.slice(startOfTheRawText+2 +rawTextSizeUint8ArrayInt+2); + Uint8ArrayBuffer = Uint8ArrayBuffer.slice(startOfTheRawText + 2 + rawTextSizeUint8ArrayInt + 2); startOfTheRawText = Uint8ArrayBuffer.indexOf(13); } - let bufferText = new Uint8Array( 0 ); - for(let 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{ + } else { limit.count[type] = 1; } - - if(Date.now() - limit.time < 5000) { - - if(limit.count[type] > limit.max[type]) { - + + 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{ + + } else { limit.count = {}; limit.time = Date.now(); } @@ -173,95 +173,95 @@ module.exports = function(req, res) { 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); - 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}', { - 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{ + const logText = logger.getText(); + + if (type === 'alpha') { + try { - //webapp的二进制回包转成可视化的结构 - if(res._body && res._headers['content-type'] === 'webapp') { + // webapp的二进制回包转成可视化的结构 + if (res._body && res._headers['content-type'] === 'webapp') { 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 + 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) { + } catch (e) { logger.error(e.stack); } } const 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') { - //根据content-type设置group便于分类查询抓包 - if(!reportData.group) { + if (type === 'alpha') { + // 根据content-type设置group便于分类查询抓包 + if (!reportData.group) { const pathName = req.REQUEST.pathname; - const fileName = pathName.substr( pathName.lastIndexOf('/') + 1); + const fileName = pathName.substr(pathName.lastIndexOf('/') + 1); reportData.group = module.exports.fingureCroup({ - resHeaders : res._headers, - reqHeaders : req.headers, - suffix : ~fileName.indexOf('.') ? fileName.substr( fileName.lastIndexOf('.') + 1) : '', - method : req.method, - mod_act : mod_act, - returnCode : res.statusCode + resHeaders: res._headers, + reqHeaders: req.headers, + suffix: fileName.indexOf('.') !== -1 ? fileName.substr(fileName.lastIndexOf('.') + 1) : '', + method: req.method, + mod_act: mod_act, + returnCode: res.statusCode }); } - if(config.appid && config.appkey) { + if (config.appid && config.appkey) { module.exports.reportCloud(reportData); - }else{ + } else { module.exports.reportAlpha(reportData); } @@ -269,28 +269,29 @@ module.exports = function(req, res) { module.exports.report(reportData); }); - + }; module.exports.fingureCroup = function(opts) { - let group = 'other', - contentType = opts.resHeaders['content-type'] || '', - suffix = opts.suffix.toLowerCase(), - //一些特殊后缀的映射 - groupMap = { - gif : 'image', - xml : 'xml', - map : 'js', - //有些js返回头不一样还是要根据后缀来 - js : 'js' - }; + let group = 'other'; + + const contentType = opts.resHeaders['content-type'] || ''; + const suffix = opts.suffix.toLowerCase(); + // 一些特殊后缀的映射 + const groupMap = { + gif: 'image', + xml: 'xml', + map: 'js', + // 有些js返回头不一样还是要根据后缀来 + js: 'js' + }; - if(!contentType) { - //没声明算html + if (!contentType) { + // 没声明算html return 'html'; } - - switch(true) { + + switch (true) { case /^text\/html/.test(contentType): group = 'html'; break; @@ -309,17 +310,17 @@ 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) !== -1: group = 'XHR'; break; - case !!~['eot', 'svg', 'ttf', 'woff'].indexOf(suffix): + case ['eot', 'svg', 'ttf', 'woff'].indexOf(suffix) !== -1: group = 'font'; break; - //其余的通过后缀区分吧 - default : - if(!suffix) { + // 其余的通过后缀区分吧 + default: + if (!suffix) { group = 'other'; - }else { + } else { group = groupMap[suffix] || 'other'; } break; @@ -329,13 +330,13 @@ module.exports.fingureCroup = function(opts) { }; module.exports.reportAlpha = function(data) { - const currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24); + const currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24, 10); - if(!data.key) { + if (!data.key) { return; } - if(!canIuse.test(data.key)) { + if (!canIuse.test(data.key)) { return; } @@ -343,34 +344,34 @@ module.exports.reportAlpha = function(data) { const logNumMax = context.MAX_ALPHA_LOG || MAX_ALPHA_LOG; post.report(reportKey, data.logText, data.logJson).done(function(isFirst) { - if(isFirst) { - //增加计数 + if (isFirst) { + // 增加计数 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{ + } else { 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; } const reportGroupKey = [data.group, data.key].join('/'); post.report(reportGroupKey, data.logText, data.logJson).done(function(isFirst) { - //这里不用计数了 + // 这里不用计数了 }).always(function() { - //上报分组 + // 上报分组 CD.checkByCmem('v2.group.send.${data.group}', 1, 60).done(function() { - post.report('v2.group.alpha', data.group, {group: data.group}); + post.report('v2.group.alpha', data.group, { group: data.group }); }); }); }; @@ -382,24 +383,24 @@ module.exports.report = 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') { + // only alpha + if (data.type !== 'alpha') { return; } @@ -408,7 +409,7 @@ module.exports.reportCloud = function(data) { const 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.appid = config.appid; @@ -426,16 +427,16 @@ module.exports.reportCloud = function(data) { 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() { @@ -450,57 +451,57 @@ module.exports.receiveCloud = function(req, res) { res.setHeader('Content-Type', 'application/json; charset=UTF-8'); res.writeHead(200); res.end(JSON.stringify({ - code: message?-1:0, + code: message ? -1 : 0, message: message || 'success' }, null, 2)); }; const 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) { - //group is allow empty + 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') { + // only 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'); } const appid = context.appid; const appkey = context.appkey; const reportKey = [appid, data.key].join('/'); - const currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24); + const currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24, 10); const 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) { @@ -509,43 +510,43 @@ module.exports.receiveCloud = function(req, res) { }).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) { - //增加计数 + if (isFirst) { + // 增加计数 CD.checkByCmem(`SUM_TSW_ALPHA_LOG_KEY.${currDays}`, logNumMax, 24 * 60 * 60).done(function(curr) { logger.debug('curr count: ' + curr); }); - }else{ + } else { 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(); } const reportGroupKey = [appid, data.group, data.key].join('/'); postOpenapi.report(reportGroupKey, data.logText, data.logJson).done(function(isFirst) { - //这里不用计数了 + // 这里不用计数了 }).always(function() { - //上报分组 + // 上报分组 CD.checkByCmem(`v2.group.${appid}.${data.group}`, 1, 60).fail(function() { returnJson(); }).done(function() { const logText = postOpenapi.encode(appid, appkey, data.group); - const logJson = postOpenapi.encode(appid, appkey, {group: data.group}); + const logJson = postOpenapi.encode(appid, appkey, { group: data.group }); postOpenapi.report(`${appid}/v2.group.alpha`, logText, logJson).always(function() { returnJson(); @@ -558,13 +559,13 @@ module.exports.receiveCloud = function(req, res) { module.exports.top100 = function(req, res) { - - let item, str, filename, result; - - if(!global.top100) { + + let item; + + if (!global.top100) { return; } - + global.top100.push({ pathname: req.REQUEST.pathname, hostname: req.headers.host, @@ -572,113 +573,113 @@ module.exports.top100 = function(req, res) { socketIp: (req.socket && req.socket.remoteAddress), header: httpUtil.getRequestHeaderStr(req), body: req.REQUEST.body, - statusCode : res.statusCode, + statusCode: res.statusCode, 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; } - - result = global.top100; + + const result = global.top100; global.top100 = null; - - + + let map = {}; let arr = []; let key; const buffer = []; - - //分析pathname聚集 + + // 分析pathname聚集 map = {}; arr = []; result.forEach(function(v, i) { - + const key = v.hostname + v.pathname; - - if(map[key]) { - map[key] ++; - }else{ + + if (map[key]) { + map[key]++; + } else { map[key] = 1; } - + }); - - for(key in map) { - + + 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) { + + // top 100 + if (i < 100) { buffer.push(v.count + '\t' + v.key); buffer.push('\r\n'); } - + }); - + buffer.push('\r\n\r\n'); - - - //分析ip聚集 + + + // 分析ip聚集 map = {}; arr = []; result.forEach(function(v, i) { - - if(map[v.ip]) { - map[v.ip] ++; - }else{ + + if (map[v.ip]) { + map[v.ip]++; + } else { map[v.ip] = 1; } - + }); - - for(key in map) { - + + 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) { + + // 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 +687,24 @@ 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'; - - + + + const str = buffer.join(''); + + const filename = __dirname + '/../../../proxy/cpu' + process.serverInfo.cpu + '.' + Date.now() + '.top100'; + + fs.writeFile(filename, Buffer.from(str, 'utf-8'), function(err) { + + if (err) { + logger.error(`top100 error ${err.message}`); + + return; + } + logger.info('top100: ' + filename); }); - + }; diff --git a/bin/tsw/util/auto-report/post.js b/bin/tsw/util/auto-report/post.js index 006c9eda..869b441d 100644 --- a/bin/tsw/util/auto-report/post.js +++ b/bin/tsw/util/auto-report/post.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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'); @@ -34,9 +34,9 @@ module.exports.getLogJsonByKey = function (uin, key) { const prefix = this.keyJson(''); let keyJson; - if(String(key).startsWith(prefix)) { + if (String(key).startsWith(prefix)) { keyJson = key; - }else{ + } else { keyJson = this.keyJson(key); } @@ -44,34 +44,41 @@ module.exports.getLogJsonByKey = function (uin, key) { }; module.exports.getLogArr = function(uin, type, key, limit) { - + const defer = Deferred.create(); const memcached = this.cmem(); const keyBitmap = this.keyBitmap(uin); - if(!memcached) { + if (!memcached) { return defer.resolve([]); } memcached.get(keyBitmap, function(err, data) { + + if (err) { + logger.error('get fail'); + defer.resolve([]); + return; + } + const start = data || 0; let keyTextArr = type === 'text' ? module.exports.keyTextArr(uin, start) : module.exports.keyJsonArr(uin, start); const keyJsonArr = module.exports.keyJsonArr(uin, start); - //如果传递进来key,则只需要关注传进来的key - if(key) { + // 如果传递进来key,则只需要关注传进来的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) { - if(err) { + if (err) { logger.error('bad key:'); logger.error(keyTextArr); logger.error((err && err.stack) || err); @@ -79,27 +86,33 @@ module.exports.getLogArr = function(uin, type, key, limit) { return; } - let i, len, index; - let arr = [], keys = [], SNKeys = [], extInfos = []; //keys和SNKeys挂在arr下面,保持跟arr同序。 - let currKey, tmpInfo = {}; + let i, + len, + index; + const arr = [], + keys = [], + SNKeys = [], + extInfos = []; // keys和SNKeys挂在arr下面,保持跟arr同序。 + let 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]); } arr.push(data[currKey]); keys.push(keyJsonArr[index].split('.').slice(-1)); - if(type == 'json') { - tmpInfo = data[currKey].curr;//内含resultCode + if (type === 'json') { + tmpInfo = data[currKey].curr;// 内含resultCode SNKeys.push(module.exports.getLogSN(data[currKey].curr && data[currKey].curr.logText)); - }else{ + } else { SNKeys.push(module.exports.getLogSN(data[currKey])); tmpInfo.resultCode = module.exports.getLogResultCode(data[currKey]); } @@ -118,13 +131,13 @@ module.exports.getLogArr = function(uin, type, key, limit) { }); }); - + return defer; }; module.exports.getLogSN = function (logText) { const 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, ''); @@ -133,7 +146,7 @@ module.exports.getLogSN = function (logText) { }; module.exports.getLogFromReg = function (logText, reg) { - if(logText && reg) { + if (logText && reg) { logText = logText.match && logText.match(reg) && logText.match(reg)[1] || 'unknown'; return logText.replace(/\s/igm, ''); @@ -149,22 +162,22 @@ module.exports.getLogResultCode = function (logText) { module.exports.report = function(key, logText, logJson) { const defer = Deferred.create(); - if(!key) { + if (!key) { return defer.reject(); } - - if(!logText) { + + if (!logText) { return defer.reject(); } - + logJson = logJson || {}; - const memcached = this.cmem(); //要用this + const memcached = this.cmem(); // 要用this const keyText = module.exports.keyText(key); const keyJson = module.exports.keyJson(key); const keyBitmap = module.exports.keyBitmap(key); - if(!memcached) { + if (!memcached) { return defer.resolve(); } @@ -172,17 +185,17 @@ module.exports.report = function(key, logText, logJson) { let isFirst = false; - if(err) { - //add err是正常的 - //return; + if (err) { + // add err是正常的 + // return; } - if(ret === true) { + if (ret === true) { isFirst = true; } memcached.incr(keyBitmap, 1, function(err, result) { - if(err) { + if (err) { logger.error(err.stack); defer.reject(); return; @@ -190,12 +203,26 @@ module.exports.report = function(key, logText, logJson) { let 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) { + + if (err) { + logger.error(err.stack); + defer.reject(); + return; + } + memcached.set([keyJson, index].join('.'), logJson, 24 * 60 * 60, function(err, ret) { + + if (err) { + logger.error(err.stack); + defer.reject(); + return; + } + defer.resolve(isFirst); }); }); @@ -208,50 +235,50 @@ module.exports.report = function(key, logText, logJson) { module.exports.keyBitmap = function(key) { - const currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24); + const currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24, 10); return ['bitmap.v4.log', currDays, key].join('.'); }; module.exports.keyJson = function(key) { - const currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24); + const currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24, 10); return ['json.v4.log', currDays, key].join('.'); }; module.exports.keyText = function(key) { - const currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24); + const currDays = parseInt(Date.now() / 1000 / 60 / 60 / 24, 10); return ['text.v4.log', currDays, key].join('.'); }; module.exports.keyJsonArr = function(key, index) { - + let i = 0; const arr = []; const 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) { - + let i = 0; const arr = []; const 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; }; -//加密 +// 加密 module.exports.encode = function(appid, appkey, data) { const input = Buffer.from(JSON.stringify(data), 'UTF-8'); const buff = zlib.deflateSync(input); @@ -262,16 +289,16 @@ module.exports.encode = function(appid, appkey, data) { return body; }; -//解密 +// 解密 module.exports.decode = function(appid, appkey, body) { const des = crypto.createDecipher('des', (appid + appkey)); let buf1 = ''; let buf2 = ''; - try{ + try { buf1 = des.update(body, 'base64', 'hex'); buf2 = des.final('hex'); - }catch(e) { + } catch (e) { logger.error(e.stack); return null; } @@ -281,9 +308,9 @@ module.exports.decode = function(appid, appkey, body) { let data = null; - try{ + 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 54e040e2..3caa30ae 100644 --- a/bin/tsw/util/auto-report/post.openapi.js +++ b/bin/tsw/util/auto-report/post.openapi.js @@ -1,11 +1,11 @@ -/*! +/* ! * 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 cmemTSW = require('data/cmem.tsw.js'); const post = require('./post.js'); diff --git a/bin/tsw/util/auto-report/src/_config.js b/bin/tsw/util/auto-report/src/_config.js index 9cb4d1d3..ce23bf17 100644 --- a/bin/tsw/util/auto-report/src/_config.js +++ b/bin/tsw/util/auto-report/src/_config.js @@ -1,23 +1,23 @@ -/*! +/* ! * 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'; + define(function(require, exports, module) { return { - + tmpl: { create: true }, all: { create: false } - + }; - + }); diff --git a/bin/tsw/util/auto-report/src/download.tmpl.html b/bin/tsw/util/auto-report/src/download.tmpl.html index 449ca3ba..f601030f 100644 --- a/bin/tsw/util/auto-report/src/download.tmpl.html +++ b/bin/tsw/util/auto-report/src/download.tmpl.html @@ -82,9 +82,9 @@ return d; } - var pad = function(n){return n < 10 ? '0' + n : n;}, + var pad = function(n){return n < 10 ? '0' + n : n;}, tz = d.getTimezoneOffset(), - tzs = (tz > 0 ? "-" : "+") + pad(parseInt(Math.abs(tz / 60))); + tzs = (tz > 0 ? "-" : "+") + pad(parseInt(Math.abs(tz / 60), 10)); if (tz === 0){ tzs = 'Z'; @@ -105,26 +105,26 @@ var timeStamp = data.timestamps || {}, defaultTime = new Date(); - + %> - @@ -137,4 +137,4 @@ - \ No newline at end of file + diff --git a/bin/tsw/util/auto-report/src/mail.tmpl.html b/bin/tsw/util/auto-report/src/mail.tmpl.html index 075088c1..49e24696 100644 --- a/bin/tsw/util/auto-report/src/mail.tmpl.html +++ b/bin/tsw/util/auto-report/src/mail.tmpl.html @@ -84,7 +84,10 @@ -<%if(data.total_arr.length){%> +<% +var i, item; +if(data.total_arr.length){ +%> @@ -95,7 +98,7 @@ - <%for(var i = 0,item; i < data.total_arr.length; i++){%> + <%for(i = 0; i < data.total_arr.length; i++){%> item = data.total_arr[i++]; diff --git a/bin/tsw/util/auto-report/src/view.tmpl.html b/bin/tsw/util/auto-report/src/view.tmpl.html index e6c2b99a..655e4f6e 100644 --- a/bin/tsw/util/auto-report/src/view.tmpl.html +++ b/bin/tsw/util/auto-report/src/view.tmpl.html @@ -1,263 +1,259 @@ - - - - - - + + + + + + diff --git a/bin/tsw/util/auto-report/tmpl.js b/bin/tsw/util/auto-report/tmpl.js index cb574e35..25b7a128 100644 --- a/bin/tsw/util/auto-report/tmpl.js +++ b/bin/tsw/util/auto-report/tmpl.js @@ -1,22 +1,24 @@ -//tmpl file list: -//auto-report/src/download.tmpl.html -//auto-report/src/mail.tmpl.html -//auto-report/src/view.tmpl.html -//auto-report/src/zenburn.tmpl.html +// tmpl file list: +// auto-report/src/download.tmpl.html +// auto-report/src/mail.tmpl.html +// auto-report/src/view.tmpl.html +// auto-report/src/zenburn.tmpl.html define(function(require, exports, module) { - const tmpl = { + var tmpl = { 'encodeHtml': function(s) { - return (s+'').replace(/[\x26\x3c\x3e\x27\x22\x60]/g, function($0) { - return '&#'+$0.charCodeAt(0)+';'; + return (String(s)).replace(/[\x26\x3c\x3e\x27\x22\x60]/g, function($0) { + return '&#' + $0.charCodeAt(0) + ';'; }); }, 'download_content_types': function(data) { - let __p=[], _p=function(s) { + var __p = [], + _p = function(s) { __p.push(s); - }, out=_p; + }, + out = _p; __p.push('\n\n\n\n\n'); return __p.join(''); @@ -24,16 +26,20 @@ define(function(require, exports, module) { 'download_index': function(data) { - let __p=[], _p=function(s) { - __p.push(s); - }, out=_p; + var __p = [], + _p = function(s) { + __p.push(s); + }, + out = _p; data = data || []; - let i, len, entry; + var i, + len, + entry; __p.push('\n \n \n \n \n
<%=item.config.title%>
\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))); + tzs = (tz > 0 ? '-' : '+') + pad(parseInt(Math.abs(tz / 60), 10)); if (tz === 0) { tzs = 'Z'; - }else if (tz % 60 != 0) { + } else if (tz % 60 != 0) { tzs += pad(Math.abs(tz % 60)); - }else{ + } else { tzs += ':00'; } - return d.getFullYear()+'-' - + pad(d.getMonth()+1)+'-' - + pad(d.getDate())+'T' - + pad(d.getHours())+':' - + pad(d.getMinutes())+':' - + pad(d.getSeconds())+'.' + return d.getFullYear() + '-' + + pad(d.getMonth() + 1) + '-' + + pad(d.getDate()) + 'T' + + pad(d.getHours()) + ':' + + pad(d.getMinutes()) + ':' + + pad(d.getSeconds()) + '.' + pad(d.getMilliseconds()) + tzs; }; - let timeStamp = data.timestamps || {}, + var timeStamp = data.timestamps || {}, defaultTime = new Date(); - - __p.push('\n\n \n\n \n \n \n \n \n \n \n \n \n \n \n \n\n \n\n\n'); - if(data.total_arr.length) { + + var i, + item; + 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++) { + for (i = 0; i < data.total_arr.length; i++) { __p.push(' item = data.total_arr[i++];\n \n \n '); }__p.push(' \n
项目名称mod_act报错次数负责人
'); @@ -181,7 +194,7 @@ define(function(require, exports, module) { _p(item.config.charge); __p.push('
'); - }else{ + } else { __p.push('

没有报错项目~

'); }__p.push('\n'); @@ -190,13 +203,15 @@ define(function(require, exports, module) { 'error': function(data) { - let __p=[], _p=function(s) { + var __p = [], + _p = function(s) { __p.push(s); - }, out=_p; + }, + 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(' '); - _p((data.config.title ? '('+data.config.title+')' : '')); + _p((data.config.title ? '(' + data.config.title + ')' : '')); __p.push(':1分钟脚本报错超过频率限制('); _p(data.minMaxNum); __p.push(')

\n\n'); @@ -206,80 +221,83 @@ define(function(require, exports, module) { 'log_view': function(data) { - let __p=[], _p=function(s) { - __p.push(s); - }, out=_p; - - const logArr = data.logArr || []; - let groupArr= data.groupArr || []; - const window = context.window || {}; - const hls = require('./highlight-tsw.js'); - const getResultCodeStyle = function (code) { + 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); - let style = ''; - if(code > 0) { + var style = ''; + if (code > 0) { code = Math.floor(code / 100); switch (code) { - case 2 : - case 6 : + case 2: + case 6: style = 'color:green;'; break; - case 3 : + case 3: style = 'color:#bf00ff;'; break; - case 4 : + case 4: style = 'color:orange;'; break; - case 5 : + case 5: style = 'color:red;'; break; } } - return style+'font-weight:;font-size:18px'; + return style + 'font-weight:;font-size:18px'; }; - const appid = context.appid; - const group = context.group; - const key = context.key; - const createLogKey = context.createLogKey; - const currPath = '/log/view/' + createLogKey(appid, group, key); + var appid = context.appid; + var group = context.group; + var key = context.key; + var createLogKey = context.createLogKey; + var currPath = '/log/view/' + createLogKey(appid, group, key); - //去重 - const tmp = [{name: '全部', href: '/log/view/' + createLogKey(appid, '', key)}]; - const groupMap= {}; - const nameMap = data.nameMap; + // 去重 + var tmp = [{ name: '全部', href: '/log/view/' + createLogKey(appid, '', key) }]; + var groupMap = {}; + var nameMap = data.nameMap; - for(const i in nameMap) { + for (var i in nameMap) { tmp.push({ - name : nameMap[i], - href : '/log/view/' + createLogKey(appid, i, key) + name: nameMap[i], + href: '/log/view/' + createLogKey(appid, i, key) }); } groupArr.sort().forEach(function(v) { - if(!v) { + if (!v) { return; } - if(groupMap[v]) { + if (groupMap[v]) { return; } - //默认展示的就去掉 - if(data.nameMap[v]) + // 默认展示的就去掉 + if (data.nameMap[v]) { return; + } groupMap[v] = 1; - const href = '/log/view/' + createLogKey(appid, v, key); - tmp.push({name: v, href: href}); + var href = '/log/view/' + createLogKey(appid, v, key); + tmp.push({ name: v, href: href }); }); groupArr = tmp; - const XSS = plug('util/xss.js'); + 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) { + if (appid) { __p.push(' 实时监控\n 测试环境'); - }else{ + } else { __p.push(' 临时染色'); }__p.push(' \n
'); - if(groupArr.length) { + if (groupArr.length) { __p.push('
'); groupArr.forEach(function(v, i) { __p.push(' '); _p(XSS.htmlEncode(v.name)); __p.push(''); - });__p.push('
'); - }if(logArr.length === 0) { - __p.push('
\n

还没有实时log

\n
'); + }); __p.push(' '); + } if (logArr.length === 0) { + __p.push('
\n

还没有实时log,故障排除?

\n
'); } logArr.forEach(function(logText, i) { logText = logText || ''; - const logLineArr = logText.split('\n'); - const reqUrl = logLineArr && logLineArr[0]; - let reqType = '', + var logLineArr = logText.split('\n'); + var reqUrl = logLineArr && logLineArr[0]; + var reqType = '', resultCode = logArr.extInfos[i].resultCode; - let hasError = false; - for(let j = 0; j < logLineArr.length; j++) { - const logTextLine = logLineArr[j]; + var hasError = false; + for (var j = 0; j < logLineArr.length; j++) { + var logTextLine = logLineArr[j]; if (!logTextLine) { continue; } - //匹配ajax失败的fail log - const retCodeReg = new RegExp('isFail:(.*)'); + // 匹配ajax失败的fail log + var retCodeReg = new RegExp('isFail:(.*)'); if (retCodeReg.exec(logTextLine)) { - const retCode = parseInt(RegExp.$1); + var retCode = parseInt(RegExp.$1, 10); if (retCode) { hasError = true; break; } } - //匹配ERRO log - if(!hasError && logTextLine.indexOf(' [ERRO] ') > 0) { + // 匹配ERRO log + if (!hasError && logTextLine.indexOf(' [ERRO] ') > 0) { hasError = true; break; } } - if(i < 2) { + if (i < 2) { logText = hls.highlight('tswlog', (logText), true).value; - }else{ + } else { logText = XSS.htmlEncode(logText); } __p.push('
\n
\n  statusCode: '); @@ -353,13 +371,13 @@ define(function(require, exports, module) { __p.push('\n\n 点击下载 云抓包™.saz\n\n 点击下载 云抓包™.har\n\n  \n

TSW 测试环境分组

\n

加入

\n

测试机采用自动发现机制,配置为测试环境后自动出现在这里

\n

测试机定义 config.isTest = true

\n

测试机个性配置参见 config.testInfo = {...}

\n

分组

'); - if(data.allGroup.length === 0) { + if (data.allGroup.length === 0) { __p.push('

还没有发现测试机

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

点击你喜欢的分组

'); }__p.push('\n