Skip to content

Commit

Permalink
Merge beb025f into c756b36
Browse files Browse the repository at this point in the history
  • Loading branch information
Necromos committed Jan 2, 2017
2 parents c756b36 + beb025f commit 38753ec
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ $ npm install koa-send

## Options

- `maxage` Browser cache max-age in milliseconds. defaults to 0
- `hidden` Allow transfer of hidden files. defaults to false
- `maxage` Browser cache max-age in milliseconds. (defaults to `0`)
- `hidden` Allow transfer of hidden files. (defaults to `false`)
- [`root`](#root-path) Root directory to restrict file access
- `gzip` Try to serve the gzipped version of a file automatically when `gzip` is supported by a client and if the requested file with `.gz` extension exists. defaults to true.
- `format` If not `false` (defaults to `true`), format the path to serve static file servers and not require a trailing slash for directories, so that you can do both `/directory` and `/directory/`
- [`setHeaders`](#setheaders) Function to set custom headers on response.
- `extensions` Try to match extensions from passed array to search for file when no extension is sufficed in URL. First found is served. (defaults to `false`)

### Root path

Expand Down
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function send(ctx, path, opts) {
var maxage = opts.maxage || opts.maxAge || 0;
var hidden = opts.hidden || false;
var format = opts.format === false ? false : true;
var extensions = Array.isArray(opts.extensions) ? opts.extensions : false;
var gzip = opts.gzip === false ? false : true;
var setHeaders = opts.setHeaders;

Expand Down Expand Up @@ -77,6 +78,21 @@ function send(ctx, path, opts) {
ctx.res.removeHeader('Content-Length');
}

if (extensions && !/\..*$/.exec(path)) {
var list = [].concat(extensions || []);
for (var i = 0; i < list.length; i++) {
var extension = list[i];
if (!(typeof extension === 'string' || extension instanceof String)) {
throw new TypeError('option extensions must be array of strings or false');
}
if (!/^\./.exec(extension)) extension = '.' + extension;
if (yield fs.exists(path + extension)) {
path = path + extension;
break;
}
}
}

// stat
try {
var stats = yield fs.stat(path);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/user.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "name": "tobi" }
111 changes: 107 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe('send(ctx, file)', function(){
var app = koa();

app.use(function *(){
var sent = yield send(this, '/test', {format: false});
var sent = yield send(this, '/test', { format: false });
assert.equal(sent, undefined);
});

Expand Down Expand Up @@ -364,7 +364,7 @@ describe('send(ctx, file)', function(){

request(app.listen())
.get('/')
.expect('Cache-Control','max-age=5')
.expect('Cache-Control', 'max-age=5')
.expect(200, done);
})

Expand All @@ -379,7 +379,7 @@ describe('send(ctx, file)', function(){

request(app.listen())
.get('/')
.expect('Cache-Control','max-age=1')
.expect('Cache-Control', 'max-age=1')
.expect(200, done);
})
})
Expand Down Expand Up @@ -418,7 +418,110 @@ describe('send(ctx, file)', function(){
var app = koa();

app.use(function *(){
yield send(this, 'test/fixtures/.hidden', {hidden: true});
yield send(this, 'test/fixtures/.hidden', { hidden: true });
});

request(app.listen())
.get('/')
.expect(200, done);
})
})
});

describe('.extensions option', function(){
describe('when trying to get a file without extension with no .extensions sufficed', function(){
it('should 404', function(done){
var app = koa();

app.use(function *(){
yield send(this, 'test/fixtures/hello');
});

request(app.listen())
.get('/')
.expect(404, done);
})
})

describe('when trying to get a file without extension with no matching .extensions', function(){
it('should 404', function(done){
var app = koa();

app.use(function *(){
yield send(this, 'test/fixtures/hello', { extensions: ['json', 'htm', 'html'] });
});

request(app.listen())
.get('/')
.expect(404, done);
})
})

describe('when trying to get a file without extension with non array .extensions', function(){
it('should 404', function(done){
var app = koa();

app.use(function *(){
yield send(this, 'test/fixtures/hello', { extensions: {} });
});

request(app.listen())
.get('/')
.expect(404, done);
})
})

describe('when trying to get a file without extension with non string array .extensions', function(){
it('throws if extensions is not array of strings', function(done){
var app = koa();

app.use(function *(){
yield send(this, 'test/fixtures/hello', { extensions: [2,{},[]] });
});

request(app.listen())
.get('/')
.expect(500)
.end(done);
})
})

describe('when trying to get a file without extension with matching .extensions sufficed first matched should be sent', function(){
it('should 200 and application/json', function(done){
var app = koa();

app.use(function *(){
yield send(this, 'test/fixtures/user', { extensions: ['html', 'json', 'txt'] });
});

request(app.listen())
.get('/')
.expect(200)
.expect('Content-Type', /application\/json/)
.end(done);
})
})

describe('when trying to get a file without extension with matching .extensions sufficed', function(){
it('should 200', function(done){
var app = koa();

app.use(function *(){
yield send(this, 'test/fixtures/hello', { extensions: ['txt'] });
});

request(app.listen())
.get('/')
.expect(200, done);
})
})

describe('when trying to get a file without extension with matching doted .extensions sufficed', function(){
it('should 200', function(done){
var app = koa();

app.use(function *(){
yield send(this, 'test/fixtures/hello', { extensions: ['.txt'] });
});

request(app.listen())
Expand Down

0 comments on commit 38753ec

Please sign in to comment.