From e167f4fba4a86628d8a0e67ead52c566d51a588b Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Wed, 24 Mar 2021 16:31:54 +1100 Subject: [PATCH] Decode payload to utf8 string --- src/lib/waku_message.spec.ts | 11 +++++++++++ src/lib/waku_message.ts | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/lib/waku_message.spec.ts b/src/lib/waku_message.spec.ts index 5d82f8db8c..5865f7fe18 100644 --- a/src/lib/waku_message.spec.ts +++ b/src/lib/waku_message.spec.ts @@ -14,4 +14,15 @@ describe('Waku Message', function () { }) ); }); + + it('Payload to utf-8', function () { + fc.assert( + fc.property(fc.string(), (s) => { + const msg = Message.fromUtf8String(s); + const utf8 = msg.utf8Payload(); + + return utf8 === s; + }) + ); + }); }); diff --git a/src/lib/waku_message.ts b/src/lib/waku_message.ts index f7734d4ba1..1b46978fb0 100644 --- a/src/lib/waku_message.ts +++ b/src/lib/waku_message.ts @@ -37,6 +37,18 @@ export class Message { }).finish(); } + utf8Payload(): string { + if (!this.payload) { + return ''; + } + + return Array.from(this.payload) + .map((char) => { + return String.fromCharCode(char); + }) + .join(''); + } + // Purely for tests purposes. // We do consider protobuf field when checking equality // As the content is held by the other fields.