// Loads the native module, mirroring @harperfast/rocksdb-js's model: a platform-specific // optionalDependency package when one exists for this platform (linux bindings are split by // libc), else a locally built artifact (`npm run build`, requires a Rust toolchain). 'use strict'; const { existsSync } = require('node:fs'); const { join } = require('node:path'); function libcSuffix() { if (process.platform !== 'linux') return ''; let isMusl = false; try { const { glibcVersionRuntime } = process.report?.getReport?.()?.header ?? {}; isMusl = !glibcVersionRuntime; } catch { // fall through to ldd probing } if (!isMusl) return '-glibc'; try { const { execSync } = require('node:child_process'); isMusl = execSync('ldd --version', { encoding: 'utf8', stdio: 'pipe' }).includes('musl'); } catch { // ldd may not exist; keep the report-based verdict } return isMusl ? '-musl' : '-glibc'; } const failures = []; const local = join(__dirname, 'hnsw-plane.node'); function loadLocal() { if (!existsSync(local)) return undefined; try { return require(local); } catch (error) { failures.push(`${local}: ${error.message}`); } } // A published platform package normally wins, because a consumer installing this package // wants the prebuild. HNSW_PREFER_LOCAL_BUILD inverts that for a checkout testing its own // build: without it, `npm install` in this repo installs the prebuild and every later // require gets the RELEASED binding rather than what the tree just compiled. let native0; if (process.env.HNSW_PREFER_LOCAL_BUILD) { native0 = loadLocal(); // no fallback: falling through to the prebuild is exactly the false pass the caller set // this variable to avoid, and it would look like a working local build if (!native0) { throw new Error( `HNSW_PREFER_LOCAL_BUILD is set but the local build could not be loaded. ${failures.join('; ') || `${local} does not exist; run \`node build.mjs\``}` ); } } if (!native0) { try { native0 = require(`@harperfast/hnsw-${process.platform}-${process.arch}${libcSuffix()}`); } catch (error) { failures.push(`platform package: ${error.message}`); } } if (!native0) { native0 = loadLocal(); } if (!native0) { throw new Error( `@harperfast/hnsw could not load a native binding for ${process.platform}-${process.arch}. ` + 'Build one with `npm run build` in ' + __dirname + ' (requires a Rust toolchain: https://rustup.rs).' + (failures.length ? ` Load failures: ${failures.join('; ')}` : '') ); } const native = native0; // A predicate that throws would surface through the ThreadsafeFunction as a fatal exception // (aborting the process); wrap it so a throw denies the batch and rejects the search instead. const nativeSearchWithPredicate = native.Plane.prototype.searchWithPredicate; native.Plane.prototype.searchWithPredicate = function (vector, k, ef, predicate, ...rest) { let predicateError; const guarded = (ids, keys, keyEnds) => { if (predicateError !== undefined) return new Uint8Array(ids.length); try { const verdicts = predicate(ids, keys, keyEnds); return verdicts instanceof Uint8Array ? verdicts : Uint8Array.from(verdicts ?? []); } catch (error) { predicateError = error; return new Uint8Array(ids.length); } }; return nativeSearchWithPredicate.call(this, vector, k, ef, guarded, ...rest).then((hits) => { if (predicateError !== undefined) throw predicateError; return hits; }); }; // The native side reports a plane fault as `failure` on a resolved result so the ids of the // records that landed survive the crossing; the JS contract is a rejection that still carries them. const nativeInsertBatch = native.Plane.prototype.insertBatch; native.Plane.prototype.insertBatch = async function (...args) { const { ids, rejected, failure } = await nativeInsertBatch.apply(this, args); if (failure === undefined || failure === null) return { ids, rejected }; const error = new Error(`${failure.reason} (batch record ${failure.index}; see error.ids for the records that landed)`); error.code = failure.code; error.index = failure.index; error.ids = ids; error.rejected = rejected; throw error; }; module.exports = native;