Swarm Feed Sequential
Swarm Feed Sequential is a previously unreleased FDP library that implements Swarm Feeds as Sequential type. There are three types of feeds: Epoch, Sequential and Streaming.
You can use it as off-chain, verifiable append-only log, either client or server side.
Additionally, this library includes FeedStorage
and BeesonMultiformatFeedStorage
, where you can use to store Uint8Array values or JSON values formatted as IPLD Beeson blocks.
npm install swarm-feed-sequential
Creates a SequentialFeed instance.
const f = new SequentialFeed(fdp.connection.bee)
Creates a sequential feed reader - SwarmFeedR
const topic = `/crdt/document/test1`
const address = `0x1111111254fb6c44bac0bed2854e76f90643097d`
const feedR = f.makeFeedR(topic, address);
Creates a sequential feed reader/writer - SwarmFeedRW
const topic = `/crdt/document/test1`
const privateKey = `.....`
const feedR = f.makeFeedRW(topic, privateKey);
Gets the last update in the feed
const feedUpdate = await feedR.getLastUpdate()
Gets the last index in the feed
const feedIndex = await feedR.getLastIndex()
Gets the last appended chunk in the feed.
const chunk = await feedR.findLastUpdate()
Downloads a chunk by index number.
const index = 0
const chunk = feedR.getUpdate(index)
Download all chunk by indices.
const indices = [0, 1, 2]
const chunks = feedR.getUpdates(indices)
Sets the upload chunk to update.
import * as Block from "multiformats/block";
import { codec, hasher } from "@fairdatasociety/beeson-multiformats";
import { BeeSon, Type } from "@fairdatasociety/beeson";
import { SequentialFeed } from "./sequential-feed";
import { Bee } from "@ethersphere/bee-js";
import { BlockDecoder } from "multiformats/block";
import { JsonValue } from "@fairdatasociety/beeson/dist/types";
const bee = new Bee()
const postageBatchId = `...`
const topic = `/crdt/document/test1`
const signer = makePrivateKeySigner(`...`)
const feedRW = f.makeFeedRW(topic, signer);
const beeson = new BeeSon({
json: {
...state,
timestamp: Date.now(),
},
});
const value = beeson;
// encode a block
const block = await Block.encode({ value, codec, hasher });
const reference = await bee.uploadData(
postageBatchId,
block.bytes
);
const index = 1
return feedRW.setUpdate(index, postageBatchId, reference.reference);
Sets the next upload chunk.
import * as Block from "multiformats/block";
import { codec, hasher } from "@fairdatasociety/beeson-multiformats";
import { BeeSon, Type } from "@fairdatasociety/beeson";
import { SequentialFeed } from "./sequential-feed";
import { Bee } from "@ethersphere/bee-js";
import { BlockDecoder } from "multiformats/block";
import { JsonValue } from "@fairdatasociety/beeson/dist/types";
const bee = new Bee()
const postageBatchId = `...`
const topic = `/crdt/document/test1`
const signer = makePrivateKeySigner(`...`)
const feedRW = f.makeFeedRW(topic, signer);
const beeson = new BeeSon({
json: {
...state,
timestamp: Date.now(),
},
});
const value = beeson;
// encode a block
const block = await Block.encode({ value, codec, hasher });
const reference = await bee.uploadData(
postageBatchId,
block.bytes
);
const index = 1
return feedRW.setLastUpdate(postageBatchId, reference.reference);