Fix bug with error when decoding
This commit is contained in:
parent
db5a363bff
commit
3c80ac3125
@ -4,7 +4,10 @@ import Refresh from "feather-icons/dist/icons/refresh-cw.svg";
|
|||||||
import "./footer.scss";
|
import "./footer.scss";
|
||||||
import Notifications from "../notifications";
|
import Notifications from "../notifications";
|
||||||
|
|
||||||
export class Footer extends Component<{}, { synced: boolean, syncing: boolean }> {
|
export class Footer extends Component<
|
||||||
|
{},
|
||||||
|
{ synced: boolean; syncing: boolean }
|
||||||
|
> {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
this.state = { synced: false, syncing: false };
|
this.state = { synced: false, syncing: false };
|
||||||
@ -15,7 +18,7 @@ export class Footer extends Component<{}, { synced: boolean, syncing: boolean }>
|
|||||||
async componentWillMount() {
|
async componentWillMount() {
|
||||||
Notes.syncedObservable.subscribe(this.onSyncedChange);
|
Notes.syncedObservable.subscribe(this.onSyncedChange);
|
||||||
Notes.syncObservable.subscribe(this.onSyncChange);
|
Notes.syncObservable.subscribe(this.onSyncChange);
|
||||||
this.setState({ synced: await Notes.isSynced() })
|
this.setState({ synced: await Notes.isSynced() });
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
@ -25,18 +28,18 @@ export class Footer extends Component<{}, { synced: boolean, syncing: boolean }>
|
|||||||
|
|
||||||
onSyncChange(state: boolean) {
|
onSyncChange(state: boolean) {
|
||||||
console.log("sync", state);
|
console.log("sync", state);
|
||||||
this.setState({ syncing: state })
|
this.setState({ syncing: state });
|
||||||
}
|
}
|
||||||
|
|
||||||
onSyncedChange(state: boolean) {
|
onSyncedChange(state: boolean) {
|
||||||
console.log("synced", state);
|
console.log("synced", state);
|
||||||
this.setState({ synced: state })
|
this.setState({ synced: state });
|
||||||
}
|
}
|
||||||
|
|
||||||
onSyncClick() {
|
onSyncClick() {
|
||||||
Notes.sync().then(() => {
|
Notes.sync().then(() => {
|
||||||
Notifications.sendInfo("Finished Synchronisation");
|
Notifications.sendInfo("Finished Synchronisation");
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
@ -46,7 +49,7 @@ export class Footer extends Component<{}, { synced: boolean, syncing: boolean }>
|
|||||||
if (this.state.syncing) {
|
if (this.state.syncing) {
|
||||||
color = "orange";
|
color = "orange";
|
||||||
text = "syncing";
|
text = "syncing";
|
||||||
extrac = "reloading"
|
extrac = "reloading";
|
||||||
} else {
|
} else {
|
||||||
if (this.state.synced) {
|
if (this.state.synced) {
|
||||||
color = "green";
|
color = "green";
|
||||||
@ -56,17 +59,21 @@ export class Footer extends Component<{}, { synced: boolean, syncing: boolean }>
|
|||||||
text = "not synced";
|
text = "not synced";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return <footer class="elv-8">
|
return (
|
||||||
|
<footer class="elv-8">
|
||||||
<span>
|
<span>
|
||||||
<span class={extrac} ><a onClick={() => this.onSyncClick()} ><Refresh style="height: 1em; width: 1em;"></Refresh></a></span>
|
<span class={extrac}>
|
||||||
|
<a onClick={() => this.onSyncClick()}>
|
||||||
|
<Refresh style="height: 1em; width: 1em;"></Refresh>
|
||||||
|
</a>
|
||||||
|
</span>
|
||||||
<span style={"margin-left: 8px; color:" + color}>{text}</span>
|
<span style={"margin-left: 8px; color:" + color}>{text}</span>
|
||||||
</span>
|
</span>
|
||||||
<span>
|
<span>
|
||||||
Welcome <b>{Notes.name}</b>
|
Welcome <b>{Notes.name}</b>
|
||||||
</span>
|
</span>
|
||||||
<span style="color: lightgrey;">
|
<span style="color: lightgrey;">v1.5</span>
|
||||||
v1.4
|
|
||||||
</span>
|
|
||||||
</footer>
|
</footer>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
316
src/notes.ts
316
src/notes.ts
@ -9,7 +9,6 @@ import * as b64 from "./helper/base64";
|
|||||||
import IDB from "./helper/indexeddb";
|
import IDB from "./helper/indexeddb";
|
||||||
import Notifications, { MessageType } from "./notifications";
|
import Notifications, { MessageType } from "./notifications";
|
||||||
|
|
||||||
|
|
||||||
export class HttpError extends Error {
|
export class HttpError extends Error {
|
||||||
constructor(public status: number, public statusText: string) {
|
constructor(public status: number, public statusText: string) {
|
||||||
super(statusText);
|
super(statusText);
|
||||||
@ -46,7 +45,7 @@ const Decoder = new TextDecoder();
|
|||||||
enum OpLogType {
|
enum OpLogType {
|
||||||
CREATE,
|
CREATE,
|
||||||
CHANGE,
|
CHANGE,
|
||||||
DELETE
|
DELETE,
|
||||||
}
|
}
|
||||||
|
|
||||||
interface OpLog {
|
interface OpLog {
|
||||||
@ -59,8 +58,8 @@ interface OpLog {
|
|||||||
* The value
|
* The value
|
||||||
*/
|
*/
|
||||||
values: {
|
values: {
|
||||||
value: Uint8Array,
|
value: Uint8Array;
|
||||||
preview: Uint8Array
|
preview: Uint8Array;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -69,41 +68,43 @@ interface OpLog {
|
|||||||
date: Date;
|
date: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type VaultList = { name: string, encrypted: boolean, id: string }[];
|
export type VaultList = { name: string; encrypted: boolean; id: string }[];
|
||||||
|
|
||||||
export interface IVault {
|
export interface IVault {
|
||||||
name: string;
|
name: string;
|
||||||
id: string;
|
id: string;
|
||||||
encrypted: boolean;
|
encrypted: boolean;
|
||||||
getAllNotes(): Promise<BaseNote[]>;
|
getAllNotes(): Promise<BaseNote[]>;
|
||||||
searchNote(term: string): Promise<BaseNote[]>
|
searchNote(term: string): Promise<BaseNote[]>;
|
||||||
newNote(): ViewNote;
|
newNote(): ViewNote;
|
||||||
saveNote(note: ViewNote, date?: Date): Promise<void>;
|
saveNote(note: ViewNote, date?: Date): Promise<void>;
|
||||||
getNote(id: string): Promise<ViewNote>;
|
getNote(id: string): Promise<ViewNote>;
|
||||||
deleteNote(id: string): Promise<void>;
|
deleteNote(id: string): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const awaitTimeout = (ms: number) => new Promise<void>(resolve => setTimeout(resolve, ms));
|
const awaitTimeout = (ms: number) =>
|
||||||
|
new Promise<void>((resolve) => setTimeout(resolve, ms));
|
||||||
|
|
||||||
class NotesProvider {
|
class NotesProvider {
|
||||||
private notesObservableServer = new Observable<Note>()
|
private notesObservableServer = new Observable<Note>();
|
||||||
public notesObservable = this.notesObservableServer.getPublicApi()
|
public notesObservable = this.notesObservableServer.getPublicApi();
|
||||||
|
|
||||||
private syncObservableServer = new Observable<boolean>()
|
private syncObservableServer = new Observable<boolean>();
|
||||||
/**
|
/**
|
||||||
* Will send false once finished and true on start
|
* Will send false once finished and true on start
|
||||||
*/
|
*/
|
||||||
public syncObservable = this.syncObservableServer.getPublicApi()
|
public syncObservable = this.syncObservableServer.getPublicApi();
|
||||||
|
|
||||||
private database = new IDB("notes", ["notes", "oplog"]);
|
private database = new IDB("notes", ["notes", "oplog"]);
|
||||||
private noteDB = this.database.getStore<DBNote>("notes");
|
private noteDB = this.database.getStore<DBNote>("notes");
|
||||||
private oplogDB = this.database.getStore<{ id: string, logs: OpLog[] }>("oplog");
|
private oplogDB = this.database.getStore<{ id: string; logs: OpLog[] }>(
|
||||||
|
"oplog"
|
||||||
|
);
|
||||||
|
|
||||||
private vaultKeys = new Map<string, Uint8Array>();
|
private vaultKeys = new Map<string, Uint8Array>();
|
||||||
|
|
||||||
|
|
||||||
public apiLock = new Lock();
|
public apiLock = new Lock();
|
||||||
public apiLockRls = this.apiLock.getLock()
|
public apiLockRls = this.apiLock.getLock();
|
||||||
|
|
||||||
private syncLock = new Lock();
|
private syncLock = new Lock();
|
||||||
|
|
||||||
@ -115,7 +116,7 @@ class NotesProvider {
|
|||||||
public syncedObservable = this.syncedObservableServer.getPublicApi();
|
public syncedObservable = this.syncedObservableServer.getPublicApi();
|
||||||
|
|
||||||
public async isSynced() {
|
public async isSynced() {
|
||||||
return (await this.oplogDB.getAll()).length <= 0
|
return (await this.oplogDB.getAll()).length <= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _name;
|
private _name;
|
||||||
@ -128,11 +129,17 @@ class NotesProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
login() {
|
login() {
|
||||||
window.location.href = `${config.auth_server}/auth?client_id=${config.client_id}&scope=${config.permission}&redirect_uri=${encodeURIComponent(config.callback_url)}&response_type=code`
|
window.location.href = `${config.auth_server}/auth?client_id=${
|
||||||
|
config.client_id
|
||||||
|
}&scope=${config.permission}&redirect_uri=${encodeURIComponent(
|
||||||
|
config.callback_url
|
||||||
|
)}&response_type=code`;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getToken(code: string) {
|
async getToken(code: string) {
|
||||||
let req = await fetch(`${config.auth_server}/api/oauth/refresh?grant_type=authorization_code&client_id=${config.client_id}&code=${code}`);
|
let req = await fetch(
|
||||||
|
`${config.auth_server}/api/oauth/refresh?grant_type=authorization_code&client_id=${config.client_id}&code=${code}`
|
||||||
|
);
|
||||||
let res = await req.json();
|
let res = await req.json();
|
||||||
if (!res.error) {
|
if (!res.error) {
|
||||||
localStorage.setItem("refreshtoken", res.token);
|
localStorage.setItem("refreshtoken", res.token);
|
||||||
@ -140,9 +147,9 @@ class NotesProvider {
|
|||||||
this._name = res.profile.name;
|
this._name = res.profile.name;
|
||||||
let kb = this.passwordToKey(res.profile.enc_key);
|
let kb = this.passwordToKey(res.profile.enc_key);
|
||||||
localStorage.setItem("enc_key", b64.encode(kb));
|
localStorage.setItem("enc_key", b64.encode(kb));
|
||||||
this.generalEncryption = kb
|
this.generalEncryption = kb;
|
||||||
} else {
|
} else {
|
||||||
return "Invalid Code"
|
return "Invalid Code";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,11 +162,11 @@ class NotesProvider {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
callback(err, null);
|
callback(err, null);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
let key = localStorage.getItem("enc_key");
|
let key = localStorage.getItem("enc_key");
|
||||||
if (key) {
|
if (key) {
|
||||||
this.generalEncryption = b64.decode(key)
|
this.generalEncryption = b64.decode(key);
|
||||||
}
|
}
|
||||||
this._name = localStorage.getItem("name");
|
this._name = localStorage.getItem("name");
|
||||||
}
|
}
|
||||||
@ -168,34 +175,42 @@ class NotesProvider {
|
|||||||
const next = () => {
|
const next = () => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.sync().then(next);
|
this.sync().then(next);
|
||||||
}, 30000)
|
}, 30000);
|
||||||
}
|
};
|
||||||
this.syncedObservableServer.send((await this.oplogDB.getAll()).length <= 0);
|
this.syncedObservableServer.send(
|
||||||
let prs = this.apiLockRls.then(lock => lock.release());
|
(await this.oplogDB.getAll()).length <= 0
|
||||||
prs.then(() => awaitTimeout(2000)).then(() => this.sync()).then(() => next());
|
);
|
||||||
|
let prs = this.apiLockRls.then((lock) => lock.release());
|
||||||
|
prs.then(() => awaitTimeout(2000))
|
||||||
|
.then(() => this.sync())
|
||||||
|
.then(() => next());
|
||||||
return prs;
|
return prs;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getJWT() {
|
private async getJWT() {
|
||||||
let lock = await this.apiLock.getLock()
|
let lock = await this.apiLock.getLock();
|
||||||
try {
|
try {
|
||||||
console.log("Getting JWT");
|
console.log("Getting JWT");
|
||||||
let req = await fetch(config.auth_server + "/api/oauth/jwt?refreshtoken=" + localStorage.getItem("refreshtoken"));
|
let req = await fetch(
|
||||||
|
config.auth_server +
|
||||||
|
"/api/oauth/jwt?refreshtoken=" +
|
||||||
|
localStorage.getItem("refreshtoken")
|
||||||
|
);
|
||||||
if (req.status !== 200) {
|
if (req.status !== 200) {
|
||||||
Notifications.sendNotification("offline", MessageType.INFO);
|
Notifications.sendNotification("offline", MessageType.INFO);
|
||||||
throw new Error("Offline")
|
throw new Error("Offline");
|
||||||
}
|
}
|
||||||
let res = await req.json();
|
let res = await req.json();
|
||||||
if (res.error) {
|
if (res.error) {
|
||||||
console.log("Refresh token invalid, forward to login")
|
console.log("Refresh token invalid, forward to login");
|
||||||
localStorage.removeItem("refreshtoken");
|
localStorage.removeItem("refreshtoken");
|
||||||
this.login()
|
this.login();
|
||||||
throw new Error("Need login!")
|
throw new Error("Need login!");
|
||||||
} else {
|
} else {
|
||||||
return res.token
|
return res.token;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
lock.release()
|
lock.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,29 +220,38 @@ class NotesProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async sync() {
|
async sync() {
|
||||||
const lock = await this.syncLock.getLock()
|
const lock = await this.syncLock.getLock();
|
||||||
const log = (...message: any[]) => {
|
const log = (...message: any[]) => {
|
||||||
console.log("[SYNC]: ", ...message)
|
console.log("[SYNC]: ", ...message);
|
||||||
}
|
};
|
||||||
|
|
||||||
this.syncObservableServer.send(true);
|
this.syncObservableServer.send(true);
|
||||||
log("Start")
|
log("Start");
|
||||||
try {
|
try {
|
||||||
log("Fetching");
|
log("Fetching");
|
||||||
let [remotes, locals, oplogs] = await Promise.all([this._secureFile.list(), this.noteDB.getAll(), this.oplogDB.getAll()]);
|
let [remotes, locals, oplogs] = await Promise.all([
|
||||||
|
this._secureFile.list(),
|
||||||
|
this.noteDB.getAll(),
|
||||||
|
this.oplogDB.getAll(),
|
||||||
|
]);
|
||||||
log("Fetched");
|
log("Fetched");
|
||||||
// Create sync pairs (remote & local)
|
// Create sync pairs (remote & local)
|
||||||
log("Building pairs");
|
log("Building pairs");
|
||||||
let pairs: { local: DBNote, remote: IFile, id: string, oplog: OpLog[] }[] = [];
|
let pairs: {
|
||||||
remotes.map(r => {
|
local: DBNote;
|
||||||
let lIdx = locals.findIndex(e => e._id === r._id);
|
remote: IFile;
|
||||||
|
id: string;
|
||||||
|
oplog: OpLog[];
|
||||||
|
}[] = [];
|
||||||
|
remotes.map((r) => {
|
||||||
|
let lIdx = locals.findIndex((e) => e._id === r._id);
|
||||||
let l: DBNote = undefined;
|
let l: DBNote = undefined;
|
||||||
if (lIdx >= 0) {
|
if (lIdx >= 0) {
|
||||||
l = locals[lIdx];
|
l = locals[lIdx];
|
||||||
locals.splice(lIdx, 1);
|
locals.splice(lIdx, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let oIdx = oplogs.findIndex(e => e.id === r._id);
|
let oIdx = oplogs.findIndex((e) => e.id === r._id);
|
||||||
let oplog: OpLog[];
|
let oplog: OpLog[];
|
||||||
if (oIdx >= 0) {
|
if (oIdx >= 0) {
|
||||||
oplog = oplogs[oIdx].logs;
|
oplog = oplogs[oIdx].logs;
|
||||||
@ -238,12 +262,12 @@ class NotesProvider {
|
|||||||
remote: r,
|
remote: r,
|
||||||
local: l,
|
local: l,
|
||||||
oplog,
|
oplog,
|
||||||
id: r._id
|
id: r._id,
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
locals.forEach(l => {
|
locals.forEach((l) => {
|
||||||
let oIdx = oplogs.findIndex(e => e.id === l._id);
|
let oIdx = oplogs.findIndex((e) => e.id === l._id);
|
||||||
let oplog: OpLog[] = undefined;
|
let oplog: OpLog[] = undefined;
|
||||||
if (oIdx >= 0) {
|
if (oIdx >= 0) {
|
||||||
oplog = oplogs[oIdx].logs;
|
oplog = oplogs[oIdx].logs;
|
||||||
@ -255,20 +279,20 @@ class NotesProvider {
|
|||||||
remote: undefined,
|
remote: undefined,
|
||||||
local: l,
|
local: l,
|
||||||
oplog,
|
oplog,
|
||||||
id: l._id
|
id: l._id,
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
oplogs.forEach(oplog => {
|
oplogs.forEach((oplog) => {
|
||||||
if (!oplog) return;
|
if (!oplog) return;
|
||||||
if (oplog.logs.length > 0)
|
if (oplog.logs.length > 0)
|
||||||
pairs.push({
|
pairs.push({
|
||||||
remote: undefined,
|
remote: undefined,
|
||||||
local: undefined,
|
local: undefined,
|
||||||
oplog: oplog.logs,
|
oplog: oplog.logs,
|
||||||
id: oplog.id
|
id: oplog.id,
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
log("Pairs builded");
|
log("Pairs builded");
|
||||||
|
|
||||||
@ -279,8 +303,8 @@ class NotesProvider {
|
|||||||
local_change: 0,
|
local_change: 0,
|
||||||
local_delete: 0,
|
local_delete: 0,
|
||||||
do_nothing: 0,
|
do_nothing: 0,
|
||||||
error: 0
|
error: 0,
|
||||||
}
|
};
|
||||||
|
|
||||||
log("Start inspection");
|
log("Start inspection");
|
||||||
for (let { local, remote, oplog, id } of pairs) {
|
for (let { local, remote, oplog, id } of pairs) {
|
||||||
@ -291,13 +315,19 @@ class NotesProvider {
|
|||||||
case OpLogType.CHANGE:
|
case OpLogType.CHANGE:
|
||||||
log(id, "REMOTE CHANGE");
|
log(id, "REMOTE CHANGE");
|
||||||
stats.remote_change++;
|
stats.remote_change++;
|
||||||
await this._secureFile.update(id, op.values.value, b64.encode(op.values.preview), op.date, old);
|
await this._secureFile.update(
|
||||||
|
id,
|
||||||
|
op.values.value,
|
||||||
|
b64.encode(op.values.preview),
|
||||||
|
op.date,
|
||||||
|
old
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
case OpLogType.DELETE:
|
case OpLogType.DELETE:
|
||||||
log(id, "REMOTE DELETE");
|
log(id, "REMOTE DELETE");
|
||||||
stats.remote_delete++;
|
stats.remote_delete++;
|
||||||
if (old) break; // if the deletion is old, just ignore
|
if (old) break; // if the deletion is old, just ignore
|
||||||
await this._secureFile.delete(id)
|
await this._secureFile.delete(id);
|
||||||
break;
|
break;
|
||||||
case OpLogType.CREATE:
|
case OpLogType.CREATE:
|
||||||
log(id, "REMOTE CREATE");
|
log(id, "REMOTE CREATE");
|
||||||
@ -314,11 +344,11 @@ class NotesProvider {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
const localChange = (id: string) => {
|
const localChange = (id: string) => {
|
||||||
//TODO implement
|
//TODO implement
|
||||||
}
|
};
|
||||||
|
|
||||||
const create = async () => {
|
const create = async () => {
|
||||||
log(id, "LOCAL CREATAE/UPDATE");
|
log(id, "LOCAL CREATAE/UPDATE");
|
||||||
@ -327,42 +357,43 @@ class NotesProvider {
|
|||||||
let note: DBNote = {
|
let note: DBNote = {
|
||||||
_id: remote._id,
|
_id: remote._id,
|
||||||
folder: remote.folder,
|
folder: remote.folder,
|
||||||
preview: b64.decode(remote.active.preview),
|
preview: b64.decode(remote.active.preview || ""),
|
||||||
time: remote.active.time,
|
time: remote.active.time,
|
||||||
__value: new Uint8Array(value)
|
__value: new Uint8Array(value),
|
||||||
}
|
};
|
||||||
await this.noteDB.set(id, note);
|
await this.noteDB.set(id, note);
|
||||||
localChange(id);
|
localChange(id);
|
||||||
}
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// log(id, "LRO: ", !!local, !!remote, !!oplog)
|
// log(id, "LRO: ", !!local, !!remote, !!oplog)
|
||||||
if (remote && !oplog) {
|
if (remote && !oplog) {
|
||||||
if (local) {
|
if (local) {
|
||||||
let old = remote.active.time.valueOf() > local.time.valueOf();
|
let old =
|
||||||
if (old)
|
remote.active.time.valueOf() > local.time.valueOf();
|
||||||
await create()
|
if (old) await create();
|
||||||
else {
|
else {
|
||||||
stats.do_nothing++;
|
stats.do_nothing++;
|
||||||
log(id, "DO NOTHING");
|
log(id, "DO NOTHING");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
await create()
|
await create();
|
||||||
}
|
}
|
||||||
} else if (!remote && local && !oplog) { // No local changes, but remote deleted
|
} else if (!remote && local && !oplog) {
|
||||||
|
// No local changes, but remote deleted
|
||||||
log("LOCAL DELETE");
|
log("LOCAL DELETE");
|
||||||
stats.local_delete++;
|
stats.local_delete++;
|
||||||
await this.noteDB.delete(id);
|
await this.noteDB.delete(id);
|
||||||
localChange(id);
|
localChange(id);
|
||||||
} else if (!remote && oplog) { // Remote does not exist, but oplog, just apply all changes including possible delete
|
} else if (!remote && oplog) {
|
||||||
await apply()
|
// Remote does not exist, but oplog, just apply all changes including possible delete
|
||||||
|
await apply();
|
||||||
} else if (remote && oplog) {
|
} else if (remote && oplog) {
|
||||||
let last = oplog[oplog.length - 1]
|
let last = oplog[oplog.length - 1];
|
||||||
let old = remote.active.time.valueOf() > last.date.valueOf();
|
let old = remote.active.time.valueOf() > last.date.valueOf();
|
||||||
|
|
||||||
if (old)
|
if (old) await create(); // Will recreate local entry
|
||||||
await create() // Will recreate local entry
|
await apply(old); // Will apply changes to remote
|
||||||
await apply(old) // Will apply changes to remote
|
|
||||||
} else {
|
} else {
|
||||||
log(id, "DO NOTHING");
|
log(id, "DO NOTHING");
|
||||||
stats.do_nothing++;
|
stats.do_nothing++;
|
||||||
@ -370,15 +401,20 @@ class NotesProvider {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
stats.error++;
|
stats.error++;
|
||||||
Notifications.sendNotification("Error syncing: " + id, MessageType.ERROR);
|
Notifications.sendNotification(
|
||||||
|
"Error syncing: " + id,
|
||||||
|
MessageType.ERROR
|
||||||
|
);
|
||||||
}
|
}
|
||||||
await this.oplogDB.delete(id);
|
await this.oplogDB.delete(id);
|
||||||
}
|
}
|
||||||
log("Stats", stats);
|
log("Stats", stats);
|
||||||
this.syncedObservableServer.send((await this.oplogDB.getAll()).length <= 0)
|
this.syncedObservableServer.send(
|
||||||
|
(await this.oplogDB.getAll()).length <= 0
|
||||||
|
);
|
||||||
} finally {
|
} finally {
|
||||||
log("Finished")
|
log("Finished");
|
||||||
lock.release()
|
lock.release();
|
||||||
this.syncObservableServer.send(false);
|
this.syncObservableServer.send(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -408,15 +444,16 @@ class NotesProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public getVaults(): Promise<VaultList> {
|
public getVaults(): Promise<VaultList> {
|
||||||
return this.noteDB.getAll()
|
return this.noteDB.getAll().then((notes) =>
|
||||||
.then(notes => notes
|
notes
|
||||||
.filter(e => Decoder.decode(e.preview) === "__VAULT__")
|
.filter((e) => Decoder.decode(e.preview) === "__VAULT__")
|
||||||
.map(e => {
|
.map((e) => {
|
||||||
let value = e.__value;
|
let value = e.__value;
|
||||||
let encrypted = false;
|
let encrypted = false;
|
||||||
if (this.decrypt(value) !== "__BASELINE__") encrypted = true;
|
if (this.decrypt(value) !== "__BASELINE__") encrypted = true;
|
||||||
return { name: e.folder, encrypted, id: e._id }
|
return { name: e.folder, encrypted, id: e._id };
|
||||||
}));
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async createVault(name: string, key?: Uint8Array) {
|
public async createVault(name: string, key?: Uint8Array) {
|
||||||
@ -425,35 +462,44 @@ class NotesProvider {
|
|||||||
_id: uuidv4(),
|
_id: uuidv4(),
|
||||||
folder: name,
|
folder: name,
|
||||||
preview: Encoder.encode("__VAULT__"),
|
preview: Encoder.encode("__VAULT__"),
|
||||||
time: new Date()
|
time: new Date(),
|
||||||
}
|
};
|
||||||
let tx = this.database.transaction();
|
let tx = this.database.transaction();
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
this.addop(vault._id, OpLogType.CREATE, {
|
this.addop(
|
||||||
|
vault._id,
|
||||||
|
OpLogType.CREATE,
|
||||||
|
{
|
||||||
value: vault.__value,
|
value: vault.__value,
|
||||||
preview: vault.preview
|
preview: vault.preview,
|
||||||
}, vault.time, tx),
|
},
|
||||||
this.noteDB.set(vault._id, vault)
|
vault.time,
|
||||||
|
tx
|
||||||
|
),
|
||||||
|
this.noteDB.set(vault._id, vault),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getVault(vault_id: string, key?: Uint8Array): Promise<IVault> {
|
public async getVault(vault_id: string, key?: Uint8Array): Promise<IVault> {
|
||||||
let vault = await this.noteDB.get(vault_id);
|
let vault = await this.noteDB.get(vault_id);
|
||||||
if (!vault) throw new Error("Vault not found!");
|
if (!vault) throw new Error("Vault not found!");
|
||||||
if (this.decrypt(vault.__value, key) !== "__BASELINE__") throw new Error("Invalid password!");
|
if (this.decrypt(vault.__value, key) !== "__BASELINE__")
|
||||||
return new NotesProvider.Vault(vault, key)
|
throw new Error("Invalid password!");
|
||||||
|
return new NotesProvider.Vault(vault, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async deleteVault(vault_id: string) {
|
public async deleteVault(vault_id: string) {
|
||||||
let vault = await this.noteDB.get(vault_id);
|
let vault = await this.noteDB.get(vault_id);
|
||||||
if (!vault) throw new Error("Vault not found!");
|
if (!vault) throw new Error("Vault not found!");
|
||||||
let v = new NotesProvider.Vault(vault);
|
let v = new NotesProvider.Vault(vault);
|
||||||
await Promise.all((await v.getAllNotes()).map(note => this.delete(note._id)));
|
await Promise.all(
|
||||||
|
(await v.getAllNotes()).map((note) => this.delete(note._id))
|
||||||
|
);
|
||||||
await this.delete(v.id); // This can also delete a vault
|
await this.delete(v.id); // This can also delete a vault
|
||||||
}
|
}
|
||||||
|
|
||||||
public passwordToKey(password: string) {
|
public passwordToKey(password: string) {
|
||||||
return new Uint8Array(sha256.arrayBuffer(password + config.client_id))
|
return new Uint8Array(sha256.arrayBuffer(password + config.client_id));
|
||||||
}
|
}
|
||||||
|
|
||||||
private _encrypt(value: Uint8Array, key?: Uint8Array): Uint8Array {
|
private _encrypt(value: Uint8Array, key?: Uint8Array): Uint8Array {
|
||||||
@ -464,42 +510,48 @@ class NotesProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private encrypt(value: string, key?: Uint8Array): Uint8Array {
|
private encrypt(value: string, key?: Uint8Array): Uint8Array {
|
||||||
let msg = this._encrypt(Encoder.encode(value), key)
|
let msg = this._encrypt(Encoder.encode(value), key);
|
||||||
return new Uint8Array(this._encrypt(msg, this.generalEncryption))
|
return new Uint8Array(this._encrypt(msg, this.generalEncryption));
|
||||||
}
|
}
|
||||||
|
|
||||||
private _decrypt(value: ArrayBuffer, key?: Uint8Array): Uint8Array {
|
private _decrypt(value: ArrayBuffer, key?: Uint8Array): Uint8Array {
|
||||||
if (!key) return new Uint8Array(value);
|
if (!key) return new Uint8Array(value);
|
||||||
var aesCtr = new aesjs.ModeOfOperation.ctr(key);
|
var aesCtr = new aesjs.ModeOfOperation.ctr(key);
|
||||||
var decryptedBytes = aesCtr.decrypt(value);
|
var decryptedBytes = aesCtr.decrypt(value);
|
||||||
return new Uint8Array(decryptedBytes)
|
return new Uint8Array(decryptedBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
private decrypt(value: ArrayBuffer, key?: Uint8Array): string {
|
private decrypt(value: ArrayBuffer, key?: Uint8Array): string {
|
||||||
let msg = this._decrypt(value, key)
|
let msg = this._decrypt(value, key);
|
||||||
return Decoder.decode(this._decrypt(msg, this.generalEncryption))
|
return Decoder.decode(this._decrypt(msg, this.generalEncryption));
|
||||||
}
|
}
|
||||||
|
|
||||||
async addop(note_id: string, type: OpLogType, values: { value: Uint8Array, preview: Uint8Array }, date: Date, transaction?: IDBPTransaction<unknown, string[]>) {
|
async addop(
|
||||||
|
note_id: string,
|
||||||
|
type: OpLogType,
|
||||||
|
values: { value: Uint8Array; preview: Uint8Array },
|
||||||
|
date: Date,
|
||||||
|
transaction?: IDBPTransaction<unknown, string[]>
|
||||||
|
) {
|
||||||
let tx = transaction || this.oplogDB.transaction();
|
let tx = transaction || this.oplogDB.transaction();
|
||||||
let oplog = await this.oplogDB.get(note_id, tx);
|
let oplog = await this.oplogDB.get(note_id, tx);
|
||||||
if (!oplog) oplog = { logs: [], id: note_id };
|
if (!oplog) oplog = { logs: [], id: note_id };
|
||||||
oplog.logs.push({
|
oplog.logs.push({
|
||||||
date: date,
|
date: date,
|
||||||
type,
|
type,
|
||||||
values
|
values,
|
||||||
})
|
});
|
||||||
this.syncedObservableServer.send(false);
|
this.syncedObservableServer.send(false);
|
||||||
await this.oplogDB.set(note_id, oplog, tx);
|
await this.oplogDB.set(note_id, oplog, tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(id: string) {
|
async delete(id: string) {
|
||||||
let lock = await this.syncLock.getLock();
|
let lock = await this.syncLock.getLock();
|
||||||
let tx = this.database.transaction(this.oplogDB, this.noteDB)
|
let tx = this.database.transaction(this.oplogDB, this.noteDB);
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
this.addop(id, OpLogType.DELETE, null, new Date(), tx),
|
this.addop(id, OpLogType.DELETE, null, new Date(), tx),
|
||||||
this.noteDB.delete(id, tx)
|
this.noteDB.delete(id, tx),
|
||||||
])
|
]);
|
||||||
lock.release();
|
lock.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -522,15 +574,17 @@ class NotesProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getAllNotes() {
|
async getAllNotes() {
|
||||||
return Notes.noteDB.getAll()
|
return Notes.noteDB.getAll().then((all) =>
|
||||||
.then(all => all.filter(e => e.folder === this.vault._id)
|
all
|
||||||
|
.filter((e) => e.folder === this.vault._id)
|
||||||
.sort(this.sort)
|
.sort(this.sort)
|
||||||
.map<BaseNote>(e => {
|
.map<BaseNote>((e) => {
|
||||||
let new_note = clonedeep(<Note>e) as BaseNote
|
let new_note = clonedeep(<Note>e) as BaseNote;
|
||||||
delete (<any>new_note).__value
|
delete (<any>new_note).__value;
|
||||||
new_note.preview = this.decrypt(e.preview)
|
new_note.preview = this.decrypt(e.preview);
|
||||||
return new_note;
|
return new_note;
|
||||||
}));
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private sort(a: DBNote, b: DBNote) {
|
private sort(a: DBNote, b: DBNote) {
|
||||||
@ -539,7 +593,7 @@ class NotesProvider {
|
|||||||
|
|
||||||
async searchNote(term: string) {
|
async searchNote(term: string) {
|
||||||
let all = await this.getAllNotes();
|
let all = await this.getAllNotes();
|
||||||
return all.filter(e => e.preview.indexOf(term) >= 0)
|
return all.filter((e) => e.preview.indexOf(term) >= 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
newNote(): ViewNote {
|
newNote(): ViewNote {
|
||||||
@ -548,8 +602,8 @@ class NotesProvider {
|
|||||||
folder: this.vault._id,
|
folder: this.vault._id,
|
||||||
time: new Date(),
|
time: new Date(),
|
||||||
__value: "",
|
__value: "",
|
||||||
preview: ""
|
preview: "",
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async saveNote(note: ViewNote, date = new Date()) {
|
async saveNote(note: ViewNote, date = new Date()) {
|
||||||
@ -558,20 +612,26 @@ class NotesProvider {
|
|||||||
let old_note = await Notes.noteDB.get(note._id, tx);
|
let old_note = await Notes.noteDB.get(note._id, tx);
|
||||||
|
|
||||||
let new_note = clonedeep(<Note>note) as DBNote;
|
let new_note = clonedeep(<Note>note) as DBNote;
|
||||||
new_note.__value = this.encrypt(note.__value)
|
new_note.__value = this.encrypt(note.__value);
|
||||||
let [title, preview] = note.__value.split("\n");
|
let [title, preview] = note.__value.split("\n");
|
||||||
if (preview) preview = "\n" + preview;
|
if (preview) preview = "\n" + preview;
|
||||||
else preview = ""
|
else preview = "";
|
||||||
new_note.preview = this.encrypt((title + preview).substr(0, 128))
|
new_note.preview = this.encrypt((title + preview).substr(0, 128));
|
||||||
new_note.time = date;
|
new_note.time = date;
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
Notes.addop(note._id, !old_note ? OpLogType.CREATE : OpLogType.CHANGE, {
|
Notes.addop(
|
||||||
|
note._id,
|
||||||
|
!old_note ? OpLogType.CREATE : OpLogType.CHANGE,
|
||||||
|
{
|
||||||
value: new_note.__value,
|
value: new_note.__value,
|
||||||
preview: new_note.preview
|
preview: new_note.preview,
|
||||||
}, date, tx),
|
},
|
||||||
Notes.noteDB.set(note._id, new_note, tx)
|
date,
|
||||||
])
|
tx
|
||||||
|
),
|
||||||
|
Notes.noteDB.set(note._id, new_note, tx),
|
||||||
|
]);
|
||||||
lock.release();
|
lock.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -586,10 +646,10 @@ class NotesProvider {
|
|||||||
deleteNote(id: string) {
|
deleteNote(id: string) {
|
||||||
return Notes.delete(id);
|
return Notes.delete(id);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const Notes = new NotesProvider()
|
const Notes = new NotesProvider();
|
||||||
export default Notes;
|
export default Notes;
|
||||||
|
|
||||||
(<any>window).api = Notes
|
(<any>window).api = Notes;
|
||||||
|
Loading…
Reference in New Issue
Block a user