Skip to content

Instantly share code, notes, and snippets.

@vdeturckheim
Last active November 12, 2017 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdeturckheim/b8eb18f02ff2a3a85e8a7d949c36fc7c to your computer and use it in GitHub Desktop.
Save vdeturckheim/b8eb18f02ff2a3a85e8a7d949c36fc7c to your computer and use it in GitHub Desktop.
const AsyncHooks = require('async_hooks');
const Http = require('http');
const context = new Map();
const emit = Http.Server.prototype.emit;
Http.Server.prototype.emit = function (type) {
if (type === 'request') {
// each time a new request arrives in the HTTP server, we associate it with the current asynchronous context
const [req, res] = [arguments[1], arguments[2]];
const id = AsyncHooks.executionAsyncId(); // this returns the current asynchronous context's id
context.set(id, req);
}
return emit.apply(this, arguments);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment