From 6cc476f860302c74e0c101a9efd47f83e9cadd20 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Fri, 17 May 2024 15:03:12 +0200 Subject: [PATCH] feat: expose standalone download function (#73) This allows downloading binaries for more than one platform. We want to use this in ipfs-desktop, to download both amd64 and arm64 binaries required for building universal DMGs. Exposing download function here allows us to 1. leverage cached downloads, avoid fetching same arch twice 2. confirm download by checkign sha512 --- src/download.js | 12 +++++++++++- src/go-platform.js | 25 ++++++++++++------------- src/index.js | 3 +++ src/post-install.js | 4 ++-- test/download.js | 35 +++++++++++++++++++++++++++++------ 5 files changed, 57 insertions(+), 22 deletions(-) diff --git a/src/download.js b/src/download.js index 0c04e8d..70b8f92 100644 --- a/src/download.js +++ b/src/download.js @@ -236,13 +236,23 @@ async function link ({ depBin, version }) { return localBin } +/** +* @param {object} options +* @param {string} options.version +* @param {string} options.platform +* @param {string} options.arch +* @param {string} options.installPath +* @param {string} options.distUrl +*/ +module.exports.download = download + /** * @param {string} [version] * @param {string} [platform] * @param {string} [arch] * @param {string} [installPath] */ -module.exports = async (version, platform, arch, installPath) => { +module.exports.downloadAndUpdateBin = async (version, platform, arch, installPath) => { const args = cleanArguments(version, platform, arch, installPath) return link({ diff --git a/src/go-platform.js b/src/go-platform.js index a600766..426cc45 100644 --- a/src/go-platform.js +++ b/src/go-platform.js @@ -2,10 +2,10 @@ function getGoOs () { switch (process.platform) { - case "sunos": - return "solaris" - case "win32": - return "windows" + case 'sunos': + return 'solaris' + case 'win32': + return 'windows' } return process.platform @@ -13,20 +13,19 @@ function getGoOs () { function getGoArch () { switch (process.arch) { - case "ia32": - return "386" - case "x64": - return "amd64" - case "arm": - return "arm" - case "arm64": - return "arm64" + case 'ia32': + return '386' + case 'x64': + return 'amd64' + case 'arm': + return 'arm' + case 'arm64': + return 'arm64' } return process.arch } - module.exports = { GOOS: getGoOs(), GOARCH: getGoArch() diff --git a/src/index.js b/src/index.js index 1ec6f6a..3475090 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,9 @@ const fs = require('fs') const path = require('path') +const { download } = require('./download') + +module.exports.download = download module.exports.path = function () { if (process.env.KUBO_BINARY) { diff --git a/src/post-install.js b/src/post-install.js index 0a86c03..1696ad8 100644 --- a/src/post-install.js +++ b/src/post-install.js @@ -1,8 +1,8 @@ 'use strict' -const download = require('./download') +const { downloadAndUpdateBin } = require('./download') -download() +downloadAndUpdateBin() .catch(err => { console.error(err) process.exit(1) diff --git a/test/download.js b/test/download.js index a4ea682..a9c34a0 100644 --- a/test/download.js +++ b/test/download.js @@ -2,14 +2,16 @@ const test = require('tape-promise').default(require('tape')) const fs = require('fs-extra') -const download = require('../src/download') +const os = require('os') +const path= require('path') +const { download, downloadAndUpdateBin } = require('../src/download') const { path: detectLocation } = require('../') const clean = require('./fixtures/clean') test('Ensure ipfs gets downloaded (current version and platform)', async (t) => { await clean() - const installPath = await download() + const installPath = await downloadAndUpdateBin() const stats = await fs.stat(installPath) t.ok(stats, 'kubo was downloaded') @@ -21,7 +23,7 @@ test('Ensure ipfs gets downloaded (current version and platform)', async (t) => test('Returns an error when version unsupported', async (t) => { await clean() - await t.rejects(download('bogusversion', 'linux'), /Error: Version 'bogusversion' not available/) + await t.rejects(downloadAndUpdateBin('bogusversion', 'linux'), /Error: Version 'bogusversion' not available/) t.end() }) @@ -31,7 +33,7 @@ test('Returns an error when KUBO_DIST_URL is 404', async (t) => { process.env.KUBO_DIST_URL = 'https://dist.ipfs.tech/notfound' - await t.rejects(download(), /404/) + await t.rejects(downloadAndUpdateBin(), /404/) delete process.env.KUBO_DIST_URL @@ -43,14 +45,13 @@ test('Returns an error when legacy GO_IPFS_DIST_URL is 404', async (t) => { process.env.GO_IPFS_DIST_URL = 'https://dist.ipfs.tech/notfound' - await t.rejects(download(), /404/) + await t.rejects(downloadAndUpdateBin(), /404/) delete process.env.GO_IPFS_DIST_URL t.end() }) - test('Path returns undefined when no binary has been downloaded', async (t) => { await clean() @@ -58,3 +59,25 @@ test('Path returns undefined when no binary has been downloaded', async (t) => { t.end() }) + +test('Ensure calling download function manually with static values works', async (t) => { + await clean() + + const { version } = require('../package.json') + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'temp-dir-')) + + console.log(tempDir) + const kuboPath = await download({ + version: `v${version}`, + platform: 'darwin', + arch: 'arm64', + distUrl: 'https://dist.ipfs.tech', + installPath: tempDir, + }) + console.log(kuboPath) + const stats = await fs.stat(kuboPath) + + t.ok(stats, 'kubo was downloaded to installPath') + + t.end() +})