Skip to content

Commit

Permalink
setup dynamic polyfill for node env
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Apr 18, 2024
1 parent e5e8cd5 commit e30bc95
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 46 deletions.
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
66 changes: 32 additions & 34 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 @@ export const DEFAULT_LOCAL_TAG_NAME = Tags.LOCAL;
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");
} catch (error) {
log.error("Failed to load localStorage polyfill:", error);
}
}

export class LocalPeerCacheDiscovery
extends TypedEventEmitter<PeerDiscoveryEvents>
implements PeerDiscovery, Startable
Expand Down

0 comments on commit e30bc95

Please sign in to comment.