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

Commit 3d4b90a

Browse files
committed
feat: Provide access to bundled libraries when in browser
Close #406
1 parent 41d32e3 commit 3d4b90a

File tree

6 files changed

+104
-2
lines changed

6 files changed

+104
-2
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,22 @@ This means:
308308
- See https://github.com/ipfs/js-ipfs for details on
309309
pubsub in js-ipfs
310310
311+
#### `Domain data types`
312+
313+
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.
314+
315+
- [`ipfs.types.Buffer`](https://www.npmjs.com/package/buffer)
316+
- [`ipfs.types.PeerId`](https://github.com/libp2p/js-peer-id)
317+
- [`ipfs.types.PeerInfo`](https://github.com/libp2p/js-peer-info)
318+
- [`ipfs.types.multiaddr`](https://github.com/multiformats/js-multiaddr)
319+
- [`ipfs.types.multibase`](https://github.com/multiformats/multibase)
320+
- [`ipfs.types.multihash`](https://github.com/multiformats/js-multihash)
321+
- [`ipfs.types.CID`](https://github.com/ipld/js-cid)
322+
- [`ipfs.types.crypto`](https://github.com/libp2p/js-libp2p-crypto)
323+
- [`ipfs.types.dagPB`](https://github.com/ipld/js-ipld-dag-pb)
324+
- [`ipfs.types.dagCBOR`](https://github.com/ipld/js-ipld-dag-cbor)
325+
- [`ipfs.types.isIPFS`](https://github.com/ipfs-shipyard/is-ipfs)
326+
311327
#### `Utility functions`
312328
313329
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`.

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@
3535
"glob": "^7.1.2",
3636
"ipfs-block": "~0.6.1",
3737
"ipfs-unixfs": "~0.1.14",
38-
"ipld-dag-pb": "~0.13.1",
38+
"ipld-dag-cbor": "^0.12.0",
39+
"ipld-dag-pb": "^0.13.1",
3940
"is-ipfs": "^0.3.2",
4041
"is-stream": "^1.1.0",
42+
"libp2p-crypto": "^0.12.1",
4143
"lru-cache": "^4.1.2",
4244
"multiaddr": "^3.1.0",
45+
"multibase": "^0.4.0",
4346
"multihashes": "~0.4.13",
4447
"ndjson": "^1.5.0",
4548
"once": "^1.4.0",

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function IpfsAPI (hostOrMultiaddr, port, opts) {
3838
const requestAPI = sendRequest(config)
3939
const cmds = loadCommands(requestAPI, config)
4040
cmds.send = requestAPI
41-
cmds.Buffer = Buffer
41+
cmds.Buffer = Buffer // Added buffer in types (this should be removed once a breaking change is release)
4242

4343
return cmds
4444
}

src/types.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
const CID = require('cids')
4+
const crypto = require('libp2p-crypto')
5+
const dagCBOR = require('ipld-dag-cbor')
6+
const dagPB = require('ipld-dag-pb')
7+
const isIPFS = require('is-ipfs')
8+
const multiaddr = require('multiaddr')
9+
const multibase = require('multibase')
10+
const multihash = require('multihashes')
11+
const PeerId = require('peer-id')
12+
const PeerInfo = require('peer-info')
13+
14+
module.exports = () => ({
15+
Buffer: Buffer,
16+
CID: CID,
17+
crypto: crypto,
18+
dagPB: dagPB,
19+
dagCBOR: dagCBOR,
20+
isIPFS: isIPFS,
21+
multiaddr: multiaddr,
22+
multibase: multibase,
23+
multihash: multihash,
24+
PeerId: PeerId,
25+
PeerInfo: PeerInfo
26+
})

src/utils/load-commands.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ function requireCommands () {
4141
pubsub: require('../pubsub'),
4242
update: require('../update'),
4343
version: require('../version'),
44+
types: require('../types'),
4445
dns: require('../dns')
4546
}
4647

test/types.spec.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const PeerId = require('peer-id')
5+
const PeerInfo = require('peer-info')
6+
const dagCBOR = require('ipld-dag-cbor')
7+
const dagPB = require('ipld-dag-pb')
8+
const crypto = require('libp2p-crypto')
9+
const isIPFS = require('is-ipfs')
10+
const multiaddr = require('multiaddr')
11+
const multibase = require('multibase')
12+
const multihash = require('multihashes')
13+
const CID = require('cids')
14+
15+
const chai = require('chai')
16+
const dirtyChai = require('dirty-chai')
17+
const expect = chai.expect
18+
chai.use(dirtyChai)
19+
20+
const IPFSApi = require('../src')
21+
22+
const f = require('./utils/factory')
23+
24+
describe('.types', function () {
25+
this.timeout(20 * 1000)
26+
27+
let ipfsd
28+
let ipfs
29+
30+
before((done) => {
31+
f.spawn({ initOptions: { bits: 1024 } }, (err, _ipfsd) => {
32+
expect(err).to.not.exist()
33+
ipfsd = _ipfsd
34+
ipfs = IPFSApi(_ipfsd.apiAddr)
35+
done()
36+
})
37+
})
38+
39+
after((done) => ipfsd.stop(done))
40+
41+
it('types object', () => {
42+
expect(ipfs.types).to.be.deep.equal({
43+
Buffer: Buffer,
44+
PeerId: PeerId,
45+
PeerInfo: PeerInfo,
46+
multiaddr: multiaddr,
47+
multibase: multibase,
48+
multihash: multihash,
49+
CID: CID,
50+
crypto: crypto,
51+
dagPB: dagPB,
52+
dagCBOR: dagCBOR,
53+
isIPFS: isIPFS
54+
})
55+
})
56+
})

0 commit comments

Comments
 (0)