feat(gz & br): support custom compression level

This commit is contained in:
curbengh 2019-12-27 07:50:03 +00:00
parent ab023da82e
commit 48868bfa7e
No known key found for this signature in database
GPG Key ID: 21EA847C35D6E034
2 changed files with 60 additions and 2 deletions

View File

@ -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('')
@ -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) {
@ -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('')
@ -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) {

View File

@ -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)
@ -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)