diff --git a/.cspell.json b/.cspell.json index 4d79829a48..41e08ddc95 100644 --- a/.cspell.json +++ b/.cspell.json @@ -63,6 +63,7 @@ "libp", "lightpush", "livechat", + "localstorage", "Merkle", "mkdir", "mplex", diff --git a/package-lock.json b/package-lock.json index 5b0b33e003..2ba4ab3877 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24321,7 +24321,6 @@ "version": "3.0.5", "resolved": "https://registry.npmjs.org/node-localstorage/-/node-localstorage-3.0.5.tgz", "integrity": "sha512-GCwtK33iwVXboZWYcqQHu3aRvXEBwmPkAMRBLeaX86ufhqslyUkLGsi4aW3INEfdQYpUB5M9qtYf3eHvAk2VBg==", - "dev": true, "dependencies": { "write-file-atomic": "^5.0.1" }, @@ -24333,7 +24332,6 @@ "version": "5.0.1", "resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-5.0.1.tgz", "integrity": "sha512-+QU2zd6OTD8XWIJCbffaiQeH9U73qIqafo1x6V1snCWYGJf6cVE0cDR4D8xRzcEnfI21IFrUPzPGtcPf8AC+Rw==", - "dev": true, "dependencies": { "imurmurhash": "^0.1.4", "signal-exit": "^4.0.1" @@ -36563,6 +36561,7 @@ "debug": "^4.3.4", "dns-query": "^0.11.2", "hi-base32": "^0.5.1", + "node-localstorage": "^3.0.5", "uint8arrays": "^5.0.1" }, "devDependencies": { @@ -36579,7 +36578,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" diff --git a/packages/discovery/package.json b/packages/discovery/package.json index d70a540713..b583a6bec5 100644 --- a/packages/discovery/package.json +++ b/packages/discovery/package.json @@ -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", @@ -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" diff --git a/packages/discovery/src/local-peer-cache/index.spec.ts b/packages/discovery/src/local-peer-cache/index.spec.ts index 28cb0677d5..6a3f07624c 100644 --- a/packages/discovery/src/local-peer-cache/index.spec.ts +++ b/packages/discovery/src/local-peer-cache/index.spec.ts @@ -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", diff --git a/packages/discovery/src/local-peer-cache/index.ts b/packages/discovery/src/local-peer-cache/index.ts index 14a79013e7..7b5e8faa1b 100644 --- a/packages/discovery/src/local-peer-cache/index.ts +++ b/packages/discovery/src/local-peer-cache/index.ts @@ -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 implements PeerDiscovery, Startable