Skip to content
Myr

Merge branch 'stream'

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

authored by visionmedia · committed by visionmedia · parents 28df150, a2cedbd · Browse files · 4 files+124 added−169 removed
This commit

State

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

Diff stats

4 files+124 added−169 removed

  • tests:+64 added−95 removed
  • other:+60 added−74 removed

4 files+124 added−169 removed

modified:lib/express/plugins/content-length.js+4 added-3 removed
modified:lib/express/plugins/static.js+10 added-66 removed
modified:lib/express/request.js+46 added-5 removed
199 unmodified lines
200 }, true)201 },202 203 /**204 * Transfer the given _stream_ with optional _callback_.205 *206 * @param {Stream} stream207 * @return {Request}208 * @api public209 */210 211 stream: function(stream, callback) {212 var self = this,213 first = true214 stream215 .addListener('error', function(err){216 if (first)217 return 'errno' in err && err.errno === 2218 ? self.notFound()219 : self.error(err)220 self.response.end()221 if (callback !== undefined) callback(err)222 })223 .addListener('data', function(data){224 if (first) {225 self.status(200)226 first = false227 self.contentType(stream.path)228 self.response.writeHeader(self.response.status, self.response.headers)229 }230 self.response.write(data, 'binary')231 })232 .addListener('end', function(){233 self.response.end()234 self.trigger('response', function(err){235 if (err) {236 stream.destroy()237 if (callback !== undefined) callback(err)238 else self.error(err)239 }240 }, true)241 })242 },243 244 /**245 * Pass control to the next matching route, or246 * the given _path_.247 *248 * NOTE: _path_ may be the request pathname only,
59 unmodified lines
308 },309 310 /**311 * Set Content-Disposition header to 'attachment',271 * with optional file _path_.312 * with optional file _filename_.313 *273 * @param {string} path314 * @param {string} filename315 * @return {Request}316 * @api public317 */318 278 attachment: function(path) {279 this.header('content-disposition', path280 ? 'attachment; filename="' + path + '"'319 attachment: function(filename) {320 this.header('content-disposition', filename321 ? 'attachment; filename="' + filename + '"'322 : 'attachment')323 return this324 },325
65 unmodified lines
modified:spec/spec.plugins.static.js+64 added-95 removed
12describe 'Express'3 describe 'StaticFile'3 describe 'Static'4 before5 StaticFile = require('express/plugins/static').File5 use(require('express/plugins/static').Static, { path: 'spec/fixtures' })6 end7 9 describe '#constructor'10 it 'should accept and assign #path'11 (new StaticFile('/foo/bar')).path.should.eql '/foo/bar'12 end13 14 it 'should throw an InvalidPathError when .. is found'15 // TODO: use throw_error when fixed...16 try { new StaticFile('/../foobar') }17 catch (e) {18 e.name.should.eql 'InvalidPathError'19 e.message.should.eql "`/../foobar' is not a valid path"20 }21 end22 end8 // TODO: spec again when JSpec has async support ... mocking out FileReadStream9 // is to much of a hassle10 24 describe 'GET /public/*'25 it 'should transfer static files'26 get('/public/user.json').body.should.include '"name":'27 end28 end29 30 describe '#sendfile()'31 describe 'when the file exists'32 it 'should transfer the file'33 get('/public/*', function(file){34 this.sendfile('spec/fixtures/' + file)35 })36 get('/public/user.json').body.should.include '"name":'37 end38 39 it 'should automatically set the content type based on extension'40 get('/public/*', function(file){41 this.sendfile('spec/fixtures/' + file)42 })43 get('/public/user.json').headers['content-type'].should.eql 'application/json'44 end45 end46 47 describe 'when "cache static files" is enabled'48 it 'should transfer the file'49 enable('cache static files')50 get('/public/*', function(file){51 this.sendfile('spec/fixtures/' + file)52 })53 get('/cached', function(){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'62 get('/cached').body.should.eql 'yes'63 end64 65 it 'should automatically set the content type based on extension'66 enable('cache static files')67 get('/public/*', function(file){68 this.sendfile('spec/fixtures/' + file)69 })70 get('/public/user.json').headers['content-type'].should.eql 'application/json'71 get('/public/user.json').headers['content-type'].should.eql 'application/json'72 get('/public/user.json').headers['content-type'].should.eql 'application/json'73 end74 end75 end76 77 describe '#download()' 78 describe 'when the file exists'79 it 'should set attachment filename'80 get('/report', function(){81 this.download('report.pdf')82 return 'foo'83 })84 get('/report').headers['content-disposition'].should.eql 'attachment; filename="report.pdf"'85 end86 87 it 'should transfer the file'88 get('/public/*', function(file){89 this.download('spec/fixtures/' + file)90 })91 get('/public/user.json').body.should.include '"name":'92 end93 94 it 'should automatically set the content type based on extension'95 get('/public/*', function(file){96 this.download('spec/fixtures/' + file)97 })98 get('/public/user.json').headers['content-type'].should.eql 'application/json'99 end100 end101 end102 11 // describe 'GET /public/*'12 // it 'should transfer static files'13 // get('/public/user.json').body.should.include '"name":'14 // end15 // end16 // 17 // describe '#sendfile()'18 // describe 'when the file exists'19 // it 'should throw an InvalidPathError when .. is found'20 // get('/public/*', function(file){21 // this.sendfile(file)22 // })23 // try { get('/public/../foobar') }24 // catch (e) {25 // e.name.should.eql 'InvalidPathError'26 // e.message.should.eql "`spec/fixtures/../foobar' is not a valid path"27 // }28 // end29 // 30 // it 'should transfer the file'31 // get('/public/*', function(file){32 // this.sendfile('spec/fixtures/' + file)33 // })34 // get('/public/user.json').body.should.include '"name":'35 // end36 // 37 // it 'should automatically set the content type based on extension'38 // get('/public/*', function(file){39 // this.sendfile('spec/fixtures/' + file)40 // })41 // get('/public/user.json').headers['content-type'].should.eql 'application/json'42 // end43 // end44 // end45 // 46 // describe '#download()' 47 // describe 'when the file exists'48 // it 'should set attachment filename'49 // get('/report', function(){50 // this.download('report.pdf')51 // return 'foo'52 // })53 // get('/report').headers['content-disposition'].should.eql 'attachment; filename="report.pdf"'54 // end55 // 56 // it 'should transfer the file'57 // get('/public/*', function(file){58 // this.download('spec/fixtures/' + file)59 // })60 // get('/public/user.json').body.should.include '"name":'61 // end62 // 63 // it 'should automatically set the content type based on extension'64 // get('/public/*', function(file){65 // this.download('spec/fixtures/' + file)66 // })67 // get('/public/user.json').headers['content-type'].should.eql 'application/json'68 // end69 // end70 // end71 // 72 end73end