Skip to content

Commit

Permalink
Move all arrayBuffer<->base64 functions to be async
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal committed Aug 17, 2018
1 parent 496ebf2 commit 911bc63
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
34 changes: 18 additions & 16 deletions 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,
Expand Down Expand Up @@ -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,
Expand Down
36 changes: 17 additions & 19 deletions libtextsecure/message_receiver.js
Expand Up @@ -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({
Expand Down Expand Up @@ -271,27 +271,22 @@ 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) {
try {
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
);
}
Expand All @@ -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
);
}
Expand Down Expand Up @@ -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,
};
Expand All @@ -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);
Expand Down

0 comments on commit 911bc63

Please sign in to comment.