Skip to content
Myr

Misc module progress

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by visionmedia · committed by visionmedia · parent abcac57 · Browse files · 6 files+142 added−98 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+142 added−98 removed

  • tests:+48 added−34 removed
  • other:+94 added−64 removed

6 files+142 added−98 removed

modified:lib/express.cookie.js+52 added
modified:lib/express.core.js+41 added-63 removed
2 unmodified lines
34(function(){5 Express = { 6 version : '0.0.1',7 utilities : {},8 modules : [],9 routes : [],10 11 // --- Halt12
13 unmodified lines
26 // --- Settings27 28 settings : {29 basepath : '/',28 29 cookie : {30 maxAge : 360031 },32 30 defaultRoute : {31 callback : function() {32 Express.respond('Page or file cannot be found', 'Not Found')33 }34 }35 },36 40 hookCallbacks : {41 request : [42 43 // Redirect helpers44 45 function (request, response) { 37 // --- Modules38 39 RedirectHelpers : {40 onInit : {41 'set home' : function(){42 Express.home = Express.settings.basepath47 },48 49 // Cookie parsing50 51 function (request, response) {52 Express.response.cookie = request.cookie = Express.parseCookie(request.headers['Cookie'])43 }54 ],55 56 response : [57 58 // Content length59 60 function(request, response) {44 }45 },46 47 ContentLength : {48 onRequest : {49 'set content length' : function() {50 Express.header('Content-Length', (Express.response.body || '').length)51 }63 ]52 }53 },54 55 /**56 * Invoke hook _name_ with _args_.
3 unmodified lines
60 * @api public61 */62 63 hook : function(name, args) {75 if (this.hookCallbacks[name])76 for (i = 0, len = this.hookCallbacks[name].length; i < len; ++i)77 if (typeof this.hookCallbacks[name][i] == 'function')78 this.hookCallbacks[name][i].apply(this, this.argsArray(arguments, 1))64 for (i = 0, len = this.modules.length; i < len; ++i)65 if (typeof this.modules[i][name] == 'function')66 this.modules[i][name].apply(this.modules[i], this.argsArray(arguments, 1))67 },68 69 /**82 * Add middlware _callback_ for hook _name_.70 * Add _module_.71 *84 * @param {string} name85 * @param {function} callback72 * @param {hash} module73 * @api public74 */75 89 use : function(name, callback) {90 Express.hookCallbacks[name].push(callback)76 use : function(module) {77 Express.settings[module.name] = {}7879 if ('init' in module) 80 module.init.call(module)8182 if ('settings' in module) 83 for (var name in module.settings)84 if (module.settings.hasOwnProperty(name))85 Express.settings[module.name][name] = module.settings[name]86 87 if ('utilities' in module) 88 for (var name in module.utilities)89 if (module.utilities.hasOwnProperty(name))90 Express.utilities[name] = module.utilities[name]91 92 Express.modules.push(module)93 },94 95 /**96 * Start Express; binding to _host_ / _port_.
36 unmodified lines
133 Express.response.body = null134 Express.response.status = 200135 Express.request = request136 request.headers = Express.arrayToHash(request.headers)135 Express.hook('request', request, response)137 Express.hook('onRequest')138 request.uri.params = Express.parseNestedParams(request.uri.params)139 request.uri.path = Express.normalizePath(request.uri.path)140 try { Express.response.body = Express.callRouteFor(request) }141 catch (e) { Express.fail(e) }140 Express.prepareCookie()141 Express.hook('response', Express.request, Express.response)142 Express.hook('onResponse')143 response.sendHeader(Express.response.status, Express.hashToArray(Express.response.headers))144 response.sendBody(Express.response.body || '')145 response.finish()146 }
12 unmodified lines
159 this.response.body = e.message ? e.name + ': ' + e.message : e.toString()160 this.response.status = 500161 },162 162 prepareCookie : function() {163 var cookie = []164 for (var key in this.response.cookie)165 if (this.response.cookie.hasOwnProperty(key))166 cookie.push(key + '=' + this.response.cookie[key])167 if (cookie.length)168 this.header('Set-Cookie', cookie.join('; '))169 },170 163 /**164 * Return an associative array generated165 * from the key / value pairs in _hash_.166 *
123 unmodified lines
290 param : function(key, defaultValue) {291 return this.request.uri.params[key] || defaultValue292 },293 302 294 /**304 * Return cookie _key_. When _value_ is present305 * then a response cookie field will be set.306 *307 * @param {string} key308 * @param {string} value309 * @return {string, hash}310 * @api public311 */312313 cookie : function(key, value) {314 return value ? this.response.cookie[key] = value :315 this.response.cookie[key]316 },317 318 /**295 * Parse _params_, splitting { user[info][name] : 'tj' } into296 * { user : { info : { name : 'tj' }}}297 *298 * @param {hash} params
116 unmodified lines
415 * @api public416 */417 418 eval : function(string) {443 return eval('with (Express){' + string + '}')419 return eval('with (Express){ with (Express.utilities){ ' + string + '}}')420 },421 422 /**423 * Escape special characters in _html_.
143 unmodified lines
567 del = Express.routeFunctionFor('delete')568 put = Express.routeFunctionFor('put')569 use = Express.use570 571 use(Express.RedirectHelpers)572 use(Express.ContentLength)573})()
modified:lib/express.mocks.js+1 added-1 removed
added:spec/spec.cookie.js+42 added
modified:spec/spec.core.js+1 added-31 removed
21 unmodified lines
22 end23 24 describe '.parseCookie()'25 it 'should parse cookie pairs'26 cookie = 'expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; q=foo%3dbar; domain=example.net'26 var cookie = 'expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; q=foo%3dbar; domain=example.net'27 parts = Express.parseCookie(cookie)28 parts.expires.should.eql 'Fri, 31-Dec-2010 23:59:59 GMT'29 parts.path.should.eql '/'30 parts.q.should.eql 'foo=bar'31 parts.domain.should.eql 'example.net'32 end33 end34 35 describe '.cookie()'36 before_each37 cookie = 'path=/; q=foo%3dbar; domain=.example.net'38 Express.response.cookie = Express.parseCookie(cookie)39 end40 41 it 'should return cookie value when key passed'42 Express.cookie('path').should.eql '/'43 Express.cookie('domain').should.eql '.example.net'44 end45 46 it 'should set response cookies when value passed'47 Express.cookie('foo', 'bar')48 Express.cookie('foo').should.eql 'bar'49 end50 end51 35 describe '.argsArray()'36 it 'should return an array of arguments'37 Express.argsArray(-{ return arguments }('foo', 'bar')).should.eql ['foo', 'bar']38 end
88 unmodified lines
127 Express.arrayToHash(headers).should.eql { 'Content-Type' : 'text/plain' }128 end129 end130 148 describe '.hook()'149 it 'should invoke hook callbacks'150 var one, two151 Express.hookCallbacks = { 'test' : [152 function(a, b){ one = [a, b] },153 function(){ two = true }154 ] }155 Express.hook('test', 1, 2)156 one.should.eql [1, 2]157 two.should.be_true158 end159 end160 131 describe '.redirect()'132 it 'should redirect to the url specified'133 get('foo', function(){ redirect('http://google.com') })134 response = get('foo')
12 unmodified lines
modified:spec/spec.node.js+5 added-3 removed