diff --git a/README.md b/README.md index ba9afb8..60fa1bf 100644 --- a/README.md +++ b/README.md @@ -48,10 +48,11 @@ app.use(staticCache(path.join(__dirname, 'public'), { - `options.maxAge` (int) - cache control max age for the files, `0` by default. - `options.cacheControl` (str) - optional cache control header. Overrides `options.maxAge`. - `options.buffer` (bool) - store the files in memory instead of streaming from the filesystem on each request. -- `options.gzip` (bool) - when request's accept-encoding include gzip, files will compressed by gzip. -- `options.usePrecompiledGzip` (bool) - try use gzip files, loaded from disk, like nginx gzip_static +- `options.gzip` (bool) - when the request's Accept-Encoding includes gzip, files will be compressed using gzip. +- `options.charset` (str | function) - optional charset appended to `Content-Type`. If a function is supplied, it receives `(file, type)`, where `file` is the relative file path, and can return a charset per file. +- `options.usePrecompiledGzip` (bool) - try to use gzip files, loaded from disk, like nginx gzip_static - `options.alias` (obj) - object map of aliases. See below. -- `options.prefix` (str) - the url prefix you wish to add, default to `''`. +- `options.prefix` (str) - the URL prefix you wish to add, defaults to `''`. - `options.dynamic` (bool) - dynamic load file which not cached on initialization. - `options.filter` (function | array) - filter files at init dir, for example - skip non build (source) files. If array set - allow only listed files - `options.preload` (bool) - caches the assets on initialization or not, default to `true`. always work together with `options.dynamic`. diff --git a/index.js b/index.js index 44cd7bb..c8848b8 100644 --- a/index.js +++ b/index.js @@ -186,7 +186,14 @@ function loadFile(name, dir, options, files) { obj.cacheControl = options.cacheControl obj.maxAge = (typeof obj.maxAge === 'number' ? obj.maxAge : options.maxAge) || 0 - obj.type = obj.mime = mime.lookup(pathname) || 'application/octet-stream' + var type = mime.lookup(pathname) || 'application/octet-stream' + var charset = typeof options.charset === 'function' + ? options.charset(name, type) + : options.charset + obj.mime = type + obj.type = charset + ? type + '; charset=' + charset + : type obj.mtime = stats.mtime obj.length = stats.size obj.md5 = crypto.createHash('md5').update(buffer).digest('base64') diff --git a/test/index.js b/test/index.js index ed74ace..79b09a8 100644 --- a/test/index.js +++ b/test/index.js @@ -150,6 +150,76 @@ describe('Static Cache', function () { }) }) + it('should serve files with configured charset', function (done) { + var app = new Koa() + app.use(staticCache(path.join(__dirname, '..'), { + charset: 'iso-8859-1', + filter(file) { + return file === 'README.md' + } + })) + var server = app.listen() + + request(server) + .get('/README.md') + .expect('Content-Type', 'text/markdown; charset=iso-8859-1') + .expect(200, done) + }) + + it('should serve files with configured charset for unknown mime types', function (done) { + var app = new Koa() + app.use(staticCache(path.join(__dirname, '..'), { + charset: 'iso-8859-1', + filter(file) { + return file === 'Makefile' + } + })) + var server = app.listen() + + request(server) + .get('/Makefile') + .expect('Content-Type', 'application/octet-stream; charset=iso-8859-1') + .expect(200, done) + }) + + it('should serve files with function configured charset', function (done) { + var app = new Koa() + app.use(staticCache(path.join(__dirname, '..'), { + charset(file, type) { + return file === 'README.md' && type === 'text/markdown' + ? 'windows-1252' + : undefined + }, + filter(file) { + return file === 'README.md' + } + })) + var server = app.listen() + + request(server) + .get('/README.md') + .expect('Content-Type', 'text/markdown; charset=windows-1252') + .expect(200, done) + }) + + it('should not append charset when charset function returns undefined', function (done) { + var app = new Koa() + app.use(staticCache(path.join(__dirname, '..'), { + charset() { + return undefined + }, + filter(file) { + return file === 'Makefile' + } + })) + var server = app.listen() + + request(server) + .get('/Makefile') + .expect('Content-Type', 'application/octet-stream') + .expect(200, done) + }) + it('should serve recursive files', function (done) { request(server) .get('/test/index.js')