-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add more content type tests and default to application/octet-s…
…tream
- Loading branch information
1 parent
abf9a0d
commit ab4ecd1
Showing
7 changed files
with
139 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** @type {import('aegir').PartialOptions} */ | ||
const options = { | ||
build: { | ||
bundlesizeMax: '132KB' | ||
} | ||
} | ||
|
||
export default options |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
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 { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' | ||
import { VerifiedFetch } from '../src/verified-fetch.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() | ||
const fs = unixfs(helia) | ||
cid = await fs.addByteStream((async function * () { | ||
yield uint8ArrayFromString('H4sICIlTHVIACw', 'base64') | ||
})()) | ||
}) | ||
|
||
afterEach(async () => { | ||
await stop(verifiedFetch) | ||
}) | ||
|
||
it('does not set content type if contentTypeParser is not passed', async () => { | ||
verifiedFetch = new VerifiedFetch({ | ||
helia | ||
}) | ||
const resp = await verifiedFetch.fetch(cid) | ||
expect(resp.headers.get('content-type')).to.equal('application/octet-stream') | ||
}) | ||
|
||
it('sets content type if contentTypeParser is passed', async () => { | ||
verifiedFetch = new VerifiedFetch({ | ||
helia | ||
}, { | ||
contentTypeParser: () => 'text/plain' | ||
}) | ||
const resp = await verifiedFetch.fetch(cid) | ||
expect(resp.headers.get('content-type')).to.equal('text/plain') | ||
}) | ||
|
||
it('supports @sgtpooki/file-type as a contentTypeParser', async () => { | ||
verifiedFetch = new VerifiedFetch({ | ||
helia | ||
}, { | ||
contentTypeParser: async (bytes) => { | ||
const type = await fileTypeFromBuffer(bytes) | ||
return type?.mime | ||
} | ||
}) | ||
const resp = await verifiedFetch.fetch(cid) | ||
expect(resp.headers.get('content-type')).to.equal('application/gzip') | ||
}) | ||
|
||
it('supports magic-bytes.js as a contentTypeParser', async () => { | ||
verifiedFetch = new VerifiedFetch({ | ||
helia | ||
}, { | ||
contentTypeParser: (bytes) => { | ||
return filetypemime(bytes)?.[0] | ||
} | ||
}) | ||
const resp = await verifiedFetch.fetch(cid) | ||
expect(resp.headers.get('content-type')).to.equal('application/gzip') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters