Skip to content

Instantly share code, notes, and snippets.

@noodlehaus
Created August 18, 2011 08:29
Show Gist options
  • Save noodlehaus/1153656 to your computer and use it in GitHub Desktop.
Save noodlehaus/1153656 to your computer and use it in GitHub Desktop.
patch the req object in express to include the pause() and resume() methods for event buffering (for async middleware)
/*
* patches the req object to have the pause() and resume() functions
* for event buffering (for async middleware).
* code is take from express' utils file
*/
const http = require('http'),
req = http.IncomingMessage.prototype;
// patch req object for async middleware
(function () {
var onData,
onEnd,
events = [];
req.pause = function () {
this.on('data', onData = function (data, encoding) {
events.push(['data', data, encoding]);
});
this.on('end', onEnd = function (data, encoding) {
events.push(['end', data, encoding]);
});
};
// resume the events
req.resume = function () {
this.removeListener('data', onData);
this.removeListener('end', onEnd);
for (var i = 0, len = events.length; i < len; ++i) {
this.emit.apply(this, events[i]);
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment