Skip to content

Commit

Permalink
feat: Add the avoid checks functionality to the download content feat…
Browse files Browse the repository at this point in the history
…ure (#450)
  • Loading branch information
LautaroPetaccio authored Mar 25, 2024
1 parent cc4f4e9 commit 0e77c2c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/client/ContentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type ContentClient = {
fetchEntitiesByPointers(pointers: string[], options?: RequestOptions): Promise<Entity[]>
fetchEntitiesByIds(ids: string[], options?: RequestOptions): Promise<Entity[]>
fetchEntityById(id: string, options?: RequestOptions): Promise<Entity>
downloadContent(contentHash: string, options?: RequestOptions): Promise<Buffer>
downloadContent(contentHash: string, options?: RequestOptions & { avoidChecks?: boolean }): Promise<Buffer>

isContentAvailable(cids: string[], options?: RequestOptions): Promise<AvailableContentResult>

Expand All @@ -39,7 +39,7 @@ export async function downloadContent(
fetcher: IFetchComponent,
baseUrl: string,
contentHash: string,
options?: Partial<RequestOptions>
options?: Partial<RequestOptions> & { avoidChecks?: boolean }
): Promise<Buffer> {
const { attempts = 3, retryDelay = 500 } = options ? options : {}
const timeout = options?.timeout ? { timeout: options.timeout } : {}
Expand All @@ -48,14 +48,16 @@ export async function downloadContent(
`fetch file with hash ${contentHash} from ${baseUrl}`,
async () => {
const content = await (await fetcher.fetch(`${baseUrl}/${contentHash}`, timeout)).buffer()
const downloadedHash = contentHash.startsWith('Qm') ? await hashV0(content) : await hashV1(content)
if (!options?.avoidChecks) {
const downloadedHash = contentHash.startsWith('Qm') ? await hashV0(content) : await hashV1(content)

// Sometimes, the downloaded file is not complete, so the hash turns out to be different.
// So we will check the hash before considering the download successful.
if (downloadedHash === contentHash) {
return content
// Sometimes, the downloaded file is not complete, so the hash turns out to be different.
// So we will check the hash before considering the download successful.
if (downloadedHash !== contentHash) {
throw new Error(`Failed to fetch file with hash ${contentHash} from ${baseUrl}`)
}
}
throw new Error(`Failed to fetch file with hash ${contentHash} from ${baseUrl}`)
return content
},
attempts,
retryDelay
Expand Down
18 changes: 17 additions & 1 deletion test/ContentClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Entity, EntityType } from '@dcl/schemas'
import { createFetchComponent } from '@well-known-components/fetch-component'
import { IFetchComponent } from '@well-known-components/http-server'
import { AvailableContentResult, ContentClient, createContentClient } from '../src'
import { getCurrentVersion } from '../src/client/utils/Helper'

describe('ContentClient', () => {
const URL = 'https://url.com'
Expand Down Expand Up @@ -161,6 +160,23 @@ describe('ContentClient', () => {
expect(fetcher.fetch).toHaveBeenCalledTimes(2)
})

it("when a file is download, then the client doesn't check if the file is correct if the avoid checks flags is set", async () => {
const failBuffer = Buffer.from('Fail')
const realBuffer = Buffer.from('Real')

const fileHash = await hashV0(realBuffer)

const fetcher = createFetchComponent()
fetcher.fetch = jest.fn().mockResolvedValue({
buffer: jest.fn().mockResolvedValueOnce(failBuffer).mockResolvedValueOnce(realBuffer)
})

const client = buildClient(URL, fetcher)
const result = await client.downloadContent(fileHash, { retryDelay: 20, avoidChecks: true })
expect(result).toEqual(failBuffer)
expect(fetcher.fetch).toHaveBeenCalledTimes(1)
})

it('When a file is downloaded and all attempts failed, then an exception is thrown', async () => {
const failBuffer = Buffer.from('Fail')
const fileHash = 'Hash'
Expand Down

0 comments on commit 0e77c2c

Please sign in to comment.