From bda758177aec6b48eb8a8d723d38a3ae71a27ed5 Mon Sep 17 00:00:00 2001 From: wherewhere Date: Sun, 15 Dec 2024 16:25:33 +0800 Subject: [PATCH] feat: add @bokuweb/zstd-wasm as option --- .github/workflows/tester.yml | 14 +------------- lib/zstd.js | 33 ++++++++++++++++++++++++++++++--- package.json | 13 +++++++++++-- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/.github/workflows/tester.yml b/.github/workflows/tester.yml index 72243f9..7ae0ec1 100644 --- a/.github/workflows/tester.yml +++ b/.github/workflows/tester.yml @@ -24,20 +24,8 @@ jobs: restore-keys: ${{ runner.os }}-npm-cache - name: Install Dependencies 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 - shell: bash - run: npm install "@mongodb-js/zstd-$PLATFORM" + run: npm install "@mongodb-js/zstd" - name: Test run: npm run test env: diff --git a/lib/zstd.js b/lib/zstd.js index a3b6a75..a0ecf36 100644 --- a/lib/zstd.js +++ b/lib/zstd.js @@ -1,8 +1,31 @@ 'use strict' -const { compress: zstd } = require('@mongodb-js/zstd') + +/** + * @returns {{ compress: (data: Buffer, level?: number) => Promise, init: () => Promise }} + */ +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') -function zstdFn() { +async function zstdFn() { const hexo = this const options = hexo.config.minify.zstd @@ -13,7 +36,11 @@ function zstdFn() { let level = options.level 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) => { const assetPath = route.get(path) let assetTxt = '' diff --git a/package.json b/package.json index 13656ed..33d895c 100644 --- a/package.json +++ b/package.json @@ -38,8 +38,17 @@ "jest": "^29.1.2", "standard": "^17.0.0" }, - "optionalDependencies": { - "@mongodb-js/zstd": "^2.0.0" + "peerDependencies": { + "@mongodb-js/zstd": "^2.0.0", + "@bokuweb/zstd-wasm": "^0.0.22" + }, + "peerDependenciesMeta": { + "@mongodb-js/zstd": { + "optional": true + }, + "@bokuweb/zstd-wasm": { + "optional": true + } }, "keywords": [ "minify",