@dikolab/kbdb 0.4.0 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -22
- package/dist/{chunk-BJPSPNGX.mjs → chunk-BJXEVALU.mjs} +115 -1
- package/dist/chunk-BJXEVALU.mjs.map +7 -0
- package/dist/cli.cjs +462 -79
- package/dist/cli.cjs.map +4 -4
- package/dist/cli.mjs +356 -79
- package/dist/cli.mjs.map +4 -4
- package/dist/kbdb-worker.cjs +413 -53
- package/dist/kbdb-worker.cjs.map +4 -4
- package/dist/mod.cjs +230 -54
- package/dist/mod.cjs.map +4 -4
- package/dist/mod.mjs +229 -53
- package/dist/mod.mjs.map +4 -4
- package/dist/src/shared/cli/typings/cli-options.interface.d.ts +5 -0
- package/dist/src/shared/embedding/classes/tfidf-embedding-provider.class.d.ts +28 -0
- package/dist/src/shared/embedding/constants/tfidf-defaults.constant.d.ts +4 -0
- package/dist/src/shared/embedding/functions/create-embedding-provider.function.d.ts +5 -2
- package/dist/src/shared/embedding/index.d.ts +2 -0
- package/dist/src/shared/log/constants/log-colors.constant.d.ts +5 -0
- package/dist/src/shared/log/constants/log-defaults.constant.d.ts +3 -0
- package/dist/src/shared/log/constants/log-icons.constant.d.ts +3 -0
- package/dist/src/shared/log/functions/configure-log.function.d.ts +12 -0
- package/dist/src/shared/log/functions/log.function.d.ts +28 -0
- package/dist/src/shared/log/functions/parse-log-level.function.d.ts +10 -0
- package/dist/src/shared/log/functions/resolve-log-level.function.d.ts +12 -0
- package/dist/src/shared/log/functions/write-stderr.function.d.ts +10 -0
- package/dist/src/shared/log/index.d.ts +10 -0
- package/dist/src/shared/log/typings/log-config.interface.d.ts +7 -0
- package/dist/src/shared/log/typings/log-level.type.d.ts +7 -0
- package/dist/src/shared/log/typings/log-output.interface.d.ts +4 -0
- package/dist/src/shared/version/constants/version.constant.d.ts +1 -1
- package/dist/src/shared/worker-client/classes/worker-client.class.d.ts +7 -0
- package/dist/src/shared/worker-client/functions/retry-on-timeout.function.d.ts +1 -0
- package/dist/src/shared/worker-daemon/classes/worker-daemon.class.d.ts +1 -0
- package/dist/src/shared/worker-daemon/functions/run-worker.function.d.ts +6 -1
- package/dist/src/shared/worker-daemon/typings/wasm-modules.interface.d.ts +5 -0
- package/dist/wasm/default-embedding/kbdb_default_embedding.d.ts +55 -0
- package/dist/wasm/default-embedding/kbdb_default_embedding.js +156 -0
- package/dist/wasm/default-embedding/kbdb_default_embedding_bg.wasm +0 -0
- package/dist/wasm/default-embedding/kbdb_default_embedding_bg.wasm.d.ts +12 -0
- package/dist/wasm/default-embedding/package.json +13 -0
- package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
- package/dist/wasm/fs-migration/kbdb_fs_migration.d.ts +38 -0
- package/dist/wasm/fs-migration/kbdb_fs_migration.js +130 -0
- package/dist/wasm/fs-migration/kbdb_fs_migration_bg.wasm +0 -0
- package/dist/wasm/fs-migration/kbdb_fs_migration_bg.wasm.d.ts +9 -0
- package/dist/wasm/fs-migration/package.json +13 -0
- package/dist/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
- package/dist/wasm/query-parser/kbdb_query_parser_bg.wasm +0 -0
- package/dist/worker.mjs +306 -54
- package/dist/worker.mjs.map +4 -4
- package/docs/details/agent-tooling.md +10 -0
- package/docs/details/cli.md +29 -13
- package/docs/details/knowledge-base.md +15 -8
- package/docs/details/mcp-server.md +16 -1
- package/docs/details/search-and-ranking.md +6 -2
- package/docs/goals/cli.md +33 -4
- package/docs/goals/content-parser.md +1 -1
- package/docs/goals/hybrid-search.md +71 -42
- package/docs/goals/mcp.md +3 -1
- package/docs/goals/overview.md +1 -1
- package/docs/goals/query-parser.md +1 -1
- package/docs/goals/semantic-dedup.md +7 -3
- package/docs/goals/worker-daemon.md +31 -20
- package/docs/modules/overview.md +43 -22
- package/docs/modules/rust/embedding/module.md +91 -0
- package/docs/modules/rust/overview.md +56 -21
- package/docs/modules/rust/query-parser/functions/module.md +7 -5
- package/docs/modules/rust/query-parser/functions/pipeline.md +6 -138
- package/docs/modules/rust/query-parser/module.md +16 -22
- package/docs/modules/rust/query-parser/types/module.md +5 -4
- package/docs/modules/rust/query-parser/types/token.md +5 -1
- package/docs/modules/rust/shared/content-parser/functions.md +1 -1
- package/docs/modules/rust/shared/memory/module.md +1 -1
- package/docs/modules/rust/shared/module.md +33 -5
- package/docs/modules/rust/shared/pipeline/functions.md +141 -0
- package/docs/modules/rust/shared/pipeline/module.md +62 -0
- package/docs/modules/rust/shared/pipeline/types.md +43 -0
- package/docs/modules/rust/shared/section/module.md +1 -1
- package/docs/modules/rust/shared/section/types.md +1 -1
- package/docs/modules/typescript/cli.md +4 -2
- package/docs/modules/typescript/kbdb-worker.md +20 -5
- package/docs/modules/typescript/overview.md +28 -6
- package/docs/modules/typescript/shared/cli/functions.md +15 -4
- package/docs/modules/typescript/shared/cli/typings.md +21 -0
- package/docs/modules/typescript/shared/embedding/functions.md +30 -3
- package/docs/modules/typescript/shared/embedding/module.md +10 -4
- package/docs/modules/typescript/shared/embedding/typings.md +7 -5
- package/docs/modules/typescript/shared/log/constants.md +82 -0
- package/docs/modules/typescript/shared/log/functions.md +131 -0
- package/docs/modules/typescript/shared/log/module.md +153 -0
- package/docs/modules/typescript/shared/log/typings.md +82 -0
- package/docs/modules/typescript/shared/module.md +22 -6
- package/docs/modules/typescript/shared/worker-daemon/functions.md +19 -6
- package/docs/modules/typescript/shared/worker-daemon/module.md +5 -2
- package/docs/modules/typescript/shared/worker-daemon/typings.md +13 -7
- package/docs/release-notes/0.4.1.md +149 -0
- package/docs/release-notes/0.4.3.md +60 -0
- package/package.json +2 -2
- package/dist/chunk-BJPSPNGX.mjs.map +0 -7
package/dist/mod.mjs
CHANGED
|
@@ -1,8 +1,120 @@
|
|
|
1
1
|
// src/shared/version/constants/version.constant.ts
|
|
2
|
-
var VERSION = "0.4.
|
|
2
|
+
var VERSION = "0.4.3";
|
|
3
3
|
|
|
4
4
|
// src/shared/worker-client/classes/worker-client.class.ts
|
|
5
5
|
import { connect } from "node:net";
|
|
6
|
+
|
|
7
|
+
// src/shared/worker-client/functions/discover-daemon.function.ts
|
|
8
|
+
import { existsSync, readFileSync, unlinkSync } from "node:fs";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import { tmpdir } from "node:os";
|
|
11
|
+
|
|
12
|
+
// src/shared/platform/functions/is-process-alive.function.ts
|
|
13
|
+
function isProcessAlive(pid) {
|
|
14
|
+
try {
|
|
15
|
+
process.kill(pid, 0);
|
|
16
|
+
return true;
|
|
17
|
+
} catch {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// src/shared/log/typings/log-level.type.ts
|
|
23
|
+
var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
|
|
24
|
+
LogLevel2[LogLevel2["DEBUG"] = 0] = "DEBUG";
|
|
25
|
+
LogLevel2[LogLevel2["INFO"] = 1] = "INFO";
|
|
26
|
+
LogLevel2[LogLevel2["WARNING"] = 2] = "WARNING";
|
|
27
|
+
LogLevel2[LogLevel2["ERROR"] = 3] = "ERROR";
|
|
28
|
+
return LogLevel2;
|
|
29
|
+
})(LogLevel || {});
|
|
30
|
+
|
|
31
|
+
// src/shared/log/constants/log-colors.constant.ts
|
|
32
|
+
var ANSI_RESET = "\x1B[0m";
|
|
33
|
+
var LOG_COLORS = {
|
|
34
|
+
[0 /* DEBUG */]: "\x1B[37m",
|
|
35
|
+
[1 /* INFO */]: "\x1B[36m",
|
|
36
|
+
[2 /* WARNING */]: "\x1B[33m",
|
|
37
|
+
[3 /* ERROR */]: "\x1B[91m"
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/shared/log/constants/log-icons.constant.ts
|
|
41
|
+
var LOG_ICONS = {
|
|
42
|
+
[0 /* DEBUG */]: "\u25B8",
|
|
43
|
+
[1 /* INFO */]: "\u2139",
|
|
44
|
+
[2 /* WARNING */]: "\u26A0",
|
|
45
|
+
[3 /* ERROR */]: "\u2716"
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// src/shared/log/constants/log-defaults.constant.ts
|
|
49
|
+
var DEFAULT_LOG_LEVEL = 3 /* ERROR */;
|
|
50
|
+
|
|
51
|
+
// src/shared/log/functions/write-stderr.function.ts
|
|
52
|
+
function writeStderr(message) {
|
|
53
|
+
const g = globalThis;
|
|
54
|
+
const proc = g["process"];
|
|
55
|
+
if (proc?.stderr) {
|
|
56
|
+
proc.stderr.write(message);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const deno = g["Deno"];
|
|
60
|
+
deno?.stderr.writeSync(new TextEncoder().encode(message));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/shared/log/functions/log.function.ts
|
|
64
|
+
var currentLevel = DEFAULT_LOG_LEVEL;
|
|
65
|
+
var outputs = {
|
|
66
|
+
[0 /* DEBUG */]: writeStderr,
|
|
67
|
+
[1 /* INFO */]: writeStderr,
|
|
68
|
+
[2 /* WARNING */]: writeStderr,
|
|
69
|
+
[3 /* ERROR */]: writeStderr
|
|
70
|
+
};
|
|
71
|
+
function log(message, level = 3 /* ERROR */) {
|
|
72
|
+
if (level < currentLevel) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const color = LOG_COLORS[level];
|
|
76
|
+
const icon = LOG_ICONS[level];
|
|
77
|
+
const name = LogLevel[level];
|
|
78
|
+
const line = `${color}${icon} [${name}] ${message}${ANSI_RESET}
|
|
79
|
+
`;
|
|
80
|
+
outputs[level](line);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/shared/worker-client/functions/discover-daemon.function.ts
|
|
84
|
+
function discoverDaemon(contextId) {
|
|
85
|
+
log("discovering daemon for context: " + contextId, 0 /* DEBUG */);
|
|
86
|
+
const pidPath = join(tmpdir(), `kbdb-${contextId}.pid`);
|
|
87
|
+
if (!existsSync(pidPath)) {
|
|
88
|
+
log(
|
|
89
|
+
"no daemon PID file found for context: " + contextId,
|
|
90
|
+
0 /* DEBUG */
|
|
91
|
+
);
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
const pidStr = readFileSync(pidPath, "utf-8").trim();
|
|
95
|
+
const pid = parseInt(pidStr, 10);
|
|
96
|
+
if (isNaN(pid) || !isProcessAlive(pid)) {
|
|
97
|
+
log(
|
|
98
|
+
"daemon process not alive for context: " + contextId,
|
|
99
|
+
0 /* DEBUG */
|
|
100
|
+
);
|
|
101
|
+
try {
|
|
102
|
+
unlinkSync(pidPath);
|
|
103
|
+
} catch {
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
log("daemon found: pid=" + pid.toString(), 0 /* DEBUG */);
|
|
108
|
+
const socketPath = join(tmpdir(), `kbdb-${contextId}.sock`);
|
|
109
|
+
const crashPath = join(tmpdir(), `kbdb-${contextId}.crash`);
|
|
110
|
+
try {
|
|
111
|
+
unlinkSync(crashPath);
|
|
112
|
+
} catch {
|
|
113
|
+
}
|
|
114
|
+
return { pid, socketPath };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/shared/worker-client/classes/worker-client.class.ts
|
|
6
118
|
var WorkerClient = class {
|
|
7
119
|
/**
|
|
8
120
|
* @param socketPath - absolute path to the daemon's IPC socket
|
|
@@ -38,11 +150,13 @@ var WorkerClient = class {
|
|
|
38
150
|
* @throws {Error} if the connection cannot be established
|
|
39
151
|
*/
|
|
40
152
|
async connect() {
|
|
153
|
+
log("connecting to " + this.socketPath, 0 /* DEBUG */);
|
|
41
154
|
const maxRetries = 5;
|
|
42
155
|
let delay = 50;
|
|
43
156
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
44
157
|
try {
|
|
45
158
|
await this.tryConnect();
|
|
159
|
+
log("connected", 0 /* DEBUG */);
|
|
46
160
|
return;
|
|
47
161
|
} catch (err) {
|
|
48
162
|
const code = err.code;
|
|
@@ -50,6 +164,10 @@ var WorkerClient = class {
|
|
|
50
164
|
if (!retriable || attempt === maxRetries) {
|
|
51
165
|
throw err;
|
|
52
166
|
}
|
|
167
|
+
log(
|
|
168
|
+
"connect retry: attempt=" + attempt.toString() + " code=" + code + " delay=" + delay.toString() + "ms",
|
|
169
|
+
0 /* DEBUG */
|
|
170
|
+
);
|
|
53
171
|
await new Promise((r) => setTimeout(r, delay));
|
|
54
172
|
delay = Math.min(delay * 2, 1e3);
|
|
55
173
|
}
|
|
@@ -57,11 +175,32 @@ var WorkerClient = class {
|
|
|
57
175
|
}
|
|
58
176
|
/** Destroys the IPC socket and clears the connection state. */
|
|
59
177
|
disconnect() {
|
|
178
|
+
log("disconnecting client", 0 /* DEBUG */);
|
|
60
179
|
if (this.socket) {
|
|
61
180
|
this.socket.destroy();
|
|
62
181
|
this.socket = null;
|
|
63
182
|
}
|
|
64
183
|
}
|
|
184
|
+
/**
|
|
185
|
+
* Kills the daemon process and disconnects the client.
|
|
186
|
+
*
|
|
187
|
+
* Discovers the daemon via PID file and sends `SIGTERM`.
|
|
188
|
+
* Always disconnects the socket regardless of kill outcome.
|
|
189
|
+
*/
|
|
190
|
+
killDaemon() {
|
|
191
|
+
const info = discoverDaemon(this.contextId);
|
|
192
|
+
log(
|
|
193
|
+
"killing daemon: pid=" + (info ? info.pid.toString() : "unknown"),
|
|
194
|
+
0 /* DEBUG */
|
|
195
|
+
);
|
|
196
|
+
if (info) {
|
|
197
|
+
try {
|
|
198
|
+
process.kill(info.pid, "SIGTERM");
|
|
199
|
+
} catch {
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
this.disconnect();
|
|
203
|
+
}
|
|
65
204
|
/**
|
|
66
205
|
* Single connection attempt with keepalive and
|
|
67
206
|
* post-connect failure handlers.
|
|
@@ -72,9 +211,14 @@ var WorkerClient = class {
|
|
|
72
211
|
sock.removeListener("error", reject);
|
|
73
212
|
sock.setKeepAlive(true, 3e4);
|
|
74
213
|
sock.on("error", (err) => {
|
|
214
|
+
log(
|
|
215
|
+
"socket error: code=" + (err.code ?? err.message),
|
|
216
|
+
0 /* DEBUG */
|
|
217
|
+
);
|
|
75
218
|
this.handleSocketFailure(err);
|
|
76
219
|
});
|
|
77
220
|
sock.on("close", () => {
|
|
221
|
+
log("socket closed", 0 /* DEBUG */);
|
|
78
222
|
if (this.pending.size > 0) {
|
|
79
223
|
this.handleSocketFailure(
|
|
80
224
|
new Error("connection closed")
|
|
@@ -96,6 +240,10 @@ var WorkerClient = class {
|
|
|
96
240
|
* disconnected.
|
|
97
241
|
*/
|
|
98
242
|
handleSocketFailure(err) {
|
|
243
|
+
log(
|
|
244
|
+
"socket failure: pending=" + this.pending.size.toString() + " err=" + err.message,
|
|
245
|
+
0 /* DEBUG */
|
|
246
|
+
);
|
|
99
247
|
const sock = this.socket;
|
|
100
248
|
this.socket = null;
|
|
101
249
|
for (const [id, call] of this.pending) {
|
|
@@ -396,6 +544,10 @@ var WorkerClient = class {
|
|
|
396
544
|
throw new Error("not connected");
|
|
397
545
|
}
|
|
398
546
|
const id = this.nextId++;
|
|
547
|
+
log(
|
|
548
|
+
"call: method=" + method + " id=" + id.toString(),
|
|
549
|
+
0 /* DEBUG */
|
|
550
|
+
);
|
|
399
551
|
const request = {
|
|
400
552
|
jsonrpc: "2.0",
|
|
401
553
|
id,
|
|
@@ -409,6 +561,10 @@ var WorkerClient = class {
|
|
|
409
561
|
call.timer = setTimeout(() => {
|
|
410
562
|
if (!this.pending.has(id)) return;
|
|
411
563
|
this.pending.delete(id);
|
|
564
|
+
log(
|
|
565
|
+
"request timeout after " + String(timeoutMs) + "ms",
|
|
566
|
+
2 /* WARNING */
|
|
567
|
+
);
|
|
412
568
|
const err = new Error(
|
|
413
569
|
`request timeout after ${String(timeoutMs)}ms`
|
|
414
570
|
);
|
|
@@ -439,14 +595,31 @@ var WorkerClient = class {
|
|
|
439
595
|
this.pending.delete(response.id);
|
|
440
596
|
if (pending.timer) clearTimeout(pending.timer);
|
|
441
597
|
if (response.error) {
|
|
598
|
+
log(
|
|
599
|
+
"response error: id=" + response.id.toString() + " code=" + response.error.code.toString(),
|
|
600
|
+
0 /* DEBUG */
|
|
601
|
+
);
|
|
442
602
|
const err = new Error(response.error.message);
|
|
443
603
|
err.code = response.error.code;
|
|
444
604
|
pending.reject(err);
|
|
445
605
|
} else {
|
|
606
|
+
log(
|
|
607
|
+
"response ok: id=" + response.id.toString(),
|
|
608
|
+
0 /* DEBUG */
|
|
609
|
+
);
|
|
446
610
|
pending.resolve(response.result);
|
|
447
611
|
}
|
|
612
|
+
} else {
|
|
613
|
+
log(
|
|
614
|
+
"unmatched response id=" + response.id.toString(),
|
|
615
|
+
0 /* DEBUG */
|
|
616
|
+
);
|
|
448
617
|
}
|
|
449
|
-
} catch {
|
|
618
|
+
} catch (parseErr) {
|
|
619
|
+
log(
|
|
620
|
+
"json parse error: " + parseErr.message,
|
|
621
|
+
0 /* DEBUG */
|
|
622
|
+
);
|
|
450
623
|
}
|
|
451
624
|
}
|
|
452
625
|
}
|
|
@@ -458,13 +631,15 @@ import { join as join5 } from "node:path";
|
|
|
458
631
|
import { tmpdir as tmpdir4 } from "node:os";
|
|
459
632
|
|
|
460
633
|
// src/shared/worker-client/functions/resolve-db-path.function.ts
|
|
461
|
-
import { existsSync } from "node:fs";
|
|
462
|
-
import { join, resolve } from "node:path";
|
|
634
|
+
import { existsSync as existsSync2 } from "node:fs";
|
|
635
|
+
import { join as join2, resolve } from "node:path";
|
|
463
636
|
function resolveDbPath(targetDir) {
|
|
464
637
|
const base = targetDir ? resolve(targetDir) : process.cwd();
|
|
465
|
-
const dbPath =
|
|
466
|
-
if (!
|
|
467
|
-
|
|
638
|
+
const dbPath = join2(base, ".kbdb");
|
|
639
|
+
if (!existsSync2(dbPath)) {
|
|
640
|
+
const msg = `database not found: ${dbPath}`;
|
|
641
|
+
log(msg, 3 /* ERROR */);
|
|
642
|
+
throw new Error(msg);
|
|
468
643
|
}
|
|
469
644
|
return dbPath;
|
|
470
645
|
}
|
|
@@ -483,53 +658,22 @@ async function computeContextId(absolutePath) {
|
|
|
483
658
|
return hash.slice(0, 16);
|
|
484
659
|
}
|
|
485
660
|
|
|
486
|
-
// src/shared/worker-client/functions/discover-daemon.function.ts
|
|
487
|
-
import { existsSync as existsSync2, readFileSync, unlinkSync } from "node:fs";
|
|
488
|
-
import { join as join2 } from "node:path";
|
|
489
|
-
import { tmpdir } from "node:os";
|
|
490
|
-
|
|
491
|
-
// src/shared/platform/functions/is-process-alive.function.ts
|
|
492
|
-
function isProcessAlive(pid) {
|
|
493
|
-
try {
|
|
494
|
-
process.kill(pid, 0);
|
|
495
|
-
return true;
|
|
496
|
-
} catch {
|
|
497
|
-
return false;
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
// src/shared/worker-client/functions/discover-daemon.function.ts
|
|
502
|
-
function discoverDaemon(contextId) {
|
|
503
|
-
const pidPath = join2(tmpdir(), `kbdb-${contextId}.pid`);
|
|
504
|
-
if (!existsSync2(pidPath)) return null;
|
|
505
|
-
const pidStr = readFileSync(pidPath, "utf-8").trim();
|
|
506
|
-
const pid = parseInt(pidStr, 10);
|
|
507
|
-
if (isNaN(pid) || !isProcessAlive(pid)) {
|
|
508
|
-
try {
|
|
509
|
-
unlinkSync(pidPath);
|
|
510
|
-
} catch {
|
|
511
|
-
}
|
|
512
|
-
return null;
|
|
513
|
-
}
|
|
514
|
-
const socketPath = join2(tmpdir(), `kbdb-${contextId}.sock`);
|
|
515
|
-
const crashPath = join2(tmpdir(), `kbdb-${contextId}.crash`);
|
|
516
|
-
try {
|
|
517
|
-
unlinkSync(crashPath);
|
|
518
|
-
} catch {
|
|
519
|
-
}
|
|
520
|
-
return { pid, socketPath };
|
|
521
|
-
}
|
|
522
|
-
|
|
523
661
|
// src/shared/worker-client/functions/spawn-daemon.function.ts
|
|
524
662
|
import { spawn } from "node:child_process";
|
|
525
663
|
function spawnDaemon(contextPath, runtime, workerScript) {
|
|
526
664
|
if (!workerScript) {
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
);
|
|
665
|
+
const msg = "workerScript must be provided (resolve at entry point)";
|
|
666
|
+
log(msg, 3 /* ERROR */);
|
|
667
|
+
throw new Error(msg);
|
|
530
668
|
}
|
|
531
669
|
const execPath = runtime === "deno" ? "deno" : process.execPath;
|
|
532
|
-
const
|
|
670
|
+
const logLevelEnv = process.env["KBDB_LOG_LEVEL"];
|
|
671
|
+
const logArgs = logLevelEnv ? ["--log-level", logLevelEnv] : [];
|
|
672
|
+
const args = runtime === "deno" ? ["run", "--allow-all", workerScript, contextPath, ...logArgs] : [workerScript, contextPath, ...logArgs];
|
|
673
|
+
log(
|
|
674
|
+
"spawning daemon: " + execPath + " " + args.join(" "),
|
|
675
|
+
0 /* DEBUG */
|
|
676
|
+
);
|
|
533
677
|
const child = spawn(execPath, args, {
|
|
534
678
|
detached: true,
|
|
535
679
|
stdio: "ignore"
|
|
@@ -544,12 +688,30 @@ import { tmpdir as tmpdir2 } from "node:os";
|
|
|
544
688
|
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
545
689
|
var POLL_INTERVAL_MS = 100;
|
|
546
690
|
async function waitForDaemon(contextId, timeoutMs = DEFAULT_TIMEOUT_MS) {
|
|
691
|
+
log("waiting for daemon: contextId=" + contextId, 0 /* DEBUG */);
|
|
547
692
|
const pidPath = join3(tmpdir2(), `kbdb-${contextId}.pid`);
|
|
548
693
|
const start = Date.now();
|
|
694
|
+
let iteration = 0;
|
|
549
695
|
while (Date.now() - start < timeoutMs) {
|
|
550
|
-
if (existsSync3(pidPath))
|
|
696
|
+
if (existsSync3(pidPath)) {
|
|
697
|
+
log("daemon PID file appeared", 0 /* DEBUG */);
|
|
698
|
+
return true;
|
|
699
|
+
}
|
|
700
|
+
iteration++;
|
|
701
|
+
if (iteration % 10 === 0) {
|
|
702
|
+
const elapsed2 = Date.now() - start;
|
|
703
|
+
log(
|
|
704
|
+
"still waiting for daemon: elapsed=" + elapsed2.toString() + "ms",
|
|
705
|
+
0 /* DEBUG */
|
|
706
|
+
);
|
|
707
|
+
}
|
|
551
708
|
await new Promise((r) => setTimeout(r, POLL_INTERVAL_MS));
|
|
552
709
|
}
|
|
710
|
+
const elapsed = Date.now() - start;
|
|
711
|
+
log(
|
|
712
|
+
"timed out waiting for daemon: elapsed=" + elapsed.toString() + "ms",
|
|
713
|
+
0 /* DEBUG */
|
|
714
|
+
);
|
|
553
715
|
return false;
|
|
554
716
|
}
|
|
555
717
|
|
|
@@ -582,19 +744,28 @@ function getIpcPath(ctx) {
|
|
|
582
744
|
async function createWorkerClient(options) {
|
|
583
745
|
const dbPath = options?.contextPath ?? resolveDbPath(options?.dbPath);
|
|
584
746
|
const contextId = await computeContextId(dbPath);
|
|
747
|
+
log("create-worker-client: contextId=" + contextId, 0 /* DEBUG */);
|
|
585
748
|
const socketPath = getIpcPath(contextId);
|
|
586
749
|
let daemon = discoverDaemon(contextId);
|
|
750
|
+
if (daemon) {
|
|
751
|
+
log("daemon found: pid=" + daemon.pid.toString(), 0 /* DEBUG */);
|
|
752
|
+
} else {
|
|
753
|
+
log("no daemon found", 0 /* DEBUG */);
|
|
754
|
+
}
|
|
587
755
|
if (!daemon && options?.autoSpawn !== false) {
|
|
588
756
|
const runtime = detectRuntime();
|
|
757
|
+
log("auto-spawning daemon", 0 /* DEBUG */);
|
|
589
758
|
spawnDaemon(dbPath, runtime, options?.workerScript);
|
|
759
|
+
log("waiting for daemon after spawn", 0 /* DEBUG */);
|
|
590
760
|
await waitForDaemon(contextId, options?.connectTimeoutMs);
|
|
591
761
|
daemon = discoverDaemon(contextId);
|
|
592
762
|
}
|
|
593
763
|
if (!daemon) {
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
);
|
|
764
|
+
const msg = `kbdb worker daemon did not start: ${readCrashReason(contextId)}`;
|
|
765
|
+
log(msg, 3 /* ERROR */);
|
|
766
|
+
throw new Error(msg);
|
|
597
767
|
}
|
|
768
|
+
log("connecting to daemon", 0 /* DEBUG */);
|
|
598
769
|
const client = new WorkerClient(
|
|
599
770
|
socketPath,
|
|
600
771
|
contextId,
|
|
@@ -621,10 +792,15 @@ function readCrashReason(contextId) {
|
|
|
621
792
|
// src/shared/worker-client/functions/get-or-create-client.function.ts
|
|
622
793
|
var cache = /* @__PURE__ */ new Map();
|
|
623
794
|
async function getOrCreateClient(options) {
|
|
795
|
+
log("getOrCreateClient called", 0 /* DEBUG */);
|
|
624
796
|
const dbPath = options?.contextPath ?? resolveDbPath(options?.dbPath);
|
|
625
797
|
const cached = cache.get(dbPath);
|
|
626
|
-
if (cached?.isConnected)
|
|
798
|
+
if (cached?.isConnected) {
|
|
799
|
+
log("returning cached client for: " + dbPath, 0 /* DEBUG */);
|
|
800
|
+
return cached;
|
|
801
|
+
}
|
|
627
802
|
cache.delete(dbPath);
|
|
803
|
+
log("creating new client for: " + dbPath, 0 /* DEBUG */);
|
|
628
804
|
const client = await createWorkerClient(options);
|
|
629
805
|
cache.set(dbPath, client);
|
|
630
806
|
return client;
|