Change the approach to be truth based
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Now just enforces that a plugin returns true, if it doesn't return true then we assume the plugin is taking care of making sure that the plugin chain continues. A sensible enhancement to this might be to improve the request.trigger function to timeout after a fixed period, log a warning, then carry on as normal ?? I'm not too sure of what 'Hooks.js' does so I may haveconfigured that incorrectly, just FYI
authored by ciaranj · committed by ciaranj · parent 27ff134 · Browse files · 10 files·+32 added−29 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+32 added−29 removed
- other:+32 added−29 removed
Provenance
- Commit
e92b01f- Tree
c9c7869- Author
ciaranj <ciaranj@gmail.com>- Committer
ciaranj <ciaranj@gmail.com>- Parent
27ff134
10 files changed+32 added−29 removed
modified:lib/express/plugins/body-decoder.js+2 added-3 removed
16 unmodified lines1718192021212223242526272627282916 unmodified lines * * - application/x-www-form-urlencoded */ request: function(event, callback) { request: function(event) { var request = event.request if (request.header('content-type') && request.header('content-type').includes('application/x-www-form-urlencoded')) request.params.post = queryString.parseQuery(request.body) callback(); return true; } }})No newline at end of fileNo newline at end of filemodified:lib/express/plugins/common-logger.js+5 added-3 removed
34 unmodified lines3536373839394041424344454647484748495051525334 unmodified lines /** * Output log data. */ response: function(event, callback) { response: function(event) { 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(); event.request.response.headers['content-length'] || 0].join(' ')) callback(); } return true; } }})No newline at end of fileNo newline at end of filemodified:lib/express/plugins/content-length.js+2 added-2 removed
6 unmodified lines7891011111213141516161718196 unmodified lines /** * Assign Content-Length header unless present. */ response: function(event, callback) { response: function(event) { var response = event.request.response response.headers['content-length'] = response.headers['content-length'] || response.body.length callback(); return true; } }})No newline at end of fileNo newline at end of filemodified:lib/express/plugins/cookie.js+4 added-4 removed
89 unmodified lines90 /**91 * Parse request cookie data.92 */93 94- request: function(event, callback) {94+ request: function(event) {95 event.request.response.cookies = []96 event.request.cookies = event.request.headers.cookie97 ? exports.parseCookie(event.request.headers.cookie)98 : {}99- callback()99+ return true;100 },101 102 /**103 * Set the Set-Cookie header when response cookies are available.104 */105 106- response: function(event, callback) {106+ response: function(event) {107 if (event.response.cookies.length)108 event.request.header('set-cookie', event.response.cookies.join(', '))109- callback();109+ return true;110 }111 }112 })modified:lib/express/plugins/method-override.js+2 added-3 removed
6 unmodified lines78910111112131415141516176 unmodified lines /** * Set request method to _method param when present. */ request: function(event, callback) { request: function(event) { if (event.request.param('_method')) event.request.method = event.request.param('_method').toLowerCase() callback(); return true; } }})No newline at end of fileNo newline at end of filemodified:lib/express/plugins/profiler.js+4 added-4 removed
28 unmodified lines293031323333343535363738394041424243444546474849505051525328 unmodified lines /** * Start timer. */ request: function(event, callback) { request: function(event) { this.start = Number(new Date) callback(); return true; }, /** * Output duration. */ response: function(event, callback) { response: function(event) { if (exports.Profiler.format === 'plot') puts(Number(new Date) - this.start) else puts(event.request.method + ' ' + event.request.url.pathname + ': ' + (Number(new Date) - this.start) + ' ms' + ' #' + ++n) callback() return true; } }})No newline at end of fileNo newline at end of filemodified:lib/express/plugins/redirect.js+2 added-2 removed
47 unmodified lines48 * Assign home to 'home', 'basepath' or '/'.49 * Assign back to 'referrer' or 'referer' headers.50 */51 52- request: function(event, callback) {52+ request: function(event) {53 event.request.home = set('home') || set('basepath') || '/'54 event.request.back = event.request.header('referrer') || event.request.header('referer')55- callback();55+ return true;56 }57 }58 })modified:lib/express/plugins/session.js+4 added-4 removed
183 unmodified lines184 /**185 * Create session id when not found; delegate to store.186 */187 188- request: function(event, callback) {188+ request: function(event) {189 var sid190 if (!(sid = event.request.cookie('sid')))191 event.request.cookie('sid', sid = utils.uid(), set('session cookie'))192 event.request.session = exports.Session.store.fetch(sid)193 event.request.session.touch()194- callback();194+ return true;195 },196 197 /**198 * Delegate to store, allowing it to save sessions changes.199 */200 201- response: function(event, callback) {201+ response: function(event) {202 exports.Session.store.commit(event.request.session)203- callback()203+ return true;204 }205 }206 })modified:lib/express/plugin.js+2 added-3 removed
54 unmodified lines55 56 trigger: function(event, callback) {57 if ('on' in this)58 if (event.name in this.on)59- this.on[event.name].call(this, event, callback)60- else callback()61- else callback()59+ return this.on[event.name].call(this, event, callback)60+ return true;61 }62 })modified:lib/express/request.js+5 added-1 removed
227 unmodified lines228 //TODO: we'd want to use the nice collection stuff that was here instead of this crappy approach...229 var pluginsExecuted= 0,230 plugin= undefined,231 totalPlugins= this.plugins.length,232+ pluginResult, 233 self= this;234 var callNextPlugin= function(error) {235 if( error || pluginsExecuted >= totalPlugins ) callback(error); // All the plugins have called back or there was an error ..236 else {237 try{238 plugin= self.plugins.at(pluginsExecuted++);238- if( plugin && plugin.trigger ) plugin.trigger(new Event(name, data), callNextPlugin)239+ if( plugin && plugin.trigger ) {240+ pluginResult= plugin.trigger(new Event(name, data), callNextPlugin);241+ if(pluginResult) callNextPlugin() // If the plugin returned true then it handled the event inline, otherwise we assume it is going to call the callback itself.242+ }243 else callNextPlugin();244 } catch(e) {245 callNextPlugin(e);246 }48 unmodified lines