From 4d549f4c8e4ccd2c9199af95b5c87262fcc4023a Mon Sep 17 00:00:00 2001 From: Diogo Silva Date: Fri, 14 Jun 2019 16:25:45 +0100 Subject: [PATCH] feat: use ipfs-provider --- README.md | 4 +++- package.json | 2 +- src/cli/bin.js | 5 +++++ src/core/commands/start-ipfs.js | 30 ++++++++++++++++++++++++++++-- src/core/config.js | 1 + 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6d1ceb86..62a1c6b9 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ As the largest software registry in the world, [npm](https://www.npmjs.com) is a The result is npm-on-ipfs: a module that wraps your package manager of choice (npm or yarn) in configuration to use [IPFS](https://ipfs.io/), not HTTP, to retrieve your dependencies from the central npm registry. It's still a work in progress, but we think you'll find it useful and awesome for the following reasons: - Having dependencies on the distributed web makes development **more available** because multiple nodes supplying tarballs means no panic if a single source goes dark - - It can also be **faster and cheaper** — if dependencies are already being hosted on your local network, this means lower bandwidth cost and higher speed + - It can also be **faster and cheaper** — if dependencies are already being hosted on your local network, this means lower bandwidth cost and higher speed - If enough dependencies are hosted on your local network (think enterprise or community development settings), that network can operate **offline-first**: Take your team on a remote mountain retreat and hack away! ## Install & use @@ -85,6 +85,8 @@ Options: timing out [default: 5000] --ipfs-mfs-prefix Which mfs prefix to use [default: "/npm-registry"] + --ipfs-disable-providers Wether to disable the search for running nodes + [default: false] --ipfs-node "proc" to start an in-process IPFS node, "disposable" to start an in-process disposable node, "go" or "js" to spawn an IPFS node as a diff --git a/package.json b/package.json index 4ea5c188..9ff94896 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "express": "^4.16.4", "express-http-proxy": "^1.5.1", "ipfs": "^0.36.3", - "ipfs-http-client": "^32.0.1", + "ipfs-provider": "^0.2.1", "ipfs-registry-mirror-common": "^1.0.13", "ipfsd-ctl": "~0.42.2", "once": "^1.4.0", diff --git a/src/cli/bin.js b/src/cli/bin.js index 0f3348f2..b6828b90 100755 --- a/src/cli/bin.js +++ b/src/cli/bin.js @@ -46,6 +46,11 @@ yargs.command('$0', 'Installs your js dependencies using IPFS', (yargs) => { // describe: 'Which mfs prefix to use', default: '/npm-registry' }) + .option('ipfs-disable-providers', { + describe: 'Whether to disable the search for running nodes', + type: 'boolean', + default: false + }) .option('ipfs-node', { describe: '"proc" to start an in-process IPFS node, "disposable" to start an in-process disposable node, "go" or "js" to spawn an IPFS node as a separate process or a multiaddr that resolves to a running node', default: 'proc' diff --git a/src/core/commands/start-ipfs.js b/src/core/commands/start-ipfs.js index 79768c74..90ba297c 100644 --- a/src/core/commands/start-ipfs.js +++ b/src/core/commands/start-ipfs.js @@ -1,7 +1,7 @@ 'use strict' -const IpfsApi = require('ipfs-http-client') const ipfsdCtrl = require('ipfsd-ctl') +const getIpfs = require('ipfs-provider') const which = require('which-promise') const promisify = require('util').promisify const request = require('ipfs-registry-mirror-common/utils/retry-request') @@ -36,6 +36,26 @@ const spawn = (createArgs, spawnArgs = { init: true }) => { } const startIpfs = async (config) => { + if (!config.ipfs.disableProviders) { + console.info('🔎 Searching for a running node') // eslint-disable-line no-console + + try { + const provider = await getIpfs({ + tryWebExt: false, + tryWindow: false + }) + + console.info(`😈 Connecting to an ${provider.provider} node`) // eslint-disable-line no-console + + return { + api: provider.ipfs, + stop: (cb) => cb() + } + } catch (e) { + console.info('💥 Couldn\'t find an available node') // eslint-disable-line no-console + } + } + if (config.ipfs.node === 'proc') { console.info(`😈 Spawning an in-process IPFS node using repo at ${config.ipfs.repo}`) // eslint-disable-line no-console @@ -82,8 +102,14 @@ const startIpfs = async (config) => { console.info(`😈 Connecting to a remote IPFS node at ${config.ipfs.node}`) // eslint-disable-line no-console + const provider = await getIpfs({ + tryWebExt: false, + tryWindow: false, + apiAddress: config.ipfs.node + }) + return { - api: new IpfsApi(config.ipfs.node), + api: provider.ipfs, stop: (cb) => cb() } } diff --git a/src/core/config.js b/src/core/config.js index 0a58ad44..9c5cafdb 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -15,6 +15,7 @@ module.exports = (overrides = {}) => { ipfs: { host: option(process.env.IPFS_HOST, overrides.ipfsHost), port: option(Number(process.env.IPFS_PORT), overrides.ipfsPort), + disableProviders: option(process.env.IPFS_DISABLE_PROVIDERS, overrides.ipfsDisableProviders), node: option(process.env.IPFS_NODE, overrides.ipfsNode), prefix: option(process.env.IPFS_MFS_PREFIX, overrides.ipfsMfsPrefix), flush: option(process.env.IPFS_FLUSH, overrides.ipfsFlush),