Repository navigation
Expand file tree
/
Copy pathnode-sqlite.ts
More file actions
36 lines (33 loc) · 1.43 KB
/
Copy pathnode-sqlite.ts
File metadata and controls
36 lines (33 loc) · 1.43 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
/**
* Node's built-in `node:sqlite`, loaded in one place, so that a runtime
* without it fails with the one explanation that fits it: CodeGraph needs
* Node.js 22.5+, or its own bundled runtime. Any other failure to open a
* database is SQLite's own, and is reported as such (see ./open-error.ts).
*/
/** The parts of `node:sqlite` CodeGraph uses. */
export interface NodeSqliteModule {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
DatabaseSync: new (path: string, options?: { readOnly?: boolean }) => any;
}
/** `node:sqlite` is missing or could not be loaded in this Node.js runtime. */
export class NodeSqliteUnavailableError extends Error {
constructor(cause: unknown) {
const reason = cause instanceof Error ? cause.message : String(cause);
super(
`Node.js ${process.version} could not load its built-in SQLite module (${reason}).\n` +
'CodeGraph requires node:sqlite (Node.js 22.5+). Install the self-contained CodeGraph ' +
'release (it bundles a compatible Node), or run on Node 22.5+.',
{ cause }
);
this.name = 'NodeSqliteUnavailableError';
}
}
/** Load `node:sqlite`, or throw {@link NodeSqliteUnavailableError}. */
export function loadNodeSqlite(): NodeSqliteModule {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require('node:sqlite') as NodeSqliteModule;
} catch (err) {
throw new NodeSqliteUnavailableError(err);
}
}