Merge branch 'master' of git://github.com/visionmedia/express
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ciaranj · committed by ciaranj · parents e125ae2, 8207e70 · Browse files · 18 files·+365 added−101 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
18 files+365 added−101 removed
- tests:+131 added−28 removed
- other:+234 added−73 removed
Provenance
- Commit
704c933- Tree
0779dcd- Author
ciaranj <ciaranj@gmail.com>- Committer
ciaranj <ciaranj@gmail.com>- Parent 1
e125ae2- Parent 2
8207e70
18 files changed+365 added−101 removed
modified:benchmarks/collection.js+1 added-1 removed
12 unmodified lines1314151617171819202190 unmodified lines12 unmodified lines array.push(a-1) return array}
suite('Collection with [0..10,000]', 1000, function(){suite('Collection with [0..10,000]', 100, function(){ array = range(0, 10000) benchmark('for', function(){ for (var i = 0, len = array.length; i < len; ++i) ;90 unmodified linesadded:benchmarks/views.js+35 added
1234567891011121314151617181920212223242526272829303132333435
require.paths.unshift('lib')require.paths.unshift('benchmarks')process.mixin(GLOBAL, require('sys'))process.mixin(GLOBAL, require('benchmark'))require('express')
print = puts
engine = { ejs: require('support/ejs/ejs'), haml: require('support/haml/lib/haml')}
options = { locals: { article: { title: 'Foo', body: 'bar' }}}
ejs = ' \n\<h1><%= article.title %></h1> \n\<p><%= article.body %></p> \n\'
haml = ' \n\%h1= article.title\n\%p= article.body \n\'
suite('Template Engines', 1000, function(){ benchmark('ejs', function(){ engine.ejs.render(ejs, options) }) benchmark('haml', function(){ engine.haml.render(haml, options) })})No newline at end of filemodified:examples/views/chat.haml.html+2 added
12345678910%h1 Chat%img.bubble{ src: '/public/images/bubble.png' }%ul#messages :each msg in messages %li= msg%form{ method: 'post' } %input{ type: 'hidden', name: '_method', value: 'put' } %input{ type: 'text', name: 'name', value: 'guest' } %input{ type: 'text', name: 'message' } %input{ type: 'submit', value: 'Send' }modified:examples/app.js+1 added-1 removed
6 unmodified lines7891011111213141547 unmodified lines6 unmodified lines use(MethodOverride) use(ContentLength) use(CommonLogger) set('root', dirname(__filename)) enable('cache views') enable('cache view contents')})
var messages = [], StaticFile = require('express/static').File47 unmodified linesmodified:lib/express/plugins/body-decoder.js+3 added-1 removed
1 2 // Express - BodyDecoder - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3 4+var queryString = require('querystring')5+6 exports.BodyDecoder = Plugin.extend({7 on: {8 9 /**5 unmodified lines15 request: function(event) {16 var request = event.request17 if (request.header('content-type') &&18 request.header('content-type').indexOf('application/x-www-form-urlencoded') > -1)17- request.params.post = parseParams(request.body)19+ request.params.post = queryString.parseQuery(request.body)20 }21 }22 })added:lib/express/plugins/cache.js+159 added
1+2+// Express - Cache - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3+4+// --- Store5+6+exports.Store = Class({7+ 8+ /**9+ * Ensure that the given _key_ is a string.10+ * Override in subclass to provide data-store specific functionality.11+ *12+ * @param {string} key13+ * @param {string} val14+ * @api public15+ */16+ 17+ set: function(key, val) {18+ if (typeof key !== 'string') throw new Error(this.name + ' store #set() key must be a string')19+ },20+ 21+ /**22+ * Ensure that the given _key_ is a string.23+ * Override in subclass to provide data-store specific functionality.24+ *25+ * @param {string} key26+ * @api public27+ */28+ 29+ get: function(key) {30+ if (typeof key !== 'string') throw new Error(this.name + 'store #get() key must be a string')31+ },32+ 33+ /**34+ * Convert to '[NAME Store]'.35+ *36+ * @return {string}37+ * @api public38+ */39+ 40+ toString: function() {41+ return '[' + this.name + ' Store]'42+ }43+})44+45+// --- Store.Memory46+47+exports.Store.Memory = exports.Store.extend({48+ 49+ /**50+ * Datastore name.51+ */52+ 53+ name: 'Memory',54+ 55+ /**56+ * Initialize data.57+ */58+ 59+ init: function() {60+ this.data = {}61+ },62+ 63+ /**64+ * Set the given _key_ to _val_, returning _val_.65+ *66+ * @param {string} key67+ * @param {string} val68+ * @return {string}69+ * @api public70+ */71+ 72+ set: function(key, val) {73+ this.__super__(key, val)74+ return this.data[key] = val75+ },76+ 77+ /**78+ * Get data found matching the given _key_.79+ *80+ * Examples:81+ *82+ * cache.get('page:front')83+ * // => '<html>...</html>'84+ *85+ * cache.get('page:*')86+ * // => { 'page:front': '<html>...</html>', 87+ * 'page:users': '<html>...</html>',88+ * ... }89+ *90+ * @param {string} key91+ * @return {mixed}92+ * @api public93+ */94+ 95+ get: function(key) {96+ this.__super__(key)97+ if (key.indexOf('*') === -1)98+ return this.data[key]99+ var vals = {},100+ regexp = this.normalize(key)101+ for (var key in this.data)102+ if (this.data.hasOwnProperty(key))103+ if (regexp.test(key))104+ vals[key] = this.data[key]105+ return vals 106+ },107+ 108+ /**109+ * Clear data matching the given _key_.110+ *111+ * Examples:112+ *113+ * cache.clear('page:front')114+ * cache.clear('page:*')115+ *116+ * @param {string} key117+ * @api public118+ */119+ 120+ clear: function(key) {121+ if (key.indexOf('*') === -1)122+ return delete this.data[key]123+ var regexp = this.normalize(key)124+ for (var key in this.data)125+ if (this.data.hasOwnProperty(key))126+ if (regexp.test(key))127+ delete this.data[key]128+ },129+ 130+ /**131+ * Convert the given key matching _pattern_ 132+ * into a RegExp.133+ *134+ * - * is converted to (.*?)135+ *136+ * @param {string} pattern137+ * @return {regexp}138+ * @api private139+ */140+ 141+ normalize: function(pattern) {142+ return new RegExp('^' + pattern.replace(/[*]/g, '(.*?)') + '$')143+ }144+})145+146+exports.Cache = Plugin.extend({147+ extend: {148+ 149+ /**150+ * Initialize extensions.151+ */152+ 153+ init: function() {154+ Request.include({155+ cache: new exports.Store.Memory156+ })157+ }158+ }159+})modified:lib/express/plugins/view.js+3 added-3 removed
13 unmodified lines14 */15 16 var engine = {17 ejs: require('support/ejs/ejs'),18- haml: require('support/haml/haml')18+ haml: require('support/haml/lib/haml')19 }20 21 // --- View22 33 unmodified lines56 * - locals: Most engines support a hash of local variable names / values.57 *58 * @param {string} view59 * @param {hash} options60- * @settings 'views', 'cache views'60+ * @settings 'views', 'cache view contents'61 * @api public62 */63 64 render: function(view, options) {15 unmodified lines80 }))81 else82 self.halt(200, content)83 }84- if (set('cache views') && cache[view])84+ if (set('cache view contents') && cache[view])85 render(cache[view])86 else87 posix.cat(path).addCallback(function(content){88 render(cache[view] = content)9 unmodified linesmodified:lib/express/core.js+2 added-1 removed
250 unmodified lines251 252 // --- Express253 254 Express = { 255- version: '0.0.1',255+ version: '0.0.2',256 config: [],257 routes: [],258 plugins: [],259 settings: {},3 unmodified lines263 // --- Defaults264 265 configure(function(){266 use(require('express/plugins/view').View)267+ use(require('express/plugins/cache').Cache)268 use(require('express/plugins/redirect').Redirect)269 use(require('express/plugins/body-decoder').BodyDecoder)270 })271 8 unmodified linesmodified:lib/express/helpers.js+1 added-58 removed
90 unmodified lines91 92 exports.escapeRegexp = function(string, chars) {93 var specials = (chars || '/ . * + ? | ( ) [ ] { } \\').split(' ').join('|\\')94 return string.replace(new RegExp('(\\' + specials + ')', 'g'), '\\$1')95-}96-97-/**98- * Decode values in _params_.99- *100- * @param {hash} params101- * @see parseNestedParams()102- * @api private103- */104-105-function decode(params) {106- for (var key in params)107- params[key] = decodeURIComponent(params[key]).replace(/\+/g, ' ')108-}109-110-/**111- * Parse nested _params_.112- *113- * @param {hash} params114- * @return {hash}115- * @see parseParams() 116- * @api public117- */118-119-exports.parseNestedParams = function(params) {120- var parts, key121- decode(params)122- for (key in params)123- if (parts = key.split('['))124- if (parts.length > 1)125- for (var i = 0, prop = params, len = parts.length; i < len; ++i) {126- var name = parts[i].replace(']', '')127- if (i == len - 1)128- prop[name] = params[key],129- prop = params, 130- delete params[key]131- else132- prop = prop[name] = prop[name] || {}133- }134- return params135-}136-137-/**138- * Parse params _str_ into a nested hash.139- *140- * @param {string} str141- * @return {hash}142- * @api public143- */144-145-exports.parseParams = function(str) {146- if (typeof str !== 'string') return147- return exports.parseNestedParams($(str.split('&')).reduce({}, function(params, pair){148- pair = pair.split('=')149- params[pair[0]] = pair[1]150- return params151- }))152-}95+}modified:lib/express/request.js+2 added-1 removed
5 unmodified lines6 */7 8 var StaticFile = require('express/static').File,9 statusBodies = require('http').STATUS_CODES,10+ queryString = require('querystring'),11 url = require('url')12 13 // --- InvalidStatusCode14 52 unmodified lines67 this.response = response68 this.url = url.parse(this.url)69 this.url.pathname = exports.normalizePath(this.url.pathname)70 this.params = { 70- get: parseParams(this.url.query) || {}, 71+ get: this.url.query ? queryString.parseQuery(this.url.query) : {}, 72 post: {}, 73 path: {}74 }75 this.plugins = $(Express.plugins).map(function(plugin){189 unmodified linesmodified:lib/support/hamlnot shown
Its text is not shown, modified.
modified:spec/node.js+1 added
31 unmodified lines32 'mime',33 'static',34 'collection',35 'plugins',36+ 'plugins.cache',37 'plugins.view',38 'plugins.common-logger',39 'plugins.content-length',40 'plugins.method-override',26 unmodified linesmodified:spec/spec.helpers.js-27 removed
1 2 describe 'Express'3- describe 'parseParams()'4- it 'should parse simple query string key / value pairs'5- parseParams('foo=bar').should.eql { foo: 'bar' }6- parseParams('foo=bar&baz=1').should.eql { foo: 'bar', baz: '1' }7- end8- 9- it 'should parse named nested params'10- var user = { user: { name: 'tj', email: 'tj@vision-media.ca' }}11- parseParams('user[name]=tj&user[email]=tj@vision-media.ca').should.eql user12- end13- 14- it 'should parse several levels of nesting'15- var user = { user: { name: 'tj', email: { primary: 'tj@vision-media.ca' }}}16- parseParams('user[name]=tj&user[email][primary]=tj@vision-media.ca').should.eql user17- end18- 19- it 'should convert + to literal space'20- parseParams('foo=bar+baz++that').should.eql { foo: 'bar baz that' }21- parseParams('user[name]=tj+holowaychuk').should.eql { user: { name: 'tj holowaychuk' }}22- end23- 24- it 'should decode hex literals'25- parseParams('foo=bar%20baz%20%20that').should.eql { foo: 'bar baz that' }26- parseParams('user[name]=tj%20holowaychuk').should.eql { user: { name: 'tj holowaychuk' }}27- end28- end29- 3 describe 'toArray()'4 describe 'when given an array'5 it 'should return the array'6 toArray([1,2,3]).should.eql [1,2,3]42 unmodified linesadded:spec/spec.plugins.cache.js+110 added
1+2+describe 'Express'3+ before_each4+ reset()5+ use(require('express/plugins/cache').Cache)6+ cache = require('express/plugins/cache')7+ end8+ 9+ describe 'Cache'10+ describe 'Request'11+ describe '#cache'12+ it 'should use memory store by default'13+ get('/item', function(){14+ return this.cache.toString()15+ })16+ get('/item').body.should.eql '[Memory Store]'17+ end18+ end19+ end20+ end21+ 22+ describe 'Store'23+ describe 'Memory'24+ before_each25+ store = new cache.Store.Memory26+ end27+ 28+ describe '#toString()'29+ it 'should return [Memory Store]'30+ store.toString().should.eql '[Memory Store]'31+ end32+ end33+ 34+ describe '#set()'35+ describe 'given a key and value'36+ it 'should set the cache data'37+ store.set('foo', 'bar')38+ store.get('foo').should.eql 'bar'39+ end40+ 41+ it 'should override existing data'42+ store.set('foo', 'bar')43+ store.set('foo', 'baz')44+ store.get('foo').should.eql 'baz'45+ end46+ 47+ it 'should return data'48+ store.set('foo', 'bar').should.eql 'bar'49+ end50+ end51+ 52+ describe 'given an abitrary key'53+ it 'should throw an error'54+ -{ store.set({}, 'foo') }.should.throw_error55+ end56+ end57+ 58+ describe 'given an abitrary value'59+ it 'should serialize as JSON'60+ store.set('user', { name: 'tj' }).should.eql { name: 'tj' }61+ end62+ end63+ end64+ 65+ describe '#get()'66+ describe 'given a key'67+ it 'should return cached value'68+ store.set('foo', 'bar')69+ store.get('foo').should.eql 'bar'70+ end71+ 72+ it 'should unserialize JSON data'73+ store.set('user', { name: 'tj' })74+ store.get('user').should.eql { name: 'tj' }75+ end76+ end77+ 78+ describe 'given wildcards'79+ it 'should return a set of caches'80+ store.set('user:1', 'a')81+ store.set('user:2', 'b')82+ store.set('foo', 'bar')83+ store.get('user:*').should.eql { 'user:1': 'a', 'user:2': 'b' }84+ end85+ end86+ end87+ 88+ describe '#clear()'89+ describe 'given a key'90+ it 'should delete previous data'91+ store.set('foo', 'bar')92+ store.clear('foo')93+ store.get('foo').should.be_null94+ end95+ end96+ 97+ describe 'given wildcards'98+ it 'should clear a set of caches'99+ store.set('user:one', '1')100+ store.set('user:two', '2')101+ store.clear('user:*')102+ store.get('user:one').should.be_null103+ store.get('user:two').should.be_null104+ end105+ end106+ end107+ 108+ end109+ end110+endmodified:spec/spec.request.js+20 added-1 removed
105 unmodified lines106 })107 get('/style.css').headers['content-type'].should.eql 'text/css'108 end109 end110+ 111+ describe '#attachment()'112+ it 'should set Content-Disposition to attachment'113+ get('/report', function(){114+ this.attachment()115+ return 'foo'116+ })117+ get('/report').headers['content-disposition'].should.eql 'attachment'118+ end119 120+ it 'should set attachment filename'121+ get('/report', function(){122+ this.attachment('report.pdf')123+ return 'foo'124+ })125+ get('/report').headers['content-disposition'].should.eql 'attachment; filename="report.pdf"'126+ end127+ end128+129 describe '#param()'130 it 'should return a route placeholder value'131 get('/user/:id', function(){132 return 'user ' + this.param('id')38 unmodified lines171 get('/user/12').body.should.eql '12'172 get('/product/ipod').body.should.eql 'ipod'173 end174 157- it 'should access request.url.params'175+ it 'should work with a query string'176 get('/user', function(){177 return this.param('page') || 'First page'178 })179 get('/user').body.should.eql 'First page'180 get('/user?page=2').body.should.eql '2'181+ get('/user?foo[]=bar&page=5').body.should.eql '5'182 end183 184 it 'should work with splats'185 get('/:path/*.*', function(path, file, ext){7 unmodified linesmodified:History.md+17 added
1 2+0.0.2 / 2010-01-103+==================4+5+ * Added view benchmarks; currently haml vs ejs6+ * Added Request#attachment() specs. Closes #1167+ * Added use of node's parseQuery() util. Closes #1238+ * Added `make init` for submodules9+ * Updated Haml10+ * Updated sample chat app to show messages on load11+ * Updated libxmljs parseString -> parseHtmlString12+ * Fixed `make init` to work with older versions of git13+ * Fixed specs can now run independant specs for those who cant build deps. Closes #12714+ * Fixed issues introduced by the node url module changes. Closes 126.15+ * Fixed two assertions failing due to Collection#keys() returning strings16+ * Fixed faulty Collection#toArray() spec due to keys() returning strings17+ * Fixed `make test` now builds libxmljs.node before testing18+19 0.0.1 / 2010-01-0320 ==================21 22 * Initial releasemodified:Makefile+3 added-2 removed
1 2 NODE = node3 4-init: lib/support/js-oo/Readme.md5- @git submodule update --init4+init:5+ @git submodule init && git submodule update6 7 test: init spec/support/libxmljs/libxmljs.node8 @$(NODE) spec/node.js all9 7 unmodified lines17 @$(NODE) examples/app.js18 19 benchmark:20 @$(NODE) benchmarks/collection.js21+ @$(NODE) benchmarks/views.js22 23 spec/support/libxmljs/libxmljs.node:24 @scons -C spec/support/libxmljs libxmljs.node25 1 unmodified linemodified:Readme.md+5 added-5 removed
56 unmodified lines57 use(MethodOverride)58 use(ContentLength)59 use(Redirect)60 set('root', dirname(__filename))61- enable('cache views')61+ enable('cache view contents')62 })63 64 get('/hello', function(){65 this.contentType('html')12 unmodified lines78 79 ## Running Tests80 81 Express uses the [JSpec](http://jspec.info) BDD JavaScript testing82-framework to write and run elegant spec suites. JSpec is froozen 83-to spec/lib and does not require seperate installation.82+framework to write and run elegant spec suites. JSpec is frozen 83+to spec/lib and does not require separate installation.84 85 To run all specifications run the following command. This will ensure86 git submodules are initialized and updated, as well as building test87 related dependencies such as libxmljs.88 89 $ make test90 91-To run independant specs (which do not require building of external apis etc) use:91+To run independent specs (which do not require building of external apis etc) use:92 93 $ make test-independant94 95-To run dependant specs (which require building of external apis etc) use:95+To run dependent specs (which require building of external apis etc) use:96 97 $ make test-dependant98 99 Run individual suites:45 unmodified lines