Skip to content

Commit

Permalink
Initial implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chakrit Wichian committed Jul 15, 2012
0 parents commit 781b8a0
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Jakefile
@@ -0,0 +1,7 @@

new jake.NpmPublishTask('publish',
[ 'Jakefile'
, 'README.md'
, 'package.json'
, 'index.js'
, 'test.js' ]);
17 changes: 17 additions & 0 deletions README.md
@@ -0,0 +1,17 @@

### HOWTO

Adds this to your `.use` configuration block.

app.use(require('connect-requestid'))

And that's it! :-)

Access the request id object with `request.id` like this:

app.get('/', function(req, resp) {
resp.send('hello request #' + req.id);
});

...

34 changes: 34 additions & 0 deletions index.js
@@ -0,0 +1,34 @@


// index.js - Main exports file
(function() {

var ID_BYTES = 32;

var crypto = require('crypto')
, cache = { };

// generate new id using secure random bytes
var generateNewId = function(callback) {
return crypto.randomBytes(ID_BYTES, function(e, buf) {
if (e) return callback(e, null);

return callback(null, buf.toString('hex'));
});
};

// the middleware
module.exports = function(req, res, next) {
return generateNewId(function(e, newId) {
if (!!e) return next(e);

req.id = newId;
next();
});
};

// export new id function for tesing.
module.exports.generateNewId = generateNewId;

})();

17 changes: 17 additions & 0 deletions package.json
@@ -0,0 +1,17 @@
{ "name": "connect-requestid"
, "version": "0.1.0-0"
, "description": "Connect middleware that adds a unique id to each request."
, "main": "index.js"
, "scripts": { "test": "test.js" }
, "repository":
{ "type": "git"
, "url": "git://github.com/chakrit/connect-requestid.git" }
, "keywords":
[ "connect"
, "middleware"
, "request"
, "id" ]
, "author": "Chakrit Wichian <service@chakrit.net> (http://chakrit.net)"
, "license": "BSD"
, "engines": { "node": "v0.6.x" }
}
4 changes: 4 additions & 0 deletions test.js
@@ -0,0 +1,4 @@

// test.js - Test index.js
var index = require('./index');

0 comments on commit 781b8a0

Please sign in to comment.