Comparing version 0.4.2 to 0.4.3
@@ -14,3 +14,3 @@ const Rocky = require('./lib/rocky') | ||
/** | ||
* Internal modules | ||
* Export internal modules | ||
*/ | ||
@@ -26,3 +26,3 @@ | ||
/** | ||
* http-proxy module | ||
* Export http-proxy module | ||
*/ | ||
@@ -29,0 +29,0 @@ |
@@ -27,6 +27,6 @@ const zlib = require('zlib') | ||
var resProto = Object.getPrototypeOf(res) | ||
var _end = resProto.end | ||
var _write = resProto.write | ||
var _writeHead = resProto.writeHead | ||
var proto = Object.getPrototypeOf(res) | ||
var _end = proto.end | ||
var _write = proto.write | ||
var _writeHead = proto.writeHead | ||
@@ -149,4 +149,4 @@ // Listen for client connection close | ||
return res.getHeader('content-encoding') === 'gzip' | ||
|| (head && head[1] && head[1]['content-encoding'] === 'gzip') | ||
|| false | ||
|| (head && head[1] && head[1]['content-encoding'] === 'gzip') | ||
|| false | ||
} | ||
@@ -153,0 +153,0 @@ |
@@ -9,15 +9,4 @@ const HttpProxy = require('http-proxy') | ||
module.exports = function forward(route, opts, req, res, done) { | ||
// Balance the request, if configured | ||
var balance = opts.balance | ||
if (balance && balance.length) { | ||
opts.target = helpers.permute(balance) | ||
} | ||
// Reply with an error if target server was not defined | ||
if (!opts.target) { | ||
return done(missingTarget(route, req, res)) | ||
} | ||
// Clone request object to avoid side-effects | ||
var forwardReq = helpers.cloneRequest(req, opts) | ||
const forwardReq = helpers.cloneRequest(req, opts) | ||
useProperHost(forwardReq, opts) | ||
@@ -34,2 +23,15 @@ | ||
const opts = forwardReq.rocky.options | ||
// Balance the request, if configured | ||
const balance = opts.balance | ||
if (balance && balance.length) { | ||
opts.target = helpers.permute(balance) | ||
} | ||
// Reply with an error if target server was not defined | ||
if (!opts.target) { | ||
return done(missingTarget(route, req, res)) | ||
} | ||
forwardStrategy(route, opts, forwardReq, res, resolver) | ||
@@ -36,0 +38,0 @@ } |
@@ -11,7 +11,7 @@ const http = require('http') | ||
module.exports = function replay(route, opts, req, res, next) { | ||
var replays = getReplays(route, req) | ||
const replays = getReplays(route, req) | ||
if (!replays.length) return next() | ||
var targets = normalize(replays, opts) | ||
var replayer = replayRequest(route, req) | ||
const targets = normalize(replays, opts) | ||
const replayer = replayRequest(route, req) | ||
@@ -30,2 +30,7 @@ useReplayStrategy(targets, opts, replayer, next) | ||
function getReplays(route, req) { | ||
var opts = req.rocky.options | ||
if (opts && Array.isArray(opts.replays) && opts.replays.length) { | ||
return opts.replays | ||
} | ||
var replays = route.replays | ||
@@ -47,4 +52,4 @@ if (replays && replays.length) { | ||
// Clone the request/response to avoid side-effects | ||
var replayRes = new ResponseStub | ||
var replayReq = helpers.cloneRequest(req, opts) | ||
const replayRes = new ResponseStub | ||
const replayReq = helpers.cloneRequest(req, opts) | ||
replayReq.rocky.isReplay = true | ||
@@ -103,3 +108,3 @@ | ||
var forwardHost = opts.forwardHost | ||
const forwardHost = opts.forwardHost | ||
if (typeof forwardHost === 'string') { | ||
@@ -106,0 +111,0 @@ params.headers.host = forwardHost |
{ | ||
"name": "rocky", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"description": "Full-featured, middleware-oriented HTTP and WebSocket proxy supporting traffic replay and intercept", | ||
@@ -5,0 +5,0 @@ "repository": "h2non/rocky", |
@@ -13,3 +13,3 @@ # rocky [![Build Status](https://api.travis-ci.org/h2non/rocky.svg?branch=master&style=flat)](https://travis-ci.org/h2non/rocky) [![Code Climate](https://codeclimate.com/github/h2non/rocky/badges/gpa.svg)](https://codeclimate.com/github/h2non/rocky) [![NPM](https://img.shields.io/npm/v/rocky.svg)](https://www.npmjs.org/package/rocky) | ||
Requires node.js +0.12 or io.js +1.6 | ||
Requires node.js +0.12 or io.js +1.6. | ||
@@ -16,0 +16,0 @@ ## Contents |
const fs = require('fs') | ||
const http = require('http') | ||
const crypto = require('crypto') | ||
const connect = require('connect') | ||
@@ -15,3 +16,2 @@ const sinon = require('sinon') | ||
const replayUrl = baseUrl + ':' + ports.replay | ||
const noop = function () {} | ||
@@ -104,3 +104,3 @@ suite('http', function () { | ||
.expect('Content-Type', 'application/json') | ||
.end(function () {}) | ||
.end(noop) | ||
@@ -198,3 +198,3 @@ var calls = 0 | ||
.expect({ 'hello': 'world' }) | ||
.end(function () {}) | ||
.end(noop) | ||
@@ -227,3 +227,4 @@ function assert(req, res) { | ||
var body = longString() | ||
var replays = 0 | ||
var body = fs.readFileSync('test/fixtures/data.json').toString() | ||
supertest(proxyUrl) | ||
@@ -236,3 +237,3 @@ .post('/test') | ||
.expect({ 'hello': 'world' }) | ||
.end(done) | ||
.end(noop) | ||
@@ -246,5 +247,7 @@ function assert(req, res) { | ||
function assertReplay(req, res) { | ||
replays += 1 | ||
expect(req.url).to.be.equal('/test') | ||
expect(res.statusCode).to.be.equal(204) | ||
expect(req.body).to.be.equal(body) | ||
if (replays == 2) done() | ||
} | ||
@@ -604,3 +607,3 @@ }) | ||
.expect({ salutation: 'hello world' }) | ||
.end(function () {}) | ||
.end(noop) | ||
@@ -831,6 +834,6 @@ function assert(req, res) { | ||
test('replay after forward with large body', function (done) { | ||
test('replay after forward with large payload', function (done) { | ||
var spy = sinon.spy() | ||
proxy = rocky() | ||
server = createTestServer(spy, 100) | ||
server = createTestServer(assertForward, 100) | ||
replay = createReplayServer(assertReplay, 100) | ||
@@ -845,3 +848,3 @@ | ||
var start = Date.now() | ||
var body = longString(1024 * 1024) | ||
var body = longString() | ||
@@ -858,5 +861,10 @@ supertest(proxyUrl) | ||
function assertForward(req, res) { | ||
expect(req.body).to.be.equal(JSON.stringify(body)) | ||
spy() | ||
} | ||
function assertReplay(req, res) { | ||
expect(spy.calledOnce).to.be.true | ||
expect(req.body.length).to.be.equal(body.length) | ||
expect(req.body).to.be.equal(JSON.stringify(body)) | ||
expect((Date.now() - start) >= 100).to.be.true | ||
@@ -900,3 +908,3 @@ done() | ||
expect(spy.calledOnce).to.be.true | ||
expect(req.body.length).to.be.equal(body.length) | ||
expect(req.body).to.be.equal(JSON.stringify(body)) | ||
expect((Date.now() - start) >= 100).to.be.true | ||
@@ -1297,9 +1305,3 @@ if (spyReplay.calledThrice) { | ||
function longString(x) { | ||
var s = '' | ||
x = +x || 1024 * 1024 * 5 | ||
while (s.length < x && x > 0) { | ||
var r = Math.random() | ||
s+= r < 0.1 ? Math.floor(r*100): String.fromCharCode(Math.floor(r*26) + (r>0.5?97:65)) | ||
} | ||
return s | ||
return crypto.randomBytes(+x || 1024 * 1024) | ||
} | ||
@@ -1312,1 +1314,3 @@ | ||
} | ||
function noop() {} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
172268
4281