fix: align libp2p types

This commit is contained in:
Sasha 2025-09-04 08:12:59 +02:00
parent a7f30b1211
commit 1361cf57e1
3 changed files with 22 additions and 22 deletions

View File

@ -1,5 +1,4 @@
import type { PeerId } from "@libp2p/interface"; import type { PeerId, StreamHandler } from "@libp2p/interface";
import type { IncomingStreamData } from "@libp2p/interface-internal";
import { import {
type ContentTopic, type ContentTopic,
type CoreProtocolResult, type CoreProtocolResult,
@ -52,7 +51,7 @@ export class FilterCore {
public async start(): Promise<void> { public async start(): Promise<void> {
try { try {
await this.libp2p.handle(FilterCodecs.PUSH, this.onRequest.bind(this), { await this.libp2p.handle(FilterCodecs.PUSH, this.onRequest, {
maxInboundStreams: 100 maxInboundStreams: 100
}); });
} catch (e) { } catch (e) {
@ -304,7 +303,7 @@ export class FilterCore {
}; };
} }
private onRequest(streamData: IncomingStreamData): void { private onRequest: StreamHandler = (streamData) => {
const { connection, stream } = streamData; const { connection, stream } = streamData;
const { remotePeer } = connection; const { remotePeer } = connection;
log.info(`Received message from ${remotePeer.toString()}`); log.info(`Received message from ${remotePeer.toString()}`);
@ -345,5 +344,5 @@ export class FilterCore {
} catch (e) { } catch (e) {
log.error("Error decoding message", e); log.error("Error decoding message", e);
} }
} };
} }

View File

@ -34,7 +34,7 @@ describe("StreamManager", () => {
createMockStream({ id: "1", protocol: MULTICODEC, writeStatus }) createMockStream({ id: "1", protocol: MULTICODEC, writeStatus })
]; ];
streamManager["libp2p"]["connectionManager"]["getConnections"] = ( (streamManager["libp2p"]["connectionManager"] as any).getConnections = (
_peerId: PeerId | undefined _peerId: PeerId | undefined
) => [con1]; ) => [con1];
@ -46,7 +46,7 @@ describe("StreamManager", () => {
}); });
it("should return undefined if no connection provided", async () => { it("should return undefined if no connection provided", async () => {
streamManager["libp2p"]["connectionManager"]["getConnections"] = ( (streamManager["libp2p"]["connectionManager"] as any).getConnections = (
_peerId: PeerId | undefined _peerId: PeerId | undefined
) => []; ) => [];
@ -70,7 +70,7 @@ describe("StreamManager", () => {
); );
con1.newStream = newStreamSpy; con1.newStream = newStreamSpy;
streamManager["libp2p"]["connectionManager"]["getConnections"] = ( (streamManager["libp2p"]["connectionManager"] as any).getConnections = (
_peerId: PeerId | undefined _peerId: PeerId | undefined
) => [con1]; ) => [con1];
@ -97,7 +97,7 @@ describe("StreamManager", () => {
); );
con1.newStream = newStreamSpy; con1.newStream = newStreamSpy;
streamManager["libp2p"]["connectionManager"]["getConnections"] = ( (streamManager["libp2p"]["connectionManager"] as any).getConnections = (
_peerId: PeerId | undefined _peerId: PeerId | undefined
) => [con1]; ) => [con1];
@ -148,7 +148,7 @@ describe("StreamManager", () => {
writeStatus: "writable" writeStatus: "writable"
}) })
]; ];
streamManager["libp2p"]["connectionManager"]["getConnections"] = ( (streamManager["libp2p"]["connectionManager"] as any).getConnections = (
_id: PeerId | undefined _id: PeerId | undefined
) => [con1]; ) => [con1];
@ -178,7 +178,6 @@ function createMockConnection(options: MockConnectionOptions = {}): Connection {
} }
} as Connection; } as Connection;
} }
type MockStreamOptions = { type MockStreamOptions = {
id?: string; id?: string;
protocol?: string; protocol?: string;

View File

@ -25,10 +25,12 @@ export type CreateEncoderParams = CreateDecoderParams & {
ephemeral?: boolean; ephemeral?: boolean;
}; };
export enum WakuEvent { export const WakuEvent = {
Connection = "waku:connection", Connection: "waku:connection",
Health = "waku:health" Health: "waku:health"
} } as const;
export type WakuEvent = (typeof WakuEvent)[keyof typeof WakuEvent];
export interface IWakuEvents { export interface IWakuEvents {
/** /**
@ -36,22 +38,22 @@ export interface IWakuEvents {
* *
* @example * @example
* ```typescript * ```typescript
* waku.addEventListener(WakuEvent.Connection, (event) => { * waku.events.addEventListener("waku:connection", (event) => {
* console.log(event.detail); // true if connected, false if disconnected * console.log(event.detail); // true if connected, false if disconnected
* }); * });
*/ */
[WakuEvent.Connection]: CustomEvent<boolean>; "waku:connection": CustomEvent<boolean>;
/** /**
* Emitted when the health status changes. * Emitted when the health status changes.
* *
* @example * @example
* ```typescript * ```typescript
* waku.addEventListener(WakuEvent.Health, (event) => { * waku.events.addEventListener("waku:health", (event) => {
* console.log(event.detail); // 'Unhealthy', 'MinimallyHealthy', or 'SufficientlyHealthy' * console.log(event.detail); // 'Unhealthy', 'MinimallyHealthy', or 'SufficientlyHealthy'
* }); * });
*/ */
[WakuEvent.Health]: CustomEvent<HealthStatus>; "waku:health": CustomEvent<HealthStatus>;
} }
export type IWakuEventEmitter = TypedEventEmitter<IWakuEvents>; export type IWakuEventEmitter = TypedEventEmitter<IWakuEvents>;
@ -66,12 +68,12 @@ export interface IWaku {
/** /**
* Emits events related to the Waku node. * Emits events related to the Waku node.
* Those are: * Those are:
* - WakuEvent.Connection * - "waku:connection"
* - WakuEvent.Health * - "waku:health"
* *
* @example * @example
* ```typescript * ```typescript
* waku.events.addEventListener(WakuEvent.Connection, (event) => { * waku.events.addEventListener("waku:connection", (event) => {
* console.log(event.detail); // true if connected, false if disconnected * console.log(event.detail); // true if connected, false if disconnected
* }); * });
* ``` * ```