|
| 1 | +const path = require('path'); |
| 2 | +const childProcess = require('child_process'); |
| 3 | +const https = require('https'); |
| 4 | +const fs = require('fs'); |
| 5 | + |
| 6 | +// /grpc-precompiled-binaries/node/grpc/v1.11.3/node-v64-darwin-x64-unknown |
| 7 | +const grpcPackageJson = require('grpc/package.json'); |
| 8 | + |
| 9 | +// Taken from https://github.com/mapbox/node-pre-gyp/blob/674fda7b28b86bb3b11889db86c15c4d06722d02/lib/util/versioning.js |
| 10 | +// so we can evaluate templates used for node-pre-gyp in grpc package |
| 11 | +function eval_template(template, opts) { |
| 12 | + Object.keys(opts).forEach(function(key) { |
| 13 | + var pattern = '{' + key + '}'; |
| 14 | + while (template.indexOf(pattern) > -1) { |
| 15 | + template = template.replace(pattern, opts[key]); |
| 16 | + } |
| 17 | + }); |
| 18 | + return template; |
| 19 | +} |
| 20 | + |
| 21 | +// This is taken from grpc package that uses node-pre-gyp to download or build native grpc core. |
| 22 | + |
| 23 | +// This matches abi version for node10 which is latest version pkg supports (as it is latest LTS) |
| 24 | +// see https://nodejs.org/en/download/releases/ |
| 25 | +const node_abi = 'node-v64'; |
| 26 | +const name = grpcPackageJson.name; |
| 27 | +const version = grpcPackageJson.version; |
| 28 | + |
| 29 | +const archString = process.argv[2]; |
| 30 | +const pluginDir = `plugin-${archString}`; |
| 31 | +// See https://console.cloud.google.com/storage/browser/node-precompiled-binaries.grpc.io/grpc/?project=grpc-testing |
| 32 | +// for existing prebuild binaries (though there are only ones for newer version). |
| 33 | +const [ |
| 34 | + // linux, darwin, win32 |
| 35 | + platform, |
| 36 | + // ia32, x64, arm, arm64 |
| 37 | + arch, |
| 38 | + // unknown, glibc, musl |
| 39 | + libc, |
| 40 | +] = archString.split('-'); |
| 41 | + |
| 42 | +const host = grpcPackageJson.binary.host; |
| 43 | +const remote_path = eval_template(grpcPackageJson.binary.remote_path, { name, version }); |
| 44 | +const package_name = eval_template(grpcPackageJson.binary.package_name, { node_abi, platform, arch, libc }); |
| 45 | +const url = host + path.join(remote_path, package_name); |
| 46 | + |
| 47 | +console.log(`Getting ${url}`); |
| 48 | +new Promise((resolve, reject) => { |
| 49 | + const file = fs.createWriteStream(`plugin-${archString}/grpc_node.tar.gz`); |
| 50 | + https |
| 51 | + .get(url, function(response) { |
| 52 | + if (response.statusCode !== 200) { |
| 53 | + const err = new Error(`response.statusCode = ${response.statusCode}`); |
| 54 | + err.response = response; |
| 55 | + reject(err); |
| 56 | + } |
| 57 | + response.pipe(file).on('finish', () => { |
| 58 | + resolve(); |
| 59 | + }); |
| 60 | + }) |
| 61 | + .on('error', e => reject(e)); |
| 62 | +}) |
| 63 | + .then(() => { |
| 64 | + console.log(`Grpc module downloaded`); |
| 65 | + const dirName = package_name.split('.')[0]; |
| 66 | + childProcess.execSync(`tar -xzf ${pluginDir}/grpc_node.tar.gz --directory ${pluginDir}`); |
| 67 | + childProcess.execSync(`mv ${pluginDir}/${dirName}/grpc_node.node ${pluginDir}/`); |
| 68 | + childProcess.execSync(`rm -rf ${pluginDir}/${dirName}`); |
| 69 | + childProcess.execSync(`rm -rf ${pluginDir}/grpc_node.tar.gz`); |
| 70 | + process.exit(0); |
| 71 | + }) |
| 72 | + .catch(err => { |
| 73 | + console.error(err); |
| 74 | + process.exit(1); |
| 75 | + }); |
0 commit comments