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
Browse files Browse the repository at this point in the history
Close #406
  • Loading branch information
vasco-santos committed Apr 4, 2018
1 parent 41d32e3 commit 3d4b90a
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,22 @@ 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.crypto`](https://github.com/libp2p/js-libp2p-crypto)
- [`ipfs.types.dagPB`](https://github.com/ipld/js-ipld-dag-pb)
- [`ipfs.types.dagCBOR`](https://github.com/ipld/js-ipld-dag-cbor)
- [`ipfs.types.isIPFS`](https://github.com/ipfs-shipyard/is-ipfs)
#### `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
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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
26 changes: 26 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

const CID = require('cids')
const crypto = require('libp2p-crypto')
const dagCBOR = require('ipld-dag-cbor')
const dagPB = require('ipld-dag-pb')
const isIPFS = require('is-ipfs')
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,
crypto: crypto,
dagPB: dagPB,
dagCBOR: dagCBOR,
isIPFS: isIPFS,
multiaddr: multiaddr,
multibase: multibase,
multihash: multihash,
PeerId: PeerId,
PeerInfo: PeerInfo
})
1 change: 1 addition & 0 deletions src/utils/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function requireCommands () {
pubsub: require('../pubsub'),
update: require('../update'),
version: require('../version'),
types: require('../types'),
dns: require('../dns')
}

Expand Down
56 changes: 56 additions & 0 deletions test/types.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* 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 crypto = require('libp2p-crypto')
const isIPFS = require('is-ipfs')
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,
crypto: crypto,
dagPB: dagPB,
dagCBOR: dagCBOR,
isIPFS: isIPFS
})
})
})

0 comments on commit 3d4b90a

Please sign in to comment.