Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: lazily load fs module

Abdullah Atta39afa98fce4a
diff --git a/apps/web/src/common/attachments.ts b/apps/web/src/common/attachments.ts
index d28dcfbc5..3191200d2 100644
--- a/apps/web/src/common/attachments.ts
+++ b/apps/web/src/common/attachments.ts
@@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import FS from "../interfaces/fs";
+import { lazify } from "../utils/lazify";
import { db } from "./db";
async function download(hash: string) {
@@ -42,13 +42,15 @@ export async function saveAttachment(hash: string) {
if (!response) return;
const { attachment, key } = response;
- await FS.saveFile(attachment.metadata.hash, {
- key,
- iv: attachment.iv,
- name: attachment.metadata.filename,
- type: attachment.metadata.type,
- isUploaded: !!attachment.dateUploaded
- });
+ await lazify(import("../interfaces/fs"), ({ default: FS }) =>
+ FS.saveFile(attachment.metadata.hash, {
+ key,
+ iv: attachment.iv,
+ name: attachment.metadata.filename,
+ type: attachment.metadata.type,
+ isUploaded: !!attachment.dateUploaded
+ })
+ );
}
type OutputTypeToReturnType = {
@@ -67,13 +69,15 @@ export async function downloadAttachment<
if (type === "base64" || type === "text")
return (await db.attachments?.read(hash, type)) as TOutputType;
- const blob = await FS.decryptFile(attachment.metadata.hash, {
- key,
- iv: attachment.iv,
- name: attachment.metadata.filename,
- type: attachment.metadata.type,
- isUploaded: !!attachment.dateUploaded
- });
+ const blob = await lazify(import("../interfaces/fs"), ({ default: FS }) =>
+ FS.decryptFile(attachment.metadata.hash, {
+ key,
+ iv: attachment.iv,
+ name: attachment.metadata.filename,
+ type: attachment.metadata.type,
+ isUploaded: !!attachment.dateUploaded
+ })
+ );
if (!blob) return;
return blob as TOutputType;
@@ -84,7 +88,9 @@ export async function checkAttachment(hash: string) {
if (!attachment) return { failed: "Attachment not found." };
try {
- const size = await FS.getUploadedFileSize(hash);
+ const size = await lazify(import("../interfaces/fs"), ({ default: FS }) =>
+ FS.getUploadedFileSize(hash)
+ );
if (size <= 0) return { failed: "File length is 0." };
} catch (e) {
return { failed: e instanceof Error ? e.message : "Unknown error." };
diff --git a/apps/web/src/common/db.ts b/apps/web/src/common/db.ts
index 83555bd26..e726847ad 100644
--- a/apps/web/src/common/db.ts
+++ b/apps/web/src/common/db.ts
@@ -21,6 +21,7 @@ import { EventSourcePolyfill as EventSource } from "event-source-polyfill";
import { DatabasePersistence, NNStorage } from "../interfaces/storage";
import { logger } from "../utils/logger";
import type Database from "@notesnook/core/dist/api";
+import { showMigrationDialog } from "./dialog-controller";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
@@ -70,7 +71,6 @@ async function initializeDatabase(persistence: DatabasePersistence) {
logger.measure("Database initialization");
if (db.migrations?.required()) {
- const { showMigrationDialog } = await import("./dialog-controller");
await showMigrationDialog();
}
diff --git a/apps/web/src/components/editor/picker.ts b/apps/web/src/components/editor/picker.ts
index 2fd92a93e..169d14e66 100644
--- a/apps/web/src/components/editor/picker.ts
+++ b/apps/web/src/components/editor/picker.ts
@@ -23,9 +23,9 @@ import { db } from "../../common/db";
import { showBuyDialog } from "../../common/dialog-controller";
import { TaskManager } from "../../common/task-manager";
import { isUserPremium } from "../../hooks/use-is-user-premium";
-import fs from "../../interfaces/fs";
import { showToast } from "../../utils/toast";
import { showFilePicker } from "../../utils/file-picker";
+import { lazify } from "../../utils/lazify";
const FILE_SIZE_LIMIT = 500 * 1024 * 1024;
const IMAGE_SIZE_LIMIT = 50 * 1024 * 1024;
@@ -154,11 +154,12 @@ async function addAttachment(
const { expectedFileHash, forceWrite, showProgress = true } = options;
const action = async () => {
- const reader: ReadableStreamReader<Uint8Array> = (
- file.stream() as unknown as ReadableStream<Uint8Array>
- ).getReader();
+ const reader = file.stream().getReader();
- const { hash, type: hashType } = await fs.hashStream(reader);
+ const { hash, type: hashType } = await lazify(
+ import("../../interfaces/fs"),
+ ({ default: FS }) => FS.hashStream(reader)
+ );
reader.releaseLock();
if (expectedFileHash && hash !== expectedFileHash)
@@ -169,7 +170,10 @@ async function addAttachment(
if (forceWrite || !exists) {
const key: SerializedKey = await getEncryptionKey();
- const output = await fs.writeEncryptedFile(file, key, hash);
+ const output = await lazify(
+ import("../../interfaces/fs"),
+ ({ default: FS }) => FS.writeEncryptedFile(file, key, hash)
+ );
if (!output) throw new Error("Could not encrypt file.");
if (forceWrite && exists) await db.attachments?.reset(hash);
diff --git a/apps/web/src/index.tsx b/apps/web/src/index.tsx
index ec6eb2039..75c05c7a9 100644
--- a/apps/web/src/index.tsx
+++ b/apps/web/src/index.tsx
@@ -24,6 +24,7 @@ import { loadDatabase } from "./hooks/use-database";
import { AppEventManager, AppEvents } from "./common/app-events";
import { BaseThemeProvider } from "./components/theme-provider";
import { register } from "./utils/stream-saver/mitm";
+import { getServiceWorkerVersion } from "./utils/version";
renderApp();
@@ -60,7 +61,6 @@ async function initializeServiceWorker() {
serviceWorker.register({
onUpdate: async (registration: ServiceWorkerRegistration) => {
if (!registration.waiting) return;
- const { getServiceWorkerVersion } = await import("./utils/version");
const { formatted } = await getServiceWorkerVersion(
registration.waiting
);
diff --git a/apps/web/src/utils/importer.ts b/apps/web/src/utils/importer.ts
index cb1addd33..647fa9fb5 100644
--- a/apps/web/src/utils/importer.ts
+++ b/apps/web/src/utils/importer.ts
@@ -18,8 +18,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { db } from "../common/db";
-import FS from "../interfaces/fs";
-
import type { Note, Notebook } from "@notesnook-importer/core";
import {
ATTACHMENTS_DIRECTORY_NAME,
@@ -30,6 +28,7 @@ import { Reader, Entry } from "./zip-reader";
import { path } from "@notesnook-importer/core/dist/src/utils/path";
export async function* importFiles(zipFiles: File[]) {
+ const { default: FS } = await import("../interfaces/fs");
for (const zip of zipFiles) {
let count = 0;
let filesRead = 0;
diff --git a/apps/web/src/utils/lazify.ts b/apps/web/src/utils/lazify.ts
new file mode 100644
index 000000000..92df81d33
--- /dev/null
+++ b/apps/web/src/utils/lazify.ts
@@ -0,0 +1,26 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+export async function lazify<T, R>(
+ loader: Promise<T>,
+ action: (module: T) => R | Promise<R>
+): Promise<R> {
+ const module = await loader;
+ return await action(module);
+}
diff --git a/apps/web/src/utils/streams/attachment-stream.ts b/apps/web/src/utils/streams/attachment-stream.ts
index 89373c65f..c1ee3fc7b 100644
--- a/apps/web/src/utils/streams/attachment-stream.ts
+++ b/apps/web/src/utils/streams/attachment-stream.ts
@@ -17,8 +17,8 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { decryptFile } from "../../interfaces/fs";
import { db } from "../../common/db";
+import { lazify } from "../lazify";
import { ZipFile } from "./zip-stream";
export const METADATA_FILENAME = "metadata.json";
@@ -55,13 +55,17 @@ export class AttachmentStream extends ReadableStream<ZipFile> {
);
const key = await db.attachments?.decryptKey(attachment.key);
- const file = await decryptFile(attachment.metadata.hash, {
- key,
- iv: attachment.iv,
- name: attachment.metadata.filename,
- type: attachment.metadata.type,
- isUploaded: !!attachment.dateUploaded
- });
+ const file = await lazify(
+ import("../../interfaces/fs"),
+ ({ decryptFile }) =>
+ decryptFile(attachment.metadata.hash, {
+ key,
+ iv: attachment.iv,
+ name: attachment.metadata.filename,
+ type: attachment.metadata.type,
+ isUploaded: !!attachment.dateUploaded
+ })
+ );
if (file) {
const filePath: string = attachment.metadata.filename;
diff --git a/apps/web/src/utils/zip.ts b/apps/web/src/utils/zip.ts
index 3b65258f0..e67e2066d 100644
--- a/apps/web/src/utils/zip.ts
+++ b/apps/web/src/utils/zip.ts
@@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { AsyncZippable } from "fflate";
+import { AsyncZippable, zip as zipAsync } from "fflate";
import { sanitizeFilename } from "@notesnook/common";
const textEncoder = new TextEncoder();
@@ -34,9 +34,8 @@ async function zip(files: File[], format: string): Promise<Uint8Array> {
{ mtime: file.date }
];
});
- const { zip } = await import("fflate");
return new Promise((resolve, reject) =>
- zip(obj, (err, data) => (err ? reject(err) : resolve(data)))
+ zipAsync(obj, (err, data) => (err ? reject(err) : resolve(data)))
);
}
export { zip };