Skip to content

Instantly share code, notes, and snippets.

@vdeturckheim vdeturckheim/server.emit.js Secret
Last active Nov 12, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.