@sqliteai/sqlite-js
Version:
SQLite JS extension for Node.js - Custom SQLite functions in JavaScript
151 lines (146 loc) • 4.55 kB
JavaScript
import { resolve } from 'path';
import { existsSync, readFileSync } from 'fs';
import { platform, arch } from 'os';
import { execSync } from 'child_process';
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
if (typeof require !== "undefined") return require.apply(this, arguments);
throw Error('Dynamic require of "' + x + '" is not supported');
});
var PLATFORM_EXTENSIONS = {
darwin: ".dylib",
linux: ".so",
win32: ".dll"
};
function isMusl() {
var _a, _b, _c;
if (platform() !== "linux") {
return false;
}
const muslFiles = [
"/lib/ld-musl-x86_64.so.1",
"/lib/ld-musl-aarch64.so.1",
"/lib/ld-musl-armhf.so.1"
];
for (const file of muslFiles) {
if (existsSync(file)) {
return true;
}
}
try {
const lddVersion = execSync("ldd --version 2>&1", {
encoding: "utf-8",
stdio: ["pipe", "pipe", "pipe"]
});
if (lddVersion.includes("musl")) {
return true;
}
} catch {
}
try {
if (existsSync("/etc/os-release")) {
const osRelease = readFileSync("/etc/os-release", "utf-8");
if (osRelease.includes("Alpine") || osRelease.includes("musl")) {
return true;
}
}
} catch {
}
try {
const report = (_b = (_a = process.report) == null ? void 0 : _a.getReport) == null ? void 0 : _b.call(_a);
if (((_c = report == null ? void 0 : report.header) == null ? void 0 : _c.glibcVersionRuntime) === "") {
return true;
}
} catch {
}
return false;
}
function getCurrentPlatform() {
const platformName = platform();
const archName = arch();
if (platformName === "darwin") {
if (archName === "arm64") return "darwin-arm64";
if (archName === "x64" || archName === "ia32") return "darwin-x86_64";
}
if (platformName === "linux") {
const muslSuffix = isMusl() ? "-musl" : "";
if (archName === "arm64") {
return `linux-arm64${muslSuffix}`;
}
if (archName === "x64" || archName === "ia32") {
return `linux-x86_64${muslSuffix}`;
}
}
if (platformName === "win32") {
if (archName === "x64" || archName === "ia32") return "win32-x86_64";
}
throw new Error(
`Unsupported platform: ${platformName}-${archName}. Supported platforms: darwin-arm64, darwin-x86_64, linux-arm64, linux-x86_64, win32-x86_64 (with glibc or musl support for Linux)`
);
}
function getPlatformPackageName() {
const currentPlatform = getCurrentPlatform();
return `@sqliteai/sqlite-js-${currentPlatform}`;
}
function getBinaryName() {
const platformName = platform();
const extension = PLATFORM_EXTENSIONS[platformName];
if (!extension) {
throw new Error(`Unknown platform: ${platformName}`);
}
return `js${extension}`;
}
// src/index.ts
var ExtensionNotFoundError = class extends Error {
constructor(message) {
super(message);
this.name = "ExtensionNotFoundError";
}
};
function tryLoadPlatformPackage() {
try {
const packageName = getPlatformPackageName();
const platformPackage = __require(packageName);
if ((platformPackage == null ? void 0 : platformPackage.path) && typeof platformPackage.path === "string") {
if (existsSync(platformPackage.path)) {
return platformPackage.path;
}
}
} catch (error) {
}
return null;
}
function getExtensionPath() {
const platformPath = tryLoadPlatformPackage();
if (platformPath) {
return resolve(platformPath);
}
const currentPlatform = getCurrentPlatform();
const packageName = getPlatformPackageName();
throw new ExtensionNotFoundError(
`SQLite JS extension not found for platform: ${currentPlatform}
The platform-specific package "${packageName}" is not installed.
This usually happens when:
1. Your platform is not supported
2. npm failed to install optional dependencies
3. You're installing with --no-optional flag
Try running: npm install --force`
);
}
function getExtensionInfo() {
return {
platform: getCurrentPlatform(),
packageName: getPlatformPackageName(),
binaryName: getBinaryName(),
path: getExtensionPath()
};
}
var index_default = {
getExtensionPath,
getExtensionInfo,
ExtensionNotFoundError
};
export { ExtensionNotFoundError, index_default as default, getBinaryName, getCurrentPlatform, getExtensionInfo, getExtensionPath, getPlatformPackageName, isMusl };
//# sourceMappingURL=index.mjs.map
//# sourceMappingURL=index.mjs.map