Skip to content

Commit

Permalink
feat: use soc api
Browse files Browse the repository at this point in the history
  • Loading branch information
nugaon committed Jun 6, 2024
1 parent 6bead63 commit 8b9b6de
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
47 changes: 34 additions & 13 deletions src/feed/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { uploadSingleOwnerChunkData } from '../chunk/soc'
import { Chunk } from '../chunk/cac'
import { FeedUpdateOptions, FetchFeedUpdateResponse, fetchFeedUpdate } from '../modules/feed'
import { ChunkParam } from '../chunk/cac'
import { FeedUpdateOptions, FetchFeedUpdateResponse, fetchLatestFeedUpdate } from '../modules/feed'
import * as socAPI from '../modules/soc'
import {
Address,
BatchId,
BeeRequestOptions,
BytesReference,
Data,
FEED_INDEX_HEX_LENGTH,
FeedReader,
FeedWriter,
Expand All @@ -18,7 +19,7 @@ import {
import { Bytes, makeBytes } from '../utils/bytes'
import { EthAddress, HexEthAddress, makeHexEthAddress } from '../utils/eth'
import { keccak256Hash } from '../utils/hash'
import { HexString, bytesToHex, makeHexString } from '../utils/hex'
import { HexString, bytesToHex, hexToBytes, makeHexString } from '../utils/hex'
import { assertAddress } from '../utils/type'
import { makeFeedIdentifier } from './identifier'
import type { FeedType } from './type'
Expand All @@ -37,19 +38,14 @@ export type Index = number | Epoch | IndexBytes | string

export interface FeedUploadOptions extends UploadOptions, FeedUpdateOptions {}

export interface FeedUpdate {
timestamp: number
reference: BytesReference
}

export async function findNextIndex(
requestOptions: BeeRequestOptions,
owner: HexEthAddress,
topic: Topic,
options?: FeedUpdateOptions,
): Promise<HexString<typeof FEED_INDEX_HEX_LENGTH>> {
try {
const feedUpdate = await fetchFeedUpdate(requestOptions, owner, topic, options)
const feedUpdate = await fetchLatestFeedUpdate(requestOptions, owner, topic, options)

return makeHexString(feedUpdate.feedIndexNext, FEED_INDEX_HEX_LENGTH)
} catch (e: any) {
Expand All @@ -64,7 +60,7 @@ export async function updateFeed(
requestOptions: BeeRequestOptions,
signer: Signer,
topic: Topic,
payload: Uint8Array | Chunk,
payload: Uint8Array | ChunkParam,
postageBatchId: BatchId,
options?: FeedUploadOptions,
): Promise<Reference> {
Expand All @@ -82,6 +78,17 @@ export function getFeedUpdateChunkReference(owner: EthAddress, topic: Topic, ind
return keccak256Hash(identifier, owner)
}

export async function downloadFeedUpdate(
requestOptions: BeeRequestOptions,
owner: EthAddress,
topic: Topic,
index: Index,
): Promise<Data> {
const identifier = makeFeedIdentifier(topic, index)

return socAPI.download(requestOptions, bytesToHex(owner), bytesToHex(identifier))
}

export function makeFeedReader(
requestOptions: BeeRequestOptions,
type: FeedType,
Expand All @@ -93,7 +100,17 @@ export function makeFeedReader(
owner,
topic,
async download(options?: FeedUpdateOptions): Promise<FetchFeedUpdateResponse> {
return fetchFeedUpdate(requestOptions, owner, topic, { ...options, type })
if (!options?.index && options?.index !== 0) {
return fetchLatestFeedUpdate(requestOptions, owner, topic, { ...options, type })
}

const update = await downloadFeedUpdate(requestOptions, hexToBytes(owner), topic, options.index)

return {
data: update,
feedIndex: options.index,
feedIndexNext: '',
}
},
}
}
Expand All @@ -104,7 +121,11 @@ export function makeFeedWriter(
topic: Topic,
signer: Signer,
): FeedWriter {
const upload = async (postageBatchId: string | Address, payload: Uint8Array | Chunk, options?: FeedUploadOptions) => {
const upload = async (
postageBatchId: string | Address,
payload: Uint8Array | ChunkParam,
options?: FeedUploadOptions,
) => {
assertAddress(postageBatchId)

return updateFeed(requestOptions, signer, topic, payload, postageBatchId, { ...options, type })
Expand Down
2 changes: 1 addition & 1 deletion src/modules/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function readFeedUpdateHeaders(headers: Record<string, string>): FeedUpdateHeade
* @param topic Topic in hex
* @param options Additional options, like index, at, type
*/
export async function fetchFeedUpdate(
export async function fetchLatestFeedUpdate(
requestOptions: BeeRequestOptions,
owner: HexEthAddress,
topic: Topic,
Expand Down
34 changes: 32 additions & 2 deletions src/modules/soc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { BatchId, BeeRequestOptions, Reference, ReferenceResponse, UploadOptions } from '../types'
import { extractUploadHeaders } from '../utils/headers'
import {
BatchId,
BeeRequestOptions,
Data,
DownloadRedundancyOptions,
Reference,
ReferenceResponse,
UploadOptions,
} from '../types'
import { wrapBytesWithHelpers } from '../utils/bytes'
import { extractDownloadHeaders, extractUploadHeaders } from '../utils/headers'
import { http } from '../utils/http'

const socEndpoint = 'soc'
Expand Down Expand Up @@ -38,3 +47,24 @@ export async function upload(

return response.data.reference
}

/**
* Download data as a byte array
*
* @param ky
* @param hash Bee content reference
*/
export async function download(
requestOptions: BeeRequestOptions,
owner: string,
identifier: string,
options?: DownloadRedundancyOptions,
): Promise<Data> {
const response = await http<ArrayBuffer>(requestOptions, {
responseType: 'arraybuffer',
url: `${socEndpoint}/${owner}/${identifier}`,
headers: extractDownloadHeaders(options),
})

return wrapBytesWithHelpers(new Uint8Array(response.data))
}

0 comments on commit 8b9b6de

Please sign in to comment.