From 871298dae5ced1ba12b0c81e80ebf1bd79fb80fd Mon Sep 17 00:00:00 2001 From: Wilson Lin Date: Sun, 12 Jul 2020 16:17:43 +1000 Subject: [PATCH] Use compression for Node.js binaries --- .github/workflows/nodejs.yml | 3 ++- nodejs/compress.js | 7 +++++++ nodejs/postinstall.js | 5 +++-- 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 nodejs/compress.js diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 2710af2..77f8e81 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -59,6 +59,7 @@ jobs: run: | npm install npm run build-binary + node compress.js - name: Install B2 CLI (macOS, Linux) if: runner.os != 'Windows' run: | @@ -73,7 +74,7 @@ jobs: run: | binary_name="$(node -e 'console.log([process.platform, process.arch, process.versions.modules].join("__"))')" b2 authorize-account ${{ secrets.CICD_CLI_B2_KEY_ID }} ${{ secrets.CICD_CLI_B2_APPLICATION_KEY }} - b2 upload-file ${{ secrets.CICD_CLI_B2_BUCKET_NAME }} ./nodejs/dist/native.node "minify-html/bin/nodejs/${{ steps.version.outputs.VERSION }}/$binary_name.node" + b2 upload-file ${{ secrets.CICD_CLI_B2_BUCKET_NAME }} ./nodejs/dist/native.node.gz "minify-html/bin/nodejs/${{ steps.version.outputs.VERSION }}/$binary_name.node.gz" package: runs-on: ubuntu-latest needs: build diff --git a/nodejs/compress.js b/nodejs/compress.js new file mode 100644 index 0000000..88e75f0 --- /dev/null +++ b/nodejs/compress.js @@ -0,0 +1,7 @@ +const {readFileSync, writeFileSync} = require('fs'); +const {join} = require('path'); +const {gzipSync} = require('zlib'); + +const src = readFileSync(join(__dirname, 'dist', 'native.node')); +const out = gzipSync(src); +writeFileSync(join(__dirname, 'dist', 'native.node.gz'), out); diff --git a/nodejs/postinstall.js b/nodejs/postinstall.js index 89fd851..6a67ed1 100644 --- a/nodejs/postinstall.js +++ b/nodejs/postinstall.js @@ -1,6 +1,7 @@ const fs = require('fs'); const https = require('https'); const path = require('path'); +const zlib = require('zlib'); const pkg = require('./package.json'); const MAX_DOWNLOAD_ATTEMPTS = 4; @@ -33,7 +34,7 @@ const downloadNativeBinary = async () => { for (let attempt = 0; ; attempt++) { let binary; try { - binary = await fetch(`https://wilsonl.in/minify-html/bin/nodejs/${pkg.version}/${binaryName}.node`); + binary = await fetch(`https://wilsonl.in/minify-html/bin/nodejs/${pkg.version}/${binaryName}.node.gz`); } catch (e) { if (e instanceof StatusError && attempt < MAX_DOWNLOAD_ATTEMPTS) { await wait(Math.random() * 2500 + 500); @@ -42,7 +43,7 @@ const downloadNativeBinary = async () => { throw e; } - fs.writeFileSync(binaryPath, binary); + fs.writeFileSync(binaryPath, zlib.gunzipSync(binary)); break; } };