From 911bc63c6723100ca9a0f1cb05362ad842d84442 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Thu, 16 Aug 2018 09:50:56 -0700 Subject: [PATCH] Move all arrayBuffer<->base64 functions to be async --- js/modules/migrate_to_sql.js | 34 +++++++++++++++-------------- libtextsecure/message_receiver.js | 36 +++++++++++++++---------------- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/js/modules/migrate_to_sql.js b/js/modules/migrate_to_sql.js index 51b897abd..1481d3560 100644 --- a/js/modules/migrate_to_sql.js +++ b/js/modules/migrate_to_sql.js @@ -1,6 +1,6 @@ /* global window, IDBKeyRange */ -const { includes, isFunction, isString, last, forEach } = require('lodash'); +const { includes, isFunction, isString, last, map } = require('lodash'); const { saveMessages, _removeMessages, @@ -83,23 +83,25 @@ async function migrateToSQL({ const status = await migrateStoreToSQLite({ db, save: async array => { - forEach(array, item => { - // In the new database, we can't store ArrayBuffers, so we turn these two fields - // into strings like MessageReceiver now does before save. + await Promise.all( + map(array, async item => { + // In the new database, we can't store ArrayBuffers, so we turn these two + // fields into strings like MessageReceiver now does before save. - // Need to set it to version two, since we're using Base64 strings now - // eslint-disable-next-line no-param-reassign - item.version = 2; - - if (item.envelope) { - // eslint-disable-next-line no-param-reassign - item.envelope = arrayBufferToString(item.envelope); - } - if (item.decrypted) { + // Need to set it to version two, since we're using Base64 strings now // eslint-disable-next-line no-param-reassign - item.decrypted = arrayBufferToString(item.decrypted); - } - }); + item.version = 2; + + if (item.envelope) { + // eslint-disable-next-line no-param-reassign + item.envelope = await arrayBufferToString(item.envelope); + } + if (item.decrypted) { + // eslint-disable-next-line no-param-reassign + item.decrypted = await arrayBufferToString(item.decrypted); + } + }) + ); await saveUnprocesseds(array); }, remove: removeUnprocessed, diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 897dd6eaa..ba9eb66af 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -32,13 +32,13 @@ function MessageReceiver(username, password, signalingKey, options = {}) { } MessageReceiver.stringToArrayBuffer = string => - dcodeIO.ByteBuffer.wrap(string, 'binary').toArrayBuffer(); + Promise.resolve(dcodeIO.ByteBuffer.wrap(string, 'binary').toArrayBuffer()); MessageReceiver.arrayBufferToString = arrayBuffer => - dcodeIO.ByteBuffer.wrap(arrayBuffer).toString('binary'); + Promise.resolve(dcodeIO.ByteBuffer.wrap(arrayBuffer).toString('binary')); MessageReceiver.stringToArrayBufferBase64 = string => - dcodeIO.ByteBuffer.wrap(string, 'base64').toArrayBuffer(); + Promise.resolve(dcodeIO.ByteBuffer.wrap(string, 'base64').toArrayBuffer()); MessageReceiver.arrayBufferToStringBase64 = arrayBuffer => - dcodeIO.ByteBuffer.wrap(arrayBuffer).toString('base64'); + Promise.resolve(dcodeIO.ByteBuffer.wrap(arrayBuffer).toString('base64')); MessageReceiver.prototype = new textsecure.EventTarget(); MessageReceiver.prototype.extend({ @@ -271,13 +271,8 @@ MessageReceiver.prototype.extend({ async queueAllCached() { const items = await this.getAllFromCache(); for (let i = 0, max = items.length; i < max; i += 1) { - if (i > 0 && i % 20 === 0) { - window.log.info('queueAllCached: Giving event loop a rest'); - // eslint-disable-next-line no-await-in-loop - await new Promise(resolve => setTimeout(resolve, 2000)); - } - - this.queueCached(items[i]); + // eslint-disable-next-line no-await-in-loop + await this.queueCached(items[i]); } }, async queueCached(item) { @@ -285,13 +280,13 @@ MessageReceiver.prototype.extend({ let envelopePlaintext = item.envelope; if (item.version === 2) { - envelopePlaintext = MessageReceiver.stringToArrayBufferBase64( + envelopePlaintext = await MessageReceiver.stringToArrayBufferBase64( envelopePlaintext ); } if (typeof envelopePlaintext === 'string') { - envelopePlaintext = MessageReceiver.stringToArrayBuffer( + envelopePlaintext = await MessageReceiver.stringToArrayBuffer( envelopePlaintext ); } @@ -302,13 +297,13 @@ MessageReceiver.prototype.extend({ let payloadPlaintext = decrypted; if (item.version === 2) { - payloadPlaintext = MessageReceiver.stringToArrayBufferBase64( + payloadPlaintext = await MessageReceiver.stringToArrayBufferBase64( payloadPlaintext ); } if (typeof payloadPlaintext === 'string') { - payloadPlaintext = MessageReceiver.stringToArrayBuffer( + payloadPlaintext = await MessageReceiver.stringToArrayBuffer( payloadPlaintext ); } @@ -375,12 +370,12 @@ MessageReceiver.prototype.extend({ ); }); }, - addToCache(envelope, plaintext) { + async addToCache(envelope, plaintext) { const id = this.getEnvelopeId(envelope); const data = { id, version: 2, - envelope: MessageReceiver.arrayBufferToStringBase64(plaintext), + envelope: await MessageReceiver.arrayBufferToStringBase64(plaintext), timestamp: Date.now(), attempts: 1, }; @@ -399,10 +394,13 @@ MessageReceiver.prototype.extend({ if (item.get('version') === 2) { item.set( 'decrypted', - MessageReceiver.arrayBufferToStringBase64(plaintext) + await MessageReceiver.arrayBufferToStringBase64(plaintext) ); } else { - item.set('decrypted', MessageReceiver.arrayBufferToString(plaintext)); + item.set( + 'decrypted', + await MessageReceiver.arrayBufferToString(plaintext) + ); } return textsecure.storage.unprocessed.save(item.attributes);