0.2.2-1 • Published 6 years ago
@jimpick/ipfs-provider v0.2.2-1
ipfs-provider
Connect to IPFS via an available provider.
This module tries to connect to IPFS via multiple providers, in order:
webextlooks for an instance in the background page of a WebExtension (used only in browser extensions, not regular pages).window.ipfsproxy in the current page (provided by the IPFS Companion browser extension).js-ipfs-http-clientwith either a user providedapiAddress, the current origin, or the default address (/ip4/127.0.0.1/tcp/5001).js-ipfsspawns an in process instance of IPFS (disabled by default: see Enable js-ipfs for more info).
This is a port of the ipfs-redux-bundle.
Install
> npm install ipfs-providerUsage
import getIpfs from 'ipfs-provider'
const { ipfs, provider } = await getIpfs({
// These are the defaults:
tryWebExt: true, // set false to bypass WebExtension verification
tryWindow: true, // set false to bypass window.ipfs verification
permissions: {} // set the window.ipfs options you want if tryWindow is true
tryApi: true, // set false to bypass js-ipfs-http-client verification
apiAddress: null // set this to use an api in that address if tryApi is true
tryJsIpfs: false, // set true to attempt js-ipfs initialisation
getJsIpfs: null, // must be set to a js-ipfs instance if tryJsIpfs is true
jsIpfsOpts: {} // set the js-ipfs options you want if tryJsIpfs is true
})ipfsis the running IPFS instance.provideris a string representing the chosen provider, either:WEBEXT,WINDOW_IPFS,IPFS_HTTP_APIorJS_IPFS.
Enable js-ipfs
To enable js-ipfs, pass the following options:
const { ipfs, provider } = await getIpfs({
tryJsIpfs: true,
getJsIpfs: () => import('ipfs'),
jsIpfsOpts: { /* advanced config */ }
})tryJsIpfsshould be set totrue.getJsIpfsshould be a function that returns a promise that resolves with aJsIpfsconstructor. This works well with dynamicimport(), so you can lazily load js-ipfs when it is needed.jsIpfsOptsshould be an object which specifies advanced configurations to the node.
Enable window.ipfs
window.ipfs is an experimental feature provided by ipfs-companion browser extension. It supports passing an optional list of permissions to display a single ACL prompt the first time it is used:
const { ipfs, provider } = await getIpfs({
tryWindow: true,
permissions: { commands: ['add','cat','id', 'version'] },
tryJsIpfs: true,
...Note: Make sure to enable at least one other provider such as js-ipfs as a fallback to ensure users without window.ipfs are able to use your app.
Test
> npm testLint
Perform standard linting on the code:
> npm run lintContribute
Feel free to dive in! Open an issue or submit PRs.
To contribute to IPFS in general, see the contributing guide.
License
MIT © Protocol Labs
0.2.2-1
6 years ago
