Avoid possible type name clash between js-waku and consuming apps

`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.
This commit is contained in:
Franck Royer 2021-04-01 11:18:35 +11:00
parent 861bc2d0b4
commit 5a967ecbcc
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
7 changed files with 40 additions and 36 deletions

View File

@ -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;

View File

@ -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';
@ -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([], {
@ -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);
}
})();

View File

@ -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);
})
@ -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;

View File

@ -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,
@ -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,
@ -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

View File

@ -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', () => {
@ -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);
@ -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);
@ -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);
@ -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);
@ -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);
@ -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);
@ -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);
@ -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);
@ -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);
});
}

View File

@ -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';
@ -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);
}

View File

@ -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';
@ -132,7 +132,7 @@ export class NimWaku {
return res.result;
}
async sendMessage(message: Message) {
async sendMessage(message: WakuMessage) {
this.checkProcess();
if (!message.payload) {