This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
feat: support secure images #10
Open
angeloashmore
wants to merge
3
commits into
unsplash:master
Choose a base branch
from
angeloashmore:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for sending this PR, we should definitely add support for
s
.One thing we will have to consider here is the cost of importing
crypto
, especially the impact it will have on the bundle size when considering the fact that this library may be used in the browser.I ran a quick test using a webpack project (in production mode) with the following code:
The result was 110 kB gzipped! 😱
https://github.com/OliverJAsh/tree-shaking-test/tree/crypto
Given that
crypto
is costly to import, I wonder whether it's right for this library to be responsible for encoding the signature. I agree it would be convenient to be able to provide a decodedtoken
to the build function and let it handle the rest, but if it comes at such a large cost then I think we should reconsider.If this wasn't handled by the library, it would be very easy for users to wrap
buildImgixUrl
to handle this:We would of course still need to add support for the
s
query parameter, but the library would just assume it's already encoded. WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! I didn't consider browser usage. There are much smaller MD5 hashing functions that could be used instead of crypto.
tiny-hashes
includes a "good enough" MD5 algorithm that could be a candidate.https://github.com/jbt/tiny-hashes/blob/master/md5/md5.js
Signing should only happen server-side since the token is a secret. It could make more sense to export an additional function like
buildImgixUrlWithToken
rather than addingtoken
to the mainbuildImgixUrl
.The
s
param is generated from the query params so I definitely think there's value in including this in the library. That being said, I could also see leaving it out to simplify the scope of the package. It's not difficult for someone to write their own function to append the signature.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That could work, since tree shaking will detect that function is not used in the browser, so consumers won't pay the price of importing
crypto
. However we don't currently distribute this module in ESM, so we'd have to do that first (since tree shaking only works with ES Modules).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you be open to adopting a bundler like Rollup, or even a tool like TSDX? If so, I can make a separate PR with the infrastructure changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be open to that. Personally I think I would prefer Rollup.