Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chakrit Wichian
committed
Jul 15, 2012
0 parents
commit 781b8a0
Showing
5 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
new jake.NpmPublishTask('publish', | ||
[ 'Jakefile' | ||
, 'README.md' | ||
, 'package.json' | ||
, 'index.js' | ||
, 'test.js' ]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
|
||
... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
})(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
// test.js - Test index.js | ||
var index = require('./index'); | ||
|