Created
August 18, 2011 08:29
-
-
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)
This file contains hidden or 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
/* | |
* 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