Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 79d7fef

Browse files
author
Alan Shaw
authored
refactor: export types and utilities statically (#1908)
Allows users to access these additional types and utilities without having to create an instance first. BREAKING CHANGE: `ipfs.util.isIPFS` and `ipfs.util.crypto` have moved to static exports and should be accessed via `const { isIPFS, crypto } = require('ipfs')`. The modules available under `ipfs.types.*` have also become static exports. License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 503e5ac commit 79d7fef

File tree

8 files changed

+60
-58
lines changed

8 files changed

+60
-58
lines changed

README.md

+18-18
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ You can check the development status at the [Kanban Board](https://waffle.io/ipf
6868
- [Crypto and Key Management](#crypto-and-key-management)
6969
- [Network](#network)
7070
- [Node Management](#node-management)
71-
- [Domain data types](#domain-data-types)
72-
- [Util](#util)
71+
- [Static types and utils](#static-types-and-utils)
7372
- [FAQ](#faq)
7473
- [Running js-ipfs with Docker](#running-js-ipfs-with-docker)
7574
- [Packages](#packages)
@@ -654,26 +653,27 @@ The core API is grouped into several areas:
654653
- [`ipfs.config.set(key, value, [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/CONFIG.md#configset)
655654
- [`ipfs.config.replace(config, [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/CONFIG.md#configreplace)
656655
657-
#### Domain data types
656+
#### Static types and utils
658657
659-
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.
658+
Aside from the default export, `ipfs` exports various types and utilities that are included in the bundle:
660659
661-
- [`ipfs.types.Buffer`](https://www.npmjs.com/package/buffer)
662-
- [`ipfs.types.PeerId`](https://github.com/libp2p/js-peer-id)
663-
- [`ipfs.types.PeerInfo`](https://github.com/libp2p/js-peer-info)
664-
- [`ipfs.types.multiaddr`](https://github.com/multiformats/js-multiaddr)
665-
- [`ipfs.types.multibase`](https://github.com/multiformats/js-multibase)
666-
- [`ipfs.types.multihash`](https://github.com/multiformats/js-multihash)
667-
- [`ipfs.types.CID`](https://github.com/ipld/js-cid)
668-
- [`ipfs.types.dagPB`](https://github.com/ipld/js-ipld-dag-pb)
669-
- [`ipfs.types.dagCBOR`](https://github.com/ipld/js-ipld-dag-cbor)
660+
- [`crypto`](https://www.npmjs.com/package/libp2p-crypto)
661+
- [`isIPFS`](https://www.npmjs.com/package/is-ipfs)
662+
- [`Buffer`](https://www.npmjs.com/package/buffer)
663+
- [`PeerId`](https://www.npmjs.com/package/peer-id)
664+
- [`PeerInfo`](https://www.npmjs.com/package/peer-info)
665+
- [`multiaddr`](https://www.npmjs.com/package/multiaddr)
666+
- [`multibase`](https://www.npmjs.com/package/multibase)
667+
- [`multihash`](https://www.npmjs.com/package/multihash)
668+
- [`CID`](https://www.npmjs.com/package/cids)
670669
671-
#### Util
670+
These can be accessed like this, for example:
672671
673-
A set of utils are exposed directly from the IPFS instance under `ipfs.util`. That way you're not required to import/require the following:
674-
675-
- [`ipfs.util.crypto`](https://github.com/libp2p/js-libp2p-crypto)
676-
- [`ipfs.util.isIPFS`](https://github.com/ipfs-shipyard/is-ipfs)
672+
```js
673+
const { CID } = require('ipfs')
674+
// ...or from an es-module:
675+
import { CID } from 'ipfs'
676+
```
677677
678678
## FAQ
679679

examples/browser-add-readable-stream/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
const repoPath = `ipfs-${Math.random()}`
77
const ipfs = new Ipfs({ repo: repoPath })
8+
const { Buffer } = Ipfs
89

910
ipfs.on('ready', () => {
1011
const directory = 'directory'
@@ -41,13 +42,13 @@ const createFiles = (directory) => {
4142
path: `${directory}/file1.txt`,
4243

4344
// content could be a stream, a url etc
44-
content: ipfs.types.Buffer.from('one', 'utf8')
45+
content: Buffer.from('one', 'utf8')
4546
}, {
4647
path: `${directory}/file2.txt`,
47-
content: ipfs.types.Buffer.from('two', 'utf8')
48+
content: Buffer.from('two', 'utf8')
4849
}, {
4950
path: `${directory}/file3.txt`,
50-
content: ipfs.types.Buffer.from('three', 'utf8')
51+
content: Buffer.from('three', 'utf8')
5152
}]
5253
}
5354

examples/browser-readablestream/utils.js

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

3+
const { Buffer } = require('../../')
4+
35
const log = (line) => {
46
const output = document.getElementById('output')
57
let message
@@ -40,7 +42,7 @@ const dragDrop = (ipfs) => {
4042
reader.onload = (event) => {
4143
ipfs.add({
4244
path: file.name,
43-
content: ipfs.types.Buffer.from(event.target.result)
45+
content: Buffer.from(event.target.result)
4446
}, {
4547
progress: (addedBytes) => {
4648
progress.textContent = `IPFS: Adding ${file.name} ${parseInt((addedBytes / file.size) * 100)}%\r\n`

src/core/index.js

+4-19
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,6 @@ class IPFS extends EventEmitter {
9898
this.log = debug('jsipfs')
9999
this.log.err = debug('jsipfs:err')
100100

101-
// IPFS types
102-
this.types = {
103-
Buffer: Buffer,
104-
PeerId: PeerId,
105-
PeerInfo: PeerInfo,
106-
multiaddr: multiaddr,
107-
multibase: multibase,
108-
multihash: multihash,
109-
CID: CID
110-
}
111-
112101
// IPFS Core Internals
113102
// this._repo - assigned above
114103
this._peerInfoBook = new PeerBook()
@@ -180,18 +169,14 @@ class IPFS extends EventEmitter {
180169

181170
this.state = require('./state')(this)
182171

183-
// ipfs.util
184-
this.util = {
185-
crypto,
186-
isIPFS
187-
}
188-
189172
boot(this)
190173
}
191174
}
192175

193-
exports = module.exports = IPFS
176+
module.exports = IPFS
177+
178+
Object.assign(module.exports, { crypto, isIPFS, Buffer, CID, multiaddr, multibase, multihash, PeerId, PeerInfo })
194179

195-
exports.createNode = (options) => {
180+
module.exports.createNode = (options) => {
196181
return new IPFS(options)
197182
}

test/core/exports.spec.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const crypto = require('libp2p-crypto')
5+
const isIPFS = require('is-ipfs')
6+
const CID = require('cids')
7+
const multiaddr = require('multiaddr')
8+
const multibase = require('multibase')
9+
const multihash = require('multihashes')
10+
const PeerId = require('peer-id')
11+
const PeerInfo = require('peer-info')
12+
const chai = require('chai')
13+
const dirtyChai = require('dirty-chai')
14+
const expect = chai.expect
15+
chai.use(dirtyChai)
16+
17+
const Ipfs = require('../../')
18+
19+
describe('exports', () => {
20+
it('should export the expected types and utilities', () => {
21+
expect(Ipfs.crypto).to.equal(crypto)
22+
expect(Ipfs.isIPFS).to.equal(isIPFS)
23+
expect(Ipfs.Buffer).to.equal(Buffer)
24+
expect(Ipfs.CID).to.equal(CID)
25+
expect(Ipfs.multiaddr).to.equal(multiaddr)
26+
expect(Ipfs.multibase).to.equal(multibase)
27+
expect(Ipfs.multihash).to.equal(multihash)
28+
expect(Ipfs.PeerId).to.equal(PeerId)
29+
expect(Ipfs.PeerInfo).to.equal(PeerInfo)
30+
})
31+
})

test/core/init.spec.js

-9
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ const expect = chai.expect
88
chai.use(dirtyChai)
99
const isNode = require('detect-node')
1010
const hat = require('hat')
11-
const crypto = require('libp2p-crypto')
12-
const isIPFS = require('is-ipfs')
1311
const IPFS = require('../../src/core')
1412

1513
const privateKey = 'CAASqAkwggSkAgEAAoIBAQChVmiObYo6pkKrMSd3OzW1cTL+RDmX1rkETYGKWV9TPXMNgElFTYoYHqT9QZomj5RI8iUmHccjzqr4J0mV+E0NpvHHOLlmDZ82lAw2Zx7saUkeQWvC0S9Z0o3aTx2sSubZV53rSomkZgQH4fYTs4RERejV4ltzLFdzQQBwWrBvlagpPHUCxKDUCnE5oIzdbD26ltWViPBWr7TfotzC8Lyi/tceqCpHMUJGMbsVgypnlgpey07MBvs71dVh5LcRen/ztsQO6Yju4D3QgWoyD0SIUdJFvBzEwL9bSiA3QjUc/fkGd7EcdN5bebYOqAi4ZIiAMLp3i4+B8Tzq/acull43AgMBAA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49bA9H2CoLpBGTV1LfY5yTFxRy4R3qnX/IzsKy567sbtkEFKJxplc/RzCQfrgbdj7k26SbKtHR3yERaFGRYq8UeAHeYC1/N19LF5BMQL4y5R4PJ1SFPeJCL/wXiMqs1maTqvKqtc4bbegNdwlxn'
@@ -105,11 +103,4 @@ describe('init', () => {
105103
})
106104
})
107105
})
108-
109-
it('util', () => {
110-
expect(ipfs.util).to.be.deep.equal({
111-
crypto: crypto,
112-
isIPFS: isIPFS
113-
})
114-
})
115106
})

test/core/interface.spec.js

-4
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,4 @@ describe('interface-ipfs-core tests', function () {
198198
}
199199
}
200200
}), { skip: !isNode })
201-
202-
tests.types(defaultCommonFactory)
203-
204-
tests.util(defaultCommonFactory, { skip: { reason: 'FIXME: currently failing' } })
205201
})

test/http-api/interface.js

-4
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,4 @@ describe('interface-ipfs-core over ipfs-http-client tests', () => {
128128
}
129129
}
130130
}))
131-
132-
tests.types(defaultCommonFactory, { skip: { reason: 'FIXME: currently failing' } })
133-
134-
tests.util(defaultCommonFactory, { skip: { reason: 'FIXME: currently failing' } })
135131
})

0 commit comments

Comments
 (0)