-
-
Save vdeturckheim/b8eb18f02ff2a3a85e8a7d949c36fc7c to your computer and use it in GitHub Desktop.
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
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