diff --git a/lib/filter.js b/lib/filter.js index 8127b23..5f4f63c 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -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) { diff --git a/test/filter.test.js b/test/filter.test.js index eb4db92..30a2849 100644 --- a/test/filter.test.js +++ b/test/filter.test.js @@ -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)