forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin-process-node.js
52 lines (42 loc) · 1.08 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
'use strict'
const IPFS = require('ipfs-core')
const uint8ArrayToString = require('uint8arrays/to-string')
const uint8ArrayFromString = require('uint8arrays/from-string')
async function main () {
// see https://github.com/multiformats/js-multiformats#multicodec-encoders--decoders--codecs for the interface definition
const codec = {
name: 'dag-test',
code: 392091,
encode: (data) => uint8ArrayFromString(JSON.stringify(data)),
decode: (buf) => JSON.parse(uint8ArrayToString(buf))
}
const node = await IPFS.create({
ipld: {
codecs: [
codec
]
}
})
const data = {
hello: 'world'
}
const cid = await node.dag.put(data, {
format: 'dag-test',
hashAlg: 'sha2-256'
})
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)
})