From 781b8a07009357da987fa79ae38a74bf2614602c Mon Sep 17 00:00:00 2001 From: Chakrit Wichian Date: Sun, 15 Jul 2012 14:54:25 +0700 Subject: [PATCH] Initial implementation. --- Jakefile | 7 +++++++ README.md | 17 +++++++++++++++++ index.js | 34 ++++++++++++++++++++++++++++++++++ package.json | 17 +++++++++++++++++ test.js | 4 ++++ 5 files changed, 79 insertions(+) create mode 100644 Jakefile create mode 100644 README.md create mode 100644 index.js create mode 100644 package.json create mode 100644 test.js diff --git a/Jakefile b/Jakefile new file mode 100644 index 0000000..015e615 --- /dev/null +++ b/Jakefile @@ -0,0 +1,7 @@ + +new jake.NpmPublishTask('publish', + [ 'Jakefile' + , 'README.md' + , 'package.json' + , 'index.js' + , 'test.js' ]); diff --git a/README.md b/README.md new file mode 100644 index 0000000..0a91fbb --- /dev/null +++ b/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); + }); + +... + diff --git a/index.js b/index.js new file mode 100644 index 0000000..5d28845 --- /dev/null +++ b/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; + +})(); + diff --git a/package.json b/package.json new file mode 100644 index 0000000..d57aefc --- /dev/null +++ b/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 (http://chakrit.net)" +, "license": "BSD" +, "engines": { "node": "v0.6.x" } +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..f3c8e3a --- /dev/null +++ b/test.js @@ -0,0 +1,4 @@ + +// test.js - Test index.js +var index = require('./index'); +