mirror of https://github.com/status-im/js-waku.git
Merge pull request #1016 from waku-org/danisharora/add-user-agent
feat!: add support for adding/setting user agent
This commit is contained in:
commit
9860ef496f
|
@ -1,4 +1,5 @@
|
|||
export { DefaultPubSubTopic } from "./lib/constants";
|
||||
export { DefaultUserAgent } from "./lib/waku";
|
||||
|
||||
export * as proto_message from "./proto/message";
|
||||
export * as proto_topic_only_message from "./proto/topic_only_message";
|
||||
|
|
|
@ -16,6 +16,7 @@ import { StoreCodec, StoreComponents } from "./waku_store";
|
|||
|
||||
export const DefaultPingKeepAliveValueSecs = 0;
|
||||
export const DefaultRelayKeepAliveValueSecs = 5 * 60;
|
||||
export const DefaultUserAgent = "js-waku";
|
||||
|
||||
const log = debug("waku:waku");
|
||||
|
||||
|
@ -34,6 +35,11 @@ export interface WakuOptions {
|
|||
* @default {@link DefaultRelayKeepAliveValueSecs}
|
||||
*/
|
||||
relayKeepAlive?: number;
|
||||
/**
|
||||
* Set the user agent string to be used in identification of the node.
|
||||
* @default {@link DefaultUserAgent}
|
||||
*/
|
||||
userAgent?: string;
|
||||
}
|
||||
|
||||
export class WakuNode implements Waku {
|
||||
|
|
|
@ -13,6 +13,7 @@ import {
|
|||
wakuRelay,
|
||||
wakuStore,
|
||||
} from "@waku/core";
|
||||
import { DefaultUserAgent } from "@waku/core";
|
||||
import { getPredefinedBootstrapNodes } from "@waku/core/lib/predefined_bootstrap_nodes";
|
||||
import type { Relay, WakuFull, WakuLight, WakuPrivacy } from "@waku/interfaces";
|
||||
import type { Libp2p } from "libp2p";
|
||||
|
@ -72,7 +73,11 @@ export async function createLightNode(
|
|||
Object.assign(libp2pOptions, { peerDiscovery });
|
||||
}
|
||||
|
||||
const libp2p = await defaultLibp2p(undefined, libp2pOptions);
|
||||
const libp2p = await defaultLibp2p(
|
||||
undefined,
|
||||
libp2pOptions,
|
||||
options?.userAgent
|
||||
);
|
||||
|
||||
const store = wakuStore(options);
|
||||
const lightPush = wakuLightPush(options);
|
||||
|
@ -101,7 +106,11 @@ export async function createPrivacyNode(
|
|||
Object.assign(libp2pOptions, { peerDiscovery });
|
||||
}
|
||||
|
||||
const libp2p = await defaultLibp2p(wakuRelay(options), libp2pOptions);
|
||||
const libp2p = await defaultLibp2p(
|
||||
wakuRelay(options),
|
||||
libp2pOptions,
|
||||
options?.userAgent
|
||||
);
|
||||
|
||||
return new WakuNode(options ?? {}, libp2p) as WakuPrivacy;
|
||||
}
|
||||
|
@ -129,7 +138,11 @@ export async function createFullNode(
|
|||
Object.assign(libp2pOptions, { peerDiscovery });
|
||||
}
|
||||
|
||||
const libp2p = await defaultLibp2p(wakuRelay(options), libp2pOptions);
|
||||
const libp2p = await defaultLibp2p(
|
||||
wakuRelay(options),
|
||||
libp2pOptions,
|
||||
options?.userAgent
|
||||
);
|
||||
|
||||
const store = wakuStore(options);
|
||||
const lightPush = wakuLightPush(options);
|
||||
|
@ -152,14 +165,20 @@ export function defaultPeerDiscovery(): (
|
|||
|
||||
export async function defaultLibp2p(
|
||||
wakuRelay?: (components: Components) => Relay,
|
||||
options?: Partial<Libp2pOptions>
|
||||
options?: Partial<Libp2pOptions>,
|
||||
userAgent?: string
|
||||
): Promise<Libp2p> {
|
||||
const libp2pOpts = Object.assign(
|
||||
{
|
||||
transports: [webSockets({ filter: filterAll })],
|
||||
streamMuxers: [mplex()],
|
||||
connectionEncryption: [noise()],
|
||||
},
|
||||
identify: {
|
||||
host: {
|
||||
agentVersion: userAgent ?? DefaultUserAgent,
|
||||
},
|
||||
},
|
||||
} as Libp2pOptions,
|
||||
wakuRelay ? { pubsub: wakuRelay } : {},
|
||||
options ?? {}
|
||||
);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { bootstrap } from "@libp2p/bootstrap";
|
||||
import type { PeerId } from "@libp2p/interface-peer-id";
|
||||
import { bytesToUtf8, utf8ToBytes } from "@waku/byte-utils";
|
||||
import { DefaultUserAgent } from "@waku/core";
|
||||
import { waitForRemotePeer } from "@waku/core/lib/wait_for_remote_peer";
|
||||
import { createLightNode, createPrivacyNode } from "@waku/create";
|
||||
import type {
|
||||
|
@ -186,3 +187,49 @@ describe("Decryption Keys", () => {
|
|||
expect(receivedMsg.timestamp?.valueOf()).to.eq(messageTimestamp.valueOf());
|
||||
});
|
||||
});
|
||||
|
||||
describe("User Agent", () => {
|
||||
let waku1: Waku;
|
||||
let waku2: Waku;
|
||||
|
||||
afterEach(async function () {
|
||||
!!waku1 && waku1.stop().catch((e) => console.log("Waku failed to stop", e));
|
||||
!!waku2 && waku2.stop().catch((e) => console.log("Waku failed to stop", e));
|
||||
});
|
||||
|
||||
it("Sets default value correctly", async function () {
|
||||
this.timeout(20_000);
|
||||
|
||||
const waku1UserAgent = "test-user-agent";
|
||||
|
||||
[waku1, waku2] = await Promise.all([
|
||||
createPrivacyNode({
|
||||
staticNoiseKey: NOISE_KEY_1,
|
||||
userAgent: waku1UserAgent,
|
||||
}).then((waku) => waku.start().then(() => waku)),
|
||||
createPrivacyNode({
|
||||
staticNoiseKey: NOISE_KEY_2,
|
||||
libp2p: { addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] } },
|
||||
}).then((waku) => waku.start().then(() => waku)),
|
||||
]);
|
||||
|
||||
await waku1.libp2p.peerStore.addressBook.set(
|
||||
waku2.libp2p.peerId,
|
||||
waku2.libp2p.getMultiaddrs()
|
||||
);
|
||||
await waku1.dial(waku2.libp2p.peerId);
|
||||
await waitForRemotePeer(waku1);
|
||||
|
||||
const [waku1PeerInfo, waku2PeerInfo] = await Promise.all([
|
||||
waku2.libp2p.peerStore.metadataBook.get(waku1.libp2p.peerId),
|
||||
waku1.libp2p.peerStore.metadataBook.get(waku2.libp2p.peerId),
|
||||
]);
|
||||
|
||||
expect(bytesToUtf8(waku1PeerInfo.get("AgentVersion")!)).to.eq(
|
||||
waku1UserAgent
|
||||
);
|
||||
expect(bytesToUtf8(waku2PeerInfo.get("AgentVersion")!)).to.eq(
|
||||
DefaultUserAgent
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue