reveal-sdk-node
Version:
RevealBI Node.js SDK
56 lines (53 loc) • 2.25 kB
JavaScript
const fs = require('fs');
const https = require('https');
const { promisify } = require('node:util');
const { createGunzip } = require('node:zlib');
const { pipeline } = require('node:stream');
const pipe = promisify(pipeline);
const {
createReadStream,
createWriteStream
} = require('node:fs');
const path = require("path");
const netpath = require("./lib/netpath");
(async function install() {
if (fs.existsSync(`${__dirname}/tsconfig.json`)) return; // not meant for development.
const nativeLibDir = __dirname + path.sep + netpath.platformArchRelativePath;
const nativeLibPath = __dirname + path.sep + netpath.binaryRelativePath;
const version = process.env.npm_package_version;
const downloadSites = ["https://dl.infragistics.com", "https://download.infragistics.com"];
const downloadPath = `/reveal/Builds/sdk/node-binaries/v${version}/${netpath.platformArch}/${netpath.binaryName}.gz`;
fs.mkdirSync(nativeLibDir, { recursive: true });
const gzipFileName = `${nativeLibPath}.gz`;
try {
await download(downloadSites[0] + downloadPath, gzipFileName);
} catch (e) {
if (e.responseStatusCode == 404 || e.responseStatusCode == 502) {
await download(downloadSites[1] + downloadPath, gzipFileName);
} else {
throw e;
}
}
const source = createReadStream(gzipFileName);
const dest = createWriteStream(nativeLibPath);
await pipe(source, createGunzip(), dest);
await fs.promises.rm(gzipFileName);
})();
async function download(downloadUrl, gzipFileName) {
await new Promise((resolve, reject) => {
https.get(downloadUrl, function(response) {
if (response.statusCode != 200) {
const e = new Error(`Failed to download, status code ${response.statusCode}`);
e.responseStatusCode = response.statusCode;
reject(e);
} else {
const file = fs.createWriteStream(gzipFileName);
response.pipe(file);
file.on("finish", () => {
file.close();
resolve();
});
}
});
});
}