Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

feat: try more ways until failing & store provider info #5

Merged
merged 18 commits into from
Sep 17, 2018

Conversation

hacdias
Copy link
Contributor

@hacdias hacdias commented Sep 10, 2018

index.js Outdated
@@ -43,15 +29,15 @@ module.exports = () => {
}

if (type === 'IPFS_INIT_FINISHED') {
return Object.assign({}, state, { ready: true, identity: payload })
return Object.assign({}, state, { ready: true, ...payload })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can use spread without adding a transpilation step to this repo

Copy link
Contributor Author

@hacdias hacdias Sep 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@olizilla it should work after 8.6.0 (https://node.green/#ES2018-features-object-rest-spread-properties) so I think it's okay.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm more thinking that we are publishing this as an npm module, and it's worth thinking about how people might want to consume it.

It's intended to be used in browsers rather than node, so it needs some thought.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@olizilla okay. I'll change that then 😄 I just saw the compatibility table on MDN and I really thought Edge and Safari worked with object spread but they don't!

Copy link
Collaborator

@olizilla olizilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests. I'd like to see support setting ipfsApi via the url query as a separate PR; it's an unrelated issue, and it's worth discussion as it gives more power to the link author than is normal.

@hacdias
Copy link
Contributor Author

hacdias commented Sep 12, 2018

@olizilla yeah, I just didn't add tests before to make sure I wasn't spending time with functionalities I'll not add here (link the link thingy).

License: MIT
Signed-off-by: Henrique Dias <[email protected]>
License: MIT
Signed-off-by: Henrique Dias <[email protected]>
License: MIT
Signed-off-by: Henrique Dias <[email protected]>
License: MIT
Signed-off-by: Henrique Dias <[email protected]>
License: MIT
Signed-off-by: Henrique Dias <[email protected]>
@hacdias
Copy link
Contributor Author

hacdias commented Sep 12, 2018

@olizilla this is a breaking change, as it removes selectIpfsApiAddress and replaces it by selectIpfsApiOpts. The same for doUpdateIpfsAPIAddress.

It also introduces js-ipfs-api as a dependency.
If the user wants to use js-ipfs, it is their responsibility to pass an instance to the bundle.

License: MIT
Signed-off-by: Henrique Dias <[email protected]>
@hacdias hacdias changed the title wip: try more ways until failing & store provider info feat(BREAKING CHANGE): try more ways until failing & store provider info Sep 12, 2018
License: MIT
Signed-off-by: Henrique Dias <[email protected]>
Copy link
Collaborator

@olizilla olizilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting there. A few tweaks and some more tests needed.

We can avoid the breaking change by offering a selectIpfsApi method that converts the apiOprs into a multiaddr, and we can do the same for the doUpdateIpfsApi, which takes a multiaddr but stores it as an opts object. That'd be nice as that's the api the UI is using currently.

README.md Outdated
// These are the defaults:
tryWindow: true, // set false to bypass window.ipfs verification
tryApi: true, // set false to bypass js-ipfs-api verification. Uses data from ipfsApi variable in localStorage
tryJsIpfs: false, // set false to bypass js-ipfs-api verification. Uses data from ipfsOpts variable in localStorage
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"set true to attempt js-ipfs initialisation"

index.js Outdated
async function getIpfs (opts = {}, { getState, dispatch }) {
dispatch({ type: 'IPFS_INIT_STARTED' })

const leave = (provider, res, apiOpts) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took me a while to figure out what leave meant in this case. Somthing like dispatchInitFinished would make it more obvious what this is for.

README.md Outdated

- Updates the API Address to `address`.
- Updates the API Options to `opts`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a warning here that calling this will dispose of any existing ipfs instance and will only try to init an js-ipfs-api instance, and it won't try window.ipfs or js-ipfs.

index.js Outdated
let identity = null
doUpdateIpfsApiOpts: (usrOpts) => (store) => {
store.dispatch({ type: 'IPFS_API_UPDATED', payload: usrOpts })
saveUserOpts('ipfsApi', usrOpts)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actions tell us that something just happened, so the call to saveUserOpts should come before the dispatch, as the api isn't really updated until it's been stored.

Now we're dealing with an api opts object the action would be more accurately called IPFS_API_OPTS_UPDATED

index.js Outdated
console.log('Trying ipfs-api', opts)

console.info('🎛️ Customise your js-ipfs-api options by setting:')
console.info('\t1. an address in the URL with `ipfsApi` param. e.g. ?ipfsApi=/ip4/127.0.0.1/tcp/5001')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's simplify this back down to "store an opts object". It gets messy having to deal with both types.

index.js Outdated

async function tryApi (opts) {
try {
console.time('IPFS_INIT_API')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got carried away with the logging here, we can merge the timer logs and the "ready!" console message into one , by removing console.time('IPFS_INIT_API') and doing:

console.time('js-ipfs-api ready!')
// ... stuff and magic
console.timeEnd('js-ipfs-api ready!')

index.js Outdated
console.info('🎛️ Customise your js-ipfs opts by setting an `ipfsOpts` value in localStorage. e.g. localStorage.setItem(\'ipfsOpts\', JSON.stringify({relay: {enabled: true}}))')
const ipfs = await initJsIpfs(Ipfs, opts)
const identity = await ipfs.id()
console.log('js-ipfs ready!')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the console.log / console.time issue in tryApi

index.test.js Outdated
const ipfsBundle = require('./index')

jest.mock('window.ipfs-fallback')
it('Should fail when connecting via window.ipfs', (done) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should fail to connect via window.ipfs when missing

index.test.js Outdated
store.doInitIpfs()
})

it('Should success when connecting via window.ipfs', (done) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should connect via window.ipfs

index.test.js Outdated
@@ -50,7 +68,7 @@ it('should initialise IPFS API by accessing it directly when running inside of i
}

const store = composeBundlesRaw(
ipfsBundle()
ipfsBundle({ tryApi: false, tryJsIpfs: false })
)()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests for

  • Should connect via js-ipfs-api
  • Should connect via js-ipfs
  • Should connect via js-ipfs-api when window.ipfs present but disabled by options
  • Should connect via js-ipfs-api when window.ipfs present but fails
  • Should connect via js-ipfs instead of window.ipfs or js-ipfs-api

License: MIT
Signed-off-by: Henrique Dias <[email protected]>
License: MIT
Signed-off-by: Henrique Dias <[email protected]>
License: MIT
Signed-off-by: Henrique Dias <[email protected]>
License: MIT
Signed-off-by: Henrique Dias <[email protected]>
License: MIT
Signed-off-by: Henrique Dias <[email protected]>
@hacdias
Copy link
Contributor Author

hacdias commented Sep 14, 2018

@olizilla apparently Travis liked the tests 😄

store.subscribeToSelectors(['selectIpfsReady'], () => {
if (first) {
expect(store.selectIpfsReady()).toBe(true)
expect(store.selectIpfsIdentity()).not.toEqual(testIdentity)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing that the identity is not equal to the test one doesn't tell us very much. It'd be useful to test for the value of selectIpfsProvider is set correctly in all these cases.

@olizilla
Copy link
Collaborator

How about making this not a breaking change by pulling the logic you have in https://github.com/ipfs-shipyard/ipfs-webui/pull/792/files#diff-1d5a0de57bad3136d70286a7887c1a8bR20 to map between the object form and the multi addr form into this bundle.

We can store the info as an object and offer a selector that formats the object as a multiaddr, and a action creator that lets the user update the api opts object by specifying a multiaddr.

License: MIT
Signed-off-by: Henrique Dias <[email protected]>
@hacdias hacdias changed the title feat(BREAKING CHANGE): try more ways until failing & store provider info feat: try more ways until failing & store provider info Sep 14, 2018
@hacdias
Copy link
Contributor Author

hacdias commented Sep 14, 2018

@olizilla done 😄

License: MIT
Signed-off-by: Henrique Dias <[email protected]>
License: MIT
Signed-off-by: Henrique Dias <[email protected]>
Copy link
Collaborator

@olizilla olizilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The multiaddr logic needs more thought.

store.doUpdateIpfsApiOpts({
host: addr[2],
port: addr[4],
protocol: 'http'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check the multi addr to see if it specifies https rather than assuming http


selectIpfsInitFailed: state => !!state.ipfs.error,
selectIpfsApiAddress: state => `/ip4/${state.ipfs.apiOpts.host}/tcp/${state.ipfs.apiOpts.port}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to figure out if this is an ip4 or ip6 host from the host value.

@olizilla
Copy link
Collaborator

I'll merge this PR, and raise an issue for the multiaddr handling.

@hacdias
Copy link
Contributor Author

hacdias commented Sep 17, 2018

Awesome! And could you release a new version too?

@olizilla olizilla merged commit 82a6e03 into master Sep 17, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants