-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdump-car.js
executable file
·60 lines (51 loc) · 1.76 KB
/
dump-car.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
#!/usr/bin/env node
/* eslint-disable no-console */
// Take a .car file and dump its contents into one file per block, with the
// filename being the CID of that block.
// Also prints a DAG-JSON form of the block and its CID to stdout.
// If `--inspect` is supplied, don't write the blocks, just print them to stdout.
import fs from 'fs'
import { CarBlockIterator } from '@ipld/car/iterator'
import * as dagCbor from '@ipld/dag-cbor'
import * as dagJson from '@ipld/dag-json'
import * as dagPb from '@ipld/dag-pb'
import * as json from 'multiformats/codecs/json'
import * as raw from 'multiformats/codecs/raw'
if (!process.argv[2]) {
console.log('Usage: dump-car.js [--inspect] <path/to/car>')
process.exit(1)
}
const codecs = {
[dagCbor.code]: dagCbor,
[dagPb.code]: dagPb,
[dagJson.code]: dagJson,
[raw.code]: raw,
[json.code]: json
}
function decode (cid, bytes) {
if (!codecs[cid.code]) {
throw new Error(`Unknown codec code: 0x${cid.code.toString(16)}`)
}
return codecs[cid.code].decode(bytes)
}
async function run () {
const inspect = process.argv.includes('--inspect')
const inStream = fs.createReadStream(process.argv.filter((a) => a !== '--inspect')[2])
const reader = await CarBlockIterator.fromIterable(inStream)
console.log(`Version: ${reader.version}`)
console.log(`Roots: [${(await reader.getRoots()).map((r) => r.toString()).join(', ')}]`)
console.log('Blocks:')
let i = 1
for await (const { cid, bytes } of reader) {
if (!inspect) {
await fs.promises.writeFile(cid.toString(), bytes)
}
const decoded = decode(cid, bytes)
console.log(`#${i++} ${cid} [${codecs[cid.code].name}]`)
console.dir(new TextDecoder().decode(dagJson.encode(decoded)))
}
}
run().catch((err) => {
console.error(err)
process.exit(1)
})