This commit is contained in:
Ming Di Leom 2025-08-17 08:40:46 +00:00 committed by GitHub
commit 5b09e8274c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 19 additions and 17 deletions

View File

@ -8,7 +8,7 @@ jobs:
strategy: strategy:
matrix: matrix:
os: [ubuntu-latest, windows-latest, macos-latest] os: [ubuntu-latest, windows-latest, macos-latest]
node-version: ["20", "22", "24"] node-version: ["22", "24"]
fail-fast: false fail-fast: false
steps: steps:
- uses: actions/checkout@v5 - uses: actions/checkout@v5

View File

@ -41,6 +41,7 @@ minify:
svg: svg:
gzip: gzip:
brotli: brotli:
zstd:
xml: xml:
json: json:
``` ```
@ -53,6 +54,7 @@ minify:
- **svg** - See [SVG](#svg) section - **svg** - See [SVG](#svg) section
- **gzip** - See [Gzip](#gzip) section - **gzip** - See [Gzip](#gzip) section
- **brotli** - See [Brotli](#brotli) section - **brotli** - See [Brotli](#brotli) section
- **zstd** - See [Zstd](#zstd) section
- **xml** - See [XML](#xml) section - **xml** - See [XML](#xml) section
- **json** - See [JSON](#json) section - **json** - See [JSON](#json) section
@ -246,7 +248,7 @@ minify:
```yaml ```yaml
minify: minify:
zstd: zstd:
enable: false enable: true
include: include:
- "*.html" - "*.html"
- "*.css" - "*.css"
@ -261,12 +263,12 @@ minify:
- "*.json" - "*.json"
``` ```
- **enable** - Enable the plugin. Defaults to `false`. - **enable** - Enable the plugin. Defaults to `true`.
- **priority** - Plugin's priority. Defaults to `10`. - **priority** - Plugin's priority. Defaults to `10`.
- **verbose** - Verbose output. Defaults to `false`. - **verbose** - Verbose output. Defaults to `false`.
- **include** - Include files. Support [wildcard](http://www.globtester.com/) pattern(s) in a string or array. - **include** - Include files. Support [wildcard](http://www.globtester.com/) pattern(s) in a string or array.
- **globOptions** - See [globbing](#globbing) section. - **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 ## Globbing

View File

@ -70,7 +70,7 @@ hexo.config.minify.brotli = Object.assign({
}, hexo.config.minify.brotli) }, hexo.config.minify.brotli)
hexo.config.minify.zstd = Object.assign({ hexo.config.minify.zstd = Object.assign({
enable: false, enable: true,
priority: 10, priority: 10,
verbose: false, verbose: false,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'], include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'],

View File

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

View File

@ -16,7 +16,7 @@
"test": "jest" "test": "jest"
}, },
"engines": { "engines": {
"node": ">= 20.9.0" "node": ">= 22.15.0"
}, },
"author": "curben", "author": "curben",
"license": "MIT", "license": "MIT",
@ -31,8 +31,7 @@
"micromatch": "^4.0.2", "micromatch": "^4.0.2",
"minify-xml": "^3.2.0", "minify-xml": "^3.2.0",
"svgo": "^4.0.0", "svgo": "^4.0.0",
"terser": "^5.3.0", "terser": "^5.3.0"
"@mongodb-js/zstd": "^2.0.1"
}, },
"devDependencies": { "devDependencies": {
"hexo": "^7.1.0", "hexo": "^7.1.0",

View File

@ -2,14 +2,16 @@
'use strict' 'use strict'
const Hexo = require('hexo') 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', () => { describe('zstd', () => {
const hexo = new Hexo(__dirname) const hexo = new Hexo(__dirname)
const z = require('../lib/filter').zstdFn.bind(hexo) const z = require('../lib/filter').zstdFn.bind(hexo)
const path = 'foo.txt' const path = 'foo.txt'
const input = 'Lorem ipsum dolor sit amet consectetur adipiscing elit fusce' const input = 'Lorem ipsum dolor sit amet consectetur adipiscing elit fusce'
const inputBuf = Buffer.from(input, 'utf8')
beforeEach(() => { beforeEach(() => {
hexo.config.minify = { hexo.config.minify = {
@ -36,7 +38,7 @@ describe('zstd', () => {
output.on('data', (chunk) => (buf.push(chunk))) output.on('data', (chunk) => (buf.push(chunk)))
output.on('end', async () => { output.on('end', async () => {
const result = Buffer.concat(buf) const result = Buffer.concat(buf)
const expected = await zstd(inputBuf) const expected = await zstd(input)
const resultUnzst = await unzstd(result) const resultUnzst = await unzstd(result)
const expectedUnzst = await unzstd(expected) const expectedUnzst = await unzstd(expected)
@ -74,7 +76,7 @@ describe('zstd', () => {
output.on('data', (chunk) => (buf.push(chunk))) output.on('data', (chunk) => (buf.push(chunk)))
output.on('end', async () => { output.on('end', async () => {
const result = Buffer.concat(buf) 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) expect(result.equals(expected)).toBe(true)
}) })
@ -98,7 +100,7 @@ describe('zstd', () => {
output.on('data', (chunk) => (buf.push(chunk))) output.on('data', (chunk) => (buf.push(chunk)))
output.on('end', async () => { output.on('end', async () => {
const result = Buffer.concat(buf) 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) expect(result.equals(expected)).toBe(true)
}) })