UNPKG

@ethersphere/bee-js

Version:
81 lines (80 loc) 3.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.probeFeed = exports.fetchLatestFeedUpdate = exports.createFeedManifest = void 0; const cafe_utility_1 = require("cafe-utility"); const bytes_1 = require("../utils/bytes"); const error_1 = require("../utils/error"); const headers_1 = require("../utils/headers"); const http_1 = require("../utils/http"); const typed_bytes_1 = require("../utils/typed-bytes"); const feedEndpoint = 'feeds'; /** * Create an initial feed root manifest * * @param requestOptions Options for making requests * @param owner Owner's ethereum address in hex * @param topic Topic in hex * @param postageBatchId Postage BatchId to be used to create the Feed Manifest * @param options Additional options, like type (default: 'sequence') */ async function createFeedManifest(requestOptions, owner, topic, stamp, options) { const response = await (0, http_1.http)(requestOptions, { method: 'post', responseType: 'json', url: `${feedEndpoint}/${owner}/${topic}`, headers: (0, headers_1.prepareRequestHeaders)(stamp, options), }); const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' }); return new typed_bytes_1.Reference(cafe_utility_1.Types.asHexString(body.reference)); } exports.createFeedManifest = createFeedManifest; function readFeedUpdateHeaders(headers) { const feedIndex = headers['swarm-feed-index']; const feedIndexNext = headers['swarm-feed-index-next']; if (!feedIndex) { throw new error_1.BeeError('Response did not contain expected swarm-feed-index!'); } if (!feedIndexNext) { throw new error_1.BeeError('Response did not contain expected swarm-feed-index-next!'); } return { feedIndex: new typed_bytes_1.FeedIndex(feedIndex), feedIndexNext: new typed_bytes_1.FeedIndex(feedIndexNext), }; } /** * Find and retrieve feed update * * The feed consists of updates. This endpoint looks up an * update that matches the provided parameters and returns * the reference it contains along with its index and the * index of the subsequent update. * * @param requestOptions Options for making requests * @param owner Owner's ethereum address * @param topic Topic * @param options Additional options, like index, at, type */ async function fetchLatestFeedUpdate(requestOptions, owner, topic, options) { const response = await (0, http_1.http)(requestOptions, { responseType: 'arraybuffer', url: `${feedEndpoint}/${owner}/${topic}`, params: options, }); return { payload: new bytes_1.Bytes(response.data), ...readFeedUpdateHeaders(response.headers), }; } exports.fetchLatestFeedUpdate = fetchLatestFeedUpdate; async function probeFeed(requestOptions, owner, topic) { const response = await (0, http_1.http)(requestOptions, { responseType: 'arraybuffer', url: `${feedEndpoint}/${owner}/${topic}`, params: { 'Swarm-Only-Root-Chunk': true, }, }); return readFeedUpdateHeaders(response.headers); } exports.probeFeed = probeFeed;