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

Commit c3c4607

Browse files
Gozalaachingbrainhugomrdias
authored
chore: make IPFS API static (remove api-manager) (#3365)
- api-manager is gone. - There is now Storage component that is glorified `{keychain, repo, peerId}` tuple, that one can `async start` and it takes care of repo bootstrapping (which `init` used to do). As per discussion with @achingbrain `init` was mostly there for historical reasons. - There is now `Network` service component that is glorified `{peerId, libp2p, bitswap}` tuple. - All components that depended upon `libp2p` or `peerId` depend on `network` service now. - They can do `await network.use(options)` and get tuple when node is started (if it is starting or started) or an exception if start has not been initiated. - This way IPFS node is no longer mutated and APIs that depend on node been started throw if called before start. - lot of TS typings were added to be able to make this changes with more confidence. - Set of interfaces were added for things like datastore, repo, bitswap - create can be passed implementations and it's useful to decouple interface from a concrete implementation. - We had no types for those and it helped having interfaces to increase coverage and enable making these changes. > I would like to migrate those to other repos, but doing it as would be more effective than having to coordinate changes across many repos. - circular dependencies between pinmanager and dag APIs are resolved. Co-authored-by: Alex Potsides <alex@achingbrain.net> Co-authored-by: Hugo Dias <hugomrdias@gmail.com>
1 parent c7d4376 commit c3c4607

File tree

125 files changed

+3784
-2112
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+3784
-2112
lines changed

docs/core-api/BITSWAP.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ The returned object contains the following keys:
159159

160160
- `provideBufLen` is an integer.
161161
- `wantlist` (array of [CID][cid]s)
162-
- `peers` (array of peer IDs as Strings)
162+
- `peers` (array of peer IDs represented by CIDs)
163163
- `blocksReceived` is a [BigNumber Int][1]
164164
- `dataReceived` is a [BigNumber Int][1]
165165
- `blocksSent` is a [BigNumber Int][1]
@@ -194,4 +194,4 @@ A great source of [examples][] can be found in the tests for this API.
194194
[examples]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/bitswap
195195
[cid]: https://www.npmjs.com/package/cids
196196
[peerid]: https://www.npmjs.com/package/peer-id
197-
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
197+
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal

examples/browser-ipns-publish/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"devDependencies": {
2828
"delay": "^4.4.0",
2929
"execa": "^4.0.3",
30-
"ipfsd-ctl": "^7.0.2",
30+
"ipfsd-ctl": "^7.1.1",
3131
"go-ipfs": "^0.7.0",
3232
"parcel-bundler": "^1.12.4",
3333
"path": "^0.12.7",

examples/explore-ethereum-blockchain/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"devDependencies": {
1313
"ipfs": "^0.52.1",
1414
"ipfs-http-client": "^48.1.1",
15-
"ipfsd-ctl": "^7.0.2",
15+
"ipfsd-ctl": "^7.1.1",
1616
"ipld-ethereum": "^5.0.1",
1717
"test-ipfs-example": "^2.0.3"
1818
}

examples/http-client-browser-pubsub/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"execa": "^4.0.3",
2222
"go-ipfs": "^0.7.0",
2323
"ipfs": "^0.52.1",
24-
"ipfsd-ctl": "^7.0.2",
24+
"ipfsd-ctl": "^7.1.1",
2525
"parcel-bundler": "^1.12.4",
2626
"test-ipfs-example": "^2.0.3"
2727
}

examples/http-client-bundle-webpack/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"copy-webpack-plugin": "^5.0.4",
2626
"execa": "^4.0.3",
2727
"ipfs": "^0.52.1",
28-
"ipfsd-ctl": "^7.0.2",
28+
"ipfsd-ctl": "^7.1.1",
2929
"react-hot-loader": "^4.12.21",
3030
"rimraf": "^3.0.2",
3131
"test-ipfs-example": "^2.0.3",

examples/http-client-name-api/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"devDependencies": {
1919
"execa": "^4.0.3",
2020
"go-ipfs": "^0.7.0",
21-
"ipfsd-ctl": "^7.0.2",
21+
"ipfsd-ctl": "^7.1.1",
2222
"parcel-bundler": "^1.12.4",
2323
"rimraf": "^3.0.2",
2424
"test-ipfs-example": "^2.0.3"

packages/interface-ipfs-core/src/add.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = (common, options) => {
5555

5656
after(() => common.clean())
5757

58-
it('should respect timeout option when adding files', () => {
58+
it('should respect timeout option when adding a file', () => {
5959
return testTimeout(() => ipfs.add('Hello', {
6060
timeout: 1
6161
}))

packages/ipfs-cli/src/commands/init.js

+15-16
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module.exports = {
2727
type: 'number',
2828
alias: 'b',
2929
default: '2048',
30-
describe: 'Number of bits to use in the generated RSA private key (defaults to 2048)'
30+
describe: 'Number of bits to use in the generated RSA private key (defaults to 2048)',
31+
coerce: Number
3132
})
3233
.option('empty-repo', {
3334
alias: 'e',
@@ -69,22 +70,20 @@ module.exports = {
6970
const IPFS = require('ipfs-core')
7071
const Repo = require('ipfs-repo')
7172

72-
const node = await IPFS.create({
73-
repo: new Repo(repoPath),
74-
init: false,
75-
start: false,
76-
config
77-
})
78-
7973
try {
80-
await node.init({
81-
algorithm: argv.algorithm,
82-
bits: argv.bits,
83-
privateKey: argv.privateKey,
84-
emptyRepo: argv.emptyRepo,
85-
profiles: argv.profile,
86-
pass: argv.pass,
87-
log: print
74+
await IPFS.create({
75+
repo: new Repo(repoPath),
76+
init: {
77+
algorithm: argv.algorithm,
78+
bits: argv.bits,
79+
privateKey: argv.privateKey,
80+
emptyRepo: argv.emptyRepo,
81+
profiles: argv.profile,
82+
pass: argv.pass
83+
},
84+
start: false,
85+
// @ts-ignore - Expects more than {}
86+
config
8887
})
8988
} catch (err) {
9089
if (err.code === 'EACCES') {

packages/ipfs-core/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@
110110
"multicodec": "^2.0.1",
111111
"multihashing-async": "^2.0.1",
112112
"native-abort-controller": "~0.0.3",
113-
"p-defer": "^3.0.0",
114113
"p-queue": "^6.6.1",
115114
"parse-duration": "^0.4.4",
116115
"peer-id": "^0.14.1",
@@ -122,7 +121,7 @@
122121
"delay": "^4.4.0",
123122
"go-ipfs": "^0.7.0",
124123
"interface-ipfs-core": "^0.142.2",
125-
"ipfsd-ctl": "^7.0.2",
124+
"ipfsd-ctl": "^7.1.1",
126125
"ipld-git": "^0.6.1",
127126
"iso-url": "^1.0.0",
128127
"nanoid": "^3.1.12",

packages/ipfs-core/src/api-manager.js

-43
This file was deleted.

packages/ipfs-core/src/components/add-all/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ const mergeOptions = require('merge-options').bind({ ignoreUndefined: true })
1313
* @param {import('..').GCLock} config.gcLock
1414
* @param {import('..').Preload} config.preload
1515
* @param {import('..').Pin} config.pin
16-
* @param {import('../init').ConstructorOptions<any, boolean>} config.options
16+
* @param {ShardingOptions} [config.options]
1717
*/
18-
module.exports = ({ block, gcLock, preload, pin, options: constructorOptions }) => {
19-
const isShardingEnabled = constructorOptions.EXPERIMENTAL && constructorOptions.EXPERIMENTAL.sharding
18+
module.exports = ({ block, gcLock, preload, pin, options }) => {
19+
const isShardingEnabled = options && options.sharding
2020
/**
2121
* Import multiple files and data into IPFS.
2222
*
@@ -205,4 +205,7 @@ function pinFile (pin, opts) {
205205
* @typedef {import('../../utils').MTime} MTime
206206
* @typedef {import('../../utils').AbortOptions} AbortOptions
207207
* @typedef {import('..').CID} CID
208+
*
209+
* @typedef {Object} ShardingOptions
210+
* @property {boolean} [sharding]
208211
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict'
2+
3+
const createWantlist = require('./wantlist')
4+
const createWantlistForPeer = require('./wantlist-for-peer')
5+
const createUnwant = require('./unwant')
6+
const createStat = require('./stat')
7+
8+
class BitswapAPI {
9+
/**
10+
* @param {Object} config
11+
* @param {NetworkService} config.network
12+
*/
13+
constructor ({ network }) {
14+
this.wantlist = createWantlist({ network })
15+
this.wantlistForPeer = createWantlistForPeer({ network })
16+
this.unwant = createUnwant({ network })
17+
this.stat = createStat({ network })
18+
}
19+
}
20+
module.exports = BitswapAPI
21+
22+
/**
23+
* @typedef {import('..').NetworkService} NetworkService
24+
* @typedef {import('..').PeerId} PeerId
25+
* @typedef {import('..').CID} CID
26+
* @typedef {import('..').AbortOptions} AbortOptions
27+
*/

packages/ipfs-core/src/components/bitswap/stat.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option')
66

77
/**
88
* @param {Object} config
9-
* @param {import('..').IPFSBitSwap} config.bitswap
9+
* @param {import('.').NetworkService} config.network
1010
*/
11-
module.exports = ({ bitswap }) => {
11+
module.exports = ({ network }) => {
1212
/**
1313
* Show diagnostic information on the bitswap agent.
1414
* Note: `bitswap.stat` and `stats.bitswap` can be used interchangeably.
1515
*
16-
* @param {import('../../utils').AbortOptions} [_options]
17-
* @returns {Promise<BitswapStats>}
18-
*
1916
* @example
2017
* ```js
2118
* const stats = await ipfs.bitswap.stat()
@@ -35,8 +32,11 @@ module.exports = ({ bitswap }) => {
3532
* // dupDataReceived: 0
3633
* // }
3734
* ```
35+
* @param {import('.').AbortOptions} [options]
36+
* @returns {Promise<BitswapStats>}
3837
*/
39-
async function stat (_options) { // eslint-disable-line require-await
38+
async function stat (options) {
39+
const { bitswap } = await network.use(options)
4040
const snapshot = bitswap.stat().snapshot
4141

4242
return {
@@ -59,13 +59,11 @@ module.exports = ({ bitswap }) => {
5959
* @typedef {object} BitswapStats - An object that contains information about the bitswap agent
6060
* @property {number} provideBufLen - an integer
6161
* @property {CID[]} wantlist
62-
* @property {string[]} peers - array of peer IDs as Strings
62+
* @property {CID[]} peers - array of peer IDs
6363
* @property {Big} blocksReceived
6464
* @property {Big} dataReceived
6565
* @property {Big} blocksSent
6666
* @property {Big} dataSent
6767
* @property {Big} dupBlksReceived
6868
* @property {Big} dupDataReceived
69-
*
70-
* @typedef {import('..').CID} CID
7169
*/

packages/ipfs-core/src/components/bitswap/unwant.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option')
66

77
/**
88
* @param {Object} config
9-
* @param {import('..').IPFSBitSwap} config.bitswap
9+
* @param {import('.').NetworkService} config.network
1010
*/
11-
module.exports = ({ bitswap }) => {
11+
module.exports = ({ network }) => {
1212
/**
1313
* Removes one or more CIDs from the wantlist
1414
*
15-
* @param {CID | CID[]} cids - The CIDs to remove from the wantlist
16-
* @param {AbortOptions} [options]
17-
* @returns {Promise<void>} - A promise that resolves once the request is complete
1815
* @example
1916
* ```JavaScript
2017
* let list = await ipfs.bitswap.wantlist()
@@ -27,8 +24,14 @@ module.exports = ({ bitswap }) => {
2724
* console.log(list)
2825
* // []
2926
* ```
27+
*
28+
* @param {CID | CID[]} cids - The CIDs to remove from the wantlist
29+
* @param {AbortOptions} [options]
30+
* @returns {Promise<void>} - A promise that resolves once the request is complete
3031
*/
31-
async function unwant (cids, options) { // eslint-disable-line require-await
32+
async function unwant (cids, options) {
33+
const { bitswap } = await network.use(options)
34+
3235
if (!Array.isArray(cids)) {
3336
cids = [cids]
3437
}
@@ -46,6 +49,5 @@ module.exports = ({ bitswap }) => {
4649
}
4750

4851
/**
49-
* @typedef {import('..').CID} CID
50-
* @typedef {import('../../utils').AbortOptions} AbortOptions
52+
* @typedef {import('.').AbortOptions} AbortOptions
5153
*/

packages/ipfs-core/src/components/bitswap/wantlist-for-peer.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@ const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option')
55

66
/**
77
* @param {Object} config
8-
* @param {import('..').IPFSBitSwap} config.bitswap
8+
* @param {import('.').NetworkService} config.network
99
*/
10-
module.exports = ({ bitswap }) => {
10+
module.exports = ({ network }) => {
1111
/**
1212
* Returns the wantlist for a connected peer
1313
*
14-
* @param {PeerId | CID | string | Uint8Array} peerId - A peer ID to return the wantlist for\
15-
* @param {AbortOptions} [options]
16-
* @returns {Promise<CID[]>} - An array of CIDs currently in the wantlist
17-
*
1814
* @example
1915
* ```js
2016
* const list = await ipfs.bitswap.wantlistForPeer(peerId)
2117
* console.log(list)
2218
* // [ CID('QmHash') ]
2319
* ```
20+
*
21+
* @param {PeerId | CID | string | Uint8Array} peerId - A peer ID to return the wantlist for\
22+
* @param {AbortOptions} [options]
23+
* @returns {Promise<CID[]>} - An array of CIDs currently in the wantlist
24+
*
2425
*/
25-
async function wantlistForPeer (peerId, options = {}) { // eslint-disable-line require-await
26+
async function wantlistForPeer (peerId, options = {}) {
27+
const { bitswap } = await network.use(options)
2628
const list = bitswap.wantlistForPeer(PeerId.createFromCID(peerId), options)
2729

2830
return Array.from(list).map(e => e[1].cid)
@@ -32,9 +34,9 @@ module.exports = ({ bitswap }) => {
3234
}
3335

3436
/**
35-
* @typedef {import('../../utils').AbortOptions} AbortOptions
36-
* @typedef {import('..').CID} CID
37-
* @typedef {import('..').PeerId} PeerId
37+
* @typedef {import('.').AbortOptions} AbortOptions
38+
* @typedef {import('.').CID} CID
39+
* @typedef {import('.').PeerId} PeerId
3840
*/
3941

4042
/**

0 commit comments

Comments
 (0)