Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuweiyou committed Nov 20, 2017
1 parent 3f1d91b commit 09e4000
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 101 deletions.
5 changes: 1 addition & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ root = true
[*]
charset = utf-8
indent_style = space
indent_size = 4
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

[{*.json,.travis.yml}]
indent_size = 2

[*.md]
max_line_length = off
6 changes: 3 additions & 3 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.DS_Store*
node_modules
coverage
.idea
node_modules/
coverage/
.idea/
34 changes: 17 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const minify = require('html-minifier').minify

module.exports = (options = {}) => async (ctx, next) => {
await next()
if (!ctx.response.is('html')) {
return
}
let body = ctx.body
if (!body) {
return
}
if (typeof body.pipe === 'function') {
return
}
if (Buffer.isBuffer(body)) {
body = body.toString('utf8')
} else if (typeof body === 'object') {
return
}
ctx.body = minify(body, options)
await next()
if (!ctx.response.is('html')) {
return
}
let body = ctx.body
if (!body) {
return
}
if (typeof body.pipe === 'function') {
return
}
if (Buffer.isBuffer(body)) {
body = body.toString('utf8')
} else if (typeof body === 'object') {
return
}
ctx.body = minify(body, options)
}
154 changes: 77 additions & 77 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,100 +5,100 @@ const PassThrough = require('stream').PassThrough
const minifier = require('..')

const options = {
collapseWhitespace: true
collapseWhitespace: true
}

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

describe('and the body is empty', () => {
it('should not crash', done => {
const app = new Koa()
app.use(minifier(options))
app.use(cxt => cxt.body = null)
describe('and the body is empty', () => {
it('should not crash', done => {
const app = new Koa()
app.use(minifier(options))
app.use(cxt => cxt.body = null)

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

describe('and the body is a string', () => {
it('should minify', done => {
const app = new Koa()
app.use(minifier(options))
app.use(cxt => cxt.body = input)
describe('and the body is a string', () => {
it('should minify', done => {
const app = new Koa()
app.use(minifier(options))
app.use(cxt => cxt.body = input)

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

describe('and the body is a buffer', () => {
it('should minify', done => {
const app = new Koa()
app.use(minifier(options))
app.use(cxt => {
cxt.response.type = 'html'
cxt.body = new Buffer(input, 'utf8')
})
request(app.listen())
.get('/')
.expect(200)
.expect('Content-Type', /text\/html/)
.expect(output, done)
})
})

request(app.listen())
.get('/')
.expect(200)
.expect(output, done)
})
describe('and the body is a buffer', () => {
it('should minify', done => {
const app = new Koa()
app.use(minifier(options))
app.use(cxt => {
cxt.response.type = 'html'
cxt.body = new Buffer(input, 'utf8')
})

describe('and the body is an object', () => {
it('should not crash', done => {
const app = new Koa()
app.use(minifier(options))
app.use(cxt => {
cxt.body = {}
cxt.response.type = 'html'
})
request(app.listen())
.get('/')
.expect(200)
.expect(output, done)
})
})

request(app.listen())
.get('/')
.expect(200, done)
})
describe('and the body is an object', () => {
it('should not crash', done => {
const app = new Koa()
app.use(minifier(options))
app.use(cxt => {
cxt.body = {}
cxt.response.type = 'html'
})

describe('and the body is a stream', () => {
it('should not minify', done => {
const app = new Koa()
app.use(minifier(options))
app.use(cxt => {
cxt.response.type = 'html'
const stream = cxt.body = new PassThrough()
stream.end(input)
})
request(app.listen())
.get('/')
.expect(200, done)
})
})

request(app.listen())
.get('/')
.expect(200)
.expect(input, done)
})
describe('and the body is a stream', () => {
it('should not minify', done => {
const app = new Koa()
app.use(minifier(options))
app.use(cxt => {
cxt.response.type = 'html'
const stream = cxt.body = new PassThrough()
stream.end(input)
})

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

describe('when the response is not HTML', () => {
it('should do nothing', done => {
const text = 'lol < > <3'
const app = new Koa()
app.use(minifier(options))
app.use(cxt => cxt.body = text)
describe('when the response is not HTML', () => {
it('should do nothing', done => {
const text = 'lol < > <3'
const app = new Koa()
app.use(minifier(options))
app.use(cxt => cxt.body = text)

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

0 comments on commit 09e4000

Please sign in to comment.