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

Commit 084b213

Browse files
committed
chore: make IPFS api static
1 parent b5ea76a commit 084b213

Some content is hidden

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

112 files changed

+3727
-2124
lines changed

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

+13-15
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,20 @@ module.exports = {
6969
const IPFS = require('ipfs-core')
7070
const Repo = require('ipfs-repo')
7171

72-
const node = await IPFS.create({
73-
repo: new Repo(repoPath),
74-
init: false,
75-
start: false,
76-
config
77-
})
78-
7972
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
73+
await IPFS.create({
74+
repo: new Repo(repoPath),
75+
init: {
76+
algorithm: argv.algorithm,
77+
bits: argv.bits,
78+
privateKey: argv.privateKey,
79+
emptyRepo: argv.emptyRepo,
80+
profiles: argv.profile,
81+
pass: argv.pass
82+
},
83+
start: false,
84+
// @ts-ignore - Expects more than {}
85+
config
8886
})
8987
} catch (err) {
9088
if (err.code === 'EACCES') {

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
*
@@ -178,4 +178,7 @@ function pinFile (pin, opts) {
178178
* @typedef {import('../../utils').MTime} MTime
179179
* @typedef {import('../../utils').AbortOptions} AbortOptions
180180
* @typedef {import('..').CID} CID
181+
*
182+
* @typedef {Object} ShardingOptions
183+
* @property {boolean} [sharding]
181184
*/
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('../../utils')
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 as Strings
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('../../utils')
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('../../utils')
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
/**

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@ const { withTimeoutOption } = require('../../utils')
44

55
/**
66
* @param {Object} config
7-
* @param {import('..').IPFSBitSwap} config.bitswap
7+
* @param {import('.').NetworkService} config.network
88
*/
9-
module.exports = ({ bitswap }) => {
9+
module.exports = ({ network }) => {
1010
/**
1111
* Returns the wantlist for your node
1212
*
13-
* @param {AbortOptions} [options]
14-
* @returns {Promise<CID[]>} - An array of CIDs currently in the wantlist.
1513
* @example
1614
* ```js
1715
* const list = await ipfs.bitswap.wantlist()
1816
* console.log(list)
1917
* // [ CID('QmHash') ]
2018
* ```
19+
*
20+
* @param {AbortOptions} [options]
21+
* @returns {Promise<CID[]>} - An array of CIDs currently in the wantlist.
2122
*/
22-
async function wantlist (options = {}) { // eslint-disable-line require-await
23+
async function wantlist (options = {}) {
24+
const { bitswap } = await network.use(options)
2325
const list = bitswap.getWantlist(options)
2426

2527
return Array.from(list).map(e => e[1].cid)
@@ -29,6 +31,6 @@ module.exports = ({ bitswap }) => {
2931
}
3032

3133
/**
32-
* @typedef {import('../../utils').AbortOptions} AbortOptions
33-
* @typedef {import('..').CID} CID
34+
* @typedef {import('.').AbortOptions} AbortOptions
35+
* @typedef {import('.').CID} CID
3436
*/

packages/ipfs-core/src/components/block/get.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ const { withTimeoutOption } = require('../../utils')
55

66
/**
77
* @param {Object} config
8-
* @param {import('..').IPFSBlockService} config.blockService
9-
* @param {import('..').Preload} config.preload
8+
* @param {import('.').BlockService} config.blockService
9+
* @param {import('.').Preload} config.preload
1010
*/
11-
module.exports = ({ blockService, preload }) => {
11+
module.exports = ({ preload, blockService }) => {
1212
/**
1313
* Get a raw IPFS block.
1414
*
@@ -22,14 +22,14 @@ module.exports = ({ blockService, preload }) => {
2222
* console.log(block.data)
2323
* ```
2424
*/
25-
async function get (cid, options = {}) { // eslint-disable-line require-await
25+
async function get (cid, options = {}) {
2626
cid = cleanCid(cid)
2727

2828
if (options.preload !== false) {
2929
preload(cid)
3030
}
3131

32-
return blockService.get(cid, options)
32+
return await blockService.get(cid, options)
3333
}
3434

3535
return withTimeoutOption(get)
@@ -39,7 +39,7 @@ module.exports = ({ blockService, preload }) => {
3939
* @typedef {Object} GetOptions
4040
* @property {boolean} [preload=true]
4141
*
42-
* @typedef {import('../../utils').AbortOptions} AbortOptions
43-
* @typedef {import('..').CID} CID
44-
* @typedef {import('..').IPLDBlock} IPLDBlock
42+
* @typedef {import('.').AbortOptions} AbortOptions
43+
* @typedef {import('.').CID} CID
44+
* @typedef {import('.').IPLDBlock} IPLDBlock
4545
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict'
2+
3+
const createGet = require('./get')
4+
const createPut = require('./put')
5+
const createRm = require('./rm')
6+
const createStat = require('./stat')
7+
8+
class BlockAPI {
9+
/**
10+
* @param {Object} config
11+
* @param {Preload} config.preload
12+
* @param {BlockService} config.blockService
13+
* @param {GCLock} config.gcLock
14+
* @param {Pin} config.pin
15+
* @param {PinManager} config.pinManager
16+
*/
17+
constructor ({ blockService, preload, gcLock, pinManager, pin }) {
18+
this.get = createGet({ blockService, preload })
19+
this.put = createPut({ blockService, preload, gcLock, pin })
20+
this.rm = createRm({ blockService, gcLock, pinManager })
21+
this.stat = createStat({ blockService, preload })
22+
}
23+
}
24+
25+
module.exports = BlockAPI
26+
27+
/**
28+
* @typedef {import('..').Preload} Preload
29+
* @typedef {import('..').BlockService} BlockService
30+
* @typedef {import('..').GCLock} GCLock
31+
* @typedef {import('..').Pin} Pin
32+
* @typedef {import('..').PinManager} PinManager
33+
* @typedef {import('..').AbortOptions} AbortOptions
34+
* @typedef {import('..').CID} CID
35+
* @typedef {import('..').IPLDBlock} IPLDBlock
36+
*/

0 commit comments

Comments
 (0)