From 3d468f34bed84527043a445c0a88471f92412177 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Wed, 4 Apr 2018 10:39:04 +0100 Subject: [PATCH] feat: Provide access to bundled libraries when in browser Close #406 --- README.md | 26 +++++++++++++++++++ package.json | 5 +++- src/index.js | 2 +- src/types.js | 22 ++++++++++++++++ src/utils/load-commands.js | 5 +++- test/types.spec.js | 52 ++++++++++++++++++++++++++++++++++++++ test/util.spec.js | 16 ++++++++++++ 7 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 src/types.js create mode 100644 test/types.spec.js diff --git a/README.md b/README.md index 392423ced..5b9446a59 100644 --- a/README.md +++ b/README.md @@ -308,6 +308,20 @@ This means: - See https://github.com/ipfs/js-ipfs for details on pubsub in js-ipfs +#### `Domain data types` + +A set of data types are exposed directly from the IPFS instance under `ipfs.types`. That way you're not required to import/require the following. + +- [`ipfs.types.Buffer`](https://www.npmjs.com/package/buffer) +- [`ipfs.types.PeerId`](https://github.com/libp2p/js-peer-id) +- [`ipfs.types.PeerInfo`](https://github.com/libp2p/js-peer-info) +- [`ipfs.types.multiaddr`](https://github.com/multiformats/js-multiaddr) +- [`ipfs.types.multibase`](https://github.com/multiformats/multibase) +- [`ipfs.types.multihash`](https://github.com/multiformats/js-multihash) +- [`ipfs.types.CID`](https://github.com/ipld/js-cid) +- [`ipfs.types.dagPB`](https://github.com/ipld/js-ipld-dag-pb) +- [`ipfs.types.dagCBOR`](https://github.com/ipld/js-ipld-dag-cbor) + #### `Utility functions` Adding to the methods defined by [`interface-ipfs-core`](https://github.com/ipfs/interface-ipfs-core), `js-ipfs-api` exposes a set of extra utility methods. These utility functions are scoped behind the `ipfs.util`. @@ -378,6 +392,18 @@ ipfs.util.addFromStream(, (err, result) => { This returns an object containing the `host` and the `port` +##### Get libp2p crypto primitives + +> `ipfs.util.crypto` + +This contains an object with the crypto primitives + +##### Get is-ipfs utilities + +> `ipfs.util.isIPFS` + +This contains an object with the is-ipfs utilities to help identifying IPFS resources + ### Callbacks and Promises If you do not pass in a callback all API functions will return a `Promise`. For example: diff --git a/package.json b/package.json index 0fabfe21c..63cfe8cbb 100644 --- a/package.json +++ b/package.json @@ -35,11 +35,14 @@ "glob": "^7.1.2", "ipfs-block": "~0.6.1", "ipfs-unixfs": "~0.1.14", - "ipld-dag-pb": "~0.13.1", + "ipld-dag-cbor": "^0.12.0", + "ipld-dag-pb": "^0.13.1", "is-ipfs": "^0.3.2", "is-stream": "^1.1.0", + "libp2p-crypto": "^0.12.1", "lru-cache": "^4.1.2", "multiaddr": "^3.1.0", + "multibase": "^0.4.0", "multihashes": "~0.4.13", "ndjson": "^1.5.0", "once": "^1.4.0", diff --git a/src/index.js b/src/index.js index 70ed59097..f0f73b782 100644 --- a/src/index.js +++ b/src/index.js @@ -38,7 +38,7 @@ function IpfsAPI (hostOrMultiaddr, port, opts) { const requestAPI = sendRequest(config) const cmds = loadCommands(requestAPI, config) cmds.send = requestAPI - cmds.Buffer = Buffer + cmds.Buffer = Buffer // Added buffer in types (this should be removed once a breaking change is release) return cmds } diff --git a/src/types.js b/src/types.js new file mode 100644 index 000000000..a6ae650c4 --- /dev/null +++ b/src/types.js @@ -0,0 +1,22 @@ +'use strict' + +const CID = require('cids') +const dagCBOR = require('ipld-dag-cbor') +const dagPB = require('ipld-dag-pb') +const multiaddr = require('multiaddr') +const multibase = require('multibase') +const multihash = require('multihashes') +const PeerId = require('peer-id') +const PeerInfo = require('peer-info') + +module.exports = () => ({ + Buffer: Buffer, + CID: CID, + dagPB: dagPB, + dagCBOR: dagCBOR, + multiaddr: multiaddr, + multibase: multibase, + multihash: multihash, + PeerId: PeerId, + PeerInfo: PeerInfo +}) diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index e9494055e..33f5d3563 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -41,6 +41,7 @@ function requireCommands () { pubsub: require('../pubsub'), update: require('../update'), version: require('../version'), + types: require('../types'), dns: require('../dns') } @@ -60,7 +61,9 @@ function requireCommands () { addFromFs: require('../util/fs-add')(send), addFromStream: require('../files/add')(send), addFromURL: require('../util/url-add')(send), - getEndpointConfig: require('../util/get-endpoint-config')(config) + getEndpointConfig: require('../util/get-endpoint-config')(config), + crypto: require('libp2p-crypto'), + isIPFS: require('is-ipfs') } return util } diff --git a/test/types.spec.js b/test/types.spec.js new file mode 100644 index 000000000..5310982ae --- /dev/null +++ b/test/types.spec.js @@ -0,0 +1,52 @@ +/* eslint-env mocha */ +'use strict' + +const PeerId = require('peer-id') +const PeerInfo = require('peer-info') +const dagCBOR = require('ipld-dag-cbor') +const dagPB = require('ipld-dag-pb') +const multiaddr = require('multiaddr') +const multibase = require('multibase') +const multihash = require('multihashes') +const CID = require('cids') + +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const expect = chai.expect +chai.use(dirtyChai) + +const IPFSApi = require('../src') + +const f = require('./utils/factory') + +describe('.types', function () { + this.timeout(20 * 1000) + + let ipfsd + let ipfs + + before((done) => { + f.spawn({ initOptions: { bits: 1024 } }, (err, _ipfsd) => { + expect(err).to.not.exist() + ipfsd = _ipfsd + ipfs = IPFSApi(_ipfsd.apiAddr) + done() + }) + }) + + after((done) => ipfsd.stop(done)) + + it('types object', () => { + expect(ipfs.types).to.be.deep.equal({ + Buffer: Buffer, + PeerId: PeerId, + PeerInfo: PeerInfo, + multiaddr: multiaddr, + multibase: multibase, + multihash: multihash, + CID: CID, + dagPB: dagPB, + dagCBOR: dagCBOR + }) + }) +}) diff --git a/test/util.spec.js b/test/util.spec.js index 0c15f3347..09fac4c91 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -165,4 +165,20 @@ describe('.util', () => { expect(endpoint).to.have.property('port') }) }) + + describe('.crypto', () => { + it('should contain the crypto primitives object', function () { + const cripto = ipfs.util.crypto + + expect(cripto).to.exist() + }) + }) + + describe('.isIPFS', () => { + it('should contain the isIPFS utilities object', function () { + const isIPFS = ipfs.util.isIPFS + + expect(isIPFS).to.exist() + }) + }) })