|
| 1 | +import type { Multiaddr } from "@multiformats/multiaddr"; |
| 2 | +import { createDecoder, DecodedMessage, waitForRemotePeer } from "@waku/core"; |
| 3 | +import { |
| 4 | + Callback, |
| 5 | + IDecoder, |
| 6 | + IFilterSubscription, |
| 7 | + LightNode, |
| 8 | + Protocols |
| 9 | +} from "@waku/interfaces"; |
| 10 | +import { |
| 11 | + contentTopicToPubsubTopic, |
| 12 | + shardInfoToPubsubTopics |
| 13 | +} from "@waku/utils"; |
| 14 | + |
| 15 | +import { createLightNode } from "./create.js"; |
| 16 | + |
| 17 | +interface CreateTopicOptions { |
| 18 | + waku?: LightNode; |
| 19 | + peer: Multiaddr; |
| 20 | +} |
| 21 | + |
| 22 | +// Given a Waku node, peer Multiaddr, and content topic, creates a decoder and |
| 23 | +// subscription for that content topic. |
| 24 | +async function prepareSubscription( |
| 25 | + waku: LightNode, |
| 26 | + contentTopic: string, |
| 27 | + peer: Multiaddr |
| 28 | +): Promise<{ |
| 29 | + decoder: IDecoder<DecodedMessage>; |
| 30 | + subscription: IFilterSubscription; |
| 31 | +}> { |
| 32 | + // Validate that the Waku node matches assumptions |
| 33 | + if (!waku.filter) { |
| 34 | + throw new Error("Filter protocol missing from Waku node"); |
| 35 | + } |
| 36 | + const { shardInfo } = waku.libp2p.components.metadata; |
| 37 | + if (!shardInfo) { |
| 38 | + throw new Error("Shard info missing from Waku node."); |
| 39 | + } |
| 40 | + |
| 41 | + // Validate content topic and ensure node is configured for its corresponding pubsub topic |
| 42 | + const pubsubTopics = shardInfoToPubsubTopics(shardInfo); |
| 43 | + const pubsubTopic = contentTopicToPubsubTopic(contentTopic); |
| 44 | + if (!pubsubTopics.includes(pubsubTopic)) |
| 45 | + throw new Error( |
| 46 | + "Content topic does not match any pubsub topic in shard info." |
| 47 | + ); |
| 48 | + |
| 49 | + await waku.dial(peer); |
| 50 | + await waitForRemotePeer(waku, [Protocols.Filter]); |
| 51 | + |
| 52 | + // Create decoder and subscription |
| 53 | + let decoder = createDecoder(contentTopic, pubsubTopic); |
| 54 | + if (decoder) decoder = decoder ?? decoder; |
| 55 | + const subscription = await waku.filter.createSubscription(pubsubTopic); |
| 56 | + |
| 57 | + return { decoder, subscription }; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Creates a subscription and streams all new messages for a content topic. |
| 62 | + * Will create a light node configured for the content topic with default settings if a node is not provided in `opts`. |
| 63 | + * Assumes node is using autosharding. |
| 64 | + * @param contentTopic |
| 65 | + * @param opts |
| 66 | + */ |
| 67 | +export async function streamContentTopic( |
| 68 | + contentTopic: string, |
| 69 | + opts: CreateTopicOptions |
| 70 | +): Promise<[ReadableStream<DecodedMessage>, LightNode]> { |
| 71 | + opts.waku = |
| 72 | + opts.waku ?? |
| 73 | + (await createLightNode({ |
| 74 | + shardInfo: { contentTopics: [contentTopic] } |
| 75 | + })); |
| 76 | + const { decoder, subscription } = await prepareSubscription( |
| 77 | + opts.waku, |
| 78 | + contentTopic, |
| 79 | + opts.peer |
| 80 | + ); |
| 81 | + |
| 82 | + // Create a ReadableStream that receives any messages for the content topic |
| 83 | + const messageStream = new ReadableStream<DecodedMessage>({ |
| 84 | + async start(controller) { |
| 85 | + await subscription.subscribe(decoder, (message) => { |
| 86 | + controller.enqueue(message); |
| 87 | + }); |
| 88 | + }, |
| 89 | + cancel() { |
| 90 | + return subscription.unsubscribe([contentTopic]); |
| 91 | + } |
| 92 | + }); |
| 93 | + return [messageStream, opts.waku]; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Subscribes to new messages for a content topic via callback function. |
| 98 | + * Will create a light node configured for the content topic with default settings if a node is not provided in `opts`. |
| 99 | + * Assumes node is using autosharding. |
| 100 | + * @param contentTopic |
| 101 | + * @param callback Called every time a new message is received on the content topic |
| 102 | + * @param opts |
| 103 | + */ |
| 104 | +export async function subscribeToContentTopic( |
| 105 | + contentTopic: string, |
| 106 | + callback: Callback<DecodedMessage>, |
| 107 | + opts: CreateTopicOptions |
| 108 | +): Promise<{ subscription: IFilterSubscription; waku: LightNode }> { |
| 109 | + opts.waku = |
| 110 | + opts.waku ?? |
| 111 | + (await createLightNode({ |
| 112 | + shardInfo: { contentTopics: [contentTopic] } |
| 113 | + })); |
| 114 | + const { decoder, subscription } = await prepareSubscription( |
| 115 | + opts.waku, |
| 116 | + contentTopic, |
| 117 | + opts.peer |
| 118 | + ); |
| 119 | + await subscription.subscribe(decoder, callback); |
| 120 | + return { subscription, waku: opts.waku }; |
| 121 | +} |
0 commit comments