Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed Jun 13, 2014
0 parents commit 2a79b33
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store*
node_modules
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

The MIT License (MIT)

Copyright (c) 2014 Jonathan Ong [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

# Koa HTML Minifier

Middleware that minifies your HTML responses using [html-minifier](https://github.com/kangax/html-minifier).
It uses `html-minifier`'s default options which are all turned off by default,
so you __have__ to set the options otherwise it's not going to do anything.

## API

```js
// do compression stuff first
app.use(require('koa-compress')());

// then use this minifier
app.use(require('koa-html-minifier')({
collapseWhitespace: true
}));
```

### Options

See: https://github.com/kangax/html-minifier#options-quick-reference
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

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)
}
}
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "koa-html-minifier",
"description": "minify HTML responses like some crazy guy",
"version": "1.0.0",
"author": {
"name": "Jonathan Ong",
"email": "[email protected]",
"url": "http:https://jongleberry.com",
"twitter": "https://twitter.com/jongleberry"
},
"license": "MIT",
"repository": "koajs/html-minifier",
"dependencies": {
"html-minifier": "*"
},
"devDependencies": {
"koa": "0",
"mocha": "1",
"supertest": "0"
},
"scripts": {
"test": "mocha --harmony-generators --reporter spec --bail"
}
}
111 changes: 111 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@

var koa = require('koa')
var request = require('supertest')
var PassThrough = require('stream').PassThrough

var minifier = require('./')

var options = {
collapseWhitespace: true
}

describe('Koa HTML Minifier', function () {
describe('when the response is HTML', function () {
var input = '<div> <p> foo </p> </div>'
var output = '<div><p>foo</p></div>'

describe('and the body is empty', function () {
it('should not crash', function (done) {
var app = koa()
app.use(minifier(options))
app.use(function* () {
this.body = null
})

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

describe('and the body is a string', function () {
it('should minify', function (done) {
var app = koa()
app.use(minifier(options))
app.use(function* () {
this.body = input
})

request(app.listen())
.get('/')
.expect(200)
.expect('Content-Type', /text\/html/)
.expect(output, done)
})
})

describe('and the body is a buffer', function () {
it('should minify', function (done) {
var app = koa()
app.use(minifier(options))
app.use(function* () {
this.response.type = 'html'
this.body = new Buffer(input, 'utf8')
})

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

describe('and the body is an object', function () {
it('should not crash', function (done) {
var app = koa()
app.use(minifier(options))
app.use(function* () {
this.body = {}
this.response.type = 'html'
})

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

describe('and the body is a stream', function () {
it('should not minify', function (done) {
var app = koa()
app.use(minifier(options))
app.use(function* () {
this.response.type = 'html'
var stream = this.body = new PassThrough()
stream.end(input)
})

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

describe('when the response is not HTML', function () {
it('should do nothing', function (done) {
var text = 'lol < > <3'
var app = koa()
app.use(minifier(options))
app.use(function* () {
this.body = text
})

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

0 comments on commit 2a79b33

Please sign in to comment.