multiformats
Advanced tools
Comparing version 9.4.14 to 9.5.0
{ | ||
"name": "multiformats", | ||
"version": "9.4.14", | ||
"version": "9.5.0", | ||
"description": "Interface for multihash, multicodec, multibase and CID", | ||
@@ -58,2 +58,7 @@ "main": "./cjs/src/index.js", | ||
}, | ||
"./traversal": { | ||
"browser": "./esm/src/traversal.js", | ||
"require": "./cjs/src/traversal.js", | ||
"import": "./esm/src/traversal.js" | ||
}, | ||
"./bases/identity": { | ||
@@ -136,2 +141,3 @@ "browser": "./esm/src/bases/identity.js", | ||
"devDependencies": { | ||
"@ipld/dag-pb": "^2.1.14", | ||
"@types/node": "^16.7.10", | ||
@@ -182,2 +188,3 @@ "@typescript-eslint/eslint-plugin": "^4.30.0", | ||
"./block": "./cjs/src/block.js", | ||
"./traversal": "./cjs/src/traversal.js", | ||
"./bases/identity": "./cjs/src/bases/identity.js", | ||
@@ -184,0 +191,0 @@ "./bases/base2": "./cjs/src/bases/base2.js", |
@@ -8,2 +8,3 @@ # multiformats | ||
* [Multihash Hashers](#multihash-hashers) | ||
* [Traversal](#traversal) | ||
* [Legacy interface](#legacy-interface) | ||
@@ -141,2 +142,38 @@ * [Implementations](#implementations) | ||
### Traversal | ||
This library contains higher-order functions for traversing graphs of data easily. | ||
`walk()` walks through the links in each block of a DAG calling a user-supplied loader function for each one, in depth-first order with no duplicate block visits. The loader should return a `Block` object and can be used to inspect and collect block ordering for a full DAG walk. The loader should `throw` on error, and return `null` if a block should be skipped by `walk()`. | ||
```js | ||
import { walk } from 'multiformats/traversal' | ||
import * as Block from 'multiformats/block' | ||
import * as codec from 'multiformats/codecs/json' | ||
import { sha256 as hasher } from 'multiformats/hashes/sha2' | ||
// build a DAG (a single block for this simple example) | ||
const value = { hello: 'world' } | ||
const block = await Block.encode({ value, codec, hasher }) | ||
const { cid } = block | ||
console.log(cid) | ||
//> CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea) | ||
// create a loader function that also collects CIDs of blocks in | ||
// their traversal order | ||
const load = (cid, blocks) => async (cid) => { | ||
// fetch a block using its cid | ||
// e.g.: const block = await fetchBlockByCID(cid) | ||
blocks.push(cid) | ||
return block | ||
} | ||
// collect blocks in this DAG starting from the root `cid` | ||
const blocks = [] | ||
await walk({ cid, load: load(cid, blocks) }) | ||
console.log(blocks) | ||
//> [CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)] | ||
``` | ||
## Legacy interface | ||
@@ -143,0 +180,0 @@ |
463399
218
14449
228
13