A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
web: stop lazy loading fs
Abdullah Atta53e4bda69d2e
diff --git a/apps/web/src/common/attachments.ts b/apps/web/src/common/attachments.tsindex 8cd5bfd8f..493c26b26 100644--- a/apps/web/src/common/attachments.ts+++ b/apps/web/src/common/attachments.ts@@ -18,10 +18,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.*/import { strings } from "@notesnook/intl";-import { lazify } from "../utils/lazify";import { logger } from "../utils/logger";import { showToast } from "../utils/toast";import { db } from "./db";+import { checkUpload, decryptFile, saveFile } from "../interfaces/fs";async function download(hash: string, groupId?: string) {const attachment = await db.attachments.attachment(hash);@@ -50,15 +50,13 @@ export async function saveAttachment(hash: string) {if (!response) return;const { attachment, key } = response;- await lazify(import("../interfaces/fs"), ({ saveFile }) =>- saveFile(attachment.hash, {- key,- iv: attachment.iv,- name: attachment.filename,- type: attachment.mimeType,- isUploaded: !!attachment.dateUploaded- })- );+ await saveFile(attachment.hash, {+ key,+ iv: attachment.iv,+ name: attachment.filename,+ type: attachment.mimeType,+ isUploaded: !!attachment.dateUploaded+ });} catch (e) {console.error(e);showToast(@@ -92,15 +90,14 @@ export async function downloadAttachment<if (type === "base64" || type === "text")return (await db.attachments.read(hash, type)) as TOutputType;- const blob = await lazify(import("../interfaces/fs"), ({ decryptFile }) =>- decryptFile(attachment.hash, {- key,- iv: attachment.iv,- name: attachment.filename,- type: attachment.mimeType,- isUploaded: !!attachment.dateUploaded- })- );+ const blob = await decryptFile(attachment.hash, {+ key,+ iv: attachment.iv,+ name: attachment.filename,+ type: attachment.mimeType,+ isUploaded: !!attachment.dateUploaded+ });+logger.debug("Attachment decrypted", { hash });if (!blob) return;@@ -121,9 +118,7 @@ export async function checkAttachment(hash: string) {if (!attachment) return { failed: "Attachment not found." };try {- await lazify(import("../interfaces/fs"), ({ checkUpload }) =>- checkUpload(hash, attachment.chunkSize, attachment.size)- );+ await checkUpload(hash, attachment.chunkSize, attachment.size);await db.attachments.markAsFailed(attachment.id);} catch (e) {const reason = e instanceof Error ? e.message : "Unknown error.";diff --git a/apps/web/src/common/db.ts b/apps/web/src/common/db.tsindex 2e620cd71..468eb3392 100644--- a/apps/web/src/common/db.ts+++ b/apps/web/src/common/db.ts@@ -27,12 +27,11 @@ import { generatePassword } from "../utils/password-generator";import { deriveKey, useKeyStore } from "../interfaces/key-store";import { logManager } from "@notesnook/core";import Config from "../utils/config";+import { FileStorage } from "../interfaces/fs";const db = database;async function initializeDatabase(persistence: DatabasePersistence) {logger.measure("Database initialization");-- const { FileStorage } = await import("../interfaces/fs");const { Compressor } = await import("../utils/compressor");let databaseKey = await useKeyStore.getState().getValue("databaseKey");diff --git a/apps/web/src/components/editor/picker.ts b/apps/web/src/components/editor/picker.tsindex 4abca9021..8f89e355a 100644--- a/apps/web/src/components/editor/picker.ts+++ b/apps/web/src/components/editor/picker.ts@@ -28,6 +28,11 @@ import { Attachment } from "@notesnook/editor";import { ImagePickerDialog } from "../../dialogs/image-picker-dialog";import { BuyDialog } from "../../dialogs/buy-dialog";import { strings } from "@notesnook/intl";+import {+ getUploadedFileSize,+ hashStream,+ writeEncryptedFile+} from "../../interfaces/fs";const FILE_SIZE_LIMIT = 500 * 1024 * 1024;const IMAGE_SIZE_LIMIT = 50 * 1024 * 1024;@@ -172,9 +177,6 @@ async function addAttachment(file: File,options: AddAttachmentOptions = {}): Promise<string> {- const { getUploadedFileSize, hashStream, writeEncryptedFile } = await import(- "../../interfaces/fs"- );const { expectedFileHash, showProgress = true } = options;let forceWrite = options.forceWrite;diff --git a/apps/web/src/utils/importer.ts b/apps/web/src/utils/importer.tsindex 26951c4b8..2a973f330 100644--- a/apps/web/src/utils/importer.ts+++ b/apps/web/src/utils/importer.ts@@ -29,7 +29,7 @@ import {NOTE_DATA_FILENAME} from "@notesnook-importer/core/dist/src/utils/note-stream";import { path } from "@notesnook-importer/core/dist/src/utils/path";-import { ZipEntry, createUnzipIterator } from "./streams/unzip-stream";+import { hashBuffer, writeEncryptedFile } from "../interfaces/fs";export async function* importFiles(zipFiles: File[]) {for (const zip of zipFiles) {@@ -74,8 +74,6 @@ async function processAttachment(if (!name || attachments[name] || (await db.attachments?.exists(name)))return;- const { hashBuffer, writeEncryptedFile } = await import("../interfaces/fs");-const data = await entry.arrayBuffer();const { hash } = await hashBuffer(new Uint8Array(data));if (hash !== name) {diff --git a/apps/web/src/utils/streams/attachment-stream.ts b/apps/web/src/utils/streams/attachment-stream.tsindex 463c1bcf7..a4d9dd7e8 100644--- a/apps/web/src/utils/streams/attachment-stream.ts+++ b/apps/web/src/utils/streams/attachment-stream.ts@@ -19,11 +19,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.import { getFileNameWithExtension, Attachment } from "@notesnook/core";import { db } from "../../common/db";-import { lazify } from "../lazify";import { showToast } from "../toast";import { makeUniqueFilename } from "./utils";import { ZipFile } from "./zip-stream";import { logger } from "../logger";+import { decryptFile } from "../../interfaces/fs";export const METADATA_FILENAME = "metadata.json";const GROUP_ID = "all-attachments";@@ -62,17 +62,13 @@ export class AttachmentStream extends ReadableStream<ZipFile> {const key = await db.attachments.decryptKey(attachment.key);if (!key) return;- const file = await lazify(- import("../../interfaces/fs"),- ({ decryptFile }) =>- decryptFile(attachment.hash, {- key,- iv: attachment.iv,- name: attachment.filename,- type: attachment.mimeType,- isUploaded: !!attachment.dateUploaded- })- );+ const file = await decryptFile(attachment.hash, {+ key,+ iv: attachment.iv,+ name: attachment.filename,+ type: attachment.mimeType,+ isUploaded: !!attachment.dateUploaded+ });if (file) {const filePath: string =diff --git a/apps/web/src/utils/streams/export-stream.ts b/apps/web/src/utils/streams/export-stream.tsindex 39cce00b6..e61e6a15f 100644--- a/apps/web/src/utils/streams/export-stream.ts+++ b/apps/web/src/utils/streams/export-stream.ts@@ -20,7 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.import { ExportableItem } from "@notesnook/common";import { db } from "../../common/db";import { ZipFile } from "./zip-stream";-import { lazify } from "../lazify";+import { streamingDecryptFile } from "../../interfaces/fs";export class ExportStream extends TransformStream<ExportableItem | Error,@@ -44,17 +44,14 @@ export class ExportStream extends TransformStream<.downloadFile("exports", item.data.hash, item.data.chunkSize);const key = await db.attachments.decryptKey(item.data.key);if (!key) return;- const stream = await lazify(- import("../../interfaces/fs"),- ({ streamingDecryptFile }) =>- streamingDecryptFile(item.data.hash, {- key,- iv: item.data.iv,- name: item.data.filename,- type: item.data.mimeType,- isUploaded: !!item.data.dateUploaded- })- );+ const stream = await streamingDecryptFile(item.data.hash, {+ key,+ iv: item.data.iv,+ name: item.data.filename,+ type: item.data.mimeType,+ isUploaded: !!item.data.dateUploaded+ });+if (!stream) return;controller.enqueue({ ...item, data: stream });report({