-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ChainSafe/publish
Add install scripts
- Loading branch information
Showing
14 changed files
with
321 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ node_modules | |
prebuilds | ||
|
||
npm-debug.log | ||
yarn-error.log | ||
yarn-error.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
#!/bin/sh -e | ||
|
||
BLST_WRAP_PREBUILD=blst_wrap.cpp | ||
BLST_WRAP_OUTPUT=blst/bindings/node.js | ||
BLST_NODE_OUTPUT=blst/bindings/node.js/blst.node | ||
BLST_NODE_TARGET=dist/blst.node | ||
|
||
cp $BLST_WRAP_PREBUILD $BLST_WRAP_OUTPUT | ||
# Copy SWIG prebuilt | ||
cp blst_wrap.cpp blst/bindings/node.js | ||
|
||
(cd blst/bindings/node.js; ./run.me) | ||
# Build node bindings | ||
./blst/bindings/node.js/run.me | ||
|
||
mkdir -p `dirname $BLST_NODE_TARGET` | ||
cp $BLST_NODE_OUTPUT $BLST_NODE_TARGET |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./lib"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import fs from "fs"; | ||
import { exec } from "child_process"; | ||
import { testBindings } from "./testBindings"; | ||
import { | ||
bindingsDirSrc, | ||
bindingsSrc, | ||
ensureDirFromFilepath, | ||
prebuiltSwigSrc, | ||
prebuiltSwigTarget, | ||
} from "./paths"; | ||
|
||
export async function buildBindings(binaryPath: string) { | ||
// Copy SWIG prebuilt | ||
fs.copyFileSync(prebuiltSwigSrc, prebuiltSwigTarget); | ||
|
||
// Use BLST run.me script to build libblst.a + blst.node | ||
await new Promise((resolve, reject): void => { | ||
const proc = exec( | ||
"./run.me", | ||
{ | ||
timeout: 3 * 60 * 1000, // ms | ||
maxBuffer: 10e6, // bytes | ||
cwd: bindingsDirSrc, | ||
}, | ||
(err, stdout, stderr) => { | ||
if (err) reject(err); | ||
else resolve(stdout.trim() || stderr); | ||
} | ||
); | ||
if (proc.stdout) proc.stdout.pipe(process.stdout); | ||
if (proc.stderr) proc.stderr.pipe(process.stderr); | ||
}); | ||
|
||
// Copy built .node file to expected path | ||
ensureDirFromFilepath(binaryPath); | ||
fs.copyFileSync(bindingsSrc, binaryPath); | ||
|
||
// Make sure downloaded bindings work | ||
await testBindings(binaryPath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { testBindings } from "./testBindings"; | ||
import { download } from "./downloadFile"; | ||
import { ensureDirFromFilepath, getBinaryName, packageJsonPath } from "./paths"; | ||
|
||
const githubReleasesDownloadUrl = | ||
"https://github.com/ChainSafe/blst-ts/releases/download"; | ||
|
||
export async function checkAndDownloadBinary(binaryPath: string) { | ||
const packageJson = require(packageJsonPath); | ||
const binaryName = getBinaryName(); | ||
const version = packageJson.version; | ||
|
||
const binaryUrl = `${githubReleasesDownloadUrl}/v${version}/${binaryName}`; | ||
|
||
ensureDirFromFilepath(binaryPath); | ||
await download(binaryUrl, binaryPath); | ||
|
||
// Make sure downloaded bindings work | ||
await testBindings(binaryPath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import fs from "fs"; | ||
import https from "https"; | ||
|
||
export class HttpError extends Error { | ||
statusCode?: number; | ||
constructor(message: string, statusCode?: number) { | ||
super(message); | ||
this.statusCode = statusCode; | ||
} | ||
} | ||
|
||
/** | ||
* Downloads file from remote HTTP[S] host and puts its contents to the | ||
* specified location. | ||
*/ | ||
export function download(url: string, filePath: string): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
const file = fs.createWriteStream(filePath); | ||
|
||
const request = https.get(url, (response) => { | ||
if (response.statusCode !== 200) { | ||
reject( | ||
new HttpError( | ||
`Failed to get '${url}' (${response.statusCode})`, | ||
response.statusCode | ||
) | ||
); | ||
return; | ||
} | ||
|
||
response.pipe(file); | ||
}); | ||
|
||
// The destination stream is ended by the time it's called | ||
file.on("finish", () => resolve()); | ||
|
||
request.on("error", (err) => { | ||
fs.unlink(filePath, () => reject(err)); | ||
}); | ||
|
||
file.on("error", (err) => { | ||
fs.unlink(filePath, () => reject(err)); | ||
}); | ||
|
||
request.end(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import fs from "fs"; | ||
import { checkAndDownloadBinary } from "./downloadBindings"; | ||
import { buildBindings } from "./buildBindings"; | ||
import { getBinaryPath } from "./paths"; | ||
import { testBindings } from "./testBindings"; | ||
|
||
const libName = "BLST native bindings"; | ||
|
||
// CLI runner | ||
install().then( | ||
() => process.exit(0), | ||
(e) => { | ||
console.log(e.stack); | ||
process.exit(1); | ||
} | ||
); | ||
|
||
async function install() { | ||
const binaryPath = getBinaryPath(); | ||
|
||
// Check if bindings already downloaded or built | ||
if (fs.existsSync(binaryPath)) { | ||
try { | ||
await testBindings(binaryPath); | ||
console.log(`Using existing ${libName} from ${binaryPath}`); | ||
return; | ||
} catch (e) { | ||
console.log(`Cached ${libName} not OK`); | ||
} | ||
} | ||
|
||
// Fetch pre-built bindings from remote repo | ||
try { | ||
console.log(`Retrieving ${libName}...`); | ||
await checkAndDownloadBinary(binaryPath); | ||
await testBindings(binaryPath); | ||
console.log(`Successfully retrieved ${libName}`); | ||
return; | ||
} catch (e) { | ||
if (e.statusCode === 404) { | ||
console.error(`${libName} not available: ${e.message}`); | ||
} else { | ||
console.error(`Error importing ${libName}: ${e.stack}`); | ||
} | ||
} | ||
|
||
// Build bindings locally from source | ||
try { | ||
console.log(`Building ${libName} from source...`); | ||
await buildBindings(binaryPath); | ||
await testBindings(binaryPath); | ||
console.log(`Successfully built ${libName} from source`); | ||
return; | ||
} catch (e) { | ||
console.error(`Error building ${libName}: ${e.stack}`); | ||
} | ||
|
||
// Fallback? | ||
throw Error(`Error downloading and building ${libName}. No fallback`); | ||
} |
Oops, something went wrong.