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

feat: also check content-type with @sgtpooki/file-type #416

Closed
wants to merge 1 commit into from

Conversation

SgtPooki
Copy link
Member

@SgtPooki SgtPooki commented Feb 5, 2024

This PR adds an additional check for content-type via a fork of the file-type library, similar to how it's used in helia-http-gateway.

I forked https://github.com/sindresorhus/file-type because it doesn't currently support browsers due to direct or dependency consumption of node:stream, node:buffer, node:fs, or node:process.

The fork could still use some updates to support streams and confirm that memory usage is reasonable, but it's a start and confirmed working in helia-service-worker-gateway.

Using this fork, we would be able to remove the file-type dependency from helia-http-gateway and de-dupe the content-type checking code.

Related issues in the original repo:

@SgtPooki SgtPooki requested a review from a team as a code owner February 5, 2024 17:56
@@ -155,6 +155,7 @@
"@ipld/dag-pb": "^4.0.8",
"@libp2p/interface": "^1.1.2",
"@libp2p/peer-id": "^4.0.5",
"@sgtpooki/file-type": "^1.0.0-1e871f8",
Copy link
Member

Choose a reason for hiding this comment

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

Does this need the specifier or can it be an actual version?

Copy link
Member Author

Choose a reason for hiding this comment

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

I published an RC version while messing with exports. I can publish a new version. I believe I updated the NPM_token and publish from github CI should work now. I'll add you to maintainers on that package

@achingbrain
Copy link
Member

achingbrain commented Feb 5, 2024

file-type really looks like it needs a new module that strips out all the old node stuff.

We now have mime-types and the file-type fork as deps - how much do these add to the bundle size?

@achingbrain
Copy link
Member

how much do these add to the bundle size?

You can find this out with npm run build -- -b in packages/verified-fetch

Somewhat eye-opening:

image

Do we really need to be able to detect all of these mime types?

@SgtPooki
Copy link
Member Author

SgtPooki commented Feb 6, 2024

Do we really need to be able to detect all of these mime types?

@achingbrain honestly, no. and mime-types only does extension recognition, where file-type does magic byte recognition (which we need more) so I think removing mime-type would be ideal, and replacing with file-type

file-type really looks like it needs a new module that strips out all the old node stuff.

We now have mime-types and the file-type fork as deps

yea, if we're open to migrating to a file-type fork I can work on re-adding stream support using browser compatible streams, and also splitting up the node stuff so file reading works as well

@SgtPooki
Copy link
Member Author

SgtPooki commented Feb 6, 2024

removing mime-types reduces bundle size by 147kb (707.2kb to 560.2kb)

image

@SgtPooki
Copy link
Member Author

SgtPooki commented Feb 7, 2024

Closing this in favor of #422

@SgtPooki SgtPooki closed this Feb 7, 2024
@SgtPooki SgtPooki deleted the fix/file-type branch February 7, 2024 23:28
achingbrain added a commit that referenced this pull request Feb 8, 2024
* adds `contentTypeParser` function to createVerifiedFetch options & implements it.
* renamed `getStreamAndContentType` to `getStreamFromAsyncIterable` that now returns a stream with the firstChunk seen, so we can pass it to the `contentTypeParser` function.
* updates tests in packages/verified-fetch & packages/interop
* updates packageDocumentation with example

Related #416
Fixes #422
---------

Co-authored-by: achingbrain <alex@achingbrain.net>
achingbrain added a commit to ipfs/helia-verified-fetch that referenced this pull request Feb 28, 2024
* adds `contentTypeParser` function to createVerifiedFetch options & implements it.
* renamed `getStreamAndContentType` to `getStreamFromAsyncIterable` that now returns a stream with the firstChunk seen, so we can pass it to the `contentTypeParser` function.
* updates tests in packages/verified-fetch & packages/interop
* updates packageDocumentation with example

Related ipfs/helia#416
Fixes ipfs/helia#422
---------

Co-authored-by: achingbrain <alex@achingbrain.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants