logos-messaging-js/packages/relay/src/topic_only_message.ts
Danish Arora 50c2c2540f
feat!: @waku/relay (#1316)
* move constants & core's func to @waku/utils

* setup @waku/relay

* use @waku/relay for create

* address: principal review

* fix: size-limit

* move all constants to @waku/core

* move TopicOnlyDecoder to relay

* merge: master

* fix: tests

* Move constants out of core (#1340)

* update package.lock

* change constants folder into a ts file

* use dependency version as * instead of version number

* add constants import from @waku/core

* add empty changelog file for relay

---------

Co-authored-by: fryorcraken.eth <110212804+fryorcraken@users.noreply.github.com>
2023-05-11 14:08:00 +05:30

52 lines
1.3 KiB
TypeScript

import type {
IDecodedMessage,
IDecoder,
IProtoMessage,
} from "@waku/interfaces";
import { TopicOnlyMessage as ProtoTopicOnlyMessage } from "@waku/proto";
import debug from "debug";
const log = debug("waku:message:topic-only");
export class TopicOnlyMessage implements IDecodedMessage {
public payload: Uint8Array = new Uint8Array();
public rateLimitProof: undefined;
public timestamp: undefined;
public meta: undefined;
public ephemeral: undefined;
constructor(
public pubSubTopic: string,
private proto: ProtoTopicOnlyMessage
) {}
get contentTopic(): string {
return this.proto.contentTopic;
}
}
export class TopicOnlyDecoder implements IDecoder<TopicOnlyMessage> {
public contentTopic = "";
fromWireToProtoObj(bytes: Uint8Array): Promise<IProtoMessage | undefined> {
const protoMessage = ProtoTopicOnlyMessage.decode(bytes);
log("Message decoded", protoMessage);
return Promise.resolve({
contentTopic: protoMessage.contentTopic,
payload: new Uint8Array(),
rateLimitProof: undefined,
timestamp: undefined,
meta: undefined,
version: undefined,
ephemeral: undefined,
});
}
async fromProtoObj(
pubSubTopic: string,
proto: IProtoMessage
): Promise<TopicOnlyMessage | undefined> {
return new TopicOnlyMessage(pubSubTopic, proto);
}
}