Skip to content

Commit

Permalink
Make Message.decode async
Browse files Browse the repository at this point in the history
Preparation for #1293 (making ICipher.decrypt asynchronous).
  • Loading branch information
lawrence-forooghian committed Jun 1, 2023
1 parent 23d7cf1 commit b8b78fe
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/common/lib/client/realtimechannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ class RealtimeChannel extends Channel {
for (let i = 0; i < presence.length; i++) {
try {
presenceMsg = presence[i];
PresenceMessage.decode(presenceMsg, options);
await PresenceMessage.decode(presenceMsg, options);
if (!presenceMsg.connectionId) presenceMsg.connectionId = connectionId;
if (!presenceMsg.timestamp) presenceMsg.timestamp = timestamp;
if (!presenceMsg.id) presenceMsg.id = id + ':' + i;
Expand Down Expand Up @@ -711,7 +711,7 @@ class RealtimeChannel extends Channel {
for (let i = 0; i < messages.length; i++) {
const msg = messages[i];
try {
Message.decode(msg, this._decodingContext);
await Message.decode(msg, this._decodingContext);
} catch (e) {
/* decrypt failed .. the most likely cause is that we have the wrong key */
Logger.logAction(Logger.LOG_ERROR, 'RealtimeChannel.processMessage()', (e as Error).toString());
Expand Down
8 changes: 4 additions & 4 deletions src/common/lib/types/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ class Message {

static serialize = Utils.encodeBody;

static decode(
static async decode(
message: Message | PresenceMessage,
inputContext: CipherOptions | EncodingDecodingContext | ChannelOptions
): void {
): Promise<void> {
const context = normaliseContext(inputContext);

let lastPayload = message.data;
Expand Down Expand Up @@ -313,7 +313,7 @@ class Message {
for (let i = 0; i < body.length; i++) {
const msg = (body[i] = Message.fromValues(body[i]));
try {
Message.decode(msg, options);
await Message.decode(msg, options);
} catch (e) {
Logger.logAction(Logger.LOG_ERROR, 'Message.fromResponseBody()', (e as Error).toString());
}
Expand All @@ -338,7 +338,7 @@ class Message {
/* if decoding fails at any point, catch and return the message decoded to
* the fullest extent possible */
try {
Message.decode(msg, options);
await Message.decode(msg, options);
} catch (e) {
Logger.logAction(Logger.LOG_ERROR, 'Message.fromEncoded()', (e as Error).toString());
}
Expand Down
4 changes: 2 additions & 2 deletions src/common/lib/types/presencemessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class PresenceMessage {
for (let i = 0; i < body.length; i++) {
const msg = (messages[i] = PresenceMessage.fromValues(body[i], true));
try {
PresenceMessage.decode(msg, options);
await PresenceMessage.decode(msg, options);
} catch (e) {
Logger.logAction(Logger.LOG_ERROR, 'PresenceMessage.fromResponseBody()', (e as Error).toString());
}
Expand All @@ -142,7 +142,7 @@ class PresenceMessage {
/* if decoding fails at any point, catch and return the message decoded to
* the fullest extent possible */
try {
PresenceMessage.decode(msg, options ?? {});
await PresenceMessage.decode(msg, options ?? {});
} catch (e) {
Logger.logAction(Logger.LOG_ERROR, 'PresenceMessage.fromEncoded()', (e as Error).toString());
}
Expand Down
8 changes: 4 additions & 4 deletions test/realtime/crypto.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
'decrypt_message_128',
2,
false,
function (channelOpts, testMessage, encryptedMessage) {
async function (channelOpts, testMessage, encryptedMessage) {
/* decrypt encrypted message; decode() also to handle data that is not string or buffer */
Message.decode(encryptedMessage, channelOpts);
await Message.decode(encryptedMessage, channelOpts);
/* compare */
testMessageEquality(done, testMessage, encryptedMessage);
}
Expand All @@ -290,9 +290,9 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
'decrypt_message_256',
2,
false,
function (channelOpts, testMessage, encryptedMessage) {
async function (channelOpts, testMessage, encryptedMessage) {
/* decrypt encrypted message; decode() also to handle data that is not string or buffer */
Message.decode(encryptedMessage, channelOpts);
await Message.decode(encryptedMessage, channelOpts);
/* compare */
testMessageEquality(done, testMessage, encryptedMessage);
}
Expand Down

0 comments on commit b8b78fe

Please sign in to comment.