Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/download-cars-fr…
Browse files Browse the repository at this point in the history
…om-verified-fetch
  • Loading branch information
achingbrain committed Feb 22, 2024
2 parents 4a151fc + 7c3ce21 commit 02b1f04
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 34 deletions.
6 changes: 1 addition & 5 deletions packages/car/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ describe('import/export car file', () => {
const otherBlockstore = new MemoryBlockstore()
const otherUnixFS = unixfs({ blockstore: otherBlockstore })
const otherCar = car({ blockstore: otherBlockstore, dagWalkers })
const cid = await otherUnixFS.addBytes(largeFile, {
chunker: fixedSize({
chunkSize: 1024
})
})
const cid = await otherUnixFS.addBytes(largeFile)

const writer = memoryCarWriter(cid)
await otherCar.export(cid, writer)
Expand Down
1 change: 1 addition & 0 deletions packages/verified-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
"progress-events": "^1.0.0"
},
"devDependencies": {
"@helia/car": "^3.0.0",
"@helia/dag-cbor": "^3.0.0",
"@helia/dag-json": "^3.0.0",
"@helia/json": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/verified-fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@
*
* The `Accept` header can be passed to override certain response processing, or to ensure that the final `Content-Type` of the response is the one that is expected.
*
* If the final `Content-Type` does not match the `Accept` header, or if the content cannot be represented in the format dictated by the `Accept` header, or you have configured a custom content type parser, and that parser returns a value that isn't in the accept header, a [406: Not Acceptible](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406) response will be returned:
* If the final `Content-Type` does not match the `Accept` header, or if the content cannot be represented in the format dictated by the `Accept` header, or you have configured a custom content type parser, and that parser returns a value that isn't in the accept header, a [406: Not Acceptable](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406) response will be returned:
*
* ```typescript
* import { verifiedFetch } from '@helia/verified-fetch'
Expand Down
22 changes: 22 additions & 0 deletions packages/verified-fetch/src/utils/responses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export function okResponse (body?: BodyInit | null): Response {
return new Response(body, {
status: 200,
statusText: 'OK'
})
}

export function notSupportedResponse (body?: BodyInit | null): Response {
const response = new Response(body, {
status: 501,
statusText: '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
}

export function notAcceptableResponse (body?: BodyInit | null): Response {
return new Response(body, {
status: 406,
statusText: 'Not Acceptable'
})
}
3 changes: 3 additions & 0 deletions packages/verified-fetch/src/utils/select-output-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { code as rawCode } from 'multiformats/codecs/raw'
import type { RequestFormatShorthand } from '../types.js'
import type { CID } from 'multiformats/cid'

/**
* This maps supported response types for each codec supported by verified-fetch
*/
const CID_TYPE_MAP: Record<number, string[]> = {
[dagCborCode]: [
'application/json',
Expand Down
29 changes: 3 additions & 26 deletions packages/verified-fetch/src/verified-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getContentDispositionFilename } from './utils/get-content-disposition-f
import { getETag } from './utils/get-e-tag.js'
import { getStreamFromAsyncIterable } from './utils/get-stream-from-async-iterable.js'
import { parseResource } from './utils/parse-resource.js'
import { notAcceptableResponse, notSupportedResponse, okResponse } from './utils/responses.js'
import { selectOutputType, queryFormatToAcceptHeader } from './utils/select-output-type.js'
import { walkPath } from './utils/walk-path.js'
import type { CIDDetail, ContentTypeParser, Resource, VerifiedFetchInit as VerifiedFetchOptions } from './index.js'
Expand Down Expand Up @@ -68,29 +69,6 @@ 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 {
const response = new Response(body, {
status: 501,
statusText: '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
}

function notAcceptableResponse (body?: BodyInit | null): Response {
return new Response(body, {
status: 406,
statusText: '406 Not Acceptable'
})
}

/**
* These are Accept header values that will cause content type sniffing to be
* skipped and set to these values.
Expand All @@ -103,8 +81,8 @@ const RAW_HEADERS = [
/**
* if the user has specified an `Accept` header, and it's in our list of
* allowable "raw" format headers, use that instead of detecting the content
* type, to avoid the user signalling that they will Accepting one mime type
* and then receiving something different.
* type. This avoids the user from receiving something different when they
* signal that they want to `Accept` a specific mime type.
*/
function getOverridenRawContentType (headers?: HeadersInit): string | undefined {
const acceptHeader = new Headers(headers).get('accept') ?? ''
Expand Down Expand Up @@ -262,7 +240,6 @@ export class VerifiedFetch {
terminalElement = pathDetails.terminalElement
} catch (err) {
this.log.error('Error walking path %s', path, err)
// return new Response(`Error walking path: ${(err as Error).message}`, { status: 500 })
}

let resolvedCID = terminalElement?.cid ?? cid
Expand Down
4 changes: 2 additions & 2 deletions packages/verified-fetch/test/accept-header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('accept header', () => {
expect(output).to.deep.equal(obj)
})

it('should return 406 Not Acceptable if the accept header cannot be honoured', async () => {
it('should return 406 Not Acceptable if the accept header cannot be adhered to', async () => {
const obj = {
hello: 'world'
}
Expand All @@ -151,7 +151,7 @@ describe('accept header', () => {
}
})
expect(resp.status).to.equal(406)
expect(resp.statusText).to.equal('406 Not Acceptable')
expect(resp.statusText).to.equal('Not Acceptable')
})

it('should support wildcards', async () => {
Expand Down

0 comments on commit 02b1f04

Please sign in to comment.