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

Commit

Permalink
chore: make IPFS api static
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Oct 31, 2020
1 parent b5ea76a commit 084b213
Show file tree
Hide file tree
Showing 112 changed files with 3,727 additions and 2,124 deletions.
28 changes: 13 additions & 15 deletions packages/ipfs-cli/src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,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
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 @@ -178,4 +178,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('../../utils')

/**
* @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 as Strings
* @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('../../utils')

/**
* @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('../../utils')

/**
* @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
16 changes: 9 additions & 7 deletions packages/ipfs-core/src/components/bitswap/wantlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ const { withTimeoutOption } = require('../../utils')

/**
* @param {Object} config
* @param {import('..').IPFSBitSwap} config.bitswap
* @param {import('.').NetworkService} config.network
*/
module.exports = ({ bitswap }) => {
module.exports = ({ network }) => {
/**
* Returns the wantlist for your node
*
* @param {AbortOptions} [options]
* @returns {Promise<CID[]>} - An array of CIDs currently in the wantlist.
* @example
* ```js
* const list = await ipfs.bitswap.wantlist()
* console.log(list)
* // [ CID('QmHash') ]
* ```
*
* @param {AbortOptions} [options]
* @returns {Promise<CID[]>} - An array of CIDs currently in the wantlist.
*/
async function wantlist (options = {}) { // eslint-disable-line require-await
async function wantlist (options = {}) {
const { bitswap } = await network.use(options)
const list = bitswap.getWantlist(options)

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

/**
* @typedef {import('../../utils').AbortOptions} AbortOptions
* @typedef {import('..').CID} CID
* @typedef {import('.').AbortOptions} AbortOptions
* @typedef {import('.').CID} CID
*/
16 changes: 8 additions & 8 deletions packages/ipfs-core/src/components/block/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const { withTimeoutOption } = require('../../utils')

/**
* @param {Object} config
* @param {import('..').IPFSBlockService} config.blockService
* @param {import('..').Preload} config.preload
* @param {import('.').BlockService} config.blockService
* @param {import('.').Preload} config.preload
*/
module.exports = ({ blockService, preload }) => {
module.exports = ({ preload, blockService }) => {
/**
* Get a raw IPFS block.
*
Expand All @@ -22,14 +22,14 @@ module.exports = ({ blockService, preload }) => {
* console.log(block.data)
* ```
*/
async function get (cid, options = {}) { // eslint-disable-line require-await
async function get (cid, options = {}) {
cid = cleanCid(cid)

if (options.preload !== false) {
preload(cid)
}

return blockService.get(cid, options)
return await blockService.get(cid, options)
}

return withTimeoutOption(get)
Expand All @@ -39,7 +39,7 @@ module.exports = ({ blockService, preload }) => {
* @typedef {Object} GetOptions
* @property {boolean} [preload=true]
*
* @typedef {import('../../utils').AbortOptions} AbortOptions
* @typedef {import('..').CID} CID
* @typedef {import('..').IPLDBlock} IPLDBlock
* @typedef {import('.').AbortOptions} AbortOptions
* @typedef {import('.').CID} CID
* @typedef {import('.').IPLDBlock} IPLDBlock
*/
36 changes: 36 additions & 0 deletions packages/ipfs-core/src/components/block/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

const createGet = require('./get')
const createPut = require('./put')
const createRm = require('./rm')
const createStat = require('./stat')

class BlockAPI {
/**
* @param {Object} config
* @param {Preload} config.preload
* @param {BlockService} config.blockService
* @param {GCLock} config.gcLock
* @param {Pin} config.pin
* @param {PinManager} config.pinManager
*/
constructor ({ blockService, preload, gcLock, pinManager, pin }) {
this.get = createGet({ blockService, preload })
this.put = createPut({ blockService, preload, gcLock, pin })
this.rm = createRm({ blockService, gcLock, pinManager })
this.stat = createStat({ blockService, preload })
}
}

module.exports = BlockAPI

/**
* @typedef {import('..').Preload} Preload
* @typedef {import('..').BlockService} BlockService
* @typedef {import('..').GCLock} GCLock
* @typedef {import('..').Pin} Pin
* @typedef {import('..').PinManager} PinManager
* @typedef {import('..').AbortOptions} AbortOptions
* @typedef {import('..').CID} CID
* @typedef {import('..').IPLDBlock} IPLDBlock
*/
Loading

0 comments on commit 084b213

Please sign in to comment.