-
Notifications
You must be signed in to change notification settings - Fork 6
feat: try more ways until failing & store provider info #5
Conversation
Signed-off-by: Henrique Dias <[email protected]>
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 }) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this 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.
@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]>
@olizilla this is a breaking change, as it removes It also introduces js-ipfs-api as a dependency. |
License: MIT Signed-off-by: Henrique Dias <[email protected]>
License: MIT Signed-off-by: Henrique Dias <[email protected]>
There was a problem hiding this 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 |
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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`. |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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!') |
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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 }) | |||
)() | |||
|
There was a problem hiding this comment.
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]>
@olizilla apparently Travis liked the tests 😄 |
store.subscribeToSelectors(['selectIpfsReady'], () => { | ||
if (first) { | ||
expect(store.selectIpfsReady()).toBe(true) | ||
expect(store.selectIpfsIdentity()).not.toEqual(testIdentity) |
There was a problem hiding this comment.
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.
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]>
License: MIT Signed-off-by: Henrique Dias <[email protected]>
@olizilla done 😄 |
License: MIT Signed-off-by: Henrique Dias <[email protected]>
License: MIT Signed-off-by: Henrique Dias <[email protected]>
There was a problem hiding this 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' |
There was a problem hiding this comment.
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}`, |
There was a problem hiding this comment.
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.
I'll merge this PR, and raise an issue for the multiaddr handling. |
Awesome! And could you release a new version too? |
Also see ipfs/ipfs-webui#785