Skip to content

Commit

Permalink
fix!: convert cid getter to function (#740)
Browse files Browse the repository at this point in the history
  • Loading branch information
AuHau authored Jul 27, 2022
1 parent d2cd180 commit 0b46862
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
8 changes: 7 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,13 @@ export interface BeeOptions extends RequestOptions {
}

export interface UploadResultWithCid extends UploadResult {
cid: string
/**
* Function that converts the reference into Swarm CIDs
*
* @throws TypeError if the reference is encrypted reference (eq. 128 chars long) which is not supported in CID
* @see https://github.com/ethersphere/swarm-cid-js
*/
cid: () => string
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export function makeReferenceOrEns(value: unknown, expectedCidType: ReferenceTyp
export function addCidConversionFunction(result: UploadResult, cidType: ReferenceType): UploadResultWithCid {
return {
...result,
get cid() {
cid() {
return encodeReference(result.reference, cidType).toString()
},
}
Expand Down
4 changes: 2 additions & 2 deletions test/integration/bee-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('Bee class', () => {
const contentType = 'text/html'

const result = await bee.uploadFile(getPostageBatch(), content, name, { contentType })
const file = await bee.downloadFile(result.cid)
const file = await bee.downloadFile(result.cid())

expect(file.name).toEqual(name)
expect(file.data).toEqual(content)
Expand Down Expand Up @@ -254,7 +254,7 @@ describe('Bee class', () => {
]

const result = await bee.uploadCollection(getPostageBatch(), directoryStructure)
const file = await bee.downloadFile(result.cid, directoryStructure[0].path)
const file = await bee.downloadFile(result.cid(), directoryStructure[0].path)

expect(file.name).toEqual(directoryStructure[0].path)
expect(file.data.text()).toEqual('hello-CID-world')
Expand Down
45 changes: 35 additions & 10 deletions test/unit/bee-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import {
randomByteArray,
testBatchId,
testChunkEncryptedReference,
testChunkHash,
testIdentity,
testJsonCid,
Expand Down Expand Up @@ -85,11 +86,32 @@ describe('Bee class', () => {
const bee = new Bee(MOCK_SERVER_URL, { defaultHeaders })
const reference = await bee.uploadFile(testBatchId, 'hello world', 'nice.txt')

expect(reference).toEqual({
cid: testJsonCid,
reference: testJsonHash,
tagUid: 123,
})
expect(reference).toEqual(
expect.objectContaining({
reference: testJsonHash,
tagUid: 123,
}),
)

expect(reference.cid()).toEqual(testJsonCid)
})

it('cid should throw for encrypted references', async () => {
uploadFileMock(testBatchId, 'nice.txt').reply(200, {
reference: testChunkEncryptedReference,
} as ReferenceResponse)

const bee = new Bee(MOCK_SERVER_URL)
const reference = await bee.uploadFile(testBatchId, 'hello world', 'nice.txt')

expect(reference).toEqual(
expect.objectContaining({
reference: testChunkEncryptedReference,
tagUid: 123,
}),
)

expect(() => reference.cid()).toThrow(TypeError)
})

describe('uploadData', () => {
Expand Down Expand Up @@ -896,11 +918,14 @@ describe('Bee class', () => {

const reference = await bee.uploadFile(testBatchId, 'hello world', 'nice.txt', { encrypt: true })

expect(reference).toEqual({
cid: testJsonCid,
reference: testJsonHash,
tagUid: 123,
})
expect(reference).toEqual(
expect.objectContaining({
reference: testJsonHash,
tagUid: 123,
}),
)

expect(reference.cid()).toEqual(testJsonCid)

expect(requestSpy.mock.calls.length).toEqual(1)
expect(requestSpy.mock.calls[0].length).toEqual(1)
Expand Down
2 changes: 2 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ export const testChunkSpan = new Uint8Array([testChunkPayload.length, 0, 0, 0, 0
export const testChunkData = new Uint8Array([...testChunkSpan, ...testChunkPayload])
// the hash is hardcoded because we would need the bmt hasher otherwise
export const testChunkHash = 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338' as Reference
export const testChunkEncryptedReference =
'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338' as Reference
export const testAddress = 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338' as Address
export const testBatchId = 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338' as BatchId

Expand Down

0 comments on commit 0b46862

Please sign in to comment.