-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #446 from ansble/feature/refactor-some-complex-fun…
…ctions Refactors two of the complex functions to be a little simpler
- Loading branch information
Showing
7 changed files
with
153 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs') | ||
, events = require('harken') | ||
, zlib = require('zlib') | ||
, brotli = require('iltorb') | ||
, mime = require('mime') | ||
|
||
, getCompression = require('../utils').getCompression | ||
|
||
, not = require('../utils').not | ||
, unmodifiedStatus = 304 | ||
, succesStatus = 200 | ||
|
||
, compressionExtension = (type) => { | ||
if (type === 'br') { | ||
return 'brot'; | ||
} else if (type === 'deflate') { | ||
return 'def'; | ||
} else { | ||
return 'tgz'; | ||
} | ||
} | ||
|
||
, getCompressor = (type) => { | ||
if (type === 'br') { | ||
return brotli.compressStream(); | ||
} else if (type === 'deflate') { | ||
return zlib.createDeflate(); | ||
} else { | ||
return zlib.createGzip(); | ||
} | ||
} | ||
|
||
, streamFile = (compression, file, connection) => { | ||
const extension = compressionExtension(compression) | ||
, compressor = getCompressor(compression); | ||
|
||
fs.stat(`${file}.${extension}`, (err, exists) => { | ||
if (!err && exists.isFile()) { | ||
fs.createReadStream(`${file}.${extension}`).pipe(connection.res); | ||
} else { | ||
// no compressed file yet... | ||
fs.createReadStream(file).pipe(compressor) | ||
.pipe(connection.res); | ||
|
||
fs.createReadStream(file).pipe(compressor) | ||
.pipe(fs.createWriteStream(`${file}.${extension}`)); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = (file, connection, config) => { | ||
const req = connection.req | ||
, res = connection.res | ||
, pathname = connection.path.pathname | ||
, compression = getCompression(req.headers['accept-encoding'], config) | ||
, expires = new Date().getTime() | ||
, maxAge = config.maxAge; | ||
|
||
fs.stat(file, (err, exists) => { | ||
if (!err && exists.isFile()) { | ||
|
||
events.required([ `etag:check:${file}`, `etag:get:${file}` ], (valid) => { | ||
if (valid[0]) { // does the etag match? YES | ||
res.statusCode = unmodifiedStatus; | ||
return res.end(); | ||
} | ||
// No match... | ||
res.setHeader('ETag', valid[1]); // the etag is item 2 in the array | ||
|
||
if (req.method.toLowerCase() === 'head') { | ||
res.writeHead(succesStatus, { | ||
'Content-Type': mime.getType(pathname) | ||
, 'Cache-Control': `maxage=${maxAge}` | ||
, Expires: new Date(expires + maxAge).toUTCString() | ||
, 'Content-Encoding': compression | ||
}); | ||
|
||
res.end(); | ||
} else if (not(compression === 'none')) { | ||
// we have compression! | ||
res.writeHead(succesStatus, { | ||
'Content-Type': mime.getType(pathname) | ||
, 'Cache-Control': `maxage=${maxAge}` | ||
, Expires: new Date(expires + maxAge).toUTCString() | ||
, 'Content-Encoding': compression | ||
}); | ||
|
||
streamFile(compression, file, connection); | ||
events.emit('static:served', pathname); | ||
} else { | ||
// no compression carry on... | ||
// return with the correct heders for the file type | ||
res.writeHead(succesStatus, { | ||
'Content-Type': mime.getType(pathname) | ||
, 'Cache-Control': `maxage=${maxAge}` | ||
, Expires: new Date(expires + maxAge).toUTCString() | ||
}); | ||
fs.createReadStream(file).pipe(res); | ||
events.emit('static:served', pathname); | ||
} | ||
}); | ||
|
||
events.emit('etag:check', { file: file, etag: req.headers['if-none-match'] }); | ||
|
||
} else { | ||
events.emit('static:missing', pathname); | ||
events.emit('error:404', connection); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
x��1 | ||
�0�=��[J�FJ��Ф�xw}����cj�T)gġ����1��D<���������cMC��R��JG. | ||
x���1 | ||
� �=������i�(�&�J�����\ԖT�R�����.g#��g5�&"�&`5�5��!z8r��W���* |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters