Move all arrayBuffer<->base64 functions to be async
This commit is contained in:
parent
496ebf2a47
commit
911bc63c67
2 changed files with 35 additions and 35 deletions
|
@ -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;
|
||||
// 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) {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
item.decrypted = arrayBufferToString(item.decrypted);
|
||||
}
|
||||
});
|
||||
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,
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue