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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"libp",
"lightpush",
"livechat",
"localstorage",
"Merkle",
"mkdir",
"mplex",
Expand Down
4 changes: 1 addition & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/discovery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"debug": "^4.3.4",
"dns-query": "^0.11.2",
"hi-base32": "^0.5.1",
"uint8arrays": "^5.0.1"
"uint8arrays": "^5.0.1",
"node-localstorage": "^3.0.5"
},
"devDependencies": {
"@libp2p/peer-id": "^4.0.4",
Expand All @@ -75,7 +76,6 @@
"chai-as-promised": "^7.1.1",
"cspell": "^8.6.1",
"mocha": "^10.3.0",
"node-localstorage": "^3.0.5",
"npm-run-all": "^4.1.5",
"rollup": "^4.12.0",
"sinon": "^17.0.1"
Expand Down
10 changes: 0 additions & 10 deletions packages/discovery/src/local-peer-cache/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ import { LocalPeerCacheDiscovery } from "./index.js";

chai.use(chaiAsPromised);

// dynamically importing the local storage polyfill for node
if (typeof window === "undefined") {
try {
const { LocalStorage } = await import("node-localstorage");
global.localStorage = new LocalStorage("./scratch");
} catch (error) {
console.error("Failed to load localStorage polyfill:", error);
}
}

const mockPeers = [
{
id: "16Uiu2HAm4v86W3bmT1BiH6oSPzcsSr24iDQpSN5Qa992BCjjwgrD",
Expand Down
10 changes: 10 additions & 0 deletions packages/discovery/src/local-peer-cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
const DEFAULT_LOCAL_TAG_VALUE = 50;
const DEFAULT_LOCAL_TAG_TTL = 100_000_000;

// dynamically importing the local storage polyfill for node environments
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.

} catch (error) {
log.error("Failed to load localStorage polyfill:", error);
}
}

export class LocalPeerCacheDiscovery
extends TypedEventEmitter<PeerDiscoveryEvents>
implements PeerDiscovery, Startable
Expand Down Expand Up @@ -144,7 +154,7 @@
}
}

function isValidStoredPeer(peer: any): peer is LocalStoragePeerInfo {

Check warning on line 157 in packages/discovery/src/local-peer-cache/index.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type

Check warning on line 157 in packages/discovery/src/local-peer-cache/index.ts

View workflow job for this annotation

GitHub Actions / proto

Unexpected any. Specify a different type
return (
peer &&
typeof peer === "object" &&
Expand Down
Loading