Move optional parameters to a single `Options` object.

This commit is contained in:
Franck Royer 2021-07-06 15:29:02 +10:00
parent 381333347e
commit 2266f31d30
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
7 changed files with 38 additions and 30 deletions

View File

@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **Breaking**: Auto select peer if none provided for store and light push protocols.
- Upgrade to `libp2p@0.31.7` and `libp2p-gossipsub@0.10.0` to avoid `TextEncoder` errors in ReactJS tests.
- Disable keep alive by default as latest nim-waku release does not support ping protocol.
- **Breaking**: Optional parameters for `WakuMessage.fromBytes` and `WakuMessage.fromUtf8String` are now passed in a single `Options` object.
### Fixed
- Disable `keepAlive` if set to `0`.

View File

@ -104,7 +104,9 @@ export default async function startChat(): Promise<void> {
rl.prompt();
const chatMessage = ChatMessage.fromUtf8String(new Date(), nick, line);
const msg = WakuMessage.fromBytes(chatMessage.encode(), ChatContentTopic);
const msg = WakuMessage.fromBytes(chatMessage.encode(), {
contentTopic: ChatContentTopic,
});
if (opts.lightPush) {
await waku.lightPush.push(msg);
} else {

View File

@ -60,5 +60,7 @@ function encodePublicKeyWakuMessage(
publicKeyMessage: PublicKeyMessage
): WakuMessage {
const payload = publicKeyMessage.encode();
return WakuMessage.fromBytes(payload, PublicKeyContentTopic);
return WakuMessage.fromBytes(payload, {
contentTopic: PublicKeyContentTopic,
});
}

View File

@ -117,7 +117,9 @@ async function encodeEncryptedWakuMessage(
};
const payload = encode(directMsg);
return WakuMessage.fromBytes(payload, DirectMessageContentTopic);
return WakuMessage.fromBytes(payload, {
contentTopic: DirectMessageContentTopic,
});
}
function sendMessage(

View File

@ -55,11 +55,10 @@ async function handleMessage(
} else {
const timestamp = new Date();
const chatMessage = ChatMessage.fromUtf8String(timestamp, nick, message);
const wakuMsg = WakuMessage.fromBytes(
chatMessage.encode(),
ChatContentTopic,
timestamp
);
const wakuMsg = WakuMessage.fromBytes(chatMessage.encode(), {
contentTopic: ChatContentTopic,
timestamp,
});
return messageSender(wakuMsg);
}
}

View File

@ -7,29 +7,30 @@ import * as proto from '../../proto/waku/v2/message';
export const DefaultContentTopic = '/waku/2/default-content/proto';
const DefaultVersion = 0;
export interface Options {
contentTopic?: string;
timestamp?: Date;
}
export class WakuMessage {
public constructor(public proto: proto.WakuMessage) {}
/**
* Create Message with a utf-8 string as payload.
*/
static fromUtf8String(
utf8: string,
contentTopic: string = DefaultContentTopic,
timestamp: Date = new Date()
): WakuMessage {
static fromUtf8String(utf8: string, opts?: Options): WakuMessage {
const payload = Buffer.from(utf8, 'utf-8');
return WakuMessage.fromBytes(payload, contentTopic, timestamp);
return WakuMessage.fromBytes(payload, opts);
}
/**
* Create Message with a byte array as payload.
*/
static fromBytes(
payload: Uint8Array,
contentTopic: string = DefaultContentTopic,
timestamp: Date = new Date()
): WakuMessage {
static fromBytes(payload: Uint8Array, opts?: Options): WakuMessage {
const { timestamp, contentTopic } = Object.assign(
{ timestamp: new Date(), contentTopic: DefaultContentTopic },
opts ? opts : {}
);
return new WakuMessage({
payload,
timestamp: timestamp.valueOf() / 1000,

View File

@ -79,11 +79,9 @@ describe('Waku Relay', () => {
const messageText = 'JS to JS communication works';
const messageTimestamp = new Date('1995-12-17T03:24:00');
const message = WakuMessage.fromUtf8String(
messageText,
undefined,
messageTimestamp
);
const message = WakuMessage.fromUtf8String(messageText, {
timestamp: messageTimestamp,
});
const receivedMsgPromise: Promise<WakuMessage> = new Promise(
(resolve) => {
@ -108,8 +106,12 @@ describe('Waku Relay', () => {
const fooMessageText = 'Published on content topic foo';
const barMessageText = 'Published on content topic bar';
const fooMessage = WakuMessage.fromUtf8String(fooMessageText, 'foo');
const barMessage = WakuMessage.fromUtf8String(barMessageText, 'bar');
const fooMessage = WakuMessage.fromUtf8String(fooMessageText, {
contentTopic: 'foo',
});
const barMessage = WakuMessage.fromUtf8String(barMessageText, {
contentTopic: 'bar',
});
const receivedBarMsgPromise: Promise<WakuMessage> = new Promise(
(resolve) => {
@ -144,10 +146,9 @@ describe('Waku Relay', () => {
const messageText =
'Published on content topic with added then deleted observer';
const message = WakuMessage.fromUtf8String(
messageText,
'added-then-deleted-observer'
);
const message = WakuMessage.fromUtf8String(messageText, {
contentTopic: 'added-then-deleted-observer',
});
// The promise **fails** if we receive a message on this observer.
const receivedMsgPromise: Promise<WakuMessage> = new Promise(