Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions src/desktop/index.js
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions src/desktop/index.test.js
Original file line number Diff line number Diff line change
@@ -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()
})
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down