mirror of https://github.com/status-im/js-waku.git
feat!: use ISender and deprecate Light Push .push (#1217)
This commit is contained in:
parent
b8f7db21c7
commit
0f6a594644
|
@ -36,7 +36,7 @@ class LightPush extends BaseProtocol implements ILightPush {
|
||||||
this.options = options || {};
|
this.options = options || {};
|
||||||
}
|
}
|
||||||
|
|
||||||
async push(
|
async send(
|
||||||
encoder: IEncoder,
|
encoder: IEncoder,
|
||||||
message: IMessage,
|
message: IMessage,
|
||||||
opts?: ProtocolOptions
|
opts?: ProtocolOptions
|
||||||
|
|
|
@ -8,3 +8,4 @@ export * from "./relay.js";
|
||||||
export * from "./store.js";
|
export * from "./store.js";
|
||||||
export * from "./waku.js";
|
export * from "./waku.js";
|
||||||
export * from "./connection_manager.js";
|
export * from "./connection_manager.js";
|
||||||
|
export * from "./sender.js";
|
||||||
|
|
|
@ -1,14 +1,4 @@
|
||||||
import type { IEncoder, IMessage } from "./message.js";
|
import type { PointToPointProtocol } from "./protocols.js";
|
||||||
import type {
|
import type { ISender } from "./sender.js";
|
||||||
PointToPointProtocol,
|
|
||||||
ProtocolOptions,
|
|
||||||
SendResult,
|
|
||||||
} from "./protocols.js";
|
|
||||||
|
|
||||||
export interface ILightPush extends PointToPointProtocol {
|
export type ILightPush = ISender & PointToPointProtocol;
|
||||||
push: (
|
|
||||||
encoder: IEncoder,
|
|
||||||
message: IMessage,
|
|
||||||
opts?: ProtocolOptions
|
|
||||||
) => Promise<SendResult>;
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,13 +1,9 @@
|
||||||
import type { GossipSub, GossipsubEvents } from "@chainsafe/libp2p-gossipsub";
|
import type { GossipSub, GossipsubEvents } from "@chainsafe/libp2p-gossipsub";
|
||||||
import type { EventEmitter } from "@libp2p/interfaces/events";
|
import type { EventEmitter } from "@libp2p/interfaces/events";
|
||||||
|
|
||||||
import type {
|
import type { IDecodedMessage, IDecoder } from "./message.js";
|
||||||
IDecodedMessage,
|
import type { Callback } from "./protocols.js";
|
||||||
IDecoder,
|
import type { ISender } from "./sender.js";
|
||||||
IEncoder,
|
|
||||||
IMessage,
|
|
||||||
} from "./message.js";
|
|
||||||
import type { Callback, SendResult } from "./protocols.js";
|
|
||||||
|
|
||||||
export interface RelayEvents {
|
export interface RelayEvents {
|
||||||
"observer:added": CustomEvent;
|
"observer:added": CustomEvent;
|
||||||
|
@ -16,8 +12,7 @@ export interface RelayEvents {
|
||||||
|
|
||||||
type IRelayEmitter = EventEmitter<RelayEvents & GossipsubEvents>;
|
type IRelayEmitter = EventEmitter<RelayEvents & GossipsubEvents>;
|
||||||
|
|
||||||
interface IRelayAPI extends GossipSub {
|
interface IRelayAPI {
|
||||||
send: (encoder: IEncoder, message: IMessage) => Promise<SendResult>;
|
|
||||||
addObserver: <T extends IDecodedMessage>(
|
addObserver: <T extends IDecodedMessage>(
|
||||||
decoder: IDecoder<T>,
|
decoder: IDecoder<T>,
|
||||||
callback: Callback<T>
|
callback: Callback<T>
|
||||||
|
@ -25,4 +20,4 @@ interface IRelayAPI extends GossipSub {
|
||||||
getMeshPeers: () => string[];
|
getMeshPeers: () => string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export type IRelay = IRelayAPI & IRelayEmitter;
|
export type IRelay = ISender & GossipSub & IRelayAPI & IRelayEmitter;
|
||||||
|
|
|
@ -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<SendResult>;
|
||||||
|
}
|
|
@ -135,9 +135,9 @@ describe("Waku Message Ephemeral field", () => {
|
||||||
|
|
||||||
log("Sending messages using light push");
|
log("Sending messages using light push");
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
waku1.lightPush.push(asymEncoder, asymMsg),
|
waku1.lightPush.send(asymEncoder, asymMsg),
|
||||||
waku1.lightPush.push(symEncoder, symMsg),
|
waku1.lightPush.send(symEncoder, symMsg),
|
||||||
waku1.lightPush.push(clearEncoder, clearMsg),
|
waku1.lightPush.send(clearEncoder, clearMsg),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
await waitForRemotePeer(waku2, [Protocols.Store]);
|
await waitForRemotePeer(waku2, [Protocols.Store]);
|
||||||
|
@ -181,10 +181,10 @@ describe("Waku Message Ephemeral field", () => {
|
||||||
await delay(200);
|
await delay(200);
|
||||||
const normalTxt = "Normal message";
|
const normalTxt = "Normal message";
|
||||||
const ephemeralTxt = "Ephemeral Message";
|
const ephemeralTxt = "Ephemeral Message";
|
||||||
await waku.lightPush.push(TestEncoder, {
|
await waku.lightPush.send(TestEncoder, {
|
||||||
payload: utf8ToBytes(normalTxt),
|
payload: utf8ToBytes(normalTxt),
|
||||||
});
|
});
|
||||||
await waku.lightPush.push(ephemeralEncoder, {
|
await waku.lightPush.send(ephemeralEncoder, {
|
||||||
payload: utf8ToBytes(ephemeralTxt),
|
payload: utf8ToBytes(ephemeralTxt),
|
||||||
});
|
});
|
||||||
while (messages.length < 2) {
|
while (messages.length < 2) {
|
||||||
|
@ -230,10 +230,10 @@ describe("Waku Message Ephemeral field", () => {
|
||||||
await delay(200);
|
await delay(200);
|
||||||
const normalTxt = "Normal message";
|
const normalTxt = "Normal message";
|
||||||
const ephemeralTxt = "Ephemeral Message";
|
const ephemeralTxt = "Ephemeral Message";
|
||||||
await waku.lightPush.push(encoder, {
|
await waku.lightPush.send(encoder, {
|
||||||
payload: utf8ToBytes(normalTxt),
|
payload: utf8ToBytes(normalTxt),
|
||||||
});
|
});
|
||||||
await waku.lightPush.push(ephemeralEncoder, {
|
await waku.lightPush.send(ephemeralEncoder, {
|
||||||
payload: utf8ToBytes(ephemeralTxt),
|
payload: utf8ToBytes(ephemeralTxt),
|
||||||
});
|
});
|
||||||
while (messages.length < 2) {
|
while (messages.length < 2) {
|
||||||
|
@ -280,10 +280,10 @@ describe("Waku Message Ephemeral field", () => {
|
||||||
await delay(200);
|
await delay(200);
|
||||||
const normalTxt = "Normal message";
|
const normalTxt = "Normal message";
|
||||||
const ephemeralTxt = "Ephemeral Message";
|
const ephemeralTxt = "Ephemeral Message";
|
||||||
await waku.lightPush.push(encoder, {
|
await waku.lightPush.send(encoder, {
|
||||||
payload: utf8ToBytes(normalTxt),
|
payload: utf8ToBytes(normalTxt),
|
||||||
});
|
});
|
||||||
await waku.lightPush.push(ephemeralEncoder, {
|
await waku.lightPush.send(ephemeralEncoder, {
|
||||||
payload: utf8ToBytes(ephemeralTxt),
|
payload: utf8ToBytes(ephemeralTxt),
|
||||||
});
|
});
|
||||||
while (messages.length < 2) {
|
while (messages.length < 2) {
|
||||||
|
|
|
@ -63,7 +63,7 @@ describe("Waku Filter", () => {
|
||||||
// correct in future versions of the protocol.
|
// correct in future versions of the protocol.
|
||||||
await delay(200);
|
await delay(200);
|
||||||
|
|
||||||
await waku.lightPush.push(TestEncoder, message);
|
await waku.lightPush.send(TestEncoder, message);
|
||||||
while (messageCount === 0) {
|
while (messageCount === 0) {
|
||||||
await delay(250);
|
await delay(250);
|
||||||
}
|
}
|
||||||
|
@ -81,10 +81,10 @@ describe("Waku Filter", () => {
|
||||||
await waku.filter.subscribe([TestDecoder], callback);
|
await waku.filter.subscribe([TestDecoder], callback);
|
||||||
|
|
||||||
await delay(200);
|
await delay(200);
|
||||||
await waku.lightPush.push(TestEncoder, {
|
await waku.lightPush.send(TestEncoder, {
|
||||||
payload: utf8ToBytes("Filtering works!"),
|
payload: utf8ToBytes("Filtering works!"),
|
||||||
});
|
});
|
||||||
await waku.lightPush.push(TestEncoder, {
|
await waku.lightPush.send(TestEncoder, {
|
||||||
payload: utf8ToBytes("Filtering still works!"),
|
payload: utf8ToBytes("Filtering still works!"),
|
||||||
});
|
});
|
||||||
while (messageCount < 2) {
|
while (messageCount < 2) {
|
||||||
|
@ -101,13 +101,13 @@ describe("Waku Filter", () => {
|
||||||
const unsubscribe = await waku.filter.subscribe([TestDecoder], callback);
|
const unsubscribe = await waku.filter.subscribe([TestDecoder], callback);
|
||||||
|
|
||||||
await delay(200);
|
await delay(200);
|
||||||
await waku.lightPush.push(TestEncoder, {
|
await waku.lightPush.send(TestEncoder, {
|
||||||
payload: utf8ToBytes("This should be received"),
|
payload: utf8ToBytes("This should be received"),
|
||||||
});
|
});
|
||||||
await delay(100);
|
await delay(100);
|
||||||
await unsubscribe();
|
await unsubscribe();
|
||||||
await delay(200);
|
await delay(200);
|
||||||
await waku.lightPush.push(TestEncoder, {
|
await waku.lightPush.send(TestEncoder, {
|
||||||
payload: utf8ToBytes("This should not be received"),
|
payload: utf8ToBytes("This should not be received"),
|
||||||
});
|
});
|
||||||
await delay(100);
|
await delay(100);
|
||||||
|
|
|
@ -46,7 +46,7 @@ describe("Waku Light Push [node only]", () => {
|
||||||
|
|
||||||
const messageText = "Light Push works!";
|
const messageText = "Light Push works!";
|
||||||
|
|
||||||
const pushResponse = await waku.lightPush.push(TestEncoder, {
|
const pushResponse = await waku.lightPush.send(TestEncoder, {
|
||||||
payload: utf8ToBytes(messageText),
|
payload: utf8ToBytes(messageText),
|
||||||
});
|
});
|
||||||
expect(pushResponse.recipients.length).to.eq(1);
|
expect(pushResponse.recipients.length).to.eq(1);
|
||||||
|
@ -87,7 +87,7 @@ describe("Waku Light Push [node only]", () => {
|
||||||
const messageText = "Light Push works!";
|
const messageText = "Light Push works!";
|
||||||
|
|
||||||
log("Send message via lightpush");
|
log("Send message via lightpush");
|
||||||
const pushResponse = await waku.lightPush.push(
|
const pushResponse = await waku.lightPush.send(
|
||||||
TestEncoder,
|
TestEncoder,
|
||||||
{ payload: utf8ToBytes(messageText) },
|
{ payload: utf8ToBytes(messageText) },
|
||||||
{
|
{
|
||||||
|
|
|
@ -412,10 +412,10 @@ describe("Waku Store", () => {
|
||||||
|
|
||||||
log("Sending messages using light push");
|
log("Sending messages using light push");
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
waku1.lightPush.push(eciesEncoder, asymMsg),
|
waku1.lightPush.send(eciesEncoder, asymMsg),
|
||||||
waku1.lightPush.push(symEncoder, symMsg),
|
waku1.lightPush.send(symEncoder, symMsg),
|
||||||
waku1.lightPush.push(otherEncoder, otherMsg),
|
waku1.lightPush.send(otherEncoder, otherMsg),
|
||||||
waku1.lightPush.push(TestEncoder, clearMsg),
|
waku1.lightPush.send(TestEncoder, clearMsg),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
await waitForRemotePeer(waku2, [Protocols.Store]);
|
await waitForRemotePeer(waku2, [Protocols.Store]);
|
||||||
|
|
Loading…
Reference in New Issue