@sqliteai/sqlite-vector
Version:
SQLite vector search extension for Node.js - Cross-platform vector embeddings and similarity search
1 lines • 11.4 kB
Source Map (JSON)
{"version":3,"sources":["../src/platform.ts","../src/index.ts"],"names":["existsSync"],"mappings":";;;;;;;;;;;AAmBO,IAAM,mBAAA,GAA8C;AAAA,EACzD,MAAA,EAAQ,QAAA;AAAA,EACR,KAAA,EAAO,KAAA;AAAA,EACP,KAAA,EAAO;AACT,CAAA;AAMO,SAAS,MAAA,GAAkB;AA7BlC,EAAA,IAAA,EAAA,EAAA,EAAA,EAAA,EAAA;AA+BE,EAAA,IAAI,QAAA,OAAe,OAAA,EAAS;AAC1B,IAAA,OAAO,KAAA;AAAA,EACT;AAGA,EAAA,MAAM,SAAA,GAAY;AAAA,IAChB,0BAAA;AAAA,IACA,2BAAA;AAAA,IACA;AAAA,GACF;AAEA,EAAA,KAAA,MAAW,QAAQ,SAAA,EAAW;AAC5B,IAAA,IAAI,UAAA,CAAW,IAAI,CAAA,EAAG;AACpB,MAAA,OAAO,IAAA;AAAA,IACT;AAAA,EACF;AAGA,EAAA,IAAI;AACF,IAAA,MAAM,UAAA,GAAa,SAAS,oBAAA,EAAsB;AAAA,MAChD,QAAA,EAAU,OAAA;AAAA,MACV,KAAA,EAAO,CAAC,MAAA,EAAQ,MAAA,EAAQ,MAAM;AAAA,KAC/B,CAAA;AAED,IAAA,IAAI,UAAA,CAAW,QAAA,CAAS,MAAM,CAAA,EAAG;AAC/B,MAAA,OAAO,IAAA;AAAA,IACT;AAAA,EACF,CAAA,CAAA,MAAQ;AAAA,EAER;AAGA,EAAA,IAAI;AACF,IAAA,IAAI,UAAA,CAAW,iBAAiB,CAAA,EAAG;AACjC,MAAA,MAAM,SAAA,GAAY,YAAA,CAAa,iBAAA,EAAmB,OAAO,CAAA;AACzD,MAAA,IAAI,UAAU,QAAA,CAAS,QAAQ,KAAK,SAAA,CAAU,QAAA,CAAS,MAAM,CAAA,EAAG;AAC9D,QAAA,OAAO,IAAA;AAAA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AAA,OAAO,IAAA;AACT;AAgBO,SAAS,gBAAA,GAA2B;AAEzC,EAAA,MAAM,eAAe,sBAAA,EAAuB;AAC5C,EAAA,IAAI,YAAA,EAAc;AAChB,IAAA,OAAO,QAAQ,YAAY,CAAA;AAAA,EAC7B;AAGA,EAAA,MAAM,kBAAkB,kBAAA,EAAmB;AAC3C,EAAA,MAAM,cAAc,sBAAA,EAAuB;AAE3C,EAAA,MAAM,IAAI,sBAAA;AAAA,IACR,mDAAmD,eAAe;;AAAA,+BAAA,EAChC,WAAW,CAAA;AAAA;AAAA;AAAA;AAAA;;AAAA,gCAAA;AAAA,GAM/C;AACF;AAmCO,SAAS,gBAAA,GAAkC;AAChD,EAAA,OAAO;AAAA,IACL,UAAU,kBAAA,EAAmB;AAAA,IAC7B,aAAa,sBAAA,EAAuB;AAAA,IACpC,YAAY,aAAA,EAAc;AAAA,IAC1B,MAAM,gBAAA;AAAiB,GACzB;AACF;AAGA,IAAO,aAAA,GAAQ;AAAA,EACb,gBAAA;AAAA,EACA,gBAAA;AAAA,EACA;AACF","file":"index.mjs","sourcesContent":["import { platform, arch } from 'node:os';\nimport { existsSync, readFileSync } from 'node:fs';\nimport { execSync } from 'node:child_process';\n\n/**\n * Supported platform identifiers\n */\nexport type Platform =\n | 'darwin-arm64'\n | 'darwin-x86_64'\n | 'linux-arm64'\n | 'linux-arm64-musl'\n | 'linux-x86_64'\n | 'linux-x86_64-musl'\n | 'win32-x86_64';\n\n/**\n * Binary extension for each platform\n */\nexport const PLATFORM_EXTENSIONS: Record<string, string> = {\n darwin: '.dylib',\n linux: '.so',\n win32: '.dll',\n} as const;\n\n/**\n * Detects if the system uses musl libc (Alpine Linux, etc.)\n * Uses multiple detection strategies for reliability\n */\nexport function isMusl(): boolean {\n // Only relevant for Linux\n if (platform() !== 'linux') {\n return false;\n }\n\n // Strategy 1: Check for musl-specific files\n const muslFiles = [\n '/lib/ld-musl-x86_64.so.1',\n '/lib/ld-musl-aarch64.so.1',\n '/lib/ld-musl-armhf.so.1',\n ];\n\n for (const file of muslFiles) {\n if (existsSync(file)) {\n return true;\n }\n }\n\n // Strategy 2: Check ldd version output\n try {\n const lddVersion = execSync('ldd --version 2>&1', {\n encoding: 'utf-8',\n stdio: ['pipe', 'pipe', 'pipe'],\n });\n\n if (lddVersion.includes('musl')) {\n return true;\n }\n } catch {\n // ldd command failed, continue to next strategy\n }\n\n // Strategy 3: Check /etc/os-release for Alpine\n try {\n if (existsSync('/etc/os-release')) {\n const osRelease = readFileSync('/etc/os-release', 'utf-8');\n if (osRelease.includes('Alpine') || osRelease.includes('musl')) {\n return true;\n }\n }\n } catch {\n // File read failed, continue to next strategy\n }\n\n // Strategy 4: Check process.report.getReport() for musl\n try {\n const report = (process as any).report?.getReport?.();\n if (report?.header?.glibcVersionRuntime === '') {\n // Empty glibc version often indicates musl\n return true;\n }\n } catch {\n // Report not available\n }\n\n return false;\n}\n\n/**\n * Gets the current platform identifier\n * @throws {Error} If the platform is unsupported\n */\nexport function getCurrentPlatform(): Platform {\n const platformName = platform();\n const archName = arch();\n\n // macOS\n if (platformName === 'darwin') {\n if (archName === 'arm64') return 'darwin-arm64';\n if (archName === 'x64' || archName === 'ia32') return 'darwin-x86_64';\n }\n\n // Linux (with musl detection)\n if (platformName === 'linux') {\n const muslSuffix = isMusl() ? '-musl' : '';\n\n if (archName === 'arm64') {\n return `linux-arm64${muslSuffix}` as Platform;\n }\n if (archName === 'x64' || archName === 'ia32') {\n return `linux-x86_64${muslSuffix}` as Platform;\n }\n }\n\n // Windows\n if (platformName === 'win32') {\n if (archName === 'x64' || archName === 'ia32') return 'win32-x86_64';\n }\n\n // Unsupported platform\n throw new Error(\n `Unsupported platform: ${platformName}-${archName}. ` +\n `Supported platforms: darwin-arm64, darwin-x86_64, linux-arm64, linux-x86_64, win32-x86_64 ` +\n `(with glibc or musl support for Linux)`\n );\n}\n\n/**\n * Gets the package name for the current platform\n */\nexport function getPlatformPackageName(): string {\n const currentPlatform = getCurrentPlatform();\n return `@sqliteai/sqlite-vector-${currentPlatform}`;\n}\n\n/**\n * Gets the binary filename for the current platform\n */\nexport function getBinaryName(): string {\n const platformName = platform();\n const extension = PLATFORM_EXTENSIONS[platformName];\n\n if (!extension) {\n throw new Error(`Unknown platform: ${platformName}`);\n }\n\n return `vector${extension}`;\n}\n","import { resolve } from 'node:path';\nimport { existsSync } from 'node:fs';\nimport {\n getCurrentPlatform,\n getPlatformPackageName,\n getBinaryName,\n type Platform\n} from './platform.js';\n\n/**\n * Error thrown when the SQLite Vector extension cannot be found\n */\nexport class ExtensionNotFoundError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'ExtensionNotFoundError';\n }\n}\n\n/**\n * Attempts to load the platform-specific package\n * @returns The path to the extension binary, or null if not found\n */\nfunction tryLoadPlatformPackage(): string | null {\n try {\n const packageName = getPlatformPackageName();\n\n // Try to dynamically import the platform package\n // This works in both CommonJS and ESM\n const platformPackage = require(packageName);\n\n if (platformPackage?.path && typeof platformPackage.path === 'string') {\n if (existsSync(platformPackage.path)) {\n return platformPackage.path;\n }\n }\n } catch (error) {\n // Platform package not installed or failed to load\n // This is expected when optionalDependencies fail\n }\n\n return null;\n}\n\n/**\n * Gets the absolute path to the SQLite Vector extension binary for the current platform\n *\n * @returns Absolute path to the extension binary (.so, .dylib, or .dll)\n * @throws {ExtensionNotFoundError} If the extension binary cannot be found\n *\n * @example\n * ```typescript\n * import { getExtensionPath } from '@sqliteai/sqlite-vector';\n *\n * const extensionPath = getExtensionPath();\n * // On macOS ARM64: /path/to/node_modules/@sqliteai/sqlite-vector-darwin-arm64/vector.dylib\n * ```\n */\nexport function getExtensionPath(): string {\n // Try to load from platform-specific package\n const platformPath = tryLoadPlatformPackage();\n if (platformPath) {\n return resolve(platformPath);\n }\n\n // If we reach here, the platform package wasn't installed\n const currentPlatform = getCurrentPlatform();\n const packageName = getPlatformPackageName();\n\n throw new ExtensionNotFoundError(\n `SQLite Vector extension not found for platform: ${currentPlatform}\\n\\n` +\n `The platform-specific package \"${packageName}\" is not installed.\\n` +\n `This usually happens when:\\n` +\n ` 1. Your platform is not supported\\n` +\n ` 2. npm failed to install optional dependencies\\n` +\n ` 3. You're installing with --no-optional flag\\n\\n` +\n `Try running: npm install --force`\n );\n}\n\n/**\n * Information about the current platform and extension\n */\nexport interface ExtensionInfo {\n /** Current platform identifier (e.g., 'darwin-arm64') */\n platform: Platform;\n /** Name of the platform-specific npm package */\n packageName: string;\n /** Filename of the binary (e.g., 'vector.dylib') */\n binaryName: string;\n /** Full path to the extension binary */\n path: string;\n}\n\n/**\n * Gets detailed information about the SQLite Vector extension\n *\n * @returns Extension information object\n *\n * @example\n * ```typescript\n * import { getExtensionInfo } from '@sqliteai/sqlite-vector';\n *\n * const info = getExtensionInfo();\n * console.log(info);\n * // {\n * // platform: 'darwin-arm64',\n * // packageName: '@sqliteai/sqlite-vector-darwin-arm64',\n * // binaryName: 'vector.dylib',\n * // path: '/path/to/vector.dylib'\n * // }\n * ```\n */\nexport function getExtensionInfo(): ExtensionInfo {\n return {\n platform: getCurrentPlatform(),\n packageName: getPlatformPackageName(),\n binaryName: getBinaryName(),\n path: getExtensionPath(),\n };\n}\n\n// Default export for CommonJS compatibility\nexport default {\n getExtensionPath,\n getExtensionInfo,\n ExtensionNotFoundError,\n};\n\n// Re-export platform utilities\nexport { getCurrentPlatform, getPlatformPackageName, getBinaryName, isMusl } from './platform.js';\nexport type { Platform } from './platform.js';\n"]}