Comparing version 0.0.28 to 0.0.29
@@ -205,6 +205,6 @@ /* | ||
var context = {}; | ||
context.unpack = RalModule.modules.converter[serviceInfo.unpack].unpack; | ||
context.pack = RalModule.modules.converter[serviceInfo.pack].pack; | ||
context.unpackConverter = RalModule.modules.converter[serviceInfo.unpack]; | ||
context.packConverter = RalModule.modules.converter[serviceInfo.pack]; | ||
// context.unpack = RalModule.modules.converter[serviceInfo.unpack].unpack; | ||
// context.pack = RalModule.modules.converter[serviceInfo.pack].pack; | ||
// context.unpackConverter = RalModule.modules.converter[serviceInfo.unpack]; | ||
// context.packConverter = RalModule.modules.converter[serviceInfo.pack]; | ||
context.protocol = RalModule.modules.protocol[serviceInfo.protocol]; | ||
@@ -215,2 +215,9 @@ context.balance = RalModule.modules.balance[serviceInfo.balance]; | ||
context.protocolContext = context.protocol.normalizeConfig(serviceInfo); | ||
context.update = function(options){ | ||
context.packConverter = RalModule.modules.converter[options.pack]; | ||
context.pack = context.packConverter.pack; | ||
context.unpackConverter = RalModule.modules.converter[options.unpack]; | ||
context.unpack = context.unpackConverter.unpack; | ||
}; | ||
contextCache[serviceID] = context; | ||
@@ -217,0 +224,0 @@ } |
@@ -12,3 +12,5 @@ /* | ||
var util = require('util'); | ||
var PassThrough = require('stream').PassThrough; | ||
function StreamConverter() { | ||
@@ -21,3 +23,3 @@ Converter.call(this); | ||
StreamConverter.prototype.unpack = function (config, data) { | ||
return data; | ||
return new PassThrough(); | ||
}; | ||
@@ -24,0 +26,0 @@ |
@@ -19,2 +19,3 @@ /* | ||
Timer = require('./timer.js'), | ||
stream = require('stream'), | ||
path = require('path'); | ||
@@ -42,4 +43,5 @@ | ||
if (!conf){ | ||
var error = new Error('invalid service name'); | ||
process.nextTick(function(){ | ||
me.emit('error', new Error('invalid service name')) | ||
me.emit('error', error); | ||
}); | ||
@@ -53,2 +55,4 @@ }else{ | ||
context.update(conf); | ||
this.on('retry', function(err){ | ||
@@ -151,5 +155,9 @@ if (this._retryTimes >= conf.retry){ | ||
*/ | ||
function unpackResponse (){ | ||
function unpackResponse (){ | ||
if (context.unpackConverter.isStreamify){ | ||
unpack = context.unpack(conf); | ||
if (unpack instanceof stream.Stream === false){ | ||
onError(new Error('invalid unpack data: not a stream but isStreamify is true')); | ||
return; | ||
} | ||
unpack.on('error', callRetry); | ||
@@ -159,3 +167,3 @@ unpack.once('end', function(){ | ||
}); | ||
unpack.on('data', onData); | ||
onData(unpack); | ||
response.pipe(unpack); | ||
@@ -234,2 +242,6 @@ }else{ | ||
if (payload && context.packConverter.isStreamify) { | ||
if (payload instanceof stream.Stream === false){ | ||
onError(new Error('invalid pack data: not a stream but isStreamify is true')); | ||
return; | ||
} | ||
//transport error event from pack | ||
@@ -236,0 +248,0 @@ payload.on('error', onError); |
{ | ||
"name": "node-ral", | ||
"version": "0.0.28", | ||
"version": "0.0.29", | ||
"description": "a rpc client for node", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -199,2 +199,35 @@ /* | ||
it('could change pack and unpack', function (done) { | ||
before(function( ok ){ | ||
isInited.on('done', ok); | ||
}); | ||
var req = RAL('CHANGE_PACK_UNPACK', { | ||
data: { | ||
msg: 'hi', | ||
name: '何方石' | ||
}, | ||
pack: 'querystring', | ||
unpack: 'json', | ||
retry: 2, | ||
timeout: 100 | ||
}); | ||
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', { | ||
data: { | ||
msg: 'hi', | ||
name: '何方石' | ||
}, | ||
retry: 2, | ||
timeout: 100 | ||
}); | ||
req.on('error', function(err){ | ||
err.message.should.be.match(/invalid pack data/); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should throw error when use a invalid service', function (done) { | ||
@@ -201,0 +234,0 @@ before(function( ok ){ |
@@ -24,2 +24,16 @@ /* | ||
}, | ||
'CHANGE_PACK_UNPACK': { | ||
unpack: 'stream', | ||
pack: 'stream', | ||
method: 'GET', | ||
encoding: 'utf-8', | ||
balance: 'random', | ||
protocol: 'http', | ||
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'} | ||
] | ||
}, | ||
'TEST_QUERY_SERV': { | ||
@@ -26,0 +40,0 @@ unpack: 'json', |
267472
7325