Skip to content

Commit

Permalink
feat: update to koa 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsc committed Jun 19, 2017
1 parent 56411d3 commit 856c416
Show file tree
Hide file tree
Showing 5 changed files with 951 additions and 136 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,5 +1,5 @@
node_js:
- "0.11"
- "7"
language: node_js
script: "npm run test-travis"
after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls"
30 changes: 13 additions & 17 deletions index.js
@@ -1,18 +1,14 @@
const minify = require('html-minifier').minify;

var minify = require('html-minifier').minify

module.exports = function (options) {
options = options || {}
return function* minifyHTML(next) {
yield* next

if (!this.response.is('html')) return
var body = this.response.body
if (!body) return
// too lazy to handle streams
if (typeof body.pipe === 'function') return
if (Buffer.isBuffer(body)) body = body.toString('utf8')
else if (typeof body === 'object') return // wtf programming
this.response.body = minify(body, options)
}
}
module.exports = (options = {}) => {
return async (ctx, next) => {
await next();
if (!ctx.response.is('html')) return;
let { body } = ctx.response;
if (!body) return;
if (typeof body.pipe === 'function') return;
if (Buffer.isBuffer(body)) body = body.toString('utf8');
else if (typeof body === 'object') return;
ctx.response.body = await minify(body, options);
};
};

0 comments on commit 856c416

Please sign in to comment.