Prefer arrow functions and callbacks (#1210)

This commit is contained in:
Lipis 2020-05-20 16:21:37 +03:00 committed by GitHub
parent 33fe223b5d
commit c427aa3cce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
64 changed files with 784 additions and 847 deletions

View file

@ -72,17 +72,15 @@ export type SocketUpdateDataIncoming =
// part of `AppState`.
(window as any).handle = null;
function byteToHex(byte: number): string {
return `0${byte.toString(16)}`.slice(-2);
}
const byteToHex = (byte: number): string => `0${byte.toString(16)}`.slice(-2);
async function generateRandomID() {
const generateRandomID = async () => {
const arr = new Uint8Array(10);
window.crypto.getRandomValues(arr);
return Array.from(arr, byteToHex).join("");
}
};
async function generateEncryptionKey() {
const generateEncryptionKey = async () => {
const key = await window.crypto.subtle.generateKey(
{
name: "AES-GCM",
@ -92,29 +90,29 @@ async function generateEncryptionKey() {
["encrypt", "decrypt"],
);
return (await window.crypto.subtle.exportKey("jwk", key)).k;
}
};
function createIV() {
const createIV = () => {
const arr = new Uint8Array(12);
return window.crypto.getRandomValues(arr);
}
};
export function getCollaborationLinkData(link: string) {
export const getCollaborationLinkData = (link: string) => {
if (link.length === 0) {
return;
}
const hash = new URL(link).hash;
return hash.match(/^#room=([a-zA-Z0-9_-]+),([a-zA-Z0-9_-]+)$/);
}
};
export async function generateCollaborationLink() {
export const generateCollaborationLink = async () => {
const id = await generateRandomID();
const key = await generateEncryptionKey();
return `${window.location.origin}${window.location.pathname}#room=${id},${key}`;
}
};
function getImportedKey(key: string, usage: string) {
return window.crypto.subtle.importKey(
const getImportedKey = (key: string, usage: string) =>
window.crypto.subtle.importKey(
"jwk",
{
alg: "A128GCM",
@ -130,12 +128,11 @@ function getImportedKey(key: string, usage: string) {
false, // extractable
[usage],
);
}
export async function encryptAESGEM(
export const encryptAESGEM = async (
data: Uint8Array,
key: string,
): Promise<EncryptedData> {
): Promise<EncryptedData> => {
const importedKey = await getImportedKey(key, "encrypt");
const iv = createIV();
return {
@ -149,13 +146,13 @@ export async function encryptAESGEM(
),
iv,
};
}
};
export async function decryptAESGEM(
export const decryptAESGEM = async (
data: ArrayBuffer,
key: string,
iv: Uint8Array,
): Promise<SocketUpdateDataIncoming> {
): Promise<SocketUpdateDataIncoming> => {
try {
const importedKey = await getImportedKey(key, "decrypt");
const decrypted = await window.crypto.subtle.decrypt(
@ -178,12 +175,12 @@ export async function decryptAESGEM(
return {
type: "INVALID_RESPONSE",
};
}
};
export async function exportToBackend(
export const exportToBackend = async (
elements: readonly ExcalidrawElement[],
appState: AppState,
) {
) => {
const json = serializeAsJSON(elements, appState);
const encoded = new TextEncoder().encode(json);
@ -233,12 +230,12 @@ export async function exportToBackend(
console.error(error);
window.alert(t("alerts.couldNotCreateShareableLink"));
}
}
};
export async function importFromBackend(
export const importFromBackend = async (
id: string | null,
privateKey: string | undefined,
) {
) => {
let elements: readonly ExcalidrawElement[] = [];
let appState: AppState = getDefaultAppState();
@ -281,9 +278,9 @@ export async function importFromBackend(
} finally {
return restore(elements, appState, { scrollToContent: true });
}
}
};
export async function exportCanvas(
export const exportCanvas = async (
type: ExportType,
elements: readonly NonDeletedExcalidrawElement[],
appState: AppState,
@ -303,7 +300,7 @@ export async function exportCanvas(
scale?: number;
shouldAddWatermark: boolean;
},
) {
) => {
if (elements.length === 0) {
return window.alert(t("alerts.cannotExportEmptyCanvas"));
}
@ -362,9 +359,9 @@ export async function exportCanvas(
if (tempCanvas !== canvas) {
tempCanvas.remove();
}
}
};
export async function loadScene(id: string | null, privateKey?: string) {
export const loadScene = async (id: string | null, privateKey?: string) => {
let data;
if (id != null) {
// the private key is used to decrypt the content from the server, take
@ -380,4 +377,4 @@ export async function loadScene(id: string | null, privateKey?: string) {
appState: data.appState && { ...data.appState },
commitToHistory: false,
};
}
};