Skip to content

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

core: use client given compression module

Abdullah Atta8f5ebe315e66
diff --git a/apps/web/src/common/db.js b/apps/web/src/common/db.js
index 0b25a7f63..c321d0b72 100644
--- a/apps/web/src/common/db.js
+++ b/apps/web/src/common/db.js
@@ -30,7 +30,13 @@ async function initializeDatabase(persistence) {
const { default: Database } = await import("@notesnook/core/api");
const { default: FS } = await import("../interfaces/fs");
- db = new Database(new NNStorage("Notesnook", persistence), EventSource, FS);
+ const { Compressor } = await import("../utils/compressor");
+ db = new Database(
+ new NNStorage("Notesnook", persistence),
+ EventSource,
+ FS,
+ new Compressor()
+ );
// if (isTesting()) {
db.host({
diff --git a/packages/core/utils/__tests__/compression.test.js b/packages/core/__mocks__/compressor.mock.js
similarity index 63%
rename from packages/core/utils/__tests__/compression.test.js
rename to packages/core/__mocks__/compressor.mock.js
index d27c1bb2c..762c68d7e 100644
--- a/packages/core/utils/__tests__/compression.test.js
+++ b/packages/core/__mocks__/compressor.mock.js
@@ -17,13 +17,21 @@ 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 { TEST_NOTE } from "../../__tests__/utils";
-import { compress, decompress } from "../compression";
+const zlib = require("zlib");
+const utils = require("util");
-test("String should compress and decompress", () => {
- let compressed = compress(TEST_NOTE.content.data);
- expect(compressed).not.toBe(TEST_NOTE.content.data);
+const gzipAsync = utils.promisify(zlib.gzip);
+const gunzipAsync = utils.promisify(zlib.gunzip);
- let decompressed = decompress(compressed);
- expect(decompressed).toBe(TEST_NOTE.content.data);
-});
+async function compress(data) {
+ return (await gzipAsync(data, { level: 6 })).toString("base64");
+}
+
+async function decompress(data) {
+ return (await gunzipAsync(Buffer.from(data, "base64"))).toString("utf-8");
+}
+
+module.exports = {
+ compress,
+ decompress
+};
diff --git a/packages/core/__tests__/utils/index.js b/packages/core/__tests__/utils/index.js
index a9c362c18..3ca71a545 100644
--- a/packages/core/__tests__/utils/index.js
+++ b/packages/core/__tests__/utils/index.js
@@ -22,6 +22,7 @@ import StorageInterface from "../../__mocks__/storage.mock";
import dayjs from "dayjs";
import { groupArray } from "../../utils/grouping";
import FS from "../../__mocks__/fs.mock";
+import Compressor from "../../__mocks__/compressor.mock";
const TEST_NOTEBOOK = {
title: "Test Notebook",
@@ -36,7 +37,7 @@ const TEST_NOTEBOOK2 = {
};
function databaseTest() {
- let db = new DB(StorageInterface, null, FS);
+ let db = new DB(StorageInterface, null, FS, Compressor);
return db.init().then(() => db);
}
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index cdeb75c8f..3e9ca0166 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -60,7 +60,7 @@ class Database {
* @param {any} storage
* @param {EventSource} eventsource
*/
- constructor(storage, eventsource, fs) {
+ constructor(storage, eventsource, fs, compressor) {
/**
* @type {EventSource}
*/
@@ -69,6 +69,7 @@ class Database {
this.lastHeartbeat = undefined; // { local: 0, server: 0 };
this.timeErrorFailures = 0;
this.eventManager = new EventManager();
+ this.compressor = compressor;
this.storage = new Storage(storage);
this.fs = new FileStorage(fs, storage);
diff --git a/packages/core/api/sync/__tests__/sync.test.skip.js b/packages/core/api/sync/__tests__/sync.test.skip.js
index b10749199..f30434ae9 100644
--- a/packages/core/api/sync/__tests__/sync.test.skip.js
+++ b/packages/core/api/sync/__tests__/sync.test.skip.js
@@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import Database from "../../index";
import { NodeStorageInterface } from "../../../__mocks__/node-storage.mock";
import FS from "../../../__mocks__/fs.mock";
+import Compressor from "../../../__mocks__/compressor.mock";
import { CHECK_IDS, EV, EVENTS } from "../../../common";
import EventSource from "eventsource";
import { delay } from "../../../__tests__/utils";
@@ -338,7 +339,12 @@ async function initializeDevice(id, capabilities = []) {
};
});
- const device = new Database(new NodeStorageInterface(), EventSource, FS);
+ const device = new Database(
+ new NodeStorageInterface(),
+ EventSource,
+ FS,
+ Compressor
+ );
// device.host({
// API_HOST: "http://192.168.10.29:5264",
// AUTH_HOST: "http://192.168.10.29:8264",
diff --git a/packages/core/collections/session-content.js b/packages/core/collections/session-content.js
index ddceaaa4d..1bada7c3b 100644
--- a/packages/core/collections/session-content.js
+++ b/packages/core/collections/session-content.js
@@ -18,7 +18,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { tinyToTiptap } from "../migrations";
-import { compress, decompress } from "../utils/compression";
import { makeSessionContentId } from "../utils/id";
import Collection from "./collection";
@@ -34,7 +33,9 @@ export default class SessionContent extends Collection {
*/
async add(sessionId, content, locked) {
if (!sessionId || !content) return;
- let data = locked ? content.data : compress(content.data);
+ let data = locked
+ ? content.data
+ : await this._db.compressor.compress(content.data);
await this._collection.addItem({
type: "sessioncontent",
@@ -57,13 +58,17 @@ export default class SessionContent extends Collection {
let session = await this._collection.getItem(sessionContentId);
if (session.contentType === "tiny" && session.compressed) {
- session.compressed = compress(tinyToTiptap(decompress(session.data)));
+ session.compressed = await this._db.compressor.compress(
+ tinyToTiptap(await this._db.compressor.decompress(session.data))
+ );
session.contentType = "tiptap";
await this._collection.addItem(session);
}
return {
- data: session.compressed ? decompress(session.data) : session.data,
+ data: session.compressed
+ ? await this._db.compressor.decompress(session.data)
+ : session.data,
type: session.contentType
};
}
diff --git a/packages/core/package-lock.json b/packages/core/package-lock.json
index fcd8e061a..86671a03a 100644
--- a/packages/core/package-lock.json
+++ b/packages/core/package-lock.json
@@ -13,10 +13,8 @@
"@microsoft/signalr-protocol-msgpack": "^6.0.7",
"@streetwriters/showdown": "^3.0.1-alpha",
"async-mutex": "^0.3.2",
- "base64-arraybuffer": "^1.0.2",
"dayjs": "^1.11.3",
"entities": "^4.3.1",
- "fflate": "^0.7.3",
"html-to-text": "github:thecodrr/node-html-to-text",
"htmlparser2": "github:thecodrr/htmlparser2",
"linkedom": "^0.14.17",
@@ -4427,13 +4425,6 @@
"dev": true,
"license": "MIT"
},
- "node_modules/base64-arraybuffer": {
- "version": "1.0.2",
- "license": "MIT",
- "engines": {
- "node": ">= 0.6.0"
- }
- },
"node_modules/bluebird": {
"version": "3.0.6",
"dev": true,
@@ -5119,10 +5110,6 @@
"node": ">=8"
}
},
- "node_modules/fflate": {
- "version": "0.7.4",
- "license": "MIT"
- },
"node_modules/fill-range": {
"version": "7.0.1",
"dev": true,
@@ -12076,9 +12063,6 @@
"version": "1.0.2",
"dev": true
},
- "base64-arraybuffer": {
- "version": "1.0.2"
- },
"bluebird": {
"version": "3.0.6",
"dev": true
@@ -12492,9 +12476,6 @@
"tough-cookie": "^2.3.3 || ^3.0.1 || ^4.0.0"
}
},
- "fflate": {
- "version": "0.7.4"
- },
"fill-range": {
"version": "7.0.1",
"dev": true,
diff --git a/packages/core/package.json b/packages/core/package.json
index 6f23f10b0..101b1ea01 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -38,10 +38,8 @@
"@notesnook/logger": "*",
"@streetwriters/showdown": "^3.0.1-alpha",
"async-mutex": "^0.3.2",
- "base64-arraybuffer": "^1.0.2",
"dayjs": "^1.11.3",
"entities": "^4.3.1",
- "fflate": "^0.7.3",
"htmlparser2": "github:thecodrr/htmlparser2",
"html-to-text": "github:thecodrr/node-html-to-text",
"linkedom": "^0.14.17",
diff --git a/packages/core/utils/compression.js b/packages/core/utils/compression.js
deleted file mode 100644
index 09f049432..000000000
--- a/packages/core/utils/compression.js
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
-This file is part of the Notesnook project (https://notesnook.com/)
-
-Copyright (C) 2022 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/>.
-*/
-
-import { decode, encode } from "base64-arraybuffer";
-import { compressSync, decompressSync, strToU8, strFromU8 } from "fflate";
-
-/**
- *
- * @param {string} data
- * @returns {string | null} An object containing compressed data
- */
-export const compress = (data) => {
- try {
- return encode(compressSync(strToU8(data)).buffer);
- } catch (e) {
- return null;
- }
-};
-
-/**
- *
- * @param {string} compressed
- * @returns {string} decompressed string
- */
-export const decompress = (compressed) => {
- return strFromU8(decompressSync(new Uint8Array(decode(compressed))));
-};