@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.cjs
CHANGED
|
@@ -30,10 +30,122 @@ __export(mod_exports, {
|
|
|
30
30
|
module.exports = __toCommonJS(mod_exports);
|
|
31
31
|
|
|
32
32
|
// src/shared/version/constants/version.constant.ts
|
|
33
|
-
var VERSION = "0.4.
|
|
33
|
+
var VERSION = "0.4.3";
|
|
34
34
|
|
|
35
35
|
// src/shared/worker-client/classes/worker-client.class.ts
|
|
36
36
|
var import_node_net = require("node:net");
|
|
37
|
+
|
|
38
|
+
// src/shared/worker-client/functions/discover-daemon.function.ts
|
|
39
|
+
var import_node_fs = require("node:fs");
|
|
40
|
+
var import_node_path = require("node:path");
|
|
41
|
+
var import_node_os = require("node:os");
|
|
42
|
+
|
|
43
|
+
// src/shared/platform/functions/is-process-alive.function.ts
|
|
44
|
+
function isProcessAlive(pid) {
|
|
45
|
+
try {
|
|
46
|
+
process.kill(pid, 0);
|
|
47
|
+
return true;
|
|
48
|
+
} catch {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/shared/log/typings/log-level.type.ts
|
|
54
|
+
var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
|
|
55
|
+
LogLevel2[LogLevel2["DEBUG"] = 0] = "DEBUG";
|
|
56
|
+
LogLevel2[LogLevel2["INFO"] = 1] = "INFO";
|
|
57
|
+
LogLevel2[LogLevel2["WARNING"] = 2] = "WARNING";
|
|
58
|
+
LogLevel2[LogLevel2["ERROR"] = 3] = "ERROR";
|
|
59
|
+
return LogLevel2;
|
|
60
|
+
})(LogLevel || {});
|
|
61
|
+
|
|
62
|
+
// src/shared/log/constants/log-colors.constant.ts
|
|
63
|
+
var ANSI_RESET = "\x1B[0m";
|
|
64
|
+
var LOG_COLORS = {
|
|
65
|
+
[0 /* DEBUG */]: "\x1B[37m",
|
|
66
|
+
[1 /* INFO */]: "\x1B[36m",
|
|
67
|
+
[2 /* WARNING */]: "\x1B[33m",
|
|
68
|
+
[3 /* ERROR */]: "\x1B[91m"
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// src/shared/log/constants/log-icons.constant.ts
|
|
72
|
+
var LOG_ICONS = {
|
|
73
|
+
[0 /* DEBUG */]: "\u25B8",
|
|
74
|
+
[1 /* INFO */]: "\u2139",
|
|
75
|
+
[2 /* WARNING */]: "\u26A0",
|
|
76
|
+
[3 /* ERROR */]: "\u2716"
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// src/shared/log/constants/log-defaults.constant.ts
|
|
80
|
+
var DEFAULT_LOG_LEVEL = 3 /* ERROR */;
|
|
81
|
+
|
|
82
|
+
// src/shared/log/functions/write-stderr.function.ts
|
|
83
|
+
function writeStderr(message) {
|
|
84
|
+
const g = globalThis;
|
|
85
|
+
const proc = g["process"];
|
|
86
|
+
if (proc?.stderr) {
|
|
87
|
+
proc.stderr.write(message);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const deno = g["Deno"];
|
|
91
|
+
deno?.stderr.writeSync(new TextEncoder().encode(message));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/shared/log/functions/log.function.ts
|
|
95
|
+
var currentLevel = DEFAULT_LOG_LEVEL;
|
|
96
|
+
var outputs = {
|
|
97
|
+
[0 /* DEBUG */]: writeStderr,
|
|
98
|
+
[1 /* INFO */]: writeStderr,
|
|
99
|
+
[2 /* WARNING */]: writeStderr,
|
|
100
|
+
[3 /* ERROR */]: writeStderr
|
|
101
|
+
};
|
|
102
|
+
function log(message, level = 3 /* ERROR */) {
|
|
103
|
+
if (level < currentLevel) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const color = LOG_COLORS[level];
|
|
107
|
+
const icon = LOG_ICONS[level];
|
|
108
|
+
const name = LogLevel[level];
|
|
109
|
+
const line = `${color}${icon} [${name}] ${message}${ANSI_RESET}
|
|
110
|
+
`;
|
|
111
|
+
outputs[level](line);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// src/shared/worker-client/functions/discover-daemon.function.ts
|
|
115
|
+
function discoverDaemon(contextId) {
|
|
116
|
+
log("discovering daemon for context: " + contextId, 0 /* DEBUG */);
|
|
117
|
+
const pidPath = (0, import_node_path.join)((0, import_node_os.tmpdir)(), `kbdb-${contextId}.pid`);
|
|
118
|
+
if (!(0, import_node_fs.existsSync)(pidPath)) {
|
|
119
|
+
log(
|
|
120
|
+
"no daemon PID file found for context: " + contextId,
|
|
121
|
+
0 /* DEBUG */
|
|
122
|
+
);
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
const pidStr = (0, import_node_fs.readFileSync)(pidPath, "utf-8").trim();
|
|
126
|
+
const pid = parseInt(pidStr, 10);
|
|
127
|
+
if (isNaN(pid) || !isProcessAlive(pid)) {
|
|
128
|
+
log(
|
|
129
|
+
"daemon process not alive for context: " + contextId,
|
|
130
|
+
0 /* DEBUG */
|
|
131
|
+
);
|
|
132
|
+
try {
|
|
133
|
+
(0, import_node_fs.unlinkSync)(pidPath);
|
|
134
|
+
} catch {
|
|
135
|
+
}
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
log("daemon found: pid=" + pid.toString(), 0 /* DEBUG */);
|
|
139
|
+
const socketPath = (0, import_node_path.join)((0, import_node_os.tmpdir)(), `kbdb-${contextId}.sock`);
|
|
140
|
+
const crashPath = (0, import_node_path.join)((0, import_node_os.tmpdir)(), `kbdb-${contextId}.crash`);
|
|
141
|
+
try {
|
|
142
|
+
(0, import_node_fs.unlinkSync)(crashPath);
|
|
143
|
+
} catch {
|
|
144
|
+
}
|
|
145
|
+
return { pid, socketPath };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/shared/worker-client/classes/worker-client.class.ts
|
|
37
149
|
var WorkerClient = class {
|
|
38
150
|
/**
|
|
39
151
|
* @param socketPath - absolute path to the daemon's IPC socket
|
|
@@ -69,11 +181,13 @@ var WorkerClient = class {
|
|
|
69
181
|
* @throws {Error} if the connection cannot be established
|
|
70
182
|
*/
|
|
71
183
|
async connect() {
|
|
184
|
+
log("connecting to " + this.socketPath, 0 /* DEBUG */);
|
|
72
185
|
const maxRetries = 5;
|
|
73
186
|
let delay = 50;
|
|
74
187
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
75
188
|
try {
|
|
76
189
|
await this.tryConnect();
|
|
190
|
+
log("connected", 0 /* DEBUG */);
|
|
77
191
|
return;
|
|
78
192
|
} catch (err) {
|
|
79
193
|
const code = err.code;
|
|
@@ -81,6 +195,10 @@ var WorkerClient = class {
|
|
|
81
195
|
if (!retriable || attempt === maxRetries) {
|
|
82
196
|
throw err;
|
|
83
197
|
}
|
|
198
|
+
log(
|
|
199
|
+
"connect retry: attempt=" + attempt.toString() + " code=" + code + " delay=" + delay.toString() + "ms",
|
|
200
|
+
0 /* DEBUG */
|
|
201
|
+
);
|
|
84
202
|
await new Promise((r) => setTimeout(r, delay));
|
|
85
203
|
delay = Math.min(delay * 2, 1e3);
|
|
86
204
|
}
|
|
@@ -88,11 +206,32 @@ var WorkerClient = class {
|
|
|
88
206
|
}
|
|
89
207
|
/** Destroys the IPC socket and clears the connection state. */
|
|
90
208
|
disconnect() {
|
|
209
|
+
log("disconnecting client", 0 /* DEBUG */);
|
|
91
210
|
if (this.socket) {
|
|
92
211
|
this.socket.destroy();
|
|
93
212
|
this.socket = null;
|
|
94
213
|
}
|
|
95
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* Kills the daemon process and disconnects the client.
|
|
217
|
+
*
|
|
218
|
+
* Discovers the daemon via PID file and sends `SIGTERM`.
|
|
219
|
+
* Always disconnects the socket regardless of kill outcome.
|
|
220
|
+
*/
|
|
221
|
+
killDaemon() {
|
|
222
|
+
const info = discoverDaemon(this.contextId);
|
|
223
|
+
log(
|
|
224
|
+
"killing daemon: pid=" + (info ? info.pid.toString() : "unknown"),
|
|
225
|
+
0 /* DEBUG */
|
|
226
|
+
);
|
|
227
|
+
if (info) {
|
|
228
|
+
try {
|
|
229
|
+
process.kill(info.pid, "SIGTERM");
|
|
230
|
+
} catch {
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
this.disconnect();
|
|
234
|
+
}
|
|
96
235
|
/**
|
|
97
236
|
* Single connection attempt with keepalive and
|
|
98
237
|
* post-connect failure handlers.
|
|
@@ -103,9 +242,14 @@ var WorkerClient = class {
|
|
|
103
242
|
sock.removeListener("error", reject);
|
|
104
243
|
sock.setKeepAlive(true, 3e4);
|
|
105
244
|
sock.on("error", (err) => {
|
|
245
|
+
log(
|
|
246
|
+
"socket error: code=" + (err.code ?? err.message),
|
|
247
|
+
0 /* DEBUG */
|
|
248
|
+
);
|
|
106
249
|
this.handleSocketFailure(err);
|
|
107
250
|
});
|
|
108
251
|
sock.on("close", () => {
|
|
252
|
+
log("socket closed", 0 /* DEBUG */);
|
|
109
253
|
if (this.pending.size > 0) {
|
|
110
254
|
this.handleSocketFailure(
|
|
111
255
|
new Error("connection closed")
|
|
@@ -127,6 +271,10 @@ var WorkerClient = class {
|
|
|
127
271
|
* disconnected.
|
|
128
272
|
*/
|
|
129
273
|
handleSocketFailure(err) {
|
|
274
|
+
log(
|
|
275
|
+
"socket failure: pending=" + this.pending.size.toString() + " err=" + err.message,
|
|
276
|
+
0 /* DEBUG */
|
|
277
|
+
);
|
|
130
278
|
const sock = this.socket;
|
|
131
279
|
this.socket = null;
|
|
132
280
|
for (const [id, call] of this.pending) {
|
|
@@ -427,6 +575,10 @@ var WorkerClient = class {
|
|
|
427
575
|
throw new Error("not connected");
|
|
428
576
|
}
|
|
429
577
|
const id = this.nextId++;
|
|
578
|
+
log(
|
|
579
|
+
"call: method=" + method + " id=" + id.toString(),
|
|
580
|
+
0 /* DEBUG */
|
|
581
|
+
);
|
|
430
582
|
const request = {
|
|
431
583
|
jsonrpc: "2.0",
|
|
432
584
|
id,
|
|
@@ -440,6 +592,10 @@ var WorkerClient = class {
|
|
|
440
592
|
call.timer = setTimeout(() => {
|
|
441
593
|
if (!this.pending.has(id)) return;
|
|
442
594
|
this.pending.delete(id);
|
|
595
|
+
log(
|
|
596
|
+
"request timeout after " + String(timeoutMs) + "ms",
|
|
597
|
+
2 /* WARNING */
|
|
598
|
+
);
|
|
443
599
|
const err = new Error(
|
|
444
600
|
`request timeout after ${String(timeoutMs)}ms`
|
|
445
601
|
);
|
|
@@ -470,14 +626,31 @@ var WorkerClient = class {
|
|
|
470
626
|
this.pending.delete(response.id);
|
|
471
627
|
if (pending.timer) clearTimeout(pending.timer);
|
|
472
628
|
if (response.error) {
|
|
629
|
+
log(
|
|
630
|
+
"response error: id=" + response.id.toString() + " code=" + response.error.code.toString(),
|
|
631
|
+
0 /* DEBUG */
|
|
632
|
+
);
|
|
473
633
|
const err = new Error(response.error.message);
|
|
474
634
|
err.code = response.error.code;
|
|
475
635
|
pending.reject(err);
|
|
476
636
|
} else {
|
|
637
|
+
log(
|
|
638
|
+
"response ok: id=" + response.id.toString(),
|
|
639
|
+
0 /* DEBUG */
|
|
640
|
+
);
|
|
477
641
|
pending.resolve(response.result);
|
|
478
642
|
}
|
|
643
|
+
} else {
|
|
644
|
+
log(
|
|
645
|
+
"unmatched response id=" + response.id.toString(),
|
|
646
|
+
0 /* DEBUG */
|
|
647
|
+
);
|
|
479
648
|
}
|
|
480
|
-
} catch {
|
|
649
|
+
} catch (parseErr) {
|
|
650
|
+
log(
|
|
651
|
+
"json parse error: " + parseErr.message,
|
|
652
|
+
0 /* DEBUG */
|
|
653
|
+
);
|
|
481
654
|
}
|
|
482
655
|
}
|
|
483
656
|
}
|
|
@@ -489,13 +662,15 @@ var import_node_path5 = require("node:path");
|
|
|
489
662
|
var import_node_os4 = require("node:os");
|
|
490
663
|
|
|
491
664
|
// src/shared/worker-client/functions/resolve-db-path.function.ts
|
|
492
|
-
var
|
|
493
|
-
var
|
|
665
|
+
var import_node_fs2 = require("node:fs");
|
|
666
|
+
var import_node_path2 = require("node:path");
|
|
494
667
|
function resolveDbPath(targetDir) {
|
|
495
|
-
const base = targetDir ? (0,
|
|
496
|
-
const dbPath = (0,
|
|
497
|
-
if (!(0,
|
|
498
|
-
|
|
668
|
+
const base = targetDir ? (0, import_node_path2.resolve)(targetDir) : process.cwd();
|
|
669
|
+
const dbPath = (0, import_node_path2.join)(base, ".kbdb");
|
|
670
|
+
if (!(0, import_node_fs2.existsSync)(dbPath)) {
|
|
671
|
+
const msg = `database not found: ${dbPath}`;
|
|
672
|
+
log(msg, 3 /* ERROR */);
|
|
673
|
+
throw new Error(msg);
|
|
499
674
|
}
|
|
500
675
|
return dbPath;
|
|
501
676
|
}
|
|
@@ -514,53 +689,22 @@ async function computeContextId(absolutePath) {
|
|
|
514
689
|
return hash.slice(0, 16);
|
|
515
690
|
}
|
|
516
691
|
|
|
517
|
-
// src/shared/worker-client/functions/discover-daemon.function.ts
|
|
518
|
-
var import_node_fs2 = require("node:fs");
|
|
519
|
-
var import_node_path2 = require("node:path");
|
|
520
|
-
var import_node_os = require("node:os");
|
|
521
|
-
|
|
522
|
-
// src/shared/platform/functions/is-process-alive.function.ts
|
|
523
|
-
function isProcessAlive(pid) {
|
|
524
|
-
try {
|
|
525
|
-
process.kill(pid, 0);
|
|
526
|
-
return true;
|
|
527
|
-
} catch {
|
|
528
|
-
return false;
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
// src/shared/worker-client/functions/discover-daemon.function.ts
|
|
533
|
-
function discoverDaemon(contextId) {
|
|
534
|
-
const pidPath = (0, import_node_path2.join)((0, import_node_os.tmpdir)(), `kbdb-${contextId}.pid`);
|
|
535
|
-
if (!(0, import_node_fs2.existsSync)(pidPath)) return null;
|
|
536
|
-
const pidStr = (0, import_node_fs2.readFileSync)(pidPath, "utf-8").trim();
|
|
537
|
-
const pid = parseInt(pidStr, 10);
|
|
538
|
-
if (isNaN(pid) || !isProcessAlive(pid)) {
|
|
539
|
-
try {
|
|
540
|
-
(0, import_node_fs2.unlinkSync)(pidPath);
|
|
541
|
-
} catch {
|
|
542
|
-
}
|
|
543
|
-
return null;
|
|
544
|
-
}
|
|
545
|
-
const socketPath = (0, import_node_path2.join)((0, import_node_os.tmpdir)(), `kbdb-${contextId}.sock`);
|
|
546
|
-
const crashPath = (0, import_node_path2.join)((0, import_node_os.tmpdir)(), `kbdb-${contextId}.crash`);
|
|
547
|
-
try {
|
|
548
|
-
(0, import_node_fs2.unlinkSync)(crashPath);
|
|
549
|
-
} catch {
|
|
550
|
-
}
|
|
551
|
-
return { pid, socketPath };
|
|
552
|
-
}
|
|
553
|
-
|
|
554
692
|
// src/shared/worker-client/functions/spawn-daemon.function.ts
|
|
555
693
|
var import_node_child_process = require("node:child_process");
|
|
556
694
|
function spawnDaemon(contextPath, runtime, workerScript) {
|
|
557
695
|
if (!workerScript) {
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
);
|
|
696
|
+
const msg = "workerScript must be provided (resolve at entry point)";
|
|
697
|
+
log(msg, 3 /* ERROR */);
|
|
698
|
+
throw new Error(msg);
|
|
561
699
|
}
|
|
562
700
|
const execPath = runtime === "deno" ? "deno" : process.execPath;
|
|
563
|
-
const
|
|
701
|
+
const logLevelEnv = process.env["KBDB_LOG_LEVEL"];
|
|
702
|
+
const logArgs = logLevelEnv ? ["--log-level", logLevelEnv] : [];
|
|
703
|
+
const args = runtime === "deno" ? ["run", "--allow-all", workerScript, contextPath, ...logArgs] : [workerScript, contextPath, ...logArgs];
|
|
704
|
+
log(
|
|
705
|
+
"spawning daemon: " + execPath + " " + args.join(" "),
|
|
706
|
+
0 /* DEBUG */
|
|
707
|
+
);
|
|
564
708
|
const child = (0, import_node_child_process.spawn)(execPath, args, {
|
|
565
709
|
detached: true,
|
|
566
710
|
stdio: "ignore"
|
|
@@ -575,12 +719,30 @@ var import_node_os2 = require("node:os");
|
|
|
575
719
|
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
576
720
|
var POLL_INTERVAL_MS = 100;
|
|
577
721
|
async function waitForDaemon(contextId, timeoutMs = DEFAULT_TIMEOUT_MS) {
|
|
722
|
+
log("waiting for daemon: contextId=" + contextId, 0 /* DEBUG */);
|
|
578
723
|
const pidPath = (0, import_node_path3.join)((0, import_node_os2.tmpdir)(), `kbdb-${contextId}.pid`);
|
|
579
724
|
const start = Date.now();
|
|
725
|
+
let iteration = 0;
|
|
580
726
|
while (Date.now() - start < timeoutMs) {
|
|
581
|
-
if ((0, import_node_fs3.existsSync)(pidPath))
|
|
727
|
+
if ((0, import_node_fs3.existsSync)(pidPath)) {
|
|
728
|
+
log("daemon PID file appeared", 0 /* DEBUG */);
|
|
729
|
+
return true;
|
|
730
|
+
}
|
|
731
|
+
iteration++;
|
|
732
|
+
if (iteration % 10 === 0) {
|
|
733
|
+
const elapsed2 = Date.now() - start;
|
|
734
|
+
log(
|
|
735
|
+
"still waiting for daemon: elapsed=" + elapsed2.toString() + "ms",
|
|
736
|
+
0 /* DEBUG */
|
|
737
|
+
);
|
|
738
|
+
}
|
|
582
739
|
await new Promise((r) => setTimeout(r, POLL_INTERVAL_MS));
|
|
583
740
|
}
|
|
741
|
+
const elapsed = Date.now() - start;
|
|
742
|
+
log(
|
|
743
|
+
"timed out waiting for daemon: elapsed=" + elapsed.toString() + "ms",
|
|
744
|
+
0 /* DEBUG */
|
|
745
|
+
);
|
|
584
746
|
return false;
|
|
585
747
|
}
|
|
586
748
|
|
|
@@ -613,19 +775,28 @@ function getIpcPath(ctx) {
|
|
|
613
775
|
async function createWorkerClient(options) {
|
|
614
776
|
const dbPath = options?.contextPath ?? resolveDbPath(options?.dbPath);
|
|
615
777
|
const contextId = await computeContextId(dbPath);
|
|
778
|
+
log("create-worker-client: contextId=" + contextId, 0 /* DEBUG */);
|
|
616
779
|
const socketPath = getIpcPath(contextId);
|
|
617
780
|
let daemon = discoverDaemon(contextId);
|
|
781
|
+
if (daemon) {
|
|
782
|
+
log("daemon found: pid=" + daemon.pid.toString(), 0 /* DEBUG */);
|
|
783
|
+
} else {
|
|
784
|
+
log("no daemon found", 0 /* DEBUG */);
|
|
785
|
+
}
|
|
618
786
|
if (!daemon && options?.autoSpawn !== false) {
|
|
619
787
|
const runtime = detectRuntime();
|
|
788
|
+
log("auto-spawning daemon", 0 /* DEBUG */);
|
|
620
789
|
spawnDaemon(dbPath, runtime, options?.workerScript);
|
|
790
|
+
log("waiting for daemon after spawn", 0 /* DEBUG */);
|
|
621
791
|
await waitForDaemon(contextId, options?.connectTimeoutMs);
|
|
622
792
|
daemon = discoverDaemon(contextId);
|
|
623
793
|
}
|
|
624
794
|
if (!daemon) {
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
);
|
|
795
|
+
const msg = `kbdb worker daemon did not start: ${readCrashReason(contextId)}`;
|
|
796
|
+
log(msg, 3 /* ERROR */);
|
|
797
|
+
throw new Error(msg);
|
|
628
798
|
}
|
|
799
|
+
log("connecting to daemon", 0 /* DEBUG */);
|
|
629
800
|
const client = new WorkerClient(
|
|
630
801
|
socketPath,
|
|
631
802
|
contextId,
|
|
@@ -652,10 +823,15 @@ function readCrashReason(contextId) {
|
|
|
652
823
|
// src/shared/worker-client/functions/get-or-create-client.function.ts
|
|
653
824
|
var cache = /* @__PURE__ */ new Map();
|
|
654
825
|
async function getOrCreateClient(options) {
|
|
826
|
+
log("getOrCreateClient called", 0 /* DEBUG */);
|
|
655
827
|
const dbPath = options?.contextPath ?? resolveDbPath(options?.dbPath);
|
|
656
828
|
const cached = cache.get(dbPath);
|
|
657
|
-
if (cached?.isConnected)
|
|
829
|
+
if (cached?.isConnected) {
|
|
830
|
+
log("returning cached client for: " + dbPath, 0 /* DEBUG */);
|
|
831
|
+
return cached;
|
|
832
|
+
}
|
|
658
833
|
cache.delete(dbPath);
|
|
834
|
+
log("creating new client for: " + dbPath, 0 /* DEBUG */);
|
|
659
835
|
const client = await createWorkerClient(options);
|
|
660
836
|
cache.set(dbPath, client);
|
|
661
837
|
return client;
|