Skip to content

Commit

Permalink
fix: remove stubs from verified-fetch tests (#431)
Browse files Browse the repository at this point in the history
The extensive stubbing in the `@helia/verified-fetch` tests have some
baked-in assumptions about how the codecs work which are not easy
to unpick.

It's quick to test using the actual codecs if the block data is already
present so remove the stubs and use a network-less Helia node to make
the tests more reliable.
  • Loading branch information
achingbrain authored Feb 13, 2024
1 parent b010184 commit cbab8b0
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 280 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,16 @@
"progress-events": "^1.0.0"
},
"devDependencies": {
"@helia/utils": "^0.0.1",
"@libp2p/logger": "^4.0.5",
"@libp2p/peer-id-factory": "^4.0.5",
"@sgtpooki/file-type": "^1.0.1",
"@types/sinon": "^17.0.3",
"aegir": "^42.2.2",
"blockstore-core": "^4.4.0",
"datastore-core": "^9.2.8",
"helia": "^4.0.1",
"it-last": "^3.0.4",
"magic-bytes.js": "^1.8.0",
"sinon": "^17.0.1",
"sinon-ts": "^2.0.0",
Expand Down
48 changes: 32 additions & 16 deletions src/verified-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { code as dagJsonCode } from '@ipld/dag-json'
import { code as dagPbCode } from '@ipld/dag-pb'
import { code as jsonCode } from 'multiformats/codecs/json'
import { decode, code as rawCode } from 'multiformats/codecs/raw'
import { identity } from 'multiformats/hashes/identity'
import { CustomProgressEvent } from 'progress-events'
import { getStreamFromAsyncIterable } from './utils/get-stream-from-async-iterable.js'
import { parseResource } from './utils/parse-resource.js'
Expand Down Expand Up @@ -62,6 +63,20 @@ function convertOptions (options?: VerifiedFetchOptions): (Omit<VerifiedFetchOpt
}
}

function okResponse (body?: BodyInit | null): Response {
return new Response(body, {
status: 200,
statusText: 'OK'
})
}

function notSupportedResponse (body?: BodyInit | null): Response {
return new Response(body, {
status: 501,
statusText: 'Not Implemented'
})
}

export class VerifiedFetch {
private readonly helia: Helia
private readonly ipns: IPNS
Expand Down Expand Up @@ -93,14 +108,14 @@ export class VerifiedFetch {

// handle vnd.ipfs.ipns-record
private async handleIPNSRecord ({ cid, path, options }: FetchHandlerFunctionArg): Promise<Response> {
const response = new Response('vnd.ipfs.ipns-record support is not implemented', { status: 501 })
const response = notSupportedResponse('vnd.ipfs.ipns-record support is not implemented')
response.headers.set('X-Content-Type-Options', 'nosniff') // see https://specs.ipfs.tech/http-gateways/path-gateway/#x-content-type-options-response-header
return response
}

// handle vnd.ipld.car
private async handleIPLDCar ({ cid, path, options }: FetchHandlerFunctionArg): Promise<Response> {
const response = new Response('vnd.ipld.car support is not implemented', { status: 501 })
const response = notSupportedResponse('vnd.ipld.car support is not implemented')
response.headers.set('X-Content-Type-Options', 'nosniff') // see https://specs.ipfs.tech/http-gateways/path-gateway/#x-content-type-options-response-header
return response
}
Expand All @@ -113,7 +128,7 @@ export class VerifiedFetch {
onProgress: options?.onProgress
})
options?.onProgress?.(new CustomProgressEvent<CIDDetail>('verified-fetch:request:end', { cid, path }))
const response = new Response(JSON.stringify(result), { status: 200 })
const response = okResponse(JSON.stringify(result))
response.headers.set('content-type', 'application/json')
return response
}
Expand All @@ -126,7 +141,7 @@ export class VerifiedFetch {
onProgress: options?.onProgress
})
options?.onProgress?.(new CustomProgressEvent<CIDDetail>('verified-fetch:request:end', { cid, path }))
const response = new Response(JSON.stringify(result), { status: 200 })
const response = okResponse(JSON.stringify(result))
response.headers.set('content-type', 'application/json')
return response
}
Expand All @@ -139,7 +154,7 @@ export class VerifiedFetch {
onProgress: options?.onProgress
})
options?.onProgress?.(new CustomProgressEvent<CIDDetail>('verified-fetch:request:end', { cid, path }))
const response = new Response(result, { status: 200 })
const response = okResponse(JSON.stringify(result))
await this.setContentType(result, path, response)
return response
}
Expand All @@ -166,7 +181,7 @@ export class VerifiedFetch {
// terminalElement = stat
} catch (err: any) {
this.log('error loading path %c/%s', dirCid, rootFilePath, err)
return new Response('Unable to find index.html for directory at given path. Support for directories with implicit root is not implemented', { status: 501 })
return notSupportedResponse('Unable to find index.html for directory at given path. Support for directories with implicit root is not implemented')
} finally {
options?.onProgress?.(new CustomProgressEvent<CIDDetail>('verified-fetch:request:end', { cid: dirCid, path: rootFilePath }))
}
Expand All @@ -183,7 +198,7 @@ export class VerifiedFetch {
const { stream, firstChunk } = await getStreamFromAsyncIterable(asyncIter, path ?? '', this.helia.logger, {
onProgress: options?.onProgress
})
const response = new Response(stream, { status: 200 })
const response = okResponse(stream)
await this.setContentType(firstChunk, path, response)

return response
Expand All @@ -194,7 +209,7 @@ export class VerifiedFetch {
options?.onProgress?.(new CustomProgressEvent<CIDDetail>('verified-fetch:request:start', { cid, path }))
const result = await this.helia.blockstore.get(cid)
options?.onProgress?.(new CustomProgressEvent<CIDDetail>('verified-fetch:request:end', { cid, path }))
const response = new Response(decode(result), { status: 200 })
const response = okResponse(decode(result))
await this.setContentType(result, path, response)
return response
}
Expand Down Expand Up @@ -261,22 +276,23 @@ export class VerifiedFetch {
* These format handlers should adjust the response headers as specified in https://specs.ipfs.tech/http-gateways/path-gateway/#response-headers
*/
private readonly formatHandlers: Record<string, FetchHandlerFunction> = {
raw: async () => new Response('application/vnd.ipld.raw support is not implemented', { status: 501 }),
raw: async () => notSupportedResponse('application/vnd.ipld.raw support is not implemented'),
car: this.handleIPLDCar,
'ipns-record': this.handleIPNSRecord,
tar: async () => new Response('application/x-tar support is not implemented', { status: 501 }),
'dag-json': async () => new Response('application/vnd.ipld.dag-json support is not implemented', { status: 501 }),
'dag-cbor': async () => new Response('application/vnd.ipld.dag-cbor support is not implemented', { status: 501 }),
json: async () => new Response('application/json support is not implemented', { status: 501 }),
cbor: async () => new Response('application/cbor support is not implemented', { status: 501 })
tar: async () => notSupportedResponse('application/x-tar support is not implemented'),
'dag-json': async () => notSupportedResponse('application/vnd.ipld.dag-json support is not implemented'),
'dag-cbor': async () => notSupportedResponse('application/vnd.ipld.dag-cbor support is not implemented'),
json: async () => notSupportedResponse('application/json support is not implemented'),
cbor: async () => notSupportedResponse('application/cbor support is not implemented')
}

private readonly codecHandlers: Record<number, FetchHandlerFunction> = {
[dagJsonCode]: this.handleDagJson,
[dagPbCode]: this.handleDagPb,
[jsonCode]: this.handleJson,
[dagCborCode]: this.handleDagCbor,
[rawCode]: this.handleRaw
[rawCode]: this.handleRaw,
[identity.code]: this.handleRaw
}

async fetch (resource: Resource, opts?: VerifiedFetchOptions): Promise<Response> {
Expand Down Expand Up @@ -318,7 +334,7 @@ export class VerifiedFetch {
if (codecHandler != null) {
response = await codecHandler.call(this, { cid, path, options, terminalElement })
} else {
return new Response(`Support for codec with code ${cid.code} is not yet implemented. Please open an issue at https://github.com/ipfs/helia/issues/new`, { status: 501 })
return notSupportedResponse(`Support for codec with code ${cid.code} is not yet implemented. Please open an issue at https://github.com/ipfs/helia/issues/new`)
}
}

Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/create-offline-helia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Helia as HeliaClass } from '@helia/utils'
import { MemoryBlockstore } from 'blockstore-core'
import { MemoryDatastore } from 'datastore-core'
import type { Helia } from '@helia/interface'

export async function createHelia (): Promise<Helia> {
const datastore = new MemoryDatastore()
const blockstore = new MemoryBlockstore()

const helia = new HeliaClass({
datastore,
blockstore,
blockBrokers: [],
routers: []
})

await helia.start()

return helia
}
Loading

0 comments on commit cbab8b0

Please sign in to comment.