Enforce stronger types for ArrayBuffers and storage

This commit is contained in:
Fedor Indutny 2021-06-14 17:09:37 -07:00 committed by GitHub
parent 61ac79e9ae
commit 8f5086227a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
56 changed files with 748 additions and 675 deletions

View file

@ -1,101 +0,0 @@
// Copyright 2016-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* global storage, _ */
// eslint-disable-next-line func-names
(function () {
const BLOCKED_NUMBERS_ID = 'blocked';
const BLOCKED_UUIDS_ID = 'blocked-uuids';
const BLOCKED_GROUPS_ID = 'blocked-groups';
function getArray(key) {
const result = storage.get(key, []);
if (!Array.isArray(result)) {
window.log.error(
`Expected storage key ${JSON.stringify(
key
)} to contain an array or nothing`
);
return [];
}
return result;
}
storage.getBlockedNumbers = () => getArray(BLOCKED_NUMBERS_ID);
storage.isBlocked = number => {
const numbers = storage.getBlockedNumbers();
return _.include(numbers, number);
};
storage.addBlockedNumber = number => {
const numbers = storage.getBlockedNumbers();
if (_.include(numbers, number)) {
return;
}
window.log.info('adding', number, 'to blocked list');
storage.put(BLOCKED_NUMBERS_ID, numbers.concat(number));
};
storage.removeBlockedNumber = number => {
const numbers = storage.getBlockedNumbers();
if (!_.include(numbers, number)) {
return;
}
window.log.info('removing', number, 'from blocked list');
storage.put(BLOCKED_NUMBERS_ID, _.without(numbers, number));
};
storage.getBlockedUuids = () => getArray(BLOCKED_UUIDS_ID);
storage.isUuidBlocked = uuid => {
const uuids = storage.getBlockedUuids();
return _.include(uuids, uuid);
};
storage.addBlockedUuid = uuid => {
const uuids = storage.getBlockedUuids();
if (_.include(uuids, uuid)) {
return;
}
window.log.info('adding', uuid, 'to blocked list');
storage.put(BLOCKED_UUIDS_ID, uuids.concat(uuid));
};
storage.removeBlockedUuid = uuid => {
const numbers = storage.getBlockedUuids();
if (!_.include(numbers, uuid)) {
return;
}
window.log.info('removing', uuid, 'from blocked list');
storage.put(BLOCKED_UUIDS_ID, _.without(numbers, uuid));
};
storage.getBlockedGroups = () => getArray(BLOCKED_GROUPS_ID);
storage.isGroupBlocked = groupId => {
const groupIds = storage.getBlockedGroups();
return _.include(groupIds, groupId);
};
storage.addBlockedGroup = groupId => {
const groupIds = storage.getBlockedGroups();
if (_.include(groupIds, groupId)) {
return;
}
window.log.info(`adding group(${groupId}) to blocked list`);
storage.put(BLOCKED_GROUPS_ID, groupIds.concat(groupId));
};
storage.removeBlockedGroup = groupId => {
const groupIds = storage.getBlockedGroups();
if (!_.include(groupIds, groupId)) {
return;
}
window.log.info(`removing group(${groupId} from blocked list`);
storage.put(BLOCKED_GROUPS_ID, _.without(groupIds, groupId));
};
})();

View file

@ -1,131 +0,0 @@
// Copyright 2014-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* global _ */
/* eslint-disable more/no-then */
// eslint-disable-next-line func-names
(function () {
window.Whisper = window.Whisper || {};
let ready = false;
let items;
let callbacks = [];
reset();
async function put(key, value) {
if (value === undefined) {
window.log.warn(`storage/put: undefined provided for key ${key}`);
}
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);
if (_.has(window, ['reduxActions', 'items', 'putItemExternal'])) {
window.reduxActions.items.putItemExternal(key, value);
}
}
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.remove before storage is ready. key:',
key
);
}
delete items[key];
await window.Signal.Data.removeItemById(key);
if (_.has(window, ['reduxActions', 'items', 'removeItemExternal'])) {
window.reduxActions.items.removeItemExternal(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 getItemsState() {
const data = _.clone(items);
const ids = Object.keys(data);
ids.forEach(id => {
data[id] = data[id].value;
});
return data;
}
function reset() {
ready = false;
items = Object.create(null);
}
const storage = {
fetch,
put,
get,
getItemsState,
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);
})();