Skip to content

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

initial

thecodrrbb675e9da237
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index 22fc027c0..b89c29274 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -101,21 +101,27 @@ class Database {
this.offers = new Offers();
this.debug = new Debug();
+ const factory = () => this.user.getEncryptionKey();
// collections
/** @type {Notes} */
- this.notes = await Notes.new(this, "notes", true, true);
+ this.notes = await Notes.new(this, "notes", factory, true, true);
/** @type {Notebooks} */
- this.notebooks = await Notebooks.new(this, "notebooks");
+ this.notebooks = await Notebooks.new(this, "notebooks", factory);
/** @type {Tags} */
- this.tags = await Tags.new(this, "tags");
+ this.tags = await Tags.new(this, "tags", factory);
/** @type {Tags} */
- this.colors = await Tags.new(this, "colors");
+ this.colors = await Tags.new(this, "colors", factory);
/** @type {Content} */
- this.content = await Content.new(this, "content", false);
+ this.content = await Content.new(this, "content", factory, false);
/** @type {Attachments} */
- this.attachments = await Attachments.new(this, "attachments");
+ this.attachments = await Attachments.new(this, "attachments", factory);
/**@type {NoteHistory} */
- this.noteHistory = await NoteHistory.new(this, "notehistory", false);
+ this.noteHistory = await NoteHistory.new(
+ this,
+ "notehistory",
+ factory,
+ false
+ );
this.trash = new Trash(this);
@@ -213,11 +219,11 @@ class Database {
await this.user.fetchUser(true);
EV.publish(EVENTS.userEmailConfirmed);
break;
- case "sync":
- if (!(await checkIsUserPremium(CHECK_IDS.databaseSync))) break;
+ // case "sync":
+ // if (!(await checkIsUserPremium(CHECK_IDS.databaseSync))) break;
- await this.syncer.remoteSync(data);
- break;
+ // await this.syncer.remoteSync(data);
+ // break;
}
};
});
diff --git a/packages/core/api/sync/collector.js b/packages/core/api/sync/collector.js
index f0bc06fb5..12a7968d9 100644
--- a/packages/core/api/sync/collector.js
+++ b/packages/core/api/sync/collector.js
@@ -16,10 +16,10 @@ class Collector {
this._lastSyncedTimestamp = lastSyncedTimestamp;
this.key = await this._db.user.getEncryptionKey();
return {
- notes: await this._encrypt(this._collect(this._db.notes.raw)),
- notebooks: await this._encrypt(this._collect(this._db.notebooks.raw)),
- content: await this._encrypt(this._collect(await this._db.content.all())),
- attachments: await this._encrypt(this._collect(this._db.attachments.all)),
+ notes: this._collect(await this._db.notes.encrypted()),
+ notebooks: this._collect(await this._db.notebooks.encrypted()),
+ content: this._collect(await this._db.content.encrypted()),
+ attachments: this._collect(await this._db.attachments.encrypted()),
settings: await this._encrypt(this._collect([this._db.settings.raw])),
vaultKey: await this._serialize(await this._db.vault._getKey()),
};
@@ -45,21 +45,19 @@ class Collector {
return array.reduce((prev, item) => {
if (!item || item.localOnly) return prev;
if (item.dateModified > this._lastSyncedTimestamp || item.migrated)
- prev.push(item);
+ prev.push(this._map(item));
return prev;
}, []);
}
- async _map(item) {
- // in case of resolved content
- delete item.resolved;
- // turn the migrated flag off so we don't keep syncing this item repeated
- delete item.migrated;
-
+ _map(item) {
return {
id: item.id,
v: CURRENT_DATABASE_VERSION,
- ...(await this._serialize(item)),
+ iv: item.iv,
+ cipher: item.cipher,
+ length: item.length,
+ alg: item.alg,
};
}
}
diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.js
index a40f53bf7..ed00a8326 100644
--- a/packages/core/api/sync/index.js
+++ b/packages/core/api/sync/index.js
@@ -39,6 +39,8 @@ import Collector from "./collector";
import Merger from "./merger";
import { areAllEmpty } from "./utils";
import { Mutex, withTimeout } from "async-mutex";
+import * as signalr from "@microsoft/signalr";
+import RealtimeMerger from "./realtimeMerger";
export default class Sync {
/**
@@ -49,9 +51,21 @@ export default class Sync {
this._db = db;
this._collector = new Collector(this._db);
this._merger = new Merger(this._db);
+ this._realtimeMerger = new RealtimeMerger(this._db);
this._tokenManager = new TokenManager(this._db.storage);
this._autoSyncTimeout = 0;
this._autoSyncInterval = 5000;
+ this._connection = new signalr.HubConnectionBuilder()
+ .withUrl(`${Constants.API_HOST}/hubs/sync`, {
+ accessTokenFactory: () => this._db.user.tokenManager.getAccessToken(),
+ })
+ .build();
+ this._connection.on("SyncItem", async (type, item) => {
+ this.stopAutoSync();
+ await this._realtimeMerger.mergeItem(type, JSON.parse(item));
+ EV.publish(EVENTS.appRefreshRequested);
+ await this.startAutoSync();
+ });
this.syncMutex = withTimeout(
new Mutex(),
@@ -66,25 +80,25 @@ export default class Sync {
return this.syncMutex
.runExclusive(() => {
this.stopAutoSync();
- return this._sync(full, force);
+ return this._realTimeSync(full, force);
})
.finally(() => this._afterSync());
}
- async remoteSync() {
- if (this.syncMutex.isLocked()) {
- this.hasNewChanges = true;
- return;
- }
- await this.syncMutex
- .runExclusive(async () => {
- this.stopAutoSync();
- this.hasNewChanges = false;
- if (await this._sync(true, false))
- EV.publish(EVENTS.appRefreshRequested);
- })
- .finally(() => this._afterSync());
- }
+ // async remoteSync() {
+ // if (this.syncMutex.isLocked()) {
+ // this.hasNewChanges = true;
+ // return;
+ // }
+ // await this.syncMutex
+ // .runExclusive(async () => {
+ // this.stopAutoSync();
+ // this.hasNewChanges = false;
+ // if (await this._realTimeSync(true, false))
+ // EV.publish(EVENTS.appRefreshRequested);
+ // })
+ // .finally(() => this._afterSync());
+ // }
async startAutoSync() {
if (!(await checkIsUserPremium(CHECK_IDS.databaseSync))) return;
@@ -153,6 +167,90 @@ export default class Sync {
return true;
}
+ _mapToIds(data) {
+ const ids = [];
+ for (let arrayKey in data) {
+ const array = data[arrayKey];
+ if (Array.isArray(array)) {
+ for (let item of array) {
+ ids.push(item.id);
+ }
+ }
+ }
+ return ids;
+ }
+
+ async _realTimeSync(full, force) {
+ if (this._connection.state !== signalr.HubConnectionState.Connected)
+ await this._connection.start();
+
+ let { lastSynced } = await this._performChecks();
+ if (force) lastSynced = 0;
+
+ // Real time sync is different from on-demand sync in a couple of ways:
+ // 1. Since everything is being done in real time, we have to send
+ // local changes before fetching remote changes.
+ // 2. All data is sent in a specific sequence:
+ // 1. Content
+ // 2. Attachments
+ // 3. Notes
+ // 4. Notebooks
+ // 5. Settings
+ // This reduces a lot of complexity in cases where only data has been synced
+ // partially.
+ // 3. Only one sync can run at a time for a single device. This allows for
+ // easy resumability.
+ // 4. Progress events are sent regularly
+
+ // We collect, encrypt, and ready local changes before asking
+ // the server for remote changes. This is done to ensure we
+ // don't accidentally send the remote changes back to the server.
+ const data = await this._collector.collect(lastSynced);
+ // save all the item ids before running the sync. this will ensure
+ // resumability.
+ // TODO: const itemIds = mapToIds(data);
+ // const syncData = { itemIds, lastSynced };
+ // await this._db.storage.write("running_sync", syncData);
+
+ // We update the local last synced time before fetching data
+ // from the server. This is necessary to ensure that if the user
+ // makes any local changes, it is not ignored in the next sync.
+ // This is also the last synced time that is set for later sync cycles.
+ const newLastSynced = Date.now();
+ if (full) {
+ await this._connection.send("FetchItems", lastSynced);
+ // TODO progress
+ var serverResponse = await new Promise((resolve) => {
+ this._connection.on("SyncCompleted", (result) => resolve(result));
+ });
+
+ await this._db.conflicts.check();
+ }
+
+ // All pending attachments are uploaded before anything else.
+ // This is done to ensure that when any note arrives on user's
+ // device, its attachments can be downloaded.
+ await this._uploadAttachments();
+
+ if (!areAllEmpty(data)) {
+ await this.sendItemsToServer(
+ "attachment",
+ data.attachments,
+ newLastSynced
+ );
+ await this.sendItemsToServer("content", data.content, newLastSynced);
+ await this.sendItemsToServer("note", data.notes, newLastSynced);
+ await this.sendItemsToServer("notebook", data.notebooks, newLastSynced);
+ await this.sendItemsToServer("settings", data.settings, newLastSynced);
+ await this.sendItemsToServer("vaultKey", [data.vaultKey], newLastSynced);
+ if (await this._connection.invoke("SyncCompleted", newLastSynced))
+ lastSynced = newLastSynced;
+ } else if (serverResponse) lastSynced = serverResponse.lastSynced;
+
+ await this._db.storage.write("lastSynced", lastSynced);
+ return true;
+ }
+
async _afterSync() {
if (!this.hasNewChanges) {
this.startAutoSync();
@@ -230,4 +328,17 @@ export default class Sync {
token
);
}
+
+ async sendItemsToServer(type, array, dateSynced) {
+ for (const item of array) {
+ if (!item) return;
+ const result = await this._connection.invoke(
+ "SyncItem",
+ type,
+ JSON.stringify(item),
+ dateSynced
+ );
+ if (result !== 1) throw new Error(`Failed to sync item: ${item.id}.`);
+ }
+ }
}
diff --git a/packages/core/api/sync/realtimeMerger.js b/packages/core/api/sync/realtimeMerger.js
new file mode 100644
index 000000000..dbb6646b2
--- /dev/null
+++ b/packages/core/api/sync/realtimeMerger.js
@@ -0,0 +1,259 @@
+import { migrations } from "../../migrations";
+import { areAllEmpty } from "./utils";
+import SparkMD5 from "spark-md5";
+import setManipulator from "../../utils/set";
+import { CURRENT_DATABASE_VERSION } from "../../common";
+
+class RealtimeMerger {
+ /**
+ *
+ * @param {import("../index").default} db
+ */
+ constructor(db) {
+ this._db = db;
+ this._mergeDefinition = {
+ settings: {
+ get: () => this._db.settings.raw,
+ set: (item) => this._db.settings.merge(item),
+ },
+ note: {
+ get: (id) => this._db.notes.note(id),
+ set: (item) => this._db.notes.merge(item),
+ },
+ notebook: {
+ get: (id) => this._db.notebooks.notebook(id),
+ set: (item) => this._db.notebooks.merge(item),
+ },
+ content: {
+ get: (id) => this._db.content.raw(id),
+ set: (item) => this._db.content.add(item),
+ conflict: async (local, remote) => {
+ let note = this._db.notes.note(local.noteId);
+ if (!note || !note.data) return;
+ note = note.data;
+
+ // if hashes are equal do nothing
+ if (
+ !note.locked &&
+ (!remote ||
+ !local ||
+ !local.data ||
+ !remote.data ||
+ remote.data === "undefined" || //TODO not sure about this
+ SparkMD5.hash(local.data) === SparkMD5.hash(remote.data))
+ )
+ return;
+
+ if (remote.deleted || local.deleted || note.locked) {
+ // if note is locked or content is deleted we keep the most recent version.
+ if (remote.dateModified > local.dateModified)
+ await this._db.content.add({ id: local.id, ...remote });
+ } else {
+ // otherwise we trigger the conflicts
+ await this._db.content.add({ ...local, conflicted: remote });
+ await this._db.notes.add({ id: local.noteId, conflicted: true });
+ await this._db.storage.write("hasConflicts", true);
+ }
+ },
+ },
+ attachment: {
+ set: async (item) => {
+ const remoteAttachment = await this._deserialize(item);
+ const localAttachment = this._db.attachments.attachment(
+ remoteAttachment.metadata.hash
+ );
+ if (
+ localAttachment &&
+ localAttachment.dateUploaded !== remoteAttachment.dateUploaded
+ ) {
+ const noteIds = localAttachment.noteIds.slice();
+ const isRemoved = await this._db.attachments.remove(
+ localAttachment.metadata.hash,
+ true
+ );
+ if (!isRemoved)
+ throw new Error(
+ "Conflict could not be resolved in one of the attachments."
+ );
+ remoteAttachment.noteIds = setManipulator.union(
+ remoteAttachment.noteIds,
+ noteIds
+ );
+ }
+ await this._db.attachments.merge(remoteAttachment);
+ },
+ },
+ vaultKey: {
+ set: async (vaultKey) =>
+ this._db.vault._setKey(await this._deserialize(vaultKey, false)),
+ },
+ };
+ }
+
+ _migrate(deserialized, version) {
+ // it is a locked note, bail out.
+ if (deserialized.alg && deserialized.cipher) return deserialized;
+
+ let type = deserialized.type;
+ if (!type && deserialized.data) type = "tiny";
+
+ if (!migrations[version]) {
+ throw new Error(
+ version > CURRENT_DATABASE_VERSION
+ ? `Cannot migrate item to v${version}. Please update your client to the latest version.`
+ : `Could not migrate item to v${version}. Please report this bug to the developers.`
+ );
+ }
+
+ const migrate = migrations[version][type];
+ if (migrate) return migrate(deserialized);
+ return deserialized;
+ }
+
+ async _deserialize(item, migrate = true) {
+ const decrypted = await this._db.storage.decrypt(this.key, item);
+ if (!decrypted) {
+ throw new Error("Decrypted item cannot be undefined.");
+ }
+
+ const deserialized = JSON.parse(decrypted);
+ deserialized.remote = true;
+ if (!migrate) return deserialized;
+ return this._migrate(deserialized, item.v);
+ }
+
+ async _mergeItem(remoteItem, get, add) {
+ remoteItem = await this._deserialize(remoteItem);
+ let localItem = await get(remoteItem.id);
+ console.log("MERGING!", localItem, remoteItem);
+ if (!localItem || remoteItem.dateModified > localItem.dateModified) {
+ await add(remoteItem);
+ }
+ }
+
+ // async _mergeArray(array, get, set) {
+ // if (!array) return;
+ // for (let item of array) {
+ // await this._mergeItem(item, get, set);
+ // }
+ // }
+
+ async _mergeItemWithConflicts(remoteItem, get, add, markAsConflicted) {
+ remoteItem = await this._deserialize(remoteItem);
+ let localItem = await get(remoteItem.id);
+
+ if (!localItem) {
+ await add(remoteItem);
+ } else {
+ const isResolved = localItem.dateResolved === remoteItem.dateModified;
+ const isModified = localItem.dateModified > this._lastSynced;
+
+ if (isModified && !isResolved) {
+ // If time difference between local item's edits & remote item's edits
+ // is less than 1 minute, we shouldn't trigger a merge conflict; instead
+ // we will keep the most recently changed item.
+ const timeDiff =
+ Math.max(remoteItem.dateModified, localItem.dateModified) -
+ Math.min(remoteItem.dateModified, localItem.dateModified);
+ const ONE_MINUTE = 60 * 1000;
+ if (timeDiff < ONE_MINUTE) {
+ if (remoteItem.dateModified > localItem.dateModified) {
+ await add(remoteItem);
+ }
+ return;
+ }
+
+ await markAsConflicted(localItem, remoteItem);
+ } else if (!isResolved) {
+ await add(remoteItem);
+ }
+ }
+ }
+
+ async mergeItem(type, item) {
+ const definition = this._mergeDefinition[type];
+ if (!type || !item || !definition) return;
+
+ if (!this.key) this.key = await this._db.user.getEncryptionKey();
+ if (!this.key.key || !this.key.salt) {
+ await this._db.user.logout(true, "User encryption key not generated.");
+ throw new Error("User encryption key not generated. Please relogin.");
+ }
+
+ if (definition.conflict) {
+ await this._mergeItemWithConflicts(
+ item,
+ definition.get,
+ definition.set,
+ definition.conflict
+ );
+ } else if (definition.get && definition.set) {
+ console.log("MERGIGNG");
+ await this._mergeItem(item, definition.get, definition.set);
+ } else if (!definition.get && definition.set) {
+ await definition.set(item);
+ }
+ }
+
+ // async _mergeArrayWithConflicts(array, mergeItem) {
+ // if (!array) return;
+ // for (let item of array) {
+ // await mergeItem(item);
+ // }
+ // }
+
+ // async merge(serverResponse, lastSynced) {
+ // if (!serverResponse) return false;
+ // this._lastSynced = lastSynced;
+ // const {
+ // notes = [],
+ // synced,
+ // notebooks = [],
+ // content = [],
+ // vaultKey,
+ // settings = [],
+ // attachments = [],
+ // } = serverResponse;
+
+ // if (synced || areAllEmpty(serverResponse)) return false;
+ // this.key = await this._db.user.getEncryptionKey();
+
+ // if (vaultKey) {
+ // await this._db.vault._setKey(await this._deserialize(vaultKey, false));
+ // }
+
+ // await this._mergeArrayWithConflicts(attachments, async (item) => {});
+
+ // await this._mergeArray(
+ // settings,
+ // () => this._db.settings.raw,
+ // (item) => this._db.settings.merge(item)
+ // );
+
+ // await this._mergeArray(
+ // notes,
+ // (id) => this._db.notes.note(id),
+ // (item) => this._db.notes.merge(item)
+ // );
+
+ // await this._mergeArray(
+ // notebooks,
+ // (id) => this._db.notebooks.notebook(id),
+ // (item) => this._db.notebooks.merge(item)
+ // );
+
+ // await this._mergeArrayWithConflicts(content, (item) =>
+ // this._mergeItemWithConflicts(
+ // item,
+ // (id) => this._db.content.raw(id),
+ // (item) => this._db.content.add(item),
+ // async (local, remote) => {}
+ // )
+ // );
+
+ // await this._db.notes.repairReferences();
+ // await this._db.notebooks.repairReferences();
+ // return true;
+ // }
+}
+export default RealtimeMerger;
diff --git a/packages/core/collections/collection.js b/packages/core/collections/collection.js
index 89bfcedb9..fb588bd8c 100644
--- a/packages/core/collections/collection.js
+++ b/packages/core/collections/collection.js
@@ -3,8 +3,14 @@ import CachedCollection from "../database/cached-collection";
import IndexedCollection from "../database/indexed-collection";
class Collection {
- static async new(db, name, cached = true, deferred = false) {
- const collection = new this(db, name, cached);
+ static async new(
+ db,
+ name,
+ encryptionKeyFactory,
+ cached = true,
+ deferred = false
+ ) {
+ const collection = new this(db, name, cached, encryptionKeyFactory);
if (!deferred) await collection.init();
else await collection._collection.indexer.init();
@@ -29,11 +35,28 @@ class Collection {
*
* @param {import("../api").default} db
*/
- constructor(db, name, cached) {
+ constructor(db, name, cached, encryptionKeyFactory) {
this._db = db;
this.collectionName = name;
- if (cached) this._collection = new CachedCollection(this._db.storage, name);
- else this._collection = new IndexedCollection(this._db.storage, name);
+ if (cached)
+ this._collection = new CachedCollection(
+ this._db.storage,
+ name,
+ encryptionKeyFactory
+ );
+ else
+ this._collection = new IndexedCollection(
+ this._db.storage,
+ name,
+ encryptionKeyFactory
+ );
+ }
+
+ async encrypted() {
+ const data = await this._collection.indexer.readMulti(
+ this._collection.indexer.getIndices()
+ );
+ return data.map((d) => d[1]);
}
}
export default Collection;
diff --git a/packages/core/collections/note-history.js b/packages/core/collections/note-history.js
index f78f9fc41..a7c5bbd7c 100644
--- a/packages/core/collections/note-history.js
+++ b/packages/core/collections/note-history.js
@@ -18,13 +18,9 @@ import SessionContent from "./session-content";
*/
export default class NoteHistory extends Collection {
- constructor(db, name, cached) {
- super(db, name, cached);
- this.versionsLimit = 100;
- }
-
async init() {
await super.init();
+ this.versionsLimit = 100;
/**
* @type {SessionContent}
@@ -32,6 +28,7 @@ export default class NoteHistory extends Collection {
this.sessionContent = await SessionContent.new(
this._db,
"sessioncontent",
+ this._collection.encryptionKeyFactory,
false
);
}
diff --git a/packages/core/collections/notebooks.js b/packages/core/collections/notebooks.js
index 43a79f90b..136e99ce8 100644
--- a/packages/core/collections/notebooks.js
+++ b/packages/core/collections/notebooks.js
@@ -104,7 +104,7 @@ export default class Notebooks extends Collection {
}
get all() {
- return sort(this._collection.getItems()).desc((t) => t.pinned);
+ return this._collection.getItems([(t) => t.dateCreated, (t) => t.pinned]);
}
get pinned() {
diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js
index ffe39b92c..7e05024e6 100644
--- a/packages/core/collections/notes.js
+++ b/packages/core/collections/notes.js
@@ -109,7 +109,8 @@ export default class Notes extends Collection {
}
get all() {
- return this._collection.getItems();
+ const items = this._collection.getItems();
+ return items;
}
get pinned() {
diff --git a/packages/core/database/cached-collection.js b/packages/core/database/cached-collection.js
index c6d5f4365..102c7d23d 100644
--- a/packages/core/database/cached-collection.js
+++ b/packages/core/database/cached-collection.js
@@ -3,16 +3,30 @@ import IndexedCollection from "./indexed-collection";
import MapStub from "../utils/map";
export default class CachedCollection extends IndexedCollection {
- constructor(context, type) {
- super(context, type);
+ constructor(context, type, encryptionKeyFactory) {
+ super(context, type, encryptionKeyFactory);
this.type = type;
this.map = new Map();
+ this.encryptionKeyFactory = encryptionKeyFactory;
}
async init() {
await super.init();
- const data = await this.indexer.readMulti(this.indexer.indices);
+ let data = await this.indexer.readMulti(this.indexer.indices);
if (this.map && this.map.dispose) this.map.dispose();
+
+ const encryptionKey =
+ this.encryptionKeyFactory && (await this.encryptionKeyFactory());
+ if (encryptionKey) {
+ for (let item of data) {
+ const [_key, value] = item;
+ const decryptedValue = JSON.parse(
+ await this.indexer.decrypt(encryptionKey, value)
+ );
+ item[1] = decryptedValue;
+ }
+ }
+
this.map = new MapStub.Map(data, this.type);
}
diff --git a/packages/core/database/indexed-collection.js b/packages/core/database/indexed-collection.js
index 1e958d4b0..5ea96bdaa 100644
--- a/packages/core/database/indexed-collection.js
+++ b/packages/core/database/indexed-collection.js
@@ -2,8 +2,9 @@ import { EV, EVENTS } from "../common";
import Indexer from "./indexer";
export default class IndexedCollection {
- constructor(context, type) {
+ constructor(context, type, encryptionKeyFactory) {
this.indexer = new Indexer(context, type);
+ this.encryptionKeyFactory = encryptionKeyFactory;
}
clear() {
@@ -35,7 +36,18 @@ export default class IndexedCollection {
// the item has become local now, so remove the flags
delete item.remote;
delete item.migrated;
- await this.indexer.write(item.id, item);
+
+ if (await this.getEncryptionKey()) {
+ const encrypted = await this.indexer.encrypt(
+ await this.getEncryptionKey(),
+ JSON.stringify(item)
+ );
+ encrypted.dateModified = item.dateModified;
+ encrypted.localOnly = item.localOnly;
+ encrypted.migrated = item.migrated;
+ encrypted.id = item.id;
+ await this.indexer.write(item.id, encrypted);
+ } else await this.indexer.write(item.id, item);
}
removeItem(id) {
@@ -56,12 +68,26 @@ export default class IndexedCollection {
return this.indexer.exists(id);
}
- getItem(id) {
- return this.indexer.read(id);
+ async getItem(id) {
+ const item = await this.indexer.read(id);
+ if (!item) return;
+
+ if ((await this.getEncryptionKey()) && item.iv && item.cipher) {
+ return JSON.parse(
+ await this.indexer.decrypt(await this.getEncryptionKey(), item)
+ );
+ } else return item;
}
async getItems(indices) {
const data = await this.indexer.readMulti(indices);
return Object.fromEntries(data);
}
+
+ async getEncryptionKey() {
+ if (!this.encryptionKeyFactory) return;
+ if (this.encryptionKey) return this.encryptionKey;
+ this.encryptionKey = await this.encryptionKeyFactory();
+ return this.encryptionKey;
+ }
}
diff --git a/packages/core/package-lock.json b/packages/core/package-lock.json
index a97d6e636..8e040b46a 100644
--- a/packages/core/package-lock.json
+++ b/packages/core/package-lock.json
@@ -1,13 +1,14 @@
{
"name": "notes-core",
- "version": "6.16.2",
+ "version": "6.16.4",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "notes-core",
- "version": "6.16.2",
+ "version": "6.16.4",
"dependencies": {
+ "@microsoft/signalr": "^6.0.1",
"@stablelib/blake2s": "^1.0.1",
"async-mutex": "^0.3.2",
"base64-arraybuffer": "^1.0.1",
@@ -1529,6 +1530,56 @@
"node": ">= 6"
}
},
+ "node_modules/@microsoft/signalr": {
+ "version": "6.0.1",
+ "resolved": "https://registry.npmjs.org/@microsoft/signalr/-/signalr-6.0.1.tgz",
+ "integrity": "sha512-witYtScUxPxl1QA69AsuVIsn54S4Rcfym3c/ON2bsA0ZWHcvskA0dUnOX9JCxOduGMEGwkMprKFfVTxUO/inzQ==",
+ "dependencies": {
+ "abort-controller": "^3.0.0",
+ "eventsource": "^1.0.7",
+ "fetch-cookie": "^0.11.0",
+ "node-fetch": "^2.6.1",
+ "ws": "^7.4.5"
+ }
+ },
+ "node_modules/@microsoft/signalr/node_modules/node-fetch": {
+ "version": "2.6.7",
+ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz",
+ "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==",
+ "dependencies": {
+ "whatwg-url": "^5.0.0"
+ },
+ "engines": {
+ "node": "4.x || >=6.0.0"
+ },
+ "peerDependencies": {
+ "encoding": "^0.1.0"
+ },
+ "peerDependenciesMeta": {
+ "encoding": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@microsoft/signalr/node_modules/tr46": {
+ "version": "0.0.3",
+ "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
+ "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o="
+ },
+ "node_modules/@microsoft/signalr/node_modules/webidl-conversions": {
+ "version": "3.0.1",
+ "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
+ "integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE="
+ },
+ "node_modules/@microsoft/signalr/node_modules/whatwg-url": {
+ "version": "5.0.0",
+ "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
+ "integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=",
+ "dependencies": {
+ "tr46": "~0.0.3",
+ "webidl-conversions": "^3.0.0"
+ }
+ },
"node_modules/@stablelib/binary": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@stablelib/binary/-/binary-1.0.1.tgz",
@@ -1820,6 +1871,17 @@
"integrity": "sha512-tsFzPpcttalNjFBCFMqsKYQcWxxen1pgJR56by//QwvJc4/OUS3kPOOttx2tSIfjsylB0pYu7f5D3K1RCxUnUg==",
"dev": true
},
+ "node_modules/abort-controller": {
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz",
+ "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==",
+ "dependencies": {
+ "event-target-shim": "^5.0.0"
+ },
+ "engines": {
+ "node": ">=6.5"
+ }
+ },
"node_modules/abortcontroller-polyfill": {
"version": "1.7.3",
"resolved": "https://registry.npmjs.org/abortcontroller-polyfill/-/abortcontroller-polyfill-1.7.3.tgz",
@@ -3963,6 +4025,25 @@
"node": ">=0.10.0"
}
},
+ "node_modules/event-target-shim": {
+ "version": "5.0.1",
+ "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz",
+ "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==",
+ "engines": {
+ "node": ">=6"
+ }
+ },
+ "node_modules/eventsource": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/eventsource/-/eventsource-1.1.0.tgz",
+ "integrity": "sha512-VSJjT5oCNrFvCS6igjzPAt5hBzQ2qPBFIbJ03zLI9SE0mxwZpMw6BfJrbFHm1a141AavMEB8JHmBhWAd66PfCg==",
+ "dependencies": {
+ "original": "^1.0.0"
+ },
+ "engines": {
+ "node": ">=0.12.0"
+ }
+ },
"node_modules/exec-sh": {
"version": "0.3.4",
"resolved": "https://registry.npmjs.org/exec-sh/-/exec-sh-0.3.4.tgz",
@@ -4121,6 +4202,17 @@
"bser": "2.1.1"
}
},
+ "node_modules/fetch-cookie": {
+ "version": "0.11.0",
+ "resolved": "https://registry.npmjs.org/fetch-cookie/-/fetch-cookie-0.11.0.tgz",
+ "integrity": "sha512-BQm7iZLFhMWFy5CZ/162sAGjBfdNWb7a8LEqqnzsHFhxT/X/SVj/z2t2nu3aJvjlbQkrAlTUApplPRjWyH4mhA==",
+ "dependencies": {
+ "tough-cookie": "^2.3.3 || ^3.0.1 || ^4.0.0"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
"node_modules/fflate": {
"version": "0.7.2",
"resolved": "https://registry.npmjs.org/fflate/-/fflate-0.7.2.tgz",
@@ -6460,6 +6552,14 @@
"node": ">= 0.8.0"
}
},
+ "node_modules/original": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/original/-/original-1.0.2.tgz",
+ "integrity": "sha512-hyBVl6iqqUOJ8FqRe+l/gS8H+kKYjrEndd5Pm1MfBtsEKA038HkkdbAl/72EAXGyonD/PFsvmVG+EvcIpliMBg==",
+ "dependencies": {
+ "url-parse": "^1.4.3"
+ }
+ },
"node_modules/os-homedir": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz",
@@ -6801,8 +6901,7 @@
"node_modules/psl": {
"version": "1.8.0",
"resolved": "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz",
- "integrity": "sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ==",
- "dev": true
+ "integrity": "sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ=="
},
"node_modules/pump": {
"version": "3.0.0",
@@ -6818,7 +6917,6 @@
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz",
"integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==",
- "dev": true,
"engines": {
"node": ">=6"
}
@@ -6837,6 +6935,11 @@
"node": ">=0.6"
}
},
+ "node_modules/querystringify": {
+ "version": "2.2.0",
+ "resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz",
+ "integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ=="
+ },
"node_modules/railroad-diagrams": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/railroad-diagrams/-/railroad-diagrams-1.0.0.tgz",
@@ -7121,6 +7224,11 @@
"resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz",
"integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg=="
},
+ "node_modules/requires-port": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
+ "integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8="
+ },
"node_modules/resolve": {
"version": "1.17.0",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.17.0.tgz",
@@ -7904,7 +8012,6 @@
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.5.0.tgz",
"integrity": "sha512-nlLsUzgm1kfLXSXfRZMc1KLAugd4hqJHDTvc2hDIwS3mZAfMEuMbc03SujMF+GEcpaX/qboeycw6iO8JwVv2+g==",
- "dev": true,
"dependencies": {
"psl": "^1.1.28",
"punycode": "^2.1.1"
@@ -8099,6 +8206,15 @@
"deprecated": "Please see https://github.com/lydell/urix#deprecated",
"dev": true
},
+ "node_modules/url-parse": {
+ "version": "1.5.4",
+ "resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.4.tgz",
+ "integrity": "sha512-ITeAByWWoqutFClc/lRZnFplgXgEZr3WJ6XngMM/N9DMIm4K8zXPCZ1Jdu0rERwO84w1WC5wkle2ubwTA4NTBg==",
+ "dependencies": {
+ "querystringify": "^2.1.1",
+ "requires-port": "^1.0.0"
+ }
+ },
"node_modules/use": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/use/-/use-3.1.1.tgz",
@@ -8324,7 +8440,6 @@
"version": "7.4.5",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.4.5.tgz",
"integrity": "sha512-xzyu3hFvomRfXKH8vOFMU3OguG6oOvhXMo3xsGy3xWExqaM2dxBbVxuD99O7m3ZUFMvvscsZDqxfgMaRr/Nr1g==",
- "dev": true,
"engines": {
"node": ">=8.3.0"
},
@@ -9587,6 +9702,47 @@
"@types/yargs": "^13.0.0"
}
},
+ "@microsoft/signalr": {
+ "version": "6.0.1",
+ "resolved": "https://registry.npmjs.org/@microsoft/signalr/-/signalr-6.0.1.tgz",
+ "integrity": "sha512-witYtScUxPxl1QA69AsuVIsn54S4Rcfym3c/ON2bsA0ZWHcvskA0dUnOX9JCxOduGMEGwkMprKFfVTxUO/inzQ==",
+ "requires": {
+ "abort-controller": "^3.0.0",
+ "eventsource": "^1.0.7",
+ "fetch-cookie": "^0.11.0",
+ "node-fetch": "^2.6.1",
+ "ws": "^7.4.5"
+ },
+ "dependencies": {
+ "node-fetch": {
+ "version": "2.6.7",
+ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz",
+ "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==",
+ "requires": {
+ "whatwg-url": "^5.0.0"
+ }
+ },
+ "tr46": {
+ "version": "0.0.3",
+ "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
+ "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o="
+ },
+ "webidl-conversions": {
+ "version": "3.0.1",
+ "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
+ "integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE="
+ },
+ "whatwg-url": {
+ "version": "5.0.0",
+ "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
+ "integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=",
+ "requires": {
+ "tr46": "~0.0.3",
+ "webidl-conversions": "^3.0.0"
+ }
+ }
+ }
+ },
"@stablelib/binary": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@stablelib/binary/-/binary-1.0.1.tgz",
@@ -9844,6 +10000,14 @@
"integrity": "sha512-tsFzPpcttalNjFBCFMqsKYQcWxxen1pgJR56by//QwvJc4/OUS3kPOOttx2tSIfjsylB0pYu7f5D3K1RCxUnUg==",
"dev": true
},
+ "abort-controller": {
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz",
+ "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==",
+ "requires": {
+ "event-target-shim": "^5.0.0"
+ }
+ },
"abortcontroller-polyfill": {
"version": "1.7.3",
"resolved": "https://registry.npmjs.org/abortcontroller-polyfill/-/abortcontroller-polyfill-1.7.3.tgz",
@@ -11699,6 +11863,19 @@
"integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==",
"dev": true
},
+ "event-target-shim": {
+ "version": "5.0.1",
+ "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz",
+ "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ=="
+ },
+ "eventsource": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/eventsource/-/eventsource-1.1.0.tgz",
+ "integrity": "sha512-VSJjT5oCNrFvCS6igjzPAt5hBzQ2qPBFIbJ03zLI9SE0mxwZpMw6BfJrbFHm1a141AavMEB8JHmBhWAd66PfCg==",
+ "requires": {
+ "original": "^1.0.0"
+ }
+ },
"exec-sh": {
"version": "0.3.4",
"resolved": "https://registry.npmjs.org/exec-sh/-/exec-sh-0.3.4.tgz",
@@ -11835,6 +12012,14 @@
"bser": "2.1.1"
}
},
+ "fetch-cookie": {
+ "version": "0.11.0",
+ "resolved": "https://registry.npmjs.org/fetch-cookie/-/fetch-cookie-0.11.0.tgz",
+ "integrity": "sha512-BQm7iZLFhMWFy5CZ/162sAGjBfdNWb7a8LEqqnzsHFhxT/X/SVj/z2t2nu3aJvjlbQkrAlTUApplPRjWyH4mhA==",
+ "requires": {
+ "tough-cookie": "^2.3.3 || ^3.0.1 || ^4.0.0"
+ }
+ },
"fflate": {
"version": "0.7.2",
"resolved": "https://registry.npmjs.org/fflate/-/fflate-0.7.2.tgz",
@@ -13699,6 +13884,14 @@
"word-wrap": "~1.2.3"
}
},
+ "original": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/original/-/original-1.0.2.tgz",
+ "integrity": "sha512-hyBVl6iqqUOJ8FqRe+l/gS8H+kKYjrEndd5Pm1MfBtsEKA038HkkdbAl/72EAXGyonD/PFsvmVG+EvcIpliMBg==",
+ "requires": {
+ "url-parse": "^1.4.3"
+ }
+ },
"os-homedir": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz",
@@ -13954,8 +14147,7 @@
"psl": {
"version": "1.8.0",
"resolved": "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz",
- "integrity": "sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ==",
- "dev": true
+ "integrity": "sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ=="
},
"pump": {
"version": "3.0.0",
@@ -13970,8 +14162,7 @@
"punycode": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz",
- "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==",
- "dev": true
+ "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A=="
},
"qclone": {
"version": "1.0.4",
@@ -13984,6 +14175,11 @@
"integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==",
"dev": true
},
+ "querystringify": {
+ "version": "2.2.0",
+ "resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz",
+ "integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ=="
+ },
"railroad-diagrams": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/railroad-diagrams/-/railroad-diagrams-1.0.0.tgz",
@@ -14216,6 +14412,11 @@
"resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz",
"integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg=="
},
+ "requires-port": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
+ "integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8="
+ },
"resolve": {
"version": "1.17.0",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.17.0.tgz",
@@ -14837,7 +15038,6 @@
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.5.0.tgz",
"integrity": "sha512-nlLsUzgm1kfLXSXfRZMc1KLAugd4hqJHDTvc2hDIwS3mZAfMEuMbc03SujMF+GEcpaX/qboeycw6iO8JwVv2+g==",
- "dev": true,
"requires": {
"psl": "^1.1.28",
"punycode": "^2.1.1"
@@ -14993,6 +15193,15 @@
"integrity": "sha1-2pN/emLiH+wf0Y1Js1wpNQZ6bHI=",
"dev": true
},
+ "url-parse": {
+ "version": "1.5.4",
+ "resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.4.tgz",
+ "integrity": "sha512-ITeAByWWoqutFClc/lRZnFplgXgEZr3WJ6XngMM/N9DMIm4K8zXPCZ1Jdu0rERwO84w1WC5wkle2ubwTA4NTBg==",
+ "requires": {
+ "querystringify": "^2.1.1",
+ "requires-port": "^1.0.0"
+ }
+ },
"use": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/use/-/use-3.1.1.tgz",
@@ -15185,7 +15394,6 @@
"version": "7.4.5",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.4.5.tgz",
"integrity": "sha512-xzyu3hFvomRfXKH8vOFMU3OguG6oOvhXMo3xsGy3xWExqaM2dxBbVxuD99O7m3ZUFMvvscsZDqxfgMaRr/Nr1g==",
- "dev": true,
"requires": {}
},
"xml-name-validator": {
diff --git a/packages/core/package.json b/packages/core/package.json
index 2626a33a6..0dc3bd5f2 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -24,6 +24,7 @@
"test": "jest --forceExit"
},
"dependencies": {
+ "@microsoft/signalr": "^6.0.1",
"@stablelib/blake2s": "^1.0.1",
"async-mutex": "^0.3.2",
"base64-arraybuffer": "^1.0.1",