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

Commit 3d468f3

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

File tree

7 files changed

+125
-3
lines changed

7 files changed

+125
-3
lines changed

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,20 @@ 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.dagPB`](https://github.com/ipld/js-ipld-dag-pb)
323+
- [`ipfs.types.dagCBOR`](https://github.com/ipld/js-ipld-dag-cbor)
324+
311325
#### `Utility functions`
312326
313327
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(<readable-stream>, (err, result) => {
378392
379393
This returns an object containing the `host` and the `port`
380394
395+
##### Get libp2p crypto primitives
396+
397+
> `ipfs.util.crypto`
398+
399+
This contains an object with the crypto primitives
400+
401+
##### Get is-ipfs utilities
402+
403+
> `ipfs.util.isIPFS`
404+
405+
This contains an object with the is-ipfs utilities to help identifying IPFS resources
406+
381407
### Callbacks and Promises
382408
383409
If you do not pass in a callback all API functions will return a `Promise`. For example:

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

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

src/utils/load-commands.js

+4-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

@@ -60,7 +61,9 @@ function requireCommands () {
6061
addFromFs: require('../util/fs-add')(send),
6162
addFromStream: require('../files/add')(send),
6263
addFromURL: require('../util/url-add')(send),
63-
getEndpointConfig: require('../util/get-endpoint-config')(config)
64+
getEndpointConfig: require('../util/get-endpoint-config')(config),
65+
crypto: require('libp2p-crypto'),
66+
isIPFS: require('is-ipfs')
6467
}
6568
return util
6669
}

test/types.spec.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 multiaddr = require('multiaddr')
9+
const multibase = require('multibase')
10+
const multihash = require('multihashes')
11+
const CID = require('cids')
12+
13+
const chai = require('chai')
14+
const dirtyChai = require('dirty-chai')
15+
const expect = chai.expect
16+
chai.use(dirtyChai)
17+
18+
const IPFSApi = require('../src')
19+
20+
const f = require('./utils/factory')
21+
22+
describe('.types', function () {
23+
this.timeout(20 * 1000)
24+
25+
let ipfsd
26+
let ipfs
27+
28+
before((done) => {
29+
f.spawn({ initOptions: { bits: 1024 } }, (err, _ipfsd) => {
30+
expect(err).to.not.exist()
31+
ipfsd = _ipfsd
32+
ipfs = IPFSApi(_ipfsd.apiAddr)
33+
done()
34+
})
35+
})
36+
37+
after((done) => ipfsd.stop(done))
38+
39+
it('types object', () => {
40+
expect(ipfs.types).to.be.deep.equal({
41+
Buffer: Buffer,
42+
PeerId: PeerId,
43+
PeerInfo: PeerInfo,
44+
multiaddr: multiaddr,
45+
multibase: multibase,
46+
multihash: multihash,
47+
CID: CID,
48+
dagPB: dagPB,
49+
dagCBOR: dagCBOR
50+
})
51+
})
52+
})

test/util.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,20 @@ describe('.util', () => {
165165
expect(endpoint).to.have.property('port')
166166
})
167167
})
168+
169+
describe('.crypto', () => {
170+
it('should contain the crypto primitives object', function () {
171+
const cripto = ipfs.util.crypto
172+
173+
expect(cripto).to.exist()
174+
})
175+
})
176+
177+
describe('.isIPFS', () => {
178+
it('should contain the isIPFS utilities object', function () {
179+
const isIPFS = ipfs.util.isIPFS
180+
181+
expect(isIPFS).to.exist()
182+
})
183+
})
168184
})

0 commit comments

Comments
 (0)