feat: add @bokuweb/zstd-wasm as option

This commit is contained in:
wherewhere 2024-12-15 16:25:33 +08:00
parent 607a226ce0
commit bda758177a
3 changed files with 42 additions and 18 deletions

View File

@ -24,20 +24,8 @@ jobs:
restore-keys: ${{ runner.os }}-npm-cache restore-keys: ${{ runner.os }}-npm-cache
- name: Install Dependencies - name: Install Dependencies
run: npm install run: npm install
- name: Determine zstd binary version
shell: bash
run: |
case "$RUNNER_OS" in
"Linux")
echo "PLATFORM=linux-x64-gnu" >> "$GITHUB_ENV" ;;
"Windows")
echo "PLATFORM=win32-x64-msvc" >> "$GITHUB_ENV" ;;
"macOS")
echo "PLATFORM=darwin-arm64" >> "$GITHUB_ENV" ;;
esac
- name: Install zstd binary - name: Install zstd binary
shell: bash run: npm install "@mongodb-js/zstd"
run: npm install "@mongodb-js/zstd-$PLATFORM"
- name: Test - name: Test
run: npm run test run: npm run test
env: env:

View File

@ -1,8 +1,31 @@
'use strict' 'use strict'
const { compress: zstd } = require('@mongodb-js/zstd')
/**
* @returns {{ compress: (data: Buffer, level?: number) => Promise<Buffer>, init: () => Promise<void> }}
*/
function importZstd() {
try {
const { compress } = require('@mongodb-js/zstd')
return { compress };
}
catch (ex) {
try {
const { init, compress } = require('@bokuweb/zstd-wasm')
return {
compress: async (buf, level) => Buffer.from(compress(buf, level)),
init
}
}
catch {
throw ex;
}
}
}
const { compress: zstd, init = undefined } = importZstd()
const { match, logFn } = require('./tools') const { match, logFn } = require('./tools')
function zstdFn() { async function zstdFn() {
const hexo = this const hexo = this
const options = hexo.config.minify.zstd const options = hexo.config.minify.zstd
@ -13,7 +36,11 @@ function zstdFn() {
let level = options.level let level = options.level
if (typeof level !== 'number') level = undefined if (typeof level !== 'number') level = undefined
return Promise.all((match(routeList, include, globOptions)).map(path => { if (typeof init === 'function') {
await init();
}
await Promise.all((match(routeList, include, globOptions)).map(path => {
return new Promise((/** @type {(value: void) => void} */ resolve, reject) => { return new Promise((/** @type {(value: void) => void} */ resolve, reject) => {
const assetPath = route.get(path) const assetPath = route.get(path)
let assetTxt = '' let assetTxt = ''

View File

@ -38,8 +38,17 @@
"jest": "^29.1.2", "jest": "^29.1.2",
"standard": "^17.0.0" "standard": "^17.0.0"
}, },
"optionalDependencies": { "peerDependencies": {
"@mongodb-js/zstd": "^2.0.0" "@mongodb-js/zstd": "^2.0.0",
"@bokuweb/zstd-wasm": "^0.0.22"
},
"peerDependenciesMeta": {
"@mongodb-js/zstd": {
"optional": true
},
"@bokuweb/zstd-wasm": {
"optional": true
}
}, },
"keywords": [ "keywords": [
"minify", "minify",