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

Commit

Permalink
feat: Provide access to bundled libraries when in browser (#732)
Browse files Browse the repository at this point in the history
Close #406
  • Loading branch information
vasco-santos authored and daviddias committed Apr 5, 2018
1 parent 9c37213 commit 994bdad
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 3 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -378,6 +392,18 @@ ipfs.util.addFromStream(<readable-stream>, (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:
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
"ipfs-block": "~0.6.1",
"ipfs-unixfs": "~0.1.14",
"ipld-dag-cbor": "^0.12.0",
"ipld-dag-pb": "~0.13.1",
"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",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
22 changes: 22 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
@@ -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
})
5 changes: 4 additions & 1 deletion src/utils/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function requireCommands () {
pubsub: require('../pubsub'),
update: require('../update'),
version: require('../version'),
types: require('../types'),
dns: require('../dns')
}

Expand All @@ -61,7 +62,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
}
Expand Down
52 changes: 52 additions & 0 deletions test/types.spec.js
Original file line number Diff line number Diff line change
@@ -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
})
})
})
16 changes: 16 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})

0 comments on commit 994bdad

Please sign in to comment.