Skip to content

Commit

Permalink
Avoid possible type name clash between js-waku and consuming apps
Browse files Browse the repository at this point in the history
`Message` is a very generic name and JS does not offer strong namespace
boundaries. Using `WakuMessage` avoid name clashing with classes
of the consumer app.
  • Loading branch information
D4nte committed Apr 1, 2021
1 parent 861bc2d commit 5a967ec
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 36 deletions.
2 changes: 1 addition & 1 deletion proto/waku/v2/waku.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ syntax = "proto3";

package waku.v2;

message WakuMessage {
message WakuMessageProto {
optional bytes payload = 1;
optional uint32 content_topic = 2;
optional uint32 version = 3;
Expand Down
6 changes: 3 additions & 3 deletions src/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import readline from 'readline';
import util from 'util';

import Waku from '../lib/waku';
import { Message } from '../lib/waku_message';
import { WakuMessage } from '../lib/waku_message';
import { TOPIC } from '../lib/waku_relay';
import { delay } from '../test_utils/delay';

Expand Down Expand Up @@ -30,7 +30,7 @@ import { ChatMessage } from './chat_message';
// TODO: Bubble event to waku, infer topic, decode msg
// Tracked with https://github.com/status-im/js-waku/issues/19
waku.libp2p.pubsub.on(TOPIC, (event) => {
const wakuMsg = Message.decode(event.data);
const wakuMsg = WakuMessage.decode(event.data);
if (wakuMsg.payload) {
const chatMsg = ChatMessage.decode(wakuMsg.payload);
const timestamp = chatMsg.timestamp.toLocaleString([], {
Expand Down Expand Up @@ -74,7 +74,7 @@ import { ChatMessage } from './chat_message';
rl.prompt();
const chatMessage = new ChatMessage(new Date(), nick, line);

const msg = Message.fromBytes(chatMessage.encode());
const msg = WakuMessage.fromBytes(chatMessage.encode());
await waku.relay.publish(msg);
}
})();
Expand Down
8 changes: 4 additions & 4 deletions src/lib/waku_message.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import fc from 'fast-check';

import { Message } from './waku_message';
import { WakuMessage } from './waku_message';

describe('Waku Message', function () {
it('Waku message round trip binary serialization', function () {
fc.assert(
fc.property(fc.string(), (s) => {
const msg = Message.fromUtf8String(s);
const msg = WakuMessage.fromUtf8String(s);
const binary = msg.toBinary();
const actual = Message.decode(binary);
const actual = WakuMessage.decode(binary);

return actual.isEqualTo(msg);
})
Expand All @@ -18,7 +18,7 @@ describe('Waku Message', function () {
it('Payload to utf-8', function () {
fc.assert(
fc.property(fc.string(), (s) => {
const msg = Message.fromUtf8String(s);
const msg = WakuMessage.fromUtf8String(s);
const utf8 = msg.utf8Payload();

return utf8 === s;
Expand Down
30 changes: 17 additions & 13 deletions src/lib/waku_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import { Reader } from 'protobufjs/minimal';

// Protecting the user from protobuf oddities
import { WakuMessage } from '../proto/waku/v2/waku';
import { WakuMessageProto } from '../proto/waku/v2/waku';

const DEFAULT_CONTENT_TOPIC = 1;
const DEFAULT_VERSION = 0;

export class Message {
export class WakuMessage {
private constructor(
public payload?: Uint8Array,
public contentTopic?: number,
Expand All @@ -17,29 +17,33 @@ export class Message {
/**
* Create Message with a utf-8 string as payload
* @param payload
* @returns {Message}
* @returns {WakuMessage}
*/
static fromUtf8String(payload: string): Message {
static fromUtf8String(payload: string): WakuMessage {
const buf = Buffer.from(payload, 'utf-8');
return new Message(buf, DEFAULT_CONTENT_TOPIC, DEFAULT_VERSION);
return new WakuMessage(buf, DEFAULT_CONTENT_TOPIC, DEFAULT_VERSION);
}

/**
* Create Message with a byte array as payload
* @param payload
* @returns {Message}
* @returns {WakuMessage}
*/
static fromBytes(payload: Uint8Array): Message {
return new Message(payload, DEFAULT_CONTENT_TOPIC, DEFAULT_VERSION);
static fromBytes(payload: Uint8Array): WakuMessage {
return new WakuMessage(payload, DEFAULT_CONTENT_TOPIC, DEFAULT_VERSION);
}

static decode(bytes: Uint8Array): Message {
const wakuMsg = WakuMessage.decode(Reader.create(bytes));
return new Message(wakuMsg.payload, wakuMsg.contentTopic, wakuMsg.version);
static decode(bytes: Uint8Array): WakuMessage {
const wakuMsg = WakuMessageProto.decode(Reader.create(bytes));
return new WakuMessage(
wakuMsg.payload,
wakuMsg.contentTopic,
wakuMsg.version
);
}

toBinary(): Uint8Array {
return WakuMessage.encode({
return WakuMessageProto.encode({
payload: this.payload,
version: this.version,
contentTopic: this.contentTopic,
Expand All @@ -61,7 +65,7 @@ export class Message {
// Purely for tests purposes.
// We do consider protobuf field when checking equality
// As the content is held by the other fields.
isEqualTo(other: Message) {
isEqualTo(other: WakuMessage) {
const payloadsAreEqual =
this.payload && other.payload
? Buffer.compare(this.payload, other.payload) === 0
Expand Down
22 changes: 11 additions & 11 deletions src/lib/waku_relay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { makeLogFileName } from '../test_utils/log_file';
import { NimWaku } from '../test_utils/nim_waku';

import Waku from './waku';
import { Message } from './waku_message';
import { WakuMessage } from './waku_message';
import { CODEC, TOPIC } from './waku_relay';

describe('Waku Relay', () => {
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('Waku Relay', () => {
it.skip('Publish', async function () {
this.timeout(10000);

const message = Message.fromUtf8String('JS to JS communication works');
const message = WakuMessage.fromUtf8String('JS to JS communication works');
// waku.libp2p.pubsub.globalSignaturePolicy = 'StrictSign';

const receivedPromise = waitForNextData(waku2.libp2p.pubsub);
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('Waku Relay', () => {
it('Js publishes to nim', async function () {
this.timeout(5000);

const message = Message.fromUtf8String('This is a message');
const message = WakuMessage.fromUtf8String('This is a message');

await waku.relay.publish(message);

Expand All @@ -158,7 +158,7 @@ describe('Waku Relay', () => {

it('Nim publishes to js', async function () {
this.timeout(5000);
const message = Message.fromUtf8String('Here is another message.');
const message = WakuMessage.fromUtf8String('Here is another message.');

const receivedPromise = waitForNextData(waku.libp2p.pubsub);

Expand Down Expand Up @@ -212,7 +212,7 @@ describe('Waku Relay', () => {
});

it('Js publishes to nim', async function () {
const message = Message.fromUtf8String('This is a message');
const message = WakuMessage.fromUtf8String('This is a message');

await waku.relay.publish(message);

Expand All @@ -228,7 +228,7 @@ describe('Waku Relay', () => {
});

it('Nim publishes to js', async function () {
const message = Message.fromUtf8String('Here is another message.');
const message = WakuMessage.fromUtf8String('Here is another message.');

const receivedPromise = waitForNextData(waku.libp2p.pubsub);

Expand Down Expand Up @@ -281,7 +281,7 @@ describe('Waku Relay', () => {
});

it('Js publishes to nim', async function () {
const message = Message.fromUtf8String('This is a message');
const message = WakuMessage.fromUtf8String('This is a message');

await waku.relay.publish(message);

Expand All @@ -297,7 +297,7 @@ describe('Waku Relay', () => {
});

it('Nim publishes to js', async function () {
const message = Message.fromUtf8String('Here is another message.');
const message = WakuMessage.fromUtf8String('Here is another message.');

const receivedPromise = waitForNextData(waku.libp2p.pubsub);

Expand Down Expand Up @@ -374,7 +374,7 @@ describe('Waku Relay', () => {
).to.be.false;

const msgStr = 'Hello there!';
const message = Message.fromUtf8String(msgStr);
const message = WakuMessage.fromUtf8String(msgStr);

const waku2ReceivedPromise = waitForNextData(waku2.libp2p.pubsub);

Expand All @@ -388,10 +388,10 @@ describe('Waku Relay', () => {
});
});

function waitForNextData(pubsub: Pubsub): Promise<Message> {
function waitForNextData(pubsub: Pubsub): Promise<WakuMessage> {
return new Promise((resolve) => {
pubsub.once(TOPIC, resolve);
}).then((msg: any) => {
return Message.decode(msg.data);
return WakuMessage.decode(msg.data);
});
}
4 changes: 2 additions & 2 deletions src/lib/waku_relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Pubsub from 'libp2p-interfaces/src/pubsub';
import { SignaturePolicy } from 'libp2p-interfaces/src/pubsub/signature-policy';

import { getWakuPeers } from './get_waku_peers';
import { Message } from './waku_message';
import { WakuMessage } from './waku_message';

export const CODEC = '/vac/waku/relay/2.0.0-beta2';

Expand Down Expand Up @@ -99,7 +99,7 @@ export class WakuRelay {
await this.pubsub.subscribe(TOPIC);
}

async publish(message: Message) {
async publish(message: WakuMessage) {
const msg = message.toBinary();
await this.pubsub.publish(TOPIC, msg);
}
Expand Down
4 changes: 2 additions & 2 deletions src/test_utils/nim_waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Multiaddr from 'multiaddr';
import multiaddr from 'multiaddr';
import PeerId from 'peer-id';

import { Message } from '../lib/waku_message';
import { WakuMessage } from '../lib/waku_message';
import { TOPIC } from '../lib/waku_relay';

import { existsAsync, mkdirAsync, openAsync } from './async_fs';
Expand Down Expand Up @@ -132,7 +132,7 @@ export class NimWaku {
return res.result;
}

async sendMessage(message: Message) {
async sendMessage(message: WakuMessage) {
this.checkProcess();

if (!message.payload) {
Expand Down

0 comments on commit 5a967ec

Please sign in to comment.