2024-03-11 18:50:34 +05:30
|
|
|
import type { PeerId } from "@libp2p/interface";
|
|
|
|
|
import { LightPushCore } from "@waku/core";
|
|
|
|
|
import {
|
|
|
|
|
Failure,
|
|
|
|
|
type IEncoder,
|
|
|
|
|
ILightPushSDK,
|
|
|
|
|
type IMessage,
|
|
|
|
|
type Libp2p,
|
|
|
|
|
type ProtocolCreateOptions,
|
2024-03-12 16:40:08 +05:30
|
|
|
ProtocolError,
|
2024-05-09 16:51:08 +05:30
|
|
|
SDKProtocolResult
|
2024-03-11 18:50:34 +05:30
|
|
|
} from "@waku/interfaces";
|
|
|
|
|
import { ensurePubsubTopicIsConfigured, Logger } from "@waku/utils";
|
|
|
|
|
|
|
|
|
|
import { BaseProtocolSDK } from "./base_protocol.js";
|
|
|
|
|
|
|
|
|
|
const log = new Logger("sdk:light-push");
|
|
|
|
|
|
2024-04-19 17:20:34 +05:30
|
|
|
class LightPushSDK extends BaseProtocolSDK implements ILightPushSDK {
|
2024-03-11 18:50:34 +05:30
|
|
|
public readonly protocol: LightPushCore;
|
|
|
|
|
|
|
|
|
|
constructor(libp2p: Libp2p, options?: ProtocolCreateOptions) {
|
2024-04-19 17:20:34 +05:30
|
|
|
super({ numPeersToUse: options?.numPeersToUse });
|
2024-03-11 18:50:34 +05:30
|
|
|
this.protocol = new LightPushCore(libp2p, options);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-09 16:51:08 +05:30
|
|
|
async send(encoder: IEncoder, message: IMessage): Promise<SDKProtocolResult> {
|
2024-03-11 18:50:34 +05:30
|
|
|
const successes: PeerId[] = [];
|
|
|
|
|
const failures: Failure[] = [];
|
|
|
|
|
|
|
|
|
|
const { pubsubTopic } = encoder;
|
|
|
|
|
try {
|
|
|
|
|
ensurePubsubTopicIsConfigured(pubsubTopic, this.protocol.pubsubTopics);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
log.error("Failed to send waku light push: pubsub topic not configured");
|
|
|
|
|
return {
|
|
|
|
|
failures: [
|
|
|
|
|
{
|
2024-03-12 16:40:08 +05:30
|
|
|
error: ProtocolError.TOPIC_NOT_CONFIGURED
|
2024-03-11 18:50:34 +05:30
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
successes: []
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const peers = await this.protocol.getPeers();
|
|
|
|
|
if (!peers.length) {
|
|
|
|
|
return {
|
|
|
|
|
successes,
|
2024-03-12 16:40:08 +05:30
|
|
|
failures: [{ error: ProtocolError.NO_PEER_AVAILABLE }]
|
2024-03-11 18:50:34 +05:30
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sendPromises = peers.map((peer) =>
|
|
|
|
|
this.protocol.send(encoder, message, peer)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const results = await Promise.allSettled(sendPromises);
|
|
|
|
|
|
|
|
|
|
for (const result of results) {
|
|
|
|
|
if (result.status === "fulfilled") {
|
|
|
|
|
const { failure, success } = result.value;
|
|
|
|
|
if (success) {
|
|
|
|
|
successes.push(success);
|
|
|
|
|
}
|
|
|
|
|
if (failure) {
|
|
|
|
|
failures.push(failure);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log.error("Failed to send message to peer", result.reason);
|
2024-03-12 16:40:08 +05:30
|
|
|
failures.push({ error: ProtocolError.GENERIC_FAIL });
|
2024-03-11 18:50:34 +05:30
|
|
|
// TODO: handle renewing faulty peers with new peers (https://github.com/waku-org/js-waku/issues/1463)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
successes,
|
|
|
|
|
failures
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function wakuLightPush(
|
|
|
|
|
init: Partial<ProtocolCreateOptions> = {}
|
|
|
|
|
): (libp2p: Libp2p) => ILightPushSDK {
|
|
|
|
|
return (libp2p: Libp2p) => new LightPushSDK(libp2p, init);
|
|
|
|
|
}
|