feat: native zstd

fixes #160
closes #162
This commit is contained in:
MDLeom 2025-08-01 10:51:23 +00:00
parent f7b6e05180
commit 2cd5786e86
No known key found for this signature in database
GPG Key ID: 06C236E63CBC68AA
4 changed files with 12 additions and 12 deletions

View File

@ -266,7 +266,7 @@ minify:
- **verbose** - Verbose output. Defaults to `false`.
- **include** - Include files. Support [wildcard](http://www.globtester.com/) pattern(s) in a string or array.
- **globOptions** - See [globbing](#globbing) section.
- **level** - Compression level. Range `1-22`. Defaults to `3`, or the value of [`DEFAULT_LEVEL`](https://github.com/mongodb-js/zstd/blob/a3a08c61c9045411c8275e248498dbc583457fb5/src/lib.rs#L9)
- **level** - Compression level. Range `1-22`. Defaults to `3`, or the value of [`ZSTD_CLEVEL_DEFAULT`](https://nodejs.org/api/zlib.html#zlib_compressor_options_1)
## Globbing

View File

@ -8,9 +8,9 @@ const zlib = require('zlib')
const { promisify } = require('util')
const gzip = promisify(zlib.gzip)
const br = promisify(zlib.brotliCompress)
const zstd = promisify(zlib.zstdCompress)
const { minify: compressXml } = require('minify-xml')
const micromatch = require('micromatch')
const { compress: zstd } = require('@mongodb-js/zstd')
const isMatch = (path = '', patterns = [], options = {}) => {
if (path && patterns) {
@ -240,7 +240,7 @@ function zstdFn () {
const routeList = route.list()
const { globOptions, include, verbose } = options
let { level } = options
if (typeof level !== 'number') level = undefined
if (typeof level !== 'number') level = zlib.constants.ZSTD_CLEVEL_DEFAULT
return Promise.all((match(routeList, include, globOptions)).map((path) => {
return new Promise((resolve, reject) => {
@ -250,8 +250,7 @@ function zstdFn () {
assetPath.on('end', async () => {
if (assetTxt.length) {
try {
const input = Buffer.from(assetTxt, 'utf-8')
const result = await zstd(input, level)
const result = await zstd(assetTxt, { params: { [zlib.constants.ZSTD_c_compressionLevel]: level } })
if (verbose) logFn.call(this, assetTxt, result, path, 'zstd')
resolve(route.set(path + '.zst', result))
} catch (err) {

View File

@ -31,8 +31,7 @@
"micromatch": "^4.0.2",
"minify-xml": "^3.2.0",
"svgo": "^4.0.0",
"terser": "^5.3.0",
"@mongodb-js/zstd": "^2.0.1"
"terser": "^5.3.0"
},
"devDependencies": {
"hexo": "^7.1.0",

View File

@ -2,14 +2,16 @@
'use strict'
const Hexo = require('hexo')
const { compress: zstd, decompress: unzstd } = require('@mongodb-js/zstd')
const zlib = require('zlib')
const { promisify } = require('util')
const zstd = promisify(zlib.zstdCompress)
const unzstd = promisify(zlib.zstdDecompress)
describe('zstd', () => {
const hexo = new Hexo(__dirname)
const z = require('../lib/filter').zstdFn.bind(hexo)
const path = 'foo.txt'
const input = 'Lorem ipsum dolor sit amet consectetur adipiscing elit fusce'
const inputBuf = Buffer.from(input, 'utf8')
beforeEach(() => {
hexo.config.minify = {
@ -36,7 +38,7 @@ describe('zstd', () => {
output.on('data', (chunk) => (buf.push(chunk)))
output.on('end', async () => {
const result = Buffer.concat(buf)
const expected = await zstd(inputBuf)
const expected = await zstd(input)
const resultUnzst = await unzstd(result)
const expectedUnzst = await unzstd(expected)
@ -74,7 +76,7 @@ describe('zstd', () => {
output.on('data', (chunk) => (buf.push(chunk)))
output.on('end', async () => {
const result = Buffer.concat(buf)
const expected = await zstd(inputBuf, level)
const expected = await zstd(input, { params: { [zlib.constants.ZSTD_c_compressionLevel]: level } })
expect(result.equals(expected)).toBe(true)
})
@ -98,7 +100,7 @@ describe('zstd', () => {
output.on('data', (chunk) => (buf.push(chunk)))
output.on('end', async () => {
const result = Buffer.concat(buf)
const expected = await zstd(inputBuf, undefined)
const expected = await zstd(input, { params: { [zlib.constants.ZSTD_c_compressionLevel]: zlib.constants.ZSTD_CLEVEL_DEFAULT } })
expect(result.equals(expected)).toBe(true)
})