feat: added `I` prefix to message/codec interfaces

To make it easier to differentiate with their implementations.
This commit is contained in:
fryorcraken.eth 2022-12-05 17:07:03 +11:00
parent a7c4bb9cbf
commit cc691e26ce
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
19 changed files with 134 additions and 123 deletions

View File

@ -1,7 +1,7 @@
import { ProtoMessage } from "@waku/interfaces"; import { IProtoMessage } from "@waku/interfaces";
import { WakuMessage as WakuMessageProto } from "@waku/proto"; import { WakuMessage as WakuMessageProto } from "@waku/proto";
const EmptyMessage: ProtoMessage = { const EmptyMessage: IProtoMessage = {
payload: undefined, payload: undefined,
contentTopic: undefined, contentTopic: undefined,
version: undefined, version: undefined,
@ -10,6 +10,6 @@ const EmptyMessage: ProtoMessage = {
ephemeral: undefined, ephemeral: undefined,
}; };
export function toProtoMessage(wire: WakuMessageProto): ProtoMessage { export function toProtoMessage(wire: WakuMessageProto): IProtoMessage {
return { ...EmptyMessage, ...wire }; return { ...EmptyMessage, ...wire };
} }

View File

@ -7,10 +7,10 @@ import type { IncomingStreamData } from "@libp2p/interface-registrar";
import type { Registrar } from "@libp2p/interface-registrar"; import type { Registrar } from "@libp2p/interface-registrar";
import type { import type {
Callback, Callback,
DecodedMessage,
Decoder,
Filter, Filter,
Message, IDecodedMessage,
IDecoder,
IMessage,
ProtocolOptions, ProtocolOptions,
} from "@waku/interfaces"; } from "@waku/interfaces";
import { import {
@ -69,7 +69,7 @@ class WakuFilter implements Filter {
private subscriptions: Map<string, Callback<any>>; private subscriptions: Map<string, Callback<any>>;
private decoders: Map< private decoders: Map<
string, // content topic string, // content topic
Set<Decoder<any>> Set<IDecoder<any>>
>; >;
constructor(public components: FilterComponents, options?: CreateOptions) { constructor(public components: FilterComponents, options?: CreateOptions) {
@ -87,8 +87,8 @@ class WakuFilter implements Filter {
* @param opts The FilterSubscriptionOpts used to narrow which messages are returned, and which peer to connect to. * @param opts The FilterSubscriptionOpts used to narrow which messages are returned, and which peer to connect to.
* @returns Unsubscribe function that can be used to end the subscription. * @returns Unsubscribe function that can be used to end the subscription.
*/ */
async subscribe<T extends DecodedMessage>( async subscribe<T extends IDecodedMessage>(
decoders: Decoder<T>[], decoders: IDecoder<T>[],
callback: Callback<T>, callback: Callback<T>,
opts?: ProtocolOptions opts?: ProtocolOptions
): Promise<UnsubscribeFunction> { ): Promise<UnsubscribeFunction> {
@ -198,7 +198,7 @@ class WakuFilter implements Filter {
return; return;
} }
let msg: Message | undefined; let msg: IMessage | undefined;
// We don't want to wait for decoding failure, just attempt to decode // We don't want to wait for decoding failure, just attempt to decode
// all messages and do the call back on the one that works // all messages and do the call back on the one that works
// noinspection ES6MissingAwait // noinspection ES6MissingAwait
@ -225,8 +225,8 @@ class WakuFilter implements Filter {
this.subscriptions.delete(requestId); this.subscriptions.delete(requestId);
} }
private addDecoders<T extends DecodedMessage>( private addDecoders<T extends IDecodedMessage>(
decoders: Map<string, Array<Decoder<T>>> decoders: Map<string, Array<IDecoder<T>>>
): void { ): void {
decoders.forEach((decoders, contentTopic) => { decoders.forEach((decoders, contentTopic) => {
const currDecs = this.decoders.get(contentTopic); const currDecs = this.decoders.get(contentTopic);
@ -238,8 +238,8 @@ class WakuFilter implements Filter {
}); });
} }
private deleteDecoders<T extends DecodedMessage>( private deleteDecoders<T extends IDecodedMessage>(
decoders: Map<string, Array<Decoder<T>>> decoders: Map<string, Array<IDecoder<T>>>
): void { ): void {
decoders.forEach((decoders, contentTopic) => { decoders.forEach((decoders, contentTopic) => {
const currDecs = this.decoders.get(contentTopic); const currDecs = this.decoders.get(contentTopic);

View File

@ -3,9 +3,9 @@ import type { PeerId } from "@libp2p/interface-peer-id";
import type { Peer } from "@libp2p/interface-peer-store"; import type { Peer } from "@libp2p/interface-peer-store";
import type { PeerStore } from "@libp2p/interface-peer-store"; import type { PeerStore } from "@libp2p/interface-peer-store";
import type { import type {
Encoder, IEncoder,
IMessage,
LightPush, LightPush,
Message,
ProtocolOptions, ProtocolOptions,
SendResult, SendResult,
} from "@waku/interfaces"; } from "@waku/interfaces";
@ -59,8 +59,8 @@ class WakuLightPush implements LightPush {
} }
async push( async push(
encoder: Encoder, encoder: IEncoder,
message: Message, message: IMessage,
opts?: ProtocolOptions opts?: ProtocolOptions
): Promise<SendResult> { ): Promise<SendResult> {
const pubSubTopic = opts?.pubSubTopic ? opts.pubSubTopic : this.pubSubTopic; const pubSubTopic = opts?.pubSubTopic ? opts.pubSubTopic : this.pubSubTopic;

View File

@ -1,10 +1,14 @@
import type { DecodedMessage, Decoder, ProtoMessage } from "@waku/interfaces"; import type {
IDecodedMessage,
IDecoder,
IProtoMessage,
} from "@waku/interfaces";
import { proto_topic_only_message as proto } from "@waku/proto"; import { proto_topic_only_message as proto } from "@waku/proto";
import debug from "debug"; import debug from "debug";
const log = debug("waku:message:topic-only"); const log = debug("waku:message:topic-only");
export class TopicOnlyMessage implements DecodedMessage { export class TopicOnlyMessage implements IDecodedMessage {
public payload: undefined; public payload: undefined;
public rateLimitProof: undefined; public rateLimitProof: undefined;
public timestamp: undefined; public timestamp: undefined;
@ -17,10 +21,10 @@ export class TopicOnlyMessage implements DecodedMessage {
} }
} }
export class TopicOnlyDecoder implements Decoder<TopicOnlyMessage> { export class TopicOnlyDecoder implements IDecoder<TopicOnlyMessage> {
public contentTopic = ""; public contentTopic = "";
fromWireToProtoObj(bytes: Uint8Array): Promise<ProtoMessage | undefined> { fromWireToProtoObj(bytes: Uint8Array): Promise<IProtoMessage | undefined> {
const protoMessage = proto.TopicOnlyMessage.decode(bytes); const protoMessage = proto.TopicOnlyMessage.decode(bytes);
log("Message decoded", protoMessage); log("Message decoded", protoMessage);
return Promise.resolve({ return Promise.resolve({
@ -34,7 +38,7 @@ export class TopicOnlyDecoder implements Decoder<TopicOnlyMessage> {
} }
async fromProtoObj( async fromProtoObj(
proto: ProtoMessage proto: IProtoMessage
): Promise<TopicOnlyMessage | undefined> { ): Promise<TopicOnlyMessage | undefined> {
return new TopicOnlyMessage(proto); return new TopicOnlyMessage(proto);
} }

View File

@ -1,10 +1,10 @@
import type { import type {
DecodedMessage as IDecodedMessage, IDecodedMessage,
Decoder as IDecoder, IDecoder,
Encoder as IEncoder, IEncoder,
Message, IMessage,
ProtoMessage, IProtoMessage,
RateLimitProof, IRateLimitProof,
} from "@waku/interfaces"; } from "@waku/interfaces";
import { proto_message as proto } from "@waku/proto"; import { proto_message as proto } from "@waku/proto";
import debug from "debug"; import debug from "debug";
@ -65,7 +65,7 @@ export class DecodedMessage implements IDecodedMessage {
return this.proto.version ?? 0; return this.proto.version ?? 0;
} }
get rateLimitProof(): RateLimitProof | undefined { get rateLimitProof(): IRateLimitProof | undefined {
return this.proto.rateLimitProof; return this.proto.rateLimitProof;
} }
} }
@ -73,11 +73,11 @@ export class DecodedMessage implements IDecodedMessage {
export class Encoder implements IEncoder { export class Encoder implements IEncoder {
constructor(public contentTopic: string, public ephemeral: boolean = false) {} constructor(public contentTopic: string, public ephemeral: boolean = false) {}
async toWire(message: Message): Promise<Uint8Array> { async toWire(message: IMessage): Promise<Uint8Array> {
return proto.WakuMessage.encode(await this.toProtoObj(message)); return proto.WakuMessage.encode(await this.toProtoObj(message));
} }
async toProtoObj(message: Message): Promise<ProtoMessage> { async toProtoObj(message: IMessage): Promise<IProtoMessage> {
const timestamp = message.timestamp ?? new Date(); const timestamp = message.timestamp ?? new Date();
return { return {
@ -113,7 +113,7 @@ export function createEncoder(
export class Decoder implements IDecoder<DecodedMessage> { export class Decoder implements IDecoder<DecodedMessage> {
constructor(public contentTopic: string) {} constructor(public contentTopic: string) {}
fromWireToProtoObj(bytes: Uint8Array): Promise<ProtoMessage | undefined> { fromWireToProtoObj(bytes: Uint8Array): Promise<IProtoMessage | undefined> {
const protoMessage = proto.WakuMessage.decode(bytes); const protoMessage = proto.WakuMessage.decode(bytes);
log("Message decoded", protoMessage); log("Message decoded", protoMessage);
return Promise.resolve({ return Promise.resolve({
@ -126,7 +126,9 @@ export class Decoder implements IDecoder<DecodedMessage> {
}); });
} }
async fromProtoObj(proto: ProtoMessage): Promise<DecodedMessage | undefined> { async fromProtoObj(
proto: IProtoMessage
): Promise<DecodedMessage | undefined> {
// https://github.com/status-im/js-waku/issues/921 // https://github.com/status-im/js-waku/issues/921
if (proto.version === undefined) { if (proto.version === undefined) {
proto.version = 0; proto.version = 0;

View File

@ -8,13 +8,13 @@ import type { PeerIdStr, TopicStr } from "@chainsafe/libp2p-gossipsub/types";
import { SignaturePolicy } from "@chainsafe/libp2p-gossipsub/types"; import { SignaturePolicy } from "@chainsafe/libp2p-gossipsub/types";
import type { import type {
Callback, Callback,
Decoder, IDecoder,
Encoder, IEncoder,
Message, IMessage,
Relay, Relay,
SendResult, SendResult,
} from "@waku/interfaces"; } from "@waku/interfaces";
import { DecodedMessage } from "@waku/interfaces"; import { IDecodedMessage } from "@waku/interfaces";
import debug from "debug"; import debug from "debug";
import { DefaultPubSubTopic } from "../constants.js"; import { DefaultPubSubTopic } from "../constants.js";
@ -25,8 +25,8 @@ import * as constants from "./constants.js";
const log = debug("waku:relay"); const log = debug("waku:relay");
export type Observer<T extends DecodedMessage> = { export type Observer<T extends IDecodedMessage> = {
decoder: Decoder<T>; decoder: IDecoder<T>;
callback: Callback<T>; callback: Callback<T>;
}; };
@ -55,7 +55,7 @@ export type CreateOptions = {
*/ */
class WakuRelay extends GossipSub implements Relay { class WakuRelay extends GossipSub implements Relay {
pubSubTopic: string; pubSubTopic: string;
defaultDecoder: Decoder<DecodedMessage>; defaultDecoder: IDecoder<IDecodedMessage>;
public static multicodec: string = constants.RelayCodecs[0]; public static multicodec: string = constants.RelayCodecs[0];
/** /**
@ -99,7 +99,7 @@ class WakuRelay extends GossipSub implements Relay {
/** /**
* Send Waku message. * Send Waku message.
*/ */
public async send(encoder: Encoder, message: Message): Promise<SendResult> { public async send(encoder: IEncoder, message: IMessage): Promise<SendResult> {
const msg = await encoder.toWire(message); const msg = await encoder.toWire(message);
if (!msg) { if (!msg) {
log("Failed to encode message, aborting publish"); log("Failed to encode message, aborting publish");
@ -113,8 +113,8 @@ class WakuRelay extends GossipSub implements Relay {
* *
* @returns Function to delete the observer * @returns Function to delete the observer
*/ */
addObserver<T extends DecodedMessage>( addObserver<T extends IDecodedMessage>(
decoder: Decoder<T>, decoder: IDecoder<T>,
callback: Callback<T> callback: Callback<T>
): () => void { ): () => void {
const observer = { const observer = {

View File

@ -6,8 +6,8 @@ import { sha256 } from "@noble/hashes/sha256";
import { concat, utf8ToBytes } from "@waku/byte-utils"; import { concat, utf8ToBytes } from "@waku/byte-utils";
import { import {
Cursor, Cursor,
DecodedMessage, IDecodedMessage,
Decoder, IDecoder,
Index, Index,
Store, Store,
} from "@waku/interfaces"; } from "@waku/interfaces";
@ -128,8 +128,8 @@ class WakuStore implements Store {
* or if an error is encountered when processing the reply, * or if an error is encountered when processing the reply,
* or if two decoders with the same content topic are passed. * or if two decoders with the same content topic are passed.
*/ */
async queryOrderedCallback<T extends DecodedMessage>( async queryOrderedCallback<T extends IDecodedMessage>(
decoders: Decoder<T>[], decoders: IDecoder<T>[],
callback: (message: T) => Promise<void | boolean> | boolean | void, callback: (message: T) => Promise<void | boolean> | boolean | void,
options?: QueryOptions options?: QueryOptions
): Promise<void> { ): Promise<void> {
@ -177,8 +177,8 @@ class WakuStore implements Store {
* or if an error is encountered when processing the reply, * or if an error is encountered when processing the reply,
* or if two decoders with the same content topic are passed. * or if two decoders with the same content topic are passed.
*/ */
async queryCallbackOnPromise<T extends DecodedMessage>( async queryCallbackOnPromise<T extends IDecodedMessage>(
decoders: Decoder<T>[], decoders: IDecoder<T>[],
callback: ( callback: (
message: Promise<T | undefined> message: Promise<T | undefined>
) => Promise<void | boolean> | boolean | void, ) => Promise<void | boolean> | boolean | void,
@ -215,8 +215,8 @@ class WakuStore implements Store {
* or if an error is encountered when processing the reply, * or if an error is encountered when processing the reply,
* or if two decoders with the same content topic are passed. * or if two decoders with the same content topic are passed.
*/ */
async *queryGenerator<T extends DecodedMessage>( async *queryGenerator<T extends IDecodedMessage>(
decoders: Decoder<T>[], decoders: IDecoder<T>[],
options?: QueryOptions options?: QueryOptions
): AsyncGenerator<Promise<T | undefined>[]> { ): AsyncGenerator<Promise<T | undefined>[]> {
let startTime, endTime; let startTime, endTime;
@ -295,11 +295,11 @@ class WakuStore implements Store {
} }
} }
async function* paginate<T extends DecodedMessage>( async function* paginate<T extends IDecodedMessage>(
connection: Connection, connection: Connection,
protocol: string, protocol: string,
queryOpts: Params, queryOpts: Params,
decoders: Map<string, Decoder<T>>, decoders: Map<string, IDecoder<T>>,
cursor?: Cursor cursor?: Cursor
): AsyncGenerator<Promise<T | undefined>[]> { ): AsyncGenerator<Promise<T | undefined>[]> {
if ( if (
@ -404,7 +404,7 @@ export function isDefined<T>(msg: T | undefined): msg is T {
} }
export async function createCursor( export async function createCursor(
message: DecodedMessage, message: IDecodedMessage,
pubsubTopic: string = DefaultPubSubTopic pubsubTopic: string = DefaultPubSubTopic
): Promise<Index> { ): Promise<Index> {
if ( if (

View File

@ -1,4 +1,4 @@
import type { DecodedMessage, Decoder } from "./message.js"; import type { IDecodedMessage, IDecoder } from "./message.js";
import type { import type {
Callback, Callback,
PointToPointProtocol, PointToPointProtocol,
@ -6,8 +6,8 @@ import type {
} from "./protocols.js"; } from "./protocols.js";
export interface Filter extends PointToPointProtocol { export interface Filter extends PointToPointProtocol {
subscribe: <T extends DecodedMessage>( subscribe: <T extends IDecodedMessage>(
decoders: Decoder<T>[], decoders: IDecoder<T>[],
callback: Callback<T>, callback: Callback<T>,
opts?: ProtocolOptions opts?: ProtocolOptions
) => Promise<() => Promise<void>>; ) => Promise<() => Promise<void>>;

View File

@ -1,4 +1,4 @@
import type { Encoder, Message } from "./message.js"; import type { IEncoder, IMessage } from "./message.js";
import type { import type {
PointToPointProtocol, PointToPointProtocol,
ProtocolOptions, ProtocolOptions,
@ -7,8 +7,8 @@ import type {
export interface LightPush extends PointToPointProtocol { export interface LightPush extends PointToPointProtocol {
push: ( push: (
encoder: Encoder, encoder: IEncoder,
message: Message, message: IMessage,
opts?: ProtocolOptions opts?: ProtocolOptions
) => Promise<SendResult>; ) => Promise<SendResult>;
} }

View File

@ -1,4 +1,4 @@
export interface RateLimitProof { export interface IRateLimitProof {
proof: Uint8Array; proof: Uint8Array;
merkleRoot: Uint8Array; merkleRoot: Uint8Array;
epoch: Uint8Array; epoch: Uint8Array;
@ -12,41 +12,41 @@ export interface RateLimitProof {
* Interface matching the protobuf library. * Interface matching the protobuf library.
* Field types matches the protobuf type over the wire * Field types matches the protobuf type over the wire
*/ */
export interface ProtoMessage { export interface IProtoMessage {
payload: Uint8Array | undefined; payload: Uint8Array | undefined;
contentTopic: string | undefined; contentTopic: string | undefined;
version: number | undefined; version: number | undefined;
timestamp: bigint | undefined; timestamp: bigint | undefined;
rateLimitProof: RateLimitProof | undefined; rateLimitProof: IRateLimitProof | undefined;
ephemeral: boolean | undefined; ephemeral: boolean | undefined;
} }
/** /**
* Interface for messages to encode and send. * Interface for messages to encode and send.
*/ */
export interface Message { export interface IMessage {
payload?: Uint8Array; payload?: Uint8Array;
timestamp?: Date; timestamp?: Date;
rateLimitProof?: RateLimitProof; rateLimitProof?: IRateLimitProof;
} }
export interface Encoder { export interface IEncoder {
contentTopic: string; contentTopic: string;
ephemeral: boolean; ephemeral: boolean;
toWire: (message: Message) => Promise<Uint8Array | undefined>; toWire: (message: IMessage) => Promise<Uint8Array | undefined>;
toProtoObj: (message: Message) => Promise<ProtoMessage | undefined>; toProtoObj: (message: IMessage) => Promise<IProtoMessage | undefined>;
} }
export interface DecodedMessage { export interface IDecodedMessage {
payload: Uint8Array | undefined; payload: Uint8Array | undefined;
contentTopic: string | undefined; contentTopic: string | undefined;
timestamp: Date | undefined; timestamp: Date | undefined;
rateLimitProof: RateLimitProof | undefined; rateLimitProof: IRateLimitProof | undefined;
ephemeral: boolean | undefined; ephemeral: boolean | undefined;
} }
export interface Decoder<T extends DecodedMessage> { export interface IDecoder<T extends IDecodedMessage> {
contentTopic: string; contentTopic: string;
fromWireToProtoObj: (bytes: Uint8Array) => Promise<ProtoMessage | undefined>; fromWireToProtoObj: (bytes: Uint8Array) => Promise<IProtoMessage | undefined>;
fromProtoObj: (proto: ProtoMessage) => Promise<T | undefined>; fromProtoObj: (proto: IProtoMessage) => Promise<T | undefined>;
} }

View File

@ -1,7 +1,7 @@
import type { PeerId } from "@libp2p/interface-peer-id"; import type { PeerId } from "@libp2p/interface-peer-id";
import type { Peer, PeerStore } from "@libp2p/interface-peer-store"; import type { Peer, PeerStore } from "@libp2p/interface-peer-store";
import type { Message } from "./message.js"; import type { IMessage } from "./message.js";
export enum Protocols { export enum Protocols {
Relay = "relay", Relay = "relay",
@ -24,7 +24,7 @@ export type ProtocolOptions = {
peerId?: PeerId; peerId?: PeerId;
}; };
export type Callback<T extends Message> = (msg: T) => void | Promise<void>; export type Callback<T extends IMessage> = (msg: T) => void | Promise<void>;
export interface SendResult { export interface SendResult {
recipients: PeerId[]; recipients: PeerId[];

View File

@ -1,12 +1,17 @@
import type { GossipSub } from "@chainsafe/libp2p-gossipsub"; import type { GossipSub } from "@chainsafe/libp2p-gossipsub";
import type { DecodedMessage, Decoder, Encoder, Message } from "./message.js"; import type {
IDecodedMessage,
IDecoder,
IEncoder,
IMessage,
} from "./message.js";
import type { Callback, SendResult } from "./protocols.js"; import type { Callback, SendResult } from "./protocols.js";
export interface Relay extends GossipSub { export interface Relay extends GossipSub {
send: (encoder: Encoder, message: Message) => Promise<SendResult>; send: (encoder: IEncoder, message: IMessage) => Promise<SendResult>;
addObserver: <T extends DecodedMessage>( addObserver: <T extends IDecodedMessage>(
decoder: Decoder<T>, decoder: IDecoder<T>,
callback: Callback<T> callback: Callback<T>
) => () => void; ) => () => void;
getMeshPeers: () => string[]; getMeshPeers: () => string[];

View File

@ -1,4 +1,4 @@
import type { DecodedMessage, Decoder } from "./message.js"; import type { IDecodedMessage, IDecoder } from "./message.js";
import type { PointToPointProtocol, ProtocolOptions } from "./protocols.js"; import type { PointToPointProtocol, ProtocolOptions } from "./protocols.js";
export enum PageDirection { export enum PageDirection {
@ -51,20 +51,20 @@ export type StoreQueryOptions = {
} & ProtocolOptions; } & ProtocolOptions;
export interface Store extends PointToPointProtocol { export interface Store extends PointToPointProtocol {
queryOrderedCallback: <T extends DecodedMessage>( queryOrderedCallback: <T extends IDecodedMessage>(
decoders: Decoder<T>[], decoders: IDecoder<T>[],
callback: (message: T) => Promise<void | boolean> | boolean | void, callback: (message: T) => Promise<void | boolean> | boolean | void,
options?: StoreQueryOptions options?: StoreQueryOptions
) => Promise<void>; ) => Promise<void>;
queryCallbackOnPromise: <T extends DecodedMessage>( queryCallbackOnPromise: <T extends IDecodedMessage>(
decoders: Decoder<T>[], decoders: IDecoder<T>[],
callback: ( callback: (
message: Promise<T | undefined> message: Promise<T | undefined>
) => Promise<void | boolean> | boolean | void, ) => Promise<void | boolean> | boolean | void,
options?: StoreQueryOptions options?: StoreQueryOptions
) => Promise<void>; ) => Promise<void>;
queryGenerator: <T extends DecodedMessage>( queryGenerator: <T extends IDecodedMessage>(
decoders: Decoder<T>[], decoders: IDecoder<T>[],
options?: StoreQueryOptions options?: StoreQueryOptions
) => AsyncGenerator<Promise<T | undefined>[]>; ) => AsyncGenerator<Promise<T | undefined>[]>;
} }

View File

@ -3,10 +3,10 @@ import {
proto, proto,
} from "@waku/core/lib/waku_message/version_0"; } from "@waku/core/lib/waku_message/version_0";
import type { import type {
Decoder as IDecoder, IDecoder,
Encoder as IEncoder, IEncoder,
Message, IMessage,
ProtoMessage, IProtoMessage,
} from "@waku/interfaces"; } from "@waku/interfaces";
import debug from "debug"; import debug from "debug";
@ -37,14 +37,14 @@ class Encoder implements IEncoder {
public ephemeral: boolean = false public ephemeral: boolean = false
) {} ) {}
async toWire(message: Message): Promise<Uint8Array | undefined> { async toWire(message: IMessage): Promise<Uint8Array | undefined> {
const protoMessage = await this.toProtoObj(message); const protoMessage = await this.toProtoObj(message);
if (!protoMessage) return; if (!protoMessage) return;
return proto.WakuMessage.encode(protoMessage); return proto.WakuMessage.encode(protoMessage);
} }
async toProtoObj(message: Message): Promise<ProtoMessage | undefined> { async toProtoObj(message: IMessage): Promise<IProtoMessage | undefined> {
const timestamp = message.timestamp ?? new Date(); const timestamp = message.timestamp ?? new Date();
if (!message.payload) { if (!message.payload) {
log("No payload to encrypt, skipping: ", message); log("No payload to encrypt, skipping: ", message);
@ -98,7 +98,7 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
} }
async fromProtoObj( async fromProtoObj(
protoMessage: ProtoMessage protoMessage: IProtoMessage
): Promise<DecodedMessage | undefined> { ): Promise<DecodedMessage | undefined> {
const cipherPayload = protoMessage.payload; const cipherPayload = protoMessage.payload;

View File

@ -2,7 +2,7 @@ import {
DecodedMessage as DecodedMessageV0, DecodedMessage as DecodedMessageV0,
proto, proto,
} from "@waku/core/lib/waku_message/version_0"; } from "@waku/core/lib/waku_message/version_0";
import type { DecodedMessage as IDecodedMessage } from "@waku/interfaces"; import type { IDecodedMessage } from "@waku/interfaces";
import { import {
generatePrivateKey, generatePrivateKey,

View File

@ -3,10 +3,10 @@ import {
proto, proto,
} from "@waku/core/lib/waku_message/version_0"; } from "@waku/core/lib/waku_message/version_0";
import type { import type {
Decoder as IDecoder, IDecoder,
Encoder as IEncoder, IEncoder,
Message, IMessage,
ProtoMessage, IProtoMessage,
} from "@waku/interfaces"; } from "@waku/interfaces";
import debug from "debug"; import debug from "debug";
@ -36,14 +36,14 @@ class Encoder implements IEncoder {
public ephemeral: boolean = false public ephemeral: boolean = false
) {} ) {}
async toWire(message: Message): Promise<Uint8Array | undefined> { async toWire(message: IMessage): Promise<Uint8Array | undefined> {
const protoMessage = await this.toProtoObj(message); const protoMessage = await this.toProtoObj(message);
if (!protoMessage) return; if (!protoMessage) return;
return proto.WakuMessage.encode(protoMessage); return proto.WakuMessage.encode(protoMessage);
} }
async toProtoObj(message: Message): Promise<ProtoMessage | undefined> { async toProtoObj(message: IMessage): Promise<IProtoMessage | undefined> {
const timestamp = message.timestamp ?? new Date(); const timestamp = message.timestamp ?? new Date();
if (!message.payload) { if (!message.payload) {
log("No payload to encrypt, skipping: ", message); log("No payload to encrypt, skipping: ", message);
@ -96,7 +96,7 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
} }
async fromProtoObj( async fromProtoObj(
protoMessage: ProtoMessage protoMessage: IProtoMessage
): Promise<DecodedMessage | undefined> { ): Promise<DecodedMessage | undefined> {
const cipherPayload = protoMessage.payload; const cipherPayload = protoMessage.payload;

View File

@ -1,7 +1,7 @@
import { bytesToUtf8, utf8ToBytes } from "@waku/byte-utils"; import { bytesToUtf8, utf8ToBytes } from "@waku/byte-utils";
import { createDecoder, createEncoder, waitForRemotePeer } from "@waku/core"; import { createDecoder, createEncoder, waitForRemotePeer } from "@waku/core";
import { createLightNode } from "@waku/create"; import { createLightNode } from "@waku/create";
import type { DecodedMessage, WakuLight } from "@waku/interfaces"; import type { IDecodedMessage, WakuLight } from "@waku/interfaces";
import { Protocols } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces";
import { expect } from "chai"; import { expect } from "chai";
import debug from "debug"; import debug from "debug";
@ -43,7 +43,7 @@ describe("Waku Filter", () => {
const messageText = "Filtering works!"; const messageText = "Filtering works!";
const message = { payload: utf8ToBytes(messageText) }; const message = { payload: utf8ToBytes(messageText) };
const callback = (msg: DecodedMessage): void => { const callback = (msg: IDecodedMessage): void => {
log("Got a message"); log("Got a message");
messageCount++; messageCount++;
expect(msg.contentTopic).to.eq(TestContentTopic); expect(msg.contentTopic).to.eq(TestContentTopic);
@ -67,7 +67,7 @@ describe("Waku Filter", () => {
this.timeout(10000); this.timeout(10000);
let messageCount = 0; let messageCount = 0;
const callback = (msg: DecodedMessage): void => { const callback = (msg: IDecodedMessage): void => {
messageCount++; messageCount++;
expect(msg.contentTopic).to.eq(TestContentTopic); expect(msg.contentTopic).to.eq(TestContentTopic);
}; };

View File

@ -7,7 +7,7 @@ import {
waitForRemotePeer, waitForRemotePeer,
} from "@waku/core"; } from "@waku/core";
import { createLightNode } from "@waku/create"; import { createLightNode } from "@waku/create";
import type { DecodedMessage, Message, WakuLight } from "@waku/interfaces"; import type { IDecodedMessage, IMessage, WakuLight } from "@waku/interfaces";
import { Protocols } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces";
import { import {
createDecoder as createEciesDecoder, createDecoder as createEciesDecoder,
@ -74,7 +74,7 @@ describe("Waku Store", () => {
await waku.dial(await nwaku.getMultiaddrWithId()); await waku.dial(await nwaku.getMultiaddrWithId());
await waitForRemotePeer(waku, [Protocols.Store]); await waitForRemotePeer(waku, [Protocols.Store]);
const messages: Message[] = []; const messages: IMessage[] = [];
let promises: Promise<void>[] = []; let promises: Promise<void>[] = [];
for await (const msgPromises of waku.store.queryGenerator([TestDecoder])) { for await (const msgPromises of waku.store.queryGenerator([TestDecoder])) {
const _promises = msgPromises.map(async (promise) => { const _promises = msgPromises.map(async (promise) => {
@ -105,7 +105,7 @@ describe("Waku Store", () => {
await waku.dial(await nwaku.getMultiaddrWithId()); await waku.dial(await nwaku.getMultiaddrWithId());
await waitForRemotePeer(waku, [Protocols.Store]); await waitForRemotePeer(waku, [Protocols.Store]);
const messages: Message[] = []; const messages: IMessage[] = [];
let promises: Promise<void>[] = []; let promises: Promise<void>[] = [];
for await (const msgPromises of waku.store.queryGenerator([TestDecoder])) { for await (const msgPromises of waku.store.queryGenerator([TestDecoder])) {
const _promises = msgPromises.map(async (promise) => { const _promises = msgPromises.map(async (promise) => {
@ -147,10 +147,10 @@ describe("Waku Store", () => {
const query = waku.store.queryGenerator([TestDecoder]); const query = waku.store.queryGenerator([TestDecoder]);
// messages in reversed order (first message at last index) // messages in reversed order (first message at last index)
const messages: DecodedMessage[] = []; const messages: IDecodedMessage[] = [];
for await (const page of query) { for await (const page of query) {
for await (const msg of page.reverse()) { for await (const msg of page.reverse()) {
messages.push(msg as DecodedMessage); messages.push(msg as IDecodedMessage);
} }
} }
@ -160,12 +160,12 @@ describe("Waku Store", () => {
// create cursor to extract messages after the 3rd index // create cursor to extract messages after the 3rd index
const cursor = await createCursor(messages[cursorIndex]); const cursor = await createCursor(messages[cursorIndex]);
const messagesAfterCursor: DecodedMessage[] = []; const messagesAfterCursor: IDecodedMessage[] = [];
for await (const page of waku.store.queryGenerator([TestDecoder], { for await (const page of waku.store.queryGenerator([TestDecoder], {
cursor, cursor,
})) { })) {
for await (const msg of page.reverse()) { for await (const msg of page.reverse()) {
messagesAfterCursor.push(msg as DecodedMessage); messagesAfterCursor.push(msg as IDecodedMessage);
} }
} }
@ -201,7 +201,7 @@ describe("Waku Store", () => {
await waku.dial(await nwaku.getMultiaddrWithId()); await waku.dial(await nwaku.getMultiaddrWithId());
await waitForRemotePeer(waku, [Protocols.Store]); await waitForRemotePeer(waku, [Protocols.Store]);
const messages: Message[] = []; const messages: IMessage[] = [];
await waku.store.queryCallbackOnPromise( await waku.store.queryCallbackOnPromise(
[TestDecoder], [TestDecoder],
async (msgPromise) => { async (msgPromise) => {
@ -243,7 +243,7 @@ describe("Waku Store", () => {
await waitForRemotePeer(waku, [Protocols.Store]); await waitForRemotePeer(waku, [Protocols.Store]);
const desiredMsgs = 14; const desiredMsgs = 14;
const messages: Message[] = []; const messages: IMessage[] = [];
await waku.store.queryCallbackOnPromise( await waku.store.queryCallbackOnPromise(
[TestDecoder], [TestDecoder],
async (msgPromise) => { async (msgPromise) => {
@ -282,7 +282,7 @@ describe("Waku Store", () => {
await waku.dial(await nwaku.getMultiaddrWithId()); await waku.dial(await nwaku.getMultiaddrWithId());
await waitForRemotePeer(waku, [Protocols.Store]); await waitForRemotePeer(waku, [Protocols.Store]);
const messages: Message[] = []; const messages: IMessage[] = [];
await waku.store.queryOrderedCallback( await waku.store.queryOrderedCallback(
[TestDecoder], [TestDecoder],
async (msg) => { async (msg) => {
@ -321,7 +321,7 @@ describe("Waku Store", () => {
await waku.dial(await nwaku.getMultiaddrWithId()); await waku.dial(await nwaku.getMultiaddrWithId());
await waitForRemotePeer(waku, [Protocols.Store]); await waitForRemotePeer(waku, [Protocols.Store]);
let messages: Message[] = []; let messages: IMessage[] = [];
await waku.store.queryOrderedCallback( await waku.store.queryOrderedCallback(
[TestDecoder], [TestDecoder],
async (msg) => { async (msg) => {
@ -413,7 +413,7 @@ describe("Waku Store", () => {
await waitForRemotePeer(waku2, [Protocols.Store]); await waitForRemotePeer(waku2, [Protocols.Store]);
const messages: DecodedMessage[] = []; const messages: IDecodedMessage[] = [];
log("Retrieve messages from store"); log("Retrieve messages from store");
for await (const msgPromises of waku2.store.queryGenerator([ for await (const msgPromises of waku2.store.queryGenerator([
@ -482,7 +482,7 @@ describe("Waku Store", () => {
const nwakuPeerId = await nwaku.getPeerId(); const nwakuPeerId = await nwaku.getPeerId();
const firstMessages: Message[] = []; const firstMessages: IMessage[] = [];
await waku.store.queryOrderedCallback( await waku.store.queryOrderedCallback(
[TestDecoder], [TestDecoder],
(msg) => { (msg) => {
@ -496,7 +496,7 @@ describe("Waku Store", () => {
} }
); );
const bothMessages: Message[] = []; const bothMessages: IMessage[] = [];
await waku.store.queryOrderedCallback( await waku.store.queryOrderedCallback(
[TestDecoder], [TestDecoder],
async (msg) => { async (msg) => {
@ -543,7 +543,7 @@ describe("Waku Store", () => {
await waitForRemotePeer(waku, [Protocols.Store]); await waitForRemotePeer(waku, [Protocols.Store]);
const desiredMsgs = 14; const desiredMsgs = 14;
const messages: Message[] = []; const messages: IMessage[] = [];
await waku.store.queryOrderedCallback( await waku.store.queryOrderedCallback(
[TestDecoder], [TestDecoder],
async (msg) => { async (msg) => {
@ -601,7 +601,7 @@ describe("Waku Store, custom pubsub topic", () => {
await waku.dial(await nwaku.getMultiaddrWithId()); await waku.dial(await nwaku.getMultiaddrWithId());
await waitForRemotePeer(waku, [Protocols.Store]); await waitForRemotePeer(waku, [Protocols.Store]);
const messages: Message[] = []; const messages: IMessage[] = [];
let promises: Promise<void>[] = []; let promises: Promise<void>[] = [];
for await (const msgPromises of waku.store.queryGenerator([TestDecoder])) { for await (const msgPromises of waku.store.queryGenerator([TestDecoder])) {
const _promises = msgPromises.map(async (promise) => { const _promises = msgPromises.map(async (promise) => {

View File

@ -4,7 +4,7 @@ import { bytesToUtf8, utf8ToBytes } from "@waku/byte-utils";
import { DefaultUserAgent, waitForRemotePeer } from "@waku/core"; import { DefaultUserAgent, waitForRemotePeer } from "@waku/core";
import { createLightNode, createPrivacyNode } from "@waku/create"; import { createLightNode, createPrivacyNode } from "@waku/create";
import type { import type {
DecodedMessage, IDecodedMessage,
Waku, Waku,
WakuLight, WakuLight,
WakuPrivacy, WakuPrivacy,
@ -180,7 +180,7 @@ describe("Decryption Keys", () => {
timestamp: messageTimestamp, timestamp: messageTimestamp,
}; };
const receivedMsgPromise: Promise<DecodedMessage> = new Promise( const receivedMsgPromise: Promise<IDecodedMessage> = new Promise(
(resolve) => { (resolve) => {
waku2.relay.addObserver(decoder, resolve); waku2.relay.addObserver(decoder, resolve);
} }