-
Notifications
You must be signed in to change notification settings - Fork 19
/
cluster-single-status.js
58 lines (48 loc) · 1.64 KB
/
cluster-single-status.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
/**
* Print the status and DAG size of a CID from IPFS Cluster. Status is expressed
* as a Pinning Services API status: "queued"/"pinning"/"pinned"/"failed" which
* is derived from the "most progressed" status among all nodes in the Cluster.
*
* Usage:
* node cluster-single-status.js bafybeia4eil5y73ooy3e7mhyk5dvcaml3ket64q6f6u5z4ozbxctw53r6i
*/
import dotenv from 'dotenv'
import { Cluster } from '@nftstorage/ipfs-cluster'
import fetch from '@web-std/fetch'
import * as d3 from 'd3-format'
import { toPSAStatus } from './lib/cluster.js'
global.fetch = fetch
dotenv.config()
const format = d3.format(',')
async function main () {
if (!process.env.CLUSTER_URL) {
throw new Error('missing IPFS Cluster URL')
}
const cid = process.argv[2]
if (!cid) {
throw new Error('missing CID argument')
}
console.log(`🔌 Using IPFS Cluster URL: ${process.env.CLUSTER_URL}`)
const headers = process.env.CLUSTER_HEADERS ? JSON.parse(process.env.CLUSTER_HEADERS) : {}
const cluster = new Cluster(process.env.CLUSTER_URL, { headers })
console.log(toPSAStatus(await cluster.status(cid)))
console.log(`${format(await dagSize(cid))} bytes`)
}
main()
async function dagSize (cid) {
const url = new URL(
`v0/dag/stat?arg=${encodeURIComponent(cid)}&progress=false`,
process.env.CLUSTER_URL
)
const response = await fetch(url.toString(), {
headers: process.env.CLUSTER_HEADERS ? JSON.parse(process.env.CLUSTER_HEADERS) : {}
})
if (!response.ok) {
throw Object.assign(
new Error(`${response.status}: ${response.statusText}`),
{ response }
)
}
const data = await response.json()
return parseInt(data.Size)
}