Skip to content
This repository has been archived by the owner on Sep 12, 2022. It is now read-only.

Commit

Permalink
Force spaces instead of tabs.
Browse files Browse the repository at this point in the history
  • Loading branch information
seglo committed Jun 7, 2014
1 parent ae83c82 commit d73f66a
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 299 deletions.
4 changes: 3 additions & 1 deletion .jshintrc
Expand Up @@ -17,5 +17,7 @@
"undef": true, // true: Require all non-global variables to be declared (prevents global leaks)
"boss": true, // true: Tolerate assignments where comparisons would be expected
"eqnull": true, // true: Tolerate use of `== null`
"node": true // Node.js
"node": true, // Node.js
"smarttabs" : true,
"indent": 2
}
1 change: 1 addition & 0 deletions Gruntfile.js
Expand Up @@ -101,6 +101,7 @@ module.exports = function(grunt) {
// plugin's task(s), then test the result.
grunt.registerTask('test', [
'clean',
'jshint',
'prism:proxyTest',
'prism:recordTest',
'prism:mockTest',
Expand Down
44 changes: 22 additions & 22 deletions lib/events.js
Expand Up @@ -10,29 +10,29 @@ var modes = require('./modes.js');
var proxies = require('./proxies.js');

module.exports = {
handleRequest: function(req, res, next) {
var proxy = proxies.getProxy(req.url);
handleRequest: function(req, res, next) {
var proxy = proxies.getProxy(req.url);

// if we have a proxy configured then handle this request, else pass the buck to the next connect middleware
if (proxy) {
if (proxy.config.mode === 'mock') {
modes.mock(proxy, req, res);
} else if (proxy.config.mode === 'proxy' || proxy.config.mode === 'record') {
modes.proxy(proxy, req, res);
} else {
throw new Error('No such mode: ' + proxy.config.mode);
}
} else {
next();
}
},
handleResponse: function(res) {
var proxy = proxies.getProxy(res.req.path);
// if we have a proxy configured then handle this request, else pass the buck to the next connect middleware
if (proxy) {
if (proxy.config.mode === 'mock') {
modes.mock(proxy, req, res);
} else if (proxy.config.mode === 'proxy' || proxy.config.mode === 'record') {
modes.proxy(proxy, req, res);
} else {
throw new Error('No such mode: ' + proxy.config.mode);
}
} else {
next();
}
},
handleResponse: function(res) {
var proxy = proxies.getProxy(res.req.path);

if (_.isUndefined(proxy) || proxy.config.mode !== 'record') {
return;
}
if (_.isUndefined(proxy) || proxy.config.mode !== 'record') {
return;
}

modes.record(proxy, res);
}
modes.record(proxy, res);
}
};
110 changes: 55 additions & 55 deletions lib/modes.js
Expand Up @@ -10,79 +10,79 @@ var utils = require('./utils.js');

// TODO: figure out how to buffer file stream into response
function writeResponse(path, res) {
var responseStr = fs.readFileSync(path).toString();
var response = JSON.parse(responseStr);
var responseStr = fs.readFileSync(path).toString();
var response = JSON.parse(responseStr);

res.writeHead(response.statusCode, {
'Content-Type': response.responseHeaders['content-type']
});
res.write(response.data);
res.end();
res.writeHead(response.statusCode, {
'Content-Type': response.responseHeaders['content-type']
});
res.write(response.data);
res.end();
}

function write404(req, res) {
res.writeHead(404, {
'Content-Type': 'text/plain'
});
res.write('No mock exists for ' + req.url);
res.end();
res.writeHead(404, {
'Content-Type': 'text/plain'
});
res.write('No mock exists for ' + req.url);
res.end();
}

function serializeResponse(proxy, res, data) {
var response = {
requestUrl: res.req.path,
responseHeaders: res.headers,
statusCode: res.statusCode,
data: data // TODO: if header content-type is JSON then save data as JSON instead of string
};
var response = {
requestUrl: res.req.path,
responseHeaders: res.headers,
statusCode: res.statusCode,
data: data // TODO: if header content-type is JSON then save data as JSON instead of string
};

var serializedResponse = JSON.stringify(response, true, 2);
var serializedResponse = JSON.stringify(response, true, 2);

var requestFilename = utils.hashUrl(res.req.path);
var requestFilename = utils.hashUrl(res.req.path);

var finalPath = path.join(proxy.config.mocksPath, requestFilename);
var finalPath = path.join(proxy.config.mocksPath, requestFilename);

// write file async to disk. overwrite if it already exists. prettyprint.
fs.writeFile(finalPath, serializedResponse);
// write file async to disk. overwrite if it already exists. prettyprint.
fs.writeFile(finalPath, serializedResponse);
}

function logSuccess(modeMsg, proxy, req) {
var target = utils.absoluteUrl(proxy, req.url);
var source = req.originalUrl;
grunt.log.verbose.writeln(modeMsg + ' request: ' + source + ' -> ' + target + '\n' + JSON.stringify(req.headers, true, 2));
var target = utils.absoluteUrl(proxy, req.url);
var source = req.originalUrl;
grunt.log.verbose.writeln(modeMsg + ' request: ' + source + ' -> ' + target + '\n' + JSON.stringify(req.headers, true, 2));
}

module.exports = {
proxy: function(proxy, req, res) {
var target = utils.absoluteUrl(proxy, req.url);
proxy: function(proxy, req, res) {
var target = utils.absoluteUrl(proxy, req.url);

proxy.server.web(req, res, {
target: target
});
proxy.server.web(req, res, {
target: target
});

logSuccess('Proxied', proxy, req);
},
record: function(proxy, res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
serializeResponse(proxy, res, data);
logSuccess('Recorded', proxy, res.req);
});
},
mock: function(proxy, req, res) {
var diskResponse = path.join(proxy.config.mocksPath, utils.hashUrl(req.url));
logSuccess('Proxied', proxy, req);
},
record: function(proxy, res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
serializeResponse(proxy, res, data);
logSuccess('Recorded', proxy, res.req);
});
},
mock: function(proxy, req, res) {
var diskResponse = path.join(proxy.config.mocksPath, utils.hashUrl(req.url));

fs.exists(diskResponse, function(exists) {
if (exists) {
writeResponse(diskResponse, res);
logSuccess('Mocked', proxy, req);
} else {
write404(req, res);
grunt.log.verbose.writeln('Returned 404 for: ' + req.url);
}
});
}
fs.exists(diskResponse, function(exists) {
if (exists) {
writeResponse(diskResponse, res);
logSuccess('Mocked', proxy, req);
} else {
write404(req, res);
grunt.log.verbose.writeln('Returned 404 for: ' + req.url);
}
});
}
};
76 changes: 38 additions & 38 deletions lib/proxies.js
Expand Up @@ -12,45 +12,45 @@ var proxies = [];

// ripped from grunt-connect-proxy to match contexts
function matchContext(context, url) {
var positiveContexts, negativeContexts, positiveMatch, negativeMatch;
var contexts = context;
if (!_.isArray(contexts)) {
contexts = [contexts];
}
positiveContexts = _.filter(contexts, function(c) {
return c.charAt(0) !== '!';
});
negativeContexts = _.filter(contexts, function(c) {
return c.charAt(0) === '!';
});
// Remove the '!' character from the contexts
negativeContexts = _.map(negativeContexts, function(c) {
return c.slice(1);
});
negativeMatch = _.find(negativeContexts, function(c) {
return url.lastIndexOf(c, 0) === 0;
});
// If any context negates this url, it must not be proxied.
if (negativeMatch) {
return false;
}
positiveMatch = _.find(positiveContexts, function(c) {
return url.lastIndexOf(c, 0) === 0;
});
// If there is any positive match, lets proxy this url.
return positiveMatch != null;
var positiveContexts, negativeContexts, positiveMatch, negativeMatch;
var contexts = context;
if (!_.isArray(contexts)) {
contexts = [contexts];
}
positiveContexts = _.filter(contexts, function(c) {
return c.charAt(0) !== '!';
});
negativeContexts = _.filter(contexts, function(c) {
return c.charAt(0) === '!';
});
// Remove the '!' character from the contexts
negativeContexts = _.map(negativeContexts, function(c) {
return c.slice(1);
});
negativeMatch = _.find(negativeContexts, function(c) {
return url.lastIndexOf(c, 0) === 0;
});
// If any context negates this url, it must not be proxied.
if (negativeMatch) {
return false;
}
positiveMatch = _.find(positiveContexts, function(c) {
return url.lastIndexOf(c, 0) === 0;
});
// If there is any positive match, lets proxy this url.
return positiveMatch != null;
}

module.exports = {
getProxy: function(path) {
return _.find(proxies, function(proxy) {
return matchContext(proxy.config.context, path);
});
},
proxies: function() {
return proxies;
},
add: function(proxy) {
proxies.push(proxy);
}
getProxy: function(path) {
return _.find(proxies, function(proxy) {
return matchContext(proxy.config.context, path);
});
},
proxies: function() {
return proxies;
},
add: function(proxy) {
proxies.push(proxy);
}
};
16 changes: 8 additions & 8 deletions lib/utils.js
Expand Up @@ -3,12 +3,12 @@
var crypto = require('crypto');

module.exports = {
hashUrl: function(url) {
var shasum = crypto.createHash('sha1');
shasum.update(url);
return shasum.digest('hex');
},
absoluteUrl: function(proxy, url) {
return (proxy.config.https ? 'https://' : 'http://') + proxy.config.host + ':' + proxy.config.port + url;
}
hashUrl: function(url) {
var shasum = crypto.createHash('sha1');
shasum.update(url);
return shasum.digest('hex');
},
absoluteUrl: function(proxy, url) {
return (proxy.config.https ? 'https://' : 'http://') + proxy.config.host + ':' + proxy.config.port + url;
}
};

0 comments on commit d73f66a

Please sign in to comment.