Enable test encoding and decoding of WakuMessage.timestamp

This commit is contained in:
Franck Royer 2021-05-28 16:00:34 +10:00
parent 3055881c57
commit 9e64eec2a6
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 27 additions and 11 deletions

View File

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

View File

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

View File

@ -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 () {