This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathutils.js
67 lines (51 loc) · 1.51 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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`)
}