Skip to content

Commit

Permalink
Expose data type from mappers. Fixes #32.
Browse files Browse the repository at this point in the history
  • Loading branch information
philbooth committed Jun 9, 2014
1 parent 2d9feb8 commit 6f84e4c
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"camelcase": true,
"curly": true,
"eqeqeq": true,
"es3": true,
"es3": false,
"forin": true,
"freeze": true,
"immed": true,
Expand Down
12 changes: 9 additions & 3 deletions doc/forwarders/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ the returned forwarder function
should look like this:

```javascript
function (/* bound options, ... */ data, separator, callback) {
function (/* bound options, ... */ data, type, separator, callback) {
}
```

Expand All @@ -59,7 +59,13 @@ are, in order:
1. `data`:
Data returned by one of the mappers.

2. `separator`:
2. `type`:
Optional type
that can be used
to ensure
sane transfer of the data.

3. `separator`:
Optional separator
that can be used
to ensure
Expand All @@ -68,7 +74,7 @@ are, in order:
it must be broken
across multiple packets.

3. `callback`:
4. `callback`:
Function to call
after the data has been sent.
The signature for this function
Expand Down
7 changes: 7 additions & 0 deletions doc/forwarders/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Each beacon request
is written to a separate file
in the nominated directory.

If the active [mapper]
has specified
a data type,
this will be used to set
an appropriate file extension.

This forwarder is currently very rudimentary
and has no awareness of file system limits
regarding the maximum number of files per directory.
Expand All @@ -20,4 +26,5 @@ contain a [UUID]
to avoid naming clashes.

[uuid]: http://www.ietf.org/rfc/rfc4122.txt
[mapper]: ../mappers/README.md

7 changes: 7 additions & 0 deletions doc/forwarders/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ It accepts two options:
HTTP method to use for the request.
The default is `'GET'`.

If the active [mapper]
has specified
a data type,
this will be used to set
the `Content-Type` header appropriately.

[http]: http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
[https]: http://en.wikipedia.org/wiki/HTTP_Secure
[mapper]: ../mappers/README.md

2 changes: 1 addition & 1 deletion src/forwarders/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ exports.initialise = function () {
return send;
};

function send (data, separator, callback) {
function send (data, type, separator, callback) {
console.log(data);
callback(null, data.length);
}
Expand Down
13 changes: 10 additions & 3 deletions src/forwarders/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@

var fs = require('fs'),
path = require('path'),
uuid = require('node-uuid');
uuid = require('node-uuid'),
extensions;

exports.initialise = function (options) {
return send.bind(null, options.fwdDir);
};

function send (directory, data, separator, callback) {
function send (directory, data, type, separator, callback) {
try {
fs.writeFile(
path.join(directory, 'boomcatch-' + uuid.v4() + '.json'),
path.join(directory, 'boomcatch-' + uuid.v4() + '.' + extensions[type || 'default']),
data,
{ mode: 420 },
function (error) {
Expand All @@ -42,3 +43,9 @@ function send (directory, data, separator, callback) {
}
}

extensions = {
json: 'json',
html: 'html',
default: 'txt'
};

14 changes: 12 additions & 2 deletions src/forwarders/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

var url = require('url'),
querystring = require('querystring'),
check = require('check-types');
check = require('check-types'),
contentTypes;

exports.initialise = function (options) {
return send.bind(null, getProtocol(options.fwdUrl), url.parse(options.fwdUrl), normaliseMethod(options.fwdMethod));
Expand All @@ -39,7 +40,7 @@ function normaliseMethod (method) {
return 'GET';
}

function send (protocol, url, method, data, separator, callback) {
function send (protocol, url, method, data, type, separator, callback) {
var length, request;

try {
Expand All @@ -55,6 +56,9 @@ function send (protocol, url, method, data, separator, callback) {
}

url.method = method;
url.headers = {
'Content-Type': contentTypes[type || 'default']
};

request = protocol.request(url, function (response) {
if (data) {
Expand All @@ -72,3 +76,9 @@ function send (protocol, url, method, data, separator, callback) {
}
}

contentTypes = {
json: 'application/json',
html: 'text/html',
default: 'text/plain'
};

2 changes: 1 addition & 1 deletion src/forwarders/udp.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function normaliseSize (size) {
return normaliseValue(size, 'positiveNumber', 512);
}

function send (host, port, size, data, separator, callback) {
function send (host, port, size, data, type, separator, callback) {
var socket, buffer;

try {
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ function getFilter (options) {
}

function getMapper (options) {
return getExtension('mapper', options, ['separator']);
return getExtension('mapper', options, ['type','separator']);
}

function getForwarder (options) {
Expand Down Expand Up @@ -540,7 +540,7 @@ function send (log, state, remoteAddress, validator, filter, mapper, forwarder,

log.info('sending ' + mappedData);

forwarder(mappedData, mapper.separator, function (error, bytesSent) {
forwarder(mappedData, mapper.type, mapper.separator, function (error, bytesSent) {
if (error) {
return fail(log, request, response, 502, error);
}
Expand Down
3 changes: 2 additions & 1 deletion src/mappers/har.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ module.exports = {
useragent(true);

return map;
}
},
type: 'json'
};

function map (data, referer, userAgent) {
Expand Down
3 changes: 2 additions & 1 deletion src/mappers/waterfall/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ var fs = require('fs'),
module.exports = {
initialise: function (options) {
return map.bind(null, getTemplate(options), getSettings(options));
}
},
type: 'html'
};

function getTemplate (options) {
Expand Down
48 changes: 44 additions & 4 deletions test/forwarders/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ suite('forwarders/file:', function () {
name: 'callback',
log: log
});
forwarder('foo bar', null, callback);
forwarder('foo bar', null, null, callback);
});

teardown(function () {
Expand All @@ -141,7 +141,7 @@ suite('forwarders/file:', function () {
assert.strictEqual(log.these.join[0], require('path'));
assert.lengthOf(log.args.join[0], 2);
assert.strictEqual(log.args.join[0][0], 'wibble');
assert.match(log.args.join[0][1], /^boomcatch-[a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}.json$/);
assert.match(log.args.join[0][1], /^boomcatch-[a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}.txt$/);
});

test('fs.writeFile was called once', function () {
Expand All @@ -161,15 +161,15 @@ suite('forwarders/file:', function () {

suite('call forwarder:', function () {
setup(function () {
forwarder('foo bar', null, function () {});
forwarder('foo bar', null, null, function () {});
});

test('path.join was called once', function () {
assert.strictEqual(log.counts.join, 2);
});

test('path.join was called correctly', function () {
assert.match(log.args.join[1][1], /^boomcatch-[a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}.json$/);
assert.match(log.args.join[1][1], /^boomcatch-[a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}.txt$/);
assert.notEqual(log.args.join[0][1], log.args.join[1][1]);
});

Expand All @@ -195,6 +195,46 @@ suite('forwarders/file:', function () {
});
});
});

suite('call forwarder with json type:', function () {
var callback;

setup(function () {
callback = spooks.fn({
name: 'callback',
log: log
});
forwarder('foo bar', 'json', null, callback);
});

teardown(function () {
callback = undefined;
});

test('path.join was called correctly', function () {
assert.match(log.args.join[0][1], /^boomcatch-[a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}.json$/);
});
});

suite('call forwarder with html type:', function () {
var callback;

setup(function () {
callback = spooks.fn({
name: 'callback',
log: log
});
forwarder('foo bar', 'html', null, callback);
});

teardown(function () {
callback = undefined;
});

test('path.join was called correctly', function () {
assert.match(log.args.join[0][1], /^boomcatch-[a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}.html$/);
});
});
});
});
});
Expand Down
48 changes: 45 additions & 3 deletions test/forwarders/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ suite('forwarders/http:', function () {
name: 'callback',
log: log
});
forwarder('foo bar', null, callback);
forwarder('foo bar', null, null, callback);
});

teardown(function () {
Expand All @@ -223,6 +223,8 @@ suite('forwarders/http:', function () {
assert.strictEqual(log.args.http[0][0].pathname, '/');
assert.strictEqual(log.args.http[0][0].href, 'http://www.example.com/');
assert.strictEqual(log.args.http[0][0].method, 'GET');
assert.isObject(log.args.http[0][0].headers);
assert.strictEqual(log.args.http[0][0].headers['Content-Type'], 'text/plain');
assert.isFunction(log.args.http[0][1]);
});

Expand Down Expand Up @@ -314,7 +316,7 @@ suite('forwarders/http:', function () {
name: 'callback',
log: log
});
forwarder({ foo: 'bar', baz: 'qux' }, null, callback);
forwarder({ foo: 'bar', baz: 'qux' }, null, null, callback);
});

teardown(function () {
Expand Down Expand Up @@ -394,6 +396,46 @@ suite('forwarders/http:', function () {
});
});
});

suite('call forwarder with json type:', function () {
var callback;

setup(function () {
callback = spooks.fn({
name: 'callback',
log: log
});
forwarder('foo bar', 'json', null, callback);
});

teardown(function () {
callback = undefined;
});

test('http.request was called correctly', function () {
assert.strictEqual(log.args.http[0][0].headers['Content-Type'], 'application/json');
});
});

suite('call forwarder with html type:', function () {
var callback;

setup(function () {
callback = spooks.fn({
name: 'callback',
log: log
});
forwarder('foo bar', 'html', null, callback);
});

teardown(function () {
callback = undefined;
});

test('http.request was called correctly', function () {
assert.strictEqual(log.args.http[0][0].headers['Content-Type'], 'text/html');
});
});
});

suite('call initialise with custom method:', function () {
Expand Down Expand Up @@ -434,7 +476,7 @@ suite('forwarders/http:', function () {
name: 'callback',
log: log
});
forwarder('foo bar', null, callback);
forwarder('foo bar', null, null, callback);
});

teardown(function () {
Expand Down
Loading

0 comments on commit 6f84e4c

Please sign in to comment.