Skip to content

Commit

Permalink
Merge d0c262d into e5d8510
Browse files Browse the repository at this point in the history
  • Loading branch information
menems committed Oct 20, 2015
2 parents e5d8510 + d0c262d commit 5ff48d5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var basename = path.basename;
var extname = path.extname;
var resolve = path.resolve;
var fs = require('mz/fs');
var co = require('co');
var Promise = require('native-or-bluebird');

/**
* Expose `send()`.
Expand Down Expand Up @@ -44,8 +46,8 @@ function send(ctx, path, opts) {
var hidden = opts.hidden || false;
var gzip = opts.gzip || opts.gzip === undefined ? true : false;

return function *(){
var encoding = this.acceptsEncodings('gzip', 'deflate', 'identity');
var _send = function *(){
var encoding = ctx.acceptsEncodings('gzip', 'deflate', 'identity');

// normalize path
path = decode(path);
Expand All @@ -58,7 +60,7 @@ function send(ctx, path, opts) {
path = resolvePath(root, path);

// hidden file support, ignore
if (!hidden && isHidden(root, path)) return;
if (!hidden && isHidden(root, path)) return Promise.resolve();

// serve gzipped file when possible
if (encoding === 'gzip' && gzip && (yield fs.exists(path + '.gz'))) {
Expand All @@ -83,9 +85,10 @@ function send(ctx, path, opts) {
}
} catch (err) {
var notfound = ['ENOENT', 'ENAMETOOLONG', 'ENOTDIR'];
if (~notfound.indexOf(err.code)) return;
if (~notfound.indexOf(err.code)) return Promise.resolve();
err.status = 500;
throw err;
//throw err;
return Promise.reject(err);
}

// stream
Expand All @@ -95,8 +98,10 @@ function send(ctx, path, opts) {
ctx.type = type(path);
ctx.body = fs.createReadStream(path);

return path;
return Promise.resolve(path);
}

return co(_send);
}

/**
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"index.js"
],
"devDependencies": {
"babel": "^5.8.23",
"istanbul-harmony": "0",
"koa": "1",
"mocha": "^2.3.3",
Expand All @@ -21,13 +22,15 @@
},
"license": "MIT",
"dependencies": {
"co": "^4.6.0",
"debug": "*",
"mz": "^2.0.0",
"native-or-bluebird": "^1.2.0",
"resolve-path": "^1.2.1"
},
"scripts": {
"test": "mocha --require should --reporter spec",
"test-cov": "node ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --require should",
"test-travis": "node ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --require should"
"test": "mocha --require should --reporter spec test/index.js test/experimental/index.js",
"test-cov": "node ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --require should test/index.js test/experimental/index.js",
"test-travis": "node ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --require should test/index.js test/experimental/index.js"
}
}
23 changes: 23 additions & 0 deletions test/experimental/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

var request = require('supertest');
var koa = require('koa');
var send = require('../../');

describe('asyncAwait support with koa experimental', function(){
it('should 200', function(done){
var app = koa();

app.experimental = true;

app.use(async function(){
var ctx = this;
var r = await send(ctx, 'test/fixtures/hello.txt');
});

request(app.listen())
.get('/')
.expect(200)
.expect('world', done);
});
});
7 changes: 7 additions & 0 deletions test/experimental/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

require('babel/register')({
optional: ['asyncToGenerator']
});
require('./async');

0 comments on commit 5ff48d5

Please sign in to comment.