From 82a0b8ecee0e3b237fe4bf2ed1906d0d7d30bde6 Mon Sep 17 00:00:00 2001 From: Wilson Lin Date: Sat, 11 Jul 2020 22:02:43 +1000 Subject: [PATCH] Use online platform binary downloader install script for Node.js instead of bundling all modules --- .github/workflows/nodejs.yml | 6 ---- nodejs/build.sh | 18 ++--------- nodejs/install.js | 59 ++++++++++++++++++++++++++++++++++++ nodejs/native/Cargo.toml | 2 +- nodejs/package.json | 15 ++++----- nodejs/src/index.ts | 4 +-- 6 files changed, 72 insertions(+), 32 deletions(-) create mode 100644 nodejs/install.js diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 4d4219e..6eb3403 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -83,10 +83,6 @@ jobs: uses: actions/setup-node@master with: node-version: 14.x - - name: Install B2 CLI - run: | - sudo pip install setuptools - sudo pip install --upgrade b2 - name: Pack and publish package working-directory: ./nodejs run: | @@ -97,8 +93,6 @@ jobs: npm install rm -rf dist node node_modules/typescript/bin/tsc - b2 authorize-account ${{ secrets.CICD_CLI_B2_KEY_ID }} ${{ secrets.CICD_CLI_B2_APPLICATION_KEY }} - b2 sync b2://${{ secrets.CICD_CLI_B2_BUCKET_NAME }}/hyperbuild/bin/nodejs/${{ steps.version.outputs.VERSION }}/ ./dist/. cp ../README.md . if [[ "${{ steps.version.outputs.VERSION }}" != "0.0.0" ]]; then npm publish diff --git a/nodejs/build.sh b/nodejs/build.sh index 04ee65e..687b77e 100644 --- a/nodejs/build.sh +++ b/nodejs/build.sh @@ -1,9 +1,8 @@ #!/usr/bin/env bash -set -e +# Builds native module for current platform and Node.js version. -# Builds hyperbuild-nodejs with native module for local testing. -# Built package will only run current platform and Node.js version. +set -e pushd "$(dirname "$0")" @@ -12,18 +11,7 @@ npm install rm -rf dist npx tsc -if [ -f native/Cargo.toml.orig ]; then - echo 'Not altering Node.js Cargo.toml file' -else - cp native/Cargo.toml native/Cargo.toml.orig - # Don't use -i as macOS requires '' argument but then Ubuntu will treat as pattern. - sed 's%^hyperbuild = .*$%hyperbuild = { path = "../.." }%' native/Cargo.toml.orig > native/Cargo.toml -fi npx neon build --release -mv native/Cargo.toml.orig native/Cargo.toml - -binary_name="$(node -e 'console.log([process.platform, process.arch, process.versions.modules].join("__"))')" - -mv native/index.node "dist/$binary_name.node" +mv native/index.node "dist/native.node" popd diff --git a/nodejs/install.js b/nodejs/install.js new file mode 100644 index 0000000..5f13548 --- /dev/null +++ b/nodejs/install.js @@ -0,0 +1,59 @@ +const fs = require('fs'); +const https = require('https'); +const path = require('path'); +const pkg = require('./package.json'); + +const MAX_DOWNLOAD_ATTEMPTS = 4; + +const binaryName = [process.platform, process.arch, process.versions.modules].join('__'); +const binaryPath = path.join(__dirname, 'dist', 'native.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://wilsonl.in/hyperbuild/bin/nodejs/${pkg.version}/${binaryName}.node`); + } catch (e) { + if (e instanceof StatusError && attempt < MAX_DOWNLOAD_ATTEMPTS) { + await wait(Math.random() * 2500 + 500); + continue; + } + throw e; + } + + fs.writeFileSync(binaryPath, binary); + break; + } +}; + +if (!fs.existsSync(binaryPath)) { + downloadNativeBinary() + .then( + () => console.log(`Downloaded ${pkg.name}`), + err => { + console.error(`Failed to download ${pkg.name}: ${err}`); + process.exit(1); + }, + ); +} diff --git a/nodejs/native/Cargo.toml b/nodejs/native/Cargo.toml index 5442efe..8db4edf 100644 --- a/nodejs/native/Cargo.toml +++ b/nodejs/native/Cargo.toml @@ -16,5 +16,5 @@ crate-type = ["cdylib"] neon-build = "0.4.0" [dependencies] -hyperbuild = "0.2.3" +hyperbuild = { path = "../.." } neon = "0.4.0" diff --git a/nodejs/package.json b/nodejs/package.json index 9a3338a..48b9cb9 100644 --- a/nodejs/package.json +++ b/nodejs/package.json @@ -4,15 +4,16 @@ "description": "Fast allocation-less HTML minifier with smart whitespace handling", "main": "dist/index.js", "files": [ - "dist/**" + "dist/**", + "install.js" ], + "scripts": { + "postinstall": "node install.js" + }, "repository": { "type": "git", "url": "git+https://github.com/wilsonzlin/hyperbuild.git" }, - "engines": { - "node": ">=8 <=14" - }, "author": { "email": "npm@wilsonl.in", "name": "Wilson Lin", @@ -24,9 +25,9 @@ }, "homepage": "https://github.com/wilsonzlin/hyperbuild#readme", "devDependencies": { - "@types/node": "^13.1.8", - "neon-cli": "^0.3.3", - "typescript": "^3.7.5" + "@types/node": "^14.0.22", + "neon-cli": "^0.4.0", + "typescript": "^3.9.6" }, "keywords": [ "build", diff --git a/nodejs/src/index.ts b/nodejs/src/index.ts index 0d6e6a9..852fd53 100644 --- a/nodejs/src/index.ts +++ b/nodejs/src/index.ts @@ -1,6 +1,4 @@ -const binaryName = [process.platform, process.arch, process.versions.modules].join('__'); - -const hyperbuild = require(`./${binaryName}.node`); +const hyperbuild = require(`./native.node`); export type Configuration = { minifyJs: boolean;