diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 1e5a251..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: Build and publish Node.js package - -on: - push: - tags: - - 'v*' - workflow_dispatch: - -jobs: - build: - runs-on: ${{ matrix.os }} - strategy: - matrix: - os: [ubuntu-20.04, macos-12, macos-14, windows-2019] - steps: - - uses: actions/checkout@v4 - - - name: Get version - id: version - shell: bash - run: | - echo "VERSION=$([[ "$GITHUB_REF" == refs/tags/v* ]] && echo ${GITHUB_REF#refs/tags/v} || echo '0.0.0')" >> $GITHUB_OUTPUT - - - name: Set up Node.js - uses: actions/setup-node@v4 - - - name: Build native module - id: module - shell: bash - run: | - npm install - npm run build-release - echo "BINARY_NAME=$(node -e 'console.log([process.platform, process.arch].join("__"))')" >> $GITHUB_OUTPUT - - - name: Upload to R2 - run: | - node .github/workflows/deploy.js index.node hexo-optimize/bin/nodejs/${{ steps.version.outputs.VERSION }}/${{ steps.module.outputs.BINARY_NAME }}.node - env: - ACCOUNT_ID: ${{ secrets.ACCOUNT_ID }} - SECRET_ACCESS_KEY: ${{ secrets.SECRET_ACCESS_KEY }} - ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }} diff --git a/.github/workflows/deploy.js b/.github/workflows/deploy.js deleted file mode 100644 index e4be94d..0000000 --- a/.github/workflows/deploy.js +++ /dev/null @@ -1,48 +0,0 @@ -const AWS = require('aws-sdk'); -const fs = require('fs'); -const process = require('process'); - -function uploadPkgToR2(accountId, secretAccessKey, accessKeyId, bucketName, filename, uploadFilePath) { - const endpointUrl = `https://${accountId}.r2.cloudflarestorage.com`; - - // Configure the AWS SDK to use the custom endpoint and credentials - const s3 = new AWS.S3({ - endpoint: new AWS.Endpoint(endpointUrl), - accessKeyId: accessKeyId, - secretAccessKey: secretAccessKey, - s3ForcePathStyle: true, // needed with custom endpoint - signatureVersion: 'v4' - }); - - console.log(`Uploading asset: ${filename} to ${uploadFilePath} in bucket ${bucketName}, using ${endpointUrl}`); - - // Set up the parameters for the S3 upload - const params = { - Bucket: bucketName, - Key: uploadFilePath, - Body: fs.createReadStream(filename) - }; - - // Perform the upload to S3 (R2 in this case) - s3.upload(params, function(err, data) { - if (err) { - console.log("An error occurred", err); - throw err; - } - console.log(`Successfully uploaded '${filename}' to ${data.Location}`); - }); -} - -if (process.argv.length < 4) { - console.log("Usage: node deploy.js "); - process.exit(1); -} - -const accountId = process.env.ACCOUNT_ID; -const secretAccessKey = process.env.SECRET_ACCESS_KEY; -const accessKeyId = process.env.ACCESS_KEY_ID; -const bucketName = "archive"; -const filename = process.argv[2]; -const uploadFilePath = process.argv[3]; - -uploadPkgToR2(accountId, secretAccessKey, accessKeyId, bucketName, filename, uploadFilePath); diff --git a/.gitignore b/.gitignore index e8ec6fa..bdae546 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,3 @@ node_modules/ package-lock.json .DS_Store tmp/ -Cargo.lock -index.node -target/ \ No newline at end of file diff --git a/.no-postinstall b/.no-postinstall deleted file mode 100644 index e69de29..0000000 diff --git a/Cargo.toml b/Cargo.toml deleted file mode 100644 index dbf57bf..0000000 --- a/Cargo.toml +++ /dev/null @@ -1,18 +0,0 @@ -[package] -name = "html-minify" -publish = false -version = "0.10.3" -description = "Node.js bindings for html-minify" -authors = ["Wilson Lin "] -edition = "2021" - -[lib] -crate-type = ["cdylib"] - -[dependencies] -minify-html = "0.10.8" - -[dependencies.neon] -version = "1" -default-features = false -features = ["napi-1", "external-buffers"] diff --git a/lib/html.js b/lib/html.js index c0f9ace..03a974d 100644 --- a/lib/html.js +++ b/lib/html.js @@ -1,8 +1,4 @@ -let minifyHtml; -try { - minifyHtml = require('../index.node'); -} catch (error) { -} +const minifyHtml = require('@minify-html/node'); const { inExcludes } = require('./util'); module.exports = function(str, path, config) { diff --git a/package.json b/package.json index 1fe77e1..5e8cd71 100644 --- a/package.json +++ b/package.json @@ -11,10 +11,7 @@ "postinstall.js" ], "scripts": { - "build": "cargo-cp-artifact --artifact cdylib html-minify index.node -- cargo build --message-format=json-render-diagnostics", - "build-release": "npm run build -- --release", - "lint": "eslint index.js lib", - "postinstall": "node postinstall.js" + "lint": "eslint index.js lib" }, "repository": "next-theme/hexo-optimize", "keywords": [ @@ -30,16 +27,13 @@ }, "homepage": "https://github.com/next-theme/hexo-optimize#readme", "dependencies": { + "@minify-html/node": "^0.16.4", "bluebird": "^3.7.2", - "cargo-cp-artifact": "^0.1", "lightningcss": "^1.24.1", "micromatch": "^4.0.5", "terser": "^5.30.3" }, "devDependencies": { - "@next-theme/eslint-config": "0.0.3", - "aws-sdk": "^2.1599.0", - "eslint": "8.57.0", - "lighthouse": "^9.6.7" + "@next-theme/eslint-config": "0.0.3" } } diff --git a/postinstall.js b/postinstall.js deleted file mode 100644 index ec1e56a..0000000 --- a/postinstall.js +++ /dev/null @@ -1,104 +0,0 @@ -/*! -MIT License - -Copyright (c) 2020 Wilson Lin - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - -https://github.com/wilsonzlin/minify-html/blob/master/nodejs/postinstall.js -*/ - -const fs = require('fs'); -const https = require('https'); -const path = require('path'); -const pkg = require('./package.json'); -const cp = require('child_process'); - -const MAX_DOWNLOAD_ATTEMPTS = 4; - -const binaryName = [process.platform, process.arch].join('__'); -const binaryPath = path.join(__dirname, 'index.node'); - -const wait = ms => new Promise(resolve => setTimeout(resolve, ms)); - -class StatusError extends Error { - constructor(status) { - super(`Bad status of ${status}`); - this.status = status; - } -} - -const fetch = url => - new Promise((resolve, reject) => { - const stream = https.get(url, resp => { - if (!resp.statusCode || resp.statusCode < 200 || resp.statusCode > 299) { - return reject(new StatusError(resp.statusCode)); - } - const parts = []; - resp.on('data', chunk => parts.push(chunk)); - resp.on('end', () => resolve(Buffer.concat(parts))); - }); - stream.on('error', reject); - }); - -const downloadNativeBinary = async() => { - for (let attempt = 0; ; attempt++) { - let binary; - try { - binary = await fetch( - `https://archive.zsq.im/hexo-optimize/bin/nodejs/${pkg.version}/${binaryName}.node` - ); - } catch (e) { - if ( - e instanceof StatusError - && e.status !== 404 - && attempt < MAX_DOWNLOAD_ATTEMPTS - ) { - await wait((Math.random() * 2500) + 500); - continue; - } - throw e; - } - - fs.writeFileSync(binaryPath, binary); - break; - } -}; - -if ( - !fs.existsSync(path.join(__dirname, '.no-postinstall')) - && !fs.existsSync(binaryPath) -) { - downloadNativeBinary().then( - () => console.log(`Downloaded ${pkg.name}`), - err => { - console.error( - `Failed to download ${pkg.name}, will build from source: ${err}` - ); - const out = cp.spawnSync('npm', ['run', 'build-release'], { - cwd: __dirname, - stdio: ['ignore', 'inherit', 'inherit'] - }); - process.exitCode = out.exitCode; - if (out.error) { - throw out.error; - } - } - ); -} diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index 72837f9..0000000 --- a/src/lib.rs +++ /dev/null @@ -1,83 +0,0 @@ -/*! -MIT License - -Copyright (c) 2020 Wilson Lin - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - -https://github.com/wilsonzlin/minify-html/blob/master/nodejs/postinstall.js -*/ - -use neon::prelude::*; -use neon::types::buffer::TypedArray; - -fn minify(mut cx: FunctionContext) -> JsResult { - let src = cx.argument::(0)?; - let opt = cx.argument::(1)?; - let cfg = minify_html::Cfg { - do_not_minify_doctype: opt - .get_opt::(&mut cx, "do_not_minify_doctype")? - .map(|v| v.value(&mut cx)) - .unwrap_or(false), - ensure_spec_compliant_unquoted_attribute_values: opt - .get_opt::(&mut cx, "ensure_spec_compliant_unquoted_attribute_values")? - .map(|v| v.value(&mut cx)) - .unwrap_or(false), - keep_closing_tags: opt - .get_opt::(&mut cx, "keep_closing_tags")? - .map(|v| v.value(&mut cx)) - .unwrap_or(false), - keep_html_and_head_opening_tags: opt - .get_opt::(&mut cx, "keep_html_and_head_opening_tags")? - .map(|v| v.value(&mut cx)) - .unwrap_or(false), - keep_spaces_between_attributes: opt - .get_opt::(&mut cx, "keep_spaces_between_attributes")? - .map(|v| v.value(&mut cx)) - .unwrap_or(false), - keep_comments: opt - .get_opt::(&mut cx, "keep_comments")? - .map(|v| v.value(&mut cx)) - .unwrap_or(false), - minify_css: opt - .get_opt::(&mut cx, "minify_css")? - .map(|v| v.value(&mut cx)) - .unwrap_or(false), - minify_js: opt - .get_opt::(&mut cx, "minify_js")? - .map(|v| v.value(&mut cx)) - .unwrap_or(false), - remove_bangs: opt - .get_opt::(&mut cx, "remove_bangs")? - .map(|v| v.value(&mut cx)) - .unwrap_or(false), - remove_processing_instructions: opt - .get_opt::(&mut cx, "remove_processing_instructions")? - .map(|v| v.value(&mut cx)) - .unwrap_or(false), - }; - let out = minify_html::minify(src.as_slice(&mut cx), &cfg); - Ok(JsBuffer::external(&mut cx, out)) -} - -#[neon::main] -fn main(mut cx: ModuleContext) -> NeonResult<()> { - cx.export_function("minify", minify)?; - Ok(()) -}