forked from vrtmrz/livesync-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeerCouchDB.ts
140 lines (138 loc) · 5.64 KB
/
PeerCouchDB.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { DirectFileManipulator, FileInfo, MetaEntry, ReadyEntry } from "./lib/src/DirectFileManipulator.ts";
import { FilePathWithPrefix, LOG_LEVEL_NOTICE, MILSTONE_DOCID } from "./lib/src/types.ts";
import { PeerCouchDBConf, FileData } from "./types.ts";
import { decodeBinary } from "./lib/src/strbin.ts";
import { isPlainText } from "./lib/src/path.ts";
import { DispatchFun, Peer } from "./Peer.ts";
import { createBinaryBlob, createTextBlob, isDocContentSame } from "./lib/src/utils.ts";
// export class PeerInstance()
export class PeerCouchDB extends Peer {
man: DirectFileManipulator;
declare config: PeerCouchDBConf;
constructor(conf: PeerCouchDBConf, dispatcher: DispatchFun) {
super(conf, dispatcher);
this.man = new DirectFileManipulator(conf);
// Fetch remote since.
this.man.since = this.getSetting("since") || "now";
}
async delete(pathSrc: string): Promise<boolean> {
const path = this.toLocalPath(pathSrc);
if (await this.isRepeating(pathSrc, false)) {
return false;
}
const r = await this.man.delete(path);
if (r) {
this.receiveLog(` ${path} deleted`);
} else {
this.receiveLog(` ${path} delete failed`, LOG_LEVEL_NOTICE);
}
return r;
}
async put(pathSrc: string, data: FileData): Promise<boolean> {
const path = this.toLocalPath(pathSrc);
if (await this.isRepeating(pathSrc, data)) {
return false;
}
const type = isPlainText(path) ? "plain" : "newnote";
const info: FileInfo = {
ctime: data.ctime,
mtime: data.mtime,
size: data.size
};
const saveData = (data.data instanceof Uint8Array) ? createBinaryBlob(data.data) : createTextBlob(data.data);
const old = await this.man.get(path as FilePathWithPrefix, true) as false | MetaEntry;
// const old = await this.getMeta(path as FilePathWithPrefix);
if (old && Math.abs(this.compareDate(info, old)) < 3600) {
const oldDoc = await this.man.getByMeta(old);
if (oldDoc && ("data" in oldDoc)) {
const d = oldDoc.type == "plain" ? createTextBlob(oldDoc.data) : createBinaryBlob(new Uint8Array(decodeBinary(oldDoc.data)));
if (await isDocContentSame(d, saveData)) {
this.normalLog(` Skipped (Same) ${path} `);
return false;
}
}
}
const r = await this.man.put(path, saveData, info, type);
if (r) {
this.receiveLog(` ${path} saved`);
} else {
this.receiveLog(` ${path} ignored`);
}
return r;
}
async get(pathSrc: FilePathWithPrefix): Promise<false | FileData> {
const path = this.toLocalPath(pathSrc) as FilePathWithPrefix;
const ret = await this.man.get(path) as false | ReadyEntry;
if (ret === false) {
return false;
}
return {
ctime: ret.ctime,
mtime: ret.mtime,
data: ret.type == "newnote" ? new Uint8Array(decodeBinary(ret.data)) : ret.data,
size: ret.size,
deleted: ret.deleted
};
}
async getMeta(pathSrc: FilePathWithPrefix): Promise<false | FileData> {
const path = this.toLocalPath(pathSrc) as FilePathWithPrefix;
const ret = await this.man.get(path, true) as false | MetaEntry;
if (ret === false) {
return false;
}
return {
ctime: ret.ctime,
mtime: ret.mtime,
data: [],
size: ret.size,
deleted: ret.deleted
};
}
async start(): Promise<void> {
const baseDir = this.toLocalPath("");
const w = await this.man._fetchJson([MILSTONE_DOCID], {}, "get", {}) as Record<string, any>;
const created = w.created;
if (this.getSetting("remote-created") !== `${created}`) {
this.man.since = "";
this.normalLog(`Remote database looks like rebuilt. fetch from the first again.`);
this.setSetting("remote-created", `${created}`);
} else {
this.normalLog(`Watch starting from ${this.man.since}`);
}
this.man.beginWatch(async (entry) => {
const d = entry.type == "plain" ? entry.data : new Uint8Array(decodeBinary(entry.data));
let path = entry.path.substring(baseDir.length);
if (path.startsWith("/")) {
path = path.substring(1);
}
if (entry.deleted || entry._deleted) {
this.sendLog(`${path} delete detected`);
await this.dispatchDeleted(path);
} else {
const docData = { ctime: entry.ctime, mtime: entry.mtime, size: entry.size, deleted: entry.deleted || entry._deleted, data: d };
this.sendLog(`${path} change detected`);
await this.dispatch(path, docData);
}
}, (entry) => {
this.setSetting("since", this.man.since);
return entry.path.startsWith(baseDir);
});
}
async dispatch(path: string, data: FileData | false) {
if (data === false) return;
if (!await this.isRepeating(path, data)) {
await this.dispatchToHub(this, this.toGlobalPath(path), data);
}
// else {
// this.receiveLog(`${path} dispatch repeating`);
// }
}
async dispatchDeleted(path: string) {
if (!await this.isRepeating(path, false)) {
await this.dispatchToHub(this, this.toGlobalPath(path), false);
}
}
async stop(): Promise<void> {
this.man.endWatch();
}
}