-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathipfs-cluster-api.js
144 lines (135 loc) · 3.63 KB
/
ipfs-cluster-api.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const delay = require('delay')
require('isomorphic-fetch')
let apiBase = process.env.IPFS_CLUSTER_API
const user = process.env.IPFS_CLUSTER_USER
const pw = process.env.IPFS_CLUSTER_PASSWORD
const auth = Buffer.from(`${user}:${pw}`).toString('base64')
const pinningTimeout = 5 * 60 * 1000 // Give up after 5 minutes
function log (...args) {
console.log('pinner cluster api:', ...args)
}
function useTunnel () {
// Override when using libp2p tunnel
apiBase = 'http://127.0.0.1:29097'
}
async function pin (cid, version, peerId) {
const apiBaseUrl = new URL(apiBase)
let name = `peer-base-pinner: ${version} ${peerId}`
if (process.env.IPFS_CLUSTER_LABEL) {
name += ` ${process.env.IPFS_CLUSTER_LABEL}`
}
const opts = `name=${encodeURIComponent(name)}`
const apiPinAdd = new URL(`/pins/${cid}?${opts}`, apiBaseUrl)
log(`pinning ${cid} to cluster, version ${version}`)
const start = Date.now()
const res = await fetch(
apiPinAdd.href,
{
method: 'POST',
headers: {
'Authorization': `Basic ${auth}`
}
}
)
if (!res.ok) {
log('Error:', res.status, res.statusText)
throw new Error('Pin add failed')
}
const apiPinStatus = new URL(`/pins/${cid}`, apiBaseUrl)
let count = 0
while (true) {
count++
if ((count % 10) === 1) {
const elapsed = `(${((Date.now() - start) / 1000).toFixed(1)}s)`
log(`waiting for ${cid} status`, elapsed)
}
const res = await fetch(
apiPinStatus.href,
{
headers: {
'Authorization': `Basic ${auth}`
}
}
)
if (!res.ok) {
throw new Error('Pin status failed')
}
const json = await res.json()
// log(JSON.stringify(json, null, 2))
if (
json.peer_map &&
json.peer_map
) {
const finished = Object.keys(json.peer_map).some(peerId => {
const peer = json.peer_map[peerId]
return peer.status === 'pinned'
})
if (finished) {
const elapsed = `(${((Date.now() - start) / 1000).toFixed(1)}s)`
log('pinned', version, cid, elapsed)
break
}
const notPinning = Object.keys(json.peer_map).some(peerId => {
const peer = json.peer_map[peerId]
return (
peer.status !== 'pinning' &&
peer.status !== 'remote' &&
peer.status !== 'unpinned' &&
peer.status !== 'pin_error' &&
peer.status !== 'pin_queued' &&
peer.status !== 'cluster_error'
)
})
const timedOut = Date.now() > start + pinningTimeout
if (timedOut) log('Timed out')
if (notPinning || timedOut) {
const elapsed = `(${((Date.now() - start) / 1000).toFixed(1)}s)`
log('Aborting ipfs-cluster pin', elapsed)
log(JSON.stringify(json, null, 2))
throw new Error('pinning failed')
}
}
await delay(1000)
}
}
async function unpin (cid) {
const apiBaseUrl = new URL(apiBase)
const apiPinRm = new URL(`/pins/${cid}`, apiBaseUrl)
log(`unpinning ${cid} from cluster`)
const res = await fetch(
apiPinRm.href,
{
method: 'DELETE',
headers: {
'Authorization': `Basic ${auth}`
}
}
)
if (!res.ok) {
throw new Error('Pin rm failed')
}
}
async function getPins () {
const apiBaseUrl = new URL(apiBase)
const apiPinLs = new URL(`/allocations?filter=all`, apiBaseUrl)
log(`listing pins on cluster`)
const res = await fetch(
apiPinLs.href,
{
headers: {
'Authorization': `Basic ${auth}`
}
}
)
if (!res.ok) {
throw new Error('Pin ls failed')
}
const json = await res.json()
return json
}
module.exports = {
useTunnel,
pin,
unpin,
getPins
}