From 9e64eec2a61d9778838d0ebdf6ee07d213b33bf4 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 28 May 2021 16:00:34 +1000 Subject: [PATCH] Enable test encoding and decoding of WakuMessage.timestamp --- CHANGELOG.md | 3 +++ src/lib/waku_message.ts | 25 +++++++++++++++---------- src/lib/waku_relay/index.spec.ts | 10 +++++++++- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c274a5375e..45acd7180f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- Enable access to `WakuMessage.timestamp`. + ## [0.5.0] - 2021-05-21 ### Added diff --git a/src/lib/waku_message.ts b/src/lib/waku_message.ts index 407a093fea..8d7936d877 100644 --- a/src/lib/waku_message.ts +++ b/src/lib/waku_message.ts @@ -11,35 +11,33 @@ export class WakuMessage { public constructor(public proto: proto.WakuMessage) {} /** - * Create Message with a utf-8 string as payload - * @param utf8 - * @param contentTopic - * @returns {WakuMessage} + * Create Message with a utf-8 string as payload. */ static fromUtf8String( utf8: string, - contentTopic: string = DefaultContentTopic + contentTopic: string = DefaultContentTopic, + timestamp: Date = new Date() ): WakuMessage { const payload = Buffer.from(utf8, 'utf-8'); return new WakuMessage({ payload, version: DefaultVersion, contentTopic, + timestamp: timestamp.valueOf() / 1000, }); } /** - * Create Message with a byte array as payload - * @param payload - * @param contentTopic - * @returns {WakuMessage} + * Create Message with a byte array as payload. */ static fromBytes( payload: Uint8Array, - contentTopic: string = DefaultContentTopic + contentTopic: string = DefaultContentTopic, + timestamp: Date = new Date() ): WakuMessage { return new WakuMessage({ payload, + timestamp: timestamp.valueOf() / 1000, version: DefaultVersion, contentTopic, }); @@ -77,4 +75,11 @@ export class WakuMessage { get version(): number | undefined { return this.proto.version; } + + get timestamp(): Date | undefined { + if (this.proto.timestamp) { + return new Date(this.proto.timestamp * 1000); + } + return; + } } diff --git a/src/lib/waku_relay/index.spec.ts b/src/lib/waku_relay/index.spec.ts index 917b41f130..d05eef324f 100644 --- a/src/lib/waku_relay/index.spec.ts +++ b/src/lib/waku_relay/index.spec.ts @@ -80,7 +80,12 @@ describe('Waku Relay', () => { this.timeout(10000); 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 = new Promise( (resolve) => { @@ -95,6 +100,9 @@ describe('Waku Relay', () => { expect(receivedMsg.contentTopic).to.eq(message.contentTopic); expect(receivedMsg.version).to.eq(message.version); expect(receivedMsg.payloadAsUtf8).to.eq(messageText); + expect(receivedMsg.timestamp?.valueOf()).to.eq( + messageTimestamp.valueOf() + ); }); it('Filter on content topics', async function () {