Comparing version 0.17.6 to 0.18.0
@@ -29,7 +29,7 @@ /** | ||
HttpProtocolBase.prototype.getName = function() { | ||
HttpProtocolBase.prototype.getName = function () { | ||
throw new Error('not implement'); | ||
}; | ||
HttpProtocolBase.prototype.normalizeConfig = HttpProtocolBase.normalizeConfig = function(config) { | ||
HttpProtocolBase.prototype.normalizeConfig = HttpProtocolBase.normalizeConfig = function (config) { | ||
config = Protocol.normalizeConfig(config); | ||
@@ -76,53 +76,52 @@ if (config.disableGzip === undefined) { | ||
HttpProtocolBase.prototype._prepareRequest = function(config) { | ||
config.requestPrepared = true; | ||
var path; | ||
HttpProtocolBase.prototype._prepareRequest = function (config) { | ||
if (!config.reqPathPrepared) { | ||
var query = urlencode.stringify(config.query, { | ||
charset: config.encoding | ||
}); | ||
var query = urlencode.stringify(config.query, { | ||
charset: config.encoding | ||
}); | ||
if (query) { | ||
// didn't handle # situation since backend should not get a hash tag | ||
if (config.path.indexOf('?') === -1) { | ||
path = config.path + '?' + query; | ||
if (query) { | ||
// didn't handle # situation since backend should not get a hash tag | ||
if (config.path.indexOf('?') === -1) { | ||
config.realPath = config.path + '?' + query; | ||
} else { | ||
config.realPath = config.path + '&' + query; | ||
} | ||
} else { | ||
path = config.path + '&' + query; | ||
config.realPath = config.path; | ||
} | ||
} else { | ||
path = config.path; | ||
} | ||
if (config.disableGzip) { | ||
if (config.headers && config.headers['accept-encoding']) { | ||
config.headers['accept-encoding'] = ''; | ||
if (config.url) { | ||
var urlparsed = urlParse(config.url); | ||
if (urlparsed.protocol) { | ||
config.https = urlparsed.protocol === 'https:'; | ||
config.server = { | ||
host: urlparsed.hostname, | ||
port: parseInt(urlparsed.port, 10) | ||
}; | ||
config.server.port = config.server.port || (config.https ? 443 : 80); | ||
config.realPath = urlparsed.path; | ||
} | ||
} | ||
} | ||
// detect if there is empty value in headers | ||
if (config.headers) { | ||
var keys = Object.keys(config.headers); | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
if (config.headers[key] === undefined || config.headers[key] === null) { | ||
config.headers[key] = ''; | ||
// handle gzip | ||
if (config.disableGzip) { | ||
if (config.headers && config.headers['accept-encoding']) { | ||
config.headers['accept-encoding'] = ''; | ||
} | ||
} | ||
} | ||
if (config.url) { | ||
var urlparsed = urlParse(config.url); | ||
if (urlparsed.protocol) { | ||
config.https = urlparsed.protocol === 'https:'; | ||
config.server = { | ||
host: urlparsed.hostname, | ||
port: parseInt(urlparsed.port, 10) | ||
}; | ||
config.server.port = config.server.port || (config.https ? 443 : 80); | ||
path = urlparsed.path; | ||
// detect if there is empty value in headers | ||
if (config.headers) { | ||
var keys = Object.keys(config.headers); | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
if (config.headers[key] === undefined || config.headers[key] === null) { | ||
config.headers[key] = ''; | ||
} | ||
} | ||
} | ||
} | ||
config.realPath = path; | ||
var opt = { | ||
@@ -148,3 +147,2 @@ host: config.server.host, | ||
var proxy = config.proxy || getProxyFromURI(opt); | ||
if (proxy) { | ||
@@ -160,3 +158,3 @@ var uri = opt.protocol + '//' + opt.host + ':' + opt.port; | ||
} else { | ||
opt.path = url.resolve(uri, path); | ||
opt.path = url.resolve(uri, config.realPath); | ||
var proxyUri = url.parse(proxy); | ||
@@ -167,7 +165,7 @@ opt.host = proxyUri.hostname; | ||
} | ||
config.requestOpt = opt; | ||
config.reqPathPrepared = true; | ||
return opt; | ||
}; | ||
HttpProtocolBase.prototype._request = function(config, callback) { | ||
HttpProtocolBase.prototype._request = function (config, callback) { | ||
var response = new ResponseStream(); | ||
@@ -177,7 +175,3 @@ var piped = false; | ||
if (!config.requestPrepared) { | ||
opt = this._prepareRequest(config); | ||
} else { | ||
opt = config.requestOpt; | ||
} | ||
opt = this._prepareRequest(config); | ||
@@ -193,3 +187,3 @@ var request; | ||
// logger.trace('request start ' + JSON.stringify(opt)); | ||
var req = request.request(opt, function(res) { | ||
var req = request.request(opt, function (res) { | ||
if (res.statusCode >= 300 && !config.ignoreStatusCode) { | ||
@@ -215,3 +209,3 @@ var statusCodeError = new Error('Server Status Error: ' + res.statusCode); | ||
if (stream) { | ||
stream.on('error', function(error) { | ||
stream.on('error', function (error) { | ||
response.emit('error', error); | ||
@@ -236,7 +230,7 @@ }); | ||
// auto end if no pipe | ||
setImmediate(function() { | ||
setImmediate(function () { | ||
piped || req.end(); | ||
}); | ||
} | ||
req.on('pipe', function() { | ||
req.on('pipe', function () { | ||
piped = true; | ||
@@ -255,3 +249,3 @@ }); | ||
ResponseStream.prototype.write = function(chunk) { | ||
ResponseStream.prototype.write = function (chunk) { | ||
// store the data | ||
@@ -261,3 +255,3 @@ this.chunks.push(chunk); | ||
ResponseStream.prototype.end = function() { | ||
ResponseStream.prototype.end = function () { | ||
var data = null; | ||
@@ -264,0 +258,0 @@ try { |
@@ -24,2 +24,12 @@ /** | ||
Procotol.prototype.beforeRequest = Procotol.beforeRequest = function (context) { | ||
if (context.beforeRequest && typeof context.beforeRequest === 'function') { | ||
context = context.beforeRequest(context); | ||
if (!context) { | ||
throw new Error('Request was canceled by beforeRequest hook since returned context was ' + context); | ||
} | ||
} | ||
return context; | ||
}; | ||
/** | ||
@@ -34,2 +44,3 @@ * communicate with server, talk will return the request object to receive payload stream | ||
Procotol.prototype.talk = function (config, callback) { | ||
config = this.beforeRequest(config); | ||
return this._request(config, callback); | ||
@@ -36,0 +47,0 @@ }; |
{ | ||
"name": "node-ral", | ||
"version": "0.17.6", | ||
"version": "0.18.0", | ||
"description": "a rpc client for node", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -28,2 +28,3 @@ /** | ||
servers.push(server.oddFail(8192)); | ||
servers.push(server.oddFail(8193)); | ||
}); | ||
@@ -67,2 +68,20 @@ | ||
it('don\'t retry on same machine', function (done) { | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
var req = ral('retry_on_same_machine', { | ||
beforeRequest: function (context) { | ||
return context; | ||
} | ||
}); | ||
req.on('data', function (data) { | ||
throw new Error('should be failed') | ||
}); | ||
req.on('error', function (err) { | ||
err.should.be.ok(); | ||
done(); | ||
}); | ||
}) | ||
}); |
@@ -498,2 +498,52 @@ /** | ||
describe('beforeRequest hook', function () { | ||
it('should trigger before request', 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(getTest.service); | ||
context.path = '/gziperror'; | ||
context.disableGzip = false; | ||
context.beforeRequest = function (context) { | ||
context.server.port.should.be.eql(8934); | ||
context.server.host.should.be.eql('127.0.0.1'); | ||
return context; | ||
} | ||
util.merge(context, getTest.request); | ||
httpProtocol.talk(context, function (res) { | ||
res.on('end', function (data) { | ||
server.close(); | ||
data.toString().should.not.be.ok; | ||
done(); | ||
}); | ||
res.on('error', function (err) { | ||
server.close(); | ||
err.message.should.be.match(/unexpected end of file/); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should cancel request by return false', function () { | ||
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(getTest.service); | ||
context.path = '/gziperror'; | ||
context.disableGzip = false; | ||
context.beforeRequest = function (context) { | ||
return false; | ||
} | ||
util.merge(context, getTest.request); | ||
try { | ||
httpProtocol.talk(context); | ||
} catch (ex) { | ||
// server.close(); | ||
ex.message.toString().should.be.eql('Request was canceled by beforeRequest hook since returned context was false'); | ||
} | ||
}); | ||
}); | ||
describe('soap protocol', function () { | ||
@@ -500,0 +550,0 @@ it.skip('should request wsdl service successfully', function (done) { |
@@ -172,3 +172,3 @@ /** | ||
var padding = []; | ||
for (var i = 0; i < 100000; i++) { | ||
for (var i = 0; i < 1000; i++) { | ||
response.write(i.toString()); | ||
@@ -175,0 +175,0 @@ } |
379103
102
11046