Repository navigation
Expand file tree
/
Copy pathsqlite.js
More file actions
439 lines (391 loc) · 14.9 KB
/
Copy pathsqlite.js
File metadata and controls
439 lines (391 loc) · 14.9 KB
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
/**
* @module sqlite
* @description Zero-native-addon SQLite wrapper (`node:sqlite` `DatabaseSync`),
* injection-safe FTS5 query tokenizer/escaper, and prototype-safe Reciprocal Rank
* Fusion (`fuseRrfResults`) Lego bricks (`INV-LEGO-12`, `INV-REC-01..05`).
*/
import { DatabaseSync } from 'node:sqlite';
import fs from 'node:fs';
import { dirname } from 'node:path';
import { okEnvelope, errorEnvelope } from './http.js';
import { isSqliteCorruptionError } from './db.js';
export { DatabaseSync, isSqliteCorruptionError };
const VALID_SYNCHRONOUS_MODES = new Set(['OFF', 'NORMAL', 'FULL', 'EXTRA', '0', '1', '2', '3']);
const MAX_FTS5_RAW_QUERY_CHARS = 2048;
const FTS5_TOKEN_PATTERN = /[\p{L}\p{N}_]+/gu;
function resolveSynchronousMode(value, fallback) {
if (value === undefined || value === null || value === '') {
return fallback;
}
if (typeof value !== 'string' && typeof value !== 'number') {
throw new RangeError(`Invalid SQLite synchronous mode: "${String(value)}"`);
}
const normalized = String(value).trim().toUpperCase();
if (!VALID_SYNCHRONOUS_MODES.has(normalized)) {
throw new RangeError(`Invalid SQLite synchronous mode: "${value}"`);
}
return normalized;
}
function configurePragmas(db, { wal, busyTimeout, synchronous, walAutocheckpoint, readOnly }) {
const defaultSync = wal !== false ? 'NORMAL' : 'FULL';
const syncMode = resolveSynchronousMode(synchronous, defaultSync);
const safeBusyTimeout =
Number.isFinite(busyTimeout) && busyTimeout >= 0 ? Math.trunc(busyTimeout) : 5000;
db.exec(`PRAGMA busy_timeout = ${safeBusyTimeout};`);
if (!readOnly) {
if (wal !== false) {
const safeAutocheckpoint =
Number.isFinite(walAutocheckpoint) && walAutocheckpoint >= 0
? Math.trunc(walAutocheckpoint)
: 1000;
db.exec('PRAGMA journal_mode = WAL;');
db.exec(`PRAGMA synchronous = ${syncMode};`);
db.exec(`PRAGMA wal_autocheckpoint = ${safeAutocheckpoint};`);
} else {
db.exec(`PRAGMA synchronous = ${syncMode};`);
}
}
db.exec('PRAGMA foreign_keys = ON;');
}
function normalizeParams(params) {
if (params === undefined) return [];
return Array.isArray(params) ? params : [params];
}
function normalizeRow(row) {
return row && typeof row === 'object' ? { ...row } : row;
}
function silenceUnhandledRejection(thenable) {
void (async () => {
try {
await thenable;
} catch {
// Ignored: synchronous TypeError is thrown immediately by transaction().
}
})();
}
function rollbackTransaction(db, depth, primaryErr) {
try {
if (depth === 0) {
db.exec('ROLLBACK');
} else {
db.exec(`ROLLBACK TO SAVEPOINT sp_${depth}; RELEASE SAVEPOINT sp_${depth}`);
}
} catch (rbErr) {
if (primaryErr && typeof primaryErr === 'object' && Object.isExtensible(primaryErr)) {
primaryErr.rollbackError = rbErr;
}
}
}
/**
* Open a Node 22 `node:sqlite` (`DatabaseSync`) connection configured with crash-safe
* WAL durability pragmas (`INV-REC-01..05`) and automatic descriptor cleanup on init failure.
*
* @param {string} [dbPath=':memory:'] - SQLite database file path or `':memory:'`
* @param {object} [options={}] - Connection & durability options
* @param {boolean} [options.wal=true] - Enable WAL journal mode when writable
* @param {number} [options.busyTimeout=5000] - Busy lock timeout in milliseconds
* @param {string|number} [options.synchronous] - Synchronous level (`'NORMAL'` for WAL, `'FULL'` otherwise)
* @param {number} [options.walAutocheckpoint=1000] - WAL autocheckpoint threshold in pages
* @param {boolean} [options.readOnly=false] - Open database in read-only mode
* @param {Function|null} [options.DatabaseClass=null] - Constructor override for fault-injection testing
* @returns {{
* db: DatabaseSync,
* dbPath: string,
* isOpen: () => boolean,
* exec: (sql: string) => void,
* run: (sql: string, params?: unknown) => { changes: number, lastInsertRowid: number },
* query: (sql: string, params?: unknown) => Array<Record<string, unknown>>,
* queryOne: (sql: string, params?: unknown) => Record<string, unknown> | null,
* transaction: <T>(fn: () => T) => T,
* healthCheck: () => object,
* close: () => void
* }}
*/
export function openSqliteDb(dbPath = ':memory:', options = {}) {
if (typeof dbPath !== 'string' || dbPath.trim().length === 0) {
throw new TypeError('openSqliteDb requires a non-empty string dbPath');
}
const {
wal = true,
busyTimeout = 5000,
synchronous,
walAutocheckpoint = 1000,
readOnly = false,
DatabaseClass = null,
} = options && typeof options === 'object' ? options : {};
if (dbPath !== ':memory:' && !dbPath.startsWith('file:') && !readOnly) {
fs.mkdirSync(dirname(dbPath), { recursive: true });
}
const DbCtor = DatabaseClass || DatabaseSync;
const db = readOnly ? new DbCtor(dbPath, { readOnly: true }) : new DbCtor(dbPath);
try {
configurePragmas(db, { wal, busyTimeout, synchronous, walAutocheckpoint, readOnly });
} catch (err) {
try {
db.close();
} catch (closeErr) {
if (err && typeof err === 'object' && Object.isExtensible(err)) {
err.cleanupErrors = [closeErr];
}
}
throw err;
}
let closed = false;
let txDepth = 0;
function assertOpen() {
if (closed) {
const closedErr = new Error('SQLite connection is closed');
closedErr.code = 'SQLITE_CLOSED';
throw closedErr;
}
}
function exec(sql) {
assertOpen();
db.exec(sql);
}
function run(sql, params = []) {
assertOpen();
const stmt = db.prepare(sql);
const res = stmt.run(...normalizeParams(params));
return {
changes: Number(res?.changes ?? 0),
lastInsertRowid: Number(res?.lastInsertRowid ?? 0),
};
}
function query(sql, params = []) {
assertOpen();
const rows = db.prepare(sql).all(...normalizeParams(params));
return Array.isArray(rows) ? rows.map(normalizeRow) : [];
}
function queryOne(sql, params = []) {
assertOpen();
const row = db.prepare(sql).get(...normalizeParams(params));
return row && typeof row === 'object' ? { ...row } : null;
}
function transaction(fn) {
if (typeof fn !== 'function') {
throw new TypeError('SQLite transaction requires a synchronous callback function');
}
assertOpen();
const depth = txDepth;
if (depth === 0) {
db.exec('BEGIN IMMEDIATE');
} else {
db.exec(`SAVEPOINT sp_${depth}`);
}
txDepth = depth + 1;
try {
const out = fn();
if (
(typeof out === 'object' || typeof out === 'function') &&
out !== null &&
typeof out.then === 'function'
) {
silenceUnhandledRejection(out);
throw new TypeError('SQLite transaction callback must be synchronous');
}
if (depth === 0) {
db.exec('COMMIT');
} else {
db.exec(`RELEASE SAVEPOINT sp_${depth}`);
}
return out;
} catch (err) {
rollbackTransaction(db, depth, err);
throw err;
} finally {
txDepth = depth;
}
}
function healthCheck() {
if (closed) {
return errorEnvelope(
'SQLITE_CLOSED',
'SQLite connection is closed',
{ dbPath },
'sqlite.health'
);
}
try {
const row = queryOne('SELECT 1 AS ok');
if (!row || Number(row.ok) !== 1) {
return errorEnvelope(
'SQLITE_UNHEALTHY',
'Unexpected SQLite health probe response',
{ dbPath, isCorruption: false },
'sqlite.health'
);
}
return okEnvelope('sqlite.health', 'SQLite connection healthy', {
dbPath,
ok: true,
});
} catch (err) {
const message =
err && typeof err === 'object' && typeof err.message === 'string' && err.message
? err.message
: String(err);
return errorEnvelope(
'SQLITE_UNHEALTHY',
message,
{ dbPath, isCorruption: isSqliteCorruptionError(err) },
'sqlite.health'
);
}
}
function close() {
if (closed) return;
closed = true;
db.close();
}
return {
db,
dbPath,
isOpen: () => !closed,
exec,
run,
query,
queryOne,
transaction,
healthCheck,
close,
};
}
/**
* Sanitize untrusted free-text user input into a deterministic SQLite FTS5 `MATCH` expression
* that cannot throw `fts5: syntax error` on boolean keywords, colons, operators, or unclosed quotes.
*
* @param {string} [rawQuery=''] - Raw user search query
* @param {object} [options={}] - Escaping options
* @param {'AND'|'OR'} [options.operator='AND'] - Boolean join operator (`'AND'` or `'OR'`)
* @param {boolean} [options.prefixLastToken=false] - Append `*` to the final quoted token for autocomplete
* @param {number} [options.maxTokens=32] - Maximum extracted tokens
* @returns {string} Escaped FTS5 query string, or `''` when no alphanumeric tokens exist
*/
export function escapeFts5Query(rawQuery = '', options = {}) {
const {
operator = 'AND',
prefixLastToken = false,
maxTokens = 32,
} = options && typeof options === 'object' ? options : {};
const normalizedOp = typeof operator === 'string' ? operator.trim().toUpperCase() : '';
if (normalizedOp !== 'AND' && normalizedOp !== 'OR') {
throw new RangeError(
`Invalid FTS5 operator "${String(operator)}": expected "AND" or "OR"`
);
}
const boundedInput = String(rawQuery ?? '').slice(0, MAX_FTS5_RAW_QUERY_CHARS);
const safeMaxTokens = Number.isFinite(maxTokens)
? Math.max(1, Math.trunc(maxTokens || 32))
: 32;
const matches = boundedInput.match(FTS5_TOKEN_PATTERN);
if (!matches || matches.length === 0) {
return '';
}
const tokens = matches.slice(0, safeMaxTokens);
const quotedTokens = tokens.map((token, index) => {
const quoted = `"${token.replace(/"/g, '""')}"`;
if (prefixLastToken === true && index === tokens.length - 1) {
return `${quoted}*`;
}
return quoted;
});
return quotedTokens.join(` ${normalizedOp} `);
}
function assignSafeProperty(target, key, value) {
if (key === '__proto__') {
Object.defineProperty(target, '__proto__', {
value,
enumerable: true,
configurable: true,
writable: true,
});
} else {
target[key] = value;
}
}
function mergePrototypeSafe(lowerPriority, higherPriority, idKey) {
const merged = {};
for (const source of [lowerPriority, higherPriority]) {
for (const [key, value] of Object.entries(source)) {
if (key === '__proto__' && idKey !== '__proto__') continue;
assignSafeProperty(merged, key, value);
}
}
return merged;
}
/**
* Fuse multiple ranked retrieval lists (e.g., FTS5 BM25 + dense pgvector/cosine results)
* using weighted Reciprocal Rank Fusion: `sum_i weight_i / (k + rank_i)` (`1`-indexed `rank`).
*
* @param {Array<Array<Record<string, unknown>>>} [rankedLists=[]] - Ordered arrays of candidate objects
* @param {object} [options={}] - RRF fusion options
* @param {number} [options.k=60] - Smoothing constant (`>= 1`)
* @param {string} [options.idKey='id'] - Document identifier property name
* @param {number} [options.limit=20] - Maximum fused items returned
* @param {number[]} [options.weights=[]] - Optional positive per-list weights (defaults to `1` per list)
* @returns {Array<Record<string, unknown> & { rrfScore: number }>} Sorted fused items
*/
export function fuseRrfResults(rankedLists = [], options = {}) {
const {
k = 60,
idKey = 'id',
limit = 20,
weights = [],
} = options && typeof options === 'object' ? options : {};
const safeK = Number.isFinite(k) && k >= 1 ? k : 60;
const safeLimit = Number.isFinite(limit) && limit >= 0 ? Math.trunc(limit) : 20;
if (safeLimit === 0 || !Array.isArray(rankedLists) || rankedLists.length === 0) {
return [];
}
const safeIdKey = typeof idKey === 'string' && idKey.length > 0 ? idKey : 'id';
const fusedMap = new Map();
for (let listIdx = 0; listIdx < rankedLists.length; listIdx += 1) {
const list = rankedLists[listIdx];
if (!Array.isArray(list)) continue;
const rawWeight = weights?.[listIdx];
const weight = Number.isFinite(rawWeight) && rawWeight > 0 ? Number(rawWeight) : 1;
const seenInList = new Set();
for (let itemIdx = 0; itemIdx < list.length; itemIdx += 1) {
const item = list[itemIdx];
if (!item || typeof item !== 'object' || Array.isArray(item)) continue;
if (!Object.hasOwn(item, safeIdKey)) continue;
const rawId = item[safeIdKey];
if (rawId === null || rawId === undefined) continue;
const docId = String(rawId);
if (docId.trim().length === 0) continue;
if (seenInList.has(docId)) continue;
seenInList.add(docId);
const rank = itemIdx + 1;
const contribution = weight / (safeK + rank);
const existing = fusedMap.get(docId);
if (!existing) {
fusedMap.set(docId, {
score: contribution,
bestRank: rank,
merged: mergePrototypeSafe({}, item, safeIdKey),
});
} else {
existing.score += contribution;
if (rank < existing.bestRank) {
existing.merged = mergePrototypeSafe(existing.merged, item, safeIdKey);
existing.bestRank = rank;
} else {
existing.merged = mergePrototypeSafe(item, existing.merged, safeIdKey);
}
}
}
}
const fusedItems = [];
for (const entry of fusedMap.values()) {
const record = mergePrototypeSafe({}, entry.merged, safeIdKey);
record.rrfScore = Number(entry.score.toFixed(10));
fusedItems.push(record);
}
fusedItems.sort(
(a, b) =>
b.rrfScore - a.rrfScore ||
String(a[safeIdKey]).localeCompare(String(b[safeIdKey]))
);
return fusedItems.slice(0, safeLimit);
}