@wiscale/velesdb-wasm 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,207 @@
1
+ # VelesDB WASM
2
+
3
+ [![npm](https://img.shields.io/npm/v/velesdb-wasm)](https://www.npmjs.com/package/velesdb-wasm)
4
+ [![License](https://img.shields.io/badge/license-ELv2-blue)](https://github.com/cyberlife-coder/VelesDB/blob/main/LICENSE)
5
+
6
+ WebAssembly build of [VelesDB](https://github.com/cyberlife-coder/VelesDB) - vector search in the browser.
7
+
8
+ ## Features
9
+
10
+ - **In-browser vector search** - No server required
11
+ - **SIMD optimized** - Uses WASM SIMD128 for fast distance calculations
12
+ - **Multiple metrics** - Cosine, Euclidean, Dot Product
13
+ - **Lightweight** - Minimal bundle size
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install velesdb-wasm
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ```javascript
24
+ import init, { VectorStore } from 'velesdb-wasm';
25
+
26
+ async function main() {
27
+ // Initialize WASM module
28
+ await init();
29
+
30
+ // Create a vector store (768 dimensions, cosine similarity)
31
+ const store = new VectorStore(768, 'cosine');
32
+
33
+ // Insert vectors (use BigInt for IDs)
34
+ store.insert(1n, new Float32Array([0.1, 0.2, ...]));
35
+ store.insert(2n, new Float32Array([0.3, 0.4, ...]));
36
+
37
+ // Search for similar vectors
38
+ const query = new Float32Array([0.15, 0.25, ...]);
39
+ const results = store.search(query, 5); // Top 5 results
40
+
41
+ // Results: [[id, score], [id, score], ...]
42
+ console.log(results);
43
+ }
44
+
45
+ main();
46
+ ```
47
+
48
+ ### High-Performance Bulk Insert
49
+
50
+ For optimal performance when inserting many vectors:
51
+
52
+ ```javascript
53
+ // Pre-allocate capacity (avoids repeated memory allocations)
54
+ const store = VectorStore.with_capacity(768, 'cosine', 100000);
55
+
56
+ // Batch insert (much faster than individual inserts)
57
+ const batch = [
58
+ [1n, [0.1, 0.2, ...]],
59
+ [2n, [0.3, 0.4, ...]],
60
+ // ... more vectors
61
+ ];
62
+ store.insert_batch(batch);
63
+
64
+ // Or reserve capacity on existing store
65
+ store.reserve(50000);
66
+ ```
67
+
68
+ ## API
69
+
70
+ ### VectorStore
71
+
72
+ ```typescript
73
+ class VectorStore {
74
+ // Create a new store
75
+ constructor(dimension: number, metric: 'cosine' | 'euclidean' | 'dot');
76
+
77
+ // Create with pre-allocated capacity (performance optimization)
78
+ static with_capacity(dimension: number, metric: string, capacity: number): VectorStore;
79
+
80
+ // Properties
81
+ readonly len: number;
82
+ readonly is_empty: boolean;
83
+ readonly dimension: number;
84
+
85
+ // Methods
86
+ insert(id: bigint, vector: Float32Array): void;
87
+ insert_batch(batch: Array<[bigint, number[]]>): void; // Bulk insert
88
+ search(query: Float32Array, k: number): Array<[bigint, number]>;
89
+ remove(id: bigint): boolean;
90
+ clear(): void;
91
+ reserve(additional: number): void; // Pre-allocate memory
92
+ memory_usage(): number;
93
+ }
94
+ ```
95
+
96
+ ## Distance Metrics
97
+
98
+ | Metric | Description | Best For |
99
+ |--------|-------------|----------|
100
+ | `cosine` | Cosine similarity | Text embeddings (BERT, GPT) |
101
+ | `euclidean` | L2 distance | Image features, spatial data |
102
+ | `dot` | Dot product | Pre-normalized vectors |
103
+
104
+ ## IndexedDB Persistence
105
+
106
+ Save and restore your vector store for offline-first applications with built-in async methods:
107
+
108
+ ```javascript
109
+ import init, { VectorStore } from 'velesdb-wasm';
110
+
111
+ async function main() {
112
+ await init();
113
+
114
+ // Create and populate a store
115
+ const store = new VectorStore(768, 'cosine');
116
+ store.insert(1n, new Float32Array(768).fill(0.1));
117
+ store.insert(2n, new Float32Array(768).fill(0.2));
118
+
119
+ // Save to IndexedDB (single async call)
120
+ await store.save('my-vectors-db');
121
+ console.log('Saved!', store.len, 'vectors');
122
+
123
+ // Later: Load from IndexedDB
124
+ const restored = await VectorStore.load('my-vectors-db');
125
+ console.log('Restored!', restored.len, 'vectors');
126
+
127
+ // Clean up: Delete database
128
+ await VectorStore.delete_database('my-vectors-db');
129
+ }
130
+
131
+ main();
132
+ ```
133
+
134
+ ### Persistence API
135
+
136
+ ```typescript
137
+ class VectorStore {
138
+ // Save to IndexedDB (async)
139
+ save(db_name: string): Promise<void>;
140
+
141
+ // Load from IndexedDB (async, static)
142
+ static load(db_name: string): Promise<VectorStore>;
143
+
144
+ // Delete IndexedDB database (async, static)
145
+ static delete_database(db_name: string): Promise<void>;
146
+
147
+ // Manual binary export/import (for localStorage, file download, etc.)
148
+ export_to_bytes(): Uint8Array;
149
+ static import_from_bytes(bytes: Uint8Array): VectorStore;
150
+ }
151
+ ```
152
+
153
+ ### Binary Format
154
+
155
+ | Field | Size | Description |
156
+ |-------|------|-------------|
157
+ | Magic | 4 bytes | `"VELS"` |
158
+ | Version | 1 byte | Format version (1) |
159
+ | Dimension | 4 bytes | Vector dimension (u32 LE) |
160
+ | Metric | 1 byte | 0=cosine, 1=euclidean, 2=dot |
161
+ | Count | 8 bytes | Number of vectors (u64 LE) |
162
+ | Vectors | variable | id (8B) + data (dim × 4B) each |
163
+
164
+ ### Performance
165
+
166
+ Ultra-fast serialization thanks to contiguous memory layout:
167
+
168
+ | Operation | 10k vectors (768D) | Throughput |
169
+ |-----------|-------------------|------------|
170
+ | Export | ~7 ms | **4479 MB/s** |
171
+ | Import | ~10 ms | **2943 MB/s** |
172
+
173
+ ## Use Cases
174
+
175
+ - **Browser-based RAG** - 100% client-side semantic search
176
+ - **Offline-first apps** - Works without internet, persists to IndexedDB
177
+ - **Privacy-preserving AI** - Data never leaves the browser
178
+ - **Electron/Tauri apps** - Desktop AI without a server
179
+ - **PWA applications** - Full offline support with service workers
180
+
181
+ ## Building from Source
182
+
183
+ ```bash
184
+ # Install wasm-pack
185
+ cargo install wasm-pack
186
+
187
+ # Build for browser
188
+ wasm-pack build --target web
189
+
190
+ # Build for Node.js
191
+ wasm-pack build --target nodejs
192
+ ```
193
+
194
+ ## Performance
195
+
196
+ Typical latencies on modern browsers:
197
+
198
+ | Operation | 768D vectors | 10K vectors |
199
+ |-----------|--------------|-------------|
200
+ | Insert | ~1 µs | ~10 ms |
201
+ | Search | ~50 µs | ~5 ms |
202
+
203
+ ## License
204
+
205
+ Elastic License 2.0 (ELv2)
206
+
207
+ See [LICENSE](https://github.com/cyberlife-coder/VelesDB/blob/main/LICENSE) for details.
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@wiscale/velesdb-wasm",
3
+ "type": "module",
4
+ "description": "VelesDB for WebAssembly - Vector search in the browser",
5
+ "version": "0.5.1",
6
+ "license": "Elastic-2.0",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/cyberlife-coder/VelesDB"
10
+ },
11
+ "files": [
12
+ "velesdb_wasm_bg.wasm",
13
+ "velesdb_wasm.js",
14
+ "velesdb_wasm.d.ts"
15
+ ],
16
+ "main": "velesdb_wasm.js",
17
+ "types": "velesdb_wasm.d.ts",
18
+ "sideEffects": [
19
+ "./snippets/*"
20
+ ]
21
+ }
@@ -0,0 +1,264 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export class VectorStore {
5
+ free(): void;
6
+ [Symbol.dispose](): void;
7
+ /**
8
+ * Inserts multiple vectors in a single batch operation.
9
+ *
10
+ * This is significantly faster than calling `insert()` multiple times
11
+ * because it pre-allocates memory and reduces per-call overhead.
12
+ *
13
+ * # Arguments
14
+ *
15
+ * * `batch` - JavaScript array of `[id, Float32Array]` pairs
16
+ *
17
+ * # Errors
18
+ *
19
+ * Returns an error if any vector dimension doesn't match store dimension.
20
+ */
21
+ insert_batch(batch: any): void;
22
+ /**
23
+ * Returns memory usage estimate in bytes.
24
+ */
25
+ memory_usage(): number;
26
+ /**
27
+ * Creates a new vector store with pre-allocated capacity.
28
+ *
29
+ * This is more efficient when you know the approximate number of vectors
30
+ * you'll be inserting, as it avoids repeated memory allocations.
31
+ *
32
+ * # Arguments
33
+ *
34
+ * * `dimension` - Vector dimension
35
+ * * `metric` - Distance metric: "cosine", "euclidean", or "dot"
36
+ * * `capacity` - Number of vectors to pre-allocate space for
37
+ *
38
+ * # Errors
39
+ *
40
+ * Returns an error if the metric is not recognized.
41
+ */
42
+ static with_capacity(dimension: number, metric: string, capacity: number): VectorStore;
43
+ /**
44
+ * Deletes the `IndexedDB` database.
45
+ *
46
+ * Use this to clear all persisted data.
47
+ *
48
+ * # Arguments
49
+ *
50
+ * * `db_name` - Name of the `IndexedDB` database to delete
51
+ *
52
+ * # Errors
53
+ *
54
+ * Returns an error if the deletion fails.
55
+ */
56
+ static delete_database(db_name: string): Promise<void>;
57
+ /**
58
+ * Exports the vector store to a binary format.
59
+ *
60
+ * The binary format contains:
61
+ * - Header: dimension (u32), metric (u8), count (u64)
62
+ * - For each vector: id (u64), data (f32 array)
63
+ *
64
+ * Use this to persist data to `IndexedDB` or `localStorage`.
65
+ *
66
+ * # Errors
67
+ *
68
+ * This function currently does not return errors but uses `Result`
69
+ * for future extensibility.
70
+ *
71
+ * # Performance
72
+ *
73
+ * Perf: Pre-allocates exact buffer size to avoid reallocations.
74
+ * Throughput: ~1600 MB/s on 10k vectors (768D)
75
+ */
76
+ export_to_bytes(): Uint8Array;
77
+ /**
78
+ * Imports a vector store from binary format.
79
+ *
80
+ * Use this to restore data from `IndexedDB` or `localStorage`.
81
+ *
82
+ * # Errors
83
+ *
84
+ * Returns an error if:
85
+ * - The data is too short or corrupted
86
+ * - The magic number is invalid
87
+ * - The version is unsupported
88
+ * - The metric byte is invalid
89
+ */
90
+ static import_from_bytes(bytes: Uint8Array): VectorStore;
91
+ /**
92
+ * Creates a new vector store with the specified dimension and distance metric.
93
+ *
94
+ * # Arguments
95
+ *
96
+ * * `dimension` - Vector dimension (e.g., 768 for BERT, 1536 for GPT)
97
+ * * `metric` - Distance metric: "cosine", "euclidean", or "dot"
98
+ *
99
+ * # Errors
100
+ *
101
+ * Returns an error if the metric is not recognized.
102
+ */
103
+ constructor(dimension: number, metric: string);
104
+ /**
105
+ * Loads a vector store from `IndexedDB`.
106
+ *
107
+ * This method restores all vectors from the browser's `IndexedDB`.
108
+ *
109
+ * # Arguments
110
+ *
111
+ * * `db_name` - Name of the `IndexedDB` database
112
+ *
113
+ * # Errors
114
+ *
115
+ * Returns an error if the database doesn't exist or is corrupted.
116
+ *
117
+ * # Example
118
+ *
119
+ * ```javascript
120
+ * const store = await VectorStore.load("my-vectors");
121
+ * console.log(store.len); // Number of restored vectors
122
+ * ```
123
+ */
124
+ static load(db_name: string): Promise<VectorStore>;
125
+ /**
126
+ * Saves the vector store to `IndexedDB`.
127
+ *
128
+ * This method persists all vectors to the browser's `IndexedDB`,
129
+ * enabling offline-first applications.
130
+ *
131
+ * # Arguments
132
+ *
133
+ * * `db_name` - Name of the `IndexedDB` database
134
+ *
135
+ * # Errors
136
+ *
137
+ * Returns an error if `IndexedDB` is not available or the save fails.
138
+ *
139
+ * # Example
140
+ *
141
+ * ```javascript
142
+ * const store = new VectorStore(768, "cosine");
143
+ * store.insert(1n, vector1);
144
+ * await store.save("my-vectors");
145
+ * ```
146
+ */
147
+ save(db_name: string): Promise<void>;
148
+ /**
149
+ * Clears all vectors from the store.
150
+ */
151
+ clear(): void;
152
+ /**
153
+ * Inserts a vector with the given ID.
154
+ *
155
+ * # Arguments
156
+ *
157
+ * * `id` - Unique identifier for the vector
158
+ * * `vector` - `Float32Array` of the vector data
159
+ *
160
+ * # Errors
161
+ *
162
+ * Returns an error if vector dimension doesn't match store dimension.
163
+ */
164
+ insert(id: bigint, vector: Float32Array): void;
165
+ /**
166
+ * Removes a vector by ID.
167
+ */
168
+ remove(id: bigint): boolean;
169
+ /**
170
+ * Searches for the k nearest neighbors to the query vector.
171
+ *
172
+ * # Arguments
173
+ *
174
+ * * `query` - Query vector as `Float32Array`
175
+ * * `k` - Number of results to return
176
+ *
177
+ * # Returns
178
+ *
179
+ * Array of [id, score] pairs sorted by relevance.
180
+ *
181
+ * # Errors
182
+ *
183
+ * Returns an error if query dimension doesn't match store dimension.
184
+ */
185
+ search(query: Float32Array, k: number): any;
186
+ /**
187
+ * Pre-allocates memory for the specified number of additional vectors.
188
+ *
189
+ * Call this before bulk insertions to avoid repeated allocations.
190
+ *
191
+ * # Arguments
192
+ *
193
+ * * `additional` - Number of additional vectors to reserve space for
194
+ */
195
+ reserve(additional: number): void;
196
+ /**
197
+ * Returns the number of vectors in the store.
198
+ */
199
+ readonly len: number;
200
+ /**
201
+ * Returns true if the store is empty.
202
+ */
203
+ readonly is_empty: boolean;
204
+ /**
205
+ * Returns the vector dimension.
206
+ */
207
+ readonly dimension: number;
208
+ }
209
+
210
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
211
+
212
+ export interface InitOutput {
213
+ readonly memory: WebAssembly.Memory;
214
+ readonly __wbg_vectorstore_free: (a: number, b: number) => void;
215
+ readonly vectorstore_clear: (a: number) => void;
216
+ readonly vectorstore_delete_database: (a: number, b: number) => number;
217
+ readonly vectorstore_dimension: (a: number) => number;
218
+ readonly vectorstore_export_to_bytes: (a: number, b: number) => void;
219
+ readonly vectorstore_import_from_bytes: (a: number, b: number, c: number) => void;
220
+ readonly vectorstore_insert: (a: number, b: number, c: bigint, d: number, e: number) => void;
221
+ readonly vectorstore_insert_batch: (a: number, b: number, c: number) => void;
222
+ readonly vectorstore_is_empty: (a: number) => number;
223
+ readonly vectorstore_len: (a: number) => number;
224
+ readonly vectorstore_load: (a: number, b: number) => number;
225
+ readonly vectorstore_memory_usage: (a: number) => number;
226
+ readonly vectorstore_new: (a: number, b: number, c: number, d: number) => void;
227
+ readonly vectorstore_remove: (a: number, b: bigint) => number;
228
+ readonly vectorstore_reserve: (a: number, b: number) => void;
229
+ readonly vectorstore_save: (a: number, b: number, c: number) => number;
230
+ readonly vectorstore_search: (a: number, b: number, c: number, d: number, e: number) => void;
231
+ readonly vectorstore_with_capacity: (a: number, b: number, c: number, d: number, e: number) => void;
232
+ readonly __wasm_bindgen_func_elem_382: (a: number, b: number, c: number) => void;
233
+ readonly __wasm_bindgen_func_elem_381: (a: number, b: number) => void;
234
+ readonly __wasm_bindgen_func_elem_93: (a: number, b: number, c: number) => void;
235
+ readonly __wasm_bindgen_func_elem_92: (a: number, b: number) => void;
236
+ readonly __wasm_bindgen_func_elem_429: (a: number, b: number, c: number, d: number) => void;
237
+ readonly __wbindgen_export: (a: number, b: number) => number;
238
+ readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
239
+ readonly __wbindgen_export3: (a: number) => void;
240
+ readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
241
+ readonly __wbindgen_export4: (a: number, b: number, c: number) => void;
242
+ }
243
+
244
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
245
+
246
+ /**
247
+ * Instantiates the given `module`, which can either be bytes or
248
+ * a precompiled `WebAssembly.Module`.
249
+ *
250
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
251
+ *
252
+ * @returns {InitOutput}
253
+ */
254
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
255
+
256
+ /**
257
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
258
+ * for everything else, calls `WebAssembly.instantiate` directly.
259
+ *
260
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
261
+ *
262
+ * @returns {Promise<InitOutput>}
263
+ */
264
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;