feat: remove V0 suffix from Decoder/Encoder

This commit is contained in:
fryorcraken.eth 2022-11-21 13:09:25 +11:00
parent e0c738c092
commit 0f5bef78d1
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4

View File

@ -1,7 +1,7 @@
import type { import type {
DecodedMessage, DecodedMessage as IDecodedMessage,
Decoder, Decoder as IDecoder,
Encoder, Encoder as IEncoder,
Message, Message,
ProtoMessage, ProtoMessage,
RateLimitProof, RateLimitProof,
@ -16,7 +16,7 @@ const OneMillion = BigInt(1_000_000);
export const Version = 0; export const Version = 0;
export { proto }; export { proto };
export class MessageV0 implements DecodedMessage { export class DecodedMessage implements IDecodedMessage {
constructor(protected proto: proto.WakuMessage) {} constructor(protected proto: proto.WakuMessage) {}
get _rawPayload(): Uint8Array | undefined { get _rawPayload(): Uint8Array | undefined {
@ -71,7 +71,7 @@ export class MessageV0 implements DecodedMessage {
} }
} }
export class EncoderV0 implements Encoder { class Encoder implements IEncoder {
constructor(public contentTopic: string, public ephemeral: boolean = false) {} constructor(public contentTopic: string, public ephemeral: boolean = false) {}
async toWire(message: Partial<Message>): Promise<Uint8Array> { async toWire(message: Partial<Message>): Promise<Uint8Array> {
@ -92,7 +92,14 @@ export class EncoderV0 implements Encoder {
} }
} }
export class DecoderV0 implements Decoder<MessageV0> { export function createEncoder(
contentTopic: string,
ephemeral = false
): Encoder {
return new Encoder(contentTopic, ephemeral);
}
export class Decoder implements IDecoder<DecodedMessage> {
constructor(public contentTopic: string, public ephemeral: boolean = false) {} constructor(public contentTopic: string, public ephemeral: boolean = false) {}
fromWireToProtoObj(bytes: Uint8Array): Promise<ProtoMessage | undefined> { fromWireToProtoObj(bytes: Uint8Array): Promise<ProtoMessage | undefined> {
@ -108,7 +115,7 @@ export class DecoderV0 implements Decoder<MessageV0> {
}); });
} }
async fromProtoObj(proto: ProtoMessage): Promise<MessageV0 | undefined> { async fromProtoObj(proto: ProtoMessage): 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;
@ -124,6 +131,13 @@ export class DecoderV0 implements Decoder<MessageV0> {
return Promise.resolve(undefined); return Promise.resolve(undefined);
} }
return new MessageV0(proto); return new DecodedMessage(proto);
} }
} }
export function createDecoder(
contentTopic: string,
ephemeral = false
): Encoder {
return new Decoder(contentTopic, ephemeral);
}