mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-04 06:43:12 +00:00
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:
parent
861bc2d0b4
commit
5a967ecbcc
@ -2,7 +2,7 @@ syntax = "proto3";
|
|||||||
|
|
||||||
package waku.v2;
|
package waku.v2;
|
||||||
|
|
||||||
message WakuMessage {
|
message WakuMessageProto {
|
||||||
optional bytes payload = 1;
|
optional bytes payload = 1;
|
||||||
optional uint32 content_topic = 2;
|
optional uint32 content_topic = 2;
|
||||||
optional uint32 version = 3;
|
optional uint32 version = 3;
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import readline from 'readline';
|
|||||||
import util from 'util';
|
import util from 'util';
|
||||||
|
|
||||||
import Waku from '../lib/waku';
|
import Waku from '../lib/waku';
|
||||||
import { Message } from '../lib/waku_message';
|
import { WakuMessage } from '../lib/waku_message';
|
||||||
import { TOPIC } from '../lib/waku_relay';
|
import { TOPIC } from '../lib/waku_relay';
|
||||||
import { delay } from '../test_utils/delay';
|
import { delay } from '../test_utils/delay';
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ import { ChatMessage } from './chat_message';
|
|||||||
// TODO: Bubble event to waku, infer topic, decode msg
|
// TODO: Bubble event to waku, infer topic, decode msg
|
||||||
// Tracked with https://github.com/status-im/js-waku/issues/19
|
// Tracked with https://github.com/status-im/js-waku/issues/19
|
||||||
waku.libp2p.pubsub.on(TOPIC, (event) => {
|
waku.libp2p.pubsub.on(TOPIC, (event) => {
|
||||||
const wakuMsg = Message.decode(event.data);
|
const wakuMsg = WakuMessage.decode(event.data);
|
||||||
if (wakuMsg.payload) {
|
if (wakuMsg.payload) {
|
||||||
const chatMsg = ChatMessage.decode(wakuMsg.payload);
|
const chatMsg = ChatMessage.decode(wakuMsg.payload);
|
||||||
const timestamp = chatMsg.timestamp.toLocaleString([], {
|
const timestamp = chatMsg.timestamp.toLocaleString([], {
|
||||||
@ -74,7 +74,7 @@ import { ChatMessage } from './chat_message';
|
|||||||
rl.prompt();
|
rl.prompt();
|
||||||
const chatMessage = new ChatMessage(new Date(), nick, line);
|
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);
|
await waku.relay.publish(msg);
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|||||||
@ -1,14 +1,14 @@
|
|||||||
import fc from 'fast-check';
|
import fc from 'fast-check';
|
||||||
|
|
||||||
import { Message } from './waku_message';
|
import { WakuMessage } from './waku_message';
|
||||||
|
|
||||||
describe('Waku Message', function () {
|
describe('Waku Message', function () {
|
||||||
it('Waku message round trip binary serialization', function () {
|
it('Waku message round trip binary serialization', function () {
|
||||||
fc.assert(
|
fc.assert(
|
||||||
fc.property(fc.string(), (s) => {
|
fc.property(fc.string(), (s) => {
|
||||||
const msg = Message.fromUtf8String(s);
|
const msg = WakuMessage.fromUtf8String(s);
|
||||||
const binary = msg.toBinary();
|
const binary = msg.toBinary();
|
||||||
const actual = Message.decode(binary);
|
const actual = WakuMessage.decode(binary);
|
||||||
|
|
||||||
return actual.isEqualTo(msg);
|
return actual.isEqualTo(msg);
|
||||||
})
|
})
|
||||||
@ -18,7 +18,7 @@ describe('Waku Message', function () {
|
|||||||
it('Payload to utf-8', function () {
|
it('Payload to utf-8', function () {
|
||||||
fc.assert(
|
fc.assert(
|
||||||
fc.property(fc.string(), (s) => {
|
fc.property(fc.string(), (s) => {
|
||||||
const msg = Message.fromUtf8String(s);
|
const msg = WakuMessage.fromUtf8String(s);
|
||||||
const utf8 = msg.utf8Payload();
|
const utf8 = msg.utf8Payload();
|
||||||
|
|
||||||
return utf8 === s;
|
return utf8 === s;
|
||||||
|
|||||||
@ -2,12 +2,12 @@
|
|||||||
import { Reader } from 'protobufjs/minimal';
|
import { Reader } from 'protobufjs/minimal';
|
||||||
|
|
||||||
// Protecting the user from protobuf oddities
|
// 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_CONTENT_TOPIC = 1;
|
||||||
const DEFAULT_VERSION = 0;
|
const DEFAULT_VERSION = 0;
|
||||||
|
|
||||||
export class Message {
|
export class WakuMessage {
|
||||||
private constructor(
|
private constructor(
|
||||||
public payload?: Uint8Array,
|
public payload?: Uint8Array,
|
||||||
public contentTopic?: number,
|
public contentTopic?: number,
|
||||||
@ -17,29 +17,33 @@ export class Message {
|
|||||||
/**
|
/**
|
||||||
* Create Message with a utf-8 string as payload
|
* Create Message with a utf-8 string as payload
|
||||||
* @param payload
|
* @param payload
|
||||||
* @returns {Message}
|
* @returns {WakuMessage}
|
||||||
*/
|
*/
|
||||||
static fromUtf8String(payload: string): Message {
|
static fromUtf8String(payload: string): WakuMessage {
|
||||||
const buf = Buffer.from(payload, 'utf-8');
|
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
|
* Create Message with a byte array as payload
|
||||||
* @param payload
|
* @param payload
|
||||||
* @returns {Message}
|
* @returns {WakuMessage}
|
||||||
*/
|
*/
|
||||||
static fromBytes(payload: Uint8Array): Message {
|
static fromBytes(payload: Uint8Array): WakuMessage {
|
||||||
return new Message(payload, DEFAULT_CONTENT_TOPIC, DEFAULT_VERSION);
|
return new WakuMessage(payload, DEFAULT_CONTENT_TOPIC, DEFAULT_VERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
static decode(bytes: Uint8Array): Message {
|
static decode(bytes: Uint8Array): WakuMessage {
|
||||||
const wakuMsg = WakuMessage.decode(Reader.create(bytes));
|
const wakuMsg = WakuMessageProto.decode(Reader.create(bytes));
|
||||||
return new Message(wakuMsg.payload, wakuMsg.contentTopic, wakuMsg.version);
|
return new WakuMessage(
|
||||||
|
wakuMsg.payload,
|
||||||
|
wakuMsg.contentTopic,
|
||||||
|
wakuMsg.version
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
toBinary(): Uint8Array {
|
toBinary(): Uint8Array {
|
||||||
return WakuMessage.encode({
|
return WakuMessageProto.encode({
|
||||||
payload: this.payload,
|
payload: this.payload,
|
||||||
version: this.version,
|
version: this.version,
|
||||||
contentTopic: this.contentTopic,
|
contentTopic: this.contentTopic,
|
||||||
@ -61,7 +65,7 @@ export class Message {
|
|||||||
// Purely for tests purposes.
|
// Purely for tests purposes.
|
||||||
// We do consider protobuf field when checking equality
|
// We do consider protobuf field when checking equality
|
||||||
// As the content is held by the other fields.
|
// As the content is held by the other fields.
|
||||||
isEqualTo(other: Message) {
|
isEqualTo(other: WakuMessage) {
|
||||||
const payloadsAreEqual =
|
const payloadsAreEqual =
|
||||||
this.payload && other.payload
|
this.payload && other.payload
|
||||||
? Buffer.compare(this.payload, other.payload) === 0
|
? Buffer.compare(this.payload, other.payload) === 0
|
||||||
|
|||||||
@ -7,7 +7,7 @@ import { makeLogFileName } from '../test_utils/log_file';
|
|||||||
import { NimWaku } from '../test_utils/nim_waku';
|
import { NimWaku } from '../test_utils/nim_waku';
|
||||||
|
|
||||||
import Waku from './waku';
|
import Waku from './waku';
|
||||||
import { Message } from './waku_message';
|
import { WakuMessage } from './waku_message';
|
||||||
import { CODEC, TOPIC } from './waku_relay';
|
import { CODEC, TOPIC } from './waku_relay';
|
||||||
|
|
||||||
describe('Waku Relay', () => {
|
describe('Waku Relay', () => {
|
||||||
@ -77,7 +77,7 @@ describe('Waku Relay', () => {
|
|||||||
it.skip('Publish', async function () {
|
it.skip('Publish', async function () {
|
||||||
this.timeout(10000);
|
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';
|
// waku.libp2p.pubsub.globalSignaturePolicy = 'StrictSign';
|
||||||
|
|
||||||
const receivedPromise = waitForNextData(waku2.libp2p.pubsub);
|
const receivedPromise = waitForNextData(waku2.libp2p.pubsub);
|
||||||
@ -141,7 +141,7 @@ describe('Waku Relay', () => {
|
|||||||
it('Js publishes to nim', async function () {
|
it('Js publishes to nim', async function () {
|
||||||
this.timeout(5000);
|
this.timeout(5000);
|
||||||
|
|
||||||
const message = Message.fromUtf8String('This is a message');
|
const message = WakuMessage.fromUtf8String('This is a message');
|
||||||
|
|
||||||
await waku.relay.publish(message);
|
await waku.relay.publish(message);
|
||||||
|
|
||||||
@ -158,7 +158,7 @@ describe('Waku Relay', () => {
|
|||||||
|
|
||||||
it('Nim publishes to js', async function () {
|
it('Nim publishes to js', async function () {
|
||||||
this.timeout(5000);
|
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);
|
const receivedPromise = waitForNextData(waku.libp2p.pubsub);
|
||||||
|
|
||||||
@ -212,7 +212,7 @@ describe('Waku Relay', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('Js publishes to nim', async function () {
|
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);
|
await waku.relay.publish(message);
|
||||||
|
|
||||||
@ -228,7 +228,7 @@ describe('Waku Relay', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('Nim publishes to js', async function () {
|
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);
|
const receivedPromise = waitForNextData(waku.libp2p.pubsub);
|
||||||
|
|
||||||
@ -281,7 +281,7 @@ describe('Waku Relay', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('Js publishes to nim', async function () {
|
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);
|
await waku.relay.publish(message);
|
||||||
|
|
||||||
@ -297,7 +297,7 @@ describe('Waku Relay', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('Nim publishes to js', async function () {
|
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);
|
const receivedPromise = waitForNextData(waku.libp2p.pubsub);
|
||||||
|
|
||||||
@ -374,7 +374,7 @@ describe('Waku Relay', () => {
|
|||||||
).to.be.false;
|
).to.be.false;
|
||||||
|
|
||||||
const msgStr = 'Hello there!';
|
const msgStr = 'Hello there!';
|
||||||
const message = Message.fromUtf8String(msgStr);
|
const message = WakuMessage.fromUtf8String(msgStr);
|
||||||
|
|
||||||
const waku2ReceivedPromise = waitForNextData(waku2.libp2p.pubsub);
|
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) => {
|
return new Promise((resolve) => {
|
||||||
pubsub.once(TOPIC, resolve);
|
pubsub.once(TOPIC, resolve);
|
||||||
}).then((msg: any) => {
|
}).then((msg: any) => {
|
||||||
return Message.decode(msg.data);
|
return WakuMessage.decode(msg.data);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import Pubsub from 'libp2p-interfaces/src/pubsub';
|
|||||||
import { SignaturePolicy } from 'libp2p-interfaces/src/pubsub/signature-policy';
|
import { SignaturePolicy } from 'libp2p-interfaces/src/pubsub/signature-policy';
|
||||||
|
|
||||||
import { getWakuPeers } from './get_waku_peers';
|
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';
|
export const CODEC = '/vac/waku/relay/2.0.0-beta2';
|
||||||
|
|
||||||
@ -99,7 +99,7 @@ export class WakuRelay {
|
|||||||
await this.pubsub.subscribe(TOPIC);
|
await this.pubsub.subscribe(TOPIC);
|
||||||
}
|
}
|
||||||
|
|
||||||
async publish(message: Message) {
|
async publish(message: WakuMessage) {
|
||||||
const msg = message.toBinary();
|
const msg = message.toBinary();
|
||||||
await this.pubsub.publish(TOPIC, msg);
|
await this.pubsub.publish(TOPIC, msg);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,7 @@ import Multiaddr from 'multiaddr';
|
|||||||
import multiaddr from 'multiaddr';
|
import multiaddr from 'multiaddr';
|
||||||
import PeerId from 'peer-id';
|
import PeerId from 'peer-id';
|
||||||
|
|
||||||
import { Message } from '../lib/waku_message';
|
import { WakuMessage } from '../lib/waku_message';
|
||||||
import { TOPIC } from '../lib/waku_relay';
|
import { TOPIC } from '../lib/waku_relay';
|
||||||
|
|
||||||
import { existsAsync, mkdirAsync, openAsync } from './async_fs';
|
import { existsAsync, mkdirAsync, openAsync } from './async_fs';
|
||||||
@ -132,7 +132,7 @@ export class NimWaku {
|
|||||||
return res.result;
|
return res.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendMessage(message: Message) {
|
async sendMessage(message: WakuMessage) {
|
||||||
this.checkProcess();
|
this.checkProcess();
|
||||||
|
|
||||||
if (!message.payload) {
|
if (!message.payload) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user