diff --git a/README.md b/README.md index 04f1977..6eaf20b 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ This module tries to connect to IPFS via multiple providers, in order: - `ipfs-companion` the IPFS instance from [IPFS Companion](https://github.com/ipfs-shipyard/ipfs-companion) directly. - `window.ipfs` in the current page via [IPFS Companion](https://github.com/ipfs-shipyard/ipfs-companion). +- `ipfs-desktop` if it's running inside [IPFS Desktop](https://github.com/ipfs-shipyard/ipfs-desktop). - `js-ipfs-api` with either a user provided `apiAddress`, the current origin, or the default `/ip4/127.0.0.1/tcp/5001` address. - `js-ipfs` **disabled by default**. Pass `tryJsIpfs: true, getJsIpfs: () => Promise` to enable it. See [Enable js-ipfs](#enable-js-ipfs) @@ -29,6 +30,7 @@ export default composeBundles( // These are the defaults: tryCompanion: true, // set false to bypass ipfs-companion verification tryWindow: true, // set false to bypass window.ipfs verification + tryDesktop: true, // set false to bypass ipfs-desktop verification tryApi: true, // set false to bypass js-ipfs-api verification. Uses data from ipfsApi variable in localStorage tryJsIpfs: false, // set true to attempt js-ipfs initialisation. getJsIpfs: null // must be set to a js-ipfs instance if tryJsIpfs is true. diff --git a/src/desktop/index.js b/src/desktop/index.js new file mode 100644 index 0000000..089e321 --- /dev/null +++ b/src/desktop/index.js @@ -0,0 +1,18 @@ +const provider = 'ipfs-desktop' + +async function tryDesktop ({ root, ipfsConnectionTest }) { + console.log('Trying IPFS Desktop') + if (root.ipfsDesktopApi) { + try { + await ipfsConnectionTest(root.ipfsDesktopApi) + console.log('Found IPFS Desktop. Nice!') + return { ipfs: root.ipfsDesktopApi, provider } + } catch (error) { + console.log('Failed to connect via IPFS Desktop', error) + } + } else { + console.log('IPFS Desktop not found.') + } +} + +module.exports = tryDesktop diff --git a/src/desktop/index.test.js b/src/desktop/index.test.js new file mode 100644 index 0000000..7a88a2f --- /dev/null +++ b/src/desktop/index.test.js @@ -0,0 +1,16 @@ +/* global it, expect, jest */ +const tryDesktop = require('./index.js') + +it('Should connect to IPFS Desktop', async (done) => { + const opts = { + root: { + ipfsDesktopApi: {} + }, + ipfsConnectionTest: jest.fn().mockResolvedValueOnce(true) + } + const res = await tryDesktop(opts) + expect(res.ipfs).toEqual(opts.root.ipfsDesktopApi) + expect(res.provider).toEqual('ipfs-desktop') + expect(opts.ipfsConnectionTest.mock.calls.length).toBe(1) + done() +}) diff --git a/src/index.js b/src/index.js index 0284682..aa4c4d3 100644 --- a/src/index.js +++ b/src/index.js @@ -6,12 +6,14 @@ const IpfsApi = require('ipfs-api') const multiaddr = require('multiaddr') const tryCompanion = require('./companion') const tryWindow = require('./window.ipfs') +const tryDesktop = require('./desktop') const tryApi = require('./js-ipfs-api') const tryJsIpfs = require('./js-ipfs') const defaultOptions = { tryWindow: true, tryCompanion: true, + tryDesktop: true, tryApi: true, tryJsIpfs: false, defaultApiAddress: '/ip4/127.0.0.1/tcp/5001', @@ -150,6 +152,12 @@ async function getIpfs (opts, { store, getState, dispatch }) { return dispatch({ type: 'IPFS_INIT_FINISHED', payload: res }) } } + if (opts.tryDesktop) { + const res = await tryDesktop({ root, ipfsConnectionTest }) + if (res) { + return dispatch({ type: 'IPFS_INIT_FINISHED', payload: res }) + } + } if (opts.tryApi) { const { apiAddress, defaultApiAddress } = getState().ipfs const { location } = root