Skip to content

Commit

Permalink
fix: identity CIDs use contentTypeParser (#49)
Browse files Browse the repository at this point in the history
* fix: identity CIDs use contentTypeParser

* chore: fix logged content type

* test: handleRaw uses contentTypeParser
  • Loading branch information
SgtPooki authored Apr 11, 2024
1 parent f59e862 commit 3014498
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/verified-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"@sgtpooki/file-type": "^1.0.1",
"@types/sinon": "^17.0.3",
"aegir": "^42.2.5",
"blockstore-core": "^4.4.0",
"blockstore-core": "^4.4.1",
"browser-readablestream-to-it": "^2.0.5",
"datastore-core": "^9.2.9",
"helia": "^4.1.0",
Expand Down
15 changes: 5 additions & 10 deletions packages/verified-fetch/src/verified-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,18 +423,13 @@ export class VerifiedFetch {
// if the user has specified an `Accept` header that corresponds to a raw
// type, honour that header, so for example they don't request
// `application/vnd.ipld.raw` but get `application/octet-stream`
const overriddenContentType = getOverridenRawContentType({ headers: options?.headers, accept })
if (overriddenContentType != null) {
response.headers.set('content-type', overriddenContentType)
} else {
await this.setContentType(result, path, response)
}
await this.setContentType(result, path, response, getOverridenRawContentType({ headers: options?.headers, accept }))

return response
}

private async setContentType (bytes: Uint8Array, path: string, response: Response): Promise<void> {
let contentType = 'application/octet-stream'
private async setContentType (bytes: Uint8Array, path: string, response: Response, defaultContentType = 'application/octet-stream'): Promise<void> {
let contentType: string | undefined

if (this.contentTypeParser != null) {
try {
Expand All @@ -455,8 +450,8 @@ export class VerifiedFetch {
this.log.error('error parsing content type', err)
}
}
this.log.trace('setting content type to "%s"', contentType)
response.headers.set('content-type', contentType)
this.log.trace('setting content type to "%s"', contentType ?? defaultContentType)
response.headers.set('content-type', contentType ?? defaultContentType)
}

/**
Expand Down
18 changes: 15 additions & 3 deletions packages/verified-fetch/test/content-type-parser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { createHeliaHTTP } from '@helia/http'
import { unixfs } from '@helia/unixfs'
import { stop } from '@libp2p/interface'
import { fileTypeFromBuffer } from '@sgtpooki/file-type'
import { expect } from 'aegir/chai'
import { filetypemime } from 'magic-bytes.js'
import { CID } from 'multiformats/cid'
import Sinon from 'sinon'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { createVerifiedFetch } from '../src/index.js'
import { VerifiedFetch } from '../src/verified-fetch.js'
import { createHelia } from './fixtures/create-offline-helia.js'
import type { Helia } from '@helia/interface'
import type { CID } from 'multiformats/cid'

describe('content-type-parser', () => {
let helia: Helia
let cid: CID
let verifiedFetch: VerifiedFetch

beforeEach(async () => {
helia = await createHeliaHTTP()
helia = await createHelia()
const fs = unixfs(helia)
cid = await fs.addByteStream((async function * () {
yield uint8ArrayFromString('H4sICIlTHVIACw', 'base64')
Expand Down Expand Up @@ -132,4 +132,16 @@ describe('content-type-parser', () => {
const resp = await verifiedFetch.fetch(cid)
expect(resp.headers.get('content-type')).to.equal('application/gzip')
})

it('can properly set content type for identity CIDs', async () => {
verifiedFetch = new VerifiedFetch({
helia
}, {
contentTypeParser: async (data) => {
return 'text/plain'
}
})
const resp = await verifiedFetch.fetch(CID.parse('bafkqablimvwgy3y'))
expect(resp.headers.get('content-type')).to.equal('text/plain')
})
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Helia as HeliaClass } from '@helia/utils'
import { MemoryBlockstore } from 'blockstore-core'
import { IdentityBlockstore } from 'blockstore-core/identity'
import { MemoryDatastore } from 'datastore-core'
import type { HeliaHTTPInit } from '@helia/http'
import type { Helia } from '@helia/interface'

export async function createHelia (init: Partial<HeliaHTTPInit> = {}): Promise<Helia> {
const datastore = new MemoryDatastore()
const blockstore = new MemoryBlockstore()
const blockstore = new IdentityBlockstore(new MemoryBlockstore())

const helia = new HeliaClass({
datastore,
Expand Down

0 comments on commit 3014498

Please sign in to comment.