Finished converting async cache api. Closes #240
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent b85cda3 · Browse files · 9 files·+53 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
9 files+53 added−109 removed
- tests:+12 added−4 removed
- other:+41 added−105 removed
Provenance
- Commit
cabb43b- Tree
621254e- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
b85cda3
9 files changed+53 added−109 removed
modified:lib/express/plugins/cache.js+3 added-2 removed
78 unmodified lines79 this.data = {}80 },81 82 /**83- * Set the given _key_ to _val_, passing _val_.83+ * Set the given _key_ to _val_.84 *85 * @param {string} key86 * @param {string} val87 * @param {function} fn2 unmodified lines90 */91 92 set: function(key, val, fn) {93 exports.Store.prototype.set.apply(this, arguments)94- return this.data[key] = new Cache(key, val), fn(val)94+ this.data[key] = new Cache(key, val)95+ if (fn instanceof Function) fn(val)96 },97 98 /**99 * Get data found matching the given _key_.127 unmodified linesmodified:lib/express/plugins/static.js+24 added-15 removed
37 unmodified lines383940414243444546474849505142434445464748495053545556515253545556575958596061626364656667686970717245 unmodified lines37 unmodified lines * @api public */ sendTo: function(request) { var cache, file = this.path if (set('cache static files') && (cache = request.cache.get('static:' + file))) return request.contentType(cache.type), request.halt(200, cache.content, 'binary') path.exists(file, function(exists){ if (!exists) return request.halt() fs.stat(file, function(err, stats){ if (err) throw err if (!stats.isFile()) return request.halt() fs.readFile(file, 'binary', function(err, content){ var file = this.path function sendFromDisc() { path.exists(file, function(exists){ if (!exists) return request.halt() fs.stat(file, function(err, stats){ if (err) throw err if (!stats.isFile()) return request.halt() fs.readFile(file, 'binary', function(err, content){ if (err) throw err request.contentType(file) if (set('cache static files')) request.cache.set('static:' + file, { type: file, content: content }) request.halt(200, content, 'binary') request.contentType(file) if (set('cache static files')) request.cache.set('static:' + file, { type: file, content: content }) request.halt(200, content, 'binary') }) }) }) }) } if (set('cache static files')) request.cache.get('static:' + file, function(cache){ if (cache) request.contentType(cache.type), request.halt(200, cache.content, 'binary') else sendFromDisc() }) else sendFromDisc() }})
// --- Static45 unmodified linesmodified:lib/express/plugins/view.js+13 added-6 removed
59 unmodified lines60 * @api public61 */62 63 render: function(view, options) {64- var cache,65- self = this,64+ var self = this,65 options = options || {},66 path = set('views') + '/' + view,67 type = path.split('.').slice(-2)[0],68 ext = extname(path),11 unmodified lines80 }))81 else82 self.halt(200, content)83 }85- if (set('cache view contents') && (cache = self.cache.get('view:' + path)))86- render(cache)87- else84+ function renderFromDisc() {85 fs.readFile(path, function(err, content){86 if (err) throw err87 set('cache view contents')91- ? render(self.cache.set('view:' + path, content))88+ ? self.cache.set('view:' + path, content, function(cache){89+ render(cache)90+ })91 : render(content)92 })93+ }94+ if (set('cache view contents'))95+ self.cache.get('view:' + path, function(cache){96+ if (cache) render(cache)97+ else renderFromDisc()98+ })99+ else100+ renderFromDisc()101 }102 })103 }104 }2 unmodified linesmodified:lib/express/request.js+1 added-18 removed
3 unmodified lines4 /**5 * Module dependencies.6 */7 8-var StaticFile = require('express/static').File,9- statusBodies = require('http').STATUS_CODES,8+var statusBodies = require('http').STATUS_CODES,9 queryString = require('querystring'),10 mime = require('express/mime'),11 utils = require('express/utils'),12 url = require('url')266 unmodified lines279 this.header('content-disposition', path280 ? 'attachment; filename="' + path + '"'281 : 'attachment')282 return this284- },285- 286- /**287- * Transfer static file at the given _path_.288- *289- * @param {string} path290- * @return {Request}291- * @api public292- */293- 294- // DEPRECATED: remove in 0.9.0295- 296- sendfile: function(path) {297- Ext.warn('Request#sendfile() is deprecated, it is now part of the Static plugin, use(Static) to re-gain this method')298- (new StaticFile(path)).send(this)299- return this283 }284 })deleted:lib/express/static.js-64 removed
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
// Express - Static - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
// DEPRECATED: remove in 0.9.0
/** * Module dependencies. */
var path = require('path'), fs = require('fs') // --- File
exports.File = new Class({ /** * Initialize with file _path_. * * @param {string} path * @api public */ constructor: function(path) { Ext.warn('require("express/static").File is deprecated, use require("express/plugins/static").File') this.path = path if (path.indexOf('..') != -1) Error.raise('InvalidPathError', "`" + path + "' is not a valid path") }, /** * Transfer static file to the given _request_. * * - Ensures the file exists * - Ensures the file is a regular file (not FIFO, Socket, etc) * - Automatically assigns content type * - Halts with 404 when failing * * @param {Request} request * @settings 'cache static files' * @api public */ send: function(request) { var cache, file = this.path if (set('cache static files') && (cache = request.cache.get(file))) return request.contentType(cache.type), request.halt(200, cache.content, 'binary') path.exists(file, function(exists){ if (!exists) return request.halt() fs.stat(file, function(err, stats){ if (err) throw err if (!stats.isFile()) return request.halt() fs.readFile(file, 'binary', function(err, content){ if (err) throw err request.contentType(file) if (set('cache static files')) request.cache.set(file, { type: file, content: content }) request.halt(200, content, 'binary') }) }) }) }})No newline at end of filemodified:spec/lib/jspec.js+2 added
1519 unmodified lines1520 * @api public1521 */1522 1523 error : function(message, e) {1524+ if ('stack' in e)1525+ require('sys').puts(e.stack + '\n')1526 throw (message ? message : '') + e.toString() + 1527 (e.line ? ' near line ' + e.line : '')1528 },1529 246 unmodified linesmodified:spec/spec.plugins.cache.js+2 added-2 removed
137 unmodified lines138 end139 140 describe '#reap()'141 it 'should destroy caches older than the given age in milliseconds'142- store.set('user:one', '1', function(){})142+ store.set('user:one', '1')143 store.data['user:one'].created = Number((5).minutes.ago)144- store.set('user:two', '2', function(){})144+ store.set('user:two', '2')145 store.data['user:two'].created = Number((2).seconds.ago)146 store.reap((1).minute)147 store.get('user:one', function(val){148 val.should.be_null8 unmodified linesmodified:spec/spec.plugins.static.js+4 added-1 removed
49 unmodified lines50 get('/public/*', function(file){51 this.sendfile('spec/fixtures/' + file)52 })53 get('/cached', function(){54- return this.cache.get('static:spec/fixtures/user.json') ? 'yes' : 'no'54+ var self = this55+ this.cache.get('static:spec/fixtures/user.json', function(val){56+ self.halt(200, val ? 'yes' : 'no')57+ })58 })59 get('/cached').body.should.eql 'no'60 get('/public/user.json').body.should.include '"name":'61 get('/cached').body.should.eql 'yes'17 unmodified linesmodified:spec/spec.plugins.view.js+4 added-1 removed
78 unmodified lines79 get('/', function(){80 this.render('hello.haml.html')81 })82 get('/cached', function(){83- return this.cache.get('view:spec/fixtures/hello.haml.html') ? 'yes' : 'no'83+ var self = this84+ this.cache.get('view:spec/fixtures/hello.haml.html', function(val){85+ self.halt(200, val ? 'yes' : 'no')86+ })87 })88 get('/cached').body.should.eql 'no'89 get('/')90 get('/cached').body.should.eql 'yes'62 unmodified lines