closevector-hnswlib-wasm
Version:
typescript and wasm bindings for Hnswlib
126 lines (121 loc) • 4.19 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const defaultParams = {
/**
* Default parameters for the HNSW index.
* @param {number} m The maximum number of outgoing connections on the graph (default: 16).
* @param {number} efConstruction The parameter that controls speed/accuracy trade-off during the index construction (default: 200).
* @param {number} randomSeed The seed value of random number generator (default: 100).
*/
initIndex: [32, 128, 100]
};
const hnswParamsForAda = {
m: 32,
efSearch: 128,
efConstruction: 128,
numNeighbors: 8,
dimensions: 1538
};
const IDBFS_STORE_NAME = "FILE_DATA";
let library;
const syncFileSystem = (action) => {
const EmscriptenFileSystemManager = library.EmscriptenFileSystemManager;
const syncAction = action === "read" ? true : action === "write" ? false : void 0;
if (syncAction === void 0)
throw new Error("Invalid action type");
return new Promise((resolve2, reject) => {
try {
EmscriptenFileSystemManager.syncFS(syncAction, () => {
resolve2();
});
} catch (error) {
reject(error);
}
});
};
const waitForFileSystemInitalized = () => {
const EmscriptenFileSystemManager = library.EmscriptenFileSystemManager;
return new Promise((resolve2, reject) => {
let totalWaitTime = 0;
const checkInterval = 100;
const maxWaitTime = 4e3;
const checkInitialization = () => {
if (EmscriptenFileSystemManager.isInitialized()) {
resolve2();
} else if (totalWaitTime >= maxWaitTime) {
reject(new Error("Failed to initialize filesystem"));
} else {
totalWaitTime += checkInterval;
setTimeout(checkInitialization, checkInterval);
}
};
setTimeout(checkInitialization, checkInterval);
});
};
const waitForFileSystemSynced = () => {
const EmscriptenFileSystemManager = library.EmscriptenFileSystemManager;
return new Promise((resolve2, reject) => {
let totalWaitTime = 0;
const checkInterval = 100;
const maxWaitTime = 4e3;
const checkInitialization = () => {
if (EmscriptenFileSystemManager.isSynced()) {
resolve2();
} else if (totalWaitTime >= maxWaitTime) {
reject(new Error("Failed to initialize filesystem"));
} else {
totalWaitTime += checkInterval;
setTimeout(checkInitialization, checkInterval);
}
};
setTimeout(checkInitialization, checkInterval);
});
};
const initializeFileSystemAsync = async (inputFsType) => {
const fsType = inputFsType == null ? "IDBFS" : inputFsType;
const EmscriptenFileSystemManager = library.EmscriptenFileSystemManager;
if (EmscriptenFileSystemManager.isInitialized()) {
return;
}
EmscriptenFileSystemManager.initializeFileSystem(fsType);
return await waitForFileSystemInitalized();
};
let promiseCalling = void 0;
const loadHnswlib = async (inputFsType) => {
if (promiseCalling) {
return promiseCalling;
}
promiseCalling = new Promise((resolve2, reject) => {
(async () => {
try {
if (typeof hnswlib !== "undefined" && hnswlib !== null) {
const lib = hnswlib();
if (lib != null)
return lib;
}
if (!library) {
const temp = await Promise.resolve().then(() => require('./hnswlib-17c95812.js'));
const factoryFunc = temp.default;
library = await factoryFunc();
await initializeFileSystemAsync(inputFsType);
return resolve2(library);
}
return resolve2(library);
} catch (err) {
console.error("----------------------------------------");
console.error("Error initializing the library:", err);
reject(err);
}
})();
promiseCalling = void 0;
});
return promiseCalling;
};
exports.IDBFS_STORE_NAME = IDBFS_STORE_NAME;
exports.defaultParams = defaultParams;
exports.hnswParamsForAda = hnswParamsForAda;
exports.loadHnswlib = loadHnswlib;
exports.syncFileSystem = syncFileSystem;
exports.waitForFileSystemInitalized = waitForFileSystemInitalized;
exports.waitForFileSystemSynced = waitForFileSystemSynced;
//# sourceMappingURL=hnswlib.js.map
;