@sqliteai/sqlite-js
Version:
SQLite JS extension for Node.js - Custom SQLite functions in JavaScript
162 lines (155 loc) • 4.82 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var path = require('path');
var fs = require('fs');
var os = require('os');
var child_process = require('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 (os.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 (fs.existsSync(file)) {
return true;
}
}
try {
const lddVersion = child_process.execSync("ldd --version 2>&1", {
encoding: "utf-8",
stdio: ["pipe", "pipe", "pipe"]
});
if (lddVersion.includes("musl")) {
return true;
}
} catch {
}
try {
if (fs.existsSync("/etc/os-release")) {
const osRelease = fs.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 = os.platform();
const archName = os.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 = os.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 (fs.existsSync(platformPackage.path)) {
return platformPackage.path;
}
}
} catch (error) {
}
return null;
}
function getExtensionPath() {
const platformPath = tryLoadPlatformPackage();
if (platformPath) {
return path.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
};
exports.ExtensionNotFoundError = ExtensionNotFoundError;
exports.default = index_default;
exports.getBinaryName = getBinaryName;
exports.getCurrentPlatform = getCurrentPlatform;
exports.getExtensionInfo = getExtensionInfo;
exports.getExtensionPath = getExtensionPath;
exports.getPlatformPackageName = getPlatformPackageName;
exports.isMusl = isMusl;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map