-
Notifications
You must be signed in to change notification settings - Fork 6
/
PeerCouchDB.ts
175 lines (173 loc) · 8.05 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { DirectFileManipulator, FileInfo, MetaEntry, ReadyEntry } from "./lib/src/API/DirectFileManipulatorV2.ts";
import { FilePathWithPrefix, LOG_LEVEL_NOTICE, MILSTONE_DOCID, TweakValues } from "./lib/src/common/types.ts";
import { PeerCouchDBConf, FileData } from "./types.ts";
import { decodeBinary } from "./lib/src/string_and_binary/convert.ts";
import { isPlainText } from "./lib/src/string_and_binary/path.ts";
import { DispatchFun, Peer } from "./Peer.ts";
import { createBinaryBlob, createTextBlob, isDocContentSame, unique } from "./lib/src/common/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.rawGet<Record<string, any>>(MILSTONE_DOCID);
if (w && "tweak_values" in w) {
if (this.config.useRemoteTweaks) {
const tweaks = Object.values(w["tweak_values"])[0] as TweakValues;
// console.log(tweaks)
const orgConf = { ...this.config } as Record<string, any>;
this.config.customChunkSize = tweaks.customChunkSize ?? this.config.customChunkSize;
this.config.minimumChunkSize = tweaks.minimumChunkSize ?? this.config.minimumChunkSize;
if (tweaks.encrypt && !this.config.passphrase) {
throw new Error("Remote database is encrypted but no passphrase provided.");
}
if (tweaks.usePathObfuscation && !this.config.obfuscatePassphrase) {
throw new Error("Remote database is obfuscated but no obfuscate passphrase provided.");
}
this.config.hashAlg = tweaks.hashAlg ?? this.config.hashAlg;
this.config.maxAgeInEden = tweaks.maxAgeInEden ?? this.config.maxAgeInEden;
this.config.maxTotalLengthInEden = tweaks.maxTotalLengthInEden ?? this.config.maxTotalLengthInEden;
this.config.maxChunksInEden = tweaks.maxChunksInEden ?? this.config.maxChunksInEden;
this.config.useEden = tweaks.useEden ?? this.config.useEden;
if (!this.config.enableCompression != !tweaks.enableCompression) {
throw new Error("Compression setting mismatched.");
}
this.config.useDynamicIterationCount = tweaks.useDynamicIterationCount ?? this.config.useDynamicIterationCount;
this.config.enableChunkSplitterV2 = tweaks.enableChunkSplitterV2 ?? this.config.enableChunkSplitterV2;
const newConf = { ...this.config } as Record<string, any>;
const diff = unique([...Object.keys(orgConf), ...Object.keys(tweaks)]).filter(k => orgConf[k] != newConf[k]);
if (diff.length > 0) {
this.normalLog(`Remote tweaks changed --->`);
for (const diffKey of diff) {
this.normalLog(`${diffKey}\t: ${orgConf[diffKey]} \t : ${newConf[diffKey]}`);
}
this.normalLog(`<--- Remote tweaks changed`);
}
}
}
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);
if (entry.path.indexOf(":") !== -1) return false;
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();
}
}