Skip to content

Commit

Permalink
feat: expose standalone download function (#73)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lidel authored May 17, 2024
1 parent a115dbe commit 6cc476f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 22 deletions.
12 changes: 11 additions & 1 deletion src/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
25 changes: 12 additions & 13 deletions src/go-platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,30 @@

function getGoOs () {
switch (process.platform) {
case "sunos":
return "solaris"
case "win32":
return "windows"
case 'sunos':
return 'solaris'
case 'win32':
return 'windows'
}

return process.platform
}

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()
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/post-install.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const download = require('./download')
const { downloadAndUpdateBin } = require('./download')

download()
downloadAndUpdateBin()
.catch(err => {
console.error(err)
process.exit(1)
Expand Down
35 changes: 29 additions & 6 deletions test/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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()
})
Expand All @@ -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

Expand All @@ -43,18 +45,39 @@ 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()

t.throws(detectLocation, /not found/, 'Path throws if binary is not installed')

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()
})

0 comments on commit 6cc476f

Please sign in to comment.