Skip to content

Commit

Permalink
feat: is reference retrievable support (#425)
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 authored Oct 13, 2021
1 parent 9dd6edd commit 76601f8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/bee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@ export class Bee {
* @param options Options that affects the request behavior
* @throws BeeArgumentError if the reference is not locally pinned
* @throws TypeError if reference is in not correct format
*
* @see [Bee API reference - `PUT /stewardship`](https://docs.ethswarm.org/api/#tag/Stewardship/paths/~1stewardship~1{reference}/put)
*/
async reuploadPinnedData(reference: Reference | string, options?: RequestOptions): Promise<void> {
assertRequestOptions(options)
Expand All @@ -560,6 +562,21 @@ export class Bee {
await stewardship.reupload(this.getKy(options), reference)
}

/**
* Checks if content specified by reference is retrievable from the network.
*
* @param reference The checked content
* @param options Options that affects the request behavior
*
* @see [Bee API reference - `GET /stewardship`](https://docs.ethswarm.org/api/#tag/Stewardship/paths/~1stewardship~1{reference}/get)
*/
async isReferenceRetrievable(reference: Reference | string, options?: RequestOptions): Promise<boolean> {
assertRequestOptions(options)
assertReference(reference)

return stewardship.isRetrievable(this.getKy(options), reference)
}

/**
* Send data to recipient or target with Postal Service for Swarm.
*
Expand Down
14 changes: 14 additions & 0 deletions src/modules/stewardship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,17 @@ export async function reupload(ky: Ky, reference: Reference): Promise<void> {
path: `${stewardshipEndpoint}/${reference}`,
})
}

interface IsRetrievableResponse {
isRetrievable: boolean
}

export async function isRetrievable(ky: Ky, reference: Reference): Promise<boolean> {
const response = await http<IsRetrievableResponse>(ky, {
method: 'get',
responseType: 'json',
path: `${stewardshipEndpoint}/${reference}`,
})

return response.data.isRetrievable
}
21 changes: 19 additions & 2 deletions test/integration/bee-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
commonMatchers,
createRandomNodeReadable,
createReadableStream,
ERR_TIMEOUT,
FEED_TIMEOUT,
getPostageBatch,
makeTestTarget,
Expand Down Expand Up @@ -282,15 +283,31 @@ describe('Bee class', () => {
})
})

describe('reupload', () => {
describe('stewardship', () => {
it('should reupload pinned data', async () => {
const content = randomByteArray(16, Date.now())

const result = await bee.uploadData(getPostageBatch(), content, { pin: true })

await sleep(10)
await bee.reuploadPinnedData(result.reference) // Does not return anything, but will throw exception if something is going wrong
})

it(
'should check if reference is retrievable',
async () => {
const content = randomByteArray(16, Date.now())
const result = await bee.uploadData(getPostageBatch(), content, { pin: true })

await sleep(10)
await expect(bee.isReferenceRetrievable(result.reference)).resolves.toEqual(true)

// Reference that has correct form, but should not exist on the network
await expect(
bee.isReferenceRetrievable('ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4332'),
).resolves.toEqual(false)
},
ERR_TIMEOUT,
)
})

describe('pss', () => {
Expand Down

0 comments on commit 76601f8

Please sign in to comment.