-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
51 lines (41 loc) · 1.47 KB
/
index.js
File metadata and controls
51 lines (41 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var http = require('http');
var fs = require('fs');
var util = require('util');
var colors = require('colors');
var format = require('util').format;
var Cache = require('./lib/cache');
var storage = require('./lib/storage');
module.exports = function Dictaphone(opts, baseUrl) {
var self = this;
var cache = new Cache({
base: baseUrl,
store: opts.cacheFile ? new storage.FileStore(opts.cacheFile) : new storage.MemoryStore(),
ignore: opts.ignoreParams
});
function log (reqData, resCode, cacheData) {
var indicator;
if (cacheData.hit) indicator = ' ';
else if (cacheData.proxied) indicator = '->';
else indicator = '-X';
var msg = format('%s %s\t%s %s %s', indicator, resCode, reqData.original.method, reqData.original.url, reqData.original.body);
if (cacheData.fullUrl) msg += '\t' + cacheData.fullUrl;
msg = cacheData.hit ? msg.white : msg.yellow;
console.log(msg);
}
self.run = function () {
var PORT = opts.port || 8080;
var HOST = 'localhost';
cache.on('response', function (data) {
log(data.request, data.response.statusCode, data.cacheInfo);
});
var server = http.createServer(cache.handleRequest);
server.listen(PORT, HOST);
var proxy = HOST + ':' + PORT;
var upstream = baseUrl || 'NONE';
console.log(util.format('%s\tdictaphone proxying %s on %s', LOGO.blue, upstream.yellow, proxy.white));
console.log();
};
return self;
}
const LOGO = '::[oo]'.inverse;
module.exports.LOGO = LOGO;