1
0
Fork 0

Make changePage async.

This commit is contained in:
Kitteh 2021-05-12 17:37:09 +01:00
parent 9c50ca2432
commit b3470be6f1
26 changed files with 119 additions and 127 deletions

View file

@ -28,12 +28,8 @@
"argsIgnorePattern": "^_"
}
],
"@typescript-eslint/no-empty-function": [
"error",
{
"allow": ["arrowFunctions"]
}
],
"@typescript-eslint/require-await": ["off"],
"@typescript-eslint/no-empty-function": ["off"],
"@typescript-eslint/ban-ts-comment": ["off"],
"sort-imports-es6-autofix/sort-imports-es6": [2],
"@typescript-eslint/no-explicit-any": [2],

View file

@ -22,8 +22,8 @@ export function NavBar(): HTMLElement {
makeElement({
tag: "a",
text: i18next.t("home_btn"),
onclick: () => {
changePage("HOME");
onclick: async () => {
await changePage("HOME");
},
}),
),
@ -31,8 +31,8 @@ export function NavBar(): HTMLElement {
makeElement({
tag: "a",
text: i18next.t("back_btn"),
onclick: () => {
(pageState.currentPage as Page).goBack();
onclick: async () => {
await (pageState.currentPage as Page).goBack();
},
}),
),
@ -40,8 +40,8 @@ export function NavBar(): HTMLElement {
makeElement({
tag: "a",
text: i18next.t("refresh_btn"),
onclick: () => {
changePage(pageState.currentPageString);
onclick: async () => {
await changePage(pageState.currentPageString);
},
}),
),
@ -59,8 +59,8 @@ export function NavBar(): HTMLElement {
makeElement({
tag: "a",
text: i18next.t("me_btn"),
onclick: () => {
changePage("ME");
onclick: async () => {
await changePage("ME");
},
}),
),

View file

@ -37,7 +37,7 @@ declare global {
}
}
function onLoad(): void {
async function onLoad(): Promise<void> {
document.body.innerHTML = "";
document.body.appendChild(NavBar());
document.body.appendChild(
@ -66,10 +66,10 @@ function onLoad(): void {
window.pageContent = document.querySelector("#pageContent");
if (process.env.NODE_ENV == "development") {
playground();
await playground();
}
renderPage();
await renderPage();
setInterval(() => {
if (pageState.currentPageString != "UNSEAL") {
@ -78,7 +78,7 @@ function onLoad(): void {
}
void getSealStatus().then((sealStatus) => {
if (sealStatus.sealed) {
changePage("UNSEAL");
void changePage("UNSEAL");
return;
}
});
@ -110,7 +110,7 @@ document.addEventListener(
},
})
.then(function (_) {
onLoad();
void onLoad();
});
},
false,

View file

@ -10,24 +10,24 @@ import i18next from "i18next";
async function prePageChecksReal() {
if (pageState.language.length == 0) {
changePage("SET_LANGUAGE");
await changePage("SET_LANGUAGE");
throw new Error("Language Not Set");
}
if (!pageState.apiURL) {
changePage("SET_VAULT_URL");
await changePage("SET_VAULT_URL");
throw new Error("Vault URL Not Set");
}
const sealStatus = await getSealStatus();
if (sealStatus.sealed) {
changePage("UNSEAL");
await changePage("UNSEAL");
throw new Error("Vault Sealed");
}
try {
await lookupSelf();
} catch (e) {
changePage("LOGIN");
await changePage("LOGIN");
throw e;
}
}
@ -78,22 +78,20 @@ export function setErrorText(text: string): void {
});
}
export function changePage(page: string, shouldSwitch = true): void {
if (pageState.currentPage && shouldSwitch) {
(pageState.currentPage as Page).cleanup();
export async function changePage(page: string): Promise<void> {
if (pageState.currentPage) {
await (pageState.currentPage as Page).cleanup();
}
pageState.currentPage = page;
if (shouldSwitch) {
renderPage();
}
await renderPage();
}
export function renderPage(): void {
export async function renderPage(): Promise<void> {
document.documentElement.dir = pageState.pageDirection;
console.log("Rendering Page: ", (pageState.currentPage as Page).name);
document.querySelector("#pageContent").innerHTML = "";
setPageTitle((pageState.currentPage as Page).name);
(pageState.currentPage as Page).render();
await (pageState.currentPage as Page).render();
}
export function setPageTitle(title: string | HTMLElement): void {
@ -121,7 +119,7 @@ export function setTitleElement(pageState: PageState): void {
makeElement({
tag: "a",
text: pageState.currentBaseMount + " ",
onclick: () => {
onclick: async () => {
pageState.currentSecretPath = [];
pageState.currentSecret = "";
pageState.currentSecretVersion = null;
@ -130,11 +128,11 @@ export function setTitleElement(pageState: PageState): void {
pageState.currentMountType.startsWith("kv") ||
pageState.currentMountType == "cubbyhole"
) {
changePage("KEY_VALUE_VIEW");
await changePage("KEY_VALUE_VIEW");
} else if (pageState.currentMountType == "totp") {
changePage("TOTP");
await changePage("TOTP");
} else if (pageState.currentMountType == "transit") {
changePage("TRANSIT_VIEW");
await changePage("TRANSIT_VIEW");
}
},
}),
@ -142,11 +140,11 @@ export function setTitleElement(pageState: PageState): void {
return makeElement({
tag: "a",
text: secretPath + " ",
onclick: () => {
onclick: async () => {
pageState.currentSecretVersion = null;
if (pageState.currentMountType.startsWith("kv")) {
pageState.currentSecretPath = secretPaths.slice(0, index + 1);
changePage("KEY_VALUE_VIEW");
await changePage("KEY_VALUE_VIEW");
}
},
});

View file

@ -33,8 +33,8 @@ export class HomePage extends Page {
children: makeElement({
tag: "a",
text: i18next.t("password_generator_btn"),
onclick: () => {
changePage("PW_GEN");
onclick: async () => {
await changePage("PW_GEN");
},
}),
}),
@ -57,7 +57,7 @@ export class HomePage extends Page {
setErrorText(error.message);
if (error.message == "permission denied") {
pageState.token = "";
changePage("LOGIN");
await changePage("LOGIN");
}
}
@ -85,7 +85,7 @@ export class HomePage extends Page {
const mountType = mount.type == "kv" ? "kv-v" + String(mount.options.version) : mount.type;
let linkText = "";
let linkPage;
let linkPage: string;
if (mount.type == "kv") {
linkText = `K/V (v${mount.options.version}) - ${baseMount}`;
linkPage = "KEY_VALUE_VIEW";
@ -106,10 +106,10 @@ export class HomePage extends Page {
children: makeElement({
tag: "a",
text: linkText,
onclick: () => {
onclick: async () => {
pageState.currentBaseMount = baseMount;
pageState.currentMountType = mountType;
changePage(linkPage);
await changePage(linkPage);
},
}),
}),

View file

@ -9,16 +9,16 @@ export class KeyValueDeletePage extends Page {
constructor() {
super();
}
goBack(): void {
async goBack(): Promise<void> {
if (pageState.currentSecretVersion != null) {
pageState.currentSecretVersion = null;
changePage("KEY_VALUE_SECRET");
await changePage("KEY_VALUE_SECRET");
} else {
pageState.currentSecret = "";
changePage("KEY_VALUE_VIEW");
await changePage("KEY_VALUE_VIEW");
}
}
render(): void {
async render(): Promise<void> {
setTitleElement(pageState);
setPageContent(
makeElement({
@ -40,7 +40,7 @@ export class KeyValueDeletePage extends Page {
pageState.currentSecret,
pageState.currentSecretVersion,
).then(() => {
this.goBack();
void this.goBack();
});
},
}),

View file

@ -10,13 +10,13 @@ export class KeyValueNewPage extends Page {
super();
}
goBack(): void {
changePage("KEY_VALUE_VIEW");
async goBack(): Promise<void> {
await changePage("KEY_VALUE_VIEW");
}
addKVNewForm: HTMLFormElement;
render(): void {
async render(): Promise<void> {
setTitleElement(pageState);
this.addKVNewForm = makeElement({
tag: "form",
@ -76,7 +76,7 @@ export class KeyValueNewPage extends Page {
path,
keyData,
);
changePage("KEY_VALUE_VIEW");
await changePage("KEY_VALUE_VIEW");
} catch (e: unknown) {
const error = e as Error;
setErrorText(error.message);

View file

@ -14,13 +14,13 @@ export class KeyValueSecretPage extends Page {
constructor() {
super();
}
goBack(): void {
async goBack(): Promise<void> {
if (pageState.currentSecretVersion != null) {
pageState.currentSecretVersion = null;
changePage("KEY_VALUE_VERSIONS");
await changePage("KEY_VALUE_VERSIONS");
} else {
pageState.currentSecret = "";
changePage("KEY_VALUE_VIEW");
await changePage("KEY_VALUE_VIEW");
}
}
async render(): Promise<void> {
@ -68,8 +68,8 @@ export class KeyValueSecretPage extends Page {
tag: "button",
id: "deleteButton",
class: ["uk-button", "uk-button-danger"],
onclick: () => {
changePage("KEY_VALUE_DELETE");
onclick: async () => {
await changePage("KEY_VALUE_DELETE");
},
text: deleteButtonText,
}),
@ -82,8 +82,8 @@ export class KeyValueSecretPage extends Page {
tag: "button",
id: "editButton",
class: ["uk-button", "uk-margin", "uk-button-primary"],
onclick: () => {
changePage("KEY_VALUE_SECRET_EDIT");
onclick: async () => {
await changePage("KEY_VALUE_SECRET_EDIT");
},
text: i18next.t("kv_secret_edit_btn"),
}),
@ -96,8 +96,8 @@ export class KeyValueSecretPage extends Page {
tag: "button",
id: "versionsButton",
class: ["uk-button", "uk-button-secondary"],
onclick: () => {
changePage("KEY_VALUE_VERSIONS");
onclick: async () => {
await changePage("KEY_VALUE_VERSIONS");
},
text: i18next.t("kv_secret_versions_btn"),
}),
@ -135,7 +135,7 @@ export class KeyValueSecretPage extends Page {
pageState.currentSecret,
pageState.currentSecretVersion,
).then((_) => {
changePage(pageState.currentPageString);
void changePage(pageState.currentPageString);
});
},
}),

View file

@ -12,10 +12,10 @@ export class KeyValueSecretEditPage extends Page {
constructor() {
super();
}
goBack(): void {
changePage("KEY_VALUE_SECRET");
async goBack(): Promise<void> {
await changePage("KEY_VALUE_SECRET");
}
render(): void {
async render(): Promise<void> {
setTitleElement(pageState);
const loadingText = makeElement({
tag: "p",
@ -71,7 +71,7 @@ export class KeyValueSecretEditPage extends Page {
JSON.parse(jar.toString()),
)
.then((_) => {
changePage("KEY_VALUE_SECRET");
void changePage("KEY_VALUE_SECRET");
return;
})
.catch((e: Error) => {

View file

@ -10,11 +10,11 @@ export class KeyValueVersionsPage extends Page {
constructor() {
super();
}
goBack(): void {
async goBack(): Promise<void> {
if (pageState.currentSecretVersion != null) {
pageState.currentSecretVersion = null;
}
changePage("KEY_VALUE_SECRET");
await changePage("KEY_VALUE_SECRET");
}
async render(): Promise<void> {
setTitleElement(pageState);
@ -39,9 +39,9 @@ export class KeyValueVersionsPage extends Page {
children: makeElement({
tag: "a",
text: `v${ver}`,
onclick: () => {
onclick: async () => {
pageState.currentSecretVersion = ver;
changePage("KEY_VALUE_SECRET");
await changePage("KEY_VALUE_SECRET");
},
}),
}),

View file

@ -10,12 +10,12 @@ export class KeyValueViewPage extends Page {
constructor() {
super();
}
goBack(): void {
async goBack(): Promise<void> {
if (pageState.currentSecretPath.length != 0) {
pageState.popCurrentSecretPath();
changePage("KEY_VALUE_VIEW");
await changePage("KEY_VALUE_VIEW");
} else {
changePage("HOME");
await changePage("HOME");
}
}
async render(): Promise<void> {
@ -39,8 +39,8 @@ export class KeyValueViewPage extends Page {
tag: "button",
text: i18next.t("kv_view_new_btn"),
class: ["uk-button", "uk-button-primary", "uk-margin-bottom"],
onclick: () => {
changePage("KEY_VALUE_NEW_SECRET");
onclick: async () => {
await changePage("KEY_VALUE_NEW_SECRET");
},
});
kvViewPageContent.appendChild(newButton);
@ -63,13 +63,13 @@ export class KeyValueViewPage extends Page {
children: makeElement({
tag: "a",
text: secret,
onclick: () => {
onclick: async () => {
if (secret.endsWith("/")) {
pageState.pushCurrentSecretPath(secret);
changePage("KEY_VALUE_VIEW");
await changePage("KEY_VALUE_VIEW");
} else {
pageState.currentSecret = secret;
changePage("KEY_VALUE_SECRET");
await changePage("KEY_VALUE_SECRET");
}
},
}),

View file

@ -12,7 +12,7 @@ export class LoginPage extends Page {
constructor() {
super();
}
render(): void {
async render(): Promise<void> {
const tokenLoginForm = makeElement({
tag: "form",
children: [
@ -139,7 +139,7 @@ export class LoginPage extends Page {
pageState.token = token as string;
lookupSelf()
.then((_) => {
changePage("HOME");
void changePage("HOME");
})
.catch((e: Error) => {
document.getElementById("tokenInput").classList.add("uk-form-danger");
@ -156,7 +156,7 @@ export class LoginPage extends Page {
usernameLogin(formData.get("username") as string, formData.get("password") as string)
.then((res) => {
pageState.token = res;
changePage("HOME");
void changePage("HOME");
})
.catch((e: Error) => {
document.getElementById("usernameInput").classList.add("uk-form-danger");

View file

@ -31,9 +31,9 @@ export class MePage extends Page {
children: makeElement({
tag: "a",
text: i18next.t("log_out_btn"),
onclick: () => {
onclick: async () => {
pageState.token = "";
changePage("HOME");
await changePage("HOME");
},
}),
}),
@ -59,7 +59,7 @@ export class MePage extends Page {
onclick: () => {
renewSelf()
.then(() => {
changePage("HOME");
void changePage("HOME");
})
.catch((e: Error) => {
setErrorText(e.message);
@ -82,7 +82,7 @@ export class MePage extends Page {
text: i18next.t("seal_vault_btn"),
onclick: async () => {
await sealVault();
changePage("UNSEAL_VAULT");
await changePage("UNSEAL_VAULT");
},
}),
}),
@ -91,8 +91,8 @@ export class MePage extends Page {
children: makeElement({
tag: "a",
text: i18next.t("change_language_btn"),
onclick: () => {
changePage("SET_LANGUAGE");
onclick: async () => {
await changePage("SET_LANGUAGE");
},
}),
}),

View file

@ -58,7 +58,7 @@ export class PwGenPage extends Page {
passwordAlphabet: HTMLSelectElement;
passwordForm: HTMLFormElement;
render(): void {
async render(): Promise<void> {
setPageContent("");
this.passwordBox = CopyableInputBox(genPassword(passwordOptionsDefault));
@ -135,7 +135,7 @@ export class PwGenPage extends Page {
);
}
cleanup(): void {
async cleanup(): Promise<void> {
this.passwordBox = undefined;
this.passwordLengthTitle = undefined;
this.passwordLengthRange = undefined;

View file

@ -15,7 +15,7 @@ export class SetLanguagePage extends Page {
constructor() {
super();
}
render(): void {
async render(): Promise<void> {
const setLanguageForm = makeElement({
tag: "form",
id: "setLanguageForm",
@ -61,7 +61,7 @@ export class SetLanguagePage extends Page {
void i18next.changeLanguage(language).then((t) => {
pageState.pageDirection = t("language_direction");
reloadNavBar();
changePage("HOME");
void changePage("HOME");
});
});
}

View file

@ -7,7 +7,7 @@ export class SetVaultURLPage extends Page {
constructor() {
super();
}
render(): void {
async render(): Promise<void> {
setPageContent(
makeElement({
tag: "form",
@ -47,7 +47,7 @@ export class SetVaultURLPage extends Page {
e.preventDefault();
const formData = new FormData(document.querySelector("#setVaultURLForm"));
pageState.apiURL = formData.get("vaultURL") as string;
changePage("HOME");
void changePage("HOME");
});
}
get name(): string {

View file

@ -20,10 +20,10 @@ export class NewTOTPPage extends Page {
constructor() {
super();
}
goBack(): void {
changePage("TOTP");
async goBack(): Promise<void> {
await changePage("TOTP");
}
render(): void {
async render(): Promise<void> {
setTitleElement(pageState);
const totpForm = makeElement({
@ -97,7 +97,7 @@ export class NewTOTPPage extends Page {
};
addNewTOTP(pageState.currentBaseMount, parms)
.then((_) => {
changePage("TOTP");
void changePage("TOTP");
})
.catch((e: Error) => {
setErrorText(`API Error: ${e.message}`);

View file

@ -34,7 +34,7 @@ export class TOTPViewPage extends Page {
tag: "a",
text: i18next.t("totp_view_new_btn"),
onclick: () => {
changePage("NEW_TOTP");
void changePage("NEW_TOTP");
},
}),
makeElement({
@ -51,7 +51,8 @@ export class TOTPViewPage extends Page {
try {
const res = await getTOTPKeys(pageState.currentBaseMount);
for (const totpKeyName in res.entries()) {
for (const totpKeyName of res) {
console.log(totpKeyName);
const totpListElement = this.makeTOTPListElement(totpKeyName);
totpList.appendChild(totpListElement);
this.totpListElements[totpKeyName] = totpListElement;
@ -81,7 +82,7 @@ export class TOTPViewPage extends Page {
}, 3000) as unknown as number;
}
cleanup(): void {
async cleanup(): Promise<void> {
clearInterval(this.refresher);
this.totpListElements = {};
}

View file

@ -13,13 +13,13 @@ export class TransitDecryptPage extends Page {
super();
}
goBack(): void {
changePage("TRANSIT_VIEW_SECRET");
async goBack(): Promise<void> {
await changePage("TRANSIT_VIEW_SECRET");
}
transitDecryptForm: HTMLFormElement;
render(): void {
async render(): Promise<void> {
setTitleElement(pageState);
setPageContent(
makeElement({

View file

@ -13,13 +13,13 @@ export class TransitEncryptPage extends Page {
super();
}
goBack(): void {
changePage("TRANSIT_VIEW_SECRET");
async goBack(): Promise<void> {
await changePage("TRANSIT_VIEW_SECRET");
}
transitEncryptForm: HTMLFormElement;
render(): void {
async render(): Promise<void> {
setTitleElement(pageState);
setPageContent(
makeElement({

View file

@ -17,8 +17,8 @@ export class TransitRewrapPage extends Page {
super();
}
goBack(): void {
changePage("TRANSIT_VIEW_SECRET");
async goBack(): Promise<void> {
await changePage("TRANSIT_VIEW_SECRET");
}
transitRewrapForm: HTMLFormElement;

View file

@ -11,8 +11,8 @@ export class TransitViewPage extends Page {
super();
}
goBack(): void {
changePage("HOME");
async goBack(): Promise<void> {
await changePage("HOME");
}
async render(): Promise<void> {
@ -28,7 +28,7 @@ export class TransitViewPage extends Page {
text: "New",
class: ["uk-button", "uk-button-primary", "uk-margin-bottom"],
onclick: () => {
changePage("TRANSIT_NEW_KEY");
void changePage("TRANSIT_NEW_KEY");
},
});
transitViewContent.appendChild(newButton);
@ -49,7 +49,7 @@ export class TransitViewPage extends Page {
text: secret,
onclick: () => {
pageState.currentSecret = secret;
changePage("TRANSIT_VIEW_SECRET");
void changePage("TRANSIT_VIEW_SECRET");
},
}),
});

View file

@ -11,8 +11,8 @@ export class TransitViewSecretPage extends Page {
super();
}
goBack(): void {
changePage("TRANSIT_VIEW");
async goBack(): Promise<void> {
await changePage("TRANSIT_VIEW");
}
async render(): Promise<void> {
@ -33,7 +33,7 @@ export class TransitViewSecretPage extends Page {
icon: "lock",
iconText: i18next.t("transit_view_encrypt_icon_text"),
onclick: () => {
changePage("TRANSIT_ENCRYPT");
void changePage("TRANSIT_ENCRYPT");
},
}),
Tile({
@ -43,7 +43,7 @@ export class TransitViewSecretPage extends Page {
icon: "mail",
iconText: i18next.t("transit_view_decrypt_icon_text"),
onclick: () => {
changePage("TRANSIT_DECRYPT");
void changePage("TRANSIT_DECRYPT");
},
}),
Tile({
@ -53,7 +53,7 @@ export class TransitViewSecretPage extends Page {
icon: "code",
iconText: i18next.t("transit_view_rewrap_icon_text"),
onclick: () => {
changePage("TRANSIT_REWRAP");
void changePage("TRANSIT_REWRAP");
},
}),
],

View file

@ -26,7 +26,7 @@ export class UnsealPage extends Page {
unsealInputContent: HTMLElement;
unsealKeyForm: HTMLFormElement;
cleanup(): void {
async cleanup(): Promise<void> {
this.deinitWebcam();
clearInterval(this.refresher);
}
@ -169,7 +169,7 @@ export class UnsealPage extends Page {
progressBar.max = keysNeeded;
if (!data.sealed) {
progressBar.value = keysNeeded;
changePage("HOME");
void changePage("HOME");
}
}

View file

@ -15,7 +15,7 @@ declare global {
}
// Please empty this function before committing.
export function playground(): void {
export async function playground(): Promise<void> {
console.log("Welcome to Playground!");
window.pageState = pageState;
window.i18next = i18next;

View file

@ -4,20 +4,17 @@ export class Page {
constructor() {
// Do Nothing
}
render(): unknown {
// Do Nothing
return null;
}
async render(): Promise<void> {}
get name(): string {
return "Page";
}
get titleSuffix(): string {
return "";
}
goBack(): void {
changePage("HOME");
async goBack(): Promise<void> {
await changePage("HOME");
}
cleanup(): void {
async cleanup(): Promise<void> {
// Do Nothing
}
}