interface-ipfs-core
Version:
A test suite and interface you can use to implement a IPFS core interface.
68 lines (51 loc) • 1.55 kB
JavaScript
import delay from 'delay'
/**
* @typedef {import('@libp2p/interface-peer-id').PeerId} PeerId
*/
/**
* @param {import('ipfs-core-types').IPFS} ipfs
* @param {string} key
* @param {{ timeout?: number, interval?: number, peerId?: PeerId }} [opts]
*/
export async function waitForWantlistKey (ipfs, key, opts = {}) {
opts.timeout = opts.timeout || 10000
opts.interval = opts.interval || 100
const end = Date.now() + opts.timeout
while (Date.now() < end) {
let list
if (opts.peerId) {
list = await ipfs.bitswap.wantlistForPeer(opts.peerId)
} else {
list = await ipfs.bitswap.wantlist()
}
if (list.some(cid => cid.toString() === key)) {
return
}
await delay(opts.interval)
}
throw new Error(`Timed out waiting for ${key} in wantlist`)
}
/**
* @param {import('ipfs-core-types').IPFS} ipfs
* @param {string} key
* @param {{ timeout?: number, interval?: number, peerId?: PeerId }} [opts]
*/
export async function waitForWantlistKeyToBeRemoved (ipfs, key, opts = {}) {
opts.timeout = opts.timeout || 10000
opts.interval = opts.interval || 100
const end = Date.now() + opts.timeout
while (Date.now() < end) {
let list
if (opts.peerId) {
list = await ipfs.bitswap.wantlistForPeer(opts.peerId)
} else {
list = await ipfs.bitswap.wantlist()
}
if (list.some(cid => cid.toString() === key)) {
await delay(opts.interval)
continue
}
return
}
throw new Error(`Timed out waiting for ${key} to be removed from wantlist`)
}