Merge branch 'async-plugins'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parents ce94168, 934ffd0 · Browse files · 10 files·+64 added−109 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+64 added−109 removed
- tests:+2 added−26 removed
- other:+62 added−83 removed
Provenance
- Commit
4c246a4- Tree
296f754- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent 1
ce94168- Parent 2
934ffd0
10 files changed+64 added−109 removed
modified:lib/express/pages/show-exceptions.js+3 added-3 removed
60 unmodified lines61626364656566676869697071727373747576777860 unmodified lines ' + hash(request.response.headers) + ' \n\ </table> \n\ <h3>Params</h3> \n\ <table id="route-params"> \n\ ' + hash(request.params) + ' \n\ ' + hash(request.params.path) + ' \n\ </table> \n\ <h3>GET</h3> \n\ <table id="get-params"> \n\ ' + hash(request.url.params) + ' \n\ ' + hash(request.params.get) + ' \n\ </table> \n\ <h3>POST</h3> \n\ <table id="post-params"> \n\ ' + hash(request.url.post) + ' \n\ ' + hash(request.params.post) + ' \n\ </table> \n\ </div> \n\ </body> \n\ </html>'}modified:lib/express/plugins/common-logger.js+1 added-1 removed
42 unmodified lines43444546474748495042 unmodified lines '[' + 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(' ')) event.request.response.headers['content-length'] || 0].join(' ')) } }})No newline at end of fileNo newline at end of filemodified:lib/express/core.js+12 added-13 removed
9 unmodified lines10 events = require('events'),11 fs = require('fs')12 13 global.merge(require('sys'))14-global.merge(require('express/exceptions'))14 global.merge(require('express/event'))15 global.merge(require('express/request'))16 global.merge(require('express/plugin'))17 global.merge(require('express/dsl'))244 unmodified lines262 * @api private263 */264 265 route: function(request, response){267- request = new Request(request, response)268- request.trigger('request')269- if (request.response.finished) return270- try {271- if (typeof (body = (new Router(request)).route()) == 'string')272- request.halt(200, body)273- }274- catch (e) {275- this.error(e, request)276- }266+ var self = this,267+ request = new Request(request, response) 268+ request.trigger('request', function(e) {269+ try {270+ if (e) throw e271+ if (request.response.finished) return272+ if (typeof (body = (new Router(request)).route()) === 'string')273+ request.halt(200, body) 274+ } catch (e) {275+ self.error(e, request)276+ }277+ })278 },279 280 /**281 * Handle errors.4 unmodified lines286 * @api private287 */288 289 error: function (e, request, response) {289- if (e instanceof ExpressError)290- throw e290 if (!(request instanceof Request))291 request = new Request(request, response),292 request.trigger('request')293 if (request.accepts('html') && set('show exceptions'))41 unmodified linesdeleted:lib/express/exceptions.js-14 removed
1234567891011121314
// Express - Exceptions - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
// --- ExpressError
ExpressError = Class({ name: 'ExpressError', init: function(message) { this.message = message }, toString: function() { return this.name + ': ' + this.message }})No newline at end of filemodified:lib/express/plugin.js+2 added-2 removed
51 unmodified lines52535455565657585959606151 unmodified lines * @param {Event} event * @api private */ trigger: function(event) { trigger: function(event, callback) { if ('on' in this) if (event.name in this.on) this.on[event.name].call(this, event) return this.on[event.name].call(this, event, callback) }})No newline at end of fileNo newline at end of filemodified:lib/express/request.js+38 added-40 removed
10 unmodified lines11 mime = require('express/mime'),12 utils = require('express/utils'),13 url = require('url')14 15-// --- InvalidStatusCode16-17-InvalidStatusCode = ExpressError.extend({18- name: 'InvalidStatusCode',19- init: function(status) {20- this.message = status + ' is an invalid HTTP response code'21- }22-})23-24-// --- InvalidResponseBody25-26-InvalidResponseBody = ExpressError.extend({27- name: 'InvalidResponseBody',28- init: function(request) {29- this.message = request.method + ' ' + JSON.encode(request.url.pathname) + ' did not respond with a body string'30- }31-})32-15 // --- Helpers16 17 /**18 * Normalize the given _path_.51 unmodified lines70 * @api public71 */72 73 header: function(key, val) {92- return val === undefined ?93- this.headers[key.toLowerCase()] :94- this.response.headers[key.toLowerCase()] = val74+ return val === undefined75+ ? this.headers[key.toLowerCase()]76+ : this.response.headers[key.toLowerCase()] = val77 },78 79 /**80 * Return a param _key_ value or null.61 unmodified lines142 * The status _code_ defaults to to 404, and _body_ will143 * default to the default body associated with the response144 * _code_.145 *164- * When an invalid status _code_ is passed, InvalidStatusCode165- * will be thrown.166- *146 * @param {int} code147 * @param {string} body148 * @param {string} encoding149 * @see statusBodies150 * @api public151 */152 174- halt: function(code, body, encoding) {153+ halt: function(code, body, encoding, callback) {154 this.status(code = code || 404)155 if (body = body || statusBodies[code])177- return this.respond(body, encoding)178- throw new InvalidStatusCode(code)156+ return this.respond(body, encoding, callback)157 },158 159 /**160 * Respond with _body_ and optional _encoding_.3 unmodified lines164 * @see Request#halt()165 * @api private166 */167 190- respond: function(body, encoding) {168+ respond: function(body, encoding, callback) {169+ var self = this170 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()171+ this.trigger('response', function(e) {172+ if (e)173+ if (callback !== undefined) callback(e)174+ else throw e175+ self.response.writeHeader(self.response.status, self.response.headers)176+ self.response.write(self.response.body, encoding)177+ self.response.close()178+ });179 },180 181 /**182 * Set Content-Type header to the mime type9 unmodified lines192 return this193 },194 195 /**215- * Trigger even _name_ with optional _data_.196+ * Trigger event _name_ with optional _data_ and _callback_ function.197+ * The _callback_ function may be the second or third argument.198 *199 * @param {string} name218- * @param {hash} data200+ * @param {object} data201+ * @param {function} callback202 * @return {Request}203 * @api public204 */205 223- trigger: function(name, data) {206+ trigger: function(name, data, callback) {207+ if (data instanceof Function)208+ callback = data,209+ date = null210 data = data || {}211 data.merge({ request: this, response: this.response })226- this.plugins.each(function(plugin){227- plugin.trigger(new Event(name, data))228- })212+ var self = this,213+ complete = 0,214+ total = this.plugins.length215+ ;(function next(e) {216+ if (e || complete === total)217+ callback(e)218+ else {219+ try {220+ if (self.plugins.at(complete++).trigger(new Event(name, data), next) !== true)221+ next()222+ } catch(e) {223+ next(e)224+ }225+ }226+ })()227 return this228 },229 230 /**41 unmodified linesmodified:lib/express/static.js+6 added-10 removed
1 2 // Express - Static - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3 4+/**5+ * Module dependencies.6+ */7+8 var path = require('path'),9 fs = require('fs')10 7-// --- InvalidPathError8-9-InvalidPathError = ExpressError.extend({10- name: 'InvalidPathError',11- init: function(path) {12- this.message = "`" + path + "' is not a valid path"13- }14-}) 15- 11 // --- File12 13 exports.File = Class({14 5 unmodified lines20 */21 22 init: function(path) {23 this.path = path29- if (path.indexOf('..') != -1) throw new InvalidPathError(path)24+ if (path.indexOf('..') != -1)25+ Error.raise('InvalidPathError', "`" + path + "' is not a valid path")26 },27 28 /**29 * Transfer static file to the given _request_.32 unmodified linesmodified:spec/spec.request.js-12 removed
105 unmodified lines106 get('/user').status.should.eql 400107 get('/user').body.should.include('Oh noes!')108 end109 end110-111- describe 'when given an invalid status code'112- it 'should throw an InvalidStatusCode exception'113- // TODO: use throw_error when fixed...114- get('/user', function(){ this.halt(123123) })115- try { get('/user') }116- catch (e) {117- e.should.be_an_instance_of ExpressError118- e.should.be_an_instance_of InvalidStatusCode119- }120- end121- end110 end111 112 describe '#contentType()'113 it 'should set Content-Type header with mime type passed'90 unmodified linesmodified:spec/spec.routing.js-13 removed
82 unmodified lines83 get('/foo').body.should.eql 'bar'84 end85 end86 87- describe 'with no response body'88- it 'should throw a InvalidResponseBody'89- // TODO: use throw_error when fixed...90- get('/user', function(){91- this.respond()92- })93- try { get('/user') }94- catch (e) {95- e.should.be_an_instance_of InvalidResponseBody96- }97- end98- end99- 87 describe 'with regular expression'88 it 'should match'89 get(/^\/user\/(\d+)\/(\w+)/, function(id, operation){90 return [id, operation].join(', ')218 unmodified linesmodified:spec/spec.static.js+2 added-1 removed
12 unmodified lines13 it 'should throw an InvalidPathError when .. is found'14 // TODO: use throw_error when fixed...15 try { new StaticFile('/../foobar') }16 catch (e) {17- e.should.be_an_instance_of InvalidPathError17+ e.name.should.eql 'InvalidPathError'18+ e.message.should.eql "`/../foobar' is not a valid path"19 }20 end21 end22 end1 unmodified line