This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathin-process-node.js
73 lines (61 loc) · 1.72 KB
/
in-process-node.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
// ordinarily we'd open a PR against the multicodec module to get our
// codec number added but since we're just testing we shim our new
// codec into the base-table.json file - this has to be done
// before requiring other modules as the int table will become read-only
const codecName = 'dag-test'
const codecNumber = 392091
const baseTable = require('multicodec/src/base-table.json')
baseTable[codecName] = codecNumber
// now require modules as usual
const IPFS = require('ipfs-core')
const multihashing = require('multihashing-async')
const multicodec = require('multicodec')
const CID = require('cids')
async function main () {
// see https://github.com/ipld/interface-ipld-format for the interface definition
const format = {
codec: codecNumber,
defaultHashAlg: multicodec.SHA2_256,
util: {
serialize (data) {
return Buffer.from(JSON.stringify(data))
},
deserialize (buf) {
return JSON.parse(buf.toString('utf8'))
},
async cid (buf) {
const multihash = await multihashing(buf, format.defaultHashAlg)
return new CID(1, format.codec, multihash)
}
}
}
const node = await IPFS.create({
ipld: {
formats: [
format
]
}
})
const data = {
hello: 'world'
}
const cid = await node.dag.put(data, {
format: codecName,
hashAlg: format.defaultHashAlg
})
console.info(`Put ${JSON.stringify(data)} = CID(${cid})`)
const {
value
} = await node.dag.get(cid)
console.info(`Get CID(${cid}) = ${JSON.stringify(value)}`)
await node.stop()
}
main()
.catch(err => {
console.error(err)
process.exit(1)
})
.then(() => {
// https://github.com/libp2p/js-libp2p/issues/779
process.exit(0)
})