Skip to content

Commit

Permalink
Make Message#fromEncoded/fromEncodedArray async
Browse files Browse the repository at this point in the history
Preparation for #1293 (making ICipher.decrypt asynchronous). This is an
unavoidable public API change.

Note that I haven’t bothered exposing a callbacks version of these
methods, since as of #1199 — which is also scheduled for version 2.0 —
the library will only be exposing a Promise API.
  • Loading branch information
lawrence-forooghian committed Jun 5, 2023
1 parent 346e5eb commit c19b688
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
16 changes: 8 additions & 8 deletions ably.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2758,17 +2758,17 @@ declare namespace Types {
*
* @param JsonObject - A `Message`-like deserialized object.
* @param channelOptions - A {@link ChannelOptions} object. If you have an encrypted channel, use this to allow the library to decrypt the data.
* @returns A `Message` object.
* @returns A promise which will be fulfilled with a `Message` object.
*/
static fromEncoded: (JsonObject: any, channelOptions?: ChannelOptions) => Message;
static fromEncoded: (JsonObject: any, channelOptions?: ChannelOptions) => Promise<Message>;
/**
* A static factory method to create an array of `Message` objects from an array of deserialized Message-like object encoded using Ably's wire protocol.
*
* @param JsonArray - An array of `Message`-like deserialized objects.
* @param channelOptions - A {@link ChannelOptions} object. If you have an encrypted channel, use this to allow the library to decrypt the data.
* @returns An array of {@link Message} objects.
* @returns A promise which will be fulfilled with an array of {@link Message} objects.
*/
static fromEncodedArray: (JsonArray: any[], channelOptions?: ChannelOptions) => Message[];
static fromEncodedArray: (JsonArray: any[], channelOptions?: ChannelOptions) => Promise<Message[]>;
/**
* The client ID of the publisher of this message.
*/
Expand Down Expand Up @@ -2812,17 +2812,17 @@ declare namespace Types {
*
* @param JsonObject - A `Message`-like deserialized object.
* @param channelOptions - A {@link ChannelOptions} object. If you have an encrypted channel, use this to allow the library to decrypt the data.
* @returns A `Message` object.
* @returns A promise which will be fulfilled with a `Message` object.
*/
fromEncoded: (JsonObject: any, channelOptions?: ChannelOptions) => Message;
fromEncoded: (JsonObject: any, channelOptions?: ChannelOptions) => Promise<Message>;
/**
* A static factory method to create an array of `Message` objects from an array of deserialized Message-like object encoded using Ably's wire protocol.
*
* @param JsonArray - An array of `Message`-like deserialized objects.
* @param channelOptions - A {@link ChannelOptions} object. If you have an encrypted channel, use this to allow the library to decrypt the data.
* @returns An array of {@link Message} objects.
* @returns A promise which will be fulfilled with an array of {@link Message} objects.
*/
fromEncodedArray: (JsonArray: any[], channelOptions?: ChannelOptions) => Message[];
fromEncodedArray: (JsonArray: any[], channelOptions?: ChannelOptions) => Promise<Message[]>;
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/common/lib/types/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ class Message {
return result;
}

static fromEncoded(encoded: unknown, inputOptions?: API.Types.ChannelOptions): Message {
static async fromEncoded(encoded: unknown, inputOptions?: API.Types.ChannelOptions): Promise<Message> {
const msg = Message.fromValues(encoded);
const options = normalizeCipherOptions(inputOptions ?? null);
/* if decoding fails at any point, catch and return the message decoded to
Expand All @@ -345,10 +345,12 @@ class Message {
return msg;
}

static fromEncodedArray(encodedArray: Array<unknown>, options?: API.Types.ChannelOptions): Message[] {
return encodedArray.map(function (encoded) {
return Message.fromEncoded(encoded, options);
});
static async fromEncodedArray(encodedArray: Array<unknown>, options?: API.Types.ChannelOptions): Promise<Message[]> {
return Promise.all(
encodedArray.map(function (encoded) {
return Message.fromEncoded(encoded, options);
})
);
}

/* This should be called on encode()d (and encrypt()d) Messages (as it
Expand Down
20 changes: 10 additions & 10 deletions test/realtime/crypto.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
return;
}

loadTestData(testResourcesPath + filename, function (err, testData) {
loadTestData(testResourcesPath + filename, async function (err, testData) {
if (err) {
done(new Error('Unable to get test assets; err = ' + displayError(err)));
return;
Expand All @@ -71,11 +71,11 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
var item = testData.items[i];

/* read messages from test data and decode (ie remove any base64 encoding). */
var createTestMessage = function () {
return Message.fromEncoded(item.encoded);
var createTestMessage = async function () {
return await Message.fromEncoded(item.encoded);
};

var encryptedMessage = Message.fromEncoded(item.encrypted);
var encryptedMessage = await Message.fromEncoded(item.encrypted);

var runTest = function (testMessage) {
/* reset channel cipher, to ensure it uses the given iv */
Expand All @@ -84,13 +84,13 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
};

// Run the test with the message’s data as-is.
runTest(createTestMessage());
runTest(await createTestMessage());

if (testPlaintextVariants) {
var testMessage = createTestMessage();
var testMessage = await createTestMessage();
if (BufferUtils.isBuffer(testMessage.data) && !(testMessage.data instanceof ArrayBuffer)) {
// Now, check that we can also handle an ArrayBuffer plaintext.
var testMessageWithArrayBufferData = createTestMessage();
var testMessageWithArrayBufferData = await createTestMessage();
testMessageWithArrayBufferData.data = BufferUtils.toArrayBuffer(testMessageWithArrayBufferData.data);
runTest(testMessageWithArrayBufferData);
}
Expand Down Expand Up @@ -305,7 +305,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async
return;
}

loadTestData(testResourcesPath + 'crypto-data-256.json', function (err, testData) {
loadTestData(testResourcesPath + 'crypto-data-256.json', async function (err, testData) {
if (err) {
done(err);
return;
Expand All @@ -315,8 +315,8 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async

for (var i = 0; i < testData.items.length; i++) {
var item = testData.items[i];
var testMessage = Message.fromEncoded(item.encoded);
var decryptedMessage = Message.fromEncoded(item.encrypted, { cipher: { key: key, iv: iv } });
var testMessage = await Message.fromEncoded(item.encoded);
var decryptedMessage = await Message.fromEncoded(item.encrypted, { cipher: { key: key, iv: iv } });
testMessageEquality(done, testMessage, decryptedMessage);
}
done();
Expand Down

0 comments on commit c19b688

Please sign in to comment.