Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions examples/custom-ipld-formats/daemon-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,24 @@ async function main () {
hello: 'world'
}

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

console.info(`Put ${JSON.stringify(data)} = CID(${cid})`)
console.info(`BLOCK Put ${JSON.stringify(data)} = CID(${cid})`)

const {
value
} = await client.dag.get(cid)
// as with PUT, we can't use the DAG API to get the block unless the server
// knows how about the codec, instead we use the BLOCK API to get the raw
// bytes and decode it locally
const bytes = await client.block.get(cid)
const value = codec.decode(bytes)

console.info(`Get CID(${cid}) = ${JSON.stringify(value)}`)
console.info(`BLOCK Get CID(${cid}) = ${JSON.stringify(value)}`)

await daemon.stop()
}
Expand Down
44 changes: 35 additions & 9 deletions examples/custom-ipld-formats/in-process-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,44 @@ async function main () {
hello: 'world'
}

const cid = await node.dag.put(data, {
format: 'dag-test',
hashAlg: 'sha2-256'
})
// we can use the DAG API for an in-process IPFS since we're adding the codec
// directly into IPFS which will do the encoding and decoding
const dagApi = async () => {
const cid = await node.dag.put(data, {
storeCodec: 'dag-test',
hashAlg: 'sha2-256'
})

console.info(`DAG Put ${JSON.stringify(data)} = CID(${cid})`)

const {
value
} = await node.dag.get(cid)

console.info(`DAG Get CID(${cid}) = ${JSON.stringify(value)}`)
}

// alternatively we can use the codec directly and put the encoded bytes
// into IPFS using the BLOCK API and then decode with the codec from the
// bytes fetched from IPFS
const blockApi = async () => {
const encoded = codec.encode(data)
const cid = await node.block.put(encoded, {
format: 'dag-test',
mhtype: 'sha2-256',
version: 1
})

console.info(`Put ${JSON.stringify(data)} = CID(${cid})`)
console.info(`BLOCK Put ${JSON.stringify(data)} = CID(${cid})`)

const {
value
} = await node.dag.get(cid)
const bytes = await node.block.get(cid)
const value = codec.decode(bytes)

console.info(`BLOCK Get CID(${cid}) = ${JSON.stringify(value)}`)
}

console.info(`Get CID(${cid}) = ${JSON.stringify(value)}`)
await dagApi()
await blockApi()

await node.stop()
}
Expand Down
15 changes: 9 additions & 6 deletions examples/custom-ipld-formats/tests/test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
'use strict'

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

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

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

async function test () {
Expand All @@ -23,4 +26,4 @@ async function test () {
await testDaemonNode()
}

test();
test()