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: Add the avoid checks functionality to the download content feature #450

Merged
merged 1 commit into from
Mar 25, 2024
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
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
Loading