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

feat: reupload #323

Merged
merged 2 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 23 additions & 0 deletions src/bee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,29 @@ export class Bee {
return pinning.getPin(this.url, reference)
}

/**
* 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<void> {
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, axiosOptions)
}

/**
* Send to recipient or target with Postal Service for Swarm
*
Expand Down
16 changes: 16 additions & 0 deletions src/modules/bzz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
AuHau marked this conversation as resolved.
Show resolved Hide resolved
await safeAxios({
...options,
method: 'patch',
url: `${url}${bzzEndpoint}/${reference}/`,
responseType: 'json',
})
}
17 changes: 17 additions & 0 deletions test/integration/bee-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
20 changes: 20 additions & 0 deletions test/integration/modules/bzz.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array> = [
{
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', () => {
Expand Down Expand Up @@ -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
})
})
})