-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (52 loc) · 2.12 KB
/
index.js
File metadata and controls
63 lines (52 loc) · 2.12 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
import { getSQLiteLibPath } from './build-macros.ts' with { type: 'macro' };
import { fileURLToPath } from 'node:url';
import { dirname, resolve } from 'node:path';
import { existsSync } from 'node:fs';
const __dirname = dirname(fileURLToPath(import.meta.url));
// Get the library path at build time using macro
const SQLITE_LIB_PATH = getSQLiteLibPath();
/**
* Get the absolute path to the bundled libsqlite3 shared library
* @returns {string} Absolute path to libsqlite3.dylib/.so
*/
export function getLibraryPath() {
// Detect platform and architecture
const platform = process.platform === 'darwin' ? 'darwin' : 'linux';
const arch = process.arch === 'arm64' ? 'aarch64' : 'x86_64';
const ext = platform === 'darwin' ? 'dylib' : 'so';
const libDir = resolve(__dirname, 'lib');
// Try platform-specific library first (highest priority)
const specificLib = resolve(libDir, `libsqlite3-${platform}-${arch}.${ext}`);
if (existsSync(specificLib)) {
return specificLib;
}
// If we have a build-time path and it exists, use it as fallback
if (SQLITE_LIB_PATH && existsSync(SQLITE_LIB_PATH)) {
return SQLITE_LIB_PATH;
}
// Fallback to generic libraries (for development/backwards compatibility)
const fallbackCandidates = [
resolve(libDir, `libsqlite3.${ext}`), // Generic for platform
resolve(libDir, 'libsqlite3.dylib'), // macOS generic
resolve(libDir, 'libsqlite3.so'), // Linux generic
resolve(libDir, 'libsqlite3.0.dylib'), // macOS versioned
];
for (const candidate of fallbackCandidates) {
if (existsSync(candidate)) {
return candidate;
}
}
// Helpful error message
const expectedLib = `libsqlite3-${platform}-${arch}.${ext}`;
throw new Error(
`SQLite library not found for ${platform}/${arch}. ` +
`Expected: ${expectedLib}. ` +
`Run 'npm run bundle-lib' to build platform-specific libraries.`
);
}
/**
* Hip alias for getLibraryPath() - for use with Database.setCustomSQLite()
* @returns {string} Absolute path to libsqlite3.dylib/.so
*/
export const pathToSQLite = getLibraryPath();
export default getLibraryPath;