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

fix: allow contentTypeParser with Helia instance #427

Merged
merged 2 commits into from
Feb 9, 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
3 changes: 1 addition & 2 deletions packages/interop/src/verified-fetch-json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ describe('@helia/verified-fetch - json', () => {
await loadFixtureDataCar(controller, 'QmQJ8fxavY54CUsxMSx9aE9Rdcmvhx8awJK2jzJp4iAqCr-tokens.uniswap.org-2024-01-18.car')
verifiedFetch = await createVerifiedFetch({
gateways: [`http://${controller.api.gatewayHost}:${controller.api.gatewayPort}`],
// Temporarily disabling delegated routers in browser until CORS issue is fixed. see https://github.com/ipshipyard/waterworks-community/issues/4
routers: process.env.RUNNER_ENV === 'node' ? [`http://${controller.api.gatewayHost}:${controller.api.gatewayPort}`] : []
routers: [`http://${controller.api.gatewayHost}:${controller.api.gatewayPort}`]
})
})

Expand Down
7 changes: 3 additions & 4 deletions packages/interop/src/verified-fetch-unixfs-dir.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ describe('@helia/verified-fetch - unixfs directory', () => {

verifiedFetch = await createVerifiedFetch({
gateways: [`http://${controller.api.gatewayHost}:${controller.api.gatewayPort}`],
// Temporarily disabling delegated routers in browser until CORS issue is fixed. see https://github.com/ipshipyard/waterworks-community/issues/4
routers: process.env.RUNNER_ENV === 'node' ? [`http://${controller.api.gatewayHost}:${controller.api.gatewayPort}`] : []
routers: [`http://${controller.api.gatewayHost}:${controller.api.gatewayPort}`]
})
})

Expand Down Expand Up @@ -61,8 +60,8 @@ describe('@helia/verified-fetch - unixfs directory', () => {
await verifiedFetch.stop()
verifiedFetch = await createVerifiedFetch({
gateways: [`http://${controller.api.gatewayHost}:${controller.api.gatewayPort}`],
// Temporarily disabling delegated routers in browser until CORS issue is fixed. see https://github.com/ipshipyard/waterworks-community/issues/4
routers: process.env.RUNNER_ENV === 'node' ? [`http://${controller.api.gatewayHost}:${controller.api.gatewayPort}`] : [],
routers: [`http://${controller.api.gatewayHost}:${controller.api.gatewayPort}`]
}, {
contentTypeParser: (bytes) => {
return filetypemime(bytes)?.[0]
}
Expand Down
6 changes: 2 additions & 4 deletions packages/interop/src/verified-fetch-websites.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ describe('@helia/verified-fetch - websites', () => {
await loadFixtureDataCar(controller, 'QmbxpRxwKXxnJQjnPqm1kzDJSJ8YgkLxH23mcZURwPHjGv-helia-identify-website.car')
verifiedFetch = await createVerifiedFetch({
gateways: [`http://${controller.api.gatewayHost}:${controller.api.gatewayPort}`],
// Temporarily disabling delegated routers in browser until CORS issue is fixed. see https://github.com/ipshipyard/waterworks-community/issues/4
routers: process.env.RUNNER_ENV === 'node' ? [`http://${controller.api.gatewayHost}:${controller.api.gatewayPort}`] : []
routers: [`http://${controller.api.gatewayHost}:${controller.api.gatewayPort}`]
})
})

Expand Down Expand Up @@ -65,8 +64,7 @@ describe('@helia/verified-fetch - websites', () => {
await loadFixtureDataCar(controller, 'QmeiDMLtPUS3RT2xAcUwsNyZz169wPke2q7im9vZpVLSYw-fake-blog.libp2p.io.car')
verifiedFetch = await createVerifiedFetch({
gateways: [`http://${controller.api.gatewayHost}:${controller.api.gatewayPort}`],
// Temporarily disabling delegated routers in browser until CORS issue is fixed. see https://github.com/ipshipyard/waterworks-community/issues/4
routers: process.env.RUNNER_ENV === 'node' ? [`http://${controller.api.gatewayHost}:${controller.api.gatewayPort}`] : []
routers: [`http://${controller.api.gatewayHost}:${controller.api.gatewayPort}`]
})
})

Expand Down
12 changes: 7 additions & 5 deletions packages/verified-fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@
*
* const fetch = await createVerifiedFetch({
* gateways: ['https://trustless-gateway.link'],
* routers: ['http://delegated-ipfs.dev'],
* routers: ['http://delegated-ipfs.dev']
* }, {
* contentTypeParser: async (bytes) => {
* // call to some magic-byte recognition library like magic-bytes, file-type, or your own custom byte recognition
* const result = await fileTypeFromBuffer(bytes)
Expand Down Expand Up @@ -285,10 +286,12 @@ export interface VerifiedFetch {
* Instead of passing a Helia instance, you can pass a list of gateways and
* routers, and a HeliaHTTP instance will be created for you.
*/
export interface CreateVerifiedFetchOptions {
export interface CreateVerifiedFetchInit {
gateways: string[]
routers?: string[]
}

export interface CreateVerifiedFetchOptions {
/**
* A function to handle parsing content type from bytes. The function you
* provide will be passed the first set of bytes we receive from the network,
Expand Down Expand Up @@ -338,11 +341,10 @@ export interface VerifiedFetchInit extends RequestInit, ProgressOptions<BubbledP
/**
* Create and return a Helia node
*/
export async function createVerifiedFetch (init?: Helia | CreateVerifiedFetchOptions): Promise<VerifiedFetch> {
let contentTypeParser: ContentTypeParser | undefined
export async function createVerifiedFetch (init?: Helia | CreateVerifiedFetchInit, options?: CreateVerifiedFetchOptions): Promise<VerifiedFetch> {
const contentTypeParser: ContentTypeParser | undefined = options?.contentTypeParser

if (!isHelia(init)) {
contentTypeParser = init?.contentTypeParser
init = await createHeliaHTTP({
blockBrokers: [
trustlessGateway({
Expand Down
Loading