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

Commit

Permalink
chore: make IPFS API static (remove api-manager) (#3365)
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
Co-authored-by: Hugo Dias <[email protected]>
  • Loading branch information
3 people authored Dec 4, 2020
1 parent c7d4376 commit c3c4607
Show file tree
Hide file tree
Showing 125 changed files with 3,784 additions and 2,112 deletions.
4 changes: 2 additions & 2 deletions docs/core-api/BITSWAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ The returned object contains the following keys:

- `provideBufLen` is an integer.
- `wantlist` (array of [CID][cid]s)
- `peers` (array of peer IDs as Strings)
- `peers` (array of peer IDs represented by CIDs)
- `blocksReceived` is a [BigNumber Int][1]
- `dataReceived` is a [BigNumber Int][1]
- `blocksSent` is a [BigNumber Int][1]
Expand Down Expand Up @@ -194,4 +194,4 @@ A great source of [examples][] can be found in the tests for this API.
[examples]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/bitswap
[cid]: https://www.npmjs.com/package/cids
[peerid]: https://www.npmjs.com/package/peer-id
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
2 changes: 1 addition & 1 deletion examples/browser-ipns-publish/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"devDependencies": {
"delay": "^4.4.0",
"execa": "^4.0.3",
"ipfsd-ctl": "^7.0.2",
"ipfsd-ctl": "^7.1.1",
"go-ipfs": "^0.7.0",
"parcel-bundler": "^1.12.4",
"path": "^0.12.7",
Expand Down
2 changes: 1 addition & 1 deletion examples/explore-ethereum-blockchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"devDependencies": {
"ipfs": "^0.52.1",
"ipfs-http-client": "^48.1.1",
"ipfsd-ctl": "^7.0.2",
"ipfsd-ctl": "^7.1.1",
"ipld-ethereum": "^5.0.1",
"test-ipfs-example": "^2.0.3"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/http-client-browser-pubsub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"execa": "^4.0.3",
"go-ipfs": "^0.7.0",
"ipfs": "^0.52.1",
"ipfsd-ctl": "^7.0.2",
"ipfsd-ctl": "^7.1.1",
"parcel-bundler": "^1.12.4",
"test-ipfs-example": "^2.0.3"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/http-client-bundle-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"copy-webpack-plugin": "^5.0.4",
"execa": "^4.0.3",
"ipfs": "^0.52.1",
"ipfsd-ctl": "^7.0.2",
"ipfsd-ctl": "^7.1.1",
"react-hot-loader": "^4.12.21",
"rimraf": "^3.0.2",
"test-ipfs-example": "^2.0.3",
Expand Down
2 changes: 1 addition & 1 deletion examples/http-client-name-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"devDependencies": {
"execa": "^4.0.3",
"go-ipfs": "^0.7.0",
"ipfsd-ctl": "^7.0.2",
"ipfsd-ctl": "^7.1.1",
"parcel-bundler": "^1.12.4",
"rimraf": "^3.0.2",
"test-ipfs-example": "^2.0.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/interface-ipfs-core/src/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = (common, options) => {

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

it('should respect timeout option when adding files', () => {
it('should respect timeout option when adding a file', () => {
return testTimeout(() => ipfs.add('Hello', {
timeout: 1
}))
Expand Down
31 changes: 15 additions & 16 deletions packages/ipfs-cli/src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports = {
type: 'number',
alias: 'b',
default: '2048',
describe: 'Number of bits to use in the generated RSA private key (defaults to 2048)'
describe: 'Number of bits to use in the generated RSA private key (defaults to 2048)',
coerce: Number
})
.option('empty-repo', {
alias: 'e',
Expand Down Expand Up @@ -69,22 +70,20 @@ module.exports = {
const IPFS = require('ipfs-core')
const Repo = require('ipfs-repo')

const node = await IPFS.create({
repo: new Repo(repoPath),
init: false,
start: false,
config
})

try {
await node.init({
algorithm: argv.algorithm,
bits: argv.bits,
privateKey: argv.privateKey,
emptyRepo: argv.emptyRepo,
profiles: argv.profile,
pass: argv.pass,
log: print
await IPFS.create({
repo: new Repo(repoPath),
init: {
algorithm: argv.algorithm,
bits: argv.bits,
privateKey: argv.privateKey,
emptyRepo: argv.emptyRepo,
profiles: argv.profile,
pass: argv.pass
},
start: false,
// @ts-ignore - Expects more than {}
config
})
} catch (err) {
if (err.code === 'EACCES') {
Expand Down
3 changes: 1 addition & 2 deletions packages/ipfs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@
"multicodec": "^2.0.1",
"multihashing-async": "^2.0.1",
"native-abort-controller": "~0.0.3",
"p-defer": "^3.0.0",
"p-queue": "^6.6.1",
"parse-duration": "^0.4.4",
"peer-id": "^0.14.1",
Expand All @@ -122,7 +121,7 @@
"delay": "^4.4.0",
"go-ipfs": "^0.7.0",
"interface-ipfs-core": "^0.142.2",
"ipfsd-ctl": "^7.0.2",
"ipfsd-ctl": "^7.1.1",
"ipld-git": "^0.6.1",
"iso-url": "^1.0.0",
"nanoid": "^3.1.12",
Expand Down
43 changes: 0 additions & 43 deletions packages/ipfs-core/src/api-manager.js

This file was deleted.

9 changes: 6 additions & 3 deletions packages/ipfs-core/src/components/add-all/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ const mergeOptions = require('merge-options').bind({ ignoreUndefined: true })
* @param {import('..').GCLock} config.gcLock
* @param {import('..').Preload} config.preload
* @param {import('..').Pin} config.pin
* @param {import('../init').ConstructorOptions<any, boolean>} config.options
* @param {ShardingOptions} [config.options]
*/
module.exports = ({ block, gcLock, preload, pin, options: constructorOptions }) => {
const isShardingEnabled = constructorOptions.EXPERIMENTAL && constructorOptions.EXPERIMENTAL.sharding
module.exports = ({ block, gcLock, preload, pin, options }) => {
const isShardingEnabled = options && options.sharding
/**
* Import multiple files and data into IPFS.
*
Expand Down Expand Up @@ -205,4 +205,7 @@ function pinFile (pin, opts) {
* @typedef {import('../../utils').MTime} MTime
* @typedef {import('../../utils').AbortOptions} AbortOptions
* @typedef {import('..').CID} CID
*
* @typedef {Object} ShardingOptions
* @property {boolean} [sharding]
*/
27 changes: 27 additions & 0 deletions packages/ipfs-core/src/components/bitswap/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

const createWantlist = require('./wantlist')
const createWantlistForPeer = require('./wantlist-for-peer')
const createUnwant = require('./unwant')
const createStat = require('./stat')

class BitswapAPI {
/**
* @param {Object} config
* @param {NetworkService} config.network
*/
constructor ({ network }) {
this.wantlist = createWantlist({ network })
this.wantlistForPeer = createWantlistForPeer({ network })
this.unwant = createUnwant({ network })
this.stat = createStat({ network })
}
}
module.exports = BitswapAPI

/**
* @typedef {import('..').NetworkService} NetworkService
* @typedef {import('..').PeerId} PeerId
* @typedef {import('..').CID} CID
* @typedef {import('..').AbortOptions} AbortOptions
*/
16 changes: 7 additions & 9 deletions packages/ipfs-core/src/components/bitswap/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option')

/**
* @param {Object} config
* @param {import('..').IPFSBitSwap} config.bitswap
* @param {import('.').NetworkService} config.network
*/
module.exports = ({ bitswap }) => {
module.exports = ({ network }) => {
/**
* Show diagnostic information on the bitswap agent.
* Note: `bitswap.stat` and `stats.bitswap` can be used interchangeably.
*
* @param {import('../../utils').AbortOptions} [_options]
* @returns {Promise<BitswapStats>}
*
* @example
* ```js
* const stats = await ipfs.bitswap.stat()
Expand All @@ -35,8 +32,11 @@ module.exports = ({ bitswap }) => {
* // dupDataReceived: 0
* // }
* ```
* @param {import('.').AbortOptions} [options]
* @returns {Promise<BitswapStats>}
*/
async function stat (_options) { // eslint-disable-line require-await
async function stat (options) {
const { bitswap } = await network.use(options)
const snapshot = bitswap.stat().snapshot

return {
Expand All @@ -59,13 +59,11 @@ module.exports = ({ bitswap }) => {
* @typedef {object} BitswapStats - An object that contains information about the bitswap agent
* @property {number} provideBufLen - an integer
* @property {CID[]} wantlist
* @property {string[]} peers - array of peer IDs as Strings
* @property {CID[]} peers - array of peer IDs
* @property {Big} blocksReceived
* @property {Big} dataReceived
* @property {Big} blocksSent
* @property {Big} dataSent
* @property {Big} dupBlksReceived
* @property {Big} dupDataReceived
*
* @typedef {import('..').CID} CID
*/
18 changes: 10 additions & 8 deletions packages/ipfs-core/src/components/bitswap/unwant.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option')

/**
* @param {Object} config
* @param {import('..').IPFSBitSwap} config.bitswap
* @param {import('.').NetworkService} config.network
*/
module.exports = ({ bitswap }) => {
module.exports = ({ network }) => {
/**
* Removes one or more CIDs from the wantlist
*
* @param {CID | CID[]} cids - The CIDs to remove from the wantlist
* @param {AbortOptions} [options]
* @returns {Promise<void>} - A promise that resolves once the request is complete
* @example
* ```JavaScript
* let list = await ipfs.bitswap.wantlist()
Expand All @@ -27,8 +24,14 @@ module.exports = ({ bitswap }) => {
* console.log(list)
* // []
* ```
*
* @param {CID | CID[]} cids - The CIDs to remove from the wantlist
* @param {AbortOptions} [options]
* @returns {Promise<void>} - A promise that resolves once the request is complete
*/
async function unwant (cids, options) { // eslint-disable-line require-await
async function unwant (cids, options) {
const { bitswap } = await network.use(options)

if (!Array.isArray(cids)) {
cids = [cids]
}
Expand All @@ -46,6 +49,5 @@ module.exports = ({ bitswap }) => {
}

/**
* @typedef {import('..').CID} CID
* @typedef {import('../../utils').AbortOptions} AbortOptions
* @typedef {import('.').AbortOptions} AbortOptions
*/
22 changes: 12 additions & 10 deletions packages/ipfs-core/src/components/bitswap/wantlist-for-peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option')

/**
* @param {Object} config
* @param {import('..').IPFSBitSwap} config.bitswap
* @param {import('.').NetworkService} config.network
*/
module.exports = ({ bitswap }) => {
module.exports = ({ network }) => {
/**
* Returns the wantlist for a connected peer
*
* @param {PeerId | CID | string | Uint8Array} peerId - A peer ID to return the wantlist for\
* @param {AbortOptions} [options]
* @returns {Promise<CID[]>} - An array of CIDs currently in the wantlist
*
* @example
* ```js
* const list = await ipfs.bitswap.wantlistForPeer(peerId)
* console.log(list)
* // [ CID('QmHash') ]
* ```
*
* @param {PeerId | CID | string | Uint8Array} peerId - A peer ID to return the wantlist for\
* @param {AbortOptions} [options]
* @returns {Promise<CID[]>} - An array of CIDs currently in the wantlist
*
*/
async function wantlistForPeer (peerId, options = {}) { // eslint-disable-line require-await
async function wantlistForPeer (peerId, options = {}) {
const { bitswap } = await network.use(options)
const list = bitswap.wantlistForPeer(PeerId.createFromCID(peerId), options)

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

/**
* @typedef {import('../../utils').AbortOptions} AbortOptions
* @typedef {import('..').CID} CID
* @typedef {import('..').PeerId} PeerId
* @typedef {import('.').AbortOptions} AbortOptions
* @typedef {import('.').CID} CID
* @typedef {import('.').PeerId} PeerId
*/

/**
Expand Down
Loading

0 comments on commit c3c4607

Please sign in to comment.