Skip to content

Commit

Permalink
feat(gz & br): support custom compression level
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Dec 27, 2019
1 parent ab023da commit 48868bf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ function gzipFn () {
const { route } = hexo
const routeList = route.list()
const { globOptions, include } = options
let { level } = options
if (typeof level !== 'number') level = zlib.constants.Z_BEST_COMPRESSION

let includeString = include || ''
if (include && Array.isArray(include)) includeString = include.join('')
Expand All @@ -151,7 +153,7 @@ function gzipFn () {
try {
// TODO: Drop Buffer
const input = Buffer.from(assetTxt, 'utf-8')
const result = await gzip(input, { level: zlib.constants.Z_BEST_COMPRESSION })
const result = await gzip(input, { level })
if (options.logger) verbose.call(this, input, result, path, 'gzip')
resolve(route.set(path + '.gz', result))
} catch (err) {
Expand All @@ -171,6 +173,8 @@ function brotliFn () {
const { route } = hexo
const routeList = route.list()
const { globOptions, include } = options
let { level } = options
if (typeof level !== 'number') level = zlib.constants.BROTLI_MAX_QUALITY

let includeString = include || ''
if (include && Array.isArray(include)) includeString = include.join('')
Expand All @@ -185,7 +189,7 @@ function brotliFn () {
if (assetTxt.length) {
try {
const input = Buffer.from(assetTxt, 'utf-8')
const result = await br(input)
const result = await br(input, { params: { [zlib.constants.BROTLI_PARAM_QUALITY]: level } })
if (options.logger) verbose.call(this, input, result, path, 'brotli')
resolve(route.set(path + '.br', result))
} catch (err) {
Expand Down
54 changes: 54 additions & 0 deletions test/filter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,43 @@ describe('gzip', () => {
expect(result).toBeUndefined()
})

test('option', async () => {
const customOpt = {
level: 1
}
hexo.config.minify.gzip.level = customOpt.level
await g()

const output = hexo.route.get(path.concat('.gz'))
const buf = []
output.on('data', (chunk) => (buf.push(chunk)))
output.on('end', async () => {
const result = Buffer.concat(buf)
const expected = await gzip(input, customOpt)

expect(result.toString('base64')).toBe(Buffer.from(expected, 'binary').toString('base64'))
})
})

test('option - invalid', async () => {
const customOpt = {
level: 9000
}
hexo.config.minify.gzip.level = customOpt.level

let expected
try {
await gzip(input, customOpt)
} catch (err) {
expected = err.message
}
try {
await g()
} catch (err) {
expect(err.message).toContain(expected)
}
})

test('include - exclude non-text file by default', async () => {
const path = 'foo.jpg'
hexo.route.set(path, input)
Expand Down Expand Up @@ -496,6 +533,23 @@ describe('brotli', () => {
expect(result).toBeUndefined()
})

test('option - invalid', async () => {
const level = 'foo'
hexo.config.minify.brotli.level = level

let expected
try {
await brotli(input, { params: { [zlib.constants.BROTLI_PARAM_QUALITY]: level } })
} catch (err) {
expected = err.message
}
try {
await b()
} catch (err) {
expect(err.message).toContain(expected)
}
})

test('include - exclude non-text file by default', async () => {
const path = 'foo.jpg'
hexo.route.set(path, input)
Expand Down

0 comments on commit 48868bf

Please sign in to comment.