Skip to content

Commit

Permalink
Merge f886946 into 36895a0
Browse files Browse the repository at this point in the history
  • Loading branch information
craftzdog committed Aug 19, 2015
2 parents 36895a0 + f886946 commit 9ccf4b6
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ app.use(require('koa-static')(root, opts));
- `hidden` Allow transfer of hidden files. defaults to false
- `index` Default file name, defaults to 'index.html'
- `defer` If true, serves after `yield next`, allowing any downstream middleware to respond first.
- `callback(ctx, path)` The regular function or the generator function, invoked when serving a static file.

## Example

Expand Down
20 changes: 18 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ function serve(root, opts) {
if (!opts.defer) {
return function *serve(next){
if (this.method == 'HEAD' || this.method == 'GET') {
if (yield send(this, this.path, opts)) return;
var path = yield send(this, this.path, opts)
if (path) {
yield invokeCallback(this, path);
return;
}
}
yield* next;
};
Expand All @@ -49,6 +53,18 @@ function serve(root, opts) {
// response is already handled
if (this.body != null || this.status != 404) return;

yield send(this, this.path, opts);
var path = yield send(this, this.path, opts);
yield invokeCallback(this, path);
};

function *invokeCallback(ctx, path) {
if (path && typeof opts.callback==='function') {
if (opts.callback.constructor.name === 'GeneratorFunction') {
yield opts.callback(ctx, path);
}
else {
opts.callback(ctx, path);
}
}
}
}
67 changes: 67 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,38 @@ describe('serve(root)', function(){
.expect(404, done);
})
})

describe('when callback is specified', function(){
it('should perform generator function callback', function(done){
var app = koa();

app.use(serve('test/fixtures', {
callback: function*(ctx, path) {
ctx.body = path;
}
}));

request(app.listen())
.get('/hello.txt')
.expect(200)
.expect(__dirname + '/fixtures/hello.txt', done);
})
it('should perform regular function callback', function(done){
var app = koa();

app.use(serve('test/fixtures', {
callback: function(ctx, path) {
ctx.body = path;
}
}));

request(app.listen())
.get('/hello.txt')
.expect(200)
.expect(__dirname + '/fixtures/hello.txt', done);
})
})

})

describe('when defer: true', function(){
Expand Down Expand Up @@ -262,5 +294,40 @@ describe('serve(root)', function(){
.expect(404, done);
})
})

describe('when callback is specified', function(){
it('should perform callback', function(done){
var app = koa();

app.use(serve('test/fixtures', {
defer: true,
callback: function*(ctx, path) {
ctx.body = path;
}
}));

request(app.listen())
.get('/hello.txt')
.expect(200)
.expect(__dirname + '/fixtures/hello.txt', done);
})
it('should perform regular function callback', function(done){
var app = koa();

app.use(serve('test/fixtures', {
defer: true,
callback: function(ctx, path) {
ctx.body = path;
}
}));

request(app.listen())
.get('/hello.txt')
.expect(200)
.expect(__dirname + '/fixtures/hello.txt', done);
})
})

})

})

0 comments on commit 9ccf4b6

Please sign in to comment.