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

Commit 347e973

Browse files
author
Alan Shaw
committed
refactor: export types and utilities statically
Allows users to access these additional types and utilities without having to create an instance of the c lient first. BREAKING CHANGE: `ipfs.util.isIPFS` has moved to a static export and should be accessed via `const { isIPFS } = require('ipfs-http-client'). The modules available under `ipfs.types.*` have also become static exports. `ipfs.util.crypto` has been removed as it is not a dependency of `ipfs-http-client` so reduces the bundle size. If you need to use libp2p crypto primitives then please see the [js-libp2p-crypto](https://github.com/libp2p/js-libp2p-crypto) project for info on how to use it in your project. Finally `ipfs.util.getEndpointConfig` is now a direct instance method, `ipfs.getEndpointConfig` resolves #902 License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent ec3eee2 commit 347e973

9 files changed

+112
-120
lines changed

README.md

+19-28
Original file line numberDiff line numberDiff line change
@@ -348,41 +348,32 @@ This means:
348348
- See https://github.com/ipfs/js-ipfs for details on
349349
pubsub in js-ipfs
350350

351-
#### Domain data types
351+
#### Instance utilities
352352

353-
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.
353+
- `ipfs.getEndpointConfig()`
354354

355-
- [`ipfs.types.Buffer`](https://www.npmjs.com/package/buffer)
356-
- [`ipfs.types.PeerId`](https://github.com/libp2p/js-peer-id)
357-
- [`ipfs.types.PeerInfo`](https://github.com/libp2p/js-peer-info)
358-
- [`ipfs.types.multiaddr`](https://github.com/multiformats/js-multiaddr)
359-
- [`ipfs.types.multibase`](https://github.com/multiformats/multibase)
360-
- [`ipfs.types.multihash`](https://github.com/multiformats/js-multihash)
361-
- [`ipfs.types.CID`](https://github.com/ipld/js-cid)
355+
Call this on your client instance to return an object containing the `host`, `port` and `protocol`.
362356

363-
#### Extra (util) functions
357+
#### Static types and utilities
364358

365-
Adding to the methods defined by [`interface-ipfs-core`](https://github.com/ipfs/interface-ipfs-core), `js-ipfs-http-client` exposes a set of extra utility methods. These utility functions are scoped behind the `ipfs.util`.
359+
Aside from the default export, `ipfs-http-client` exports various types and utilities that are included in the bundle:
366360

367-
Complete documentation for these methods is coming with: https://github.com/ipfs/js-ipfs-http-client/pull/305
361+
- [`isIPFS`](https://www.npmjs.com/package/is-ipfs)
362+
- [`Buffer`](https://www.npmjs.com/package/buffer)
363+
- [`PeerId`](https://www.npmjs.com/package/peer-id)
364+
- [`PeerInfo`](https://www.npmjs.com/package/peer-info)
365+
- [`multiaddr`](https://www.npmjs.com/package/multiaddr)
366+
- [`multibase`](https://www.npmjs.com/package/multibase)
367+
- [`multihash`](https://www.npmjs.com/package/multihash)
368+
- [`CID`](https://www.npmjs.com/package/cids)
368369

369-
##### Get endpoint configuration (host and port)
370+
These can be accessed like this, for example:
370371

371-
> `ipfs.util.getEndpointConfig()`
372-
373-
This returns an object containing the `host`, `port` and `protocol`
374-
375-
##### Get libp2p crypto primitives
376-
377-
> `ipfs.util.crypto`
378-
379-
This contains an object with the crypto primitives
380-
381-
##### Get is-ipfs utilities
382-
383-
> `ipfs.util.isIPFS`
384-
385-
This contains an object with the is-ipfs utilities to help identifying IPFS resources
372+
```js
373+
const { CID } = require('ipfs-http-client')
374+
// ...or from an es-module:
375+
import { CID } from 'ipfs-http-client'
376+
```
386377

387378
## Development
388379

File renamed without changes.

src/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
'use strict'
22
/* global self */
33

4+
const isIPFS = require('is-ipfs')
5+
const CID = require('cids')
46
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')
511
const loadCommands = require('./utils/load-commands')
612
const getConfig = require('./utils/default-config')
713
const sendRequest = require('./utils/send-request')
@@ -39,7 +45,6 @@ function ipfsClient (hostOrMultiaddr, port, opts) {
3945
const requestAPI = sendRequest(config)
4046
const cmds = loadCommands(requestAPI, config)
4147
cmds.send = requestAPI
42-
cmds.Buffer = Buffer // Added buffer in types (this should be removed once a breaking change is release)
4348

4449
return cmds
4550
}
@@ -54,3 +59,5 @@ function toHostAndPort (multiaddr) {
5459
}
5560

5661
module.exports = ipfsClient
62+
63+
Object.assign(module.exports, { isIPFS, Buffer, CID, multiaddr, multibase, multihash, PeerId, PeerInfo })

src/utils/load-commands.js

+8-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
function requireCommands () {
4-
const cmds = {
4+
return {
55
// Files Regular (not MFS)
66
add: require('../files-regular/add'),
77
addReadableStream: require('../files-regular/add-readable-stream'),
@@ -19,6 +19,9 @@ function requireCommands () {
1919
lsReadableStream: require('../files-regular/ls-readable-stream'),
2020
lsPullStream: require('../files-regular/ls-pull-stream'),
2121

22+
// Files MFS (Mutable Filesystem)
23+
files: require('../files-mfs'),
24+
2225
// Block
2326
block: require('../block'),
2427
bitswap: require('../bitswap'),
@@ -50,30 +53,14 @@ function requireCommands () {
5053
refs: require('../refs'),
5154
repo: require('../repo'),
5255
stop: require('../stop'),
56+
shutdown: require('../stop'),
5357
stats: require('../stats'),
5458
update: require('../update'),
5559
version: require('../version'),
56-
types: require('../types'),
57-
resolve: require('../resolve')
58-
}
59-
60-
// shutdown is an alias for stop
61-
cmds.shutdown = cmds.stop
62-
63-
// Files MFS (Mutable Filesystem)
64-
cmds.files = (send) => {
65-
return require('../files-mfs')(send)
60+
resolve: require('../resolve'),
61+
// ipfs-http-client instance
62+
getEndpointConfig: (send, config) => require('../get-endpoint-config')(config)
6663
}
67-
68-
cmds.util = (send, config) => {
69-
return {
70-
getEndpointConfig: require('../util/get-endpoint-config')(config),
71-
crypto: require('libp2p-crypto'),
72-
isIPFS: require('is-ipfs')
73-
}
74-
}
75-
76-
return cmds
7764
}
7865

7966
function loadCommands (send, config) {

test/constructor.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-env mocha */
1+
/* eslint-env mocha, browser */
22
'use strict'
33

44
const multiaddr = require('multiaddr')
@@ -114,7 +114,7 @@ function clientWorks (client, done) {
114114
}
115115

116116
function expectConfig (ipfs, { host, port, protocol, apiPath }) {
117-
const conf = ipfs.util.getEndpointConfig()
117+
const conf = ipfs.getEndpointConfig()
118118
expect(conf.host).to.equal(host || 'localhost')
119119
expect(conf.port).to.equal(port || '5001')
120120
expect(conf.protocol).to.equal(protocol || 'http')

test/endpoint-config.spec.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
'use strict'
4+
5+
const chai = require('chai')
6+
const dirtyChai = require('dirty-chai')
7+
const expect = chai.expect
8+
chai.use(dirtyChai)
9+
const isNode = require('detect-node')
10+
11+
const ipfsClient = require('../src')
12+
const f = require('./utils/factory')
13+
14+
describe('.getEndpointConfig', () => {
15+
if (!isNode) { return }
16+
17+
let ipfsd
18+
let ipfs
19+
20+
before(function (done) {
21+
this.timeout(20 * 1000) // slow CI
22+
23+
f.spawn({ initOptions: { bits: 1024, profile: 'test' } }, (err, _ipfsd) => {
24+
expect(err).to.not.exist()
25+
ipfsd = _ipfsd
26+
ipfs = ipfsClient(_ipfsd.apiAddr)
27+
done()
28+
})
29+
})
30+
31+
after(function (done) {
32+
this.timeout(10 * 1000)
33+
if (!ipfsd) return done()
34+
ipfsd.stop(done)
35+
})
36+
37+
it('should return the endpoint configuration', function () {
38+
const endpoint = ipfs.getEndpointConfig()
39+
40+
expect(endpoint.host).to.equal('127.0.0.1')
41+
expect(endpoint.protocol).to.equal('http')
42+
expect(endpoint['api-path']).to.equal('/api/v0/')
43+
// changes per test run so we just assert it exists.
44+
expect(endpoint).to.have.property('port')
45+
})
46+
})

test/exports.spec.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* eslint-env mocha, browser */
2+
'use strict'
3+
4+
const isIPFS = require('is-ipfs')
5+
const CID = require('cids')
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+
const chai = require('chai')
12+
const dirtyChai = require('dirty-chai')
13+
const expect = chai.expect
14+
chai.use(dirtyChai)
15+
16+
const IpfsHttpClient = require('../')
17+
18+
describe('exports', () => {
19+
it('should export the expected types and utilities', () => {
20+
expect(IpfsHttpClient.isIPFS).to.equal(isIPFS)
21+
expect(IpfsHttpClient.Buffer).to.equal(Buffer)
22+
expect(IpfsHttpClient.CID).to.equal(CID)
23+
expect(IpfsHttpClient.multiaddr).to.equal(multiaddr)
24+
expect(IpfsHttpClient.multibase).to.equal(multibase)
25+
expect(IpfsHttpClient.multihash).to.equal(multihash)
26+
expect(IpfsHttpClient.PeerId).to.equal(PeerId)
27+
expect(IpfsHttpClient.PeerInfo).to.equal(PeerInfo)
28+
})
29+
})

test/interface.spec.js

-4
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,4 @@ describe('interface-ipfs-core tests', () => {
282282
}
283283
}
284284
}))
285-
286-
tests.types(defaultCommonFactory)
287-
288-
tests.util(defaultCommonFactory, { skip: { reason: 'FIXME currently failing' } })
289285
})

test/util.spec.js

-64
This file was deleted.

0 commit comments

Comments
 (0)