Comparing version 0.0.35 to 0.0.36
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -10,3 +11,3 @@ * 2014/8/16 | ||
module.exports = { | ||
'SIMPLE_GET': { | ||
SIMPLE_GET: { | ||
unpack: 'raw', | ||
@@ -19,7 +20,8 @@ pack: 'raw', | ||
timeout: 10000, | ||
retry:1, | ||
server: [ | ||
{ host: '127.0.0.1', port: 8192} | ||
] | ||
retry: 1, | ||
server: [{ | ||
host: '127.0.0.1', | ||
port: 8192 | ||
}] | ||
} | ||
}; | ||
}; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -13,7 +14,7 @@ * 2014/8/16 | ||
RAL.init({ | ||
confDir : __dirname + path.sep + './config', | ||
logger : { | ||
confDir: path.join(__dirname, './config'), | ||
logger: { | ||
disable: true | ||
}, | ||
currentIDC : 'tc' | ||
currentIDC: 'tc' | ||
}); | ||
@@ -23,7 +24,7 @@ | ||
var pr = cp.fork(__dirname + path.sep + './server/server.js'); | ||
var pr = cp.fork(path.join(__dirname, './server/server.js')); | ||
module.exports = RAL; | ||
module.exports.end = function(){ | ||
module.exports.end = function () { | ||
pr.kill(); | ||
}; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -12,10 +13,11 @@ * 2014/8/16 | ||
http.createServer(function (request, response) { | ||
if (Math.random()>0.99){ | ||
if (Math.random() > 0.99) { | ||
response.writeHead(503); | ||
}else{ | ||
} | ||
else { | ||
response.writeHead(200); | ||
} | ||
setTimeout(function(){ | ||
setTimeout(function () { | ||
response.end(); | ||
},10); | ||
}, 10); | ||
}).listen(8192); |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -7,5 +8,7 @@ * 2014/8/16 | ||
/* eslint-disable no-console */ | ||
'use strict'; | ||
var RAL = require('./init.js'); | ||
var ral = require('./init.js'); | ||
var async = require('async'); | ||
@@ -16,26 +19,31 @@ var http = require('http'); | ||
//wait fork server start | ||
setTimeout(function(){ | ||
//preload | ||
var preload = RAL('SIMPLE_GET'); | ||
preload.on('data', function(){ | ||
// wait fork server start | ||
setTimeout(function () { | ||
// preload | ||
var preload = ral('SIMPLE_GET'); | ||
preload.on('data', function () { | ||
var tasks = [ | ||
function(cb){startBenchmark('ral', ralRequest, cb);}, | ||
function(cb){startBenchmark('http', httpRequest, cb);} | ||
function (cb) { | ||
startBenchmark('ral', ralRequest, cb); | ||
}, | ||
function (cb) { | ||
startBenchmark('http', httpRequest, cb); | ||
} | ||
]; | ||
async.series(tasks, function(){ | ||
RAL.end(); | ||
async.series(tasks, function () { | ||
ral.end(); | ||
}); | ||
}); | ||
},500); | ||
}, 500); | ||
var count = 500; | ||
function startBenchmark(name, func, callback){ | ||
function startBenchmark(name, func, callback) { | ||
var tasks = []; | ||
for(var i=0; i< count; i++){ | ||
for (var i = 0; i < count; i++) { | ||
tasks.push(func); | ||
} | ||
var start = now(); | ||
async.series(tasks, function(err,results){ | ||
async.series(tasks, function (err, results) { | ||
var end = now(); | ||
@@ -45,13 +53,14 @@ var failCount = 0; | ||
var lastErr; | ||
results.forEach(function(err){ | ||
if (err){ | ||
lastErr = err; | ||
failCount++; | ||
} | ||
else{ | ||
succCount++; | ||
} | ||
results.forEach(function (err) { | ||
if (err) { | ||
lastErr = err; | ||
failCount++; | ||
} | ||
else { | ||
succCount++; | ||
} | ||
}); | ||
lastErr = lastErr || 'none'; | ||
console.log(name, 'avg:', ((end-start)/count).toFixed(3), 'ms time:', (end-start).toFixed(3), 'ms ', 'succ:', succCount, 'failed:', failCount, 'lastError:', lastErr); | ||
console.log(name, 'avg:', ((end - start) / count).toFixed(3), 'ms time:', (end - start).toFixed(3), 'ms ', | ||
'succ:', succCount, 'failed:', failCount, 'lastError:', lastErr); | ||
callback(); | ||
@@ -61,8 +70,8 @@ }); | ||
function ralRequest(callback){ | ||
var req = RAL('SIMPLE_GET'); | ||
req.on('data', function(){ | ||
function ralRequest(callback) { | ||
var req = ral('SIMPLE_GET'); | ||
req.on('data', function () { | ||
callback(null); | ||
}); | ||
req.on('error', function(err){ | ||
req.on('error', function (err) { | ||
callback(null, err); | ||
@@ -72,5 +81,5 @@ }); | ||
function httpRequest(callback){ | ||
function httpRequest(callback) { | ||
var options = { | ||
host : '127.0.0.1', | ||
host: '127.0.0.1', | ||
path: '/', | ||
@@ -82,7 +91,8 @@ port: 8192, | ||
var req = http.request(options, function(res){ | ||
if (res.statusCode > 200 ){ | ||
var req = http.request(options, function (res) { | ||
if (res.statusCode > 200) { | ||
callback(null, new Error('503')); | ||
}else{ | ||
req.on('close', function(data){ | ||
} | ||
else { | ||
req.on('close', function (data) { | ||
callback(null); | ||
@@ -92,3 +102,3 @@ }); | ||
}); | ||
req.on('error', function(err){ | ||
req.on('error', function (err) { | ||
callback(null, err); | ||
@@ -95,0 +105,0 @@ }); |
@@ -1,12 +0,23 @@ | ||
function showMem () { | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
* 2014/8/10 | ||
*/ | ||
/* eslint-disable no-console */ | ||
'use strict'; | ||
function showMem() { | ||
global.gc(); | ||
var mem = process.memoryUsage(); | ||
var format = function(bytes){ | ||
return (bytes / 1024 / 1024).toFixed(2) + 'MB'; | ||
}; | ||
console.log('Process: heapTotal', format(mem.heapTotal), 'heapUsed', format(mem.heapUsed), | ||
'rss', format(mem.rss)); | ||
console.log('---------------------------------------------------------------------------') | ||
var mem = process.memoryUsage(); | ||
var format = function (bytes) { | ||
return (bytes / 1024 / 1024).toFixed(2) + 'MB'; | ||
}; | ||
console.log('Process: heapTotal', format(mem.heapTotal), 'heapUsed', format(mem.heapUsed), | ||
'rss', format(mem.rss)); | ||
console.log('---------------------------------------------------------------------------'); | ||
} | ||
module.exports.showMem = showMem; | ||
module.exports.showMem = showMem; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file entrance | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -18,2 +19,2 @@ * 2014/8/8 | ||
module.exports.ConfigNormalizer = require('./lib/config/configNormalizer.js'); | ||
module.exports.Middleware = require('./lib/middleware.js'); | ||
module.exports.Middleware = require('./lib/middleware.js'); |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -16,5 +17,6 @@ * 2014/8/4 | ||
* Create a balance chooser | ||
* | ||
* @constructor | ||
*/ | ||
function Balance(){ | ||
function Balance() { | ||
RalModule.call(this); | ||
@@ -26,14 +28,20 @@ } | ||
/** | ||
* get a server from server list | ||
* | ||
* @param balanceContext {BalanceContext} | ||
* @param {Object} balanceContext [description] | ||
*/ | ||
Balance.prototype.fetchServer = function(balanceContext){ | ||
Balance.prototype.fetchServer = function (balanceContext) { | ||
throw new Error('Not Implemented'); | ||
}; | ||
Balance.prototype.getContextClass = function(){ | ||
/** | ||
* get context handle class | ||
* | ||
* @return {Class} [description] | ||
*/ | ||
Balance.prototype.getContextClass = function () { | ||
return BalanceContext; | ||
}; | ||
Balance.prototype.getCategory = function(){ | ||
Balance.prototype.getCategory = function () { | ||
return 'balance'; | ||
@@ -45,9 +53,9 @@ }; | ||
* Will update when service info changed | ||
* @param serviceID | ||
* @param service | ||
* @constructor | ||
* | ||
* @param {string} serviceID [description] | ||
* @param {Object} service [description] | ||
*/ | ||
function BalanceContext(serviceID, service){ | ||
function BalanceContext(serviceID, service) { | ||
var me = this; | ||
//save idc info for future feature | ||
// save idc info for future feature | ||
me.currentIDC = ctx.currentIDC; | ||
@@ -57,12 +65,13 @@ me.serviceID = serviceID; | ||
me.crossIDCServers = []; | ||
service.server.forEach(function(server){ | ||
//get server list with same idc | ||
if (server.idc === me.currentIDC || !server.idc || me.currentIDC === ctx.IDC_ALL || service.hybird){ | ||
service.server.forEach(function (server) { | ||
// get server list with same idc | ||
if (server.idc === me.currentIDC || !server.idc || me.currentIDC === ctx.IDC_ALL || service.hybird) { | ||
me.reqIDCServers.push(server); | ||
}else{ | ||
} | ||
else { | ||
me.crossIDCServers.push(server); | ||
} | ||
}); | ||
if (me.reqIDCServers.length === 0){ | ||
//if there is no server in specify idc, use cross idc server | ||
if (me.reqIDCServers.length === 0) { | ||
// if there is no server in specify idc, use cross idc server | ||
logger.warning('Current IDC server is zero, use cross idc server ServiceID=' + serviceID); | ||
@@ -69,0 +78,0 @@ me.reqIDCServers = service.server; |
@@ -1,3 +0,5 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* | ||
* http://fis.baidu.com/ | ||
@@ -11,4 +13,3 @@ * 2014/8/4 | ||
var ralUtil = require('./util.js'); | ||
var util = require('util'); | ||
var expect = require('chai').expect; | ||
var cexpect = require('chai').expect; | ||
var iconv = require('iconv-lite'); | ||
@@ -25,3 +26,4 @@ var fs = require('fs'); | ||
* global config | ||
* @type {{}} | ||
* | ||
* @type {Object} | ||
*/ | ||
@@ -37,5 +39,5 @@ var config = {}; | ||
* regularly update config with config nomalizer | ||
* @param interval | ||
* @param all | ||
* @param cb | ||
* @param {number} interval [description] | ||
* @param {boolean} all [description] | ||
* @param {Function} cb [description] | ||
*/ | ||
@@ -55,3 +57,4 @@ var enableUpdate = function (interval, all, cb) { | ||
}); | ||
} else { | ||
} | ||
else { | ||
logger.warning('config update failed: ' + err.message); | ||
@@ -71,22 +74,17 @@ } | ||
var isAutoUpdateEnabled = function () { | ||
return updateInterval ? true : false; | ||
return !!updateInterval; | ||
}; | ||
/** | ||
* parse rawConf to valid conf | ||
* @param rawConf | ||
* @returns {*} | ||
* load rawConf to valid conf | ||
* @param {Object} inputRawConf [description] | ||
* @return {Object} [description] | ||
*/ | ||
function loadRawConf(rawConf) { | ||
function loadRawConf(inputRawConf) { | ||
/** | ||
* validate | ||
* @param serviceID | ||
* @param serviceInfo | ||
* @param {string} serviceID [description] | ||
* @param {Object} serviceInfo [description] | ||
*/ | ||
function validate(serviceID, serviceInfo) { | ||
/** | ||
* generate a handy error info | ||
* @param property | ||
* @returns {string} | ||
*/ | ||
function info(property) { | ||
@@ -97,3 +95,3 @@ return ['[', serviceID, '] config error => ', '[', property, ']'].join(''); | ||
function should(object, property) { | ||
return expect(object[property], info(property)); | ||
return cexpect(object[property], info(property)); | ||
} | ||
@@ -104,11 +102,11 @@ | ||
should(serviceInfo, 'encoding').to.be.a('string'); | ||
expect(iconv.encodingExists(serviceInfo.encoding), info('encoding is valid')).to.be.true; | ||
cexpect(iconv.encodingExists(serviceInfo.encoding), info('encoding is valid')).to.be.true; | ||
should(serviceInfo, 'pack').to.be.a('string'); | ||
expect(RalModule.modules.converter, 'invalid pack').to.have.property(serviceInfo.pack); | ||
cexpect(RalModule.modules.converter, 'invalid pack').to.have.property(serviceInfo.pack); | ||
should(serviceInfo, 'unpack').to.be.a('string'); | ||
expect(RalModule.modules.converter, 'invalid unpack').to.have.property(serviceInfo.unpack); | ||
cexpect(RalModule.modules.converter, 'invalid unpack').to.have.property(serviceInfo.unpack); | ||
should(serviceInfo, 'balance').to.be.a('string'); | ||
expect(RalModule.modules.balance, 'invalid balance').to.have.property(serviceInfo.balance); | ||
cexpect(RalModule.modules.balance, 'invalid balance').to.have.property(serviceInfo.balance); | ||
should(serviceInfo, 'protocol').to.be.a('string'); | ||
expect(RalModule.modules.protocol, 'invalid protocol').to.have.property(serviceInfo.protocol); | ||
cexpect(RalModule.modules.protocol, 'invalid protocol').to.have.property(serviceInfo.protocol); | ||
should(serviceInfo, 'hybird').to.be.a('boolean'); | ||
@@ -124,3 +122,3 @@ should(serviceInfo, 'server').to.be.a('array').and.not.to.be.empty; | ||
var clone = ralUtil.deepClone(rawConf); | ||
var clone = ralUtil.deepClone(inputRawConf); | ||
updateNeededRawConf = {}; | ||
@@ -130,6 +128,6 @@ var needUpdate = false; | ||
if (normalizerManager.needUpdate(serviceInfo)) { | ||
updateNeededRawConf[serviceID] = rawConf[serviceID]; | ||
updateNeededRawConf[serviceID] = inputRawConf[serviceID]; | ||
needUpdate = true; | ||
} | ||
//normalize | ||
// normalize | ||
clone[serviceID] = serviceInfo = normalizerManager.apply(serviceInfo); | ||
@@ -141,3 +139,4 @@ validate(serviceID, serviceInfo); | ||
enableUpdate(); | ||
} else { | ||
} | ||
else { | ||
logger.notice('config auto update wase disabled'); | ||
@@ -151,20 +150,21 @@ } | ||
* load config by config folder | ||
* @param confPath | ||
* @returns {{}} | ||
* @param {string} confPath [description] | ||
* @return {Object} [description] | ||
*/ | ||
function load(confPath) { | ||
function _load(confPath) { | ||
function loadConfByPath(normalizedPath) { | ||
var confFromFile = {}; | ||
function _loadByFile(confPath) { | ||
//load js or json as config | ||
var ext = path.extname(confPath); | ||
var name = path.basename(confPath, ext); | ||
function loadFile(filePath) { | ||
// load js or json as config | ||
var ext = path.extname(filePath); | ||
var name = path.basename(filePath, ext); | ||
var data; | ||
if (ext === '.js') { | ||
logger.trace('load config from ' + confPath); | ||
data = require(confPath); | ||
} else if (ext === '.json') { | ||
logger.trace('load config from ' + confPath); | ||
var content = fs.readFileSync(confPath); | ||
logger.trace('load config from ' + filePath); | ||
data = require(filePath); | ||
} | ||
else if (ext === '.json') { | ||
logger.trace('load config from ' + filePath); | ||
var content = fs.readFileSync(filePath); | ||
data = JSON.parse(content.toString()); | ||
@@ -174,3 +174,4 @@ } | ||
ctx.currentIDC = data.idc.toString(); | ||
} else { | ||
} | ||
else { | ||
_.extend(confFromFile, data); | ||
@@ -180,13 +181,14 @@ } | ||
function _loadByFolder(confPath) { | ||
var files = ralUtil.readdirSync(confPath); | ||
files.map(_loadByFile); | ||
function loadFolder(folderPath) { | ||
var files = ralUtil.readdirSync(folderPath); | ||
files.map(loadFile); | ||
} | ||
var stats = fs.statSync(confPath); | ||
var stats = fs.statSync(normalizedPath); | ||
if (stats.isFile()) { | ||
_loadByFile(confPath); | ||
} else if (stats.isDirectory()) { | ||
_loadByFolder(confPath); | ||
loadFile(normalizedPath); | ||
} | ||
else if (stats.isDirectory()) { | ||
loadFolder(normalizedPath); | ||
} | ||
return confFromFile; | ||
@@ -197,10 +199,11 @@ } | ||
try { | ||
//load raw conf | ||
rawConf = _load(confPath); | ||
// load raw conf | ||
rawConf = loadConfByPath(confPath); | ||
_.map(rawConf, function (value, key) { | ||
value.serviceID = key; | ||
}); | ||
//parse conf to normalized config | ||
// parse conf to normalized config | ||
loadRawConf(rawConf); | ||
} catch (err) { | ||
} | ||
catch (err) { | ||
logger.fatal('config [' + confPath + '] load failed'); | ||
@@ -214,3 +217,5 @@ throw err; | ||
* get runtime config context | ||
* @param serviceID | ||
* @param {string} serviceID [description] | ||
* @param {Object} options [description] | ||
* @return {Object} [description] | ||
*/ | ||
@@ -222,4 +227,4 @@ function parseContext(serviceID, options) { | ||
context.balance = RalModule.modules.balance[serviceInfo.balance]; | ||
var contextClass = context.balance.getContextClass(); | ||
context.balanceContext = new contextClass(serviceID, serviceInfo); | ||
var ContextClass = context.balance.getContextClass(); | ||
context.balanceContext = new ContextClass(serviceID, serviceInfo); | ||
context.protocolContext = context.protocol.normalizeConfig(serviceInfo); | ||
@@ -242,3 +247,3 @@ context.packConverter = RalModule.modules.converter[options.pack || serviceInfo.pack]; | ||
module.exports.getConf = function (name) { | ||
//return a copy | ||
// return a copy | ||
if (config[name]) { | ||
@@ -250,2 +255,6 @@ var conf = ralUtil.deepClone(config[name]); | ||
/** | ||
* get all service keys | ||
* @return {Array} service keys | ||
*/ | ||
module.exports.getConfNames = function () { | ||
@@ -255,2 +264,6 @@ return Object.keys(config); | ||
/** | ||
* get raw conf | ||
* @return {Object} raw conf | ||
*/ | ||
module.exports.getRawConf = function () { | ||
@@ -266,2 +279,2 @@ return ralUtil.deepClone(rawConf); | ||
module.exports.disableUpdate = disableUpdate; | ||
module.exports.isAutoUpdateEnabled = isAutoUpdateEnabled; | ||
module.exports.isAutoUpdateEnabled = isAutoUpdateEnabled; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file config normalizer base class | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -12,3 +13,3 @@ * 2014/8/19 | ||
function ConfigNormalizer(){ | ||
function ConfigNormalizer() { | ||
RalModule.call(this); | ||
@@ -19,14 +20,18 @@ } | ||
ConfigNormalizer.prototype.normalizeConfig = function (config){ | ||
/** | ||
* normalize config | ||
* @param {Object} config [description] | ||
*/ | ||
ConfigNormalizer.prototype.normalizeConfig = function (config) { | ||
throw new Error('Not Implemented'); | ||
}; | ||
ConfigNormalizer.prototype.getCategory = function (){ | ||
ConfigNormalizer.prototype.getCategory = function () { | ||
return 'normalizer'; | ||
}; | ||
ConfigNormalizer.prototype.needUpdate = function (config){ | ||
ConfigNormalizer.prototype.needUpdate = function (config) { | ||
return false; | ||
}; | ||
module.exports = ConfigNormalizer; | ||
module.exports = ConfigNormalizer; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file config updater for interval update config | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -16,5 +17,4 @@ * 2014/8/19 | ||
var path = require('path'); | ||
var cluster = require('cluster'); | ||
var DATA_PATH = __dirname + path.sep + '../../tmp/config_cache.json'; | ||
var TMP_PATH = __dirname + path.sep + '../../tmp'; | ||
var DATA_PATH = path.join(__dirname, '../../tmp/config_cache.json'); | ||
var TMP_PATH = path.join(__dirname, '../../tmp'); | ||
@@ -28,5 +28,5 @@ var DELAY_DELETE = 3000; | ||
* used to update config with ConfigNormalizer | ||
* @constructor | ||
* @param {Object} options [description] | ||
*/ | ||
function Updater(options){ | ||
function Updater(options) { | ||
EventEmitter.call(this); | ||
@@ -36,11 +36,12 @@ options = options || {}; | ||
this.dataPath = options.dataPath || DATA_PATH; | ||
//make sure tmpPath is exist and old cache file is deleted | ||
if (!fs.existsSync(this.tmpPath)){ | ||
// make sure tmpPath is exist and old cache file is deleted | ||
if (!fs.existsSync(this.tmpPath)) { | ||
fs.mkdirSync(this.tmpPath); | ||
}else{ | ||
if (fs.existsSync(this.dataPath)){ | ||
try{ | ||
} | ||
else { | ||
if (fs.existsSync(this.dataPath)) { | ||
try { | ||
fs.unlinkSync(this.dataPath); | ||
}catch(e){ | ||
} | ||
catch (e) {} | ||
} | ||
@@ -53,7 +54,13 @@ } | ||
Updater.prototype.checkResult = function(cb){ | ||
/** | ||
* check whether the update is done | ||
* @param {Function} cb [description] | ||
*/ | ||
Updater.prototype.checkResult = function (cb) { | ||
var me = this; | ||
function _check(){ | ||
fs.readFile(me.dataPath, function(err, data){ | ||
if (err){ | ||
var task; | ||
function check() { | ||
fs.readFile(me.dataPath, function (err, data) { | ||
if (err) { | ||
clearInterval(task); | ||
@@ -67,15 +74,16 @@ me.emit('read:error'); | ||
var content = data.toString(); | ||
if (content){ | ||
if (content) { | ||
clearInterval(task); | ||
var config = JSON.parse(content); | ||
setTimeout(function(){ | ||
fs.unlink(me.dataPath, function(err){ | ||
var conf = JSON.parse(content); | ||
setTimeout(function () { | ||
fs.unlink(me.dataPath, function (err) { | ||
err && logger.warning('config_cache delete failed' + err.message); | ||
}); | ||
},DELAY_DELETE); | ||
cb && cb(null, config); | ||
}, DELAY_DELETE); | ||
cb && cb(null, conf); | ||
logger.trace('read config_cache succ'); | ||
me.emit('read:succ'); | ||
me.running = false; | ||
}else{ | ||
} | ||
else { | ||
logger.trace('waiting config_cache'); | ||
@@ -86,38 +94,46 @@ me.emit('read:fail'); | ||
} | ||
var task = setInterval(_check, CHECK_INTERVAL); | ||
_check(); | ||
task = setInterval(check, CHECK_INTERVAL); | ||
check(); | ||
}; | ||
Updater.prototype.update = function(cb, all){ | ||
/** | ||
* run interval update | ||
* @param {Function} cb [description] | ||
* @param {boolean} all [description] | ||
*/ | ||
Updater.prototype.update = function (cb, all) { | ||
var me = this; | ||
//make sure every updater won't run multi times | ||
if (me.running){ | ||
cb(new Error("updater still running")); | ||
// make sure every updater won't run multi times | ||
if (me.running) { | ||
cb(new Error('updater still running')); | ||
return; | ||
} | ||
me.running = true; | ||
//create a file to share update result between cluster | ||
fs.open(me.dataPath, 'wx', function(err, fd){ | ||
if (err){ | ||
// create a file to share update result between cluster | ||
fs.open(me.dataPath, 'wx', function (err, fd) { | ||
if (err) { | ||
me.emit('wait'); | ||
me.checkResult(cb); | ||
}else{ | ||
} | ||
else { | ||
logger.trace('config normalize start'); | ||
me.emit('update:start'); | ||
var conf = all? config.getRawConf() : config.getUpdateNeededRawConf(); | ||
var conf = all ? config.getRawConf() : config.getUpdateNeededRawConf(); | ||
var failed = false; | ||
_.map(conf, function(value, key){ | ||
try{ | ||
_.map(conf, function (value, key) { | ||
try { | ||
conf[key] = config.normalize(value); | ||
//add a conf from tag | ||
// add a conf from tag | ||
conf[key].__from__ = 'updater_' + (new Date()).getTime(); | ||
}catch(e){ | ||
} | ||
catch (e) { | ||
logger.fatal('config normalize failed'); | ||
fs.close(fd); | ||
//delete failed file | ||
fs.unlink(me.dataPath, function(err){ | ||
// delete failed file | ||
fs.unlink(me.dataPath, function (err) { | ||
err && logger.warning('config_cache delete failed' + err.message); | ||
}); | ||
cb && cb (e); | ||
cb && cb(e); | ||
me.emit('normalize:fail'); | ||
@@ -130,6 +146,6 @@ me.running = false; | ||
me.emit('update:end'); | ||
!failed && fs.write(fd, JSON.stringify(conf),0 , 'utf-8', function(err){ | ||
!failed && fs.write(fd, JSON.stringify(conf), 0, 'utf-8', function (err) { | ||
err && logger.warning('config_cache write failed ' + err.message); | ||
me.checkResult(cb); | ||
fs.close(fd, function(err){ | ||
fs.close(fd, function (err) { | ||
err && logger.warning('config_cache close failed' + err.message); | ||
@@ -145,2 +161,2 @@ }); | ||
module.exports = updater; | ||
module.exports.Updater = Updater; | ||
module.exports.Updater = Updater; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file config normalizer manager | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -11,14 +12,14 @@ * 2014/8/19 | ||
function NormalizerManager(){ | ||
function NormalizerManager() { | ||
this.normalizers = ['default']; | ||
} | ||
NormalizerManager.prototype.setConfigNormalizer = function(normalizers){ | ||
NormalizerManager.prototype.setConfigNormalizer = function (normalizers) { | ||
this.normalizers = normalizers; | ||
}; | ||
NormalizerManager.prototype.needUpdate = function(config){ | ||
NormalizerManager.prototype.needUpdate = function (config) { | ||
var need = false; | ||
this._travel(config, function(normalizer){ | ||
if (normalizer.needUpdate(config)){ | ||
this._travel(config, function (normalizer) { | ||
if (normalizer.needUpdate(config)) { | ||
need = true; | ||
@@ -30,13 +31,14 @@ } | ||
NormalizerManager.prototype._travel = function(config, func){ | ||
NormalizerManager.prototype._travel = function (config, func) { | ||
var me = this; | ||
this.normalizers.forEach(function(normalizer, index){ | ||
if (typeof normalizer === "string"){ | ||
this.normalizers.forEach(function (normalizer, index) { | ||
if (typeof normalizer === 'string') { | ||
var instance = RalModule.modules.normalizer[normalizer]; | ||
if (instance){ | ||
if (instance) { | ||
me.normalizers[index] = instance; | ||
normalizer = instance; | ||
}else{ | ||
throw new Error('invalid normalizer :' + normalizer) | ||
} | ||
else { | ||
throw new Error('invalid normalizer :' + normalizer); | ||
} | ||
} | ||
@@ -47,4 +49,4 @@ func(normalizer); | ||
NormalizerManager.prototype.apply = function(config){ | ||
this._travel(config, function(normalizer){ | ||
NormalizerManager.prototype.apply = function (config) { | ||
this._travel(config, function (normalizer) { | ||
var serviceID = config.serviceID; | ||
@@ -57,2 +59,2 @@ config = normalizer.normalizeConfig(config); | ||
module.exports = NormalizerManager; | ||
module.exports = NormalizerManager; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -9,7 +10,7 @@ * 2014/8/4 | ||
var logger = require('./logger.js')('Converter'); | ||
// var logger = require('./logger.js')('Converter'); | ||
var RalModule = require('./ralmodule.js'); | ||
var util = require('util'); | ||
function Converter(){ | ||
function Converter() { | ||
RalModule.call(this); | ||
@@ -22,6 +23,7 @@ } | ||
* pack data to stream | ||
* @param data | ||
* @param config | ||
* | ||
* @param {Object} config [description] | ||
* @param {Object} data [description] | ||
*/ | ||
Converter.prototype.pack = function(config, data){ | ||
Converter.prototype.pack = function (config, data) { | ||
throw new Error('Not Implemented'); | ||
@@ -32,9 +34,10 @@ }; | ||
* unpack stream to data | ||
* @param config | ||
* | ||
* @param {Object} config [description] | ||
*/ | ||
Converter.prototype.unpack = function(config){ | ||
Converter.prototype.unpack = function (config) { | ||
throw new Error('Not Implemented'); | ||
}; | ||
Converter.prototype.getCategory = function(){ | ||
Converter.prototype.getCategory = function () { | ||
return 'converter'; | ||
@@ -45,2 +48,2 @@ }; | ||
module.exports = Converter; | ||
module.exports = Converter; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -10,4 +11,3 @@ * 2014/8/4 | ||
module.exports.IDC_ALL = 'all'; | ||
module.exports.currentIDC = module.exports.IDC_ALL; | ||
//default config update time is 60s | ||
module.exports.updateInterval = 60000; | ||
module.exports.currentIDC = module.exports.IDC_ALL; // default config update time is 60s | ||
module.exports.updateInterval = 60000; |
@@ -1,6 +0,8 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
* 2014/8/5 | ||
*/ | ||
/*eslint-env node, mocha */ | ||
@@ -13,3 +15,3 @@ 'use strict'; | ||
function RandomBalance(){ | ||
function RandomBalance() { | ||
Balance.call(this); | ||
@@ -20,18 +22,18 @@ } | ||
RandomBalance.prototype.getName = function(){ | ||
RandomBalance.prototype.getName = function () { | ||
return 'random'; | ||
}; | ||
RandomBalance.prototype.fetchServer = function(balanceContext){ | ||
RandomBalance.prototype.fetchServer = function (balanceContext) { | ||
var servers = balanceContext.reqIDCServers; | ||
if (servers.length === 1){ | ||
if (servers.length === 1) { | ||
return servers[0]; | ||
} | ||
//Math.random takes 250ms on first call | ||
// Math.random takes 250ms on first call | ||
var index = Math.floor(Math.random() * servers.length); | ||
logger.trace('RandomBalance fetchServer index=' + index + ' ServiceID=' + balanceContext.serviceID); | ||
//TODO add server filter | ||
logger.trace('RandomBalance fetchServer index=' + index + ' ServiceID=' + balanceContext.serviceID); | ||
// TODO add server filter | ||
return servers[index]; | ||
}; | ||
module.exports = RandomBalance; | ||
module.exports = RandomBalance; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -13,3 +14,3 @@ * 2014/8/5 | ||
function RoundRobinBalance(){ | ||
function RoundRobinBalance() { | ||
Balance.call(this); | ||
@@ -20,9 +21,9 @@ } | ||
RoundRobinBalance.prototype.getName = function(){ | ||
RoundRobinBalance.prototype.getName = function () { | ||
return 'roundrobin'; | ||
}; | ||
RoundRobinBalance.prototype.fetchServer = function(balanceContext){ | ||
RoundRobinBalance.prototype.fetchServer = function (balanceContext) { | ||
var servers = balanceContext.reqIDCServers; | ||
if (servers.length === 1){ | ||
if (servers.length === 1) { | ||
return servers[0]; | ||
@@ -32,6 +33,10 @@ } | ||
balanceContext.lastRoundRobinID++; | ||
if (balanceContext.lastRoundRobinID < 0 || balanceContext.lastRoundRobinID >= servers.length){ | ||
if (balanceContext.lastRoundRobinID < 0 || balanceContext.lastRoundRobinID >= servers.length) { | ||
balanceContext.lastRoundRobinID = 0; | ||
} | ||
logger.trace('RoundRobinBalance fetchServer RoundRobinID=' + balanceContext.lastRoundRobinID + ' ServiceID=' + balanceContext.serviceID); | ||
logger.trace( | ||
['RoundRobinBalance fetchServer RoundRobinID=', balanceContext.lastRoundRobinID, ' ServiceID=', | ||
balanceContext.serviceID | ||
].join('') | ||
); | ||
return servers[balanceContext.lastRoundRobinID]; | ||
@@ -38,0 +43,0 @@ }; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -10,9 +11,9 @@ * 2014/8/7 | ||
var Converter = require('../../converter.js'); | ||
var logger = require('../../logger.js')('FormDataConverter'); | ||
// var logger = require('../../logger.js')('FormDataConverter'); | ||
var util = require('util'); | ||
var FormData; | ||
var FormDataCls; | ||
var iconv = require('iconv-lite'); | ||
var _ = require('underscore'); | ||
function FormDataConverter(){ | ||
function FormDataConverter() { | ||
Converter.call(this); | ||
@@ -23,17 +24,19 @@ } | ||
FormDataConverter.prototype.unpack = function(config){ | ||
FormDataConverter.prototype.unpack = function (config) { | ||
throw new Error('Not Implemented'); | ||
}; | ||
FormDataConverter.prototype.pack = function(config, data){ | ||
FormDataConverter.prototype.pack = function (config, data) { | ||
data = data || {}; | ||
FormData = FormData || require('form-data'); | ||
var formData = new FormData(); | ||
//only handle fisrt level key-value | ||
_.map(data, function(value, key){ | ||
if (!FormDataCls) { | ||
FormDataCls = require('form-data'); | ||
} | ||
var formData = new FormDataCls(); | ||
// only handle fisrt level key-value | ||
_.map(data, function (value, key) { | ||
var options = null; | ||
if (_.isString(value)){ | ||
if (_.isString(value)) { | ||
value = iconv.encode(value, config.encoding); | ||
} | ||
if (value.options){ | ||
if (value.options) { | ||
options = value.options; | ||
@@ -45,4 +48,4 @@ delete value.options; | ||
config.headers = config.headers || {}; | ||
config.headers['Content-Type'] = "multipart/form-data;boundary=" + formData.getBoundary(); | ||
if (config.syncLength){ | ||
config.headers['Content-Type'] = 'multipart/form-data;boundary=' + formData.getBoundary(); | ||
if (config.syncLength) { | ||
config.headers['Content-Length'] = formData.getLengthSync(); | ||
@@ -53,3 +56,3 @@ } | ||
FormDataConverter.prototype.getName = function(){ | ||
FormDataConverter.prototype.getName = function () { | ||
return 'formdata'; | ||
@@ -61,2 +64,1 @@ }; | ||
module.exports = FormDataConverter; | ||
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -27,3 +28,4 @@ * 2014/8/7 | ||
return object; | ||
} catch (ex) { | ||
} | ||
catch (ex) { | ||
logger.trace('unpack urlencode data failed ServiceID=' + config.serviceID); | ||
@@ -37,7 +39,8 @@ throw ex; | ||
var buffer; | ||
try{ | ||
try { | ||
buffer = urlencode.stringify(data, { | ||
charset: config.encoding | ||
}); | ||
}catch(e){ | ||
} | ||
catch (e) { | ||
logger.trace('pack urlencode data failed data=' + data + ' ServiceID=' + config.serviceID); | ||
@@ -47,5 +50,4 @@ throw e; | ||
config.headers = config.headers || {}; | ||
config.headers['Content-Type'] = | ||
"application/x-www-form-urlencoded; charset=" + | ||
(config.encoding ? config.encoding : 'utf-8'); | ||
var encoding = config.encoding ? config.encoding : 'utf-8'; | ||
config.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=' + encoding; | ||
config.headers['Content-Length'] = buffer.length; | ||
@@ -60,2 +62,2 @@ logger.trace('pack urlencode data succ ServiceID=' + config.serviceID + ' data=' + buffer); | ||
module.exports = FormConverter; | ||
module.exports = FormConverter; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -14,3 +15,3 @@ * 2014/8/5 | ||
function JsonConverter(){ | ||
function JsonConverter() { | ||
Converter.call(this); | ||
@@ -21,3 +22,3 @@ } | ||
JsonConverter.prototype.unpack = function(config, data){ | ||
JsonConverter.prototype.unpack = function (config, data) { | ||
try { | ||
@@ -28,3 +29,4 @@ var str = iconv.decode(data, config.encoding); | ||
return obj; | ||
} catch (ex) { | ||
} | ||
catch (ex) { | ||
logger.trace('unpack json data failed ServiceID=' + config.serviceID); | ||
@@ -35,8 +37,9 @@ throw ex; | ||
JsonConverter.prototype.pack = function(config, data){ | ||
JsonConverter.prototype.pack = function (config, data) { | ||
data = data || {}; | ||
var buffer; | ||
try{ | ||
try { | ||
buffer = iconv.encode(JSON.stringify(data), config.encoding); | ||
}catch(ex){ | ||
} | ||
catch (ex) { | ||
logger.trace('pack json data failed data=' + data + ' ServiceID=' + config.serviceID); | ||
@@ -49,6 +52,6 @@ throw ex; | ||
JsonConverter.prototype.getName = function(){ | ||
JsonConverter.prototype.getName = function () { | ||
return 'json'; | ||
}; | ||
module.exports = JsonConverter; | ||
module.exports = JsonConverter; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -14,3 +15,2 @@ * 2014/8/7 | ||
var ralUtil = require('../../util.js'); | ||
var urlencode = require('urlencode'); | ||
@@ -37,2 +37,2 @@ function QuertStringConverter() { | ||
module.exports = QuertStringConverter; | ||
module.exports = QuertStringConverter; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -11,3 +12,3 @@ * 2014/8/11 | ||
var Converter = require('../../converter.js'); | ||
var logger = require('../../logger.js')('RawConverter'); | ||
// var logger = require('../../logger.js')('RawConverter'); | ||
var util = require('util'); | ||
@@ -33,2 +34,2 @@ | ||
module.exports = RawConverter; | ||
module.exports = RawConverter; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -10,3 +11,3 @@ * 2014/12/23 | ||
var Converter = require('../../converter.js'); | ||
var logger = require('../../logger.js')('StreamConverter'); | ||
// var logger = require('../../logger.js')('StreamConverter'); | ||
var util = require('util'); | ||
@@ -36,2 +37,2 @@ var PassThrough = require('stream').PassThrough; | ||
module.exports = StreamConverter; | ||
module.exports = StreamConverter; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -25,3 +26,4 @@ * 2014/8/5 | ||
str = iconv.decode(data, config.encoding); | ||
} catch (e) { | ||
} | ||
catch (e) { | ||
logger.trace('unpack string data failed data=' + data + ' ServiceID=' + config.serviceID); | ||
@@ -39,3 +41,4 @@ throw e; | ||
buffer = iconv.encode(data.toString(), config.encoding); | ||
} catch (e) { | ||
} | ||
catch (e) { | ||
logger.trace('pack string data failed data=' + data + ' ServiceID=' + config.serviceID); | ||
@@ -52,2 +55,2 @@ throw e; | ||
module.exports = StringConverter; | ||
module.exports = StringConverter; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -23,3 +24,3 @@ * 2014/8/19 | ||
function DefaultConfig(){ | ||
function DefaultConfig() { | ||
ConfigNormalizer.call(this); | ||
@@ -30,6 +31,6 @@ } | ||
DefaultConfig.prototype.normalizeConfig = function(config){ | ||
//inherit default config | ||
_.map(defaultServiceInfo, function(value, key){ | ||
if (config.hasOwnProperty(key) === false){ | ||
DefaultConfig.prototype.normalizeConfig = function (config) { | ||
// inherit default config | ||
_.map(defaultServiceInfo, function (value, key) { | ||
if (config.hasOwnProperty(key) === false) { | ||
config[key] = value; | ||
@@ -43,6 +44,6 @@ } | ||
DefaultConfig.prototype.getName = function(){ | ||
DefaultConfig.prototype.getName = function () { | ||
return 'default'; | ||
}; | ||
module.exports = DefaultConfig; | ||
module.exports = DefaultConfig; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -12,3 +13,2 @@ * 2014/8/5 | ||
var util = require('util'); | ||
var url = require('url'); | ||
var Stream = require('stream').Stream; | ||
@@ -41,11 +41,12 @@ var urlencode = require('urlencode'); | ||
HttpProtocol.prototype._request = function (config, callback) { | ||
var response = new ResponseStream(), | ||
query = urlencode.stringify(config.query, { | ||
charset: config.encoding | ||
}), | ||
path, | ||
piped = false; | ||
var response = new ResponseStream(); | ||
var query = urlencode.stringify(config.query, { | ||
charset: config.encoding | ||
}); | ||
var piped = false; | ||
var path; | ||
if (query) { | ||
path = config.path + '?' + query; | ||
} else { | ||
} | ||
else { | ||
path = config.path; | ||
@@ -59,3 +60,3 @@ } | ||
headers: config.headers, | ||
//disable http pool to avoid connect problem https://github.com/mikeal/request/issues/465 | ||
// disable http pool to avoid connect problem https://github.com/mikeal/request/issues/465 | ||
agent: false | ||
@@ -71,3 +72,4 @@ }; | ||
opt.rejectUnauthorized = config.rejectUnauthorized; | ||
} else { | ||
} | ||
else { | ||
request = require('http'); | ||
@@ -87,4 +89,5 @@ } | ||
req.end(); | ||
} else { | ||
//auto end if no pipe | ||
} | ||
else { | ||
// auto end if no pipe | ||
process.nextTick(function () { | ||
@@ -109,3 +112,3 @@ piped || req.end(); | ||
ResponseStream.prototype.write = function (chunk) { | ||
//store the data | ||
// store the data | ||
this.chunks.push(chunk); | ||
@@ -120,3 +123,4 @@ }; | ||
logger.trace('response end'); | ||
} catch (ex) { | ||
} | ||
catch (ex) { | ||
logger.trace('response failed errmsg=' + ex.message); | ||
@@ -126,3 +130,3 @@ this.emit('error', ex); | ||
} | ||
//emit data at once | ||
// emit data at once | ||
this.emit('data', data); | ||
@@ -132,2 +136,2 @@ this.emit('end'); | ||
module.exports = HttpProtocol; | ||
module.exports = HttpProtocol; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -10,3 +11,3 @@ * 2014/12/24 | ||
var Protocol = require('../../protocol.js'); | ||
var logger = require('../../logger.js')('SoapProtocol'); | ||
// var logger = require('../../logger.js')('SoapProtocol'); | ||
var util = require('util'); | ||
@@ -17,3 +18,3 @@ var soap = require('soap'); | ||
function SoapProtocol(){ | ||
function SoapProtocol() { | ||
Protocol.call(this); | ||
@@ -24,11 +25,11 @@ } | ||
SoapProtocol.prototype.getName = function(){ | ||
SoapProtocol.prototype.getName = function () { | ||
return 'soap'; | ||
}; | ||
SoapProtocol.prototype.normalizeConfig = SoapProtocol.normalizeConfig = function(config){ | ||
SoapProtocol.prototype.normalizeConfig = SoapProtocol.normalizeConfig = function (config) { | ||
return config; | ||
}; | ||
SoapProtocol.prototype._request = function(config, callback){ | ||
SoapProtocol.prototype._request = function (config, callback) { | ||
var abort = false; | ||
@@ -44,15 +45,19 @@ var req = new EventEmitter(); | ||
function request(client){ | ||
function request(client) { | ||
callback && callback(res); | ||
if (config.payload === undefined){ | ||
if (config.payload === undefined) { | ||
req.emit('error', new Error('soap protocol doesn\'t support stream converter')); | ||
} | ||
if (config.soapService && config.soapPort){ | ||
if (config.soapService && config.soapPort) { | ||
client = client[config.soapService][config.soapPort]; | ||
} | ||
client[config.method].call(client, config.payload, function(err, result){ | ||
if (abort) | ||
client[config.method].call(client, config.payload, function (err, result) { | ||
if (abort) { | ||
return; | ||
if (err) return res.emit('error', err); | ||
} | ||
if (err) { | ||
res.emit('error', err); | ||
return; | ||
} | ||
res.emit('data', result); | ||
@@ -63,6 +68,6 @@ res.emit('end'); | ||
if (!clientCache[config.wsdl] || config.noClientCache){ | ||
soap.createClient(config.wsdl, config.options || {}, function(err, client){ | ||
if (err){ | ||
process.nextTick(function(){ | ||
if (!clientCache[config.wsdl] || config.noClientCache) { | ||
soap.createClient(config.wsdl, config.options || {}, function (err, client) { | ||
if (err) { | ||
process.nextTick(function () { | ||
req.emit('error', err); | ||
@@ -72,6 +77,6 @@ }); | ||
} | ||
if (config.security){ | ||
if (config.security) { | ||
client.setSecurity(config.security); | ||
} | ||
if (config.headers){ | ||
if (config.headers) { | ||
if (config.headers instanceof Array === false) { | ||
@@ -86,7 +91,8 @@ config.headers = [config.headers]; | ||
}); | ||
}else{ | ||
} | ||
else { | ||
request(clientCache[config.wsdl]); | ||
} | ||
req.abort = function(){ | ||
req.abort = function () { | ||
abort = true; | ||
@@ -97,2 +103,2 @@ }; | ||
module.exports = SoapProtocol; | ||
module.exports = SoapProtocol; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral logger | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -7,2 +8,4 @@ * 2014/8/4 | ||
/*eslint-disable fecs-camelcase, camelcase */ | ||
'use strict'; | ||
@@ -13,35 +16,50 @@ | ||
var cluster = require('cluster'); | ||
var clusterID = (cluster.isWorker ? cluster.worker.id: 'main'); | ||
var clusterID = (cluster.isWorker ? cluster.worker.id : 'main'); | ||
var options = { | ||
'format_wf' : '%L: %t [%f:%N] errno[%E] logId[%l] uri[%U] user[%u] refer[%{referer}i] cookie[%{cookie}i] %S %M', | ||
'log_path' : __dirname + path.sep + '../logs', | ||
'app' : 'yog-ral' | ||
format_wf: '%L: %t [%f:%N] errno[%E] logId[%l] uri[%U] user[%u] refer[%{referer}i] cookie[%{cookie}i] %S %M', | ||
log_path: path.join(__dirname, '../logs'), | ||
app: 'yog-ral' | ||
}; | ||
module.exports = function (prefix) { | ||
prefix = '[cluster '+ clusterID +']['+prefix + "] "; | ||
prefix = '[cluster ' + clusterID + '][' + prefix + '] '; | ||
return { | ||
notice: function (msg) { | ||
if (options.disable) return; | ||
logger.getLogger(options).notice('[yog-ral] ' + prefix + Array.prototype.slice.call(arguments, 0).join('')); | ||
if (options.disable) { | ||
return; | ||
} | ||
var content = Array.prototype.slice.call(arguments, 0).join(''); | ||
logger.getLogger(options).notice('[yog-ral] ' + prefix + content); | ||
}, | ||
warning: function (msg) { | ||
if (options.disable) return; | ||
logger.getLogger(options).warning('[yog-ral] ' + prefix + Array.prototype.slice.call(arguments, 0).join('')); | ||
if (options.disable) { | ||
return; | ||
} | ||
var content = Array.prototype.slice.call(arguments, 0).join(''); | ||
logger.getLogger(options).warning('[yog-ral] ' + prefix + content); | ||
}, | ||
fatal: function (msg) { | ||
if (options.disable) return; | ||
logger.getLogger(options).fatal('[yog-ral] ' + prefix + Array.prototype.slice.call(arguments, 0).join('')); | ||
if (options.disable) { | ||
return; | ||
} | ||
var content = Array.prototype.slice.call(arguments, 0).join(''); | ||
logger.getLogger(options).fatal('[yog-ral] ' + prefix + content); | ||
}, | ||
trace: function () { | ||
if (options.disable) return; | ||
logger.getLogger(options).trace('[yog-ral] ' + prefix + Array.prototype.slice.call(arguments, 0).join('')); | ||
if (options.disable) { | ||
return; | ||
} | ||
var content = Array.prototype.slice.call(arguments, 0).join(''); | ||
logger.getLogger(options).trace('[yog-ral] ' + prefix + content); | ||
}, | ||
debug: function (msg) { | ||
if (options.disable) return; | ||
logger.getLogger(options).debug('[yog-ral] ' + prefix + Array.prototype.slice.call(arguments, 0).join('')); | ||
if (options.disable) { | ||
return; | ||
} | ||
var content = Array.prototype.slice.call(arguments, 0).join(''); | ||
logger.getLogger(options).debug('[yog-ral] ' + prefix + content); | ||
} | ||
@@ -51,4 +69,4 @@ }; | ||
module.exports.__defineSetter__('options', function(opts){ | ||
module.exports.__defineSetter__('options', function (opts) { | ||
options = opts; | ||
}); |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral middleware | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -19,2 +20,2 @@ * 2014/9/4 | ||
}; | ||
}; | ||
}; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file ral protocol base class | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -9,7 +10,7 @@ * 2014/8/4 | ||
var logger = require('./logger.js')('Protocol'); | ||
// var logger = require('./logger.js')('Protocol'); | ||
var RalModule = require('./ralmodule.js'); | ||
var util = require('util'); | ||
function Procotol(){ | ||
function Procotol() { | ||
RalModule.call(this); | ||
@@ -20,3 +21,3 @@ } | ||
Procotol.prototype.normalizeConfig = Procotol.normalizeConfig = function(context){ | ||
Procotol.prototype.normalizeConfig = Procotol.normalizeConfig = function (context) { | ||
return context; | ||
@@ -28,10 +29,12 @@ }; | ||
* the callback will return a response stream for unpack | ||
* @param config | ||
* @param callback | ||
* | ||
* @param {Object} config [description] | ||
* @param {Function} callback [description] | ||
* @return {Object} [description] | ||
*/ | ||
Procotol.prototype.talk = function(config, callback){ | ||
Procotol.prototype.talk = function (config, callback) { | ||
return this._request(config, callback); | ||
}; | ||
Procotol.prototype.getCategory = function(){ | ||
Procotol.prototype.getCategory = function () { | ||
return 'protocol'; | ||
@@ -42,6 +45,7 @@ }; | ||
* actual request function, need to be implemented | ||
* @param config | ||
* @private | ||
* | ||
* @param {Object} config [description] | ||
* @param {Function} callback [description] | ||
*/ | ||
Procotol.prototype._request = function(config, callback){ | ||
Procotol.prototype._request = function (config, callback) { | ||
throw new Error('Not Implemented'); | ||
@@ -48,0 +52,0 @@ }; |
173
lib/ral.js
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file ral request core | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -7,16 +8,18 @@ * 2014/8/7 | ||
/*eslint-disable fecs-camelcase, camelcase */ | ||
'use strict'; | ||
var ralUtil = require('./util.js'), | ||
util = require('util'), | ||
ctx = require('./ctx.js'), | ||
loggerGen = require('./logger.js'), | ||
logger = require('./logger.js')('RAL'), | ||
config = require('./config.js'), | ||
RalModule = require('./ralmodule.js'), | ||
iconv = require('iconv-lite'), | ||
EventEmitter = require('events').EventEmitter, | ||
now = require('performance-now'), | ||
Timer = require('./timer.js'), | ||
stream = require('stream'), | ||
path = require('path'); | ||
var ralUtil = require('./util.js'); | ||
var util = require('util'); | ||
var ctx = require('./ctx.js'); | ||
var loggerGen = require('./logger.js'); | ||
var logger = require('./logger.js')('RAL'); | ||
var config = require('./config.js'); | ||
var RalModule = require('./ralmodule.js'); | ||
var iconv = require('iconv-lite'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var now = require('performance-now'); | ||
var Timer = require('./timer.js'); | ||
var stream = require('stream'); | ||
var path = require('path'); | ||
@@ -29,2 +32,8 @@ iconv.extendNodeEncodings(); | ||
/** | ||
* ral request runner | ||
* | ||
* @param {string} serviceName [description] | ||
* @param {Object} options [description] | ||
*/ | ||
function RalRunner(serviceName, options) { | ||
@@ -39,3 +48,3 @@ var me = this; | ||
options = options || {}; | ||
var payload, request; | ||
var payload; | ||
var timer = this.timer = new Timer(['request', 'talk', 'pack', 'write', 'read', 'unpack']); | ||
@@ -49,5 +58,6 @@ timer.start('request'); | ||
}); | ||
} else { | ||
} | ||
else { | ||
var context = conf.context = config.getContext(serviceName, options); | ||
//normalize conf and options for merge to request options | ||
// normalize conf and options for merge to request options | ||
context.protocol.normalizeConfig(conf); | ||
@@ -62,3 +72,4 @@ context.protocol.normalizeConfig(options); | ||
}); | ||
} else { | ||
} | ||
else { | ||
this._retryTimes++; | ||
@@ -78,8 +89,33 @@ timer.start('request'); | ||
/** | ||
* node-ral request handler | ||
* | ||
*/ | ||
RalRunner.prototype.doRequest = function () { | ||
var timer = this.timer, | ||
context = this.conf.context, | ||
conf = this.conf, | ||
unpack, payload, request, response, abort = false, | ||
me = this; | ||
var timer = this.timer; | ||
var context = this.conf.context; | ||
var conf = this.conf; | ||
/** | ||
* unpack stream | ||
*/ | ||
var unpack; | ||
/** | ||
* request body | ||
*/ | ||
var payload; | ||
/** | ||
* request writeable stream | ||
*/ | ||
var request; | ||
/** | ||
* response readable stream | ||
*/ | ||
var response; | ||
/** | ||
* abort flag | ||
* | ||
* @type {Boolean} | ||
*/ | ||
var abort = false; | ||
var me = this; | ||
@@ -103,3 +139,3 @@ function onError(err) { | ||
function onEnd() { | ||
//store request time when response end | ||
// store request time when response end | ||
me.emit('end'); | ||
@@ -109,3 +145,3 @@ } | ||
function onData(data) { | ||
//prevent data invoked after abort | ||
// prevent data invoked after abort | ||
if (!abort) { | ||
@@ -118,6 +154,8 @@ clearTimeout(me.timeout); | ||
me.emit('data', data); | ||
} catch (err) { | ||
} | ||
catch (err) { | ||
onError(err); | ||
} | ||
} else { | ||
} | ||
else { | ||
me.emit('data', data); | ||
@@ -131,7 +169,7 @@ } | ||
abort = true; | ||
//end timer | ||
// end timer | ||
timer.end('write'); | ||
timer.end('talk'); | ||
timer.end('request'); | ||
//remove event listen | ||
// remove event listen | ||
if (unpack && unpack.removeAllListeners) { | ||
@@ -143,3 +181,3 @@ unpack.removeAllListeners(); | ||
if (request) { | ||
//end stream | ||
// end stream | ||
request.abort(); | ||
@@ -176,3 +214,4 @@ request.removeAllListeners('error'); | ||
response.pipe(unpack); | ||
} else { | ||
} | ||
else { | ||
response.on('data', function (data) { | ||
@@ -183,3 +222,4 @@ try { | ||
timer.end('unpack'); | ||
} catch (ex) { | ||
} | ||
catch (ex) { | ||
callRetry(ex); | ||
@@ -206,7 +246,7 @@ return; | ||
}); | ||
//pipe the response stream to unpack stream | ||
// pipe the response stream to unpack stream | ||
unpackResponse(); | ||
//transport error event from unpack | ||
// transport error event from unpack | ||
response.on('error', callRetry); | ||
//store request time when response end | ||
// store request time when response end | ||
response.once('end', onEnd); | ||
@@ -216,9 +256,10 @@ } | ||
timer.start('talk'); | ||
//need pack data first to make sure the context which handled by converter can be passed into protocol | ||
// need pack data first to make sure the context which handled by converter can be passed into protocol | ||
timer.start('pack'); | ||
if (conf.data) { | ||
//create a pack converter stream | ||
// create a pack converter stream | ||
try { | ||
payload = context.pack(conf, conf.data); | ||
} catch (err) { | ||
} | ||
catch (err) { | ||
onError(err); | ||
@@ -230,3 +271,3 @@ return; | ||
//set payload directly when converter is not streamify | ||
// set payload directly when converter is not streamify | ||
if (context.packConverter.isStreamify === false) { | ||
@@ -236,13 +277,14 @@ conf.payload = payload; | ||
//choose a real server | ||
// choose a real server | ||
conf.server = context.balance.fetchServer(context.balanceContext); | ||
//set timeout | ||
// set timeout | ||
this.timeout = setTimeout(onTimeout, conf.timeout); | ||
//create a request stream | ||
// create a request stream | ||
timer.start('write'); | ||
try { | ||
request = this.request = context.protocol.talk(conf, onResp); | ||
} catch (err) { | ||
} | ||
catch (err) { | ||
onError(err); | ||
@@ -258,3 +300,3 @@ return; | ||
} | ||
//transport error event from pack | ||
// transport error event from pack | ||
payload.on('error', onError); | ||
@@ -308,7 +350,7 @@ payload.pipe(request); | ||
confDir: null, | ||
extDir: [__dirname + path.sep + '/ext'], | ||
extDir: [path.join(__dirname, '/ext')], | ||
logger: { | ||
"log_path": path.dirname(require.main ? require.main.filename : __dirname) + path.sep + './logs', | ||
"IS_OMP": 2, | ||
"app": "ral" | ||
log_path: path.dirname(require.main ? require.main.filename : __dirname) + path.sep + './logs', | ||
IS_OMP: 2, | ||
app: 'ral' | ||
}, | ||
@@ -318,6 +360,16 @@ currentIDC: 'all' | ||
RAL.appendExtPath = function (path) { | ||
defaultOptions.extDir.push(path); | ||
/** | ||
* append extesion module path | ||
* | ||
* @param {string} extPath [description] | ||
*/ | ||
RAL.appendExtPath = function (extPath) { | ||
defaultOptions.extDir.push(extPath); | ||
}; | ||
/** | ||
* set config normalizer | ||
* | ||
* @param {Array} normalizers [description] | ||
*/ | ||
RAL.setConfigNormalizer = function (normalizers) { | ||
@@ -327,2 +379,8 @@ config.normalizerManager.setConfigNormalizer(normalizers); | ||
/** | ||
* get normlized config | ||
* | ||
* @param {string} name [description] | ||
* @return {Object} [description] | ||
*/ | ||
RAL.getConf = function (name) { | ||
@@ -332,2 +390,8 @@ return config.getConf(name); | ||
/** | ||
* get raw conf | ||
* | ||
* @param {string} name [description] | ||
* @return {Object} [description] | ||
*/ | ||
RAL.getRawConf = function (name) { | ||
@@ -337,2 +401,7 @@ return config.getRawConf(name); | ||
/** | ||
* init ral instance | ||
* | ||
* @param {Object} options [description] | ||
*/ | ||
RAL.init = function (options) { | ||
@@ -347,2 +416,2 @@ options = ralUtil.merge(defaultOptions, options); | ||
module.exports = RAL; | ||
module.exports = RAL; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file ral module base class | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -9,3 +10,3 @@ * 2014/8/5 | ||
var util = require('util'); | ||
// var util = require('util'); | ||
var _ = require('underscore'); | ||
@@ -16,4 +17,3 @@ var logger = require('./logger.js')('RalModule'); | ||
function RalModule(){ | ||
} | ||
function RalModule() {} | ||
@@ -25,27 +25,37 @@ RalModule.modules = {}; | ||
*/ | ||
RalModule.prototype.getName = function(){ | ||
RalModule.prototype.getName = function () { | ||
throw new Error('Not Implemented'); | ||
}; | ||
RalModule.prototype.getCategory = function(){ | ||
/** | ||
* get module category | ||
*/ | ||
RalModule.prototype.getCategory = function () { | ||
throw new Error('Not Implemented'); | ||
}; | ||
RalModule.load = function(pathOrModule){ | ||
/** | ||
* load modules from path | ||
* | ||
* @param {string} pathOrModule [description] | ||
*/ | ||
RalModule.load = function (pathOrModule) { | ||
function loadFile(filePath){ | ||
function loadFile(filePath) { | ||
var ext = path.extname(filePath); | ||
if (ext === '.js') { | ||
logger.trace('load module from ' +filePath); | ||
var moduleClass = require(filePath); | ||
loadModule(new moduleClass()); | ||
logger.trace('load module from ' + filePath); | ||
var ModuleClass = require(filePath); | ||
loadModule(new ModuleClass()); | ||
} | ||
} | ||
function loadModule(module){ | ||
var catagory, name; | ||
function loadModule(module) { | ||
var catagory; | ||
var name; | ||
try { | ||
catagory = module.getCategory(); | ||
name = module.getName(); | ||
}catch(e){ | ||
} | ||
catch (e) { | ||
logger.warning('[' + pathOrModule + '] is skiped since not RalModule'); | ||
@@ -55,3 +65,3 @@ return; | ||
RalModule.modules[catagory] = RalModule.modules[catagory] || {}; | ||
if (RalModule.modules[catagory][name]){ | ||
if (RalModule.modules[catagory][name]) { | ||
logger.warning('module ' + name + ' override'); | ||
@@ -62,8 +72,10 @@ } | ||
if (_.isString(pathOrModule)){ | ||
if (_.isString(pathOrModule)) { | ||
var files = ralUtil.readdirSync(pathOrModule); | ||
files.map(loadFile); | ||
}else if (pathOrModule.getCategory && pathOrModule.getName){ | ||
} | ||
else if (pathOrModule.getCategory && pathOrModule.getName) { | ||
loadModule(pathOrModule); | ||
}else{ | ||
} | ||
else { | ||
logger.trace('[' + pathOrModule + '] is skiped since not RalModule'); | ||
@@ -73,2 +85,2 @@ } | ||
module.exports = RalModule; | ||
module.exports = RalModule; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file timer | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -11,6 +12,6 @@ * 2014/8/11 | ||
function Timer(preset){ | ||
function Timer(preset) { | ||
this.context = {}; | ||
var me = this; | ||
preset.forEach(function(key){ | ||
preset.forEach(function (key) { | ||
me.context[key] = { | ||
@@ -22,10 +23,22 @@ cost: 0 | ||
Timer.prototype.start = function(name){ | ||
this.context[name] = this.context[name] || {cost : 0}; | ||
/** | ||
* start timer for a subjet | ||
* | ||
* @param {string} name [description] | ||
*/ | ||
Timer.prototype.start = function (name) { | ||
this.context[name] = this.context[name] || { | ||
cost: 0 | ||
}; | ||
this.context[name].start = now(); | ||
}; | ||
Timer.prototype.end = function(name, start){ | ||
/** | ||
* end subject timer | ||
* | ||
* @param {string} name [description] | ||
*/ | ||
Timer.prototype.end = function (name) { | ||
var timer = this.context[name]; | ||
if (timer){ | ||
if (timer) { | ||
timer.end = now(); | ||
@@ -36,2 +49,2 @@ timer.cost = timer.end - timer.start; | ||
module.exports = Timer; | ||
module.exports = Timer; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file util | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -13,8 +14,9 @@ * 2014/8/8 | ||
util.merge = function(source, target){ | ||
if(_.isObject(source) && _.isObject(target)){ | ||
_.map(target, function(value, key){ | ||
util.merge = function (source, target) { | ||
if (_.isObject(source) && _.isObject(target)) { | ||
_.map(target, function (value, key) { | ||
source[key] = util.merge(source[key], value); | ||
}); | ||
} else { | ||
} | ||
else { | ||
source = target; | ||
@@ -30,21 +32,21 @@ } | ||
* aware of DateTime won't be parse well when using this method | ||
* @param object | ||
* @returns {*} | ||
* @param {Object} object [description] | ||
* @return {Object} [description] | ||
*/ | ||
util.deepClone = function(object){ | ||
return JSON.parse(JSON.stringify(object)) | ||
util.deepClone = function (object) { | ||
return JSON.parse(JSON.stringify(object)); | ||
}; | ||
util.qs = function(object){ | ||
util.qs = function (object) { | ||
var content = []; | ||
_.map(object, function(value, key){ | ||
content.push(key+'='+value); | ||
_.map(object, function (value, key) { | ||
content.push(key + '=' + value); | ||
}); | ||
return content.join(' ') | ||
return content.join(' '); | ||
}; | ||
util.readdirSync = function(dir) { | ||
util.readdirSync = function (dir) { | ||
var results = []; | ||
var list = fs.readdirSync(dir); | ||
list.forEach(function(file) { | ||
list.forEach(function (file) { | ||
file = dir + '/' + file; | ||
@@ -56,6 +58,6 @@ var stat = fs.statSync(file); | ||
else { | ||
results.push(file) | ||
results.push(file); | ||
} | ||
}); | ||
return results | ||
return results; | ||
}; |
133
package.json
{ | ||
"name": "node-ral", | ||
"version": "0.0.35", | ||
"version": "0.0.36", | ||
"description": "a rpc client for node", | ||
@@ -8,5 +8,5 @@ "main": "index.js", | ||
"pub": "npm publish --registry=http://registry.npmjs.org", | ||
"test": "mocha -t 20000 --reporter dot --check-leaks test/", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot -t 20000 --check-leaks test/", | ||
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks -t 20000 --slow 0 test/" | ||
"test": "mocha -t 200000 --reporter dot --check-leaks test/", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot -t 200000 --check-leaks test/", | ||
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks -t 200000 --slow 0 test/" | ||
}, | ||
@@ -27,2 +27,123 @@ "repository": { | ||
}, | ||
"eslintConfig": { | ||
"env": { | ||
"browser": true, | ||
"amd": true, | ||
"jasmine": true, | ||
"node": true | ||
}, | ||
"globals": { | ||
"baidu": true, | ||
"T": true, | ||
"$": true, | ||
"jQuery": true, | ||
"goog": true, | ||
"fis": true, | ||
"angular": true, | ||
"setImmediate": true, | ||
"ActiveXObject": true, | ||
"unescape": true, | ||
"escape": true | ||
}, | ||
"rules": { | ||
"no-console": 0, | ||
"no-constant-condition": 1, | ||
"no-comma-dangle": 2, | ||
"no-debugger": 2, | ||
"no-dupe-keys": 1, | ||
"no-empty-class": 2, | ||
"no-ex-assign": 2, | ||
"no-extra-boolean-cast": 1, | ||
"no-extra-semi": 2, | ||
"no-func-assign": 1, | ||
"no-inner-declarations": 1, | ||
"no-invalid-regexp": 2, | ||
"no-negated-in-lhs": 2, | ||
"no-obj-calls": 2, | ||
"no-sparse-arrays": 2, | ||
"no-unreachable": 2, | ||
"use-isnan": 2, | ||
"handle-callback-err": 0, | ||
"valid-typeof": 2, | ||
"curly": [2, "all"], | ||
"eqeqeq": [2, "allow-null"], | ||
"guard-for-in": 1, | ||
"no-else-return": 1, | ||
"no-empty-label": 1, | ||
"no-eval": 1, | ||
"no-extend-native": 2, | ||
"no-extra-bind": 1, | ||
"no-implied-eval": 1, | ||
"no-iterator": 2, | ||
"no-irregular-whitespace": 1, | ||
"no-lone-blocks": 1, | ||
"no-loop-func": 1, | ||
"no-multi-str": 1, | ||
"no-native-reassign": 2, | ||
"no-new-wrappers": 2, | ||
"no-octal": 1, | ||
"no-octal-escape": 1, | ||
"no-proto": 2, | ||
"no-redeclare": 1, | ||
"no-self-compare": 2, | ||
"no-unneeded-ternary": 2, | ||
"no-with": 1, | ||
"radix": 2, | ||
"wrap-iife": [2, "any"], | ||
"no-delete-var": 1, | ||
"no-label-var": 1, | ||
"no-shadow-restricted-names": 2, | ||
"no-undef": 2, | ||
"no-undef-init": 1, | ||
"no-unused-vars": [1, { | ||
"vars": "local", | ||
"args": "none" | ||
}], | ||
"no-use-before-define": [2, "nofunc"], | ||
"brace-style": [1, "stroustrup", {}], | ||
"comma-spacing": [2, { | ||
"before": false, | ||
"after": true | ||
}], | ||
"comma-style": [2, "last"], | ||
"new-cap": 2, | ||
"new-parens": 1, | ||
"no-array-constructor": 2, | ||
"no-new-object": 2, | ||
"no-spaced-func": 2, | ||
"no-space-before-semi": 2, | ||
"no-trailing-spaces": 2, | ||
"no-wrap-func": 2, | ||
"no-mixed-spaces-and-tabs": 2, | ||
"one-var": [2, "never"], | ||
"operator-linebreak": [2, "before"], | ||
"quotes": [2, "single"], | ||
"semi": [2, "always"], | ||
"space-after-keywords": [2, "always"], | ||
"space-before-function-parentheses": [2, { | ||
"anonymous": "always", | ||
"named": "never" | ||
}], | ||
"space-before-blocks": [2, "always"], | ||
"space-in-brackets": [2, "never"], | ||
"space-in-parens": [2, "never"], | ||
"space-return-throw-case": 1, | ||
"space-unary-ops": 1, | ||
"spaced-line-comment": [2, "always", { | ||
"exceptions": ["-", "+", ""] | ||
}], | ||
"max-nested-callbacks": [1, 5], | ||
"no-underscore-dangle": 0, | ||
"no-unused-expressions": 0, | ||
"max-depth": [1, 6], | ||
"max-len": [2, 120, 4], | ||
"max-params": [1, 6] | ||
} | ||
}, | ||
"homepage": "https://github.com/fex-team/node-ral", | ||
@@ -38,3 +159,3 @@ "dependencies": { | ||
"urlencode": "0.2.0", | ||
"yog-log": "0.0.10" | ||
"yog-log": "0.0.16" | ||
}, | ||
@@ -49,2 +170,2 @@ "devDependencies": { | ||
} | ||
} | ||
} |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -7,11 +8,14 @@ * 2014/8/4 | ||
/*eslint-env node, mocha */ | ||
/* eslint-disable no-wrap-func, max-nested-callbacks */ | ||
'use strict'; | ||
var should = require('should'); | ||
var balance = require('../lib/balance.js'); | ||
var Balance = require('../lib/balance.js'); | ||
var config = require('../lib/config.js'); | ||
var path = require('path'); | ||
var ctx = require('../lib/ctx.js'); | ||
var BalanceContext = balance.BalanceContext; | ||
var Balance = balance; | ||
var BalanceContext = Balance.BalanceContext; | ||
var RandomBalance = require('../lib/ext/balance/random.js'); | ||
@@ -21,6 +25,8 @@ var RoundRobinBalance = require('../lib/ext/balance/roundrobin.js'); | ||
describe('balance', function(){ | ||
it('should fail when get name', function(){ | ||
describe('balance', function () { | ||
it('should fail when get name', function () { | ||
var balance = new Balance(); | ||
(function(){balance.getName();}).should.be.throw(/Not Implemented/); | ||
(function () { | ||
balance.getName(); | ||
}).should.be.throw(/Not Implemented/); | ||
}); | ||
@@ -33,3 +39,3 @@ | ||
it('should get context class', function(){ | ||
it('should get context class', function () { | ||
var balance = new Balance(); | ||
@@ -41,6 +47,6 @@ balance.getContextClass().should.be.equal(BalanceContext); | ||
describe('balance context', function() { | ||
describe('balance context', function () { | ||
it('get server by all idc', function() { | ||
var conf = config.load(__dirname + path.sep + './config/idc_config.js'); | ||
it('get server by all idc', function () { | ||
var conf = config.load(path.join(__dirname, './config/idc_config.js')); | ||
var context = new BalanceContext('bookService', conf.bookService); | ||
@@ -50,4 +56,4 @@ context.reqIDCServers.should.eql(conf.bookService.server); | ||
it('get server by tc idc', function() { | ||
var conf = config.load(__dirname + path.sep + './config/idc_config.js'); | ||
it('get server by tc idc', function () { | ||
var conf = config.load(path.join(__dirname, './config/idc_config.js')); | ||
ctx.currentIDC = 'tc'; | ||
@@ -61,4 +67,4 @@ var context = new BalanceContext('bookService', conf.bookService); | ||
it('server conf without idc can use for all idc', function() { | ||
var conf = config.load(__dirname + path.sep + './config/idc_config.js'); | ||
it('server conf without idc can use for all idc', function () { | ||
var conf = config.load(path.join(__dirname, './config/idc_config.js')); | ||
ctx.currentIDC = 'tc'; | ||
@@ -70,4 +76,4 @@ var context = new BalanceContext('bookService2', conf.bookService2); | ||
it('service conf with hybird mode can use all server', function() { | ||
var conf = config.load(__dirname + path.sep + './config/idc_config.js'); | ||
it('service conf with hybird mode can use all server', function () { | ||
var conf = config.load(path.join(__dirname, './config/idc_config.js')); | ||
ctx.currentIDC = 'st'; | ||
@@ -79,4 +85,4 @@ var context = new BalanceContext('bookService3', conf.bookService3); | ||
describe('random balance', function() { | ||
it('has right name', function(){ | ||
describe('random balance', function () { | ||
it('has right name', function () { | ||
var balance = new RandomBalance(); | ||
@@ -91,3 +97,3 @@ balance.getName().should.be.equal('random'); | ||
it('has right context class', function(){ | ||
it('has right context class', function () { | ||
var converter = new RandomBalance(); | ||
@@ -97,8 +103,8 @@ converter.getContextClass().should.be.equal(BalanceContext); | ||
it('get server by random', function() { | ||
var conf = config.load(__dirname + path.sep + './config/idc_config.js'); | ||
it('get server by random', function () { | ||
var conf = config.load(path.join(__dirname, './config/idc_config.js')); | ||
ctx.currentIDC = 'tc'; | ||
var context = new BalanceContext('bookService3', conf.bookService3); | ||
var balance = new RandomBalance(); | ||
Math.random = function(){ | ||
Math.random = function () { | ||
return 0.1; | ||
@@ -111,4 +117,4 @@ }; | ||
it('get server by random for real random', function() { | ||
var conf = config.load(__dirname + path.sep + './config/idc_config.js'); | ||
it('get server by random for real random', function () { | ||
var conf = config.load(path.join(__dirname, './config/idc_config.js')); | ||
ctx.currentIDC = 'tc'; | ||
@@ -121,11 +127,13 @@ var context = new BalanceContext('bookService3', conf.bookService3); | ||
it('direct use single server', function() { | ||
var conf = config.load(__dirname + path.sep + './config/singleserver_config.js'); | ||
it('direct use single server', function () { | ||
var conf = config.load(path.join(__dirname, './config/singleserver_config.js')); | ||
ctx.currentIDC = 'st'; | ||
var context = new BalanceContext('bookService', conf.bookService); | ||
var balance = new RandomBalance(); | ||
Math.random = function(){ | ||
Math.random = function () { | ||
throw new Error(); | ||
}; | ||
(function(){balance.fetchServer(context);}).should.not.throwError(); | ||
(function () { | ||
balance.fetchServer(context); | ||
}).should.not.throwError(); | ||
var server = balance.fetchServer(context); | ||
@@ -137,4 +145,4 @@ Math.random = SourceRandom; | ||
describe('roundrobin balance', function() { | ||
it('has right name', function(){ | ||
describe('roundrobin balance', function () { | ||
it('has right name', function () { | ||
var balance = new RoundRobinBalance(); | ||
@@ -149,3 +157,3 @@ balance.getName().should.be.equal('roundrobin'); | ||
it('has right context class', function(){ | ||
it('has right context class', function () { | ||
var converter = new RoundRobinBalance(); | ||
@@ -155,4 +163,4 @@ converter.getContextClass().should.be.equal(BalanceContext); | ||
it('get server by roundrobin', function() { | ||
var conf = config.load(__dirname + path.sep + './config/idc_config.js'); | ||
it('get server by roundrobin', function () { | ||
var conf = config.load(path.join(__dirname, './config/idc_config.js')); | ||
var context = new BalanceContext('bookService3', conf.bookService3); | ||
@@ -168,4 +176,4 @@ var balance = new RoundRobinBalance(); | ||
it('direct use single server', function() { | ||
var conf = config.load(__dirname + path.sep + './config/singleserver_config.js'); | ||
it('direct use single server', function () { | ||
var conf = config.load(path.join(__dirname, './config/singleserver_config.js')); | ||
ctx.currentIDC = 'tc'; | ||
@@ -178,2 +186,2 @@ var context = new BalanceContext('bookService', conf.bookService); | ||
}); | ||
}); | ||
}); |
@@ -0,1 +1,10 @@ | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
* 2014/8/5 | ||
*/ | ||
/* eslint-disable no-wrap-func */ | ||
'use strict'; | ||
@@ -9,79 +18,100 @@ | ||
RalModule.load(__dirname + path.sep + '../lib/ext'); | ||
RalModule.load(path.join(__dirname, '../lib/ext')); | ||
describe('config loadRawConfr', function() { | ||
describe('config loadRawConfr', function () { | ||
it('loadRawConf right config', function() { | ||
var right_conf = require('./config/right_config.js'); | ||
(function(){config.loadRawConf(right_conf);}).should.not.throw(); | ||
it('loadRawConf right config', function () { | ||
var rightConf = require('./config/right_config.js'); | ||
(function () { | ||
config.loadRawConf(rightConf); | ||
}).should.not.throw(); | ||
}); | ||
it('loadRawConf config without balance', function() { | ||
var wrong_conf = require('./config/wrong_config.js').without_balance; | ||
(function(){config.loadRawConf(wrong_conf);}).should.throw(/balance/); | ||
it('loadRawConf config without balance', function () { | ||
var wrongConf = require('./config/wrong_config.js').without_balance; | ||
(function () { | ||
config.loadRawConf(wrongConf); | ||
}).should.throw(/balance/); | ||
}); | ||
// it('loadRawConf config without unpack', function() { | ||
// var wrong_conf = require('./config/wrong_config.js').without_unpack; | ||
// (function(){config.loadRawConf(wrong_conf);}).should.throw(/unpack/); | ||
// }); | ||
// | ||
// it('loadRawConf config without pack', function() { | ||
// var wrong_conf = require('./config/wrong_config.js').without_pack; | ||
// (function(){config.loadRawConf(wrong_conf);}).should.throw(/pack/); | ||
// }); | ||
// it('loadRawConf config without unpack', function() { | ||
// var wrongConf = require('./config/wrong_config.js').withoutUnpack; | ||
// (function(){config.loadRawConf(wrongConf);}).should.throw(/unpack/); | ||
// }); | ||
// | ||
// it('loadRawConf config without pack', function() { | ||
// var wrongConf = require('./config/wrong_config.js').withoutPack; | ||
// (function(){config.loadRawConf(wrongConf);}).should.throw(/pack/); | ||
// }); | ||
it('loadRawConf config without protocol', function() { | ||
var wrong_conf = require('./config/wrong_config.js').without_protocol; | ||
(function(){config.loadRawConf(wrong_conf);}).should.throw(/protocol/); | ||
it('loadRawConf config without protocol', function () { | ||
var wrongConf = require('./config/wrong_config.js').withoutProtocol; | ||
(function () { | ||
config.loadRawConf(wrongConf); | ||
}).should.throw(/protocol/); | ||
}); | ||
it('loadRawConf config with invalid encoding', function() { | ||
var wrong_conf = require('./config/wrong_config.js').with_invalid_encoding; | ||
(function(){config.loadRawConf(wrong_conf);}).should.throw(/encoding is valid/); | ||
it('loadRawConf config with invalid encoding', function () { | ||
var wrongConf = require('./config/wrong_config.js').withInvalidEncoding; | ||
(function () { | ||
config.loadRawConf(wrongConf); | ||
}).should.throw(/encoding is valid/); | ||
}); | ||
it('loadRawConf config with out server', function() { | ||
var wrong_conf = require('./config/wrong_config.js').without_server; | ||
(function(){config.loadRawConf(wrong_conf);}).should.throw(/server/); | ||
it('loadRawConf config with out server', function () { | ||
var wrongConf = require('./config/wrong_config.js').withoutServer; | ||
(function () { | ||
config.loadRawConf(wrongConf); | ||
}).should.throw(/server/); | ||
}); | ||
it('loadRawConf config with out server info', function() { | ||
var wrong_conf = require('./config/wrong_config.js').without_server_info; | ||
(function(){config.loadRawConf(wrong_conf);}).should.throw(/server/); | ||
it('loadRawConf config with out server info', function () { | ||
var wrongConf = require('./config/wrong_config.js').withoutServerInfo; | ||
(function () { | ||
config.loadRawConf(wrongConf); | ||
}).should.throw(/server/); | ||
}); | ||
it('loadRawConf config with out port', function() { | ||
var wrong_conf = require('./config/wrong_config.js').without_port; | ||
(function(){config.loadRawConf(wrong_conf);}).should.throw(/port/); | ||
it('loadRawConf config with out port', function () { | ||
var wrongConf = require('./config/wrong_config.js').withoutPort; | ||
(function () { | ||
config.loadRawConf(wrongConf); | ||
}).should.throw(/port/); | ||
}); | ||
it('loadRawConf config with invalid pack', function() { | ||
var wrong_conf = require('./config/wrong_config.js').with_invalid_pack; | ||
(function(){config.loadRawConf(wrong_conf);}).should.throw(/invalid pack/); | ||
it('loadRawConf config with invalid pack', function () { | ||
var wrongConf = require('./config/wrong_config.js').withInvalidPack; | ||
(function () { | ||
config.loadRawConf(wrongConf); | ||
}).should.throw(/invalid pack/); | ||
}); | ||
}); | ||
describe('load config', function() { | ||
describe('load config', function () { | ||
it('load by file', function() { | ||
var conf = config.load(__dirname + path.sep + './config/single_config.js'); | ||
conf.should.have.properties('bookService', 'bookServiceBNS', 'bookListService', 'bookListServiceWithCUI'); | ||
it('load by file', function () { | ||
var conf = config.load(path.join(__dirname, './config/single_config.js')); | ||
conf.should.have.properties('bookService', 'bookServiceBNS', 'bookListService', | ||
'bookListServiceWithCUI'); | ||
}); | ||
it('load by json', function() { | ||
var conf = config.load(__dirname + path.sep + './config/json_config.json'); | ||
it('load by json', function () { | ||
var conf = config.load(path.join(__dirname, './config/json_config.json')); | ||
conf.should.have.properties('bookService'); | ||
}); | ||
it('load by wrong file path', function() { | ||
(function(){config.load(__dirname + path.sep + './config/single_config_w.js');}).should.throwError(); | ||
it('load by wrong file path', function () { | ||
(function () { | ||
config.load(path.join(__dirname, './config/single_config_w.js')); | ||
}).should.throwError(); | ||
}); | ||
it('load by wrong folder path', function() { | ||
(function(){config.load(__dirname + path.sep + './config/directory_w');}).should.throwError(); | ||
it('load by wrong folder path', function () { | ||
(function () { | ||
config.load(path.join(__dirname, './config/directory_w')); | ||
}).should.throwError(); | ||
}); | ||
it('load by directory', function() { | ||
var conf = config.load(__dirname + path.sep + './config/directory'); | ||
it('load by directory', function () { | ||
config.load(path.join(__dirname, './config/directory')); | ||
var confs = config.getConfNames(); | ||
@@ -91,2 +121,2 @@ confs.should.containEql('bookService', 'bookServiceBNS', 'bookListService', 'bookListServiceWithCUI'); | ||
}); | ||
}); | ||
}); |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -10,3 +11,3 @@ * 2014/9/11 | ||
module.exports = { | ||
idc: "tc" | ||
}; | ||
idc: 'tc' | ||
}; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -10,26 +11,38 @@ * 2014/8/4 | ||
module.exports = { | ||
'bookService' : { | ||
unpack : 'json', | ||
bookService: { | ||
unpack: 'json', | ||
pack: 'json', | ||
//指定服务端交互的编码格式,由于Node.js环境只支持UTF-8,因此yog-ral会自动进行转码工作 | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'}, | ||
{ host : 'tc.yd.baidu.com', port : 80, idc : 'tc'} | ||
] | ||
// 指定服务端交互的编码格式,由于Node.js环境只支持UTF-8,因此yog-ral会自动进行转码工作 | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}, { | ||
host: 'tc.yd.baidu.com', | ||
port: 80, | ||
idc: 'tc' | ||
}] | ||
}, | ||
'bookServiceBNS' : { | ||
unpack : 'json', | ||
bookServiceBNS: { | ||
unpack: 'json', | ||
pack: 'json', | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
//可选的备用服务器信息,启动时如果没能成功获取BNS信息,会使用备用服务器信息,如果没有备用信息则启动失败 | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'}, | ||
{ host : 'tc.yd.baidu.com', port : 80, idc : 'tc'} | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [ | ||
// 可选的备用服务器信息,启动时如果没能成功获取BNS信息,会使用备用服务器信息,如果没有备用信息则启动失败 | ||
{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}, { | ||
host: 'tc.yd.baidu.com', | ||
port: 80, | ||
idc: 'tc' | ||
} | ||
] | ||
} | ||
}; | ||
}; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -10,35 +11,41 @@ * 2014/8/4 | ||
module.exports = { | ||
'bookListService' : { | ||
retry : 1, | ||
timeout : 500, | ||
//设置Request参数的打包协议 | ||
pack : 'form', | ||
//设置Response的解包协议 | ||
unpack : 'json', | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
//设置协议的默认参数,可被用户覆盖 | ||
method : 'POST', | ||
url : '/book/list', | ||
headers : { | ||
'user-agent' : 'yog-ral' | ||
bookListService: { | ||
retry: 1, | ||
timeout: 500, | ||
// 设置Request参数的打包协议 | ||
pack: 'form', | ||
// 设置Response的解包协议 | ||
unpack: 'json', | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
// 设置协议的默认参数,可被用户覆盖 | ||
method: 'POST', | ||
url: '/book/list', | ||
headers: { | ||
'user-agent': 'yog-ral' | ||
}, | ||
server : [ | ||
//由于不是所有上游服务均设置了TAG,BNS也支持设置idc | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'} | ||
server: [ | ||
// 由于不是所有上游服务均设置了TAG,BNS也支持设置idc | ||
{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
} | ||
] | ||
}, | ||
'bookListServiceWithCUI' : { | ||
retry : 1, | ||
timeout : 500, | ||
pack : 'form', | ||
unpack : 'json', | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'} | ||
] | ||
bookListServiceWithCUI: { | ||
retry: 1, | ||
timeout: 500, | ||
pack: 'form', | ||
unpack: 'json', | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}] | ||
} | ||
}; | ||
}; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -10,40 +11,54 @@ * 2014/8/4 | ||
module.exports = { | ||
'bookService' : { | ||
unpack : 'json', | ||
bookService: { | ||
unpack: 'json', | ||
pack: 'json', | ||
//指定服务端交互的编码格式,由于Node.js环境只支持UTF-8,因此yog-ral会自动进行转码工作 | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'}, | ||
{ host : 'tc.yd.baidu.com', port : 80, idc : 'tc'} | ||
] | ||
// 指定服务端交互的编码格式,由于Node.js环境只支持UTF-8,因此yog-ral会自动进行转码工作 | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}, { | ||
host: 'tc.yd.baidu.com', | ||
port: 80, | ||
idc: 'tc' | ||
}] | ||
}, | ||
'bookService2' : { | ||
unpack : 'json', | ||
bookService2: { | ||
unpack: 'json', | ||
pack: 'json', | ||
//指定服务端交互的编码格式,由于Node.js环境只支持UTF-8,因此yog-ral会自动进行转码工作 | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
{ host : 'st.yd.baidu.com', port : 80}, | ||
{ host : 'tc.yd.baidu.com', port : 80, idc : 'tc'} | ||
] | ||
// 指定服务端交互的编码格式,由于Node.js环境只支持UTF-8,因此yog-ral会自动进行转码工作 | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [{ | ||
host: 'st.yd.baidu.com', | ||
port: 80 | ||
}, { | ||
host: 'tc.yd.baidu.com', | ||
port: 80, | ||
idc: 'tc' | ||
}] | ||
}, | ||
'bookService3' : { | ||
unpack : 'json', | ||
bookService3: { | ||
unpack: 'json', | ||
pack: 'json', | ||
//指定服务端交互的编码格式,由于Node.js环境只支持UTF-8,因此yog-ral会自动进行转码工作 | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
//开启混联,无视idc | ||
hybird : true, | ||
server : [ | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'}, | ||
{ host : 'tc.yd.baidu.com', port : 80, idc : 'tc'} | ||
] | ||
// 指定服务端交互的编码格式,由于Node.js环境只支持UTF-8,因此yog-ral会自动进行转码工作 | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
// 开启混联,无视idc | ||
hybird: true, | ||
server: [{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}, { | ||
host: 'tc.yd.baidu.com', | ||
port: 80, | ||
idc: 'tc' | ||
}] | ||
} | ||
}; | ||
}; |
@@ -8,6 +8,8 @@ { | ||
"protocol": "http", | ||
"server": [ | ||
{ "host": "st.yd.baidu.com", "port": 80, "idc": "st"} | ||
] | ||
"server": [{ | ||
"host": "st.yd.baidu.com", | ||
"port": 80, | ||
"idc": "st" | ||
}] | ||
} | ||
} | ||
} |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -10,59 +11,77 @@ * 2014/8/4 | ||
module.exports = { | ||
'bookService' : { | ||
unpack : 'json', | ||
bookService: { | ||
unpack: 'json', | ||
pack: 'json', | ||
//指定服务端交互的编码格式,由于Node.js环境只支持UTF-8,因此yog-ral会自动进行转码工作 | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'}, | ||
{ host : 'tc.yd.baidu.com', port : 80, idc : 'tc'} | ||
] | ||
// 指定服务端交互的编码格式,由于Node.js环境只支持UTF-8,因此yog-ral会自动进行转码工作 | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}, { | ||
host: 'tc.yd.baidu.com', | ||
port: 80, | ||
idc: 'tc' | ||
}] | ||
}, | ||
'bookServiceBNS' : { | ||
unpack : 'json', | ||
bookServiceBNS: { | ||
unpack: 'json', | ||
pack: 'json', | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
//可选的备用服务器信息,启动时如果没能成功获取BNS信息,会使用备用服务器信息,如果没有备用信息则启动失败 | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'}, | ||
{ host : 'tc.yd.baidu.com', port : 80, idc : 'tc'} | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [ | ||
// 可选的备用服务器信息,启动时如果没能成功获取BNS信息,会使用备用服务器信息,如果没有备用信息则启动失败 | ||
{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}, { | ||
host: 'tc.yd.baidu.com', | ||
port: 80, | ||
idc: 'tc' | ||
} | ||
] | ||
}, | ||
'bookListService' : { | ||
retry : 1, | ||
timeout : 500, | ||
//设置Request参数的打包协议 | ||
pack : 'form', | ||
//设置Response的解包协议 | ||
unpack : 'json', | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
//设置协议的默认参数,可被用户覆盖 | ||
method : 'POST', | ||
url : '/book/list', | ||
headers : { | ||
'user-agent' : 'yog-ral' | ||
bookListService: { | ||
retry: 1, | ||
timeout: 500, | ||
// 设置Request参数的打包协议 | ||
pack: 'form', | ||
// 设置Response的解包协议 | ||
unpack: 'json', | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
// 设置协议的默认参数,可被用户覆盖 | ||
method: 'POST', | ||
url: '/book/list', | ||
headers: { | ||
'user-agent': 'yog-ral' | ||
}, | ||
server : [ | ||
//由于不是所有上游服务均设置了TAG,BNS也支持设置idc | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'} | ||
server: [ | ||
// 由于不是所有上游服务均设置了TAG,BNS也支持设置idc | ||
{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
} | ||
] | ||
}, | ||
'bookListServiceWithCUI' : { | ||
retry : 1, | ||
timeout : 500, | ||
pack : 'form', | ||
unpack : 'json', | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'} | ||
] | ||
bookListServiceWithCUI: { | ||
retry: 1, | ||
timeout: 500, | ||
pack: 'form', | ||
unpack: 'json', | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}] | ||
} | ||
}; | ||
}; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -10,59 +11,77 @@ * 2014/8/4 | ||
module.exports = { | ||
'bookService' : { | ||
unpack : 'json', | ||
bookService: { | ||
unpack: 'json', | ||
pack: 'json', | ||
//指定服务端交互的编码格式,由于Node.js环境只支持UTF-8,因此yog-ral会自动进行转码工作 | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'}, | ||
{ host : 'tc.yd.baidu.com', port : 80, idc : 'tc'} | ||
] | ||
// 指定服务端交互的编码格式,由于Node.js环境只支持UTF-8,因此yog-ral会自动进行转码工作 | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}, { | ||
host: 'tc.yd.baidu.com', | ||
port: 80, | ||
idc: 'tc' | ||
}] | ||
}, | ||
'bookServiceBNS' : { | ||
unpack : 'json', | ||
bookServiceBNS: { | ||
unpack: 'json', | ||
pack: 'json', | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
//可选的备用服务器信息,启动时如果没能成功获取BNS信息,会使用备用服务器信息,如果没有备用信息则启动失败 | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'}, | ||
{ host : 'tc.yd.baidu.com', port : 80, idc : 'tc'} | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [ | ||
// 可选的备用服务器信息,启动时如果没能成功获取BNS信息,会使用备用服务器信息,如果没有备用信息则启动失败 | ||
{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}, { | ||
host: 'tc.yd.baidu.com', | ||
port: 80, | ||
idc: 'tc' | ||
} | ||
] | ||
}, | ||
'bookListService' : { | ||
retry : 1, | ||
timeout : 500, | ||
//设置Request参数的打包协议 | ||
pack : 'form', | ||
//设置Response的解包协议 | ||
unpack : 'json', | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
//设置协议的默认参数,可被用户覆盖 | ||
method : 'POST', | ||
url : '/book/list', | ||
headers : { | ||
'user-agent' : 'yog-ral' | ||
bookListService: { | ||
retry: 1, | ||
timeout: 500, | ||
// 设置Request参数的打包协议 | ||
pack: 'form', | ||
// 设置Response的解包协议 | ||
unpack: 'json', | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
// 设置协议的默认参数,可被用户覆盖 | ||
method: 'POST', | ||
url: '/book/list', | ||
headers: { | ||
'user-agent': 'yog-ral' | ||
}, | ||
server : [ | ||
//由于不是所有上游服务均设置了TAG,BNS也支持设置idc | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'} | ||
server: [ | ||
// 由于不是所有上游服务均设置了TAG,BNS也支持设置idc | ||
{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
} | ||
] | ||
}, | ||
'bookListServiceWithCUI' : { | ||
retry : 1, | ||
timeout : 500, | ||
pack : 'form', | ||
unpack : 'json', | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'} | ||
] | ||
bookListServiceWithCUI: { | ||
retry: 1, | ||
timeout: 500, | ||
pack: 'form', | ||
unpack: 'json', | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}] | ||
} | ||
}; | ||
}; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -10,13 +11,15 @@ * 2014/8/5 | ||
module.exports = { | ||
'bookService': { | ||
bookService: { | ||
unpack: 'json', | ||
pack: 'json', | ||
//指定服务端交互的编码格式,由于Node.js环境只支持UTF-8,因此yog-ral会自动进行转码工作 | ||
// 指定服务端交互的编码格式,由于Node.js环境只支持UTF-8,因此yog-ral会自动进行转码工作 | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [ | ||
{ host: 'st.yd.baidu.com', port: 80, idc: 'st'} | ||
] | ||
server: [{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}] | ||
} | ||
}; | ||
}; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -7,14 +8,23 @@ * 2014/8/4 | ||
/*eslint-disable fecs-camelcase, camelcase */ | ||
'use strict'; | ||
module.exports.without_pack = { | ||
'bookServiceBNS' : { | ||
unpack : 'json', | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
//可选的备用服务器信息,启动时如果没能成功获取BNS信息,会使用备用服务器信息,如果没有备用信息则启动失败 | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'}, | ||
{ host : 'tc.yd.baidu.com', port : 80, idc : 'tc'} | ||
module.exports.withoutPack = { | ||
bookServiceBNS: { | ||
unpack: 'json', | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [ | ||
// 可选的备用服务器信息,启动时如果没能成功获取BNS信息,会使用备用服务器信息,如果没有备用信息则启动失败 | ||
{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}, { | ||
host: 'tc.yd.baidu.com', | ||
port: 80, | ||
idc: 'tc' | ||
} | ||
] | ||
@@ -24,12 +34,19 @@ } | ||
module.exports.without_unpack = { | ||
'bookServiceBNS' : { | ||
pack : 'json', | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
//可选的备用服务器信息,启动时如果没能成功获取BNS信息,会使用备用服务器信息,如果没有备用信息则启动失败 | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'}, | ||
{ host : 'tc.yd.baidu.com', port : 80, idc : 'tc'} | ||
module.exports.withoutUnpack = { | ||
bookServiceBNS: { | ||
pack: 'json', | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [ | ||
// 可选的备用服务器信息,启动时如果没能成功获取BNS信息,会使用备用服务器信息,如果没有备用信息则启动失败 | ||
{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}, { | ||
host: 'tc.yd.baidu.com', | ||
port: 80, | ||
idc: 'tc' | ||
} | ||
] | ||
@@ -40,11 +57,18 @@ } | ||
module.exports.without_balance = { | ||
'bookServiceBNS' : { | ||
unpack : 'json', | ||
pack : 'json', | ||
encoding : 'GBK', | ||
protocol : 'http', | ||
server : [ | ||
//可选的备用服务器信息,启动时如果没能成功获取BNS信息,会使用备用服务器信息,如果没有备用信息则启动失败 | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'}, | ||
{ host : 'tc.yd.baidu.com', port : 80, idc : 'tc'} | ||
bookServiceBNS: { | ||
unpack: 'json', | ||
pack: 'json', | ||
encoding: 'GBK', | ||
protocol: 'http', | ||
server: [ | ||
// 可选的备用服务器信息,启动时如果没能成功获取BNS信息,会使用备用服务器信息,如果没有备用信息则启动失败 | ||
{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}, { | ||
host: 'tc.yd.baidu.com', | ||
port: 80, | ||
idc: 'tc' | ||
} | ||
] | ||
@@ -54,12 +78,19 @@ } | ||
module.exports.without_protocol = { | ||
'bookServiceBNS' : { | ||
unpack : 'json', | ||
pack : 'json', | ||
encoding : 'GBK', | ||
balance : 'random', | ||
server : [ | ||
//可选的备用服务器信息,启动时如果没能成功获取BNS信息,会使用备用服务器信息,如果没有备用信息则启动失败 | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'}, | ||
{ host : 'tc.yd.baidu.com', port : 80, idc : 'tc'} | ||
module.exports.withoutProtocol = { | ||
bookServiceBNS: { | ||
unpack: 'json', | ||
pack: 'json', | ||
encoding: 'GBK', | ||
balance: 'random', | ||
server: [ | ||
// 可选的备用服务器信息,启动时如果没能成功获取BNS信息,会使用备用服务器信息,如果没有备用信息则启动失败 | ||
{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}, { | ||
host: 'tc.yd.baidu.com', | ||
port: 80, | ||
idc: 'tc' | ||
} | ||
] | ||
@@ -69,13 +100,20 @@ } | ||
module.exports.with_invalid_encoding = { | ||
'bookServiceBNS' : { | ||
unpack : 'json', | ||
pack : 'json', | ||
encoding : 'GBK1', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
//可选的备用服务器信息,启动时如果没能成功获取BNS信息,会使用备用服务器信息,如果没有备用信息则启动失败 | ||
{ host : 'st.yd.baidu.com', port : 80, idc : 'st'}, | ||
{ host : 'tc.yd.baidu.com', port : 80, idc : 'tc'} | ||
module.exports.withInvalidEncoding = { | ||
bookServiceBNS: { | ||
unpack: 'json', | ||
pack: 'json', | ||
encoding: 'GBK1', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [ | ||
// 可选的备用服务器信息,启动时如果没能成功获取BNS信息,会使用备用服务器信息,如果没有备用信息则启动失败 | ||
{ | ||
host: 'st.yd.baidu.com', | ||
port: 80, | ||
idc: 'st' | ||
}, { | ||
host: 'tc.yd.baidu.com', | ||
port: 80, | ||
idc: 'tc' | ||
} | ||
] | ||
@@ -85,48 +123,47 @@ } | ||
module.exports.without_server = { | ||
'bookServiceBNS' : { | ||
unpack : 'json', | ||
pack : 'json', | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http' | ||
module.exports.withoutServer = { | ||
bookServiceBNS: { | ||
unpack: 'json', | ||
pack: 'json', | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http' | ||
} | ||
}; | ||
module.exports.without_server_info = { | ||
'bookServiceBNS' : { | ||
unpack : 'json', | ||
pack : 'json', | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
] | ||
module.exports.withoutServerInfo = { | ||
bookServiceBNS: { | ||
unpack: 'json', | ||
pack: 'json', | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [] | ||
} | ||
}; | ||
module.exports.without_port = { | ||
'bookServiceBNS' : { | ||
unpack : 'json', | ||
pack : 'json', | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
{ host : '127.0.0.1'} | ||
] | ||
module.exports.withoutPort = { | ||
bookServiceBNS: { | ||
unpack: 'json', | ||
pack: 'json', | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [{ | ||
host: '127.0.0.1' | ||
}] | ||
} | ||
}; | ||
module.exports.with_invalid_pack = { | ||
'bookServiceBNS' : { | ||
unpack : 'json', | ||
pack : 'json233', | ||
encoding : 'GBK', | ||
balance : 'random', | ||
protocol : 'http', | ||
server : [ | ||
{ host : '127.0.0.1'} | ||
] | ||
module.exports.withInvalidPack = { | ||
bookServiceBNS: { | ||
unpack: 'json', | ||
pack: 'json233', | ||
encoding: 'GBK', | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [{ | ||
host: '127.0.0.1' | ||
}] | ||
} | ||
}; | ||
}; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -7,2 +8,4 @@ * 2014/8/5 | ||
/* eslint-disable no-wrap-func, max-nested-callbacks */ | ||
'use strict'; | ||
@@ -22,6 +25,4 @@ | ||
var _ = require('underscore'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var post_test = require('./protocol/http_protocol_post_test.js'); | ||
var FormData = require('form-data'); | ||
var postTest = require('./protocol/http_protocol_post_test.js'); | ||
var FormDataCls = require('form-data'); | ||
@@ -87,3 +88,3 @@ var mockUTF8Context = { | ||
a: 1, | ||
b: "张三" | ||
b: '张三' | ||
}; | ||
@@ -100,3 +101,5 @@ var pack = jsonConverter.pack(mockUTF8Context, data); | ||
var pack = converter.pack(mockUTF8Context, data); | ||
(function(){jsonConverter.unpack(mockUTF8Context, pack);}).should.throwError(); | ||
(function () { | ||
jsonConverter.unpack(mockUTF8Context, pack); | ||
}).should.throwError(); | ||
}); | ||
@@ -108,3 +111,3 @@ | ||
a: 1, | ||
b: "张三" | ||
b: '张三' | ||
}; | ||
@@ -195,13 +198,13 @@ var pack = jsonConverter.pack(mockGBKContext, data); | ||
var httpProtocol = new HttpProtocol(); | ||
var server = post_test.createServer(); | ||
util.merge(options, post_test.service); | ||
var server = postTest.createServer(); | ||
util.merge(options, postTest.service); | ||
options = HttpProtocol.normalizeConfig(options); | ||
var pack = converter.pack(options, data); | ||
util.merge(options, { | ||
server: post_test.request.server | ||
server: postTest.request.server | ||
}); | ||
var request = httpProtocol.talk(options, function(res){ | ||
res.on('data', function (data) { | ||
var request = httpProtocol.talk(options, function (res) { | ||
res.on('data', function (data2) { | ||
server.close(); | ||
data.toString().should.be.equal('hear you 张三李四'); | ||
data2.toString().should.be.equal('hear you 张三李四'); | ||
done(); | ||
@@ -220,13 +223,13 @@ }); | ||
var httpProtocol = new HttpProtocol(); | ||
util.merge(options, post_test.service); | ||
util.merge(options, postTest.service); | ||
util.merge(options, { | ||
server: post_test.request.server | ||
server: postTest.request.server | ||
}); | ||
options = HttpProtocol.normalizeConfig(options); | ||
var server = post_test.createServer('gbk'); | ||
var server = postTest.createServer('gbk'); | ||
var pack = converter.pack(options, data); | ||
var request = httpProtocol.talk(options, function (res) { | ||
res.on('data', function(data){ | ||
data.toString().should.be.equal('hear you 张三李四'); | ||
res.on('data', function (data2) { | ||
server.close(); | ||
data2.toString().should.be.equal('hear you 张三李四'); | ||
done(); | ||
@@ -241,3 +244,3 @@ }); | ||
var data = { | ||
name: "hefangshi" | ||
name: 'hefangshi' | ||
}; | ||
@@ -264,6 +267,6 @@ (function () { | ||
options.syncLength = true; | ||
var httpProtocol = new HttpProtocol(); | ||
util.merge(options, post_test.service); | ||
// var httpProtocol = new HttpProtocol(); | ||
util.merge(options, postTest.service); | ||
options = HttpProtocol.normalizeConfig(options); | ||
var pack = converter.pack(options, data); | ||
converter.pack(options, data); | ||
options.headers['Content-Length'].should.be.eql(169); | ||
@@ -288,3 +291,3 @@ }); | ||
a: 1, | ||
b: "张三" | ||
b: '张三' | ||
}; | ||
@@ -300,3 +303,3 @@ var pack = converter.pack(mockUTF8Context, data); | ||
a: 1, | ||
b: "张三" | ||
b: '张三' | ||
}; | ||
@@ -324,12 +327,12 @@ var pack = converter.pack(mockGBKContext, data); | ||
var httpProtocol = new HttpProtocol(); | ||
util.merge(options, post_test.service); | ||
util.merge(options, post_test.request); | ||
util.merge(options, postTest.service); | ||
util.merge(options, postTest.request); | ||
options = HttpProtocol.normalizeConfig(options); | ||
var pack = converter.pack(options, data); | ||
var server = post_test.createServer(); | ||
var server = postTest.createServer(); | ||
options.payload = pack; | ||
var request = httpProtocol.talk(options, function (res) { | ||
res.on('data', function(data){ | ||
data.toString().should.be.equal('hear you 张三李四'); | ||
httpProtocol.talk(options, function (res) { | ||
res.on('data', function (resData) { | ||
server.close(); | ||
resData.toString().should.be.equal('hear you 张三李四'); | ||
done(); | ||
@@ -356,3 +359,3 @@ }); | ||
a: 1, | ||
b: "张三" | ||
b: '张三' | ||
}; | ||
@@ -368,3 +371,3 @@ converter.pack(mockUTF8Context, data); | ||
a: 1, | ||
b: "张三" | ||
b: '张三' | ||
}; | ||
@@ -390,6 +393,6 @@ var pack = urlencodeConverter.pack(mockUTF8Context, data); | ||
var converter = new RawConverter(); | ||
var data = new Buffer("abc"); | ||
var data = new Buffer('abc'); | ||
var pack = converter.pack({}, data); | ||
var unpack = converter.unpack({}, pack); | ||
unpack.toString().should.be.eql("abc"); | ||
unpack.toString().should.be.eql('abc'); | ||
}); | ||
@@ -411,16 +414,16 @@ }); | ||
var converter = new StreamConverter(); | ||
var form = new FormData(); | ||
var form = new FormDataCls(); | ||
form.append('name', '张三李四'); | ||
var options = _.clone(mockUTF8Context); | ||
var httpProtocol = new HttpProtocol(); | ||
var server = post_test.createServer(); | ||
util.merge(options, post_test.service); | ||
var server = postTest.createServer(); | ||
util.merge(options, postTest.service); | ||
options = HttpProtocol.normalizeConfig(options); | ||
var pack = converter.pack(options, form); | ||
util.merge(options, { | ||
server: post_test.request.server | ||
server: postTest.request.server | ||
}); | ||
options.headers = {}; | ||
options.headers['Content-Type'] = "multipart/form-data;boundary=" + form.getBoundary(); | ||
var request = httpProtocol.talk(options, function(res){ | ||
options.headers['Content-Type'] = 'multipart/form-data;boundary=' + form.getBoundary(); | ||
var request = httpProtocol.talk(options, function (res) { | ||
res.on('data', function (data) { | ||
@@ -431,3 +434,3 @@ server.close(); | ||
}); | ||
res.on('error', function(){ | ||
res.on('error', function () { | ||
server.close(); | ||
@@ -434,0 +437,0 @@ }); |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -7,2 +8,4 @@ * 2014/9/4 | ||
/* eslint-disable no-wrap-func, max-nested-callbacks, fecs-camelcase, camelcase */ | ||
'use strict'; | ||
@@ -18,11 +21,11 @@ | ||
var m = middleware({ | ||
confDir : __dirname + path.sep + './middleware/config', | ||
logger : { | ||
"log_path" : __dirname + path.sep + '../logs', | ||
"app" : "yog-ral" | ||
confDir: path.join(__dirname, './middleware/config'), | ||
logger: { | ||
log_path: path.join(__dirname, '../logs'), | ||
app: 'yog-ral' | ||
}, | ||
currentIDC : 'tc' | ||
currentIDC: 'tc' | ||
}); | ||
var req = {}; | ||
m(req, {}, function(){ | ||
m(req, {}, function () { | ||
req.RAL.should.be.ok; | ||
@@ -33,2 +36,2 @@ config.getConf('FROM_MIDDLEWARE').should.be.ok; | ||
}); | ||
}); | ||
}); |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -10,3 +11,3 @@ * 2014/9/4 | ||
module.exports = { | ||
'FROM_MIDDLEWARE': { | ||
FROM_MIDDLEWARE: { | ||
unpack: 'json', | ||
@@ -19,8 +20,16 @@ pack: 'querystring', | ||
query: 'from=ral', | ||
server: [ | ||
{ host: '127.0.0.1', port: 8192, idc: 'tc'}, | ||
{ host: '127.0.0.1', port: 8193, idc: 'tc'}, | ||
{ host: '127.0.0.1', port: 8194, idc: 'st'} | ||
] | ||
server: [{ | ||
host: '127.0.0.1', | ||
port: 8192, | ||
idc: 'tc' | ||
}, { | ||
host: '127.0.0.1', | ||
port: 8193, | ||
idc: 'tc' | ||
}, { | ||
host: '127.0.0.1', | ||
port: 8194, | ||
idc: 'st' | ||
}] | ||
} | ||
}; | ||
}; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -7,2 +8,5 @@ * 2014/8/5 | ||
/* eslint-disable no-wrap-func */ | ||
/* eslint-disable max-nested-callbacks, no-console */ | ||
'use strict'; | ||
@@ -38,12 +42,2 @@ | ||
var mockRequest = { | ||
options: { | ||
timeout: 100, | ||
query: 'b=1', | ||
headers: { | ||
'User-Agent': 'Webkit' | ||
} | ||
} | ||
}; | ||
describe('protocol', function () { | ||
@@ -74,12 +68,12 @@ it('should fail when get name', function () { | ||
describe('http protocol with get method',function(){ | ||
describe('http protocol with get method', function () { | ||
it('should work fine with GET method', function (done) { | ||
var get_test = require('./protocol/http_protocol_get_test.js'); | ||
//start a http server for get | ||
var server = get_test.createServer(); | ||
var getTest = require('./protocol/http_protocol_get_test.js'); | ||
// start a http server for get | ||
var server = getTest.createServer(); | ||
var httpProtocol = new HttpProtocol(); | ||
var context = HttpProtocol.normalizeConfig(get_test.service); | ||
util.merge(context, get_test.request); | ||
httpProtocol.talk(context, function(res){ | ||
res.on('data', function(data){ | ||
var context = HttpProtocol.normalizeConfig(getTest.service); | ||
util.merge(context, getTest.request); | ||
httpProtocol.talk(context, function (res) { | ||
res.on('data', function (data) { | ||
server.close(); | ||
@@ -93,10 +87,10 @@ data.toString().should.be.equal('hear you'); | ||
it('should work fine with GET method and querystring', function (done) { | ||
var get_test = require('./protocol/http_protocol_get_test.js'); | ||
//start a http server for get | ||
var server = get_test.createServer(); | ||
var getTest = require('./protocol/http_protocol_get_test.js'); | ||
// start a http server for get | ||
var server = getTest.createServer(); | ||
var httpProtocol = new HttpProtocol(); | ||
var context = HttpProtocol.normalizeConfig(get_test.service); | ||
util.merge(context, get_test.request_with_query); | ||
httpProtocol.talk(context, function(res){ | ||
res.on('data', function(data){ | ||
var context = HttpProtocol.normalizeConfig(getTest.service); | ||
util.merge(context, getTest.requestWithQuery); | ||
httpProtocol.talk(context, function (res) { | ||
res.on('data', function (data) { | ||
server.close(); | ||
@@ -110,10 +104,10 @@ data.toString().should.be.equal('hear you hefangshi'); | ||
it('should got 404 status when GET 404', function (done) { | ||
var get_test = require('./protocol/http_protocol_get_test.js'); | ||
//start a http server for get | ||
var server = get_test.createServer(); | ||
var getTest = require('./protocol/http_protocol_get_test.js'); | ||
// start a http server for get | ||
var server = getTest.createServer(); | ||
var httpProtocol = new HttpProtocol(); | ||
var context = HttpProtocol.normalizeConfig(get_test.service); | ||
util.merge(context, get_test.request_404); | ||
var context = HttpProtocol.normalizeConfig(getTest.service); | ||
util.merge(context, getTest.request404); | ||
var stream = httpProtocol.talk(context); | ||
stream.on('error', function(err){ | ||
stream.on('error', function (err) { | ||
server.close(); | ||
@@ -126,10 +120,10 @@ err.should.match(/Server Status Error: 404/); | ||
it('should got 503 status when GET 503', function (done) { | ||
var get_test = require('./protocol/http_protocol_get_test.js'); | ||
//start a http server for get | ||
var server = get_test.createServer(); | ||
var getTest = require('./protocol/http_protocol_get_test.js'); | ||
// start a http server for get | ||
var server = getTest.createServer(); | ||
var httpProtocol = new HttpProtocol(); | ||
var context = HttpProtocol.normalizeConfig(get_test.service); | ||
util.merge(context, get_test.request_404); | ||
var context = HttpProtocol.normalizeConfig(getTest.service); | ||
util.merge(context, getTest.request404); | ||
var stream = httpProtocol.talk(context); | ||
stream.on('error', function(err){ | ||
stream.on('error', function (err) { | ||
server.close(); | ||
@@ -141,47 +135,48 @@ err.should.match(/Server Status Error: 503/); | ||
it('should work well with https', function(done) { | ||
var get_test = require('./protocol/http_protocol_get_test.js'); | ||
//start a http server for get | ||
// var server = get_test.createServer(); | ||
it('should work well with https', function (done) { | ||
var getTest = require('./protocol/http_protocol_get_test.js'); | ||
// start a http server for get | ||
// var server = getTest.createServer(); | ||
var httpProtocol = new HttpProtocol(); | ||
var context = HttpProtocol.normalizeConfig(get_test.request_https); | ||
var stream = httpProtocol.talk(context, function(res){ | ||
res.on('data', function(data){ | ||
var context = HttpProtocol.normalizeConfig(getTest.requestHttps); | ||
var stream = httpProtocol.talk(context, function (res) { | ||
res.on('data', function (data) { | ||
done(); | ||
}); | ||
}); | ||
stream.on('error', function(err){ | ||
stream.on('error', function (err) { | ||
console.log(err); | ||
err.should.be.null; | ||
}) | ||
}); | ||
}); | ||
}); | ||
describe('http protocol with post method',function(){ | ||
describe('http protocol with post method', function () { | ||
it('should work fine with POST method', function (done) { | ||
var post_test = require('./protocol/http_protocol_post_test.js'); | ||
//start a http server for post | ||
var server = post_test.createServer(); | ||
var postTest = require('./protocol/http_protocol_post_test.js'); | ||
// start a http server for post | ||
var server = postTest.createServer(); | ||
var httpProtocol = new HttpProtocol(); | ||
var context = HttpProtocol.normalizeConfig(post_test.service); | ||
util.merge(context, post_test.request); | ||
var request = httpProtocol.talk(context, function(res){ | ||
res.on('data', function(data){ | ||
var context = HttpProtocol.normalizeConfig(postTest.service); | ||
util.merge(context, postTest.request); | ||
var request = httpProtocol.talk(context, function (res) { | ||
res.on('data', function (data) { | ||
server.close(); | ||
data.toString().should.be.equal('hear you hefangshi with file http_protocol_post_test.js'); | ||
data.toString().should.be.equal( | ||
'hear you hefangshi with file http_protocol_post_test.js'); | ||
done(); | ||
}); | ||
}); | ||
post_test.request.data.pipe(request); | ||
postTest.request.data.pipe(request); | ||
}); | ||
it('should work fine with POST method when post a plan object', function (done) { | ||
var post_test = require('./protocol/http_protocol_post_test.js'); | ||
//start a http server for post | ||
var server = post_test.createServer(); | ||
var postTest = require('./protocol/http_protocol_post_test.js'); | ||
// start a http server for post | ||
var server = postTest.createServer(); | ||
var httpProtocol = new HttpProtocol(); | ||
var context =HttpProtocol.normalizeConfig(post_test.service); | ||
util.merge(context, post_test.request_with_urlencode); | ||
var request = httpProtocol.talk(context, function(res){ | ||
res.on('data', function(data){ | ||
var context = HttpProtocol.normalizeConfig(postTest.service); | ||
util.merge(context, postTest.requestWithUrlencode); | ||
var request = httpProtocol.talk(context, function (res) { | ||
res.on('data', function (data) { | ||
server.close(); | ||
@@ -192,31 +187,32 @@ data.toString().should.be.equal('hear you hefangshi'); | ||
}); | ||
post_test.request_with_urlencode.data.pipe(request); | ||
postTest.requestWithUrlencode.data.pipe(request); | ||
}); | ||
it('should work fine with POST gbk form', function (done) { | ||
var post_test = require('./protocol/http_protocol_post_test.js'); | ||
//start a http server for post | ||
var server = post_test.createServer('gbk'); | ||
var postTest = require('./protocol/http_protocol_post_test.js'); | ||
// start a http server for post | ||
var server = postTest.createServer('gbk'); | ||
var httpProtocol = new HttpProtocol(); | ||
var context = HttpProtocol.normalizeConfig(post_test.service); | ||
util.merge(context, post_test.request_gbk_form); | ||
var request = httpProtocol.talk(context, function(res){ | ||
res.on('data', function(data){ | ||
var context = HttpProtocol.normalizeConfig(postTest.service); | ||
util.merge(context, postTest.requestGBKForm); | ||
var request = httpProtocol.talk(context, function (res) { | ||
res.on('data', function (data) { | ||
server.close(); | ||
data.toString().should.be.equal('hear you 何方石 with file http_protocol_post_test.js'); | ||
data.toString().should.be.equal( | ||
'hear you 何方石 with file http_protocol_post_test.js'); | ||
done(); | ||
}); | ||
}); | ||
post_test.request_gbk_form.data.pipe(request); | ||
postTest.requestGBKForm.data.pipe(request); | ||
}); | ||
it('should got 404 status when GET 404', function (done) { | ||
var post_test = require('./protocol/http_protocol_post_test.js'); | ||
//start a http server for post | ||
var server = post_test.createServer(); | ||
var postTest = require('./protocol/http_protocol_post_test.js'); | ||
// start a http server for post | ||
var server = postTest.createServer(); | ||
var httpProtocol = new HttpProtocol(); | ||
var context = HttpProtocol.normalizeConfig(post_test.service); | ||
util.merge(context, post_test.request_404); | ||
var context = HttpProtocol.normalizeConfig(postTest.service); | ||
util.merge(context, postTest.request404); | ||
var stream = httpProtocol.talk(context); | ||
stream.on('error', function(err){ | ||
stream.on('error', function (err) { | ||
server.close(); | ||
@@ -229,10 +225,10 @@ err.should.match(/Server Status Error: 404/); | ||
it('should got 503 status when GET 503', function (done) { | ||
var post_test = require('./protocol/http_protocol_post_test.js'); | ||
//start a http server for post | ||
var server = post_test.createServer(); | ||
var postTest = require('./protocol/http_protocol_post_test.js'); | ||
// start a http server for post | ||
var server = postTest.createServer(); | ||
var httpProtocol = new HttpProtocol(); | ||
var options = HttpProtocol.normalizeConfig(post_test.service); | ||
util.merge(options, post_test.request_404); | ||
var options = HttpProtocol.normalizeConfig(postTest.service); | ||
util.merge(options, postTest.request404); | ||
var stream = httpProtocol.talk(options); | ||
stream.on('error', function(err){ | ||
stream.on('error', function (err) { | ||
server.close(); | ||
@@ -258,3 +254,5 @@ err.should.match(/Server Status Error: 503/); | ||
var context = HttpProtocol.normalizeConfig(mockHTTPService2); | ||
context.query.should.be.eql({a: '1'}); | ||
context.query.should.be.eql({ | ||
a: '1' | ||
}); | ||
}); | ||
@@ -265,4 +263,4 @@ }); | ||
it('should request wsdl service successfully', function (done) { | ||
var soap_test = require('./protocol/soap_protocol.js'); | ||
var context = SoapProtocol.normalizeConfig(soap_test); | ||
var soapTest = require('./protocol/soap_protocol.js'); | ||
var context = SoapProtocol.normalizeConfig(soapTest); | ||
context.method = 'GetCityForecastByZIP'; | ||
@@ -273,4 +271,4 @@ context.payload = { | ||
var soapProtocol = new SoapProtocol(); | ||
soapProtocol.talk(context, function(res){ | ||
res.on('data', function(data){ | ||
soapProtocol.talk(context, function (res) { | ||
res.on('data', function (data) { | ||
data.GetCityForecastByZIPResult.should.be.ok; | ||
@@ -283,4 +281,4 @@ done(); | ||
it('should request wsdl service with service.port successfully', function (done) { | ||
var soap_test = require('./protocol/soap_protocol.js'); | ||
var context = SoapProtocol.normalizeConfig(soap_test); | ||
var soapTest = require('./protocol/soap_protocol.js'); | ||
var context = SoapProtocol.normalizeConfig(soapTest); | ||
context.soapService = 'Weather'; | ||
@@ -293,4 +291,4 @@ context.soapPort = 'WeatherSoap12'; | ||
var soapProtocol = new SoapProtocol(); | ||
soapProtocol.talk(context, function(res){ | ||
res.on('data', function(data){ | ||
soapProtocol.talk(context, function (res) { | ||
res.on('data', function (data) { | ||
data.GetCityForecastByZIPResult.should.be.ok; | ||
@@ -301,2 +299,2 @@ done(); | ||
}); | ||
}); | ||
}); |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -11,3 +12,3 @@ * 2014/8/6 | ||
module.exports.__defineGetter__('service', function(){ | ||
module.exports.__defineGetter__('service', function () { | ||
return { | ||
@@ -21,25 +22,25 @@ timeout: 1000, | ||
module.exports.request = { | ||
server : { | ||
host : '127.0.0.1', | ||
port : 8934 | ||
server: { | ||
host: '127.0.0.1', | ||
port: 8934 | ||
} | ||
}; | ||
module.exports.request_404 = { | ||
module.exports.request404 = { | ||
path: '/404', | ||
server : { | ||
host : '127.0.0.1', | ||
port : 8934 | ||
server: { | ||
host: '127.0.0.1', | ||
port: 8934 | ||
} | ||
}; | ||
module.exports.request_503 = { | ||
module.exports.request503 = { | ||
path: '/error', | ||
server : { | ||
host : '127.0.0.1', | ||
port : 8934 | ||
server: { | ||
host: '127.0.0.1', | ||
port: 8934 | ||
} | ||
}; | ||
module.exports.request_with_query = { | ||
module.exports.requestWithQuery = { | ||
path: '/hello', | ||
@@ -49,9 +50,9 @@ query: { | ||
}, | ||
server : { | ||
host : '127.0.0.1', | ||
port : 8934 | ||
server: { | ||
host: '127.0.0.1', | ||
port: 8934 | ||
} | ||
}; | ||
module.exports.request_https = { | ||
module.exports.requestHttps = { | ||
https: true, | ||
@@ -61,4 +62,4 @@ method: 'GET', | ||
rejectUnauthorized: false, | ||
server : { | ||
host : 'travis-ci.org', | ||
server: { | ||
host: 'travis-ci.org', | ||
port: 443 | ||
@@ -68,6 +69,6 @@ } | ||
module.exports.createServer = function(){ | ||
module.exports.createServer = function () { | ||
return http.createServer(function (request, response) { | ||
var info = url.parse(request.url); | ||
var pathname =info.pathname; | ||
var pathname = info.pathname; | ||
if (pathname === '/hello') { | ||
@@ -78,5 +79,5 @@ response.writeHead(200, { | ||
var content = 'hear you'; | ||
if (info.query){ | ||
if (info.query) { | ||
info.query = urlencode.parse(info.query); | ||
if (info.query.name){ | ||
if (info.query.name) { | ||
content += ' ' + info.query.name; | ||
@@ -87,3 +88,4 @@ } | ||
response.end(); | ||
}else if (pathname === '/error'){ | ||
} | ||
else if (pathname === '/error') { | ||
response.writeHead(503, { | ||
@@ -93,3 +95,4 @@ 'Content-Type': 'text/plain' | ||
response.end(); | ||
}else{ | ||
} | ||
else { | ||
response.writeHead(404, { | ||
@@ -102,2 +105,2 @@ 'Content-Type': 'text/plain' | ||
}).listen(8934); | ||
}; | ||
}; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -11,9 +12,10 @@ * 2014/8/6 | ||
var formidable = require('formidable'); | ||
var formData = require('form-data'); | ||
var FormDataCls = require('form-data'); | ||
var fs = require('fs'); | ||
var CombinedStream = require('combined-stream'); | ||
var iconv = require('iconv-lite'); | ||
var path = require('path'); | ||
iconv.extendNodeEncodings(); | ||
module.exports.__defineGetter__('service', function(){ | ||
module.exports.__defineGetter__('service', function () { | ||
return { | ||
@@ -26,9 +28,9 @@ timeout: 1000, | ||
var form = new formData(); | ||
var form = new FormDataCls(); | ||
form.append('name', 'hefangshi'); | ||
form.append('passwd', 'what'); | ||
form.append('file', fs.createReadStream(__dirname + '/' + 'http_protocol_post_test.js')); | ||
form.append('file', fs.createReadStream(path.join(__dirname, 'http_protocol_post_test.js'))); | ||
module.exports.__defineGetter__('request',function() { | ||
module.exports.__defineGetter__('request', function () { | ||
return { | ||
@@ -39,3 +41,4 @@ server: { | ||
}, | ||
headers:{'Content-Type':"multipart/form-data;boundary=" + form.getBoundary() | ||
headers: { | ||
'Content-Type': 'multipart/form-data;boundary=' + form.getBoundary() | ||
}, | ||
@@ -46,30 +49,34 @@ data: form | ||
//create a buffer stream | ||
// create a buffer stream | ||
var combinedStream = CombinedStream.create(); | ||
combinedStream.append(new Buffer(urlencode.stringify({name:'hefangshi'}))); | ||
combinedStream.append(new Buffer(urlencode.stringify({ | ||
name: 'hefangshi' | ||
}))); | ||
module.exports.request_with_urlencode = { | ||
server : { | ||
host : '127.0.0.1', | ||
port : 8934 | ||
module.exports.requestWithUrlencode = { | ||
server: { | ||
host: '127.0.0.1', | ||
port: 8934 | ||
}, | ||
headers : { | ||
'Content-Type' : 'application/x-www-form-urlencoded; charset=utf-8' | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' | ||
}, | ||
data: combinedStream | ||
}; | ||
//create a buffer stream | ||
// create a buffer stream | ||
var gbkStream = CombinedStream.create(); | ||
gbkStream.append(new Buffer(urlencode.stringify({name:'何方石'}, {charset: 'gbk'}))); | ||
gbkStream.append(new Buffer(urlencode.stringify({ | ||
name: '何方石' | ||
}, { | ||
charset: 'gbk' | ||
}))); | ||
module.exports.request_with_gbk = { | ||
server : { | ||
host : '127.0.0.1', | ||
port : 8934 | ||
module.exports.requestWithGBK = { | ||
server: { | ||
host: '127.0.0.1', | ||
port: 8934 | ||
}, | ||
encoding : 'gbk', | ||
headers : { | ||
'Content-Type' : "application/x-www-form-urlencoded; charset=gbk" | ||
encoding: 'gbk', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded; charset=gbk' | ||
}, | ||
@@ -79,37 +86,37 @@ data: gbkStream | ||
var gbk_form = new formData(); | ||
gbk_form.append('name', iconv.encode('何方石','gbk')); | ||
gbk_form.append('passwd', 'what'); | ||
gbk_form.append('file', fs.createReadStream(__dirname + '/' + 'http_protocol_post_test.js')); | ||
var gbkForm = new FormDataCls(); | ||
gbkForm.append('name', iconv.encode('何方石', 'gbk')); | ||
gbkForm.append('passwd', 'what'); | ||
gbkForm.append('file', fs.createReadStream(path.join(__dirname, 'http_protocol_post_test.js'))); | ||
module.exports.request_gbk_form = { | ||
server : { | ||
host : '127.0.0.1', | ||
port : 8934 | ||
module.exports.requestGBKForm = { | ||
server: { | ||
host: '127.0.0.1', | ||
port: 8934 | ||
}, | ||
encoding : 'gbk', | ||
headers : { | ||
'Content-Type' : "multipart/form-data;boundary=" + gbk_form.getBoundary() | ||
encoding: 'gbk', | ||
headers: { | ||
'Content-Type': 'multipart/form-data;boundary=' + gbkForm.getBoundary() | ||
}, | ||
data: gbk_form | ||
data: gbkForm | ||
}; | ||
module.exports.request_404 = { | ||
module.exports.request404 = { | ||
path: '/404', | ||
server : { | ||
host : '127.0.0.1', | ||
port : 8934 | ||
server: { | ||
host: '127.0.0.1', | ||
port: 8934 | ||
} | ||
}; | ||
module.exports.request_503 = { | ||
module.exports.request503 = { | ||
path: '/error', | ||
server : { | ||
host : '127.0.0.1', | ||
port : 8934 | ||
server: { | ||
host: '127.0.0.1', | ||
port: 8934 | ||
} | ||
}; | ||
module.exports.request_with_query = { | ||
module.exports.requestWithQuery = { | ||
path: '/hello', | ||
@@ -119,12 +126,12 @@ query: { | ||
}, | ||
server : { | ||
host : '127.0.0.1', | ||
port : 8934 | ||
server: { | ||
host: '127.0.0.1', | ||
port: 8934 | ||
} | ||
}; | ||
module.exports.createServer = function(encoding){ | ||
module.exports.createServer = function (encoding) { | ||
return http.createServer(function (request, response) { | ||
var info = url.parse(request.url); | ||
var pathname =info.pathname; | ||
var pathname = info.pathname; | ||
if (pathname === '/hello' && request.method === 'POST') { | ||
@@ -134,8 +141,10 @@ var content = 'hear you'; | ||
formIn.encoding = encoding || 'utf-8'; | ||
formIn.parse(request, function(err, fields, files) { | ||
response.writeHead(200, {'content-type': 'text/plain'}); | ||
if (fields.name){ | ||
content += ' ' +fields.name; | ||
formIn.parse(request, function (err, fields, files) { | ||
response.writeHead(200, { | ||
'content-type': 'text/plain' | ||
}); | ||
if (fields.name) { | ||
content += ' ' + fields.name; | ||
} | ||
if (files.file){ | ||
if (files.file) { | ||
content += ' with file ' + files.file.name; | ||
@@ -146,3 +155,4 @@ } | ||
}); | ||
}else if (pathname === '/error' && request.method === 'POST'){ | ||
} | ||
else if (pathname === '/error' && request.method === 'POST') { | ||
response.writeHead(503, { | ||
@@ -152,3 +162,4 @@ 'Content-Type': 'text/plain' | ||
response.end(); | ||
}else{ | ||
} | ||
else { | ||
response.writeHead(404, { | ||
@@ -161,2 +172,2 @@ 'Content-Type': 'text/plain' | ||
}).listen(8934); | ||
}; | ||
}; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -18,2 +19,2 @@ * 2014/12/24 | ||
module.exports = config; | ||
module.exports = config; |
231
test/ral.js
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -7,5 +8,10 @@ * 2014/8/8 | ||
/*global before*/ | ||
/* eslint-disable max-nested-callbacks, no-console */ | ||
/* eslint-disable fecs-camelcase, camelcase */ | ||
/* eslint-disable no-wrap-func */ | ||
'use strict'; | ||
var RAL = require('../lib/ral.js'); | ||
var ral = require('../lib/ral.js'); | ||
var path = require('path'); | ||
@@ -20,3 +26,3 @@ var server = require('./ral/server.js'); | ||
beforeEach(function(){ | ||
beforeEach(function () { | ||
servers.push(server.bookService(8192)); | ||
@@ -27,13 +33,18 @@ servers.push(server.bookService(8193)); | ||
afterEach(function(){ | ||
servers.map(function(server){try{server.close()}catch(e){}}); | ||
afterEach(function () { | ||
servers.map(function (srv) { | ||
try { | ||
srv.close(); | ||
} | ||
catch (e) {} | ||
}); | ||
servers = []; | ||
}); | ||
it('show raw request time', function(done){ | ||
it('show raw request time', function (done) { | ||
var body = ''; | ||
var req = require('http').request('http://127.0.0.1:8192', function(res) { | ||
res.on('data',function(d){ | ||
var req = require('http').request('http://127.0.0.1:8192', function (res) { | ||
res.on('data', function (d) { | ||
body += d; | ||
}).on('end', function(){ | ||
}).on('end', function () { | ||
var data = JSON.parse(body); | ||
@@ -43,3 +54,3 @@ data.port.should.be.eql(8192); | ||
}); | ||
}).on('error', function(e) { | ||
}).on('error', function (e) { | ||
e.should.not.be.ok; | ||
@@ -50,4 +61,4 @@ }); | ||
it('show request time with module request', function(done){ | ||
require('request').get('http://127.0.0.1:8192', {} , function(err, response, body){ | ||
it('show request time with module request', function (done) { | ||
require('request').get('http://127.0.0.1:8192', {}, function (err, response, body) { | ||
(err === null).should.be.true; | ||
@@ -61,9 +72,9 @@ var data = JSON.parse(body); | ||
it('should init successfully', function () { | ||
RAL.init({ | ||
confDir : __dirname + path.sep + './ral/config', | ||
logger : { | ||
"log_path" : __dirname + path.sep + '../logs', | ||
"app" : "yog-ral" | ||
ral.init({ | ||
confDir: path.join(__dirname, './ral/config'), | ||
logger: { | ||
log_path: path.join(__dirname, '../logs'), | ||
app: 'yog-ral' | ||
}, | ||
currentIDC : 'tc' | ||
currentIDC: 'tc' | ||
}); | ||
@@ -74,8 +85,8 @@ isInited.emit('done'); | ||
it('should make request correctly', function (done) { | ||
before(function( ok ){ | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('GET_QS_SERV'); | ||
req.on('data', function(data){ | ||
[8192,8193].should.containEql(data.port); | ||
var req = ral('GET_QS_SERV'); | ||
req.on('data', function (data) { | ||
[8192, 8193].should.containEql(data.port); | ||
data.port.should.not.eql(8194); | ||
@@ -92,3 +103,3 @@ data.query.from.should.eql('ral'); | ||
// }); | ||
// var req = RAL('GET_QS_SERV'); | ||
// var req = ral('GET_QS_SERV'); | ||
// req.on('header', function(header){ | ||
@@ -101,6 +112,6 @@ // console.log(header); | ||
it('should make request with data correctly', function (done) { | ||
before(function( ok ){ | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('GET_QS_SERV', { | ||
var req = ral('GET_QS_SERV', { | ||
data: { | ||
@@ -111,5 +122,7 @@ msg: 'hi', | ||
}); | ||
req.on('data', function(data){ | ||
servers.map(function(server){server.close()}); | ||
[8192,8193].should.containEql(data.port); | ||
req.on('data', function (data) { | ||
servers.map(function (srv) { | ||
srv.close(); | ||
}); | ||
[8192, 8193].should.containEql(data.port); | ||
data.port.should.not.eql(8194); | ||
@@ -124,12 +137,12 @@ data.query.from.should.eql('ral'); | ||
it('should override the options correctly', function (done) { | ||
before(function( ok ){ | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('GET_QS_SERV', { | ||
path : '/404', | ||
var req = ral('GET_QS_SERV', { | ||
path: '/404', | ||
tag: 404 | ||
}); | ||
req.on('error', function(err){ | ||
req.on('error', function (err) { | ||
err.toString().should.be.match(/404/); | ||
var req_normal = RAL('GET_QS_SERV',{ | ||
var reqNormal = ral('GET_QS_SERV', { | ||
query: { | ||
@@ -139,3 +152,3 @@ normal: true | ||
}); | ||
req_normal.on('data', function(data){ | ||
reqNormal.on('data', function (data) { | ||
data.query.from.should.eql('ral'); | ||
@@ -149,13 +162,13 @@ done(); | ||
it('should get large content correctly', function (done) { | ||
before(function( ok ){ | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('GET_QS_SERV', { | ||
var req = ral('GET_QS_SERV', { | ||
path: '/largecontent' | ||
}); | ||
req.on('data', function(data){ | ||
req.on('data', function (data) { | ||
data.res.should.be.an.instanceOf(Object); | ||
done(); | ||
}); | ||
req.on('error', function(err){ | ||
req.on('error', function (err) { | ||
err.should.not.be.ok; | ||
@@ -167,6 +180,6 @@ done(); | ||
it('should report unpack error', function (done) { | ||
before(function( ok ){ | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('GET_QS_SERV', { | ||
var req = ral('GET_QS_SERV', { | ||
data: { | ||
@@ -177,3 +190,3 @@ msg: 'hi' | ||
}); | ||
req.on('error', function(err){ | ||
req.on('error', function (err) { | ||
err.toString().should.be.match(/Encoding/); | ||
@@ -185,6 +198,6 @@ done(); | ||
it('should invoke timeout', function (done) { | ||
before(function( ok ){ | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('GET_QS_SERV', { | ||
var req = ral('GET_QS_SERV', { | ||
path: '/timeout', | ||
@@ -198,6 +211,6 @@ data: { | ||
}); | ||
req.on('data', function(data){ | ||
req.on('data', function (data) { | ||
console.log(data); | ||
}); | ||
req.on('error', function(err){ | ||
req.on('error', function (err) { | ||
err.should.be.match(/request time out/); | ||
@@ -209,6 +222,6 @@ done(); | ||
it('should make POST request with form data correctly', function (done) { | ||
before(function( ok ){ | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('POST_QS_SERV', { | ||
var req = ral('POST_QS_SERV', { | ||
data: { | ||
@@ -219,5 +232,7 @@ msg: 'hi', | ||
}); | ||
req.on('data', function(data){ | ||
servers.map(function(server){server.close()}); | ||
[8192,8193].should.containEql(data.port); | ||
req.on('data', function (data) { | ||
servers.map(function (srv) { | ||
srv.close(); | ||
}); | ||
[8192, 8193].should.containEql(data.port); | ||
data.port.should.not.eql(8194); | ||
@@ -231,6 +246,6 @@ data.query.msg.should.eql('hi'); | ||
it('could change pack and unpack', function (done) { | ||
before(function( ok ){ | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('CHANGE_PACK_UNPACK', { | ||
var req = ral('CHANGE_PACK_UNPACK', { | ||
data: { | ||
@@ -245,7 +260,7 @@ msg: 'hi', | ||
}); | ||
req.on('data', function(data){ | ||
req.on('data', function (data) { | ||
data.query.msg.should.eql('hi'); | ||
data.query.name.should.eql('何方石'); | ||
data.query.from.should.eql('change'); | ||
req = RAL('CHANGE_PACK_UNPACK', { | ||
req = ral('CHANGE_PACK_UNPACK', { | ||
data: { | ||
@@ -258,3 +273,3 @@ msg: 'hi', | ||
}); | ||
req.on('error', function(err){ | ||
req.on('error', function (err) { | ||
err.message.should.be.match(/invalid pack data/); | ||
@@ -267,6 +282,6 @@ done(); | ||
it('should throw error when use a invalid service', function (done) { | ||
before(function( ok ){ | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('POST_QS_SERV_INVALID', { | ||
var req = ral('POST_QS_SERV_INVALID', { | ||
data: { | ||
@@ -277,3 +292,3 @@ msg: 'hi', | ||
}); | ||
req.on('error', function(err){ | ||
req.on('error', function (err) { | ||
err.toString().should.be.match(/invalid service name/); | ||
@@ -285,6 +300,6 @@ done(); | ||
it('should caught buffer pack error', function (done) { | ||
before(function( ok ){ | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('GET_QS_SERV', { | ||
var req = ral('GET_QS_SERV', { | ||
data: { | ||
@@ -295,3 +310,3 @@ some: 'how' | ||
}); | ||
req.on('error', function(error){ | ||
req.on('error', function (error) { | ||
error.toString().should.be.match(/Encoding not recognized/); | ||
@@ -303,9 +318,9 @@ done(); | ||
it('should caught buffer unpack error', function (done) { | ||
before(function( ok ){ | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('GET_QS_SERV', { | ||
var req = ral('GET_QS_SERV', { | ||
path: '/content' | ||
}); | ||
req.on('error', function(error){ | ||
req.on('error', function (error) { | ||
error.toString().should.be.match(/Unexpected token/); | ||
@@ -317,10 +332,10 @@ done(); | ||
it('should caught error when server failed', function (done) { | ||
before(function( ok ){ | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('GET_QS_SERV', { | ||
var req = ral('GET_QS_SERV', { | ||
path: '/close', | ||
timeout:200 | ||
timeout: 200 | ||
}); | ||
req.on('error', function(error){ | ||
req.on('error', function (error) { | ||
error.toString().should.be.match(/request time out/); | ||
@@ -332,23 +347,37 @@ done(); | ||
it('query config should independent', function (done) { | ||
before(function( ok ){ | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var query = {"pageSize":"20","pageNo":"1","catId":"5000009","catLevel":"3"}; | ||
var req = RAL('TEST_QUERY_SERV', { | ||
query:query | ||
var query = { | ||
pageSize: '20', | ||
pageNo: '1', | ||
catId: '5000009', | ||
catLevel: '3' | ||
}; | ||
var req = ral('TEST_QUERY_SERV', { | ||
query: query | ||
}); | ||
req.on('data', function(data){ | ||
req.on('data', function (data) { | ||
data.query.should.be.eql(query); | ||
query = {"pageSize":"20","pageNo":"1","keyword":"TOTO"}; | ||
var req2 = RAL('TEST_QUERY_SERV', { | ||
query = { | ||
pageSize: '20', | ||
pageNo: '1', | ||
keyword: 'TOTO' | ||
}; | ||
var req2 = ral('TEST_QUERY_SERV', { | ||
query: query | ||
}); | ||
req2.on('data', function(data){ | ||
data.query.should.be.eql(query); | ||
query = {"pageSize":"20","pageNo":"1","catId":"5000009","catLevel":"4"}; | ||
var req3 = RAL('TEST_QUERY_SERV', { | ||
req2.on('data', function (data2) { | ||
data2.query.should.be.eql(query); | ||
query = { | ||
pageSize: '20', | ||
pageNo: '1', | ||
catId: '5000009', | ||
catLevel: '4' | ||
}; | ||
var req3 = ral('TEST_QUERY_SERV', { | ||
query: query | ||
}); | ||
req3.on('data', function(data){ | ||
data.query.should.be.eql(query); | ||
req3.on('data', function (data3) { | ||
data3.query.should.be.eql(query); | ||
done(); | ||
@@ -361,12 +390,12 @@ }); | ||
it('should catch onData error when catchCallback is true', function (done) { | ||
before(function( ok ){ | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('GET_QS_SERV', { | ||
var req = ral('GET_QS_SERV', { | ||
catchCallback: true | ||
}); | ||
req.on('data', function(data){ | ||
req.on('data', function (data) { | ||
throw new Error('lalala'); | ||
}); | ||
req.on('error', function(err){ | ||
req.on('error', function (err) { | ||
err.message.should.be.match(/lalal/); | ||
@@ -377,7 +406,7 @@ done(); | ||
it('should work fine with soap', function(done) { | ||
before(function( ok ){ | ||
it('should work fine with soap', function (done) { | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('SOAP', { | ||
var req = ral('SOAP', { | ||
method: 'GetCityForecastByZIP', | ||
@@ -388,3 +417,3 @@ data: { | ||
}); | ||
req.on('data', function(data){ | ||
req.on('data', function (data) { | ||
data.GetCityForecastByZIPResult.should.be.ok; | ||
@@ -395,7 +424,7 @@ done(); | ||
it('should work fine with soap timeout', function(done) { | ||
before(function( ok ){ | ||
it('should work fine with soap timeout', function (done) { | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('SOAP', { | ||
var req = ral('SOAP', { | ||
timeout: 1, | ||
@@ -407,3 +436,3 @@ method: 'GetCityForecastByZIP', | ||
}); | ||
req.on('error', function(err){ | ||
req.on('error', function (err) { | ||
err.message.should.be.match(/request time out/); | ||
@@ -414,8 +443,8 @@ done(); | ||
it('should use right context when concurrency request', function(done) { | ||
before(function( ok ){ | ||
it('should use right context when concurrency request', function (done) { | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var count = 2; | ||
RAL('CHANGE_PACK_UNPACK', { | ||
ral('CHANGE_PACK_UNPACK', { | ||
data: { | ||
@@ -429,3 +458,3 @@ msg: 'hi', | ||
timeout: 100 | ||
}).on('data', function(data){ | ||
}).on('data', function (data) { | ||
data.query.msg.should.eql('hi'); | ||
@@ -436,3 +465,3 @@ data.query.name.should.eql('何方石'); | ||
}); | ||
RAL('CHANGE_PACK_UNPACK', { | ||
ral('CHANGE_PACK_UNPACK', { | ||
data: { | ||
@@ -446,3 +475,3 @@ msg: 'hi', | ||
timeout: 100 | ||
}).on('error', function(err){ | ||
}).on('error', function (err) { | ||
err.message.should.be.match(/invalid pack data/); | ||
@@ -459,2 +488,2 @@ partialDone(); | ||
}); | ||
}); | ||
}); |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -10,3 +11,3 @@ * 2014/8/8 | ||
module.exports = { | ||
'GET_QS_SERV': { | ||
GET_QS_SERV: { | ||
unpack: 'json', | ||
@@ -19,9 +20,17 @@ pack: 'querystring', | ||
query: 'from=ral', | ||
server: [ | ||
{ host: '127.0.0.1', port: 8192, idc: 'tc'}, | ||
{ host: '127.0.0.1', port: 8193, idc: 'tc'}, | ||
{ host: '127.0.0.1', port: 8194, idc: 'st'} | ||
] | ||
server: [{ | ||
host: '127.0.0.1', | ||
port: 8192, | ||
idc: 'tc' | ||
}, { | ||
host: '127.0.0.1', | ||
port: 8193, | ||
idc: 'tc' | ||
}, { | ||
host: '127.0.0.1', | ||
port: 8194, | ||
idc: 'st' | ||
}] | ||
}, | ||
'CHANGE_PACK_UNPACK': { | ||
CHANGE_PACK_UNPACK: { | ||
unpack: 'stream', | ||
@@ -34,9 +43,17 @@ pack: 'stream', | ||
query: 'from=change', | ||
server: [ | ||
{ host: '127.0.0.1', port: 8192, idc: 'tc'}, | ||
{ host: '127.0.0.1', port: 8193, idc: 'tc'}, | ||
{ host: '127.0.0.1', port: 8194, idc: 'st'} | ||
] | ||
server: [{ | ||
host: '127.0.0.1', | ||
port: 8192, | ||
idc: 'tc' | ||
}, { | ||
host: '127.0.0.1', | ||
port: 8193, | ||
idc: 'tc' | ||
}, { | ||
host: '127.0.0.1', | ||
port: 8194, | ||
idc: 'st' | ||
}] | ||
}, | ||
'TEST_QUERY_SERV': { | ||
TEST_QUERY_SERV: { | ||
unpack: 'json', | ||
@@ -48,7 +65,9 @@ pack: 'querystring', | ||
protocol: 'http', | ||
server: [ | ||
{ host: '127.0.0.1', port: 8193, idc: 'tc'} | ||
] | ||
server: [{ | ||
host: '127.0.0.1', | ||
port: 8193, | ||
idc: 'tc' | ||
}] | ||
}, | ||
'POST_QS_SERV': { | ||
POST_QS_SERV: { | ||
unpack: 'json', | ||
@@ -61,8 +80,16 @@ pack: 'form', | ||
query: 'from=ral', | ||
server: [ | ||
{ host: '127.0.0.1', port: 8192, idc: 'tc'}, | ||
{ host: '127.0.0.1', port: 8193, idc: 'tc'}, | ||
{ host: '127.0.0.1', port: 8194, idc: 'st'} | ||
] | ||
server: [{ | ||
host: '127.0.0.1', | ||
port: 8192, | ||
idc: 'tc' | ||
}, { | ||
host: '127.0.0.1', | ||
port: 8193, | ||
idc: 'tc' | ||
}, { | ||
host: '127.0.0.1', | ||
port: 8194, | ||
idc: 'st' | ||
}] | ||
} | ||
}; | ||
}; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -17,8 +18,6 @@ * 2014/12/24 | ||
timeout: 100000, | ||
server: [ | ||
{ | ||
host: 'wsf.cdyne.com', | ||
port: 80 | ||
} | ||
] | ||
}; | ||
server: [{ | ||
host: 'wsf.cdyne.com', | ||
port: 80 | ||
}] | ||
}; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -16,7 +17,7 @@ * 2014/8/8 | ||
module.exports.bookService = function(port, encoding){ | ||
module.exports.bookService = function (port, encoding) { | ||
return http.createServer(function (request, response) { | ||
var info = url.parse(request.url); | ||
var pathname =info.pathname; | ||
if (pathname === '/error'){ | ||
var pathname = info.pathname; | ||
if (pathname === '/error') { | ||
response.writeHead(503, { | ||
@@ -26,3 +27,4 @@ 'Content-Type': 'text/plain' | ||
response.end(); | ||
}else if (pathname === '/404'){ | ||
} | ||
else if (pathname === '/404') { | ||
response.writeHead(404, { | ||
@@ -33,8 +35,10 @@ 'Content-Type': 'text/plain' | ||
response.end(); | ||
}else if (pathname === '/largecontent'){ | ||
} | ||
else if (pathname === '/largecontent') { | ||
response.writeHead(200, { | ||
'Content-Type': 'text/plain' | ||
}); | ||
fs.createReadStream(__dirname + path.sep + './data/bigone.json').pipe(response); | ||
}else if (pathname === '/timeout'){ | ||
fs.createReadStream(path.join(__dirname, './data/bigone.json')).pipe(response); | ||
} | ||
else if (pathname === '/timeout') { | ||
response.writeHead(200, { | ||
@@ -48,22 +52,25 @@ 'Content-Type': 'text/plain' | ||
response.write(str.slice(0, 1)); | ||
setTimeout(function(){ | ||
setTimeout(function () { | ||
response.write(str.slice(1)); | ||
response.end(); | ||
},200) | ||
}else if (pathname === '/content'){ | ||
}, 200); | ||
} | ||
else if (pathname === '/content') { | ||
response.writeHead(200, { | ||
'Content-Type': 'text/plain' | ||
}); | ||
response.write("aabb"); | ||
response.write('aabb'); | ||
response.end(); | ||
}else if (pathname === '/close'){ | ||
} | ||
else if (pathname === '/close') { | ||
this.close(); | ||
}else{ | ||
} | ||
else { | ||
response.writeHead(200, { | ||
'Content-Type': 'application/json' | ||
}); | ||
if (request.method == 'POST'){ | ||
if (request.method === 'POST') { | ||
var formIn = new formidable.IncomingForm(); | ||
formIn.encoding = encoding || 'utf-8'; | ||
formIn.parse(request, function(err, fields) { | ||
formIn.parse(request, function (err, fields) { | ||
response.write(JSON.stringify({ | ||
@@ -75,3 +82,4 @@ port: port, | ||
}); | ||
}else{ | ||
} | ||
else { | ||
response.write(JSON.stringify({ | ||
@@ -85,2 +93,2 @@ port: port, | ||
}).listen(port); | ||
}; | ||
}; |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -7,2 +8,4 @@ * 2014/8/8 | ||
/* eslint-disable no-wrap-func */ | ||
'use strict'; | ||
@@ -16,3 +19,3 @@ | ||
it('should load ext successfuly', function () { | ||
RalModule.load(__dirname + path.sep + '../lib/ext'); | ||
RalModule.load(path.join(__dirname, '../lib/ext')); | ||
RalModule.modules.balance.random.should.be.ok; | ||
@@ -31,12 +34,12 @@ RalModule.modules.balance.roundrobin.should.be.ok; | ||
RalModule.load({ | ||
getCategory: function(){ | ||
return "a"; | ||
getCategory: function () { | ||
return 'a'; | ||
} | ||
}); | ||
RalModule.load({ | ||
getCategory: function(){ | ||
return "b"; | ||
getCategory: function () { | ||
return 'b'; | ||
}, | ||
getName: function(){ | ||
return "c"; | ||
getName: function () { | ||
return 'c'; | ||
} | ||
@@ -49,10 +52,14 @@ }); | ||
it('should throw error for unimplemented ralmodule', function () { | ||
function mock (){ | ||
function mock() { | ||
RalModule.call(this); | ||
} | ||
util.inherits(mock, RalModule); | ||
(function(){mock.getName()}).should.throwError(); | ||
(function(){mock.getCategory()}).should.throwError(); | ||
(function () { | ||
mock.getName(); | ||
}).should.throwError(); | ||
(function () { | ||
mock.getCategory(); | ||
}).should.throwError(); | ||
}); | ||
}); |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -7,2 +8,4 @@ * 2014/8/20 | ||
/* eslint-disable no-wrap-func */ | ||
'use strict'; | ||
@@ -14,7 +17,6 @@ | ||
var RalModule = require('../lib/ralmodule.js'); | ||
var CTX = require('../lib/ctx.js'); | ||
var configUpdater = require('../lib/config/configUpdater.js'); | ||
var normalizeManager = require('../lib/config.js').normalizerManager; | ||
RalModule.load(__dirname + path.sep + '../lib/ext'); | ||
RalModule.load(path.join(__dirname, '../lib/ext')); | ||
@@ -24,3 +26,3 @@ describe('config updater', function () { | ||
it('update config successful', function (done) { | ||
config.load(__dirname + path.sep + './update/config'); | ||
config.load(path.join(__dirname, './update/config')); | ||
configUpdater.update(function (err, conf) { | ||
@@ -33,3 +35,3 @@ conf.SIMPLE.__from__.should.be.startWith('updater'); | ||
it('can\'t run update twice at same time', function (done) { | ||
config.load(__dirname + path.sep + './update/config'); | ||
config.load(path.join(__dirname, './update/config')); | ||
configUpdater.update(function (err, conf) {}, true); | ||
@@ -43,10 +45,9 @@ configUpdater.update(function (err, conf) { | ||
it('updater will create cache folder on start', function () { | ||
var tmpPath = __dirname + path.sep + './tmp'; | ||
var filePath = __dirname + path.sep + './tmp/data_cache.json'; | ||
var tmpPath = path.join(__dirname, './tmp'); | ||
var filePath = path.join(__dirname, './tmp/data_cache.json'); | ||
try { | ||
fs.unlinkSync(filePath); | ||
fs.rmdirSync(tmpPath); | ||
} catch (e) { | ||
} | ||
catch (e) {} | ||
var myUpdater = new configUpdater.Updater({ | ||
@@ -56,2 +57,3 @@ tmpPath: tmpPath, | ||
}); | ||
myUpdater.should.be.ok; | ||
fs.existsSync(tmpPath).should.be.true; | ||
@@ -61,4 +63,4 @@ }); | ||
it('updater will delete cache file on start', function () { | ||
var tmpPath = __dirname + path.sep + './tmp'; | ||
var filePath = __dirname + path.sep + './tmp/data_cache.json'; | ||
var tmpPath = path.join(__dirname, './tmp'); | ||
var filePath = path.join(__dirname, './tmp/data_cache.json'); | ||
try { | ||
@@ -70,3 +72,4 @@ if (!fs.existsSync(tmpPath)) { | ||
fs.closeSync(fd); | ||
} catch (e) {} | ||
} | ||
catch (e) {} | ||
fs.existsSync(filePath).should.be.true; | ||
@@ -77,2 +80,3 @@ var myUpdater = new configUpdater.Updater({ | ||
}); | ||
myUpdater.should.be.ok; | ||
fs.existsSync(filePath).should.be.false; | ||
@@ -82,5 +86,5 @@ }); | ||
it('updater will fail when normlizer throw error', function (done) { | ||
var tmpPath = __dirname + path.sep + './tmp'; | ||
var filePath = __dirname + path.sep + './tmp/data_cache.json'; | ||
config.load(__dirname + path.sep + './update/config'); | ||
var tmpPath = path.join(__dirname, './tmp'); | ||
var filePath = path.join(__dirname, './tmp/data_cache.json'); | ||
config.load(path.join(__dirname, './update/config')); | ||
var fake = { | ||
@@ -105,4 +109,4 @@ normalizeConfig: function () { | ||
var fake = { | ||
normalizeConfig: function (config) { | ||
return config; | ||
normalizeConfig: function (conf) { | ||
return conf; | ||
}, | ||
@@ -114,3 +118,3 @@ needUpdate: function () { | ||
normalizeManager.setConfigNormalizer([fake, 'default']); | ||
config.load(__dirname + path.sep + './update/config'); | ||
config.load(path.join(__dirname, './update/config')); | ||
config.isAutoUpdateEnabled().should.be.true; | ||
@@ -124,4 +128,4 @@ config.disableUpdate(); | ||
var fake = { | ||
normalizeConfig: function (config) { | ||
return config; | ||
normalizeConfig: function (conf) { | ||
return conf; | ||
}, | ||
@@ -133,8 +137,9 @@ needUpdate: function () { | ||
normalizeManager.setConfigNormalizer([fake, 'default']); | ||
config.load(__dirname + path.sep + './update/config'); | ||
//delete config cache produced by previous tests | ||
var filePath = __dirname + path.sep + '../tmp/config_cache.json'; | ||
config.load(path.join(__dirname, './update/config')); | ||
// delete config cache produced by previous tests | ||
var filePath = path.join(__dirname, '../tmp/config_cache.json'); | ||
try { | ||
fs.unlinkSync(filePath); | ||
} catch (e) {} | ||
} | ||
catch (e) {} | ||
config.enableUpdate(100, false, function (err, confs) { | ||
@@ -150,7 +155,7 @@ confs.should.be.eql({}); | ||
var fake = { | ||
normalizeConfig: function (config) { | ||
return config; | ||
normalizeConfig: function (conf) { | ||
return conf; | ||
}, | ||
needUpdate: function () { | ||
//need update | ||
// need update | ||
return true; | ||
@@ -160,8 +165,9 @@ } | ||
normalizeManager.setConfigNormalizer([fake, 'default']); | ||
config.load(__dirname + path.sep + './update/config'); | ||
//delete config cache produced by previous tests | ||
var filePath = __dirname + path.sep + '../tmp/config_cache.json'; | ||
config.load(path.join(__dirname, './update/config')); | ||
// delete config cache produced by previous tests | ||
var filePath = path.join(__dirname, '../tmp/config_cache.json'); | ||
try { | ||
fs.unlinkSync(filePath); | ||
} catch (e) {} | ||
} | ||
catch (e) {} | ||
config.enableUpdate(100, false, function (err, confs) { | ||
@@ -177,8 +183,8 @@ confs.SIMPLE.__from__.should.be.match(/updater/); | ||
var fake = { | ||
normalizeConfig: function (config) { | ||
config._TEST_RAW_CONF_ = true; | ||
return config; | ||
normalizeConfig: function (conf) { | ||
conf._TEST_RAW_CONF_ = true; | ||
return conf; | ||
}, | ||
needUpdate: function () { | ||
//need update | ||
// need update | ||
return true; | ||
@@ -188,8 +194,9 @@ } | ||
normalizeManager.setConfigNormalizer([fake, 'default']); | ||
config.load(__dirname + path.sep + './update/config'); | ||
//delete config cache produced by previous tests | ||
var filePath = __dirname + path.sep + '../tmp/config_cache.json'; | ||
config.load(path.join(__dirname, './update/config')); | ||
// delete config cache produced by previous tests | ||
var filePath = path.join(__dirname, '../tmp/config_cache.json'); | ||
try { | ||
fs.unlinkSync(filePath); | ||
} catch (e) {} | ||
} | ||
catch (e) {} | ||
config.enableUpdate(100, false, function (err, confs) { | ||
@@ -206,2 +213,2 @@ confs.SIMPLE.__from__.should.be.match(/updater/); | ||
}); | ||
}); | ||
}); |
@@ -1,3 +0,4 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
@@ -11,9 +12,11 @@ * 2014/8/20 | ||
module.exports = { | ||
'SIMPLE': { | ||
SIMPLE: { | ||
balance: 'random', | ||
protocol: 'http', | ||
server: [ | ||
{ host: '127.0.0.1', port: 8192, idc: 'tc'}, | ||
] | ||
server: [{ | ||
host: '127.0.0.1', | ||
port: 8192, | ||
idc: 'tc' | ||
}] | ||
} | ||
}; | ||
}; |
@@ -1,7 +0,11 @@ | ||
/* | ||
* fis | ||
/** | ||
* @file node-ral | ||
* @author hefangshi@baidu.com | ||
* http://fis.baidu.com/ | ||
* 2014/8/20 | ||
* | ||
*/ | ||
/* eslint-disable no-console */ | ||
'use strict'; | ||
@@ -16,6 +20,4 @@ | ||
if (cluster.isMaster) { | ||
console.log("master start..."); | ||
console.log('master start...'); | ||
// Fork workers. | ||
@@ -25,26 +27,27 @@ for (var i = 0; i < numCPUs; i++) { | ||
} | ||
} else { | ||
RalModule.load(__dirname + path.sep + '../../lib/ext'); | ||
} | ||
else { | ||
RalModule.load(path(__dirname, '../../lib/ext')); | ||
config.load(__dirname + path.sep + '../update/config'); | ||
config.load(path(__dirname, '../update/config')); | ||
console.log('cluster:', cluster.worker.id.toString(), 'config come from', config.getConf('SIMPLE').__from__); | ||
configUpdater.on('read:fail', function(){ | ||
configUpdater.on('read:fail', function () { | ||
console.log('cluster:', cluster.worker.id.toString(), 'read fail'); | ||
}); | ||
configUpdater.on('read:succ', function(){ | ||
configUpdater.on('read:succ', function () { | ||
console.log('cluster:', cluster.worker.id.toString(), 'read succ'); | ||
console.log('cluster:', cluster.worker.id.toString(), 'config come from', config.getConf('SIMPLE').__from__); | ||
}); | ||
configUpdater.on('read:error', function(){ | ||
configUpdater.on('read:error', function () { | ||
console.log('cluster:', cluster.worker.id.toString(), 'read error'); | ||
}); | ||
configUpdater.on('wait', function(){ | ||
configUpdater.on('wait', function () { | ||
console.log('cluster:', cluster.worker.id.toString(), 'wait'); | ||
}); | ||
configUpdater.on('update:start', function(){ | ||
configUpdater.on('update:start', function () { | ||
console.log('cluster:', cluster.worker.id.toString(), 'update start'); | ||
}); | ||
configUpdater.on('update:end', function(){ | ||
configUpdater.on('update:end', function () { | ||
console.log('cluster:', cluster.worker.id.toString(), 'update end'); | ||
@@ -51,0 +54,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
281762
7923
8
+ Addedfast-strftime@1.1.1(transitive)
+ Addedyog-log@0.0.16(transitive)
- Removedyog-log@0.0.10(transitive)
Updatedyog-log@0.0.16