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

Optimize js-waku for the Browser #337

Open
9 of 15 tasks
D4nte opened this issue Dec 2, 2021 · 8 comments
Open
9 of 15 tasks

Optimize js-waku for the Browser #337

D4nte opened this issue Dec 2, 2021 · 8 comments
Assignees
Projects

Comments

@D4nte
Copy link
Contributor

D4nte commented Dec 2, 2021

Problem

js-waku targets the browser. NodeJS user should use nim-waku and JSON RPC API.

There are a number of issues when using js-waku in the browser:

  • Node/browser switches in the code
  • Polyfills for using NodeJS APIs (buffer, crypto, stream, process)
  • Bundle is too big (>1MB, should target 200kb).
  • Several crypto libraries are used, increasing bundle size.

References

Tasks

Acceptance Criteria

Analysis done in #527. Once done, action plan can be updated.

Note, if some dependencies do need polyfills, then it is possible to bundle the polyfill so that users do not have to set it up. It is also possible to only bundle the necessary APIs. see https://github.com/ethers-io/ethers.js/blob/4e6d121fb8aa7327290afab7653364be8ddd8d81/packages/shims/src/index.js

@D4nte D4nte added this to New in js-waku Dec 2, 2021
@D4nte D4nte moved this from New to Backlog in js-waku Dec 2, 2021
@teacoat
Copy link

teacoat commented Dec 2, 2021

For reference, here is my current polyfill instructions for my package:

Crypto-browserify

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }'
        - install 'crypto-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "crypto": false }

Fix

npm i crypto-browserify --save

Then add to your tsconfig.json:

"compilerOptions": {
    "paths": {
        "crypto": [
            "node_modules/crypto-browserify"
        ]
    }
}

Stream-browserify

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
        - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "stream": false }

Fix

npm i stream-browserify --save

Then add to your tsconfig.json:

"compilerOptions": {
    "paths": {
        "crypto": [
            "node_modules/stream-browserify"
        ]
    }
}

DUMP_SESSION_KEYS

constants.js:6 Uncaught TypeError: Cannot read properties of undefined (reading 'DUMP_SESSION_KEYS')

Fix

npm i process --save

Add polyfill somewhere:

global.process = require('process');

@teacoat
Copy link

teacoat commented Dec 2, 2021

As well as my commonJS list that I'm using for angular:

"allowedCommonJsDependencies": [
                "it-concat",
                "crypto",
                "debug",
                "libp2p",
                "libp2p-gossipsub/src/utils",
                "@chainsafe/libp2p-noise/dist/src/noise",
                "libp2p-bootstrap",
                "libp2p-websockets",
                "libp2p-websockets/src/filters",
                "multiaddr",
                "peer-id",
                "ecies-geth",
                "secp256k1",
                "libp2p-gossipsub",
                "hashconnect"
            ],

@D4nte
Copy link
Contributor Author

D4nte commented Dec 2, 2021

Thanks for that. Regarding DUMP_SESSION_KEYS it looks like I need to do some upstream fix: https://github.com/ChainSafe/js-libp2p-noise/blob/5d2e6ac201e9ba395d9ca465519335040653944c/src/constants.ts#L4

Thanks for the list of deps, I'll try to remove some once migrated to browser only and will probably have to do some upstream fixes :)

@D4nte
Copy link
Contributor Author

D4nte commented Jan 10, 2022

Note that I am updating the documentation to cover polyfills with react-scripts: waku-org/js.waku.guide#19

@D4nte
Copy link
Contributor Author

D4nte commented Feb 15, 2022

@chainsafe/libp2p-noise

This library brings two issues:

The bundle size is mostly due to the usage of node-forge, which is also used by libp2p-crypto.

node-forge https://github.com/digitalbazaar/forge actually provide ways to bundle only what is necessary. Need to check if the libp2p libraries actually leverage this bundling optimization.

@D4nte
Copy link
Contributor Author

D4nte commented Feb 17, 2022

It is possible to keep the package size small with node-forge: https://mobile.twitter.com/IAmTrySound/status/1476899235723456515 by using source-map-js: vitejs/vite#6556
Not sure if libp2p libraries use source-map-js. If they don't it might be a way to reduce their size.

@D4nte D4nte moved this from Backlog (old, to groom) to Icebox in js-waku Feb 21, 2022
@D4nte
Copy link
Contributor Author

D4nte commented Feb 21, 2022

Moved to icebox. Once #527 is done, we can update this one and move it back to backlog.

@D4nte D4nte mentioned this issue Apr 16, 2022
@fryorcraken fryorcraken changed the title Optimize js-waku for the Browser [Milestone] Optimize js-waku for the Browser Aug 7, 2023
@fryorcraken fryorcraken changed the title [Milestone] Optimize js-waku for the Browser [Epic] Optimize js-waku for the Browser Aug 24, 2023
@fryorcraken fryorcraken removed the milestone Tracks a subteam milestone label Aug 24, 2023
@fryorcraken fryorcraken added the epic Tracks a yearly team epic (only for waku-org/pm repo) label Aug 24, 2023
@weboko weboko self-assigned this Aug 31, 2023
@weboko
Copy link
Collaborator

weboko commented Sep 21, 2023

updated description to suit better needs of the issue

@fryorcraken fryorcraken added E:Comprehensive dev testing See https://github.com/waku-org/pm/issues/90 for details and removed epic Tracks a yearly team epic (only for waku-org/pm repo) E:2023-qa labels Sep 22, 2023
@fryorcraken fryorcraken changed the title [Epic] Optimize js-waku for the Browser Optimize js-waku for the Browser Sep 22, 2023
@chair28980 chair28980 removed the E:Comprehensive dev testing See https://github.com/waku-org/pm/issues/90 for details label Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: To Do
js-waku
Icebox
Development

No branches or pull requests

5 participants