refactor: better express what the function members do

This commit is contained in:
fryorcraken.eth 2022-09-30 12:55:25 +10:00
parent 26967b6334
commit 25fe598082
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
11 changed files with 50 additions and 41 deletions

View File

@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- `Message` interface changed to ensure implementations do not omit fields.
- `Decoder` and `Encoder` interfaces change to better express what the function members do.
## [0.29.0] - 2022-09-21

View File

@ -78,14 +78,14 @@ export interface Message {
export interface Encoder {
contentTopic: string;
encode: (message: Partial<Message>) => Promise<Uint8Array | undefined>;
encodeProto: (message: Partial<Message>) => Promise<ProtoMessage | undefined>;
toWire: (message: Partial<Message>) => Promise<Uint8Array | undefined>;
toProtoObj: (message: Partial<Message>) => Promise<ProtoMessage | undefined>;
}
export interface Decoder<T extends Message> {
contentTopic: string;
decodeProto: (bytes: Uint8Array) => Promise<ProtoMessage | undefined>;
decode: (proto: ProtoMessage) => Promise<T | undefined>;
fromWireToProtoObj: (bytes: Uint8Array) => Promise<ProtoMessage | undefined>;
fromProtoObj: (proto: ProtoMessage) => Promise<T | undefined>;
}
export interface SendResult {

View File

@ -199,7 +199,7 @@ export class WakuFilter {
// noinspection ES6MissingAwait
decoders.forEach(async (dec) => {
if (msg) return;
const decoded = await dec.decode(toProtoMessage(protoMessage));
const decoded = await dec.fromProtoObj(toProtoMessage(protoMessage));
if (!decoded) {
log("Not able to decode message");
return;

View File

@ -79,7 +79,7 @@ export class WakuLightPush {
const recipients: PeerId[] = [];
try {
const protoMessage = await encoder.encodeProto(message);
const protoMessage = await encoder.toProtoObj(message);
if (!protoMessage) {
log("Failed to encode to protoMessage, aborting push");
return { recipients };

View File

@ -20,7 +20,7 @@ export class TopicOnlyMessage implements Message {
export class TopicOnlyDecoder implements Decoder<TopicOnlyMessage> {
public contentTopic = "";
decodeProto(bytes: Uint8Array): Promise<ProtoMessage | undefined> {
fromWireToProtoObj(bytes: Uint8Array): Promise<ProtoMessage | undefined> {
const protoMessage = proto.TopicOnlyMessage.decode(bytes);
log("Message decoded", protoMessage);
return Promise.resolve({
@ -32,7 +32,9 @@ export class TopicOnlyDecoder implements Decoder<TopicOnlyMessage> {
});
}
async decode(proto: ProtoMessage): Promise<TopicOnlyMessage | undefined> {
async fromProtoObj(
proto: ProtoMessage
): Promise<TopicOnlyMessage | undefined> {
return new TopicOnlyMessage(proto);
}
}

View File

@ -10,10 +10,10 @@ describe("Waku Message version 0", function () {
await fc.assert(
fc.asyncProperty(fc.uint8Array({ minLength: 1 }), async (payload) => {
const encoder = new EncoderV0(TestContentTopic);
const bytes = await encoder.encode({ payload });
const bytes = await encoder.toWire({ payload });
const decoder = new DecoderV0(TestContentTopic);
const protoResult = await decoder.decodeProto(bytes);
const result = (await decoder.decode(protoResult!)) as MessageV0;
const protoResult = await decoder.fromWireToProtoObj(bytes);
const result = (await decoder.fromProtoObj(protoResult!)) as MessageV0;
expect(result.contentTopic).to.eq(TestContentTopic);
expect(result.version).to.eq(0);

View File

@ -63,11 +63,11 @@ export class MessageV0 implements Message {
export class EncoderV0 implements Encoder {
constructor(public contentTopic: string) {}
async encode(message: Partial<Message>): Promise<Uint8Array> {
return proto.WakuMessage.encode(await this.encodeProto(message));
async toWire(message: Partial<Message>): Promise<Uint8Array> {
return proto.WakuMessage.encode(await this.toProtoObj(message));
}
async encodeProto(message: Partial<Message>): Promise<ProtoMessage> {
async toProtoObj(message: Partial<Message>): Promise<ProtoMessage> {
const timestamp = message.timestamp ?? new Date();
return {
@ -83,7 +83,7 @@ export class EncoderV0 implements Encoder {
export class DecoderV0 implements Decoder<MessageV0> {
constructor(public contentTopic: string) {}
decodeProto(bytes: Uint8Array): Promise<ProtoMessage | undefined> {
fromWireToProtoObj(bytes: Uint8Array): Promise<ProtoMessage | undefined> {
const protoMessage = proto.WakuMessage.decode(bytes);
log("Message decoded", protoMessage);
return Promise.resolve({
@ -95,7 +95,7 @@ export class DecoderV0 implements Decoder<MessageV0> {
});
}
async decode(proto: ProtoMessage): Promise<MessageV0 | undefined> {
async fromProtoObj(proto: ProtoMessage): Promise<MessageV0 | undefined> {
// https://github.com/status-im/js-waku/issues/921
if (proto.version === undefined) {
proto.version = 0;

View File

@ -28,12 +28,12 @@ describe("Waku Message version 1", function () {
const publicKey = getPublicKey(privateKey);
const encoder = new AsymEncoder(TestContentTopic, publicKey);
const bytes = await encoder.encode({ payload });
const bytes = await encoder.toWire({ payload });
const decoder = new AsymDecoder(TestContentTopic, privateKey);
const protoResult = await decoder.decodeProto(bytes!);
const protoResult = await decoder.fromWireToProtoObj(bytes!);
if (!protoResult) throw "Failed to proto decode";
const result = await decoder.decode(protoResult);
const result = await decoder.fromProtoObj(protoResult);
if (!result) throw "Failed to decode";
expect(result.contentTopic).to.equal(TestContentTopic);
@ -63,12 +63,12 @@ describe("Waku Message version 1", function () {
bobPublicKey,
alicePrivateKey
);
const bytes = await encoder.encode({ payload });
const bytes = await encoder.toWire({ payload });
const decoder = new AsymDecoder(TestContentTopic, bobPrivateKey);
const protoResult = await decoder.decodeProto(bytes!);
const protoResult = await decoder.fromWireToProtoObj(bytes!);
if (!protoResult) throw "Failed to proto decode";
const result = await decoder.decode(protoResult);
const result = await decoder.fromProtoObj(protoResult);
if (!result) throw "Failed to decode";
expect(result.contentTopic).to.equal(TestContentTopic);
@ -88,12 +88,12 @@ describe("Waku Message version 1", function () {
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
async (payload, symKey) => {
const encoder = new SymEncoder(TestContentTopic, symKey);
const bytes = await encoder.encode({ payload });
const bytes = await encoder.toWire({ payload });
const decoder = new SymDecoder(TestContentTopic, symKey);
const protoResult = await decoder.decodeProto(bytes!);
const protoResult = await decoder.fromWireToProtoObj(bytes!);
if (!protoResult) throw "Failed to proto decode";
const result = await decoder.decode(protoResult);
const result = await decoder.fromProtoObj(protoResult);
if (!result) throw "Failed to decode";
expect(result.contentTopic).to.equal(TestContentTopic);
@ -116,12 +116,12 @@ describe("Waku Message version 1", function () {
const sigPubKey = getPublicKey(sigPrivKey);
const encoder = new SymEncoder(TestContentTopic, symKey, sigPrivKey);
const bytes = await encoder.encode({ payload });
const bytes = await encoder.toWire({ payload });
const decoder = new SymDecoder(TestContentTopic, symKey);
const protoResult = await decoder.decodeProto(bytes!);
const protoResult = await decoder.fromWireToProtoObj(bytes!);
if (!protoResult) throw "Failed to proto decode";
const result = await decoder.decode(protoResult);
const result = await decoder.fromProtoObj(protoResult);
if (!result) throw "Failed to decode";
expect(result.contentTopic).to.equal(TestContentTopic);

View File

@ -52,14 +52,14 @@ export class AsymEncoder implements Encoder {
private sigPrivKey?: Uint8Array
) {}
async encode(message: Partial<Message>): Promise<Uint8Array | undefined> {
const protoMessage = await this.encodeProto(message);
async toWire(message: Partial<Message>): Promise<Uint8Array | undefined> {
const protoMessage = await this.toProtoObj(message);
if (!protoMessage) return;
return proto.WakuMessage.encode(protoMessage);
}
async encodeProto(
async toProtoObj(
message: Partial<Message>
): Promise<ProtoMessage | undefined> {
const timestamp = message.timestamp ?? new Date();
@ -88,14 +88,14 @@ export class SymEncoder implements Encoder {
private sigPrivKey?: Uint8Array
) {}
async encode(message: Partial<Message>): Promise<Uint8Array | undefined> {
const protoMessage = await this.encodeProto(message);
async toWire(message: Partial<Message>): Promise<Uint8Array | undefined> {
const protoMessage = await this.toProtoObj(message);
if (!protoMessage) return;
return proto.WakuMessage.encode(protoMessage);
}
async encodeProto(
async toProtoObj(
message: Partial<Message>
): Promise<ProtoMessage | undefined> {
const timestamp = message.timestamp ?? new Date();
@ -121,7 +121,9 @@ export class AsymDecoder extends DecoderV0 implements Decoder<MessageV1> {
super(contentTopic);
}
async decode(protoMessage: ProtoMessage): Promise<MessageV1 | undefined> {
async fromProtoObj(
protoMessage: ProtoMessage
): Promise<MessageV1 | undefined> {
const cipherPayload = protoMessage.payload;
if (protoMessage.version !== Version) {
@ -177,7 +179,9 @@ export class SymDecoder extends DecoderV0 implements Decoder<MessageV1> {
super(contentTopic);
}
async decode(protoMessage: ProtoMessage): Promise<MessageV1 | undefined> {
async fromProtoObj(
protoMessage: ProtoMessage
): Promise<MessageV1 | undefined> {
const cipherPayload = protoMessage.payload;
if (protoMessage.version !== Version) {

View File

@ -96,7 +96,7 @@ export class WakuRelay extends GossipSub {
encoder: Encoder,
message: Partial<Message>
): Promise<SendResult> {
const msg = await encoder.encode(message);
const msg = await encoder.toWire(message);
if (!msg) {
log("Failed to encode message, aborting publish");
return { recipients: [] };
@ -139,7 +139,7 @@ export class WakuRelay extends GossipSub {
if (event.detail.msg.topic !== pubSubTopic) return;
log(`Message received on ${pubSubTopic}`);
const topicOnlyMsg = await this.defaultDecoder.decodeProto(
const topicOnlyMsg = await this.defaultDecoder.fromWireToProtoObj(
event.detail.msg.data
);
if (!topicOnlyMsg || !topicOnlyMsg.contentTopic) {
@ -153,14 +153,16 @@ export class WakuRelay extends GossipSub {
}
await Promise.all(
Array.from(observers).map(async ({ decoder, callback }) => {
const protoMsg = await decoder.decodeProto(event.detail.msg.data);
const protoMsg = await decoder.fromWireToProtoObj(
event.detail.msg.data
);
if (!protoMsg) {
log(
"Internal error: message previously decoded failed on 2nd pass."
);
return;
}
const msg = await decoder.decode(protoMsg);
const msg = await decoder.fromProtoObj(protoMsg);
if (msg) {
callback(msg);
} else {

View File

@ -342,7 +342,7 @@ async function* paginate<T extends Message>(
if (typeof contentTopic !== "undefined") {
const decoder = decoders.get(contentTopic);
if (decoder) {
return decoder.decode(toProtoMessage(protoMsg));
return decoder.fromProtoObj(toProtoMessage(protoMsg));
}
}
return Promise.resolve(undefined);