forked from storacha/w3cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
can.js
290 lines (268 loc) · 7.49 KB
/
can.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* eslint-env browser */
import fs from 'node:fs'
import { Readable } from 'node:stream'
import * as Link from 'multiformats/link'
import * as raw from 'multiformats/codecs/raw'
import { base58btc } from 'multiformats/bases/base58'
import * as Digest from 'multiformats/hashes/digest'
import { Piece } from '@web3-storage/data-segment'
import ora from 'ora'
import {
getClient,
uploadListResponseToString,
storeListResponseToString,
filecoinInfoToString,
parseCarLink,
streamToBlob,
blobListResponseToString,
} from './lib.js'
/**
* @param {string} [blobPath]
*/
export async function blobAdd(blobPath) {
const client = await getClient()
const spinner = ora('Reading data').start()
/** @type {Blob} */
let blob
try {
blob = await streamToBlob(
/** @type {ReadableStream<Uint8Array>} */
(Readable.toWeb(blobPath ? fs.createReadStream(blobPath) : process.stdin))
)
} catch (/** @type {any} */ err) {
spinner.fail(`Error: failed to read data: ${err.message}`)
process.exit(1)
}
spinner.start('Storing')
const { multihash } = await client.capability.blob.add(blob, {
receiptsEndpoint: client._receiptsEndpoint.toString()
})
const cid = Link.create(raw.code, multihash)
spinner.stopAndPersist({ symbol: '⁂', text: `Stored ${base58btc.encode(multihash.bytes)} (${cid})` })
}
/**
* Print out all the blobs in the current space.
*
* @param {object} opts
* @param {boolean} [opts.json]
* @param {string} [opts.cursor]
* @param {number} [opts.size]
*/
export async function blobList(opts = {}) {
const client = await getClient()
const listOptions = {}
if (opts.size) {
listOptions.size = parseInt(String(opts.size))
}
if (opts.cursor) {
listOptions.cursor = opts.cursor
}
const spinner = ora('Listing Blobs').start()
const res = await client.capability.blob.list(listOptions)
spinner.stop()
console.log(blobListResponseToString(res, opts))
}
/**
* @param {string} digestStr
*/
export async function blobRemove(digestStr) {
const spinner = ora(`Removing ${digestStr}`).start()
let digest
try {
digest = Digest.decode(base58btc.decode(digestStr))
} catch {
spinner.fail(`Error: "${digestStr}" is not a base58btc encoded multihash`)
process.exit(1)
}
const client = await getClient()
try {
await client.capability.blob.remove(digest)
spinner.stopAndPersist({ symbol: '⁂', text: `Removed ${digestStr}` })
} catch (/** @type {any} */ err) {
spinner.fail(`Error: blob remove failed: ${err.message ?? err}`)
console.error(err)
process.exit(1)
}
}
/**
* @param {string} cidStr
*/
export async function indexAdd(cidStr) {
const client = await getClient()
const spinner = ora('Adding').start()
const cid = parseCarLink(cidStr)
if (!cid) {
spinner.fail(`Error: "${cidStr}" is not a valid index CID`)
process.exit(1)
}
await client.capability.index.add(cid)
spinner.stopAndPersist({ symbol: '⁂', text: `Added index ${cid}` })
}
/**
* @param {string} carPath
*/
export async function storeAdd(carPath) {
const client = await getClient()
const spinner = ora('Reading CAR').start()
/** @type {Blob} */
let blob
try {
const data = await fs.promises.readFile(carPath)
blob = new Blob([data])
} catch (/** @type {any} */ err) {
spinner.fail(`Error: failed to read CAR: ${err.message}`)
process.exit(1)
}
spinner.start('Storing')
const cid = await client.capability.store.add(blob)
console.log(cid.toString())
spinner.stopAndPersist({ symbol: '⁂', text: `Stored ${cid}` })
}
/**
* Print out all the CARs in the current space.
*
* @param {object} opts
* @param {boolean} [opts.json]
* @param {string} [opts.cursor]
* @param {number} [opts.size]
* @param {boolean} [opts.pre]
*/
export async function storeList(opts = {}) {
const client = await getClient()
const listOptions = {}
if (opts.size) {
listOptions.size = parseInt(String(opts.size))
}
if (opts.cursor) {
listOptions.cursor = opts.cursor
}
if (opts.pre) {
listOptions.pre = opts.pre
}
const spinner = ora('Listing CARs').start()
const res = await client.capability.store.list(listOptions)
spinner.stop()
console.log(storeListResponseToString(res, opts))
}
/**
* @param {string} cidStr
*/
export async function storeRemove(cidStr) {
const shard = parseCarLink(cidStr)
if (!shard) {
console.error(`Error: ${cidStr} is not a CAR CID`)
process.exit(1)
}
const client = await getClient()
try {
await client.capability.store.remove(shard)
} catch (/** @type {any} */ err) {
console.error(`Store remove failed: ${err.message ?? err}`)
console.error(err)
process.exit(1)
}
}
/**
* @param {string} root
* @param {string} shard
* @param {object} opts
* @param {string[]} opts._
*/
export async function uploadAdd(root, shard, opts) {
const client = await getClient()
let rootCID
try {
rootCID = Link.parse(root)
} catch (/** @type {any} */ err) {
console.error(`Error: failed to parse root CID: ${root}: ${err.message}`)
process.exit(1)
}
/** @type {import('@web3-storage/w3up-client/types').CARLink[]} */
const shards = []
for (const str of [shard, ...opts._]) {
try {
shards.push(Link.parse(str))
} catch (/** @type {any} */ err) {
console.error(`Error: failed to parse shard CID: ${str}: ${err.message}`)
process.exit(1)
}
}
const spinner = ora('Adding upload').start()
await client.capability.upload.add(rootCID, shards)
spinner.stopAndPersist({ symbol: '⁂', text: `Upload added ${rootCID}` })
}
/**
* Print out all the uploads in the current space.
*
* @param {object} opts
* @param {boolean} [opts.json]
* @param {boolean} [opts.shards]
* @param {string} [opts.cursor]
* @param {number} [opts.size]
* @param {boolean} [opts.pre]
*/
export async function uploadList(opts = {}) {
const client = await getClient()
const listOptions = {}
if (opts.size) {
listOptions.size = parseInt(String(opts.size))
}
if (opts.cursor) {
listOptions.cursor = opts.cursor
}
if (opts.pre) {
listOptions.pre = opts.pre
}
const spinner = ora('Listing uploads').start()
const res = await client.capability.upload.list(listOptions)
spinner.stop()
console.log(uploadListResponseToString(res, opts))
}
/**
* Remove the upload from the upload list.
*
* @param {string} rootCid
*/
export async function uploadRemove(rootCid) {
let root
try {
root = Link.parse(rootCid.trim())
} catch (/** @type {any} */ err) {
console.error(`Error: ${rootCid} is not a CID`)
process.exit(1)
}
const client = await getClient()
try {
await client.capability.upload.remove(root)
} catch (/** @type {any} */ err) {
console.error(`Upload remove failed: ${err.message ?? err}`)
console.error(err)
process.exit(1)
}
}
/**
* Get filecoin information for given PieceCid.
*
* @param {string} pieceCid
* @param {object} opts
* @param {boolean} [opts.json]
* @param {boolean} [opts.raw]
*/
export async function filecoinInfo(pieceCid, opts) {
let pieceInfo
try {
pieceInfo = Piece.fromString(pieceCid)
} catch (/** @type {any} */ err) {
console.error(`Error: ${pieceCid} is not a Link`)
process.exit(1)
}
const spinner = ora('Getting filecoin info').start()
const client = await getClient()
const info = await client.capability.filecoin.info(pieceInfo.link)
if (info.out.error) {
spinner.fail(`Error: failed to get filecoin info: ${info.out.error.message}`)
process.exit(1)
}
spinner.stop()
console.log(filecoinInfoToString(info.out.ok, opts))
}