mhart / aws4

Signs and prepares Node.js requests using AWS Signature Version 4
MIT License
703 stars 176 forks source link

Consider replacing `crypto` dependency with lighter-weight alternative #85

Closed stevendesu closed 5 years ago

stevendesu commented 5 years ago

I'm pretty obsessed with trying to keep my bundles as small as possible to increase load time on mobile devices. That's why when I switched from the AWS SDK (~1.9 MB) to aws4 (~11 KB) I was really excited to see the massive drop in my bundle size. However the bundle was still significantly larger than I thought it should be. Without aws4 (or the AWS SDK) my bundle is 759 KB. When I add aws4 (and make no other changes) it jumps to 1.1 MB -- an additional 350 KB just by adding an 11 KB library

Doing some investigation I found out the reason for this. When Webpack bundles aws4, it also has to bundle crypto. This pulls in browserify-crypto which is a roughly 300 KB library adding support for every hash and encryption under the sun -- all so we can sha256 something.

Replacing crypto with a light-weight sha256-only library like sha.js could reduce final bundle size for web assets by 300+ KB.

Alternatively instead of pulling in another library at all, you could consider rolling your own sha256 implementation from the standard. It's not that complex.

Your other two dependencies (url and querystring) are likewise getting bundled automatically, but they don't have nearly the same impact on the final file size that crypto does.

stevendesu commented 5 years ago

Just noticed aws4fetch -- so this whole request may be a moot point. Looks like it can do what I'm looking for.

mhart commented 5 years ago

Yeah, this is a Node.js library, so I'm not going to replace built-in modules for 3rd party ones. Obviously you can bundle it for the browser, but that was never the intention for this lib.

JacopoKenzo commented 1 year ago

@stevendesu thank you for sharing aws4fetch, way better solution.