forked from nftstorage/nft.storage-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster-car-upload.js
48 lines (39 loc) · 1.23 KB
/
cluster-car-upload.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
/**
* Upload a CAR file to IPFS Cluster.
*
* TODO: CAR is buffered in memory
*
* Usage:
* node cluster-car-upload.js path/to/file.car
*/
import fs from 'fs/promises'
import path from 'path'
import dotenv from 'dotenv'
import { Cluster } from '@nftstorage/ipfs-cluster'
import fetch from '@web-std/fetch'
import { FormData } from '@web-std/form-data'
import { File, Blob } from '@web-std/file'
Object.assign(global, { fetch, File, Blob, FormData })
dotenv.config()
async function main () {
if (!process.env.CLUSTER_URL) {
throw new Error('missing IPFS Cluster URL')
}
console.log(`🔌 Using IPFS Cluster URL: ${process.env.CLUSTER_URL}`)
const carPath = process.argv[2]
if (!carPath) {
throw new Error('missing CAR file argument')
}
const data = await fs.readFile(carPath)
const file = new File([data], path.basename(carPath), { type: 'application/car' })
const headers = process.env.CLUSTER_HEADERS ? JSON.parse(process.env.CLUSTER_HEADERS) : {}
const cluster = new Cluster(process.env.CLUSTER_URL, { headers })
try {
const res = await cluster.add(file, { local: true })
console.log(res)
} catch (err) {
console.error(err)
if (err.response) console.error(await err.response.text())
}
}
main()