Skip to content
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

fix: identity CIDs use contentTypeParser #49

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update to get ipfs/js-stores#303

"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'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the offline helia.

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')
})
})
Comment on lines +136 to +146
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test for this fix

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())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allow identity blocks when testing


const helia = new HeliaClass({
datastore,
Expand Down
Loading