Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: no single-property object returned #341

Merged
merged 5 commits into from
Jun 8, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor!: no single-property object returned
AuHau committed Jun 3, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 9ad3bda43121366613b62bcb9bdaccf0724977ed
3 changes: 1 addition & 2 deletions src/bee.ts
Original file line number Diff line number Diff line change
@@ -26,7 +26,6 @@ import type {
SOCReader,
Topic,
BeeOptions,
ReferenceResponse,
JsonFeedOptions,
AnyJson,
Pin,
@@ -491,7 +490,7 @@ export class Bee {
topic: string,
data: T,
options?: JsonFeedOptions,
): Promise<ReferenceResponse> {
): Promise<Reference> {
assertBatchId(postageBatchId)

const hashedTopic = this.makeFeedTopic(topic)
6 changes: 3 additions & 3 deletions src/chunk/soc.ts
Original file line number Diff line number Diff line change
@@ -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'
@@ -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())
@@ -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)
5 changes: 2 additions & 3 deletions src/feed/index.ts
Original file line number Diff line number Diff line change
@@ -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,
@@ -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)
@@ -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)

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'

@@ -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)
7 changes: 4 additions & 3 deletions src/modules/chunk.ts
Original file line number Diff line number Diff line change
@@ -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'

@@ -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',
@@ -36,7 +37,7 @@ export async function upload(
responseType: 'json',
})

return response.data
return response.data.reference
}

/**
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'

@@ -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',
@@ -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
@@ -204,7 +204,7 @@ export interface FeedWriter extends FeedReader {
postageBatchId: string | BatchId,
reference: ChunkReference | Reference,
options?: FeedUploadOptions,
): Promise<ReferenceResponse>
): Promise<Reference>
}

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

/**
4 changes: 2 additions & 2 deletions test/integration/chunk/bmt.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
}
})

@@ -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
@@ -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 () => {
4 changes: 2 additions & 2 deletions test/integration/feed/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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 HexString) as ChunkReference
}

// FIXME helper function for setting up test state for testing finding feed updates
4 changes: 2 additions & 2 deletions test/integration/modules/chunk.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})

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)
@@ -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)
16 changes: 8 additions & 8 deletions test/integration/modules/pinning.spec.ts
Original file line number Diff line number Diff line change
@@ -121,16 +121,16 @@ 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)

const pinningResponse = await pinning.pin(BEE_URL, testChunkHash)
expect(pinningResponse).toBeOneOf([createdResponse, okResponse])
})

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)

const pinningResponse = await pinning.unpin(BEE_URL, testChunkHash)
expect(pinningResponse).toEqual(okResponse)
@@ -149,8 +149,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)

const pinningResponse = await pinning.pin(BEE_URL, testChunkHash)
expect(pinningResponse).toBeOneOf([createdResponse, okResponse])
@@ -164,8 +164,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)

const pinningResponse = await pinning.pin(BEE_URL, testChunkHash)
expect(pinningResponse).toBeOneOf([createdResponse, okResponse])
4 changes: 2 additions & 2 deletions test/unit/feed/json.spec.ts
Original file line number Diff line number Diff line change
@@ -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
@@ -23,7 +23,7 @@ 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)
bee.received(1).uploadData(testAddress, expectedBytes)