ipfs / helia

An implementation of IPFS in JavaScript
https://helia.io
Other
814 stars 81 forks source link

feat: support `Accept` header in `@helia/verified-fetch` #438

Closed achingbrain closed 4 months ago

achingbrain commented 4 months ago

Let users get raw data back from CIDs that would otherwise trigger decoding as JSON or CBOR etc by specifying an Accept header.

const res = await verifiedFetch(cid, {
  headers: {
    accept: 'application/octet-stream'
  }
})
console.info(res.headers.get('accept')) // application/octet-stream

Make sure the content-type matches the accept header:

const res = await verifiedFetch(cid, {
  headers: {
    accept: 'application/vnd.ipld.raw'
  }
})
console.info(res.headers.get('accept')) // application/vnd.ipld.raw

Support multiple values, match the first one:

const res = await verifiedFetch(cid, {
  headers: {
    accept: 'application/vnd.ipld.raw, application/octet-stream, */*'
  }
})
console.info(res.headers.get('accept')) // application/vnd.ipld.raw

If they specify an Accept header we can't honour, one that doesn't match the eventual content type, or one returned by the content type parser return a 406:

const res = await verifiedFetch(cid, {
  headers: {
    accept: 'application/what-even-is-this'
  }
})
console.info(res.status) // 406

Change checklist

achingbrain commented 4 months ago

@lidel @2color please take another look - I've added a lot more tests, support for wildcards and q-factor and also the IPLD serialized type conversions from https://specs.ipfs.tech/http-gateways/path-gateway/#accept-request-header