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

@ -7,6 +7,7 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable no-restricted-syntax */
import { ipcRenderer } from 'electron';
import {
@ -44,6 +45,7 @@ import {
ClientJobType,
ConversationType,
IdentityKeyType,
ItemKeyType,
ItemType,
MessageType,
MessageTypeUnhydrated,
@ -132,7 +134,6 @@ const dataInterface: ClientInterface = {
createOrUpdateItem,
getItemById,
getAllItems,
bulkAddItems,
removeItemById,
removeAllItems,
@ -692,14 +693,14 @@ async function removeAllSignedPreKeys() {
// Items
const ITEM_KEYS: { [key: string]: Array<string> | undefined } = {
const ITEM_KEYS: Partial<Record<ItemKeyType, Array<string>>> = {
identityKey: ['value.pubKey', 'value.privKey'],
senderCertificate: ['value.serialized'],
senderCertificateNoE164: ['value.serialized'],
signaling_key: ['value'],
profileKey: ['value'],
};
async function createOrUpdateItem(data: ItemType) {
async function createOrUpdateItem<K extends ItemKeyType>(data: ItemType<K>) {
const { id } = data;
if (!id) {
throw new Error(
@ -712,7 +713,7 @@ async function createOrUpdateItem(data: ItemType) {
await channels.createOrUpdateItem(updated);
}
async function getItemById(id: string) {
async function getItemById<K extends ItemKeyType>(id: K): Promise<ItemType<K>> {
const keys = ITEM_KEYS[id];
const data = await channels.getItemById(id);
@ -721,23 +722,24 @@ async function getItemById(id: string) {
async function getAllItems() {
const items = await channels.getAllItems();
return map(items, item => {
const { id } = item;
const keys = ITEM_KEYS[id];
const result = Object.create(null);
return Array.isArray(keys) ? keysToArrayBuffer(keys, item) : item;
});
}
async function bulkAddItems(array: Array<ItemType>) {
const updated = map(array, data => {
const { id } = data;
const keys = ITEM_KEYS[id];
for (const id of Object.keys(items)) {
const key = id as ItemKeyType;
const value = items[key];
return keys && Array.isArray(keys) ? keysFromArrayBuffer(keys, data) : data;
});
await channels.bulkAddItems(updated);
const keys = ITEM_KEYS[key];
const deserializedValue = Array.isArray(keys)
? keysToArrayBuffer(keys, { value }).value
: value;
result[key] = deserializedValue;
}
return result;
}
async function removeItemById(id: string) {
async function removeItemById(id: ItemKeyType) {
await channels.removeItemById(id);
}
async function removeAllItems() {

View file

@ -15,6 +15,7 @@ import { ConversationModel } from '../models/conversations';
import { StoredJob } from '../jobs/types';
import { ReactionType } from '../types/Reactions';
import { ConversationColorType, CustomColorType } from '../types/Colors';
import { StorageAccessType } from '../types/Storage.d';
export type AttachmentDownloadJobType = {
id: string;
@ -48,7 +49,12 @@ export type IdentityKeyType = {
timestamp: number;
verified: number;
};
export type ItemType = any;
export type ItemKeyType = keyof StorageAccessType;
export type AllItemsType = Partial<StorageAccessType>;
export type ItemType<K extends ItemKeyType> = {
id: K;
value: StorageAccessType[K];
};
export type MessageType = MessageAttributesType;
export type MessageTypeUnhydrated = {
json: string;
@ -177,12 +183,11 @@ export type DataInterface = {
removeAllSignedPreKeys: () => Promise<void>;
getAllSignedPreKeys: () => Promise<Array<SignedPreKeyType>>;
createOrUpdateItem: (data: ItemType) => Promise<void>;
getItemById: (id: string) => Promise<ItemType | undefined>;
bulkAddItems: (array: Array<ItemType>) => Promise<void>;
removeItemById: (id: string) => Promise<void>;
createOrUpdateItem<K extends ItemKeyType>(data: ItemType<K>): Promise<void>;
getItemById<K extends ItemKeyType>(id: K): Promise<ItemType<K> | undefined>;
removeItemById: (id: ItemKeyType) => Promise<void>;
removeAllItems: () => Promise<void>;
getAllItems: () => Promise<Array<ItemType>>;
getAllItems: () => Promise<AllItemsType>;
createOrUpdateSenderKey: (key: SenderKeyType) => Promise<void>;
getSenderKeyById: (id: string) => Promise<SenderKeyType | undefined>;

View file

@ -44,6 +44,8 @@ import {
ConversationType,
EmojiType,
IdentityKeyType,
AllItemsType,
ItemKeyType,
ItemType,
MessageType,
MessageTypeUnhydrated,
@ -123,7 +125,6 @@ const dataInterface: ServerInterface = {
createOrUpdateItem,
getItemById,
getAllItems,
bulkAddItems,
removeItemById,
removeAllItems,
@ -2170,24 +2171,34 @@ async function getAllSignedPreKeys(): Promise<Array<SignedPreKeyType>> {
}
const ITEMS_TABLE = 'items';
function createOrUpdateItem(data: ItemType): Promise<void> {
function createOrUpdateItem<K extends ItemKeyType>(
data: ItemType<K>
): Promise<void> {
return createOrUpdate(ITEMS_TABLE, data);
}
function getItemById(id: string): Promise<ItemType> {
function getItemById<K extends ItemKeyType>(
id: K
): Promise<ItemType<K> | undefined> {
return getById(ITEMS_TABLE, id);
}
async function getAllItems(): Promise<Array<ItemType>> {
async function getAllItems(): Promise<AllItemsType> {
const db = getInstance();
const rows: JSONRows = db
.prepare<EmptyQuery>('SELECT json FROM items ORDER BY id ASC;')
.all();
return rows.map(row => jsonToObject(row.json));
const items = rows.map(row => jsonToObject(row.json));
const result: AllItemsType = Object.create(null);
for (const { id, value } of items) {
const key = id as ItemKeyType;
result[key] = value;
}
return result;
}
function bulkAddItems(array: Array<ItemType>): Promise<void> {
return bulkAdd(ITEMS_TABLE, array);
}
function removeItemById(id: string): Promise<void> {
function removeItemById(id: ItemKeyType): Promise<void> {
return removeById(ITEMS_TABLE, id);
}
function removeAllItems(): Promise<void> {