-
Notifications
You must be signed in to change notification settings - Fork 105
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: require content-type parser to set content-type #423
Changes from 3 commits
c4f3355
e31f907
63d0530
abf9a0d
ab4ecd1
1a291d7
f2e80c4
03e9835
e7e24da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -75,7 +75,7 @@ | |||||
* const fetch = await createVerifiedFetch({ | ||||||
* gateways: ['https://trustless-gateway.link'], | ||||||
* routers: ['http://delegated-ipfs.dev'] | ||||||
*}) | ||||||
* }) | ||||||
* | ||||||
* const resp = await fetch('ipfs://bafy...') | ||||||
* | ||||||
|
@@ -112,6 +112,26 @@ | |||||
* const json = await resp.json() | ||||||
* ``` | ||||||
* | ||||||
* ### Custom content-type parsing | ||||||
* | ||||||
* By default, `@helia/verified-fetch` does not set the `Content-Type` header. This is because the `.json()`, `.text()`, `.blob()`, and `.arrayBuffer()` methods will usually work as expected. You can provide a `contentTypeParser` function to the `createVerifiedFetch` function to handle parsing the content type. The function you provide will be passed the first bytes we receive from the network. | ||||||
* | ||||||
* @example Customizing content-type parsing | ||||||
* | ||||||
* ```typescript | ||||||
* import { createVerifiedFetch } from '@helia/verified-fetch' | ||||||
* import { fileTypeFromBuffer } from '@sgtpooki/file-type' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not referencing Not referencing |
||||||
* | ||||||
* const fetch = await createVerifiedFetch({ | ||||||
* gateways: ['https://trustless-gateway.link'], | ||||||
* routers: ['http://delegated-ipfs.dev'], | ||||||
* contentTypeParser: async (bytes) => { | ||||||
2color marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
* // call to some magic-byte recognition library like magic-bytes, file-type, or your own custom byte recognition | ||||||
* return fileTypeFromBuffer(bytes)?.mime ?? 'application/octet-stream' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* } | ||||||
* }) | ||||||
* ``` | ||||||
* | ||||||
* ## Comparison to fetch | ||||||
* | ||||||
* This module attempts to act as similarly to the `fetch()` API as possible. | ||||||
|
@@ -229,7 +249,7 @@ | |||||
import { trustlessGateway } from '@helia/block-brokers' | ||||||
import { createHeliaHTTP } from '@helia/http' | ||||||
import { delegatedHTTPRouting } from '@helia/routers' | ||||||
import { VerifiedFetch as VerifiedFetchClass } from './verified-fetch.js' | ||||||
import { VerifiedFetch as VerifiedFetchClass, type ContentTypeParser } from './verified-fetch.js' | ||||||
import type { Helia } from '@helia/interface' | ||||||
import type { IPNSRoutingEvents, ResolveDnsLinkProgressEvents, ResolveProgressEvents } from '@helia/ipns' | ||||||
import type { GetEvents } from '@helia/unixfs' | ||||||
|
@@ -262,8 +282,16 @@ export interface VerifiedFetch { | |||||
export interface CreateVerifiedFetchWithOptions { | ||||||
gateways: string[] | ||||||
routers?: string[] | ||||||
/** | ||||||
* 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, and should return a string that will be used as the value for the `Content-Type` | ||||||
* header in the response. | ||||||
*/ | ||||||
contentTypeParser?: ContentTypeParser | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to add contentTypeParser to verifiedFetch options? I don't think we need to allow passing it for each call, but we could. LMK There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps not in the first instance to keep it simple? We can always add it later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to accept an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure, I think maybe we want to create a separation between the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thats why I went with this initially. we could always update if we really need to, but i don't imagine createVerifiedFetch init args will vary much |
||||||
} | ||||||
|
||||||
export type { ContentTypeParser } | ||||||
|
||||||
export type BubbledProgressEvents = | ||||||
// unixfs | ||||||
GetEvents | | ||||||
|
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could link to #422 if desired.