Skip to content
Myr

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

10 files+64 added−109 removed

modified:lib/express/pages/show-exceptions.js+3 added-3 removed
modified:lib/express/plugins/common-logger.js+1 added-1 removed
modified:lib/express/core.js+12 added-13 removed
9 unmodified lines
10 events = require('events'),11 fs = require('fs')1213global.merge(require('sys'))14global.merge(require('express/exceptions'))14global.merge(require('express/event'))15global.merge(require('express/request'))16global.merge(require('express/plugin'))17global.merge(require('express/dsl'))
244 unmodified lines
262 * @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 lines
286 * @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 lines
deleted:lib/express/exceptions.js-14 removed
modified:lib/express/plugin.js+2 added-2 removed
modified:lib/express/request.js+38 added-40 removed
10 unmodified lines
11 mime = require('express/mime'),12 utils = require('express/utils'),13 url = require('url')1415// --- InvalidStatusCode1617InvalidStatusCode = ExpressError.extend({18 name: 'InvalidStatusCode',19 init: function(status) {20 this.message = status + ' is an invalid HTTP response code'21 }22})2324// --- InvalidResponseBody2526InvalidResponseBody = 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})3215// --- Helpers1617/**18 * Normalize the given _path_.
51 unmodified lines
70 * @api public71 */7273 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 lines
142 * 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 */152174 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 lines
164 * @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 type
9 unmodified lines
192 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 */205223 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 lines
modified:lib/express/static.js+6 added-10 removed
12// Express - Static - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)34/**5 * Module dependencies.6 */78var path = require('path'),9 fs = require('fs')10 7// --- InvalidPathError89InvalidPathError = ExpressError.extend({10 name: 'InvalidPathError',11 init: function(path) {12 this.message = "`" + path + "' is not a valid path"13 }14}) 15 11// --- File1213exports.File = Class({14
5 unmodified lines
20 */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 lines
modified:spec/spec.request.js-12 removed
105 unmodified lines
106 get('/user').status.should.eql 400107 get('/user').body.should.include('Oh noes!')108 end109 end110111 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 end111112 describe '#contentType()'113 it 'should set Content-Type header with mime type passed'
90 unmodified lines
modified:spec/spec.routing.js-13 removed
82 unmodified lines
83 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 lines
modified:spec/spec.static.js+2 added-1 removed
12 unmodified lines
13 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 end
1 unmodified line