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 */
|
/* global window, IDBKeyRange */
|
||||||
|
|
||||||
const { includes, isFunction, isString, last, forEach } = require('lodash');
|
const { includes, isFunction, isString, last, map } = require('lodash');
|
||||||
const {
|
const {
|
||||||
saveMessages,
|
saveMessages,
|
||||||
_removeMessages,
|
_removeMessages,
|
||||||
|
@ -83,23 +83,25 @@ async function migrateToSQL({
|
||||||
const status = await migrateStoreToSQLite({
|
const status = await migrateStoreToSQLite({
|
||||||
db,
|
db,
|
||||||
save: async array => {
|
save: async array => {
|
||||||
forEach(array, item => {
|
await Promise.all(
|
||||||
// In the new database, we can't store ArrayBuffers, so we turn these two fields
|
map(array, async item => {
|
||||||
// into strings like MessageReceiver now does before save.
|
// 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
|
// Need to set it to version two, since we're using Base64 strings now
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
item.version = 2;
|
item.version = 2;
|
||||||
|
|
||||||
if (item.envelope) {
|
if (item.envelope) {
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
item.envelope = arrayBufferToString(item.envelope);
|
item.envelope = await arrayBufferToString(item.envelope);
|
||||||
}
|
}
|
||||||
if (item.decrypted) {
|
if (item.decrypted) {
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
item.decrypted = arrayBufferToString(item.decrypted);
|
item.decrypted = await arrayBufferToString(item.decrypted);
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
);
|
||||||
await saveUnprocesseds(array);
|
await saveUnprocesseds(array);
|
||||||
},
|
},
|
||||||
remove: removeUnprocessed,
|
remove: removeUnprocessed,
|
||||||
|
|
|
@ -32,13 +32,13 @@ function MessageReceiver(username, password, signalingKey, options = {}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
MessageReceiver.stringToArrayBuffer = string =>
|
MessageReceiver.stringToArrayBuffer = string =>
|
||||||
dcodeIO.ByteBuffer.wrap(string, 'binary').toArrayBuffer();
|
Promise.resolve(dcodeIO.ByteBuffer.wrap(string, 'binary').toArrayBuffer());
|
||||||
MessageReceiver.arrayBufferToString = arrayBuffer =>
|
MessageReceiver.arrayBufferToString = arrayBuffer =>
|
||||||
dcodeIO.ByteBuffer.wrap(arrayBuffer).toString('binary');
|
Promise.resolve(dcodeIO.ByteBuffer.wrap(arrayBuffer).toString('binary'));
|
||||||
MessageReceiver.stringToArrayBufferBase64 = string =>
|
MessageReceiver.stringToArrayBufferBase64 = string =>
|
||||||
dcodeIO.ByteBuffer.wrap(string, 'base64').toArrayBuffer();
|
Promise.resolve(dcodeIO.ByteBuffer.wrap(string, 'base64').toArrayBuffer());
|
||||||
MessageReceiver.arrayBufferToStringBase64 = arrayBuffer =>
|
MessageReceiver.arrayBufferToStringBase64 = arrayBuffer =>
|
||||||
dcodeIO.ByteBuffer.wrap(arrayBuffer).toString('base64');
|
Promise.resolve(dcodeIO.ByteBuffer.wrap(arrayBuffer).toString('base64'));
|
||||||
|
|
||||||
MessageReceiver.prototype = new textsecure.EventTarget();
|
MessageReceiver.prototype = new textsecure.EventTarget();
|
||||||
MessageReceiver.prototype.extend({
|
MessageReceiver.prototype.extend({
|
||||||
|
@ -271,13 +271,8 @@ MessageReceiver.prototype.extend({
|
||||||
async queueAllCached() {
|
async queueAllCached() {
|
||||||
const items = await this.getAllFromCache();
|
const items = await this.getAllFromCache();
|
||||||
for (let i = 0, max = items.length; i < max; i += 1) {
|
for (let i = 0, max = items.length; i < max; i += 1) {
|
||||||
if (i > 0 && i % 20 === 0) {
|
// eslint-disable-next-line no-await-in-loop
|
||||||
window.log.info('queueAllCached: Giving event loop a rest');
|
await this.queueCached(items[i]);
|
||||||
// eslint-disable-next-line no-await-in-loop
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
||||||
}
|
|
||||||
|
|
||||||
this.queueCached(items[i]);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async queueCached(item) {
|
async queueCached(item) {
|
||||||
|
@ -285,13 +280,13 @@ MessageReceiver.prototype.extend({
|
||||||
let envelopePlaintext = item.envelope;
|
let envelopePlaintext = item.envelope;
|
||||||
|
|
||||||
if (item.version === 2) {
|
if (item.version === 2) {
|
||||||
envelopePlaintext = MessageReceiver.stringToArrayBufferBase64(
|
envelopePlaintext = await MessageReceiver.stringToArrayBufferBase64(
|
||||||
envelopePlaintext
|
envelopePlaintext
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof envelopePlaintext === 'string') {
|
if (typeof envelopePlaintext === 'string') {
|
||||||
envelopePlaintext = MessageReceiver.stringToArrayBuffer(
|
envelopePlaintext = await MessageReceiver.stringToArrayBuffer(
|
||||||
envelopePlaintext
|
envelopePlaintext
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -302,13 +297,13 @@ MessageReceiver.prototype.extend({
|
||||||
let payloadPlaintext = decrypted;
|
let payloadPlaintext = decrypted;
|
||||||
|
|
||||||
if (item.version === 2) {
|
if (item.version === 2) {
|
||||||
payloadPlaintext = MessageReceiver.stringToArrayBufferBase64(
|
payloadPlaintext = await MessageReceiver.stringToArrayBufferBase64(
|
||||||
payloadPlaintext
|
payloadPlaintext
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof payloadPlaintext === 'string') {
|
if (typeof payloadPlaintext === 'string') {
|
||||||
payloadPlaintext = MessageReceiver.stringToArrayBuffer(
|
payloadPlaintext = await MessageReceiver.stringToArrayBuffer(
|
||||||
payloadPlaintext
|
payloadPlaintext
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -375,12 +370,12 @@ MessageReceiver.prototype.extend({
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
addToCache(envelope, plaintext) {
|
async addToCache(envelope, plaintext) {
|
||||||
const id = this.getEnvelopeId(envelope);
|
const id = this.getEnvelopeId(envelope);
|
||||||
const data = {
|
const data = {
|
||||||
id,
|
id,
|
||||||
version: 2,
|
version: 2,
|
||||||
envelope: MessageReceiver.arrayBufferToStringBase64(plaintext),
|
envelope: await MessageReceiver.arrayBufferToStringBase64(plaintext),
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
attempts: 1,
|
attempts: 1,
|
||||||
};
|
};
|
||||||
|
@ -399,10 +394,13 @@ MessageReceiver.prototype.extend({
|
||||||
if (item.get('version') === 2) {
|
if (item.get('version') === 2) {
|
||||||
item.set(
|
item.set(
|
||||||
'decrypted',
|
'decrypted',
|
||||||
MessageReceiver.arrayBufferToStringBase64(plaintext)
|
await MessageReceiver.arrayBufferToStringBase64(plaintext)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
item.set('decrypted', MessageReceiver.arrayBufferToString(plaintext));
|
item.set(
|
||||||
|
'decrypted',
|
||||||
|
await MessageReceiver.arrayBufferToString(plaintext)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return textsecure.storage.unprocessed.save(item.attributes);
|
return textsecure.storage.unprocessed.save(item.attributes);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue