ruvector/npm/core/src/index.cjs.ts
rUv 47dd1a7539 fix: Add CommonJS build and remove WASM dependency
🔧 @ruvector/core@0.1.5
- Added CommonJS build (index.cjs.js) for require() compatibility
- Created dual build system (ESM + CJS)
- Fixed package.json exports to point to correct CJS file

🔧 ruvector@0.1.11
- Updated to @ruvector/core@^0.1.5
- Removed @ruvector/wasm optional dependency (not yet buildable)
- Improved error messages for unsupported platforms

Fixes #13 - Missing CommonJS build issue
2025-11-25 18:05:36 +00:00

99 lines
2.7 KiB
TypeScript

/**
* @ruvector/core - CommonJS wrapper
*
* This file provides CommonJS compatibility for projects using require()
*/
import { platform, arch } from 'node:os';
// Platform detection types
type Platform = 'linux' | 'darwin' | 'win32';
type Architecture = 'x64' | 'arm64';
/**
* Distance metric for similarity calculation
*/
export enum DistanceMetric {
/** Euclidean (L2) distance */
Euclidean = 'euclidean',
/** Cosine similarity (1 - cosine distance) */
Cosine = 'cosine',
/** Dot product similarity */
DotProduct = 'dot',
}
/**
* Get platform-specific package name
*/
function getPlatformPackage(): string {
const plat = platform() as Platform;
const architecture = arch() as Architecture;
// Map Node.js platform names to package names
const packageMap: Record<string, string> = {
'linux-x64': 'ruvector-core-linux-x64-gnu',
'linux-arm64': 'ruvector-core-linux-arm64-gnu',
'darwin-x64': 'ruvector-core-darwin-x64',
'darwin-arm64': 'ruvector-core-darwin-arm64',
'win32-x64': 'ruvector-core-win32-x64-msvc',
};
const key = `${plat}-${architecture}`;
const packageName = packageMap[key];
if (!packageName) {
throw new Error(
`Unsupported platform: ${plat}-${architecture}. ` +
`Supported platforms: ${Object.keys(packageMap).join(', ')}`
);
}
return packageName;
}
/**
* Load the native binding for the current platform
*/
function loadNativeBinding() {
const packageName = getPlatformPackage();
try {
// Try to require the platform-specific package
return require(packageName);
} catch (error: any) {
// Fallback: try loading from local platforms directory
try {
const plat = platform() as Platform;
const architecture = arch() as Architecture;
const platformKey = `${plat}-${architecture}`;
const platformMap: Record<string, string> = {
'linux-x64': 'linux-x64-gnu',
'linux-arm64': 'linux-arm64-gnu',
'darwin-x64': 'darwin-x64',
'darwin-arm64': 'darwin-arm64',
'win32-x64': 'win32-x64-msvc',
};
const localPath = `../platforms/${platformMap[platformKey]}/ruvector.node`;
return require(localPath);
} catch (fallbackError: any) {
throw new Error(
`Failed to load native binding: ${error.message}\n` +
`Fallback also failed: ${fallbackError.message}\n` +
`Platform: ${platform()}-${arch()}\n` +
`Expected package: ${packageName}`
);
}
}
}
// Load the native module
const nativeBinding = loadNativeBinding();
// Export everything from the native binding
module.exports = nativeBinding;
// Also export as default
module.exports.default = nativeBinding;
// Re-export DistanceMetric
module.exports.DistanceMetric = DistanceMetric;