From 0f6a59464426b94dd14841de075ff10a4ad52e33 Mon Sep 17 00:00:00 2001 From: Sasha <118575614+weboko@users.noreply.github.com> Date: Tue, 21 Mar 2023 02:07:59 +0100 Subject: [PATCH] feat!: use ISender and deprecate Light Push .push (#1217) --- packages/core/src/lib/light_push/index.ts | 2 +- packages/interfaces/src/index.ts | 1 + packages/interfaces/src/light_push.ts | 16 +++------------- packages/interfaces/src/relay.ts | 15 +++++---------- packages/interfaces/src/sender.ts | 10 ++++++++++ packages/tests/tests/ephemeral.node.spec.ts | 18 +++++++++--------- packages/tests/tests/filter.node.spec.ts | 10 +++++----- packages/tests/tests/light_push.node.spec.ts | 4 ++-- packages/tests/tests/store.node.spec.ts | 8 ++++---- 9 files changed, 40 insertions(+), 44 deletions(-) create mode 100644 packages/interfaces/src/sender.ts diff --git a/packages/core/src/lib/light_push/index.ts b/packages/core/src/lib/light_push/index.ts index b31784900a..52c6fb18fe 100644 --- a/packages/core/src/lib/light_push/index.ts +++ b/packages/core/src/lib/light_push/index.ts @@ -36,7 +36,7 @@ class LightPush extends BaseProtocol implements ILightPush { this.options = options || {}; } - async push( + async send( encoder: IEncoder, message: IMessage, opts?: ProtocolOptions diff --git a/packages/interfaces/src/index.ts b/packages/interfaces/src/index.ts index 37b427b100..2d8baf54b3 100644 --- a/packages/interfaces/src/index.ts +++ b/packages/interfaces/src/index.ts @@ -8,3 +8,4 @@ export * from "./relay.js"; export * from "./store.js"; export * from "./waku.js"; export * from "./connection_manager.js"; +export * from "./sender.js"; diff --git a/packages/interfaces/src/light_push.ts b/packages/interfaces/src/light_push.ts index ad217b60de..1244edee5a 100644 --- a/packages/interfaces/src/light_push.ts +++ b/packages/interfaces/src/light_push.ts @@ -1,14 +1,4 @@ -import type { IEncoder, IMessage } from "./message.js"; -import type { - PointToPointProtocol, - ProtocolOptions, - SendResult, -} from "./protocols.js"; +import type { PointToPointProtocol } from "./protocols.js"; +import type { ISender } from "./sender.js"; -export interface ILightPush extends PointToPointProtocol { - push: ( - encoder: IEncoder, - message: IMessage, - opts?: ProtocolOptions - ) => Promise; -} +export type ILightPush = ISender & PointToPointProtocol; diff --git a/packages/interfaces/src/relay.ts b/packages/interfaces/src/relay.ts index d90dfb3a1f..70b8225deb 100644 --- a/packages/interfaces/src/relay.ts +++ b/packages/interfaces/src/relay.ts @@ -1,13 +1,9 @@ import type { GossipSub, GossipsubEvents } from "@chainsafe/libp2p-gossipsub"; import type { EventEmitter } from "@libp2p/interfaces/events"; -import type { - IDecodedMessage, - IDecoder, - IEncoder, - IMessage, -} from "./message.js"; -import type { Callback, SendResult } from "./protocols.js"; +import type { IDecodedMessage, IDecoder } from "./message.js"; +import type { Callback } from "./protocols.js"; +import type { ISender } from "./sender.js"; export interface RelayEvents { "observer:added": CustomEvent; @@ -16,8 +12,7 @@ export interface RelayEvents { type IRelayEmitter = EventEmitter; -interface IRelayAPI extends GossipSub { - send: (encoder: IEncoder, message: IMessage) => Promise; +interface IRelayAPI { addObserver: ( decoder: IDecoder, callback: Callback @@ -25,4 +20,4 @@ interface IRelayAPI extends GossipSub { getMeshPeers: () => string[]; } -export type IRelay = IRelayAPI & IRelayEmitter; +export type IRelay = ISender & GossipSub & IRelayAPI & IRelayEmitter; diff --git a/packages/interfaces/src/sender.ts b/packages/interfaces/src/sender.ts new file mode 100644 index 0000000000..efdc2f4531 --- /dev/null +++ b/packages/interfaces/src/sender.ts @@ -0,0 +1,10 @@ +import type { IEncoder, IMessage } from "./message.js"; +import type { ProtocolOptions, SendResult } from "./protocols.js"; + +export interface ISender { + send: ( + encoder: IEncoder, + message: IMessage, + opts?: ProtocolOptions + ) => Promise; +} diff --git a/packages/tests/tests/ephemeral.node.spec.ts b/packages/tests/tests/ephemeral.node.spec.ts index 2821458cf8..8e274e6e78 100644 --- a/packages/tests/tests/ephemeral.node.spec.ts +++ b/packages/tests/tests/ephemeral.node.spec.ts @@ -135,9 +135,9 @@ describe("Waku Message Ephemeral field", () => { log("Sending messages using light push"); await Promise.all([ - waku1.lightPush.push(asymEncoder, asymMsg), - waku1.lightPush.push(symEncoder, symMsg), - waku1.lightPush.push(clearEncoder, clearMsg), + waku1.lightPush.send(asymEncoder, asymMsg), + waku1.lightPush.send(symEncoder, symMsg), + waku1.lightPush.send(clearEncoder, clearMsg), ]); await waitForRemotePeer(waku2, [Protocols.Store]); @@ -181,10 +181,10 @@ describe("Waku Message Ephemeral field", () => { await delay(200); const normalTxt = "Normal message"; const ephemeralTxt = "Ephemeral Message"; - await waku.lightPush.push(TestEncoder, { + await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes(normalTxt), }); - await waku.lightPush.push(ephemeralEncoder, { + await waku.lightPush.send(ephemeralEncoder, { payload: utf8ToBytes(ephemeralTxt), }); while (messages.length < 2) { @@ -230,10 +230,10 @@ describe("Waku Message Ephemeral field", () => { await delay(200); const normalTxt = "Normal message"; const ephemeralTxt = "Ephemeral Message"; - await waku.lightPush.push(encoder, { + await waku.lightPush.send(encoder, { payload: utf8ToBytes(normalTxt), }); - await waku.lightPush.push(ephemeralEncoder, { + await waku.lightPush.send(ephemeralEncoder, { payload: utf8ToBytes(ephemeralTxt), }); while (messages.length < 2) { @@ -280,10 +280,10 @@ describe("Waku Message Ephemeral field", () => { await delay(200); const normalTxt = "Normal message"; const ephemeralTxt = "Ephemeral Message"; - await waku.lightPush.push(encoder, { + await waku.lightPush.send(encoder, { payload: utf8ToBytes(normalTxt), }); - await waku.lightPush.push(ephemeralEncoder, { + await waku.lightPush.send(ephemeralEncoder, { payload: utf8ToBytes(ephemeralTxt), }); while (messages.length < 2) { diff --git a/packages/tests/tests/filter.node.spec.ts b/packages/tests/tests/filter.node.spec.ts index b5471056c3..6453e6b11e 100644 --- a/packages/tests/tests/filter.node.spec.ts +++ b/packages/tests/tests/filter.node.spec.ts @@ -63,7 +63,7 @@ describe("Waku Filter", () => { // correct in future versions of the protocol. await delay(200); - await waku.lightPush.push(TestEncoder, message); + await waku.lightPush.send(TestEncoder, message); while (messageCount === 0) { await delay(250); } @@ -81,10 +81,10 @@ describe("Waku Filter", () => { await waku.filter.subscribe([TestDecoder], callback); await delay(200); - await waku.lightPush.push(TestEncoder, { + await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes("Filtering works!"), }); - await waku.lightPush.push(TestEncoder, { + await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes("Filtering still works!"), }); while (messageCount < 2) { @@ -101,13 +101,13 @@ describe("Waku Filter", () => { const unsubscribe = await waku.filter.subscribe([TestDecoder], callback); await delay(200); - await waku.lightPush.push(TestEncoder, { + await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes("This should be received"), }); await delay(100); await unsubscribe(); await delay(200); - await waku.lightPush.push(TestEncoder, { + await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes("This should not be received"), }); await delay(100); diff --git a/packages/tests/tests/light_push.node.spec.ts b/packages/tests/tests/light_push.node.spec.ts index 789255e89a..84b91d7385 100644 --- a/packages/tests/tests/light_push.node.spec.ts +++ b/packages/tests/tests/light_push.node.spec.ts @@ -46,7 +46,7 @@ describe("Waku Light Push [node only]", () => { const messageText = "Light Push works!"; - const pushResponse = await waku.lightPush.push(TestEncoder, { + const pushResponse = await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes(messageText), }); expect(pushResponse.recipients.length).to.eq(1); @@ -87,7 +87,7 @@ describe("Waku Light Push [node only]", () => { const messageText = "Light Push works!"; log("Send message via lightpush"); - const pushResponse = await waku.lightPush.push( + const pushResponse = await waku.lightPush.send( TestEncoder, { payload: utf8ToBytes(messageText) }, { diff --git a/packages/tests/tests/store.node.spec.ts b/packages/tests/tests/store.node.spec.ts index 0c04876acc..3b593fe208 100644 --- a/packages/tests/tests/store.node.spec.ts +++ b/packages/tests/tests/store.node.spec.ts @@ -412,10 +412,10 @@ describe("Waku Store", () => { log("Sending messages using light push"); await Promise.all([ - waku1.lightPush.push(eciesEncoder, asymMsg), - waku1.lightPush.push(symEncoder, symMsg), - waku1.lightPush.push(otherEncoder, otherMsg), - waku1.lightPush.push(TestEncoder, clearMsg), + waku1.lightPush.send(eciesEncoder, asymMsg), + waku1.lightPush.send(symEncoder, symMsg), + waku1.lightPush.send(otherEncoder, otherMsg), + waku1.lightPush.send(TestEncoder, clearMsg), ]); await waitForRemotePeer(waku2, [Protocols.Store]);