/
engineClient.ts
499 lines (461 loc) · 12.8 KB
/
engineClient.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
import {
APIUtils,
BulkGetNoteMetaResp,
BulkGetNoteResp,
BulkWriteNotesOpts,
ConfigUtils,
DeleteNoteResp,
DeleteSchemaResp,
DendronAPI,
DendronError,
DEngineClient,
DEngineInitResp,
DHookDict,
DVault,
EngineDeleteOpts,
EngineEventEmitter,
EngineInfoResp,
EngineSchemaWriteOpts,
EngineWriteOptsV2,
ERROR_SEVERITY,
ERROR_STATUS,
Event,
EventEmitter,
FindNoteOpts,
FuseEngine,
GetDecorationsOpts,
GetDecorationsResp,
GetNoteBlocksOpts,
GetNoteBlocksResp,
GetNoteMetaResp,
GetNoteResp,
GetSchemaResp,
NoteChangeEntry,
NoteDictsUtils,
NoteFnameDictUtils,
NoteIndexProps,
NoteProps,
NotePropsByFnameDict,
NotePropsByIdDict,
NotePropsMeta,
NoteUtils,
QueryNotesOpts,
QuerySchemaResp,
RenameNoteOpts,
RenameNoteResp,
RenderNoteOpts,
SchemaModuleProps,
VaultUtils,
WriteNoteResp,
WriteSchemaResp,
QueryNotesResp,
} from "@dendronhq/common-all";
import { createLogger, DConfig, DLogger } from "@dendronhq/common-server";
import fs from "fs-extra";
import _ from "lodash";
import {
NoteIndexLightProps,
SQLiteMetadataStore,
} from "./drivers/SQLiteMetadataStore";
import { HistoryService } from "./history";
import { EngineUtils } from "./utils";
type DendronEngineClientOpts = {
vaults: DVault[];
ws: string;
};
export class DendronEngineClient implements DEngineClient, EngineEventEmitter {
private _onNoteChangedEmitter = new EventEmitter<NoteChangeEntry[]>();
private _config;
public notes: NotePropsByIdDict;
public noteFnames: NotePropsByFnameDict;
public wsRoot: string;
public ws: string;
public fuseEngine: FuseEngine;
public api: DendronAPI;
public vaults: DVault[];
public history?: HistoryService;
public logger: DLogger;
public hooks: DHookDict;
static create({
port,
vaults,
ws,
history,
logger,
}: {
port: number | string;
history?: HistoryService;
logger?: DLogger;
} & DendronEngineClientOpts) {
const api = new DendronAPI({
endpoint: APIUtils.getLocalEndpoint(
_.isString(port) ? parseInt(port, 10) : port
),
apiPath: "api",
logger,
});
return new DendronEngineClient({ api, vaults, ws, history });
}
static getPort({ wsRoot }: { wsRoot: string }): number {
const portFile = EngineUtils.getPortFilePathForWorkspace({ wsRoot });
if (!fs.pathExistsSync(portFile)) {
throw new DendronError({ message: "no port file" });
}
return _.toInteger(_.trim(fs.readFileSync(portFile, { encoding: "utf8" })));
}
constructor({
api,
vaults,
ws,
history,
logger,
}: {
api: DendronAPI;
history?: HistoryService;
logger?: DLogger;
} & DendronEngineClientOpts) {
this.api = api;
this.notes = {};
this.noteFnames = {};
this.vaults = vaults;
this.wsRoot = ws;
this.ws = ws;
this.history = history;
this.logger = logger || createLogger();
const config = DConfig.readConfigSync(ws);
this.fuseEngine = new FuseEngine({
fuzzThreshold: ConfigUtils.getLookup(config).note.fuzzThreshold,
});
this.hooks = ConfigUtils.getWorkspace(config).hooks || {
onCreate: [],
};
this._config = config;
}
/**
* Event that fires upon the changing of note state in the engine after a set
* of NoteProps has been changed AND those changes have been reflected on the
* engine side. Note creation, deletion, and updates are all fired from this
* event.
*/
get onEngineNoteStateChanged(): Event<NoteChangeEntry[]> {
return this._onNoteChangedEmitter.event;
}
dispose() {
this._onNoteChangedEmitter.dispose();
}
/**
* Load all nodes
*/
async init(): Promise<DEngineInitResp> {
const resp = await this.api.workspaceInit({
uri: this.ws,
config: { vaults: this.vaults },
});
if (resp.error && resp.error.severity !== ERROR_SEVERITY.MINOR) {
return {
data: resp.data,
error: resp.error,
};
}
if (!resp.data) {
throw new DendronError({ message: "no data" });
}
const { notes, config } = resp.data;
this._config = config;
this.notes = notes;
this.noteFnames = NoteFnameDictUtils.createNotePropsByFnameDict(this.notes);
await this.fuseEngine.replaceNotesIndex(notes);
return {
error: resp.error,
data: {
notes,
config,
wsRoot: this.wsRoot,
vaults: this.vaults,
},
};
}
/**
* See {@link DStore.getNote}
*/
async getNote(id: string): Promise<GetNoteResp> {
if (this._config.dev?.enableEngineV3) {
return this.api.noteGet({ id, ws: this.ws });
} else {
const maybeNote = this.notes[id];
if (maybeNote) {
return { data: _.cloneDeep(maybeNote) };
} else {
return {
error: DendronError.createFromStatus({
status: ERROR_STATUS.CONTENT_NOT_FOUND,
message: `NoteProps not found for key ${id}.`,
severity: ERROR_SEVERITY.MINOR,
}),
};
}
}
}
async getNoteMeta(id: string): Promise<GetNoteMetaResp> {
if (this._config.dev?.enableEngineV3) {
return this.api.noteGetMeta({ id, ws: this.ws });
} else {
return this.getNote(id);
}
}
/**
* See {@link DEngine.bulkGetNotes}
* TODO: remove this.notes
*/
async bulkGetNotes(ids: string[]): Promise<BulkGetNoteResp> {
if (this._config.dev?.enableEngineV3) {
return this.api.noteBulkGet({ ids, ws: this.ws });
} else {
return {
data: ids.map((id) => {
return _.cloneDeep(this.notes[id]);
}),
};
}
}
/**
* See {@link DEngine.bulkGetNotesMeta}
* TODO: remove this.notes
*/
async bulkGetNotesMeta(ids: string[]): Promise<BulkGetNoteMetaResp> {
if (this._config.dev?.enableEngineV3) {
return this.api.noteBulkGetMeta({ ids, ws: this.ws });
} else {
return this.bulkGetNotes(ids);
}
}
/**
* See {@link DStore.findNotes}
*/
async findNotes(opts: FindNoteOpts): Promise<NoteProps[]> {
const resp = await this.api.noteFind({ ...opts, ws: this.ws });
return resp.data!;
}
/**
* See {@link DStore.findNotesMeta}
*/
async findNotesMeta(opts: FindNoteOpts): Promise<NotePropsMeta[]> {
const resp = await this.api.noteFindMeta({ ...opts, ws: this.ws });
return resp.data!;
}
async bulkWriteNotes(opts: BulkWriteNotesOpts) {
const resp = await this.api.engineBulkAdd({ opts, ws: this.ws });
const changed = resp.data;
if (changed) {
await this.refreshNotesV2(changed);
this._onNoteChangedEmitter.fire(changed);
}
return resp;
}
async deleteNote(
id: string,
opts?: EngineDeleteOpts
): Promise<DeleteNoteResp> {
const ws = this.ws;
const resp = await this.api.engineDelete({ id, opts, ws });
if (!resp.data) {
throw new DendronError({
message: `Failed to delete note with id ${id}`,
payload: resp.error,
});
}
await this.refreshNotesV2(resp.data);
if (resp.data !== undefined) {
this._onNoteChangedEmitter.fire(resp.data);
}
return {
data: resp.data,
};
}
async deleteSchema(
id: string,
opts?: EngineDeleteOpts
): Promise<DeleteSchemaResp> {
const ws = this.ws;
const resp = await this.api.schemaDelete({ id, opts, ws });
if (!resp?.data?.notes) {
throw new DendronError({ message: "bad delete operation" });
}
const { notes } = resp.data;
this.notes = notes;
this.noteFnames = NoteFnameDictUtils.createNotePropsByFnameDict(this.notes);
this.fuseEngine.replaceNotesIndex(notes);
return {
data: resp.data,
};
}
async info(): Promise<EngineInfoResp> {
const resp = await this.api.engineInfo();
return resp;
}
async queryNotes(opts: QueryNotesOpts): Promise<QueryNotesResp> {
const { qs, onlyDirectChildren, vault, originalQS } = opts;
let noteIndexProps: NoteIndexProps[] | NoteIndexLightProps[];
const config = DConfig.readConfigSync(this.wsRoot);
if (config.workspace.metadataStore === "sqlite") {
try {
const resp = await SQLiteMetadataStore.search(qs);
noteIndexProps = resp.hits;
this.logger.debug({ ctx: "queryNote", query: resp.query });
} catch (err) {
fs.appendFileSync("/tmp/out.log", "ERROR: unable to query note", {
encoding: "utf8",
});
noteIndexProps = [];
}
} else {
noteIndexProps = await this.fuseEngine.queryNote({
qs,
onlyDirectChildren,
originalQS,
});
}
let noteProps = noteIndexProps.map((ent) => this.notes[ent.id]);
// TODO: hack
if (!_.isUndefined(vault)) {
noteProps = noteProps.filter((ent) =>
VaultUtils.isEqual(vault, ent.vault, this.wsRoot)
);
}
return noteProps;
}
async renderNote(opts: RenderNoteOpts) {
return this.api.noteRender({ ...opts, ws: this.ws });
}
async refreshNotesV2(notes: NoteChangeEntry[]) {
if (_.isUndefined(notes)) {
return;
}
const noteDicts = {
notesById: this.notes,
notesByFname: this.noteFnames,
};
notes.forEach((ent: NoteChangeEntry) => {
const uri = NoteUtils.getURI({ note: ent.note, wsRoot: this.wsRoot });
if (ent.status === "delete") {
NoteDictsUtils.delete(ent.note, noteDicts);
this.history?.add({ source: "engine", action: "delete", uri });
} else {
if (ent.status === "create") {
this.history?.add({ source: "engine", action: "create", uri });
}
if (ent.status === "update") {
// If the note id has changed, delete previous entry from dict before adding
if (ent.prevNote && ent.prevNote.id !== ent.note.id) {
NoteDictsUtils.delete(ent.prevNote, noteDicts);
}
ent.note.children = _.sortBy(
ent.note.children,
(id) =>
_.get(
this.notes,
id,
_.find(notes, (ent) => ent.note.id === id)?.note || {
title: "foo",
}
).title
);
}
NoteDictsUtils.add(ent.note, noteDicts);
}
});
this.fuseEngine.replaceNotesIndex(this.notes);
}
/** Renames the note.
*
* WARNING: When doing bulk operations. Do not invoke multiple requests to this
* command in parallel, wait for a single call to finish before requesting another call.
* Otherwise some race condition starts to cause intermittent failures.
* */
async renameNote(opts: RenameNoteOpts): Promise<RenameNoteResp> {
const resp = await this.api.engineRenameNote({ ...opts, ws: this.ws });
if (resp.error || _.isUndefined(resp.data)) {
throw resp.error;
}
await this.refreshNotesV2(resp.data as NoteChangeEntry[]);
if (resp.data) {
this._onNoteChangedEmitter.fire(resp.data);
}
return resp;
}
async sync(): Promise<DEngineInitResp> {
const resp = await this.api.workspaceSync({ ws: this.ws });
if (!resp.data) {
throw new DendronError({ message: "no data", payload: resp });
}
const { notes, config } = resp.data;
this.notes = notes;
this.noteFnames = NoteFnameDictUtils.createNotePropsByFnameDict(this.notes);
await this.fuseEngine.replaceNotesIndex(notes);
return {
error: resp.error,
data: {
notes,
vaults: this.vaults,
wsRoot: this.wsRoot,
config,
},
};
}
async writeNote(
note: NoteProps,
opts?: EngineWriteOptsV2
): Promise<WriteNoteResp> {
const resp = await this.api.engineWrite({
node: note,
opts,
ws: this.ws,
});
const changed = resp.data;
if (resp.error) {
return resp;
}
if (changed) {
await this.refreshNotesV2(changed);
this._onNoteChangedEmitter.fire(changed);
}
return resp;
}
// ~~~ schemas
async getSchema(id: string): Promise<GetSchemaResp> {
return this.api.schemaRead({ id, ws: this.ws });
}
async querySchema(qs: string): Promise<QuerySchemaResp> {
const out = await this.api.schemaQuery({ qs, ws: this.ws });
return _.defaults(out, { data: [] });
}
async writeSchema(
schema: SchemaModuleProps,
opts?: EngineSchemaWriteOpts
): Promise<WriteSchemaResp> {
this.logger.debug({
ctx: "engineClient.writeSchema",
schema: schema.fname,
metaOnly: opts?.metaOnly,
});
return this.api.schemaWrite({ schema, ws: this.ws, opts });
}
async getNoteBlocks({
id,
filterByAnchorType,
}: GetNoteBlocksOpts): Promise<GetNoteBlocksResp> {
const out = await this.api.getNoteBlocks({
id,
filterByAnchorType,
ws: this.ws,
});
return out;
}
async getDecorations(opts: GetDecorationsOpts): Promise<GetDecorationsResp> {
const out = await this.api.getDecorations({
...opts,
ws: this.ws,
});
return out;
}
}