Code Monkey home page Code Monkey logo

ipfs-redux-bundle's Introduction

⛔️ DEPRECATED

This library is being deprecated and replaced with more flexible ipfs-provider.

This library will not be maintained.


ipfs-redux-bundle

A redux bundle to create an IPFS instance and pass it as an extraArg to other store methods.

Build Status

See https://reduxbundler.com for more info on the wonders of redux bundles.

This module tries to connect to IPFS via multiple providers, in order:

  • ipfs-companion the IPFS instance from IPFS Companion directly.
  • window.ipfs in the current page via IPFS Companion.
  • js-ipfs-http-client 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

Usage

Add ipfs-redux-bundle to your store

bundles/index.js

import { composeBundles } from 'redux-bundler'
import ipfsBundle from 'ipfs-redux-bundle'
// ... import other bundles

export default composeBundles(
  ipfsBundle({
    // These are the defaults:
    tryCompanion: true,   // set false to bypass ipfs-companion verification
    tryWindow: true,      // set false to bypass window.ipfs verification
    tryApi: true,         // set false to bypass js-ipfs-http-client 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.
  })
  // ... add bundles here
)

In your app, you can now connect up the doInitIpfs function. Here we init IPFS when our root component mounts:

App.js

import React, { Component } from 'react'
import { connect } from 'redux-bundler-react'

export class App extends Component {
  componentDidMount () {
    this.props.doInitIpfs()
  }
  render () {
    // ipfs-css powered gorgeous ui here.
  }
}

export default connect(
  'doInitIpfs',
  App
)

Enable js-ipfs

To enable js-ipfs, intialise the bundle with the following opts

  ipfsBundle({
    tryJsIpfs: true,
    getJsIpfs: () => import('ipfs')
  })
  • tryJsIpfs should be set to true
  • getJsIpfs should be a function that returns a promise that resolves with a JsIpfs constructor. This works well with dynamic import(), so you can lazily load js-ipfs when it is needed.

API

Adds the following methods to the redux store.

store.selectIpfsReady()

  • boolean - Is the IPFS instance ready to use yet?

store.selectIpfsInitFailed()

  • boolean - Did the IPFS instance fail to start?

store.selectIpfsInvalidAddress()

  • boolean - Is the last API Address you tried to use invalid?

store.selectIpfsProvider()

  • string - Can be window.ipfs, js-ipfs-api or js-ipfs.

store.selectIpfsApiAddress()

  • string - The API address of the IPFS instance.

store.doInitIpfs()

store.doStopIpfs()

  • Stops the IPFS instance. It is only intended to use with js-ipfs.

store.doUpdateIpfsApiAddress(address)

  • Updates the API Address to address.

store.doDismissIpfsInvalidAddress()

  • Dismisses the invalid address error.

Contribute

Feel free to dive in! Open an issue or submit PRs.

License

MIT © Protocol Labs

ipfs-redux-bundle's People

Contributors

dependabot[bot] avatar fsdiogo avatar hacdias avatar lidel avatar olizilla avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ipfs-redux-bundle's Issues

Inform the user when they use an invalid multiaddr

As we're not using the multiaddr provided by the user if we find it to be invalid we're going to need to inform them about it.

Create a helper function that grabs the ipfsApi from localStorage, validates it, an either logs an error if its invalid, or returns a valid multiaddr

selectIpfsIdentity is always undefined

Since ipfs-redux-bundle never calls ipfs.id(), the result of this selector is always undefined. I noticed IPFS Web UI has a bundle only for the identity. I believe the reason is that this one is not working as it should.

Ensure all deps are loaded over IPFS

Right now window.ipfs-fallback loads js-ipfs or js-ipfs-api from https://unpkg.com. We want to ensure that all our deps are loaded via IPFS.

In IPLD Explorer https://github.com/ipfs-shipyard/ipld-explorer/blob/ff64b2fba5ad556af83617a488dc5a7b71aad572/src/bundles/ipfs.js I've pulled the ipfs redux bundle back inline, and used dynamic import() calls to selectively load js-ipfs-api or js-ipfs. Under the hood it's create-react-app and webpack, that manage splitting the js bundles where it finds calls to import() in code.

It'd be ideal if we could use that same logic here. I haven't checked if the webpack config used in create-react-app will transpile calls to import() from dependencies. Beyond that it'd be great to make this module useable by any build system, but as there isn't a huge amount of code here, it might be simpler to create new modules for different bundling mechanisms.

Alternatives are

  • get js-ipfs-api and js-ipfs modules published to ipfs as part of the release process, and use those addresses in place of unpkg (@achingbrain is working on npm-on-ipfs as a much bigger effort to share npm modules over IPFS)
  • ?

fix tests, and mock js-ipfs-api

We don't need the tests to start a real ipfs instance to check that we do the right thing. Use https://github.com/nock/nock to mock out the required response to keep the tests fast and not dependent on the dev not running an api.

Of note we're testing in jest in a node process, so are tests are drifting away from how we expect this module to be used. I'm less bothered about that than I am about having tests for all the logic that this module performs, and not having the test runs be affected by my local set up.

Refactor to use ipfs-provider internally

ipfs-provider is a light framework for getting IPFS API based on custom fallback logic.
We should avoid having two codebases that do the same thing and:

  • switch ipfs-redux-bundle to be a think wrapper on top of ipfs-provider
  • do not define ipfs-http-client version as a dependency, but use one provided by user of this library
    (for example, webui should define what version of ipfs-http-client should be used, and bumping that version should not require new release of ipfs-redux-bundle)

Ref. https://github.com/ipfs-shipyard/ipfs-provider

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.