mirror of https://github.com/status-im/js-waku.git
Enable test encoding and decoding of WakuMessage.timestamp
This commit is contained in:
parent
3055881c57
commit
9e64eec2a6
|
@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Enable access to `WakuMessage.timestamp`.
|
||||||
|
|
||||||
## [0.5.0] - 2021-05-21
|
## [0.5.0] - 2021-05-21
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
|
@ -11,35 +11,33 @@ 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.
|
||||||
* @param utf8
|
|
||||||
* @param contentTopic
|
|
||||||
* @returns {WakuMessage}
|
|
||||||
*/
|
*/
|
||||||
static fromUtf8String(
|
static fromUtf8String(
|
||||||
utf8: string,
|
utf8: string,
|
||||||
contentTopic: string = DefaultContentTopic
|
contentTopic: string = DefaultContentTopic,
|
||||||
|
timestamp: Date = new Date()
|
||||||
): WakuMessage {
|
): WakuMessage {
|
||||||
const payload = Buffer.from(utf8, 'utf-8');
|
const payload = Buffer.from(utf8, 'utf-8');
|
||||||
return new WakuMessage({
|
return new WakuMessage({
|
||||||
payload,
|
payload,
|
||||||
version: DefaultVersion,
|
version: DefaultVersion,
|
||||||
contentTopic,
|
contentTopic,
|
||||||
|
timestamp: timestamp.valueOf() / 1000,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create Message with a byte array as payload
|
* Create Message with a byte array as payload.
|
||||||
* @param payload
|
|
||||||
* @param contentTopic
|
|
||||||
* @returns {WakuMessage}
|
|
||||||
*/
|
*/
|
||||||
static fromBytes(
|
static fromBytes(
|
||||||
payload: Uint8Array,
|
payload: Uint8Array,
|
||||||
contentTopic: string = DefaultContentTopic
|
contentTopic: string = DefaultContentTopic,
|
||||||
|
timestamp: Date = new Date()
|
||||||
): WakuMessage {
|
): WakuMessage {
|
||||||
return new WakuMessage({
|
return new WakuMessage({
|
||||||
payload,
|
payload,
|
||||||
|
timestamp: timestamp.valueOf() / 1000,
|
||||||
version: DefaultVersion,
|
version: DefaultVersion,
|
||||||
contentTopic,
|
contentTopic,
|
||||||
});
|
});
|
||||||
|
@ -77,4 +75,11 @@ export class WakuMessage {
|
||||||
get version(): number | undefined {
|
get version(): number | undefined {
|
||||||
return this.proto.version;
|
return this.proto.version;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get timestamp(): Date | undefined {
|
||||||
|
if (this.proto.timestamp) {
|
||||||
|
return new Date(this.proto.timestamp * 1000);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,7 +80,12 @@ describe('Waku Relay', () => {
|
||||||
this.timeout(10000);
|
this.timeout(10000);
|
||||||
|
|
||||||
const messageText = 'JS to JS communication works';
|
const messageText = 'JS to JS communication works';
|
||||||
const message = WakuMessage.fromUtf8String(messageText);
|
const messageTimestamp = new Date('1995-12-17T03:24:00');
|
||||||
|
const message = WakuMessage.fromUtf8String(
|
||||||
|
messageText,
|
||||||
|
undefined,
|
||||||
|
messageTimestamp
|
||||||
|
);
|
||||||
|
|
||||||
const receivedMsgPromise: Promise<WakuMessage> = new Promise(
|
const receivedMsgPromise: Promise<WakuMessage> = new Promise(
|
||||||
(resolve) => {
|
(resolve) => {
|
||||||
|
@ -95,6 +100,9 @@ describe('Waku Relay', () => {
|
||||||
expect(receivedMsg.contentTopic).to.eq(message.contentTopic);
|
expect(receivedMsg.contentTopic).to.eq(message.contentTopic);
|
||||||
expect(receivedMsg.version).to.eq(message.version);
|
expect(receivedMsg.version).to.eq(message.version);
|
||||||
expect(receivedMsg.payloadAsUtf8).to.eq(messageText);
|
expect(receivedMsg.payloadAsUtf8).to.eq(messageText);
|
||||||
|
expect(receivedMsg.timestamp?.valueOf()).to.eq(
|
||||||
|
messageTimestamp.valueOf()
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Filter on content topics', async function () {
|
it('Filter on content topics', async function () {
|
||||||
|
|
Loading…
Reference in New Issue