signal-desktop/libtextsecure/key_worker.js

64 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2015-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2018-07-21 21:51:20 +00:00
/* global window, postMessage, textsecure, close */
/* eslint-disable more/no-then, no-global-assign, no-restricted-globals, no-unused-vars */
/*
* Load this script in a Web Worker to generate new prekeys without
* tying up the main thread.
* https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
*
* Because workers don't have access to the window or localStorage, we
* create our own version that proxies back to the caller for actual
* storage.
*
* Example usage:
*
var myWorker = new Worker('/js/key_worker.js');
myWorker.onmessage = function(e) {
switch(e.data.method) {
case 'set':
localStorage.setItem(e.data.key, e.data.value);
break;
case 'remove':
localStorage.removeItem(e.data.key);
break;
case 'done':
console.error(e.data.keys);
}
};
*/
2018-07-21 21:51:20 +00:00
let store = {};
window.textsecure.storage.impl = {
2018-07-21 21:51:20 +00:00
/** ***************************
*** Override Storage Routines ***
2018-07-21 21:51:20 +00:00
**************************** */
put(key, value) {
if (value === undefined) throw new Error('Tried to store undefined');
store[key] = value;
2018-07-21 21:51:20 +00:00
postMessage({ method: 'set', key, value });
},
2018-07-21 21:51:20 +00:00
get(key, defaultValue) {
if (key in store) {
return store[key];
}
2018-07-21 21:51:20 +00:00
return defaultValue;
},
2018-07-21 21:51:20 +00:00
remove(key) {
delete store[key];
2018-07-21 21:51:20 +00:00
postMessage({ method: 'remove', key });
},
};
2018-07-21 21:51:20 +00:00
// eslint-disable-next-line no-undef
onmessage = e => {
store = e.data;
2018-07-21 21:51:20 +00:00
textsecure.protocol_wrapper.generateKeys().then(keys => {
postMessage({ method: 'done', keys });
close();
});
};