Initial stab at async plugin execution
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ciaranj · committed by ciaranj · parent 5458e5d · Browse files · 12 files·+82 added−39 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
12 files+82 added−39 removed
- other:+82 added−39 removed
Provenance
- Commit
4063d2e- Tree
b4f67ed- Author
ciaranj <ciaranj@gmail.com>- Committer
ciaranj <ciaranj@gmail.com>- Parent
5458e5d
12 files changed+82 added−39 removed
modified:lib/express/plugins/body-decoder.js+3 added-1 removed
10 unmodified lines11121314151516171819202122232410 unmodified lines * * - application/x-www-form-urlencoded */ request: function(event) { request: function(event, callback) { var request = event.request if (request.header('content-type') && request.header('content-type').indexOf('application/x-www-form-urlencoded') > -1) request.params.post = queryString.parseQuery(request.body) callback(); } }})No newline at end of fileNo newline at end of filemodified:lib/express/plugins/common-logger.js+2 added-1 removed
34 unmodified lines35363738393940414243444546474849505134 unmodified lines /** * Output log data. */ response: function(event) { response: function(event, callback) { puts([event.request.connection.remoteAddress, '-', '-', '[' + format(new Date) + ']', '"' + event.request.method.toUpperCase() + ' ' + (event.request.url.pathname || '/') + ' HTTP/' + event.request.httpVersion + '"', event.request.response.status, event.request.response.headers['content-length'] || 0].join(' ')) callback(); } }})No newline at end of fileNo newline at end of filemodified:lib/express/plugins/content-length.js+2 added-1 removed
6 unmodified lines78910111112131415161718196 unmodified lines /** * Assign Content-Length header unless present. */ response: function(event) { response: function(event, callback) { var response = event.request.response response.headers['content-length'] = response.headers['content-length'] || response.body.length callback(); } }})No newline at end of fileNo newline at end of filemodified:lib/express/plugins/cookie.js+4 added-2 removed
89 unmodified lines90 /**91 * Parse request cookie data.92 */93 94- request: function(event) {94+ request: function(event, callback) {95 event.request.response.cookies = []96 event.request.cookies = event.request.headers.cookie ? 97 exports.parseCookie(event.request.headers.cookie) : 98 {}99+ callback();100 },101 102 /**103 * Set the Set-Cookie header when response cookies are available.104 */105 105- response: function(event) {106+ response: function(event, callback) {107 if (event.response.cookies.length)108 event.request.header('set-cookie', event.response.cookies.join(', '))109+ callback();110 }111 }112 })modified:lib/express/plugins/hooks.js+4 added-2 removed
46 unmodified lines4748495051515253545556575859606161626364656667686946 unmodified lines /** * Invoke all before handlers. */ request: function(event) { request: function(event, callback) { $(exports.callbacks.before).each(function(fn){ fn.call(event.request, event.request) }) callback(); }, /** * Invoke all after handlers. */ response: function(event) { response: function(event, callback) { $(exports.callbacks.after).each(function(fn){ fn.call(event.request, event.request) }) callback(); } }})No newline at end of fileNo newline at end of filemodified:lib/express/plugins/method-override.js+3 added-1 removed
6 unmodified lines789101111121314151617186 unmodified lines /** * Set request method to _method param when present. */ request: function(event) { request: function(event, callback) { if (event.request.param('_method')) event.request.method = event.request.param('_method').toLowerCase() callback(); } }})No newline at end of fileNo newline at end of filemodified:lib/express/plugins/profiler.js+4 added-2 removed
28 unmodified lines29 /**30 * Start timer.31 */32 33- request: function(event) {33+ request: function(event, callback) {34 this.start = Number(new Date)35+ callback();36 },37 38 /**39 * Output duration.40 */41 41- response: function(event) {42+ response: function(event, callback) {43 if (exports.Profiler.format === 'plot')44 puts(Number(new Date) - this.start)45 else46 puts(event.request.method + ' ' + 47 event.request.url.pathname + ': ' + 48 (Number(new Date) - this.start) + ' ms' +49 ' #' + ++n)50+ callback()51 }52 }53 })modified:lib/express/plugins/redirect.js+2 added-1 removed
47 unmodified lines48 * Assign home to 'home', 'basepath' or '/'.49 * Assign back to 'referrer' or 'referer' headers.50 */51 52- request: function(event) {52+ request: function(event, callback) {53 event.request.home = set('home') || set('basepath') || '/'54 event.request.back = event.request.header('referrer') || event.request.header('referer')55+ callback();56 }57 }58 })modified:lib/express/plugins/session.js+4 added-2 removed
184 unmodified lines185 /**186 * Create session id when not found; delegate to store.187 */188 189- request: function(event) {189+ request: function(event, callback) {190 var sid191 if (!(sid = event.request.cookie('sid')))192 event.request.cookie('sid', sid = utils.uid(), set('session cookie'))193 event.request.session = exports.Session.store.fetch(sid)194 event.request.session.touch()195+ callback();196 },197 198 /**199 * Delegate to store, allowing it to save sessions changes.200 */201 201- response: function(event) {202+ response: function(event, callback) {203 exports.Session.store.commit(event.request.session)204+ callback()205 }206 }207 })modified:lib/express/core.js+13 added-11 removed
265 unmodified lines266 * @api private267 */268 269 route: function(request, response){270+ request = new Request(request, response) 270- request = new Request(request, response)271- request.trigger('request')272- if (request.response.finished) return273- try {274- if (typeof (body = (new Router(request)).route()) == 'string')275- request.halt(200, body)276- }277- catch (e) {278- this.error(e, request)279- }271+ var self= this;272+ request.trigger('request', null, function(error) { 273+ if (request.response.finished) return274+ try {275+ if (typeof (body = (new Router(request)).route()) == 'string')276+ request.halt(200, body) 277+ }278+ catch (e) {279+ self.error(e, request)280+ }281+ });282 },283 284 /**285 * Handle errors.8 unmodified lines294 if (e instanceof ExpressError)295 throw e296 if (!(request instanceof Request))297 request = new Request(request, response),296- request.trigger('request')298+ request.trigger('request',null, function(error) {/*NOOP?*/ } )299 if (request.accepts('html') && set('show exceptions'))300 request.halt(500, require('express/pages/show-exceptions').render(request, e))301 else302 request.halt(500)38 unmodified linesmodified:lib/express/plugin.js+4 added-2 removed
45 unmodified lines46 * @param {Event} event47 * @api private48 */49 50- trigger: function(event) {50+ trigger: function(event, callback) {51 if ('on' in this)52 if (event.name in this.on)53- this.on[event.name].call(this, event)53+ this.on[event.name].call(this, event, callback)54+ else callback()55+ else callback()56 }57 })modified:lib/express/request.js+37 added-13 removed
169 unmodified lines170 * @see statusBodies171 * @api public172 */173 174- halt: function(code, body, encoding) {174+ halt: function(code, body, encoding, callback) {175 this.status(code = code || 404)176 if (body = body || statusBodies[code])177- return this.respond(body, encoding)177+ return this.respond(body, encoding, callback)178 throw new InvalidStatusCode(code)179 },180 181 /**4 unmodified lines186 * @see Request#halt()187 * @api private188 */189 190- respond: function(body, encoding) {190+ respond: function(body, encoding, callback) {191 this.response.body = body192- this.trigger('response')193- if (typeof this.response.body != 'string') throw new InvalidResponseBody(this)194- if (typeof this.response.status != 'number') throw new InvalidStatusCode(this.response.status)195- this.response.writeHeader(this.response.status, this.response.headers)196- this.response.write(this.response.body, encoding)197- this.response.close()192+ var self= this;193+ this.trigger('response', null, function (error) {194+ if (typeof self.response.body != 'string') {callback( new InvalidResponseBody(self) ); return; }195+ if (typeof self.response.status != 'number'){ calback( new InvalidStatusCode(self.response.status) ); return; }196+ self.response.writeHeader(self.response.status, self.response.headers)197+ self.response.write(self.response.body, encoding)198+ self.response.close()199+ });200 },201 202 /**203 * Set Content-Type header to the mime type17 unmodified lines221 * @return {Request}222 * @api public223 */224 223- trigger: function(name, data) {225+ trigger: function(name, data, callback) {226 data = process.mixin(data || {}, {227 request: this,228 response: this.response229 })230+ //TODO: we'd want to use the nice collection stuff that was here instead of this crappy approach...231+ var pluginsExecuted= 0,232+ plugin= undefined,233+ totalPlugins= this.plugins.length(),234+ self= this;235+ if( callback ) { 236+ var callNextPlugin= function(error) {237+ if( error || pluginsExecuted >= totalPlugins ) callback(error); // All the plugins have called back or there was an error ..238+ else {239+ try{240+ plugin= self.plugins.at(pluginsExecuted++);241+ if( plugin && plugin.trigger ) plugin.trigger(new Event(name, data), callNextPlugin)242+ else callNextPlugin();243+ } catch(e) {244+ callNextPlugin(e);245+ }246+ }247+ }; 248+ callNextPlugin();249+ }250+ else {228- this.plugins.each(function(plugin){229- plugin.trigger(new Event(name, data))230- })251+ this.plugins.each(function(plugin){252+ plugin.trigger(new Event(name, data)) 253+ }) 254+ }255 return this256 },257 258 /**41 unmodified lines