Static is now a plugin
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 31e608e · Browse files · 5 files·+161 added−69 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+161 added−69 removed
- tests:+70 added−69 removed
- other:+91 added−0 removed
Provenance
- Commit
a797342- Tree
666c894- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
31e608e
5 files changed+161 added−69 removed
added:lib/express/plugins/static.js+90 added
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+ 11+// --- File12+13+exports.File = new Class({14+ 15+ /**16+ * Initialize with file _path_.17+ *18+ * @param {string} path19+ * @api public20+ */21+ 22+ constructor: function(path) {23+ this.path = path24+ 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_.30+ *31+ * - Ensures the file exists32+ * - Ensures the file is a regular file (not FIFO, Socket, etc)33+ * - Automatically assigns content type34+ * - Halts with 404 when failing35+ *36+ * @param {Request} request37+ * @settings 'cache static files'38+ * @api public39+ */40+ 41+ sendTo: function(request) {42+ var cache, file = this.path43+ if (set('cache static files') && (cache = request.cache.get(file)))44+ return request.contentType(cache.type),45+ request.halt(200, cache.content, 'binary')46+ path.exists(file, function(exists){47+ if (!exists) return request.halt()48+ fs.stat(file, function(err, stats){49+ if (err) throw err50+ if (!stats.isFile()) return request.halt()51+ fs.readFile(file, 'binary', function(err, content){52+ if (err) throw err53+ request.contentType(file)54+ if (set('cache static files'))55+ request.cache.set(file, { type: file, content: content })56+ request.halt(200, content, 'binary')57+ })58+ })59+ })60+ }61+})62+63+// --- Static64+65+exports.Static = Plugin.extend({66+ extend: {67+ 68+ /**69+ * Initialize extensions.70+ */71+ 72+ init: function() {73+ Request.include({74+ 75+ /**76+ * Transfer static file at the given _path_.77+ *78+ * @param {string} path79+ * @return {Request}80+ * @api public81+ */82+83+ sendfile: function(path) {84+ (new exports.File(path)).sendTo(this)85+ return this86+ }87+ })88+ }89+ }90+})modified:lib/express/plugins.js+1 added
1234567893 unmodified lines
// Express - Plugins - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
global.merge(require('express/plugins/hooks'))global.merge(require('express/plugins/static'))global.merge(require('express/plugins/flash'))global.merge(require('express/plugins/cache'))global.merge(require('express/plugins/cookie'))global.merge(require('express/plugins/session'))3 unmodified linesmodified:spec/node.js+1 added-1 removed
19 unmodified lines2021222324242526273 unmodified lines3132333435363738399 unmodified lines19 unmodified lines 'routing', 'utils', 'request', 'mime', 'static', 'plugins', 'plugins.cache', 'plugins.view', 'plugins.content-length',3 unmodified lines 'plugins.hooks', 'plugins.cookie', 'plugins.session', 'plugins.flash', 'plugins.static', ]}
switch (process.ARGV[2]) {9 unmodified linesadded:spec/spec.plugins.static.js+69 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
describe 'Express' describe 'StaticFile' before StaticFile = require('express/plugins/static').File use(require('express/plugins/static').Static) end describe '#constructor' it 'should accept and assign #path' (new StaticFile('/foo/bar')).path.should.eql '/foo/bar' end it 'should throw an InvalidPathError when .. is found' // TODO: use throw_error when fixed... try { new StaticFile('/../foobar') } catch (e) { e.name.should.eql 'InvalidPathError' e.message.should.eql "`/../foobar' is not a valid path" } end end describe '#sendfile()' describe 'when the file exists' it 'should transfer the file' get('/public/*', function(file){ this.sendfile('spec/fixtures/' + file) }) get('/public/user.json').body.should.include '"name":' end it 'should automatically set the content type based on extension' get('/public/*', function(file){ this.sendfile('spec/fixtures/' + file) }) get('/public/user.json').headers['content-type'].should.eql 'application/json' end end describe 'when "cache static files" is enabled' it 'should transfer the file' enable('cache static files') get('/public/*', function(file){ this.sendfile('spec/fixtures/' + file) }) get('/cached', function(){ return this.cache.get('spec/fixtures/user.json') ? 'yes' : 'no' }) get('/cached').body.should.eql 'no' get('/public/user.json').body.should.include '"name":' get('/cached').body.should.eql 'yes' get('/cached').body.should.eql 'yes' end it 'should automatically set the content type based on extension' enable('cache static files') get('/public/*', function(file){ this.sendfile('spec/fixtures/' + file) }) get('/public/user.json').headers['content-type'].should.eql 'application/json' get('/public/user.json').headers['content-type'].should.eql 'application/json' get('/public/user.json').headers['content-type'].should.eql 'application/json' end end end endendNo newline at end of filedeleted:spec/spec.static.js-68 removed
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
describe 'Express' describe 'StaticFile' before StaticFile = require('express/static').File end describe '#constructor' it 'should accept and assign #path' (new StaticFile('/foo/bar')).path.should.eql '/foo/bar' end it 'should throw an InvalidPathError when .. is found' // TODO: use throw_error when fixed... try { new StaticFile('/../foobar') } catch (e) { e.name.should.eql 'InvalidPathError' e.message.should.eql "`/../foobar' is not a valid path" } end end describe '#sendfile()' describe 'when the file exists' it 'should transfer the file' get('/public/*', function(file){ this.sendfile('spec/fixtures/' + file) }) get('/public/user.json').body.should.include '"name":' end it 'should automatically set the content type based on extension' get('/public/*', function(file){ this.sendfile('spec/fixtures/' + file) }) get('/public/user.json').headers['content-type'].should.eql 'application/json' end end describe 'when "cache static files" is enabled' it 'should transfer the file' enable('cache static files') get('/public/*', function(file){ this.sendfile('spec/fixtures/' + file) }) get('/cached', function(){ return this.cache.get('spec/fixtures/user.json') ? 'yes' : 'no' }) get('/cached').body.should.eql 'no' get('/public/user.json').body.should.include '"name":' get('/cached').body.should.eql 'yes' get('/cached').body.should.eql 'yes' end it 'should automatically set the content type based on extension' enable('cache static files') get('/public/*', function(file){ this.sendfile('spec/fixtures/' + file) }) get('/public/user.json').headers['content-type'].should.eql 'application/json' get('/public/user.json').headers['content-type'].should.eql 'application/json' get('/public/user.json').headers['content-type'].should.eql 'application/json' end end end endendNo newline at end of file