From 59bb79ad70f85b83ee79328090eb5adbbc7dcfc1 Mon Sep 17 00:00:00 2001
From: timcui 错误堆栈',
parseErr.stack,
'
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('
\npath: '); @@ -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全息日志:
\n'); _p(tmpl.encodeHtml(data.logText || '').replace(/\r\n|\r|\n/g, ''); @@ -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 += `
')); __p.push('
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 += `服务器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<%=item.config.title%> | 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 @@ - - - - -