Skip to content

Commit

Permalink
Merge 171b545 into c79139d
Browse files Browse the repository at this point in the history
  • Loading branch information
omsmith committed Nov 20, 2015
2 parents c79139d + 171b545 commit 46c25e3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 70 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
@@ -1,9 +1,7 @@
sudo: false
node_js:
- "4"
- "3"
- "2"
- "0.12"
- "5"
language: node_js
script: "npm run test-travis"
after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls"
9 changes: 3 additions & 6 deletions Readme.md
Expand Up @@ -28,12 +28,9 @@ var app = koa();
app.use(conditional());
app.use(etag());

app.use(function(next){
return function *(){
yield next;
this.body = 'Hello World';
}
})
app.use(function(ctx) {
ctx.body = 'Hello World';
});

app.listen(3000);

Expand Down
59 changes: 31 additions & 28 deletions index.js
Expand Up @@ -22,35 +22,38 @@ module.exports = etag;
*/

function etag(options) {
return function *etag(next){
yield* next;

// no body
var body = this.body;
if (!body || this.response.get('ETag')) return;

// type
var status = this.status / 100 | 0;

// 2xx
if (2 != status) return;

// hash
var etag;
if (body instanceof Stream) {
if (!body.path) return;
var s = yield fs.stat(body.path).catch(noop);
if (!s) return;
etag = calculate(s, options);
} else if (('string' == typeof body) || Buffer.isBuffer(body)) {
etag = calculate(body, options);
} else {
etag = calculate(JSON.stringify(body), options);
}

// add etag
if (etag) this.response.etag = etag;
return function etag(ctx, next) {
return next()
.then(() => etagUp(ctx))
.then(entity => etagCalculate(ctx, entity, options));
};
}

function etagUp(ctx, options) {
// no body
var body = ctx.body;
if (!body || ctx.response.get('ETag')) return;

// type
var status = ctx.status / 100 | 0;

// 2xx
if (2 != status) return;

if (body instanceof Stream) {
if (!body.path) return;
return fs.stat(body.path).catch(noop);
} else if (('string' == typeof body) || Buffer.isBuffer(body)) {
return body;
} else {
return JSON.stringify(body);
}
}

function etagCalculate(ctx, entity, options) {
if (!entity) return;

ctx.response.etag = calculate(entity, options);
}

function noop() {}
8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -18,7 +18,7 @@
],
"devDependencies": {
"istanbul": "^0.4.0",
"koa": "*",
"koa": "^2.0.0-alpha.3",
"should": "3",
"mocha": "2",
"supertest": "0",
Expand All @@ -30,8 +30,8 @@
"mz": "^1.0.0"
},
"scripts": {
"test": "mocha --harmony --reporter spec --require should",
"test-cov": "node --harmony node_modules/.bin/istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --require should",
"test-travis": "node --harmony node_modules/.bin/istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter dot --require should"
"test": "mocha --reporter spec --require should",
"test-cov": "node node_modules/.bin/istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --require should",
"test-travis": "node node_modules/.bin/istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter dot --require should"
}
}
63 changes: 34 additions & 29 deletions test/index.js
@@ -1,18 +1,18 @@

var request = require('supertest');
var koa = require('koa');
var Koa = require('koa');
var etag = require('..');
var fs = require('fs');

describe('etag()', function(){
describe('when body is missing', function(){
it('should not add ETag', function(done){
var app = koa();
var app = new Koa();

app.use(etag());

app.use(function *(next){
yield next;
app.use(function (ctx, next){
return next();
});

request(app.listen())
Expand All @@ -23,14 +23,14 @@ describe('etag()', function(){

describe('when ETag is exists', function(){
it('should not add ETag', function(done){
var app = koa();
var app = new Koa();

app.use(etag());

app.use(function *(next){
this.body = {hi: 'etag'};
this.etag = 'etaghaha';
yield next;
app.use(function (ctx, next){
ctx.body = {hi: 'etag'};
ctx.etag = 'etaghaha';
return next();
});

request(app.listen())
Expand All @@ -43,13 +43,14 @@ describe('etag()', function(){

describe('when body is a string', function(){
it('should add ETag', function(done){
var app = koa();
var app = new Koa();

app.use(etag());

app.use(function *(next){
yield next;
this.body = 'Hello World';
app.use(function (ctx, next){
return next().then(function() {
ctx.body = 'Hello World';
});
});

request(app.listen())
Expand All @@ -61,13 +62,14 @@ describe('etag()', function(){

describe('when body is a Buffer', function(){
it('should add ETag', function(done){
var app = koa();
var app = new Koa();

app.use(etag());

app.use(function *(next){
yield next;
this.body = new Buffer('Hello World');
app.use(function (ctx, next){
return next().then(function() {
ctx.body = new Buffer('Hello World');
});
});

request(app.listen())
Expand All @@ -79,13 +81,14 @@ describe('etag()', function(){

describe('when body is JSON', function(){
it('should add ETag', function(done){
var app = koa();
var app = new Koa();

app.use(etag());

app.use(function *(next){
yield next;
this.body = { foo: 'bar' };
app.use(function (ctx, next){
return next().then(function() {
ctx.body = { foo: 'bar' };
});
});

request(app.listen())
Expand All @@ -97,13 +100,14 @@ describe('etag()', function(){

describe('when body is a stream with a .path', function(){
it('should add an ETag', function(done){
var app = koa();
var app = new Koa();

app.use(etag());

app.use(function *(next){
yield next;
this.body = fs.createReadStream('package.json');
app.use(function (ctx, next){
return next().then(function() {
ctx.body = fs.createReadStream('package.json');
});
});

request(app.listen())
Expand All @@ -115,14 +119,15 @@ describe('etag()', function(){

describe('when with options', function(){
it('should add weak ETag', function(done){
var app = koa();
var app = new Koa();
var options = {weak: true};

app.use(etag(options));

app.use(function *(next){
yield next;
this.body = 'Hello World';
app.use(function (ctx, next){
return next().then(function() {
ctx.body = 'Hello World';
});
});

request(app.listen())
Expand Down

0 comments on commit 46c25e3

Please sign in to comment.