134 lines
3.3 KiB
TypeScript
Raw Normal View History

import type { PeerId } from "@libp2p/interface";
import { LightPushCore } from "@waku/core";
import {
type CoreProtocolResult,
Failure,
type IEncoder,
ILightPush,
type IMessage,
type ISendOptions,
type Libp2p,
type LightPushProtocolOptions,
ProtocolError,
Protocols,
SDKProtocolResult
} from "@waku/interfaces";
import { Logger } from "@waku/utils";
import { PeerManager } from "../peer_manager/index.js";
import { RetryManager } from "./retry_manager.js";
const log = new Logger("sdk:light-push");
const DEFAULT_MAX_ATTEMPTS = 3;
const DEFAULT_SEND_OPTIONS: LightPushProtocolOptions = {
autoRetry: true,
retryIntervalMs: 1000,
maxAttempts: DEFAULT_MAX_ATTEMPTS,
numPeersToUse: 1
};
type LightPushConstructorParams = {
peerManager: PeerManager;
libp2p: Libp2p;
options?: Partial<LightPushProtocolOptions>;
};
export class LightPush implements ILightPush {
private readonly config: LightPushProtocolOptions;
private readonly retryManager: RetryManager;
private readonly peerManager: PeerManager;
private readonly protocol: LightPushCore;
public constructor(params: LightPushConstructorParams) {
this.config = {
...DEFAULT_SEND_OPTIONS,
...(params.options || {})
} as LightPushProtocolOptions;
this.peerManager = params.peerManager;
feat!: re-architect connection manager (#2445) * remove public pubsub field and redundant util * add hangUp and improve dial operations, improve keepAliveManager and remove unused method, move utils and add tests * improve public dial method to start keep alive checks * move dial method * implement discovery dialer * implement discovery dialer with queue with tests * add discovery dialer e2e tests, change local discovery log tag, update other tests * remove comment * add issue link, remove only * implement shard reader component * create evetns module, remove unused connection manager events and related tests * implement network indicator * implement connection limiter, change public API of connection manager, implement recovery strategy * decouple keep alive maanger * add connection manager js-doc * refactor keep alive manager, cover with tests * add tests for connection manager main facade * add tests for connection limiter * add e2e tests for connection manager modules pass js-waku config during test node init remove dns discovery for js-waku * restructure dialing tests * address last e2e tests * address review * add logging for main methods * decouple pure dialer class, update network monitor with specific metrics * remove console.log * remove usage of protocols * update sdk package tests * add connect await promise * add debug for e2e tests * enable only packages tests * use only one file * revert debugging * up interface for netwrok manager * add logs * add more logs * add more logs * add another logs * remove .only * remove log statements * skip the test with follow up
2025-07-09 21:23:14 +02:00
this.protocol = new LightPushCore(params.libp2p);
this.retryManager = new RetryManager({
peerManager: params.peerManager,
retryIntervalMs: this.config.retryIntervalMs
});
}
public get multicodec(): string {
return this.protocol.multicodec;
}
public start(): void {
this.retryManager.start();
}
public stop(): void {
this.retryManager.stop();
}
public async send(
encoder: IEncoder,
message: IMessage,
options: ISendOptions = {}
): Promise<SDKProtocolResult> {
options = {
...this.config,
...options
};
const { pubsubTopic } = encoder;
log.info("send: attempting to send a message to pubsubTopic:", pubsubTopic);
const peerIds = await this.peerManager.getPeers({
protocol: Protocols.LightPush,
pubsubTopic: encoder.pubsubTopic
});
const coreResults: CoreProtocolResult[] =
peerIds?.length > 0
? await Promise.all(
peerIds.map((peerId) =>
this.protocol.send(encoder, message, peerId).catch((_e) => ({
success: null,
failure: {
error: ProtocolError.GENERIC_FAIL
}
}))
)
)
: [];
const results: SDKProtocolResult = coreResults.length
? {
successes: coreResults
.filter((v) => v.success)
.map((v) => v.success) as PeerId[],
failures: coreResults
.filter((v) => v.failure)
.map((v) => v.failure) as Failure[]
}
: {
successes: [],
failures: [
{
error: ProtocolError.NO_PEER_AVAILABLE
}
]
};
if (options.autoRetry && results.successes.length === 0) {
const sendCallback = (peerId: PeerId): Promise<CoreProtocolResult> =>
this.protocol.send(encoder, message, peerId);
this.retryManager.push(
sendCallback.bind(this),
options.maxAttempts || DEFAULT_MAX_ATTEMPTS,
encoder.pubsubTopic
);
}
return results;
}
}