kinecosystem / kin-node

DEPRECATED! Please use Kinetic: https://developer.kin.org/docs/kinetic
https://developer.kin.org/docs/kinetic
MIT License
16 stars 14 forks source link

(question) possible to reduce size of package + dependencies? #3

Closed doc-l closed 3 years ago

doc-l commented 3 years ago

First of all great work on this SDK! It's super easy, works great. I have set it up in an AWS Lambda serverless function, which also works (yay for serverless blockchain communication!), but the package size is quite big: almost 50MB zipped. The AWS Lambda limit is exactly 50MB zipped, so it barely fits inside a Lambda function. However, when you need a couple more dependencies you already have a problem. This is probably due to gRPC and its many many subdependencies.

Would it be possible to create a minimal version, check if all dependencies are really needed or something so the package size can be decreased?

Thanks!

kikengineering commented 3 years ago

There seems to be some decent wins that can be done by separating out a few of the agora-api dependencies into dev only dependencies.

The two biggest culprits seem to be grpc and sodium-native. The former I think we're somewhat stuck using for now. The latter, at least taking a peak locally, seems to be largely driven by the intermediate build objects. sodium-native is currently used (transitively) by stellar-base.

We can poke around a bit with the npm dependency / build system to try and make sodium-native more manageable.

kikengineering commented 3 years ago

Perhaps as mentioned in https://github.com/kinecosystem/kin-node/issues/2#issuecomment-696424338, we can just remove sodium-native as a dependency, and let consumers decide whether or not they want to pull the dependency in.

kikengineering commented 3 years ago

The agora-api changes have been pulled in to kin-sdk-v2@0.2.2 (https://github.com/kinecosystem/kin-node/commit/a3f83142845c643b83ffc813cd17231151acf3d3).

As for sodium-native, a comment has been added to the readme indicating it can be safely removed (at the cost of some performance). Running the following steps now produces a zipped node_modules that is about ~24 MiB:

  1. npm i --production
  2. rm -rf node_modules/sodium-native
  3. zip modules.zip node_modules

Let us know if this is sufficient for you use cases.

doc-l commented 3 years ago

Thanks a lot, this works! The package size now decreased to around 35MB instead of 49MB. This gives plenty of room for other stuff. It's still quite big, but I guess gRPC is quite heavy as well.

By the way, if you use serverless and the serverless-webpack plugin, you can get it working easily by adding this in serverless.yml:

custom:
  webpack:
    packager: 'yarn'
    packagerOptions:
      scripts:
        - rm -rf node_modules/grpc/src/node/extension_binary/* # Remove grpc for other systems
        - rm -rf node_modules/sodium-native
        - npm rebuild grpc --target=12.0.0 --target_arch=x64 --target_platform=linux --target_libc=glibc # add grpc for node lambda env
    includeModules:
      forceExclude:
        - aws-sdk