mirror of https://github.com/waku-org/js-waku.git
Move optional parameters to a single `Options` object.
This commit is contained in:
parent
381333347e
commit
2266f31d30
|
@ -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.
|
- **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.
|
- 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.
|
- 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
|
### Fixed
|
||||||
- Disable `keepAlive` if set to `0`.
|
- Disable `keepAlive` if set to `0`.
|
||||||
|
|
|
@ -104,7 +104,9 @@ export default async function startChat(): Promise<void> {
|
||||||
rl.prompt();
|
rl.prompt();
|
||||||
const chatMessage = ChatMessage.fromUtf8String(new Date(), nick, line);
|
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) {
|
if (opts.lightPush) {
|
||||||
await waku.lightPush.push(msg);
|
await waku.lightPush.push(msg);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -60,5 +60,7 @@ function encodePublicKeyWakuMessage(
|
||||||
publicKeyMessage: PublicKeyMessage
|
publicKeyMessage: PublicKeyMessage
|
||||||
): WakuMessage {
|
): WakuMessage {
|
||||||
const payload = publicKeyMessage.encode();
|
const payload = publicKeyMessage.encode();
|
||||||
return WakuMessage.fromBytes(payload, PublicKeyContentTopic);
|
return WakuMessage.fromBytes(payload, {
|
||||||
|
contentTopic: PublicKeyContentTopic,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,7 +117,9 @@ async function encodeEncryptedWakuMessage(
|
||||||
};
|
};
|
||||||
|
|
||||||
const payload = encode(directMsg);
|
const payload = encode(directMsg);
|
||||||
return WakuMessage.fromBytes(payload, DirectMessageContentTopic);
|
return WakuMessage.fromBytes(payload, {
|
||||||
|
contentTopic: DirectMessageContentTopic,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendMessage(
|
function sendMessage(
|
||||||
|
|
|
@ -55,11 +55,10 @@ async function handleMessage(
|
||||||
} else {
|
} else {
|
||||||
const timestamp = new Date();
|
const timestamp = new Date();
|
||||||
const chatMessage = ChatMessage.fromUtf8String(timestamp, nick, message);
|
const chatMessage = ChatMessage.fromUtf8String(timestamp, nick, message);
|
||||||
const wakuMsg = WakuMessage.fromBytes(
|
const wakuMsg = WakuMessage.fromBytes(chatMessage.encode(), {
|
||||||
chatMessage.encode(),
|
contentTopic: ChatContentTopic,
|
||||||
ChatContentTopic,
|
timestamp,
|
||||||
timestamp
|
});
|
||||||
);
|
|
||||||
return messageSender(wakuMsg);
|
return messageSender(wakuMsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,29 +7,30 @@ import * as proto from '../../proto/waku/v2/message';
|
||||||
export const DefaultContentTopic = '/waku/2/default-content/proto';
|
export const DefaultContentTopic = '/waku/2/default-content/proto';
|
||||||
const DefaultVersion = 0;
|
const DefaultVersion = 0;
|
||||||
|
|
||||||
|
export interface Options {
|
||||||
|
contentTopic?: string;
|
||||||
|
timestamp?: Date;
|
||||||
|
}
|
||||||
|
|
||||||
export class WakuMessage {
|
export class WakuMessage {
|
||||||
public constructor(public proto: proto.WakuMessage) {}
|
public constructor(public proto: proto.WakuMessage) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create Message with a utf-8 string as payload.
|
* Create Message with a utf-8 string as payload.
|
||||||
*/
|
*/
|
||||||
static fromUtf8String(
|
static fromUtf8String(utf8: string, opts?: Options): WakuMessage {
|
||||||
utf8: string,
|
|
||||||
contentTopic: string = DefaultContentTopic,
|
|
||||||
timestamp: Date = new Date()
|
|
||||||
): WakuMessage {
|
|
||||||
const payload = Buffer.from(utf8, 'utf-8');
|
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.
|
* Create Message with a byte array as payload.
|
||||||
*/
|
*/
|
||||||
static fromBytes(
|
static fromBytes(payload: Uint8Array, opts?: Options): WakuMessage {
|
||||||
payload: Uint8Array,
|
const { timestamp, contentTopic } = Object.assign(
|
||||||
contentTopic: string = DefaultContentTopic,
|
{ timestamp: new Date(), contentTopic: DefaultContentTopic },
|
||||||
timestamp: Date = new Date()
|
opts ? opts : {}
|
||||||
): WakuMessage {
|
);
|
||||||
return new WakuMessage({
|
return new WakuMessage({
|
||||||
payload,
|
payload,
|
||||||
timestamp: timestamp.valueOf() / 1000,
|
timestamp: timestamp.valueOf() / 1000,
|
||||||
|
|
|
@ -79,11 +79,9 @@ describe('Waku Relay', () => {
|
||||||
|
|
||||||
const messageText = 'JS to JS communication works';
|
const messageText = 'JS to JS communication works';
|
||||||
const messageTimestamp = new Date('1995-12-17T03:24:00');
|
const messageTimestamp = new Date('1995-12-17T03:24:00');
|
||||||
const message = WakuMessage.fromUtf8String(
|
const message = WakuMessage.fromUtf8String(messageText, {
|
||||||
messageText,
|
timestamp: messageTimestamp,
|
||||||
undefined,
|
});
|
||||||
messageTimestamp
|
|
||||||
);
|
|
||||||
|
|
||||||
const receivedMsgPromise: Promise<WakuMessage> = new Promise(
|
const receivedMsgPromise: Promise<WakuMessage> = new Promise(
|
||||||
(resolve) => {
|
(resolve) => {
|
||||||
|
@ -108,8 +106,12 @@ describe('Waku Relay', () => {
|
||||||
|
|
||||||
const fooMessageText = 'Published on content topic foo';
|
const fooMessageText = 'Published on content topic foo';
|
||||||
const barMessageText = 'Published on content topic bar';
|
const barMessageText = 'Published on content topic bar';
|
||||||
const fooMessage = WakuMessage.fromUtf8String(fooMessageText, 'foo');
|
const fooMessage = WakuMessage.fromUtf8String(fooMessageText, {
|
||||||
const barMessage = WakuMessage.fromUtf8String(barMessageText, 'bar');
|
contentTopic: 'foo',
|
||||||
|
});
|
||||||
|
const barMessage = WakuMessage.fromUtf8String(barMessageText, {
|
||||||
|
contentTopic: 'bar',
|
||||||
|
});
|
||||||
|
|
||||||
const receivedBarMsgPromise: Promise<WakuMessage> = new Promise(
|
const receivedBarMsgPromise: Promise<WakuMessage> = new Promise(
|
||||||
(resolve) => {
|
(resolve) => {
|
||||||
|
@ -144,10 +146,9 @@ describe('Waku Relay', () => {
|
||||||
|
|
||||||
const messageText =
|
const messageText =
|
||||||
'Published on content topic with added then deleted observer';
|
'Published on content topic with added then deleted observer';
|
||||||
const message = WakuMessage.fromUtf8String(
|
const message = WakuMessage.fromUtf8String(messageText, {
|
||||||
messageText,
|
contentTopic: 'added-then-deleted-observer',
|
||||||
'added-then-deleted-observer'
|
});
|
||||||
);
|
|
||||||
|
|
||||||
// The promise **fails** if we receive a message on this observer.
|
// The promise **fails** if we receive a message on this observer.
|
||||||
const receivedMsgPromise: Promise<WakuMessage> = new Promise(
|
const receivedMsgPromise: Promise<WakuMessage> = new Promise(
|
||||||
|
|
Loading…
Reference in New Issue