Skip to content

Commit

Permalink
Adds capability to manipulate request from host app before sending up…
Browse files Browse the repository at this point in the history
…stream
  • Loading branch information
craigedmunds committed Sep 16, 2016
1 parent 2aa7fca commit 9ba45b8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = function (host, opts) {
if (opts.noRecord) {
throw new RecordingDisabledError('Recording Disabled');
} else {
return proxy(req, body, host).then(function (pres) {
return proxy(req, body, host, opts.preFlight).then(function (pres) {
return record(pres.req, pres, file);
});
}
Expand Down
12 changes: 10 additions & 2 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var mods = { 'http:': http, 'https:': https };
* @returns {Promise.<http.IncomingMessage>}
*/

module.exports = function proxy(req, body, host) {
module.exports = function proxy(req, body, host, preFlight) {
return new Promise(function (resolve /* , reject */) {
var uri = url.parse(host);
var mod = mods[uri.protocol] || http;
Expand All @@ -41,7 +41,15 @@ module.exports = function proxy(req, body, host) {
rejectUnauthorized: false
};

debug('r', r);
if (preFlight) {
debug('r pre pre-flight', r);
r = preFlight(r);

debug('r post pre-flight', r);
}
else {
debug('r no pre-flight', r);
}

var preq = mod.request(r, function (pres) {
resolve(pres);
Expand Down
4 changes: 4 additions & 0 deletions src/tape.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var path = require("path");
var debug = require('debug')('yakbak:tape');

/**
* <%- req.method %> <%- decodeURIComponent(req.path) %>
Expand All @@ -17,7 +18,10 @@ module.exports = function (req, res) {

res.setHeader("x-yakbak-tape", path.basename(__filename, ".js"));

debug("statusCode", <%- JSON.stringify(res.statusCode) %>);

<% body.forEach(function (data) { -%>
res.write(new Buffer(<%- JSON.stringify(data.toString('base64')) %>, "base64"));
<% }); -%>
res.end();
Expand Down

0 comments on commit 9ba45b8

Please sign in to comment.