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

chore: setup localstorage dynamic polyfill for node env #1963

Closed
wants to merge 1 commit into from

Conversation

danisharora099
Copy link
Collaborator

Problem

Reference to #1953, we see local storage related functionality breaking on node environments because localStorage is undefined.

While js-waku is targeted for browser environments, we need to offer flexibility in terms of its usage in node environment without things breaking.

Solution

Allow dynamic imports to polyfill localStorage in node environments.

Notes

Contribution checklist:

  • covered by unit tests;
  • covered by e2e test;
  • add ! in title if breaks public API;

@danisharora099 danisharora099 marked this pull request as ready for review April 18, 2024 09:51
@danisharora099 danisharora099 requested a review from a team as a code owner April 18, 2024 09:51
if (typeof window === "undefined") {
try {
const { LocalStorage } = await import("node-localstorage");
global.localStorage = new LocalStorage("./mock-localstorage");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there any benefit of providing this to NodeJS?

To me just ignoring local storage is already good enough, better way would be to prevent it from running in NodeJS.

Copy link
Collaborator Author

@danisharora099 danisharora099 Apr 19, 2024

Choose a reason for hiding this comment

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

To me just ignoring local storage is already good enough

ignoring would mean that we there would still be a failing action, and an error log associated with it which imo isn't ideal

better way would be to prevent it from running in NodeJS

which would imply we disallow usage of local-peer-cache-discovery in nodejs environments which is relatively more trivial, and also disables the functionality

very straightforward to use a mock localstorage (mock DB of sorts) instead with dynamic polyfilling approach in the PR

Copy link
Collaborator

Choose a reason for hiding this comment

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

I am concerned about us trying to add polyfills to library for the browser only - this can lead to unintentional leak of the polyfill to browser. Did you check this?

failing action, and an error log associated

as for this - we can do a better logging then and add getter for localStorage with embedded error handling

how does this sound?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am concerned about us trying to add polyfills to library for the browser only

We are introducing the polyfill for nodejs, not browsers. node-localstorage is a nodejs dependency that creates a mock localstorage.

this can lead to unintentional leak of the polyfill to browser. Did you check this?

Yes - the browser tests do check for this potential leak.

@danisharora099
Copy link
Collaborator Author

closing this PR in favor of deprioritising nodejs

@danisharora099 danisharora099 deleted the fix/local-storage-node branch May 9, 2024 10:18
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.

bug: localStorage is not defined
3 participants