Skip to content

Commit c658a51

Browse files
committed
fix: update for v0.10 DAG API, demonstrate BLOCK vs DAG
Ref: ipfs/js-ipfs#3917
1 parent b0f9639 commit c658a51

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

examples/custom-ipld-formats/daemon-node.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,24 @@ async function main () {
3636
hello: 'world'
3737
}
3838

39-
const cid = await client.dag.put(data, {
39+
// we cannot use the DAG API to put a custom codec unless that codec is on
40+
// the server and can handle our input data, but the BLOCK API accepts our
41+
// encoded bytes and "format"
42+
const encoded = codec.encode(data)
43+
const cid = await client.block.put(encoded, {
4044
format: 'dag-test',
41-
hashAlg: 'sha2-256'
45+
mhtype: 'sha2-256'
4246
})
4347

44-
console.info(`Put ${JSON.stringify(data)} = CID(${cid})`)
48+
console.info(`BLOCK Put ${JSON.stringify(data)} = CID(${cid})`)
4549

46-
const {
47-
value
48-
} = await client.dag.get(cid)
50+
// as with PUT, we can't use the DAG API to get the block unless the server
51+
// knows how about the codec, instead we use the BLOCK API to get the raw
52+
// bytes and decode it locally
53+
const bytes = await client.block.get(cid)
54+
const value = codec.decode(bytes)
4955

50-
console.info(`Get CID(${cid}) = ${JSON.stringify(value)}`)
56+
console.info(`BLOCK Get CID(${cid}) = ${JSON.stringify(value)}`)
5157

5258
await daemon.stop()
5359
}

examples/custom-ipld-formats/in-process-node.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,44 @@ async function main () {
2525
hello: 'world'
2626
}
2727

28-
const cid = await node.dag.put(data, {
29-
format: 'dag-test',
30-
hashAlg: 'sha2-256'
31-
})
28+
// we can use the DAG API for an in-process IPFS since we're adding the codec
29+
// directly into IPFS which will do the encoding and decoding
30+
const dagApi = async () => {
31+
const cid = await node.dag.put(data, {
32+
storeCodec: 'dag-test',
33+
hashAlg: 'sha2-256'
34+
})
35+
36+
console.info(`DAG Put ${JSON.stringify(data)} = CID(${cid})`)
37+
38+
const {
39+
value
40+
} = await node.dag.get(cid)
41+
42+
console.info(`DAG Get CID(${cid}) = ${JSON.stringify(value)}`)
43+
}
44+
45+
// alternatively we can use the codec directly and put the encoded bytes
46+
// into IPFS using the BLOCK API and then decode with the codec from the
47+
// bytes fetched from IPFS
48+
const blockApi = async () => {
49+
const encoded = codec.encode(data)
50+
const cid = await node.block.put(encoded, {
51+
format: 'dag-test',
52+
mhtype: 'sha2-256',
53+
version: 1
54+
})
3255

33-
console.info(`Put ${JSON.stringify(data)} = CID(${cid})`)
56+
console.info(`BLOCK Put ${JSON.stringify(data)} = CID(${cid})`)
3457

35-
const {
36-
value
37-
} = await node.dag.get(cid)
58+
const bytes = await node.block.get(cid)
59+
const value = codec.decode(bytes)
60+
61+
console.info(`BLOCK Get CID(${cid}) = ${JSON.stringify(value)}`)
62+
}
3863

39-
console.info(`Get CID(${cid}) = ${JSON.stringify(value)}`)
64+
await dagApi()
65+
await blockApi()
4066

4167
await node.stop()
4268
}
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
'use strict'
22

33
const path = require('path')
4-
const { node } = require('test-util-ipfs-example');
4+
const { node } = require('test-util-ipfs-example')
55

66
const testInProcessNode = async () => {
77
await node.waitForOutput(
8-
'Put {"hello":"world"} = CID(bagn7ofysecj2eolrvekol2wl6cuneukuzwrqtq6by4x3xgiu2r6gb46lnakyq)\n' +
9-
'Get CID(bagn7ofysecj2eolrvekol2wl6cuneukuzwrqtq6by4x3xgiu2r6gb46lnakyq) = {"hello":"world"}', 'node', [path.resolve(__dirname, '../in-process-node.js')])
8+
'DAG Put {"hello":"world"} = CID(bagn7ofysecj2eolrvekol2wl6cuneukuzwrqtq6by4x3xgiu2r6gb46lnakyq)\n' +
9+
'DAG Get CID(bagn7ofysecj2eolrvekol2wl6cuneukuzwrqtq6by4x3xgiu2r6gb46lnakyq) = {"hello":"world"}\n' +
10+
'BLOCK Put {"hello":"world"} = CID(bagn7ofysecj2eolrvekol2wl6cuneukuzwrqtq6by4x3xgiu2r6gb46lnakyq)\n' +
11+
'BLOCK Get CID(bagn7ofysecj2eolrvekol2wl6cuneukuzwrqtq6by4x3xgiu2r6gb46lnakyq) = {"hello":"world"}',
12+
'node', [path.resolve(__dirname, '../in-process-node.js')])
1013
}
1114

1215
const testDaemonNode = async () => {
1316
await node.waitForOutput(
14-
'Put {"hello":"world"} = CID(bagn7ofysecj2eolrvekol2wl6cuneukuzwrqtq6by4x3xgiu2r6gb46lnakyq)\n' +
15-
'Get CID(bagn7ofysecj2eolrvekol2wl6cuneukuzwrqtq6by4x3xgiu2r6gb46lnakyq) = {"hello":"world"}', 'node', [path.resolve(__dirname, '../daemon-node.js')])
17+
'BLOCK Put {"hello":"world"} = CID(bagn7ofysecj2eolrvekol2wl6cuneukuzwrqtq6by4x3xgiu2r6gb46lnakyq)\n' +
18+
'BLOCK Get CID(bagn7ofysecj2eolrvekol2wl6cuneukuzwrqtq6by4x3xgiu2r6gb46lnakyq) = {"hello":"world"}', 'node', [path.resolve(__dirname, '../daemon-node.js')])
1619
}
1720

1821
async function test () {
@@ -23,4 +26,4 @@ async function test () {
2326
await testDaemonNode()
2427
}
2528

26-
test();
29+
test()

0 commit comments

Comments
 (0)