From 747b249a0bd2e73acdd62c2b1c1073a2a577810e Mon Sep 17 00:00:00 2001 From: TJ Krusinski Date: Sat, 8 Mar 2014 12:11:05 -0500 Subject: [PATCH 1/3] adds content-length header if there is one for serving files --- index.js | 3 +++ test/test.file-download.js | 10 ++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 3fc9db8..0d299e3 100644 --- a/index.js +++ b/index.js @@ -202,6 +202,9 @@ var Framer = module.exports = function Framer(opts) { }); res.setHeader('Content-Type', s3res.headers['content-type']); + if (s3res.headers['content-length']) { + res.setHeader('Content-Length', s3res.headers['content-length']); + } return s3res.pipe(res); }) diff --git a/test/test.file-download.js b/test/test.file-download.js index 83411cb..a9c09fa 100644 --- a/test/test.file-download.js +++ b/test/test.file-download.js @@ -14,9 +14,9 @@ describe('test image downloads', function () { var s3Options = { secure: false, - key: 'bogus', - secret: 'bogus', - bucket: 'bogus' + key: process.env.S3KEY || 'bogus', + secret: process.env.S3SECRET || 'bogus', + bucket: process.env.S3BUCKET || 'bogus' }; var s3Client = { @@ -27,10 +27,12 @@ describe('test image downloads', function () { mock.on = function (evt, cb) { if (evt === 'response') { if (imagePath === expectedPath) { + var len = fs.statSync(path.join(__dirname, 'file.txt')).size; var obj = fs.createReadStream(path.join(__dirname, 'file.txt')); obj.statusCode = 200; obj.headers = {}; obj.headers['content-type'] = 'text/html'; + obj.headers['content-length'] = len; cb(obj); } else { @@ -53,7 +55,7 @@ describe('test image downloads', function () { var serveFile = framer.serveFile({ prefix: '/file', cacheMaxAge: maxAge }); var client = http.createServer(function (req, res) { serveFile(req, res); - }).listen(PORT); + }); before(function (done) { client.listen(PORT, done); From 088f2c778a525f6b8405561d9ebfccf826801acb Mon Sep 17 00:00:00 2001 From: TJ Krusinski Date: Sat, 8 Mar 2014 12:19:00 -0500 Subject: [PATCH 2/3] sets transfer-encoding chunked for image resizing --- index.js | 1 + test/test.image-download.js | 3 +++ 2 files changed, 4 insertions(+) diff --git a/index.js b/index.js index 0d299e3..d61e5c9 100644 --- a/index.js +++ b/index.js @@ -167,6 +167,7 @@ var Framer = module.exports = function Framer(opts) { } res.setHeader('Content-Type', s3res.headers['content-type']); + res.setHeader('transfer-encoding', 'chunked'); if (sizeOptions === 'raw') { return s3res.pipe(res); diff --git a/test/test.image-download.js b/test/test.image-download.js index f3ff37f..b626983 100644 --- a/test/test.image-download.js +++ b/test/test.image-download.js @@ -109,6 +109,7 @@ describe('test image downloads', function () { assert.equal(200, res.statusCode); assert(expectedFile.equals(body)); assert.equal('image/jpeg', res.headers['content-type']); + assert.equal('chunked', res.headers['transfer-encoding']); done(); }); }); @@ -127,6 +128,7 @@ describe('test image downloads', function () { assert.equal(200, res.statusCode); assert(expectedFile.equals(body)); assert.equal('image/jpeg', res.headers['content-type']); + assert.equal('chunked', res.headers['transfer-encoding']); done(); }); }); @@ -140,6 +142,7 @@ describe('test image downloads', function () { assert.equal(200, res.statusCode); assert.equal('public, max-age=' + maxAge, res.headers['cache-control']); assert.equal('image/jpeg', res.headers['content-type']); + assert.equal('chunked', res.headers['transfer-encoding']); done(); }); }); From 91f4cf2f0200be8b93360542264f2fc7fb8b723e Mon Sep 17 00:00:00 2001 From: TJ Krusinski Date: Sat, 8 Mar 2014 12:23:18 -0500 Subject: [PATCH 3/3] adds content-length headers to raw image download --- index.js | 5 ++++- test/test.image-download.js | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d61e5c9..d966421 100644 --- a/index.js +++ b/index.js @@ -170,6 +170,9 @@ var Framer = module.exports = function Framer(opts) { res.setHeader('transfer-encoding', 'chunked'); if (sizeOptions === 'raw') { + if (s3res.headers['content-length']) { + res.setHeader('Content-Length', s3res.headers['content-length']); + } return s3res.pipe(res); } @@ -204,7 +207,7 @@ var Framer = module.exports = function Framer(opts) { res.setHeader('Content-Type', s3res.headers['content-type']); if (s3res.headers['content-length']) { - res.setHeader('Content-Length', s3res.headers['content-length']); + res.setHeader('Content-Length', s3res.headers['content-length']); } return s3res.pipe(res); diff --git a/test/test.image-download.js b/test/test.image-download.js index b626983..b781ba8 100644 --- a/test/test.image-download.js +++ b/test/test.image-download.js @@ -29,10 +29,12 @@ describe('test image downloads', function () { mock.on = function (evt, cb) { if (evt === 'response') { if (imagePath === expectedPath) { + var len = fs.statSync(path.join(__dirname, 'file.txt')).size; var obj = fs.createReadStream(path.join(__dirname, 'image.jpg')); obj.statusCode = 200; obj.headers = {}; obj.headers['content-type'] = 'image/jpeg'; + obj.headers['content-length'] = len; cb(obj); } else { @@ -71,6 +73,7 @@ describe('test image downloads', function () { assert.equal(200, res.statusCode); assert.equal(expectedFile.toString(), body.toString()); assert.equal('image/jpeg', res.headers['content-type']); + assert.ok(res.headers['content-length']); done(); }); });