Skip to content

Commit 3014498

Browse files
authored
fix: identity CIDs use contentTypeParser (#49)
* fix: identity CIDs use contentTypeParser * chore: fix logged content type * test: handleRaw uses contentTypeParser
1 parent f59e862 commit 3014498

4 files changed

Lines changed: 23 additions & 15 deletions

File tree

packages/verified-fetch/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"@sgtpooki/file-type": "^1.0.1",
9898
"@types/sinon": "^17.0.3",
9999
"aegir": "^42.2.5",
100-
"blockstore-core": "^4.4.0",
100+
"blockstore-core": "^4.4.1",
101101
"browser-readablestream-to-it": "^2.0.5",
102102
"datastore-core": "^9.2.9",
103103
"helia": "^4.1.0",

packages/verified-fetch/src/verified-fetch.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -423,18 +423,13 @@ export class VerifiedFetch {
423423
// if the user has specified an `Accept` header that corresponds to a raw
424424
// type, honour that header, so for example they don't request
425425
// `application/vnd.ipld.raw` but get `application/octet-stream`
426-
const overriddenContentType = getOverridenRawContentType({ headers: options?.headers, accept })
427-
if (overriddenContentType != null) {
428-
response.headers.set('content-type', overriddenContentType)
429-
} else {
430-
await this.setContentType(result, path, response)
431-
}
426+
await this.setContentType(result, path, response, getOverridenRawContentType({ headers: options?.headers, accept }))
432427

433428
return response
434429
}
435430

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

439434
if (this.contentTypeParser != null) {
440435
try {
@@ -455,8 +450,8 @@ export class VerifiedFetch {
455450
this.log.error('error parsing content type', err)
456451
}
457452
}
458-
this.log.trace('setting content type to "%s"', contentType)
459-
response.headers.set('content-type', contentType)
453+
this.log.trace('setting content type to "%s"', contentType ?? defaultContentType)
454+
response.headers.set('content-type', contentType ?? defaultContentType)
460455
}
461456

462457
/**

packages/verified-fetch/test/content-type-parser.spec.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import { createHeliaHTTP } from '@helia/http'
21
import { unixfs } from '@helia/unixfs'
32
import { stop } from '@libp2p/interface'
43
import { fileTypeFromBuffer } from '@sgtpooki/file-type'
54
import { expect } from 'aegir/chai'
65
import { filetypemime } from 'magic-bytes.js'
6+
import { CID } from 'multiformats/cid'
77
import Sinon from 'sinon'
88
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
99
import { createVerifiedFetch } from '../src/index.js'
1010
import { VerifiedFetch } from '../src/verified-fetch.js'
11+
import { createHelia } from './fixtures/create-offline-helia.js'
1112
import type { Helia } from '@helia/interface'
12-
import type { CID } from 'multiformats/cid'
1313

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

1919
beforeEach(async () => {
20-
helia = await createHeliaHTTP()
20+
helia = await createHelia()
2121
const fs = unixfs(helia)
2222
cid = await fs.addByteStream((async function * () {
2323
yield uint8ArrayFromString('H4sICIlTHVIACw', 'base64')
@@ -132,4 +132,16 @@ describe('content-type-parser', () => {
132132
const resp = await verifiedFetch.fetch(cid)
133133
expect(resp.headers.get('content-type')).to.equal('application/gzip')
134134
})
135+
136+
it('can properly set content type for identity CIDs', async () => {
137+
verifiedFetch = new VerifiedFetch({
138+
helia
139+
}, {
140+
contentTypeParser: async (data) => {
141+
return 'text/plain'
142+
}
143+
})
144+
const resp = await verifiedFetch.fetch(CID.parse('bafkqablimvwgy3y'))
145+
expect(resp.headers.get('content-type')).to.equal('text/plain')
146+
})
135147
})

packages/verified-fetch/test/fixtures/create-offline-helia.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Helia as HeliaClass } from '@helia/utils'
22
import { MemoryBlockstore } from 'blockstore-core'
3+
import { IdentityBlockstore } from 'blockstore-core/identity'
34
import { MemoryDatastore } from 'datastore-core'
45
import type { HeliaHTTPInit } from '@helia/http'
56
import type { Helia } from '@helia/interface'
67

78
export async function createHelia (init: Partial<HeliaHTTPInit> = {}): Promise<Helia> {
89
const datastore = new MemoryDatastore()
9-
const blockstore = new MemoryBlockstore()
10+
const blockstore = new IdentityBlockstore(new MemoryBlockstore())
1011

1112
const helia = new HeliaClass({
1213
datastore,

0 commit comments

Comments
 (0)