|
| 1 | +const fs = require('fs'); |
| 2 | +const fse = require('fs-extra'); |
| 3 | +const path = require('path'); |
| 4 | +const R = require('ramda'); |
| 5 | +const request = require('request-promise-native'); |
| 6 | +const stream = require('stream'); |
| 7 | +const tar = require('tar-fs'); |
| 8 | +const zlib = require('zlib'); |
| 9 | + |
| 10 | +const vendorPath = path.resolve('..', 'vendor'); |
| 11 | +const distrosFilePath = path.join(vendorPath, 'openssl_distributions.json'); |
| 12 | +const extractPath = path.join(vendorPath, 'openssl'); |
| 13 | + |
| 14 | +const getOSName = () => { |
| 15 | + if (process.platform === 'win32') { |
| 16 | + if (process.arch === "x64" || process.env.hasOwnProperty("PROCESSOR_ARCHITEW6432")) { |
| 17 | + return 'win64'; |
| 18 | + } else { |
| 19 | + return 'win32'; |
| 20 | + } |
| 21 | + } else if (process.platform === 'darwin') { |
| 22 | + return 'macOS'; |
| 23 | + } else { |
| 24 | + // We only discover distros for Mac and Windows. We don't care about any other OS. |
| 25 | + return 'unknown'; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +const getCompilerVersion = () => { |
| 30 | + // TODO: Get actual compiler version. For now, just assume latest compiler for distros in openssl_distributions.js |
| 31 | + const osName = getOSName(); |
| 32 | + if (osName === 'win32' || osName === 'win64') { |
| 33 | + return 'vs15'; |
| 34 | + } else if (osName === 'macOS') { |
| 35 | + return 'clang-9'; |
| 36 | + } else { |
| 37 | + // We only discover distros for Mac and Windows. We don't care about any other OS. |
| 38 | + return 'unknown'; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +const getIsDebug = () => true // TODO: Determine if we are GYPing in Debug |
| 43 | + |
| 44 | +const getMatchingDistributionName = () => |
| 45 | + `${getOSName()}-${getCompilerVersion()}-static${getIsDebug() ? '-debug' : ''}`; |
| 46 | + |
| 47 | +const getDistributionsConfig = () => |
| 48 | + fse.readFile(distrosFilePath, 'utf8') |
| 49 | + .then(JSON.parse) |
| 50 | + |
| 51 | +const getDistrbutionURLFromConfig = (config) => { |
| 52 | + const distName = getMatchingDistributionName(); |
| 53 | + const distURL = R.propOr(null, distName, config); |
| 54 | + |
| 55 | + if (!distURL) { |
| 56 | + return Promise.reject(new Error('No matching distribution for this operating system')); |
| 57 | + } |
| 58 | + return Promise.resolve(distURL); |
| 59 | +} |
| 60 | + |
| 61 | +const fetchFileFromURL = (distUrl) => request({ |
| 62 | + method: 'GET', |
| 63 | + uri: distUrl, |
| 64 | + encoding: null, |
| 65 | + gzip: true |
| 66 | +}); |
| 67 | + |
| 68 | +const extractFile = (body) => new Promise((resolve, reject) => { |
| 69 | + const streamableBody = new stream.Readable(); |
| 70 | + streamableBody.push(body); |
| 71 | + streamableBody.push(null); |
| 72 | + streamableBody |
| 73 | + .pipe(zlib.createGunzip()) |
| 74 | + .on('error', reject) |
| 75 | + .pipe(tar.extract(extractPath)) |
| 76 | + .on('error', reject) |
| 77 | + .on('close', resolve); |
| 78 | +}); |
| 79 | + |
| 80 | +const acquireOpenSSL = () => |
| 81 | + getDistributionsConfig() |
| 82 | + .then(getDistrbutionURLFromConfig) |
| 83 | + .then(fetchFileFromURL) |
| 84 | + .then(extractFile) |
| 85 | + .catch((e) => { |
| 86 | + console.error(e); |
| 87 | + process.exit(1); |
| 88 | + }); |
| 89 | + |
| 90 | +acquireOpenSSL(); |
0 commit comments