From e023d4053233f5ef70364db2baac2a5c478808f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Fri, 21 May 2021 10:11:18 +0200 Subject: [PATCH 1/2] feat: reupload --- src/bee.ts | 16 ++++++++++++++++ src/modules/bzz.ts | 16 ++++++++++++++++ test/integration/bee-class.spec.ts | 17 +++++++++++++++++ test/integration/modules/bzz.spec.ts | 20 ++++++++++++++++++++ 4 files changed, 69 insertions(+) diff --git a/src/bee.ts b/src/bee.ts index e99f7c89..e951fa86 100644 --- a/src/bee.ts +++ b/src/bee.ts @@ -272,6 +272,22 @@ export class Bee { return pinning.getPin(this.url, reference) } + async reuploadPinnedData(reference: Reference | string): Promise { + assertReference(reference) + + try { + // TODO: This should be detected by Bee, but until https://github.com/ethersphere/bee/issues/1803 is resolved + // it is good idea to do some input validation on our side. + await this.getPin(reference) + } catch (e) { + if (e.status === 404) { + throw new BeeArgumentError('The passed reference is not locally pinned!', reference) + } + } + + await bzz.reupload(this.url, reference) + } + /** * Send to recipient or target with Postal Service for Swarm * diff --git a/src/modules/bzz.ts b/src/modules/bzz.ts index 265dd1c2..0c3e8948 100644 --- a/src/modules/bzz.ts +++ b/src/modules/bzz.ts @@ -182,3 +182,19 @@ export async function uploadCollection( return response.data.reference } + +/** + * Reupload locally pinned data + * @param url + * @param reference + * @param options + * @throws BeeResponseError if not locally pinned or invalid data + */ +export async function reupload(url: string, reference: Reference, options?: AxiosRequestConfig): Promise { + await safeAxios({ + ...options, + method: 'patch', + url: `${url}${bzzEndpoint}/${reference}/`, + responseType: 'json', + }) +} diff --git a/test/integration/bee-class.spec.ts b/test/integration/bee-class.spec.ts index 2b1ec797..0510650d 100644 --- a/test/integration/bee-class.spec.ts +++ b/test/integration/bee-class.spec.ts @@ -191,6 +191,23 @@ describe('Bee class', () => { }) }) + describe('reupload', () => { + it('should reupload pinned data', async () => { + const content = randomByteArray(16, Date.now()) + + const hash = await bee.uploadData(await getPostageBatch(), content) + await bee.pin(hash) + await bee.reuploadPinnedData(hash) // Does not return anything, but will throw exception if something is going wrong + }) + + it('should throw error if data is not pinned', async () => { + const content = randomByteArray(16, Date.now()) + + const hash = await bee.uploadData(await getPostageBatch(), content) + await expect(bee.reuploadPinnedData(hash)).rejects.toThrowError(BeeArgumentError) + }) + }) + describe('pss', () => { it( 'should send and receive data', diff --git a/test/integration/modules/bzz.spec.ts b/test/integration/modules/bzz.spec.ts index 423e1fc3..e7ec7060 100644 --- a/test/integration/modules/bzz.spec.ts +++ b/test/integration/modules/bzz.spec.ts @@ -210,6 +210,18 @@ describe('modules/bzz', () => { expect(file1.name).toEqual(fileName) expect(file1.data).toEqual(data) }) + + it('should reupload directory', async () => { + const directoryStructure: Collection = [ + { + path: '0', + data: Uint8Array.from([0]), + }, + ] + + const hash = await bzz.uploadCollection(BEE_URL, directoryStructure, await getPostageBatch(), { pin: true }) + await bzz.reupload(BEE_URL, hash) // Does not return anything, but will throw error if something is wrong + }) }) describe('file', () => { @@ -279,5 +291,13 @@ describe('modules/bzz', () => { }, BIG_FILE_TIMEOUT, ) + + it('should reupload file', async () => { + const data = 'hello world' + const filename = 'hello.txt' + + const hash = await bzz.uploadFile(BEE_URL, data, await getPostageBatch(), filename, { pin: true }) + await bzz.reupload(BEE_URL, hash) // Does not return anything, but will throw error if something is wrong + }) }) }) From 652e161f7f710587ba5a0adae521929c7289106b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Wed, 26 May 2021 08:47:33 +0200 Subject: [PATCH 2/2] feat: expose axiosOptions --- src/bee.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/bee.ts b/src/bee.ts index e951fa86..26b211fc 100644 --- a/src/bee.ts +++ b/src/bee.ts @@ -272,7 +272,14 @@ export class Bee { return pinning.getPin(this.url, reference) } - async reuploadPinnedData(reference: Reference | string): Promise { + /** + * Instructs the Bee node to reupload a locally pinned data into the network. + * + * @param reference + * @param axiosOptions + * @throws BeeArgumentError if the reference is not locally pinned + */ + async reuploadPinnedData(reference: Reference | string, axiosOptions?: AxiosRequestConfig): Promise { assertReference(reference) try { @@ -285,7 +292,7 @@ export class Bee { } } - await bzz.reupload(this.url, reference) + await bzz.reupload(this.url, reference, axiosOptions) } /**