|
| 1 | +/* eslint-disable no-console */ |
| 2 | +const os = require('os'); |
| 3 | +const path = require('path'); |
| 4 | +const { promises: fs, createWriteStream, createReadStream } = require('fs'); |
| 5 | +const { getDownloadURL } = require('mongodb-download-url'); |
| 6 | +const { pipeline } = require('stream'); |
| 7 | +const { promisify } = require('util'); |
| 8 | +const decompress = require('decompress'); |
| 9 | +const tar = require('tar'); |
| 10 | + |
| 11 | +const PACKAGE_ROOT = process.cwd(); |
| 12 | + |
| 13 | +// const MONOREPO_ROOT = path.resolve(__dirname, '..'); |
| 14 | + |
| 15 | +const CACHE_DIR = path.join(os.tmpdir(), '.compass-csfle-library-cache'); |
| 16 | + |
| 17 | +const fetch = require('make-fetch-happen').defaults({ |
| 18 | + cacheManager: CACHE_DIR |
| 19 | +}); |
| 20 | + |
| 21 | +const UPDATE_CACHE = process.argv.includes('--update-cache'); |
| 22 | + |
| 23 | +const CSFLE_DIRECTORY = path.resolve( |
| 24 | + PACKAGE_ROOT, |
| 25 | + 'src', |
| 26 | + 'deps', |
| 27 | + 'csfle' |
| 28 | +); |
| 29 | + |
| 30 | +const download = async(url, destDir) => { |
| 31 | + const destFileName = path.basename(url); |
| 32 | + const destFilePath = path.join(destDir, destFileName); |
| 33 | + |
| 34 | + const res = await fetch(url); |
| 35 | + |
| 36 | + // If cache item stored by `make-fetch-happen` is stale, it will check if |
| 37 | + // resource was not modified with the remote and can return a 304 with a body |
| 38 | + // present. In that case we don't want to throw an error, but rather proceed |
| 39 | + // with the normal flow |
| 40 | + if (!res.ok && res.status !== 304) { |
| 41 | + throw new Error(`Failed to fetch ${url}: ${res.statusText}`); |
| 42 | + } |
| 43 | + |
| 44 | + if (!UPDATE_CACHE) { |
| 45 | + await promisify(pipeline)(res.body, createWriteStream(destFilePath)); |
| 46 | + } |
| 47 | + |
| 48 | + return destFilePath; |
| 49 | +}; |
| 50 | + |
| 51 | +(async() => { |
| 52 | + const packageJson = require(path.join(PACKAGE_ROOT, 'package.json')); |
| 53 | + |
| 54 | + if (UPDATE_CACHE) { |
| 55 | + console.log('Re-populating csfle library cache at %s', CACHE_DIR); |
| 56 | + |
| 57 | + try { |
| 58 | + await fs.rmdir(CACHE_DIR, { recursive: true }); |
| 59 | + } catch (e) { /* ignore */ } |
| 60 | + } else { |
| 61 | + console.log( |
| 62 | + 'Downloading csfle library for package %s', |
| 63 | + packageJson.name |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + const downloadOptions = { |
| 68 | + enterprise: true, |
| 69 | + csfle: true |
| 70 | + }; |
| 71 | + if (process.platform === 'linux') { |
| 72 | + // The CSFLE shared library is built for different distros, |
| 73 | + // but since it only depends on glibc, we can just download |
| 74 | + // a CSFLE library from a distro with a low glibc version |
| 75 | + // such as RHEL7. |
| 76 | + downloadOptions.distro = 'rhel70'; |
| 77 | + } |
| 78 | + let artifactInfo; |
| 79 | + // Try getting the latest stable csfle library, if none exists |
| 80 | + // (which is the case at the time of writing), fall back to |
| 81 | + // a 6.0 rc candidate. We can remove this try/catch after 6.0.0. |
| 82 | + try { |
| 83 | + artifactInfo = await getDownloadURL(downloadOptions); |
| 84 | + } catch { |
| 85 | + artifactInfo = await getDownloadURL({ |
| 86 | + ...downloadOptions, |
| 87 | + version: '>= 6.0.0-rc4' |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + console.log('Downloading csfle artifact', artifactInfo, 'to', CSFLE_DIRECTORY); |
| 92 | + await fs.mkdir(CSFLE_DIRECTORY, { recursive: true }); |
| 93 | + const artifactPath = await download(artifactInfo.url, CSFLE_DIRECTORY); |
| 94 | + |
| 95 | + if (artifactInfo.ext === 'zip') { |
| 96 | + // For .zip files, use `decompress` |
| 97 | + await decompress(artifactPath, CSFLE_DIRECTORY); |
| 98 | + } else { |
| 99 | + // For .tar files, `decompress` is buggy, so we use `tar` instead |
| 100 | + await promisify(pipeline)( |
| 101 | + createReadStream(artifactPath), |
| 102 | + tar.x({ |
| 103 | + C: CSFLE_DIRECTORY |
| 104 | + })); |
| 105 | + } |
| 106 | +})().catch((err) => { |
| 107 | + if (err) { |
| 108 | + console.error(err); |
| 109 | + } |
| 110 | + |
| 111 | + process.exit(1); |
| 112 | +}); |
0 commit comments