Comparing version 0.0.42 to 0.1.0
@@ -10,8 +10,5 @@ /** | ||
var Protocol = require('../../protocol.js'); | ||
var Protocol = require('./httpProtocolBase.js'); | ||
var logger = require('../../logger.js')('HttpProtocol'); | ||
var util = require('util'); | ||
var Stream = require('stream').Stream; | ||
var urlencode = require('urlencode'); | ||
var zlib = require('zlib'); | ||
@@ -24,2 +21,4 @@ function HttpProtocol() { | ||
HttpProtocol.normalizeConfig = Protocol.prototype.normalizeConfig; | ||
HttpProtocol.prototype.getName = function () { | ||
@@ -29,132 +28,2 @@ return 'http'; | ||
HttpProtocol.prototype.normalizeConfig = HttpProtocol.normalizeConfig = function (config) { | ||
config = Protocol.normalizeConfig(config); | ||
if (config.disableGzip === undefined) { | ||
config.disableGzip = true; | ||
} | ||
if (typeof config.query !== 'object') { | ||
config.query = urlencode.parse(config.query, { | ||
charset: config.encoding | ||
}); | ||
} | ||
if (config.path && config.path[0] !== '/') { | ||
config.path = '/' + config.path; | ||
} | ||
return config; | ||
}; | ||
HttpProtocol.prototype._request = function (config, callback) { | ||
var response = new ResponseStream(); | ||
var query = urlencode.stringify(config.query, { | ||
charset: config.encoding | ||
}); | ||
var piped = false; | ||
var path; | ||
if (query) { | ||
// didn't handle # situation since backend should not get a hash tag | ||
if (config.path.indexOf('?') === -1) { | ||
path = config.path + '?' + query; | ||
} | ||
else { | ||
path = config.path + '&' + query; | ||
} | ||
} | ||
else { | ||
path = config.path; | ||
} | ||
if (config.disableGzip) { | ||
if (config.headers && config.headers['accept-encoding']) { | ||
config.headers['accept-encoding'] = ''; | ||
} | ||
} | ||
var opt = { | ||
host: config.server.host, | ||
port: config.server.port, | ||
path: path, | ||
method: config.method, | ||
headers: config.headers, | ||
// disable http pool to avoid connect problem https://github.com/mikeal/request/issues/465 | ||
agent: false | ||
}; | ||
var request; | ||
if (config.https) { | ||
request = require('https'); | ||
opt.key = config.key; | ||
opt.cert = config.cert; | ||
opt.rejectUnauthorized = config.rejectUnauthorized; | ||
} | ||
else { | ||
request = require('http'); | ||
} | ||
logger.trace('request start ' + JSON.stringify(opt)); | ||
var req = request.request(opt, function (res) { | ||
if (res.statusCode >= 300 && !config.ignoreStatusCode) { | ||
req.emit('error', new Error('Server Status Error: ' + res.statusCode)); | ||
} | ||
// 添加 gzip与deflate 处理 | ||
switch (config.headers ? res.headers['content-encoding'] : '') { | ||
// or, just use zlib.createUnzip() to handle both cases | ||
case 'gzip': | ||
res.pipe(zlib.createGunzip()).pipe(response); | ||
break; | ||
case 'deflate': | ||
res.pipe(zlib.createInflate()).pipe(response); | ||
break; | ||
default: | ||
res.pipe(response); | ||
break; | ||
} | ||
callback && callback(response); | ||
}); | ||
if (config.payload) { | ||
req.write(config.payload); | ||
req.end(); | ||
} | ||
else { | ||
// auto end if no pipe | ||
process.nextTick(function () { | ||
piped || req.end(); | ||
}); | ||
} | ||
req.on('pipe', function () { | ||
piped = true; | ||
}); | ||
return req; | ||
}; | ||
function ResponseStream() { | ||
this.writable = true; | ||
this.data = null; | ||
this.chunks = []; | ||
} | ||
util.inherits(ResponseStream, Stream); | ||
ResponseStream.prototype.write = function (chunk) { | ||
// store the data | ||
this.chunks.push(chunk); | ||
}; | ||
ResponseStream.prototype.end = function () { | ||
var data = null; | ||
try { | ||
data = Buffer.concat(this.chunks); | ||
this.chunks = []; | ||
logger.trace('response end'); | ||
} | ||
catch (ex) { | ||
logger.trace('response failed errmsg=' + ex.message); | ||
this.emit('error', ex); | ||
return; | ||
} | ||
// emit data at once | ||
this.emit('data', data); | ||
this.emit('end'); | ||
}; | ||
module.exports = HttpProtocol; |
@@ -298,2 +298,3 @@ /** | ||
path: this.conf.path, | ||
query: JSON.stringify(this.conf.query), | ||
remote: this.conf.server.host + ':' + this.conf.server.port, | ||
@@ -392,4 +393,5 @@ cost: this.timer.context.request.cost.toFixed(3), | ||
config.load(options.confDir); | ||
ctx.DEBUG = (process.env.RAL_DEBUG === 'true'); | ||
}; | ||
module.exports = RAL; |
{ | ||
"name": "node-ral", | ||
"version": "0.0.42", | ||
"version": "0.1.0", | ||
"description": "a rpc client for node", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
node-ral | ||
=========== | ||
[![Build Status](https://travis-ci.org/fex-team/node-ral.svg?branch=master)](https://travis-ci.org/fex-team/node-ral) | ||
[![Coverage Status](https://coveralls.io/repos/fex-team/node-ral/badge.png)](https://coveralls.io/r/fex-team/node-ral) | ||
[![Build Status][travis-image]][travis-url] [![Coveralls Status][coveralls-image]][coveralls-url] [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] | ||
`node-ral` 是一个专为 `Node` 服务端应用打造的一款工业级后端服务管理库,它的特色是统一了各种通信协议、数据格式的请求接口,提供了集中化的服务资源配置管理能力,以及完善的异常处理和日志记录。 | ||
@@ -142,1 +142,11 @@ | ||
请查阅文档[WIKI](https://github.com/fex-team/node-ral/wiki) | ||
[downloads-image]: http://img.shields.io/npm/dm/node-ral.svg | ||
[npm-url]: https://npmjs.org/package/node-ral | ||
[npm-image]: http://img.shields.io/npm/v/node-ral.svg | ||
[travis-url]: https://travis-ci.org/fex-team/node-ral | ||
[travis-image]: http://img.shields.io/travis/fex-team/node-ral.svg | ||
[coveralls-url]: https://coveralls.io/r/fex-team/node-ral | ||
[coveralls-image]: http://img.shields.io/coveralls/fex-team/node-ral/master.svg |
@@ -15,2 +15,3 @@ /** | ||
var HttpProtocol = require('../lib/ext/protocol/httpProtocol.js'); | ||
var HttpsProtocol = require('../lib/ext/protocol/httpsProtocol.js'); | ||
var SoapProtocol = require('../lib/ext/protocol/soapProtocol.js'); | ||
@@ -142,6 +143,21 @@ var util = require('../lib/util.js'); | ||
stream.on('error', function (err) { | ||
console.log(err); | ||
err.should.be.null; | ||
}); | ||
}); | ||
it('should work well with https protocol', function (done) { | ||
var getTest = require('./protocol/http_protocol_get_test.js'); | ||
// start a http server for get | ||
// var server = getTest.createServer(); | ||
var httpsProtocol = new HttpsProtocol(); | ||
var context = HttpsProtocol.normalizeConfig(getTest.requestHttpsWithProtocol); | ||
var stream = httpsProtocol.talk(context, function (res) { | ||
res.on('data', function (data) { | ||
done(); | ||
}); | ||
}); | ||
stream.on('error', function (err) { | ||
err.should.be.null; | ||
}); | ||
}); | ||
}); | ||
@@ -287,2 +303,3 @@ | ||
}); | ||
}); |
@@ -65,2 +65,12 @@ /** | ||
module.exports.requestHttpsWithProtocol = { | ||
method: 'GET', | ||
path: '/', | ||
rejectUnauthorized: false, | ||
server: { | ||
host: 'travis-ci.org', | ||
port: 443 | ||
} | ||
}; | ||
module.exports.createServer = function () { | ||
@@ -67,0 +77,0 @@ return http.createServer(function (request, response) { |
@@ -97,3 +97,3 @@ /** | ||
it('auto updater should be triggered is normalizer need update', function (done) { | ||
it.skip('auto updater should be triggered is normalizer need update', function (done) { | ||
var fake = { | ||
@@ -100,0 +100,0 @@ normalizeConfig: function (conf) { |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
289144
67
8117
152
9