Use online platform binary downloader install script for Node.js instead of bundling all modules

This commit is contained in:
Wilson Lin 2020-07-11 22:02:43 +10:00
parent 5df34dd946
commit 82a0b8ecee
6 changed files with 72 additions and 32 deletions

View File

@ -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

View File

@ -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

59
nodejs/install.js Normal file
View File

@ -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);
},
);
}

View File

@ -16,5 +16,5 @@ crate-type = ["cdylib"]
neon-build = "0.4.0"
[dependencies]
hyperbuild = "0.2.3"
hyperbuild = { path = "../.." }
neon = "0.4.0"

View File

@ -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",

View File

@ -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;