From 906c93329e4094c79e3f7f5c56e1b78afd778e1a Mon Sep 17 00:00:00 2001 From: Danish Arora <35004822+danisharora099@users.noreply.github.com> Date: Thu, 11 Jan 2024 19:08:21 +0530 Subject: [PATCH] chore!: discourage the use of relay in browsers (#1778) * add a comment discouraging relay * move createRelayNode under an explicit export * fix: build --- package-lock.json | 2 +- packages/sdk/package.json | 13 +++++ packages/sdk/src/create.ts | 42 ---------------- packages/sdk/src/relay/index.ts | 48 +++++++++++++++++++ packages/tests/tests/enr.node.spec.ts | 2 +- packages/tests/tests/relay/index.node.spec.ts | 2 +- .../tests/tests/relay/interop.node.spec.ts | 2 +- .../tests/relay/multiple_pubsub.node.spec.ts | 2 +- .../tests/tests/relay/publish.node.spec.ts | 2 +- .../tests/tests/relay/subscribe.node.spec.ts | 2 +- .../tests/wait_for_remote_peer.node.spec.ts | 3 +- packages/tests/tests/waku.node.spec.ts | 4 +- 12 files changed, 72 insertions(+), 52 deletions(-) create mode 100644 packages/sdk/src/relay/index.ts diff --git a/package-lock.json b/package-lock.json index 681770ea4b..cce31779af 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26642,6 +26642,7 @@ "license": "MIT OR Apache-2.0", "dependencies": { "@noble/hashes": "^1.3.2", + "@waku/interfaces": "0.0.20", "chai": "^4.3.10", "debug": "^4.3.4", "uint8arrays": "^4.0.4" @@ -26651,7 +26652,6 @@ "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.2.3", "@waku/build-utils": "*", - "@waku/interfaces": "0.0.20", "cspell": "^7.3.2", "npm-run-all": "^4.1.5", "rollup": "^4.6.0" diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 2a100202b5..74c9e2bd2b 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -8,6 +8,19 @@ ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" + }, + "./relay": { + "types": "./dist/relay/index.d.ts", + "import": "./dist/relay/index.js" + } + }, + "typesVersions": { + "*": { + "*": [ + "*", + "dist/*", + "dist/*/index" + ] } }, "type": "module", diff --git a/packages/sdk/src/create.ts b/packages/sdk/src/create.ts index 63e33ee482..4265fb6f4b 100644 --- a/packages/sdk/src/create.ts +++ b/packages/sdk/src/create.ts @@ -22,7 +22,6 @@ import type { Libp2pComponents, LightNode, ProtocolCreateOptions, - RelayNode, ShardingParams } from "@waku/interfaces"; import { wakuPeerExchangeDiscovery } from "@waku/peer-exchange"; @@ -136,47 +135,6 @@ export async function createLightNode( ) as LightNode; } -/** - * Create a Waku node that uses Waku Relay to send and receive messages, - * enabling some privacy preserving properties. - */ -export async function createRelayNode( - options?: ProtocolCreateOptions & WakuOptions & Partial -): Promise { - options = options ?? {}; - - if (options.shardInfo) { - ensureShardingConfigured(options.shardInfo); - } - - const libp2pOptions = options?.libp2p ?? {}; - const peerDiscovery = libp2pOptions.peerDiscovery ?? []; - if (options?.defaultBootstrap) { - peerDiscovery.push(...defaultPeerDiscoveries()); - Object.assign(libp2pOptions, { peerDiscovery }); - } - - const libp2p = await defaultLibp2p( - options.shardInfo, - wakuGossipSub(options), - libp2pOptions, - options?.userAgent - ); - - const relay = wakuRelay(options); - - return new WakuNode( - options, - options.pubsubTopics, - libp2p, - options.shardInfo, - undefined, - undefined, - undefined, - relay - ) as RelayNode; -} - /** * Create a Waku node that uses all Waku protocols. * diff --git a/packages/sdk/src/relay/index.ts b/packages/sdk/src/relay/index.ts new file mode 100644 index 0000000000..d02b989d93 --- /dev/null +++ b/packages/sdk/src/relay/index.ts @@ -0,0 +1,48 @@ +import { WakuNode, WakuOptions } from "@waku/core"; +import type { ProtocolCreateOptions, RelayNode } from "@waku/interfaces"; +import { RelayCreateOptions, wakuGossipSub, wakuRelay } from "@waku/relay"; + +import { defaultLibp2p, defaultPeerDiscoveries } from "../create.js"; + +/** + * Create a Waku node that uses Waku Relay to send and receive messages, + * enabling some privacy preserving properties. + * * @remarks + * This function creates a Relay Node using the Waku Relay protocol. + * While it is technically possible to use this function in a browser environment, + * it is not recommended due to potential performance issues and limited browser capabilities. + * If you are developing a browser-based application, consider alternative approaches like creating a Light Node + * or use this function with caution. + */ +export async function createRelayNode( + options?: ProtocolCreateOptions & WakuOptions & Partial +): Promise { + options = options ?? {}; + + const libp2pOptions = options?.libp2p ?? {}; + const peerDiscovery = libp2pOptions.peerDiscovery ?? []; + if (options?.defaultBootstrap) { + peerDiscovery.push(...defaultPeerDiscoveries()); + Object.assign(libp2pOptions, { peerDiscovery }); + } + + const libp2p = await defaultLibp2p( + options.shardInfo, + wakuGossipSub(options), + libp2pOptions, + options?.userAgent + ); + + const relay = wakuRelay(options); + + return new WakuNode( + options, + options.pubsubTopics, + libp2p, + options.shardInfo, + undefined, + undefined, + undefined, + relay + ) as RelayNode; +} diff --git a/packages/tests/tests/enr.node.spec.ts b/packages/tests/tests/enr.node.spec.ts index f6b7872b65..5959c31f9f 100644 --- a/packages/tests/tests/enr.node.spec.ts +++ b/packages/tests/tests/enr.node.spec.ts @@ -2,7 +2,7 @@ import { waitForRemotePeer } from "@waku/core"; import { EnrDecoder } from "@waku/enr"; import type { RelayNode } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; -import { createRelayNode } from "@waku/sdk"; +import { createRelayNode } from "@waku/sdk/relay"; import { expect } from "chai"; import { makeLogFileName, NOISE_KEY_1, tearDownNodes } from "../src/index.js"; diff --git a/packages/tests/tests/relay/index.node.spec.ts b/packages/tests/tests/relay/index.node.spec.ts index d6bb19fcff..c62e0b2a3f 100644 --- a/packages/tests/tests/relay/index.node.spec.ts +++ b/packages/tests/tests/relay/index.node.spec.ts @@ -13,7 +13,7 @@ import { createDecoder as createSymDecoder, createEncoder as createSymEncoder } from "@waku/message-encryption/symmetric"; -import { createRelayNode } from "@waku/sdk"; +import { createRelayNode } from "@waku/sdk/relay"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; diff --git a/packages/tests/tests/relay/interop.node.spec.ts b/packages/tests/tests/relay/interop.node.spec.ts index 47cf2eeeba..1304bc9743 100644 --- a/packages/tests/tests/relay/interop.node.spec.ts +++ b/packages/tests/tests/relay/interop.node.spec.ts @@ -1,7 +1,7 @@ import type { PeerId } from "@libp2p/interface/peer-id"; import { DecodedMessage, waitForRemotePeer } from "@waku/core"; import { DefaultPubsubTopic, Protocols, RelayNode } from "@waku/interfaces"; -import { createRelayNode } from "@waku/sdk"; +import { createRelayNode } from "@waku/sdk/relay"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; diff --git a/packages/tests/tests/relay/multiple_pubsub.node.spec.ts b/packages/tests/tests/relay/multiple_pubsub.node.spec.ts index f070eef140..f9d7fcc582 100644 --- a/packages/tests/tests/relay/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/relay/multiple_pubsub.node.spec.ts @@ -11,7 +11,7 @@ import { SingleShardInfo } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; -import { createRelayNode } from "@waku/sdk"; +import { createRelayNode } from "@waku/sdk/relay"; import { contentTopicToPubsubTopic, singleShardInfoToPubsubTopic diff --git a/packages/tests/tests/relay/publish.node.spec.ts b/packages/tests/tests/relay/publish.node.spec.ts index f550bb4a9e..1f4f8fe1ae 100644 --- a/packages/tests/tests/relay/publish.node.spec.ts +++ b/packages/tests/tests/relay/publish.node.spec.ts @@ -1,6 +1,6 @@ import { createEncoder } from "@waku/core"; import { IRateLimitProof, RelayNode, SendError } from "@waku/interfaces"; -import { createRelayNode } from "@waku/sdk"; +import { createRelayNode } from "@waku/sdk/relay"; import { utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; diff --git a/packages/tests/tests/relay/subscribe.node.spec.ts b/packages/tests/tests/relay/subscribe.node.spec.ts index bb67659cf3..324820bf89 100644 --- a/packages/tests/tests/relay/subscribe.node.spec.ts +++ b/packages/tests/tests/relay/subscribe.node.spec.ts @@ -1,6 +1,6 @@ import { createDecoder, createEncoder } from "@waku/core"; import { DefaultPubsubTopic, RelayNode } from "@waku/interfaces"; -import { createRelayNode } from "@waku/sdk"; +import { createRelayNode } from "@waku/sdk/relay"; import { utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; diff --git a/packages/tests/tests/wait_for_remote_peer.node.spec.ts b/packages/tests/tests/wait_for_remote_peer.node.spec.ts index 586d2574c0..3ca2263d94 100644 --- a/packages/tests/tests/wait_for_remote_peer.node.spec.ts +++ b/packages/tests/tests/wait_for_remote_peer.node.spec.ts @@ -1,7 +1,8 @@ import { waitForRemotePeer } from "@waku/core"; import type { LightNode, RelayNode } from "@waku/interfaces"; import { DefaultPubsubTopic, Protocols } from "@waku/interfaces"; -import { createLightNode, createRelayNode } from "@waku/sdk"; +import { createLightNode } from "@waku/sdk"; +import { createRelayNode } from "@waku/sdk/relay"; import { expect } from "chai"; import { diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index e7aa69f176..bfd812517d 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -14,9 +14,9 @@ import { } from "@waku/message-encryption/symmetric"; import { createLightNode, - createEncoder as createPlainEncoder, - createRelayNode + createEncoder as createPlainEncoder } from "@waku/sdk"; +import { createRelayNode } from "@waku/sdk/relay"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai";