From 4298448a6697050aedd660eda6c627e75f782920 Mon Sep 17 00:00:00 2001 From: dcaldwell Date: Fri, 13 May 2016 15:03:30 -0700 Subject: [PATCH 1/5] added ignoreCookies option to remove cookies from req object used to create tape name hash --- cli.js | 0 index.js | 14 +++++++++++--- package.json | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) mode change 100644 => 100755 cli.js diff --git a/cli.js b/cli.js old mode 100644 new mode 100755 diff --git a/index.js b/index.js index 429e014..d4731fd 100644 --- a/index.js +++ b/index.js @@ -10,12 +10,14 @@ var buffer = require('./lib/buffer'); var proxy = require('./lib/proxy'); var record = require('./lib/record'); var debug = require('debug')('yakbak:server'); +var _ = require('lodash'); /** * Returns a new yakbak proxy middleware. * @param {String} host The hostname to proxy to * @param {Object} opts * @param {String} opts.dirname The tapes directory + * @param {Boolean} opts.ignoreCookies ignore cookie values for tape name hash * @returns {Function} */ @@ -28,7 +30,7 @@ module.exports = function (host, opts) { debug('req', req.url); return buffer(req).then(function (body) { - var file = path.join(opts.dirname, tapename(req, body)); + var file = path.join(opts.dirname, tapename(req, body, opts)); return Promise.try(function () { return require.resolve(file); @@ -52,11 +54,17 @@ module.exports = function (host, opts) { * Returns the tape name for `req`. * @param {http.IncomingMessage} req * @param {Array.} body + * @param {Object} opts * @returns {String} */ -function tapename(req, body) { - return hash.sync(req, Buffer.concat(body)) + '.js'; +function tapename(req, body, opts) { + var tempReq = _.cloneDeep(req); + if (opts.ignoreCookies) { + delete tempReq.headers.cookie; + delete tempReq.rawHeaders; + } + return hash.sync(tempReq, Buffer.concat(body)) + '.js'; } /** diff --git a/package.json b/package.json index f84e8a9..ab150ef 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "debug": "~2.1.2", "ejs": "~2.3.1", "incoming-message-hash": "~3.2.1", + "lodash": "^4.12.0", "mkdirp": "~0.5.0" } } From fbd90baed0b4e524dcb6eef355c320ae3e28167c Mon Sep 17 00:00:00 2001 From: dcaldwell Date: Fri, 13 May 2016 16:34:58 -0700 Subject: [PATCH 2/5] added recordStatusCodes options to provide a range of acceptable status codes for recording tapes. If the status code of the request is not in this range, a tape will not be created --- index.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index d4731fd..9ae3506 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,7 @@ var _ = require('lodash'); * @param {Object} opts * @param {String} opts.dirname The tapes directory * @param {Boolean} opts.ignoreCookies ignore cookie values for tape name hash + * @param {Array} opts.recordStatusCodes Array of two integers e.g. [200, 300) indicating the range of acceptable status codes for recording tapes * @returns {Function} */ @@ -29,6 +30,9 @@ module.exports = function (host, opts) { debug('req', req.url); + delete req.rawHeaders; + opts.recordStatusCodes = opts.recordStatusCodes || [0,999]; + return buffer(req).then(function (body) { var file = path.join(opts.dirname, tapename(req, body, opts)); @@ -36,7 +40,13 @@ module.exports = function (host, opts) { return require.resolve(file); }).catch(ModuleNotFoundError, function (/* err */) { return proxy(req, body, host).then(function (res) { - return record(res.req, res, file); + if (_.inRange(res.statusCode, opts.recordStatusCodes[0], opts.recordStatusCodes[1])) + return record(res.req, res, file); + else { + var err = new Error('Status code not in acceptable range, skipping recording'); + err.res = res; + throw err; + } }); }); @@ -44,10 +54,15 @@ module.exports = function (host, opts) { return require(file); }).then(function (tape) { return tape(req, res); + }).catch(function(err) { + if (err.res) { + res.statusCode = err.res.statusCode; + res.headers = err.res.headers; + res.end(); + return null; + } }); - }; - }; /** @@ -62,7 +77,6 @@ function tapename(req, body, opts) { var tempReq = _.cloneDeep(req); if (opts.ignoreCookies) { delete tempReq.headers.cookie; - delete tempReq.rawHeaders; } return hash.sync(tempReq, Buffer.concat(body)) + '.js'; } From 75a5764fbd11cbcc62d041f5703621b9f9b01e09 Mon Sep 17 00:00:00 2001 From: dcaldwell Date: Fri, 12 Aug 2016 17:54:36 -0700 Subject: [PATCH 3/5] fixed lint errors --- index.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 1c202fc..28fb5eb 100644 --- a/index.js +++ b/index.js @@ -33,7 +33,7 @@ module.exports = function (host, opts) { debug('req', req.url); delete req.rawHeaders; - opts.recordStatusCodes = opts.recordStatusCodes || [0,999]; + opts.recordStatusCodes = opts.recordStatusCodes || [0, 999]; return buffer(req).then(function (body) { var file = path.join(opts.dirname, tapename(req, body, opts)); @@ -45,14 +45,14 @@ module.exports = function (host, opts) { throw new RecordingDisabledError('Recording Disabled'); } else { return proxy(req, body, host).then(function (pres) { - if (_.inRange(pres.statusCode, opts.recordStatusCodes[0], opts.recordStatusCodes[1])) + if (_.inRange(pres.statusCode, opts.recordStatusCodes[0], opts.recordStatusCodes[1])) { return record(pres.req, pres, file); - else { + } else { throw new StatusCodeOutOfRangeError('Status code out of range, skipping recording', res); } }); } - }) + }); }).then(function (file) { return require(file); }).then(function (tape) { @@ -64,12 +64,14 @@ module.exports = function (host, opts) { /* eslint-enable no-console */ res.statusCode = err.status; res.end(err.message); - }).catch(StatusCodeOutOfRangeError, function(err) { + }).catch(StatusCodeOutOfRangeError, function (err) { + /* eslint-disable no-console */ console.log(err.message); + /* eslint-enable no-console */ res.statusCode = err.res.statusCode; res.headers = err.res.headers; res.end(); - }) + }); }; }; @@ -83,6 +85,7 @@ module.exports = function (host, opts) { function tapename(req, body, opts) { var tempReq = _.cloneDeep(req); + if (opts.ignoreCookies) { delete tempReq.headers.cookie; } From 66baca7882991a8dffd084bd5526afcf7a1f41e5 Mon Sep 17 00:00:00 2001 From: dcaldwell Date: Mon, 15 Aug 2016 10:53:16 -0700 Subject: [PATCH 4/5] Removed console log statement for status code out of range --- index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/index.js b/index.js index 28fb5eb..33d1233 100644 --- a/index.js +++ b/index.js @@ -65,9 +65,6 @@ module.exports = function (host, opts) { res.statusCode = err.status; res.end(err.message); }).catch(StatusCodeOutOfRangeError, function (err) { - /* eslint-disable no-console */ - console.log(err.message); - /* eslint-enable no-console */ res.statusCode = err.res.statusCode; res.headers = err.res.headers; res.end(); From 884cde7af8e9592370ece6945ba570a050008aac Mon Sep 17 00:00:00 2001 From: dcaldwell Date: Mon, 15 Aug 2016 11:44:00 -0700 Subject: [PATCH 5/5] Fixed variable name bug --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 33d1233..131d93d 100644 --- a/index.js +++ b/index.js @@ -48,7 +48,7 @@ module.exports = function (host, opts) { if (_.inRange(pres.statusCode, opts.recordStatusCodes[0], opts.recordStatusCodes[1])) { return record(pres.req, pres, file); } else { - throw new StatusCodeOutOfRangeError('Status code out of range, skipping recording', res); + throw new StatusCodeOutOfRangeError('Status code out of range, skipping recording', pres); } }); }