Skip to content

Commit

Permalink
refactor!: no single-property object returned (#341)
Browse files Browse the repository at this point in the history
Co-authored-by: nugaon <50576770+nugaon@users.noreply.github.com>
  • Loading branch information
AuHau and nugaon committed Jun 8, 2021
1 parent d2a65ee commit 572253c
Show file tree
Hide file tree
Showing 16 changed files with 52 additions and 44 deletions.
3 changes: 1 addition & 2 deletions src/bee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import type {
SOCReader,
Topic,
BeeOptions,
ReferenceResponse,
JsonFeedOptions,
AnyJson,
Pin,
Expand Down Expand Up @@ -492,7 +491,7 @@ export class Bee {
topic: string,
data: T,
options?: JsonFeedOptions,
): Promise<ReferenceResponse> {
): Promise<Reference> {
assertBatchId(postageBatchId)

const hashedTopic = this.makeFeedTopic(topic)
Expand Down
6 changes: 3 additions & 3 deletions src/chunk/soc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
MIN_PAYLOAD_SIZE,
assertValidChunkData,
} from './cac'
import { ReferenceResponse, UploadOptions, Signature, Signer, BatchId } from '../types'
import { UploadOptions, Signature, Signer, BatchId, Reference } from '../types'
import { bytesToHex } from '../utils/hex'
import * as socAPI from '../modules/soc'
import * as chunkAPI from '../modules/chunk'
Expand Down Expand Up @@ -137,7 +137,7 @@ export async function uploadSingleOwnerChunk(
chunk: SingleOwnerChunk,
postageBatchId: BatchId,
options?: UploadOptions,
): Promise<ReferenceResponse> {
): Promise<Reference> {
const owner = bytesToHex(chunk.owner())
const identifier = bytesToHex(chunk.identifier())
const signature = bytesToHex(chunk.signature())
Expand All @@ -163,7 +163,7 @@ export async function uploadSingleOwnerChunkData(
identifier: Identifier,
data: Uint8Array,
options?: UploadOptions,
): Promise<ReferenceResponse> {
): Promise<Reference> {
assertAddress(postageBatchId)
const cac = makeContentAddressedChunk(data)
const soc = await makeSingleOwnerChunk(cac, identifier, signer)
Expand Down
5 changes: 2 additions & 3 deletions src/feed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { FeedUpdateOptions, fetchFeedUpdate } from '../modules/feed'
import {
REFERENCE_HEX_LENGTH,
Reference,
ReferenceResponse,
UploadOptions,
ENCRYPTED_REFERENCE_HEX_LENGTH,
ENCRYPTED_REFERENCE_BYTES_LENGTH,
Expand Down Expand Up @@ -94,7 +93,7 @@ export async function uploadFeedUpdate(
reference: ChunkReference,
postageBatchId: BatchId,
options?: FeedUploadOptions,
): Promise<ReferenceResponse> {
): Promise<Reference> {
const identifier = makeFeedIdentifier(topic, index)
const at = options?.at ?? Date.now() / 1000.0
const timestamp = writeUint64BigEndian(at)
Expand Down Expand Up @@ -128,7 +127,7 @@ export async function updateFeed(
reference: ChunkReference,
postageBatchId: BatchId,
options?: FeedUploadOptions,
): Promise<ReferenceResponse> {
): Promise<Reference> {
const ownerHex = makeHexEthAddress(signer.address)
const nextIndex = await findNextIndex(url, ownerHex, topic, options)

Expand Down
4 changes: 2 additions & 2 deletions src/feed/json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FeedWriter, ReferenceResponse, FeedReader, AnyJson, Address } from '../types'
import { FeedWriter, FeedReader, AnyJson, Address, Reference } from '../types'
import { Bee } from '../bee'
import { assertAddress } from '../utils/type'

Expand All @@ -25,7 +25,7 @@ export async function setJsonData(
writer: FeedWriter,
postageBatchId: string | Address,
data: AnyJson,
): Promise<ReferenceResponse> {
): Promise<Reference> {
assertAddress(postageBatchId)

const serializedData = serializeJson(data)
Expand Down
7 changes: 4 additions & 3 deletions src/modules/chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Readable } from 'stream'
import type { BatchId, ReferenceResponse, UploadOptions } from '../types'
import { extractUploadHeaders } from '../utils/headers'
import { safeAxios } from '../utils/safe-axios'
import { Reference } from '../types'

const endpoint = '/chunks'

Expand All @@ -16,14 +17,14 @@ const endpoint = '/chunks'
* @param url Bee URL
* @param data Chunk data to be uploaded
* @param postageBatchId Postage BatchId that will be assigned to uploaded data
* @param options Aditional options like tag, encryption, pinning
* @param options Additional options like tag, encryption, pinning
*/
export async function upload(
url: string,
data: Uint8Array,
postageBatchId: BatchId,
options?: UploadOptions,
): Promise<ReferenceResponse> {
): Promise<Reference> {
const response = await safeAxios<ReferenceResponse>({
...options?.axiosOptions,
method: 'post',
Expand All @@ -36,7 +37,7 @@ export async function upload(
responseType: 'json',
})

return response.data
return response.data.reference
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/modules/soc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BatchId, ReferenceResponse, UploadOptions } from '../types'
import { BatchId, Reference, ReferenceResponse, UploadOptions } from '../types'
import { extractUploadHeaders } from '../utils/headers'
import { safeAxios } from '../utils/safe-axios'

Expand All @@ -23,7 +23,7 @@ export async function upload(
data: Uint8Array,
postageBatchId: BatchId,
options?: UploadOptions,
): Promise<ReferenceResponse> {
): Promise<Reference> {
const response = await safeAxios<ReferenceResponse>({
...options?.axiosOptions,
method: 'post',
Expand All @@ -37,5 +37,5 @@ export async function upload(
params: { sig: signature },
})

return response.data
return response.data.reference
}
4 changes: 2 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export interface FeedWriter extends FeedReader {
postageBatchId: string | BatchId,
reference: ChunkReference | Reference,
options?: FeedUploadOptions,
): Promise<ReferenceResponse>
): Promise<Reference>
}

/**
Expand Down Expand Up @@ -235,7 +235,7 @@ export interface SOCWriter extends SOCReader {
identifier: Identifier,
data: Uint8Array,
options?: UploadOptions,
) => Promise<ReferenceResponse>
) => Promise<Reference>
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/integration/bee-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ describe('Bee class', () => {
const socWriter = bee.makeSOCWriter(testIdentity.privateKey)

const reference = await socWriter.upload(getPostageBatch(), identifier, testChunkPayload)
expect(reference).toEqual({ reference: socHash })
expect(reference).toEqual(socHash)

const soc = await socWriter.download(identifier)
const payload = soc.payload()
Expand Down Expand Up @@ -376,7 +376,7 @@ describe('Bee class', () => {
const socWriter = bee.makeSOCWriter()

const reference = await socWriter.upload(getPostageBatch(), identifier, testChunkPayload)
expect(reference).toEqual({ reference: '00019ec85e8859aa641cf149fbd1147ac7965a9cad1dfe4ab7beaa12d5dc8027' })
expect(reference).toEqual('00019ec85e8859aa641cf149fbd1147ac7965a9cad1dfe4ab7beaa12d5dc8027')
})

it('should prioritize signer passed to method', async () => {
Expand All @@ -393,7 +393,7 @@ describe('Bee class', () => {
const socWriter = bee.makeSOCWriter(testIdentity.privateKey)

const reference = await socWriter.upload(getPostageBatch(), identifier, testChunkPayload)
expect(reference).toEqual({ reference: 'd1a21cce4c86411f6af2f621ce9a3a0aa3cc5cea6cc9e1b28523d28411398cfb' })
expect(reference).toEqual('d1a21cce4c86411f6af2f621ce9a3a0aa3cc5cea6cc9e1b28523d28411398cfb')
})

it('should throw if no signers are passed', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/integration/chunk/bmt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('bmt', () => {

const reference = bytesToHex(bmtHash(data))
const response = await chunk.upload(beeUrl(), data, getPostageBatch())
expect(response).toEqual({ reference })
expect(response).toEqual(reference)
}
})

Expand All @@ -28,6 +28,6 @@ describe('bmt', () => {

const reference = bytesToHex(bmtHash(data))
const response = await chunk.upload(beeUrl(), data, getPostageBatch())
expect(response).toEqual({ reference })
expect(response).toEqual(reference)
})
})
2 changes: 1 addition & 1 deletion test/integration/chunk/cac.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('cac', () => {
const reference = bytesToHex(address)
const response = await chunkAPI.upload(beeUrl(), cac.data, getPostageBatch())

expect(response).toEqual({ reference })
expect(response).toEqual(reference)
})

test('download content address chunk', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/chunk/soc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('soc', () => {

const response = await uploadSingleOwnerChunk(beeUrl(), soc, getPostageBatch())

expect(response).toEqual({ reference: socAddress })
expect(response).toEqual(socAddress)
})

test('download single owner chunk', async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/integration/feed/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fetchFeedUpdate } from '../../../src/modules/feed'
import { HexString, hexToBytes, makeHexString } from '../../../src/utils/hex'
import { hexToBytes, makeHexString } from '../../../src/utils/hex'
import { beeUrl, ERR_TIMEOUT, getPostageBatch, testIdentity } from '../../utils'
import { ChunkReference, downloadFeedUpdate, findNextIndex, Index, uploadFeedUpdate } from '../../../src/feed'
import { Bytes, assertBytes } from '../../../src/utils/bytes'
Expand All @@ -15,9 +15,9 @@ function makeChunk(index: number) {

async function uploadChunk(url: string, index: number): Promise<ChunkReference> {
const chunk = makeChunk(index)
const referenceResponse = await chunkAPI.upload(url, chunk.data, getPostageBatch())
const reference = await chunkAPI.upload(url, chunk.data, getPostageBatch())

return hexToBytes(referenceResponse.reference as HexString) as ChunkReference
return hexToBytes(reference) as ChunkReference
}

// FIXME helper function for setting up test state for testing finding feed updates
Expand Down
4 changes: 2 additions & 2 deletions test/integration/modules/chunk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ describe('modules/chunk', () => {
const reference = 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338'

const response = await chunk.upload(BEE_URL, data, getPostageBatch())
expect(response).toEqual({ reference })
expect(response).toEqual(reference)

const downloadedData = await chunk.download(BEE_URL, response.reference)
const downloadedData = await chunk.download(BEE_URL, response)
expect(downloadedData).toEqual(data)
})

Expand Down
15 changes: 12 additions & 3 deletions test/integration/modules/feed.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { createFeedManifest, fetchFeedUpdate } from '../../../src/modules/feed'
import { HexString, hexToBytes, makeHexString } from '../../../src/utils/hex'
import { beeUrl, ERR_TIMEOUT, getPostageBatch, testIdentity, tryDeleteChunkFromLocalStorage } from '../../utils'
import {
beeUrl,
commonMatchers,
ERR_TIMEOUT,
getPostageBatch,
testIdentity,
tryDeleteChunkFromLocalStorage,
} from '../../utils'
import { upload as uploadSOC } from '../../../src/modules/soc'
import type { Topic } from '../../../src/types'

commonMatchers()

describe('modules/feed', () => {
const url = beeUrl()
const owner = makeHexString(testIdentity.address, 40)
Expand Down Expand Up @@ -42,10 +51,10 @@ describe('modules/feed', () => {
await tryDeleteChunkFromLocalStorage(cacAddress)

const socResponse = await uploadSOC(url, owner, identifier, signature, socData, getPostageBatch())
expect(typeof socResponse.reference).toBe('string')
expect(socResponse).toBeType('string')

const feedUpdate = await fetchFeedUpdate(url, owner, oneUpdateTopic)
expect(typeof feedUpdate.reference).toBe('string')
expect(feedUpdate.reference).toBeType('string')
expect(feedUpdate.feedIndex).toEqual('0000000000000000')
expect(feedUpdate.feedIndexNext).toEqual('0000000000000001')
}, 21000)
Expand Down
16 changes: 8 additions & 8 deletions test/integration/modules/pinning.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ describe('modules/pin', () => {

describe('should work with chunks', () => {
it('should pin existing chunk', async () => {
const chunkResponse = await chunk.upload(BEE_URL, testChunkData, getPostageBatch())
expect(chunkResponse).toEqual({ reference: testChunkHash })
const chunkReference = await chunk.upload(BEE_URL, testChunkData, getPostageBatch())
expect(chunkReference).toEqual(testChunkHash)

await pinning.pin(BEE_URL, testChunkHash) // Nothing is asserted as nothing is returned, will throw error if something is wrong
})

it('should unpin existing chunk', async () => {
const chunkResponse = await chunk.upload(BEE_URL, testChunkData, getPostageBatch())
expect(chunkResponse).toEqual({ reference: testChunkHash })
const chunkReference = await chunk.upload(BEE_URL, testChunkData, getPostageBatch())
expect(chunkReference).toEqual(testChunkHash)

await pinning.unpin(BEE_URL, testChunkHash) // Nothing is asserted as nothing is returned, will throw error if something is wrong
})
Expand All @@ -133,8 +133,8 @@ describe('modules/pin', () => {
})

it('should return pinning status of existing chunk', async () => {
const chunkResponse = await chunk.upload(BEE_URL, testChunkData, getPostageBatch())
expect(chunkResponse).toEqual({ reference: testChunkHash })
const chunkReference = await chunk.upload(BEE_URL, testChunkData, getPostageBatch())
expect(chunkReference).toEqual(testChunkHash)

await pinning.pin(BEE_URL, testChunkHash) // Nothing is asserted as nothing is returned, will throw error if something is wrong
const pinningStatus = await pinning.getPin(BEE_URL, testChunkHash)
Expand All @@ -146,8 +146,8 @@ describe('modules/pin', () => {
})

it('should return list of pinned chunks', async () => {
const chunkResponse = await chunk.upload(BEE_URL, testChunkData, getPostageBatch())
expect(chunkResponse).toEqual({ reference: testChunkHash })
const chunkReference = await chunk.upload(BEE_URL, testChunkData, getPostageBatch())
expect(chunkReference).toEqual(testChunkHash)

await pinning.pin(BEE_URL, testChunkHash) // Nothing is asserted as nothing is returned, will throw error if something is wrong
})
Expand Down
6 changes: 3 additions & 3 deletions test/unit/feed/json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface CircularReference {

describe('JsonFeed', () => {
const DATA_REFERENCE = testChunkHash as Reference
const FEED_REFERENCE_HASH = 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a1111'
const FEED_REFERENCE_HASH = 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a1111' as Reference
const FEED_REFERENCE = {
reference: FEED_REFERENCE_HASH,
} as FetchFeedUpdateResponse
Expand All @@ -23,9 +23,9 @@ describe('JsonFeed', () => {
bee.uploadData(Arg.all()).resolves(DATA_REFERENCE)

const writer = Substitute.for<FeedWriter>()
writer.upload(Arg.all()).resolves(FEED_REFERENCE)
writer.upload(Arg.all()).resolves(FEED_REFERENCE_HASH)

await expect(setJsonData(bee, writer, testAddress, data as AnyJson)).resolves.toEqual(FEED_REFERENCE)
await expect(setJsonData(bee, writer, testAddress, data as AnyJson)).resolves.toEqual(FEED_REFERENCE_HASH)
bee.received(1).uploadData(testAddress, expectedBytes)
writer.received(1).upload(testAddress, DATA_REFERENCE)
})
Expand Down

0 comments on commit 572253c

Please sign in to comment.