move storage.js to ts
parent
747bcb766c
commit
6bd835dfc3
@ -1,107 +0,0 @@
|
|||||||
/* eslint-disable more/no-then */
|
|
||||||
|
|
||||||
// eslint-disable-next-line func-names
|
|
||||||
(function() {
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
window.Whisper = window.Whisper || {};
|
|
||||||
|
|
||||||
let ready = false;
|
|
||||||
let items;
|
|
||||||
let callbacks = [];
|
|
||||||
|
|
||||||
reset();
|
|
||||||
|
|
||||||
async function put(key, value) {
|
|
||||||
if (value === undefined) {
|
|
||||||
throw new Error('Tried to store undefined');
|
|
||||||
}
|
|
||||||
if (!ready) {
|
|
||||||
window.log.warn('Called storage.put before storage is ready. key:', key);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = { id: key, value };
|
|
||||||
|
|
||||||
items[key] = data;
|
|
||||||
await window.Signal.Data.createOrUpdateItem(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
function get(key, defaultValue) {
|
|
||||||
if (!ready) {
|
|
||||||
window.log.warn('Called storage.get before storage is ready. key:', key);
|
|
||||||
}
|
|
||||||
|
|
||||||
const item = items[key];
|
|
||||||
if (!item) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return item.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function remove(key) {
|
|
||||||
if (!ready) {
|
|
||||||
window.log.warn('Called storage.get before storage is ready. key:', key);
|
|
||||||
}
|
|
||||||
|
|
||||||
delete items[key];
|
|
||||||
await window.Signal.Data.removeItemById(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
function onready(callback) {
|
|
||||||
if (ready) {
|
|
||||||
callback();
|
|
||||||
} else {
|
|
||||||
callbacks.push(callback);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function callListeners() {
|
|
||||||
if (ready) {
|
|
||||||
callbacks.forEach(callback => callback());
|
|
||||||
callbacks = [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function fetch() {
|
|
||||||
this.reset();
|
|
||||||
const array = await window.Signal.Data.getAllItems();
|
|
||||||
|
|
||||||
for (let i = 0, max = array.length; i < max; i += 1) {
|
|
||||||
const item = array[i];
|
|
||||||
const { id } = item;
|
|
||||||
items[id] = item;
|
|
||||||
}
|
|
||||||
|
|
||||||
ready = true;
|
|
||||||
callListeners();
|
|
||||||
}
|
|
||||||
|
|
||||||
function reset() {
|
|
||||||
ready = false;
|
|
||||||
items = Object.create(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
const storage = {
|
|
||||||
fetch,
|
|
||||||
put,
|
|
||||||
get,
|
|
||||||
remove,
|
|
||||||
onready,
|
|
||||||
reset,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Keep a reference to this storage system, since there are scenarios where
|
|
||||||
// we need to replace it with the legacy storage system for a while.
|
|
||||||
window.newStorage = storage;
|
|
||||||
|
|
||||||
window.textsecure = window.textsecure || {};
|
|
||||||
window.textsecure.storage = window.textsecure.storage || {};
|
|
||||||
|
|
||||||
window.installStorage = newStorage => {
|
|
||||||
window.storage = newStorage;
|
|
||||||
window.textsecure.storage.impl = newStorage;
|
|
||||||
};
|
|
||||||
|
|
||||||
window.installStorage(storage);
|
|
||||||
})();
|
|
@ -1,76 +0,0 @@
|
|||||||
/* global window, dcodeIO */
|
|
||||||
|
|
||||||
/* eslint-disable no-proto, no-restricted-syntax, guard-for-in */
|
|
||||||
|
|
||||||
window.textsecure = window.textsecure || {};
|
|
||||||
|
|
||||||
/** *******************************
|
|
||||||
*** Type conversion utilities ***
|
|
||||||
******************************** */
|
|
||||||
// Strings/arrays
|
|
||||||
// TODO: Throw all this shit in favor of consistent types
|
|
||||||
// TODO: Namespace
|
|
||||||
const StaticByteBufferProto = new dcodeIO.ByteBuffer().__proto__;
|
|
||||||
const StaticArrayBufferProto = new ArrayBuffer().__proto__;
|
|
||||||
const StaticUint8ArrayProto = new Uint8Array().__proto__;
|
|
||||||
function getString(thing) {
|
|
||||||
if (thing === Object(thing)) {
|
|
||||||
if (thing.__proto__ === StaticUint8ArrayProto) {
|
|
||||||
return String.fromCharCode.apply(null, thing);
|
|
||||||
}
|
|
||||||
if (thing.__proto__ === StaticArrayBufferProto) {
|
|
||||||
return getString(new Uint8Array(thing));
|
|
||||||
}
|
|
||||||
if (thing.__proto__ === StaticByteBufferProto) {
|
|
||||||
return thing.toString('binary');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return thing;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getStringable(thing) {
|
|
||||||
return (
|
|
||||||
typeof thing === 'string' ||
|
|
||||||
typeof thing === 'number' ||
|
|
||||||
typeof thing === 'boolean' ||
|
|
||||||
(thing === Object(thing) &&
|
|
||||||
(thing.__proto__ === StaticArrayBufferProto ||
|
|
||||||
thing.__proto__ === StaticUint8ArrayProto ||
|
|
||||||
thing.__proto__ === StaticByteBufferProto))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Number formatting utils
|
|
||||||
window.textsecure.utils = (() => {
|
|
||||||
const self = {};
|
|
||||||
self.unencodeNumber = number => number.split('.');
|
|
||||||
self.isNumberSane = number => number[0] === '+' && /^[0-9]+$/.test(number.substring(1));
|
|
||||||
|
|
||||||
/** ************************
|
|
||||||
*** JSON'ing Utilities ***
|
|
||||||
************************* */
|
|
||||||
function ensureStringed(thing) {
|
|
||||||
if (getStringable(thing)) {
|
|
||||||
return getString(thing);
|
|
||||||
} else if (thing instanceof Array) {
|
|
||||||
const res = [];
|
|
||||||
for (let i = 0; i < thing.length; i += 1) {
|
|
||||||
res[i] = ensureStringed(thing[i]);
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
} else if (thing === Object(thing)) {
|
|
||||||
const res = {};
|
|
||||||
for (const key in thing) {
|
|
||||||
res[key] = ensureStringed(thing[key]);
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
} else if (thing === null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
throw new Error(`unsure of how to jsonify object of type ${typeof thing}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.jsonThing = thing => JSON.stringify(ensureStringed(thing));
|
|
||||||
|
|
||||||
return self;
|
|
||||||
})();
|
|
@ -1,40 +0,0 @@
|
|||||||
/* global window, textsecure, localStorage */
|
|
||||||
|
|
||||||
// eslint-disable-next-line func-names
|
|
||||||
(function() {
|
|
||||||
/** **********************************************
|
|
||||||
*** Utilities to store data in local storage ***
|
|
||||||
*********************************************** */
|
|
||||||
window.textsecure = window.textsecure || {};
|
|
||||||
window.textsecure.storage = window.textsecure.storage || {};
|
|
||||||
|
|
||||||
// Overrideable storage implementation
|
|
||||||
window.textsecure.storage.impl = window.textsecure.storage.impl || {
|
|
||||||
/** ***************************
|
|
||||||
*** Base Storage Routines ***
|
|
||||||
**************************** */
|
|
||||||
put(key, value) {
|
|
||||||
if (value === undefined) {
|
|
||||||
throw new Error('Tried to store undefined');
|
|
||||||
}
|
|
||||||
localStorage.setItem(`${key}`, textsecure.utils.jsonThing(value));
|
|
||||||
},
|
|
||||||
|
|
||||||
get(key, defaultValue) {
|
|
||||||
const value = localStorage.getItem(`${key}`);
|
|
||||||
if (value === null) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
return JSON.parse(value);
|
|
||||||
},
|
|
||||||
|
|
||||||
remove(key) {
|
|
||||||
localStorage.removeItem(`${key}`);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
window.textsecure.storage.put = (key, value) => textsecure.storage.impl.put(key, value);
|
|
||||||
window.textsecure.storage.get = (key, defaultValue) =>
|
|
||||||
textsecure.storage.impl.get(key, defaultValue);
|
|
||||||
window.textsecure.storage.remove = key => textsecure.storage.impl.remove(key);
|
|
||||||
})();
|
|
@ -1,70 +0,0 @@
|
|||||||
/* global textsecure, window */
|
|
||||||
|
|
||||||
// eslint-disable-next-line func-names
|
|
||||||
(function() {
|
|
||||||
/** *******************************************
|
|
||||||
*** Utilities to store data about the user ***
|
|
||||||
********************************************* */
|
|
||||||
window.textsecure = window.textsecure || {};
|
|
||||||
window.textsecure.storage = window.textsecure.storage || {};
|
|
||||||
|
|
||||||
window.textsecure.storage.user = {
|
|
||||||
setNumberAndDeviceId(number, deviceId, deviceName) {
|
|
||||||
textsecure.storage.put('number_id', `${number}.${deviceId}`);
|
|
||||||
if (deviceName) {
|
|
||||||
textsecure.storage.put('device_name', deviceName);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
getNumber() {
|
|
||||||
const numberId = textsecure.storage.get('number_id');
|
|
||||||
if (numberId === undefined) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
return textsecure.utils.unencodeNumber(numberId)[0];
|
|
||||||
},
|
|
||||||
|
|
||||||
isSignInByLinking() {
|
|
||||||
const isSignInByLinking = textsecure.storage.get('is_sign_in_by_linking');
|
|
||||||
if (isSignInByLinking === undefined) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return isSignInByLinking;
|
|
||||||
},
|
|
||||||
|
|
||||||
setSignInByLinking(isLinking) {
|
|
||||||
textsecure.storage.put('is_sign_in_by_linking', isLinking);
|
|
||||||
},
|
|
||||||
|
|
||||||
isSignWithRecoveryPhrase() {
|
|
||||||
const isRecoveryPhraseUsed = textsecure.storage.get('is_sign_in_recovery_phrase');
|
|
||||||
if (isRecoveryPhraseUsed === undefined) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return isRecoveryPhraseUsed;
|
|
||||||
},
|
|
||||||
setSignWithRecoveryPhrase(isRecoveryPhraseUsed) {
|
|
||||||
textsecure.storage.put('is_sign_in_recovery_phrase', isRecoveryPhraseUsed);
|
|
||||||
},
|
|
||||||
|
|
||||||
getLastProfileUpdateTimestamp() {
|
|
||||||
return textsecure.storage.get('last_profile_update_timestamp');
|
|
||||||
},
|
|
||||||
|
|
||||||
setLastProfileUpdateTimestamp(lastUpdateTimestamp) {
|
|
||||||
textsecure.storage.put('last_profile_update_timestamp', lastUpdateTimestamp);
|
|
||||||
},
|
|
||||||
|
|
||||||
getDeviceId() {
|
|
||||||
const numberId = textsecure.storage.get('number_id');
|
|
||||||
if (numberId === undefined) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
return textsecure.utils.unencodeNumber(numberId)[1];
|
|
||||||
},
|
|
||||||
|
|
||||||
getDeviceName() {
|
|
||||||
return textsecure.storage.get('device_name');
|
|
||||||
},
|
|
||||||
};
|
|
||||||
})();
|
|
@ -1,21 +1,23 @@
|
|||||||
function markEverDone() {
|
import { Storage } from './storage';
|
||||||
storage.put('chromiumRegistrationDoneEver', '');
|
|
||||||
|
async function markEverDone() {
|
||||||
|
await Storage.put('chromiumRegistrationDoneEver', '');
|
||||||
}
|
}
|
||||||
function markDone() {
|
async function markDone() {
|
||||||
this.markEverDone();
|
await markEverDone();
|
||||||
storage.put('chromiumRegistrationDone', '');
|
await Storage.put('chromiumRegistrationDone', '');
|
||||||
}
|
}
|
||||||
function isDone() {
|
function isDone() {
|
||||||
return storage.get('chromiumRegistrationDone') === '';
|
return Storage.get('chromiumRegistrationDone') === '';
|
||||||
}
|
}
|
||||||
function everDone() {
|
function everDone() {
|
||||||
return (
|
return (
|
||||||
storage.get('chromiumRegistrationDoneEver') === '' ||
|
Storage.get('chromiumRegistrationDoneEver') === '' ||
|
||||||
storage.get('chromiumRegistrationDone') === ''
|
Storage.get('chromiumRegistrationDone') === ''
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
function remove() {
|
async function remove() {
|
||||||
storage.remove('chromiumRegistrationDone');
|
await Storage.remove('chromiumRegistrationDone');
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Registration = { markEverDone, markDone, isDone, everDone, remove };
|
export const Registration = { markEverDone, markDone, isDone, everDone, remove };
|
||||||
|
@ -0,0 +1,138 @@
|
|||||||
|
import * as Data from '../data/data';
|
||||||
|
|
||||||
|
let ready = false;
|
||||||
|
|
||||||
|
type ValueType = string | number | boolean;
|
||||||
|
type InsertedValueType = { id: string; value: ValueType };
|
||||||
|
let items: Record<string, InsertedValueType>;
|
||||||
|
let callbacks: Array<() => void> = [];
|
||||||
|
|
||||||
|
reset();
|
||||||
|
|
||||||
|
async function put(key: string, value: ValueType) {
|
||||||
|
if (value === undefined) {
|
||||||
|
throw new Error('Tried to store undefined');
|
||||||
|
}
|
||||||
|
if (!ready) {
|
||||||
|
window.log.warn('Called storage.put before storage is ready. key:', key);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data: InsertedValueType = { id: key, value };
|
||||||
|
|
||||||
|
items[key] = data;
|
||||||
|
await Data.createOrUpdateItem(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
function get(key: string, defaultValue?: ValueType) {
|
||||||
|
if (!ready) {
|
||||||
|
window.log.warn('Called storage.get before storage is ready. key:', key);
|
||||||
|
}
|
||||||
|
|
||||||
|
const item = items[key];
|
||||||
|
if (!item) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return item.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function remove(key: string) {
|
||||||
|
if (!ready) {
|
||||||
|
window.log.warn('Called storage.get before storage is ready. key:', key);
|
||||||
|
}
|
||||||
|
|
||||||
|
// tslint:disable-next-line: no-dynamic-delete
|
||||||
|
delete items[key];
|
||||||
|
await Data.removeItemById(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
function onready(callback: () => void) {
|
||||||
|
if (ready) {
|
||||||
|
callback();
|
||||||
|
} else {
|
||||||
|
callbacks.push(callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function callListeners() {
|
||||||
|
if (ready) {
|
||||||
|
callbacks.forEach(callback => {
|
||||||
|
callback();
|
||||||
|
});
|
||||||
|
callbacks = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetch() {
|
||||||
|
reset();
|
||||||
|
const array = await Data.getAllItems();
|
||||||
|
|
||||||
|
// tslint:disable-next-line: one-variable-per-declaration
|
||||||
|
for (let i = 0, max = array.length; i < max; i += 1) {
|
||||||
|
const item = array[i];
|
||||||
|
const { id } = item;
|
||||||
|
items[id] = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
ready = true;
|
||||||
|
callListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
function reset() {
|
||||||
|
ready = false;
|
||||||
|
items = Object.create(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function setLocalPubKey(pubkey: string) {
|
||||||
|
await put('number_id', `${pubkey}.1`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getNumber() {
|
||||||
|
const numberId = get('number_id') as string | undefined;
|
||||||
|
if (numberId === undefined) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return numberId.split('.')[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isSignInByLinking() {
|
||||||
|
const isByLinking = get('is_sign_in_by_linking');
|
||||||
|
if (isByLinking === undefined) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return isByLinking;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function setSignInByLinking(isLinking: boolean) {
|
||||||
|
await put('is_sign_in_by_linking', isLinking);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isSignWithRecoveryPhrase() {
|
||||||
|
const isRecoveryPhraseUsed = get('is_sign_in_recovery_phrase');
|
||||||
|
if (isRecoveryPhraseUsed === undefined) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return isRecoveryPhraseUsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function setSignWithRecoveryPhrase(isRecoveryPhraseUsed: boolean) {
|
||||||
|
await put('is_sign_in_recovery_phrase', isRecoveryPhraseUsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getLastProfileUpdateTimestamp() {
|
||||||
|
return get('last_profile_update_timestamp');
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function setLastProfileUpdateTimestamp(lastUpdateTimestamp: number) {
|
||||||
|
await put('last_profile_update_timestamp', lastUpdateTimestamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getCurrentRecoveryPhrase() {
|
||||||
|
return Storage.get('mnemonic') as string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveRecoveryPhrase(mnemonic: string) {
|
||||||
|
return Storage.put('mnemonic', mnemonic);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Storage = { fetch, put, get, remove, onready, reset };
|
Loading…
Reference in New Issue