Skip to content

Commit

Permalink
feat: support options.preload
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Jan 6, 2017
1 parent a35a163 commit 79934bb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -20,7 +20,7 @@ Differences between this library and other libraries such as [static](https://gi

- There is no directory or `index.html` support.
- You may optionally store the data in memory - it streams by default.
- Caches the assets on initialization - you need to restart the process to update the assets.
- Caches the assets on initialization - you need to restart the process to update the assets.(can turn off with options.preload = false)
- Uses MD5 hash sum as an ETag.
- Uses .gz files if present on disk, like nginx gzip_static module

Expand Down Expand Up @@ -55,6 +55,7 @@ app.use(staticCache(path.join(__dirname, 'public'), {
- `files` (obj) - optional files object. See below.
- `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 togather with `options.dynamic`.

### Aliases

Expand Down
18 changes: 10 additions & 8 deletions index.js
Expand Up @@ -26,9 +26,11 @@ module.exports = function staticCache(dir, options, files) {
if (Array.isArray(options.filter)) fileFilter = function (file) { return ~options.filter.indexOf(file) }
if (typeof options.filter === 'function') fileFilter = options.filter

readDir(dir).filter(fileFilter).forEach(function (name) {
loadFile(name, dir, options, files)
})
if (options.preload !== false) {
readDir(dir).filter(fileFilter).forEach(function (name) {
loadFile(name, dir, options, files)
})
}

if (options.alias) {
Object.keys(options.alias).forEach(function (key) {
Expand All @@ -44,7 +46,7 @@ module.exports = function staticCache(dir, options, files) {

return function* staticCache(next) {
// only accept HEAD and GET
if (this.method !== 'HEAD' && this.method !== 'GET') return yield next;
if (this.method !== 'HEAD' && this.method !== 'GET') return yield next

// decode for `/%E4%B8%AD%E6%96%87`
// normalize for `//index`
Expand Down Expand Up @@ -102,7 +104,7 @@ module.exports = function staticCache(dir, options, files) {
if (this.method === 'HEAD')
return

var acceptGzip = this.acceptsEncodings('gzip') === 'gzip';
var acceptGzip = this.acceptsEncodings('gzip') === 'gzip'

if (file.zipBuffer) {
if (acceptGzip) {
Expand All @@ -122,7 +124,7 @@ module.exports = function staticCache(dir, options, files) {
if (file.buffer) {
if (shouldGzip) {

var gzFile = files[filename + '.gz'];
var gzFile = files[filename + '.gz']
if (options.usePrecompiledGzip && gzFile && gzFile.buffer) { // if .gz file already read from disk
file.zipBuffer = gzFile.buffer
} else {
Expand Down Expand Up @@ -159,9 +161,9 @@ module.exports = function staticCache(dir, options, files) {

function safeDecodeURIComponent(text) {
try {
return decodeURIComponent(text);
return decodeURIComponent(text)
} catch (e) {
return text;
return text
}
}

Expand Down
30 changes: 25 additions & 5 deletions test/index.js
Expand Up @@ -2,6 +2,7 @@ var fs = require('fs')
var crypto = require('crypto')
var zlib = require('zlib')
var request = require('supertest')
var should = require('should')
var koa = require('koa')
var http = require('http')
var path = require('path')
Expand Down Expand Up @@ -343,7 +344,7 @@ describe('Static Cache', function () {
var app = koa()
app.use(staticCache({dynamic: false}))
var server = app.listen()
fs.writeFileSync('a.js', 'hello world');
fs.writeFileSync('a.js', 'hello world')

request(server)
.get('/a.js')
Expand All @@ -357,7 +358,7 @@ describe('Static Cache', function () {
var app = koa()
app.use(staticCache({dynamic: true}))
var server = app.listen()
fs.writeFileSync('a.js', 'hello world');
fs.writeFileSync('a.js', 'hello world')

request(server)
.get('/a.js')
Expand All @@ -371,7 +372,7 @@ describe('Static Cache', function () {
var app = koa()
app.use(staticCache({dynamic: true, prefix: '/static'}))
var server = app.listen()
fs.writeFileSync('a.js', 'hello world');
fs.writeFileSync('a.js', 'hello world')

request(server)
.get('/static/a.js')
Expand All @@ -385,7 +386,7 @@ describe('Static Cache', function () {
var app = koa()
app.use(staticCache({dynamic: true, prefix: '/static'}))
var server = app.listen()
fs.writeFileSync('a.js', 'hello world');
fs.writeFileSync('a.js', 'hello world')

request(server)
.get('/a.js')
Expand All @@ -399,7 +400,7 @@ describe('Static Cache', function () {
var app = koa()
app.use(staticCache({dynamic: true}))
var server = app.listen()
fs.writeFileSync('.a.js', 'hello world');
fs.writeFileSync('.a.js', 'hello world')

request(server)
.get('/.a.js')
Expand Down Expand Up @@ -459,4 +460,23 @@ describe('Static Cache', function () {
.get('/Makefile')
.expect(404, done)
})

it('should options.dynamic and options.preload works fine', function (done) {
var app = koa()
var files = {}
app.use(staticCache({
dir: path.join(__dirname, '..'),
preload: false,
dynamic: true,
files: files
}))
files.should.eql({})
request(app.listen())
.get('/Makefile')
.expect(200, function (err, res) {
should.not.exist(err)
files.should.have.keys('/Makefile')
done()
})
})
})

0 comments on commit 79934bb

Please sign in to comment.